sinter-io 0.59.0

Persistent, evidence-backed code graph for coding agents
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
//! Phase 5/6 gate: query, affected, path, impact against a small repo.

use std::path::Path;
use std::process::Command;

use protobuf::Message;
use scip::types::Index;

fn sinter(repo: &Path, args: &[&str]) -> (bool, String) {
    let out = Command::new(env!("CARGO_BIN_EXE_sinter"))
        .args(args)
        .env("HOME", repo)
        .env("USERPROFILE", repo)
        .current_dir(repo)
        .output()
        .expect("run sinter");
    (
        out.status.success(),
        format!(
            "{}{}",
            String::from_utf8_lossy(&out.stdout),
            String::from_utf8_lossy(&out.stderr)
        ),
    )
}

fn git(repo: &Path, args: &[&str]) {
    let ok = Command::new("git")
        .args(args)
        .current_dir(repo)
        .env("GIT_AUTHOR_NAME", "t")
        .env("GIT_AUTHOR_EMAIL", "t@t")
        .env("GIT_COMMITTER_NAME", "t")
        .env("GIT_COMMITTER_EMAIL", "t@t")
        .output()
        .expect("run git")
        .status
        .success();
    assert!(ok, "git {args:?} failed");
}

#[test]
fn no_callers_assertion_distinguishes_violation_from_snapshot_scoped_holding() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "pub fn called() {}\npub fn production_caller() { called(); }\npub fn leaf() {}\n\n#[cfg(test)]\nmod tests {\n    use super::called;\n    #[test]\n    fn test_caller() { called(); }\n}\n",
    )
    .unwrap();
    std::fs::write(
        repo.join("Cargo.toml"),
        "[package]\nname='fixture'\nversion='0.1.0'\nedition='2024'\n",
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);

    // A valid, fresh compiler index makes the empty traversal complete for
    // this indexed snapshot. The graph assertion still stays non-runtime-
    // exhaustive (`coverage.conclusive` is false).
    std::fs::write(
        repo.join("index.scip"),
        Index::default().write_to_bytes().unwrap(),
    )
    .unwrap();
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    let (ok, out) = sinter(repo, &["assert", "no-callers", "called", "--json"]);
    assert!(!ok, "a violated assertion must exit 1: {out}");
    let violated: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(violated["status"], "violated");
    assert_eq!(violated["observed_callers"], 1);
    assert_eq!(violated["ignored_out_of_scope"]["count"], 1);
    assert_eq!(violated["coverage"]["universe"]["mode"], "repository");

    std::fs::write(
        repo.join("workspace.toml"),
        "[workspace]\nname='fixture-workspace'\n[members]\nfixture='.'\n",
    )
    .unwrap();
    let (ok, out) = sinter(repo, &["workspace", "workspace.toml"]);
    assert!(ok, "{out}");
    let (ok, workspace_assertion) = sinter(
        repo,
        &[
            "assert",
            "no-callers",
            "fixture:called",
            "--workspace",
            "workspace.toml",
            "--json",
        ],
    );
    assert!(
        !ok,
        "a violated workspace assertion must exit 1: {workspace_assertion}"
    );
    let workspace_assertion: serde_json::Value =
        serde_json::from_str(&workspace_assertion).unwrap();
    assert_eq!(workspace_assertion["observed_callers"], 1);
    assert_eq!(workspace_assertion["ignored_out_of_scope"]["count"], 1);
    assert_eq!(
        workspace_assertion["ignored_out_of_scope"]["by_scope"]["test"],
        1
    );

    let (ok, out) = sinter(repo, &["assert", "no-callers", "leaf", "--json"]);
    assert!(ok, "{out}");
    let holding: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(holding["status"], "holds_for_indexed_snapshot");
    assert_eq!(holding["observed_callers"], 0);
    assert_eq!(
        holding["coverage"]["completeness"],
        "complete_for_indexed_snapshot"
    );
    assert_eq!(holding["coverage"]["conclusive"], false);
    assert_eq!(holding["assertion"]["runtime_exhaustive"], false);

    std::fs::remove_file(repo.join("index.scip")).unwrap();
    let (ok, not_proven) = sinter(repo, &["assert", "no-callers", "leaf", "--json"]);
    assert!(!ok, "an incomplete snapshot must not pass: {not_proven}");
    let not_proven: serde_json::Value = serde_json::from_str(&not_proven).unwrap();
    assert_eq!(not_proven["status"], "not_proven");
    assert_eq!(not_proven["coverage"]["completeness"], "partial");
}

#[test]
fn managed_citations_follow_symbol_identity_and_bare_locations_do_not_prove_it() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::create_dir_all(repo.join("docs")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "pub fn first() {}\n\npub fn cited() {}\n",
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    let (ok, citation) = sinter(repo, &["cite", "cited"]);
    assert!(ok, "{citation}");
    assert!(citation.contains("sinter-cite:v1"), "{citation}");
    assert!(citation.contains("symbol:"), "{citation}");
    std::fs::write(repo.join("docs/design.md"), &citation).unwrap();

    let (ok, verified) = sinter(repo, &["verify-doc", "docs/design.md", "--json"]);
    assert!(ok, "{verified}");
    let verified: serde_json::Value = serde_json::from_str(&verified).unwrap();
    assert_eq!(verified["status"], "current");
    assert_eq!(verified["summary"]["managed_current"], 1);

    // The stable key still resolves after line movement, so the verifier can
    // report both the stale rendered target and its current replacement.
    std::fs::write(
        repo.join("src/lib.rs"),
        "// inserted\n// inserted again\npub fn first() {}\n\npub fn cited() {}\n",
    )
    .unwrap();
    let (ok, moved) = sinter(repo, &["verify-doc", "docs/design.md", "--json"]);
    assert!(!ok, "a moved citation must fail the gate: {moved}");
    let moved: serde_json::Value = serde_json::from_str(&moved).unwrap();
    assert_eq!(moved["status"], "stale");
    assert_eq!(moved["citations"][0]["status"], "moved");
    assert_ne!(
        moved["citations"][0]["cited_target"],
        moved["citations"][0]["current_target"]
    );

    std::fs::write(repo.join("docs/bare.md"), "See src/lib.rs:5.\n").unwrap();
    let (ok, bare) = sinter(repo, &["verify-doc", "docs/bare.md", "--json"]);
    assert!(
        !ok,
        "a bare path/line cannot prove semantic identity: {bare}"
    );
    let bare: serde_json::Value = serde_json::from_str(&bare).unwrap();
    assert_eq!(bare["status"], "not_proven");
    assert_eq!(bare["citations"][0]["status"], "location_only");
}

#[test]
fn query_affected_path_impact() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "mod util;\nuse crate::util::core_fn;\n\n/// Entry point.\npub fn entry() -> u32 {\n    core_fn()\n}\n\npub fn test_entry() -> u32 {\n    entry()\n}\n",
    )
    .unwrap();
    std::fs::write(
        repo.join("src/util.rs"),
        "/// The core.\npub fn core_fn() -> u32 {\n    41\n}\n",
    )
    .unwrap();

    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);

    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    // query: content-bearing result.
    let (ok, out) = sinter(repo, &["query", "core_fn"]);
    assert!(ok, "{out}");
    assert!(out.contains("fn core_fn()"), "{out}");
    assert!(out.contains("The core."), "{out}");

    // affected: transitive, cross-file (entry via import, test_entry via scope).
    let (ok, out) = sinter(repo, &["affected", "core_fn"]);
    assert!(ok, "{out}");
    assert!(out.contains("entry"), "{out}");
    assert!(out.contains("test_entry"), "{out}");
    // Direct callers are stated apart from the transitive total, so a
    // blast radius never reads as a caller count.
    assert!(out.contains("3 dependents of core_fn"), "{out}");
    assert!(
        out.contains("1 direct in 1 file(s); 1 file(s) import it, 1 transitive"),
        "{out}"
    );
    let (_, out) = sinter(repo, &["affected", "core_fn", "--depth", "1"]);
    assert!(
        out.contains("2 dependents of core_fn") && !out.contains("test_entry"),
        "{out}"
    );
    let (_, out) = sinter(repo, &["affected", "core_fn", "--json"]);
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(
        (
            v["total"].as_u64(),
            v["direct"].as_u64(),
            v["direct_files"].as_u64(),
            v["importing_files"].as_u64()
        ),
        (Some(3), Some(1), Some(1), Some(1)),
        "{out}"
    );

    // evidence filter: scip-only finds nothing (no index present) —
    // grep-style exit 1 for a valid query with no results.
    let (ok, out) = sinter(repo, &["affected", "core_fn", "--evidence", "scip"]);
    assert!(!ok, "{out}");
    assert!(out.contains("0 dependents"), "{out}");

    // path: entry reaches core_fn through the import-evidence call edge.
    let (ok, out) = sinter(repo, &["path", "test_entry", "core_fn"]);
    assert!(ok, "{out}");
    assert!(out.contains("-[calls/"), "{out}");
    assert!(
        out.lines()
            .next()
            .is_some_and(|line| line.ends_with("core_fn")),
        "{out}"
    );

    // impact: edit core_fn, commit, ask for the blast radius of the commit.
    std::fs::write(
        repo.join("src/util.rs"),
        "/// The core.\npub fn core_fn() -> u32 {\n    42\n}\n",
    )
    .unwrap();
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "change core"]);
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");
    let (ok, out) = sinter(repo, &["impact", "HEAD~1..HEAD"]);
    assert!(ok, "{out}");
    assert!(out.contains("core_fn"), "{out}");
    assert!(out.contains("entry"), "{out}");
    assert!(out.contains("test_entry"), "{out}"); // matched as affected test

    // H1 regression: touching the imported file must not drop the
    // cross-file import-evidence edges — dependents survive the rebuild.
    std::fs::write(
        repo.join("src/util.rs"),
        "/// The core.\npub fn core_fn() -> u32 {\n    43\n}\n",
    )
    .unwrap();
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");
    let (ok, out) = sinter(repo, &["affected", "core_fn"]);
    assert!(ok, "{out}");
    assert!(out.contains("entry"), "{out}");
    assert!(out.contains("test_entry"), "{out}");
}

/// Dynamic dispatch is blast radius: `affected <ImplType::method>` must
/// reach callers of the trait method through the `dynamic` fan-out edge,
/// and evidence filtering without "dynamic" must exclude them.
#[test]
fn affected_through_dyn_dispatch() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "mod cat;\nmod dog;\n\npub trait Speak {\n    fn speak(&self);\n}\n\npub fn announce(s: &dyn Speak) {\n    Speak::speak(s);\n}\n",
    )
    .unwrap();
    let dog =
        "use crate::Speak;\n\npub struct Dog;\n\nimpl Speak for Dog {\n    fn speak(&self) {}\n}\n";
    std::fs::write(repo.join("src/dog.rs"), dog).unwrap();
    std::fs::write(
        repo.join("src/cat.rs"),
        "use crate::Speak;\n\npub struct Cat;\n\nimpl Speak for Cat {\n    fn speak(&self) {}\n}\n",
    )
    .unwrap();

    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);

    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    // Changing the impl must surface the caller of the trait method, with
    // the dynamic evidence visible on the bridging edge.
    let (ok, out) = sinter(repo, &["affected", "Dog::speak"]);
    assert!(ok, "{out}");
    assert!(out.contains("announce"), "{out}");
    assert!(out.contains("/dynamic"), "{out}");

    // The other direction is the same blast radius: changing the trait
    // method must reach every explicit implementation of it, not just the
    // trait method's callers. Explicit `impl Trait for T` is written down,
    // so these edges carry the evidence that bound the impl (import here),
    // not the dynamic evidence Go's structural satisfaction must use.
    let (ok, out) = sinter(repo, &["affected", "Speak::speak", "--max-depth", "2"]);
    assert!(ok, "{out}");
    assert!(out.contains("Dog::speak"), "{out}");
    assert!(out.contains("Cat::speak"), "{out}");

    // Incremental: touching one impl file re-resolves the trait's file and
    // tears down its dynamic fan-out — the OTHER impl's edge must survive
    // the rebuild (dst-file facts rejoin the resolution set).
    std::fs::write(repo.join("src/dog.rs"), dog.replace("{}", "{ }")).unwrap();
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");
    let (ok, out) = sinter(repo, &["affected", "Cat::speak"]);
    assert!(ok, "{out}");
    assert!(out.contains("announce"), "{out}");
    assert!(out.contains("/dynamic"), "{out}");

    // Incremental: touching the trait's own file re-derives the fan-out
    // (impl files re-resolve through their `uses` reference on the trait).
    std::fs::write(
        repo.join("src/lib.rs"),
        "mod cat;\nmod dog;\n\npub trait Speak {\n    fn speak(&self);\n}\n\npub fn announce(s: &dyn Speak) {\n    Speak::speak(s)\n}\n",
    )
    .unwrap();
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");
    let (ok, out) = sinter(repo, &["affected", "Dog::speak"]);
    assert!(ok, "{out}");
    assert!(out.contains("announce"), "{out}");
    assert!(out.contains("/dynamic"), "{out}");

    // Honesty: excluding dynamic evidence excludes the fan-out.
    let (_, out) = sinter(
        repo,
        &["affected", "Dog::speak", "--evidence", "import,scope,scip"],
    );
    assert!(!out.contains("announce"), "{out}");

    // --certain also excludes it (Dynamic is Inferred by construction).
    let (_, out) = sinter(repo, &["affected", "Dog::speak", "--certain"]);
    assert!(!out.contains("announce"), "{out}");
}

fn sinter_code(repo: &Path, args: &[&str]) -> (Option<i32>, String) {
    let out = Command::new(env!("CARGO_BIN_EXE_sinter"))
        .args(args)
        .env("HOME", repo)
        .env("USERPROFILE", repo)
        .current_dir(repo)
        .output()
        .expect("run sinter");
    (
        out.status.code(),
        format!(
            "{}{}",
            String::from_utf8_lossy(&out.stdout),
            String::from_utf8_lossy(&out.stderr)
        ),
    )
}

/// New surface: `--json` on the read commands (mirroring the MCP tool
/// shapes), grep-style exit codes, `--limit` on affected, and `--repo`
/// accepted by lifecycle commands.
#[test]
fn json_flags_exit_codes_and_repo_flag() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "mod util;\nuse crate::util::core_fn;\n\n/// Entry point.\npub fn entry() -> u32 {\n    core_fn()\n}\n\npub fn other_entry() -> u32 {\n    core_fn()\n}\n",
    )
    .unwrap();
    std::fs::write(
        repo.join("src/util.rs"),
        "/// The core.\npub fn core_fn() -> u32 {\n    41\n}\n",
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);

    // Lifecycle commands accept --repo (positional still works elsewhere).
    let (code, out) = sinter_code(repo, &["build", "--repo", "."]);
    assert_eq!(code, Some(0), "{out}");
    // Both at once is a usage error.
    let (code, _) = sinter_code(repo, &["build", ".", "--repo", "."]);
    assert_eq!(code, Some(2));

    // query --json: MCP `query` shape.
    let (code, out) = sinter_code(repo, &["query", "core_fn", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v["exact"], serde_json::json!(true), "{out}");
    assert_eq!(v["results"][0]["name"], serde_json::json!("core_fn"));

    // show --json: MCP `show` shape.
    let (code, out) = sinter_code(repo, &["show", "core_fn", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v["symbol"]["name"], serde_json::json!("core_fn"));
    assert!(v["incoming"].as_array().is_some(), "{out}");
    // Every edge exposes `site`; the call from `entry` names its call site
    // (`core_fn()` on line 6 of src/lib.rs).
    let entry_call = v["incoming"]
        .as_array()
        .unwrap()
        .iter()
        .find(|e| {
            e["symbol"].as_str().is_some_and(|s| s.ends_with("entry")) && e["relation"] == "calls"
        })
        .unwrap_or_else(|| panic!("no call edge from entry: {out}"));
    assert_eq!(
        entry_call["site"],
        serde_json::json!("src/lib.rs:6"),
        "{out}"
    );

    // affected --json: MCP `affected` shape, terse entries.
    let (code, out) = sinter_code(repo, &["affected", "core_fn", "--json", "--coverage"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert!(v["total"].as_u64().unwrap() >= 2, "{out}");
    assert!(v["dependents"][0]["s"].is_string(), "{out}");
    assert!(v["dependents"][0]["c"].is_string(), "{out}");
    assert_eq!(v["coverage"]["status"], "found", "{out}");
    assert_eq!(v["coverage"]["completeness"], "partial", "{out}");
    assert_eq!(
        v["coverage"]["filters"]["relations"]["mode"], "all_dependencies",
        "{out}"
    );
    assert!(
        v["coverage"]["evidence"]["possible"]["results"]
            .as_u64()
            .unwrap()
            >= 1,
        "{out}"
    );

    // deps uses the same snapshot/filter/evidence coverage contract.
    let (code, out) = sinter_code(repo, &["deps", "entry", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert!(v["snapshot"].is_string(), "{out}");
    assert_eq!(v["coverage"]["status"], "found", "{out}");
    assert_eq!(v["coverage"]["completeness"], "partial", "{out}");
    assert!(v["dependencies"][0]["c"].is_string(), "{out}");

    // affected --limit: truncation footer, ask-style.
    let (code, out) = sinter_code(repo, &["affected", "core_fn", "--limit", "1"]);
    assert_eq!(code, Some(0), "{out}");
    assert!(out.contains("more dependents below cutoff"), "{out}");
    assert!(out.contains("--limit"), "{out}");

    // path --json: MCP `path` shape.
    let (code, out) = sinter_code(repo, &["path", "entry", "core_fn", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v["found"], serde_json::json!(true), "{out}");
    assert!(v["steps"][0]["relation"].is_string(), "{out}");
    assert!(v["steps"][0]["confidence"].is_string(), "{out}");
    assert_eq!(v["coverage"]["status"], "found", "{out}");
    assert_eq!(v["coverage"]["completeness"], "partial", "{out}");
    // Each hop names where it is written.
    assert_eq!(
        v["steps"][0]["site"],
        serde_json::json!("src/lib.rs:6"),
        "{out}"
    );

    // impact --json: the ImpactReport shape.
    let (code, out) = sinter_code(repo, &["impact", "HEAD", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert!(v["changed_symbols"].is_array(), "{out}");

    // Grep-style exit codes: 1 = valid query, no results.
    let (code, out) = sinter_code(repo, &["show", "no_such_symbol_zzqx"]);
    assert_eq!(code, Some(1), "{out}");
    assert!(out.contains("sinter ask"), "no concept-search hint: {out}");
    let (code, _) = sinter_code(repo, &["query", "zzqxzzqx"]);
    assert_eq!(code, Some(1));
    let (code, out) = sinter_code(repo, &["path", "core_fn", "entry"]);
    assert_eq!(code, Some(1), "{out}");
    assert!(out.contains("no path"), "{out}");
    // 2 = usage/execution error (bad evidence kind).
    let (code, _) = sinter_code(repo, &["affected", "core_fn", "--evidence", "bogus"]);
    assert_eq!(code, Some(2));

    // map accepts --repo like the other read commands.
    let (code, out) = sinter_code(repo, &["map", "--repo", "."]);
    assert_eq!(code, Some(0), "{out}");
    assert!(out.contains("nodes"), "{out}");
}

/// Empty graph: read commands say so instead of "no match", build warns.
#[test]
fn empty_graph_says_so() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::write(repo.join("notes.txt"), "no source here\n").unwrap();

    let (code, out) = sinter_code(repo, &["build"]);
    assert_eq!(code, Some(0), "{out}");
    assert!(out.contains("0 source files found under"), "{out}");
    assert!(out.contains("wrong directory?"), "{out}");

    let (code, out) = sinter_code(repo, &["query", "anything"]);
    assert_eq!(code, Some(2), "{out}");
    assert!(out.contains("graph") && out.contains("is empty"), "{out}");
    assert!(out.contains("right directory"), "{out}");
}

/// Every traversal carries coverage. Missing/stale SCIP makes hits and misses
/// partial; a fresh index can only claim completeness for the indexed graph,
/// never runtime exhaustiveness.
#[test]
fn negative_answers_flag_stale_scip() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "pub fn entry() -> u32 {\n    core_fn()\n}\n\npub fn core_fn() -> u32 {\n    41\n}\n\npub fn orphan() {}\n",
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    // No index: explicit syntax-only coverage gap.
    let (_, out) = sinter(repo, &["path", "core_fn", "entry"]);
    assert!(
        out.contains("no path")
            && out.contains("coverage: partial")
            && out.contains("gap: scip missing"),
        "{out}"
    );

    // Index older than the source: inconclusive.
    let index = repo.join(".sinter/index.scip");
    std::fs::write(&index, b"").unwrap();
    let old = std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_000_000);
    std::fs::OpenOptions::new()
        .write(true)
        .open(&index)
        .unwrap()
        .set_modified(old)
        .unwrap();
    let (_, out) = sinter(repo, &["path", "core_fn", "entry"]);
    assert!(
        out.contains("no path") && out.contains("gap: scip stale"),
        "{out}"
    );
    let (_, out) = sinter(repo, &["affected", "orphan"]);
    assert!(
        out.contains("0 dependents") && out.contains("gap: scip stale"),
        "{out}"
    );
    // A hit carries the same partial coverage instead of looking exhaustive.
    let (_, out) = sinter(repo, &["path", "entry", "core_fn"]);
    assert!(out.contains("coverage: partial"), "{out}");

    // Index newer than the source: still not-proven, without stale/missing.
    std::fs::OpenOptions::new()
        .write(true)
        .open(&index)
        .unwrap()
        .set_modified(std::time::SystemTime::now())
        .unwrap();
    let (_, out) = sinter(repo, &["path", "core_fn", "entry"]);
    assert!(
        out.contains("no path")
            && out.contains("coverage: complete_for_indexed_snapshot")
            && !out.contains("gap: scip stale")
            && !out.contains("gap: scip missing"),
        "{out}"
    );

    let (_, out) = sinter(repo, &["path", "entry", "core_fn", "--json", "--coverage"]);
    let value: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(
        value["coverage"]["completeness"], "complete_for_indexed_snapshot",
        "{out}"
    );
    assert_eq!(value["coverage"]["conclusive"], false, "{out}");
    assert!(
        value["coverage"]["available_sources"]
            .as_array()
            .unwrap()
            .iter()
            .any(|source| source["kind"] == "scip" && source["status"] == "available"),
        "{out}"
    );
}

/// Installation drift is reported by maintenance verbs, never beside a
/// query answer: agents read stderr with stdout, and a nag on every
/// `show` obscures the result it decorates.
#[test]
fn query_verbs_never_nag_about_stale_artifacts() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "pub fn entry() -> u32 {\n    41\n}\n",
    )
    .unwrap();
    // A managed AGENTS.md block with stale contents.
    std::fs::write(
        repo.join("AGENTS.md"),
        "<!-- BEGIN sinter (managed by `sinter install`; edits inside are overwritten) -->\nold\n<!-- END sinter -->\n",
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);

    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");
    assert!(out.contains("AGENTS.md sinter block is stale"), "{out}");

    for verb in [
        &["show", "entry"][..],
        &["ask", "entry"],
        &["affected", "entry"],
        &["path", "entry", "entry"],
    ] {
        let (_, out) = sinter(repo, verb);
        assert!(!out.contains("is stale"), "{verb:?} nagged: {out}");
    }
}

/// `show` names trait implementors and dynamic fan-out explicitly rather
/// than folding them into used-by / calls tallies.
#[test]
fn show_lists_implementations_and_dispatch() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "pub trait Speak {\n    fn speak(&self);\n}\n\npub struct Dog;\npub struct Cat;\n\nimpl Speak for Dog {\n    fn speak(&self) {}\n}\n\nimpl Speak for Cat {\n    fn speak(&self) {}\n}\n\npub fn announce(s: &dyn Speak) {\n    Speak::speak(s);\n}\n",
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    let (_, out) = sinter(repo, &["show", "Speak"]);
    assert!(out.contains("implemented by (2)    Cat, Dog"), "{out}");
    let (_, out) = sinter(repo, &["show", "Speak::speak"]);
    assert!(
        out.contains("dispatches to (2)    Cat::speak, Dog::speak"),
        "{out}"
    );
    assert!(
        !out.contains("calls ("),
        "dynamic edges leaked into calls: {out}"
    );
    let (_, out) = sinter(repo, &["show", "Dog"]);
    assert!(out.contains("implements       Speak"), "{out}");

    // A miss explains itself: forward reach, and who does reach the target.
    // Forward reach is no longer empty — the method-level `implements` edge
    // carries Dog::speak up to Speak::speak, and its fan-out on to the
    // sibling impl — but announce only ever calls INTO the trait method.
    let (_, out) = sinter(repo, &["path", "Dog::speak", "announce"]);
    assert!(out.contains("no path Dog::speak -> announce"), "{out}");
    assert!(
        out.contains("forward search from Dog::speak reached 2 symbol(s)"),
        "{out}"
    );
    assert!(
        out.contains("nothing reaches announce under this filter"),
        "{out}"
    );
    let (_, out) = sinter(repo, &["path", "Dog", "Dog::speak"]);
    assert!(out.contains("Dog::speak is reached by (1):"), "{out}");
    assert!(out.contains("Speak::speak [calls/dynamic]"), "{out}");
    let (_, out) = sinter(repo, &["path", "announce", "Dog::speak", "--certain"]);
    assert!(
        out.contains("1 incoming edge(s) excluded by --evidence/--certain"),
        "{out}"
    );
}

#[test]
fn stable_handles_relocate_and_snapshot_preconditions_fail_typed() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(repo.join("src/lib.rs"), "pub fn run() -> u8 { 1 }\n").unwrap();

    let (code, out) = sinter_code(repo, &["build"]);
    assert_eq!(code, Some(0), "{out}");
    let (code, out) = sinter_code(repo, &["query", "run", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let before: serde_json::Value = serde_json::from_str(&out).unwrap();
    let old_id = before["results"][0]["snapshot_id"]
        .as_str()
        .unwrap()
        .to_string();
    let symbol_key = before["results"][0]["symbol_key"]
        .as_str()
        .unwrap()
        .to_string();
    assert_eq!(before["results"][0]["id"], symbol_key);
    let old_snapshot = before["snapshot"].as_str().unwrap().to_string();

    std::fs::write(
        repo.join("src/lib.rs"),
        "// harmless offset shift\n\npub fn run() -> u8 { 1 }\n",
    )
    .unwrap();

    let (code, out) = sinter_code(repo, &["show", &symbol_key, "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let current: serde_json::Value = serde_json::from_str(&out).unwrap();
    let new_id = current["symbol"]["snapshot_id"]
        .as_str()
        .unwrap()
        .to_string();
    let new_snapshot = current["snapshot"].as_str().unwrap().to_string();
    assert_ne!(new_id, old_id);
    assert_ne!(new_snapshot, old_snapshot);
    assert_eq!(current["symbol"]["id"], symbol_key);
    assert_eq!(current["symbol"]["symbol_key"], symbol_key);

    let (code, out) = sinter_code(repo, &["show", &old_id, "--json"]);
    assert_eq!(code, Some(2), "{out}");
    let relocated: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(relocated["error"]["code"], "relocated_handle", "{out}");
    assert_eq!(relocated["outcome"]["status"], "relocated", "{out}");
    assert_eq!(relocated["error"]["candidates"][0]["snapshot_id"], new_id);
    assert_eq!(relocated["error"]["candidates"][0]["id"], symbol_key);
    assert_eq!(
        relocated["error"]["candidates"][0]["symbol_key"],
        symbol_key
    );

    let (code, out) = sinter_code(
        repo,
        &[
            "show",
            &symbol_key,
            "--if-snapshot",
            &old_snapshot,
            "--json",
        ],
    );
    assert_eq!(code, Some(2), "{out}");
    let stale: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(stale["error"]["code"], "stale_snapshot", "{out}");
    assert_eq!(stale["error"]["expected_snapshot"], old_snapshot);
    assert_eq!(stale["error"]["actual_snapshot"], new_snapshot);

    let (code, out) = sinter_code(
        repo,
        &["show", &new_id, "--if-snapshot", &new_snapshot, "--json"],
    );
    assert_eq!(code, Some(0), "{out}");
}

#[test]
fn duplicate_declarations_make_stable_key_ambiguity_explicit() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::write(
        repo.join("duplicate.py"),
        "def duplicate(value):\n    return value\n\ndef duplicate(value, other):\n    return value + other\n",
    )
    .unwrap();
    let (code, out) = sinter_code(repo, &["build"]);
    assert_eq!(code, Some(0), "{out}");
    let (code, out) = sinter_code(repo, &["query", "duplicate", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let query: serde_json::Value = serde_json::from_str(&out).unwrap();
    let results = query["results"].as_array().unwrap();
    assert_eq!(results.len(), 2, "{out}");
    let key = results[0]["symbol_key"].as_str().unwrap();
    assert_eq!(results[1]["symbol_key"], key);

    let (code, out) = sinter_code(repo, &["show", key, "--json"]);
    assert_eq!(code, Some(2), "{out}");
    let ambiguous: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(ambiguous["error"]["code"], "ambiguous_symbol", "{out}");
    assert_eq!(
        ambiguous["error"]["candidates"].as_array().unwrap().len(),
        2
    );
}

#[test]
fn syntax_visible_test_callers_drive_affected_and_impact_selection() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    let source = |leaf: usize| {
        format!(
            "pub fn leaf() -> usize {{ {leaf} }}\n\
             pub fn dispatch() -> usize {{ leaf() }}\n\
             #[cfg(test)]\n\
             mod tests {{\n\
                 use super::dispatch;\n\
                 #[test]\n\
                 fn dispatch_works() {{\n\
                     let result = dispatch();\n\
                     assert!(result > 0);\n\
                 }}\n\
             }}\n"
        )
    };
    std::fs::write(repo.join("src/lib.rs"), source(1)).unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "base"]);
    std::fs::write(repo.join("src/lib.rs"), source(2)).unwrap();
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "change leaf"]);
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    let (code, out) = sinter_code(
        repo,
        &[
            "affected",
            "dispatch",
            "--depth",
            "1",
            "--relations",
            "calls",
            "--include-tests",
            "--json",
        ],
    );
    assert_eq!(code, Some(0), "{out}");
    let affected: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert!(
        affected["dependents"]
            .as_array()
            .unwrap()
            .iter()
            .any(|dependent| dependent["s"] == "tests::dispatch_works"),
        "direct test caller missing: {out}"
    );
    // Hidden by default, but counted: the answer never reads as "no callers".
    let (code, out) = sinter_code(
        repo,
        &[
            "affected",
            "dispatch",
            "--depth",
            "1",
            "--relations",
            "calls",
            "--json",
        ],
    );
    assert_eq!(code, Some(1), "{out}");
    let hidden: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(hidden["tests_hidden"], 1, "{out}");
    assert!(
        hidden.get("reason").is_none(),
        "hidden tests are not a filter: {out}"
    );

    let (code, out) = sinter_code(
        repo,
        &[
            "affected",
            "leaf",
            "--relations",
            "calls",
            "--include-tests",
            "--json",
        ],
    );
    assert_eq!(code, Some(0), "{out}");
    let affected: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert!(
        affected["dependents"]
            .as_array()
            .unwrap()
            .iter()
            .any(|dependent| dependent["s"] == "tests::dispatch_works"),
        "transitive test caller missing: {out}"
    );

    let (code, out) = sinter_code(repo, &["impact", "HEAD~1..HEAD", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let impact: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert!(
        impact["affected_tests"]
            .as_array()
            .unwrap()
            .iter()
            .any(|test| test["qualified"] == "tests::dispatch_works"),
        "impact did not select the evidence-backed test: {out}"
    );
}

#[test]
fn lookup_prefers_production_over_fixture_copies() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::create_dir_all(repo.join("tests/fixtures/a/src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "use crate::helper;\npub fn entry() -> u32 { 1 }\npub fn caller() -> u32 { entry() }\n",
    )
    .unwrap();
    std::fs::write(
        repo.join("tests/fixtures/a/src/lib.rs"),
        "pub fn entry() -> u32 { 2 }\n",
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    // Ambiguous only because of the fixture copy: resolve, mention the rest.
    let (ok, out) = sinter(repo, &["show", "entry"]);
    assert!(ok, "{out}");
    assert!(out.contains("src/lib.rs"), "{out}");
    // The pick leads the card, said once (no stderr `note:` twin), and the
    // Next hint carries the selector that re-resolves to the same node.
    assert!(
        out.starts_with("resolved: entry@src/lib.rs (1 other ignored by fixture: "),
        "{out}"
    );
    assert!(!out.contains("note:"), "{out}");
    // JSON keeps the diagnostic in the envelope.
    let (ok, json) = sinter(repo, &["show", "entry", "--json"]);
    assert!(ok, "{json}");
    assert!(json.contains("1 other `entry` ignored (fixture)"), "{json}");
    assert!(
        out.contains("Next: sinter affected entry@src/lib.rs --max-depth 3"),
        "{out}"
    );

    // Explicit file suffix still reaches the fixture copy.
    let (ok, out) = sinter(repo, &["show", "entry@tests/fixtures/a/src/lib.rs"]);
    assert!(ok, "{out}");

    // affected counts file imports separately from callers.
    let (ok, out) = sinter(repo, &["affected", "entry", "--json"]);
    assert!(ok, "{out}");
    let json = out.lines().find(|l| l.starts_with('{')).unwrap();
    let v: serde_json::Value = serde_json::from_str(json).unwrap();
    assert_eq!(v["direct"], 1, "{out}");
    assert!(v.get("importing_files").is_some(), "{out}");
}

/// `show` is one bounded screen: `--limit` collapses every relation group
/// to `… (+N) · --limit`, `--relations` drops the others, `--scope` drops
/// edges whose far end is outside the selected corpus roles.
#[test]
fn show_is_bounded_by_limit_relations_and_scope() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::create_dir_all(repo.join("tests")).unwrap();
    std::fs::write(
        repo.join("Cargo.toml"),
        "[package]\nname = \"x\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
    )
    .unwrap();
    let mut lib = String::from("pub struct Node;\n");
    for i in 0..5 {
        lib.push_str(&format!("pub fn user_{i}(n: &Node) -> &Node {{ n }}\n"));
    }
    std::fs::write(repo.join("src/lib.rs"), lib).unwrap();
    std::fs::write(
        repo.join("tests/probe.rs"),
        "use x::Node;\n#[test]\nfn probe_uses_node() { let _n: Option<Node> = None; }\n",
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);
    let (ok, out) = sinter(repo, &["build", "."]);
    assert!(ok, "{out}");

    // Used-by is a one-line tally unless `--callers` asks for the files.
    let (ok, tally) = sinter(repo, &["show", "Node", "--scope", "production"]);
    assert!(ok, "{tally}");
    assert!(tally.contains("used by: 1 files, 5 edges ("), "{tally}");
    assert!(!tally.contains("src/lib.rs:2   5 edges"), "{tally}");
    let (ok, full) = sinter(
        repo,
        &["show", "Node", "--scope", "production", "--callers"],
    );
    assert!(ok, "{full}");
    assert!(full.contains("used by (1 files, 5 edges)"), "{full}");
    // Every kept site, capped: five users referencing Node twice each
    // (parameter and return type) list their lines instead of collapsing
    // to one representative, and say how many the cap dropped.
    assert!(
        full.contains("src/lib.rs:2, :3, :4, :5 (+2 more)   5 edges"),
        "{full}"
    );
    assert!(!full.contains("--limit"), "{full}");

    let (ok, out) = sinter(repo, &["show", "user_0", "--limit", "0"]);
    assert!(ok, "{out}");
    assert!(out.contains("… (+1) · --limit"), "{out}");

    let (ok, out) = sinter(repo, &["show", "Node", "--relations", "calls"]);
    assert!(ok, "{out}");
    assert!(!out.contains("used by"), "{out}");

    let (ok, out) = sinter(repo, &["show", "Node", "--json", "--limit", "2"]);
    assert!(ok, "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    let uses = v["incoming"]
        .as_array()
        .unwrap()
        .iter()
        .filter(|e| e["relation"] == "uses")
        .count();
    assert_eq!(uses, 2, "{out}");
    assert_eq!(v["totals"]["incoming"]["uses"], 6, "{out}");
    assert_eq!(v["truncated"]["incoming"]["uses"], 4, "{out}");
}

#[test]
fn no_dependents_assertion_counts_non_call_edges_and_no_callers_hints_at_it() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "pub const LIMIT: usize = 3;\npub struct Config;\npub fn build() -> Config { let _ = LIMIT; Config }\npub struct Unused;\n",
    )
    .unwrap();
    std::fs::write(
        repo.join("Cargo.toml"),
        "[package]\nname='fixture'\nversion='0.1.0'\nedition='2024'\n",
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);
    std::fs::write(
        repo.join("index.scip"),
        Index::default().write_to_bytes().unwrap(),
    )
    .unwrap();
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    // no-callers on a struct is not a useful question; say which is.
    let (ok, out) = sinter(repo, &["assert", "no-callers", "Config", "--json"]);
    let no_callers: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(
        no_callers["hint"],
        "no-callers counts calls edges only; use assert no-dependents for struct",
        "{ok} {out}"
    );
    let (_, text) = sinter(repo, &["assert", "no-callers", "Config"]);
    assert!(
        text.contains("hint: no-callers counts calls edges only"),
        "{text}"
    );
    // The default payload drops the doctor-shaped graph block; --verbose keeps it.
    assert!(no_callers["coverage"]["graph"].is_null(), "{out}");
    assert!(!no_callers["coverage"]["completeness"].is_null(), "{out}");
    assert!(!no_callers["coverage"]["universe"].is_null(), "{out}");
    assert!(!no_callers["coverage"]["limitations"].is_null(), "{out}");
    let (_, out) = sinter(
        repo,
        &["assert", "no-callers", "Config", "--json", "--verbose"],
    );
    let verbose: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert!(verbose["coverage"]["graph"].is_object(), "{out}");

    let (ok, out) = sinter(repo, &["assert", "no-dependents", "Config", "--json"]);
    assert!(!ok, "a used struct must violate no-dependents: {out}");
    let violated: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(violated["status"], "violated");
    assert_eq!(violated["assertion"]["kind"], "no_dependents");
    assert!(
        violated["observed_dependents"].as_u64().unwrap() >= 1,
        "{out}"
    );
    assert_eq!(violated["dependents"][0]["name"], "build");
    assert!(violated["hint"].is_null(), "{out}");

    let (ok, out) = sinter(repo, &["assert", "no-dependents", "Unused", "--json"]);
    assert!(ok, "{out}");
    let holding: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(holding["status"], "holds_for_indexed_snapshot");
    assert_eq!(holding["observed_dependents"], 0);
    let (_, text) = sinter(repo, &["assert", "no-dependents", "Unused"]);
    assert!(text.contains("assert no-dependents"), "{text}");
    assert!(text.contains("0 observed dependent(s)"), "{text}");
}

/// `assert deletable` tallies every scope; a missing symbol is an error
/// (2), not a failed assertion (1); the compact JSON stays small; the
/// holding rule ignores partial files outside the asserted scope; `show`
/// answers `@file:line`, marks the line, and names same-stem siblings.
#[test]
fn deletable_no_match_exit_and_scope_local_completeness() {
    let dir = tempfile::tempdir().unwrap();
    let repo = dir.path();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::create_dir_all(repo.join("tests/fixtures")).unwrap();
    std::fs::write(
        repo.join("src/lib.rs"),
        "pub mod env;\npub fn called() {}\npub fn production_caller() { called(); }\npub fn leaf() {}\n\n#[cfg(test)]\nmod tests {\n    use super::called;\n    #[test]\n    fn test_caller() { called(); }\n}\n",
    )
    .unwrap();
    std::fs::write(
        repo.join("src/env.rs"),
        "pub struct Env;\npub fn leaf_helper() {}\n",
    )
    .unwrap();
    std::fs::write(repo.join("tests/env.rs"), "pub struct Env;\n").unwrap();
    // A fixture with a syntax error: partial syntax tree, outside production.
    std::fs::write(repo.join("tests/fixtures/broken.rs"), "fn broken( {\n").unwrap();
    std::fs::write(
        repo.join("Cargo.toml"),
        "[package]\nname='fixture'\nversion='0.1.0'\nedition='2024'\n",
    )
    .unwrap();
    std::fs::write(
        repo.join("index.scip"),
        Index::default().write_to_bytes().unwrap(),
    )
    .unwrap();
    git(repo, &["init", "-q"]);
    git(repo, &["add", "."]);
    git(repo, &["commit", "-qm", "init"]);
    let (ok, out) = sinter(repo, &["build"]);
    assert!(ok, "{out}");

    // deletable: every scope, grouped, plain words, exit 1 when anything depends.
    let (code, out) = sinter_code(repo, &["assert", "deletable", "called", "--json"]);
    assert_eq!(code, Some(1), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v["status"], "has_dependents", "{out}");
    assert_eq!(v["dependents_by_scope"]["production"], 1, "{out}");
    assert_eq!(v["dependents_by_scope"]["test"], 1, "{out}");
    assert!(v["coverage"]["universe"]["mode"].is_string(), "{out}");
    assert!(v["coverage"]["limitations"].is_array(), "{out}");
    assert!(v["snapshot"].is_string(), "{out}");
    assert_eq!(
        v["coverage"]["compiler_index"],
        serde_json::json!({"state": "fresh"}),
        "{out}"
    );
    assert!(v["assertion"].get("meaning").is_none(), "{out}");
    let row = &v["dependents"][0];
    for key in [
        "name",
        "site",
        "relation",
        "evidence",
        "confidence",
        "scope",
    ] {
        assert!(row.get(key).is_some(), "{key} missing: {out}");
    }
    assert!(row.get("signature").is_none(), "{out}");
    let (code, text) = sinter_code(repo, &["assert", "deletable", "called"]);
    assert_eq!(code, Some(1), "{text}");
    assert!(text.contains("has_dependents"), "{text}");
    assert!(
        text.contains("  production (1)\n    production_caller"),
        "{text}"
    );
    assert!(
        text.contains("  test (1)\n    tests::test_caller"),
        "{text}"
    );
    let (code, out) = sinter_code(repo, &["assert", "deletable", "leaf", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v["status"], "none_observed", "{out}");
    // --verbose keeps the full envelope.
    let (_, out) = sinter_code(
        repo,
        &["assert", "deletable", "leaf", "--json", "--verbose"],
    );
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert!(v["assertion"]["meaning"].is_string(), "{out}");
    assert!(v["coverage"]["graph"].is_object(), "{out}");

    // A symbol that does not exist is an error, not a failed assertion.
    let (code, out) = sinter_code(repo, &["assert", "no-callers", "no_such_symbol_xyz"]);
    assert_eq!(code, Some(2), "{out}");
    let (code, out) = sinter_code(
        repo,
        &["assert", "no-callers", "no_such_symbol_xyz", "--json"],
    );
    assert_eq!(code, Some(2), "{out}");
    assert!(out.contains("no_match"), "{out}");

    // Ignored out-of-scope rows are tallied inline by scope; the compact
    // JSON is the decision and its qualifiers only.
    let (_, out) = sinter_code(repo, &["assert", "no-callers", "called"]);
    assert!(out.contains("ignored out of scope: test 1;"), "{out}");
    let (_, out) = sinter_code(repo, &["assert", "no-callers", "called", "--json"]);
    assert!(
        out.len() < 800,
        "compact assert JSON is {} bytes: {out}",
        out.len()
    );

    // The partial fixture file is outside `production`, so the negative
    // claim over production still holds for the indexed snapshot.
    let (code, out) = sinter_code(repo, &["assert", "no-callers", "leaf", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v["status"], "holds_for_indexed_snapshot", "{out}");
    // Asserting over the fixture scope too puts the gap inside the claim.
    let (code, out) = sinter_code(
        repo,
        &[
            "assert",
            "no-callers",
            "leaf",
            "--scope",
            "production,fixture",
            "--json",
        ],
    );
    assert_eq!(code, Some(1), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v["status"], "not_proven", "{out}");
    // Compact `--json` codes the gap; `--verbose` still names the file.
    assert!(
        v["coverage"]["limitations"]
            .as_array()
            .unwrap()
            .iter()
            .any(|l| l == "partial_syntax_in_scope"),
        "{out}"
    );
    let (_, out) = sinter_code(
        repo,
        &[
            "assert",
            "no-callers",
            "leaf",
            "--scope",
            "production,fixture",
            "--json",
            "--verbose",
        ],
    );
    assert!(out.contains("tests/fixtures/broken.rs"), "{out}");

    // show: same-stem sibling in another file, `@file:line`, marked line.
    let (ok, out) = sinter(repo, &["show", "leaf"]);
    assert!(ok, "{out}");
    assert!(out.contains("also_see: leaf_helper@src/env.rs"), "{out}");
    let (ok, out) = sinter(repo, &["show", "@src/lib.rs:3", "--body"]);
    assert!(ok, "{out}");
    assert!(out.contains("function production_caller"), "{out}");
    assert!(out.contains("  > pub fn production_caller()"), "{out}");
    let (ok, out) = sinter(repo, &["show", "@lib.rs:3", "--json"]);
    assert!(ok, "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v["symbol"]["qualified"], "production_caller", "{out}");
    let (ok, out) = sinter(repo, &["assert", "no-callers", "leaf"]);
    assert!(ok, "{out}");
    assert!(out.contains("also_see: leaf_helper@src/env.rs"), "{out}");

    // query: fuzzy is exit 1; production copies rank first.
    let (code, out) = sinter_code(repo, &["query", "calle"]);
    assert_eq!(code, Some(1), "{out}");
    assert!(out.contains("close names"), "{out}");
    let (code, out) = sinter_code(repo, &["query", "Env", "--scope", "all", "--json"]);
    assert_eq!(code, Some(0), "{out}");
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v["results"][0]["file"], "src/env.rs", "{out}");
    assert_eq!(v["results"][1]["file"], "tests/env.rs", "{out}");
}