codenexus 0.3.3

A queryable code knowledge graph tool built on LadybugDB and tree-sitter
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
//! codenexus-verify — Cross-validation harness for CodeNexus parsing.
//!
//! Indexes a target repo with CodeNexus, fetches the gitnexus reference index
//! for the same repo, compares node/edge counts and query result sets, and
//! emits a Markdown diff report. See
//! `openspec/changes/cross-validate-parsing-with-gitnexus/` for the full spec.

mod codenexus_stats;
mod gitnexus_client;
mod query_compare;
mod report;
mod type_map;

use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use std::path::{Path, PathBuf};
use std::process::Command as StdCommand;

use query_compare::{QueryDiff, Side};
use type_map::TypeMap;

/// Cross-validate CodeNexus parsing against gitnexus reference indexes.
#[derive(Parser, Debug)]
#[command(name = "codenexus-verify", version, about)]
struct Cli {
    /// Path to the gitnexus binary. If omitted, searches PATH.
    #[arg(long, global = true)]
    gitnexus_binary: Option<PathBuf>,

    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand, Debug)]
enum Command {
    /// Index a single repo with CodeNexus, fetch gitnexus reference, compare, report.
    Single {
        /// Path to the target repository root.
        #[arg(long)]
        repo: PathBuf,

        /// Project name (used as the CodeNexus index key and gitnexus repo name).
        #[arg(long)]
        name: String,

        /// Primary language of the repo (c / rust / fortran / python / typescript).
        #[arg(long)]
        language: String,

        /// Skip CodeNexus indexing if results/<name>.codenexus.json already exists.
        #[arg(long)]
        resume: bool,
    },

    /// Run the full index-compare-report flow over every entry in a corpus JSON.
    Batch {
        /// Path to samples.json.
        #[arg(long, default_value = "tools/verification/samples.json")]
        corpus: PathBuf,

        /// Skip CodeNexus indexing for samples that already have a results JSON.
        #[arg(long)]
        resume: bool,

        /// Run only the named sample (repeatable).
        #[arg(long = "only", action = clap::ArgAction::Append)]
        only: Vec<String>,
    },

    /// Clone/checkout sample repos listed in samples.json.
    FetchSamples {
        /// Path to samples.json.
        #[arg(long, default_value = "tools/verification/samples.json")]
        corpus: PathBuf,
    },
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    let gitnexus_binary = cli.gitnexus_binary.as_deref();
    match cli.command {
        Command::Single {
            repo,
            name,
            language,
            resume,
        } => run_single_orchestrator(&repo, &name, &language, resume, gitnexus_binary),
        Command::Batch {
            corpus,
            resume,
            only,
        } => run_batch(&corpus, resume, &only, gitnexus_binary),
        Command::FetchSamples { corpus } => run_fetch_samples(&corpus),
    }
}

/// Default DB path used by `codenexus index` (matches CLI args.rs default).
const DEFAULT_DB: &str = "./codenexus.lbug";

/// Default type map path.
const TYPE_MAP_PATH: &str = "tools/verification/type_map.json";

/// Default queries directory.
const QUERIES_DIR: &str = "tools/verification/queries";

/// Task 8.1: Full single-sample orchestrator.
///
/// Wires together: CodeNexus index+extract → gitnexus reference fetch →
/// query comparison → report generation. Produces three output files:
/// - `results/<name>.codenexus.json`
/// - `results/<name>.gitnexus.json`
/// - `results/<name>.report.md`
fn run_single_orchestrator(
    repo: &Path,
    name: &str,
    language: &str,
    resume: bool,
    gitnexus_binary: Option<&Path>,
) -> Result<()> {
    // 1. CodeNexus side: index + extract + write codenexus.json
    let codenexus_stats = codenexus_stats::run_single(repo, name, language, resume)?;

    // 2. gitnexus side: fetch reference + write gitnexus.json.
    //    When --resume is set and a previously-written reference JSON exists,
    //    load it directly. This bypasses `gitnexus cypher` subprocess calls,
    //    which is necessary when the installed gitnexus binary cannot read
    //    the indexed DB (e.g. storage version mismatch between the DB
    //    producer and the binary in PATH). Falls back to a fresh fetch
    //    when the JSON is absent so the default non-resume flow is unchanged.
    let gitnexus_stats = if resume {
        match gitnexus_client::load_reference(name) {
            Ok(stats) => {
                eprintln!("[resume] loaded gitnexus reference for `{name}` from disk");
                stats
            }
            Err(e) => {
                eprintln!(
                    "[resume] no usable gitnexus reference for `{name}` ({e}); fetching fresh"
                );
                let stats = gitnexus_client::fetch_reference(name, gitnexus_binary)?;
                let _ = gitnexus_client::write_reference(name, &stats)?;
                stats
            }
        }
    } else {
        let stats = gitnexus_client::fetch_reference(name, gitnexus_binary)?;
        let gn_path = gitnexus_client::write_reference(name, &stats)?;
        eprintln!("[ok] wrote {gn_path:?}");
        stats
    };

    // 3. Load type map
    let type_map = TypeMap::load(&PathBuf::from(TYPE_MAP_PATH))?;

    // 4. Run all 8 query comparisons
    let query_diffs = run_query_comparisons(name, gitnexus_binary)?;

    // 5. Generate + write report
    let markdown = report::generate_report(
        name,
        &codenexus_stats,
        &gitnexus_stats,
        &query_diffs,
        &type_map,
    );
    let report_path = report::write_report(name, &markdown)?;
    eprintln!("[ok] wrote {report_path:?}");

    // Print summary
    let overall_pass = query_diffs
        .iter()
        .all(|(_, d)| matches!(d, QueryDiff::Match { .. }));
    eprintln!(
        "[done] {} — overall {}",
        name,
        if overall_pass { "PASS" } else { "FAIL" }
    );
    Ok(())
}

/// Execute all .cql files in the queries directory against both sides and
/// collect the diffs.
fn run_query_comparisons(
    name: &str,
    gitnexus_binary: Option<&Path>,
) -> Result<Vec<(String, QueryDiff)>> {
    let queries_dir = Path::new(QUERIES_DIR);
    let db_path = Path::new(DEFAULT_DB);

    // Look up the CodeNexus project_id by name so CQL queries can filter
    // by `__PID__` to prevent cross-project data contamination (project
    // memory hard constraint).
    let repo = codenexus::storage::repository::Repository::open(db_path)
        .with_context(|| format!("failed to open CodeNexus DB at {}", db_path.display()))?;
    let project_id = codenexus_stats::lookup_project_id(repo.connection(), name)?;
    eprintln!("[query] project '{name}' → id {project_id}");

    let mut cql_files: Vec<PathBuf> = std::fs::read_dir(queries_dir)
        .with_context(|| format!("failed to read queries dir {}", queries_dir.display()))?
        .filter_map(|e| e.ok())
        .map(|e| e.path())
        .filter(|p| p.extension().is_some_and(|ext| ext == "cql"))
        .collect();
    cql_files.sort();

    let mut diffs = Vec::with_capacity(cql_files.len());
    for cql_path in &cql_files {
        let query_name = cql_path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("unknown")
            .to_string();
        eprintln!("[query] {query_name}");

        let content = std::fs::read_to_string(cql_path)
            .with_context(|| format!("failed to read {}", cql_path.display()))?;

        let cn_cql = query_compare::extract_query_for_side(&content, Side::Codenexus)?;
        let gn_cql = query_compare::extract_query_for_side(&content, Side::Gitnexus)?;

        let cn_results =
            query_compare::execute_codenexus_query(db_path, &cn_cql, Some(&project_id))
                .with_context(|| format!("CodeNexus query failed for {query_name}"))?;
        let gn_results = query_compare::execute_gitnexus_query(name, &gn_cql, gitnexus_binary)
            .with_context(|| format!("gitnexus query failed for {query_name}"))?;

        let diff = query_compare::compare_query_results(&cn_results, &gn_results);
        eprintln!(
            "  CodeNexus={}, gitnexus={}, diff={}",
            cn_results.len(),
            gn_results.len(),
            match &diff {
                QueryDiff::Match { count } => format!("MATCH({count})"),
                QueryDiff::CriticalDiff {
                    missing_in_codenexus,
                    missing_in_gitnexus,
                } => format!(
                    "CRITICAL(missing_cn={}, missing_gn={})",
                    missing_in_codenexus.len(),
                    missing_in_gitnexus.len()
                ),
            }
        );
        diffs.push((query_name, diff));
    }
    Ok(diffs)
}

/// Batch dispatcher (task 8.7).
fn run_batch(
    corpus: &PathBuf,
    resume: bool,
    only: &[String],
    gitnexus_binary: Option<&Path>,
) -> Result<()> {
    let content = std::fs::read_to_string(corpus)
        .with_context(|| format!("failed to read corpus {}", corpus.display()))?;
    let corpus: serde_json::Value = serde_json::from_str(&content)
        .with_context(|| format!("failed to parse corpus JSON {}", corpus.display()))?;

    let samples = corpus
        .get("samples")
        .and_then(|v| v.as_array())
        .ok_or_else(|| anyhow::anyhow!("corpus JSON missing `samples` array"))?;

    let mut summaries = Vec::new();

    for sample in samples {
        let name = sample
            .get("name")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("sample missing `name`"))?;
        let language = sample
            .get("language")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("sample missing `language`"))?;
        let repo_path = sample
            .get("repo_path")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("sample missing `repo_path`"))?;

        if !only.is_empty() && !only.iter().any(|o| o == name) {
            eprintln!("[skip] {name} (not in --only filter)");
            continue;
        }

        eprintln!("\n=== batch: {name} ({language}) ===");
        match run_single_orchestrator(
            Path::new(repo_path),
            name,
            language,
            resume,
            gitnexus_binary,
        ) {
            Ok(()) => {
                // Read the generated report to extract severity counts.
                let report_path =
                    Path::new("tools/verification/results").join(format!("{name}.report.md"));
                let severities = read_severity_counts(&report_path);
                let overall_pass = severities.critical == 0;
                summaries.push(report::SampleSummary {
                    name: name.to_string(),
                    language: language.to_string(),
                    overall_pass,
                    severities,
                });
            }
            Err(e) => {
                eprintln!("[error] {name} failed: {e:#}");
                summaries.push(report::SampleSummary {
                    name: name.to_string(),
                    language: language.to_string(),
                    overall_pass: false,
                    severities: report::SeverityCounts {
                        critical: 1,
                        major: 0,
                        minor: 0,
                    },
                });
            }
        }
    }

    let aggregate_md = report::generate_aggregate_report(&summaries);
    let agg_path = report::write_aggregate_report(&aggregate_md)?;
    eprintln!("\n[done] aggregate report: {agg_path:?}");
    Ok(())
}

/// Read a per-sample report file and extract severity counts from the Summary table.
fn read_severity_counts(report_path: &Path) -> report::SeverityCounts {
    let content = std::fs::read_to_string(report_path).unwrap_or_default();
    let mut critical = 0usize;
    let mut major = 0usize;
    let mut minor = 0usize;
    for line in content.lines() {
        if line.contains("Critical discrepancies") {
            if let Some(val) = line.split('|').nth(2) {
                critical = val.trim().parse().unwrap_or(0);
            }
        }
        if line.contains("Major discrepancies") {
            if let Some(val) = line.split('|').nth(2) {
                major = val.trim().parse().unwrap_or(0);
            }
        }
        if line.contains("Minor discrepancies") {
            if let Some(val) = line.split('|').nth(2) {
                minor = val.trim().parse().unwrap_or(0);
            }
        }
    }
    report::SeverityCounts {
        critical,
        major,
        minor,
    }
}

/// Fetch-samples subcommand: delegates to the bash script.
fn run_fetch_samples(corpus: &Path) -> Result<()> {
    let script = Path::new("tools/verification/fetch_samples.sh");
    if !script.exists() {
        anyhow::bail!("fetch_samples.sh not found at {}", script.display());
    }
    let status = StdCommand::new("bash")
        .arg(script)
        .arg(corpus)
        .status()
        .context("failed to spawn fetch_samples.sh")?;
    if !status.success() {
        anyhow::bail!("fetch_samples.sh exited with {status}");
    }
    Ok(())
}

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

    #[test]
    fn gitnexus_binary_flag_parses() {
        let cli = Cli::parse_from([
            "codenexus-verify",
            "--gitnexus-binary",
            "/usr/local/bin/gitnexus",
            "single",
            "--repo",
            ".",
            "--name",
            "test",
            "--language",
            "rust",
        ]);
        assert_eq!(
            cli.gitnexus_binary,
            Some(PathBuf::from("/usr/local/bin/gitnexus"))
        );
    }

    #[test]
    fn gitnexus_binary_none_uses_path() {
        let cli = Cli::parse_from([
            "codenexus-verify",
            "single",
            "--repo",
            ".",
            "--name",
            "test",
            "--language",
            "rust",
        ]);
        assert!(cli.gitnexus_binary.is_none());
    }

    // --- read_severity_counts ---

    #[test]
    fn read_severity_counts_parses_report() {
        let report = "| Category | Count |\n| --- | --- |\n| Critical discrepancies | 2 |\n| Major discrepancies | 5 |\n| Minor discrepancies | 10 |";
        let dir = tempfile::TempDir::new().unwrap();
        let report_path = dir.path().join("test.report.md");
        std::fs::write(&report_path, report).unwrap();
        let counts = read_severity_counts(&report_path);
        assert_eq!(counts.critical, 2);
        assert_eq!(counts.major, 5);
        assert_eq!(counts.minor, 10);
    }

    #[test]
    fn read_severity_counts_returns_zeros_for_missing_file() {
        let counts = read_severity_counts(std::path::Path::new("/nonexistent/report.md"));
        assert_eq!(counts.critical, 0);
        assert_eq!(counts.major, 0);
        assert_eq!(counts.minor, 0);
    }

    #[test]
    fn read_severity_counts_returns_zeros_for_empty_file() {
        let dir = tempfile::TempDir::new().unwrap();
        let report_path = dir.path().join("empty.report.md");
        std::fs::write(&report_path, "").unwrap();
        let counts = read_severity_counts(&report_path);
        assert_eq!(counts.critical, 0);
        assert_eq!(counts.major, 0);
        assert_eq!(counts.minor, 0);
    }
}