graphify-watch 0.4.1

File watching and auto-rebuild for graphify
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
//! File watching and auto-rebuild for graphify.
//!
//! Uses `notify` + debouncing to watch for file changes and trigger
//! incremental graph rebuilds. Port of Python `watch.py`.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::Duration;

use notify::RecursiveMode;
use notify_debouncer_mini::new_debouncer;
use thiserror::Error;
use tokio::sync::mpsc;
use tracing::{debug, info, warn};

/// Debounce duration before triggering a rebuild.
const DEBOUNCE_DURATION: Duration = Duration::from_secs(3);

/// Default ignore patterns for files that should not trigger rebuilds.
const IGNORE_PATTERNS: &[&str] = &[
    ".git",
    "node_modules",
    "__pycache__",
    ".pyc",
    "target",
    "graphify-out",
    ".DS_Store",
];

/// Errors from the watcher.
#[derive(Debug, Error)]
pub enum WatchError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("notify error: {0}")]
    Notify(#[from] notify::Error),

    #[error("watch setup failed: {0}")]
    Setup(String),

    #[error("rebuild failed: {0}")]
    Rebuild(String),
}

/// Check if a path should be ignored based on common patterns.
fn should_ignore(path: &Path) -> bool {
    let path_str = path.to_string_lossy();
    IGNORE_PATTERNS.iter().any(|p| path_str.contains(p))
}

/// Filter changed paths to only include relevant source files.
fn filter_changes(paths: &[PathBuf]) -> Vec<PathBuf> {
    paths
        .iter()
        .filter(|p| !should_ignore(p))
        .cloned()
        .collect()
}

/// Run the full pipeline: detect -> extract -> build -> cluster -> analyze -> export.
///
/// When `changed_files` is provided, only those files have their cache invalidated
/// before extraction, achieving an incremental rebuild without re-parsing unchanged files.
fn rebuild(
    root: &Path,
    output_dir: &Path,
    changed_files: Option<&[PathBuf]>,
) -> Result<(), WatchError> {
    let cache_dir = output_dir.join("cache");

    // ── Step 0: Invalidate cache for changed files ──
    if let Some(changed) = changed_files {
        for path in changed {
            let _ = graphify_cache::invalidate_cached(path, root, &cache_dir);
        }
        info!(
            "rebuild: invalidated cache for {} changed file(s)",
            changed.len()
        );
    }

    // ── Step 1: Detect files ──
    info!("rebuild: detecting files...");
    let detection = graphify_detect::detect(root);
    info!(
        "rebuild: found {} files (~{} words)",
        detection.total_files, detection.total_words
    );

    // ── Step 2: Extract AST ──
    let code_files: Vec<PathBuf> = detection
        .files
        .get(&graphify_detect::FileType::Code)
        .map(|v| v.iter().map(|f| root.join(f)).collect())
        .unwrap_or_default();

    if code_files.is_empty() {
        info!("rebuild: no code files found, skipping");
        return Ok(());
    }

    info!(
        "rebuild: extracting AST from {} code files...",
        code_files.len()
    );
    let mut ast_result = graphify_core::model::ExtractionResult::default();
    let mut cache_hits = 0usize;
    let mut errors = 0usize;
    for file_path in &code_files {
        if let Some(cached) = graphify_cache::load_cached_from::<
            graphify_core::model::ExtractionResult,
        >(file_path, root, &cache_dir)
        {
            cache_hits += 1;
            ast_result.nodes.extend(cached.nodes);
            ast_result.edges.extend(cached.edges);
            ast_result.hyperedges.extend(cached.hyperedges);
            continue;
        }
        // Extract fresh, catching panics
        match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            graphify_extract::extract(std::slice::from_ref(file_path))
        })) {
            Ok(fresh) => {
                let _ = graphify_cache::save_cached_to(file_path, &fresh, root, &cache_dir);
                ast_result.nodes.extend(fresh.nodes);
                ast_result.edges.extend(fresh.edges);
                ast_result.hyperedges.extend(fresh.hyperedges);
            }
            Err(_) => {
                errors += 1;
                warn!("rebuild: extraction panicked for {}", file_path.display());
            }
        }
    }
    if cache_hits > 0 {
        info!(
            "rebuild: cache {} hits, {} extracted fresh",
            cache_hits,
            code_files.len() - cache_hits
        );
    }
    if errors > 0 {
        warn!("rebuild: {} file(s) had extraction errors", errors);
    }
    info!(
        "rebuild: Pass 1 (AST): {} nodes, {} edges",
        ast_result.nodes.len(),
        ast_result.edges.len()
    );

    let extractions = vec![ast_result];

    // ── Step 3: Build graph ──
    info!("rebuild: building graph...");
    let graph = graphify_build::build(&extractions)
        .map_err(|e| WatchError::Rebuild(format!("build failed: {e}")))?;
    info!(
        "rebuild: graph has {} nodes, {} edges",
        graph.node_count(),
        graph.edge_count()
    );

    // ── Step 4: Cluster ──
    info!("rebuild: detecting communities...");
    let communities = graphify_cluster::cluster(&graph);
    let cohesion = graphify_cluster::score_all(&graph, &communities);

    let community_labels: HashMap<usize, String> = communities
        .iter()
        .map(|(cid, nodes)| {
            let label = nodes
                .first()
                .and_then(|id| graph.get_node(id))
                .map(|n| n.label.clone())
                .unwrap_or_else(|| format!("Community {}", cid));
            (*cid, label)
        })
        .collect();
    info!("rebuild: {} communities detected", communities.len());

    // ── Step 5: Analyze ──
    info!("rebuild: analyzing...");
    let god_list = graphify_analyze::god_nodes(&graph, 10);
    let surprise_list = graphify_analyze::surprising_connections(&graph, &communities, 5);
    let questions = graphify_analyze::suggest_questions(&graph, &communities, &community_labels, 7);

    // ── Step 6: Export all formats ──
    std::fs::create_dir_all(output_dir)
        .map_err(|e| WatchError::Rebuild(format!("create output dir: {e}")))?;

    let _ = graphify_export::export_json(&graph, output_dir);
    let _ = graphify_export::export_html(&graph, &communities, &community_labels, output_dir, None);
    let _ = graphify_export::export_graphml(&graph, output_dir);
    let _ = graphify_export::export_cypher(&graph, output_dir);
    let _ = graphify_export::export_svg(&graph, &communities, output_dir);
    let _ = graphify_export::export_wiki(&graph, &communities, &community_labels, output_dir);

    // Report
    let detection_json = serde_json::json!({
        "total_files": detection.total_files,
        "total_words": detection.total_words,
        "warning": detection.warning,
    });
    let god_json: Vec<serde_json::Value> = god_list
        .iter()
        .map(|g| serde_json::json!({"label": g.label, "edges": g.degree}))
        .collect();
    let surprise_json: Vec<serde_json::Value> = surprise_list
        .iter()
        .map(|s| serde_json::to_value(s).unwrap_or_default())
        .collect();
    let question_json: Vec<serde_json::Value> = questions
        .iter()
        .map(|q| serde_json::to_value(q).unwrap_or_default())
        .collect();
    let token_cost: HashMap<String, usize> =
        HashMap::from([("input".to_string(), 0), ("output".to_string(), 0)]);

    let root_str = root.to_string_lossy();
    let report = graphify_export::generate_report(
        &graph,
        &communities,
        &cohesion,
        &community_labels,
        &god_json,
        &surprise_json,
        &detection_json,
        &token_cost,
        &root_str,
        Some(&question_json),
    );
    let report_path = output_dir.join("GRAPH_REPORT.md");
    let _ = std::fs::write(&report_path, &report);

    // Save manifest
    let manifest_path = output_dir.join(".graphify_manifest.json");
    let manifest = graphify_detect::Manifest {
        files: detection
            .files
            .iter()
            .flat_map(|(ft, paths)| paths.iter().map(move |p| (p.clone(), *ft)))
            .collect(),
    };
    let _ = graphify_detect::save_manifest(&manifest_path, &manifest);

    info!("rebuild: done");
    Ok(())
}

/// Watch `root` for file changes and trigger rebuilds into `output_dir`.
///
/// This is an async loop that runs until cancelled. On each batch of
/// debounced file changes, it logs the changed paths and invokes an
/// incremental rebuild (only changed files have their cache invalidated).
///
/// # Arguments
/// * `root` - Directory to watch recursively.
/// * `output_dir` - Where to write rebuild output.
pub async fn watch_directory(root: &Path, output_dir: &Path) -> Result<(), WatchError> {
    let (tx, mut rx) = mpsc::channel::<Vec<PathBuf>>(100);

    let mut debouncer = new_debouncer(
        DEBOUNCE_DURATION,
        move |res: Result<Vec<notify_debouncer_mini::DebouncedEvent>, notify::Error>| match res {
            Ok(events) => {
                let paths: Vec<PathBuf> = events.into_iter().map(|e| e.path).collect();
                if let Err(e) = tx.blocking_send(paths) {
                    warn!("Failed to send watch events: {}", e);
                }
            }
            Err(e) => {
                warn!("Watch error: {}", e);
            }
        },
    )
    .map_err(|e| WatchError::Setup(e.to_string()))?;

    debouncer.watcher().watch(root, RecursiveMode::Recursive)?;

    info!(
        "Watching {} for changes (output: {})",
        root.display(),
        output_dir.display()
    );
    println!("Watching {} for changes...", root.display());

    // Run initial build (full)
    println!("Running initial build...");
    match rebuild(root, output_dir, None) {
        Ok(()) => println!("Initial build complete."),
        Err(e) => eprintln!("Initial build failed: {e}"),
    }

    while let Some(changed_paths) = rx.recv().await {
        let relevant = filter_changes(&changed_paths);

        if relevant.is_empty() {
            debug!("Ignoring changes in excluded paths");
            continue;
        }

        info!("{} file(s) changed, triggering rebuild...", relevant.len());
        println!(
            "Files changed ({}), triggering incremental rebuild...",
            relevant.len()
        );

        for p in &relevant {
            debug!("  changed: {}", p.display());
        }

        match rebuild(root, output_dir, Some(&relevant)) {
            Ok(()) => {
                println!("Rebuild complete.");
            }
            Err(e) => {
                eprintln!("Rebuild failed: {e}");
            }
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_should_ignore_git() {
        assert!(should_ignore(Path::new("/repo/.git/objects/abc")));
        assert!(should_ignore(Path::new("/repo/node_modules/foo.js")));
        assert!(should_ignore(Path::new("/repo/__pycache__/mod.pyc")));
        assert!(should_ignore(Path::new("/repo/target/debug/build")));
        assert!(should_ignore(Path::new("/repo/graphify-out/graph.json")));
    }

    #[test]
    fn test_should_not_ignore_source() {
        assert!(!should_ignore(Path::new("/repo/src/main.rs")));
        assert!(!should_ignore(Path::new("/repo/lib/utils.py")));
        assert!(!should_ignore(Path::new("/repo/README.md")));
    }

    #[test]
    fn test_filter_changes() {
        let paths = vec![
            PathBuf::from("/repo/src/main.rs"),
            PathBuf::from("/repo/.git/HEAD"),
            PathBuf::from("/repo/src/lib.rs"),
            PathBuf::from("/repo/node_modules/foo/index.js"),
        ];
        let filtered = filter_changes(&paths);
        assert_eq!(filtered.len(), 2);
        assert!(filtered.contains(&PathBuf::from("/repo/src/main.rs")));
        assert!(filtered.contains(&PathBuf::from("/repo/src/lib.rs")));
    }

    #[test]
    fn test_filter_changes_all_ignored() {
        let paths = vec![
            PathBuf::from("/repo/.git/HEAD"),
            PathBuf::from("/repo/.DS_Store"),
        ];
        let filtered = filter_changes(&paths);
        assert!(filtered.is_empty());
    }

    #[test]
    fn test_filter_changes_empty() {
        let filtered = filter_changes(&[]);
        assert!(filtered.is_empty());
    }

    #[test]
    fn test_rebuild_empty_dir() {
        let dir = tempfile::tempdir().unwrap();
        let output = tempfile::tempdir().unwrap();
        // Should succeed with empty directory (no code files)
        let result = rebuild(dir.path(), output.path(), None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_rebuild_with_code_files() {
        let dir = tempfile::tempdir().unwrap();
        let output = tempfile::tempdir().unwrap();
        let src = dir.path().join("src");
        std::fs::create_dir_all(&src).unwrap();
        std::fs::write(
            src.join("main.rs"),
            "fn main() { hello(); }\nfn hello() { println!(\"hi\"); }\n",
        )
        .unwrap();
        std::fs::write(
            src.join("lib.rs"),
            "pub fn add(a: i32, b: i32) -> i32 { a + b }\n",
        )
        .unwrap();

        let result = rebuild(dir.path(), output.path(), None);
        assert!(result.is_ok());

        // Check that output files were created
        assert!(output.path().join("graph.json").exists());
        assert!(output.path().join("graph.html").exists());
        assert!(output.path().join("GRAPH_REPORT.md").exists());
    }

    #[test]
    fn test_incremental_rebuild() {
        let dir = tempfile::tempdir().unwrap();
        let output = tempfile::tempdir().unwrap();
        let src = dir.path().join("src");
        std::fs::create_dir_all(&src).unwrap();
        std::fs::write(
            src.join("main.rs"),
            "fn main() { hello(); }\nfn hello() { println!(\"hi\"); }\n",
        )
        .unwrap();

        // Initial full build
        let result = rebuild(dir.path(), output.path(), None);
        assert!(result.is_ok());

        // Incremental rebuild with changed files
        let changed = vec![src.join("main.rs")];
        let result = rebuild(dir.path(), output.path(), Some(&changed));
        assert!(result.is_ok());
    }
}