axiom-truth 0.15.0

Axiom — the truth layer: JTBD decoding, truth packages, validation, verifier reports, and calibration
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
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
// Copyright 2024-2026 Reflective Labs
// SPDX-License-Identifier: MIT
// See LICENSE file in the project root for full license information.

//! Truth Package spine types.
//!
//! This module is the v0.15 Axiom release surface for JTBD-as-source package
//! generation, verifier reports, observation adapter receipts, lineage checks,
//! and decoder calibration. It intentionally stays app-neutral: apps own raw
//! transcripts and adapters, Helm owns operator surfaces, Organism owns
//! Formation selection, and Converge owns promotion authority.

use crate::jtbd::JTBDMetadata;
use crate::{CompileError, compile_intent, parse_truth_document};
use chrono::{DateTime, Duration, Utc};
use converge_pack::{ContextFact, FactEvidenceRef, FactTraceLink};
use organism_pack::{ForbiddenAction, IntentPacket};
use serde::{Deserialize, Serialize};
use serde_json::json;
use sha2::{Digest, Sha256};
use std::collections::{BTreeMap, BTreeSet};
use uuid::Uuid;

const DECODER_VERSION: &str = "0.10.0";
const TRUTH_VERSION: &str = "v1";
/// Deterministic epoch used as the anchor for generated `.truths` `Expires:`
/// values. The IntentPacket's `expires` timestamp is `EPOCH + time_budget`
/// when the JTBD declares a budget, and `EPOCH` otherwise. The runtime carries
/// the budget separately via `IntentPacket.context["time_budget_seconds"]`.
const DETERMINISTIC_EXPIRES_EPOCH: &str = "2099-01-01T00:00:00Z";

/// JTBD-declared time budget the runtime must enforce for a job.
///
/// v0.11 carries only a duration in seconds. Richer expiry semantics
/// (deadline-relative-to-event, business-day windows, etc.) are deferred until
/// a marquee job demonstrates they are needed. Presence makes the
/// `TimeBudgetExhausted` stop reason reachable on a real run, which in turn
/// makes the `Exhausted` verdict reachable in `AxiomRunReport`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct TimeBudget(u64);

impl TimeBudget {
    pub const fn from_seconds(seconds: u64) -> Self {
        Self(seconds)
    }

    pub const fn from_minutes(minutes: u64) -> Self {
        Self(minutes * 60)
    }

    pub const fn from_hours(hours: u64) -> Self {
        Self(hours * 3600)
    }

    pub const fn as_seconds(self) -> u64 {
        self.0
    }
}

fn deterministic_expires_line(budget: Option<TimeBudget>) -> String {
    let epoch: DateTime<Utc> = DateTime::parse_from_rfc3339(DETERMINISTIC_EXPIRES_EPOCH)
        .expect("DETERMINISTIC_EXPIRES_EPOCH is a valid RFC-3339 timestamp")
        .with_timezone(&Utc);
    let expires = match budget {
        Some(b) => {
            let seconds = i64::try_from(b.as_seconds()).unwrap_or(i64::MAX);
            epoch + Duration::seconds(seconds)
        }
        None => epoch,
    };
    expires.format("%Y-%m-%dT%H:%M:%SZ").to_string()
}

/// Structured JTBD input supplied by a human or a higher-level authoring UI.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JtbdInput {
    /// Stable job key used as the root of package-local clause IDs.
    pub key: String,
    /// Actor trying to make progress.
    pub actor: String,
    /// Functional job the actor wants done.
    pub functional_job: String,
    /// Downstream value or risk reduction the job serves.
    pub so_that: String,
    /// Evidence that must exist before the job can be treated as satisfied.
    #[serde(default)]
    pub evidence_required: Vec<ClauseInput>,
    /// Failure modes the package must guard against.
    #[serde(default)]
    pub failure_modes: Vec<ClauseInput>,
    /// Optional time budget the runtime must enforce. When present, the
    /// generated `.truths` `Expires:` is anchored at `EPOCH + time_budget` and
    /// the IntentPacket context carries `time_budget_seconds` so a runtime can
    /// produce `TimeBudgetExhausted` honestly. Absent budgets fall back to the
    /// unbounded epoch sentinel.
    #[serde(default)]
    pub time_budget: Option<TimeBudget>,
}

impl JtbdInput {
    pub fn new(
        key: impl Into<String>,
        actor: impl Into<String>,
        functional_job: impl Into<String>,
        so_that: impl Into<String>,
    ) -> Self {
        Self {
            key: key.into(),
            actor: actor.into(),
            functional_job: functional_job.into(),
            so_that: so_that.into(),
            evidence_required: Vec::new(),
            failure_modes: Vec::new(),
            time_budget: None,
        }
    }

    /// Builder helper: attach a time budget to a JTBD input.
    #[must_use]
    pub fn with_time_budget(mut self, budget: TimeBudget) -> Self {
        self.time_budget = Some(budget);
        self
    }

    /// Convert legacy `.truths` JTBD metadata into the new structured source
    /// shape. The caller supplies the package-local job key.
    pub fn from_metadata(key: impl Into<String>, metadata: &JTBDMetadata) -> Self {
        Self {
            key: key.into(),
            actor: metadata.actor.clone(),
            functional_job: metadata.job_functional.clone(),
            so_that: metadata.so_that.clone(),
            evidence_required: metadata
                .evidence_required
                .iter()
                .cloned()
                .map(ClauseInput::new)
                .collect(),
            failure_modes: metadata
                .failure_modes
                .iter()
                .cloned()
                .map(ClauseInput::new)
                .collect(),
            time_budget: None,
        }
    }
}

/// A clause in a collection field. Explicit keys preserve identity across
/// substantial wording changes; absent keys are derived deterministically from
/// canonical text.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClauseInput {
    pub key: Option<String>,
    pub text: String,
}

impl ClauseInput {
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            key: None,
            text: text.into(),
        }
    }

    pub fn with_key(key: impl Into<String>, text: impl Into<String>) -> Self {
        Self {
            key: Some(key.into()),
            text: text.into(),
        }
    }
}

/// Canonical structured JTBD document with deterministic clause IDs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JtbdDocument {
    pub key: String,
    pub clauses: Vec<JtbdClause>,
    /// JTBD-declared time budget the runtime must enforce. Not a clause —
    /// budgets are policy, not job content — but participates in deterministic
    /// package regeneration through the `.truths` `Expires:` value and the
    /// IntentPacket context.
    #[serde(default)]
    pub time_budget: Option<TimeBudget>,
}

impl JtbdDocument {
    pub fn from_input(input: JtbdInput) -> Result<Self, TruthPackageError> {
        let key = normalized_key(&input.key, "job");
        let mut clauses = vec![
            scalar_clause(&key, JtbdClauseKind::Actor, "actor", input.actor)?,
            scalar_clause(
                &key,
                JtbdClauseKind::FunctionalJob,
                "functional_job",
                input.functional_job,
            )?,
            scalar_clause(&key, JtbdClauseKind::SoThat, "so_that", input.so_that)?,
        ];

        clauses.extend(collection_clauses(
            &key,
            JtbdClauseKind::EvidenceRequired,
            "evidence",
            input.evidence_required,
        )?);
        clauses.extend(collection_clauses(
            &key,
            JtbdClauseKind::FailureMode,
            "failure",
            input.failure_modes,
        )?);

        ensure_unique_clause_ids(&clauses)?;

        Ok(Self {
            key,
            clauses,
            time_budget: input.time_budget,
        })
    }

    pub fn clause_ids(&self) -> impl Iterator<Item = &ClauseId> {
        self.clauses.iter().map(|clause| &clause.id)
    }

    pub fn clause(&self, id: &ClauseId) -> Option<&JtbdClause> {
        self.clauses.iter().find(|clause| &clause.id == id)
    }

    pub fn clauses_by_kind(&self, kind: JtbdClauseKind) -> impl Iterator<Item = &JtbdClause> {
        self.clauses
            .iter()
            .filter(move |clause| clause.kind == kind)
    }

    fn known_clause_ids(&self) -> BTreeSet<ClauseId> {
        self.clause_ids().cloned().collect()
    }
}

/// One canonical JTBD clause.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JtbdClause {
    pub id: ClauseId,
    pub kind: JtbdClauseKind,
    pub key: String,
    pub text: String,
    pub canonical_text: String,
    pub fingerprint: ClauseFingerprint,
}

/// Clause category used by deterministic decoder rules.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum JtbdClauseKind {
    Actor,
    FunctionalJob,
    SoThat,
    EvidenceRequired,
    FailureMode,
}

/// Deterministic, package-local clause address.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ClauseId(String);

impl ClauseId {
    pub fn new(root_key: &str, path: &str) -> Self {
        Self(format!("jtbd.{root_key}.{path}"))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for ClauseId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

/// SHA-256 hash of canonical clause text.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ClauseFingerprint(String);

impl ClauseFingerprint {
    pub fn from_text(text: &str) -> Self {
        let canonical = canonicalize_clause_text(text);
        let mut hasher = Sha256::new();
        hasher.update(canonical.as_bytes());
        Self(hex_lower(&hasher.finalize()))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }

    pub fn short(&self) -> &str {
        &self.0[..12]
    }
}

/// Artifact identity in a Truth Package lineage map.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ArtifactId(String);

impl ArtifactId {
    pub fn new(value: impl Into<String>) -> Self {
        Self(value.into())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for ArtifactId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

/// Deterministic Truth Package identity.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct TruthPackageId(String);

impl TruthPackageId {
    pub fn new(value: impl Into<String>) -> Self {
        Self(value.into())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for TruthPackageId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

/// Artifact category for lineage review.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ArtifactKind {
    TruthPackageManifest,
    TruthProjection,
    Scenario,
    Predicate,
    PolicyRequirement,
    InvariantArtifact,
    SimulationCase,
    CalibrationSuggestion,
    CalibrationConcern,
    ReplayProfile,
    IntentField,
    ProofObligation,
    VerifierExpectation,
}

/// Deterministic Truth Package manifest produced by the Axiom decoder.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TruthPackage {
    pub package_id: TruthPackageId,
    pub truth_version: String,
    pub source_jtbd: JtbdDocument,
    pub generated_truths: String,
    pub artifacts: TruthPackageArtifacts,
    pub intent_packet: IntentPacket,
    pub proof_obligations: Vec<ProofObligation>,
    pub verifier_spec: VerifierSpec,
    pub replay_profile: ReplayProfile,
    pub lineage: LineageMap,
}

impl TruthPackage {
    /// Return the immutable generated `.truths` projection as a versioned view.
    pub fn base_projection(&self) -> TruthProjectionVersion {
        let source_clause_ids: Vec<ClauseId> = self.source_jtbd.clause_ids().cloned().collect();
        TruthProjectionVersion {
            package_id: self.package_id.clone(),
            base_truth_version: self.truth_version.clone(),
            projection_version: self.truth_version.clone(),
            truths: self.generated_truths.clone(),
            source: TruthProjectionSource::BaseGenerated,
            lineage: ArtifactLineage::new(
                ArtifactId::new(format!("truth_projection.{}", self.source_jtbd.key)),
                ArtifactKind::TruthProjection,
                source_clause_ids,
                "truth_projection.v0",
                DECODER_VERSION,
                &self.source_jtbd,
            ),
        }
    }

    /// Apply a human-authored `.truths` projection overlay without mutating the
    /// immutable generated package. The returned projection is a versioned view
    /// over the package, not a replacement for `generated_truths`.
    pub fn apply_projection_overlay(
        &self,
        overlay: TruthProjectionOverlay,
    ) -> Result<TruthProjectionVersion, TruthOverlayError> {
        apply_truth_projection_overlay(self, overlay)
    }

    /// Compute the post-run verdict for an `AxiomRunObservation` against this
    /// package's verifier spec.
    ///
    /// The verdict reflects only what can be judged from the observation. Deep
    /// authority recompute, invariant enforcement, and promotion gating remain
    /// Converge's responsibility — this function reads what the run reported
    /// and decides whether the contract was kept.
    ///
    /// Precedence:
    /// 1. Promoted facts citing unknown clauses are a lineage violation →
    ///    `Invalid`.
    /// 2. Forbidden action text observed in promoted fact summaries or replay
    ///    notes → `Invalid`. Sharper enforcement runs through typed invariant
    ///    violations carried by the observed stop reason.
    /// 3. Unexpected stop reason → categorize: invalid-class variants
    ///    (`InvariantViolated`, `PromotionRejected`, `RuntimeError`,
    ///    `AgentRefused`) → `Invalid`; budget exhaustion → `Exhausted`;
    ///    HITL or human intervention → `Blocked`; everything else → `Invalid`.
    /// 4. Expected stop reason: every `EvidenceRequired` clause must be cited
    ///    by at least one promoted fact's `source_clause_ids`; otherwise the
    ///    verdict is `Invalid` (the contract specified evidence the run did
    ///    not produce). All conditions met → `Satisfied`.
    pub fn verify(&self, observation: &AxiomRunObservation) -> AxiomRunVerdict {
        compute_verdict(self, observation)
    }
}

/// Artifact groups reserved by the Truth Package contract.
///
/// The release decoder fills the deterministic spine plus reviewed calibration
/// suggestion and concern artifacts. Later decoders can populate richer
/// scenario, predicate, policy, simulation, and invariant artifacts without
/// changing package identity rules.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TruthPackageArtifacts {
    pub scenarios: Vec<GeneratedArtifact>,
    pub predicates: Vec<GeneratedArtifact>,
    pub policy_requirements: Vec<GeneratedArtifact>,
    pub evidence_expectations: Vec<GeneratedArtifact>,
    pub simulation_cases: Vec<GeneratedArtifact>,
    pub invariant_expectations: Vec<GeneratedArtifact>,
    #[serde(default)]
    pub calibration_suggestions: Vec<GeneratedArtifact>,
    /// Reviewed decoder *concerns* — clauses the runtime was required to
    /// satisfy but did not. v0.15 introduces this as a typed signal: future
    /// decoded packages may add prompts, defaults, warnings, or alternate
    /// scaffolding for these clause shapes. The source JTBD's evidence
    /// requirements are never weakened by accepting a concern.
    #[serde(default)]
    pub calibration_concerns: Vec<GeneratedArtifact>,
}

/// Reviewable generated artifact summary.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GeneratedArtifact {
    pub artifact_id: ArtifactId,
    pub artifact_kind: ArtifactKind,
    pub summary: String,
    pub source_clause_ids: Vec<ClauseId>,
}

/// Human-authored override for the generated `.truths` projection.
///
/// The overlay is separate from the package so regeneration remains idempotent:
/// the same JTBD still produces the same base package, and reviewable human
/// edits live in their own versioned artifact.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TruthProjectionOverlay {
    pub overlay_id: ArtifactId,
    pub target_package_id: TruthPackageId,
    pub target_truth_version: String,
    pub projection_version: String,
    pub edited_truths: String,
    pub reason: String,
    pub source_clause_ids: Vec<ClauseId>,
}

impl TruthProjectionOverlay {
    pub fn new(
        target_package_id: TruthPackageId,
        target_truth_version: impl Into<String>,
        projection_version: impl Into<String>,
        edited_truths: impl Into<String>,
        reason: impl Into<String>,
        source_clause_ids: Vec<ClauseId>,
    ) -> Self {
        let target_truth_version = target_truth_version.into();
        let projection_version = projection_version.into();
        let edited_truths = edited_truths.into();
        let overlay_id = overlay_id_for(
            &target_package_id,
            &target_truth_version,
            &projection_version,
            &edited_truths,
        );
        Self {
            overlay_id,
            target_package_id,
            target_truth_version,
            projection_version,
            edited_truths,
            reason: reason.into(),
            source_clause_ids,
        }
    }
}

/// Versioned `.truths` projection view. `BaseGenerated` is the deterministic
/// package output; `OverlayApplied` is a human-authored edit layered above it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TruthProjectionVersion {
    pub package_id: TruthPackageId,
    pub base_truth_version: String,
    pub projection_version: String,
    pub truths: String,
    pub source: TruthProjectionSource,
    pub lineage: ArtifactLineage,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TruthProjectionSource {
    BaseGenerated,
    OverlayApplied {
        overlay_id: ArtifactId,
        reason: String,
    },
}

/// Obligation that must be checked by the verifier or by downstream runtime
/// evidence.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProofObligation {
    pub artifact_id: ArtifactId,
    pub kind: ProofObligationKind,
    pub description: String,
    pub source_clause_ids: Vec<ClauseId>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProofObligationKind {
    EvidenceRequired,
    FailureGuard,
}

/// Post-run expectations. This is data only; live run wiring is deferred.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerifierSpec {
    pub expected_stop_reasons: BTreeSet<ExpectedStopReason>,
    pub required_evidence: Vec<String>,
    pub forbidden_actions: Vec<ForbiddenAction>,
    pub satisfaction_conditions: Vec<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ExpectedStopReason {
    Converged,
    CriteriaMet,
    UserCancelled,
    HumanInterventionRequired,
    CycleBudgetExhausted,
    FactBudgetExhausted,
    TokenBudgetExhausted,
    TimeBudgetExhausted,
    InvariantViolated,
    PromotionRejected,
    RuntimeError,
    AgentRefused,
    HitlGatePending,
}

/// Deterministic replay metadata for the package spine.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReplayProfile {
    pub profile_id: ArtifactId,
    pub deterministic: bool,
    pub input_clause_ids: Vec<ClauseId>,
}

/// Post-run verifier verdict emitted by Axiom after comparing an observed run
/// against a Truth Package's verifier spec.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AxiomRunVerdict {
    Satisfied,
    Blocked,
    Exhausted,
    Invalid,
}

/// Converge stop reason shape captured without depending on Converge internals.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ObservedStopReason {
    Converged,
    CriteriaMet {
        criteria: Vec<String>,
    },
    UserCancelled,
    HumanInterventionRequired {
        criteria: Vec<String>,
        approval_refs: Vec<String>,
    },
    CycleBudgetExhausted {
        cycles_executed: u32,
        limit: u32,
    },
    FactBudgetExhausted {
        facts_count: u32,
        limit: u32,
    },
    TokenBudgetExhausted {
        tokens_consumed: u64,
        limit: u64,
    },
    TimeBudgetExhausted {
        duration_ms: u64,
        limit_ms: u64,
    },
    InvariantViolated {
        class: String,
        name: String,
        reason: String,
    },
    PromotionRejected {
        proposal_id: String,
        reason: String,
    },
    RuntimeError {
        message: String,
        category: String,
    },
    AgentRefused {
        agent_id: String,
        reason: String,
    },
    HitlGatePending {
        gate_id: String,
        proposal_id: String,
        summary: String,
        agent_id: String,
        cycle: u32,
    },
}

impl ObservedStopReason {
    pub fn expectation_kind(&self) -> ExpectedStopReason {
        match self {
            Self::Converged => ExpectedStopReason::Converged,
            Self::CriteriaMet { .. } => ExpectedStopReason::CriteriaMet,
            Self::UserCancelled => ExpectedStopReason::UserCancelled,
            Self::HumanInterventionRequired { .. } => ExpectedStopReason::HumanInterventionRequired,
            Self::CycleBudgetExhausted { .. } => ExpectedStopReason::CycleBudgetExhausted,
            Self::FactBudgetExhausted { .. } => ExpectedStopReason::FactBudgetExhausted,
            Self::TokenBudgetExhausted { .. } => ExpectedStopReason::TokenBudgetExhausted,
            Self::TimeBudgetExhausted { .. } => ExpectedStopReason::TimeBudgetExhausted,
            Self::InvariantViolated { .. } => ExpectedStopReason::InvariantViolated,
            Self::PromotionRejected { .. } => ExpectedStopReason::PromotionRejected,
            Self::RuntimeError { .. } => ExpectedStopReason::RuntimeError,
            Self::AgentRefused { .. } => ExpectedStopReason::AgentRefused,
            Self::HitlGatePending { .. } => ExpectedStopReason::HitlGatePending,
        }
    }

    pub fn matches_expected(&self, expected: &BTreeSet<ExpectedStopReason>) -> bool {
        expected.contains(&self.expectation_kind())
    }

    pub fn is_budget_exhausted(&self) -> bool {
        matches!(
            self,
            Self::CycleBudgetExhausted { .. }
                | Self::FactBudgetExhausted { .. }
                | Self::TokenBudgetExhausted { .. }
                | Self::TimeBudgetExhausted { .. }
        )
    }

    pub fn is_blocked(&self) -> bool {
        matches!(
            self,
            Self::HumanInterventionRequired { .. } | Self::HitlGatePending { .. }
        )
    }

    pub fn is_invalid(&self) -> bool {
        matches!(
            self,
            Self::InvariantViolated { .. }
                | Self::PromotionRejected { .. }
                | Self::RuntimeError { .. }
                | Self::AgentRefused { .. }
        )
    }
}

/// One promoted fact as rendered by an Axiom run report.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PromotedFactRecord {
    pub context_key: String,
    pub fact_id: String,
    pub summary: String,
    pub source_clause_ids: Vec<ClauseId>,
    pub evidence_refs: Vec<EvidenceRefRecord>,
    pub trace_link: Option<TraceLinkRecord>,
    #[serde(default)]
    pub promotion_authority: Option<PromotionAuthorityRecord>,
}

impl PromotedFactRecord {
    pub fn from_context_fact(fact: &ContextFact, source_clause_ids: Vec<ClauseId>) -> Self {
        let promotion = fact.promotion_record();
        Self {
            context_key: format!("{:?}", fact.key()),
            fact_id: fact.id().as_str().to_string(),
            summary: fact.text().map_or_else(
                || format!("{} v{}", fact.payload_family(), fact.payload_version()),
                ToString::to_string,
            ),
            source_clause_ids,
            evidence_refs: promotion
                .evidence_refs()
                .iter()
                .map(evidence_ref_record)
                .collect(),
            trace_link: Some(trace_link_record(promotion.trace_link())),
            promotion_authority: Some(PromotionAuthorityRecord {
                gate_id: promotion.gate_id().as_str().to_string(),
                policy_version_hash: promotion.policy_version_hash().to_hex(),
                approver_id: promotion.approver().id().as_str().to_string(),
                approver_kind: format!("{:?}", promotion.approver().kind()),
            }),
        }
    }
}

/// Evidence reference attached to a promoted fact.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EvidenceRefRecord {
    pub evidence_id: String,
    pub source: String,
}

/// Replay trace link attached to a promoted fact.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TraceLinkRecord {
    pub trace_id: String,
    pub location: Option<String>,
    pub replayable: bool,
}

/// Promotion authority Converge observed when a fact became authoritative.
///
/// This is report evidence, not delegated authority. Axiom declares
/// requirements in the Truth Package; Converge still recomputes authority at
/// promotion and records the gate/policy identity here.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PromotionAuthorityRecord {
    pub gate_id: String,
    pub policy_version_hash: String,
    pub approver_id: String,
    pub approver_kind: String,
}

/// Integrity proof summary captured from the Converge run boundary.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RunIntegrityProof {
    pub merkle_root: String,
    pub clock_time: u64,
    pub fact_count: usize,
    pub algorithm: String,
}

impl RunIntegrityProof {
    pub fn sha256_merkle(
        merkle_root: impl Into<String>,
        clock_time: u64,
        fact_count: usize,
    ) -> Self {
        Self {
            merkle_root: merkle_root.into(),
            clock_time,
            fact_count,
            algorithm: "sha256-merkle".to_string(),
        }
    }
}

/// Wiring-free observation of a run that Axiom can package into a report.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AxiomRunObservation {
    pub stop_reason: ObservedStopReason,
    pub promoted_facts: Vec<PromotedFactRecord>,
    pub integrity: RunIntegrityProof,
    pub replay_notes: Vec<String>,
    #[serde(default)]
    pub run_stages: Vec<AxiomRunStageRecord>,
}

impl AxiomRunObservation {
    pub fn from_stages(
        stop_reason: ObservedStopReason,
        integrity: RunIntegrityProof,
        replay_notes: Vec<String>,
        run_stages: Vec<AxiomRunStageRecord>,
    ) -> Self {
        let promoted_facts = run_stages
            .iter()
            .flat_map(|stage| stage.promoted_facts.iter().cloned())
            .collect();

        Self {
            stop_reason,
            promoted_facts,
            integrity,
            replay_notes,
            run_stages,
        }
    }
}

/// One reportable execution stage inside a larger job run.
///
/// A job may run through more than one Converge boundary. For example, a
/// dynamic design Formation can converge before a selected work Formation runs.
/// The top-level report still carries the overall observed stop reason; stages
/// preserve the intermediate stop reasons, promoted facts, trace links, and
/// integrity proofs that explain how the job got there.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AxiomRunStageRecord {
    pub stage_id: String,
    pub formation_id: Option<String>,
    pub observed_stop_reason: ObservedStopReason,
    pub promoted_facts: Vec<PromotedFactRecord>,
    pub integrity: RunIntegrityProof,
    pub replay_notes: Vec<String>,
}

/// Result status for an app-specific adapter that maps raw app/runtime output
/// into an [`AxiomRunObservation`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ObservationAdapterStatus {
    Succeeded,
    Rejected,
}

impl ObservationAdapterStatus {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Succeeded => "succeeded",
            Self::Rejected => "rejected",
        }
    }
}

/// App-neutral input used to construct an [`ObservationAdapterReceipt`].
///
/// Apps still own raw transcript schemas and mapping logic. This input names
/// the deterministic audit envelope Axiom and Helm can share without importing
/// app crates or storing raw transcript bodies.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ObservationAdapterReceiptInput {
    pub adapter_id: String,
    pub adapter_version: String,
    pub status: ObservationAdapterStatus,
    pub source_app: String,
    pub source_run_id: String,
    pub source_transcript_ref: String,
    pub source_transcript_hash: String,
    pub package_id: TruthPackageId,
    pub truth_version: String,
    pub domain_hint: String,
    pub observation_hash: Option<String>,
    pub mapped_fact_ids: Vec<String>,
    pub mapped_clause_ids: Vec<ClauseId>,
    pub dropped_source_fields: Vec<String>,
    pub warnings: Vec<String>,
    pub errors: Vec<String>,
    pub replay_notes: Vec<String>,
}

/// Deterministic audit receipt for an app adapter execution.
///
/// A successful adapter should pair this receipt with the produced
/// [`AxiomRunObservation`]. A rejected adapter should emit the receipt with
/// `status: Rejected`, explicit errors, and no observation hash. The receipt is
/// intentionally backlink-oriented: it stores ids, refs, hashes, mapped fact
/// ids, and mapped clause ids, but not raw app transcript bodies.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ObservationAdapterReceipt {
    pub receipt_id: ArtifactId,
    pub adapter_id: String,
    pub adapter_version: String,
    pub status: ObservationAdapterStatus,
    pub source_app: String,
    pub source_run_id: String,
    pub source_transcript_ref: String,
    pub source_transcript_hash: String,
    pub package_id: TruthPackageId,
    pub truth_version: String,
    pub domain_hint: String,
    pub observation_hash: Option<String>,
    pub mapped_fact_ids: Vec<String>,
    pub mapped_clause_ids: Vec<ClauseId>,
    pub dropped_source_fields: Vec<String>,
    pub warnings: Vec<String>,
    pub errors: Vec<String>,
    pub replay_notes: Vec<String>,
}

impl ObservationAdapterReceipt {
    pub fn new(input: ObservationAdapterReceiptInput) -> Self {
        let receipt_id = observation_adapter_receipt_id(&input);
        Self {
            receipt_id,
            adapter_id: input.adapter_id,
            adapter_version: input.adapter_version,
            status: input.status,
            source_app: input.source_app,
            source_run_id: input.source_run_id,
            source_transcript_ref: input.source_transcript_ref,
            source_transcript_hash: input.source_transcript_hash,
            package_id: input.package_id,
            truth_version: input.truth_version,
            domain_hint: input.domain_hint,
            observation_hash: input.observation_hash,
            mapped_fact_ids: input.mapped_fact_ids,
            mapped_clause_ids: input.mapped_clause_ids,
            dropped_source_fields: input.dropped_source_fields,
            warnings: input.warnings,
            errors: input.errors,
            replay_notes: input.replay_notes,
        }
    }
}

fn evidence_ref_record(evidence_ref: &FactEvidenceRef) -> EvidenceRefRecord {
    match evidence_ref {
        FactEvidenceRef::Observation(id) => EvidenceRefRecord {
            evidence_id: id.as_str().to_string(),
            source: "observation".to_string(),
        },
        FactEvidenceRef::HumanApproval(id) => EvidenceRefRecord {
            evidence_id: id.as_str().to_string(),
            source: "human_approval".to_string(),
        },
        FactEvidenceRef::Derived(id) => EvidenceRefRecord {
            evidence_id: id.as_str().to_string(),
            source: "derived".to_string(),
        },
    }
}

fn trace_link_record(trace_link: &FactTraceLink) -> TraceLinkRecord {
    match trace_link {
        FactTraceLink::Local(local) => TraceLinkRecord {
            trace_id: local.trace_id().as_str().to_string(),
            location: Some(format!("span:{}", local.span_id().as_str())),
            replayable: true,
        },
        FactTraceLink::Remote(remote) => TraceLinkRecord {
            trace_id: remote.reference().as_str().to_string(),
            location: Some(remote.system().as_str().to_string()),
            replayable: false,
        },
    }
}

/// Auditable post-run report over an app-neutral observation.
///
/// Callers adapt app or runtime output into [`AxiomRunObservation`], then use
/// [`AxiomRunReport::verify`] to compute the verdict against a
/// [`TruthPackage`]. Axiom owns the normalized verifier semantics; callers own
/// raw transcripts and adapter execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AxiomRunReport {
    pub package_id: TruthPackageId,
    pub truth_version: String,
    pub intent_packet_id: Uuid,
    pub source_clause_ids: Vec<ClauseId>,
    pub verifier_spec: VerifierSpec,
    pub verdict: AxiomRunVerdict,
    pub observed_stop_reason: ObservedStopReason,
    pub promoted_facts: Vec<PromotedFactRecord>,
    pub integrity: RunIntegrityProof,
    pub replay_profile_id: ArtifactId,
    pub replay_notes: Vec<String>,
    #[serde(default)]
    pub run_stages: Vec<AxiomRunStageRecord>,
}

impl AxiomRunReport {
    pub fn from_observation(
        package: &TruthPackage,
        verdict: AxiomRunVerdict,
        observation: AxiomRunObservation,
    ) -> Self {
        Self {
            package_id: package.package_id.clone(),
            truth_version: package.truth_version.clone(),
            intent_packet_id: package.intent_packet.id,
            source_clause_ids: package.source_jtbd.clause_ids().cloned().collect(),
            verifier_spec: package.verifier_spec.clone(),
            verdict,
            observed_stop_reason: observation.stop_reason,
            promoted_facts: observation.promoted_facts,
            integrity: observation.integrity,
            replay_profile_id: package.replay_profile.profile_id.clone(),
            replay_notes: observation.replay_notes,
            run_stages: observation.run_stages,
        }
    }

    /// Compute the verdict for `observation` against `package`'s verifier spec
    /// and build the corresponding `AxiomRunReport`.
    ///
    /// This is the canonical v0.11 entry point: callers with a raw
    /// `AxiomRunObservation` (hand-built, replayed, or adapted from a Converge
    /// run record) should prefer `verify` to `from_observation`. The latter
    /// remains available for adapters that have already computed the verdict
    /// elsewhere.
    pub fn verify(package: &TruthPackage, observation: AxiomRunObservation) -> Self {
        let verdict = package.verify(&observation);
        Self::from_observation(package, verdict, observation)
    }

    pub fn expected_stop_reason_matched(&self) -> bool {
        self.observed_stop_reason
            .matches_expected(&self.verifier_spec.expected_stop_reasons)
    }

    pub fn stage(&self, stage_id: &str) -> Option<&AxiomRunStageRecord> {
        self.run_stages
            .iter()
            .find(|stage| stage.stage_id == stage_id)
    }

    /// Prove that every promoted fact in this report traces back to the source
    /// JTBD clauses it served and that the truth version is consistent.
    ///
    /// Checks performed:
    /// - report `package_id` matches `package.package_id`
    /// - report `truth_version` matches `package.truth_version`
    /// - every `source_clause_ids` entry on every promoted fact is a known
    ///   clause in the package
    /// - every promoted fact cites at least one `EvidenceRequired` or
    ///   `FailureMode` clause it serves (scope-only facts that cite only
    ///   `Actor` / `FunctionalJob` / `SoThat` are rejected — facts must
    ///   discharge an evidence requirement or guard a failure mode)
    ///
    /// On success returns a `FactLineageAudit` summarizing which evidence and
    /// failure-mode clauses were covered by the run's facts.
    pub fn audit_fact_lineage(
        &self,
        package: &TruthPackage,
    ) -> Result<FactLineageAudit, FactLineageAuditError> {
        if self.package_id != package.package_id {
            return Err(FactLineageAuditError::PackageMismatch {
                report: self.package_id.clone(),
                package: package.package_id.clone(),
            });
        }
        if self.truth_version != package.truth_version {
            return Err(FactLineageAuditError::TruthVersionMismatch {
                report: self.truth_version.clone(),
                package: package.truth_version.clone(),
            });
        }

        let known_ids: BTreeSet<&ClauseId> = package
            .source_jtbd
            .clauses
            .iter()
            .map(|clause| &clause.id)
            .collect();
        let evidence_ids: BTreeSet<&ClauseId> = package
            .source_jtbd
            .clauses_by_kind(JtbdClauseKind::EvidenceRequired)
            .map(|clause| &clause.id)
            .collect();
        let failure_ids: BTreeSet<&ClauseId> = package
            .source_jtbd
            .clauses_by_kind(JtbdClauseKind::FailureMode)
            .map(|clause| &clause.id)
            .collect();

        let mut evidence_coverage: BTreeSet<ClauseId> = BTreeSet::new();
        let mut failure_coverage: BTreeSet<ClauseId> = BTreeSet::new();

        for fact in &self.promoted_facts {
            let mut serves_contract = false;
            for clause_id in &fact.source_clause_ids {
                if !known_ids.contains(clause_id) {
                    return Err(FactLineageAuditError::UnknownClause {
                        fact_id: fact.fact_id.clone(),
                        clause_id: clause_id.clone(),
                    });
                }
                if evidence_ids.contains(clause_id) {
                    evidence_coverage.insert(clause_id.clone());
                    serves_contract = true;
                }
                if failure_ids.contains(clause_id) {
                    failure_coverage.insert(clause_id.clone());
                    serves_contract = true;
                }
            }
            if !serves_contract {
                return Err(FactLineageAuditError::ScopeOnlyFact {
                    fact_id: fact.fact_id.clone(),
                });
            }
        }

        Ok(FactLineageAudit {
            package_id: package.package_id.clone(),
            truth_version: package.truth_version.clone(),
            facts_audited: self.promoted_facts.len(),
            evidence_coverage,
            failure_coverage,
        })
    }
}

/// Summary of a successful fact-lineage audit. Captures which evidence and
/// failure-mode clauses the run's promoted facts covered.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FactLineageAudit {
    pub package_id: TruthPackageId,
    pub truth_version: String,
    pub facts_audited: usize,
    pub evidence_coverage: BTreeSet<ClauseId>,
    pub failure_coverage: BTreeSet<ClauseId>,
}

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum FactLineageAuditError {
    #[error("report package_id {report:?} does not match audit package {package:?}")]
    PackageMismatch {
        report: TruthPackageId,
        package: TruthPackageId,
    },
    #[error("report truth_version {report} does not match audit package {package}")]
    TruthVersionMismatch { report: String, package: String },
    #[error("promoted fact {fact_id} cites clause {clause_id} not present in the package")]
    UnknownClause {
        fact_id: String,
        clause_id: ClauseId,
    },
    #[error(
        "promoted fact {fact_id} does not cite any evidence_required or failure_mode clause it serves"
    )]
    ScopeOnlyFact { fact_id: String },
}

/// Audited verifier outcome that can seed decoder calibration.
///
/// This is decoder learning input only. It deliberately carries report,
/// clause, verifier, and promotion-policy evidence; it carries no Formation
/// choice, authority recomputation result, or specialist execution handle.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LearningEpisode {
    pub episode_id: ArtifactId,
    pub source_run_id: String,
    pub domain_hint: String,
    pub package_id: TruthPackageId,
    pub truth_version: String,
    pub verdict: AxiomRunVerdict,
    pub observed_stop_reason: String,
    pub source_clause_signals: Vec<LearningClauseSignal>,
    /// Audit trail only: the run-time fact IDs that contributed to this
    /// episode's clause coverage. Not consumed when generating calibration
    /// records — priors are derived from `source_clause_signals`. Carried so
    /// future operators can trace a calibration record back to the specific
    /// promoted facts that justified it. Do not remove without replacing the
    /// audit hook.
    pub promoted_fact_ids: Vec<String>,
    pub promotion_policy_hashes: Vec<String>,
    pub verifier_required_evidence: Vec<String>,
    pub verifier_forbidden_actions: Vec<String>,
}

impl LearningEpisode {
    pub fn from_report(
        source_run_id: impl Into<String>,
        domain_hint: impl Into<String>,
        package: &TruthPackage,
        report: &AxiomRunReport,
        audit: &FactLineageAudit,
    ) -> Self {
        let source_run_id = source_run_id.into();
        let domain_hint = domain_hint.into();
        let mut promotion_policy_hashes: Vec<String> = report
            .promoted_facts
            .iter()
            .filter_map(|fact| fact.promotion_authority.as_ref())
            .map(|authority| authority.policy_version_hash.clone())
            .collect();
        promotion_policy_hashes.sort();
        promotion_policy_hashes.dedup();

        let source_clause_signals = package
            .source_jtbd
            .clauses
            .iter()
            .map(|clause| {
                let evidence = audit.evidence_coverage.contains(&clause.id);
                let failure_guard = audit.failure_coverage.contains(&clause.id);
                let coverage_status = match (evidence, failure_guard) {
                    (true, true) => ClauseCoverageStatus::CoveredAsEvidenceAndFailureGuard,
                    (true, false) => ClauseCoverageStatus::CoveredAsEvidence,
                    (false, true) => ClauseCoverageStatus::CoveredAsFailureGuard,
                    (false, false) => ClauseCoverageStatus::Uncovered,
                };
                LearningClauseSignal {
                    clause_id: clause.id.clone(),
                    clause_kind: clause.kind,
                    fingerprint: clause.fingerprint.clone(),
                    coverage_status,
                }
            })
            .collect();

        let episode_id = learning_episode_id(
            &source_run_id,
            &domain_hint,
            &package.package_id,
            &package.truth_version,
            report.verdict,
        );

        Self {
            episode_id,
            source_run_id,
            domain_hint,
            package_id: package.package_id.clone(),
            truth_version: package.truth_version.clone(),
            verdict: report.verdict,
            observed_stop_reason: format!("{:?}", report.observed_stop_reason),
            source_clause_signals,
            promoted_fact_ids: report
                .promoted_facts
                .iter()
                .map(|fact| fact.fact_id.clone())
                .collect(),
            promotion_policy_hashes,
            verifier_required_evidence: report.verifier_spec.required_evidence.clone(),
            verifier_forbidden_actions: report
                .verifier_spec
                .forbidden_actions
                .iter()
                .map(|forbidden| forbidden.action.clone())
                .collect(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LearningClauseSignal {
    pub clause_id: ClauseId,
    pub clause_kind: JtbdClauseKind,
    pub fingerprint: ClauseFingerprint,
    pub coverage_status: ClauseCoverageStatus,
}

/// Whether a clause was cited by promoted facts during the run that produced
/// the surrounding `LearningEpisode`.
///
/// v0.15 replaces the v0.13 pair of booleans (`covered_as_evidence` plus
/// `covered_as_failure_guard`) with this enum so an `Uncovered`
/// `EvidenceRequired` clause can be expressed as a typed signal — the input
/// to v0.15's calibration *concern* records.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ClauseCoverageStatus {
    /// No promoted fact cited this clause. For `EvidenceRequired` clauses
    /// this is the "missing evidence" signal v0.15 learns from. For scope
    /// clauses (`Actor`, `FunctionalJob`, `SoThat`) this is the normal
    /// resting state and is ignored by calibration.
    #[default]
    Uncovered,
    /// At least one promoted fact cited this clause as evidence and none as
    /// a failure guard.
    CoveredAsEvidence,
    /// At least one promoted fact cited this clause as a failure guard and
    /// none as evidence.
    CoveredAsFailureGuard,
    /// At least one promoted fact cited this clause in both roles.
    CoveredAsEvidenceAndFailureGuard,
}

impl ClauseCoverageStatus {
    /// True when the clause appeared as evidence in any promoted fact.
    pub fn was_covered_as_evidence(self) -> bool {
        matches!(
            self,
            Self::CoveredAsEvidence | Self::CoveredAsEvidenceAndFailureGuard
        )
    }

    /// True when the clause appeared as a failure guard in any promoted fact.
    pub fn was_covered_as_failure_guard(self) -> bool {
        matches!(
            self,
            Self::CoveredAsFailureGuard | Self::CoveredAsEvidenceAndFailureGuard
        )
    }

    /// True when no promoted fact cited this clause.
    pub fn is_uncovered(self) -> bool {
        matches!(self, Self::Uncovered)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct CalibrationKey {
    pub clause_kind: JtbdClauseKind,
    pub normalized_clause_shape: String,
    pub domain_hint: String,
    pub decoder_rule_id: String,
    pub fingerprint_class: String,
}

impl CalibrationKey {
    pub fn for_clause(
        clause: &JtbdClause,
        domain_hint: impl Into<String>,
        decoder_rule_id: impl Into<String>,
    ) -> Self {
        Self {
            clause_kind: clause.kind,
            normalized_clause_shape: canonicalize_clause_text(&clause.canonical_text),
            domain_hint: domain_hint.into(),
            decoder_rule_id: decoder_rule_id.into(),
            fingerprint_class: clause.fingerprint.short().to_string(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CalibrationValue {
    pub suggested_evidence_templates: Vec<String>,
    pub suggested_failure_scenarios: Vec<String>,
    pub suggested_policy_requirements: Vec<String>,
    pub suggested_verifier_expectations: Vec<String>,
    /// Absolute confidence in this prior, expressed in basis points (1 bp =
    /// 0.01%, range 0–10_000). v0.13 derives the value from the source
    /// episode's verdict via `confidence_for_verdict`: 9_000 (90%) for
    /// `Satisfied`, 7_000 (70%) for `Blocked` or `Invalid`, 4_000 (40%) for
    /// `Exhausted`. The scale is absolute, not relative or decaying — v0.14+
    /// review workflows are expected to adjust it on accept/reject and may
    /// introduce decay later. Operators reviewing a calibration record can
    /// read this directly to gauge how much to trust the prior.
    pub confidence_basis_points: u16,
    pub rationale: String,
    pub source_episode_ids: Vec<ArtifactId>,
    /// Whether this prior reinforces a working pattern or raises a concern
    /// about an unsatisfied evidence requirement.
    ///
    /// v0.15 introduces this field. `#[serde(default)]` preserves the
    /// v0.13/v0.14 wire format: persisted records without this field
    /// deserialize as `Reinforcement`, which matches their semantics. Accepted
    /// `Reinforcement` records add `CalibrationSuggestion` artifacts to a
    /// regenerated package; accepted `Concern` records add
    /// `CalibrationConcern` artifacts. Neither path modifies the source
    /// JTBD's evidence requirements or forbidden actions.
    #[serde(default)]
    pub signal_kind: CalibrationSignalKind,
}

/// Whether a calibration record reinforces a covered clause or raises a
/// concern about an uncovered one.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CalibrationSignalKind {
    /// The decoder should keep reaching for this prior — a clause shape that
    /// was covered by a promoted fact and contributed to a successful (or
    /// otherwise informative) run. Default to preserve v0.13/v0.14
    /// semantics for records persisted before v0.15.
    #[default]
    Reinforcement,
    /// The clause shape was required (or forbidden) and the run did not
    /// produce a satisfying citation. The decoder should treat this as a
    /// warning, prompt, default-expectation candidate, or alternate
    /// scaffolding hook on the next package generation. The source JTBD
    /// remains authoritative.
    Concern,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CalibrationStatus {
    Proposed,
    Accepted,
    Rejected,
    Reset,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CalibrationRecord {
    pub record_id: ArtifactId,
    pub key: CalibrationKey,
    pub value: CalibrationValue,
    pub status: CalibrationStatus,
    pub review_note: Option<String>,
}

impl CalibrationRecord {
    pub fn with_status(
        mut self,
        status: CalibrationStatus,
        review_note: impl Into<String>,
    ) -> Self {
        self.status = status;
        self.review_note = Some(review_note.into());
        self
    }

    pub fn accepted(mut self, review_note: impl Into<String>) -> Self {
        self.status = CalibrationStatus::Accepted;
        self.review_note = Some(review_note.into());
        self
    }
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CalibrationTable {
    pub records: Vec<CalibrationRecord>,
}

impl CalibrationTable {
    pub fn new(mut records: Vec<CalibrationRecord>) -> Self {
        records.sort_by(|left, right| left.record_id.as_str().cmp(right.record_id.as_str()));
        Self { records }
    }

    pub fn accepted_for_clause<'a>(
        &'a self,
        clause: &JtbdClause,
        domain_hint: &str,
        decoder_rule_id: &str,
    ) -> impl Iterator<Item = &'a CalibrationRecord> {
        let key = CalibrationKey::for_clause(clause, domain_hint, decoder_rule_id);
        self.records
            .iter()
            .filter(move |record| record.status == CalibrationStatus::Accepted && record.key == key)
    }

    /// Serialize the table as JSON Lines — one `CalibrationRecord` per line,
    /// in record-id order. The output is byte-deterministic for any table
    /// holding the same set of records: `to_jsonl` sorts borrowed references
    /// at serialization time, so tables constructed without `new()` (raw
    /// struct literal, direct `Deserialize`, or hand-mutated `records`)
    /// still produce canonical output. Suitable for content-addressable
    /// storage, append-only audit logs, or git-tracked operator review
    /// workflows.
    pub fn to_jsonl(&self) -> String {
        let mut sorted: Vec<&CalibrationRecord> = self.records.iter().collect();
        sorted.sort_by(|left, right| left.record_id.as_str().cmp(right.record_id.as_str()));
        let mut out = String::new();
        for record in sorted {
            let line = serde_json::to_string(record)
                .expect("calibration record serializes (every field is serde-derived)");
            out.push_str(&line);
            out.push('\n');
        }
        out
    }

    /// Accept a proposed (or previously reviewed) calibration record.
    ///
    /// The `note` is mandatory — empty or whitespace-only notes raise
    /// `CalibrationReviewError::EmptyNote`. Accepting an already-accepted
    /// record is allowed; the new note replaces the previous one. Records
    /// not present in the table raise `RecordNotFound`.
    pub fn accept(
        &mut self,
        record_id: &ArtifactId,
        note: impl Into<String>,
    ) -> Result<(), CalibrationReviewError> {
        self.review(record_id, CalibrationStatus::Accepted, note.into())
    }

    /// Reject a calibration record. The decoder will never enrich a package
    /// with a rejected record, regardless of the originating episode's verdict.
    pub fn reject(
        &mut self,
        record_id: &ArtifactId,
        note: impl Into<String>,
    ) -> Result<(), CalibrationReviewError> {
        self.review(record_id, CalibrationStatus::Rejected, note.into())
    }

    /// Reset a calibration record. Reset records do not enrich packages, but
    /// the next matching learning episode may re-propose them — unlike
    /// `Rejected`, which signals "this prior was wrong" rather than "this
    /// prior is stale."
    pub fn reset(
        &mut self,
        record_id: &ArtifactId,
        note: impl Into<String>,
    ) -> Result<(), CalibrationReviewError> {
        self.review(record_id, CalibrationStatus::Reset, note.into())
    }

    fn review(
        &mut self,
        record_id: &ArtifactId,
        status: CalibrationStatus,
        note: String,
    ) -> Result<(), CalibrationReviewError> {
        if note.trim().is_empty() {
            return Err(CalibrationReviewError::EmptyNote {
                record_id: record_id.clone(),
                status,
            });
        }
        let record = self
            .records
            .iter_mut()
            .find(|record| &record.record_id == record_id)
            .ok_or_else(|| CalibrationReviewError::RecordNotFound {
                record_id: record_id.clone(),
            })?;
        record.status = status;
        record.review_note = Some(note);
        Ok(())
    }

    /// Parse a JSON Lines calibration table. Blank lines are skipped; every
    /// non-blank line must deserialize into a `CalibrationRecord`. Duplicate
    /// `record_id` values are rejected. The returned table is re-sorted by
    /// `CalibrationTable::new`, so JSONL round-trip is order-stable even if
    /// the input was hand-edited out of order.
    pub fn from_jsonl(input: &str) -> Result<Self, CalibrationPersistenceError> {
        let mut records = Vec::new();
        let mut seen_ids: BTreeSet<ArtifactId> = BTreeSet::new();
        for (index, raw_line) in input.lines().enumerate() {
            let line = raw_line.trim();
            if line.is_empty() {
                continue;
            }
            let record: CalibrationRecord = serde_json::from_str(line).map_err(|err| {
                CalibrationPersistenceError::InvalidLine {
                    line_number: index + 1,
                    message: err.to_string(),
                }
            })?;
            if !seen_ids.insert(record.record_id.clone()) {
                return Err(CalibrationPersistenceError::DuplicateRecord {
                    record_id: record.record_id.clone(),
                });
            }
            records.push(record);
        }
        Ok(Self::new(records))
    }
}

const CALIBRATION_DECODER_RULE_ID: &str = "decoder_calibration.v0.13";

pub fn calibration_records_from_learning_episode(
    package: &TruthPackage,
    episode: &LearningEpisode,
) -> Result<Vec<CalibrationRecord>, CalibrationError> {
    if package.package_id != episode.package_id {
        return Err(CalibrationError::PackageMismatch {
            package: package.package_id.clone(),
            episode: episode.package_id.clone(),
        });
    }
    if package.truth_version != episode.truth_version {
        return Err(CalibrationError::TruthVersionMismatch {
            package: package.truth_version.clone(),
            episode: episode.truth_version.clone(),
        });
    }

    let mut records = Vec::new();
    for signal in &episode.source_clause_signals {
        let clause = package
            .source_jtbd
            .clause(&signal.clause_id)
            .ok_or_else(|| CalibrationError::UnknownClause {
                clause_id: signal.clause_id.clone(),
            })?;

        if !signal.coverage_status.is_uncovered() {
            records.push(reinforcement_record_for_clause(
                package, episode, clause, signal,
            ));
        } else if should_emit_concern(clause.kind, episode.verdict) {
            records.push(concern_record_for_clause(package, episode, clause));
        }
    }
    Ok(records)
}

/// v0.15 — should `calibration_records_from_learning_episode` emit a `Concern`
/// record for an uncovered clause given the run's verdict?
///
/// Concerns only fire for `EvidenceRequired` clauses (uncovered failure modes
/// are normal: most failure modes don't get explicitly cited by promoted
/// facts; only the ones a fact actively guards against do). Verdicts that
/// trigger concerns:
///
/// - `Invalid` — missing evidence is one root cause of an invalid verdict;
///   strongest signal.
/// - `Blocked` — a gate stopped the run before evidence could arrive; still
///   useful signal about which clause shapes recurrently delay promotion.
/// - `Satisfied` — no required clause was truly missing (a Satisfied run
///   means all evidence was cited), so no concern.
/// - `Exhausted` — budget ran out before evidence could arrive; the signal
///   is real but noisier than `Invalid` / `Blocked`. Deferred to a future
///   milestone if the noise turns out to be tolerable.
fn should_emit_concern(clause_kind: JtbdClauseKind, verdict: AxiomRunVerdict) -> bool {
    clause_kind == JtbdClauseKind::EvidenceRequired
        && matches!(verdict, AxiomRunVerdict::Invalid | AxiomRunVerdict::Blocked)
}

fn reinforcement_record_for_clause(
    package: &TruthPackage,
    episode: &LearningEpisode,
    clause: &JtbdClause,
    signal: &LearningClauseSignal,
) -> CalibrationRecord {
    let key = CalibrationKey::for_clause(clause, &episode.domain_hint, CALIBRATION_DECODER_RULE_ID);
    let mut value = CalibrationValue {
        suggested_evidence_templates: Vec::new(),
        suggested_failure_scenarios: Vec::new(),
        suggested_policy_requirements: Vec::new(),
        suggested_verifier_expectations: Vec::new(),
        confidence_basis_points: confidence_for_verdict(episode.verdict),
        rationale: format!(
            "learned from {} verdict {:?} for package {}",
            episode.source_run_id, episode.verdict, package.package_id
        ),
        source_episode_ids: vec![episode.episode_id.clone()],
        signal_kind: CalibrationSignalKind::Reinforcement,
    };

    if signal.coverage_status.was_covered_as_evidence() {
        value
            .suggested_evidence_templates
            .push(clause.canonical_text.clone());
        value.suggested_policy_requirements.push(format!(
            "require before promotion: {}",
            clause.canonical_text
        ));
        value
            .suggested_verifier_expectations
            .push(clause.canonical_text.clone());
    }
    if signal.coverage_status.was_covered_as_failure_guard() {
        value
            .suggested_failure_scenarios
            .push(clause.canonical_text.clone());
        value.suggested_policy_requirements.push(format!(
            "forbid promotion when observed: {}",
            clause.canonical_text
        ));
        value
            .suggested_verifier_expectations
            .push(format!("forbidden action: {}", clause.canonical_text));
    }

    let record_id = calibration_record_id(&key, &episode.episode_id);
    CalibrationRecord {
        record_id,
        key,
        value,
        status: CalibrationStatus::Proposed,
        review_note: None,
    }
}

fn concern_record_for_clause(
    package: &TruthPackage,
    episode: &LearningEpisode,
    clause: &JtbdClause,
) -> CalibrationRecord {
    let key = CalibrationKey::for_clause(clause, &episode.domain_hint, CALIBRATION_DECODER_RULE_ID);
    let value = CalibrationValue {
        // Concern records do not suggest templates/scenarios/policy/verifier
        // entries — they raise a typed warning about an uncovered clause.
        // The decoder consults `signal_kind` and produces a different
        // artifact kind on apply.
        suggested_evidence_templates: Vec::new(),
        suggested_failure_scenarios: Vec::new(),
        suggested_policy_requirements: Vec::new(),
        suggested_verifier_expectations: Vec::new(),
        confidence_basis_points: confidence_for_concern(episode.verdict),
        rationale: format!(
            "concern from {} verdict {:?} for package {}: evidence requirement \"{}\" was not cited by any promoted fact",
            episode.source_run_id, episode.verdict, package.package_id, clause.canonical_text
        ),
        source_episode_ids: vec![episode.episode_id.clone()],
        signal_kind: CalibrationSignalKind::Concern,
    };

    let record_id = calibration_record_id(&key, &episode.episode_id);
    CalibrationRecord {
        record_id,
        key,
        value,
        status: CalibrationStatus::Proposed,
        review_note: None,
    }
}

fn confidence_for_concern(verdict: AxiomRunVerdict) -> u16 {
    match verdict {
        // Invalid runs give the strongest concern signal: a contract was
        // declared and demonstrably not met.
        AxiomRunVerdict::Invalid => 8_000,
        // Blocked runs are weaker: the evidence might still arrive after
        // the HITL gate opens.
        AxiomRunVerdict::Blocked => 5_500,
        // The other verdicts do not emit concern records today; fall through
        // to a low default so any future caller sees a conservative weight.
        AxiomRunVerdict::Satisfied | AxiomRunVerdict::Exhausted => 3_000,
    }
}

fn confidence_for_verdict(verdict: AxiomRunVerdict) -> u16 {
    match verdict {
        AxiomRunVerdict::Satisfied => 9_000,
        AxiomRunVerdict::Blocked | AxiomRunVerdict::Invalid => 7_000,
        AxiomRunVerdict::Exhausted => 4_000,
    }
}

fn learning_episode_id(
    source_run_id: &str,
    domain_hint: &str,
    package_id: &TruthPackageId,
    truth_version: &str,
    verdict: AxiomRunVerdict,
) -> ArtifactId {
    let mut hasher = Sha256::new();
    hasher.update(source_run_id.as_bytes());
    hasher.update(b"\0");
    hasher.update(domain_hint.as_bytes());
    hasher.update(b"\0");
    hasher.update(package_id.as_str().as_bytes());
    hasher.update(b"\0");
    hasher.update(truth_version.as_bytes());
    hasher.update(b"\0");
    hasher.update(format!("{verdict:?}").as_bytes());
    ArtifactId::new(format!(
        "learning_episode.{}",
        hex_lower(&hasher.finalize())
    ))
}

fn observation_adapter_receipt_id(input: &ObservationAdapterReceiptInput) -> ArtifactId {
    let serialized = serde_json::to_vec(input)
        .expect("ObservationAdapterReceiptInput has no fallible serialization fields");
    let mut hasher = Sha256::new();
    hasher.update(serialized);
    ArtifactId::new(format!(
        "observation_adapter_receipt.{}",
        hex_lower(&hasher.finalize())
    ))
}

fn calibration_record_id(key: &CalibrationKey, episode_id: &ArtifactId) -> ArtifactId {
    let mut hasher = Sha256::new();
    hasher.update(format!("{:?}", key.clause_kind).as_bytes());
    hasher.update(b"\0");
    hasher.update(key.normalized_clause_shape.as_bytes());
    hasher.update(b"\0");
    hasher.update(key.domain_hint.as_bytes());
    hasher.update(b"\0");
    hasher.update(key.decoder_rule_id.as_bytes());
    hasher.update(b"\0");
    hasher.update(key.fingerprint_class.as_bytes());
    hasher.update(b"\0");
    hasher.update(episode_id.as_str().as_bytes());
    ArtifactId::new(format!(
        "calibration_record.{}",
        hex_lower(&hasher.finalize())
    ))
}

fn calibration_summary(value: &CalibrationValue) -> String {
    match value.signal_kind {
        CalibrationSignalKind::Reinforcement => {
            if let Some(first) = value.suggested_evidence_templates.first() {
                return format!("suggests evidence template: {first}");
            }
            if let Some(first) = value.suggested_failure_scenarios.first() {
                return format!("suggests failure scenario: {first}");
            }
            if let Some(first) = value.suggested_policy_requirements.first() {
                return format!("suggests policy requirement: {first}");
            }
            "reinforces reviewed decoder prior".to_string()
        }
        CalibrationSignalKind::Concern => {
            format!("raises decoder concern: {}", value.rationale)
        }
    }
}

pub fn apply_decoder_calibration(
    mut package: TruthPackage,
    table: &CalibrationTable,
    domain_hint: &str,
) -> Result<TruthPackage, CalibrationError> {
    let baseline_required_evidence = package.verifier_spec.required_evidence.clone();
    // ForbiddenAction (from organism_pack) does not derive PartialEq, so the
    // non-weakening invariant compares serialized JSON values instead.
    let baseline_forbidden_actions = serde_json::to_value(&package.verifier_spec.forbidden_actions)
        .expect("forbidden actions serialize for invariant baseline");

    let mut suggestions = Vec::new();
    let mut concerns = Vec::new();
    for clause in &package.source_jtbd.clauses {
        for record in table.accepted_for_clause(clause, domain_hint, CALIBRATION_DECODER_RULE_ID) {
            let (artifact_kind, id_prefix) = match record.value.signal_kind {
                CalibrationSignalKind::Reinforcement => (
                    ArtifactKind::CalibrationSuggestion,
                    "calibration_suggestion",
                ),
                CalibrationSignalKind::Concern => {
                    (ArtifactKind::CalibrationConcern, "calibration_concern")
                }
            };
            let artifact = GeneratedArtifact {
                artifact_id: ArtifactId::new(format!(
                    "{id_prefix}.{}.{}",
                    clause.key,
                    record.record_id.as_str()
                )),
                artifact_kind,
                summary: format!(
                    "calibration {} {}",
                    record.record_id.as_str(),
                    calibration_summary(&record.value)
                ),
                source_clause_ids: vec![clause.id.clone()],
            };
            package.lineage.artifacts.push(ArtifactLineage::new(
                artifact.artifact_id.clone(),
                artifact_kind,
                artifact.source_clause_ids.clone(),
                format!("decoder_calibration.{}", record.record_id.as_str()),
                DECODER_VERSION,
                &package.source_jtbd,
            ));
            match record.value.signal_kind {
                CalibrationSignalKind::Reinforcement => suggestions.push(artifact),
                CalibrationSignalKind::Concern => concerns.push(artifact),
            }
        }
    }

    package
        .artifacts
        .calibration_suggestions
        .extend(suggestions);
    package.artifacts.calibration_concerns.extend(concerns);
    package.lineage.validate_closure(&package.source_jtbd)?;

    // Non-weakening invariant — accepting calibration records must never
    // change the source JTBD's declared evidence requirements or forbidden
    // actions. Concern records propose decoder-side affordances, not
    // contract relaxation.
    debug_assert_eq!(
        package.verifier_spec.required_evidence, baseline_required_evidence,
        "calibration must not modify verifier_spec.required_evidence",
    );
    debug_assert_eq!(
        serde_json::to_value(&package.verifier_spec.forbidden_actions)
            .expect("forbidden actions serialize for invariant check"),
        baseline_forbidden_actions,
        "calibration must not modify verifier_spec.forbidden_actions",
    );

    Ok(package)
}

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum CalibrationError {
    #[error("calibration package_id {package:?} does not match episode package_id {episode:?}")]
    PackageMismatch {
        package: TruthPackageId,
        episode: TruthPackageId,
    },
    #[error("calibration truth version {package} does not match episode truth version {episode}")]
    TruthVersionMismatch { package: String, episode: String },
    #[error("calibration episode references unknown clause {clause_id}")]
    UnknownClause { clause_id: ClauseId },
    #[error(transparent)]
    Lineage(#[from] LineageError),
}

/// Errors raised while loading a persisted calibration table from JSONL.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum CalibrationPersistenceError {
    #[error("calibration line {line_number} did not parse: {message}")]
    InvalidLine { line_number: usize, message: String },
    #[error("duplicate calibration record id {record_id}")]
    DuplicateRecord { record_id: ArtifactId },
}

/// Errors raised when applying an operator review action to a calibration
/// table.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum CalibrationReviewError {
    #[error("calibration record {record_id} not found in table")]
    RecordNotFound { record_id: ArtifactId },
    #[error("review action {status:?} on {record_id} requires a non-empty note")]
    EmptyNote {
        record_id: ArtifactId,
        status: CalibrationStatus,
    },
}

fn compute_verdict(package: &TruthPackage, observation: &AxiomRunObservation) -> AxiomRunVerdict {
    let spec = &package.verifier_spec;

    // 1. Lineage closure: every cited clause must belong to this package.
    let known_clause_ids: BTreeSet<&ClauseId> = package
        .source_jtbd
        .clauses
        .iter()
        .map(|clause| &clause.id)
        .collect();
    let lineage_violation = observation
        .promoted_facts
        .iter()
        .flat_map(|fact| fact.source_clause_ids.iter())
        .any(|id| !known_clause_ids.contains(id));
    if lineage_violation {
        return AxiomRunVerdict::Invalid;
    }

    // 2. Forbidden action observed — best-effort substring match against
    //    promoted fact summaries and replay notes. Typed invariant violations
    //    on the stop reason are the stronger signal; this catches textual
    //    reports that name a forbidden outcome without raising a typed one.
    let forbidden_observed = spec.forbidden_actions.iter().any(|forbidden| {
        let needle = forbidden.action.as_str();
        observation
            .promoted_facts
            .iter()
            .any(|fact| fact.summary.contains(needle))
            || observation
                .replay_notes
                .iter()
                .any(|note| note.contains(needle))
            || observation
                .run_stages
                .iter()
                .any(|stage| stage.replay_notes.iter().any(|note| note.contains(needle)))
    });
    if forbidden_observed {
        return AxiomRunVerdict::Invalid;
    }

    // 3. Unexpected stop reason — categorize the deviation.
    if !observation
        .stop_reason
        .matches_expected(&spec.expected_stop_reasons)
    {
        if observation.stop_reason.is_invalid() {
            return AxiomRunVerdict::Invalid;
        }
        if observation.stop_reason.is_budget_exhausted() {
            return AxiomRunVerdict::Exhausted;
        }
        if observation.stop_reason.is_blocked() {
            return AxiomRunVerdict::Blocked;
        }
        return AxiomRunVerdict::Invalid;
    }

    // 4. Expected stop reason — every required-evidence clause must be cited
    //    by at least one promoted fact.
    let required_evidence_ids: BTreeSet<&ClauseId> = package
        .source_jtbd
        .clauses_by_kind(JtbdClauseKind::EvidenceRequired)
        .map(|clause| &clause.id)
        .collect();
    let cited_clause_ids: BTreeSet<&ClauseId> = observation
        .promoted_facts
        .iter()
        .flat_map(|fact| fact.source_clause_ids.iter())
        .collect();
    if !required_evidence_ids.is_subset(&cited_clause_ids) {
        return AxiomRunVerdict::Invalid;
    }

    AxiomRunVerdict::Satisfied
}

/// One artifact-to-clause lineage statement.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArtifactLineage {
    pub artifact_id: ArtifactId,
    pub artifact_kind: ArtifactKind,
    pub source_clause_ids: Vec<ClauseId>,
    pub decoder_rule_id: String,
    pub decoder_version: String,
    pub input_fingerprints: Vec<ClauseFingerprint>,
}

impl ArtifactLineage {
    pub fn new(
        artifact_id: ArtifactId,
        artifact_kind: ArtifactKind,
        source_clause_ids: Vec<ClauseId>,
        decoder_rule_id: impl Into<String>,
        decoder_version: impl Into<String>,
        document: &JtbdDocument,
    ) -> Self {
        let input_fingerprints = source_clause_ids
            .iter()
            .filter_map(|id| document.clause(id))
            .map(|clause| clause.fingerprint.clone())
            .collect();

        Self {
            artifact_id,
            artifact_kind,
            source_clause_ids,
            decoder_rule_id: decoder_rule_id.into(),
            decoder_version: decoder_version.into(),
            input_fingerprints,
        }
    }
}

/// Explicit disposition for a clause that is not used by an artifact yet.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ClauseDisposition {
    Deferred { reason: String },
    Rejected { reason: String },
}

/// Bidirectional closure check for clause-to-artifact custody.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct LineageMap {
    pub artifacts: Vec<ArtifactLineage>,
    pub clause_dispositions: BTreeMap<ClauseId, ClauseDisposition>,
}

impl LineageMap {
    pub fn single_artifact_from_document(
        document: &JtbdDocument,
        artifact_id: ArtifactId,
        artifact_kind: ArtifactKind,
        decoder_rule_id: impl Into<String>,
        decoder_version: impl Into<String>,
    ) -> Self {
        let source_clause_ids: Vec<ClauseId> = document.clause_ids().cloned().collect();
        Self {
            artifacts: vec![ArtifactLineage::new(
                artifact_id,
                artifact_kind,
                source_clause_ids,
                decoder_rule_id,
                decoder_version,
                document,
            )],
            clause_dispositions: BTreeMap::new(),
        }
    }

    pub fn validate_closure(&self, document: &JtbdDocument) -> Result<(), LineageError> {
        let known = document.known_clause_ids();
        let mut accounted = BTreeSet::new();

        for artifact in &self.artifacts {
            if artifact.source_clause_ids.is_empty() {
                return Err(LineageError::ArtifactWithoutSource {
                    artifact_id: artifact.artifact_id.clone(),
                });
            }

            for clause_id in &artifact.source_clause_ids {
                if !known.contains(clause_id) {
                    return Err(LineageError::UnknownArtifactClause {
                        artifact_id: artifact.artifact_id.clone(),
                        clause_id: clause_id.clone(),
                    });
                }
                accounted.insert(clause_id.clone());
            }
        }

        for clause_id in self.clause_dispositions.keys() {
            if !known.contains(clause_id) {
                return Err(LineageError::UnknownDispositionClause {
                    clause_id: clause_id.clone(),
                });
            }
            accounted.insert(clause_id.clone());
        }

        for clause_id in &known {
            if !accounted.contains(clause_id) {
                return Err(LineageError::UnaccountedClause {
                    clause_id: clause_id.clone(),
                });
            }
        }

        Ok(())
    }
}

/// Errors while normalizing the Truth Package spine.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum TruthPackageError {
    #[error("empty JTBD clause: {field}")]
    EmptyClause { field: String },
    #[error("duplicate clause id: {id}")]
    DuplicateClauseId { id: ClauseId },
}

/// Errors while validating a lineage map.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum LineageError {
    #[error("artifact {artifact_id} has no source clauses")]
    ArtifactWithoutSource { artifact_id: ArtifactId },
    #[error("artifact {artifact_id} references unknown clause {clause_id}")]
    UnknownArtifactClause {
        artifact_id: ArtifactId,
        clause_id: ClauseId,
    },
    #[error("clause disposition references unknown clause {clause_id}")]
    UnknownDispositionClause { clause_id: ClauseId },
    #[error("clause {clause_id} is not used, deferred, or rejected")]
    UnaccountedClause { clause_id: ClauseId },
}

#[derive(Debug, thiserror::Error)]
pub enum TruthOverlayError {
    #[error("overlay targets package {target}, but package is {actual}")]
    PackageMismatch {
        target: TruthPackageId,
        actual: TruthPackageId,
    },
    #[error("overlay targets truth version {target}, but package version is {actual}")]
    TruthVersionMismatch { target: String, actual: String },
    #[error("overlay projection version must not be empty")]
    EmptyProjectionVersion,
    #[error("overlay reason must not be empty")]
    EmptyReason,
    #[error("overlay must name at least one source clause")]
    MissingSourceClauses,
    #[error("overlay references unknown clause {clause_id}")]
    UnknownSourceClause { clause_id: ClauseId },
    #[error("overlay .truths projection did not parse: {message}")]
    TruthProjectionParse { message: String },
}

/// Decode a structured JTBD into the first deterministic Truth Package spine.
///
/// This intentionally uses rule-based generation only. LLM-backed enrichment
/// can be added later, but must feed this deterministic manifest builder.
pub fn decode_jtbd(input: JtbdInput) -> Result<TruthPackage, DecodeJtbdError> {
    let document = JtbdDocument::from_input(input)?;
    let package_id = package_id_for_document(&document);
    let generated_truths = generate_truth_projection(&document);
    let parsed_truth = parse_truth_document(&generated_truths).map_err(|err| {
        DecodeJtbdError::TruthProjectionParse {
            message: err.to_string(),
        }
    })?;
    let mut intent_packet = compile_intent(&parsed_truth)?;
    intent_packet.id = deterministic_uuid(package_id.as_str());
    intent_packet.context = json!({
        "truth_package_id": package_id.as_str(),
        "truth_version": TRUTH_VERSION,
        "source_clause_ids": document
            .clause_ids()
            .map(ClauseId::as_str)
            .collect::<Vec<_>>(),
    });
    if let Some(budget) = document.time_budget
        && let Some(ctx) = intent_packet.context.as_object_mut()
    {
        ctx.insert(
            "time_budget_seconds".to_string(),
            json!(budget.as_seconds()),
        );
    }

    let artifacts = build_artifacts(&document);
    let proof_obligations = build_proof_obligations(&document);
    let verifier_spec = build_verifier_spec(&document);
    let replay_profile = ReplayProfile {
        profile_id: ArtifactId::new(format!("replay_profile.{}", document.key)),
        deterministic: true,
        input_clause_ids: document.clause_ids().cloned().collect(),
    };
    let lineage = build_lineage_map(
        &document,
        &artifacts,
        &proof_obligations,
        &verifier_spec,
        &replay_profile,
    );
    lineage.validate_closure(&document)?;

    Ok(TruthPackage {
        package_id,
        truth_version: TRUTH_VERSION.to_string(),
        source_jtbd: document,
        generated_truths,
        artifacts,
        intent_packet,
        proof_obligations,
        verifier_spec,
        replay_profile,
        lineage,
    })
}

pub fn apply_truth_projection_overlay(
    package: &TruthPackage,
    overlay: TruthProjectionOverlay,
) -> Result<TruthProjectionVersion, TruthOverlayError> {
    if overlay.target_package_id != package.package_id {
        return Err(TruthOverlayError::PackageMismatch {
            target: overlay.target_package_id,
            actual: package.package_id.clone(),
        });
    }
    if overlay.target_truth_version != package.truth_version {
        return Err(TruthOverlayError::TruthVersionMismatch {
            target: overlay.target_truth_version,
            actual: package.truth_version.clone(),
        });
    }
    if canonicalize_clause_text(&overlay.projection_version).is_empty() {
        return Err(TruthOverlayError::EmptyProjectionVersion);
    }
    if canonicalize_clause_text(&overlay.reason).is_empty() {
        return Err(TruthOverlayError::EmptyReason);
    }
    if overlay.source_clause_ids.is_empty() {
        return Err(TruthOverlayError::MissingSourceClauses);
    }

    let known = package.source_jtbd.known_clause_ids();
    for clause_id in &overlay.source_clause_ids {
        if !known.contains(clause_id) {
            return Err(TruthOverlayError::UnknownSourceClause {
                clause_id: clause_id.clone(),
            });
        }
    }

    parse_truth_document(&overlay.edited_truths).map_err(|err| {
        TruthOverlayError::TruthProjectionParse {
            message: err.to_string(),
        }
    })?;

    let lineage = ArtifactLineage::new(
        overlay.overlay_id.clone(),
        ArtifactKind::TruthProjection,
        overlay.source_clause_ids.clone(),
        "truth_projection_overlay.v0",
        DECODER_VERSION,
        &package.source_jtbd,
    );

    Ok(TruthProjectionVersion {
        package_id: package.package_id.clone(),
        base_truth_version: package.truth_version.clone(),
        projection_version: overlay.projection_version,
        truths: overlay.edited_truths,
        source: TruthProjectionSource::OverlayApplied {
            overlay_id: overlay.overlay_id,
            reason: overlay.reason,
        },
        lineage,
    })
}

#[derive(Debug, thiserror::Error)]
pub enum DecodeJtbdError {
    #[error(transparent)]
    TruthPackage(#[from] TruthPackageError),
    #[error("generated truth projection did not parse: {message}")]
    TruthProjectionParse { message: String },
    #[error(transparent)]
    Intent(#[from] CompileError),
    #[error(transparent)]
    Lineage(#[from] LineageError),
}

pub fn canonicalize_clause_text(text: &str) -> String {
    text.split_whitespace().collect::<Vec<_>>().join(" ")
}

fn package_id_for_document(document: &JtbdDocument) -> TruthPackageId {
    let mut hasher = Sha256::new();
    hasher.update(document.key.as_bytes());
    for clause in &document.clauses {
        hasher.update(clause.id.as_str().as_bytes());
        hasher.update(clause.fingerprint.as_str().as_bytes());
    }
    let digest = hex_lower(&hasher.finalize());
    TruthPackageId::new(format!("truth_package.{}.{}", document.key, &digest[..12]))
}

fn overlay_id_for(
    package_id: &TruthPackageId,
    target_truth_version: &str,
    projection_version: &str,
    edited_truths: &str,
) -> ArtifactId {
    let mut hasher = Sha256::new();
    hasher.update(package_id.as_str().as_bytes());
    hasher.update(target_truth_version.as_bytes());
    hasher.update(projection_version.as_bytes());
    hasher.update(canonicalize_clause_text(edited_truths).as_bytes());
    let digest = hex_lower(&hasher.finalize());
    ArtifactId::new(format!("truth_projection_overlay.{}", &digest[..12]))
}

fn generate_truth_projection(document: &JtbdDocument) -> String {
    let actor = required_clause(document, JtbdClauseKind::Actor);
    let functional_job = required_clause(document, JtbdClauseKind::FunctionalJob);
    let so_that = required_clause(document, JtbdClauseKind::SoThat);
    let evidence: Vec<&JtbdClause> = document
        .clauses_by_kind(JtbdClauseKind::EvidenceRequired)
        .collect();
    let failures: Vec<&JtbdClause> = document
        .clauses_by_kind(JtbdClauseKind::FailureMode)
        .collect();

    let mut out = String::new();
    push_line(
        &mut out,
        &format!("Truth: {}", sentence_title(&functional_job.canonical_text)),
    );
    push_line(&mut out, "");
    push_line(&mut out, "  Intent:");
    push_line(
        &mut out,
        &format!("    Outcome: {}", so_that.canonical_text),
    );
    push_line(&mut out, "");
    push_line(&mut out, "  Authority:");
    push_line(&mut out, &format!("    Actor: {}", actor.canonical_text));
    push_line(
        &mut out,
        &format!("    May: attempt {}", functional_job.canonical_text),
    );
    push_line(
        &mut out,
        &format!(
            "    Expires: {}",
            deterministic_expires_line(document.time_budget)
        ),
    );
    if !failures.is_empty() {
        push_line(&mut out, "");
        push_line(&mut out, "  Constraint:");
        for failure in &failures {
            push_line(
                &mut out,
                &format!("    Must Not: {}", failure.canonical_text),
            );
        }
    }
    push_line(&mut out, "");
    push_line(&mut out, "  Evidence:");
    for item in &evidence {
        push_line(&mut out, &format!("    Requires: {}", item.canonical_text));
    }
    push_line(&mut out, "    Provenance: axiom_truth_package");
    push_line(&mut out, "    Audit: truth_package_lineage");
    push_line(&mut out, "");
    push_line(&mut out, "  @acceptance @invariant");
    push_line(
        &mut out,
        "  Scenario: Job is satisfied by required evidence",
    );
    push_line(&mut out, "    Given the actor has the declared job");
    push_line(&mut out, "    When the system evaluates the truth package");
    push_line(
        &mut out,
        "    Then the declared outcome is supported by required evidence",
    );
    out
}

fn build_artifacts(document: &JtbdDocument) -> TruthPackageArtifacts {
    let all_clause_ids: Vec<ClauseId> = document.clause_ids().cloned().collect();
    let actor = required_clause(document, JtbdClauseKind::Actor);
    let functional_job = required_clause(document, JtbdClauseKind::FunctionalJob);
    let scenarios = vec![GeneratedArtifact {
        artifact_id: ArtifactId::new(format!("scenario.{}.satisfaction", document.key)),
        artifact_kind: ArtifactKind::Scenario,
        summary: "Job is satisfied by required evidence".to_string(),
        source_clause_ids: all_clause_ids.clone(),
    }];
    let policy_requirements = build_policy_requirement_artifacts(document, actor, functional_job);
    let evidence_expectations = document
        .clauses_by_kind(JtbdClauseKind::EvidenceRequired)
        .map(|clause| GeneratedArtifact {
            artifact_id: ArtifactId::new(format!("evidence_expectation.{}", clause.key)),
            artifact_kind: ArtifactKind::VerifierExpectation,
            summary: clause.canonical_text.clone(),
            source_clause_ids: vec![clause.id.clone()],
        })
        .collect();
    let invariant_expectations = document
        .clauses_by_kind(JtbdClauseKind::FailureMode)
        .map(|clause| GeneratedArtifact {
            artifact_id: ArtifactId::new(format!("invariant_expectation.{}", clause.key)),
            artifact_kind: ArtifactKind::InvariantArtifact,
            summary: format!("forbid {}", clause.canonical_text),
            source_clause_ids: vec![clause.id.clone()],
        })
        .collect();
    let simulation_cases = vec![GeneratedArtifact {
        artifact_id: ArtifactId::new(format!("simulation_case.{}.baseline", document.key)),
        artifact_kind: ArtifactKind::SimulationCase,
        summary: "baseline deterministic package readiness case".to_string(),
        source_clause_ids: all_clause_ids,
    }];

    TruthPackageArtifacts {
        scenarios,
        predicates: Vec::new(),
        policy_requirements,
        evidence_expectations,
        simulation_cases,
        invariant_expectations,
        calibration_suggestions: Vec::new(),
        calibration_concerns: Vec::new(),
    }
}

fn build_policy_requirement_artifacts(
    document: &JtbdDocument,
    actor: &JtbdClause,
    functional_job: &JtbdClause,
) -> Vec<GeneratedArtifact> {
    let mut artifacts = vec![GeneratedArtifact {
        artifact_id: ArtifactId::new(format!("policy_requirement.{}.authority", document.key)),
        artifact_kind: ArtifactKind::PolicyRequirement,
        summary: format!(
            "authority envelope: {} may attempt {} only through current Converge promotion policy",
            actor.canonical_text, functional_job.canonical_text
        ),
        source_clause_ids: vec![actor.id.clone(), functional_job.id.clone()],
    }];

    artifacts.extend(
        document
            .clauses_by_kind(JtbdClauseKind::EvidenceRequired)
            .map(|clause| GeneratedArtifact {
                artifact_id: ArtifactId::new(format!(
                    "policy_requirement.{}.evidence.{}",
                    document.key, clause.key
                )),
                artifact_kind: ArtifactKind::PolicyRequirement,
                summary: format!("require before promotion: {}", clause.canonical_text),
                source_clause_ids: vec![clause.id.clone()],
            }),
    );

    artifacts.extend(
        document
            .clauses_by_kind(JtbdClauseKind::FailureMode)
            .map(|clause| GeneratedArtifact {
                artifact_id: ArtifactId::new(format!(
                    "policy_requirement.{}.failure.{}",
                    document.key, clause.key
                )),
                artifact_kind: ArtifactKind::PolicyRequirement,
                summary: format!("forbid promotion when observed: {}", clause.canonical_text),
                source_clause_ids: vec![clause.id.clone()],
            }),
    );

    artifacts
}

fn build_proof_obligations(document: &JtbdDocument) -> Vec<ProofObligation> {
    let mut obligations = Vec::new();
    obligations.extend(
        document
            .clauses_by_kind(JtbdClauseKind::EvidenceRequired)
            .map(|clause| ProofObligation {
                artifact_id: ArtifactId::new(format!("proof_obligation.evidence.{}", clause.key)),
                kind: ProofObligationKind::EvidenceRequired,
                description: format!("evidence required: {}", clause.canonical_text),
                source_clause_ids: vec![clause.id.clone()],
            }),
    );
    obligations.extend(
        document
            .clauses_by_kind(JtbdClauseKind::FailureMode)
            .map(|clause| ProofObligation {
                artifact_id: ArtifactId::new(format!("proof_obligation.failure.{}", clause.key)),
                kind: ProofObligationKind::FailureGuard,
                description: format!("failure mode must be guarded: {}", clause.canonical_text),
                source_clause_ids: vec![clause.id.clone()],
            }),
    );
    obligations
}

fn build_verifier_spec(document: &JtbdDocument) -> VerifierSpec {
    let required_evidence = document
        .clauses_by_kind(JtbdClauseKind::EvidenceRequired)
        .map(|clause| clause.canonical_text.clone())
        .collect();
    let forbidden_actions = document
        .clauses_by_kind(JtbdClauseKind::FailureMode)
        .map(|clause| ForbiddenAction {
            action: clause.canonical_text.clone(),
            reason: "failure_mode".to_string(),
        })
        .collect();
    let satisfaction_conditions = vec![
        required_clause(document, JtbdClauseKind::SoThat)
            .canonical_text
            .clone(),
    ];

    VerifierSpec {
        expected_stop_reasons: BTreeSet::from([
            ExpectedStopReason::Converged,
            ExpectedStopReason::CriteriaMet,
        ]),
        required_evidence,
        forbidden_actions,
        satisfaction_conditions,
    }
}

fn build_lineage_map(
    document: &JtbdDocument,
    artifacts: &TruthPackageArtifacts,
    proof_obligations: &[ProofObligation],
    verifier_spec: &VerifierSpec,
    replay_profile: &ReplayProfile,
) -> LineageMap {
    let all_clause_ids: Vec<ClauseId> = document.clause_ids().cloned().collect();
    let mut lineages = vec![
        ArtifactLineage::new(
            ArtifactId::new(format!("manifest.{}", document.key)),
            ArtifactKind::TruthPackageManifest,
            all_clause_ids.clone(),
            "truth_package_manifest.v0",
            DECODER_VERSION,
            document,
        ),
        ArtifactLineage::new(
            ArtifactId::new(format!("truth_projection.{}", document.key)),
            ArtifactKind::TruthProjection,
            all_clause_ids.clone(),
            "truth_projection.v0",
            DECODER_VERSION,
            document,
        ),
        ArtifactLineage::new(
            ArtifactId::new(format!("intent_packet.{}", document.key)),
            ArtifactKind::IntentField,
            all_clause_ids.clone(),
            "intent_packet.v0",
            DECODER_VERSION,
            document,
        ),
        ArtifactLineage::new(
            ArtifactId::new(format!("verifier_spec.{}", document.key)),
            ArtifactKind::VerifierExpectation,
            verifier_source_clause_ids(document, verifier_spec),
            "verifier_spec.v0",
            DECODER_VERSION,
            document,
        ),
        ArtifactLineage::new(
            replay_profile.profile_id.clone(),
            ArtifactKind::ReplayProfile,
            replay_profile.input_clause_ids.clone(),
            "replay_profile.v0",
            DECODER_VERSION,
            document,
        ),
    ];

    for artifact in generated_artifact_iter(artifacts) {
        lineages.push(ArtifactLineage::new(
            artifact.artifact_id.clone(),
            artifact.artifact_kind,
            artifact.source_clause_ids.clone(),
            "generated_artifact.v0",
            DECODER_VERSION,
            document,
        ));
    }
    for obligation in proof_obligations {
        lineages.push(ArtifactLineage::new(
            obligation.artifact_id.clone(),
            ArtifactKind::ProofObligation,
            obligation.source_clause_ids.clone(),
            "proof_obligation.v0",
            DECODER_VERSION,
            document,
        ));
    }

    LineageMap {
        artifacts: lineages,
        clause_dispositions: BTreeMap::new(),
    }
}

fn generated_artifact_iter(
    artifacts: &TruthPackageArtifacts,
) -> impl Iterator<Item = &GeneratedArtifact> {
    artifacts
        .scenarios
        .iter()
        .chain(artifacts.predicates.iter())
        .chain(artifacts.policy_requirements.iter())
        .chain(artifacts.evidence_expectations.iter())
        .chain(artifacts.simulation_cases.iter())
        .chain(artifacts.invariant_expectations.iter())
        .chain(artifacts.calibration_suggestions.iter())
        .chain(artifacts.calibration_concerns.iter())
}

fn verifier_source_clause_ids(
    document: &JtbdDocument,
    verifier_spec: &VerifierSpec,
) -> Vec<ClauseId> {
    let mut ids = BTreeSet::new();
    ids.insert(required_clause(document, JtbdClauseKind::SoThat).id.clone());
    if !verifier_spec.required_evidence.is_empty() {
        ids.extend(
            document
                .clauses_by_kind(JtbdClauseKind::EvidenceRequired)
                .map(|clause| clause.id.clone()),
        );
    }
    if !verifier_spec.forbidden_actions.is_empty() {
        ids.extend(
            document
                .clauses_by_kind(JtbdClauseKind::FailureMode)
                .map(|clause| clause.id.clone()),
        );
    }
    ids.into_iter().collect()
}

fn required_clause(document: &JtbdDocument, kind: JtbdClauseKind) -> &JtbdClause {
    document
        .clauses_by_kind(kind)
        .next()
        .expect("JtbdDocument always contains scalar clauses")
}

fn deterministic_uuid(seed: &str) -> Uuid {
    let mut hasher = Sha256::new();
    hasher.update(seed.as_bytes());
    let digest = hasher.finalize();
    let mut bytes = [0_u8; 16];
    bytes.copy_from_slice(&digest[..16]);
    bytes[6] = (bytes[6] & 0x0f) | 0x50;
    bytes[8] = (bytes[8] & 0x3f) | 0x80;
    Uuid::from_bytes(bytes)
}

fn sentence_title(value: &str) -> String {
    let mut chars = value.chars();
    let Some(first) = chars.next() else {
        return "Untitled job".to_string();
    };
    format!("{}{}", first.to_uppercase(), chars.as_str())
}

fn push_line(buffer: &mut String, line: &str) {
    buffer.push_str(line);
    buffer.push('\n');
}

fn scalar_clause(
    root_key: &str,
    kind: JtbdClauseKind,
    path: &str,
    text: String,
) -> Result<JtbdClause, TruthPackageError> {
    build_clause(root_key, kind, path, path, text)
}

fn collection_clauses(
    root_key: &str,
    kind: JtbdClauseKind,
    path_prefix: &str,
    inputs: Vec<ClauseInput>,
) -> Result<Vec<JtbdClause>, TruthPackageError> {
    let mut implicit_counts = BTreeMap::<String, usize>::new();
    for input in &inputs {
        if input.key.is_none() {
            let canonical = canonicalize_clause_text(&input.text);
            let fingerprint = ClauseFingerprint::from_text(&canonical);
            let key = implicit_clause_key(&canonical, &fingerprint);
            *implicit_counts.entry(key).or_default() += 1;
        }
    }

    let mut clauses = Vec::with_capacity(inputs.len());
    for input in inputs {
        let canonical = canonicalize_clause_text(&input.text);
        let fingerprint = ClauseFingerprint::from_text(&canonical);
        let explicit = input.key.is_some();
        let base_key = input.key.map_or_else(
            || implicit_clause_key(&canonical, &fingerprint),
            |key| normalized_key(&key, "clause"),
        );
        let key = if explicit || implicit_counts.get(&base_key).copied().unwrap_or(0) <= 1 {
            base_key
        } else {
            format!("{base_key}_{}", fingerprint.short())
        };
        let path = format!("{path_prefix}.{key}");
        clauses.push(build_clause(root_key, kind, &path, &key, input.text)?);
    }

    clauses.sort_by(|left, right| left.id.cmp(&right.id));
    ensure_unique_clause_ids(&clauses)?;
    Ok(clauses)
}

fn build_clause(
    root_key: &str,
    kind: JtbdClauseKind,
    path: &str,
    key: &str,
    text: String,
) -> Result<JtbdClause, TruthPackageError> {
    let canonical_text = canonicalize_clause_text(&text);
    if canonical_text.is_empty() {
        return Err(TruthPackageError::EmptyClause {
            field: path.to_string(),
        });
    }

    Ok(JtbdClause {
        id: ClauseId::new(root_key, path),
        kind,
        key: key.to_string(),
        text,
        fingerprint: ClauseFingerprint::from_text(&canonical_text),
        canonical_text,
    })
}

fn ensure_unique_clause_ids(clauses: &[JtbdClause]) -> Result<(), TruthPackageError> {
    let mut seen = BTreeSet::new();
    for clause in clauses {
        if !seen.insert(clause.id.clone()) {
            return Err(TruthPackageError::DuplicateClauseId {
                id: clause.id.clone(),
            });
        }
    }
    Ok(())
}

fn implicit_clause_key(canonical_text: &str, fingerprint: &ClauseFingerprint) -> String {
    let slug = slugify(canonical_text);
    if slug.is_empty() {
        format!("clause_{}", fingerprint.short())
    } else {
        slug
    }
}

fn normalized_key(value: &str, fallback_prefix: &str) -> String {
    let slug = slugify(value);
    if slug.is_empty() {
        let fingerprint = ClauseFingerprint::from_text(value);
        format!("{fallback_prefix}_{}", fingerprint.short())
    } else {
        slug
    }
}

fn slugify(value: &str) -> String {
    let mut out = String::new();
    let mut last_was_separator = false;

    for ch in value.chars() {
        if ch.is_ascii_alphanumeric() {
            out.push(ch.to_ascii_lowercase());
            last_was_separator = false;
        } else if !last_was_separator {
            out.push('_');
            last_was_separator = true;
        }
    }

    out.trim_matches('_').to_string()
}

fn hex_lower(bytes: &[u8]) -> String {
    use std::fmt::Write as _;

    let mut out = String::with_capacity(bytes.len() * 2);
    for byte in bytes {
        write!(&mut out, "{byte:02x}").expect("writing to String cannot fail");
    }
    out
}

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

    fn vendor_input() -> JtbdInput {
        JtbdInput {
            key: "Vendor Commitment".to_string(),
            actor: "finance controller".to_string(),
            functional_job: "approve a vendor commitment".to_string(),
            so_that: "spend is traceable and policy-compliant".to_string(),
            evidence_required: vec![
                ClauseInput::new("vendor assessment"),
                ClauseInput::with_key("po", "purchase order"),
            ],
            failure_modes: vec![
                ClauseInput::new("bypassed approval"),
                ClauseInput::new("missing audit trail"),
            ],
            time_budget: None,
        }
    }

    #[test]
    fn deterministic_clause_ids_do_not_depend_on_collection_order() {
        let mut reordered = vendor_input();
        reordered.evidence_required.reverse();
        reordered.failure_modes.reverse();

        let original = JtbdDocument::from_input(vendor_input()).unwrap();
        let reordered = JtbdDocument::from_input(reordered).unwrap();

        assert_eq!(original, reordered);

        let ids: Vec<&str> = original.clause_ids().map(ClauseId::as_str).collect();
        assert!(ids.contains(&"jtbd.vendor_commitment.actor"));
        assert!(ids.contains(&"jtbd.vendor_commitment.functional_job"));
        assert!(ids.contains(&"jtbd.vendor_commitment.so_that"));
        assert!(ids.contains(&"jtbd.vendor_commitment.evidence.vendor_assessment"));
        assert!(ids.contains(&"jtbd.vendor_commitment.evidence.po"));
        assert!(ids.contains(&"jtbd.vendor_commitment.failure.bypassed_approval"));
        assert!(!ids.iter().any(|id| id.contains("[0]") || id.contains(".0")));
    }

    #[test]
    fn explicit_clause_key_preserves_id_while_fingerprint_changes() {
        let mut first = vendor_input();
        first.evidence_required = vec![ClauseInput::with_key("risk_review", "risk review")];

        let mut second = first.clone();
        second.evidence_required = vec![ClauseInput::with_key(
            "risk_review",
            "fresh risk review with policy citations",
        )];

        let first = JtbdDocument::from_input(first).unwrap();
        let second = JtbdDocument::from_input(second).unwrap();

        let first_clause = first
            .clauses
            .iter()
            .find(|clause| clause.key == "risk_review")
            .unwrap();
        let second_clause = second
            .clauses
            .iter()
            .find(|clause| clause.key == "risk_review")
            .unwrap();

        assert_eq!(first_clause.id, second_clause.id);
        assert_ne!(first_clause.fingerprint, second_clause.fingerprint);
    }

    #[test]
    fn lineage_map_closure_requires_every_clause_to_be_accounted_for() {
        let document = JtbdDocument::from_input(vendor_input()).unwrap();
        let map = LineageMap::single_artifact_from_document(
            &document,
            ArtifactId::new("truth_projection.vendor_commitment.v1"),
            ArtifactKind::TruthProjection,
            "truth_projection.v0",
            "0.10.0",
        );

        assert!(map.validate_closure(&document).is_ok());

        let mut missing_one = map.clone();
        missing_one.artifacts[0].source_clause_ids.pop();
        assert!(matches!(
            missing_one.validate_closure(&document),
            Err(LineageError::UnaccountedClause { .. })
        ));

        let mut unknown = map;
        unknown.artifacts[0]
            .source_clause_ids
            .push(ClauseId::new("vendor_commitment", "evidence.unknown"));
        assert!(matches!(
            unknown.validate_closure(&document),
            Err(LineageError::UnknownArtifactClause { .. })
        ));
    }

    #[test]
    fn decode_jtbd_builds_parseable_package_spine() {
        let package = decode_jtbd(vendor_input()).unwrap();

        assert!(
            package
                .generated_truths
                .contains("Truth: Approve a vendor commitment")
        );
        assert!(
            package
                .generated_truths
                .contains("Requires: purchase order")
        );
        assert!(
            package
                .generated_truths
                .contains("Must Not: bypassed approval")
        );
        assert_eq!(package.truth_version, TRUTH_VERSION);
        assert_eq!(
            package.intent_packet.outcome,
            "spend is traceable and policy-compliant"
        );
        assert_eq!(
            package
                .intent_packet
                .context
                .get("truth_package_id")
                .and_then(serde_json::Value::as_str),
            Some(package.package_id.as_str())
        );
        assert_eq!(package.verifier_spec.required_evidence.len(), 2);
        assert_eq!(package.verifier_spec.forbidden_actions.len(), 2);
        assert_eq!(package.proof_obligations.len(), 4);
        assert!(
            package
                .lineage
                .validate_closure(&package.source_jtbd)
                .is_ok()
        );
    }

    #[test]
    fn decode_jtbd_is_deterministic_for_same_semantic_input() {
        let mut reordered = vendor_input();
        reordered.evidence_required.reverse();
        reordered.failure_modes.reverse();

        let first = decode_jtbd(vendor_input()).unwrap();
        let second = decode_jtbd(reordered).unwrap();

        assert_eq!(first.package_id, second.package_id);
        assert_eq!(first.intent_packet.id, second.intent_packet.id);
        assert_eq!(first.generated_truths, second.generated_truths);
        assert_eq!(
            serde_json::to_value(&first).unwrap(),
            serde_json::to_value(&second).unwrap()
        );
    }

    #[test]
    fn truth_projection_overlay_versions_without_mutating_package() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let original_truths = package.generated_truths.clone();
        let mut edited_truths = package.generated_truths.clone();
        edited_truths.push_str("    And a finance owner can review the evidence\n");
        let actor_clause = package
            .source_jtbd
            .clauses_by_kind(JtbdClauseKind::Actor)
            .next()
            .unwrap()
            .id
            .clone();
        let overlay = TruthProjectionOverlay::new(
            package.package_id.clone(),
            package.truth_version.clone(),
            "v1.operator-review",
            edited_truths.clone(),
            "operator clarified review evidence",
            vec![actor_clause.clone()],
        );

        let applied = package.apply_projection_overlay(overlay.clone()).unwrap();

        assert_eq!(package.generated_truths, original_truths);
        assert_eq!(applied.truths, edited_truths);
        assert_eq!(applied.projection_version, "v1.operator-review");
        assert_eq!(
            applied.source,
            TruthProjectionSource::OverlayApplied {
                overlay_id: overlay.overlay_id,
                reason: "operator clarified review evidence".to_string(),
            }
        );
        assert_eq!(applied.lineage.source_clause_ids, vec![actor_clause]);
        assert_eq!(package.base_projection().truths, original_truths);
    }

    #[test]
    fn truth_projection_overlay_rejects_mismatches_and_unknown_clauses() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let known_clause = package.source_jtbd.clause_ids().next().unwrap().clone();
        let valid_overlay = TruthProjectionOverlay::new(
            package.package_id.clone(),
            package.truth_version.clone(),
            "v1.operator-review",
            package.generated_truths.clone(),
            "operator clarified review evidence",
            vec![known_clause],
        );

        let mut wrong_package = valid_overlay.clone();
        wrong_package.target_package_id = TruthPackageId::new("truth_package.other");
        assert!(matches!(
            package.apply_projection_overlay(wrong_package),
            Err(TruthOverlayError::PackageMismatch { .. })
        ));

        let mut wrong_version = valid_overlay.clone();
        wrong_version.target_truth_version = "v0".to_string();
        assert!(matches!(
            package.apply_projection_overlay(wrong_version),
            Err(TruthOverlayError::TruthVersionMismatch { .. })
        ));

        let mut unknown_clause = valid_overlay.clone();
        unknown_clause.source_clause_ids =
            vec![ClauseId::new("vendor_commitment", "actor.missing")];
        assert!(matches!(
            package.apply_projection_overlay(unknown_clause),
            Err(TruthOverlayError::UnknownSourceClause { .. })
        ));

        let mut invalid_truth = valid_overlay;
        invalid_truth.edited_truths = "Truth: Broken\n\n  Unknown:\n    Value: bad\n".to_string();
        assert!(matches!(
            package.apply_projection_overlay(invalid_truth),
            Err(TruthOverlayError::TruthProjectionParse { .. })
        ));
    }

    #[test]
    fn observed_stop_reason_matches_expected_set() {
        let expected = BTreeSet::from([
            ExpectedStopReason::Converged,
            ExpectedStopReason::CriteriaMet,
        ]);

        let criteria_met = ObservedStopReason::CriteriaMet {
            criteria: vec!["evidence_ready".to_string()],
        };
        let exhausted = ObservedStopReason::TokenBudgetExhausted {
            tokens_consumed: 10_001,
            limit: 10_000,
        };

        assert!(criteria_met.matches_expected(&expected));
        assert!(!exhausted.matches_expected(&expected));
        assert!(exhausted.is_budget_exhausted());
        assert_eq!(
            ObservedStopReason::HitlGatePending {
                gate_id: "gate-1".to_string(),
                proposal_id: "proposal-1".to_string(),
                summary: "approval required".to_string(),
                agent_id: "truth-policy-gate".to_string(),
                cycle: 2,
            }
            .expectation_kind(),
            ExpectedStopReason::HitlGatePending
        );
    }

    #[test]
    fn axiom_run_report_carries_stop_reason_facts_and_integrity() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let evidence_clause = package
            .source_jtbd
            .clauses_by_kind(JtbdClauseKind::EvidenceRequired)
            .next()
            .unwrap()
            .id
            .clone();
        let observation = AxiomRunObservation {
            stop_reason: ObservedStopReason::Converged,
            promoted_facts: vec![PromotedFactRecord {
                context_key: "Evidence".to_string(),
                fact_id: "fact.vendor_assessment".to_string(),
                summary: "vendor assessment present".to_string(),
                source_clause_ids: vec![evidence_clause.clone()],
                evidence_refs: vec![EvidenceRefRecord {
                    evidence_id: "evidence.vendor_assessment".to_string(),
                    source: "axiom_truth_package".to_string(),
                }],
                trace_link: Some(TraceLinkRecord {
                    trace_id: "trace.vendor_commitment.1".to_string(),
                    location: Some("fixture://vendor_commitment".to_string()),
                    replayable: true,
                }),
                promotion_authority: None,
            }],
            integrity: RunIntegrityProof::sha256_merkle("sha256:abc123", 7, 5),
            replay_notes: vec!["deterministic replay profile matched".to_string()],
            run_stages: Vec::new(),
        };

        let report =
            AxiomRunReport::from_observation(&package, AxiomRunVerdict::Satisfied, observation);

        assert_eq!(report.package_id, package.package_id);
        assert_eq!(report.truth_version, "v1");
        assert_eq!(report.intent_packet_id, package.intent_packet.id);
        assert_eq!(report.verdict, AxiomRunVerdict::Satisfied);
        assert!(report.expected_stop_reason_matched());
        assert_eq!(report.promoted_facts.len(), 1);
        assert_eq!(
            report.promoted_facts[0].source_clause_ids,
            vec![evidence_clause]
        );
        assert_eq!(report.integrity.merkle_root, "sha256:abc123");
        assert_eq!(report.integrity.clock_time, 7);
        assert_eq!(report.integrity.fact_count, 5);
        assert_eq!(
            report.source_clause_ids.len(),
            package.source_jtbd.clauses.len()
        );
        assert_eq!(
            serde_json::to_value(&report).unwrap()["observed_stop_reason"]["kind"],
            "converged"
        );
    }

    fn evidence_clause_id(package: &TruthPackage, key: &str) -> ClauseId {
        package
            .source_jtbd
            .clauses_by_kind(JtbdClauseKind::EvidenceRequired)
            .find(|clause| clause.key == key)
            .map_or_else(
                || panic!("missing evidence clause {key}"),
                |clause| clause.id.clone(),
            )
    }

    fn promoted_fact(
        context_key: &str,
        fact_id: &str,
        summary: &str,
        source_clause_ids: Vec<ClauseId>,
    ) -> PromotedFactRecord {
        PromotedFactRecord {
            context_key: context_key.to_string(),
            fact_id: fact_id.to_string(),
            summary: summary.to_string(),
            source_clause_ids,
            evidence_refs: vec![EvidenceRefRecord {
                evidence_id: format!("evidence.{fact_id}"),
                source: "test_fixture".to_string(),
            }],
            trace_link: Some(TraceLinkRecord {
                trace_id: format!("trace.{fact_id}"),
                location: Some("test://verifier".to_string()),
                replayable: true,
            }),
            promotion_authority: None,
        }
    }

    fn satisfying_observation(package: &TruthPackage) -> AxiomRunObservation {
        let promoted_facts: Vec<PromotedFactRecord> = package
            .source_jtbd
            .clauses_by_kind(JtbdClauseKind::EvidenceRequired)
            .map(|clause| {
                promoted_fact(
                    "Evidence",
                    &format!("fact.{}", clause.key),
                    &format!("{} observed", clause.canonical_text),
                    vec![clause.id.clone()],
                )
            })
            .collect();
        AxiomRunObservation {
            stop_reason: ObservedStopReason::Converged,
            promoted_facts,
            integrity: RunIntegrityProof::sha256_merkle("sha256:test", 1, 1),
            replay_notes: vec!["deterministic".to_string()],
            run_stages: Vec::new(),
        }
    }

    #[test]
    fn verify_satisfied_when_evidence_complete_and_stop_expected() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let observation = satisfying_observation(&package);

        assert_eq!(package.verify(&observation), AxiomRunVerdict::Satisfied);

        let report = AxiomRunReport::verify(&package, observation);
        assert_eq!(report.verdict, AxiomRunVerdict::Satisfied);
        assert!(report.expected_stop_reason_matched());
    }

    #[test]
    fn verify_invalid_when_promoted_fact_cites_unknown_clause() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let mut observation = satisfying_observation(&package);
        observation.promoted_facts.push(promoted_fact(
            "Evidence",
            "fact.unknown",
            "stray fact with no real clause",
            vec![ClauseId::new("vendor_commitment", "evidence.missing")],
        ));

        assert_eq!(package.verify(&observation), AxiomRunVerdict::Invalid);
    }

    #[test]
    fn verify_invalid_when_forbidden_action_text_appears_in_summary() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let mut observation = satisfying_observation(&package);
        observation.promoted_facts.push(promoted_fact(
            "Diagnostic",
            "fact.violation",
            "bypassed approval detected on commitment ABC",
            vec![evidence_clause_id(&package, "vendor_assessment")],
        ));

        assert_eq!(package.verify(&observation), AxiomRunVerdict::Invalid);
    }

    #[test]
    fn verify_invalid_when_forbidden_action_text_appears_in_replay_note() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let mut observation = satisfying_observation(&package);
        observation
            .replay_notes
            .push("missing audit trail surfaced post-run".to_string());

        assert_eq!(package.verify(&observation), AxiomRunVerdict::Invalid);
    }

    #[test]
    fn verify_exhausted_when_unexpected_budget_exhaustion() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let mut observation = satisfying_observation(&package);
        observation.stop_reason = ObservedStopReason::TokenBudgetExhausted {
            tokens_consumed: 1_000_000,
            limit: 100_000,
        };

        assert_eq!(package.verify(&observation), AxiomRunVerdict::Exhausted);
    }

    #[test]
    fn verify_blocked_when_unexpected_hitl_gate_pending() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let mut observation = satisfying_observation(&package);
        observation.stop_reason = ObservedStopReason::HitlGatePending {
            gate_id: "gate-1".to_string(),
            proposal_id: "proposal-1".to_string(),
            summary: "approval required".to_string(),
            agent_id: "policy-gate".to_string(),
            cycle: 3,
        };

        assert_eq!(package.verify(&observation), AxiomRunVerdict::Blocked);
    }

    #[test]
    fn verify_invalid_when_invariant_violated() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let mut observation = satisfying_observation(&package);
        observation.stop_reason = ObservedStopReason::InvariantViolated {
            class: "structural".to_string(),
            name: "spend_authority".to_string(),
            reason: "ceiling exceeded".to_string(),
        };

        assert_eq!(package.verify(&observation), AxiomRunVerdict::Invalid);
    }

    #[test]
    fn verify_invalid_when_expected_stop_but_evidence_missing() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let evidence = evidence_clause_id(&package, "vendor_assessment");
        // Cite only vendor_assessment but not the second `po` evidence clause.
        let observation = AxiomRunObservation {
            stop_reason: ObservedStopReason::Converged,
            promoted_facts: vec![promoted_fact(
                "Evidence",
                "fact.vendor_assessment",
                "vendor assessment captured",
                vec![evidence],
            )],
            integrity: RunIntegrityProof::sha256_merkle("sha256:test", 1, 1),
            replay_notes: vec![],
            run_stages: Vec::new(),
        };

        assert_eq!(package.verify(&observation), AxiomRunVerdict::Invalid);
    }

    #[test]
    fn time_budget_absent_uses_epoch_sentinel_and_omits_context_field() {
        let package = decode_jtbd(vendor_input()).unwrap();

        assert!(
            package
                .generated_truths
                .contains("Expires: 2099-01-01T00:00:00Z")
        );
        assert_eq!(package.source_jtbd.time_budget, None);
        assert!(
            package
                .intent_packet
                .context
                .get("time_budget_seconds")
                .is_none()
        );
    }

    #[test]
    fn time_budget_shifts_expires_and_populates_intent_context() {
        let input = vendor_input().with_time_budget(TimeBudget::from_minutes(45));
        let package = decode_jtbd(input).unwrap();

        assert!(
            package
                .generated_truths
                .contains("Expires: 2099-01-01T00:45:00Z")
        );
        assert_eq!(
            package.source_jtbd.time_budget,
            Some(TimeBudget::from_minutes(45))
        );
        assert_eq!(
            package.intent_packet.context["time_budget_seconds"],
            serde_json::json!(45 * 60)
        );
    }

    #[test]
    fn time_budget_preserves_decode_determinism() {
        let first =
            decode_jtbd(vendor_input().with_time_budget(TimeBudget::from_hours(2))).unwrap();
        let second =
            decode_jtbd(vendor_input().with_time_budget(TimeBudget::from_hours(2))).unwrap();

        assert_eq!(first.package_id, second.package_id);
        assert_eq!(first.intent_packet.id, second.intent_packet.id);
        assert_eq!(first.generated_truths, second.generated_truths);
        assert_eq!(
            serde_json::to_value(&first).unwrap(),
            serde_json::to_value(&second).unwrap()
        );
    }

    #[test]
    fn audit_fact_lineage_succeeds_when_facts_cite_evidence_clauses() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let report = AxiomRunReport::verify(&package, satisfying_observation(&package));

        let audit = report.audit_fact_lineage(&package).unwrap();
        assert_eq!(audit.package_id, package.package_id);
        assert_eq!(audit.truth_version, package.truth_version);
        assert_eq!(
            audit.facts_audited,
            package.verifier_spec.required_evidence.len()
        );
        assert_eq!(audit.evidence_coverage.len(), 2);
        assert!(audit.failure_coverage.is_empty());
    }

    #[test]
    fn audit_fact_lineage_rejects_unknown_clause() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let mut observation = satisfying_observation(&package);
        observation.promoted_facts.push(promoted_fact(
            "Evidence",
            "fact.unknown",
            "stray fact",
            vec![ClauseId::new("vendor_commitment", "evidence.missing")],
        ));
        // Use from_observation directly so the verdict path doesn't short-circuit
        // before audit_fact_lineage sees the unknown clause.
        let report =
            AxiomRunReport::from_observation(&package, AxiomRunVerdict::Satisfied, observation);

        match report.audit_fact_lineage(&package) {
            Err(FactLineageAuditError::UnknownClause { fact_id, .. }) => {
                assert_eq!(fact_id, "fact.unknown");
            }
            other => panic!("expected UnknownClause, got {other:?}"),
        }
    }

    #[test]
    fn audit_fact_lineage_rejects_fact_that_cites_only_scope_clauses() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let actor_clause = package
            .source_jtbd
            .clauses_by_kind(JtbdClauseKind::Actor)
            .next()
            .unwrap()
            .id
            .clone();
        let observation = AxiomRunObservation {
            stop_reason: ObservedStopReason::Converged,
            promoted_facts: vec![promoted_fact(
                "Diagnostic",
                "fact.scope-only",
                "actor identity confirmed",
                vec![actor_clause],
            )],
            integrity: RunIntegrityProof::sha256_merkle("sha256:test", 1, 1),
            replay_notes: vec![],
            run_stages: Vec::new(),
        };
        let report =
            AxiomRunReport::from_observation(&package, AxiomRunVerdict::Satisfied, observation);

        match report.audit_fact_lineage(&package) {
            Err(FactLineageAuditError::ScopeOnlyFact { fact_id }) => {
                assert_eq!(fact_id, "fact.scope-only");
            }
            other => panic!("expected ScopeOnlyFact, got {other:?}"),
        }
    }

    #[test]
    fn audit_fact_lineage_rejects_package_id_mismatch() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let mut report = AxiomRunReport::verify(&package, satisfying_observation(&package));
        report.package_id = TruthPackageId::new("truth_package.other");

        assert!(matches!(
            report.audit_fact_lineage(&package),
            Err(FactLineageAuditError::PackageMismatch { .. })
        ));
    }

    #[test]
    fn audit_fact_lineage_rejects_truth_version_mismatch() {
        let package = decode_jtbd(vendor_input()).unwrap();
        let mut report = AxiomRunReport::verify(&package, satisfying_observation(&package));
        report.truth_version = "v0".to_string();

        assert!(matches!(
            report.audit_fact_lineage(&package),
            Err(FactLineageAuditError::TruthVersionMismatch { .. })
        ));
    }
}