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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Markdown report generator (tasks 6.1-6.4).
//!
//! Produces per-sample Markdown reports and an aggregate batch report. The
//! severity model (Rule 5: deterministic, not model-decided):
//!
//! - **critical**: query result set differs OR a comparable type is entirely
//!   missing from one side (count = 0 on one side, > 0 on the other).
//! - **major**: comparable type count delta > 10% (relative to the larger
//!   count; if both are 0, delta = 0).
//! - **minor**: comparable type count delta ≤ 10%.

use anyhow::{Context, Result};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use crate::codenexus_stats::CodeNexusStats;
use crate::gitnexus_client::GitnexusStats;
use crate::query_compare::QueryDiff;
use crate::type_map::{CanonicalType, TypeMap};

/// Per-severity counter.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SeverityCounts {
    pub critical: usize,
    pub major: usize,
    pub minor: usize,
}

/// Aggregate per-sample summary used by `generate_aggregate_report`.
#[derive(Debug, Clone)]
pub struct SampleSummary {
    pub name: String,
    pub language: String,
    pub overall_pass: bool,
    pub severities: SeverityCounts,
}

/// Threshold (fraction) above which a count delta becomes `major`.
const MAJOR_DELTA_THRESHOLD: f64 = 0.10;

/// Classify the delta between two counts into a severity tier.
///
/// Returns `"critical"` if one side is zero and the other is non-zero,
/// `"major"` if the relative delta exceeds 10%, otherwise `"minor"`.
fn classify_delta(codenexus: u64, gitnexus: u64) -> &'static str {
    if codenexus == 0 && gitnexus > 0 {
        return "critical";
    }
    if gitnexus == 0 && codenexus > 0 {
        return "critical";
    }
    let larger = codenexus.max(gitnexus) as f64;
    let smaller = codenexus.min(gitnexus) as f64;
    if larger == 0.0 {
        return "minor";
    }
    let rel_delta = (larger - smaller) / larger;
    if rel_delta > MAJOR_DELTA_THRESHOLD {
        "major"
    } else {
        "minor"
    }
}

/// Format a percentage string with 2 decimals, e.g. `"3.45%"`.
fn pct(codenexus: u64, gitnexus: u64) -> String {
    let larger = codenexus.max(gitnexus) as f64;
    let smaller = codenexus.min(gitnexus) as f64;
    if larger == 0.0 {
        return "0.00%".to_string();
    }
    format!("{:.2}%", (larger - smaller) / larger * 100.0)
}

/// Task 6.1: Generate a per-sample Markdown report.
///
/// The report contains:
/// 1. Summary header (OVERALL PASS/FAIL + per-severity counts)
/// 2. Node type comparison table (comparable types only)
/// 3. Edge type comparison table (comparable types only)
/// 4. Query comparison table
/// 5. Unmapped types list (codenexus_only / gitnexus_only / analysis_artifact)
pub fn generate_report(
    name: &str,
    codenexus_stats: &CodeNexusStats,
    gitnexus_stats: &GitnexusStats,
    query_diffs: &[(String, QueryDiff)],
    type_map: &TypeMap,
) -> String {
    let mut out = String::new();
    out.push_str(&format!("# Verification Report: {name}\n\n"));

    // --- Severity tally ---
    let mut severities = SeverityCounts::default();

    // --- Node comparison (comparable types) ---
    out.push_str("## Node Type Comparison (Comparable Types)\n\n");
    out.push_str("| Type | CodeNexus | gitnexus | Delta | Severity |\n");
    out.push_str("|------|-----------|----------|-------|----------|\n");

    // Collect all canonical comparable node types from the type map.
    let mut comparable_nodes: BTreeMap<String, ()> = BTreeMap::new();
    for raw in codenexus_stats.node_counts_by_type.keys() {
        if let CanonicalType::Comparable(canon) = type_map.normalize_codenexus_node(raw) {
            comparable_nodes.insert(canon, ());
        }
    }
    for raw in gitnexus_stats.node_counts_by_label.keys() {
        if let CanonicalType::Comparable(canon) = type_map.normalize_gitnexus_node(raw) {
            comparable_nodes.insert(canon, ());
        }
    }

    for canon in comparable_nodes.keys() {
        let cn = sum_counts_canonical_node(codenexus_stats, type_map, canon);
        let gn = sum_counts_canonical_node_gitnexus(gitnexus_stats, type_map, canon);
        let severity = classify_delta(cn, gn);
        increment_severity(&mut severities, severity);
        out.push_str(&format!(
            "| {canon} | {cn} | {gn} | {} | {severity} |\n",
            pct(cn, gn),
        ));
    }
    out.push('\n');

    // --- Edge comparison (comparable types) ---
    out.push_str("## Edge Type Comparison (Comparable Types)\n\n");
    out.push_str("| Type | CodeNexus | gitnexus | Delta | Severity |\n");
    out.push_str("|------|-----------|----------|-------|----------|\n");

    let mut comparable_edges: BTreeMap<String, ()> = BTreeMap::new();
    for raw in codenexus_stats.edge_counts_by_type.keys() {
        if let CanonicalType::Comparable(canon) = type_map.normalize_codenexus_edge(raw) {
            comparable_edges.insert(canon, ());
        }
    }
    for raw in gitnexus_stats.edge_counts_by_type.keys() {
        if let CanonicalType::Comparable(canon) = type_map.normalize_gitnexus_edge(raw) {
            comparable_edges.insert(canon, ());
        }
    }

    for canon in comparable_edges.keys() {
        let cn = sum_counts_canonical_edge(codenexus_stats, type_map, canon);
        let gn = sum_counts_canonical_edge_gitnexus(gitnexus_stats, type_map, canon);
        let severity = classify_delta(cn, gn);
        increment_severity(&mut severities, severity);
        out.push_str(&format!(
            "| {canon} | {cn} | {gn} | {} | {severity} |\n",
            pct(cn, gn),
        ));
    }
    out.push('\n');

    // --- Query comparison ---
    out.push_str("## Query Comparison\n\n");
    out.push_str("| Query | Result | Missing in CodeNexus | Missing in gitnexus |\n");
    out.push_str("|-------|--------|----------------------|---------------------|\n");
    for (query_name, diff) in query_diffs {
        match diff {
            QueryDiff::Match { count } => {
                out.push_str(&format!("| {query_name} | MATCH ({count}) | 0 | 0 |\n"));
            }
            QueryDiff::CriticalDiff {
                missing_in_codenexus,
                missing_in_gitnexus,
            } => {
                severities.critical += 1;
                out.push_str(&format!(
                    "| {query_name} | CRITICAL | {} | {} |\n",
                    missing_in_codenexus.len(),
                    missing_in_gitnexus.len(),
                ));
            }
        }
    }
    out.push('\n');

    // --- Unmapped types (informational) ---
    out.push_str("## Unmapped Types (Informational)\n\n");
    out.push_str("### CodeNexus-only (not indexed by gitnexus)\n\n");
    out.push_str("| Type | Count |\n|------|-------|\n");
    for (raw, count) in &codenexus_stats.node_counts_by_type {
        if matches!(
            type_map.normalize_codenexus_node(raw),
            CanonicalType::CodenexusOnly(_)
        ) {
            out.push_str(&format!("| {raw} | {count} |\n"));
        }
    }
    out.push('\n');
    out.push_str("### gitnexus-only (not indexed by CodeNexus)\n\n");
    out.push_str("| Type | Count |\n|------|-------|\n");
    for (raw, count) in &gitnexus_stats.node_counts_by_label {
        if matches!(
            type_map.normalize_gitnexus_node(raw),
            CanonicalType::GitnexusOnly(_)
        ) {
            out.push_str(&format!("| {raw} | {count} |\n"));
        }
    }
    out.push('\n');
    out.push_str("### Analysis Artifacts (excluded from comparison)\n\n");
    out.push_str("| Type | CodeNexus | gitnexus |\n|------|-----------|----------|\n");
    let mut analysis_artifacts: BTreeMap<String, ()> = BTreeMap::new();
    for raw in codenexus_stats.node_counts_by_type.keys() {
        if matches!(
            type_map.normalize_codenexus_node(raw),
            CanonicalType::AnalysisArtifact(_)
        ) {
            analysis_artifacts.insert(raw.clone(), ());
        }
    }
    for raw in gitnexus_stats.node_counts_by_label.keys() {
        if matches!(
            type_map.normalize_gitnexus_node(raw),
            CanonicalType::AnalysisArtifact(_)
        ) {
            analysis_artifacts.insert(raw.clone(), ());
        }
    }
    for raw in analysis_artifacts.keys() {
        let cn = codenexus_stats
            .node_counts_by_type
            .get(raw)
            .copied()
            .unwrap_or(0);
        let gn = gitnexus_stats
            .node_counts_by_label
            .get(raw)
            .copied()
            .unwrap_or(0);
        out.push_str(&format!("| {raw} | {cn} | {gn} |\n"));
    }
    out.push('\n');

    // --- Summary header (prepend after we know the totals) ---
    let overall_pass = severities.critical == 0;
    let mut header = String::new();
    header.push_str("## Summary\n\n");
    header.push_str("| Metric | Value |\n|--------|-------|\n");
    header.push_str(&format!(
        "| Overall | {} |\n",
        if overall_pass { "PASS" } else { "FAIL" },
    ));
    header.push_str(&format!(
        "| Critical discrepancies | {} |\n",
        severities.critical
    ));
    header.push_str(&format!("| Major discrepancies | {} |\n", severities.major));
    header.push_str(&format!("| Minor discrepancies | {} |\n", severities.minor));
    header.push_str(&format!(
        "| CodeNexus file_count (by lang) | {} |\n",
        codenexus_stats
            .file_counts_by_language
            .iter()
            .map(|(k, v)| format!("{k}={v}"))
            .collect::<Vec<_>>()
            .join(", "),
    ));
    header.push_str(&format!(
        "| gitnexus file_count | {} |\n",
        gitnexus_stats.file_count
    ));
    header.push('\n');

    // Splice header after the title.
    let title_end = out.find("\n\n").unwrap_or(out.len());
    let mut full = String::new();
    full.push_str(&out[..title_end + 2]);
    full.push_str(&header);
    full.push_str(&out[title_end + 2..]);
    full
}

/// Task 6.3: Write a per-sample Markdown report to `results/<name>.report.md`.
pub fn write_report(name: &str, markdown: &str) -> Result<PathBuf> {
    let dir = Path::new("tools/verification/results");
    std::fs::create_dir_all(dir)?;
    let path = dir.join(format!("{name}.report.md"));
    std::fs::write(&path, markdown)
        .with_context(|| format!("failed to write {}", path.display()))?;
    Ok(path)
}

/// Task 6.4: Generate an aggregate batch report ranking samples by severity.
///
/// Writes to `tools/verification/results/_aggregate.report.md`.
pub fn generate_aggregate_report(summaries: &[SampleSummary]) -> String {
    let mut out = String::new();
    out.push_str("# Aggregate Verification Report\n\n");
    out.push_str("## Per-Sample Summary\n\n");
    out.push_str("| Sample | Language | Overall | Critical | Major | Minor |\n");
    out.push_str("|--------|----------|---------|----------|-------|-------|\n");
    for s in summaries {
        out.push_str(&format!(
            "| {} | {} | {} | {} | {} | {} |\n",
            s.name,
            s.language,
            if s.overall_pass { "PASS" } else { "FAIL" },
            s.severities.critical,
            s.severities.major,
            s.severities.minor,
        ));
    }
    out.push('\n');

    // Ranking (by critical desc, then major desc, then minor desc).
    let mut ranked: Vec<&SampleSummary> = summaries.iter().collect();
    ranked.sort_by(|a, b| {
        b.severities
            .critical
            .cmp(&a.severities.critical)
            .then_with(|| b.severities.major.cmp(&a.severities.major))
            .then_with(|| b.severities.minor.cmp(&a.severities.minor))
    });
    out.push_str("## Ranking (by critical count desc)\n\n");
    for (i, s) in ranked.iter().enumerate() {
        out.push_str(&format!(
            "{}. **{}** — critical={}, major={}, minor={}, overall={}\n",
            i + 1,
            s.name,
            s.severities.critical,
            s.severities.major,
            s.severities.minor,
            if s.overall_pass { "PASS" } else { "FAIL" },
        ));
    }

    let total_pass = summaries.iter().filter(|s| s.overall_pass).count();
    let total_fail = summaries.len() - total_pass;
    out.push_str(&format!(
        "\n**Total: {} pass, {} fail (of {} samples)**\n",
        total_pass,
        total_fail,
        summaries.len(),
    ));
    out
}

/// Write the aggregate report to `results/_aggregate.report.md`.
pub fn write_aggregate_report(markdown: &str) -> Result<PathBuf> {
    let dir = Path::new("tools/verification/results");
    std::fs::create_dir_all(dir)?;
    let path = dir.join("_aggregate.report.md");
    std::fs::write(&path, markdown)
        .with_context(|| format!("failed to write {}", path.display()))?;
    Ok(path)
}

// -- helpers --

fn increment_severity(severities: &mut SeverityCounts, tier: &str) {
    match tier {
        "critical" => severities.critical += 1,
        "major" => severities.major += 1,
        "minor" => severities.minor += 1,
        _ => {}
    }
}

/// Sum CodeNexus node counts that map to a given canonical type.
fn sum_counts_canonical_node(stats: &CodeNexusStats, type_map: &TypeMap, canon: &str) -> u64 {
    stats
        .node_counts_by_type
        .iter()
        .filter(|(raw, _)| {
            matches!(type_map.normalize_codenexus_node(raw), CanonicalType::Comparable(c) if c == canon)
        })
        .map(|(_, v)| v)
        .sum()
}

/// Sum gitnexus node counts that map to a given canonical type.
fn sum_counts_canonical_node_gitnexus(
    stats: &GitnexusStats,
    type_map: &TypeMap,
    canon: &str,
) -> u64 {
    stats
        .node_counts_by_label
        .iter()
        .filter(|(raw, _)| {
            matches!(type_map.normalize_gitnexus_node(raw), CanonicalType::Comparable(c) if c == canon)
        })
        .map(|(_, v)| v)
        .sum()
}

/// Sum CodeNexus edge counts that map to a given canonical type.
fn sum_counts_canonical_edge(stats: &CodeNexusStats, type_map: &TypeMap, canon: &str) -> u64 {
    stats
        .edge_counts_by_type
        .iter()
        .filter(|(raw, _)| {
            matches!(type_map.normalize_codenexus_edge(raw), CanonicalType::Comparable(c) if c == canon)
        })
        .map(|(_, v)| v)
        .sum()
}

/// Sum gitnexus edge counts that map to a given canonical type.
fn sum_counts_canonical_edge_gitnexus(
    stats: &GitnexusStats,
    type_map: &TypeMap,
    canon: &str,
) -> u64 {
    stats
        .edge_counts_by_type
        .iter()
        .filter(|(raw, _)| {
            matches!(type_map.normalize_gitnexus_edge(raw), CanonicalType::Comparable(c) if c == canon)
        })
        .map(|(_, v)| v)
        .sum()
}

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

    fn load_type_map() -> TypeMap {
        let path = PathBuf::from("tools/verification/type_map.json");
        TypeMap::load(&path).expect("type_map.json must load")
    }

    fn sample_codenexus_stats() -> CodeNexusStats {
        let mut node_counts = BTreeMap::new();
        node_counts.insert("Function".to_string(), 100);
        node_counts.insert("Class".to_string(), 0);
        node_counts.insert("GlobalVar".to_string(), 45); // codenexus_only
        node_counts.insert("Community".to_string(), 10); // analysis_artifact
        let mut edge_counts = BTreeMap::new();
        edge_counts.insert("CALLS".to_string(), 500);
        edge_counts.insert("FFI_CALLS".to_string(), 50); // codenexus_only
        CodeNexusStats {
            name: "demo".to_string(),
            node_counts_by_type: node_counts,
            edge_counts_by_type: edge_counts,
            file_counts_by_language: {
                let mut m = BTreeMap::new();
                m.insert("rust".to_string(), 50);
                m
            },
        }
    }

    fn sample_gitnexus_stats() -> GitnexusStats {
        let mut node_counts = BTreeMap::new();
        node_counts.insert("Function".to_string(), 102); // 2% delta → minor
        node_counts.insert("Class".to_string(), 10); // codenexus 0 → critical
        node_counts.insert("CodeElement".to_string(), 200); // gitnexus_only
        node_counts.insert("Community".to_string(), 10); // analysis_artifact
        let mut edge_counts = BTreeMap::new();
        edge_counts.insert("CALLS".to_string(), 600); // 16.7% delta → major
        GitnexusStats {
            name: "demo".to_string(),
            node_counts_by_label: node_counts,
            edge_counts_by_type: edge_counts,
            file_count: 50,
        }
    }

    #[test]
    fn classify_delta_critical_when_one_side_zero() {
        assert_eq!(classify_delta(0, 10), "critical");
        assert_eq!(classify_delta(10, 0), "critical");
    }

    #[test]
    fn classify_delta_minor_when_both_zero() {
        assert_eq!(classify_delta(0, 0), "minor");
    }

    #[test]
    fn classify_delta_major_when_above_threshold() {
        // 100 vs 120 → 16.67% delta → major
        assert_eq!(classify_delta(100, 120), "major");
        assert_eq!(classify_delta(120, 100), "major");
    }

    #[test]
    fn classify_delta_minor_when_at_or_below_threshold() {
        // 100 vs 105 → 5% delta → minor
        assert_eq!(classify_delta(100, 105), "minor");
        // 100 vs 110 → exactly 10% → minor (≤ 10%)
        assert_eq!(classify_delta(100, 110), "minor");
    }

    #[test]
    fn generate_report_includes_summary_header() {
        let tm = load_type_map();
        let cn = sample_codenexus_stats();
        let gn = sample_gitnexus_stats();
        let diffs = vec![(
            "callers_of_function".to_string(),
            QueryDiff::Match { count: 50 },
        )];
        let report = generate_report("demo", &cn, &gn, &diffs, &tm);
        assert!(report.contains("# Verification Report: demo"));
        assert!(report.contains("## Summary"));
        assert!(report.contains("Overall"));
        // Class is comparable, codenexus=0, gitnexus=10 → critical
        assert!(report.contains("| class | 0 | 10 | 100.00% | critical |"));
        // Function: 100 vs 102 → 2% delta → minor
        assert!(report.contains("| function | 100 | 102 | 1.96% | minor |"));
        // CALLS: 500 vs 600 → 16.67% → major
        assert!(report.contains("| calls | 500 | 600 | 16.67% | major |"));
        // Query match row
        assert!(report.contains("| callers_of_function | MATCH (50) | 0 | 0 |"));
        // Unmapped types section
        assert!(report.contains("### CodeNexus-only"));
        assert!(report.contains("| GlobalVar | 45 |"));
        assert!(report.contains("### gitnexus-only"));
        assert!(report.contains("| CodeElement | 200 |"));
        assert!(report.contains("### Analysis Artifacts"));
    }

    #[test]
    fn generate_report_marks_fail_when_critical_present() {
        let tm = load_type_map();
        let cn = sample_codenexus_stats();
        let gn = sample_gitnexus_stats();
        let diffs = vec![(
            "extends_chain".to_string(),
            QueryDiff::CriticalDiff {
                missing_in_codenexus: vec![("foo".to_string(), "a.rs".to_string())],
                missing_in_gitnexus: vec![],
            },
        )];
        let report = generate_report("demo", &cn, &gn, &diffs, &tm);
        assert!(report.contains("| Overall | FAIL |"));
        assert!(report.contains("| extends_chain | CRITICAL | 1 | 0 |"));
    }

    #[test]
    fn generate_aggregate_report_ranks_by_critical_desc() {
        let summaries = vec![
            SampleSummary {
                name: "alpha".to_string(),
                language: "rust".to_string(),
                overall_pass: true,
                severities: SeverityCounts {
                    critical: 0,
                    major: 2,
                    minor: 5,
                },
            },
            SampleSummary {
                name: "bravo".to_string(),
                language: "python".to_string(),
                overall_pass: false,
                severities: SeverityCounts {
                    critical: 3,
                    major: 1,
                    minor: 0,
                },
            },
            SampleSummary {
                name: "charlie".to_string(),
                language: "c".to_string(),
                overall_pass: false,
                severities: SeverityCounts {
                    critical: 1,
                    major: 0,
                    minor: 1,
                },
            },
        ];
        let report = generate_aggregate_report(&summaries);
        assert!(report.contains("# Aggregate Verification Report"));
        assert!(report.contains("## Ranking (by critical count desc)"));
        // bravo (3 critical) must rank before charlie (1 critical) before alpha (0 critical)
        let bravo_idx = report.find("1. **bravo**").unwrap_or(usize::MAX);
        let charlie_idx = report.find("3. **alpha**").unwrap_or(usize::MAX);
        let alpha_idx = report.find("2. **charlie**").unwrap_or(usize::MAX);
        assert!(bravo_idx < alpha_idx);
        assert!(alpha_idx < charlie_idx);
        assert!(report.contains("**Total: 1 pass, 2 fail (of 3 samples)**"));
    }
}