Skip to main content

work_graph/
lib.rs

1//! Work graph (Implementation Graph) Beat B MVP indexer.
2//!
3//! Indexes what is **Present** in a QualiaDB checkout: git tip, crates, files,
4//! ALL_BOUND / vibe catalog invoke ids, and simple doc-page cites.
5//!
6//! Honesty: this emit is **Present** only. It does not stamp Live or Planned.
7//! No `ImplGraph.*` Host ids are invented.
8
9pub mod emit;
10pub mod extract;
11pub mod git;
12pub mod model;
13pub mod walk;
14
15use std::fs;
16use std::io;
17use std::path::{Path, PathBuf};
18
19use crate::model::{DocCite, FamilyNode, FileNode, Honesty, Project, WorkGraph};
20
21pub const ALL_BOUND_REL: &str = "crates/qualia-core-db/src/poet_host/invoke/ids.rs";
22pub const CATALOG_REL: &str = "crates/vibe/src/catalog/ids.rs";
23pub const DEFAULT_OUT_REL: &str = "target/work-graph";
24
25#[derive(Debug)]
26pub enum IndexError {
27    Io(io::Error),
28    Git(String),
29    Extract(extract::ExtractError),
30    MissingSoT { path: PathBuf, what: &'static str },
31}
32
33impl std::fmt::Display for IndexError {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        match self {
36            IndexError::Io(e) => write!(f, "io: {e}"),
37            IndexError::Git(e) => write!(f, "git: {e}"),
38            IndexError::Extract(e) => write!(f, "extract: {e}"),
39            IndexError::MissingSoT { path, what } => {
40                write!(f, "missing {what} at {}", path.display())
41            }
42        }
43    }
44}
45
46impl std::error::Error for IndexError {}
47
48impl From<io::Error> for IndexError {
49    fn from(value: io::Error) -> Self {
50        IndexError::Io(value)
51    }
52}
53
54impl From<extract::ExtractError> for IndexError {
55    fn from(value: extract::ExtractError) -> Self {
56        IndexError::Extract(value)
57    }
58}
59
60/// Index a QualiaDB (or QualiaDB-shaped) checkout into a presence graph.
61pub fn index_root(root: &Path) -> Result<WorkGraph, IndexError> {
62    let root = root.canonicalize().map_err(|e| {
63        IndexError::Io(io::Error::new(
64            e.kind(),
65            format!("root {}: {e}", root.display()),
66        ))
67    })?;
68
69    let tip = git::read_tip(&root)?;
70    let files = walk::list_project_files(&root)?;
71    let crates = walk::list_crates(&root, &files)?;
72
73    let all_bound_path = root.join(ALL_BOUND_REL);
74    if !all_bound_path.is_file() {
75        return Err(IndexError::MissingSoT {
76            path: all_bound_path,
77            what: "ALL_BOUND SoT (poet_host/invoke/ids.rs)",
78        });
79    }
80    let all_bound_src = fs::read_to_string(&all_bound_path)?;
81    let invoke_ids = extract::extract_all_bound(&all_bound_src)?;
82
83    let catalog_path = root.join(CATALOG_REL);
84    let (catalog_ids, catalog_present) = if catalog_path.is_file() {
85        let src = fs::read_to_string(&catalog_path)?;
86        (extract::extract_catalog_ids(&src)?, true)
87    } else {
88        (Vec::new(), false)
89    };
90
91    let families = extract::families_from_ids(&invoke_ids);
92    let family_names: Vec<String> = families.iter().map(|f| f.name.clone()).collect();
93
94    let doc_pages: Vec<FileNode> = files
95        .iter()
96        .filter(|f| walk::is_doc_page(&f.path))
97        .cloned()
98        .collect();
99
100    let mut doc_cites = Vec::new();
101    for page in &doc_pages {
102        let abs = root.join(&page.path);
103        let Ok(text) = fs::read_to_string(&abs) else {
104            continue;
105        };
106        if text.len() > walk::MAX_DOC_SCRAPE_BYTES {
107            continue;
108        }
109        for invoke in extract::cited_invoke_ids(&text, &invoke_ids) {
110            doc_cites.push(DocCite {
111                about: invoke,
112                doc_path: page.path.clone(),
113                kind: "invoke",
114            });
115        }
116        for family in extract::cited_families(&text, &family_names) {
117            doc_cites.push(DocCite {
118                about: family,
119                doc_path: page.path.clone(),
120                kind: "family",
121            });
122        }
123    }
124
125    Ok(WorkGraph {
126        honesty: Honesty::presence_index(),
127        project: Project {
128            root: root.to_string_lossy().into_owned(),
129            tip_sha: tip.sha,
130            branch: tip.branch,
131            dirty: tip.dirty,
132            indexed_at_unix: unix_now(),
133        },
134        files,
135        crates,
136        invoke_ids,
137        catalog_ids,
138        catalog_present,
139        families,
140        doc_pages: doc_pages.into_iter().map(|f| f.path).collect(),
141        doc_cites,
142        sources: model::Sources {
143            all_bound: ALL_BOUND_REL.to_string(),
144            catalog: if catalog_present {
145                Some(CATALOG_REL.to_string())
146            } else {
147                None
148            },
149        },
150    })
151}
152
153/// `default-out` directory.
154pub fn default_out_dir(root: &Path) -> PathBuf {
155    root.join(DEFAULT_OUT_REL)
156}
157
158fn unix_now() -> u64 {
159    std::time::SystemTime::now()
160        .duration_since(std::time::UNIX_EPOCH)
161        .map(|d| d.as_secs())
162        .unwrap_or(0)
163}
164
165/// Small helper used by tests: family member counts stay aligned with invoke ids.
166pub fn family_count(families: &[FamilyNode], name: &str) -> Option<u32> {
167    families
168        .iter()
169        .find(|f| f.name == name)
170        .map(|f| f.member_count)
171}