scc-cli 0.2.11

System Context Compiler CLI, daemon, MCP server, and Claude Code plugin
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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Atlas recall benchmark integration tests (Wave 8 ยง57): `scc bench atlas`
//! on the fixtures fallback path (no corpus dir) must complete and print
//! the recall table with per-section recall, overall, and the gate verdict.

use std::path::{Path, PathBuf};

mod common;

fn workspace() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .to_path_buf()
}

fn copy_tree(src: &Path, dst: &Path) {
    std::fs::create_dir_all(dst).unwrap();
    for entry in std::fs::read_dir(src).unwrap() {
        let entry = entry.unwrap();
        let name = entry.file_name();
        if name == ".scc" {
            continue; // state, not fixture content
        }
        let from = entry.path();
        let to = dst.join(&name);
        if from.is_dir() {
            std::fs::create_dir_all(&to).unwrap();
            copy_tree(&from, &to);
        } else {
            std::fs::copy(&from, &to).unwrap();
        }
    }
}

// trace:exempt reason=unit-test

/// Turn `dir` into a real git checkout and return its HEAD sha โ€” the value
/// the blind lock pins. The blind guard runs `git rev-parse HEAD` on each
/// clone before scoring, so integration tests need real repos (the fixture
/// copies are plain dirs).
// trace:exempt reason=unit-test
fn git_repo(dir: &Path) -> String {
    for args in [
        &["init", "-q"][..],
        &["add", "-A"][..],
        &[
            "-c", "user.email=bench@test", "-c", "user.name=bench",
            "-c", "commit.gpgsign=false", "commit", "-q", "-m",
            "fixture",
        ][..],
    ] {
        let out = std::process::Command::new("git")
            .args(args)
            .current_dir(dir)
            .output()
            .expect("git command runs");
        assert!(
            out.status.success(),
            "git {args:?} failed: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    }
    let out = std::process::Command::new("git")
        .args(["rev-parse", "HEAD"])
        .current_dir(dir)
        .output()
        .expect("git rev-parse runs");
    String::from_utf8_lossy(&out.stdout).trim().to_string()
}
// trace:v1 id=test.scc.bench.atlas verifies=REQ-SCC-TEST exercises=impl.scc.bench.atlas

#[test]
fn bench_atlas_fixtures_fallback_prints_table() {
    // Hermetic workspace: fixtures/ + benchmarks/tasks.json but NO
    // benchmarks/corpus, so the fixtures fallback path triggers regardless
    // of the state of the real benchmarks/ directory.
    let ws = workspace();
    let tmp = tempfile::TempDir::new().unwrap();
    let root = tmp.path().join("ws");
    std::fs::create_dir_all(root.join("benchmarks")).unwrap();
    for entry in std::fs::read_dir(ws.join("fixtures")).unwrap() {
        let e = entry.unwrap();
        if e.file_type().unwrap().is_dir() {
            copy_tree(&e.path(), &root.join("fixtures").join(e.file_name()));
        }
    }
    std::fs::copy(
        ws.join("benchmarks/tasks.json"),
        root.join("benchmarks/tasks.json"),
    )
    .unwrap();

    let out = std::process::Command::new(common::scc())
        .args(["bench", "atlas"])
        .current_dir(&root)
        .output()
        .expect("scc bench atlas runs");
    assert!(
        out.status.success(),
        "`scc bench atlas` failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(stdout.contains("scc bench atlas"), "{stdout}");
    assert!(stdout.contains("fixtures fallback"), "{stdout}");
    // v2 report: startup-required layer columns + precision/density/tokens
    assert!(stdout.contains("arch"), "{stdout}");
    assert!(stdout.contains("entry"), "{stdout}");
    assert!(stdout.contains("prec"), "{stdout}");
    assert!(stdout.contains("f/1k"), "{stdout}");
    assert!(stdout.contains("mean"), "{stdout}");
    assert!(stdout.contains("gate:"), "{stdout}");
    assert!(stdout.contains("PASS") || stdout.contains("FAIL"), "{stdout}");
}

#[test]
fn bench_atlas_corpus_mode_with_explicit_dirs() {
    // Corpus mode: `--corpus`/`--ground-truth` point at a hermetic temp
    // workspace holding one real fixture repo + its ground-truth doc. The
    // table must print per-section recall, overall, and the gate verdict,
    // with the missing ground-truth item flagged.
    let ws = workspace();
    let tmp = tempfile::TempDir::new().unwrap();
    let corpus = tmp.path().join("corpus");
    let gt = tmp.path().join("ground-truth");
    std::fs::create_dir_all(&corpus).unwrap();
    std::fs::create_dir_all(&gt).unwrap();
    copy_tree(
        &ws.join("fixtures/http-service-python"),
        &corpus.join("http-service-python"),
    );
    std::fs::write(
        gt.join("http-service-python.md"),
        "## components\n- root\n- services\n## entrypoints\n- handle_transcripts\n## flows\n- TranscriptRepository\n## ownership\n- zzz_missing_store\n## contracts\n- GET /api/transcripts\n- GET /api/zzz_missing\n## tests\n- test_transcripts\n",
    )
    .unwrap();

    let out = std::process::Command::new(common::scc())
        .args([
            "bench",
            "atlas",
            "--corpus",
            corpus.to_str().unwrap(),
            "--ground-truth",
            gt.to_str().unwrap(),
        ])
        .current_dir(tmp.path())
        .output()
        .expect("scc bench atlas runs");
    assert!(
        out.status.success(),
        "`scc bench atlas` failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(stdout.contains("scc bench atlas"), "{stdout}");
    assert!(stdout.contains("corpus"), "mode: {stdout}");
    assert!(stdout.contains("http-service-python"), "{stdout}");
    assert!(stdout.contains("overall"), "{stdout}");
    assert!(stdout.contains("mean"), "{stdout}");
    assert!(stdout.contains("gate:"), "{stdout}");
    assert!(stdout.contains("PASS") || stdout.contains("FAIL"), "{stdout}");
    assert!(
        stdout.contains("state_authority:zzz_missing_store"),
        "missed items must be listed: {stdout}"
    );
}

#[test]
fn bench_atlas_holdout_compares_and_writes_results_file() {
    // Holdout protocol: dev corpus + ground truth via --corpus/--ground-truth
    // (temp dirs), holdout corpus at <root>/benchmarks/holdout with ground
    // truth at <root>/benchmarks/holdout-ground-truth. The run must print
    // both reports, the gap summary, and write
    // <root>/benchmarks/results/holdout-v3.txt.
    let ws = workspace();
    let tmp = tempfile::TempDir::new().unwrap();
    let corpus = tmp.path().join("corpus");
    let gt = tmp.path().join("ground-truth");
    std::fs::create_dir_all(&corpus).unwrap();
    std::fs::create_dir_all(&gt).unwrap();
    copy_tree(
        &ws.join("fixtures/http-service-python"),
        &corpus.join("http-service-python"),
    );
    std::fs::write(
        gt.join("http-service-python.md"),
        "## components\n- root\n- services\n## entrypoints\n- handle_transcripts\n## flows\n- TranscriptRepository\n## ownership\n- zzz_missing_store\n## contracts\n- GET /api/transcripts\n- GET /api/zzz_missing\n## tests\n- test_transcripts\n",
    )
    .unwrap();

    // holdout dirs under the workspace root, as the protocol requires
    let holdout = tmp.path().join("benchmarks").join("holdout");
    let holdout_gt = tmp.path().join("benchmarks").join("holdout-ground-truth");
    std::fs::create_dir_all(&holdout).unwrap();
    std::fs::create_dir_all(&holdout_gt).unwrap();
    copy_tree(
        &ws.join("fixtures/http-service-python"),
        &holdout.join("http-service-python"),
    );
    // slightly different ground truth so the gap is not trivially zero
    std::fs::write(
        holdout_gt.join("http-service-python.md"),
        "## components\n- root\n- services\n## entrypoints\n- handle_transcripts\n## flows\n- TranscriptRepository\n## ownership\n- zzz_missing_store\n## contracts\n- GET /api/transcripts\n## tests\n- test_transcripts\n",
    )
    .unwrap();

    let out = std::process::Command::new(common::scc())
        .args([
            "bench",
            "atlas",
            "--holdout",
            "--corpus",
            corpus.to_str().unwrap(),
            "--ground-truth",
            gt.to_str().unwrap(),
        ])
        .current_dir(tmp.path())
        .output()
        .expect("scc bench atlas --holdout runs");
    assert!(
        out.status.success(),
        "`scc bench atlas --holdout` failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(stdout.contains("=== DEVELOPMENT corpus ==="), "{stdout}");
    assert!(stdout.contains("=== VALIDATION corpus ==="), "{stdout}");
    assert!(stdout.contains("=== gap (validation - development) ==="), "{stdout}");
    assert!(stdout.contains("verdict:"), "{stdout}");
    assert!(
        stdout.contains("NO OVERFIT") || stdout.contains("BORDERLINE") || stdout.contains("OVERFIT"),
        "verdict string missing: {stdout}"
    );
    let results = tmp.path().join("benchmarks/results/holdout-v3.txt");
    let text = std::fs::read_to_string(&results)
        .unwrap_or_else(|e| panic!("results file missing: {e}"));
    assert!(text.contains("overall (gate)"), "{text}");
    assert!(text.contains("## verdict:"), "{text}");
    assert!(text.contains("validation repo overall recall"), "{text}");
}

#[test]
fn bench_atlas_holdout_errors_when_holdout_corpus_missing() {
    // The holdout corpus is a required protocol input: a missing dir must
    // fail with a clear error, not silently run an empty holdout.
    let ws = workspace();
    let tmp = tempfile::TempDir::new().unwrap();
    let corpus = tmp.path().join("corpus");
    let gt = tmp.path().join("ground-truth");
    std::fs::create_dir_all(&corpus).unwrap();
    std::fs::create_dir_all(&gt).unwrap();
    copy_tree(
        &ws.join("fixtures/http-service-python"),
        &corpus.join("http-service-python"),
    );
    std::fs::write(
        gt.join("http-service-python.md"),
        "## components\n- root\n## entrypoints\n- handle_transcripts\n## flows\n- TranscriptRepository\n## ownership\n- s\n## contracts\n- GET /api/transcripts\n",
    )
    .unwrap();

    let out = std::process::Command::new(common::scc())
        .args([
            "bench",
            "atlas",
            "--holdout",
            "--corpus",
            corpus.to_str().unwrap(),
            "--ground-truth",
            gt.to_str().unwrap(),
        ])
        .current_dir(tmp.path())
        .output()
        .expect("scc bench atlas --holdout runs");
    assert!(!out.status.success(), "expected failure, got success");
    let stderr = String::from_utf8_lossy(&out.stderr).to_string();
    assert!(
        stderr.contains("holdout corpus dir not found"),
        "clear error expected: {stderr}"
    );
}

// trace:exempt reason=unit-test

#[test]
// trace:exempt reason=unit-test
fn bench_atlas_blind_prints_aggregates_only_and_writes_results_file() {
    // Blind protocol: validation corpus at <root>/benchmarks/holdout and
    // blind corpus at <root>/benchmarks/blind-test (both with ground
    // truth). Output must be aggregates ONLY: no per-repo rows, no
    // filenames, no missed keys; benchmarks/results/blind-v1.txt written.
    let ws = workspace();
    let tmp = tempfile::TempDir::new().unwrap();

    let validation = tmp.path().join("benchmarks").join("holdout");
    let validation_gt = tmp.path().join("benchmarks").join("holdout-ground-truth");
    let blind = tmp.path().join("benchmarks").join("blind-test");
    let blind_gt = tmp.path().join("benchmarks").join("blind-test-ground-truth");
    std::fs::create_dir_all(&validation).unwrap();
    std::fs::create_dir_all(&validation_gt).unwrap();
    std::fs::create_dir_all(&blind).unwrap();
    std::fs::create_dir_all(&blind_gt).unwrap();
    copy_tree(
        &ws.join("fixtures/http-service-python"),
        &validation.join("http-service-python"),
    );
    copy_tree(
        &ws.join("fixtures/http-service-python"),
        &blind.join("http-service-python"),
    );
    // Wave 13: the blind corpus is frozen โ€” make the clone a real git
    // checkout and pin its HEAD in benchmarks/blind-lock.json so the guard
    // (git rev-parse HEAD == lock commit) runs for real.
    let commit = git_repo(&blind.join("http-service-python"));
    std::fs::write(
        tmp.path().join("benchmarks/blind-lock.json"),
        format!(
            r#"{{"blind-test": {{"http-service-python": {{"url": "https://example.invalid/http-service-python", "commit": "{commit}"}}}}}}"#
        ),
    )
    .unwrap();
    std::fs::write(
        validation_gt.join("http-service-python.md"),
        "## components\n- root\n- services\n## entrypoints\n- handle_transcripts\n## flows\n- TranscriptRepository\n## ownership\n- zzz_missing_store\n## contracts\n- GET /api/transcripts\n- GET /api/zzz_missing\n## tests\n- test_transcripts\n",
    )
    .unwrap();
    // slightly different ground truth so the gap is not trivially zero
    std::fs::write(
        blind_gt.join("http-service-python.md"),
        "## components\n- root\n- services\n## entrypoints\n- handle_transcripts\n## flows\n- TranscriptRepository\n## ownership\n- zzz_missing_store\n## contracts\n- GET /api/transcripts\n## tests\n- test_transcripts\n",
    )
    .unwrap();

    let out = std::process::Command::new(common::scc())
        .args(["bench", "atlas", "--blind"])
        .current_dir(tmp.path())
        .output()
        .expect("scc bench atlas --blind runs");
    assert!(
        out.status.success(),
        "`scc bench atlas --blind` failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    assert!(stdout.contains("aggregates only"), "{stdout}");
    assert!(
        stdout.contains("generalization gap (blind - validation)"),
        "{stdout}"
    );
    // Wave 11: the blind manifest hash and transfer ratios are printed
    assert!(
        stdout.contains("blind manifest sha256:"),
        "manifest hash in blind output: {stdout}"
    );
    assert!(
        stdout.contains("blind transfer ratio"),
        "transfer ratio in blind output: {stdout}"
    );
    // aggregates-only: the repo filename must never appear, and no per-repo
    // miss lines may leak
    assert!(
        !stdout.contains("http-service-python"),
        "no repo names in blind output: {stdout}"
    );
    assert!(
        !stdout.contains("missed:"),
        "no missed keys in blind output: {stdout}"
    );
    let results = tmp.path().join("benchmarks/results/blind-v1.txt");
    let text = std::fs::read_to_string(&results)
        .unwrap_or_else(|e| panic!("blind-v1.txt missing: {e}"));
    assert!(text.contains("aggregates only"), "{text}");
    assert!(
        text.contains("blind-test failures are never shown to tuning agents"),
        "{text}"
    );
    assert!(text.contains("overall (gate)"), "{text}");
    assert!(
        text.contains("blind manifest sha256:"),
        "manifest hash in results header: {text}"
    );
    assert!(
        text.contains("blind transfer ratio"),
        "transfer ratio in results file: {text}"
    );
    assert!(!text.contains("http-service-python"), "no repo rows: {text}");
    assert!(!text.contains("missed:"), "no missed keys: {text}");
}

#[test]
fn bench_atlas_blind_refuses_diagnose() {
    // The blind corpus is not diagnosable: diagnosis prints per-repo miss
    // lines, which would leak the blind misses.
    let tmp = tempfile::TempDir::new().unwrap();
    std::fs::create_dir_all(tmp.path().join("benchmarks/holdout")).unwrap();
    std::fs::create_dir_all(tmp.path().join("benchmarks/holdout-ground-truth")).unwrap();
    std::fs::create_dir_all(tmp.path().join("benchmarks/blind-test")).unwrap();
    std::fs::create_dir_all(tmp.path().join("benchmarks/blind-test-ground-truth")).unwrap();

    let out = std::process::Command::new(common::scc())
        .args(["bench", "atlas", "--blind", "--diagnose"])
        .current_dir(tmp.path())
        .output()
        .expect("scc bench atlas --blind --diagnose runs");
    assert!(!out.status.success(), "expected failure, got success");
    let stderr = String::from_utf8_lossy(&out.stderr).to_string();
    assert!(
        stderr.contains("blind corpus is not diagnosable"),
        "clear error expected: {stderr}"
    );
}

// trace:exempt reason=unit-test

#[test]
// trace:exempt reason=unit-test
fn bench_atlas_blind_errors_when_manifest_changes() {
    // Wave 11: `--blind` verifies the sha256 manifest of the frozen blind
    // set (ground-truth keys + clone list) against the previous run before
    // scoring. A bogus previous hash (mismatch) must refuse to score with a
    // clear error โ€” before any indexing.
    let tmp = tempfile::TempDir::new().unwrap();
    let root = tmp.path();
    std::fs::create_dir_all(root.join("benchmarks/holdout")).unwrap();
    std::fs::create_dir_all(root.join("benchmarks/holdout-ground-truth")).unwrap();
    std::fs::create_dir_all(root.join("benchmarks/blind-test")).unwrap();
    std::fs::create_dir_all(root.join("benchmarks/blind-test-ground-truth")).unwrap();
    std::fs::write(
        root.join("benchmarks/blind-test-ground-truth/repo.md"),
        "## architecture\n- root\n",
    )
    .unwrap();
    // Wave 13: the lock is a protocol artifact (missing = hard error) โ€” the
    // empty lock is fine here since the blind-test dir has no clones.
    std::fs::write(
        root.join("benchmarks/blind-lock.json"),
        r#"{"blind-test": {}}"#,
    )
    .unwrap();
    std::fs::create_dir_all(root.join("benchmarks/results")).unwrap();
    std::fs::write(
        root.join("benchmarks/results/blind-v1.txt"),
        "# Blind v1 โ€” validation vs blind (aggregates only)\n\
         blind manifest sha256: 0000000000000000000000000000000000000000000000000000000000000000\n",
    )
    .unwrap();

    let out = std::process::Command::new(common::scc())
        .args(["bench", "atlas", "--blind"])
        .current_dir(root)
        .output()
        .expect("scc bench atlas --blind runs");
    assert!(
        !out.status.success(),
        "expected failure on manifest mismatch, got success"
    );
    let stderr = String::from_utf8_lossy(&out.stderr).to_string();
    assert!(
        stderr.contains("blind-test set changed"),
        "clear error expected: {stderr}"
    );
}

// trace:exempt reason=unit-test

#[test]
// trace:exempt reason=unit-test
fn bench_atlas_blind_errors_when_clone_head_mismatches_lock() {
    // Wave 13: `--blind` verifies each clone's on-disk HEAD against the
    // committed benchmarks/blind-lock.json BEFORE scoring. A clone sitting
    // at a commit other than the pin must fail with a clear error naming
    // the repo, the actual commit, and the pin.
    let tmp = tempfile::TempDir::new().unwrap();
    let root = tmp.path();
    std::fs::create_dir_all(root.join("benchmarks/holdout")).unwrap();
    std::fs::create_dir_all(root.join("benchmarks/holdout-ground-truth")).unwrap();
    let blind = root.join("benchmarks/blind-test");
    let blind_gt = root.join("benchmarks/blind-test-ground-truth");
    std::fs::create_dir_all(&blind).unwrap();
    std::fs::create_dir_all(&blind_gt).unwrap();
    let ws = workspace();
    copy_tree(
        &ws.join("fixtures/http-service-python"),
        &blind.join("http-service-python"),
    );
    let commit = git_repo(&blind.join("http-service-python"));
    // pin a DIFFERENT commit than the clone is actually at
    let wrong = if commit == "a".repeat(40) {
        "b".repeat(40)
    } else {
        "a".repeat(40)
    };
    std::fs::write(
        root.join("benchmarks/blind-lock.json"),
        format!(
            r#"{{"blind-test": {{"http-service-python": {{"url": "https://example.invalid/http-service-python", "commit": "{wrong}"}}}}}}"#
        ),
    )
    .unwrap();

    let out = std::process::Command::new(common::scc())
        .args(["bench", "atlas", "--blind"])
        .current_dir(root)
        .output()
        .expect("scc bench atlas --blind runs");
    assert!(
        !out.status.success(),
        "expected failure on clone HEAD mismatch, got success"
    );
    let stderr = String::from_utf8_lossy(&out.stderr).to_string();
    assert!(
        stderr.contains("blind-test repo http-service-python at")
            && stderr.contains(&format!("lock pins {wrong}")),
        "clear error expected: {stderr}"
    );
}

fn holdout_json(
    dev_overall: f64,
    dev_contracts: f64,
    val_overall: f64,
    val_contracts: f64,
) -> String {
    // Minimal but complete HoldoutComparison JSON (the serde shape the
    // `--holdout --json` path writes); sections default to the overall.
    format!(
        r#"{{
  "dev": {{"mode": "corpus: old", "repos": [], "mean_architecture": {d}, "mean_entrypoints": {d},
           "mean_behavior": {d}, "mean_state_authority": {d}, "mean_contracts": {dc},
           "mean_landmarks": 0.0, "mean_tests": 0.0, "mean_overall": {d},
           "mean_precision": 0.0, "mean_f2": 0.0, "mean_density": 0.0,
           "mean_atlas_tokens": 0.0, "scored": 1, "skipped": 0, "gate_passed": true}},
  "holdout": {{"mode": "validation: x", "repos": [], "mean_architecture": {v}, "mean_entrypoints": {v},
              "mean_behavior": {v}, "mean_state_authority": {v}, "mean_contracts": {vc},
              "mean_landmarks": 0.0, "mean_tests": 0.0, "mean_overall": {v},
              "mean_precision": 0.0, "mean_f2": 0.0, "mean_density": 0.0,
              "mean_atlas_tokens": 0.0, "scored": 1, "skipped": 0, "gate_passed": true}},
  "gap_architecture": 0.0, "gap_entrypoints": 0.0, "gap_behavior": 0.0,
  "gap_state_authority": 0.0, "gap_contracts": 0.0, "gap_overall": 0.0,
  "verdict": "NO_OVERFIT", "results_file": "old"
}}"#,
        d = dev_overall,
        dc = dev_contracts,
        v = val_overall,
        vc = val_contracts
    )
}

#[test]
fn bench_atlas_compare_applies_wave11_gates() {
    // Wave 11: `--compare OLD NEW` loads two saved holdout JSON results and
    // applies the GE gate + per-section regression guard. Generalizing run
    // (dev and validation both improve) passes; a validation section
    // regression beyond the guard fails with exit code 1.
    let tmp = tempfile::TempDir::new().unwrap();
    let old = tmp.path().join("old.json");
    let new_pass = tmp.path().join("new-pass.json");
    let new_fail = tmp.path().join("new-fail.json");
    std::fs::write(&old, holdout_json(0.50, 0.50, 0.50, 0.50)).unwrap();
    // dev 0.50 -> 0.56, validation 0.50 -> 0.53: GE = 0.5, no regressions
    std::fs::write(&new_pass, holdout_json(0.56, 0.56, 0.53, 0.53)).unwrap();
    // validation contracts regress 0.50 -> 0.30 (beyond the 0.05 guard)
    std::fs::write(&new_fail, holdout_json(0.56, 0.56, 0.51, 0.30)).unwrap();

    let scc = common::scc();
    let pass = std::process::Command::new(scc)
        .args([
            "bench",
            "atlas",
            "--compare",
            old.to_str().unwrap(),
            new_pass.to_str().unwrap(),
        ])
        .current_dir(tmp.path())
        .output()
        .expect("scc bench atlas --compare passes");
    assert!(
        pass.status.success(),
        "generalizing run must pass: {}",
        String::from_utf8_lossy(&pass.stderr)
    );
    let stdout = String::from_utf8_lossy(&pass.stdout).to_string();
    assert!(stdout.contains("generalization efficiency"), "{stdout}");
    assert!(stdout.contains("0.500"), "GE 0.5 rendered: {stdout}");
    assert!(stdout.contains("verdict: PASS"), "{stdout}");

    let fail = std::process::Command::new(scc)
        .args([
            "bench",
            "atlas",
            "--compare",
            old.to_str().unwrap(),
            new_fail.to_str().unwrap(),
        ])
        .current_dir(tmp.path())
        .output()
        .expect("scc bench atlas --compare fails");
    assert!(
        !fail.status.success(),
        "regressing run must fail the guard"
    );
    let stderr = String::from_utf8_lossy(&fail.stderr).to_string();
    assert!(
        stderr.contains("generalization gates FAILED"),
        "gate failure surfaced: {stderr}"
    );

    // a missing/unparseable result file is a clear error
    let missing = std::process::Command::new(scc)
        .args([
            "bench",
            "atlas",
            "--compare",
            old.to_str().unwrap(),
            tmp.path().join("nope.json").to_str().unwrap(),
        ])
        .current_dir(tmp.path())
        .output()
        .expect("scc bench atlas --compare missing file");
    assert!(!missing.status.success());
    let stderr = String::from_utf8_lossy(&missing.stderr).to_string();
    assert!(stderr.contains("cannot read"), "{stderr}");
}