aicx 0.10.0

Context search and retrieval for AI-agent session history — find relevant past events, recover user intent, identify unfinished deliverables, and spot claims never backed by results
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
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
//! Lane 2-5 schema anchor for the vc-intents truth pipeline.
//!
//! These are the stable, serializable record shapes that the downstream lanes
//! converge on (MASTER Phase 2 §3 "Define stable JSON schemas"). Lane 1 (human
//! intent) already exists as [`super::IntentRecord`]; this module adds the four
//! remaining lanes as the single anchor every lane stage must agree on:
//!
//! - Lane 2 — Agent Claim ([`ClaimRecord`]): what an agent asserted was done.
//! - Lane 3 — Evidence / Result ([`ResultRecord`]): what evidence proves.
//! - Lane 4 — Contract Fracture ([`ContractFracture`]): doc/contract vs runtime.
//! - Lane 5 — Clarify ([`ClarifyQuestion`]): a bounded human decision prompt.
//!
//! Doctrine carried in the types themselves: a claim is never a result
//! (`verification_status` defaults to `Unverified`); a result needs evidence
//! pointers; clarify asks decisions, not discoverable facts.
//!
//! Vibecrafted with AI Agents by Vetcoders (c)2026 Vetcoders

use serde::{Deserialize, Serialize};
use std::path::Path;

// ── Export envelope (P0 temporal contract) ───────────────────────

/// Earliest/latest source-message timestamps covered by an export, RFC3339.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct TimeCoverage {
    pub earliest: String,
    pub latest: String,
}

/// Machine-readable export envelope every lane export is wrapped in (MASTER
/// "Required Output Contracts"). Carries the full temporal contract so a
/// downstream agent can always answer "when is this from?" — absolute
/// `generated_at` (with year), source time coverage, explicit timezone
/// assumptions, and warnings whenever any timestamp is partial or inferred.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct LaneExport<T: Serialize> {
    /// Schema identifier+version for this envelope, e.g. `aicx.lanes.v1`.
    pub schema_version: String,
    /// Absolute extraction timestamp, RFC3339 with year.
    pub generated_at: String,
    pub project: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repo: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_time_coverage: Option<TimeCoverage>,
    pub source_files: Vec<String>,
    /// Which lane produced this export: `claims` | `results` | `clarify`.
    pub extraction_mode: String,
    /// Role filter applied at the source: `agent_only` | `user_only` | `all`.
    pub role_filter: String,
    /// e.g. "all timestamps normalized to UTC (RFC3339) at source".
    pub timezone_assumptions: String,
    /// Non-empty whenever temporal truth is degraded (partial/inferred times,
    /// missing years, inferred session order). Never silently full.
    pub warnings: Vec<String>,
    pub payload: T,
}

pub const LANE_SCHEMA_VERSION: &str = "aicx.lanes.v1";
pub const UTC_TIMEZONE_ASSUMPTION: &str =
    "all timestamps normalized to UTC (RFC3339, full date+year) at source";

// ── Lane 2 — Agent Claim ─────────────────────────────────────────

/// A claim is an agent-originated assertion that something was done. Claims are
/// audit targets, never truth — every claim needs evidence or stays unverified.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ClaimRecord {
    pub id: String,
    pub project: String,
    pub source_session: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_agent: Option<String>,
    /// Always `assistant` / `agent` — a claim cannot originate from user text.
    pub source_role: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_span: Option<String>,
    pub claim_text: String,
    pub claim_type: ClaimType,
    pub claimed_status: String,
    /// Absolute source-message timestamp (RFC3339, full date+year), when known.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<String>,
    /// True when the source timestamp was inferred (fallback) or absent —
    /// partial temporal truth is marked, never silently presented as full.
    pub timestamp_partial: bool,
    /// Absolute extraction timestamp (RFC3339).
    pub extracted_at: String,
    pub claimed_files: Vec<String>,
    pub claimed_commands: Vec<String>,
    pub claimed_artifacts: Vec<String>,
    pub related_intents: Vec<String>,
    pub evidence_refs: Vec<String>,
    pub verification_status: VerificationStatus,
    pub risk_flags: Vec<String>,
}

/// Claim taxonomy (MASTER §305). Note `Green`/`ReadyToPush`/`Shippable`/
/// `NoBlockers` are the highest-risk claims — the "all green" / "production
/// ready" applause verdict — and must never be promoted to a Result without
/// Lane 3 evidence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ClaimType {
    Implemented,
    Fixed,
    Tested,
    Verified,
    Migrated,
    Installed,
    Documented,
    Green,
    ReadyToPush,
    Shippable,
    NoBlockers,
    Blocked,
}

impl ClaimType {
    /// snake_case label, matching the serde representation.
    pub fn label(self) -> &'static str {
        match self {
            Self::Implemented => "implemented",
            Self::Fixed => "fixed",
            Self::Tested => "tested",
            Self::Verified => "verified",
            Self::Migrated => "migrated",
            Self::Installed => "installed",
            Self::Documented => "documented",
            Self::Green => "green",
            Self::ReadyToPush => "ready_to_push",
            Self::Shippable => "shippable",
            Self::NoBlockers => "no_blockers",
            Self::Blocked => "blocked",
        }
    }

    /// The applause claims ("production ready", "all green") that must never
    /// be promoted to a Result without Lane 3 evidence. `Green` is included:
    /// "all green" is the classic evidence-free applause verdict.
    pub fn is_high_risk(self) -> bool {
        matches!(
            self,
            Self::Green | Self::ReadyToPush | Self::Shippable | Self::NoBlockers
        )
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum VerificationStatus {
    Unverified,
    Verified,
    Contradicted,
    Partial,
    Stale,
}

impl Default for VerificationStatus {
    /// A fresh claim is unverified until Lane 3 proves otherwise.
    fn default() -> Self {
        Self::Unverified
    }
}

// ── Lane 3 — Evidence / Result ───────────────────────────────────

/// A result exists only when backed by evidence (passing command, test result,
/// committed file, observable behavior...). No evidence means no result — it
/// stays a [`ClaimRecord`]. Named `ResultRecord` to avoid shadowing `std::Result`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ResultRecord {
    pub id: String,
    pub project: String,
    pub evidence_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub command: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exit_status: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub artifact_path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub observed_output_excerpt: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<String>,
    pub related_claims: Vec<String>,
    pub related_intents: Vec<String>,
    pub result_status: ResultStatus,
    pub confidence: u8,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reproducibility_notes: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ResultStatus {
    Pass,
    Fail,
    Partial,
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EvidenceKind {
    Commit,
    TestRun,
    RuntimeProbe,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EvidenceRecord {
    pub id: String,
    pub kind: EvidenceKind,
    pub excerpt: String,
    pub observed_at: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub anchor: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub claim_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub files_touched: Option<Vec<String>>,
}

// ── Lane 4 — Contract Fracture ───────────────────────────────────

/// A mismatch between a promised surface (docs/contract) and the runtime
/// surface: docs promise `spotlight.md` but runtime never emits it; a canonical
/// contract exists only untracked; tests are green but the promised behavior is
/// absent. A fracture carries the decision it forces, not just the gap.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ContractFracture {
    /// Stable id of the [`ClaimRecord`] this fracture was detected on — the
    /// machine-readable link Lane 5 uses for `linked_claims`.
    pub claim_id: String,
    /// Human-readable provenance ("agent claim ... (session ...)"); display
    /// only, never used as a join key.
    pub contract_source: String,
    pub promised_surface: String,
    pub runtime_surface: String,
    pub evidence: Vec<String>,
    pub severity: FractureSeverity,
    /// Candidate resolutions, typically A/B/C (e.g. implement runtime to match
    /// contract, or reduce contract to current runtime).
    pub options: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recommended_clarify_question: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub linked_intents: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub linked_claims: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub linked_results: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FractureSeverity {
    Low,
    Medium,
    High,
    Critical,
}

// ── Lane 5 — Clarify ─────────────────────────────────────────────

/// A bounded human decision prompt, generated only AFTER intents, claims, and
/// results are known. Clarify asks decisions (keep contract or reduce it? ship
/// with known gap or keep hardening?), never discoverable facts (where is the
/// file? did the test pass?). Always carries a default recommendation so a
/// non-dev operator can answer "I trust you" instead of guessing.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClarifyQuestion {
    pub decision_id: String,
    /// The decision question itself — asks what to decide, never what to look up.
    pub question: String,
    pub why_now: String,
    pub known_facts: Vec<String>,
    /// Decision options, preferably A/B/C.
    pub options: Vec<String>,
    pub default_recommendation: String,
    pub cost_of_not_deciding: String,
    pub linked_intents: Vec<String>,
    pub linked_claims: Vec<String>,
    pub linked_results: Vec<String>,
}

// ── Lane 2 classification primitive ──────────────────────────────

/// Lowercased word tokens, split on non-alphanumeric boundaries. Unicode
/// letters (Polish diacritics included) count as word characters, so
/// "naprawiłem" survives as one token.
fn claim_tokens(text: &str) -> Vec<String> {
    text.split(|c: char| !c.is_alphanumeric())
        .filter(|t| !t.is_empty())
        .map(str::to_lowercase)
        .collect()
}

/// True when `phrase` (space-separated lowercase words) appears in `tokens`
/// as a contiguous token sequence. Token-boundary matching, never substring:
/// "incomplete" does not contain the token "complete".
fn contains_phrase(tokens: &[String], phrase: &str) -> bool {
    let needle: Vec<&str> = phrase.split_whitespace().collect();
    if needle.is_empty() || needle.len() > tokens.len() {
        return false;
    }
    tokens
        .windows(needle.len())
        .any(|w| w.iter().zip(needle.iter()).all(|(t, n)| t == n))
}

/// Classify a claim sentence into its [`ClaimType`] by surface claim-language.
///
/// Case-insensitive token-boundary phrase match in priority order (most
/// specific first): "no blockers" must not be misread as `Blocked`, and
/// "ready to ship" must not be swallowed by a generic ship/done marker.
/// Matching is on word boundaries, never raw substrings — "incomplete" is not
/// `complete`, "abandoned" is not `done`, "greenfield" is not `green`.
/// Returns `None` when no claim marker is present — absence is NOT
/// `Implemented` by default; a claim must actually claim something.
///
/// Markers cover English and Polish (the repo deliberately supports PL claim
/// language, mirroring `TYPED_DIRECTIVE_HEAD_MARKERS`); Polish needles are
/// listed both with and without diacritics ("naprawiłem"/"naprawilem").
///
/// `Green` / `ReadyToPush` / `Shippable` / `NoBlockers` are the highest-risk
/// claims (the "production ready" / "all green" applause verdict);
/// classification only labels them — Lane 3 must still demand evidence before
/// any of them becomes a Result.
pub fn classify_claim(text: &str) -> Option<ClaimType> {
    let tokens = claim_tokens(text);
    if tokens.is_empty() {
        return None;
    }
    // (needle, type) — list order IS precedence; first matching phrase wins.
    const RULES: &[(&str, ClaimType)] = &[
        ("no blocker", ClaimType::NoBlockers),
        ("no blockers", ClaimType::NoBlockers),
        ("zero blocker", ClaimType::NoBlockers),
        ("zero blockers", ClaimType::NoBlockers),
        ("bez blokerów", ClaimType::NoBlockers),
        ("bez blokerow", ClaimType::NoBlockers),
        ("blocked", ClaimType::Blocked),
        ("blocker", ClaimType::Blocked),
        ("blockers", ClaimType::Blocked),
        ("zablokowane", ClaimType::Blocked),
        ("production ready", ClaimType::ReadyToPush),
        ("ready to push", ClaimType::ReadyToPush),
        ("ready to ship", ClaimType::ReadyToPush),
        ("ready to merge", ClaimType::ReadyToPush),
        ("gotowe do push", ClaimType::ReadyToPush),
        ("gotowe do pusha", ClaimType::ReadyToPush),
        ("można pushować", ClaimType::ReadyToPush),
        ("mozna pushowac", ClaimType::ReadyToPush),
        ("shippable", ClaimType::Shippable),
        ("ship it", ClaimType::Shippable),
        ("migration complete", ClaimType::Migrated),
        ("migrated", ClaimType::Migrated),
        ("installed", ClaimType::Installed),
        ("wdrożone", ClaimType::Implemented),
        ("wdrozone", ClaimType::Implemented),
        ("docs updated", ClaimType::Documented),
        ("documented", ClaimType::Documented),
        ("verified", ClaimType::Verified),
        ("działa", ClaimType::Verified),
        ("dziala", ClaimType::Verified),
        ("all green", ClaimType::Green),
        ("tests green", ClaimType::Green),
        ("green", ClaimType::Green),
        ("wszystko zielone", ClaimType::Green),
        ("testy zielone", ClaimType::Green),
        ("zielone", ClaimType::Green),
        ("tests pass", ClaimType::Tested),
        ("passing", ClaimType::Tested),
        ("tested", ClaimType::Tested),
        ("przetestowane", ClaimType::Tested),
        ("testy przechodzą", ClaimType::Tested),
        ("testy przechodza", ClaimType::Tested),
        ("fixed", ClaimType::Fixed),
        ("naprawione", ClaimType::Fixed),
        ("naprawiłem", ClaimType::Fixed),
        ("naprawilem", ClaimType::Fixed),
        ("implemented", ClaimType::Implemented),
        ("shipped", ClaimType::Implemented),
        ("complete", ClaimType::Implemented),
        ("completed", ClaimType::Implemented),
        ("done", ClaimType::Implemented),
        ("zrobione", ClaimType::Implemented),
        ("gotowe", ClaimType::Implemented),
    ];
    RULES
        .iter()
        .find(|(needle, _)| contains_phrase(&tokens, needle))
        .map(|(_, kind)| *kind)
}

/// A minimal source row for Lane 2 claim extraction: one message with its role
/// and provenance. Decoupled from any store/session reader so the pure transform
/// is testable without a corpus.
#[derive(Debug, Clone)]
pub struct ClaimSource {
    pub role: String,
    pub text: String,
    pub project: String,
    pub session_id: String,
    pub agent: Option<String>,
    pub source_ref: String,
    /// Absolute source-message timestamp (RFC3339), when known.
    pub timestamp: Option<String>,
    /// True when the timestamp came from a fallback (previous line, file
    /// mtime, extraction time) rather than the message itself.
    pub timestamp_partial: bool,
}

/// True when `role` names an agent-originated row. Lane 2 doctrine: claims
/// are agent-originated, so user/system/tool/developer rows never become
/// claim sources. Public as THE single role predicate shared by the source
/// build in the CLI (`load_session_claims`) and the in-lane re-guard in
/// [`extract_claims`] — no hand-synced duplicates.
pub fn is_agent_role(role: &str) -> bool {
    matches!(
        role.to_lowercase().as_str(),
        "assistant" | "agent" | "model" | "gemini"
    )
}

/// True when `role` names a human/operator-originated row. Lane 1 doctrine:
/// intents belong to humans — a strict allowlist (not merely "not an agent"),
/// so tool/system/unknown rows never enter the intent lane.
pub fn is_user_role(role: &str) -> bool {
    matches!(role.to_lowercase().as_str(), "user" | "human" | "operator")
}

// ── Lane 1 — Human Intent (per-session view) ─────────────────────

/// One classified user-originated line from a session, for the per-session
/// truth report. Corpus-level Lane 1 stays `aicx intents` (user frames by
/// default); this is the same-classifier per-session view.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct UserIntentLine {
    pub id: String,
    pub session_id: String,
    pub source_role: String,
    /// Classified kind: intent | decision | question | assumption | …
    pub entry_type: String,
    pub confidence: f32,
    pub raw_text: String,
    pub source_ref: String,
    /// Absolute source-message timestamp (RFC3339), when known.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<String>,
    pub timestamp_partial: bool,
    pub extracted_at: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
}

#[derive(Debug, Clone)]
pub struct CodescribeParser {
    in_codescribe: bool,
}

impl Default for CodescribeParser {
    fn default() -> Self {
        Self::new()
    }
}

impl CodescribeParser {
    pub fn new() -> Self {
        Self {
            in_codescribe: false,
        }
    }

    pub fn process(&mut self, line: &str) -> (String, bool) {
        let mut cleaned = String::new();
        let mut is_voice = self.in_codescribe;

        let mut chars = line.chars().peekable();
        while let Some(c) = chars.next() {
            if c == '<' {
                let mut tag_buf = String::new();
                while let Some(&next_c) = chars.peek() {
                    if next_c == '>' {
                        chars.next();
                        break;
                    }
                    tag_buf.push(next_c);
                    chars.next();
                }

                let tag_trimmed = tag_buf.trim();
                if tag_trimmed.starts_with("codescribe") {
                    self.in_codescribe = true;
                    is_voice = true;
                } else if tag_trimmed == "/codescribe" {
                    self.in_codescribe = false;
                } else {
                    cleaned.push('<');
                    cleaned.push_str(&tag_buf);
                    cleaned.push('>');
                }
            } else {
                cleaned.push(c);
            }
        }

        (cleaned.trim().to_string(), is_voice)
    }
}

/// Lane 1 stage (pure, per-session): classify user-originated lines into
/// intent-bearing entries. Doctrine enforced here:
/// - intents are user-originated — agent/tool/system rows never produce one
///   ([`is_user_role`] allowlist, re-guarded in-lane like [`extract_claims`]);
/// - no manufactured intents — a line that classifies to nothing is skipped,
///   and pasted-reference material is rejected by the shared classifier;
/// - result/outcome-shaped lines belong to the evidence lane even when a user
///   pasted them — they never become human intent;
/// - identical lines are deduplicated (earliest occurrence wins), since agent
///   logs frequently double-record the same user turn.
pub fn extract_user_intent_lines(
    sources: &[ClaimSource],
    extracted_at: &str,
) -> Vec<UserIntentLine> {
    let mut out: Vec<UserIntentLine> = Vec::new();
    let mut seen = std::collections::HashSet::new();
    for (idx, src) in sources.iter().enumerate() {
        if !is_user_role(&src.role) {
            continue;
        }
        let mut codescribe_parser = CodescribeParser::new();
        for line in src.text.lines() {
            let (cleaned, is_voice) = codescribe_parser.process(line);
            let line = cleaned.trim();
            if line.is_empty() {
                continue;
            }
            let Some((entry_type, confidence)) = super::classify_line_entry_type(line, true) else {
                continue;
            };
            if matches!(
                entry_type,
                crate::types::EntryType::Result | crate::types::EntryType::Outcome
            ) {
                continue;
            }
            if !seen.insert(line.to_string()) {
                continue;
            }
            let source_val = if is_voice {
                Some("voice_transcript".to_string())
            } else {
                None
            };
            out.push(UserIntentLine {
                id: format!(
                    "intent-{}-{}-{}",
                    src.session_id,
                    idx,
                    claim_hash8(line, &src.source_ref)
                ),
                session_id: src.session_id.clone(),
                source_role: src.role.clone(),
                entry_type: entry_type.as_str().to_string(),
                confidence,
                raw_text: line.to_string(),
                source_ref: src.source_ref.clone(),
                timestamp: src.timestamp.clone(),
                timestamp_partial: src.timestamp_partial,
                extracted_at: extracted_at.to_string(),
                source: source_val,
            });
        }
    }
    out
}

const FNV_OFFSET: u64 = 0xcbf2_9ce4_8422_2325;
const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;

/// FNV-1a 64-bit fold step — inline, dependency-free, deterministic.
fn fnv1a64(mut hash: u64, bytes: &[u8]) -> u64 {
    for &b in bytes {
        hash ^= u64::from(b);
        hash = hash.wrapping_mul(FNV_PRIME);
    }
    hash
}

/// 8-hex deterministic content hash over `(claim_text, source_ref)`, used to
/// disambiguate claim ids across separate `extract_claims` calls. A NUL
/// separator keeps the pair unambiguous ("ab"+"c" vs "a"+"bc").
fn claim_hash8(claim_text: &str, source_ref: &str) -> String {
    let h = fnv1a64(FNV_OFFSET, claim_text.as_bytes());
    let h = fnv1a64(h, &[0]);
    let h = fnv1a64(h, source_ref.as_bytes());
    format!("{:08x}", (h ^ (h >> 32)) as u32)
}

/// Lane 2 stage (pure): turn agent/assistant source rows into Unverified
/// [`ClaimRecord`]s. Doctrine enforced here:
/// - claims are agent-originated — user rows never produce a claim;
/// - a row whose text carries no claim marker is skipped — no manufactured
///   claims (absence of a marker is not a claim);
/// - every claim is `Unverified` until Lane 3 supplies evidence;
/// - the applause claims (green/ready/shippable/no-blockers) are flagged
///   high-risk.
///
/// Claim id shape: `claim-<session_id>-<row_index>-<hash8>` where `hash8` is a
/// deterministic FNV-1a content hash of `(claim_text, source_ref)`. Uniqueness
/// scope: ids are unique within one call per source row, and the content hash
/// disambiguates rows that share `(session_id, index)` across separate calls
/// over different sources. Identical input rows deliberately produce identical
/// ids (deterministic, re-runnable extraction — not globally unique).
pub fn extract_claims(sources: &[ClaimSource], extracted_at: &str) -> Vec<ClaimRecord> {
    sources
        .iter()
        .enumerate()
        .filter_map(|(i, s)| {
            if !is_agent_role(&s.role) {
                return None;
            }
            // Classify the leading non-empty line, not the whole message: a long
            // reply incidentally contains markers ("fixed"/"ready"/"green") deep
            // in prose that are not the claim. Status claims lead their message.
            let claim_line = s
                .text
                .lines()
                .map(str::trim)
                .find(|l| !l.is_empty())
                .unwrap_or("");
            let claim_type = classify_claim(claim_line)?;
            let risk_flags = if claim_type.is_high_risk() {
                vec!["high_risk_unverified_claim".to_string()]
            } else {
                Vec::new()
            };
            Some(ClaimRecord {
                id: format!(
                    "claim-{}-{i}-{}",
                    s.session_id,
                    claim_hash8(claim_line, &s.source_ref)
                ),
                project: s.project.clone(),
                source_session: s.session_id.clone(),
                source_agent: s.agent.clone(),
                source_role: s.role.clone(),
                source_span: Some(s.source_ref.clone()),
                claim_text: claim_line.to_string(),
                claim_type,
                claimed_status: claim_type.label().to_string(),
                timestamp: s.timestamp.clone(),
                timestamp_partial: s.timestamp_partial || s.timestamp.is_none(),
                extracted_at: extracted_at.to_string(),
                claimed_files: Vec::new(),
                claimed_commands: Vec::new(),
                claimed_artifacts: Vec::new(),
                related_intents: Vec::new(),
                evidence_refs: Vec::new(),
                verification_status: VerificationStatus::Unverified,
                risk_flags,
            })
        })
        .collect()
}

// ── Lane 3 stages — evidence collection + claim verification ─────

/// Pull path-looking tokens out of a claim sentence: backtick-quoted spans and
/// bare tokens containing `/` that look like repo-relative file paths. These are
/// the artifacts the claim implicitly stakes its truth on.
/// A token is only checkable evidence when it can be resolved against the repo
/// root (or absolutely). `~`-prefixed paths and glob patterns name surfaces we
/// cannot honestly test here — skipping them avoids manufacturing false
/// contradictions (absence of checkable evidence is NOT a Fail).
fn is_checkable_path(token: &str) -> bool {
    // On Windows a claim can name a `C:\…\file.rs`-shaped artifact that carries
    // no `/` at all; accept the OS separator there. Gated on `cfg!(windows)` so
    // Unix heuristics stay byte-identical (no new false positives on tokens
    // that merely contain a backslash).
    let has_separator = token.contains('/') || (cfg!(windows) && token.contains('\\'));
    has_separator && !token.contains("://") && !token.starts_with('~') && !token.contains('*')
}

/// Final path component, honoring the backslash separator on Windows so
/// Windows-shaped tokens (`C:\…\file.rs`) resolve their file-ish suffix.
fn last_path_component(token: &str) -> Option<&str> {
    if cfg!(windows) {
        token.rsplit(['/', '\\']).next()
    } else {
        token.rsplit('/').next()
    }
}

fn path_tokens(text: &str) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    // backtick-quoted spans first — the explicit form
    let mut rest = text;
    while let Some(start) = rest.find('`') {
        let after = &rest[start + 1..];
        let Some(end) = after.find('`') else { break };
        let token = after[..end].trim();
        if !token.contains(' ') && is_checkable_path(token) {
            out.push(token.to_string());
        }
        rest = &after[end + 1..];
    }
    // bare path-like tokens (contain '/', no URL scheme, end in a file-ish name)
    for word in text.split_whitespace() {
        let w = word.trim_matches(|c: char| ",.;:()[]\"'".contains(c));
        if is_checkable_path(w)
            && !w.starts_with('`')
            && last_path_component(w).is_some_and(|f| f.contains('.'))
        {
            out.push(w.to_string());
        }
    }
    out.sort();
    out.dedup();
    out
}

/// Redact the user's home prefix in an absolute path to `~` so excerpts do
/// not leak the local username into exported lane records. Home is resolved
/// the same way as everywhere else in the repo (`dirs::home_dir()`, which
/// honors `$HOME` on Unix). Only whole-component prefixes are redacted —
/// `/Users/username` is not touched when home is `/Users/user`.
fn redact_home(path: &str) -> String {
    let Some(home) = crate::os_user_home() else {
        return path.to_string();
    };
    let home = home.to_string_lossy();
    let home = home.trim_end_matches(['/', '\\']);
    if home.is_empty() || !path.starts_with(home) {
        return path.to_string();
    }
    let rest = &path[home.len()..];
    match rest.chars().next() {
        None => "~".to_string(),
        // Whole-component boundary on either separator (Windows home is
        // `C:\Users\<user>`). Normalise the shown suffix to `/` so redacted
        // excerpts read consistently across OSes and never leak the local
        // username; the unredacted `artifact_path` keeps native separators.
        Some('/') | Some('\\') => format!("~{}", rest.replace('\\', "/")),
        Some(_) => path.to_string(),
    }
}

/// Lane 3 stage (deterministic, read-only): for every file path a claim names,
/// check whether the artifact actually exists under `repo_root` (or absolutely).
/// Existence yields a `Pass` result, absence a `Fail` — both are evidence; a
/// claim that names no artifact gets no result here and stays unverified.
/// Nothing is executed — this collects repo-state evidence only. Absolute
/// paths under the user's home are redacted to `~` in
/// `observed_output_excerpt` (the raw `artifact_path` stays checkable).
pub fn collect_artifact_evidence(
    claims: &[ClaimRecord],
    repo_root: &Path,
    collected_at: &str,
) -> Vec<ResultRecord> {
    let mut results = Vec::new();
    for claim in claims {
        for token in path_tokens(&claim.claim_text) {
            let candidate = Path::new(&token);
            let exists = if candidate.is_absolute() {
                candidate.exists()
            } else {
                repo_root.join(candidate).exists()
            };
            let status = if exists {
                ResultStatus::Pass
            } else {
                ResultStatus::Fail
            };
            results.push(ResultRecord {
                id: format!("result-{}-{}", claim.id, results.len()),
                project: claim.project.clone(),
                evidence_type: "artifact_existence".to_string(),
                command: None,
                exit_status: None,
                artifact_path: Some(token.clone()),
                observed_output_excerpt: Some({
                    let shown_token = redact_home(&token);
                    let shown_root = redact_home(&repo_root.display().to_string());
                    if exists {
                        format!("{shown_token}: exists in {shown_root}")
                    } else {
                        format!("{shown_token}: NOT FOUND in {shown_root}")
                    }
                }),
                timestamp: Some(collected_at.to_string()),
                related_claims: vec![claim.id.clone()],
                related_intents: Vec::new(),
                result_status: status,
                confidence: 8,
                reproducibility_notes: Some("ls-level filesystem check, re-runnable".to_string()),
            });
        }
    }
    results
}

/// Helper to identify if a line is a failing cargo test line.
fn is_failing_test_line(line: &str) -> bool {
    if !line.contains("test result:") {
        return false;
    }
    let lower = line.to_lowercase();
    if !lower.contains("failed") {
        return false;
    }
    if line.to_uppercase().contains("FAILED") {
        return true;
    }
    if let Some(idx) = lower.find("failed") {
        let prefix = &lower[..idx];
        let last_word = prefix.split_whitespace().next_back();
        let num = last_word.and_then(|w| w.parse::<usize>().ok());
        if let Some(n) = num {
            return n > 0;
        }
    }
    false
}

/// Helper to determine if an evidence record unambiguously overlaps with a claim.
fn is_unambiguous_overlap(claim: &ClaimRecord, ev: &EvidenceRecord) -> bool {
    match ev.kind {
        EvidenceKind::Commit => {
            // Check if commit SHA (anchor) is mentioned in claim text
            if ev.anchor.as_ref().is_some_and(|sha| {
                sha.len() >= 7
                    && claim
                        .claim_text
                        .to_lowercase()
                        .contains(&sha.to_lowercase())
            }) {
                return true;
            }
            // Check if any touched file is mentioned in claim text or claim's claimed_files
            let claim_files = path_tokens(&claim.claim_text);
            if let Some(ref files) = ev.files_touched {
                for f in files {
                    if claim_files.contains(f) || claim.claimed_files.contains(f) {
                        return true;
                    }
                }
            }
            // Check if the excerpt mentions any claim files
            for f in &claim_files {
                if ev.excerpt.contains(f) {
                    return true;
                }
            }
        }
        EvidenceKind::TestRun => {
            // A test run overlaps with a claim if the claim mentions tests or test commands
            let text = claim.claim_text.to_lowercase();
            let is_test_claim = text.contains("test")
                || text.contains("testy")
                || text.contains("przetestowane")
                || text.contains("green")
                || text.contains("zielon");

            if is_test_claim {
                return true;
            }

            if ev
                .anchor
                .as_ref()
                .is_some_and(|cmd| text.contains(&cmd.to_lowercase()))
            {
                return true;
            }
        }
        EvidenceKind::RuntimeProbe => {
            // A runtime probe overlaps if the claim mentions the command
            if ev.anchor.as_ref().is_some_and(|cmd| {
                claim
                    .claim_text
                    .to_lowercase()
                    .contains(&cmd.to_lowercase())
            }) {
                return true;
            }
        }
    }
    false
}

/// Lane 3 verification stage: audit claims against evidence records.
/// Turns agent claims (Lane 2 output) into evidence-backed ResultRecords.
/// Pure function.
pub fn audit_claims_against_evidence(
    claims: &[ClaimRecord],
    evidence: &[EvidenceRecord],
    audited_at: &str,
) -> Vec<ResultRecord> {
    let mut results = Vec::new();

    for claim in claims {
        // Find matching evidence records
        let mut matched_ev: Vec<&EvidenceRecord> = Vec::new();
        for ev in evidence {
            let is_direct = ev.claim_id.as_deref() == Some(&claim.id);
            let is_overlap = is_unambiguous_overlap(claim, ev);
            if is_direct || is_overlap {
                matched_ev.push(ev);
            }
        }

        if matched_ev.is_empty() {
            continue;
        }

        let first_fail_ev = matched_ev.iter().copied().find(|ev| {
            ev.kind == EvidenceKind::TestRun && ev.excerpt.lines().any(is_failing_test_line)
        });

        if let Some(first_fail_ev) = first_fail_ev {
            // Generate Fail ResultRecord
            let evidence_type = match first_fail_ev.kind {
                EvidenceKind::Commit => "commit",
                EvidenceKind::TestRun => "test_run",
                EvidenceKind::RuntimeProbe => "runtime_probe",
            }
            .to_string();

            let (command, exit_status) = if first_fail_ev.kind == EvidenceKind::RuntimeProbe
                || first_fail_ev.kind == EvidenceKind::TestRun
            {
                (first_fail_ev.anchor.clone(), Some(1))
            } else {
                (None, None)
            };

            results.push(ResultRecord {
                id: format!("result-audit-{}-{}", claim.id, results.len()),
                project: claim.project.clone(),
                evidence_type,
                command,
                exit_status,
                artifact_path: None,
                observed_output_excerpt: Some(first_fail_ev.excerpt.clone()),
                timestamp: Some(audited_at.to_string()),
                related_claims: vec![claim.id.clone()],
                related_intents: Vec::new(),
                result_status: ResultStatus::Fail,
                confidence: 9,
                reproducibility_notes: Some(
                    "Audited against test run output (failing)".to_string(),
                ),
            });
        } else {
            // All matched evidence is passing/supporting.
            // Check the high-risk gate
            let has_direct = matched_ev
                .iter()
                .any(|ev| ev.claim_id.as_deref() == Some(&claim.id));
            let can_verify = !claim.claim_type.is_high_risk() || has_direct;

            if can_verify {
                // Find primary evidence (prefer direct link if available)
                let primary_ev = matched_ev
                    .iter()
                    .find(|ev| ev.claim_id.as_deref() == Some(&claim.id))
                    .cloned()
                    .unwrap_or(matched_ev[0]);

                let evidence_type = match primary_ev.kind {
                    EvidenceKind::Commit => "commit",
                    EvidenceKind::TestRun => "test_run",
                    EvidenceKind::RuntimeProbe => "runtime_probe",
                }
                .to_string();

                let command = if primary_ev.kind == EvidenceKind::RuntimeProbe
                    || primary_ev.kind == EvidenceKind::TestRun
                {
                    primary_ev.anchor.clone()
                } else {
                    None
                };

                let artifact_path = if primary_ev.kind == EvidenceKind::Commit {
                    primary_ev
                        .files_touched
                        .as_ref()
                        .and_then(|files| files.first().cloned())
                } else {
                    None
                };

                results.push(ResultRecord {
                    id: format!("result-audit-{}-{}", claim.id, results.len()),
                    project: claim.project.clone(),
                    evidence_type,
                    command,
                    exit_status: Some(0),
                    artifact_path,
                    observed_output_excerpt: Some(primary_ev.excerpt.clone()),
                    timestamp: Some(audited_at.to_string()),
                    related_claims: vec![claim.id.clone()],
                    related_intents: Vec::new(),
                    result_status: ResultStatus::Pass,
                    confidence: 9,
                    reproducibility_notes: Some(format!(
                        "Audited and verified via matching {} evidence record",
                        if has_direct {
                            "direct"
                        } else {
                            "circumstantial"
                        }
                    )),
                });
            } else {
                // High-risk claim with only circumstantial evidence is NOT verified
            }
        }
    }

    results
}

/// Lane 3 verification (pure): fold evidence into claims. Pass evidence
/// verifies, Fail evidence contradicts, mixed evidence is Partial; a claim
/// nothing points at stays exactly as it was (Unverified by default — no
/// evidence means no result, and no result means no promotion).
pub fn verify_claims(claims: &mut [ClaimRecord], results: &[ResultRecord]) {
    for claim in claims.iter_mut() {
        let mut pass = 0usize;
        let mut fail = 0usize;
        for r in results
            .iter()
            .filter(|r| r.related_claims.contains(&claim.id))
        {
            claim.evidence_refs.push(r.id.clone());
            match r.result_status {
                ResultStatus::Pass => pass += 1,
                ResultStatus::Fail => fail += 1,
                ResultStatus::Partial | ResultStatus::Unknown => {}
            }
        }
        claim.verification_status = match (pass, fail) {
            (0, 0) => claim.verification_status, // untouched — stays Unverified
            (_, 0) => VerificationStatus::Verified,
            (0, _) => VerificationStatus::Contradicted,
            (_, _) => VerificationStatus::Partial,
        };
    }
}

// ── Lane 4 stage — contract fracture detection ───────────────────

/// Lane 4 stage (pure): surface promise-vs-runtime fractures from verified
/// claims. A contradicted claim IS a fracture (the agent said X, the repo says
/// not-X); an applause claim (green/ready/shippable/no-blockers) with no evidence is
/// a fracture-in-waiting and gets surfaced at Medium so it cannot pass as truth.
pub fn detect_fractures(claims: &[ClaimRecord]) -> Vec<ContractFracture> {
    let mut fractures = Vec::new();
    for claim in claims {
        match claim.verification_status {
            VerificationStatus::Contradicted => {
                let severity = if claim.claim_type.is_high_risk() {
                    FractureSeverity::Critical
                } else {
                    FractureSeverity::High
                };
                fractures.push(ContractFracture {
                    claim_id: claim.id.clone(),
                    contract_source: format!(
                        "agent claim {} (session {})",
                        claim.id, claim.source_session
                    ),
                    promised_surface: claim.claim_text.clone(),
                    runtime_surface: "evidence contradicts the claim (named artifact missing)"
                        .to_string(),
                    evidence: claim.evidence_refs.clone(),
                    severity,
                    options: vec![
                        "A: implement/repair so the claim becomes true".to_string(),
                        "B: retract the claim and reopen the task".to_string(),
                        "C: accept the gap and record it as known debt".to_string(),
                    ],
                    recommended_clarify_question: Some(format!("clarify-{}", claim.id)),
                    linked_intents: claim.related_intents.clone(),
                    linked_claims: vec![claim.id.clone()],
                    linked_results: claim.evidence_refs.clone(),
                    timestamp: claim.timestamp.clone(),
                });
            }
            VerificationStatus::Unverified if claim.claim_type.is_high_risk() => {
                fractures.push(ContractFracture {
                    claim_id: claim.id.clone(),
                    contract_source: format!(
                        "agent claim {} (session {})",
                        claim.id, claim.source_session
                    ),
                    promised_surface: claim.claim_text.clone(),
                    runtime_surface: "no evidence collected — applause verdict unbacked"
                        .to_string(),
                    evidence: Vec::new(),
                    severity: FractureSeverity::Medium,
                    options: vec![
                        "A: demand evidence (run gates) before trusting the verdict".to_string(),
                        "B: treat as unverified and keep hardening".to_string(),
                        "C: accept the verdict on trust and ship".to_string(),
                    ],
                    recommended_clarify_question: Some(format!("clarify-{}", claim.id)),
                    linked_intents: claim.related_intents.clone(),
                    linked_claims: vec![claim.id.clone()],
                    linked_results: Vec::new(),
                    timestamp: claim.timestamp.clone(),
                });
            }
            _ => {}
        }
    }
    fractures
}

/// Lane 4 stage (pure): detect contract fractures by analyzing user intents,
/// agent claims, and verified result records. Surfaces:
/// 1. Contradicted claims (evidence failure).
/// 2. Unsupported high-risk claims (unverified status but high risk claim type).
/// 3. Orphaned intents (user intents with no addressing claim and no addressing result).
pub fn detect_contract_fractures(
    intents: &[super::IntentRecord],
    claims: &[ClaimRecord],
    results: &[ResultRecord],
) -> Vec<ContractFracture> {
    let mut local_claims = claims.to_vec();
    verify_claims(&mut local_claims, results);

    let mut fractures = Vec::new();

    // 1. Contradicted claims & 2. Unsupported high-risk claims
    for claim in &local_claims {
        match claim.verification_status {
            VerificationStatus::Contradicted => {
                let severity = if claim.claim_type.is_high_risk() {
                    FractureSeverity::Critical
                } else {
                    FractureSeverity::High
                };
                fractures.push(ContractFracture {
                    claim_id: claim.id.clone(),
                    contract_source: format!(
                        "agent claim {} (session {})",
                        claim.id, claim.source_session
                    ),
                    promised_surface: claim.claim_text.clone(),
                    runtime_surface: "evidence contradicts the claim (named artifact missing)"
                        .to_string(),
                    evidence: claim.evidence_refs.clone(),
                    severity,
                    options: vec![
                        "A: implement/repair so the claim becomes true".to_string(),
                        "B: retract the claim and reopen the task".to_string(),
                        "C: accept the gap and record it as known debt".to_string(),
                    ],
                    recommended_clarify_question: Some(format!("clarify-{}", claim.id)),
                    linked_intents: claim.related_intents.clone(),
                    linked_claims: vec![claim.id.clone()],
                    linked_results: claim.evidence_refs.clone(),
                    timestamp: claim.timestamp.clone(),
                });
            }
            VerificationStatus::Unverified if claim.claim_type.is_high_risk() => {
                fractures.push(ContractFracture {
                    claim_id: claim.id.clone(),
                    contract_source: format!(
                        "agent claim {} (session {})",
                        claim.id, claim.source_session
                    ),
                    promised_surface: claim.claim_text.clone(),
                    runtime_surface: "no evidence collected — applause verdict unbacked"
                        .to_string(),
                    evidence: Vec::new(),
                    severity: FractureSeverity::Medium,
                    options: vec![
                        "A: demand evidence (run gates) before trusting the verdict".to_string(),
                        "B: treat as unverified and keep hardening".to_string(),
                        "C: accept the verdict on trust and ship".to_string(),
                    ],
                    recommended_clarify_question: Some(format!("clarify-{}", claim.id)),
                    linked_intents: claim.related_intents.clone(),
                    linked_claims: vec![claim.id.clone()],
                    linked_results: Vec::new(),
                    timestamp: claim.timestamp.clone(),
                });
            }
            _ => {}
        }
    }

    // 3. Orphaned intents
    for intent in intents {
        if intent.kind != super::IntentKind::Intent {
            continue;
        }

        // Check if there is any claim addressing this intent
        let has_addressing_claim = local_claims.iter().any(|claim| {
            if claim.project != intent.project {
                return false;
            }
            if claim.related_intents.contains(&intent.summary) {
                return true;
            }
            let claim_lower = claim.claim_text.to_lowercase();
            let intent_lower = intent.summary.to_lowercase();
            if claim_lower.contains(&intent_lower) || intent_lower.contains(&claim_lower) {
                return true;
            }

            let intent_words: Vec<&str> = intent_lower
                .split(|c: char| !c.is_alphanumeric())
                .filter(|w| w.len() >= 3)
                .collect();
            let claim_words: Vec<&str> = claim_lower
                .split(|c: char| !c.is_alphanumeric())
                .filter(|w| w.len() >= 3)
                .collect();

            intent_words.iter().any(|iw| {
                claim_words
                    .iter()
                    .any(|cw| cw.contains(iw) || iw.contains(cw))
            })
        });

        // Check if there is any result addressing this intent
        let has_addressing_result = results.iter().any(|result| {
            if result.project != intent.project {
                return false;
            }
            if result.related_intents.contains(&intent.summary) {
                return true;
            }
            let intent_lower = intent.summary.to_lowercase();
            if let Some(cmd) = &result.command {
                let cmd_lower = cmd.to_lowercase();
                if cmd_lower.contains(&intent_lower) {
                    return true;
                }
            }
            if let Some(excerpt) = &result.observed_output_excerpt {
                let excerpt_lower = excerpt.to_lowercase();
                if excerpt_lower.contains(&intent_lower) {
                    return true;
                }
            }
            if let Some(notes) = &result.reproducibility_notes {
                let notes_lower = notes.to_lowercase();
                if notes_lower.contains(&intent_lower) {
                    return true;
                }
            }

            let intent_words: Vec<&str> = intent_lower
                .split(|c: char| !c.is_alphanumeric())
                .filter(|w| w.len() >= 3)
                .collect();

            let mut result_text = String::new();
            if let Some(cmd) = &result.command {
                result_text.push_str(cmd);
                result_text.push(' ');
            }
            if let Some(excerpt) = &result.observed_output_excerpt {
                result_text.push_str(excerpt);
                result_text.push(' ');
            }
            if let Some(notes) = &result.reproducibility_notes {
                result_text.push_str(notes);
            }
            let result_lower = result_text.to_lowercase();
            let result_words: Vec<&str> = result_lower
                .split(|c: char| !c.is_alphanumeric())
                .filter(|w| w.len() >= 3)
                .collect();

            intent_words.iter().any(|iw| {
                result_words
                    .iter()
                    .any(|rw| rw.contains(iw) || iw.contains(rw))
            })
        });

        if !has_addressing_claim && !has_addressing_result {
            let summary_hash = claim_hash8(&intent.summary, &intent.source_chunk);
            let claim_id = format!("intent-orphan-{}-{}", intent.session_id, summary_hash);

            fractures.push(ContractFracture {
                claim_id: claim_id.clone(),
                contract_source: format!(
                    "user intent \"{}\" (session {})",
                    intent.summary, intent.session_id
                ),
                promised_surface: intent.summary.clone(),
                runtime_surface: "no agent claim and no result addresses this user intent (silently dropped work)".to_string(),
                evidence: Vec::new(),
                severity: FractureSeverity::Low,
                options: vec![
                    "A: implement/repair the codebase to satisfy the user intent".to_string(),
                    "B: document the dropped intent as out of scope or deferred".to_string(),
                    "C: ignore and accept the gap".to_string(),
                ],
                recommended_clarify_question: Some(format!("clarify-{}", claim_id)),
                linked_intents: vec![intent.summary.clone()],
                linked_claims: Vec::new(),
                linked_results: Vec::new(),
                timestamp: intent.timestamp.clone(),
            });
        }
    }

    fractures
}

// ── Lane 5 stage — clarify generation ────────────────────────────

/// Hard ceiling on clarify questions per run — clarify is a decision-gathering
/// mechanism, not a questionnaire.
pub const CLARIFY_MAX_QUESTIONS: usize = 5;

fn validate_and_sanitize_options(options: &[String]) -> Vec<String> {
    // Check if options are degenerate or less than 2
    let is_degenerate = options.len() < 2
        || options.iter().any(|o| {
            let o_lower = o.to_lowercase();
            let clean = if o_lower.starts_with("a:")
                || o_lower.starts_with("b:")
                || o_lower.starts_with("c:")
            {
                o_lower[2..].trim().to_string()
            } else if o_lower.starts_with("a. ")
                || o_lower.starts_with("b. ")
                || o_lower.starts_with("c. ")
                || o_lower.starts_with("a: ")
                || o_lower.starts_with("b: ")
                || o_lower.starts_with("c: ")
            {
                o_lower[3..].trim().to_string()
            } else {
                o_lower.trim().to_string()
            };
            clean == "yes"
                || clean == "no"
                || clean == "maybe"
                || clean == "true"
                || clean == "false"
        });

    if is_degenerate {
        vec![
            "A: implement/repair to match the promise".to_string(),
            "B: retract or descope the promise".to_string(),
            "C: accept the gap and document it".to_string(),
        ]
    } else {
        options.to_vec()
    }
}

/// Lane 5 stage (pure): turn the sharpest fractures into bounded A/B/C
/// decision questions. Questions ask what the human must DECIDE (keep the
/// promise, retract it, or ship with the gap) — never facts the system already
/// determined (the known facts ride along in `known_facts`). At most
/// `min(max, CLARIFY_MAX_QUESTIONS)` questions, severest fractures first;
/// severity ties break on `claim_id` so the selection under the cap is
/// deterministic regardless of input order.
pub fn generate_clarify(fractures: &[ContractFracture], max: usize) -> Vec<ClarifyQuestion> {
    let cap = max.min(CLARIFY_MAX_QUESTIONS);
    let mut ordered: Vec<&ContractFracture> = fractures.iter().collect();

    // Sort logic:
    // Category 1: Critical & High (contradicted claims)
    // Category 2: Medium (unverified high-risk claims)
    // Category 3: Low (orphaned intents)
    // within each: sorted by recency (newest first, i.e., tb.cmp(ta))
    // tie-breaker: claim_id ascending
    let rank = |s: FractureSeverity| match s {
        FractureSeverity::Critical => 0,
        FractureSeverity::High => 1,
        FractureSeverity::Medium => 2,
        FractureSeverity::Low => 3,
    };

    ordered.sort_by(|a, b| {
        let rank_a = rank(a.severity);
        let rank_b = rank(b.severity);
        rank_a
            .cmp(&rank_b)
            .then_with(|| match (&a.timestamp, &b.timestamp) {
                (Some(ta), Some(tb)) => tb.cmp(ta),
                (Some(_), None) => std::cmp::Ordering::Less,
                (None, Some(_)) => std::cmp::Ordering::Greater,
                (None, None) => std::cmp::Ordering::Equal,
            })
            .then_with(|| a.claim_id.cmp(&b.claim_id))
    });

    ordered
        .into_iter()
        .take(cap)
        .map(|f| {
            let opts = validate_and_sanitize_options(&f.options);
            let default_recommendation = opts
                .first()
                .cloned()
                .unwrap_or_else(|| "A: implement/repair to match the promise".to_string());
            ClarifyQuestion {
                decision_id: f
                    .recommended_clarify_question
                    .clone()
                    .unwrap_or_else(|| format!("clarify-{}", f.claim_id)),
                question: format!(
                    "The promise \"{}\" does not match runtime ({}). Repair it, retract it, or ship with the gap?",
                    f.promised_surface, f.runtime_surface
                ),
                why_now: format!(
                    "{:?}-severity fracture between a recorded promise and the live repo; \
                     leaving it undecided lets a false claim harden into assumed truth",
                    f.severity
                ),
                known_facts: {
                    let mut facts = vec![
                        format!("promised: {}", f.promised_surface),
                        format!("observed: {}", f.runtime_surface),
                    ];
                    facts.extend(f.evidence.iter().map(|e| format!("evidence: {e}")));
                    facts
                },
                options: opts,
                default_recommendation,
                cost_of_not_deciding: "the gap survives as invisible debt and every future agent \
                                       plans on top of a promise the runtime does not keep"
                    .to_string(),
                linked_intents: f.linked_intents.clone(),
                linked_claims: f.linked_claims.clone(),
                linked_results: f.linked_results.clone(),
            }
        })
        .collect()
}

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

    #[test]
    fn lane_schemas_serialize_to_json() {
        // Anchor smoke: each lane record must serialize (lane outputs are JSON).
        let claim = ClaimRecord {
            id: "claim-1".into(),
            project: "aicx".into(),
            source_session: "sess-a".into(),
            source_agent: Some("codex".into()),
            source_role: "assistant".into(),
            source_span: None,
            claim_text: "fixed F2 dead --unresolved".into(),
            claim_type: ClaimType::Fixed,
            claimed_status: "done".into(),
            timestamp: Some("2026-06-08T18:06:26Z".into()),
            timestamp_partial: false,
            extracted_at: "2026-06-09T20:41:00Z".into(),
            claimed_files: vec!["src/main.rs".into()],
            claimed_commands: vec!["cargo test -p aicx --lib".into()],
            claimed_artifacts: vec![],
            related_intents: vec!["intent-1".into()],
            evidence_refs: vec![],
            verification_status: VerificationStatus::default(),
            risk_flags: vec![],
        };
        assert_eq!(claim.verification_status, VerificationStatus::Unverified);

        let result = ResultRecord {
            id: "result-1".into(),
            project: "aicx".into(),
            evidence_type: "command".into(),
            command: Some("cargo test -p aicx --lib".into()),
            exit_status: Some(0),
            artifact_path: None,
            observed_output_excerpt: Some("679 passed; 0 failed".into()),
            timestamp: None,
            related_claims: vec!["claim-1".into()],
            related_intents: vec![],
            result_status: ResultStatus::Pass,
            confidence: 9,
            reproducibility_notes: None,
        };

        let fracture = ContractFracture {
            claim_id: "claim-1".into(),
            contract_source: "docs/TB_ARTIFACT_CONTRACT.md".into(),
            promised_surface: "spotlight.md renderer".into(),
            runtime_surface: "absent (0 renderers in tb_core/)".into(),
            evidence: vec!["rg spotlight tb_core/ -> 0".into()],
            severity: FractureSeverity::High,
            options: vec!["build renderer".into(), "downgrade contract".into()],
            recommended_clarify_question: Some("clarify-1".into()),
            linked_intents: vec![],
            linked_claims: vec!["claim-1".into()],
            linked_results: vec![],
            timestamp: None,
        };

        let clarify = ClarifyQuestion {
            decision_id: "clarify-1".into(),
            question: "Build the promised renderer or drop the promise?".into(),
            why_now: "contract promises an artifact runtime never emits".into(),
            known_facts: vec!["spotlight.md has 0 renderers".into()],
            options: vec!["A build it".into(), "B drop promise".into()],
            default_recommendation: "A".into(),
            cost_of_not_deciding: "every fresh clone trusts a dead promise".into(),
            linked_intents: vec![],
            linked_claims: vec![],
            linked_results: vec!["result-1".into()],
        };

        for json in [
            serde_json::to_string(&claim).unwrap(),
            serde_json::to_string(&result).unwrap(),
            serde_json::to_string(&fracture).unwrap(),
            serde_json::to_string(&clarify).unwrap(),
        ] {
            assert!(!json.is_empty());
        }
    }

    #[test]
    fn classify_claim_maps_taxonomy_and_respects_precedence() {
        use ClaimType::*;
        // straight taxonomy hits
        assert_eq!(classify_claim("this is done"), Some(Implemented));
        assert_eq!(classify_claim("shipped the adapter"), Some(Implemented));
        assert_eq!(classify_claim("fixed the dead filter"), Some(Fixed));
        assert_eq!(classify_claim("all tests pass now"), Some(Tested));
        assert_eq!(classify_claim("verified against the repo"), Some(Verified));
        assert_eq!(classify_claim("migration complete"), Some(Migrated));
        assert_eq!(classify_claim("installed via pipx"), Some(Installed));
        assert_eq!(classify_claim("docs updated"), Some(Documented));
        assert_eq!(classify_claim("the suite is green"), Some(Green));
        assert_eq!(
            classify_claim("this is production ready"),
            Some(ReadyToPush)
        );
        assert_eq!(classify_claim("shippable as-is"), Some(Shippable));

        // precedence edges
        assert_eq!(
            classify_claim("no blockers remain"),
            Some(NoBlockers),
            "'no blockers' must not be misread as Blocked",
        );
        assert_eq!(classify_claim("blocked on review"), Some(Blocked));
        assert_eq!(
            classify_claim("it is ready to ship"),
            Some(ReadyToPush),
            "'ready to ship' must not be swallowed by Shippable/ship-it",
        );

        // no claim marker -> None (absence is not Implemented)
        assert_eq!(classify_claim("just exploring some options"), None);
        assert_eq!(classify_claim(""), None);
    }

    fn mk_source(role: &str, text: &str, refr: &str) -> ClaimSource {
        ClaimSource {
            role: role.to_string(),
            text: text.to_string(),
            project: "aicx".to_string(),
            session_id: "s1".to_string(),
            agent: Some("codex".to_string()),
            source_ref: refr.to_string(),
            timestamp: Some("2026-06-09T20:41:00Z".to_string()),
            timestamp_partial: false,
        }
    }

    #[test]
    fn extract_claims_keeps_agent_claims_drops_user_and_unmarked() {
        let sources = vec![
            // user row has a marker ("fixed") but claims are agent-originated -> dropped
            mk_source("user", "fixed the bug please", "u1"),
            mk_source("assistant", "fixed the dead filter", "a1"),
            // no claim marker -> dropped (absence is not a claim)
            mk_source("assistant", "just thinking out loud", "a2"),
            mk_source("assistant", "this is production ready", "a3"),
        ];

        let claims = extract_claims(&sources, "2026-06-09T20:45:00Z");
        assert_eq!(claims.len(), 2, "only marked agent rows survive");

        let fixed = &claims[0];
        assert_eq!(fixed.claim_type, ClaimType::Fixed);
        assert_eq!(fixed.source_role, "assistant");
        assert_eq!(fixed.claimed_status, "fixed");
        assert_eq!(fixed.verification_status, VerificationStatus::Unverified);
        assert!(fixed.risk_flags.is_empty());

        let ready = &claims[1];
        assert_eq!(ready.claim_type, ClaimType::ReadyToPush);
        // the applause verdict is flagged so Lane 3 must demand evidence
        assert_eq!(
            ready.risk_flags,
            vec!["high_risk_unverified_claim".to_string()]
        );
    }

    #[test]
    fn user_intent_lines_keep_user_rows_and_drop_agent_and_tool_text() {
        let sources = vec![
            mk_source("user", "Decision: ship the lanes envelope first", "u1"),
            // agent text with an intent-shaped marker must NEVER enter Lane 1
            mk_source(
                "assistant",
                "Decision: I delivered everything already",
                "a1",
            ),
            // tool/system rows are outside the strict user allowlist
            mk_source("tool", "decision: noise from a tool row", "t1"),
            // unclassified user chatter is skipped — no manufactured intents
            mk_source("user", "hello there team", "u2"),
        ];

        let lines = extract_user_intent_lines(&sources, "2026-06-09T21:00:00Z");
        assert_eq!(lines.len(), 1, "only classified USER lines survive");
        let line = &lines[0];
        assert_eq!(line.source_role, "user");
        assert_eq!(line.entry_type, "decision");
        assert_eq!(line.session_id, "s1");
        // P0 temporal: absolute source timestamp + extraction stamp, explicit
        // partial marker
        assert_eq!(line.timestamp.as_deref(), Some("2026-06-09T20:41:00Z"));
        assert!(!line.timestamp_partial);
        assert_eq!(line.extracted_at, "2026-06-09T21:00:00Z");
    }

    #[test]
    fn assistant_completion_text_becomes_claim_never_intent() {
        // Lane separation: the same assistant row feeds Lane 2 and is invisible
        // to Lane 1.
        let sources = vec![mk_source(
            "assistant",
            "implemented the report command, suite is green",
            "a1",
        )];
        let claims = extract_claims(&sources, "2026-06-09T21:00:00Z");
        let lines = extract_user_intent_lines(&sources, "2026-06-09T21:00:00Z");
        assert_eq!(claims.len(), 1, "completion text is an audit target");
        assert!(lines.is_empty(), "agent text must never enter Lane 1");
    }

    #[test]
    fn user_intent_lines_propagate_partial_timestamp_marker() {
        let mut src = mk_source("user", "decision: keep UTC everywhere", "u1");
        src.timestamp_partial = true;
        let lines = extract_user_intent_lines(&[src], "2026-06-09T21:00:00Z");
        assert_eq!(lines.len(), 1);
        assert!(
            lines[0].timestamp_partial,
            "partial time is never silently presented as full"
        );
    }

    #[test]
    fn claims_carry_absolute_time_and_mark_partial_explicitly() {
        // P0 temporal: a claim must carry the absolute source timestamp (with
        // year) AND the extraction timestamp; a missing source time is marked
        // partial — never silently presented as full temporal truth.
        let mut with_time = mk_source("assistant", "fixed the parser", "a1");
        with_time.timestamp = Some("2026-06-09T20:41:00Z".to_string());
        let mut no_time = mk_source("assistant", "tests pass on the suite", "a2");
        no_time.timestamp = None;

        let claims = extract_claims(&[with_time, no_time], "2026-06-09T21:00:00Z");
        assert_eq!(claims.len(), 2);

        assert_eq!(claims[0].timestamp.as_deref(), Some("2026-06-09T20:41:00Z"));
        assert!(claims[0].timestamp.as_deref().unwrap().starts_with("2026-"));
        assert!(!claims[0].timestamp_partial);
        assert_eq!(claims[0].extracted_at, "2026-06-09T21:00:00Z");

        assert!(claims[1].timestamp.is_none());
        assert!(
            claims[1].timestamp_partial,
            "missing source time must be marked partial"
        );
    }

    #[test]
    fn lane_export_envelope_carries_temporal_contract() {
        let export = LaneExport {
            schema_version: LANE_SCHEMA_VERSION.to_string(),
            generated_at: "2026-06-09T21:00:00Z".to_string(),
            project: "aicx".to_string(),
            repo: Some("/repo".to_string()),
            session_id: Some("s1".to_string()),
            source_time_coverage: Some(TimeCoverage {
                earliest: "2026-06-09T20:00:00Z".to_string(),
                latest: "2026-06-09T20:59:00Z".to_string(),
            }),
            source_files: vec!["~/.claude/projects/x/s1.jsonl".to_string()],
            extraction_mode: "claims".to_string(),
            role_filter: "agent_only".to_string(),
            timezone_assumptions: UTC_TIMEZONE_ASSUMPTION.to_string(),
            warnings: vec!["1 claim has a partial timestamp".to_string()],
            payload: Vec::<ClaimRecord>::new(),
        };
        let json = serde_json::to_string(&export).unwrap();
        for key in [
            "schema_version",
            "generated_at",
            "source_time_coverage",
            "timezone_assumptions",
            "warnings",
            "2026-06-09T21:00:00Z",
        ] {
            assert!(json.contains(key), "envelope must expose {key}");
        }
    }

    #[test]
    fn evidence_verifies_and_contradiction_marks_contradicted() {
        let sources = vec![
            mk_source("assistant", "fixed `src/intents/schema.rs` for good", "a1"),
            mk_source(
                "assistant",
                "implemented `src/does_not_exist.rs` fully",
                "a2",
            ),
            mk_source("assistant", "verified the run end to end", "a3"),
        ];
        let mut claims = extract_claims(&sources, "2026-06-09T21:00:00Z");
        assert_eq!(claims.len(), 3);

        // repo root = this crate's source tree; schema.rs exists, the other not
        let repo = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
        let results = collect_artifact_evidence(&claims, repo, "2026-06-09T21:01:00Z");
        assert_eq!(results.len(), 2, "only path-naming claims yield evidence");

        verify_claims(&mut claims, &results);
        assert_eq!(
            claims[0].verification_status,
            VerificationStatus::Verified,
            "existing artifact verifies the claim"
        );
        assert!(!claims[0].evidence_refs.is_empty());
        assert_eq!(
            claims[1].verification_status,
            VerificationStatus::Contradicted,
            "missing artifact contradicts the claim"
        );
        assert_eq!(
            claims[2].verification_status,
            VerificationStatus::Unverified,
            "claim without evidence stays unverified — never promoted"
        );
        assert!(claims[2].evidence_refs.is_empty());
    }

    #[test]
    fn fractures_surface_contradictions_and_unbacked_applause() {
        let sources = vec![
            mk_source("assistant", "implemented `src/nope.rs` end to end", "a1"),
            mk_source("assistant", "this is production ready", "a2"),
            mk_source("assistant", "fixed `src/intents/schema.rs`", "a3"),
        ];
        let mut claims = extract_claims(&sources, "2026-06-09T21:00:00Z");
        let repo = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
        let results = collect_artifact_evidence(&claims, repo, "2026-06-09T21:01:00Z");
        verify_claims(&mut claims, &results);

        let fractures = detect_fractures(&claims);
        assert_eq!(
            fractures.len(),
            2,
            "contradicted claim + unbacked applause fracture; verified claim does not"
        );
        assert_eq!(fractures[0].severity, FractureSeverity::High);
        assert_eq!(fractures[1].severity, FractureSeverity::Medium);
        assert!(fractures.iter().all(|f| !f.options.is_empty()));

        // machine-readable link: fracture carries the originating claim id,
        // while contract_source stays human-readable provenance
        assert_eq!(fractures[0].claim_id, claims[0].id);
        assert_eq!(fractures[1].claim_id, claims[1].id);
        assert!(fractures[0].contract_source.contains(&claims[0].id));

        // Lane 5 links questions to claims via claim_id, not contract_source
        let questions = generate_clarify(&fractures, 5);
        assert_eq!(questions[0].linked_claims, vec![claims[0].id.clone()]);
    }

    #[test]
    fn clarify_caps_at_five_and_asks_decisions_not_facts() {
        // 7 fractures in -> at most 5 questions out, severest first.
        let mk_fracture = |i: usize, severity| ContractFracture {
            claim_id: format!("claim-s1-{i}"),
            contract_source: format!("agent claim claim-s1-{i}"),
            promised_surface: format!("promise {i}"),
            runtime_surface: "absent".to_string(),
            evidence: vec![format!("result-{i}")],
            severity,
            options: vec![
                "A: repair".to_string(),
                "B: retract".to_string(),
                "C: ship with gap".to_string(),
            ],
            recommended_clarify_question: Some(format!("clarify-{i}")),
            linked_intents: vec![],
            linked_claims: vec![format!("claim-s1-{i}")],
            linked_results: vec![format!("result-{i}")],
            timestamp: None,
        };
        let fractures: Vec<ContractFracture> = (0..7)
            .map(|i| {
                mk_fracture(
                    i,
                    if i == 6 {
                        FractureSeverity::Critical
                    } else {
                        FractureSeverity::Medium
                    },
                )
            })
            .collect();

        let questions = generate_clarify(&fractures, 10);
        assert_eq!(questions.len(), CLARIFY_MAX_QUESTIONS, "hard cap at 5");
        assert_eq!(
            questions[0].decision_id, "clarify-6",
            "severest fracture first"
        );

        for q in &questions {
            // decision-shaped: a real question with >=2 actionable options,
            // a default, and a named cost — not a fact lookup.
            assert!(q.question.ends_with('?'));
            assert!(
                q.question.contains("Repair it, retract it, or ship"),
                "question asks for a decision"
            );
            assert!(q.options.len() >= 2);
            assert!(!q.default_recommendation.is_empty());
            assert!(!q.cost_of_not_deciding.is_empty());
            assert!(
                !q.known_facts.is_empty(),
                "facts ride along instead of being asked"
            );
        }

        // an explicit lower max narrows further
        assert_eq!(generate_clarify(&fractures, 2).len(), 2);
    }

    #[test]
    fn classify_claim_matches_word_boundaries_not_substrings() {
        // P2-12: substring matching produced false positives; token-boundary
        // matching must not see a marker inside a larger word.
        assert_eq!(classify_claim("the spec is incomplete"), None);
        assert_eq!(classify_claim("abandoned the approach"), None);
        assert_eq!(classify_claim("greenfield rewrite plan"), None);
        assert_eq!(classify_claim("no trespassing rules apply"), None);

        // the real markers still hit on their own word boundaries
        assert_eq!(
            classify_claim("the migration is complete"),
            Some(ClaimType::Implemented)
        );
        assert_eq!(classify_claim("it is green"), Some(ClaimType::Green));
    }

    #[test]
    fn classify_claim_supports_polish_markers() {
        use ClaimType::*;
        // with and without diacritics — both spellings occur in the wild
        assert_eq!(classify_claim("naprawione w parserze"), Some(Fixed));
        assert_eq!(classify_claim("naprawiłem ten bug"), Some(Fixed));
        assert_eq!(classify_claim("naprawilem ten bug"), Some(Fixed));
        assert_eq!(classify_claim("zrobione"), Some(Implemented));
        assert_eq!(classify_claim("gotowe"), Some(Implemented));
        assert_eq!(classify_claim("wdrożone na staging"), Some(Implemented));
        assert_eq!(classify_claim("wdrozone na staging"), Some(Implemented));
        assert_eq!(classify_claim("przetestowane lokalnie"), Some(Tested));
        assert_eq!(classify_claim("testy przechodzą"), Some(Tested));
        assert_eq!(classify_claim("testy przechodza"), Some(Tested));
        assert_eq!(classify_claim("działa na produkcji"), Some(Verified));
        assert_eq!(classify_claim("dziala end to end"), Some(Verified));
        assert_eq!(classify_claim("wszystko zielone"), Some(Green));
        assert_eq!(classify_claim("testy zielone"), Some(Green));
        assert_eq!(classify_claim("bez blokerów"), Some(NoBlockers));
        assert_eq!(classify_claim("bez blokerow"), Some(NoBlockers));
        assert_eq!(classify_claim("gotowe do push"), Some(ReadyToPush));
        assert_eq!(classify_claim("gotowe do pusha"), Some(ReadyToPush));
        assert_eq!(classify_claim("można pushować"), Some(ReadyToPush));
        assert_eq!(classify_claim("mozna pushowac"), Some(ReadyToPush));

        // precedence: the ReadyToPush phrase outranks the generic "gotowe"
        assert_eq!(
            classify_claim("gotowe do push po review"),
            Some(ReadyToPush)
        );
    }

    #[test]
    fn green_claim_is_high_risk_applause() {
        // P2-14: "all green" is the classic applause verdict without evidence.
        assert!(ClaimType::Green.is_high_risk());
        assert!(ClaimType::ReadyToPush.is_high_risk());
        assert!(!ClaimType::Fixed.is_high_risk());

        let claims = extract_claims(
            &[mk_source("assistant", "all green", "a1")],
            "2026-06-09T21:00:00Z",
        );
        assert_eq!(claims.len(), 1);
        assert_eq!(claims[0].claim_type, ClaimType::Green);
        assert_eq!(
            claims[0].risk_flags,
            vec!["high_risk_unverified_claim".to_string()]
        );

        // an unverified green claim surfaces as a Medium fracture
        let fractures = detect_fractures(&claims);
        assert_eq!(fractures.len(), 1);
        assert_eq!(fractures[0].severity, FractureSeverity::Medium);
        assert_eq!(fractures[0].claim_id, claims[0].id);
    }

    #[test]
    fn claim_ids_are_deterministic_and_content_scoped() {
        // P2-02: claim-<sid>-<i> collided across separate extract_claims calls;
        // the content hash disambiguates while staying deterministic.
        let a = vec![mk_source("assistant", "fixed the parser", "a1")];
        let b = vec![mk_source("assistant", "fixed the linter", "a1")];

        let first = extract_claims(&a, "2026-06-09T21:00:00Z");
        let again = extract_claims(&a, "2026-06-09T22:00:00Z");
        let other = extract_claims(&b, "2026-06-09T21:00:00Z");

        assert_eq!(first[0].id, again[0].id, "same input -> same id");
        assert_ne!(
            first[0].id, other[0].id,
            "same (session, index), different claim text -> different id"
        );
        assert!(first[0].id.starts_with("claim-s1-0-"));
        let suffix = first[0].id.rsplit('-').next().unwrap();
        assert_eq!(suffix.len(), 8, "8-hex content hash suffix");
        assert!(suffix.chars().all(|c| c.is_ascii_hexdigit()));

        // same text, different source_ref -> different id too
        let c = vec![mk_source("assistant", "fixed the parser", "a2")];
        let other_ref = extract_claims(&c, "2026-06-09T21:00:00Z");
        assert_ne!(first[0].id, other_ref[0].id);
    }

    #[test]
    fn clarify_breaks_severity_ties_deterministically_by_claim_id() {
        // P3-10: >cap fractures of the same severity must yield a stable,
        // input-order-independent selection (secondary sort key: claim_id).
        let mk = |id: &str| ContractFracture {
            claim_id: id.to_string(),
            contract_source: format!("agent claim {id}"),
            promised_surface: format!("promise {id}"),
            runtime_surface: "absent".to_string(),
            evidence: Vec::new(),
            severity: FractureSeverity::Medium,
            options: vec!["A: repair".to_string(), "B: retract".to_string()],
            recommended_clarify_question: None,
            linked_intents: vec![],
            linked_claims: vec![id.to_string()],
            linked_results: vec![],
            timestamp: None,
        };
        let shuffled = ["c-9", "c-3", "c-7", "c-1", "c-5", "c-8", "c-2"];
        let fractures: Vec<ContractFracture> = shuffled.iter().map(|id| mk(id)).collect();

        let picked: Vec<String> = generate_clarify(&fractures, 5)
            .into_iter()
            .map(|q| q.linked_claims[0].clone())
            .collect();
        assert_eq!(picked, vec!["c-1", "c-2", "c-3", "c-5", "c-7"]);

        // reversed input order picks the exact same set in the same order
        let reversed: Vec<ContractFracture> = shuffled.iter().rev().map(|id| mk(id)).collect();
        let picked_rev: Vec<String> = generate_clarify(&reversed, 5)
            .into_iter()
            .map(|q| q.linked_claims[0].clone())
            .collect();
        assert_eq!(picked, picked_rev);
    }

    #[test]
    fn artifact_evidence_redacts_home_prefix_in_excerpt() {
        // P2-03: absolute paths under the user's home must not leak the local
        // username into exported excerpts. Uses the real home dir (read-only,
        // no env mutation) with a path that is guaranteed not to exist.
        let home = dirs::home_dir().expect("home dir resolvable in tests");
        let missing = home.join("aicx-schema-redaction-test-does-not-exist.rs");
        let text = format!("implemented {} fully", missing.display());

        let claims = extract_claims(
            &[mk_source("assistant", &text, "a1")],
            "2026-06-09T21:00:00Z",
        );
        let results = collect_artifact_evidence(&claims, Path::new("/tmp"), "2026-06-09T21:01:00Z");
        assert_eq!(results.len(), 1);

        let excerpt = results[0].observed_output_excerpt.as_deref().unwrap();
        assert!(
            excerpt.starts_with("~/aicx-schema-redaction-test-does-not-exist.rs"),
            "home prefix redacted to ~ in excerpt: {excerpt}"
        );
        assert!(
            !excerpt.contains(home.to_string_lossy().as_ref()),
            "raw home path must not leak into excerpt: {excerpt}"
        );
        // the raw artifact_path stays checkable (unredacted)
        assert_eq!(
            results[0].artifact_path.as_deref(),
            Some(missing.display().to_string().as_str())
        );
    }

    #[test]
    fn empty_inputs_yield_empty_outputs() {
        // P3-14 edge: empty pipelines stay empty, no panics, no manufactured
        // records.
        assert!(extract_claims(&[], "2026-06-09T21:00:00Z").is_empty());
        assert!(generate_clarify(&[], 5).is_empty());
    }

    #[test]
    fn is_agent_role_accepts_agent_rows_and_rejects_the_rest() {
        // The single shared predicate behind role_filter="agent_only": both
        // the CLI source build and the extract_claims re-guard call THIS.
        for role in ["assistant", "agent", "model", "gemini"] {
            assert!(is_agent_role(role), "{role} is agent-originated");
        }
        // case-insensitive
        assert!(is_agent_role("Assistant"));
        assert!(is_agent_role("MODEL"));
        for role in ["user", "system", "tool", "developer", "operator", ""] {
            assert!(!is_agent_role(role), "{role:?} must never source a claim");
        }
    }

    #[test]
    fn test_evidence_serialization() {
        let ev = EvidenceRecord {
            id: "ev-1".to_string(),
            kind: EvidenceKind::TestRun,
            excerpt: "test result: ok. 5 passed; 0 failed".to_string(),
            observed_at: "2026-06-12T02:53:00Z".to_string(),
            anchor: Some("cargo test".to_string()),
            claim_id: Some("claim-123".to_string()),
            files_touched: None,
        };
        let serialized = serde_json::to_string(&ev).unwrap();
        let deserialized: EvidenceRecord = serde_json::from_str(&serialized).unwrap();
        assert_eq!(ev, deserialized);

        let serialized_kind = serde_json::to_string(&EvidenceKind::Commit).unwrap();
        assert_eq!(serialized_kind, "\"commit\"");
        let deserialized_kind: EvidenceKind = serde_json::from_str(&serialized_kind).unwrap();
        assert_eq!(deserialized_kind, EvidenceKind::Commit);
    }

    #[test]
    fn test_audit_claims_against_evidence_verified_path() {
        // non-high-risk claim with direct linked evidence
        let claim = ClaimRecord {
            id: "claim-1".into(),
            project: "aicx".into(),
            source_session: "sess-a".into(),
            source_agent: Some("codex".into()),
            source_role: "assistant".into(),
            source_span: None,
            claim_text: "fixed the database schema".into(),
            claim_type: ClaimType::Fixed,
            claimed_status: "fixed".into(),
            timestamp: Some("2026-06-08T18:06:26Z".into()),
            timestamp_partial: false,
            extracted_at: "2026-06-09T20:41:00Z".into(),
            claimed_files: vec![],
            claimed_commands: vec![],
            claimed_artifacts: vec![],
            related_intents: vec![],
            evidence_refs: vec![],
            verification_status: VerificationStatus::default(),
            risk_flags: vec![],
        };

        let ev = EvidenceRecord {
            id: "ev-1".to_string(),
            kind: EvidenceKind::RuntimeProbe,
            excerpt: "database migration executed successfully".to_string(),
            observed_at: "2026-06-12T02:53:00Z".to_string(),
            anchor: Some("migration command".to_string()),
            claim_id: Some("claim-1".to_string()),
            files_touched: None,
        };

        let results = audit_claims_against_evidence(&[claim], &[ev], "2026-06-12T02:54:00Z");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].result_status, ResultStatus::Pass);
        assert_eq!(results[0].related_claims, vec!["claim-1".to_string()]);
        assert_eq!(results[0].command, Some("migration command".to_string()));
    }

    #[test]
    fn test_audit_claims_against_evidence_contradicted_path() {
        // Claim: "tests green"
        let claim = ClaimRecord {
            id: "claim-green".into(),
            project: "aicx".into(),
            source_session: "sess-a".into(),
            source_agent: Some("codex".into()),
            source_role: "assistant".into(),
            source_span: None,
            claim_text: "tests green".into(),
            claim_type: ClaimType::Green,
            claimed_status: "green".into(),
            timestamp: Some("2026-06-08T18:06:26Z".into()),
            timestamp_partial: false,
            extracted_at: "2026-06-09T20:41:00Z".into(),
            claimed_files: vec![],
            claimed_commands: vec![],
            claimed_artifacts: vec![],
            related_intents: vec![],
            evidence_refs: vec![],
            verification_status: VerificationStatus::default(),
            risk_flags: vec![],
        };

        // Evidence contains a failing test result
        let ev = EvidenceRecord {
            id: "ev-fail".to_string(),
            kind: EvidenceKind::TestRun,
            excerpt:
                "running 10 tests\ntest result: FAILED. 9 passed; 1 failed; 0 ignored; 0 measured"
                    .to_string(),
            observed_at: "2026-06-12T02:53:00Z".to_string(),
            anchor: Some("cargo test".to_string()),
            claim_id: None, // circumstantial/unlinked
            files_touched: None,
        };

        let results = audit_claims_against_evidence(
            std::slice::from_ref(&claim),
            &[ev],
            "2026-06-12T02:54:00Z",
        );
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].result_status, ResultStatus::Fail);
        assert_eq!(results[0].related_claims, vec!["claim-green".to_string()]);

        // Also test verify_claims updates the claim status to Contradicted
        let mut claims = vec![claim];
        verify_claims(&mut claims, &results);
        assert_eq!(
            claims[0].verification_status,
            VerificationStatus::Contradicted
        );
    }

    #[test]
    fn test_audit_claims_against_evidence_unsupported_path() {
        let claim = ClaimRecord {
            id: "claim-unsupported".into(),
            project: "aicx".into(),
            source_session: "sess-a".into(),
            source_agent: Some("codex".into()),
            source_role: "assistant".into(),
            source_span: None,
            claim_text: "fixed a minor spelling mistake".into(),
            claim_type: ClaimType::Fixed,
            claimed_status: "fixed".into(),
            timestamp: Some("2026-06-08T18:06:26Z".into()),
            timestamp_partial: false,
            extracted_at: "2026-06-09T20:41:00Z".into(),
            claimed_files: vec![],
            claimed_commands: vec![],
            claimed_artifacts: vec![],
            related_intents: vec![],
            evidence_refs: vec![],
            verification_status: VerificationStatus::default(),
            risk_flags: vec![],
        };

        // No evidence matching this claim
        let results = audit_claims_against_evidence(&[claim], &[], "2026-06-12T02:54:00Z");
        assert!(results.is_empty());
    }

    #[test]
    fn test_audit_claims_against_evidence_high_risk_gate() {
        // High-risk claim
        let claim = ClaimRecord {
            id: "claim-highrisk".into(),
            project: "aicx".into(),
            source_session: "sess-a".into(),
            source_agent: Some("codex".into()),
            source_role: "assistant".into(),
            source_span: None,
            claim_text: "this is production ready".into(),
            claim_type: ClaimType::ReadyToPush,
            claimed_status: "ready_to_push".into(),
            timestamp: Some("2026-06-08T18:06:26Z".into()),
            timestamp_partial: false,
            extracted_at: "2026-06-09T20:41:00Z".into(),
            claimed_files: vec![],
            claimed_commands: vec![],
            claimed_artifacts: vec![],
            related_intents: vec![],
            evidence_refs: vec![],
            verification_status: VerificationStatus::default(),
            risk_flags: vec!["high_risk_unverified_claim".to_string()],
        };

        // Circumstantial evidence that is passing
        let ev = EvidenceRecord {
            id: "ev-circumstantial".to_string(),
            kind: EvidenceKind::RuntimeProbe,
            excerpt: "production-ready checklist looks good".to_string(),
            observed_at: "2026-06-12T02:53:00Z".to_string(),
            anchor: Some("checklist check".to_string()),
            claim_id: None, // unlinked
            files_touched: None,
        };

        // It should NOT be verified
        let results = audit_claims_against_evidence(
            std::slice::from_ref(&claim),
            std::slice::from_ref(&ev),
            "2026-06-12T02:54:00Z",
        );
        assert!(
            results.is_empty(),
            "High-risk claim with only circumstantial evidence is not verified (no pass result generated)"
        );

        // Now link it directly
        let mut ev_linked = ev;
        ev_linked.claim_id = Some("claim-highrisk".to_string());

        let results_linked =
            audit_claims_against_evidence(&[claim], &[ev_linked], "2026-06-12T02:54:00Z");
        assert_eq!(results_linked.len(), 1);
        assert_eq!(
            results_linked[0].result_status,
            ResultStatus::Pass,
            "Verified with direct linked evidence"
        );
    }

    #[test]
    fn test_detect_contract_fractures_contradicted() {
        let claim = ClaimRecord {
            id: "claim-test-1".into(),
            project: "aicx".into(),
            source_session: "sess-123".into(),
            source_agent: Some("codex".into()),
            source_role: "assistant".into(),
            source_span: None,
            claim_text: "fixed broken connection".into(),
            claim_type: ClaimType::Fixed,
            claimed_status: "fixed".into(),
            timestamp: None,
            timestamp_partial: false,
            extracted_at: "2026-06-12T02:54:00Z".into(),
            claimed_files: vec![],
            claimed_commands: vec![],
            claimed_artifacts: vec![],
            related_intents: vec![],
            evidence_refs: vec![],
            verification_status: VerificationStatus::Unverified,
            risk_flags: vec![],
        };

        let result = ResultRecord {
            id: "result-test-1".into(),
            project: "aicx".into(),
            evidence_type: "test_run".into(),
            command: Some("cargo test".into()),
            exit_status: Some(1),
            artifact_path: None,
            observed_output_excerpt: Some("test failed".into()),
            timestamp: None,
            related_claims: vec!["claim-test-1".into()],
            related_intents: vec![],
            result_status: ResultStatus::Fail,
            confidence: 9,
            reproducibility_notes: None,
        };

        let fractures = detect_contract_fractures(&[], &[claim], &[result]);
        assert_eq!(fractures.len(), 1);
        let f = &fractures[0];
        assert_eq!(f.claim_id, "claim-test-1");
        assert_eq!(f.severity, FractureSeverity::High);
        assert!(f.contract_source.contains("claim-test-1"));
        assert!(f.contract_source.contains("sess-123"));
        assert_eq!(f.promised_surface, "fixed broken connection");
        assert!(f.runtime_surface.contains("contradicts"));
        assert_eq!(f.evidence, vec!["result-test-1".to_string()]);
    }

    #[test]
    fn test_detect_contract_fractures_unsupported_high_risk() {
        let claim = ClaimRecord {
            id: "claim-test-2".into(),
            project: "aicx".into(),
            source_session: "sess-123".into(),
            source_agent: Some("codex".into()),
            source_role: "assistant".into(),
            source_span: None,
            claim_text: "everything is production ready".into(),
            claim_type: ClaimType::ReadyToPush,
            claimed_status: "ready_to_push".into(),
            timestamp: None,
            timestamp_partial: false,
            extracted_at: "2026-06-12T02:54:00Z".into(),
            claimed_files: vec![],
            claimed_commands: vec![],
            claimed_artifacts: vec![],
            related_intents: vec![],
            evidence_refs: vec![],
            verification_status: VerificationStatus::Unverified,
            risk_flags: vec![],
        };

        let fractures = detect_contract_fractures(&[], &[claim], &[]);
        assert_eq!(fractures.len(), 1);
        let f = &fractures[0];
        assert_eq!(f.claim_id, "claim-test-2");
        assert_eq!(f.severity, FractureSeverity::Medium);
        assert_eq!(f.promised_surface, "everything is production ready");
        assert!(f.runtime_surface.contains("no evidence"));
        assert!(f.evidence.is_empty());
    }

    #[test]
    fn test_detect_contract_fractures_orphaned_intent() {
        use super::super::{IntentKind, IntentRecord};

        let intent = IntentRecord {
            kind: IntentKind::Intent,
            summary: "add zero-width character support".to_string(),
            context: None,
            evidence: vec![],
            project: "aicx".to_string(),
            agent: "operator".to_string(),
            date: "2026-06-12".to_string(),
            timestamp: None,
            session_id: "sess-123".to_string(),
            count: None,
            first_chunk: None,
            last_chunk: None,
            source_chunk: "chunk-1".to_string(),
            source: None,
            honesty: Default::default(),
        };

        let fractures = detect_contract_fractures(&[intent], &[], &[]);
        assert_eq!(fractures.len(), 1);
        let f = &fractures[0];
        assert!(f.claim_id.starts_with("intent-orphan-"));
        assert_eq!(f.severity, FractureSeverity::Low);
        assert_eq!(f.promised_surface, "add zero-width character support");
        assert!(f.runtime_surface.contains("no agent claim"));
        assert!(f.evidence.is_empty());
    }

    #[test]
    fn test_detect_contract_fractures_clean_session() {
        use super::super::{IntentKind, IntentRecord};

        let intent = IntentRecord {
            kind: IntentKind::Intent,
            summary: "fix database migration".to_string(),
            context: None,
            evidence: vec![],
            project: "aicx".to_string(),
            agent: "operator".to_string(),
            date: "2026-06-12".to_string(),
            timestamp: None,
            session_id: "sess-123".to_string(),
            count: None,
            first_chunk: None,
            last_chunk: None,
            source_chunk: "chunk-1".to_string(),
            source: None,
            honesty: Default::default(),
        };

        let claim = ClaimRecord {
            id: "claim-test-3".into(),
            project: "aicx".into(),
            source_session: "sess-123".into(),
            source_agent: Some("codex".into()),
            source_role: "assistant".into(),
            source_span: None,
            claim_text: "fixed database migration".into(),
            claim_type: ClaimType::Fixed,
            claimed_status: "fixed".into(),
            timestamp: None,
            timestamp_partial: false,
            extracted_at: "2026-06-12T02:54:00Z".into(),
            claimed_files: vec![],
            claimed_commands: vec![],
            claimed_artifacts: vec![],
            related_intents: vec![],
            evidence_refs: vec![],
            verification_status: VerificationStatus::Unverified,
            risk_flags: vec![],
        };

        let result = ResultRecord {
            id: "result-test-3".into(),
            project: "aicx".into(),
            evidence_type: "runtime_probe".into(),
            command: Some("cargo run".into()),
            exit_status: Some(0),
            artifact_path: None,
            observed_output_excerpt: Some("fixed database migration".into()),
            timestamp: None,
            related_claims: vec!["claim-test-3".into()],
            related_intents: vec![],
            result_status: ResultStatus::Pass,
            confidence: 9,
            reproducibility_notes: None,
        };

        // All matched and verified -> ZERO fractures!
        let fractures = detect_contract_fractures(&[intent], &[claim], &[result]);
        assert_eq!(fractures.len(), 0);
    }

    #[test]
    fn test_generate_clarify_hard_cap_and_priority_order() {
        // Construct 12 candidate fractures of various severities and timestamps,
        // and verify exactly 5 questions come out, sorted by priority first (Critical > High > Medium > Low).
        let mk_frac =
            |id: &str, severity: FractureSeverity, timestamp: Option<&str>| ContractFracture {
                claim_id: id.to_string(),
                contract_source: format!("agent claim {id}"),
                promised_surface: format!("promise {id}"),
                runtime_surface: "absent".to_string(),
                evidence: Vec::new(),
                severity,
                options: vec!["A: repair".to_string(), "B: retract".to_string()],
                recommended_clarify_question: None,
                linked_intents: vec![],
                linked_claims: vec![id.to_string()],
                linked_results: vec![],
                timestamp: timestamp.map(String::from),
            };

        let candidates = vec![
            mk_frac(
                "c-low-1",
                FractureSeverity::Low,
                Some("2026-06-10T12:00:00Z"),
            ),
            mk_frac(
                "c-crit-1",
                FractureSeverity::Critical,
                Some("2026-06-01T12:00:00Z"),
            ),
            mk_frac(
                "c-high-1",
                FractureSeverity::High,
                Some("2026-06-05T12:00:00Z"),
            ),
            mk_frac(
                "c-med-1",
                FractureSeverity::Medium,
                Some("2026-06-03T12:00:00Z"),
            ),
            mk_frac(
                "c-low-2",
                FractureSeverity::Low,
                Some("2026-06-11T12:00:00Z"),
            ),
            mk_frac(
                "c-crit-2",
                FractureSeverity::Critical,
                Some("2026-06-02T12:00:00Z"),
            ),
            mk_frac(
                "c-high-2",
                FractureSeverity::High,
                Some("2026-06-06T12:00:00Z"),
            ),
            mk_frac(
                "c-med-2",
                FractureSeverity::Medium,
                Some("2026-06-04T12:00:00Z"),
            ),
            mk_frac(
                "c-low-3",
                FractureSeverity::Low,
                Some("2026-06-12T12:00:00Z"),
            ),
            mk_frac(
                "c-crit-3",
                FractureSeverity::Critical,
                Some("2026-06-03T12:00:00Z"),
            ),
            mk_frac(
                "c-high-3",
                FractureSeverity::High,
                Some("2026-06-07T12:00:00Z"),
            ),
            mk_frac(
                "c-med-3",
                FractureSeverity::Medium,
                Some("2026-06-05T12:00:00Z"),
            ),
        ];

        let questions = generate_clarify(&candidates, 12);
        assert_eq!(questions.len(), 5);

        // Priority order: Category 1 (Critical > High) > Category 2 (Medium) > Category 3 (Low)
        // Within same priority, sorted by recency (newest first).
        // Criticals:
        // - c-crit-3 (2026-06-03)
        // - c-crit-2 (2026-06-02)
        // - c-crit-1 (2026-06-01)
        // Highs:
        // - c-high-3 (2026-06-07)
        // - c-high-2 (2026-06-06)
        // So the top 5 questions should be:
        // 1. c-crit-3
        // 2. c-crit-2
        // 3. c-crit-1
        // 4. c-high-3
        // 5. c-high-2
        assert_eq!(questions[0].linked_claims[0], "c-crit-3");
        assert_eq!(questions[1].linked_claims[0], "c-crit-2");
        assert_eq!(questions[2].linked_claims[0], "c-crit-1");
        assert_eq!(questions[3].linked_claims[0], "c-high-3");
        assert_eq!(questions[4].linked_claims[0], "c-high-2");
    }

    #[test]
    fn test_generate_clarify_recency_sorting_under_same_priority() {
        let mk_frac = |id: &str, timestamp: Option<&str>| ContractFracture {
            claim_id: id.to_string(),
            contract_source: format!("agent claim {id}"),
            promised_surface: format!("promise {id}"),
            runtime_surface: "absent".to_string(),
            evidence: Vec::new(),
            severity: FractureSeverity::Medium,
            options: vec!["A: repair".to_string(), "B: retract".to_string()],
            recommended_clarify_question: None,
            linked_intents: vec![],
            linked_claims: vec![id.to_string()],
            linked_results: vec![],
            timestamp: timestamp.map(String::from),
        };

        let candidates = vec![
            mk_frac("c-1", Some("2026-06-08T18:00:00Z")),
            mk_frac("c-2", None),
            mk_frac("c-3", Some("2026-06-09T18:00:00Z")),
            mk_frac("c-4", Some("2026-06-07T18:00:00Z")),
            mk_frac("c-5", None),
        ];

        let questions = generate_clarify(&candidates, 5);
        assert_eq!(questions.len(), 5);

        // Sorted by recency (newest first, Nones last)
        // 1. c-3 (2026-06-09)
        // 2. c-1 (2026-06-08)
        // 3. c-4 (2026-06-07)
        // 4. c-2 (None, claim_id tie-break)
        // 5. c-5 (None, claim_id tie-break)
        assert_eq!(questions[0].linked_claims[0], "c-3");
        assert_eq!(questions[1].linked_claims[0], "c-1");
        assert_eq!(questions[2].linked_claims[0], "c-4");
        assert_eq!(questions[3].linked_claims[0], "c-2");
        assert_eq!(questions[4].linked_claims[0], "c-5");
    }

    #[test]
    fn test_generate_clarify_option_quality_gate() {
        let mk_frac = |id: &str, options: Vec<String>| ContractFracture {
            claim_id: id.to_string(),
            contract_source: format!("agent claim {id}"),
            promised_surface: format!("promise {id}"),
            runtime_surface: "absent".to_string(),
            evidence: Vec::new(),
            severity: FractureSeverity::Medium,
            options,
            recommended_clarify_question: None,
            linked_intents: vec![],
            linked_claims: vec![id.to_string()],
            linked_results: vec![],
            timestamp: None,
        };

        let candidates = vec![
            mk_frac("c-degenerate-1", vec!["yes".to_string(), "no".to_string()]),
            mk_frac(
                "c-degenerate-2",
                vec![
                    "A: true".to_string(),
                    "B: false".to_string(),
                    "C: maybe".to_string(),
                ],
            ),
            mk_frac("c-too-few", vec!["only one option".to_string()]),
            mk_frac(
                "c-good",
                vec!["A: keep the promise".to_string(), "B: drop it".to_string()],
            ),
        ];

        let questions = generate_clarify(&candidates, 4);
        assert_eq!(questions.len(), 4);

        // Index 0: degenerate yes/no -> replaced with default options
        assert_eq!(questions[0].linked_claims[0], "c-degenerate-1");
        assert_eq!(
            questions[0].options[0],
            "A: implement/repair to match the promise"
        );
        assert_eq!(questions[0].options[1], "B: retract or descope the promise");

        // Index 1: degenerate true/false/maybe -> replaced with default options
        assert_eq!(questions[1].linked_claims[0], "c-degenerate-2");
        assert_eq!(
            questions[1].options[0],
            "A: implement/repair to match the promise"
        );
        assert_eq!(questions[1].options[1], "B: retract or descope the promise");

        // Index 2: good options -> preserved!
        assert_eq!(questions[2].linked_claims[0], "c-good");
        assert_eq!(questions[2].options[0], "A: keep the promise");
        assert_eq!(questions[2].options[1], "B: drop it");

        // Index 3: too few options -> replaced with default options
        assert_eq!(questions[3].linked_claims[0], "c-too-few");
        assert_eq!(
            questions[3].options[0],
            "A: implement/repair to match the promise"
        );
        assert_eq!(questions[3].options[1], "B: retract or descope the promise");
    }

    #[test]
    fn test_generate_clarify_anchor_linkage_and_serde_roundtrip() {
        let fracture = ContractFracture {
            claim_id: "claim-test-123".to_string(),
            contract_source: "agent claim claim-test-123".to_string(),
            promised_surface: "some promise".to_string(),
            runtime_surface: "some runtime".to_string(),
            evidence: vec!["result-evidence-1".to_string()],
            severity: FractureSeverity::High,
            options: vec!["A: opt1".to_string(), "B: opt2".to_string()],
            recommended_clarify_question: Some("clarify-question-123".to_string()),
            linked_intents: vec!["intent-abc".to_string()],
            linked_claims: vec!["claim-test-123".to_string()],
            linked_results: vec!["result-evidence-1".to_string()],
            timestamp: Some("2026-06-12T03:00:00Z".to_string()),
        };

        let questions = generate_clarify(&[fracture], 1);
        assert_eq!(questions.len(), 1);
        let q = &questions[0];

        // Verify anchor linkage fields are set correctly
        assert_eq!(q.linked_intents, vec!["intent-abc".to_string()]);
        assert_eq!(q.linked_claims, vec!["claim-test-123".to_string()]);
        assert_eq!(q.linked_results, vec!["result-evidence-1".to_string()]);

        // Serde roundtrip check
        let json_str = serde_json::to_string(&q).expect("Should serialize successfully");
        let deserialized: ClarifyQuestion =
            serde_json::from_str(&json_str).expect("Should deserialize successfully");
        assert_eq!(q, &deserialized);
    }
}