vela-protocol 0.940.6

Core library for the Vela scientific knowledge protocol: replayable frontier state, signed canonical events, and proof packets.
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
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
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
//! T7: the typed claim-level edge layer — the FrontierGraph substrate.
//!
//! The full `.vela/graph/frontier-graph.v1.json` export is a broad
//! provenance graph (findings, sources, events, proposals, evidence
//! atoms — tens of thousands of nodes) built by the Python tooling.
//! This module is the *claim-level* view T7 reasons over: findings as
//! nodes (Claim nodes), and the typed relations between them
//! (`SUPPORTS / CONTRADICTS / DEPENDS_ON / DERIVED_FROM / IMPROVES /
//! GENERALIZES / SPECIALIZES / SUPERSEDES`, plus the legacy `EXTENDS`
//! and `REPLICATES`) as typed edges.
//!
//! It is a derived view, like [`crate::project::ReverseDepIndex`]: built
//! from the canonical findings + links, never an authority. Where a causal
//! closure keeps only the `depends`/`supports` subset, the FrontierGraph
//! keeps *every* typed relation so queries like "which contradictions are
//! open" and "what improves this result" are first-class.
//!
//! Cross-frontier (`vf_…@vfr_…`) targets are recorded as edges to an
//! external node id but are not resolved here — cross-frontier
//! traversal is a separate step (P2). Within a single merged Project,
//! though, every endpoint that exists is linked.

use std::collections::{BTreeMap, BTreeSet, HashMap};

use serde::Serialize;

use crate::evidence_diff::derive_status_provenance;
use crate::project::Project;

/// The canonical T7 relation vocabulary. Each variant maps to one or
/// more `link_type` strings from [`crate::bundle::VALID_LINK_TYPES`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum EdgeKind {
    Supports,
    Contradicts,
    DependsOn,
    DerivedFrom,
    Improves,
    Generalizes,
    Specializes,
    Supersedes,
    Extends,
    Replicates,
    /// A cross-domain transfer (`vtr_`): the source claim discharges a premise
    /// of the target claim via a kernel-verified verifier-homomorphism.
    Discharges,
}

impl EdgeKind {
    /// Map a stored `link_type` string to its canonical edge kind.
    /// `depends` is DEPENDS_ON and `synthesized_from` is DERIVED_FROM;
    /// unknown strings return `None` (the link is skipped from the
    /// typed graph rather than silently mis-categorized).
    #[must_use]
    pub fn from_link_type(link_type: &str) -> Option<Self> {
        Some(match link_type {
            "supports" => Self::Supports,
            "contradicts" => Self::Contradicts,
            "depends" => Self::DependsOn,
            "synthesized_from" | "derived_from" => Self::DerivedFrom,
            "improves" => Self::Improves,
            "generalizes" => Self::Generalizes,
            "specializes" => Self::Specializes,
            "supersedes" => Self::Supersedes,
            "extends" => Self::Extends,
            "replicates" => Self::Replicates,
            "discharges" => Self::Discharges,
            // A cross-problem reduction "A implies B" means B's resolution rests
            // on A (solving A solves B), so it participates in the dependency
            // blast-radius exactly as `depends`. Mapped onto the existing
            // `DependsOn` kind (no new variant, so no wire-format change), which
            // is what makes the `implies` links the ingest already mints become
            // real typed graph edges instead of being silently dropped.
            "implies" => Self::DependsOn,
            _ => return None,
        })
    }

    /// Lowercase canonical string for this edge kind.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Supports => "supports",
            Self::Contradicts => "contradicts",
            Self::DependsOn => "depends_on",
            Self::DerivedFrom => "derived_from",
            Self::Improves => "improves",
            Self::Generalizes => "generalizes",
            Self::Specializes => "specializes",
            Self::Supersedes => "supersedes",
            Self::Extends => "extends",
            Self::Replicates => "replicates",
            Self::Discharges => "discharges",
        }
    }

    /// Every edge kind, for enumeration and parsing.
    pub const ALL: [EdgeKind; 11] = [
        Self::Supports,
        Self::Contradicts,
        Self::DependsOn,
        Self::DerivedFrom,
        Self::Improves,
        Self::Generalizes,
        Self::Specializes,
        Self::Supersedes,
        Self::Extends,
        Self::Replicates,
        Self::Discharges,
    ];

    /// Parse from either the canonical string ([`Self::as_str`]) or a
    /// raw `link_type` ([`Self::from_link_type`]).
    #[must_use]
    pub fn parse(s: &str) -> Option<Self> {
        Self::ALL
            .iter()
            .copied()
            .find(|k| k.as_str() == s)
            .or_else(|| Self::from_link_type(s))
    }
}

/// A typed, directed edge between two claim nodes. `target` may be a
/// cross-frontier id (`vf_…@vfr_…`); `target_in_frontier` records
/// whether the endpoint resolves to a node present in this graph.
#[derive(Debug, Clone, Serialize)]
pub struct Edge {
    pub source: String,
    pub target: String,
    pub kind: EdgeKind,
    pub note: String,
    pub target_in_frontier: bool,
}

/// The product-facing state of a finding (memo §6) — **Plane 2** of the four
/// status planes (see `docs/THEORY.md` Part II §24): distinct from the cross-source
/// resolution words (Plane 1), the Belnap/bilattice epistemic status (Plane 3),
/// and the review-lifecycle signals (Plane 4). A pure, derived classification
/// of a finding's review verdict + confidence into the five words the platform
/// speaks — never persisted, recomputed on read.
/// `Refuted`/`Contested` are live disagreement; `Fragile` is established
/// but thin; `Established` is accepted with real support; `Open` is the
/// default working state. State is orthogonal to the verifier gate: it
/// reads the *review* verdict, not the trust gate, and never collapses
/// to green/red (§8).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum FindingState {
    Open,
    Established,
    Refuted,
    Contested,
    Fragile,
}

impl FindingState {
    /// Lowercase canonical string.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Open => "open",
            Self::Established => "established",
            Self::Refuted => "refuted",
            Self::Contested => "contested",
            Self::Fragile => "fragile",
        }
    }

    /// Derive a finding's state from its review flags and confidence.
    /// Pure and total. Precedence, strongest disqualifier first:
    /// 1. a rejected review verdict → `Refuted`;
    /// 2. a contested/needs-revision verdict or the legacy `contested` flag →
    ///    `Contested`;
    /// 3. an accepted review verdict → `Established`, or `Fragile` when
    ///    confidence sits below `FRAGILE_CONFIDENCE` (accepted but thin);
    /// 4. everything else → `Open`.
    ///
    /// Verifier-gate status is deliberately excluded. Verification and review
    /// authority are orthogonal planes: a frozen verifier can establish that
    /// its method accepted the claim, but only a review verdict can change this
    /// product-facing review state.
    #[must_use]
    pub fn derive(flags: &crate::bundle::Flags, confidence: f64) -> Self {
        use crate::bundle::ReviewState;
        match flags.review_state {
            Some(ReviewState::Rejected) => return Self::Refuted,
            Some(ReviewState::Contested) | Some(ReviewState::NeedsRevision) => {
                return Self::Contested;
            }
            None if flags.contested => return Self::Contested,
            _ => {}
        }
        if flags.review_state == Some(ReviewState::Accepted) {
            if confidence < FRAGILE_CONFIDENCE {
                Self::Fragile
            } else {
                Self::Established
            }
        } else {
            Self::Open
        }
    }

    /// Explicit verdict-only alias retained for callers that use the review
    /// plane directly.
    #[must_use]
    pub fn of(flags: &crate::bundle::Flags, confidence: f64) -> Self {
        Self::derive(flags, confidence)
    }
}

/// A review-accepted finding below this confidence is
/// `Fragile` rather than `Established` — accepted, but resting on thin ground.
pub const FRAGILE_CONFIDENCE: f64 = 0.6;

/// A claim node: a finding plus the small slice of state the graph
/// queries report.
#[derive(Debug, Clone, Serialize)]
pub struct Node {
    pub id: String,
    pub label: String,
    pub contested: bool,
    pub gap: bool,
    pub confidence: f64,
    /// The product-facing finding state (§6), derived from the verdict +
    /// confidence at build time so the graph is self-describing for the
    /// map's state lens and the boundary query.
    pub state: FindingState,
}

/// A load-bearing dependency of a claim (GPT §11): a node every (or much) of
/// the claim's support funnels through. `weight` is how many support nodes
/// vanish if it is removed; `single_point_of_failure` is true when that is the
/// claim's entire support.
#[derive(Debug, Clone, Serialize)]
pub struct Dominator {
    pub node: String,
    pub label: String,
    /// The dominator's own finding state, when it is a finding in this graph.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<FindingState>,
    pub weight: usize,
    pub single_point_of_failure: bool,
}

/// Direction of a [`FrontierGraph::blast_radius`] query.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlastDirection {
    /// What the start rests on: its support, forward over `source → target`.
    Upstream,
    /// What rests on the start: its dependents, the impact if it moved.
    Downstream,
    /// Both sides.
    Both,
}

/// One node reached by a blast-radius traversal, with its hop distance.
#[derive(Debug, Clone, Serialize)]
pub struct ReachNode {
    pub id: String,
    pub label: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<FindingState>,
    pub distance: usize,
}

/// The dependency-impact neighborhood of a claim (memo §7.3, §18.8 — the
/// dependency-impact "blast radius"): what it rests on (`upstream`) and what
/// rests on it (`downstream`, the impact if it moved), plus the load-bearing
/// single points of failure on its support (the minimal-evidence-cut of §13).
/// A derived view over declared links: relations are candidates, not
/// adjudicated truth, and the impact is STRUCTURAL — that a result is in the
/// blast radius is not a claim that it is wrong.
#[derive(Debug, Clone, Serialize)]
pub struct BlastRadius {
    pub center: String,
    pub center_label: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub center_state: Option<FindingState>,
    pub kinds: Vec<&'static str>,
    pub summary: BlastSummary,
    pub single_points_of_failure: Vec<Dominator>,
    pub upstream: Vec<ReachNode>,
    pub downstream: Vec<ReachNode>,
}

/// Headline counts for a [`BlastRadius`].
#[derive(Debug, Clone, Serialize)]
pub struct BlastSummary {
    pub upstream: usize,
    pub downstream: usize,
    pub max_downstream_distance: usize,
    pub single_points_of_failure: usize,
}

impl BlastRadius {
    /// A stable JSON view, marked derived and structural.
    #[must_use]
    pub fn to_json(&self) -> serde_json::Value {
        let mut v = serde_json::to_value(self).unwrap_or(serde_json::Value::Null);
        if let serde_json::Value::Object(ref mut m) = v {
            m.insert("schema".into(), serde_json::json!("vela.blast_radius.v0.1"));
            m.insert(
                "claim_boundary".into(),
                serde_json::json!({
                    "graph_is_derived": true,
                    "edges_are_declared_links": true,
                    "relations_are_candidates_not_adjudicated": true,
                    "impact_is_structural_not_a_truth_claim": true,
                }),
            );
        }
        v
    }
}

/// `"num/den"` string for a `{0,1}` support coordinate (`"1/1"` present, `"0/1"`
/// absent) — matches the atlas's coordinate serialization so the two reads are
/// comparable byte-for-byte. The status layer is corner-valued (support is just
/// "the support set is non-empty"), so these are the only two coordinates.
fn coord_string(present: bool) -> String {
    if present {
        "1/1".to_string()
    } else {
        "0/1".to_string()
    }
}

/// The Belnap corner letter of a `(support, refutation)` `{0,1}` pair.
fn corner_letter(support: bool, refute: bool) -> char {
    use crate::status_provenance::BelnapStatus;
    match (support, refute) {
        (true, true) => BelnapStatus::Both,
        (true, false) => BelnapStatus::True,
        (false, true) => BelnapStatus::False,
        (false, false) => BelnapStatus::None,
    }
    .letter()
}

/// The graded status of the center finding: the canonical bilattice point
/// `(support kappa, refute kappa)` from `derive_status_provenance`, its conflict
/// degree `min(x, y)`, and the v1 Belnap corner it thresholds to.
#[derive(Debug, Clone, Serialize)]
pub struct GradedStatus {
    pub support_kappa: String,
    pub refute_kappa: String,
    pub conflict: String,
    pub belnap: char,
}

/// One dependent whose support kappa genuinely drops when the center's support
/// is retracted (the retraction theorem applied at the kappa level). A
/// structurally-reachable dependent with `delta_kappa = 0` has alternative
/// support and is omitted — the calculus prunes what reachability overcounts.
#[derive(Debug, Clone, Serialize)]
pub struct GradedImpact {
    pub id: String,
    pub label: String,
    pub kappa_before: String,
    pub kappa_after: String,
    pub delta_kappa: String,
    /// The center is a single point of failure for this dependent: retracting it
    /// leaves the dependent with no support (`kappa_after = 0`).
    pub support_killed: bool,
}

/// Headline counts for the graded cascade.
#[derive(Debug, Clone, Serialize)]
pub struct GradedBlastSummary {
    pub downstream_candidates: usize,
    pub weakened: usize,
    pub killed: usize,
    pub semiring: &'static str,
}

/// The calculus reading of a blast radius (memo §7.3, project_true_math_atlas
/// roadmap #4 — the licensed propagation). Wraps the structural [`BlastRadius`]
/// with the center's canonical graded status and the genuine downstream impact:
/// each dependent's support kappa is min-propagated along the dependency edges
/// (the Bottleneck semiring — "a chain is as strong as its weakest premise"),
/// computed before and after the center's support is retracted. `delta_kappa`
/// is the real impact.
#[derive(Debug, Clone, Serialize)]
pub struct GradedBlast {
    pub structural: BlastRadius,
    pub center_status: GradedStatus,
    pub impacted: Vec<GradedImpact>,
    pub summary: GradedBlastSummary,
}

impl GradedBlast {
    /// JSON: the structural view, extended with the graded reading and an honest
    /// boundary on what the calculus does and does not claim here.
    #[must_use]
    pub fn to_json(&self) -> serde_json::Value {
        let mut v = self.structural.to_json();
        if let serde_json::Value::Object(ref mut m) = v {
            m.insert(
                "schema".into(),
                serde_json::json!("vela.blast_radius.graded.v0.1"),
            );
            m.insert(
                "center_status".into(),
                serde_json::to_value(&self.center_status).unwrap_or_default(),
            );
            m.insert(
                "graded_impact".into(),
                serde_json::to_value(&self.impacted).unwrap_or_default(),
            );
            m.insert(
                "graded_summary".into(),
                serde_json::to_value(&self.summary).unwrap_or_default(),
            );
            m.insert(
                "graded_boundary".into(),
                serde_json::json!({
                    "support_kappa": "per-finding canonical support degree — kappa of the support polynomial (derive_status_provenance + the bilattice)",
                    "propagation": "Bottleneck semiring (max, min): a chain is as strong as its weakest premise — the kernel's licensed reading for dependency support",
                    "delta_kappa": "the drop in a dependent's support kappa when the center's support is retracted (kappa -> 0); the retraction theorem applied at the kappa level",
                    "pruning": "a structurally-reachable dependent with delta_kappa = 0 has alternative support and is NOT listed",
                }),
            );
        }
        v
    }
}

/// Memoized Bottleneck-semiring propagation of canonical support along the
/// dependency edges. On the corner-valued status layer support is `{0,1}`
/// ("the support set is non-empty"), so the Bottleneck `min` along a chain is
/// boolean AND — a dependent has propagated support iff itself and every
/// required premise do. `zeroed` is the retracted center (its support is `false`,
/// the retraction theorem's effect); pass `""` for the unperturbed pass.
struct PropCtx<'a> {
    graph: &'a FrontierGraph,
    project: &'a Project,
    kinds: &'a [EdgeKind],
    zeroed: &'a str,
    own: HashMap<String, bool>,
    memo: HashMap<String, bool>,
}

impl<'a> PropCtx<'a> {
    fn new(
        graph: &'a FrontierGraph,
        project: &'a Project,
        kinds: &'a [EdgeKind],
        zeroed: &'a str,
    ) -> Self {
        PropCtx {
            graph,
            project,
            kinds,
            zeroed,
            own: HashMap::new(),
            memo: HashMap::new(),
        }
    }

    /// The finding's OWN support (canonical, per-finding): `true` iff its support
    /// set is non-empty, or `false` if it is the retracted center. Memoized.
    fn own_support(&mut self, d: &str) -> bool {
        if d == self.zeroed {
            return false;
        }
        if let Some(v) = self.own.get(d) {
            return *v;
        }
        let k = !derive_status_provenance(&self.project.events, d)
            .support
            .is_empty();
        self.own.insert(d.to_string(), k);
        k
    }

    /// `prop(d) = own(d) AND (AND over d's dependency premises p of prop(p))`.
    /// The Bottleneck `min` on `{0,1}` support. Cycle-safe: a node already on the
    /// path contributes only its own support (no infinite descent).
    fn prop(&mut self, d: &str, on_path: &mut BTreeSet<String>) -> bool {
        if let Some(v) = self.memo.get(d) {
            return *v;
        }
        let own = self.own_support(d);
        if !on_path.insert(d.to_string()) {
            return own; // d is already on the current path: a cycle
        }
        let premises: Vec<String> = self
            .graph
            .edges
            .iter()
            .filter(|e| e.source == d && self.kinds.contains(&e.kind))
            .map(|e| e.target.clone())
            .collect();
        let mut k = own;
        for p in premises {
            k = k && self.prop(&p, on_path);
        }
        on_path.remove(d);
        self.memo.insert(d.to_string(), k);
        k
    }
}

/// The typed claim-level graph for one frontier (or one merged
/// multi-frontier Project).
#[derive(Debug, Clone, Default)]
pub struct FrontierGraph {
    nodes: BTreeMap<String, Node>,
    edges: Vec<Edge>,
}

impl FrontierGraph {
    /// Build the typed graph from a project's findings and links.
    /// Every finding becomes a node; every link whose type maps to a
    /// known [`EdgeKind`] becomes a typed edge.
    #[must_use]
    pub fn from_project(project: &Project) -> Self {
        let mut nodes = BTreeMap::new();
        for f in &project.findings {
            let label = f.assertion.text.chars().take(120).collect::<String>();
            nodes.insert(
                f.id.clone(),
                Node {
                    id: f.id.clone(),
                    label,
                    contested: f.flags.contested,
                    gap: f.flags.gap,
                    confidence: f.confidence.score,
                    state: FindingState::derive(&f.flags, f.confidence.score),
                },
            );
        }

        let mut edges = Vec::new();
        for f in &project.findings {
            for link in &f.links {
                let Some(kind) = EdgeKind::from_link_type(&link.link_type) else {
                    continue;
                };
                // P2 cross-frontier resolution: a `vf_X@vfr_Y` target
                // resolves to the bare `vf_X` node when that node is
                // present — which it is under `serve --frontiers <dir>`,
                // since the merge pulls every frontier's findings into
                // one Project. Content-addressed ids are globally unique
                // by content, so collapsing to the bare id is sound and
                // lets typed traversal compose across frontiers. A
                // target whose bare id is absent keeps its raw form and
                // is flagged out-of-frontier.
                let bare = crate::bundle::bare_finding_id(&link.target);
                let (target, target_in_frontier) = if nodes.contains_key(bare) {
                    (bare.to_string(), true)
                } else {
                    (link.target.clone(), false)
                };
                edges.push(Edge {
                    source: f.id.clone(),
                    target,
                    kind,
                    note: link.note.clone(),
                    target_in_frontier,
                });
            }
        }

        Self { nodes, edges }
    }

    /// Build a typed graph directly from an `(source, target, kind)` edge list,
    /// for a premise graph that is NOT a findings frontier — e.g. a Mathlib
    /// declaration-dependency graph (`from --uses--> to`, mapped to
    /// `DependsOn`). Every endpoint becomes a minimal node (label = id, neutral
    /// `Open` state, confidence 1.0). The κ-from-events graded cascade
    /// (`blast_radius_graded`) is NOT meaningful here (no verifier status and no
    /// events); use the structural `blast_radius` — over a CONJUNCTIVE premise
    /// graph (every used premise is required) that is also exact, since every
    /// transitive dependent is genuinely impacted with no alternative route.
    #[must_use]
    pub fn from_edges<I>(raw: I) -> Self
    where
        I: IntoIterator<Item = (String, String, EdgeKind)>,
    {
        let mut nodes: BTreeMap<String, Node> = BTreeMap::new();
        let mut edges = Vec::new();
        for (source, target, kind) in raw {
            for id in [&source, &target] {
                nodes.entry(id.clone()).or_insert_with(|| Node {
                    id: id.clone(),
                    label: id.clone(),
                    contested: false,
                    gap: false,
                    confidence: 1.0,
                    state: FindingState::Open,
                });
            }
            edges.push(Edge {
                source,
                target,
                kind,
                note: String::new(),
                target_in_frontier: true,
            });
        }
        Self { nodes, edges }
    }

    /// In-degree of every node (count of edges of `kinds` whose target is the
    /// node), sorted descending — the most-depended-on premises first. The
    /// highest-in-degree node is the highest-leverage retraction target.
    #[must_use]
    pub fn in_degree_ranked(&self, kinds: &[EdgeKind]) -> Vec<(String, usize)> {
        let mut deg: BTreeMap<String, usize> = BTreeMap::new();
        for e in &self.edges {
            if kinds.is_empty() || kinds.contains(&e.kind) {
                *deg.entry(e.target.clone()).or_default() += 1;
            }
        }
        let mut v: Vec<(String, usize)> = deg.into_iter().collect();
        v.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0)));
        v
    }

    /// Number of claim nodes.
    #[must_use]
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// Number of typed edges.
    #[must_use]
    pub fn edge_count(&self) -> usize {
        self.edges.len()
    }

    /// True iff the node exists in the graph.
    #[must_use]
    pub fn contains(&self, id: &str) -> bool {
        self.nodes.contains_key(id)
    }

    /// All edges of a given kind.
    pub fn edges_of_kind(&self, kind: EdgeKind) -> impl Iterator<Item = &Edge> {
        self.edges.iter().filter(move |e| e.kind == kind)
    }

    /// All typed edges, in build order. The read accessor the boundary
    /// query and the path-finder traverse over (they live in sibling
    /// modules and cannot see the private field).
    #[must_use]
    pub fn all_edges(&self) -> &[Edge] {
        &self.edges
    }

    /// One node by id, if present.
    #[must_use]
    pub fn node(&self, id: &str) -> Option<&Node> {
        self.nodes.get(id)
    }

    /// Every node, in stable id order.
    pub fn nodes(&self) -> impl Iterator<Item = &Node> {
        self.nodes.values()
    }

    /// Per-edge-kind counts, for summaries.
    #[must_use]
    pub fn edge_kind_counts(&self) -> BTreeMap<&'static str, usize> {
        let mut counts: BTreeMap<&'static str, usize> = BTreeMap::new();
        for e in &self.edges {
            *counts.entry(e.kind.as_str()).or_default() += 1;
        }
        counts
    }

    /// Unordered, deduplicated contradiction pairs. Because
    /// `contradicts` is conceptually symmetric but stored as a directed
    /// link, an `A→B` and a `B→A` contradiction collapse to one pair.
    /// Endpoints are sorted so the pair is stable.
    #[must_use]
    pub fn contradiction_pairs(&self) -> Vec<(String, String)> {
        let mut pairs = BTreeSet::new();
        for e in self.edges_of_kind(EdgeKind::Contradicts) {
            let (a, b) = if e.source <= e.target {
                (e.source.clone(), e.target.clone())
            } else {
                (e.target.clone(), e.source.clone())
            };
            pairs.insert((a, b));
        }
        pairs.into_iter().collect()
    }

    /// The transitive closure reachable from `start` following only
    /// edges of one kind (e.g. the full `improves` or `supersedes`
    /// lineage downstream of a result). Excludes `start` itself.
    #[must_use]
    pub fn closure_of_kind(&self, start: &str, kind: EdgeKind) -> BTreeSet<String> {
        let mut seen = BTreeSet::new();
        let mut stack = vec![start.to_string()];
        while let Some(node) = stack.pop() {
            for e in self
                .edges
                .iter()
                .filter(|e| e.kind == kind && e.source == node)
            {
                if seen.insert(e.target.clone()) {
                    stack.push(e.target.clone());
                }
            }
        }
        seen
    }

    /// Breadth-first exploration outward from `start`, treating edges
    /// as undirected for reach, up to `max_hops`. Returns each reached
    /// node's hop distance and the edges wholly inside the explored
    /// subgraph. This is the engine for multi-hop "deep" queries — the
    /// counterpart to single-hop neighbor lookups.
    #[must_use]
    pub fn explore(&self, start: &str, max_hops: usize) -> Exploration {
        if !self.nodes.contains_key(start) {
            return Exploration::default();
        }
        // Build undirected adjacency once (O(E)) so the BFS costs
        // O(reached + incident edges), not a full edge rescan per
        // frontier node per hop.
        let mut adj: HashMap<&str, Vec<&str>> = HashMap::new();
        for e in &self.edges {
            adj.entry(e.source.as_str()).or_default().push(&e.target);
            adj.entry(e.target.as_str()).or_default().push(&e.source);
        }
        let mut node_hops: BTreeMap<String, usize> = BTreeMap::new();
        node_hops.insert(start.to_string(), 0);
        let mut frontier = vec![start.to_string()];
        for hop in 1..=max_hops {
            let mut next = Vec::new();
            for n in &frontier {
                for &neighbor in adj.get(n.as_str()).into_iter().flatten() {
                    if !node_hops.contains_key(neighbor) {
                        node_hops.insert(neighbor.to_string(), hop);
                        next.push(neighbor.to_string());
                    }
                }
            }
            if next.is_empty() {
                break;
            }
            frontier = next;
        }
        let edges = self
            .edges
            .iter()
            .filter(|e| node_hops.contains_key(&e.source) && node_hops.contains_key(&e.target))
            .cloned()
            .collect();
        Exploration { node_hops, edges }
    }

    /// Look up a node's label (assertion snippet), if present.
    #[must_use]
    pub fn label_of(&self, id: &str) -> Option<&str> {
        self.nodes.get(id).map(|n| n.label.as_str())
    }

    /// Support-reachable nodes from `start` following only `kinds`, in stored
    /// `source → target` direction (the premises `start` rests on), optionally
    /// removing one node from the graph. Excludes `start` itself.
    fn support_reach(
        &self,
        start: &str,
        kinds: &[EdgeKind],
        remove: Option<&str>,
    ) -> BTreeSet<String> {
        let mut seen = BTreeSet::new();
        let mut stack = vec![start.to_string()];
        while let Some(node) = stack.pop() {
            for e in &self.edges {
                if e.source != node || !kinds.contains(&e.kind) {
                    continue;
                }
                if remove == Some(e.target.as_str()) {
                    continue;
                }
                if e.target.as_str() == start {
                    continue;
                }
                if seen.insert(e.target.clone()) {
                    stack.push(e.target.clone());
                }
            }
        }
        seen
    }

    /// The support-graph dominators of a claim (GPT §11): the load-bearing
    /// dependencies its support funnels through. A node `d` (≠ `z`) is a
    /// dominator with `weight` = how many of `z`'s support-reachable nodes
    /// vanish if `d` is removed (including `d` itself). A dominator whose
    /// weight equals the full support size is a single point of failure —
    /// every support path out of `z` runs through it. Sorted by weight
    /// descending, then id. Pure; recomputed on read.
    #[must_use]
    pub fn support_dominators(&self, z: &str, kinds: &[EdgeKind]) -> Vec<Dominator> {
        let full = self.support_reach(z, kinds, None);
        if full.is_empty() {
            return vec![];
        }
        let total = full.len();
        let mut doms: Vec<Dominator> = full
            .iter()
            .map(|d| {
                let without = self.support_reach(z, kinds, Some(d));
                let weight = full.iter().filter(|n| !without.contains(*n)).count();
                Dominator {
                    node: d.clone(),
                    label: self.label_of(d).unwrap_or("").to_string(),
                    state: self.nodes.get(d).map(|n| n.state),
                    weight,
                    single_point_of_failure: weight == total && total >= 2,
                }
            })
            .collect();
        doms.sort_by(|a, b| b.weight.cmp(&a.weight).then(a.node.cmp(&b.node)));
        doms
    }

    /// The default dependency edge kinds for a blast-radius query: the relations
    /// where the source rests on the target, so removing the target weakens the
    /// source. Lineage kinds (`Improves`/`Generalizes`/…) are excluded — they
    /// are not load-bearing dependency.
    pub const DEPENDENCY_KINDS: [EdgeKind; 4] = [
        EdgeKind::Supports,
        EdgeKind::DependsOn,
        EdgeKind::DerivedFrom,
        EdgeKind::Discharges,
    ];

    /// The REQUIRED-premise kinds: a claim's support is *bottlenecked* by these
    /// (if the premise fails, the claim weakens — the calculus's "weakest
    /// premise" reading). `Supports` is corroboration (an alternative, not a
    /// requirement), so it is NOT a bottleneck and is excluded from the graded
    /// min-propagation: losing one corroboration does not weaken a claim that
    /// still has another.
    pub const REQUIRED_PREMISE_KINDS: [EdgeKind; 3] = [
        EdgeKind::DependsOn,
        EdgeKind::DerivedFrom,
        EdgeKind::Discharges,
    ];

    /// Resolve a query to a node id: an exact id; else an id *prefix* (ids are
    /// addressed by prefix, like `deep_trace` — a substring match would let a
    /// bare number like "617" spuriously hit the hex tail of a content-addressed
    /// id); else the first finding (in id order) whose assertion contains the
    /// query, case-insensitively, so a problem number or snippet resolves.
    #[must_use]
    pub fn find_node(&self, query: &str) -> Option<String> {
        if self.nodes.contains_key(query) {
            return Some(query.to_string());
        }
        if let Some(n) = self.nodes.values().find(|n| n.id.starts_with(query)) {
            return Some(n.id.clone());
        }
        let q = query.to_lowercase();
        self.nodes
            .values()
            .find(|n| n.label.to_lowercase().contains(&q))
            .map(|n| n.id.clone())
    }

    /// Directed reachability from `start` with hop distance, following `kinds`
    /// either forward (`source → target`, the support `start` rests on) or
    /// reverse (`target → source`, the dependents that rest on `start`).
    /// Excludes `start`.
    fn reach_directed(
        &self,
        start: &str,
        kinds: &[EdgeKind],
        reverse: bool,
    ) -> BTreeMap<String, usize> {
        let mut dist: BTreeMap<String, usize> = BTreeMap::new();
        dist.insert(start.to_string(), 0);
        let mut frontier = vec![start.to_string()];
        let mut hop = 0usize;
        while !frontier.is_empty() {
            hop += 1;
            let mut next = Vec::new();
            for node in &frontier {
                for e in &self.edges {
                    if !kinds.contains(&e.kind) {
                        continue;
                    }
                    let (from, to) = if reverse {
                        (&e.target, &e.source)
                    } else {
                        (&e.source, &e.target)
                    };
                    if from != node {
                        continue;
                    }
                    if !dist.contains_key(to) {
                        dist.insert(to.clone(), hop);
                        next.push(to.clone());
                    }
                }
            }
            frontier = next;
        }
        dist.remove(start);
        dist
    }

    /// Build a sorted [`ReachNode`] list (by distance, then id) from a distance
    /// map produced by [`Self::reach_directed`].
    fn reach_nodes(&self, dist: &BTreeMap<String, usize>) -> Vec<ReachNode> {
        let mut v: Vec<ReachNode> = dist
            .iter()
            .map(|(id, &distance)| ReachNode {
                id: id.clone(),
                label: self.label_of(id).unwrap_or("").to_string(),
                state: self.nodes.get(id).map(|n| n.state),
                distance,
            })
            .collect();
        v.sort_by(|a, b| a.distance.cmp(&b.distance).then(a.id.cmp(&b.id)));
        v
    }

    /// The dependency-impact neighborhood of `start` (memo §7.3): `upstream`
    /// (what it rests on), `downstream` (what rests on it — the blast radius if
    /// it moved), and the single points of failure on its support. An empty
    /// `kinds` defaults to [`Self::DEPENDENCY_KINDS`]. Pure; recomputed on read.
    #[must_use]
    pub fn blast_radius(
        &self,
        start: &str,
        kinds: &[EdgeKind],
        direction: BlastDirection,
    ) -> BlastRadius {
        let kinds: Vec<EdgeKind> = if kinds.is_empty() {
            Self::DEPENDENCY_KINDS.to_vec()
        } else {
            kinds.to_vec()
        };
        let want_up = matches!(direction, BlastDirection::Upstream | BlastDirection::Both);
        let want_down = matches!(direction, BlastDirection::Downstream | BlastDirection::Both);

        let upstream = if want_up {
            self.reach_nodes(&self.reach_directed(start, &kinds, false))
        } else {
            Vec::new()
        };
        let downstream = if want_down {
            self.reach_nodes(&self.reach_directed(start, &kinds, true))
        } else {
            Vec::new()
        };
        let single_points_of_failure: Vec<Dominator> = if want_up {
            self.support_dominators(start, &kinds)
                .into_iter()
                .filter(|d| d.single_point_of_failure)
                .collect()
        } else {
            Vec::new()
        };
        let max_downstream_distance = downstream.iter().map(|n| n.distance).max().unwrap_or(0);
        BlastRadius {
            center: start.to_string(),
            center_label: self.label_of(start).unwrap_or("").to_string(),
            center_state: self.nodes.get(start).map(|n| n.state),
            kinds: kinds.iter().map(|k| k.as_str()).collect(),
            summary: BlastSummary {
                upstream: upstream.len(),
                downstream: downstream.len(),
                max_downstream_distance,
                single_points_of_failure: single_points_of_failure.len(),
            },
            single_points_of_failure,
            upstream,
            downstream,
        }
    }

    /// The calculus reading of the blast radius (the licensed propagation,
    /// project_true_math_atlas roadmap #4): the center's canonical graded status,
    /// and for each structural downstream dependent the drop in its support kappa
    /// when the center's support is retracted, min-propagated along the dependency
    /// edges (the Bottleneck semiring — a chain is as strong as its weakest
    /// premise). Needs `project` for the per-finding provenance (events).
    /// Dependents whose kappa does not drop are pruned: they have alternative
    /// support, so structural reachability overcounted them.
    #[must_use]
    pub fn blast_radius_graded(
        &self,
        project: &Project,
        start: &str,
        kinds: &[EdgeKind],
        direction: BlastDirection,
    ) -> GradedBlast {
        let structural = self.blast_radius(start, kinds, direction);
        let center_sp = derive_status_provenance(&project.events, start);
        let center_support = !center_sp.support.is_empty();
        let center_refute = !center_sp.refute.is_empty();
        let center_status = GradedStatus {
            support_kappa: coord_string(center_support),
            refute_kappa: coord_string(center_refute),
            // conflict = min(support, refute) on {0,1} = AND.
            conflict: coord_string(center_support && center_refute),
            belnap: corner_letter(center_support, center_refute),
        };

        // The cascade min-propagates over REQUIRED premises only (the structural
        // `kinds` govern reachability, but corroborating `supports` is not a
        // bottleneck). A dependent reachable only via `supports` therefore does
        // not lose support and is pruned.
        let mut before = PropCtx::new(self, project, &Self::REQUIRED_PREMISE_KINDS, "");
        let mut after = PropCtx::new(self, project, &Self::REQUIRED_PREMISE_KINDS, start);
        // `true` first so the support-losing dependents sort ahead; then id.
        let mut scored: Vec<(bool, GradedImpact)> = Vec::new();
        for node in &structural.downstream {
            let b = before.prop(&node.id, &mut BTreeSet::new());
            let a = after.prop(&node.id, &mut BTreeSet::new());
            // Support genuinely drops only when it was present and is now gone.
            if b && !a {
                scored.push((
                    true,
                    GradedImpact {
                        id: node.id.clone(),
                        label: node.label.clone(),
                        kappa_before: coord_string(b),
                        kappa_after: coord_string(a),
                        delta_kappa: coord_string(b && !a),
                        support_killed: !a,
                    },
                ));
            }
        }
        // strongest impact first, then id for stable ordering
        scored.sort_by(|x, y| y.0.cmp(&x.0).then(x.1.id.cmp(&y.1.id)));
        let killed = scored.iter().filter(|(_, g)| g.support_killed).count();
        let weakened = scored.len();
        let impacted: Vec<GradedImpact> = scored.into_iter().map(|(_, g)| g).collect();
        let summary = GradedBlastSummary {
            downstream_candidates: structural.downstream.len(),
            weakened,
            killed,
            semiring: "bottleneck",
        };
        GradedBlast {
            structural,
            center_status,
            impacted,
            summary,
        }
    }

    /// Serialize to a stable claim-level JSON view. This is a focused
    /// `vela.frontier_graph.claims.v0.1` artifact — deliberately
    /// distinct from the broad provenance `vela.frontier_graph.v0.1`
    /// export so the two are never confused. Always marked derived.
    #[must_use]
    pub fn to_json(&self) -> serde_json::Value {
        let nodes: Vec<&Node> = self.nodes.values().collect();
        serde_json::json!({
            "schema": "vela.frontier_graph.claims.v0.1",
            "summary": {
                "nodes": self.node_count(),
                "edges": self.edge_count(),
                "edge_kinds": self.edge_kind_counts(),
                "contradiction_pairs": self.contradiction_pairs().len(),
            },
            "nodes": nodes,
            "edges": self.edges,
            "claim_boundary": {
                "graph_is_derived": true,
                "edges_are_declared_links": true,
                "relations_are_candidates_not_adjudicated": true,
            },
        })
    }
}

/// Result of a multi-hop [`FrontierGraph::explore`]: each reached node
/// keyed to its hop distance, plus the edges inside the explored
/// subgraph.
#[derive(Debug, Clone, Default)]
pub struct Exploration {
    node_hops: BTreeMap<String, usize>,
    pub edges: Vec<Edge>,
}

impl Exploration {
    /// Total nodes reached (including the start node at hop 0).
    #[must_use]
    pub fn node_count(&self) -> usize {
        self.node_hops.len()
    }

    /// The greatest hop distance reached.
    #[must_use]
    pub fn max_hop(&self) -> usize {
        self.node_hops.values().copied().max().unwrap_or(0)
    }

    /// Sorted node ids at exactly `hop` distance from the start.
    #[must_use]
    pub fn nodes_at(&self, hop: usize) -> Vec<&str> {
        self.node_hops
            .iter()
            .filter(|(_, h)| **h == hop)
            .map(|(id, _)| id.as_str())
            .collect()
    }

    /// Per-edge-kind counts within the explored subgraph.
    #[must_use]
    pub fn edge_kind_counts(&self) -> BTreeMap<&'static str, usize> {
        let mut counts: BTreeMap<&'static str, usize> = BTreeMap::new();
        for e in &self.edges {
            *counts.entry(e.kind.as_str()).or_default() += 1;
        }
        counts
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::project::assemble;
    use crate::project::reverse_dep_index_tests::{link_to, synth_finding};

    fn link_typed(target: &str, link_type: &str) -> crate::bundle::Link {
        let mut link = link_to(target);
        link.link_type = link_type.into();
        link
    }

    #[test]
    fn review_verdict_matrix_is_independent_of_verifier_status() {
        use crate::bundle::{Flags, ReviewState};
        let cases = [
            (None, false, 0.9, FindingState::Open),
            (
                Some(ReviewState::Accepted),
                false,
                0.3,
                FindingState::Fragile,
            ),
            (
                Some(ReviewState::Accepted),
                false,
                0.9,
                FindingState::Established,
            ),
            (
                Some(ReviewState::Rejected),
                false,
                0.9,
                FindingState::Refuted,
            ),
            (
                Some(ReviewState::Contested),
                false,
                0.9,
                FindingState::Contested,
            ),
            (
                Some(ReviewState::NeedsRevision),
                false,
                0.9,
                FindingState::Contested,
            ),
            (None, true, 0.9, FindingState::Contested),
        ];

        for (review_state, contested, confidence, expected) in cases {
            let flags = Flags {
                review_state,
                contested,
                ..Default::default()
            };
            assert_eq!(FindingState::derive(&flags, confidence), expected);
        }
    }

    #[test]
    fn verified_gate_does_not_establish_an_unreviewed_graph_node() {
        use crate::verifier_attachment::{GateStatus, claim_digest, derive_gate_status};

        let finding = synth_finding(0, vec![]);
        let digest = claim_digest(&finding.assertion.text);
        let attachments = crate::test_support::verified_attachment_pair(&finding);
        assert_eq!(
            derive_gate_status(&digest, &attachments).status,
            GateStatus::Verified,
            "the direct verifier gate remains fully functional"
        );

        let finding_id = finding.id.clone();
        let mut project = assemble("review-only-state", vec![], 0, 0, "test");
        project.findings = vec![finding];
        project.verifier_attachments = attachments;
        let graph = FrontierGraph::from_project(&project);
        assert_eq!(
            graph.node(&finding_id).expect("finding node").state,
            FindingState::Open,
            "verification must not be folded into the review-verdict state"
        );
    }

    #[test]
    fn from_edges_builds_a_premise_graph_and_blasts_downstream() {
        // A tiny conjunctive premise graph: thm1 and thm2 both use lemma; lemma
        // uses prim. Retract `prim` -> impacts lemma, thm1, thm2 (all transitive
        // dependents), at depths 1, 2, 2.
        let edges = vec![
            ("thm1".to_string(), "lemma".to_string(), EdgeKind::DependsOn),
            ("thm2".to_string(), "lemma".to_string(), EdgeKind::DependsOn),
            ("lemma".to_string(), "prim".to_string(), EdgeKind::DependsOn),
        ];
        let g = FrontierGraph::from_edges(edges);
        assert_eq!(g.node_count(), 4);
        assert_eq!(g.edge_count(), 3);
        // in-degree: lemma is referenced twice, prim once.
        let ranked = g.in_degree_ranked(&[EdgeKind::DependsOn]);
        assert_eq!(ranked[0], ("lemma".to_string(), 2));
        // downstream of `prim` (the dependents that rest on it) = lemma, thm1, thm2.
        let blast = g.blast_radius("prim", &[EdgeKind::DependsOn], BlastDirection::Downstream);
        assert_eq!(
            blast.summary.downstream, 3,
            "all transitive dependents impacted"
        );
        assert_eq!(blast.summary.max_downstream_distance, 2);
        let ids: Vec<&str> = blast.downstream.iter().map(|n| n.id.as_str()).collect();
        assert!(ids.contains(&"lemma") && ids.contains(&"thm1") && ids.contains(&"thm2"));
        // a leaf (thm1) has no dependents.
        let leaf = g.blast_radius("thm1", &[EdgeKind::DependsOn], BlastDirection::Downstream);
        assert_eq!(leaf.summary.downstream, 0);
    }

    #[test]
    fn every_valid_link_type_maps_to_an_edge_kind() {
        // Drift guard: a link type accepted by validation but unmapped
        // here would be silently dropped from the typed graph.
        for lt in crate::bundle::VALID_LINK_TYPES {
            assert!(
                EdgeKind::from_link_type(lt).is_some(),
                "VALID_LINK_TYPES has '{lt}' with no EdgeKind mapping"
            );
        }
    }

    #[test]
    fn maps_all_t7_link_types_to_edge_kinds() {
        for (s, k) in [
            ("supports", EdgeKind::Supports),
            ("contradicts", EdgeKind::Contradicts),
            ("depends", EdgeKind::DependsOn),
            ("synthesized_from", EdgeKind::DerivedFrom),
            ("derived_from", EdgeKind::DerivedFrom),
            ("improves", EdgeKind::Improves),
            ("generalizes", EdgeKind::Generalizes),
            ("specializes", EdgeKind::Specializes),
            ("supersedes", EdgeKind::Supersedes),
            ("extends", EdgeKind::Extends),
            ("replicates", EdgeKind::Replicates),
        ] {
            assert_eq!(EdgeKind::from_link_type(s), Some(k));
        }
        assert_eq!(EdgeKind::from_link_type("nonsense"), None);
    }

    #[test]
    fn builds_typed_nodes_and_edges_from_project() {
        let base = synth_finding(0, vec![]);
        let a = synth_finding(1, vec![link_typed(&base.id, "improves")]);
        let b = synth_finding(2, vec![link_typed(&a.id, "supersedes")]);
        let (base_id, a_id, b_id) = (base.id.clone(), a.id.clone(), b.id.clone());

        let mut project = assemble("fg", vec![], 0, 0, "test");
        project.findings = vec![base, a, b];

        let g = FrontierGraph::from_project(&project);
        assert_eq!(g.node_count(), 3);
        assert_eq!(g.edge_count(), 2);
        assert!(g.contains(&base_id));

        let improves: Vec<&Edge> = g.edges_of_kind(EdgeKind::Improves).collect();
        assert_eq!(improves.len(), 1);
        assert_eq!(improves[0].source, a_id);
        assert_eq!(improves[0].target, base_id);
        assert!(improves[0].target_in_frontier);

        // a improves base, b supersedes a — closure of `improves` from
        // `a` reaches base; supersedes is a different kind.
        assert!(
            g.closure_of_kind(&a_id, EdgeKind::Improves)
                .contains(&base_id)
        );
        assert!(
            g.closure_of_kind(&b_id, EdgeKind::Supersedes)
                .contains(&a_id)
        );
    }

    #[test]
    fn explore_walks_multiple_hops_with_distances() {
        let base = synth_finding(0, vec![]);
        let a = synth_finding(1, vec![link_typed(&base.id, "improves")]);
        let b = synth_finding(2, vec![link_typed(&a.id, "supersedes")]);
        let (base_id, a_id, b_id) = (base.id.clone(), a.id.clone(), b.id.clone());
        let mut project = assemble("explore", vec![], 0, 0, "test");
        project.findings = vec![base, a, b];

        let g = FrontierGraph::from_project(&project);
        let ex = g.explore(&base_id, 2);
        assert_eq!(ex.node_count(), 3);
        assert_eq!(ex.node_hops[&base_id], 0);
        assert_eq!(ex.node_hops[&a_id], 1);
        assert_eq!(ex.node_hops[&b_id], 2);
        assert_eq!(ex.max_hop(), 2);
        assert_eq!(ex.nodes_at(1), vec![a_id.as_str()]);

        // Bounded by max_hops: depth 1 reaches only the immediate neighbor.
        assert_eq!(g.explore(&base_id, 1).node_count(), 2);
    }

    #[test]
    fn support_dominators_find_the_single_point_of_failure() {
        // z depends_on a depends_on b: every support path out of z funnels
        // through a, so a is a single point of failure (weight = full = 2);
        // b is a leaf (weight 1).
        let b = synth_finding(0, vec![]);
        let a = synth_finding(1, vec![link_typed(&b.id, "depends")]);
        let z = synth_finding(2, vec![link_typed(&a.id, "depends")]);
        let (a_id, b_id, z_id) = (a.id.clone(), b.id.clone(), z.id.clone());
        let mut project = assemble("dom", vec![], 0, 0, "test");
        project.findings = vec![b, a, z];

        let g = FrontierGraph::from_project(&project);
        let doms = g.support_dominators(&z_id, &[EdgeKind::DependsOn]);
        // a and b are both reachable; a dominates everything.
        let a_dom = doms.iter().find(|d| d.node == a_id).unwrap();
        let b_dom = doms.iter().find(|d| d.node == b_id).unwrap();
        assert_eq!(a_dom.weight, 2);
        assert!(a_dom.single_point_of_failure);
        assert_eq!(b_dom.weight, 1);
        assert!(!b_dom.single_point_of_failure);
        // top dominator is `a`.
        assert_eq!(doms.first().unwrap().node, a_id);
    }

    #[test]
    fn support_dominators_empty_for_unsupported_claim() {
        let z = synth_finding(0, vec![]);
        let z_id = z.id.clone();
        let mut project = assemble("dom2", vec![], 0, 0, "test");
        project.findings = vec![z];
        let g = FrontierGraph::from_project(&project);
        assert!(
            g.support_dominators(&z_id, &[EdgeKind::DependsOn])
                .is_empty()
        );
    }

    #[test]
    fn blast_radius_splits_upstream_and_downstream() {
        // z depends_on a depends_on b (the same chain as the dominator test).
        let b = synth_finding(0, vec![]);
        let a = synth_finding(1, vec![link_typed(&b.id, "depends")]);
        let z = synth_finding(2, vec![link_typed(&a.id, "depends")]);
        let (a_id, b_id, z_id) = (a.id.clone(), b.id.clone(), z.id.clone());
        let mut project = assemble("blast", vec![], 0, 0, "test");
        project.findings = vec![b, a, z];
        let g = FrontierGraph::from_project(&project);

        // z rests on a (hop 1) and b (hop 2); nothing rests on z.
        let from_z = g.blast_radius(&z_id, &[EdgeKind::DependsOn], BlastDirection::Both);
        assert_eq!(from_z.summary.upstream, 2);
        assert_eq!(from_z.summary.downstream, 0);
        let up_ids: Vec<&str> = from_z.upstream.iter().map(|n| n.id.as_str()).collect();
        assert_eq!(up_ids, vec![a_id.as_str(), b_id.as_str()]); // sorted by distance
        assert_eq!(from_z.upstream[0].distance, 1);
        assert_eq!(from_z.upstream[1].distance, 2);
        // a is the single point of failure on z's support.
        assert_eq!(from_z.single_points_of_failure.len(), 1);
        assert_eq!(from_z.single_points_of_failure[0].node, a_id);

        // b is rested on BY a (hop 1) and z (hop 2); it rests on nothing.
        let from_b = g.blast_radius(&b_id, &[EdgeKind::DependsOn], BlastDirection::Both);
        assert_eq!(from_b.summary.upstream, 0);
        assert_eq!(from_b.summary.downstream, 2);
        assert_eq!(from_b.summary.max_downstream_distance, 2);
        let down_ids: Vec<&str> = from_b.downstream.iter().map(|n| n.id.as_str()).collect();
        assert_eq!(down_ids, vec![a_id.as_str(), z_id.as_str()]);

        // direction filters: downstream-only yields no upstream work.
        let down_only = g.blast_radius(&b_id, &[EdgeKind::DependsOn], BlastDirection::Downstream);
        assert!(down_only.upstream.is_empty());
        assert_eq!(down_only.downstream.len(), 2);
    }

    #[test]
    fn blast_radius_graded_prunes_unweakened_dependents() {
        use crate::events::{EVENT_SCHEMA, NULL_HASH, StateActor, StateEvent, StateTarget};
        let asserted = |idx: usize, target: &str| StateEvent {
            schema: EVENT_SCHEMA.to_string(),
            id: format!("vev_g_{idx:04}"),
            kind: "finding.asserted".into(),
            target: StateTarget {
                r#type: "finding".to_string(),
                id: target.to_string(),
            },
            actor: StateActor {
                id: "reviewer:test".to_string(),
                r#type: "human".to_string(),
            },
            timestamp: format!("2026-06-17T00:00:{:02}Z", idx % 60),
            reason: "graded blast test".to_string(),
            before_hash: NULL_HASH.to_string(),
            after_hash: NULL_HASH.to_string(),
            payload: serde_json::json!({}),
            caveats: vec![],
            signature: None,
        };

        // z depends_on a depends_on b (a required chain); s corroborates b via
        // `supports` only (not a required premise). All four are asserted, so
        // each has own support kappa = 1.
        let b = synth_finding(0, vec![]);
        let a = synth_finding(1, vec![link_typed(&b.id, "depends")]);
        let z = synth_finding(2, vec![link_typed(&a.id, "depends")]);
        let s = synth_finding(3, vec![link_typed(&b.id, "supports")]);
        let (a_id, z_id, s_id, b_id) = (a.id.clone(), z.id.clone(), s.id.clone(), b.id.clone());
        let mut project = assemble("graded", vec![], 0, 0, "test");
        project.events = vec![
            asserted(0, &b_id),
            asserted(1, &a_id),
            asserted(2, &z_id),
            asserted(3, &s_id),
        ];
        project.findings = vec![b, a, z, s];
        let g = FrontierGraph::from_project(&project);

        let gb = g.blast_radius_graded(&project, &b_id, &[], BlastDirection::Downstream);

        // b is supported: center reads T with support kappa 1.
        assert_eq!(gb.center_status.belnap, 'T');
        assert_eq!(gb.center_status.support_kappa, "1/1");

        // Structural downstream of b is {a, z, s}; the graded impact is {a, z}
        // only — s corroborates via `supports`, not a required premise, so its
        // kappa does not drop (Δκ = 0) and it is pruned.
        let ids: Vec<&str> = gb.impacted.iter().map(|i| i.id.as_str()).collect();
        assert!(ids.contains(&a_id.as_str()) && ids.contains(&z_id.as_str()));
        assert!(
            !ids.contains(&s_id.as_str()),
            "a supports-only corroborator must be pruned from the graded impact"
        );
        assert_eq!(gb.summary.downstream_candidates, 3);
        assert_eq!(gb.summary.weakened, 2);
        assert_eq!(gb.summary.killed, 2);
        for imp in &gb.impacted {
            assert_eq!(imp.kappa_before, "1/1");
            assert_eq!(imp.kappa_after, "0/1");
            assert_eq!(imp.delta_kappa, "1/1");
            assert!(imp.support_killed);
        }
    }

    #[test]
    fn contradiction_pairs_dedupe_symmetrically() {
        let x = synth_finding(0, vec![]);
        let y = synth_finding(1, vec![link_typed(&x.id, "contradicts")]);
        // x also contradicts y (the reverse direction): one pair, not two.
        let mut x = x;
        x.links.push(link_typed(&y.id, "contradicts"));
        let (x_id, y_id) = (x.id.clone(), y.id.clone());

        let mut project = assemble("fg-contra", vec![], 0, 0, "test");
        project.findings = vec![x, y];

        let g = FrontierGraph::from_project(&project);
        let pairs = g.contradiction_pairs();
        assert_eq!(pairs.len(), 1);
        let expected = if x_id <= y_id {
            (x_id, y_id)
        } else {
            (y_id, x_id)
        };
        assert_eq!(pairs[0], expected);
    }

    #[test]
    fn cross_frontier_target_is_flagged_out_of_frontier() {
        let f = synth_finding(
            0,
            vec![link_typed("vf_abcdef0123456789@vfr_remote", "supports")],
        );
        let mut project = assemble("fg-xf", vec![], 0, 0, "test");
        project.findings = vec![f];

        let g = FrontierGraph::from_project(&project);
        let edge = g.edges_of_kind(EdgeKind::Supports).next().unwrap();
        assert!(!edge.target_in_frontier);
    }

    #[test]
    fn cross_frontier_link_resolves_in_merged_project() {
        // Simulates `serve --frontiers <dir>`: the remote target's
        // finding is present in the merged Project, so a `@vfr_…` link
        // resolves to the bare node and composes for traversal (P2).
        let remote = synth_finding(0, vec![]);
        let cross_target = format!("{}@vfr_remote", remote.id);
        let local = synth_finding(1, vec![link_typed(&cross_target, "depends")]);
        let (remote_id, local_id) = (remote.id.clone(), local.id.clone());

        let mut project = assemble("fg-merge", vec![], 0, 0, "test");
        project.findings = vec![remote, local];

        let g = FrontierGraph::from_project(&project);
        let edge = g.edges_of_kind(EdgeKind::DependsOn).next().unwrap();
        assert!(edge.target_in_frontier, "bare id present → resolves");
        assert_eq!(edge.target, remote_id, "target rewritten to bare id");
        // Traversal now composes across the former frontier boundary.
        assert!(
            g.closure_of_kind(&local_id, EdgeKind::DependsOn)
                .contains(&remote_id)
        );
    }

    #[test]
    fn to_json_is_marked_derived_with_claim_boundary() {
        let project = assemble("fg-json", vec![], 0, 0, "test");
        let g = FrontierGraph::from_project(&project);
        let v = g.to_json();
        assert_eq!(v["schema"], "vela.frontier_graph.claims.v0.1");
        assert_eq!(v["claim_boundary"]["graph_is_derived"], true);
        assert_eq!(
            v["claim_boundary"]["relations_are_candidates_not_adjudicated"],
            true
        );
    }
}