kdo-graph 0.1.0-alpha.2

Internal crate for kdo — workspace dependency graph, discovery, traversal, content hashing. Not intended for direct use; API may change without notice.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Workspace discovery and graph construction.

use crate::hash::content_hash_dir;
use indexmap::IndexMap;
use kdo_core::{DepKind, Dependency, KdoError, Project};
use kdo_resolver::{manifest_filenames, parse_manifest};
use petgraph::algo::is_cyclic_directed;
use petgraph::graph::{DiGraph, NodeIndex};
use petgraph::visit::Bfs;
use petgraph::Direction;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use tracing::{debug, info, warn};

/// The workspace dependency graph.
///
/// Wraps a `petgraph::DiGraph` where nodes are [`Project`]s and edges are [`DepKind`]s.
#[derive(Debug)]
pub struct WorkspaceGraph {
    /// Root directory of the workspace.
    pub root: PathBuf,
    /// Directed graph of projects.
    pub graph: DiGraph<Project, DepKind>,
    /// Map from project name to node index.
    name_to_idx: IndexMap<String, NodeIndex>,
}

/// Serializable project summary for JSON output.
#[derive(Debug, Serialize, Deserialize)]
pub struct ProjectSummary {
    /// Project name.
    pub name: String,
    /// Detected language.
    pub language: String,
    /// Context summary if available.
    pub summary: Option<String>,
    /// Number of direct dependencies.
    pub dep_count: usize,
}

/// Serializable graph output.
#[derive(Debug, Serialize, Deserialize)]
pub struct GraphOutput {
    /// All projects in the workspace.
    pub projects: Vec<ProjectSummary>,
    /// Edges as (source_name, target_name, kind).
    pub edges: Vec<(String, String, String)>,
}

impl WorkspaceGraph {
    /// Discover all projects under `root` by walking the filesystem.
    ///
    /// Uses the `ignore` crate to respect `.gitignore` and `.kdoignore`.
    /// Parses manifests in parallel with rayon.
    pub fn discover(root: &Path) -> Result<Self, KdoError> {
        let root = root
            .canonicalize()
            .map_err(|_| KdoError::ManifestNotFound(root.to_path_buf()))?;

        info!(root = %root.display(), "discovering workspace");

        let manifest_names = manifest_filenames();
        let walker = ignore::WalkBuilder::new(&root)
            .hidden(true)
            .git_ignore(true)
            .add_custom_ignore_filename(".kdoignore")
            .filter_entry(|entry| {
                let name = entry.file_name().to_string_lossy();
                // Skip node_modules, target, .git, etc.
                !matches!(
                    name.as_ref(),
                    "node_modules" | "target" | ".git" | ".kdo" | "dist" | "build" | "__pycache__"
                )
            })
            .build();

        // Collect manifest paths
        let manifest_paths: Vec<PathBuf> = walker
            .filter_map(|entry| entry.ok())
            .filter(|entry| {
                entry.file_type().map(|ft| ft.is_file()).unwrap_or(false)
                    && entry
                        .file_name()
                        .to_str()
                        .map(|name| manifest_names.contains(&name))
                        .unwrap_or(false)
            })
            .map(|entry| entry.into_path())
            .collect();

        // Apply kdo.toml workspace.projects / workspace.exclude globs if present.
        let manifest_paths = apply_workspace_globs(&root, manifest_paths);

        debug!(count = manifest_paths.len(), "found manifest files");

        // Filter out manifests in directories that have a more specific manifest.
        // E.g., if a dir has both Anchor.toml and Cargo.toml, Anchor takes priority.
        let filtered = filter_manifests(&manifest_paths);

        // Parse manifests in parallel
        let results: Vec<Result<(Project, Vec<Dependency>), KdoError>> = filtered
            .par_iter()
            .map(|path| parse_manifest(path, &root))
            .collect();

        let mut graph = DiGraph::new();
        let mut name_to_idx = IndexMap::new();
        let mut all_deps: Vec<(String, Vec<Dependency>)> = Vec::new();

        for result in results {
            match result {
                Ok((mut project, deps)) => {
                    // Compute content hash
                    project.content_hash = content_hash_dir(&project.path);

                    let name = project.name.clone();
                    let idx = graph.add_node(project);
                    name_to_idx.insert(name.clone(), idx);
                    all_deps.push((name, deps));
                }
                Err(KdoError::ManifestNotFound(_)) => {
                    // Skip workspace-root manifests
                    continue;
                }
                Err(e) => {
                    warn!(error = %e, "skipping unparseable manifest");
                }
            }
        }

        // Wire up edges for local/workspace dependencies
        for (source_name, deps) in &all_deps {
            if let Some(&source_idx) = name_to_idx.get(source_name) {
                for dep in deps {
                    if let Some(&target_idx) = name_to_idx.get(&dep.name) {
                        graph.add_edge(source_idx, target_idx, dep.kind.clone());
                    }
                }
            }
        }

        info!(
            projects = name_to_idx.len(),
            edges = graph.edge_count(),
            "workspace graph built"
        );

        Ok(Self {
            root,
            graph,
            name_to_idx,
        })
    }

    /// Get a project by name.
    pub fn get_project(&self, name: &str) -> Result<&Project, KdoError> {
        let idx = self
            .name_to_idx
            .get(name)
            .ok_or_else(|| KdoError::ProjectNotFound(name.to_string()))?;
        Ok(&self.graph[*idx])
    }

    /// Get all projects.
    pub fn projects(&self) -> Vec<&Project> {
        self.graph.node_weights().collect()
    }

    /// Dependency closure: all transitive dependencies of a project (DFS on outgoing edges).
    pub fn dependency_closure(&self, name: &str) -> Result<Vec<&Project>, KdoError> {
        let start = self
            .name_to_idx
            .get(name)
            .ok_or_else(|| KdoError::ProjectNotFound(name.to_string()))?;

        let mut visited = Vec::new();
        let mut stack = vec![*start];
        let mut seen = std::collections::HashSet::new();
        seen.insert(*start);

        while let Some(node) = stack.pop() {
            // Skip the start node itself
            if node != *start {
                visited.push(&self.graph[node]);
            }
            for neighbor in self.graph.neighbors_directed(node, Direction::Outgoing) {
                if seen.insert(neighbor) {
                    stack.push(neighbor);
                }
            }
        }

        Ok(visited)
    }

    /// Affected set: all projects that transitively depend on the given project.
    /// Uses BFS on the reversed graph (incoming edges).
    pub fn affected_set(&self, name: &str) -> Result<Vec<&Project>, KdoError> {
        let start = self
            .name_to_idx
            .get(name)
            .ok_or_else(|| KdoError::ProjectNotFound(name.to_string()))?;

        // BFS on reversed edges
        let reversed = petgraph::visit::Reversed(&self.graph);
        let mut bfs = Bfs::new(&reversed, *start);
        let mut affected = Vec::new();

        while let Some(node) = bfs.next(&reversed) {
            if node != *start {
                affected.push(&self.graph[node]);
            }
        }

        Ok(affected)
    }

    /// Detect cycles in the dependency graph.
    pub fn detect_cycles(&self) -> Result<(), KdoError> {
        if is_cyclic_directed(&self.graph) {
            // Find a cycle for the error message
            let cycle_desc = find_cycle_description(&self.graph);
            return Err(KdoError::CircularDependency(cycle_desc));
        }
        Ok(())
    }

    /// Get a serializable summary of all projects.
    pub fn project_summaries(&self) -> Vec<ProjectSummary> {
        self.graph
            .node_indices()
            .map(|idx| {
                let project = &self.graph[idx];
                let dep_count = self
                    .graph
                    .neighbors_directed(idx, Direction::Outgoing)
                    .count();
                ProjectSummary {
                    name: project.name.clone(),
                    language: project.language.to_string(),
                    summary: project.context_summary.clone(),
                    dep_count,
                }
            })
            .collect()
    }

    /// Get the full graph as a serializable structure.
    pub fn to_graph_output(&self) -> GraphOutput {
        let projects = self.project_summaries();
        let edges = self
            .graph
            .edge_indices()
            .filter_map(|edge_idx| {
                let (source, target) = self.graph.edge_endpoints(edge_idx)?;
                let kind = &self.graph[edge_idx];
                Some((
                    self.graph[source].name.clone(),
                    self.graph[target].name.clone(),
                    kind.to_string(),
                ))
            })
            .collect();

        GraphOutput { projects, edges }
    }

    /// Generate a DOT representation of the graph.
    pub fn to_dot(&self) -> String {
        let mut dot = String::from("digraph workspace {\n  rankdir=LR;\n");
        for idx in self.graph.node_indices() {
            let project = &self.graph[idx];
            dot.push_str(&format!(
                "  \"{}\" [label=\"{}\\n({})\"];\n",
                project.name, project.name, project.language
            ));
        }
        for edge_idx in self.graph.edge_indices() {
            if let Some((source, target)) = self.graph.edge_endpoints(edge_idx) {
                let kind = &self.graph[edge_idx];
                dot.push_str(&format!(
                    "  \"{}\" -> \"{}\" [label=\"{}\"];\n",
                    self.graph[source].name, self.graph[target].name, kind
                ));
            }
        }
        dot.push_str("}\n");
        dot
    }

    /// Generate a text representation of the graph.
    pub fn to_text(&self) -> String {
        let mut out = String::new();
        for idx in self.graph.node_indices() {
            let project = &self.graph[idx];
            out.push_str(&format!("{} ({})\n", project.name, project.language));
            for neighbor in self.graph.neighbors_directed(idx, Direction::Outgoing) {
                let dep = &self.graph[neighbor];
                out.push_str(&format!("  -> {}\n", dep.name));
            }
        }
        out
    }

    /// Dependency closure as JSON string.
    pub fn dependency_closure_json(&self, project: &str) -> Result<String, KdoError> {
        let deps = self.dependency_closure(project)?;
        let names: Vec<&str> = deps.iter().map(|p| p.name.as_str()).collect();
        serde_json::to_string_pretty(&names).map_err(|e| KdoError::ParseError {
            path: std::path::PathBuf::from("<json>"),
            source: e.into(),
        })
    }

    /// Affected set as JSON string.
    pub fn affected_set_json(&self, project: &str) -> Result<String, KdoError> {
        let affected = self.affected_set(project)?;
        let names: Vec<&str> = affected.iter().map(|p| p.name.as_str()).collect();
        serde_json::to_string_pretty(&names).map_err(|e| KdoError::ParseError {
            path: std::path::PathBuf::from("<json>"),
            source: e.into(),
        })
    }

    /// Return all projects in topological order (dependencies before dependents).
    ///
    /// Falls back to insertion order if the graph has cycles (which `detect_cycles` should
    /// have caught earlier).
    pub fn topological_order(&self) -> Vec<&Project> {
        match petgraph::algo::toposort(&self.graph, None) {
            Ok(indices) => indices.iter().map(|idx| &self.graph[*idx]).collect(),
            Err(_) => self.projects(),
        }
    }

    /// Find projects affected by changes since a git ref.
    ///
    /// Shells out to `git diff --name-only` to find changed files,
    /// then maps them to owning projects.
    pub fn affected_since_ref(&self, base_ref: &str) -> Result<Vec<String>, KdoError> {
        let output = std::process::Command::new("git")
            .args(["diff", "--name-only", &format!("{base_ref}...HEAD")])
            .current_dir(&self.root)
            .output()?;

        if !output.status.success() {
            // Fallback: try without ...HEAD (for uncommitted changes)
            let output = std::process::Command::new("git")
                .args(["diff", "--name-only", base_ref])
                .current_dir(&self.root)
                .output()?;

            if !output.status.success() {
                return Ok(Vec::new());
            }
            return self.map_paths_to_projects(&String::from_utf8_lossy(&output.stdout));
        }

        self.map_paths_to_projects(&String::from_utf8_lossy(&output.stdout))
    }

    /// Map changed file paths to their owning project names.
    fn map_paths_to_projects(&self, diff_output: &str) -> Result<Vec<String>, KdoError> {
        let mut affected = std::collections::HashSet::new();
        for line in diff_output.lines() {
            let changed_path = self.root.join(line.trim());
            for project in self.projects() {
                if changed_path.starts_with(&project.path) {
                    affected.insert(project.name.clone());
                }
            }
        }
        let mut result: Vec<String> = affected.into_iter().collect();
        result.sort();
        Ok(result)
    }
}

/// Apply `workspace.projects` and `workspace.exclude` glob filters from `kdo.toml`.
/// If no `kdo.toml` is present or no globs are set, returns the input unchanged.
fn apply_workspace_globs(root: &Path, paths: Vec<PathBuf>) -> Vec<PathBuf> {
    use globset::{Glob, GlobSetBuilder};

    let kdo_toml = root.join("kdo.toml");
    if !kdo_toml.exists() {
        return paths;
    }
    let Ok(config) = kdo_core::WorkspaceConfig::load(&kdo_toml) else {
        return paths;
    };

    let build_set = |patterns: &[String]| -> Option<globset::GlobSet> {
        if patterns.is_empty() {
            return None;
        }
        let mut builder = GlobSetBuilder::new();
        for p in patterns {
            if let Ok(g) = Glob::new(p) {
                builder.add(g);
            }
        }
        builder.build().ok()
    };

    let include = build_set(&config.workspace.project_globs);
    let exclude = build_set(&config.workspace.exclude);

    paths
        .into_iter()
        .filter(|path| {
            let Ok(rel) = path.strip_prefix(root) else {
                return true;
            };
            let parent_rel = rel.parent().unwrap_or(Path::new(""));

            if let Some(ex) = &exclude {
                if ex.is_match(parent_rel) {
                    return false;
                }
            }
            if let Some(inc) = &include {
                return inc.is_match(parent_rel);
            }
            true
        })
        .collect()
}

/// Filter manifests to prefer more specific ones (Anchor.toml > Cargo.toml in same dir).
fn filter_manifests(paths: &[PathBuf]) -> Vec<PathBuf> {
    let mut by_dir: IndexMap<PathBuf, Vec<PathBuf>> = IndexMap::new();
    for path in paths {
        let dir = path.parent().unwrap_or(Path::new(".")).to_path_buf();
        by_dir.entry(dir).or_default().push(path.clone());
    }

    let mut filtered = Vec::new();
    for (_dir, manifests) in &by_dir {
        // Priority: Anchor.toml > Cargo.toml > package.json > pyproject.toml
        let has_anchor = manifests
            .iter()
            .any(|p| p.file_name().map(|f| f == "Anchor.toml").unwrap_or(false));
        for manifest in manifests {
            let filename = manifest
                .file_name()
                .map(|f| f.to_string_lossy().to_string())
                .unwrap_or_default();
            // If Anchor.toml exists, skip Cargo.toml in the same dir
            if has_anchor && filename == "Cargo.toml" {
                continue;
            }
            filtered.push(manifest.clone());
        }
    }

    filtered
}

/// Find a cycle and return a human-readable description.
fn find_cycle_description(graph: &DiGraph<Project, DepKind>) -> String {
    // Use petgraph's toposort which returns the node involved in a cycle on error
    match petgraph::algo::toposort(graph, None) {
        Ok(_) => "unknown cycle".to_string(),
        Err(cycle) => {
            let node = &graph[cycle.node_id()];
            format!("cycle involving '{}'", node.name)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_filter_manifests_anchor_priority() {
        let paths = vec![
            PathBuf::from("/project/Anchor.toml"),
            PathBuf::from("/project/Cargo.toml"),
            PathBuf::from("/project/sub/Cargo.toml"),
        ];
        let filtered = filter_manifests(&paths);
        assert!(filtered.contains(&PathBuf::from("/project/Anchor.toml")));
        assert!(!filtered.contains(&PathBuf::from("/project/Cargo.toml")));
        assert!(filtered.contains(&PathBuf::from("/project/sub/Cargo.toml")));
    }
}