agent-chorus 0.14.1

Local-first CLI to read, compare, and hand off context across Codex, Claude, Gemini, and Cursor sessions.
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
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
use anyhow::{anyhow, Context, Result};
use globset::{Glob, GlobSet, GlobSetBuilder};
use serde_json::{json, Value};
use sha2::{Digest, Sha256};
use std::collections::BTreeSet;
use std::env;
use std::fs;
use std::io::Write;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};

const ZERO_SHA: &str = "0000000000000000000000000000000000000000";
const MAX_CHANGED_FILES_DISPLAYED: usize = 12;
/// Current manifest schema version understood by this tool.
/// Increment on backward-incompatible manifest changes. See P11 in the
/// agent-context plan for the enforcement contract on verify.
const CURRENT_SCHEMA_VERSION: u64 = 1;
const REQUIRED_FILES: &[&str] = &[
    "00_START_HERE.md",
    "10_SYSTEM_OVERVIEW.md",
    "20_CODE_MAP.md",
    "30_BEHAVIORAL_INVARIANTS.md",
    "40_OPERATIONS_AND_RELEASE.md",
];
const STRUCTURED_FILES: &[&str] = &[
    "routes.json",
    "completeness_contract.json",
    "reporting_rules.json",
];
const TASK_FAMILIES: &[&str] = &["lookup", "impact_analysis", "planning", "diagnosis"];

// P8 — hostile input & platform safety bounds.
/// Maximum bytes we will read into a pack file (F23). Files larger than this
/// are skipped with a warning so seal cannot OOM on a rogue asset.
const MAX_PACK_FILE_BYTES: u64 = 5_000_000;
/// Bytes inspected for NUL detection when classifying a file as binary (F19).
const BINARY_SNIFF_BYTES: usize = 8_192;
/// Maximum directory walk depth (F20). Guards against symlink loops and
/// pathological nested-dir layouts when resolving glob patterns.
const MAX_WALK_DEPTH: usize = 20;

pub struct BuildOptions {
    pub reason: Option<String>,
    pub base: Option<String>,
    pub head: Option<String>,
    pub pack_dir: Option<String>,
    /// Reserved: will be used when `build` constructs the start-here template with change summaries.
    #[allow(dead_code)]
    pub changed_files: Vec<String>,
    pub force_snapshot: bool,
}

pub struct InitOptions {
    pub pack_dir: Option<String>,
    pub cwd: Option<String>,
    pub force: bool,
    /// P8/F20: when true, dereference symlinks whose canonical target escapes
    /// the repo root. Default false (safe).
    pub follow_symlinks: bool,
    /// P13/F46: tiered adoption. Tier 1 = CODE_MAP + routes.json + manifest
    /// only. Tier 2 = Tier 1 + BEHAVIORAL_INVARIANTS + completeness_contract.
    /// Tier 3 = full pack (default, matches legacy behavior).
    pub tier: InitTier,
}

/// P13/F46: adoption tier. Tier 3 (default) preserves the existing full-pack
/// scaffolding. Tier 1 and Tier 2 scaffold a progressively smaller subset so
/// teams adopting the skill can start with a narrow, high-signal core and
/// expand later.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum InitTier {
    /// Minimal: 20_CODE_MAP.md + routes.json only. Manifest is still written
    /// at seal time.
    One,
    /// Tier 1 + 30_BEHAVIORAL_INVARIANTS.md + completeness_contract.json.
    Two,
    /// Full pack — same set as pre-P13 init.
    Three,
}

impl Default for InitTier {
    fn default() -> Self {
        InitTier::Three
    }
}

pub struct SealOptions {
    pub reason: Option<String>,
    pub base: Option<String>,
    pub head: Option<String>,
    pub pack_dir: Option<String>,
    pub cwd: Option<String>,
    pub force: bool,
    pub force_snapshot: bool,
    /// P8/F20: when true, dereference symlinks whose canonical target escapes
    /// the repo root. Default false (safe).
    pub follow_symlinks: bool,
}

struct FileMeta {
    path: String,
    /// P8/F21: lowercased copy of `path` for case-insensitive FS collision
    /// detection during verify. Written into the manifest alongside `path`.
    path_lower: String,
    sha256: String,
    bytes: u64,
    words: usize,
}

struct ManifestBundle {
    value: Value,
    stable_checksum: String,
    pack_checksum: String,
}

/// P1 — semantic baseline fields recorded on the manifest at seal so later
/// verification can detect drift without re-reading the full repo.
///
/// All fields are computed up-front by helpers in this module; `build_manifest`
/// simply serializes them. Helpers degrade to empty values when their source
/// config files are absent so `build_manifest` never has to branch on presence.
#[derive(Default)]
struct SemanticBaseline {
    /// Glob pattern -> number of repo-relative files currently matching.
    /// Sourced from `completeness_contract.json` `required_file_families[]`
    /// and `reporting_rules.json` `groupable_families[]`.
    family_counts: std::collections::BTreeMap<String, usize>,
    /// Numeric claims extracted from prose in `.agent-context/current/*.md`.
    /// Each entry: `{noun, count, file, line}`.
    declared_counts: Vec<Value>,
    /// For every file referenced in `search_scope.json` `verification_shortcuts`,
    /// top-level function signatures keyed `"<file>::<fn_name>"`.
    shortcut_signatures: std::collections::BTreeMap<String, String>,
    /// SHA256 of the dependency-declaring files that exist at the repo root.
    /// Keys: `pyproject`, `cargo`, `npm`. Missing files are simply omitted.
    dependencies_snapshot: std::collections::BTreeMap<String, String>,
}

pub struct VerifyOptions {
    pub pack_dir: Option<String>,
    pub cwd: String,
    pub ci: bool,
    pub base: Option<String>,
    /// When true and the manifest is unreadable or fails pack_checksum, attempt
    /// to restore `current/` from the most recent intact snapshot (F32).
    pub repair: bool,
    /// When combined with `repair`, perform the restore without prompting for
    /// interactive confirmation via stdin.
    pub repair_yes: bool,
    /// P3: when true, emit the suggest-patches JSON payload
    /// `{changed_files, pack_sections_to_update, diff_excerpt, baseline_drift}`
    /// on stdout and exit early. Disables normal human-readable output.
    pub suggest_patches: bool,
    /// P6: opt-in CI gate that fails if any commit in the PR range touches
    /// both `.agent-context/**` and non-pack paths. Intended for teams that
    /// want the "pack edits land as their own commit" convention enforced
    /// at merge time. Off by default.
    pub enforce_separate_commits: bool,
}

/// Result of running the freshness check as a reusable helper.
struct FreshnessResult {
    /// "pass", "warn", "skip", or "skipped"
    status: String,
    /// Context-relevant files that changed
    changed_files: Vec<String>,
    /// Whether .agent-context/current/ was touched in the diff
    pack_updated: bool,
    /// Reason when status is "skipped" (F24/F25/F27 — shallow-clone, initial-commit, non-git).
    /// None when status is pass/warn/skip.
    skipped_reason: Option<String>,
    /// P3: Pack sections (by filename) affected by the changed files, resolved
    /// through the `zones[]` array in `.agent-context/relevance.json`.
    /// Empty when no zone map is present or none of the changed files match.
    affected_sections: Vec<String>,
}

/// Detect whether `cwd` lives inside a shallow git clone. Returns `Ok(true)`
/// if `git rev-parse --is-shallow-repository` prints `true`. Non-zero exit
/// (e.g. non-git directory) returns `Ok(false)` so callers can perform their
/// own non-git detection.
fn is_shallow_repo(cwd: &Path) -> Result<bool> {
    let raw = run_git(&["rev-parse", "--is-shallow-repository"], cwd, true)?;
    Ok(raw.trim() == "true")
}

/// Detect whether `cwd` is inside a git repository. Uses `git rev-parse --git-dir`
/// which succeeds (non-empty stdout) inside any git repo and fails otherwise.
fn is_git_repo(cwd: &Path) -> bool {
    !run_git(&["rev-parse", "--git-dir"], cwd, true)
        .map(|s| s.trim().is_empty())
        .unwrap_or(true)
}

/// Count commits reachable from HEAD. Returns `Ok(None)` when the count
/// cannot be determined (e.g. no HEAD yet).
fn commit_count(cwd: &Path) -> Result<Option<u64>> {
    let raw = run_git(&["rev-list", "--count", "HEAD"], cwd, true)?;
    Ok(raw.trim().parse::<u64>().ok())
}

/// Resolve the current branch for manifest metadata. Returns `(branch, detached)`:
/// - `(Some(name), false)` for normal branches
/// - `(None, true)` when HEAD is detached (either `git symbolic-ref -q HEAD` fails
///   or `rev-parse --abbrev-ref HEAD` prints the literal `HEAD`)
/// - `(None, false)` only if git is absent entirely
fn resolve_branch(cwd: &Path) -> (Option<String>, bool) {
    // symbolic-ref fails on detached HEAD, succeeds otherwise.
    let symbolic = Command::new("git")
        .args(["symbolic-ref", "-q", "HEAD"])
        .current_dir(cwd)
        .output();
    let symbolic_ok = matches!(symbolic, Ok(ref out) if out.status.success());

    let abbrev = run_git(&["rev-parse", "--abbrev-ref", "HEAD"], cwd, true)
        .unwrap_or_default()
        .trim()
        .to_string();

    if !symbolic_ok || abbrev == "HEAD" {
        return (None, true);
    }
    if abbrev.is_empty() {
        return (None, false);
    }
    (Some(abbrev), false)
}

/// Outcome of the P11 schema-version gate on verify.
///
/// - `Ok` means the manifest is safe to process. A human-readable warning may
///   still be emitted (missing field, or manifest older than the current tool).
/// - `Err` means the manifest is newer than this tool understands; the caller
///   should fail loudly and ask the user to upgrade chorus.
pub(crate) enum SchemaVersionCheck {
    Ok { warning: Option<String> },
    TooNew { message: String },
}

/// Enforce `manifest.schema_version` against [`CURRENT_SCHEMA_VERSION`].
///
/// Contract (see P11 / F34 in the agent-context plan):
/// - missing field -> Ok, back-compat deprecation warning
/// - equal to current -> Ok, no warning
/// - older than current -> Ok, upgrade-recommended warning
/// - newer than current -> TooNew (verify must fail loudly)
pub(crate) fn check_schema_version(manifest: &Value) -> SchemaVersionCheck {
    let raw = manifest.get("schema_version");
    let version = raw.and_then(|v| v.as_u64());
    match (raw, version) {
        (None, _) => SchemaVersionCheck::Ok {
            warning: Some(format!(
                "manifest has no schema_version field; treating as v1 for back-compat. Re-seal to upgrade to v{CURRENT_SCHEMA_VERSION}."
            )),
        },
        (Some(_), None) => SchemaVersionCheck::Ok {
            warning: Some(format!(
                "manifest.schema_version is not a positive integer; treating as v1 for back-compat. Re-seal to upgrade to v{CURRENT_SCHEMA_VERSION}."
            )),
        },
        (Some(_), Some(v)) if v == CURRENT_SCHEMA_VERSION => SchemaVersionCheck::Ok { warning: None },
        (Some(_), Some(v)) if v < CURRENT_SCHEMA_VERSION => SchemaVersionCheck::Ok {
            warning: Some(format!(
                "manifest is schema v{v}, tool is v{CURRENT_SCHEMA_VERSION}. Re-seal to upgrade."
            )),
        },
        (Some(_), Some(v)) => SchemaVersionCheck::TooNew {
            message: format!(
                "manifest schema v{v} is newer than this tool (v{CURRENT_SCHEMA_VERSION}). Upgrade chorus."
            ),
        },
    }
}

/// Compute the SHA256 of the running `chorus` binary, used as
/// `manifest.verifier_sha256`. Returns `None` when the current executable
/// cannot be located or read (rare, but possible on exotic platforms).
fn current_exe_sha256() -> Option<String> {
    let path = std::env::current_exe().ok()?;
    let bytes = fs::read(&path).ok()?;
    Some(sha256_hex(&bytes))
}

fn check_freshness_inner(base: &str, cwd: &Path) -> Result<FreshnessResult> {
    // P3: delegate to the zone-map-aware variant. When no `relevance.json`
    // exists, or it only declares the legacy `include/exclude` shape, the
    // helper silently falls back to the Pass-0 hardcoded behavior.
    check_freshness_with_zones(base, cwd)
}

/// P3: zone-aware freshness check.
///
/// Extends [`check_freshness_inner`]'s original behavior with a zone map. When
/// `.agent-context/relevance.json` declares a `zones[]` array, each changed
/// file is resolved to its affected pack sections via the zone map and the
/// union is returned in `affected_sections`. When the file has no zone map,
/// or the map only declares legacy `include/exclude` rules, this falls back to
/// the Pass-0 `is_context_relevant_with_rules` behavior (the existing
/// hardcoded defaults).
///
/// Pass-0 semantics preserved:
/// - F27 non-git → skipped
/// - F24 shallow clone → skipped
/// - F25 initial commit → skipped
///
/// New P3 behavior:
/// - Zone map validation: any zone whose `paths[]` resolves to zero tracked
///   files is treated as an authoring bug and returned as a `fail` status so
///   `verify` surfaces it loudly.
fn check_freshness_with_zones(base: &str, cwd: &Path) -> Result<FreshnessResult> {
    // F27: non-git directory → explicit skipped status rather than silent empty diff.
    if !is_git_repo(cwd) {
        return Ok(FreshnessResult {
            status: "skipped".to_string(),
            changed_files: Vec::new(),
            pack_updated: false,
            skipped_reason: Some("non-git".to_string()),
            affected_sections: Vec::new(),
        });
    }

    // F24: shallow clone (CI `fetch-depth: 1`) makes `git diff origin/main...HEAD`
    // silently return empty. Surface this as an advisory skip instead of "pass".
    if is_shallow_repo(cwd).unwrap_or(false) {
        return Ok(FreshnessResult {
            status: "skipped".to_string(),
            changed_files: Vec::new(),
            pack_updated: false,
            skipped_reason: Some(
                "shallow-clone: increase fetch-depth to >=20".to_string(),
            ),
            affected_sections: Vec::new(),
        });
    }

    // F25: initial commit → no HEAD~1 to diff against. Return explicit skipped
    // status rather than relying on the fallback diff quietly producing empty output.
    if let Some(1) = commit_count(cwd)? {
        return Ok(FreshnessResult {
            status: "skipped".to_string(),
            changed_files: Vec::new(),
            pack_updated: false,
            skipped_reason: Some("initial-commit".to_string()),
            affected_sections: Vec::new(),
        });
    }

    // Resolve the repo root for zone-map lookup. Fall back to cwd on failure so
    // non-standard layouts still get freshness behavior (they just won't get
    // zone resolution).
    let repo_root = git_repo_root(cwd).unwrap_or_else(|_| cwd.to_path_buf());
    let zone_map = load_zone_map(&repo_root);

    // Validate zone map: each zone's paths must resolve to at least one tracked
    // file (per P3 plan). If not, emit a `fail` result so verify fails loudly.
    if let Some(ref zm) = zone_map {
        if let Some(msg) = validate_zone_map(zm, &repo_root)? {
            return Ok(FreshnessResult {
                status: "fail".to_string(),
                changed_files: Vec::new(),
                pack_updated: false,
                skipped_reason: Some(msg),
                affected_sections: Vec::new(),
            });
        }
    }

    let changed_files_raw = {
        let with_base = run_git(&["diff", "--name-only", &format!("{base}...HEAD")], cwd, true)?;
        if with_base.is_empty() {
            run_git(&["diff", "--name-only", "HEAD~1"], cwd, true)?
        } else {
            with_base
        }
    };

    let mut pack_touched = false;
    let mut relevant = Vec::new();
    let mut affected = BTreeSet::new();

    for file_path in changed_files_raw.lines().map(|line| line.trim()).filter(|line| !line.is_empty()) {
        if file_path.starts_with(".agent-context/current/") {
            pack_touched = true;
            continue;
        }
        match &zone_map {
            Some(zm) => {
                let sections = resolve_affected_sections(file_path, zm);
                if !sections.is_empty() {
                    relevant.push(file_path.to_string());
                    for s in sections {
                        affected.insert(s);
                    }
                }
            }
            None => {
                if is_context_relevant(file_path) {
                    relevant.push(file_path.to_string());
                }
            }
        }
    }

    let affected_vec: Vec<String> = affected.into_iter().collect();

    if relevant.is_empty() {
        return Ok(FreshnessResult {
            status: "pass".to_string(),
            changed_files: Vec::new(),
            pack_updated: pack_touched,
            skipped_reason: None,
            affected_sections: affected_vec,
        });
    }

    if pack_touched {
        return Ok(FreshnessResult {
            status: "pass".to_string(),
            changed_files: relevant,
            pack_updated: true,
            skipped_reason: None,
            affected_sections: affected_vec,
        });
    }

    Ok(FreshnessResult {
        status: "warn".to_string(),
        changed_files: relevant,
        pack_updated: false,
        skipped_reason: None,
        affected_sections: affected_vec,
    })
}

/// P6: inspect every commit in `base..HEAD` and return human-readable error
/// lines for commits that touch both `.agent-context/**` and non-pack paths.
/// Empty return means the range passes the separate-commit convention.
///
/// This is intentionally conservative: it only flags commits that *mix*
/// pack and non-pack changes. Pure-pack or pure-code commits are fine, as
/// are ranges where pack and code commits are interleaved but separated.
fn check_separate_commits(base: &str, cwd: &Path) -> Result<Vec<String>> {
    // If git history isn't usable (non-git, shallow clone, initial commit)
    // this check has nothing to say — return empty rather than fail loudly,
    // since the surrounding verify flow already reports those cases.
    if !is_git_repo(cwd) {
        return Ok(Vec::new());
    }
    if is_shallow_repo(cwd).unwrap_or(false) {
        return Ok(Vec::new());
    }

    let range = format!("{base}..HEAD");
    let log = run_git(&["log", "--format=%H", &range], cwd, true)?;
    if log.trim().is_empty() {
        return Ok(Vec::new());
    }

    let mut errors = Vec::new();
    for sha in log.lines().map(str::trim).filter(|s| !s.is_empty()) {
        let files = run_git(
            &["diff-tree", "--name-only", "--no-commit-id", "-r", sha],
            cwd,
            true,
        )?;
        let mut touches_pack = false;
        let mut touches_non_pack = false;
        for path in files.lines().map(str::trim).filter(|s| !s.is_empty()) {
            if path.starts_with(".agent-context/") {
                touches_pack = true;
            } else {
                touches_non_pack = true;
            }
            if touches_pack && touches_non_pack {
                break;
            }
        }
        if touches_pack && touches_non_pack {
            errors.push(format!("commit {sha} mixes pack + non-pack changes"));
        }
    }

    Ok(errors)
}

/// P6: persist the most recent freshness warning to
/// `.agent-context/current/.last_freshness.json` so the pre-push hook can
/// later detect a pack-only follow-up push and report "warning appears
/// addressed". Silent no-op when the pack directory is missing — this runs
/// from check-freshness paths that may fire before init has scaffolded
/// `current/` (e.g. a warn surfaced during a seal dry run).
fn write_last_freshness_state(repo_root: &Path, result: &FreshnessResult) {
    if result.status != "warn" {
        return;
    }
    let pack_root = resolve_pack_root(repo_root, None);
    let current_dir = pack_root.join("current");
    if !current_dir.exists() {
        return;
    }
    let state_path = current_dir.join(".last_freshness.json");
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);
    let payload = json!({
        "changed_files": result.changed_files,
        "affected_sections": result.affected_sections,
        "timestamp": timestamp,
    });
    // Best-effort: serialization failure or write failure must not break
    // freshness reporting. The hook tolerates a missing state file.
    if let Ok(serialized) = serde_json::to_string_pretty(&payload) {
        let _ = fs::write(&state_path, serialized);
    }
}

pub fn build(options: BuildOptions) -> Result<()> {
    // Wrapper: route to init or seal based on current pack state.
    let cwd = env::current_dir().context("Failed to resolve current directory")?;
    let repo_root = git_repo_root(&cwd)?;
    let pack_root = resolve_pack_root(&repo_root, options.pack_dir.as_deref());
    let current_dir = pack_root.join("current");

    if !current_dir.exists() || is_dir_empty(&current_dir)? {
        // No pack yet: initialize templates. Tier 3 preserves the legacy
        // full-pack behavior here; tiered adoption is only wired through the
        // direct `agent-context init --tier <N>` entrypoint, not through the
        // legacy `build` wrapper.
        return init(InitOptions {
            pack_dir: options.pack_dir,
            cwd: Some(cwd.display().to_string()),
            force: false,
            follow_symlinks: false,
            tier: InitTier::Three,
        });
    }

    // Pack exists: seal existing content.
    seal(SealOptions {
        reason: options.reason,
        base: options.base,
        head: options.head,
        pack_dir: options.pack_dir,
        cwd: Some(cwd.display().to_string()),
        force: false,
        force_snapshot: options.force_snapshot,
        follow_symlinks: false,
    })
}

pub fn init(options: InitOptions) -> Result<()> {
    let cwd = options
        .cwd
        .map(PathBuf::from)
        .unwrap_or_else(|| env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
    // F27: non-git directory → fail loudly rather than silently producing an
    // ill-formed pack (empty branch / head_sha, no freshness signal).
    if !is_git_repo(&cwd) {
        return Err(anyhow!(
            "[context-pack] init failed: not a git repository (cwd: {})",
            cwd.display()
        ));
    }
    let repo_root = git_repo_root(&cwd)?;
    let repo_name = repo_root
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or("repo")
        .to_string();
    let branch = run_git(&["rev-parse", "--abbrev-ref", "HEAD"], &repo_root, true)?
        .trim()
        .to_string();
    let head_sha = run_git(&["rev-parse", "HEAD"], &repo_root, true)?
        .trim()
        .to_string();

    let pack_root = resolve_pack_root(&repo_root, options.pack_dir.as_deref());
    let current_dir = pack_root.join("current");
    let guide_path = pack_root.join("GUIDE.md");
    let relevance_path = pack_root.join("relevance.json");

    if current_dir.exists() && !options.force {
        let mut has_files = false;
        for entry in fs::read_dir(&current_dir).with_context(|| {
            format!("Failed to read {}", current_dir.display())
        })? {
            if entry.is_ok() {
                has_files = true;
                break;
            }
        }
        if has_files {
            return Err(anyhow!(
                "[context-pack] init aborted: {} is not empty (use --force to overwrite)",
                rel_path(&current_dir, &repo_root)
            ));
        }
    }

    ensure_dir(&current_dir)?;
    ensure_dir(&pack_root)?;

    let generated_at = now_stamp();

    // P13/F46: pick the template set that matches the requested adoption tier.
    // Tier 3 is the default and preserves legacy behavior (all files). Tier 2
    // drops the optional markdown + structured files the team hasn't committed
    // to yet; Tier 1 is the narrowest viable starting point.
    let templates: Vec<(&str, String)> = match options.tier {
        InitTier::One => vec![
            ("20_CODE_MAP.md", build_template_code_map()),
            ("routes.json", build_routes_json()),
        ],
        InitTier::Two => vec![
            ("20_CODE_MAP.md", build_template_code_map()),
            ("30_BEHAVIORAL_INVARIANTS.md", build_template_invariants()),
            ("routes.json", build_routes_json()),
            (
                "completeness_contract.json",
                build_completeness_contract_json(),
            ),
        ],
        InitTier::Three => vec![
            (
                "00_START_HERE.md",
                build_template_start_here(&repo_name, &branch, &head_sha, &generated_at),
            ),
            ("10_SYSTEM_OVERVIEW.md", build_template_system_overview()),
            ("20_CODE_MAP.md", build_template_code_map()),
            ("30_BEHAVIORAL_INVARIANTS.md", build_template_invariants()),
            ("40_OPERATIONS_AND_RELEASE.md", build_template_operations()),
            ("routes.json", build_routes_json()),
            (
                "completeness_contract.json",
                build_completeness_contract_json(),
            ),
            ("reporting_rules.json", build_reporting_rules_json()),
            ("search_scope.json", build_search_scope_json()),
        ],
    };

    for (name, content) in &templates {
        write_text(&current_dir.join(name), content)?;
    }

    if !relevance_path.exists() || options.force {
        // P3: detect a `study/` directory at repo root and tailor the default
        // zone map accordingly so freshness surfaces the right pack sections.
        let has_study = repo_root.join("study").is_dir();
        write_text(
            &relevance_path,
            &default_relevance_json_with_study(has_study),
        )?;
    }

    if !guide_path.exists() || options.force {
        write_text(&guide_path, &build_guide())?;
    }

    // Wire agent config files with context-pack routing instructions.
    let agent_configs = [
        (
            "CLAUDE.md",
            "agent-chorus:context-pack:claude",
            build_context_pack_routing_block("claude"),
        ),
        (
            "AGENTS.md",
            "agent-chorus:context-pack:codex",
            build_context_pack_routing_block("codex"),
        ),
        (
            "GEMINI.md",
            "agent-chorus:context-pack:gemini",
            build_context_pack_routing_block("gemini"),
        ),
    ];
    for (filename, marker, routing_block) in &agent_configs {
        upsert_context_pack_block(&repo_root.join(filename), routing_block, marker)?;
    }
    println!("[context-pack] agent config files wired (CLAUDE.md, AGENTS.md, GEMINI.md)");

    // Auto-install the pre-push hook so freshness warnings fire on every main push.
    match install_hooks(&repo_root.to_string_lossy(), false) {
        Ok(_) => println!("[context-pack] pre-push hook installed"),
        Err(_) => eprintln!("[context-pack] WARN: could not auto-install pre-push hook — run `chorus context-pack install-hooks` manually"),
    }

    println!(
        "[context-pack] init completed: {}",
        rel_path(&current_dir, &repo_root)
    );
    println!(
        "[context-pack] next: fill markdown + structured files, then run `chorus context-pack seal`"
    );

    Ok(())
}

fn check_content_quality(current_dir: &Path) -> Vec<String> {
    let mut warnings = Vec::new();

    // CODE_MAP: Risk column presence and non-empty values
    let code_map_path = current_dir.join("20_CODE_MAP.md");
    if let Ok(content) = fs::read_to_string(&code_map_path) {
        let has_risk_header = content.lines().any(|l| {
            let lower = l.to_lowercase();
            l.contains('|') && lower.contains("risk")
        });
        if !has_risk_header {
            warnings.push("20_CODE_MAP.md: no Risk column found — add a Risk column to each table row (e.g. \"Silent failure if missed\")".to_string());
        } else {
            let empty_risk_count = content.lines().filter(|l| {
                l.starts_with('|') &&
                !l.contains("---") &&
                !l.to_lowercase().contains("risk") &&
                {
                    let cells: Vec<&str> = l.split('|').map(|c| c.trim()).filter(|c| !c.is_empty()).collect();
                    cells.last().map(|c| c.is_empty()).unwrap_or(false)
                }
            }).count();
            if empty_risk_count > 0 {
                warnings.push(format!("20_CODE_MAP.md: {empty_risk_count} row(s) have an empty Risk column — fill with \"Silent failure if missed\", \"KeyError at runtime\", etc."));
            }
        }
    }

    // BEHAVIORAL_INVARIANTS: checklist has rows with explicit file paths
    let invariants_path = current_dir.join("30_BEHAVIORAL_INVARIANTS.md");
    if let Ok(content) = fs::read_to_string(&invariants_path) {
        let table_rows: Vec<&str> = content.lines().filter(|l| {
            l.starts_with('|') &&
            !l.contains("---") &&
            !l.to_lowercase().contains("change") &&
            !l.to_lowercase().contains("files that must")
        }).collect();
        if table_rows.is_empty() {
            warnings.push("30_BEHAVIORAL_INVARIANTS.md: Update Checklist has no rows — add at least one change-type row with explicit file paths".to_string());
        } else {
            let has_file_path = table_rows.iter().any(|row| {
                // Look for path-like tokens: word chars + slash or dot + word chars
                let re_like = row.contains('/') || row.chars().filter(|c| *c == '.').count() > 0;
                re_like && row.len() > 20
            });
            if !has_file_path {
                warnings.push("30_BEHAVIORAL_INVARIANTS.md: checklist rows do not appear to name explicit file paths — rows should list files by path, not just description".to_string());
            }
        }
    }

    // SYSTEM_OVERVIEW: runtime or silent failure modes section
    let overview_path = current_dir.join("10_SYSTEM_OVERVIEW.md");
    if let Ok(content) = fs::read_to_string(&overview_path) {
        let has_runtime = content.lines().any(|l| {
            l.starts_with("## ") && (l.to_lowercase().contains("runtime") || l.to_lowercase().contains("silent failure"))
        });
        if !has_runtime {
            warnings.push("10_SYSTEM_OVERVIEW.md: no Runtime Architecture or Silent Failure Modes section found — agents need runtime behavior documented to diagnose silent failures".to_string());
        }
    }

    warnings
}

/// Returns `true` when `repo_root/<rel_path>` is tracked as git-ignored.
/// `git check-ignore -q -- <path>` exits 0 when ignored, 1 when not.
/// Anything else (including non-git / git absent) is treated as "not ignored"
/// so this helper is safe to call from the seal path without surfacing noise.
fn is_git_ignored(repo_root: &Path, rel_path: &str) -> bool {
    match Command::new("git")
        .args(["check-ignore", "-q", "--", rel_path])
        .current_dir(repo_root)
        .output()
    {
        Ok(out) => out.status.code() == Some(0),
        Err(_) => false,
    }
}

/// F28: return warning strings for zone paths (search_scope.json `search_directories`
/// and `verification_shortcuts`) whose on-disk file is git-ignored. Silent on
/// missing config, unreadable JSON, or missing files — those cases are either
/// already covered by other seal-time validation or are explicit opt-outs.
fn collect_gitignore_zone_warnings(repo_root: &Path, current_dir: &Path) -> Vec<String> {
    let mut warnings = Vec::new();
    let search_scope_path = current_dir.join("search_scope.json");
    let scope = match read_json(&search_scope_path) {
        Ok(Some(v)) => v,
        _ => return warnings,
    };
    let families = match scope.get("task_families").and_then(|v| v.as_object()) {
        Some(obj) => obj,
        None => return warnings,
    };

    let mut seen_warnings = BTreeSet::new();

    for (_task_name, family_val) in families {
        let entry = match family_val.as_object() {
            Some(obj) => obj,
            None => continue,
        };

        if let Some(dirs) = entry.get("search_directories").and_then(|v| v.as_array()) {
            for dir in dirs.iter().filter_map(|v| v.as_str()) {
                // Resolve dir relative to repo root. Warn per-dir only (don't walk).
                let on_disk = repo_root.join(dir);
                if on_disk.exists() && is_git_ignored(repo_root, dir) {
                    let msg = format!(
                        "zone path '{dir}' matches git-ignored file '{dir}' — update .gitignore or remove the zone"
                    );
                    if seen_warnings.insert(msg.clone()) {
                        warnings.push(msg);
                    }
                }
            }
        }

        if let Some(shortcuts) = entry.get("verification_shortcuts").and_then(|v| v.as_object()) {
            for file_key in shortcuts.keys() {
                // Shortcut keys may use "path:line" form; path comes first.
                let rel = file_key.split(':').next().unwrap_or(file_key.as_str());
                let on_disk = repo_root.join(rel);
                if on_disk.exists() && is_git_ignored(repo_root, rel) {
                    let msg = format!(
                        "zone path '{file_key}' matches git-ignored file '{rel}' — update .gitignore or remove the zone"
                    );
                    if seen_warnings.insert(msg.clone()) {
                        warnings.push(msg);
                    }
                }
            }
        }
    }

    warnings
}

fn is_hook_installed(repo_root: &Path) -> bool {
    let hooks_path = run_git(&["config", "--get", "core.hooksPath"], repo_root, true)
        .unwrap_or_default();
    let hooks_dir = if hooks_path.trim().is_empty() {
        repo_root.join(".githooks")
    } else {
        let p = Path::new(hooks_path.trim());
        if p.is_absolute() {
            p.to_path_buf()
        } else {
            repo_root.join(hooks_path.trim())
        }
    };
    let pre_push = hooks_dir.join("pre-push");
    if !pre_push.exists() {
        return false;
    }
    fs::read_to_string(&pre_push).map(|content| {
        content.contains("# --- agent-chorus:pre-push:start ---") ||
        content.contains("# --- agent-bridge:pre-push:start ---")
    }).unwrap_or(false)
}

pub fn seal(options: SealOptions) -> Result<()> {
    let cwd = options
        .cwd
        .map(PathBuf::from)
        .unwrap_or_else(|| env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
    // F27: non-git directory → fail loudly. Previous behavior silently produced a
    // manifest with empty branch/head_sha and skipped freshness with no explanation.
    if !is_git_repo(&cwd) {
        return Err(anyhow!(
            "[context-pack] seal failed: not a git repository (cwd: {})",
            cwd.display()
        ));
    }
    let repo_root = git_repo_root(&cwd)?;
    let repo_name = repo_root
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or("repo")
        .to_string();
    // F26: detect detached HEAD so manifest `branch` is `null`+`detached: true`
    // rather than the literal string "HEAD".
    let (branch_opt, detached) = resolve_branch(&repo_root);
    let branch = branch_opt.clone().unwrap_or_default();
    if detached {
        eprintln!(
            "[context-pack] NOTICE: HEAD is detached — manifest recorded as branch: null, detached: true"
        );
    }
    let head_sha = match options.head.as_ref() {
        Some(sha) if !sha.trim().is_empty() => Some(sha.trim().to_string()),
        _ => {
            let discovered = run_git(&["rev-parse", "HEAD"], &repo_root, true)?;
            if discovered.trim().is_empty() {
                None
            } else {
                Some(discovered.trim().to_string())
            }
        }
    };

    let pack_root = resolve_pack_root(&repo_root, options.pack_dir.as_deref());
    let current_dir = pack_root.join("current");
    let snapshots_dir = pack_root.join("snapshots");
    let history_path = pack_root.join("history.jsonl");
    let manifest_path = current_dir.join("manifest.json");
    let lock_path = pack_root.join("seal.lock");

    if !current_dir.exists() {
        return Err(anyhow!(
            "[context-pack] seal failed: {} does not exist (run init first)",
            rel_path(&current_dir, &repo_root)
        ));
    }

    let _lock = acquire_lock(&lock_path)?;
    ensure_dir(&snapshots_dir)?;

    let required_files = required_files_for_mode(&current_dir);

    for file in &required_files {
        let path = current_dir.join(file);
        if !path.exists() {
            return Err(anyhow!(
                "[context-pack] seal failed: missing required file {}",
                rel_path(&path, &repo_root)
            ));
        }
        if !options.force {
            // P8 — read via helper so a binary/non-UTF-8 required file raises
            // a clear error instead of panicking read_to_string.
            let content = read_file_for_pack(&path, &repo_root, options.follow_symlinks)
                .map_err(|e| anyhow!("[context-pack] seal failed: cannot read {}: {}", rel_path(&path, &repo_root), e))?;
            if content.contains("<!-- AGENT:") {
                return Err(anyhow!(
                    "[context-pack] seal failed: template markers remain in {} (use --force to override)",
                    rel_path(&path, &repo_root)
                ));
            }
        }
    }

    validate_structured_layer(&repo_root, &current_dir)?;

    let generated_at = now_stamp();
    let reason = options
        .reason
        .unwrap_or_else(|| "manual-seal".to_string());

    // Update 00_START_HERE.md snapshot metadata BEFORE collecting file checksums
    // so the manifest reflects the updated content.
    update_start_here_snapshot(&current_dir, branch.trim(), head_sha.as_deref(), &generated_at)?;

    // P1 — compute semantic baseline once, pass into build_manifest. Helpers
    // degrade to empty for repos without the optional structured configs.
    // Resolved here (pre file-hashing) because P5 handlebar expansion consumes
    // `family_counts` before the files are hashed.
    let baseline = collect_semantic_baseline(&repo_root, &current_dir);

    // P5 — expand `{{counts.<slug>}}` handlebars and detect stale prose
    // numeric claims before collect_files_meta hashes the files. The expanded
    // bytes are what get sealed into the manifest, so prose and manifest
    // agree by construction.
    let (slug_counts, noun_counts) = derive_count_maps(&baseline.family_counts);
    let expansion_reports =
        apply_count_templates(&current_dir, &required_files, &slug_counts, &noun_counts);
    let mut all_mismatches: Vec<NumericClaimMismatch> = Vec::new();
    for report in &expansion_reports {
        let abs_path = current_dir.join(&report.file);
        // Write back the expanded content only when it changed — avoids a
        // spurious snapshot bump on repos that don't use handlebars.
        let changed = match fs::read_to_string(&abs_path) {
            Ok(current) => current != report.expanded,
            Err(_) => false,
        };
        if changed {
            write_text_atomic(&abs_path, &report.expanded)?;
        }
        all_mismatches.extend(report.mismatches.iter().cloned());
    }
    if !all_mismatches.is_empty() {
        let mut msg = String::from(
            "[context-pack] seal failed: prose numeric claims disagree with authoritative family_counts:\n",
        );
        for m in &all_mismatches {
            msg.push_str(&format!(
                "  - {}:{}: claimed {} {}, authoritative {}\n",
                m.file, m.line, m.claimed_count, m.noun, m.authoritative_count
            ));
        }
        if options.force {
            // With --force we downgrade the failure to a warning so the
            // author can unstick an unusual seal. verify() still flags the
            // drift on the next run, preserving the audit trail.
            eprintln!("{}", msg.trim_end());
            eprintln!("[context-pack] WARN: --force downgraded count-claim failures to warnings");
        } else {
            msg.push_str(
                "  Fix: update prose to `{{counts.<slug>}}` or surround with `<!-- count-claim: ignore -->` / `<!-- count-claim: end -->`. Use --force to override.\n",
            );
            return Err(anyhow!(msg));
        }
    }

    let files_meta = collect_files_meta(
        &current_dir,
        &repo_root,
        &required_files,
        options.follow_symlinks,
    )?;

    let previous_manifest = read_json(&manifest_path)?;

    // P13/F58 + F50 — carry forward the last-known-good pointer and the
    // optional alias map from the previous manifest so re-seals don't wipe
    // values that the team configured or that verify --ci promoted.
    let carried_last_good = previous_manifest
        .as_ref()
        .and_then(|m| m.get("last_known_good_sha"))
        .cloned()
        .unwrap_or(Value::Null);
    let carried_aliases = previous_manifest
        .as_ref()
        .and_then(|m| m.get("aliases"))
        .cloned()
        .unwrap_or_else(|| Value::Object(serde_json::Map::new()));

    // P11-drift/F38: compute tool_hashes so verify can catch tampered scripts.
    let tool_hashes = compute_tool_hashes(&current_dir);

    let mut manifest = build_manifest(
        &generated_at,
        &repo_root,
        &repo_name,
        branch.trim(),
        detached,
        head_sha.as_deref(),
        &reason,
        options.base.as_deref(),
        &Vec::new(),
        &files_meta,
        &baseline,
        &tool_hashes,
    );
    if let Some(obj) = manifest.value.as_object_mut() {
        obj.insert("last_known_good_sha".to_string(), carried_last_good);
        obj.insert("aliases".to_string(), carried_aliases);
    }

    write_text_atomic(
        &manifest_path,
        &format!("{}\n", serde_json::to_string_pretty(&manifest.value)?),
    )?;
    let previous_stable = previous_manifest
        .as_ref()
        .and_then(|value| value.get("stable_checksum"))
        .and_then(|value| value.as_str())
        .map(|value| value.to_string());
    let previous_head = previous_manifest
        .as_ref()
        .and_then(|value| value.get("head_sha"))
        .and_then(|value| value.as_str())
        .map(|value| value.to_string());

    let changed = options.force_snapshot
        || previous_manifest.is_none()
        || previous_stable.as_deref() != Some(manifest.stable_checksum.as_str())
        || previous_head != head_sha;

    let quality_warnings = check_content_quality(&current_dir);
    for w in &quality_warnings {
        eprintln!("[context-pack] WARN: {w}");
    }

    // F28: warn if any zone path (search_scope.json search_directories / verification_shortcuts)
    // resolves to a file that is git-ignored — a strong signal the pack and .gitignore disagree.
    for w in collect_gitignore_zone_warnings(&repo_root, &current_dir) {
        eprintln!("[context-pack] WARN: {w}");
    }

    if !is_hook_installed(&repo_root) {
        eprintln!("[context-pack] WARN: pre-push hook is not installed — run `chorus context-pack install-hooks` to enable staleness detection on main pushes");
    }

    if changed {
        let mut snapshot_id = format!(
            "{}_{}",
            compact_timestamp(&generated_at),
            short_sha(head_sha.as_deref())
        );
        let mut snapshot_dir = snapshots_dir.join(&snapshot_id);
        let mut counter = 1;
        while snapshot_dir.exists() {
            snapshot_id = format!(
                "{}_{}-{}",
                compact_timestamp(&generated_at),
                short_sha(head_sha.as_deref()),
                counter
            );
            snapshot_dir = snapshots_dir.join(&snapshot_id);
            counter += 1;
        }

        // P12 / F42 — compute the audit trail fields *before* copying the
        // snapshot so the previous snapshot's markdown is still the correct
        // baseline. `prose_diff_sections` records which H2 headings changed
        // vs the most recent snapshot; empty on first-seal.
        let prose_diff_sections = compute_prose_diff_sections(&snapshots_dir, &current_dir);
        let sealed_by = git_committer_identity(&repo_root);

        copy_dir_recursive(&current_dir, &snapshot_dir)?;

        // P12 / F42 — write the audit-trail fields alongside the existing
        // history fields. Older readers keep working (additive schema).
        let history_entry = json!({
            "snapshot_id": snapshot_id,
            "generated_at": generated_at,
            "branch": branch.trim(),
            "head_sha": head_sha,
            "base_sha": options.base,
            "reason": reason,
            "changed_files": Vec::<String>::new(),
            "pack_checksum": manifest.pack_checksum,
            // P12/F42 — audit trail.
            "sealed_by": sealed_by,
            "prose_diff_sections": prose_diff_sections,
            "seal_reason": reason,
        });
        append_jsonl(&history_path, &history_entry)?;

        println!(
            "[context-pack] sealed: {} (snapshot {})",
            rel_path(&pack_root, &repo_root),
            history_entry.get("snapshot_id").and_then(|v| v.as_str()).unwrap_or("unknown")
        );
    } else {
        println!("[context-pack] unchanged; no new snapshot created");
    }

    Ok(())
}

pub fn sync_main(
    local_ref: &str,
    local_sha: &str,
    remote_ref: &str,
    remote_sha: &str,
) -> Result<()> {
    let cwd = env::current_dir().context("Failed to resolve current directory")?;
    let repo_root = git_repo_root(&cwd)?;

    if !is_main_push(local_ref, remote_ref) {
        println!("[context-pack] skipped (push is not targeting main)");
        return Ok(());
    }

    if local_sha.trim().is_empty() || is_zero_sha(local_sha) {
        println!("[context-pack] skipped (main deletion or empty local sha)");
        return Ok(());
    }

    let changed_files = compute_changed_files(&repo_root, Some(remote_sha), local_sha)?;
    let rules = load_relevance_rules(&repo_root);
    let relevant: Vec<&String> = changed_files
        .iter()
        .filter(|path| is_context_relevant_with_rules(path, &rules))
        .collect();

    if relevant.is_empty() {
        println!("[context-pack] skipped (no context-relevant file changes)");
        return Ok(());
    }

    // Advisory-only: warn but never block the push or auto-build
    eprintln!(
        "[context-pack] ADVISORY: context-relevant files changed on main push. \
         Update pack content with your agent, then run 'chorus context-pack seal'."
    );

    Ok(())
}

/// Legacy rollback entrypoint. Kept for internal callers / tests that still
/// use the positional signature. New code should prefer
/// [`rollback_with_options`] so `--latest-good` is always plumbable.
#[allow(dead_code)]
pub fn rollback(snapshot: Option<&str>, pack_dir: Option<&str>) -> Result<()> {
    rollback_with_options(RollbackOptions {
        snapshot: snapshot.map(|s| s.to_string()),
        pack_dir: pack_dir.map(|s| s.to_string()),
        latest_good: false,
    })
}

/// P13/F58: structured rollback options. Splits `latest_good` out from the
/// snapshot ID so callers can't accidentally pass a literal "latest-good"
/// string as a snapshot name.
///
/// TODO(P13-continuation): The items below are deliberately deferred from the
/// P13 cut because they are each large enough to deserve their own package;
/// they live in the plan under P13 but are scoped for a follow-up PR.
///   - F48 — `explain-diff` subcommand.
///   - F49 — monorepo multi-team mode.
///   - F51 — canonical routing template (cross-track with team_skills).
///   - F52 — scheduled re-run of acceptance tests.
///   - F53 — cross-file integrity check.
///   - F54 — difficulty floor for acceptance tests.
///   - F59 — cryptographic history chain.
///   - F45 — AUTHORING_TODO.md.
pub struct RollbackOptions {
    pub snapshot: Option<String>,
    pub pack_dir: Option<String>,
    /// When true, ignore `snapshot` and roll back to the snapshot whose
    /// `head_sha` matches `manifest.json`'s `last_known_good_sha`.
    pub latest_good: bool,
}

pub fn rollback_with_options(options: RollbackOptions) -> Result<()> {
    let cwd = env::current_dir().context("Failed to resolve current directory")?;
    let repo_root = git_repo_root(&cwd)?;
    let pack_root = resolve_pack_root(&repo_root, options.pack_dir.as_deref());
    let current_dir = pack_root.join("current");
    let snapshots_dir = pack_root.join("snapshots");
    let lock_path = pack_root.join("seal.lock");

    // Rollback mutates .agent-context/current/ — serialize against seal (F29).
    let _lock = acquire_lock(&lock_path)?;

    let mut snapshot_ids = fs::read_dir(&snapshots_dir)
        .with_context(|| format!("Failed to list snapshots at {}", snapshots_dir.display()))?
        .filter_map(|entry| entry.ok())
        .filter(|entry| entry.path().is_dir())
        .filter_map(|entry| entry.file_name().to_str().map(|s| s.to_string()))
        .collect::<Vec<_>>();
    snapshot_ids.sort();

    if snapshot_ids.is_empty() {
        return Err(anyhow!(
            "[context-pack] no snapshots found in {}",
            rel_path(&snapshots_dir, &repo_root)
        ));
    }

    // P13/F58: --latest-good reads the manifest's `last_known_good_sha`, then
    // picks the snapshot whose history entry matches it. Fail loudly when
    // the pointer is missing — silently falling back to "latest" would hide
    // the very drift this flag is meant to surface.
    let target_snapshot = if options.latest_good {
        let manifest_path = current_dir.join("manifest.json");
        let good_sha = read_json(&manifest_path)?
            .as_ref()
            .and_then(|m| m.get("last_known_good_sha"))
            .and_then(|v| v.as_str())
            .map(|s| s.to_string())
            .filter(|s| !s.is_empty())
            .ok_or_else(|| {
                anyhow!(
                    "[context-pack] rollback --latest-good failed: manifest.json has no \
                     `last_known_good_sha` (run `verify --ci` on a green commit first)"
                )
            })?;

        find_snapshot_for_head_sha(&pack_root, &good_sha)?.ok_or_else(|| {
            anyhow!(
                "[context-pack] rollback --latest-good failed: no snapshot matches \
                 last_known_good_sha `{}`",
                good_sha
            )
        })?
    } else {
        options
            .snapshot
            .clone()
            .unwrap_or_else(|| snapshot_ids.last().cloned().unwrap_or_default())
    };

    if !snapshot_ids.iter().any(|id| id == &target_snapshot) {
        // F55: if not found in the active snapshots directory, check the rotated
        // history index so rollback can still locate snapshots referenced in
        // archived history files.
        if !snapshot_known_to_history_index(&pack_root, &target_snapshot) {
            return Err(anyhow!(
                "[context-pack] snapshot not found: {}",
                target_snapshot
            ));
        }
    }

    let source_dir = snapshots_dir.join(&target_snapshot);
    if !source_dir.exists() {
        return Err(anyhow!(
            "[context-pack] snapshot directory missing: {} (tracked in history_index but files were pruned)",
            rel_path(&source_dir, &repo_root)
        ));
    }
    if current_dir.exists() {
        fs::remove_dir_all(&current_dir)
            .with_context(|| format!("Failed to clear {}", current_dir.display()))?;
    }
    ensure_dir(&current_dir)?;
    copy_dir_recursive(&source_dir, &current_dir)?;

    println!(
        "[context-pack] restored snapshot {} -> {}",
        target_snapshot,
        rel_path(&current_dir, &repo_root)
    );
    Ok(())
}

/// P13/F58: rewrite `manifest.json`'s `last_known_good_sha` field to the given
/// SHA. Read -> mutate -> atomic write. Caller is expected to only invoke this
/// when verify --ci has passed; silently returns an error to let callers
/// treat the write as best-effort.
fn update_last_known_good(manifest_path: &Path, sha: &str) -> Result<()> {
    if !manifest_path.exists() {
        return Err(anyhow!(
            "manifest.json not found at {}",
            manifest_path.display()
        ));
    }
    let raw = fs::read_to_string(manifest_path)
        .with_context(|| format!("Failed to read {}", manifest_path.display()))?;
    let mut value: Value = serde_json::from_str(&raw)
        .with_context(|| format!("Failed to parse {}", manifest_path.display()))?;
    if let Some(obj) = value.as_object_mut() {
        obj.insert(
            "last_known_good_sha".to_string(),
            Value::String(sha.to_string()),
        );
    } else {
        return Err(anyhow!("manifest root is not a JSON object"));
    }
    let serialized = serde_json::to_string_pretty(&value)?;
    write_text_atomic(manifest_path, &format!("{serialized}\n"))?;
    Ok(())
}

/// P13/F58: locate the snapshot whose history entry's `head_sha` matches
/// `target_sha`. Scans `history.jsonl` first, then falls back to the rotated
/// history index files. Returns `Ok(Some(snapshot_id))` when found. The match
/// is exact — callers that want short-sha matching should normalize first.
fn find_snapshot_for_head_sha(pack_root: &Path, target_sha: &str) -> Result<Option<String>> {
    let scan_file = |path: &Path| -> Option<String> {
        let raw = fs::read_to_string(path).ok()?;
        // Scan newest-first: later entries overwrite earlier matches.
        let mut best: Option<String> = None;
        for line in raw.lines() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }
            let value: Value = match serde_json::from_str(trimmed) {
                Ok(v) => v,
                Err(_) => continue,
            };
            let sha = value.get("head_sha").and_then(|v| v.as_str()).unwrap_or("");
            if sha == target_sha {
                if let Some(id) = value
                    .get("snapshot_id")
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string())
                {
                    best = Some(id);
                }
            }
        }
        best
    };

    let active = pack_root.join("history.jsonl");
    if let Some(id) = scan_file(&active) {
        return Ok(Some(id));
    }

    // Fall back to rotated archives named in `history_index.json`.
    let index_path = pack_root.join("history_index.json");
    if let Ok(Some(index)) = read_json(&index_path) {
        if let Some(files) = index.get("files").and_then(|f| f.as_array()) {
            for entry in files {
                let name = match entry.get("name").and_then(|n| n.as_str()) {
                    Some(n) => n,
                    None => continue,
                };
                let rotated = pack_root.join(name);
                if let Some(id) = scan_file(&rotated) {
                    return Ok(Some(id));
                }
            }
        }
    }
    Ok(None)
}

/// Check whether the requested snapshot ID appears in any rotated history file
/// recorded in `history_index.json`. Allows rollback to surface a useful error
/// when the snapshots directory and the history index have diverged (F55).
fn snapshot_known_to_history_index(pack_root: &Path, snapshot_id: &str) -> bool {
    let index_path = pack_root.join("history_index.json");
    let index = match read_json(&index_path) {
        Ok(Some(v)) => v,
        _ => return false,
    };
    let files = match index.get("files").and_then(|f| f.as_array()) {
        Some(a) => a,
        None => return false,
    };
    for entry in files {
        let name = match entry.get("name").and_then(|n| n.as_str()) {
            Some(n) => n,
            None => continue,
        };
        let rotated_path = pack_root.join(name);
        if let Ok(raw) = fs::read_to_string(&rotated_path) {
            for line in raw.lines() {
                let trimmed = line.trim();
                if trimmed.is_empty() {
                    continue;
                }
                if let Ok(val) = serde_json::from_str::<Value>(trimmed) {
                    if val.get("snapshot_id").and_then(|s| s.as_str()) == Some(snapshot_id) {
                        return true;
                    }
                }
            }
        }
    }
    false
}

const HOOK_SENTINEL_START: &str = "# --- agent-chorus:pre-push:start ---";
const HOOK_SENTINEL_END: &str = "# --- agent-chorus:pre-push:end ---";
// Legacy sentinels for backward compatibility during migration
const LEGACY_HOOK_SENTINEL_START: &str = "# --- agent-bridge:pre-push:start ---";
const LEGACY_HOOK_SENTINEL_END: &str = "# --- agent-bridge:pre-push:end ---";
// P1 — sentinels for the opt-in post-commit-reconcile hook. Kept distinct from
// the pre-push sentinels so installing or uninstalling one never mangles the
// other.
const POST_COMMIT_SENTINEL_START: &str = "# --- agent-chorus:post-commit:start ---";
const POST_COMMIT_SENTINEL_END: &str = "# --- agent-chorus:post-commit:end ---";

pub fn install_hooks(cwd: &str, dry_run: bool) -> Result<()> {
    install_hooks_with_options(cwd, dry_run, false)
}

/// P1 — `install-hooks` extended surface. Callers that want the post-commit
/// reconcile hook pass `enable_post_commit_reconcile: true`. The base
/// pre-push install path is unchanged so existing callers keep working.
pub fn install_hooks_with_options(
    cwd: &str,
    dry_run: bool,
    enable_post_commit_reconcile: bool,
) -> Result<()> {
    let cwd_path = PathBuf::from(cwd);
    let repo_root = git_repo_root(&cwd_path)?;

    let existing_hooks_path = run_git(&["config", "--get", "core.hooksPath"], &repo_root, true)?;

    // Determine hooks directory — prefer existing if set, otherwise use .githooks
    let hooks_dir = if !existing_hooks_path.is_empty() {
        if existing_hooks_path != ".githooks" {
            println!(
                "[context-pack] NOTE: core.hooksPath is '{}'; appending chorus hook there.",
                existing_hooks_path
            );
        }
        repo_root.join(&existing_hooks_path)
    } else {
        repo_root.join(".githooks")
    };

    let pre_push_path = hooks_dir.join("pre-push");
    let chorus_section = format!(
        "{}\n{}\n{}",
        HOOK_SENTINEL_START,
        build_pre_push_hook_section(),
        HOOK_SENTINEL_END
    );

    let final_content = if pre_push_path.exists() {
        let existing = fs::read_to_string(&pre_push_path).unwrap_or_default();
        // Detect new or legacy sentinels
        let (has_sentinel, sentinel_start, sentinel_end_str) =
            if existing.contains(HOOK_SENTINEL_START) && existing.contains(HOOK_SENTINEL_END) {
                (true, HOOK_SENTINEL_START, HOOK_SENTINEL_END)
            } else if existing.contains(LEGACY_HOOK_SENTINEL_START) && existing.contains(LEGACY_HOOK_SENTINEL_END) {
                (true, LEGACY_HOOK_SENTINEL_START, LEGACY_HOOK_SENTINEL_END)
            } else {
                (false, "", "")
            };
        if has_sentinel {
            // Replace existing chorus section
            let start_idx = existing.find(sentinel_start).unwrap();
            let end_idx = existing.find(sentinel_end_str).unwrap() + sentinel_end_str.len();
            // Trim trailing newline after end sentinel if present
            let end_idx = if existing.as_bytes().get(end_idx) == Some(&b'\n') {
                end_idx + 1
            } else {
                end_idx
            };
            format!("{}{}\n{}", &existing[..start_idx], chorus_section, &existing[end_idx..])
        } else {
            // Append chorus section to existing hook
            let mut content = existing;
            if !content.ends_with('\n') {
                content.push('\n');
            }
            content.push('\n');
            content.push_str(&chorus_section);
            content.push('\n');
            content
        }
    } else {
        // Create new hook file with shebang
        format!("#!/usr/bin/env bash\nset -euo pipefail\n\n{}\n", chorus_section)
    };

    let content_unchanged = if pre_push_path.exists() {
        fs::read_to_string(&pre_push_path).unwrap_or_default() == final_content
    } else {
        false
    };

    if !dry_run {
        ensure_dir(&hooks_dir)?;
        write_text(&pre_push_path, &final_content)?;
        #[cfg(unix)]
        {
            let mut perms = fs::metadata(&pre_push_path)?.permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&pre_push_path, perms)?;
        }
        // Only set core.hooksPath if it wasn't already configured
        if existing_hooks_path.is_empty() {
            run_git(&["config", "core.hooksPath", ".githooks"], &repo_root, false)?;
        }
    }

    let status = if dry_run {
        "planned"
    } else if content_unchanged {
        "unchanged"
    } else {
        "updated"
    };
    println!(
        "[context-pack] {}: {}",
        status,
        rel_path(&pre_push_path, &repo_root)
    );
    if !dry_run {
        println!("[context-pack] pre-push hook is active");
    }

    // P1 — opt-in post-commit reconcile hook. When enabled, we write
    // `.githooks/post-commit` (or the configured hooks dir) that invokes
    // `chorus agent-context post-commit-reconcile` when the commit touched
    // `.agent-context/**`. This keeps the manifest's `post_commit_sha` aligned
    // with HEAD after a pack-bearing commit lands.
    if enable_post_commit_reconcile {
        let post_commit_path = hooks_dir.join("post-commit");
        let post_commit_section = format!(
            "{}\n{}\n{}",
            POST_COMMIT_SENTINEL_START,
            build_post_commit_hook_section(),
            POST_COMMIT_SENTINEL_END
        );
        let final_pc = if post_commit_path.exists() {
            let existing = fs::read_to_string(&post_commit_path).unwrap_or_default();
            if existing.contains(POST_COMMIT_SENTINEL_START)
                && existing.contains(POST_COMMIT_SENTINEL_END)
            {
                let start_idx = existing.find(POST_COMMIT_SENTINEL_START).unwrap();
                let end_idx = existing.find(POST_COMMIT_SENTINEL_END).unwrap()
                    + POST_COMMIT_SENTINEL_END.len();
                let end_idx = if existing.as_bytes().get(end_idx) == Some(&b'\n') {
                    end_idx + 1
                } else {
                    end_idx
                };
                format!(
                    "{}{}\n{}",
                    &existing[..start_idx],
                    post_commit_section,
                    &existing[end_idx..]
                )
            } else {
                let mut content = existing;
                if !content.ends_with('\n') {
                    content.push('\n');
                }
                content.push('\n');
                content.push_str(&post_commit_section);
                content.push('\n');
                content
            }
        } else {
            format!(
                "#!/usr/bin/env bash\nset -euo pipefail\n\n{}\n",
                post_commit_section
            )
        };
        if !dry_run {
            ensure_dir(&hooks_dir)?;
            write_text(&post_commit_path, &final_pc)?;
            #[cfg(unix)]
            {
                let mut perms = fs::metadata(&post_commit_path)?.permissions();
                perms.set_mode(0o755);
                fs::set_permissions(&post_commit_path, perms)?;
            }
        }
        println!(
            "[context-pack] {}: {}",
            if dry_run { "planned" } else { "updated" },
            rel_path(&post_commit_path, &repo_root)
        );
    }

    Ok(())
}

/// P4 — shipped PreToolUse hook template. Kept inline so `install-hooks
/// --install-settings-template` can be used without the shipped
/// `templates/` directory on disk (published binaries, `npx` invocations
/// where the template tree is not unpacked alongside the CLI).
///
/// Keep in sync with `cli/templates/settings.agent-context.json`.
const SETTINGS_TEMPLATE_JSON: &str = include_str!("../templates/settings.agent-context.json");

/// P4 — merge `templates/settings.agent-context.json` into
/// `<cwd>/.claude/settings.json`, preserving existing keys and existing
/// `hooks.PreToolUse` entries. Missing-file → creates the file. Existing
/// file → deep-merge the template's `hooks.PreToolUse` entries, skipping
/// any entry whose `matcher` already has a command identical to one we
/// would insert. Writes are atomic so a crash mid-write never leaves
/// `settings.json` half-rewritten.
///
/// Idempotency: running twice produces identical content because the
/// matcher+command pair is the de-dup key.
pub fn install_settings_template(cwd: &str, dry_run: bool) -> Result<()> {
    let cwd_path = PathBuf::from(cwd);
    let claude_dir = cwd_path.join(".claude");
    let settings_path = claude_dir.join("settings.json");

    // Parse the shipped template once. A broken template is a programmer
    // bug, not a user error — fail fast and loudly.
    let template: Value = serde_json::from_str(SETTINGS_TEMPLATE_JSON)
        .context("Failed to parse embedded settings.agent-context.json template")?;

    let existing: Value = if settings_path.exists() {
        let raw = fs::read_to_string(&settings_path)
            .with_context(|| format!("Failed to read {}", settings_path.display()))?;
        if raw.trim().is_empty() {
            Value::Object(serde_json::Map::new())
        } else {
            serde_json::from_str(&raw)
                .with_context(|| format!("Failed to parse {}", settings_path.display()))?
        }
    } else {
        Value::Object(serde_json::Map::new())
    };

    let merged = merge_settings_template(&existing, &template);
    let merged_text = format!("{}\n", serde_json::to_string_pretty(&merged)?);

    let existing_text = if settings_path.exists() {
        fs::read_to_string(&settings_path).unwrap_or_default()
    } else {
        String::new()
    };
    let unchanged = existing_text == merged_text;

    if dry_run {
        println!(
            "[agent-context] planned: {} ({})",
            rel_path(&settings_path, &cwd_path),
            if unchanged { "unchanged" } else { "merge PreToolUse hooks" }
        );
        return Ok(());
    }

    if unchanged {
        println!(
            "[agent-context] unchanged: {}",
            rel_path(&settings_path, &cwd_path)
        );
        return Ok(());
    }

    ensure_dir(&claude_dir)?;
    write_text_atomic(&settings_path, &merged_text)?;
    println!(
        "[agent-context] updated: {} (PreToolUse hooks merged)",
        rel_path(&settings_path, &cwd_path)
    );
    Ok(())
}

/// P4 — pure helper so merge behavior is unit-testable without touching
/// the filesystem. Combines two settings.json values with the following
/// rules:
///
/// 1. Non-`hooks` keys from `existing` survive unchanged.
/// 2. For `hooks.PreToolUse`, each template entry is appended unless the
///    existing config already contains an entry with the same `matcher`
///    AND a command string that matches the template's command. This
///    preserves user-authored matchers while keeping the merge idempotent.
/// 3. For any other `hooks.*` array in the template, entries are appended
///    only when their serialized form is not already present.
pub(crate) fn merge_settings_template(existing: &Value, template: &Value) -> Value {
    let mut out = match existing {
        Value::Object(map) => map.clone(),
        _ => serde_json::Map::new(),
    };

    let template_hooks = match template.get("hooks").and_then(|v| v.as_object()) {
        Some(h) => h,
        None => return Value::Object(out),
    };

    let existing_hooks = out
        .get("hooks")
        .and_then(|v| v.as_object())
        .cloned()
        .unwrap_or_default();
    let mut merged_hooks = existing_hooks.clone();

    for (hook_name, template_entries) in template_hooks {
        let template_arr = match template_entries.as_array() {
            Some(arr) => arr,
            None => continue,
        };
        let mut combined: Vec<Value> = merged_hooks
            .get(hook_name)
            .and_then(|v| v.as_array())
            .cloned()
            .unwrap_or_default();

        for entry in template_arr {
            if !hooks_entry_already_present(&combined, entry) {
                combined.push(entry.clone());
            }
        }

        merged_hooks.insert(hook_name.clone(), Value::Array(combined));
    }

    out.insert("hooks".to_string(), Value::Object(merged_hooks));
    Value::Object(out)
}

/// P4 — idempotency check for a single hook entry. An entry is "already
/// present" when some existing entry shares the same `matcher` value AND
/// one of its inner `hooks[].command` strings equals one of the template
/// entry's commands. This lets users add their own commands to a matcher
/// we ship without tripping duplicate detection, while still keeping our
/// own commands from being appended twice.
fn hooks_entry_already_present(existing_arr: &[Value], candidate: &Value) -> bool {
    let candidate_matcher = candidate.get("matcher").and_then(|v| v.as_str());
    let candidate_commands: Vec<&str> = candidate
        .get("hooks")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|h| h.get("command").and_then(|c| c.as_str()))
                .collect()
        })
        .unwrap_or_default();

    for existing in existing_arr {
        let existing_matcher = existing.get("matcher").and_then(|v| v.as_str());
        if existing_matcher != candidate_matcher {
            continue;
        }
        let existing_commands: Vec<&str> = existing
            .get("hooks")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|h| h.get("command").and_then(|c| c.as_str()))
                    .collect()
            })
            .unwrap_or_default();
        for cmd in &candidate_commands {
            if existing_commands.contains(cmd) {
                return true;
            }
        }
    }
    false
}

/// P1 — `chorus agent-context post-commit-reconcile`. Reads the current
/// manifest, stamps `post_commit_sha` with the current git HEAD, and writes
/// the manifest atomically back. No-op (Ok) when the manifest is absent —
/// this lets the post-commit hook run unconditionally without crashing on
/// repos that have not run `init` yet.
///
/// Invariant: never mutates `head_sha_at_seal`, `head_sha`, or any content
/// hashes. Only `post_commit_sha` (and `post_commit_reconciled_at` for
/// operator forensics) are updated.
pub fn post_commit_reconcile(cwd: Option<&str>, pack_dir: Option<&str>) -> Result<()> {
    let cwd_path = match cwd {
        Some(c) => PathBuf::from(c),
        None => env::current_dir().context("Failed to resolve current directory")?,
    };
    if !is_git_repo(&cwd_path) {
        return Err(anyhow!(
            "[context-pack] post-commit-reconcile failed: not a git repository (cwd: {})",
            cwd_path.display()
        ));
    }
    let repo_root = git_repo_root(&cwd_path)?;
    let pack_root = resolve_pack_root(&repo_root, pack_dir);
    let current_dir = pack_root.join("current");
    let manifest_path = current_dir.join("manifest.json");
    if !manifest_path.exists() {
        eprintln!(
            "[context-pack] post-commit-reconcile: manifest not found at {} — skipping",
            rel_path(&manifest_path, &repo_root)
        );
        return Ok(());
    }

    let lock_path = pack_root.join("seal.lock");
    // F29/F30: reconcile rewrites the manifest, so we acquire the same lock
    // seal uses. Verify is lock-free and unaffected.
    let _lock = acquire_lock(&lock_path)?;

    let raw = fs::read_to_string(&manifest_path)
        .with_context(|| format!("Failed to read {}", manifest_path.display()))?;
    let mut manifest: Value = serde_json::from_str(&raw)
        .with_context(|| format!("Failed to parse {}", manifest_path.display()))?;

    let head_sha = run_git(&["rev-parse", "HEAD"], &repo_root, true)?
        .trim()
        .to_string();
    if head_sha.is_empty() {
        return Err(anyhow!(
            "[context-pack] post-commit-reconcile failed: could not resolve git HEAD"
        ));
    }

    if let Some(obj) = manifest.as_object_mut() {
        obj.insert("post_commit_sha".to_string(), Value::String(head_sha.clone()));
        obj.insert(
            "post_commit_reconciled_at".to_string(),
            Value::String(now_stamp()),
        );
    } else {
        return Err(anyhow!(
            "[context-pack] post-commit-reconcile failed: manifest is not a JSON object"
        ));
    }

    write_text_atomic(
        &manifest_path,
        &format!("{}\n", serde_json::to_string_pretty(&manifest)?),
    )?;
    println!(
        "[context-pack] post-commit-reconcile: post_commit_sha={} ({})",
        short_sha(Some(&head_sha)),
        rel_path(&manifest_path, &repo_root)
    );
    Ok(())
}

pub fn verify(options: VerifyOptions) -> Result<()> {
    let cwd_path = PathBuf::from(&options.cwd);
    let repo_root = git_repo_root(&cwd_path).unwrap_or_else(|_| cwd_path.clone());
    let pack_root = resolve_pack_root(&repo_root, options.pack_dir.as_deref());
    let current_dir = pack_root.join("current");
    let manifest_path = current_dir.join("manifest.json");

    // P3: `--suggest-patches` short-circuits verify to emit a structured JSON
    // payload meant for agent consumption. Integrity/freshness checks still
    // feed it via `suggest_patches()` but no human-readable output is printed.
    if options.suggest_patches {
        let base_ref = options.base.as_deref().unwrap_or("origin/main");
        let payload = suggest_patches(base_ref, &cwd_path)?;
        println!("{}", serde_json::to_string_pretty(&payload)?);
        return Ok(());
    }

    // Verify is intentionally lock-free: it only reads. Writes are serialized
    // through seal/rollback's `acquire_lock` (F30). If a future `--watch` mode
    // is added, it should take a shared read lock; single-shot verify does not.

    if !manifest_path.exists() {
        if options.repair {
            return run_repair(&repo_root, &pack_root, options.repair_yes);
        }
        if options.ci {
            let result = json!({
                "integrity": "fail",
                "freshness": "skip",
                "changed_files": [],
                "pack_updated": false,
                "exit_code": 1
            });
            println!("{}", serde_json::to_string_pretty(&result)?);
            std::process::exit(1);
        }
        return Err(anyhow!(
            "[agent-context] verify failed: manifest.json not found at {}",
            manifest_path.display()
        ));
    }

    let manifest_content = match fs::read_to_string(&manifest_path) {
        Ok(s) => s,
        Err(err) => {
            if options.repair {
                eprintln!(
                    "[agent-context] manifest unreadable ({}); attempting repair",
                    err
                );
                return run_repair(&repo_root, &pack_root, options.repair_yes);
            }
            return Err(err).with_context(|| {
                format!("Failed to read manifest at {}", manifest_path.display())
            });
        }
    };
    let manifest: serde_json::Value = match serde_json::from_str(&manifest_content) {
        Ok(m) => m,
        Err(err) => {
            if options.repair {
                eprintln!(
                    "[agent-context] manifest.json is malformed ({}); attempting repair",
                    err
                );
                return run_repair(&repo_root, &pack_root, options.repair_yes);
            }
            return Err(anyhow!("Failed to parse manifest.json: {}", err));
        }
    };

    // P11 / F34: schema_version gate. This is the first check so we refuse
    // fast when the manifest was sealed by a newer chorus than this one.
    match check_schema_version(&manifest) {
        SchemaVersionCheck::Ok { warning } => {
            if let Some(msg) = warning {
                eprintln!("[agent-context] WARN: {msg}");
            }
        }
        SchemaVersionCheck::TooNew { message } => {
            if options.ci {
                let result = json!({
                    "integrity": "fail",
                    "freshness": "skip",
                    "changed_files": [],
                    "pack_updated": false,
                    "exit_code": 1,
                    "schema_error": message,
                });
                println!("{}", serde_json::to_string_pretty(&result)?);
                std::process::exit(1);
            }
            return Err(anyhow!("[agent-context] verify failed: {message}"));
        }
    }

    let files = manifest.get("files").and_then(|f| f.as_array());
    if files.is_none() {
        if options.repair {
            eprintln!(
                "[agent-context] manifest.json missing 'files' array; attempting repair"
            );
            return run_repair(&repo_root, &pack_root, options.repair_yes);
        }
        if options.ci {
            let result = json!({
                "integrity": "fail",
                "freshness": "skip",
                "changed_files": [],
                "pack_updated": false,
                "exit_code": 1
            });
            println!("{}", serde_json::to_string_pretty(&result)?);
            std::process::exit(1);
        }
        return Err(anyhow!("[agent-context] verify failed: manifest has no 'files' array"));
    }

    // F31 TOCTOU mitigation: snapshot every file's bytes at one instant, then
    // hash and compare. If any file changes between snapshot and compare, we
    // re-hash once and warn.
    let files_arr = files.unwrap();
    let mut pass_count = 0usize;
    let mut fail_count = 0usize;

    // P13/F50: read the manifest's `aliases` map (if present). Keys are the
    // canonical filenames recorded in `files[]`; values are the on-disk name
    // the team prefers. When the canonical path is missing, verify retries
    // with the aliased name before flagging the file as missing.
    let aliases = resolve_pack_aliases(&manifest);

    for file_entry in files_arr {
        let file_path_str = file_entry
            .get("path")
            .and_then(|p| p.as_str())
            .unwrap_or("unknown");
        let expected_hash = file_entry
            .get("sha256")
            .and_then(|h| h.as_str())
            .unwrap_or("");
        let (actual_path, alias_used) =
            resolve_file_with_alias(&current_dir, file_path_str, &aliases);

        if !actual_path.exists() {
            if !options.ci {
                eprintln!("  FAIL  {}  (file missing)", file_path_str);
            }
            fail_count += 1;
            continue;
        }
        if let Some(alias_name) = alias_used {
            if !options.ci {
                println!(
                    "  NOTE  {} resolved via alias `{}`",
                    file_path_str, alias_name
                );
            }
        }

        let actual_hash = match hash_file_stable(&actual_path) {
            Ok(h) => h,
            Err(err) => {
                if !options.ci {
                    eprintln!("  FAIL  {}  (read error: {})", file_path_str, err);
                }
                fail_count += 1;
                continue;
            }
        };

        if actual_hash == expected_hash {
            if !options.ci {
                println!("  PASS  {}", file_path_str);
            }
            pass_count += 1;
        } else {
            if !options.ci {
                eprintln!("  FAIL  {}  (checksum mismatch)", file_path_str);
            }
            fail_count += 1;
        }
    }

    // Verify pack_checksum if present
    if let Some(expected_pack_checksum) = manifest.get("pack_checksum").and_then(|c| c.as_str()) {
        let mut file_entries: Vec<String> = Vec::new();
        for f in files_arr {
            let p = f.get("path").and_then(|v| v.as_str()).unwrap_or("unknown");
            let h = f.get("sha256").and_then(|v| v.as_str()).unwrap_or("");
            file_entries.push(format!("{}:{}", p, h));
        }
        let combined = file_entries.join("\n");
        let actual_pack_checksum = sha256_hex(combined.as_bytes());
        if actual_pack_checksum == expected_pack_checksum {
            if !options.ci {
                println!("  PASS  pack_checksum");
            }
            pass_count += 1;
        } else {
            if options.repair {
                eprintln!(
                    "[agent-context] pack_checksum mismatch; attempting repair"
                );
                return run_repair(&repo_root, &pack_root, options.repair_yes);
            }
            if !options.ci {
                eprintln!("  FAIL  pack_checksum (mismatch)");
            }
            fail_count += 1;
        }
    }

    let integrity_passed = fail_count == 0;
    let integrity_status = if integrity_passed { "pass" } else { "fail" };

    // P5 — drift check: re-resolve family_counts from the current repo, then
    // re-scan each pack markdown for prose numeric claims. Any mismatch is a
    // warning (never a hard fail on verify — verify only reports drift). This
    // catches the window between a successful seal and a subsequent repo
    // change that invalidated the sealed counts.
    let drift_counts = resolve_family_counts(&repo_root, &current_dir);
    let (_drift_slugs, drift_noun_counts) = derive_count_maps(&drift_counts);
    let mut drift_mismatches: Vec<NumericClaimMismatch> = Vec::new();
    if !drift_noun_counts.is_empty() {
        if let Ok(entries) = fs::read_dir(&current_dir) {
            let mut md_paths: Vec<PathBuf> = Vec::new();
            for entry in entries.flatten() {
                let path = entry.path();
                if path.is_file()
                    && path.extension().and_then(|ext| ext.to_str()) == Some("md")
                {
                    md_paths.push(path);
                }
            }
            md_paths.sort();
            for path in md_paths {
                let label = path
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("")
                    .to_string();
                let text = match fs::read_to_string(&path) {
                    Ok(t) => t,
                    Err(_) => continue,
                };
                drift_mismatches
                    .extend(extract_numeric_claims(&text, &drift_noun_counts, &label));
            }
        }
    }
    if !drift_mismatches.is_empty() && !options.ci {
        eprintln!(
            "  WARN  count drift: {} prose claim(s) disagree with current repo:",
            drift_mismatches.len()
        );
        for m in &drift_mismatches {
            eprintln!(
                "    - {}:{}: prose says {} {}, repo currently has {}",
                m.file, m.line, m.claimed_count, m.noun, m.authoritative_count
            );
        }
    }

    // Run freshness check
    let base_ref = options.base.as_deref().unwrap_or("origin/main");
    let freshness = if options.ci || options.base.is_some() {
        match check_freshness_inner(base_ref, &cwd_path) {
            Ok(result) => result,
            Err(_) => FreshnessResult {
                status: "skip".to_string(),
                changed_files: Vec::new(),
                pack_updated: false,
                skipped_reason: None,
                affected_sections: Vec::new(),
            },
        }
    } else {
        // When not in CI and no base specified, attempt freshness but treat errors as skip
        match check_freshness_inner(base_ref, &cwd_path) {
            Ok(result) => result,
            Err(_) => FreshnessResult {
                status: "skip".to_string(),
                changed_files: Vec::new(),
                pack_updated: false,
                skipped_reason: None,
                affected_sections: Vec::new(),
            },
        }
    };

    // P2 — structural checks. Non-fatal by default: warnings surface in CI
    // JSON under `structural_warnings[]` and in human output as a summary
    // block, but they do not flip the exit code. This preserves existing
    // verify behavior for already-sealed packs while making drift visible.
    let structural_warnings = run_structural_checks(&manifest, &repo_root, &current_dir);

    // P11-drift / F38 — re-hash shipped helper scripts against the manifest's
    // `tool_hashes` baseline. Produces an empty report for manifests that
    // predate the field, so older packs keep verifying cleanly.
    let tool_integrity = compute_tool_integrity_report(&manifest, &current_dir);

    if options.ci {
        // P7: compute the diff-since-seal payload so CI can surface acceptance
        // tests whose `invalidated_by` function signatures have drifted. When
        // any are present AND the pack wasn't updated to reconcile, CI must
        // fail so the reconciler can re-validate the tests. Errors from the
        // helper degrade to an empty list — we never want CI to fail because
        // the diff computation itself hit a transient condition.
        let diff_result = diff_since_seal(&cwd_path, options.pack_dir.as_deref())
            .unwrap_or_else(|_| DiffSinceSealResult {
                value: json!({
                    "baseline_sha": Value::Null,
                    "pack_updated": false,
                    "zones": Vec::<Value>::new(),
                    "acceptance_tests_invalidated": Vec::<Value>::new(),
                    "recommended_reconciliation_actions": Vec::<String>::new(),
                }),
                acceptance_tests_invalidated: Vec::new(),
            });
        let pack_updated_in_diff = diff_result
            .value
            .get("pack_updated")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);
        let acceptance_gate_failing =
            !diff_result.acceptance_tests_invalidated.is_empty() && !pack_updated_in_diff;

        // P3: zone-map authoring bugs surface as freshness.status == "fail"
        // and must be treated as a hard failure alongside integrity/warn.
        // P7: gate on acceptance_tests_invalidated when the pack is stale.
        // P11-drift / F38: tampered helper scripts are always a hard fail.
        let tool_integrity_failing = !tool_integrity.mismatches.is_empty();
        let exit_code = if !integrity_passed
            || freshness.status == "warn"
            || freshness.status == "fail"
            || acceptance_gate_failing
            || tool_integrity_failing
        {
            1
        } else {
            0
        };
        let mut result_obj = serde_json::Map::new();
        result_obj.insert("integrity".to_string(), json!(integrity_status));
        result_obj.insert("freshness".to_string(), json!(freshness.status));
        result_obj.insert("changed_files".to_string(), json!(freshness.changed_files));
        result_obj.insert("pack_updated".to_string(), json!(freshness.pack_updated));
        if let Some(reason) = &freshness.skipped_reason {
            result_obj.insert("skipped_reason".to_string(), json!(reason));
        }
        // P3: emit affected_sections so downstream consumers (CI, agents) can
        // target the right pack files. Always emit (possibly empty) so schema
        // stays stable.
        result_obj.insert(
            "affected_sections".to_string(),
            json!(freshness.affected_sections),
        );
        // P2: emit structural warnings so CI consumers can surface drift
        // without parsing stderr. Shape: [{kind, message, affected_pack_files}].
        result_obj.insert(
            "structural_warnings".to_string(),
            structural_warnings_as_json(&structural_warnings),
        );
        // P11-drift / F38: emit tool_integrity so CI can surface tampering
        // findings alongside the other gates. Shape is stable even when the
        // manifest predates the field (mismatches[] empty, extra_files[]
        // empty).
        result_obj.insert(
            "tool_integrity".to_string(),
            tool_integrity_as_json(&tool_integrity),
        );
        result_obj.insert("exit_code".to_string(), json!(exit_code));

        // P13/F58: promote the manifest's seal-time HEAD to `last_known_good_sha`
        // when this CI run is fully green. Best-effort: a write failure does
        // not flip the exit code or leak detail — CI already passed.
        if exit_code == 0 {
            let seal_sha = manifest
                .get("head_sha_at_seal")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string())
                .or_else(|| {
                    manifest
                        .get("head_sha")
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_string())
                });
            if let Some(sha) = seal_sha.filter(|s| !s.is_empty()) {
                let _ = update_last_known_good(&manifest_path, &sha);
            }
        }

        println!("{}", serde_json::to_string_pretty(&Value::Object(result_obj))?);
        if exit_code != 0 {
            std::process::exit(exit_code);
        }
        return Ok(());
    }

    // Human-readable output
    let total = pass_count + fail_count;
    println!("\n  Results: {pass_count}/{total} passed");

    if !integrity_passed {
        eprintln!("[agent-context] verify: {} file(s) did not match", fail_count);
    } else {
        println!("  Context pack integrity verified.");
    }

    // Show freshness info in human-readable mode
    match freshness.status.as_str() {
        "pass" => {
            if freshness.changed_files.is_empty() {
                println!("  Freshness: PASS (no context-relevant files changed)");
            } else {
                println!("  Freshness: PASS (agent-context was updated)");
            }
        }
        "warn" => {
            println!(
                "  Freshness: WARNING — {} context-relevant file(s) changed but .agent-context/current/ was not updated:",
                freshness.changed_files.len()
            );
            for f in &freshness.changed_files {
                println!("    - {}", f);
            }
            if !freshness.affected_sections.is_empty() {
                println!("  Affected pack sections:");
                for s in &freshness.affected_sections {
                    println!("    - {}", s);
                }
            }
            println!("  Consider running: chorus agent-context build");
        }
        "fail" => {
            if let Some(reason) = &freshness.skipped_reason {
                eprintln!("  Freshness: FAIL ({})", reason);
            } else {
                eprintln!("  Freshness: FAIL (zone map invalid)");
            }
        }
        "skipped" => {
            if let Some(reason) = &freshness.skipped_reason {
                println!("  Freshness: skipped ({})", reason);
            } else {
                println!("  Freshness: skipped");
            }
        }
        _ => {
            println!("  Freshness: skipped (no git history available)");
        }
    }

    // P2: structural warnings summary (non-fatal). Rendered after freshness
    // so the human reader sees integrity → freshness → structural in the
    // same top-down order as the CI JSON keys.
    if !structural_warnings.is_empty() {
        eprintln!(
            "  Structural: {} warning(s)",
            structural_warnings.len()
        );
        for w in &structural_warnings {
            eprintln!("    [{}] {}", w.kind, w.message);
            if !w.affected_pack_files.is_empty() {
                eprintln!(
                    "        affected: {}",
                    w.affected_pack_files.join(", ")
                );
            }
        }
    }

    // P11-drift / F38 — surface tool-integrity mismatches in the human
    // renderer too, so `verify` without `--ci` still flags tampered helpers.
    // A clean report is implicit (no block printed); any mismatch is a hard
    // fail so callers treat it the same as an integrity checksum miss.
    if !tool_integrity.mismatches.is_empty() {
        eprintln!(
            "  Tool integrity: FAIL ({} file(s) differ from manifest)",
            tool_integrity.mismatches.len()
        );
        for m in &tool_integrity.mismatches {
            eprintln!("    FAIL  tools/{}  ({})", m.file, m.reason);
        }
    }

    // P3: zone-map validation failures are hard fails alongside integrity.
    if freshness.status == "fail" {
        let reason = freshness
            .skipped_reason
            .clone()
            .unwrap_or_else(|| "zone map invalid".to_string());
        return Err(anyhow!("[agent-context] verify failed: {}", reason));
    }

    if !tool_integrity.mismatches.is_empty() {
        return Err(anyhow!(
            "[agent-context] verify failed: {} tool file(s) differ from manifest",
            tool_integrity.mismatches.len()
        ));
    }

    if !integrity_passed {
        Err(anyhow!("[agent-context] verify failed: {} file(s) did not match", fail_count))
    } else {
        Ok(())
    }
}

/// Hash a file, re-hashing once if the bytes change mid-read (F31 TOCTOU mitigation).
///
/// Reads the file bytes twice with a quick second attempt. If the hash is
/// stable on both reads we return it; otherwise we emit a warning and return
/// the second (later) hash so the verify comparison reflects the most recent
/// observable state.
fn hash_file_stable(path: &Path) -> Result<String> {
    let first = fs::read(path)
        .with_context(|| format!("Failed to read {}", path.display()))?;
    let first_hash = sha256_hex(&first);
    // Fast re-read to detect a racing writer. We read at most twice.
    let second = match fs::read(path) {
        Ok(b) => b,
        Err(_) => return Ok(first_hash),
    };
    let second_hash = sha256_hex(&second);
    if first_hash == second_hash {
        Ok(first_hash)
    } else {
        eprintln!(
            "[agent-context] WARN: {} changed during verify; using re-hashed value",
            path.display()
        );
        Ok(second_hash)
    }
}

/// Restore `current/` from the most recent intact snapshot (F32).
///
/// Scans `.agent-context/snapshots/` for directories containing a parseable
/// `manifest.json`. The most recent (lexicographic) one wins. Without `--yes`
/// the plan is printed and stdin confirmation is required; exit code 2 means
/// the user declined.
fn run_repair(repo_root: &Path, pack_root: &Path, yes: bool) -> Result<()> {
    let current_dir = pack_root.join("current");
    let snapshots_dir = pack_root.join("snapshots");

    if !snapshots_dir.exists() {
        return Err(anyhow!(
            "[agent-context] repair failed: no snapshots directory at {} (no recovery snapshot found)",
            rel_path(&snapshots_dir, repo_root)
        ));
    }

    let mut candidates: Vec<String> = fs::read_dir(&snapshots_dir)
        .with_context(|| format!("Failed to list snapshots at {}", snapshots_dir.display()))?
        .filter_map(|entry| entry.ok())
        .filter(|entry| entry.path().is_dir())
        .filter_map(|entry| entry.file_name().to_str().map(|s| s.to_string()))
        .collect();
    candidates.sort();
    candidates.reverse();

    let mut selected: Option<String> = None;
    for candidate in &candidates {
        let manifest = snapshots_dir.join(candidate).join("manifest.json");
        if manifest.exists() {
            if let Ok(raw) = fs::read_to_string(&manifest) {
                if serde_json::from_str::<Value>(&raw).is_ok() {
                    selected = Some(candidate.clone());
                    break;
                }
            }
        }
    }

    let snapshot_id = selected.ok_or_else(|| {
        anyhow!(
            "[agent-context] repair failed: no intact snapshot found in {} (no recovery snapshot found)",
            rel_path(&snapshots_dir, repo_root)
        )
    })?;
    let source_dir = snapshots_dir.join(&snapshot_id);

    println!(
        "[agent-context] repair plan: restore {} -> {}",
        rel_path(&source_dir, repo_root),
        rel_path(&current_dir, repo_root)
    );
    println!("  - clears current contents of the pack directory");
    println!(
        "  - copies files from snapshot {} into place (manifest.json included)",
        snapshot_id
    );

    if !yes {
        eprint!("Proceed with repair? [y/N] ");
        std::io::stderr().flush().ok();
        let mut buf = String::new();
        if std::io::stdin().read_line(&mut buf).is_err() {
            return Err(anyhow!(
                "[agent-context] repair aborted: could not read confirmation"
            ));
        }
        let answer = buf.trim().to_lowercase();
        if answer != "y" && answer != "yes" {
            eprintln!("[agent-context] repair declined by user");
            std::process::exit(2);
        }
    }

    // Serialize with seal/rollback (F29) so a concurrent seal can't race us.
    let lock_path = pack_root.join("seal.lock");
    let _lock = acquire_lock(&lock_path)?;

    if current_dir.exists() {
        fs::remove_dir_all(&current_dir)
            .with_context(|| format!("Failed to clear {}", current_dir.display()))?;
    }
    ensure_dir(&current_dir)?;
    copy_dir_recursive(&source_dir, &current_dir)?;

    println!(
        "[agent-context] repair completed: restored snapshot {} to {}",
        snapshot_id,
        rel_path(&current_dir, repo_root)
    );
    Ok(())
}

pub fn check_freshness(base: &str, cwd: &str) -> Result<()> {
    let cwd_path = PathBuf::from(cwd);
    let result = check_freshness_inner(base, &cwd_path)?;

    // P6: record the warning so a later pack-only push can recognize it.
    if result.status == "warn" {
        let repo_root = git_repo_root(&cwd_path).unwrap_or_else(|_| cwd_path.clone());
        write_last_freshness_state(&repo_root, &result);
    }

    match result.status.as_str() {
        "pass" => {
            if result.changed_files.is_empty() {
                println!("PASS agent-context-freshness (no context-relevant files changed)");
            } else {
                println!("PASS agent-context-freshness (agent-context was updated)");
            }
        }
        "warn" => {
            println!(
                "WARNING: {} context-relevant file(s) changed but .agent-context/current/ was not updated:",
                result.changed_files.len()
            );
            for file_path in &result.changed_files {
                println!("  - {}", file_path);
            }
            // P3: surface affected pack sections so agents know which files to patch.
            if !result.affected_sections.is_empty() {
                println!();
                println!("Affected pack sections:");
                for s in &result.affected_sections {
                    println!("  - {}", s);
                }
            }
            println!();
            println!("Consider running: chorus agent-context build");
        }
        "fail" => {
            let reason = result
                .skipped_reason
                .clone()
                .unwrap_or_else(|| "zone map invalid".to_string());
            return Err(anyhow!(
                "[agent-context] freshness failed: {}",
                reason
            ));
        }
        "skipped" => {
            if let Some(reason) = &result.skipped_reason {
                println!("SKIPPED agent-context-freshness ({reason})");
            } else {
                println!("SKIPPED agent-context-freshness");
            }
        }
        _ => {}
    }

    Ok(())
}

fn git_repo_root(cwd: &Path) -> Result<PathBuf> {
    let root = run_git(&["rev-parse", "--show-toplevel"], cwd, true)?;
    if root.trim().is_empty() {
        Ok(cwd.to_path_buf())
    } else {
        Ok(PathBuf::from(root.trim()))
    }
}

fn run_git(args: &[&str], cwd: &Path, allow_failure: bool) -> Result<String> {
    let output = Command::new("git")
        .args(args)
        .current_dir(cwd)
        .output()
        .with_context(|| format!("Failed to run git {}", args.join(" ")))?;

    if output.status.success() {
        Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
    } else if allow_failure {
        Ok(String::new())
    } else {
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
        let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
        Err(anyhow!(
            "git {} failed: {}{}{}",
            args.join(" "),
            stderr,
            if !stderr.is_empty() && !stdout.is_empty() { "\n" } else { "" },
            stdout
        ))
    }
}

fn resolve_pack_root(repo_root: &Path, pack_dir: Option<&str>) -> PathBuf {
    let dir = pack_dir
        .map(|value| value.to_string())
        .or_else(|| env::var("CHORUS_CONTEXT_PACK_DIR").ok())
        .or_else(|| env::var("BRIDGE_CONTEXT_PACK_DIR").ok())
        .unwrap_or_else(|| ".agent-context".to_string());
    let dir_path = PathBuf::from(dir);
    if dir_path.is_absolute() {
        dir_path
    } else {
        repo_root.join(dir_path)
    }
}

/// P13/F50: read the manifest's `aliases` object (canonical -> alias)
/// into a `BTreeMap` for deterministic iteration. Missing field, non-object,
/// or non-string values degrade to an empty map so unmodified manifests
/// continue to verify unchanged.
fn resolve_pack_aliases(manifest: &Value) -> std::collections::BTreeMap<String, String> {
    let mut out = std::collections::BTreeMap::new();
    if let Some(obj) = manifest.get("aliases").and_then(|v| v.as_object()) {
        for (canonical, alias_val) in obj {
            if let Some(alias_str) = alias_val.as_str() {
                if !alias_str.is_empty() {
                    out.insert(canonical.to_string(), alias_str.to_string());
                }
            }
        }
    }
    out
}

/// P13/F50: resolve a canonical pack-file name against the on-disk layout,
/// falling back to the aliased name when the canonical path is missing.
/// Returns the resolved path and — when an alias was consulted — the alias
/// name so callers can surface it in human output.
fn resolve_file_with_alias(
    current_dir: &Path,
    canonical: &str,
    aliases: &std::collections::BTreeMap<String, String>,
) -> (PathBuf, Option<String>) {
    let direct = current_dir.join(canonical);
    if direct.exists() {
        return (direct, None);
    }
    if let Some(alias) = aliases.get(canonical) {
        let aliased = current_dir.join(alias);
        if aliased.exists() {
            return (aliased, Some(alias.clone()));
        }
    }
    // Fall back to the direct path so downstream "does not exist" branches
    // still see the canonical name in error messages.
    (direct, None)
}

fn ensure_dir(path: &Path) -> Result<()> {
    fs::create_dir_all(path)
        .with_context(|| format!("Failed to create directory {}", path.display()))?;
    Ok(())
}

fn write_text(path: &Path, text: &str) -> Result<()> {
    if let Some(parent) = path.parent() {
        ensure_dir(parent)?;
    }
    fs::write(path, text).with_context(|| format!("Failed to write {}", path.display()))?;
    Ok(())
}

/// Atomically write `bytes` to `path`.
///
/// Writes to a sibling `*.tmp` file, fsyncs the contents, then renames into place.
/// On POSIX the rename is atomic, so either the old file is fully intact or the
/// new contents are visible — a partial write is never observable (F33).
fn atomic_write(path: &Path, bytes: &[u8]) -> Result<()> {
    let parent = path
        .parent()
        .ok_or_else(|| anyhow!("Missing parent for {}", path.display()))?;
    ensure_dir(parent)?;
    let tmp_name = format!(
        ".{}.tmp.{}",
        path.file_name()
            .and_then(|s| s.to_str())
            .unwrap_or("context-pack.tmp"),
        std::process::id()
    );
    let tmp = parent.join(tmp_name);
    {
        let mut file = fs::OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(&tmp)
            .with_context(|| format!("Failed to open {}", tmp.display()))?;
        file.write_all(bytes)
            .with_context(|| format!("Failed to write {}", tmp.display()))?;
        file.sync_all()
            .with_context(|| format!("Failed to fsync {}", tmp.display()))?;
    }
    fs::rename(&tmp, path)
        .with_context(|| format!("Failed to move {} -> {}", tmp.display(), path.display()))?;
    Ok(())
}

fn write_text_atomic(path: &Path, text: &str) -> Result<()> {
    atomic_write(path, text.as_bytes())
}

/// Reserved: content generator for auto-fill build mode.
/// Will be wired when `build --auto-fill` is implemented.
#[allow(dead_code)]
fn read_package_version(path: &Path) -> Option<String> {
    let raw = fs::read_to_string(path).ok()?;
    let value: Value = serde_json::from_str(&raw).ok()?;
    value.get("version").and_then(|v| v.as_str()).map(|v| v.to_string())
}

/// Reserved: content generator for auto-fill build mode.
/// Will be wired when `build --auto-fill` is implemented.
#[allow(dead_code)]
fn parse_cargo_version(raw: &str) -> Option<String> {
    for line in raw.lines() {
        let trimmed = line.trim();
        if let Some(rest) = trimmed.strip_prefix("version") {
            let value = rest.trim();
            if let Some(eq_rest) = value.strip_prefix('=') {
                let candidate = eq_rest.trim().trim_matches('"').to_string();
                if !candidate.is_empty() {
                    return Some(candidate);
                }
            }
        }
    }
    None
}

fn compute_changed_files(repo_root: &Path, base: Option<&str>, head: &str) -> Result<Vec<String>> {
    if head.trim().is_empty() {
        return Ok(Vec::new());
    }

    let output = if base.map(|value| value.trim().is_empty() || is_zero_sha(value)).unwrap_or(true) {
        run_git(&["show", "--pretty=format:", "--name-only", head], repo_root, true)?
    } else {
        let range = format!("{}..{}", base.unwrap_or(""), head);
        run_git(&["diff", "--name-only", &range], repo_root, true)?
    };

    Ok(output
        .lines()
        .map(|line| line.trim().replace('\\', "/"))
        .filter(|line| !line.is_empty())
        .collect())
}

/// Reserved: content generator for auto-fill build mode.
/// Will be wired when `build --auto-fill` is implemented.
#[allow(dead_code)]
fn normalize_changed_files(files: &[String]) -> Vec<String> {
    let mut set = BTreeSet::new();
    for file in files {
        let normalized = file.trim().replace('\\', "/");
        if !normalized.is_empty() {
            set.insert(normalized);
        }
    }
    set.into_iter().collect()
}

/// Reserved: content generator for auto-fill build mode.
/// Will be wired when `build --auto-fill` is implemented.
#[allow(dead_code)]
fn summarize_path_counts(paths: &[String]) -> Vec<(String, usize)> {
    let mut buckets = vec![
        ("scripts/".to_string(), "scripts".to_string(), 0usize),
        ("cli/src/".to_string(), "cli/src".to_string(), 0usize),
        ("schemas/".to_string(), "schemas".to_string(), 0usize),
        ("fixtures/".to_string(), "fixtures".to_string(), 0usize),
        (".github/workflows/".to_string(), ".github/workflows".to_string(), 0usize),
        ("docs/".to_string(), "docs".to_string(), 0usize),
    ];

    for file in paths {
        for (prefix, _name, count) in &mut buckets {
            if file.starts_with(prefix.as_str()) {
                *count += 1;
                break;
            }
        }
    }

    buckets
        .into_iter()
        .filter(|(_, _, count)| *count > 0)
        .map(|(_, name, count)| (name, count))
        .collect()
}

/// Failure modes for [`read_file_for_pack`] that callers can downgrade to
/// warnings (skip file) instead of aborting seal. See P8 / F19, F20, F23.
#[derive(Debug)]
enum PackReadError {
    /// File contains NUL bytes in the first [`BINARY_SNIFF_BYTES`] bytes — we
    /// treat it as a binary blob and refuse to hash it (F19).
    LikelyBinary(PathBuf),
    /// File larger than [`MAX_PACK_FILE_BYTES`] — refuse to read to avoid
    /// OOM / slow hashing on pack-adjacent logs or assets (F23).
    TooLarge(PathBuf, u64),
    /// File is a symlink whose canonical target escapes the repo root (F20).
    /// Only raised when `follow_symlinks` is false.
    SymlinkEscape(PathBuf, PathBuf),
    /// Anything else filesystem-level (missing file, permissions).
    IoError(PathBuf, std::io::Error),
}

impl std::fmt::Display for PackReadError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PackReadError::LikelyBinary(p) => {
                write!(f, "binary content (NUL bytes detected) at {}", p.display())
            }
            PackReadError::TooLarge(p, n) => {
                write!(f, "file too large ({} bytes, limit {}) at {}", n, MAX_PACK_FILE_BYTES, p.display())
            }
            PackReadError::SymlinkEscape(p, target) => {
                write!(f, "symlink {} escapes repo root (target: {})", p.display(), target.display())
            }
            PackReadError::IoError(p, e) => {
                write!(f, "io error reading {}: {}", p.display(), e)
            }
        }
    }
}

/// P8 — read a file that is destined for the pack (hashed into the manifest)
/// in a way that cannot panic on hostile input.
///
/// Behaviour:
/// - Refuses files whose canonical target is outside `repo_root`, unless
///   `follow_symlinks` is true (F20).
/// - Refuses files whose size exceeds [`MAX_PACK_FILE_BYTES`] (F23).
/// - Refuses files whose first [`BINARY_SNIFF_BYTES`] bytes contain NUL (F19).
/// - Otherwise reads as bytes and decodes with [`String::from_utf8_lossy`],
///   replacing invalid sequences with U+FFFD (F19 — never panic).
fn read_file_for_pack(
    path: &Path,
    repo_root: &Path,
    follow_symlinks: bool,
) -> std::result::Result<String, PackReadError> {
    // F20 — check symlink status before committing to the read.
    match fs::symlink_metadata(path) {
        Ok(meta) if meta.file_type().is_symlink() => {
            let target = match fs::canonicalize(path) {
                Ok(t) => t,
                Err(e) => return Err(PackReadError::IoError(path.to_path_buf(), e)),
            };
            let root_canonical = fs::canonicalize(repo_root).unwrap_or_else(|_| repo_root.to_path_buf());
            if !target.starts_with(&root_canonical) && !follow_symlinks {
                return Err(PackReadError::SymlinkEscape(path.to_path_buf(), target));
            }
        }
        Ok(_) => {}
        Err(e) => return Err(PackReadError::IoError(path.to_path_buf(), e)),
    }

    // F23 — size guard.
    let metadata = fs::metadata(path)
        .map_err(|e| PackReadError::IoError(path.to_path_buf(), e))?;
    if metadata.len() > MAX_PACK_FILE_BYTES {
        return Err(PackReadError::TooLarge(path.to_path_buf(), metadata.len()));
    }

    // F19 — read as bytes, detect binary, decode lossily.
    let bytes = fs::read(path)
        .map_err(|e| PackReadError::IoError(path.to_path_buf(), e))?;
    let sniff_len = bytes.len().min(BINARY_SNIFF_BYTES);
    if bytes[..sniff_len].contains(&0u8) {
        return Err(PackReadError::LikelyBinary(path.to_path_buf()));
    }
    Ok(String::from_utf8_lossy(&bytes).into_owned())
}

/// Check whether `target` resolves inside `repo_root`. Used by symlink policy
/// and glob-pattern sanitization. Best-effort canonicalization — if either
/// path cannot be canonicalized (e.g. the target does not exist yet), we
/// fall back to the as-given value.
#[allow(dead_code)]
fn is_within_repo_root(repo_root: &Path, target: &Path) -> bool {
    let root = fs::canonicalize(repo_root).unwrap_or_else(|_| repo_root.to_path_buf());
    let canonical = fs::canonicalize(target).unwrap_or_else(|_| target.to_path_buf());
    canonical.starts_with(&root)
}

/// P8/F22 — reject glob patterns that could resolve outside the repo root.
///
/// Conservative rules:
/// - Absolute paths (`/foo/**`, `C:\…`) are rejected.
/// - Patterns containing `..` are rejected; callers should not need to walk
///   out of the repo to describe pack scope.
/// - Other patterns are passed through — the glob library is responsible for
///   matching safety at resolution time.
fn validate_pack_glob(pattern: &str, _repo_root: &Path) -> Result<()> {
    let normalized = pattern.replace('\\', "/");
    if normalized.is_empty() {
        return Err(anyhow!(
            "[context-pack] invalid glob pattern: empty string"
        ));
    }
    if normalized.starts_with('/') {
        return Err(anyhow!(
            "[context-pack] invalid glob pattern {:?}: absolute paths are not allowed; use a repo-relative pattern",
            pattern
        ));
    }
    // Windows absolute path heuristic: "C:" style drive letter.
    let bytes = normalized.as_bytes();
    if bytes.len() >= 2 && bytes[1] == b':' && (bytes[0] as char).is_ascii_alphabetic() {
        return Err(anyhow!(
            "[context-pack] invalid glob pattern {:?}: absolute paths are not allowed; use a repo-relative pattern",
            pattern
        ));
    }
    // Reject any `..` segment — either leading or embedded, the pattern
    // could escape the repo root once resolved.
    for segment in normalized.split('/') {
        if segment == ".." {
            return Err(anyhow!(
                "[context-pack] invalid glob pattern {:?}: `..` path traversal is not allowed",
                pattern
            ));
        }
    }
    Ok(())
}

fn collect_files_meta(
    current_dir: &Path,
    repo_root: &Path,
    relative_paths: &[String],
    follow_symlinks: bool,
) -> Result<Vec<FileMeta>> {
    let mut out = Vec::new();
    for relative_path in relative_paths {
        let absolute_path = current_dir.join(relative_path);
        let content = match read_file_for_pack(&absolute_path, repo_root, follow_symlinks) {
            Ok(c) => c,
            Err(err) => {
                // P8 — skip hostile files with a clear warning rather than
                // panicking the entire seal.
                eprintln!("[context-pack] WARN: skipping pack file: {}", err);
                continue;
            }
        };
        let metadata = fs::metadata(&absolute_path)
            .with_context(|| format!("Failed to stat {}", absolute_path.display()))?;
        out.push(FileMeta {
            path: relative_path.clone(),
            path_lower: relative_path.to_lowercase(),
            sha256: sha256_hex(content.as_bytes()),
            bytes: metadata.len(),
            words: content.split_whitespace().count(),
        });
    }
    Ok(out)
}

// ---- P1: semantic baseline helpers ----------------------------------------
//
// Each helper is self-contained and degrades to empty on absent/malformed
// input so `collect_semantic_baseline` can compose them without branching.

/// P1 — resolve globs from `completeness_contract.json` `required_file_families[]`
/// and `reporting_rules.json` `groupable_families[]`. Keyed by the raw glob;
/// value is the number of repo-relative files currently matching.
///
/// Returns an empty map if both config files are absent or do not define
/// `task_families`. Malformed JSON is treated as absent.
fn resolve_family_counts(
    repo_root: &Path,
    current_dir: &Path,
) -> std::collections::BTreeMap<String, usize> {
    let mut patterns: BTreeSet<String> = BTreeSet::new();

    let completeness_path = current_dir.join("completeness_contract.json");
    if let Ok(Some(completeness)) = read_json(&completeness_path) {
        if let Some(families) = completeness
            .get("task_families")
            .and_then(|v| v.as_object())
        {
            for (_task, entry) in families {
                if let Some(list) = entry
                    .get("required_file_families")
                    .and_then(|v| v.as_array())
                {
                    for item in list {
                        if let Some(p) = item.as_str() {
                            patterns.insert(p.to_string());
                        }
                    }
                }
            }
        }
    }

    let reporting_path = current_dir.join("reporting_rules.json");
    if let Ok(Some(reporting)) = read_json(&reporting_path) {
        if let Some(families) = reporting
            .get("task_families")
            .and_then(|v| v.as_object())
        {
            for (_task, entry) in families {
                if let Some(list) = entry.get("groupable_families").and_then(|v| v.as_array()) {
                    for item in list {
                        if let Some(p) = item.as_str() {
                            patterns.insert(p.to_string());
                        }
                    }
                }
            }
        }
    }

    let mut out = std::collections::BTreeMap::new();
    for pattern in patterns {
        let count = resolve_pattern_matches(repo_root, &pattern)
            .map(|v| v.len())
            .unwrap_or(0);
        out.insert(pattern, count);
    }
    out
}

/// P1 — noun+count pattern for prose claim extraction. Matches occurrences like
/// "6 files", "12 scripts", "study doc" (singular/plural). Kept narrow to avoid
/// false positives on unrelated numeric text.
///
/// The pattern is intentionally lightweight — a single pass returns
/// `(count_str, noun_str)` pairs per line which callers normalize.
fn prose_claim_nouns() -> &'static [&'static str] {
    &[
        "study doc",
        "study docs",
        "script",
        "scripts",
        "test",
        "tests",
        "file",
        "files",
        "API symbol",
        "API symbols",
        "brand",
        "brands",
    ]
}

/// Parse a single markdown file body into `{noun, count, file, line}` Value
/// entries using the prose-claim regex. Lines inside a
/// `<!-- count-claim: ignore -->` region (toggled by any occurrence of the
/// literal comment; region persists until a matching `<!-- count-claim: end -->`
/// or EOF) are skipped to give authors an explicit opt-out.
fn extract_declared_counts_from_text(text: &str, file_label: &str, out: &mut Vec<Value>) {
    let nouns = prose_claim_nouns();
    let mut ignore = false;
    for (idx, raw_line) in text.lines().enumerate() {
        // Region toggles — any explicit end closes the region; otherwise a
        // stray `count-claim: ignore` marker opens one.
        if raw_line.contains("<!-- count-claim: end -->")
            || raw_line.contains("<!-- count-claim: /ignore -->")
        {
            ignore = false;
            continue;
        }
        if raw_line.contains("<!-- count-claim: ignore -->") {
            ignore = true;
            // If the ignore marker is inline with content, still skip this
            // line so callers can annotate a specific sentence without
            // opening a multi-line region.
            continue;
        }
        if ignore {
            continue;
        }

        // Walk the line and find `<number> <noun>` occurrences. We match the
        // longest noun first (e.g. "study docs" before "study doc") to avoid
        // double counting plural forms.
        let mut cursor = 0;
        let bytes = raw_line.as_bytes();
        while cursor < bytes.len() {
            if !bytes[cursor].is_ascii_digit() {
                cursor += 1;
                continue;
            }
            // Capture the leading integer.
            let start = cursor;
            while cursor < bytes.len() && bytes[cursor].is_ascii_digit() {
                cursor += 1;
            }
            let number_str = &raw_line[start..cursor];
            // Must be followed by exactly one whitespace then a recognized
            // noun phrase. Allow tab/space.
            let after_digits = cursor;
            if after_digits >= bytes.len()
                || !(bytes[after_digits] == b' ' || bytes[after_digits] == b'\t')
            {
                continue;
            }
            let rest = &raw_line[after_digits + 1..];
            let mut matched: Option<&'static str> = None;
            // Try longest noun first by iterating sorted-desc by length.
            let mut ordered: Vec<&&str> = nouns.iter().collect();
            ordered.sort_by_key(|s| std::cmp::Reverse(s.len()));
            for noun in ordered {
                if rest.len() < noun.len() {
                    continue;
                }
                if !rest.is_char_boundary(noun.len()) {
                    continue;
                }
                let candidate = &rest[..noun.len()];
                if candidate.eq_ignore_ascii_case(noun) {
                    // Require a word boundary after the noun (end of line,
                    // whitespace, or punctuation) so "scripts" doesn't match
                    // inside "scriptsdirectory".
                    let next = rest.as_bytes().get(noun.len()).copied();
                    let is_boundary = match next {
                        None => true,
                        Some(b) => !(b.is_ascii_alphanumeric() || b == b'_'),
                    };
                    if is_boundary {
                        matched = Some(noun);
                        break;
                    }
                }
            }
            if let Some(noun) = matched {
                if let Ok(count) = number_str.parse::<u64>() {
                    out.push(json!({
                        "noun": noun,
                        "count": count,
                        "file": file_label,
                        "line": (idx + 1) as u64,
                    }));
                }
                // Advance cursor past the matched noun to avoid re-scanning.
                cursor = after_digits + 1 + noun.len();
            }
        }
    }
}

/// P1 — scan every `.agent-context/current/*.md` file for numeric prose
/// claims. Binary / unreadable files are skipped silently (P8 boundary).
fn extract_declared_counts(current_dir: &Path) -> Vec<Value> {
    let mut out: Vec<Value> = Vec::new();
    let entries = match fs::read_dir(current_dir) {
        Ok(e) => e,
        Err(_) => return out,
    };
    let mut md_paths: Vec<PathBuf> = Vec::new();
    for entry in entries.flatten() {
        let path = entry.path();
        if !path.is_file() {
            continue;
        }
        if path.extension().and_then(|ext| ext.to_str()) != Some("md") {
            continue;
        }
        md_paths.push(path);
    }
    // Deterministic order so identical repos produce identical manifests.
    md_paths.sort();
    for path in md_paths {
        let file_label = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("")
            .to_string();
        let text = match fs::read_to_string(&path) {
            Ok(t) => t,
            Err(_) => continue,
        };
        extract_declared_counts_from_text(&text, &file_label, &mut out);
    }
    out
}

// ---- P5: count SSOT via seal-time template expansion ----------------------
//
// Prose becomes a template; manifest's `family_counts` becomes authoritative.
// Seal expands handlebars like `{{counts.scripts_run}}` before hashing, then
// scans remaining prose for stale numeric claims. Verify re-computes counts
// and re-scans to catch drift between seal and verify.

/// P5 — single occurrence of a stale prose numeric claim produced by
/// `extract_numeric_claims`. The caller formats these into user-facing errors
/// (on seal) or warnings (on verify / `--force`).
#[derive(Debug, Clone, PartialEq, Eq)]
struct NumericClaimMismatch {
    file: String,
    line: u64,
    claimed_count: u64,
    authoritative_count: u64,
    noun: String,
}

/// P5 — normalize a glob pattern into a handlebar slug. Rules:
/// - strip glob wildcards (`*`, `**`, `?`)
/// - replace non-word chars (including `/`, `.`, `-`) with `_`
/// - collapse repeated `_` and trim leading/trailing `_`
///
/// Examples:
/// - `scripts/run_*.py`  → `scripts_run`
/// - `src/brand_lift/*.py` → `src_brand_lift`
/// - `tests/**/*.py` → `tests`
fn slug_for_count_key(pattern: &str) -> String {
    let mut out = String::with_capacity(pattern.len());
    for ch in pattern.chars() {
        if ch == '*' || ch == '?' {
            // Strip glob wildcards entirely — they are not part of the
            // stable slug identifier.
            continue;
        }
        if ch.is_ascii_alphanumeric() || ch == '_' {
            out.push(ch);
        } else {
            // Any other char (`/`, `.`, `-`, whitespace, ...) becomes `_`.
            out.push('_');
        }
    }
    // Collapse runs of `_` and trim edges.
    let mut collapsed = String::with_capacity(out.len());
    let mut prev_underscore = true;
    for ch in out.chars() {
        if ch == '_' {
            if !prev_underscore {
                collapsed.push('_');
            }
            prev_underscore = true;
        } else {
            collapsed.push(ch);
            prev_underscore = false;
        }
    }
    let trimmed = collapsed.trim_matches('_').to_string();
    trimmed
}

/// P5 — expand `{{counts.<slug>}}` handlebars in `content` using the supplied
/// authoritative map. Unknown slugs are left as literal text so an authoring
/// mistake is visible to the numeric-claim scan (the literal handlebar still
/// won't match the prose regex, so this is purely informational).
///
/// Whitespace inside the braces is tolerated: `{{ counts.scripts_run }}` is
/// treated the same as `{{counts.scripts_run}}`.
fn expand_count_handlebars(
    content: &str,
    counts: &std::collections::HashMap<String, usize>,
) -> String {
    let bytes = content.as_bytes();
    let mut out = String::with_capacity(content.len());
    let mut i = 0;
    while i < bytes.len() {
        // Fast path: look for the `{{` opener. Anything else is copied
        // verbatim.
        if i + 1 < bytes.len() && bytes[i] == b'{' && bytes[i + 1] == b'{' {
            // Find the matching `}}`.
            if let Some(close) = content[i + 2..].find("}}") {
                let inner = &content[i + 2..i + 2 + close];
                let trimmed = inner.trim();
                if let Some(slug) = trimmed.strip_prefix("counts.") {
                    let slug = slug.trim();
                    if let Some(value) = counts.get(slug) {
                        out.push_str(&value.to_string());
                        i = i + 2 + close + 2;
                        continue;
                    }
                }
                // Not a recognized counts handlebar — leave the literal
                // text as-is so authors see the typo.
                out.push_str(&content[i..i + 2 + close + 2]);
                i = i + 2 + close + 2;
                continue;
            }
        }
        // Push the current char (UTF-8 safe via char_indices).
        let ch_start = i;
        let ch_end = (1..=4)
            .map(|n| ch_start + n)
            .find(|&end| content.is_char_boundary(end))
            .unwrap_or(ch_start + 1);
        out.push_str(&content[ch_start..ch_end]);
        i = ch_end;
    }
    out
}

/// P5 — given already-expanded markdown content and an authoritative
/// noun→count map, return every prose `<n> <noun>` claim whose value
/// disagrees with the authoritative count.
///
/// Contract:
/// - Skips lines inside `<!-- count-claim: ignore -->` .. `<!-- count-claim: end -->`
///   regions (identical semantics to P1's `extract_declared_counts`).
/// - If a noun has no entry in `authoritative`, the claim is left alone — we
///   only flag values that can be checked against a known-good answer.
/// - Claims whose value matches authoritative produce no output.
/// - The `file` label is supplied by the caller since this helper operates on
///   a single file body.
fn extract_numeric_claims(
    content: &str,
    authoritative: &std::collections::HashMap<String, usize>,
    file_label: &str,
) -> Vec<NumericClaimMismatch> {
    let nouns = prose_claim_nouns();
    let mut out: Vec<NumericClaimMismatch> = Vec::new();
    let mut ignore = false;
    for (idx, raw_line) in content.lines().enumerate() {
        if raw_line.contains("<!-- count-claim: end -->")
            || raw_line.contains("<!-- count-claim: /ignore -->")
        {
            ignore = false;
            continue;
        }
        if raw_line.contains("<!-- count-claim: ignore -->") {
            ignore = true;
            continue;
        }
        if ignore {
            continue;
        }

        // Same single-pass number+noun scan as extract_declared_counts_from_text.
        let mut cursor = 0;
        let bytes = raw_line.as_bytes();
        while cursor < bytes.len() {
            if !bytes[cursor].is_ascii_digit() {
                cursor += 1;
                continue;
            }
            let start = cursor;
            while cursor < bytes.len() && bytes[cursor].is_ascii_digit() {
                cursor += 1;
            }
            let number_str = &raw_line[start..cursor];
            let after_digits = cursor;
            if after_digits >= bytes.len()
                || !(bytes[after_digits] == b' ' || bytes[after_digits] == b'\t')
            {
                continue;
            }
            let rest = &raw_line[after_digits + 1..];
            let mut matched: Option<&'static str> = None;
            let mut ordered: Vec<&&str> = nouns.iter().collect();
            ordered.sort_by_key(|s| std::cmp::Reverse(s.len()));
            for noun in ordered {
                if rest.len() < noun.len() {
                    continue;
                }
                if !rest.is_char_boundary(noun.len()) {
                    continue;
                }
                let candidate = &rest[..noun.len()];
                if candidate.eq_ignore_ascii_case(noun) {
                    let next = rest.as_bytes().get(noun.len()).copied();
                    let is_boundary = match next {
                        None => true,
                        Some(b) => !(b.is_ascii_alphanumeric() || b == b'_'),
                    };
                    if is_boundary {
                        matched = Some(noun);
                        break;
                    }
                }
            }
            if let Some(noun) = matched {
                if let Ok(claimed) = number_str.parse::<u64>() {
                    // Look up authoritative: exact noun first, then
                    // singular form (e.g. "scripts" → "script"), then the
                    // plural form if the claim uses the singular.
                    let singular = noun.trim_end_matches('s');
                    let plural = format!("{}s", noun);
                    let auth = authoritative
                        .get(noun)
                        .or_else(|| authoritative.get(singular))
                        .or_else(|| authoritative.get(plural.as_str()))
                        .copied();
                    if let Some(authoritative_count) = auth {
                        let authoritative_count = authoritative_count as u64;
                        if claimed != authoritative_count {
                            out.push(NumericClaimMismatch {
                                file: file_label.to_string(),
                                line: (idx + 1) as u64,
                                claimed_count: claimed,
                                authoritative_count,
                                noun: noun.to_string(),
                            });
                        }
                    }
                }
                cursor = after_digits + 1 + noun.len();
            }
        }
    }
    out
}

/// P5 — build the two derived maps (by slug for handlebar expansion; by noun
/// for prose-claim authoritative lookup) from P1's family_counts.
///
/// Slug map: every glob key becomes `slug_for_count_key(glob) → count`.
///
/// Noun map: for each registered prose noun, we accumulate the sum of all
/// family_counts whose slug contains the noun as a word token (split on `_`).
/// If no slug matches, the noun is simply absent — the prose scan then leaves
/// any `<n> <noun>` claim alone. This keeps the check conservative: we only
/// flag prose numbers we can cross-check with confidence.
fn derive_count_maps(
    family_counts: &std::collections::BTreeMap<String, usize>,
) -> (
    std::collections::HashMap<String, usize>,
    std::collections::HashMap<String, usize>,
) {
    let mut slug_map: std::collections::HashMap<String, usize> =
        std::collections::HashMap::new();
    for (glob, count) in family_counts {
        let slug = slug_for_count_key(glob);
        if !slug.is_empty() {
            // Sum duplicates rather than silently overwrite — two globs that
            // resolve to the same slug (e.g. `a/run_*.py` + `a/run_*.rs`)
            // should aggregate, not mask each other.
            *slug_map.entry(slug).or_insert(0) += *count;
        }
    }

    let mut noun_map: std::collections::HashMap<String, usize> =
        std::collections::HashMap::new();
    for noun in prose_claim_nouns() {
        // Split the noun on whitespace so "study docs" matches against the
        // slug tokens `study` or `docs`.
        let noun_lower = noun.to_ascii_lowercase();
        let noun_parts: Vec<&str> = noun_lower.split_whitespace().collect();
        let mut accum = 0usize;
        let mut matched_any = false;
        for (glob, count) in family_counts {
            let slug = slug_for_count_key(glob).to_ascii_lowercase();
            let tokens: Vec<&str> = slug.split('_').filter(|s| !s.is_empty()).collect();
            // A slug matches the noun when any of the noun parts appears as a
            // slug token. Plural/singular forms are both tried.
            let matches = noun_parts.iter().any(|np| {
                let np_single = np.trim_end_matches('s');
                tokens.iter().any(|t| *t == *np || *t == np_single)
            });
            if matches {
                accum += *count;
                matched_any = true;
            }
        }
        if matched_any {
            noun_map.insert((*noun).to_string(), accum);
        }
    }
    (slug_map, noun_map)
}

/// P5 — per-file summary returned to `seal()` so it can either fail the seal
/// or surface warnings. The caller writes the expanded content back to disk
/// before `collect_files_meta` hashes it.
struct CountExpansionReport {
    /// Relative filename (used for diagnostics only).
    file: String,
    /// Content after handlebar expansion. This is what seal writes back.
    expanded: String,
    /// Mismatched prose claims found in `expanded`.
    mismatches: Vec<NumericClaimMismatch>,
}

/// P5 — orchestration helper used by `seal`. Reads every required markdown
/// file in `current_dir`, expands handlebars, scans for stale prose claims,
/// and returns one report per file. Non-markdown required files (the JSON
/// structured layer) are skipped.
fn apply_count_templates(
    current_dir: &Path,
    required_files: &[String],
    slug_counts: &std::collections::HashMap<String, usize>,
    noun_counts: &std::collections::HashMap<String, usize>,
) -> Vec<CountExpansionReport> {
    let mut reports: Vec<CountExpansionReport> = Vec::new();
    for file in required_files {
        if !file.ends_with(".md") {
            continue;
        }
        let path = current_dir.join(file);
        let original = match fs::read_to_string(&path) {
            Ok(t) => t,
            Err(_) => continue,
        };
        let expanded = expand_count_handlebars(&original, slug_counts);
        let mismatches = extract_numeric_claims(&expanded, noun_counts, file);
        reports.push(CountExpansionReport {
            file: file.clone(),
            expanded,
            mismatches,
        });
    }
    reports
}

/// P1 — parse Python top-level function signatures via a regex that tolerates
/// decorators, async, and multi-line parameter lists. `ast`-level parsing would
/// be ideal but would introduce a non-stdlib dep; the regex is narrow enough to
/// only trigger on `def name(...)` at column 0.
fn parse_python_signatures(
    source: &str,
    file_label: &str,
    out: &mut std::collections::BTreeMap<String, String>,
) {
    let mut lines = source.lines().peekable();
    let mut accumulated = String::new();
    let mut collecting = false;
    let mut fn_name = String::new();
    while let Some(line) = lines.next() {
        if !collecting {
            // Detect `def NAME(` or `async def NAME(` at column 0 (no leading
            // whitespace) — top-level only per the P1 spec.
            let trimmed_prefix = line.trim_start_matches(|c: char| c == '\t' || c == ' ');
            let is_indented = trimmed_prefix.len() != line.len();
            if is_indented {
                continue;
            }
            let candidate = if let Some(rest) = line.strip_prefix("async def ") {
                Some(rest)
            } else {
                line.strip_prefix("def ")
            };
            let rest = match candidate {
                Some(r) => r,
                None => continue,
            };
            let paren = match rest.find('(') {
                Some(p) => p,
                None => continue,
            };
            let name_part = &rest[..paren];
            if !name_part
                .chars()
                .all(|c| c.is_alphanumeric() || c == '_')
                || name_part.is_empty()
            {
                continue;
            }
            fn_name = name_part.to_string();
            accumulated.clear();
            accumulated.push_str(line.trim_end());
            // Short-circuit: signature fits on one line.
            if line.contains(':') && paren_balanced(line) {
                let sig = one_line_signature(&accumulated);
                out.insert(format!("{}::{}", file_label, fn_name), sig);
                fn_name.clear();
                continue;
            }
            collecting = true;
        } else {
            accumulated.push(' ');
            accumulated.push_str(line.trim());
            if accumulated.contains(':') && paren_balanced(&accumulated) {
                let sig = one_line_signature(&accumulated);
                out.insert(format!("{}::{}", file_label, fn_name), sig);
                fn_name.clear();
                accumulated.clear();
                collecting = false;
            }
        }
    }
}

/// Parens balanced on the accumulated prefix so we know the param list closed.
fn paren_balanced(s: &str) -> bool {
    let mut depth: i32 = 0;
    for ch in s.chars() {
        match ch {
            '(' => depth += 1,
            ')' => {
                depth -= 1;
                if depth < 0 {
                    return false;
                }
            }
            _ => {}
        }
    }
    depth == 0
}

/// Normalize whitespace in a captured signature so equal signatures compare
/// byte-identically across cross-platform line endings and indent variants.
fn one_line_signature(raw: &str) -> String {
    let mut out = String::with_capacity(raw.len());
    let mut prev_space = false;
    for ch in raw.chars() {
        if ch.is_whitespace() {
            if !prev_space && !out.is_empty() {
                out.push(' ');
            }
            prev_space = true;
        } else {
            out.push(ch);
            prev_space = false;
        }
    }
    // Trim trailing colon-only variants and trailing whitespace.
    out.trim().to_string()
}

/// P1 — Rust top-level `fn NAME(params) -> RetType` or `fn NAME(params)`.
/// Visibility modifiers and generics are preserved in the captured signature.
fn parse_rust_signatures(
    source: &str,
    file_label: &str,
    out: &mut std::collections::BTreeMap<String, String>,
) {
    let mut lines = source.lines().peekable();
    let mut acc = String::new();
    let mut collecting = false;
    let mut fn_name = String::new();
    while let Some(line) = lines.next() {
        if !collecting {
            // Top-level fn: leading whitespace disallowed to skip method impls.
            let is_indented = line.chars().next().map(|c| c.is_whitespace()).unwrap_or(false);
            if is_indented {
                continue;
            }
            // Trim visibility and `async`/`unsafe`/`extern` qualifiers.
            let mut scan = line.trim_start();
            for prefix in ["pub(crate) ", "pub ", "async ", "unsafe ", "extern ", "const "] {
                if let Some(rest) = scan.strip_prefix(prefix) {
                    scan = rest;
                }
            }
            let rest = match scan.strip_prefix("fn ") {
                Some(r) => r,
                None => continue,
            };
            // Extract the function name up to '(' or '<'.
            let stop = rest
                .find(|c: char| c == '(' || c == '<' || c.is_whitespace())
                .unwrap_or(rest.len());
            let name = &rest[..stop];
            if name.is_empty()
                || !name.chars().all(|c| c.is_alphanumeric() || c == '_')
            {
                continue;
            }
            fn_name = name.to_string();
            acc.clear();
            acc.push_str(line.trim_end());
            // Signature ends at `{` or `;` with balanced parens.
            if (acc.contains('{') || acc.contains(';')) && paren_balanced(&acc) {
                let sig = rust_signature_head(&acc);
                out.insert(format!("{}::{}", file_label, fn_name), sig);
                fn_name.clear();
                continue;
            }
            collecting = true;
        } else {
            acc.push(' ');
            acc.push_str(line.trim());
            if (acc.contains('{') || acc.contains(';')) && paren_balanced(&acc) {
                let sig = rust_signature_head(&acc);
                out.insert(format!("{}::{}", file_label, fn_name), sig);
                fn_name.clear();
                acc.clear();
                collecting = false;
            }
        }
    }
}

/// Capture the signature up to (but not including) the body-opening `{` or the
/// trailing `;`. Whitespace is normalized for stable comparison.
fn rust_signature_head(raw: &str) -> String {
    // Prefer the first `{` only if it is after a balanced-paren prefix;
    // otherwise fall back to the first `;`. In practice the simpler "split at
    // first of {/;" is correct for top-level declarations.
    let cut = raw
        .find('{')
        .or_else(|| raw.find(';'))
        .unwrap_or(raw.len());
    one_line_signature(&raw[..cut])
}

/// P1 — TypeScript / JavaScript top-level `function NAME(...)` or
/// `const NAME = (...)` / `export const NAME = (...)` arrow forms. Heuristics
/// are intentionally narrow: class methods, default exports, and IIFEs are
/// skipped so we do not emit false positives.
fn parse_ts_signatures(
    source: &str,
    file_label: &str,
    out: &mut std::collections::BTreeMap<String, String>,
) {
    let lines: Vec<&str> = source.lines().collect();
    let mut i = 0;
    while i < lines.len() {
        let line = lines[i];
        // Only top-level (no leading indent).
        let is_indented = line
            .chars()
            .next()
            .map(|c| c.is_whitespace())
            .unwrap_or(false);
        if is_indented {
            i += 1;
            continue;
        }
        let trimmed = line.trim_start();
        // `function NAME(` — accumulate until paren balance.
        let fn_prefix_candidates = [
            "export async function ",
            "export function ",
            "async function ",
            "function ",
        ];
        let mut captured_name: Option<String> = None;
        for prefix in fn_prefix_candidates {
            if let Some(rest) = trimmed.strip_prefix(prefix) {
                if let Some(paren) = rest.find('(') {
                    let name = &rest[..paren];
                    if !name.is_empty()
                        && name.chars().all(|c| c.is_alphanumeric() || c == '_')
                    {
                        captured_name = Some(name.to_string());
                    }
                }
                break;
            }
        }
        // `const NAME = (...) =>` or `export const NAME = (...) =>`.
        if captured_name.is_none() {
            for prefix in ["export const ", "const ", "export let ", "let "] {
                if let Some(rest) = trimmed.strip_prefix(prefix) {
                    if let Some(eq) = rest.find('=') {
                        let name = rest[..eq].trim();
                        let after_eq = rest[eq + 1..].trim_start();
                        if !name.is_empty()
                            && name.chars().all(|c| c.is_alphanumeric() || c == '_')
                            && (after_eq.starts_with('(') || after_eq.starts_with("async ("))
                        {
                            captured_name = Some(name.to_string());
                        }
                    }
                    break;
                }
            }
        }
        let name = match captured_name {
            Some(n) => n,
            None => {
                i += 1;
                continue;
            }
        };

        // Accumulate until paren-balanced + contains `=>` or `{`.
        let mut acc = String::new();
        acc.push_str(line.trim_end());
        let mut terminated = false;
        let mut j = i + 1;
        if (acc.contains("=>") || acc.contains('{')) && paren_balanced(&acc) {
            terminated = true;
        }
        while !terminated && j < lines.len() {
            acc.push(' ');
            acc.push_str(lines[j].trim());
            if (acc.contains("=>") || acc.contains('{')) && paren_balanced(&acc) {
                terminated = true;
                break;
            }
            j += 1;
        }
        // Capture everything up to the first `{` (function body) or `=>`.
        let cut = acc
            .find('{')
            .or_else(|| acc.find("=>"))
            .unwrap_or(acc.len());
        let sig = one_line_signature(&acc[..cut]);
        out.insert(format!("{}::{}", file_label, name), sig);
        i = if terminated { j + 1 } else { j };
    }
}

/// P1 — `parse_shortcut_signatures` dispatches by file extension. Languages
/// beyond Python / Rust / TypeScript / JavaScript are stubbed out with an
/// empty map (the manifest simply records no signatures for those files).
fn parse_shortcut_signatures_for_file(
    path: &Path,
    file_label: &str,
) -> std::collections::BTreeMap<String, String> {
    let mut out = std::collections::BTreeMap::new();
    let source = match fs::read_to_string(path) {
        Ok(s) => s,
        Err(_) => return out,
    };
    let ext = path
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("")
        .to_ascii_lowercase();
    match ext.as_str() {
        "py" => parse_python_signatures(&source, file_label, &mut out),
        "rs" => parse_rust_signatures(&source, file_label, &mut out),
        "ts" | "tsx" | "js" | "jsx" | "mjs" | "cjs" => {
            parse_ts_signatures(&source, file_label, &mut out)
        }
        _ => {
            // Stub — no parser for this language. Future P1 extensions
            // (Go, Java, etc.) would add their branch here.
        }
    }
    out
}

/// P1 — iterate every file referenced in `search_scope.json`
/// `verification_shortcuts[]` and collect top-level function signatures.
///
/// Per the plan: keyed by `"<file>::<fn_name>"` -> signature string.
/// Returns an empty map if `search_scope.json` is absent or malformed so
/// `build_manifest` never has to branch on presence.
fn parse_shortcut_signatures(
    repo_root: &Path,
    current_dir: &Path,
) -> std::collections::BTreeMap<String, String> {
    let mut out = std::collections::BTreeMap::new();
    let search_scope_path = current_dir.join("search_scope.json");
    let scope = match read_json(&search_scope_path) {
        Ok(Some(v)) => v,
        _ => return out,
    };
    let families = match scope.get("task_families").and_then(|v| v.as_object()) {
        Some(f) => f,
        None => return out,
    };

    let mut seen_files: BTreeSet<String> = BTreeSet::new();
    for (_task, entry) in families {
        let shortcuts = match entry
            .get("verification_shortcuts")
            .and_then(|v| v.as_object())
        {
            Some(s) => s,
            None => continue,
        };
        for (key, _value) in shortcuts {
            // Keys can take the form "path" or "path:line" — strip the suffix.
            let file_key = key.split(':').next().unwrap_or(key).to_string();
            if file_key.is_empty() {
                continue;
            }
            if !seen_files.insert(file_key.clone()) {
                continue;
            }
            let abs = repo_root.join(&file_key);
            if !abs.exists() {
                continue;
            }
            let sigs = parse_shortcut_signatures_for_file(&abs, &file_key);
            for (k, v) in sigs {
                out.insert(k, v);
            }
        }
    }
    out
}

/// P1 — SHA256 of dependency-declaring file contents. Records one entry per
/// file that exists; absent files are omitted.
fn compute_dependencies_snapshot(
    repo_root: &Path,
) -> std::collections::BTreeMap<String, String> {
    let mut out = std::collections::BTreeMap::new();
    let candidates: &[(&str, &str)] = &[
        ("pyproject", "pyproject.toml"),
        ("cargo", "Cargo.toml"),
        ("npm", "package.json"),
    ];
    for (key, filename) in candidates {
        let path = repo_root.join(filename);
        if !path.exists() {
            continue;
        }
        let bytes = match fs::read(&path) {
            Ok(b) => b,
            Err(_) => continue,
        };
        out.insert((*key).to_string(), sha256_hex(&bytes));
    }
    out
}

/// P1 — compose the full semantic baseline. Safe to call when pack-config
/// files are absent: each helper degrades to empty.
fn collect_semantic_baseline(repo_root: &Path, current_dir: &Path) -> SemanticBaseline {
    SemanticBaseline {
        family_counts: resolve_family_counts(repo_root, current_dir),
        declared_counts: extract_declared_counts(current_dir),
        shortcut_signatures: parse_shortcut_signatures(repo_root, current_dir),
        dependencies_snapshot: compute_dependencies_snapshot(repo_root),
    }
}

/// P11-drift / F38 — snapshot SHA256 of every regular file under
/// `<current_dir>/tools/`. Stored on the manifest at seal time so
/// `check-tool-integrity` (and `verify --ci`) can detect local tampering of
/// shipped helper scripts such as `verify_context_pack.py` and
/// `freshness.py`.
///
/// Missing `tools/` directory → empty map (not an error). Binary or
/// unreadable files are skipped silently so a transient read failure does
/// not block the seal; the next seal will pick them back up.
///
/// Keys are relative to `tools/` (e.g. `"verify_context_pack.py"`) and sorted
/// so the serialized JSON is deterministic across runs.
fn compute_tool_hashes(current_dir: &Path) -> std::collections::BTreeMap<String, String> {
    let mut out = std::collections::BTreeMap::new();
    let tools_dir = current_dir.join("tools");
    if !tools_dir.exists() {
        return out;
    }
    let entries = match fs::read_dir(&tools_dir) {
        Ok(it) => it,
        Err(_) => return out,
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if !path.is_file() {
            continue;
        }
        let name = match path.file_name().and_then(|s| s.to_str()) {
            Some(s) => s.to_string(),
            None => continue,
        };
        let bytes = match fs::read(&path) {
            Ok(b) => b,
            Err(_) => continue,
        };
        out.insert(name, sha256_hex(&bytes));
    }
    out
}

/// P11-drift / F38 — outcome of re-hashing the `tools/` directory against the
/// manifest's `tool_hashes` field. `mismatches` names every shipped helper
/// whose on-disk SHA256 diverges from the sealed value (or is missing
/// entirely); `extra_files` names files present on disk that the manifest
/// does not cover so callers can decide whether to treat them as drift.
#[derive(Debug, Default, Clone)]
pub(crate) struct ToolIntegrityReport {
    /// Files whose hash differs from the manifest entry, or that the
    /// manifest lists but are no longer present on disk.
    pub mismatches: Vec<ToolIntegrityMismatch>,
    /// Files in `tools/` not referenced by the manifest. Surfaced separately
    /// from `mismatches` because a fresh helper landing between seals is not
    /// necessarily tampering — but it is worth naming.
    pub extra_files: Vec<String>,
}

/// P11-drift / F38 — one element of [`ToolIntegrityReport::mismatches`]. Used
/// by the human renderer and by the CI JSON so the failure log always names
/// the specific file that drifted.
#[derive(Debug, Clone)]
pub(crate) struct ToolIntegrityMismatch {
    pub file: String,
    /// Short description of the failure mode: `"checksum mismatch"` or
    /// `"file missing"`.
    pub reason: String,
}

/// P11-drift / F38 — re-hash every file under `<current_dir>/tools/` and
/// compare against the manifest's `tool_hashes` field. Missing manifest or
/// missing `tool_hashes` field → empty report (silent pass). A manifest that
/// records a file which is absent on disk yields a `"file missing"`
/// mismatch so tampering via deletion is caught.
pub(crate) fn compute_tool_integrity_report(
    manifest: &Value,
    current_dir: &Path,
) -> ToolIntegrityReport {
    let mut report = ToolIntegrityReport::default();
    let recorded = match manifest
        .get("tool_hashes")
        .and_then(|v| v.as_object())
    {
        Some(obj) => obj,
        None => return report,
    };

    // Check every manifest-recorded tool against the filesystem.
    let tools_dir = current_dir.join("tools");
    for (file, expected_value) in recorded.iter() {
        let expected = match expected_value.as_str() {
            Some(s) => s,
            None => continue,
        };
        let abs = tools_dir.join(file);
        if !abs.exists() {
            report.mismatches.push(ToolIntegrityMismatch {
                file: file.clone(),
                reason: "file missing".to_string(),
            });
            continue;
        }
        let actual = match fs::read(&abs) {
            Ok(b) => sha256_hex(&b),
            Err(_) => {
                report.mismatches.push(ToolIntegrityMismatch {
                    file: file.clone(),
                    reason: "read error".to_string(),
                });
                continue;
            }
        };
        if actual != expected {
            report.mismatches.push(ToolIntegrityMismatch {
                file: file.clone(),
                reason: "checksum mismatch".to_string(),
            });
        }
    }

    // Surface extra (unsealed) files so CI consumers can decide policy.
    if tools_dir.exists() {
        if let Ok(entries) = fs::read_dir(&tools_dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                if !path.is_file() {
                    continue;
                }
                let name = match path.file_name().and_then(|s| s.to_str()) {
                    Some(s) => s.to_string(),
                    None => continue,
                };
                if !recorded.contains_key(&name) {
                    report.extra_files.push(name);
                }
            }
        }
    }
    report.extra_files.sort();
    report.mismatches.sort_by(|a, b| a.file.cmp(&b.file));
    report
}

/// P11-drift / F38 — public entry point for `chorus agent-context
/// check-tool-integrity`. Loads the manifest, runs the comparison, and prints
/// a human-readable report. Exits with `Err` when any mismatch is present so
/// callers (and CI) can treat a non-zero exit as a tampering signal.
pub fn check_tool_integrity(cwd: &str, pack_dir: Option<&str>) -> Result<()> {
    let cwd_path = PathBuf::from(cwd);
    let repo_root = git_repo_root(&cwd_path).unwrap_or_else(|_| cwd_path.clone());
    let pack_root = resolve_pack_root(&repo_root, pack_dir);
    let current_dir = pack_root.join("current");
    let manifest_path = current_dir.join("manifest.json");

    if !manifest_path.exists() {
        return Err(anyhow!(
            "[agent-context] check-tool-integrity failed: manifest.json not found at {}",
            manifest_path.display()
        ));
    }

    let raw = fs::read_to_string(&manifest_path)
        .with_context(|| format!("Failed to read {}", manifest_path.display()))?;
    let manifest: Value = serde_json::from_str(&raw)
        .with_context(|| format!("Failed to parse {}", manifest_path.display()))?;

    let report = compute_tool_integrity_report(&manifest, &current_dir);

    if manifest.get("tool_hashes").is_none() {
        println!(
            "[agent-context] check-tool-integrity: manifest has no tool_hashes field (sealed by an older chorus); nothing to verify"
        );
        return Ok(());
    }

    let recorded_count = manifest
        .get("tool_hashes")
        .and_then(|v| v.as_object())
        .map(|o| o.len())
        .unwrap_or(0);

    if report.mismatches.is_empty() {
        println!(
            "[agent-context] check-tool-integrity: PASS ({} tool file(s) verified)",
            recorded_count
        );
        if !report.extra_files.is_empty() {
            eprintln!(
                "  note: {} file(s) under tools/ are not covered by the manifest:",
                report.extra_files.len()
            );
            for f in &report.extra_files {
                eprintln!("    - {}", f);
            }
            eprintln!("  (run `chorus agent-context seal` to pick them up)");
        }
        return Ok(());
    }

    eprintln!(
        "[agent-context] check-tool-integrity: FAIL ({} tool file(s) drifted)",
        report.mismatches.len()
    );
    for m in &report.mismatches {
        eprintln!("  FAIL  tools/{}  ({})", m.file, m.reason);
    }
    Err(anyhow!(
        "[agent-context] check-tool-integrity: {} tool file(s) differ from manifest",
        report.mismatches.len()
    ))
}

#[allow(clippy::too_many_arguments)]
fn build_manifest(
    generated_at: &str,
    _repo_root: &Path,
    repo_name: &str,
    branch: &str,
    detached: bool,
    head_sha: Option<&str>,
    reason: &str,
    base_sha: Option<&str>,
    changed_files: &[String],
    files_meta: &[FileMeta],
    baseline: &SemanticBaseline,
    tool_hashes: &std::collections::BTreeMap<String, String>,
) -> ManifestBundle {
    let pack_checksum_input = files_meta
        .iter()
        .map(|meta| format!("{}:{}", meta.path, meta.sha256))
        .collect::<Vec<_>>()
        .join("\n");
    let pack_checksum = sha256_hex(pack_checksum_input.as_bytes());

    let stable_input = files_meta
        .iter()
        .filter(|meta| meta.path != "00_START_HERE.md")
        .map(|meta| format!("{}:{}", meta.path, meta.sha256))
        .collect::<Vec<_>>()
        .join("\n");
    let stable_checksum = sha256_hex(stable_input.as_bytes());

    let words_total: usize = files_meta.iter().map(|meta| meta.words).sum();
    let bytes_total: u64 = files_meta.iter().map(|meta| meta.bytes).sum();

    let files = files_meta
        .iter()
        .map(|meta| {
            json!({
                "path": meta.path,
                // P8/F21: lowercased path for case-insensitive FS collision
                // detection on verify. Additive field; keeps existing shape.
                "path_lower": meta.path_lower,
                "sha256": meta.sha256,
                "bytes": meta.bytes,
                "words": meta.words,
            })
        })
        .collect::<Vec<_>>();

    // F26: detached HEAD → branch is null + detached: true rather than the literal "HEAD".
    let branch_value: Value = if detached || branch.is_empty() || branch == "HEAD" {
        Value::Null
    } else {
        Value::String(branch.to_string())
    };

    // P11 / F36: forensic tooling-version fields.
    // `chorus_version` pins the sealing tool; `verifier_sha256` is the hash
    // of the binary that sealed it (when available). `skill_version` is
    // reserved for the team_skills track to populate — we leave it null here
    // so a later chorus release or team_skills scaffolder can fill it in.
    let chorus_version = env!("CARGO_PKG_VERSION");
    let verifier_sha256 = match current_exe_sha256() {
        Some(hash) => Value::String(hash),
        None => {
            eprintln!(
                "[agent-context] WARN: could not hash current chorus binary; \
                 manifest.verifier_sha256 will be null"
            );
            Value::Null
        }
    };

    // P1 — semantic baseline serialization. Empty maps/arrays are emitted
    // rather than omitted so consumers can rely on the keys being present.
    let family_counts_value = {
        let mut map = serde_json::Map::new();
        for (k, v) in &baseline.family_counts {
            map.insert(k.clone(), json!(*v));
        }
        Value::Object(map)
    };
    let declared_counts_value = Value::Array(baseline.declared_counts.clone());
    let shortcut_signatures_value = {
        let mut map = serde_json::Map::new();
        for (k, v) in &baseline.shortcut_signatures {
            map.insert(k.clone(), Value::String(v.clone()));
        }
        Value::Object(map)
    };
    let dependencies_snapshot_value = {
        let mut map = serde_json::Map::new();
        for (k, v) in &baseline.dependencies_snapshot {
            map.insert(k.clone(), Value::String(v.clone()));
        }
        Value::Object(map)
    };

    // P11-drift / F38 — tool_hashes. One entry per regular file under
    // `tools/`, keyed by filename. Empty when the pack does not ship any
    // helper scripts (older installs, `init` without `--with-tools`).
    let tool_hashes_value = {
        let mut map = serde_json::Map::new();
        for (k, v) in tool_hashes {
            map.insert(k.clone(), Value::String(v.clone()));
        }
        Value::Object(map)
    };

    // P1 — `head_sha_at_seal` is the new canonical name for the seal-time HEAD.
    // We keep `head_sha` populated for one release so readers pinned to the
    // older field continue to work. `post_commit_sha` is null at seal; the
    // new `post-commit-reconcile` subcommand populates it after a commit.
    let head_sha_at_seal_value: Value = match head_sha {
        Some(s) => Value::String(s.to_string()),
        None => Value::Null,
    };

    let value = json!({
        "schema_version": CURRENT_SCHEMA_VERSION,
        "chorus_version": chorus_version,
        "skill_version": Value::Null,
        "verifier_sha256": verifier_sha256,
        "generated_at": generated_at,
        "repo_name": repo_name,
        "repo_root": ".",
        "branch": branch_value,
        "detached": detached,
        "head_sha": head_sha,
        "head_sha_at_seal": head_sha_at_seal_value,
        "post_commit_sha": Value::Null,
        // P13/F58: last-known-good pointer. Null at seal time; the verify path
        // (under --ci when integrity+freshness pass) promotes the manifest's
        // `head_sha_at_seal` into this field so `rollback --latest-good` has
        // a stable target. Rebuilt manifests start null — a subsequent --ci
        // verify seals the new last-good.
        "last_known_good_sha": Value::Null,
        // P13/F50: pack-file alias map. Keys are canonical filenames (e.g.
        // `20_CODE_MAP.md`); values are the on-disk name the team prefers.
        // The verifier accepts either name when resolving required files.
        // Empty object = no aliases configured (the default).
        "aliases": Value::Object(serde_json::Map::new()),
        "build_reason": reason,
        "base_sha": base_sha,
        "changed_files": changed_files,
        "files_count": files_meta.len(),
        "words_total": words_total,
        "bytes_total": bytes_total,
        "pack_checksum": pack_checksum,
        "stable_checksum": stable_checksum,
        "files": files,
        // P1 — semantic baseline fields.
        "family_counts": family_counts_value,
        "declared_counts": declared_counts_value,
        "shortcut_signatures": shortcut_signatures_value,
        "dependencies_snapshot": dependencies_snapshot_value,
        // P11-drift / F38 — per-file SHA256 of shipped helper scripts under
        // `.agent-context/current/tools/`. Consumed by
        // `check-tool-integrity` and `verify --ci` to detect tampering.
        "tool_hashes": tool_hashes_value,
    });

    ManifestBundle {
        value,
        stable_checksum,
        pack_checksum,
    }
}

// History rotation thresholds (F55).
const HISTORY_ROTATE_MAX_BYTES: u64 = 5 * 1024 * 1024;
const HISTORY_ROTATE_MAX_ENTRIES: usize = 1000;

/// P12 / F42 — resolve the git committer identity for `history.jsonl`'s
/// `sealed_by` field. Uses `git config user.name` + `user.email` at the repo
/// root so the audit trail records the person running `chorus agent-context
/// seal`, not whatever shell user happens to own the process. Returns
/// `"name <email>"` or an empty string when git isn't configured.
fn git_committer_identity(repo_root: &Path) -> String {
    let name = run_git(&["config", "user.name"], repo_root, true)
        .unwrap_or_default()
        .trim()
        .to_string();
    let email = run_git(&["config", "user.email"], repo_root, true)
        .unwrap_or_default()
        .trim()
        .to_string();
    match (name.is_empty(), email.is_empty()) {
        (true, true) => String::new(),
        (false, true) => name,
        (true, false) => format!("<{email}>"),
        (false, false) => format!("{name} <{email}>"),
    }
}

/// P12 / F42 — compute the H2 section headings whose body changed vs the
/// most recent snapshot's markdown files. Returns heading names prefixed by
/// the file they live in (e.g. `"20_CODE_MAP.md#Contexts"`). Empty list on
/// first seal (no previous snapshot) or when snapshots are unreadable.
fn compute_prose_diff_sections(snapshots_dir: &Path, current_dir: &Path) -> Vec<String> {
    let latest = match most_recent_snapshot_dir(snapshots_dir) {
        Some(p) => p,
        None => return Vec::new(),
    };
    let mut changed: Vec<String> = Vec::new();
    for file_name in REQUIRED_FILES {
        let prev_path = latest.join(file_name);
        let cur_path = current_dir.join(file_name);
        let prev = fs::read_to_string(&prev_path).unwrap_or_default();
        let cur = fs::read_to_string(&cur_path).unwrap_or_default();
        if prev == cur {
            continue;
        }
        let prev_sections = split_markdown_h2_sections(&prev);
        let cur_sections = split_markdown_h2_sections(&cur);
        // Union of headings, stable order via iteration over cur first.
        let mut seen: BTreeSet<String> = BTreeSet::new();
        for (heading, body) in &cur_sections {
            if prev_sections.get(heading) != Some(body) {
                let key = format!("{}#{}", file_name, heading);
                if seen.insert(key.clone()) {
                    changed.push(key);
                }
            }
        }
        for (heading, _body) in &prev_sections {
            if !cur_sections.contains_key(heading) {
                let key = format!("{}#{}", file_name, heading);
                if seen.insert(key.clone()) {
                    changed.push(key);
                }
            }
        }
    }
    changed
}

/// Walk `snapshots_dir` and return the most recent snapshot path by name
/// (snapshot IDs are `<timestamp>_<sha>[-N]` so lexical sort == temporal
/// sort). Returns `None` when the directory is absent or empty.
fn most_recent_snapshot_dir(snapshots_dir: &Path) -> Option<PathBuf> {
    let entries = fs::read_dir(snapshots_dir).ok()?;
    let mut ids: Vec<PathBuf> = entries
        .filter_map(|e| e.ok())
        .filter(|e| e.path().is_dir())
        .map(|e| e.path())
        .collect();
    if ids.is_empty() {
        return None;
    }
    ids.sort();
    ids.pop()
}

/// Split markdown into `(heading, body)` pairs keyed by H2 heading text.
/// H1 and H3+ headings are ignored. Used by
/// [`compute_prose_diff_sections`] for the history audit trail.
fn split_markdown_h2_sections(text: &str) -> std::collections::BTreeMap<String, String> {
    let mut out: std::collections::BTreeMap<String, String> = std::collections::BTreeMap::new();
    let mut current_heading: Option<String> = None;
    let mut body = String::new();
    for line in text.lines() {
        if let Some(rest) = line.strip_prefix("## ") {
            if let Some(h) = current_heading.take() {
                out.insert(h, body.clone());
            }
            body.clear();
            current_heading = Some(rest.trim().to_string());
            continue;
        }
        if current_heading.is_some() {
            body.push_str(line);
            body.push('\n');
        }
    }
    if let Some(h) = current_heading.take() {
        out.insert(h, body);
    }
    out
}

fn append_jsonl(path: &Path, value: &Value) -> Result<()> {
    if let Some(parent) = path.parent() {
        ensure_dir(parent)?;
    }
    rotate_history_if_needed(path)?;
    let mut file = fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)
        .with_context(|| format!("Failed to open {}", path.display()))?;
    writeln!(file, "{}", serde_json::to_string(value)?)
        .with_context(|| format!("Failed to append {}", path.display()))?;
    file.sync_all().ok();
    Ok(())
}

/// Rotate `history.jsonl` when it exceeds 5MB or 1000 lines (F55).
///
/// The active file is renamed to `history.jsonl.{N}` (next integer), a fresh
/// empty `history.jsonl` is created, and `history_index.json` is rewritten with
/// an entry describing the rotated file. Rollback consults the index to locate
/// snapshot IDs that live in historical files.
fn rotate_history_if_needed(history_path: &Path) -> Result<()> {
    if !history_path.exists() {
        return Ok(());
    }
    let metadata = match fs::metadata(history_path) {
        Ok(m) => m,
        Err(_) => return Ok(()),
    };
    let size = metadata.len();
    let line_count = if size > 0 {
        fs::read_to_string(history_path)
            .map(|s| s.lines().filter(|l| !l.trim().is_empty()).count())
            .unwrap_or(0)
    } else {
        0
    };

    if size < HISTORY_ROTATE_MAX_BYTES && line_count < HISTORY_ROTATE_MAX_ENTRIES {
        return Ok(());
    }

    let parent = history_path
        .parent()
        .ok_or_else(|| anyhow!("Missing parent for {}", history_path.display()))?;
    let base_name = history_path
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or("history.jsonl");

    // Find next rotation index.
    let mut next_index: u32 = 1;
    if let Ok(entries) = fs::read_dir(parent) {
        for entry in entries.flatten() {
            if let Some(name) = entry.file_name().to_str() {
                if let Some(rest) = name.strip_prefix(&format!("{}.", base_name)) {
                    if let Ok(n) = rest.parse::<u32>() {
                        if n >= next_index {
                            next_index = n + 1;
                        }
                    }
                }
            }
        }
    }

    let rotated_name = format!("{}.{}", base_name, next_index);
    let rotated_path = parent.join(&rotated_name);

    // Gather first/last snapshot IDs from the current file to record in index.
    let (first_id, last_id, entries) = summarize_history_file(history_path);

    fs::rename(history_path, &rotated_path).with_context(|| {
        format!(
            "Failed to rotate {} -> {}",
            history_path.display(),
            rotated_path.display()
        )
    })?;

    // Start a fresh empty file so subsequent appends land cleanly.
    atomic_write(history_path, b"")?;

    // Rewrite the history index atomically.
    let index_path = parent.join("history_index.json");
    let mut files_arr: Vec<Value> = match read_json(&index_path)? {
        Some(v) => v
            .get("files")
            .and_then(|f| f.as_array())
            .cloned()
            .unwrap_or_default(),
        None => Vec::new(),
    };
    files_arr.push(json!({
        "name": rotated_name,
        "first_id": first_id,
        "last_id": last_id,
        "entries": entries,
    }));
    let index_value = json!({
        "schema_version": 1,
        "active": base_name,
        "files": files_arr,
    });
    atomic_write(
        &index_path,
        format!("{}\n", serde_json::to_string_pretty(&index_value)?).as_bytes(),
    )?;

    eprintln!(
        "[context-pack] rotated history: {} -> {} ({} entries, {} bytes)",
        base_name, rotated_name, entries, size
    );

    Ok(())
}

fn summarize_history_file(path: &Path) -> (Option<String>, Option<String>, usize) {
    let raw = match fs::read_to_string(path) {
        Ok(s) => s,
        Err(_) => return (None, None, 0),
    };
    let mut first_id: Option<String> = None;
    let mut last_id: Option<String> = None;
    let mut count = 0usize;
    for line in raw.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        count += 1;
        if let Ok(v) = serde_json::from_str::<Value>(trimmed) {
            let id = v
                .get("snapshot_id")
                .and_then(|s| s.as_str())
                .map(|s| s.to_string());
            if first_id.is_none() {
                first_id = id.clone();
            }
            if id.is_some() {
                last_id = id;
            }
        }
    }
    (first_id, last_id, count)
}

fn read_json(path: &Path) -> Result<Option<Value>> {
    if !path.exists() {
        return Ok(None);
    }
    let raw = fs::read_to_string(path)
        .with_context(|| format!("Failed to read {}", path.display()))?;
    let value = serde_json::from_str::<Value>(&raw)
        .with_context(|| format!("Failed to parse {}", path.display()))?;
    Ok(Some(value))
}

fn copy_dir_recursive(source: &Path, destination: &Path) -> Result<()> {
    ensure_dir(destination)?;
    for entry in fs::read_dir(source)
        .with_context(|| format!("Failed to read {}", source.display()))?
    {
        let entry = entry.with_context(|| format!("Failed to read entry in {}", source.display()))?;
        let source_path = entry.path();
        let destination_path = destination.join(entry.file_name());
        if source_path.is_dir() {
            copy_dir_recursive(&source_path, &destination_path)?;
        } else {
            if let Some(parent) = destination_path.parent() {
                ensure_dir(parent)?;
            }
            fs::copy(&source_path, &destination_path).with_context(|| {
                format!(
                    "Failed to copy {} -> {}",
                    source_path.display(),
                    destination_path.display()
                )
            })?;
        }
    }
    Ok(())
}

fn rel_path(path: &Path, base: &Path) -> String {
    path.strip_prefix(base)
        .map(|relative| relative.display().to_string())
        .unwrap_or_else(|_| path.display().to_string())
}

fn is_dir_empty(path: &Path) -> Result<bool> {
    if !path.exists() {
        return Ok(true);
    }
    let mut entries = fs::read_dir(path)
        .with_context(|| format!("Failed to read {}", path.display()))?;
    Ok(entries.next().is_none())
}

#[derive(Debug)]
struct FileLock {
    path: PathBuf,
}

/// Maximum time (seconds) to wait for a lock before giving up (F29).
const LOCK_WAIT_SECS: u64 = 10;
/// Initial backoff interval (ms) when retrying lock acquisition.
const LOCK_BACKOFF_MS: u64 = 50;
/// Maximum backoff interval (ms).
const LOCK_BACKOFF_MAX_MS: u64 = 500;

fn acquire_lock(path: &Path) -> Result<FileLock> {
    acquire_lock_with_timeout(path, LOCK_WAIT_SECS)
}

/// Acquire the seal lock with bounded wait.
///
/// The lock covers the entire `read-manifest → write-files → write-history`
/// transaction (F29). If another process holds the lock but its PID is dead,
/// the stale lock is reclaimed with a warning. Live holders cause the caller
/// to wait with exponential backoff up to `timeout_secs`, then fail with a
/// clear message.
fn acquire_lock_with_timeout(path: &Path, timeout_secs: u64) -> Result<FileLock> {
    if let Some(parent) = path.parent() {
        ensure_dir(parent)?;
    }
    let start = SystemTime::now();
    let mut backoff_ms = LOCK_BACKOFF_MS;
    loop {
        match try_create_lock(path) {
            Ok(lock) => return Ok(lock),
            Err(LockAttempt::HeldByDeadPid(pid)) => {
                eprintln!(
                    "[context-pack] WARNING: cleaned stale lock (pid {} no longer running)",
                    pid
                );
                let _ = fs::remove_file(path);
                // Loop will retry immediately.
            }
            Err(LockAttempt::Held) => {
                let elapsed = SystemTime::now()
                    .duration_since(start)
                    .unwrap_or_default()
                    .as_secs();
                if elapsed >= timeout_secs {
                    return Err(anyhow!(
                        "[context-pack] another seal is in progress (lock: {}); waited {}s",
                        path.display(),
                        timeout_secs
                    ));
                }
                std::thread::sleep(std::time::Duration::from_millis(backoff_ms));
                backoff_ms = (backoff_ms * 2).min(LOCK_BACKOFF_MAX_MS);
            }
            Err(LockAttempt::Io(err)) => {
                return Err(anyhow!(
                    "[context-pack] failed to acquire lock ({}): {}",
                    path.display(),
                    err
                ));
            }
        }
    }
}

enum LockAttempt {
    HeldByDeadPid(u32),
    Held,
    Io(std::io::Error),
}

fn try_create_lock(path: &Path) -> std::result::Result<FileLock, LockAttempt> {
    match fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(path)
    {
        Ok(mut file) => {
            let pid = std::process::id();
            if let Err(err) = writeln!(file, "{}", pid) {
                let _ = fs::remove_file(path);
                return Err(LockAttempt::Io(err));
            }
            let _ = file.sync_all();
            Ok(FileLock {
                path: path.to_path_buf(),
            })
        }
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
            if let Ok(content) = fs::read_to_string(path) {
                if let Ok(pid) = content.trim().parse::<u32>() {
                    let is_running = Command::new("kill")
                        .arg("-0")
                        .arg(pid.to_string())
                        .stdout(std::process::Stdio::null())
                        .stderr(std::process::Stdio::null())
                        .status()
                        .map(|s| s.success())
                        .unwrap_or(false);
                    if !is_running {
                        return Err(LockAttempt::HeldByDeadPid(pid));
                    }
                }
            }
            Err(LockAttempt::Held)
        }
        Err(error) => Err(LockAttempt::Io(error)),
    }
}

impl Drop for FileLock {
    fn drop(&mut self) {
        let _ = fs::remove_file(&self.path);
    }
}

fn sha256_hex(input: &[u8]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(input);
    format!("{:x}", hasher.finalize())
}

fn is_zero_sha(value: &str) -> bool {
    let trimmed = value.trim();
    !trimmed.is_empty() && trimmed.chars().all(|ch| ch == '0') && trimmed.len() == ZERO_SHA.len()
}

fn short_sha(sha: Option<&str>) -> String {
    match sha {
        Some(value) if !value.trim().is_empty() && !is_zero_sha(value) => value.chars().take(12).collect(),
        _ => "none".to_string(),
    }
}

fn compact_timestamp(iso: &str) -> String {
    let mut compact = iso.replace(['-', ':'], "");
    if let Some(dot_idx) = compact.find('.') {
        if let Some(z_rel) = compact[dot_idx..].find('Z') {
            let end = dot_idx + z_rel + 1;
            compact.replace_range(dot_idx..end, "Z");
        }
    }
    compact
}

fn now_stamp() -> String {
    let duration = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default();
    let secs = duration.as_secs();

    // Days since epoch calculation
    let days = secs / 86400;
    let time_of_day = secs % 86400;
    let hours = time_of_day / 3600;
    let minutes = (time_of_day % 3600) / 60;
    let seconds = time_of_day % 60;

    // Civil date from days since epoch (algorithm from Howard Hinnant)
    let z = days as i64 + 719468;
    let era = if z >= 0 { z } else { z - 146096 } / 146097;
    let doe = (z - era * 146097) as u64;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let y = yoe as i64 + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = if m <= 2 { y + 1 } else { y };

    format!("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z", y, m, d, hours, minutes, seconds)
}

fn is_main_push(local_ref: &str, remote_ref: &str) -> bool {
    local_ref == "refs/heads/main" || remote_ref == "refs/heads/main"
}

fn is_context_relevant(file_path: &str) -> bool {
    let normalized = file_path.replace('\\', "/");
    if normalized.starts_with("blog/")
        || normalized.starts_with("notes/")
        || normalized.starts_with("drafts/")
        || normalized.starts_with("scratch/")
        || normalized.starts_with("tmp/")
        || normalized.starts_with(".agent-context/")
        || normalized.starts_with("docs/demo-")
    {
        return false;
    }

    if matches!(
        normalized.as_str(),
        "README.md"
            | "PROTOCOL.md"
            | "CONTRIBUTING.md"
            | "SKILL.md"
            | "CLAUDE.md"
            | "AGENTS.md"
            | "package.json"
            | "package-lock.json"
            | "cli/Cargo.toml"
            | "cli/Cargo.lock"
            | "docs/architecture.svg"
            | "docs/silo-tax-before-after.webp"
    ) {
        return true;
    }

    normalized.starts_with("scripts/")
        || normalized.starts_with("cli/src/")
        || normalized.starts_with("schemas/")
        || normalized.starts_with("fixtures/golden/")
        || normalized.starts_with("fixtures/session-store/")
        || normalized.starts_with(".github/workflows/")
}

/// Load relevance rules from `.agent-context/relevance.json` if it exists.
/// Returns None if the file is missing or contains invalid JSON.
/// Expected format: { "include": ["pattern", ...], "exclude": ["pattern", ...] }
fn load_relevance_rules(repo_root: &Path) -> Option<Value> {
    let rules_path = repo_root.join(".agent-context").join("relevance.json");
    let raw = fs::read_to_string(&rules_path).ok()?;
    let rules: Value = serde_json::from_str(&raw).ok()?;
    if rules.is_object()
        && (rules.get("include").and_then(|v| v.as_array()).is_some()
            || rules.get("exclude").and_then(|v| v.as_array()).is_some())
    {
        Some(rules)
    } else {
        None
    }
}

/// P3: a single zone in the relevance.json zone map.
#[derive(Debug, Clone)]
pub(crate) struct Zone {
    /// Glob patterns (repo-relative) that trigger this zone.
    pub paths: Vec<String>,
    /// Pack section filenames (e.g. `20_CODE_MAP.md`) affected when a file
    /// matching `paths` changes.
    pub affects: Vec<String>,
}

/// P3: load the zone map from `<repo_root>/.agent-context/relevance.json`.
///
/// Returns `None` when:
/// - the file is missing
/// - the JSON is invalid
/// - no `zones` key is present (legacy include/exclude shape — handled
///   separately by `load_relevance_rules` / `is_context_relevant_with_rules`)
///
/// Malformed zone entries (non-object, missing paths/affects, non-string
/// values) are silently skipped — they never abort loading, but they may make
/// the zone empty.
pub(crate) fn load_zone_map(repo_root: &Path) -> Option<Vec<Zone>> {
    let rules_path = repo_root.join(".agent-context").join("relevance.json");
    let raw = fs::read_to_string(&rules_path).ok()?;
    let parsed: Value = serde_json::from_str(&raw).ok()?;
    let zones_val = parsed.get("zones")?.as_array()?;
    let mut zones = Vec::new();
    for z in zones_val {
        let Some(obj) = z.as_object() else { continue };
        let paths: Vec<String> = obj
            .get("paths")
            .and_then(|v| v.as_array())
            .map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())
            .unwrap_or_default();
        let affects: Vec<String> = obj
            .get("affects")
            .and_then(|v| v.as_array())
            .map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())
            .unwrap_or_default();
        if paths.is_empty() && affects.is_empty() {
            continue;
        }
        zones.push(Zone { paths, affects });
    }
    Some(zones)
}

/// P3: resolve a single changed file to the set of pack sections affected,
/// according to the zone map. Returns deduplicated section names in the
/// insertion order of the first matching zone. Empty if no zone matches.
///
/// Matching uses the same globset semantics as Pass-0's
/// `is_context_relevant_with_rules` for consistency with the existing behavior.
pub(crate) fn resolve_affected_sections(file_path: &str, zones: &[Zone]) -> Vec<String> {
    let normalized = file_path.replace('\\', "/");
    let mut out = BTreeSet::new();
    for zone in zones {
        let patterns: Vec<&str> = zone.paths.iter().map(|s| s.as_str()).collect();
        let Some(glob_set) = build_glob_set(&patterns) else {
            continue;
        };
        if glob_set.is_match(&normalized) {
            for section in &zone.affects {
                out.insert(section.clone());
            }
        }
    }
    out.into_iter().collect()
}

/// P3: validate that every zone's `paths[]` resolves to at least one tracked
/// file. Returns `Ok(Some(msg))` with the first offending zone path when the
/// zone map has a pattern resolving to zero tracked files, per the plan's
/// "Verification fails if any zone's `paths` resolve to zero tracked files"
/// rule.
///
/// Implementation uses `git ls-files` once and then applies each zone's
/// globset in-memory. On non-git or empty ls-files output, we conservatively
/// skip validation (the outer freshness check already handles non-git).
fn validate_zone_map(zones: &[Zone], repo_root: &Path) -> Result<Option<String>> {
    if zones.is_empty() {
        return Ok(None);
    }
    let tracked = run_git(&["ls-files"], repo_root, true)?;
    if tracked.is_empty() {
        // Nothing to validate against — treat as a soft pass.
        return Ok(None);
    }
    let tracked_files: Vec<String> = tracked
        .lines()
        .map(|l| l.trim().to_string())
        .filter(|l| !l.is_empty())
        .collect();

    for zone in zones {
        for pattern in &zone.paths {
            let Some(glob_set) = build_glob_set(&[pattern.as_str()]) else {
                // Invalid glob — treat as a user authoring bug and surface it.
                return Ok(Some(format!(
                    "zone path '{pattern}' is not a valid glob pattern"
                )));
            };
            let any_match = tracked_files.iter().any(|f| glob_set.is_match(f));
            if !any_match {
                return Ok(Some(format!(
                    "zone path '{pattern}' resolves to zero tracked files"
                )));
            }
        }
    }
    Ok(None)
}

/// P3: compute the `suggest-patches` JSON payload for agent consumption.
///
/// Shape:
/// ```json
/// {
///   "changed_files": ["..."],
///   "pack_sections_to_update": ["20_CODE_MAP.md", ...],
///   "diff_excerpt": "<git diff excerpt, capped at 2KB>",
///   "baseline_drift": []
/// }
/// ```
///
/// `baseline_drift` is reserved for P2 (baseline manifest + drift detection)
/// and is always emitted as `[]` here so downstream schema consumers can
/// depend on the field existing.
pub(crate) fn suggest_patches(base: &str, cwd: &Path) -> Result<Value> {
    // Run the zone-aware freshness check. Treat failures as "no suggestion"
    // rather than propagating errors — callers still get a structured payload.
    let freshness = check_freshness_with_zones(base, cwd).unwrap_or(FreshnessResult {
        status: "skip".to_string(),
        changed_files: Vec::new(),
        pack_updated: false,
        skipped_reason: None,
        affected_sections: Vec::new(),
    });

    // Collect a bounded diff excerpt (2KB cap). We intentionally use `git diff`
    // content (not just names) so agents can reason about what changed; the
    // cap prevents runaway payloads on massive diffs.
    let diff_excerpt = collect_diff_excerpt(base, cwd).unwrap_or_default();

    Ok(json!({
        "changed_files": freshness.changed_files,
        "pack_sections_to_update": freshness.affected_sections,
        "diff_excerpt": diff_excerpt,
        // Reserved for P2 (baseline + drift check). Always an array for schema
        // stability even when empty.
        "baseline_drift": Vec::<Value>::new(),
    }))
}

/// P4 — evaluate `acceptance_tests.md` in the sealed pack and return one
/// `Value` per test with its `id`, `status`, and (when present) the full
/// `invalidated_by` list plus the subset that matched P2's drift signal.
///
/// Parsing contract: the file is expected to declare tests as YAML-style
/// front-matter blocks or simple markdown bullets with structured hints.
/// To avoid a YAML dependency and to keep this readable when P2 hasn't yet
/// integrated, we parse a lightweight schema:
///
///   ### test: <id>
///   - invalidated_by: fn_a, fn_b
///
/// Any block without a matching `### test:` header is skipped. Missing
/// file, unreadable file, or zero tests → returns an empty Vec.
///
/// NOTE (P2 integration): until P2 lands the `SIGNATURE_DRIFT` detector,
/// `drifted_functions` will always be empty and every test stays `ready`.
/// That's intentional — P4 ships the invalidated_by data pipeline now so
/// downstream agents can rely on the field existing; actual drift-triggered
/// status transitions come online the moment P2 is wired.
fn evaluate_acceptance_tests(
    current_dir: &Path,
    drifted_functions: &std::collections::BTreeSet<String>,
) -> Vec<Value> {
    let path = current_dir.join("acceptance_tests.md");
    let Ok(raw) = fs::read_to_string(&path) else {
        return Vec::new();
    };
    let mut out: Vec<Value> = Vec::new();
    let mut current_id: Option<String> = None;
    let mut current_invalidated: Vec<String> = Vec::new();

    let flush = |id: Option<String>,
                 invalidated: Vec<String>,
                 drifted: &std::collections::BTreeSet<String>,
                 out: &mut Vec<Value>| {
        if let Some(id) = id {
            let matched: Vec<String> = invalidated
                .iter()
                .filter(|fn_name| drifted.contains(*fn_name))
                .cloned()
                .collect();
            let status = if matched.is_empty() {
                "ready"
            } else {
                "needs_revalidation"
            };
            out.push(json!({
                "id": id,
                "status": status,
                "invalidated_by": invalidated,
                "invalidated_by_matched": matched,
            }));
        }
    };

    for line in raw.lines() {
        let trimmed = line.trim();
        if let Some(rest) = trimmed
            .strip_prefix("### test:")
            .or_else(|| trimmed.strip_prefix("### Test:"))
            .or_else(|| trimmed.strip_prefix("###test:"))
        {
            flush(
                current_id.take(),
                std::mem::take(&mut current_invalidated),
                drifted_functions,
                &mut out,
            );
            current_id = Some(rest.trim().to_string());
            continue;
        }
        if current_id.is_some() {
            // Accept "- invalidated_by: a, b" or "invalidated_by: [a, b]".
            let lower = trimmed.trim_start_matches(['-', ' ', '*']).to_string();
            if let Some(rest) = lower
                .strip_prefix("invalidated_by:")
                .or_else(|| lower.strip_prefix("invalidated_by :"))
            {
                // Strip optional `[...]` wrapper.
                let cleaned = rest.trim().trim_start_matches('[').trim_end_matches(']');
                for part in cleaned.split(',') {
                    let name = part.trim().trim_matches(|c: char| c == '"' || c == '\'');
                    if !name.is_empty() {
                        current_invalidated.push(name.to_string());
                    }
                }
            }
        }
    }
    flush(
        current_id.take(),
        current_invalidated,
        drifted_functions,
        &mut out,
    );
    out
}

/// P4 — collect the set of function names flagged as signature-drifted by
/// P2. Today we read them from an optional `signature_drift.json` under
/// `.agent-context/current/` because P2 may land after P4 does. When P2 is
/// integrated and stamps drift into the manifest or an alternate file, this
/// helper is the single place to update.
///
/// Graceful-degradation contract:
/// - No file present → empty set (every acceptance test stays `ready`).
/// - Unreadable or unparseable file → empty set.
/// - File present with a `drifted_functions` array → each entry joins the set.
fn collect_signature_drift(current_dir: &Path) -> std::collections::BTreeSet<String> {
    let mut out = std::collections::BTreeSet::new();
    let path = current_dir.join("signature_drift.json");
    let Ok(raw) = fs::read_to_string(&path) else {
        return out;
    };
    let Ok(value): Result<Value, _> = serde_json::from_str(&raw) else {
        return out;
    };
    if let Some(arr) = value.get("drifted_functions").and_then(|v| v.as_array()) {
        for entry in arr {
            if let Some(name) = entry.as_str() {
                out.insert(name.to_string());
            }
        }
    }
    out
}

/// Capture at most `MAX_DIFF_EXCERPT_BYTES` bytes from `git diff`. Truncates
/// on a UTF-8 boundary; if the diff fails (non-git / invalid base) returns an
/// empty string.
const MAX_DIFF_EXCERPT_BYTES: usize = 2 * 1024;
fn collect_diff_excerpt(base: &str, cwd: &Path) -> Result<String> {
    let raw = run_git(&["diff", &format!("{base}...HEAD")], cwd, true)?;
    let truncated = if raw.len() > MAX_DIFF_EXCERPT_BYTES {
        // Find the last char boundary below the cap so we don't split a
        // multi-byte sequence mid-sequence.
        let mut end = MAX_DIFF_EXCERPT_BYTES;
        while !raw.is_char_boundary(end) && end > 0 {
            end -= 1;
        }
        format!("{}\n… [truncated at {} bytes]", &raw[..end], MAX_DIFF_EXCERPT_BYTES)
    } else {
        raw
    };
    Ok(truncated)
}

/// P7 — result of the zone-grouped diff-since-seal computation. Returned by
/// [`diff_since_seal`] for CLI rendering and consumed by `verify --ci` so a
/// non-empty `acceptance_tests_invalidated` can fail CI.
pub(crate) struct DiffSinceSealResult {
    /// Full JSON payload written to stdout by the subcommand.
    pub value: Value,
    /// Mirrors `value["acceptance_tests_invalidated"]` for fast CI inspection.
    pub acceptance_tests_invalidated: Vec<Value>,
}

/// P7 — resolve the seal-time baseline commit. Prefers `post_commit_sha` when
/// the P1 post-commit reconcile step populated it; otherwise falls back to
/// `head_sha_at_seal`. Returns `None` when neither is present as a non-empty
/// string (e.g. an old manifest written before P1, or a pre-seal state).
pub(crate) fn resolve_seal_baseline_sha(manifest: &Value) -> Option<String> {
    let pick = |key: &str| -> Option<String> {
        manifest
            .get(key)
            .and_then(|v| v.as_str())
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
    };
    pick("post_commit_sha").or_else(|| pick("head_sha_at_seal"))
}

/// P7 — parse the acceptance-tests markdown (authored by P4) into a list of
/// `{test_id?, invalidated_by: [function_names]}` entries. When the file is
/// missing, malformed, or carries no `invalidated_by` markers, returns an
/// empty vector so `diff_since_seal` degrades gracefully.
///
/// Expected shape (per P4 plan):
///
/// ```markdown
/// ## Q1: …
/// - invalidated_by: compute_lift_with_ci, resolve_sample
/// ```
///
/// We accept both inline (`invalidated_by: fn_a, fn_b`) and line-prefixed
/// (`- invalidated_by:`) forms. Function names are comma/whitespace-split and
/// trimmed. The `test_id` is taken from the nearest preceding `##` heading
/// when present; otherwise omitted.
pub(crate) fn parse_acceptance_invalidated_by(current_dir: &Path) -> Vec<Value> {
    let path = current_dir.join("acceptance_tests.md");
    let Ok(raw) = fs::read_to_string(&path) else {
        return Vec::new();
    };
    let mut out = Vec::new();
    let mut current_heading: Option<String> = None;
    for line in raw.lines() {
        let trimmed = line.trim();
        if let Some(stripped) = trimmed.strip_prefix("## ") {
            current_heading = Some(stripped.trim().to_string());
            continue;
        }
        // Accept `- invalidated_by:`, `invalidated_by:`, and `* invalidated_by:`.
        let body = trimmed
            .trim_start_matches('-')
            .trim_start_matches('*')
            .trim();
        if let Some(rest) = body.strip_prefix("invalidated_by:") {
            let fns: Vec<String> = rest
                .split(|c: char| c == ',' || c.is_whitespace())
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect();
            if fns.is_empty() {
                continue;
            }
            let mut entry = serde_json::Map::new();
            if let Some(ref h) = current_heading {
                entry.insert("test_id".to_string(), Value::String(h.clone()));
            }
            entry.insert(
                "invalidated_by".to_string(),
                Value::Array(fns.into_iter().map(Value::String).collect()),
            );
            out.push(Value::Object(entry));
        }
    }
    out
}

/// P7 — given a list of signature-drift entries and acceptance-test
/// `invalidated_by` bindings, return the tests that should be marked as
/// needing revalidation. Each drift entry may shape as
/// `{"file": "...", "fn": "name"}` (P2 canonical shape) or simply the
/// function name as a string; both are accepted so the scorer works before
/// P2 lands.
pub(crate) fn match_invalidated_tests(
    drifts: &[Value],
    invalidated_by: &[Value],
) -> Vec<Value> {
    if drifts.is_empty() || invalidated_by.is_empty() {
        return Vec::new();
    }
    let drift_fns: BTreeSet<String> = drifts
        .iter()
        .filter_map(|d| {
            if let Some(s) = d.as_str() {
                return Some(s.to_string());
            }
            d.as_object()
                .and_then(|o| {
                    o.get("fn")
                        .or_else(|| o.get("function"))
                        .or_else(|| o.get("name"))
                })
                .and_then(|v| v.as_str())
                .map(|s| s.to_string())
        })
        .collect();
    let mut matched = Vec::new();
    for entry in invalidated_by {
        let Some(obj) = entry.as_object() else { continue };
        let Some(fns) = obj.get("invalidated_by").and_then(|v| v.as_array()) else {
            continue;
        };
        let hit_fns: Vec<String> = fns
            .iter()
            .filter_map(|v| v.as_str())
            .filter(|name| drift_fns.contains(*name))
            .map(|s| s.to_string())
            .collect();
        if hit_fns.is_empty() {
            continue;
        }
        let mut out_obj = serde_json::Map::new();
        if let Some(id) = obj.get("test_id") {
            out_obj.insert("test_id".to_string(), id.clone());
        }
        out_obj.insert(
            "matched_drifts".to_string(),
            Value::Array(hit_fns.into_iter().map(Value::String).collect()),
        );
        matched.push(Value::Object(out_obj));
    }
    matched
}

/// P7 — compose the reconciler's natural-language bullet list from the diff
/// payload. We keep this as plain strings so downstream agents can paste them
/// into a prompt without further formatting.
fn recommended_actions(
    zones: &[Value],
    acceptance_invalidated: &[Value],
    pack_updated: bool,
) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    for zone in zones {
        let affects: Vec<String> = zone
            .get("affects")
            .and_then(|v| v.as_array())
            .map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())
            .unwrap_or_default();
        let drifts_n = zone
            .get("signature_drifts")
            .and_then(|v| v.as_array())
            .map(|a| a.len())
            .unwrap_or(0);
        let count_deltas_n = zone
            .get("count_deltas")
            .and_then(|v| v.as_array())
            .map(|a| a.len())
            .unwrap_or(0);
        let deletions_n = zone
            .get("deleted_files")
            .and_then(|v| v.as_array())
            .map(|a| a.len())
            .unwrap_or(0);
        let changed_n = zone
            .get("changed_files")
            .and_then(|v| v.as_array())
            .map(|a| a.len())
            .unwrap_or(0);
        if affects.is_empty() || changed_n == 0 {
            continue;
        }
        for section in &affects {
            if drifts_n > 0 {
                out.push(format!(
                    "Update {section}: {drifts_n} signature drift(s)"
                ));
            }
            if count_deltas_n > 0 {
                out.push(format!(
                    "Run expand_counts for {section}: {count_deltas_n} family count delta(s)"
                ));
            }
            if deletions_n > 0 {
                out.push(format!(
                    "Update {section}: {deletions_n} file(s) deleted"
                ));
            }
            if drifts_n == 0 && count_deltas_n == 0 && deletions_n == 0 {
                out.push(format!(
                    "Review {section}: {changed_n} file(s) changed in zone"
                ));
            }
        }
    }
    if !acceptance_invalidated.is_empty() {
        out.push(format!(
            "Revalidate {} acceptance test(s) whose `invalidated_by` function drifted",
            acceptance_invalidated.len()
        ));
    }
    if !pack_updated {
        out.push(
            "Re-seal the pack (`chorus agent-context seal --force`) after patching sections"
                .to_string(),
        );
    }
    out
}

/// P7 — subagent reconciliation diff. Produces a zone-grouped JSON payload
/// comparing the current HEAD against the seal-time baseline recorded on the
/// manifest. Used by the orchestrator after parallel subagents modify code so
/// a single reconciler subagent can patch the right pack sections and re-seal.
///
/// Shape:
///
/// ```json
/// {
///   "baseline_sha": "...",
///   "pack_updated": false,
///   "zones": [{
///     "paths": [...],
///     "affects": [...],
///     "changed_files": [...],
///     "signature_drifts": [],
///     "count_deltas": [],
///     "deleted_files": []
///   }],
///   "acceptance_tests_invalidated": [],
///   "recommended_reconciliation_actions": []
/// }
/// ```
///
/// When P2 drift detection is not yet integrated, `signature_drifts`,
/// `count_deltas`, and `deleted_files` default to empty arrays — see the
/// `TODO(P2-integration)` markers in the body. The schema stays stable so
/// downstream consumers can rely on the keys existing.
pub(crate) fn diff_since_seal(cwd: &Path, pack_dir: Option<&str>) -> Result<DiffSinceSealResult> {
    let repo_root = git_repo_root(cwd).unwrap_or_else(|_| cwd.to_path_buf());
    let pack_root = resolve_pack_root(&repo_root, pack_dir);
    let current_dir = pack_root.join("current");
    let manifest_path = current_dir.join("manifest.json");

    // Preserve the PASS-through shape even when the manifest is absent: the
    // subcommand is opportunistic and must emit a usable payload rather than
    // surface a hard error for agents.
    let manifest: Value = match fs::read_to_string(&manifest_path) {
        Ok(raw) => serde_json::from_str(&raw).unwrap_or_else(|_| Value::Null),
        Err(_) => Value::Null,
    };

    let baseline_sha = resolve_seal_baseline_sha(&manifest);

    // When we have no baseline (unsealed pack, or very old pre-P1 manifest)
    // and no git history, return an empty-but-shaped payload.
    let zones_map = load_zone_map(&repo_root);
    let Some(zones) = zones_map else {
        let payload = json!({
            "baseline_sha": baseline_sha,
            "pack_updated": false,
            "zones": Vec::<Value>::new(),
            "acceptance_tests_invalidated": Vec::<Value>::new(),
            "recommended_reconciliation_actions": Vec::<String>::new(),
        });
        return Ok(DiffSinceSealResult {
            value: payload,
            acceptance_tests_invalidated: Vec::new(),
        });
    };

    // Resolve the diff range: prefer <baseline>..HEAD when baseline is known
    // and reachable. Otherwise fall back to HEAD~1..HEAD so the command still
    // produces useful output on fresh repos. Non-git directories produce an
    // empty change list and the zone-level payload stays empty.
    let changed_files_raw = match &baseline_sha {
        Some(sha) => {
            // `rev-parse --verify <sha>^{commit}` returns empty (allow_failure)
            // when the baseline commit isn't reachable — typical in shallow
            // clones or when the seal was done against a deleted branch.
            let verify = run_git(
                &["rev-parse", "--verify", &format!("{sha}^{{commit}}")],
                &repo_root,
                true,
            )?;
            if verify.is_empty() {
                // Baseline unreachable (e.g. shallow clone). Fall back to HEAD~1.
                run_git(&["diff", "--name-only", "HEAD~1..HEAD"], &repo_root, true)?
            } else {
                run_git(
                    &["diff", "--name-only", &format!("{sha}..HEAD")],
                    &repo_root,
                    true,
                )?
            }
        }
        None => run_git(&["diff", "--name-only", "HEAD~1..HEAD"], &repo_root, true)
            .unwrap_or_default(),
    };

    // Partition changed files: pack-internal edits don't belong in zones; they
    // drive `pack_updated`. Non-pack files are zone-grouped.
    let mut pack_updated = false;
    let mut non_pack_changes: Vec<String> = Vec::new();
    for file in changed_files_raw
        .lines()
        .map(|l| l.trim())
        .filter(|l| !l.is_empty())
    {
        if file.starts_with(".agent-context/current/") {
            pack_updated = true;
            continue;
        }
        non_pack_changes.push(file.to_string());
    }

    // Zone-group the non-pack changes via P3's resolve_affected_sections.
    // We construct a zone entry per authored zone (paths+affects) and collect
    // the subset of non_pack_changes whose glob matches.
    let mut zone_entries: Vec<Value> = Vec::new();
    for zone in &zones {
        let patterns: Vec<&str> = zone.paths.iter().map(|s| s.as_str()).collect();
        let Some(glob_set) = build_glob_set(&patterns) else {
            continue;
        };
        let matched: Vec<String> = non_pack_changes
            .iter()
            .filter(|f| glob_set.is_match(f.replace('\\', "/").as_str()))
            .cloned()
            .collect();
        if matched.is_empty() {
            continue;
        }

        // TODO(P2-integration): when P2 lands, compute signature_drifts,
        // count_deltas, and deleted_files by diffing the manifest baseline
        // (family_counts, shortcut_signatures) against current repo state
        // for the files in `matched`. Today P2 is not integrated in pass1,
        // so these degrade to [] with the schema preserved.
        let signature_drifts: Vec<Value> = Vec::new();
        let count_deltas: Vec<Value> = Vec::new();
        let deleted_files: Vec<Value> = Vec::new();

        zone_entries.push(json!({
            "paths": zone.paths,
            "affects": zone.affects,
            "changed_files": matched,
            "signature_drifts": signature_drifts,
            "count_deltas": count_deltas,
            "deleted_files": deleted_files,
        }));
    }

    // P4 acceptance-test schema: optional, additive. Collect all drifts across
    // zones so cross-zone drifts can still match an acceptance test's
    // `invalidated_by` list.
    let all_drifts: Vec<Value> = zone_entries
        .iter()
        .flat_map(|z| {
            z.get("signature_drifts")
                .and_then(|v| v.as_array())
                .cloned()
                .unwrap_or_default()
        })
        .collect();
    let acceptance_bindings = parse_acceptance_invalidated_by(&current_dir);
    let acceptance_invalidated = match_invalidated_tests(&all_drifts, &acceptance_bindings);

    let actions = recommended_actions(&zone_entries, &acceptance_invalidated, pack_updated);

    let payload = json!({
        "baseline_sha": baseline_sha,
        "pack_updated": pack_updated,
        "zones": zone_entries,
        "acceptance_tests_invalidated": acceptance_invalidated.clone(),
        "recommended_reconciliation_actions": actions,
    });

    Ok(DiffSinceSealResult {
        value: payload,
        acceptance_tests_invalidated: acceptance_invalidated,
    })
}

/// P7 — render the `diff --since-seal` payload in a compact human-readable
/// form. Mirrors the JSON contract so operators can eyeball zone grouping +
/// recommended actions without piping through `jq`.
pub fn render_diff_since_seal_text(payload: &Value) {
    let baseline = payload
        .get("baseline_sha")
        .and_then(|v| v.as_str())
        .map(|s| short_sha(Some(s)))
        .unwrap_or_else(|| "(none)".to_string());
    let pack_updated = payload
        .get("pack_updated")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    println!("agent-context diff --since-seal");
    println!("  baseline: {baseline}");
    println!("  pack_updated: {pack_updated}");

    let zones = payload.get("zones").and_then(|v| v.as_array());
    match zones {
        Some(arr) if !arr.is_empty() => {
            println!("  zones ({}):", arr.len());
            for zone in arr {
                let affects: Vec<String> = zone
                    .get("affects")
                    .and_then(|v| v.as_array())
                    .map(|a| a.iter().filter_map(|v| v.as_str().map(String::from)).collect())
                    .unwrap_or_default();
                let changed: Vec<String> = zone
                    .get("changed_files")
                    .and_then(|v| v.as_array())
                    .map(|a| a.iter().filter_map(|v| v.as_str().map(String::from)).collect())
                    .unwrap_or_default();
                println!(
                    "    - affects: [{}] ({} file(s) changed)",
                    affects.join(", "),
                    changed.len()
                );
                for f in changed.iter().take(MAX_CHANGED_FILES_DISPLAYED) {
                    println!("        {f}");
                }
                if changed.len() > MAX_CHANGED_FILES_DISPLAYED {
                    println!(
                        "        … and {} more",
                        changed.len() - MAX_CHANGED_FILES_DISPLAYED
                    );
                }
            }
        }
        _ => {
            println!("  zones: (none — no context-relevant code changes)");
        }
    }

    let invalidated = payload
        .get("acceptance_tests_invalidated")
        .and_then(|v| v.as_array());
    if let Some(arr) = invalidated {
        if !arr.is_empty() {
            println!("  acceptance_tests_invalidated:");
            for entry in arr {
                let id = entry
                    .get("test_id")
                    .and_then(|v| v.as_str())
                    .unwrap_or("(unknown)");
                println!("    - {id}");
            }
        }
    }

    let actions = payload
        .get("recommended_reconciliation_actions")
        .and_then(|v| v.as_array());
    if let Some(arr) = actions {
        if !arr.is_empty() {
            println!("  recommended_reconciliation_actions:");
            for a in arr {
                if let Some(s) = a.as_str() {
                    println!("    - {s}");
                }
            }
        }
    }
}

fn build_glob_set(patterns: &[&str]) -> Option<GlobSet> {
    let mut builder = GlobSetBuilder::new();
    for pattern in patterns {
        if let Ok(glob) = Glob::new(pattern) {
            builder.add(glob);
        }
    }
    builder.build().ok()
}

/// Determine if a file is context-relevant using loaded rules or hardcoded defaults.
fn is_context_relevant_with_rules(file_path: &str, rules: &Option<Value>) -> bool {
    let normalized = file_path.replace('\\', "/");

    if let Some(rules) = rules {
        if let Some(excludes_array) = rules.get("exclude").and_then(|v| v.as_array()) {
            let patterns: Vec<&str> = excludes_array.iter().filter_map(|v| v.as_str()).collect();
            if let Some(glob_set) = build_glob_set(&patterns) {
                if glob_set.is_match(&normalized) {
                    return false;
                }
            }
        }
        if let Some(includes_array) = rules.get("include").and_then(|v| v.as_array()) {
            let patterns: Vec<&str> = includes_array.iter().filter_map(|v| v.as_str()).collect();
            if let Some(glob_set) = build_glob_set(&patterns) {
                if glob_set.is_match(&normalized) {
                    return true;
                }
            }
        }
        return false;
    }

    // Fall back to hardcoded defaults
    is_context_relevant(file_path)
}

/// Reserved: content generator for auto-fill build mode.
/// Will be wired when `build --auto-fill` is implemented.
#[allow(dead_code)]
fn build_start_here(
    repo_name: &str,
    branch: &str,
    head_sha: &str,
    package_version: &str,
    cargo_version: &str,
    generated_at: &str,
    changed_files: &[String],
) -> String {
    let changed_summary = if changed_files.is_empty() {
        "- No explicit change range provided (manual build).".to_string()
    } else {
        changed_files
            .iter()
            .take(MAX_CHANGED_FILES_DISPLAYED)
            .map(|path| format!("- {}", path))
            .collect::<Vec<_>>()
            .join("\n")
    };

    format!(
        "# Context Pack: Start Here\n\nThis context pack is the first-stop index for agent work in this repository.\n\n## Snapshot\n- Repo: `{repo_name}`\n- Branch at generation: `{branch}`\n- HEAD commit: `{head_sha}`\n- Node package version: `{package_version}`\n- Rust crate version: `{cargo_version}`\n- Generated at: `{generated_at}`\n\n## Read Order (Token-Efficient)\n1. Read this file.\n2. Read `10_SYSTEM_OVERVIEW.md` for architecture and execution paths.\n3. Read `30_BEHAVIORAL_INVARIANTS.md` before changing behavior.\n4. Use `20_CODE_MAP.md` to deep dive only relevant files.\n5. Use `40_OPERATIONS_AND_RELEASE.md` for tests, release, and maintenance.\n\n## Fast Facts\n- Product: Local-first cross-agent session chorus CLI.\n- Implementations: Node (`scripts/read_session.cjs`) and Rust (`cli/src/main.rs`).\n- Quality gate: Node/Rust parity + schema validation + edge-case checks.\n- Core risk: behavior drift between Node and Rust command/output contracts.\n\n## Last Change Range Input\n{changed_summary}\n\n## Scope Rule\nFor \"understand this repo end-to-end\" requests:\n- Start with this pack only.\n- Open source files only after this pack identifies a precise target.\n- Treat this pack as the source of navigation and invariants.\n"
    )
}

/// Reserved: content generator for auto-fill build mode.
/// Will be wired when `build --auto-fill` is implemented.
#[allow(dead_code)]
fn build_system_overview(
    package_version: &str,
    cargo_version: &str,
    tracked_file_count: usize,
    path_counts: &[(String, usize)],
    command_surface: &[(&str, &str, Vec<&str>)],
) -> String {
    let command_rows = command_surface
        .iter()
        .map(|(command, intent, paths)| {
            format!(
                "| `{}` | {} | {} |",
                command,
                intent,
                paths
                    .iter()
                    .map(|path| format!("`{}`", path))
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        })
        .collect::<Vec<_>>()
        .join("\n");

    let path_lines = if path_counts.is_empty() {
        "- No tracked path counts available.".to_string()
    } else {
        path_counts
            .iter()
            .map(|(name, count)| format!("- {}: {} tracked files", name, count))
            .collect::<Vec<_>>()
            .join("\n")
    };

    format!(
        "# System Overview\n\n## Product Shape\n- Package version: `{package_version}`\n- Crate version: `{cargo_version}`\n- Tracked files: `{tracked_file_count}`\n- Delivery: npm package (`chorus`) + Rust binary (`chorus`).\n\n## Runtime Architecture\n1. User asks a provider agent for cross-agent status.\n2. Agent invokes chorus command (`read`, `list`, `search`, `compare`, `report`, `diff`, `relevance`, `send`, `messages`, `setup`, `doctor`, `trash-talk`, `context-pack`).\n3. Chorus resolves session stores (Codex/Claude/Gemini/Cursor), applies redaction, and returns terminal text or JSON.\n4. Agent answers user with evidence from chorus output.\n\n## Dual-Implementation Contract\n- Node path: `scripts/read_session.cjs` + `scripts/adapters/*.cjs`.\n- Rust path: `cli/src/main.rs`, `cli/src/agents.rs`, `cli/src/report.rs`, `cli/src/adapters/*.rs`.\n- Protocol authority: `PROTOCOL.md` and `schemas/*.json`.\n- Parity guard: `scripts/conformance.sh`.\n\n## Command Surface\n| Command | Intent | Primary Paths |\n| --- | --- | --- |\n{command_rows}\n\n## Tracked Path Density\n{path_lines}\n"
    )
}

/// Reserved: content generator for auto-fill build mode.
/// Will be wired when `build --auto-fill` is implemented.
#[allow(dead_code)]
fn build_code_map() -> String {
    r#"# Code Map

## High-Impact Paths
| Path | What | Why It Matters | Change Risk |
| --- | --- | --- | --- |
| `scripts/read_session.cjs` | Node CLI command parser + execution engine | Defines behavior for all user-facing commands in Node distribution | High |
| `scripts/adapters/*.cjs` | Node agent-specific session adapters | Session discovery, parsing, and metadata quality for each provider | High |
| `cli/src/main.rs` | Rust CLI command/arg handling | Entry-point parity with Node and release binary behavior | High |
| `cli/src/agents.rs` | Rust session parsing + redaction + read/list/search | Largest behavioral surface and most error-code paths | High |
| `cli/src/report.rs` | Rust compare/report logic | Cross-agent divergence logic and report markdown/json structure | High |
| `schemas/*.json` | JSON contract definitions | External compatibility for `--json` users and tests | High |
| `PROTOCOL.md` | Versioned protocol contract | Human contract that aligns Node, Rust, and tests | High |
| `README.md` | Public command docs and examples | User expectations and documentation-driven behavior | Medium |
| `scripts/conformance.sh` | Parity checks across implementations | Prevents silent behavior drift before release | High |
| `scripts/test_edge_cases.sh` | Edge and error-path checks | Guards hard-to-debug regressions in parse/error handling | High |
| `.github/workflows/ci.yml` | Mandatory validation workflow | Ensures checks run on push/PR | Medium |
| `.github/workflows/release.yml` | Release pipeline | Controls publish safety and artifact generation | Medium |

## Extension Recipe (New Agent)
1. Implement adapter in Rust: `cli/src/adapters/<agent>.rs` and register in `cli/src/adapters/mod.rs`.
2. Implement adapter in Node: `scripts/adapters/<agent>.cjs` and register in `scripts/adapters/registry.cjs`.
3. Add schema enum coverage in `schemas/*.json`.
4. Add fixtures and golden expectations under `fixtures/`.
5. Validate parity and edge cases through test scripts.
"#
    .to_string()
}

/// Reserved: content generator for auto-fill build mode.
/// Will be wired when `build --auto-fill` is implemented.
#[allow(dead_code)]
fn build_invariants() -> String {
    r#"# Behavioral Invariants

These constraints are contract-level and must be preserved unless intentionally versioned.

## Core Protocol Invariants
1. `read`, `list`, `search`, `compare`, and `report` must align with `PROTOCOL.md`.
2. Node and Rust outputs must remain behaviorally equivalent for shared commands.
3. `--json` failures must emit structured error JSON with stable error codes.
4. Secret-like tokens must be redacted before output.
5. Session selection defaults: latest scoped session first, then documented fallback behavior.

## Session Semantics
1. "current/latest" means newest session.
2. "past/previous session" means one session before newest.
3. `read --last N` returns last N assistant messages joined by `\n---\n`.
4. CWD scoping applies where provider data supports it.

## Update Checklist Before Merging Behavior Changes
1. Update code in both Node and Rust paths when command semantics change.
2. Update schema files for JSON shape changes.
3. Update `README.md` and `PROTOCOL.md` for public contract changes.
4. Update fixtures/golden outputs.
5. Run conformance, edge-case, and schema validation scripts.
"#
    .to_string()
}

/// Reserved: content generator for auto-fill build mode.
/// Will be wired when `build --auto-fill` is implemented.
#[allow(dead_code)]
fn build_operations() -> String {
    r#"# Operations And Release

## Standard Validation
```bash
npm run check
cargo test --manifest-path cli/Cargo.toml
```

## Main CI Checks
- `scripts/conformance.sh`
- `scripts/test_edge_cases.sh`
- `scripts/check_readme_examples.sh`
- `scripts/check_package_contents.sh`
- `scripts/validate_schemas.sh`

## Release Flow
1. Push tag `v*` to trigger `.github/workflows/release.yml`.
2. Verify phase runs conformance/docs/schema/version checks.
3. Package/publish Node artifact.
4. Build/upload Rust binaries and publish crate when tokens are configured.

## Context Pack Maintenance Contract
1. Build pack manually: `chorus context-pack build`.
2. Install branch-aware pre-push hook: `chorus context-pack install-hooks`.
3. On `main` push, hook runs `context-pack:sync-main`.
4. Sync updates the pack only when changed files are context-relevant.
5. Snapshots are saved under `.agent-context/snapshots/` for rollback/recovery.

## Rollback/Recovery
- Restore latest snapshot: `chorus context-pack rollback`
- Restore named snapshot: `chorus context-pack rollback --snapshot <snapshot_id>`
"#
    .to_string()
}

/// Update the Snapshot metadata lines in 00_START_HERE.md so they stay in sync
/// with manifest.json.  Only touches Branch, HEAD commit, and Generated at.
fn update_start_here_snapshot(
    current_dir: &Path,
    branch: &str,
    head_sha: Option<&str>,
    generated_at: &str,
) -> Result<()> {
    let start_here = current_dir.join("00_START_HERE.md");
    if !start_here.exists() {
        return Ok(());
    }
    let content = fs::read_to_string(&start_here)
        .with_context(|| format!("Failed to read {}", start_here.display()))?;

    let mut result = String::with_capacity(content.len());
    for line in content.lines() {
        if line.starts_with("- Branch at generation: `") {
            result.push_str(&format!("- Branch at generation: `{}`", branch));
        } else if line.starts_with("- HEAD commit: `") {
            result.push_str(&format!(
                "- HEAD commit: `{}`",
                head_sha.unwrap_or("unknown")
            ));
        } else if line.starts_with("- Generated at: `") {
            result.push_str(&format!("- Generated at: `{}`", generated_at));
        } else {
            result.push_str(line);
        }
        result.push('\n');
    }
    // Preserve trailing newline behavior of original
    if !content.ends_with('\n') {
        result.pop();
    }

    write_text_atomic(&start_here, &result)?;
    Ok(())
}

fn has_structured_layer(current_dir: &Path) -> bool {
    current_dir.join("routes.json").exists()
}

fn required_files_for_mode(current_dir: &Path) -> Vec<String> {
    // P13/F46: tiered adoption writes a subset of REQUIRED_FILES at init time,
    // so on a seal we only enforce the files the pack author actually
    // scaffolded. The legacy Tier-3 pack (all REQUIRED_FILES present) keeps
    // the strict check because every file is on disk; Tier 1/2 packs ship a
    // smaller set and we don't want seal to reject them for a file the team
    // explicitly opted out of.
    let mut files: Vec<String> = REQUIRED_FILES
        .iter()
        .filter(|name| current_dir.join(name).exists())
        .map(|value| value.to_string())
        .collect();
    if files.is_empty() {
        // Fall back to the canonical set so seal still produces a helpful
        // "missing file X" error when the pack is empty rather than a
        // silent no-op.
        files = REQUIRED_FILES
            .iter()
            .map(|value| value.to_string())
            .collect();
    }
    if has_structured_layer(current_dir) {
        files.extend(
            STRUCTURED_FILES
                .iter()
                .filter(|name| current_dir.join(name).exists())
                .map(|value| value.to_string()),
        );
    }
    files
}

fn walk_files(root_dir: &Path, current_dir: &Path, out: &mut Vec<String>) -> Result<()> {
    walk_files_bounded(root_dir, current_dir, out, 0)
}

/// P8/F20 — bounded directory walk. `depth` is the distance from the walk's
/// initial root. We stop descending past [`MAX_WALK_DEPTH`] to prevent
/// symlink-loop hangs and runaway recursion on pathological layouts.
fn walk_files_bounded(
    root_dir: &Path,
    current_dir: &Path,
    out: &mut Vec<String>,
    depth: usize,
) -> Result<()> {
    if depth >= MAX_WALK_DEPTH {
        eprintln!(
            "[context-pack] WARN: walk depth limit ({}) reached at {} — skipping deeper entries",
            MAX_WALK_DEPTH,
            current_dir.display()
        );
        return Ok(());
    }
    for entry in fs::read_dir(current_dir)
        .with_context(|| format!("Failed to read {}", current_dir.display()))?
    {
        let entry =
            entry.with_context(|| format!("Failed to read entry in {}", current_dir.display()))?;
        let entry_path = entry.path();
        if entry.file_name() == ".git" {
            continue;
        }
        // F20 — do NOT descend into symlinked directories; they can loop.
        // Pack snapshotting still follows symlinks in `copy_dir_recursive`
        // via fs::copy (which dereferences) so any in-repo symlink still
        // copies correctly, but walking is bounded here.
        let is_symlink = fs::symlink_metadata(&entry_path)
            .map(|m| m.file_type().is_symlink())
            .unwrap_or(false);
        if is_symlink {
            continue;
        }
        if entry_path.is_dir() {
            walk_files_bounded(root_dir, &entry_path, out, depth + 1)?;
        } else if entry_path.is_file() {
            out.push(rel_path(&entry_path, root_dir).replace('\\', "/"));
        }
    }
    Ok(())
}

fn resolve_pattern_matches(repo_root: &Path, pattern: &str) -> Result<Vec<String>> {
    let normalized = pattern.replace('\\', "/");
    if !normalized.contains('*') && !normalized.contains('?') && !normalized.contains('[') {
        let target = repo_root.join(&normalized);
        if target.exists() {
            return Ok(vec![normalized]);
        }
        return Ok(Vec::new());
    }

    let glob = Glob::new(&normalized)
        .with_context(|| format!("Invalid glob pattern in structured pack: {}", pattern))?;
    let mut builder = GlobSetBuilder::new();
    builder.add(glob);
    let matcher = builder
        .build()
        .with_context(|| format!("Failed to compile glob pattern {}", pattern))?;

    let mut files = Vec::new();
    walk_files(repo_root, repo_root, &mut files)?;
    Ok(files
        .into_iter()
        .filter(|file_path| matcher.is_match(file_path))
        .collect())
}

fn validate_pattern_matches(repo_root: &Path, pattern: &str, label: &str) -> Result<()> {
    if resolve_pattern_matches(repo_root, pattern)?.is_empty() {
        return Err(anyhow!(
            "[context-pack] seal failed: {} did not match any files: {}",
            label,
            pattern
        ));
    }
    Ok(())
}

fn validate_structured_layer(repo_root: &Path, current_dir: &Path) -> Result<()> {
    let routes_path = current_dir.join("routes.json");
    if !routes_path.exists() {
        return Ok(());
    }

    let completeness_path = current_dir.join("completeness_contract.json");
    let reporting_path = current_dir.join("reporting_rules.json");

    for required_path in [&completeness_path, &reporting_path] {
        if !required_path.exists() {
            return Err(anyhow!(
                "[context-pack] seal failed: structured mode requires {}",
                rel_path(required_path, repo_root)
            ));
        }
    }

    let routes = read_json(&routes_path)?
        .ok_or_else(|| anyhow!("[context-pack] seal failed: routes.json is missing"))?;
    let completeness = read_json(&completeness_path)?.ok_or_else(|| {
        anyhow!("[context-pack] seal failed: completeness_contract.json is missing")
    })?;
    let reporting = read_json(&reporting_path)?
        .ok_or_else(|| anyhow!("[context-pack] seal failed: reporting_rules.json is missing"))?;

    let routes_map = routes
        .get("task_routes")
        .and_then(|value| value.as_object())
        .ok_or_else(|| anyhow!("[context-pack] seal failed: routes.json must define task_routes"))?;
    let completeness_map = completeness
        .get("task_families")
        .and_then(|value| value.as_object())
        .ok_or_else(|| {
            anyhow!(
                "[context-pack] seal failed: completeness_contract.json must define task_families"
            )
        })?;
    let reporting_map = reporting
        .get("task_families")
        .and_then(|value| value.as_object())
        .ok_or_else(|| {
            anyhow!(
                "[context-pack] seal failed: reporting_rules.json must define task_families"
            )
        })?;

    for task in TASK_FAMILIES {
        let route = routes_map
            .get(*task)
            .and_then(|value| value.as_object())
            .ok_or_else(|| {
                anyhow!(
                    "[context-pack] seal failed: routes.json is missing task_routes.{}",
                    task
                )
            })?;
        let completeness_entry = completeness_map
            .get(*task)
            .and_then(|value| value.as_object())
            .ok_or_else(|| {
                anyhow!(
                    "[context-pack] seal failed: completeness_contract.json is missing task_families.{}",
                    task
                )
            })?;
        let reporting_entry = reporting_map
            .get(*task)
            .and_then(|value| value.as_object())
            .ok_or_else(|| {
                anyhow!(
                    "[context-pack] seal failed: reporting_rules.json is missing task_families.{}",
                    task
                )
            })?;

        let completeness_ref = route
            .get("completeness_ref")
            .and_then(|value| value.as_str())
            .ok_or_else(|| {
                anyhow!(
                    "[context-pack] seal failed: routes.json task_routes.{} must define completeness_ref",
                    task
                )
            })?;
        if completeness_ref != *task {
            return Err(anyhow!(
                "[context-pack] seal failed: routes.json completeness_ref for {} must equal {}",
                task,
                task
            ));
        }
        let reporting_ref = route
            .get("reporting_ref")
            .and_then(|value| value.as_str())
            .ok_or_else(|| {
                anyhow!(
                    "[context-pack] seal failed: routes.json task_routes.{} must define reporting_ref",
                    task
                )
            })?;
        if reporting_ref != *task {
            return Err(anyhow!(
                "[context-pack] seal failed: routes.json reporting_ref for {} must equal {}",
                task,
                task
            ));
        }

        for key in ["pack_read_order", "fallback_files"] {
            if let Some(entries) = route.get(key).and_then(|value| value.as_array()) {
                for entry in entries {
                    let entry = entry.as_str().ok_or_else(|| {
                        anyhow!(
                            "[context-pack] seal failed: routes.json {} entries must be strings",
                            key
                        )
                    })?;
                    let target_path = current_dir.join(entry);
                    if !target_path.exists() {
                        return Err(anyhow!(
                            "[context-pack] seal failed: routes.json references missing pack file {}",
                            entry
                        ));
                    }
                }
            }
        }

        for key in [
            "contractually_required_files",
            "required_file_families",
            "required_chain_members",
        ] {
            if let Some(entries) = completeness_entry.get(key).and_then(|value| value.as_array()) {
                for entry in entries {
                    let pattern = entry.as_str().ok_or_else(|| {
                        anyhow!(
                            "[context-pack] seal failed: completeness_contract.json {} entries must be strings",
                            key
                        )
                    })?;
                    validate_pattern_matches(repo_root, pattern, &format!("completeness_contract.json {}", key))?;
                }
            }
        }

        let optional_budget = reporting_entry
            .get("optional_verify_budget")
            .and_then(|value| value.as_i64())
            .ok_or_else(|| {
                anyhow!(
                    "[context-pack] seal failed: reporting_rules.json optional_verify_budget must be an integer for {}",
                    task
                )
            })?;
        if optional_budget < 0 {
            return Err(anyhow!(
                "[context-pack] seal failed: reporting_rules.json optional_verify_budget must be a non-negative integer for {}",
                task
            ));
        }

        for key in ["groupable_families", "never_enumerate_individually"] {
            if let Some(entries) = reporting_entry.get(key).and_then(|value| value.as_array()) {
                for entry in entries {
                    let pattern = entry.as_str().ok_or_else(|| {
                        anyhow!(
                            "[context-pack] seal failed: reporting_rules.json {} entries must be strings",
                            key
                        )
                    })?;
                    validate_pattern_matches(repo_root, pattern, &format!("reporting_rules.json {}", key))?;
                }
            }
        }
    }

    // Validate search_scope.json if present (not required — backward compat)
    let search_scope_path = current_dir.join("search_scope.json");
    if search_scope_path.exists() {
        let scope = read_json(&search_scope_path)?
            .ok_or_else(|| anyhow!("[context-pack] seal failed: search_scope.json is empty"))?;
        if let Some(scope_families) = scope.get("task_families").and_then(|v| v.as_object()) {
            for task in TASK_FAMILIES {
                if let Some(entry) = scope_families.get(*task).and_then(|v| v.as_object()) {
                    // Validate search_directories exist on disk
                    if let Some(dirs) = entry.get("search_directories").and_then(|v| v.as_array())
                    {
                        for dir in dirs {
                            if let Some(dir_str) = dir.as_str() {
                                // P8/F22 — reject path-traversal / absolute
                                // patterns before doing any filesystem lookup.
                                validate_pack_glob(dir_str, repo_root).map_err(|e| {
                                    anyhow!(
                                        "[context-pack] seal failed: search_scope.json ({}) {}",
                                        task, e
                                    )
                                })?;
                                let dir_path = repo_root.join(dir_str);
                                if !dir_path.exists() {
                                    return Err(anyhow!(
                                        "[context-pack] seal failed: search_scope.json references missing directory {}",
                                        dir_str
                                    ));
                                }
                            }
                        }
                    }
                    // Validate verification_shortcuts reference real files
                    if let Some(shortcuts) =
                        entry.get("verification_shortcuts").and_then(|v| v.as_object())
                    {
                        for (file_path, _) in shortcuts {
                            // P8/F22 — reject path-traversal / absolute
                            // shortcut keys before touching disk.
                            let shortcut_path = file_path.split(':').next().unwrap_or(file_path);
                            validate_pack_glob(shortcut_path, repo_root).map_err(|e| {
                                anyhow!(
                                    "[context-pack] seal failed: search_scope.json ({}) verification_shortcuts {}",
                                    task, e
                                )
                            })?;
                            let file_on_disk = repo_root.join(shortcut_path);
                            if !file_on_disk.exists() {
                                return Err(anyhow!(
                                    "[context-pack] seal failed: search_scope.json verification_shortcuts references missing file {}",
                                    file_path
                                ));
                            }
                        }
                    }
                }
            }
        }
    }

    if let Some(entries) = reporting
        .get("global_rules")
        .and_then(|value| value.get("authoritative_vs_derived_paths"))
        .and_then(|value| value.as_array())
    {
        for entry in entries {
            let entry = entry.as_object().ok_or_else(|| {
                anyhow!(
                    "[context-pack] seal failed: reporting_rules.json authoritative_vs_derived_paths entries must be objects"
                )
            })?;
            let pattern = entry
                .get("pattern")
                .and_then(|value| value.as_str())
                .ok_or_else(|| {
                    anyhow!(
                        "[context-pack] seal failed: reporting_rules.json authoritative_vs_derived_paths entries must contain pattern"
                    )
                })?;
            let role = entry
                .get("role")
                .and_then(|value| value.as_str())
                .ok_or_else(|| {
                    anyhow!(
                        "[context-pack] seal failed: reporting_rules.json authoritative_vs_derived_paths entries must contain role"
                    )
                })?;
            validate_pattern_matches(
                repo_root,
                pattern,
                "reporting_rules.json authoritative_vs_derived_paths",
            )?;
            if role == "authoritative" && pattern.contains("_generated/") {
                return Err(anyhow!(
                    "[context-pack] seal failed: generated files cannot be marked as authoritative edit targets"
                ));
            }
        }
    }

    Ok(())
}

/// Build the routing block content for agent config files.
///
/// P13/F47: every block starts with a mandatory session-start freshness gate
/// so agents compare `head_sha_at_seal` against `git rev-parse HEAD` before
/// any reasoning. This catches the common failure mode where an agent trusts
/// a stale pack after the underlying commit moved.
fn build_context_pack_routing_block(agent_kind: &str) -> String {
    // Shared freshness-gate preamble (F47). Kept identical across agents so
    // every routing block carries the same contract.
    let freshness_gate = "**Before any reasoning, check `.agent-context/current/manifest.json`'s `head_sha_at_seal` vs `git rev-parse HEAD`. If they diverge, warn the user.**";

    if agent_kind == "codex" {
        format!(
            "## Context Pack\n\
             \n\
             {freshness_gate}\n\
             \n\
             When asked to understand this repository:\n\
             \n\
             1. Read `.agent-context/current/00_START_HERE.md`.\n\
             2. Read `.agent-context/current/routes.json`.\n\
             3. Identify the active task type in `routes.json`.\n\
             4. Read the matching entries in `completeness_contract.json`, `reporting_rules.json`, and `search_scope.json`.\n\
             5. Search ONLY within the directories listed in `search_scope.json` for your task type.\n\
             6. Use `verification_shortcuts` to check specific line ranges instead of reading full files.\n\
             7. Do not enumerate files in directories marked `exclude_from_search`.\n\
             8. Do not open repo files before those steps unless a referenced structured file is missing.\n\
             \n\
             If `.agent-context/current/routes.json` is missing, fall back to the markdown pack only."
        )
    } else {
        format!(
            "## Context Pack\n\
             \n\
             {freshness_gate}\n\
             \n\
             **BEFORE starting any task**, read the context pack in this order:\n\
             \n\
             1. `.agent-context/current/00_START_HERE.md` — entrypoint, routing, stop rules\n\
             2. `.agent-context/current/30_BEHAVIORAL_INVARIANTS.md` — change checklists, file families, what NOT to do\n\
             3. `.agent-context/current/20_CODE_MAP.md` — navigation index, tracing flows\n\
             \n\
             Read these three files BEFORE opening any repo source files. Then open only the files the pack identifies as relevant.\n\
             \n\
             For architecture questions, also read `10_SYSTEM_OVERVIEW.md`. For test/deploy questions, also read `40_OPERATIONS_AND_RELEASE.md`."
        )
    }
}

fn build_routes_json() -> String {
    serde_json::to_string_pretty(&json!({
        "schema_version": 1,
        "task_routes": {
            "lookup": {
                "description": "Find a value, threshold, URL, or authoritative file.",
                "pack_read_order": ["00_START_HERE.md", "20_CODE_MAP.md", "reporting_rules.json"],
                "fallback_files": ["30_BEHAVIORAL_INVARIANTS.md"],
                "completeness_ref": "lookup",
                "reporting_ref": "lookup"
            },
            "impact_analysis": {
                "description": "List every file or file family that must change.",
                "pack_read_order": [
                    "00_START_HERE.md",
                    "30_BEHAVIORAL_INVARIANTS.md",
                    "completeness_contract.json",
                    "reporting_rules.json",
                    "20_CODE_MAP.md"
                ],
                "fallback_files": ["10_SYSTEM_OVERVIEW.md"],
                "completeness_ref": "impact_analysis",
                "reporting_ref": "impact_analysis"
            },
            "planning": {
                "description": "Write an implementation plan with files, commands, and validation.",
                "pack_read_order": [
                    "00_START_HERE.md",
                    "20_CODE_MAP.md",
                    "30_BEHAVIORAL_INVARIANTS.md",
                    "completeness_contract.json",
                    "reporting_rules.json"
                ],
                "fallback_files": ["40_OPERATIONS_AND_RELEASE.md"],
                "completeness_ref": "planning",
                "reporting_ref": "planning"
            },
            "diagnosis": {
                "description": "Rank likely root causes and cite the runtime path.",
                "pack_read_order": [
                    "00_START_HERE.md",
                    "10_SYSTEM_OVERVIEW.md",
                    "30_BEHAVIORAL_INVARIANTS.md",
                    "completeness_contract.json",
                    "reporting_rules.json"
                ],
                "fallback_files": ["20_CODE_MAP.md"],
                "completeness_ref": "diagnosis",
                "reporting_ref": "diagnosis"
            }
        }
    })).unwrap_or_else(|_| "{}".to_string()) + "\n"
}

fn build_completeness_contract_json() -> String {
    serde_json::to_string_pretty(&json!({
        "schema_version": 1,
        "task_families": {
            "lookup": {
                "minimum_sufficient_evidence": [
                    "exact answer",
                    "authoritative source path",
                    "one supporting chain only if the task asks for authority"
                ],
                "required_chain_members": [],
                "contractually_required_files": [],
                "required_file_families": []
            },
            "impact_analysis": {
                "minimum_sufficient_evidence": [
                    "complete blast radius",
                    "required file families",
                    "contractually required pass-through layers"
                ],
                "required_chain_members": [],
                "contractually_required_files": [],
                "required_file_families": []
            },
            "planning": {
                "minimum_sufficient_evidence": [
                    "files to create or modify",
                    "commands in order",
                    "validation criteria"
                ],
                "required_chain_members": [],
                "contractually_required_files": [],
                "required_file_families": []
            },
            "diagnosis": {
                "minimum_sufficient_evidence": [
                    "ranked root causes",
                    "runtime path or failure chain",
                    "confirmation method for each cause"
                ],
                "required_chain_members": [],
                "contractually_required_files": [],
                "required_file_families": []
            }
        }
    })).unwrap_or_else(|_| "{}".to_string()) + "\n"
}

fn build_reporting_rules_json() -> String {
    serde_json::to_string_pretty(&json!({
        "schema_version": 1,
        "global_rules": {
            "grouped_reporting_default": true,
            "authoritative_vs_derived_paths": []
        },
        "task_families": {
            "lookup": {
                "optional_verify_budget": 1,
                "stop_after": "Stop after the authoritative source and one optional supporting check.",
                "stop_unless": [
                    "a structured artifact references a missing file",
                    "markdown and structured artifacts disagree",
                    "code contradicts the structured contract",
                    "the task explicitly asks for concrete instances rather than grouped families"
                ],
                "groupable_families": [],
                "never_enumerate_individually": []
            },
            "impact_analysis": {
                "optional_verify_budget": 2,
                "stop_after": "Stop after the blast radius is complete and required families are grouped correctly.",
                "stop_unless": [
                    "a structured artifact references a missing file",
                    "markdown and structured artifacts disagree",
                    "code contradicts the structured contract",
                    "the task explicitly asks for concrete instances rather than grouped families"
                ],
                "groupable_families": [],
                "never_enumerate_individually": []
            },
            "planning": {
                "optional_verify_budget": 2,
                "stop_after": "Stop after the plan is executable without further repo browsing.",
                "stop_unless": [
                    "a structured artifact references a missing file",
                    "markdown and structured artifacts disagree",
                    "code contradicts the structured contract",
                    "the task explicitly asks for concrete instances rather than grouped families"
                ],
                "groupable_families": [],
                "never_enumerate_individually": []
            },
            "diagnosis": {
                "optional_verify_budget": 3,
                "stop_after": "Stop after the ranked runtime chain is established and each cause has a confirmation method.",
                "stop_unless": [
                    "a structured artifact references a missing file",
                    "markdown and structured artifacts disagree",
                    "code contradicts the structured contract",
                    "the task explicitly asks for concrete instances rather than grouped families"
                ],
                "groupable_families": [],
                "never_enumerate_individually": []
            }
        }
    })).unwrap_or_else(|_| "{}".to_string()) + "\n"
}

fn build_search_scope_json() -> String {
    serde_json::to_string_pretty(&json!({
        "schema_version": 1,
        "description": "Search scope boundaries for search-and-verify agents (e.g. Codex). Bounds WHERE to search, not WHEN to stop.",
        "task_families": {
            "lookup": {
                "search_directories": [],
                "exclude_from_search": [],
                "verification_shortcuts": {}
            },
            "impact_analysis": {
                "search_directories": [],
                "exclude_from_search": [],
                "verification_shortcuts": {},
                "derived_file_policy": "Do not list generated/compiled/bundled output files as change targets. They are produced by a build/generate step."
            },
            "planning": {
                "search_directories": [],
                "exclude_from_search": [],
                "verification_shortcuts": {}
            },
            "diagnosis": {
                "search_directories": [],
                "exclude_from_search": [],
                "verification_shortcuts": {}
            }
        }
    }))
    .unwrap_or_else(|_| "{}".to_string())
        + "\n"
}

/// Upsert a managed block into a file (prepend if new, replace if exists).
/// Block is delimited by HTML comment markers.
/// Idempotent — running twice produces the same result.
fn upsert_context_pack_block(
    file_path: &Path,
    block: &str,
    marker_prefix: &str,
) -> Result<()> {
    let start_marker = format!("<!-- {}:start -->", marker_prefix);
    let end_marker = format!("<!-- {}:end -->", marker_prefix);
    let managed_block = format!("{}\n{}\n{}", start_marker, block, end_marker);

    if file_path.exists() {
        let content = fs::read_to_string(file_path)
            .with_context(|| format!("Failed to read {}", file_path.display()))?;
        let new_content = if let (Some(start_idx), Some(end_idx)) =
            (content.find(&start_marker), content.find(&end_marker))
        {
            // Replace existing managed block in place
            format!(
                "{}{}{}",
                &content[..start_idx],
                managed_block,
                &content[end_idx + end_marker.len()..]
            )
        } else {
            // Prepend before existing content
            format!("{}\n\n{}", managed_block, content)
        };
        write_text(file_path, &new_content)?;
    } else {
        write_text(file_path, &format!("{}\n", managed_block))?;
    }
    Ok(())
}

/// P3: placeholder form of the default relevance.json, used by tests and any
/// caller that does not want study/ auto-detection. The `init()` flow uses
/// [`default_relevance_json_with_study`] directly.
#[allow(dead_code)]
fn default_relevance_json() -> String {
    // Pass-0 default kept a legacy `include`/`exclude` pair so the existing
    // freshness path stays working without a zone map. P3 adds a default
    // `zones[]` with study/, docs/, and common dependency files so new repos
    // get zone-aware freshness out of the box.
    default_relevance_json_with_study(false)
}

/// P3: assemble the default relevance.json contents, optionally including a
/// `study/**` zone when the repo actually has a `study/` directory. Callers
/// that do not already know whether `study/` exists should use
/// [`default_relevance_json`], which defaults to the placeholder form used by
/// the old behavior.
fn default_relevance_json_with_study(has_study: bool) -> String {
    let study_zone = if has_study {
        r#"    {"paths": ["study/**", "docs/methodology/**"], "affects": ["10_SYSTEM_OVERVIEW.md", "30_BEHAVIORAL_INVARIANTS.md"]},
"#
    } else {
        // Keep a placeholder entry so the default file has the correct shape
        // even when no study/ directory exists yet. Docs alone is a safe
        // default because most repos have a docs/ tree.
        r#"    {"paths": ["docs/**"], "affects": ["10_SYSTEM_OVERVIEW.md", "30_BEHAVIORAL_INVARIANTS.md"]},
"#
    };
    format!(
        r#"{{
  "include": ["**"],
  "exclude": [
    ".agent-context/**",
    ".git/**",
    "node_modules/**",
    "target/**",
    "dist/**",
    "build/**",
    "vendor/**",
    "tmp/**"
  ],
  "zones": [
{study_zone}    {{"paths": ["src/**", "cli/src/**"], "affects": ["20_CODE_MAP.md", "30_BEHAVIORAL_INVARIANTS.md"]}},
    {{"paths": ["scripts/run_*.py", "scripts/**"], "affects": ["20_CODE_MAP.md", "40_OPERATIONS_AND_RELEASE.md"]}},
    {{"paths": ["pyproject.toml", "Cargo.toml", "package.json", "cli/Cargo.toml"], "affects": ["40_OPERATIONS_AND_RELEASE.md"]}}
  ]
}}
"#
    )
}

fn build_template_start_here(
    repo_name: &str,
    branch: &str,
    head_sha: &str,
    generated_at: &str,
) -> String {
    format!(
        r#"# Context Pack: Start Here

## Snapshot
- Repo: `{repo_name}`
- Branch at generation: `{branch}`
- HEAD commit: `{head_sha}`
- Generated at: `{generated_at}`

## Read Order — MANDATORY before starting work
1. Read this file completely.
2. Read `30_BEHAVIORAL_INVARIANTS.md` — change checklists, file families, negative guidance.
3. Read `20_CODE_MAP.md` — navigation index, tracing flows, extension recipe.

Do NOT open repo source files until you have read steps 1-3. These three files give you enough context to avoid common mistakes (wrong patterns, missing files, deprecated approaches).

Read on demand:
- `10_SYSTEM_OVERVIEW.md` — for architecture or diagnosis tasks.
- `40_OPERATIONS_AND_RELEASE.md` — for test, CI, or deploy tasks.

## Task-Type Routing
**Impact analysis** (list every file that must change): read `30_BEHAVIORAL_INVARIANTS.md` Update Checklist *before* `20_CODE_MAP.md` — the checklist has the full blast radius per change type. CODE_MAP alone is not exhaustive.
**Navigation / lookup** (find a file, find a value): start with `20_CODE_MAP.md` Scope Rule.
**Planning** (add a new feature/module): follow the Extension Recipe in `20_CODE_MAP.md`, then cross-check the BEHAVIORAL_INVARIANTS checklist for that change type.
**Diagnosis** (silent failures, unexpected output): start with `10_SYSTEM_OVERVIEW.md` Silent Failure Modes, then the relevant diagnostic row in `30_BEHAVIORAL_INVARIANTS.md`.

## Structured Routing
- Read `routes.json` after this file to identify the task family before opening repo files.
- Read the matching entries in `completeness_contract.json` and `reporting_rules.json`.
- Use `search_scope.json` for search directory boundaries and verification shortcuts.
- Treat the structured files as authoritative for stop conditions, grouped reporting, and contractual completeness.

## Stop Rules
- Stop when the minimum sufficient evidence for the active task is satisfied.
- Use optional verification sparingly and stay within the task-family verify budget.
- Continue exploring only when a structured file is missing, the pack disagrees with the repo, or the task explicitly asks for concrete instances instead of grouped families.

## Fast Facts
<!-- AGENT: Replace with 3-5 bullets covering product, languages/entry points, quality gate, core risk. -->

## Scope Rule
<!-- AGENT: Provide navigation rules — what to open first for each area of the codebase, what to skip. -->
"#
    )
}

fn build_template_system_overview() -> String {
    r#"# System Overview

<!-- AGENT: Fill by introspecting the repository. -->

## Product Shape
<!-- AGENT: Add package version(s), tracked file count, delivery mechanism(s). -->

## Runtime Architecture
<!-- AGENT: Describe primary execution flow in 3-5 numbered steps. -->

## Silent Failure Modes
<!-- AGENT: List any code paths where a failure produces no error — null return, silent drop, unchecked default.
These are the hardest things to find by reading code and the most valuable to have written down.
Example: "If selector has no match in prompts.yml, resolver returns null — Spark UDF propagates as null row with no error logged."
If none are known, write "None identified." -->

## Command/API Surface
<!-- AGENT: Table | Command/Endpoint | Intent | Primary Source Files | -->

## Tracked Path Density
<!-- AGENT: Summarize top-level directory distribution (git ls-files). -->
"#
    .to_string()
}

fn build_template_code_map() -> String {
    r#"# Code Map

## High-Impact Paths

> **This table is a navigation index, not a complete blast-radius list.** For impact analysis tasks,
> read `30_BEHAVIORAL_INVARIANTS.md` Update Checklist first — it has the full file set per change type.
> Use this table to navigate to those files once you know which are relevant. Verify coverage with grep.

<!-- AGENT: Identify 8-15 key paths. Use [Approach 1], [Approach 2], or [Both] in the Approach column
if the repo has coexisting architectural patterns — omit the column if there is only one approach.
Risk must be filled: use "Silent failure if missed", "KeyError at runtime", "Build drift", etc.
Authority must be filled: "authoritative" (edit this file), "derived" (generated/compiled — do not edit directly), or "reference" (read-only context).
| Path | Approach | What | Why It Matters | Risk | Authority |
| --- | --- | --- | --- | --- | --- | -->

## Quick Lookup Shortcuts
<!-- AGENT: Add 4-6 common lookup patterns. Map intent to exact file and what to look for.
| I need to find... | Open this file | Look for |
| --- | --- | --- | -->

## Cross-Cutting Tracing Flows
<!-- AGENT: For changes that ripple through multiple layers, document the full chain.
Example: "New parameter through call chain: schema → step → client → wrapper → tests"
List files in dependency order so agents trace the change correctly. -->

## Minimum Sufficient Evidence
<!-- AGENT: For each common task family, define the minimum file set needed before an answer is complete.
Keep this short. Example:
- Lookup: authoritative source file + one support check.
- Impact analysis: invariant checklist row + grouped file families.
- Planning: target files + commands + validation.
- Diagnosis: runtime path + likely failure point + confirmation method. -->

## Extension Recipe
<!-- AGENT: Describe how to add a new module/adapter/plugin. List all files that must change together. -->
"#
    .to_string()
}

fn build_template_invariants() -> String {
    r#"# Behavioral Invariants

<!-- AGENT: List contract-level constraints to preserve. -->

## Core Invariants
<!-- AGENT: 3-8 numbered items. Each must be a testable statement, not a description.
Good: "Every selector in a spec must match an entry in prompts.yml — missing match raises ValueError at sync time."
Bad: "Prompts must be valid." -->

## Update Checklist Before Merging Behavior Changes
<!-- AGENT: One row per common change type. The "Files that must change together" column must list
explicit file paths — not descriptions, not directory names. Agents will use these rows as a checklist.
If a missed file causes a silent production failure, say so explicitly in the row.
| Change type | Files that must change together |
| --- | --- | -->

## File Families
<!-- AGENT: List homogeneous file families where all members change the same way.
For each family, state: the glob pattern, how many members, and whether to report as a family
or enumerate individually. Agents should inspect one representative unless divergence is suspected.
Example: "models/assets_gen/_specs/*.prompt.yml (20 files) — report as family, do not enumerate individually."
Example: "models/assets_gen/_generated/*.yml (17 files) — derived, never list as change targets." -->

## Often Reviewed But Not Always Required
<!-- AGENT: List files that are commonly inspected during debugging or planning but are not contractually
required for every change. Separate these from the must-change checklist so agents do not over-read. -->

## Negative Guidance
<!-- AGENT: List patterns that agents commonly over-explore. Be explicit about what NOT to do.
Example: "Do not enumerate _generated/ files individually for impact analysis — they are regenerated by a build step."
Example: "Do not inspect both sync and async wrappers unless the parameter is known to diverge between them."
Example: "Do not open test files to determine blast radius — tests are updated after source, not before." -->
"#
    .to_string()
}

fn build_template_operations() -> String {
    r#"# Operations And Release

## Standard Validation
<!-- AGENT: Add local validation commands (tests, linters, etc.). -->

## CI Checks
<!-- AGENT: List CI workflows/steps that gate merges. -->

## Release Flow
<!-- AGENT: Describe how releases are triggered and what they produce. -->

## Context Pack Maintenance
1. Initialize scaffolding: `chorus context-pack init` (pre-push hook installed automatically)
2. Have your agent fill in the template sections.
3. Seal the pack: `chorus context-pack seal`
4. When freshness warnings appear on push, update content then run `chorus context-pack seal`

## Rollback/Recovery
- Restore latest snapshot: `chorus context-pack rollback`
- Restore named snapshot: `chorus context-pack rollback --snapshot <snapshot_id>`
"#
    .to_string()
}

fn build_guide() -> String {
    r#"# Context Pack Generation Guide

This guide tells AI agents how to fill in the context pack templates.

## Process
1. Read each file in `.agent-context/current/` in numeric order.
2. Fill the markdown templates with repository-derived content.
3. Fill the structured files (`routes.json`, `completeness_contract.json`, `reporting_rules.json`) with deterministic repo-specific rules.
4. After filling all sections, run `chorus context-pack seal` to finalize (manifest + snapshot).

## Quality Criteria
- Content must be factual and verifiable from the repository.
- Prefer concise bullets over long prose.
- Keep total word count under ~2000 words across all files.
- Do not include secrets or credentials.
- Keep structured artifacts explicit and deterministic; do not auto-generate them from freeform prose.
- If unsure, note `TBD` rather than inventing details.

## When to Update
- After significant architectural or contract changes.
- After adding new commands/APIs/features.
- When `chorus context-pack check-freshness` reports stale content.
"#
    .to_string()
}

/// P11-drift / F37 — tag line whose SHA256 value is the canonical fingerprint of
/// the hook body below it. Callers embed this in the hook and the hook's own
/// drift-check preamble re-computes the fingerprint at runtime, excluding this
/// line itself. Kept as a constant so tests can anchor on it without
/// re-deriving the string.
const HOOK_SHA256_TAG: &str = "# AGENT_CONTEXT_HOOK_SHA256=";
/// P11-drift / F37 — opt-out marker. A user who hand-edits the managed hook
/// section deliberately can add this comment (anywhere before the SHA256 tag)
/// to tell the runtime drift check to stay silent.
///
/// Kept as a constant so the hook body and the tests stay aligned on the
/// exact literal. The Rust code only needs the shell-level string; tests
/// assert on it, so `#[allow(dead_code)]` covers the non-test build.
#[allow(dead_code)]
const HOOK_CUSTOM_MARKER: &str = "# AGENT_CONTEXT_HOOK_CUSTOM";

/// P11-drift / F37 — build the agent-chorus pre-push section as written inside
/// `.githooks/pre-push`. The returned string begins with a SHA256 tag line
/// followed by a runtime drift-check preamble; the embedded hash is the
/// SHA256 of the section content *excluding* the tag line, so the at-runtime
/// check can recompute it in place and warn when the installed copy has
/// drifted from what `install-hooks` would produce today.
///
/// Contract:
/// - Runs silently when the installed hook matches the canonical fingerprint.
/// - Warns on stderr (non-fatal) when the hash differs.
/// - Stays silent when the user has inserted the `HOOK_CUSTOM_MARKER`,
///   acknowledging they own any future drift.
fn build_pre_push_hook_section() -> String {
    // P12 / F44 — shell hygiene:
    //   - `set -u` so unset variables fail fast (the `${1:-origin}` default
    //     style is what makes this safe even when git invokes the hook
    //     without both positional args).
    //   - Every `$VAR` interpolation is quoted.
    //   - User-controlled paths pass through a `--` separator to `git diff`
    //     so a path beginning with `-` cannot be interpreted as a flag.
    r#"set -u
remote_name="${1:-origin}"
remote_url="${2:-unknown}"

# P6: when the push range only touches `.agent-context/`, skip the freshness
# cycle entirely. This avoids the noise loop where a code push warns "pack is
# stale", the agent updates the pack and pushes, and the hook re-warns about
# the pack's own commit.
#
# The state file `.agent-context/current/.last_freshness.json` records the
# most recent warning (changed_files + affected_sections + timestamp). On a
# pack-only follow-up push, the hook reads that state and, if the new pack
# paths plausibly cover the affected_sections, prints
# "warning appears addressed: sections [...]".
pack_only_skip() {
  local base="${1:-}"
  local head="${2:-}"

  if [[ -z "$base" || -z "$head" || "$base" == "0000000000000000000000000000000000000000" ]]; then
    return 1
  fi

  # P12/F44 — `--` separator ensures SHA/path values beginning with `-`
  # cannot be parsed as git options.
  local changed
  changed="$(git diff --name-only "${base}..${head}" -- 2>/dev/null || true)"
  if [[ -z "$changed" ]]; then
    return 1
  fi

  # Return non-zero (NOT pack-only) if any path does not start with
  # `.agent-context/`. The inverted grep is important: we must see no
  # non-pack line for this to be a pack-only push.
  if printf '%s\n' "$changed" | grep -vE '^\.agent-context/' >/dev/null 2>&1; then
    return 1
  fi

  echo "[context-pack] pack-only push, skipping freshness check"

  local state_file=".agent-context/current/.last_freshness.json"
  if [[ -f "$state_file" ]]; then
    # Extract affected_sections from the last warning. Prefer jq; fall back to
    # python3 so the check still works on minimal CI images.
    local sections=""
    if command -v jq >/dev/null 2>&1; then
      sections="$(jq -r '.affected_sections[]?' "$state_file" 2>/dev/null | tr '\n' ' ')"
    elif command -v python3 >/dev/null 2>&1; then
      sections="$(python3 -c "import json,sys
try:
    d=json.load(open('$state_file'))
    for s in d.get('affected_sections', []):
        print(s)
except Exception:
    pass" 2>/dev/null | tr '\n' ' ')"
    fi

    if [[ -n "${sections// /}" ]]; then
      # A pack-only push that touches the same section files the prior warning
      # named is treated as "probably addresses the warning". This is
      # best-effort reporting, not a hard guarantee.
      local covered=""
      local missing=""
      for section in $sections; do
        if printf '%s\n' "$changed" | grep -F ".agent-context/current/${section}" >/dev/null 2>&1; then
          covered="${covered}${section} "
        else
          missing="${missing}${section} "
        fi
      done
      if [[ -n "${covered// /}" ]]; then
        echo "[context-pack] warning appears addressed: sections [${covered% }] updated"
      fi
      if [[ -n "${missing// /}" ]]; then
        echo "[context-pack] note: sections still referenced by last warning: [${missing% }]"
      fi
    fi
  fi

  return 0
}

run_context_sync() {
  local local_ref="${1:-}"
  local local_sha="${2:-}"
  local remote_ref="${3:-}"
  local remote_sha="${4:-}"

  if command -v chorus >/dev/null 2>&1; then
    chorus context-pack sync-main \
      --local-ref "$local_ref" \
      --local-sha "$local_sha" \
      --remote-ref "$remote_ref" \
      --remote-sha "$remote_sha"
    return
  fi

  if [[ -f scripts/read_session.cjs ]]; then
    node scripts/read_session.cjs context-pack sync-main \
      --local-ref "$local_ref" \
      --local-sha "$local_sha" \
      --remote-ref "$remote_ref" \
      --remote-sha "$remote_sha"
    return
  fi

  echo "[context-pack] WARN: chorus command not found; skipping context-pack sync"
}

while read -r local_ref local_sha remote_ref remote_sha; do
  if [[ "$local_ref" == "refs/heads/main" || "$remote_ref" == "refs/heads/main" ]]; then
    echo "[context-pack] validating main push for ${remote_name} (${remote_url})"
    if pack_only_skip "$remote_sha" "$local_sha"; then
      continue
    fi
    run_context_sync "$local_ref" "$local_sha" "$remote_ref" "$remote_sha"
  fi
done"#
    .to_string()
}

/// P1 — shell body for the post-commit-reconcile hook. Runs
/// `chorus agent-context post-commit-reconcile` only when the just-landed
/// commit touched `.agent-context/**`. Silent no-op when the chorus CLI is
/// unavailable (so the hook never blocks a normal `git commit`).
fn build_post_commit_hook_section() -> String {
    r#"# Only reconcile when the commit touched pack content; checking the full
# `.agent-context/` tree keeps this O(1) for the common non-pack commit.
if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -q '^\.agent-context/'; then
  if command -v chorus >/dev/null 2>&1; then
    chorus agent-context post-commit-reconcile >/dev/null 2>&1 || true
  fi
fi"#
    .to_string()
}

// ============================================================================
// P2 — Structural verifier
//
// Extends `verify` with structural checks that complement Pass-0's byte-level
// integrity. Ported from team_skills' `scripts/verify_context_pack.py` (the
// machine-checkable pack-author contract) and extended with four P1-baseline
// drift checks:
//
//   1. `family_counts` drift   — resolve globs NOW, compare vs manifest
//   2. `declared_counts` drift — re-extract prose numbers, name stale files
//   3. `shortcut_signatures`   — re-parse signatures, `SIGNATURE_DRIFT` per fn
//   4. `dependencies_snapshot` — re-hash deps files, point at 40_OPERATIONS
//
// Ported from team_skills:
//   a. Template-marker absence in pack JSON ({name}, {domain}, {module}, REPLACE, <!-- AGENT:)
//   b. `completeness_contract.json` glob existence + cardinality >=1
//   c. `search_scope.json` verification_shortcuts `look_for` present in file
//   d. Routing files (CLAUDE.md/GEMINI.md/AGENTS.md) reference canonical paths
//
// Plus `contractually_required_files[]` existence (catches deletions; #5).
//
// Design notes:
// - Warnings are non-fatal by default so existing tests / already-sealed packs
//   don't regress. CI JSON surfaces them under `structural_warnings[]` with a
//   stable shape: `{kind, message, affected_pack_files: []}`.
// - `affected_pack_files` is derived directly from the check category per the
//   P3 zone-map contract: template-marker names the file; family-count drift
//   points at 20_CODE_MAP.md + 40_OPERATIONS_AND_RELEASE.md; signature drift
//   points at 20_CODE_MAP.md + search_scope.json; deps drift points at
//   40_OPERATIONS_AND_RELEASE.md; etc. This is the fixed P3-style affects[]
//   list; a future pass will read zones from `relevance.json` when present.
// - Each helper is intentionally small and pure (manifest JSON + filesystem)
//   so tests can exercise a single check without standing up the full verify.
// ============================================================================

/// P2 — structural warning emitted by `run_structural_checks`. Consumers
/// (human output, CI JSON) render these identically; the `kind` discriminator
/// is the machine-readable category.
#[derive(Clone, Debug)]
struct StructuralWarning {
    /// Upper-snake-case category, e.g. "TEMPLATE_MARKER", "FAMILY_COUNT_DRIFT",
    /// "SIGNATURE_DRIFT", "DECLARED_COUNT_DRIFT", "DEPENDENCIES_DRIFT",
    /// "CONTRACT_GLOB_MISS", "LOOK_FOR_MISSING", "ROUTING_MISSING_REF",
    /// "CONTRACT_REQUIRED_FILE_MISSING".
    kind: String,
    message: String,
    /// Pack filenames (repo-relative under `.agent-context/current/`) whose
    /// prose probably needs updating to clear this warning. Empty is a valid
    /// value (e.g. when the check names a repo file, not a pack file).
    affected_pack_files: Vec<String>,
}

/// P2 — template markers that must never appear in sealed pack JSON. Matches
/// the team_skills reference + the Claude-specific scaffolding tag we emit.
fn template_marker_candidates() -> &'static [&'static str] {
    &["{name}", "{domain}", "{module}", "REPLACE", "<!-- AGENT:"]
}

/// P2 check (a): fail on template markers left in any `*.json` file under the
/// pack's current directory.
fn check_template_markers(current_dir: &Path, out: &mut Vec<StructuralWarning>) {
    let entries = match fs::read_dir(current_dir) {
        Ok(e) => e,
        Err(_) => return,
    };
    let mut paths: Vec<PathBuf> = Vec::new();
    for e in entries.flatten() {
        let p = e.path();
        if p.is_file() && p.extension().and_then(|x| x.to_str()) == Some("json") {
            paths.push(p);
        }
    }
    paths.sort();
    for path in paths {
        let text = match fs::read_to_string(&path) {
            Ok(t) => t,
            Err(_) => continue,
        };
        for marker in template_marker_candidates() {
            if text.contains(marker) {
                let fname = path
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("")
                    .to_string();
                out.push(StructuralWarning {
                    kind: "TEMPLATE_MARKER".to_string(),
                    message: format!(
                        "template marker '{}' found in pack JSON: {}",
                        marker, fname
                    ),
                    affected_pack_files: vec![fname.clone()],
                });
            }
        }
    }
}

/// P2 check (b) + #5: `completeness_contract.json` required_file_families[]
/// globs must still resolve to at least one file AND
/// `contractually_required_files[]` entries must still exist on disk.
fn check_contract_files_exist(
    repo_root: &Path,
    current_dir: &Path,
    out: &mut Vec<StructuralWarning>,
) {
    let contract_path = current_dir.join("completeness_contract.json");
    let Ok(Some(contract)) = read_json(&contract_path) else {
        return;
    };
    // Support both the flat `contracts` shape (team_skills reference) and
    // the `task_families` shape (chorus scaffolding). Walk whichever is
    // present so the check stays agnostic.
    let mut entries: Vec<(String, &serde_json::Map<String, Value>)> = Vec::new();
    if let Some(map) = contract.get("contracts").and_then(|v| v.as_object()) {
        for (k, v) in map {
            if let Some(obj) = v.as_object() {
                entries.push((k.clone(), obj));
            }
        }
    }
    if let Some(map) = contract.get("task_families").and_then(|v| v.as_object()) {
        for (k, v) in map {
            if let Some(obj) = v.as_object() {
                entries.push((k.clone(), obj));
            }
        }
    }

    for (name, entry) in entries {
        if let Some(list) = entry
            .get("contractually_required_files")
            .and_then(|v| v.as_array())
        {
            for item in list {
                if let Some(rel) = item.as_str() {
                    if !repo_root.join(rel).exists() {
                        out.push(StructuralWarning {
                            kind: "CONTRACT_REQUIRED_FILE_MISSING".to_string(),
                            message: format!(
                                "completeness_contract '{name}': required file missing on disk: {rel}"
                            ),
                            affected_pack_files: vec![
                                "completeness_contract.json".to_string(),
                                "20_CODE_MAP.md".to_string(),
                            ],
                        });
                    }
                }
            }
        }
        if let Some(list) = entry.get("required_file_families").and_then(|v| v.as_array()) {
            for item in list {
                if let Some(pattern) = item.as_str() {
                    // Template markers in globs are their own failure mode
                    // (redundant with check_template_markers but cheap to
                    // call here for clarity of error message).
                    let has_marker = template_marker_candidates()
                        .iter()
                        .any(|m| pattern.contains(m));
                    if has_marker {
                        out.push(StructuralWarning {
                            kind: "CONTRACT_GLOB_MISS".to_string(),
                            message: format!(
                                "completeness_contract '{name}': template marker in glob: {pattern}"
                            ),
                            affected_pack_files: vec![
                                "completeness_contract.json".to_string(),
                            ],
                        });
                        continue;
                    }
                    let matches =
                        resolve_pattern_matches(repo_root, pattern).unwrap_or_default();
                    if matches.is_empty() {
                        out.push(StructuralWarning {
                            kind: "CONTRACT_GLOB_MISS".to_string(),
                            message: format!(
                                "completeness_contract '{name}': glob matches no files: {pattern}"
                            ),
                            affected_pack_files: vec![
                                "completeness_contract.json".to_string(),
                                "20_CODE_MAP.md".to_string(),
                            ],
                        });
                    }
                }
            }
        }
    }
}

/// P12 / F40 — strip line and block comments from `text` based on the file
/// extension. Called by [`check_verification_shortcuts_look_for`] before
/// matching `look_for` so a string that only appears inside a comment is
/// surfaced as drift rather than a spurious pass.
///
/// Language handling:
/// - `.py`: `#...$` line comments and `"""..."""` / `'''...'''` docstrings
/// - `.rs`: `//...$` line comments and `/*...*/` block comments
/// - `.ts` / `.tsx` / `.js` / `.jsx`: `//...$` line comments and
///   `/*...*/` block comments
/// - Any other (or unknown) extension: returns `text` unchanged. Callers
///   then fall back to the existing substring contract — consumers of older
///   schemas with unusual extensions keep working.
///
/// This is a regex-free implementation: the nested docstring/block-comment
/// contract is easier to reason about as a small state machine than as a
/// multi-line regex, and it keeps us from pulling `regex` into the core
/// structural-check path for a single feature.
fn strip_source_comments(text: &str, extension: &str) -> String {
    match extension {
        "py" => strip_python_comments(text),
        "rs" | "ts" | "tsx" | "js" | "jsx" | "cjs" | "mjs" => strip_c_style_comments(text),
        _ => text.to_string(),
    }
}

/// Python-specific variant: strip `#...$` line comments and `"""..."""` /
/// `'''..'''` docstring-style triple-quoted string blocks. This is a
/// best-effort scanner — it does not understand nested expressions inside
/// f-strings, which is fine for look_for drift detection where we only care
/// about surfacing comment-only matches, not exact lexing.
fn strip_python_comments(text: &str) -> String {
    let bytes = text.as_bytes();
    let mut out = String::with_capacity(text.len());
    let mut i = 0;
    while i < bytes.len() {
        let c = bytes[i];
        // Triple-quoted block: consume until the matching closer.
        if i + 2 < bytes.len() && (c == b'"' || c == b'\'')
            && bytes[i + 1] == c
            && bytes[i + 2] == c
        {
            let quote = c;
            i += 3;
            while i + 2 < bytes.len() {
                if bytes[i] == quote && bytes[i + 1] == quote && bytes[i + 2] == quote {
                    i += 3;
                    break;
                }
                // Preserve newlines so line-anchored searches still work.
                if bytes[i] == b'\n' {
                    out.push('\n');
                }
                i += 1;
            }
            // Reached EOF mid-docstring — stop consuming.
            if i + 2 >= bytes.len() {
                break;
            }
            continue;
        }
        // Line comment: drop everything up to the next newline.
        if c == b'#' {
            while i < bytes.len() && bytes[i] != b'\n' {
                i += 1;
            }
            continue;
        }
        out.push(c as char);
        i += 1;
    }
    out
}

/// C-family variant: strip `//...$` line comments and `/*...*/` block
/// comments. Newlines inside block comments are preserved so line numbers
/// downstream (if any) stay aligned. Does not track strings, which is safe
/// for look_for drift detection since a `//` inside a string literal still
/// represents "this substring appears in real code-ish context" — users who
/// need perfect lexing can fall back to `look_for_regex`.
fn strip_c_style_comments(text: &str) -> String {
    let bytes = text.as_bytes();
    let mut out = String::with_capacity(text.len());
    let mut i = 0;
    while i < bytes.len() {
        let c = bytes[i];
        if c == b'/' && i + 1 < bytes.len() {
            let next = bytes[i + 1];
            if next == b'/' {
                while i < bytes.len() && bytes[i] != b'\n' {
                    i += 1;
                }
                continue;
            }
            if next == b'*' {
                i += 2;
                while i + 1 < bytes.len() {
                    if bytes[i] == b'*' && bytes[i + 1] == b'/' {
                        i += 2;
                        break;
                    }
                    if bytes[i] == b'\n' {
                        out.push('\n');
                    }
                    i += 1;
                }
                continue;
            }
        }
        out.push(c as char);
        i += 1;
    }
    out
}

/// P12 / F40 — true when `extension` is one of the languages
/// [`strip_source_comments`] actually trims. Used to decide whether a
/// `look_for` failure should report "matches only comments" instead of the
/// generic "string not found" message.
fn extension_supports_comment_strip(extension: &str) -> bool {
    matches!(
        extension,
        "py" | "rs" | "ts" | "tsx" | "js" | "jsx" | "cjs" | "mjs"
    )
}

/// Extract a lowercase file extension (without the `.`) from a path string.
/// Returns empty when no extension is present.
fn path_extension(rel: &str) -> String {
    Path::new(rel)
        .extension()
        .and_then(|ext| ext.to_str())
        .map(|s| s.to_ascii_lowercase())
        .unwrap_or_default()
}

/// P2 check (c) / P12 F40: every `search_scope.json` verification_shortcut
/// with a `look_for` string (or new `look_for_regex`) must match inside the
/// referenced file AFTER comments are stripped for supported languages.
///
/// Handles both shortcut shapes:
/// - team_skills: `verification_shortcuts: [{file, look_for, [look_for_regex]}]`
/// - chorus default scaffold: `verification_shortcuts: { "path": "hint" }`
///   (no `look_for` — we silently skip; there's nothing to verify).
///
/// `look_for_regex` takes precedence over `look_for` when both are present.
/// Invalid regex falls through to a dedicated warning so the author can fix
/// the pattern without a mysterious "no match" error.
fn check_verification_shortcuts_look_for(
    repo_root: &Path,
    current_dir: &Path,
    out: &mut Vec<StructuralWarning>,
) {
    let scope_path = current_dir.join("search_scope.json");
    let Ok(Some(scope)) = read_json(&scope_path) else {
        return;
    };
    let Some(families) = scope.get("task_families").and_then(|v| v.as_object()) else {
        return;
    };
    for (family, data) in families {
        // Array form (team_skills).
        if let Some(arr) = data.get("verification_shortcuts").and_then(|v| v.as_array()) {
            for shortcut in arr {
                let Some(obj) = shortcut.as_object() else { continue };
                let Some(rel) = obj.get("file").and_then(|v| v.as_str()) else {
                    out.push(StructuralWarning {
                        kind: "LOOK_FOR_MISSING".to_string(),
                        message: format!(
                            "search_scope {family}: verification shortcut missing 'file'"
                        ),
                        affected_pack_files: vec!["search_scope.json".to_string()],
                    });
                    continue;
                };
                let look_for = obj
                    .get("look_for")
                    .and_then(|v| v.as_str())
                    .unwrap_or("");
                let look_for_regex = obj
                    .get("look_for_regex")
                    .and_then(|v| v.as_str())
                    .unwrap_or("");
                let path = repo_root.join(rel);
                if !path.exists() {
                    out.push(StructuralWarning {
                        kind: "LOOK_FOR_MISSING".to_string(),
                        message: format!(
                            "search_scope {family}: missing verification file: {rel}"
                        ),
                        affected_pack_files: vec!["search_scope.json".to_string()],
                    });
                    continue;
                }
                if look_for.is_empty() && look_for_regex.is_empty() {
                    continue;
                }
                let raw = fs::read_to_string(&path).unwrap_or_default();
                let extension = path_extension(rel);
                let stripped = strip_source_comments(&raw, &extension);

                // look_for_regex takes precedence when present.
                if !look_for_regex.is_empty() {
                    match regex_lite_match(&stripped, look_for_regex) {
                        Ok(true) => continue,
                        Ok(false) => {
                            out.push(StructuralWarning {
                                kind: "LOOK_FOR_MISSING".to_string(),
                                message: format!(
                                    "search_scope {family}: look_for_regex did not match in {rel}: {look_for_regex}"
                                ),
                                affected_pack_files: vec![
                                    "search_scope.json".to_string(),
                                    "20_CODE_MAP.md".to_string(),
                                ],
                            });
                        }
                        Err(msg) => {
                            out.push(StructuralWarning {
                                kind: "LOOK_FOR_MISSING".to_string(),
                                message: format!(
                                    "search_scope {family}: look_for_regex invalid for {rel}: {msg}"
                                ),
                                affected_pack_files: vec!["search_scope.json".to_string()],
                            });
                        }
                    }
                    continue;
                }

                // Substring path.
                if stripped.contains(look_for) {
                    continue;
                }
                // Distinguish "only matches in comments" from "doesn't match at all"
                // so authors can tell whether the pack pointer is stale or wrong.
                let message = if extension_supports_comment_strip(&extension)
                    && raw.contains(look_for)
                {
                    format!(
                        "search_scope {family}: look_for matches only comments in {rel}: {look_for}"
                    )
                } else {
                    format!(
                        "search_scope {family}: look_for string not found in {rel}: {look_for}"
                    )
                };
                out.push(StructuralWarning {
                    kind: "LOOK_FOR_MISSING".to_string(),
                    message,
                    affected_pack_files: vec![
                        "search_scope.json".to_string(),
                        "20_CODE_MAP.md".to_string(),
                    ],
                });
            }
        }
        // Object form (chorus scaffold) — nothing to verify at `look_for` level,
        // but catching a missing file is still useful. We intentionally do not
        // emit a redundant existence error here because `validate_structured_layer`
        // already runs this check at seal time.
    }
}

/// P12 / F40 — minimalist regex matcher used by `check_verification_shortcuts_look_for`.
/// We intentionally avoid pulling a new `regex` dependency into the CLI crate
/// for the single look_for_regex feature. The subset we need here covers
/// literal characters, character classes `[...]`, escapes `\d \w \s \. \\`,
/// anchors `^$`, and repetition `* + ? {n,m}`. We lean on the already-present
/// `globset`-style semantics: any unsupported construct falls through to
/// `Err` with a human-readable message so authors get a clear diagnostic.
///
/// For the P12 contract (comment-aware look_for match, regex upgrade path)
/// this scope is sufficient — users who need full PCRE can still author
/// the substring form. Returns `Ok(true)` on match, `Ok(false)` on clean
/// non-match, `Err(msg)` when the pattern cannot be compiled.
fn regex_lite_match(text: &str, pattern: &str) -> std::result::Result<bool, String> {
    let compiled = compile_regex_lite(pattern)?;
    Ok(regex_lite_search(&compiled, text))
}

#[derive(Debug, Clone)]
enum RegexLiteTok {
    /// Literal character run.
    Literal(String),
    /// Character class; `invert` flips ASCII range membership.
    Class { chars: Vec<char>, ranges: Vec<(char, char)>, invert: bool },
    /// Any char (.)
    AnyChar,
    /// Digit, word, whitespace shorthand classes.
    Digit,
    Word,
    Space,
    /// ^ / $ anchors.
    LineStart,
    LineEnd,
}

#[derive(Debug, Clone)]
struct RegexLiteAtom {
    tok: RegexLiteTok,
    /// Minimum repetitions.
    min: usize,
    /// Maximum repetitions, `None` for unbounded.
    max: Option<usize>,
}

fn compile_regex_lite(pattern: &str) -> std::result::Result<Vec<RegexLiteAtom>, String> {
    let mut atoms: Vec<RegexLiteAtom> = Vec::new();
    let bytes: Vec<char> = pattern.chars().collect();
    let mut i = 0;
    while i < bytes.len() {
        let ch = bytes[i];
        let tok = match ch {
            '.' => {
                i += 1;
                RegexLiteTok::AnyChar
            }
            '^' => {
                i += 1;
                RegexLiteTok::LineStart
            }
            '$' => {
                i += 1;
                RegexLiteTok::LineEnd
            }
            '(' | ')' | '|' => {
                return Err(format!("unsupported construct `{ch}` (grouping/alternation not implemented)"));
            }
            '[' => {
                // Character class.
                i += 1;
                let mut invert = false;
                if i < bytes.len() && bytes[i] == '^' {
                    invert = true;
                    i += 1;
                }
                let mut chars = Vec::new();
                let mut ranges = Vec::new();
                let mut closed = false;
                while i < bytes.len() {
                    if bytes[i] == ']' {
                        closed = true;
                        i += 1;
                        break;
                    }
                    let c = if bytes[i] == '\\' && i + 1 < bytes.len() {
                        i += 1;
                        bytes[i]
                    } else {
                        bytes[i]
                    };
                    // Range like a-z.
                    if i + 2 < bytes.len() && bytes[i + 1] == '-' && bytes[i + 2] != ']' {
                        let end = if bytes[i + 2] == '\\' && i + 3 < bytes.len() {
                            i += 1;
                            bytes[i + 2]
                        } else {
                            bytes[i + 2]
                        };
                        ranges.push((c, end));
                        i += 3;
                    } else {
                        chars.push(c);
                        i += 1;
                    }
                }
                if !closed {
                    return Err("unterminated character class".to_string());
                }
                RegexLiteTok::Class { chars, ranges, invert }
            }
            '\\' => {
                if i + 1 >= bytes.len() {
                    return Err("trailing backslash".to_string());
                }
                let esc = bytes[i + 1];
                i += 2;
                match esc {
                    'd' => RegexLiteTok::Digit,
                    'w' => RegexLiteTok::Word,
                    's' => RegexLiteTok::Space,
                    '.' | '\\' | '(' | ')' | '[' | ']' | '{' | '}' | '+' | '*' | '?' | '|' | '^' | '$' => {
                        RegexLiteTok::Literal(esc.to_string())
                    }
                    'n' => RegexLiteTok::Literal("\n".to_string()),
                    't' => RegexLiteTok::Literal("\t".to_string()),
                    other => return Err(format!("unsupported escape `\\{other}`")),
                }
            }
            other => {
                i += 1;
                RegexLiteTok::Literal(other.to_string())
            }
        };

        let (min, max, consumed) = parse_regex_quantifier(&bytes, i)?;
        i += consumed;
        atoms.push(RegexLiteAtom { tok, min, max });
    }
    Ok(atoms)
}

fn parse_regex_quantifier(
    bytes: &[char],
    i: usize,
) -> std::result::Result<(usize, Option<usize>, usize), String> {
    if i >= bytes.len() {
        return Ok((1, Some(1), 0));
    }
    match bytes[i] {
        '*' => Ok((0, None, 1)),
        '+' => Ok((1, None, 1)),
        '?' => Ok((0, Some(1), 1)),
        '{' => {
            // Parse {n} or {n,m}.
            let mut j = i + 1;
            let mut n_str = String::new();
            while j < bytes.len() && bytes[j].is_ascii_digit() {
                n_str.push(bytes[j]);
                j += 1;
            }
            if n_str.is_empty() {
                return Err("empty {} quantifier".to_string());
            }
            let n: usize = n_str.parse().map_err(|_| "bad {} quantifier".to_string())?;
            if j < bytes.len() && bytes[j] == '}' {
                return Ok((n, Some(n), j + 1 - i));
            }
            if j < bytes.len() && bytes[j] == ',' {
                j += 1;
                let mut m_str = String::new();
                while j < bytes.len() && bytes[j].is_ascii_digit() {
                    m_str.push(bytes[j]);
                    j += 1;
                }
                if j >= bytes.len() || bytes[j] != '}' {
                    return Err("unterminated {} quantifier".to_string());
                }
                let max = if m_str.is_empty() {
                    None
                } else {
                    Some(m_str.parse().map_err(|_| "bad {} quantifier".to_string())?)
                };
                return Ok((n, max, j + 1 - i));
            }
            Err("unterminated {} quantifier".to_string())
        }
        _ => Ok((1, Some(1), 0)),
    }
}

fn regex_lite_search(atoms: &[RegexLiteAtom], text: &str) -> bool {
    let chars: Vec<char> = text.chars().collect();
    for start in 0..=chars.len() {
        if regex_lite_match_at(atoms, 0, &chars, start).is_some() {
            return true;
        }
    }
    false
}

/// Returns `Some(end_pos)` on match, `None` on no-match. Recursive matcher:
/// enumerates greedy-first repetition counts and backtracks only when the
/// rest of the pattern fails. Compact enough to stay readable for the P12
/// scope; a future pass should replace this with the `regex` crate when we
/// want broader pattern support.
fn regex_lite_match_at(
    atoms: &[RegexLiteAtom],
    atom_idx: usize,
    text: &[char],
    pos: usize,
) -> Option<usize> {
    if atom_idx >= atoms.len() {
        return Some(pos);
    }
    let atom = &atoms[atom_idx];
    // Anchors do not consume characters.
    if let RegexLiteTok::LineStart = atom.tok {
        // Match at pos 0 or after a newline.
        if pos == 0 || (pos > 0 && text[pos - 1] == '\n') {
            return regex_lite_match_at(atoms, atom_idx + 1, text, pos);
        }
        return None;
    }
    if let RegexLiteTok::LineEnd = atom.tok {
        if pos == text.len() || text[pos] == '\n' {
            return regex_lite_match_at(atoms, atom_idx + 1, text, pos);
        }
        return None;
    }
    // Greedy: consume up to max, then try each fallback count down to min.
    let mut count = 0;
    let mut cursor = pos;
    while cursor < text.len() && atom.max.map_or(true, |m| count < m)
        && atom_matches_char(&atom.tok, text[cursor])
    {
        cursor += 1;
        count += 1;
    }
    loop {
        if count >= atom.min {
            if let Some(end) = regex_lite_match_at(atoms, atom_idx + 1, text, cursor) {
                return Some(end);
            }
        }
        if count == 0 {
            break;
        }
        count -= 1;
        cursor -= 1;
    }
    None
}

fn atom_matches_char(tok: &RegexLiteTok, c: char) -> bool {
    match tok {
        RegexLiteTok::Literal(s) => s.chars().next().map_or(false, |lit| lit == c),
        RegexLiteTok::AnyChar => c != '\n',
        RegexLiteTok::Digit => c.is_ascii_digit(),
        RegexLiteTok::Word => c.is_ascii_alphanumeric() || c == '_',
        RegexLiteTok::Space => c.is_whitespace(),
        RegexLiteTok::Class { chars, ranges, invert } => {
            let mut hit = chars.contains(&c);
            if !hit {
                for (lo, hi) in ranges {
                    if c >= *lo && c <= *hi {
                        hit = true;
                        break;
                    }
                }
            }
            if *invert { !hit } else { hit }
        }
        _ => false,
    }
}

/// P2 check (d): routing files (CLAUDE.md / GEMINI.md / AGENTS.md) must
/// reference the canonical pack paths. Missing files are not errors (repos
/// may scaffold only a subset of agents); *present* files must be correct.
fn check_routing_files(repo_root: &Path, out: &mut Vec<StructuralWarning>) {
    let claude = repo_root.join("CLAUDE.md");
    let gemini = repo_root.join("GEMINI.md");
    let agents = repo_root.join("AGENTS.md");

    if claude.exists() {
        let text = fs::read_to_string(&claude).unwrap_or_default();
        if !text.contains("00_START_HERE.md") || !text.contains("30_BEHAVIORAL_INVARIANTS.md") {
            out.push(StructuralWarning {
                kind: "ROUTING_MISSING_REF".to_string(),
                message:
                    "CLAUDE.md does not reference canonical pack paths (00_START_HERE.md + 30_BEHAVIORAL_INVARIANTS.md)"
                        .to_string(),
                affected_pack_files: vec!["CLAUDE.md".to_string()],
            });
        }
    }
    if gemini.exists() {
        let text = fs::read_to_string(&gemini).unwrap_or_default();
        if !text.contains("00_START_HERE.md") || !text.contains("30_BEHAVIORAL_INVARIANTS.md") {
            out.push(StructuralWarning {
                kind: "ROUTING_MISSING_REF".to_string(),
                message:
                    "GEMINI.md does not reference canonical pack paths (00_START_HERE.md + 30_BEHAVIORAL_INVARIANTS.md)"
                        .to_string(),
                affected_pack_files: vec!["GEMINI.md".to_string()],
            });
        }
    }
    if agents.exists() {
        let text = fs::read_to_string(&agents).unwrap_or_default();
        // AGENTS.md is the search-and-verify routing: expect routes.json reference.
        if !text.contains("routes.json") {
            out.push(StructuralWarning {
                kind: "ROUTING_MISSING_REF".to_string(),
                message: "AGENTS.md does not reference canonical routes.json".to_string(),
                affected_pack_files: vec!["AGENTS.md".to_string()],
            });
        }
    }
}

/// P2 check 1: compare manifest's stored `family_counts` (from seal) against a
/// live resolution. Emits one `FAMILY_COUNT_DRIFT` warning per pattern whose
/// count changed; names the prose files that most commonly quote code-family
/// counts (20_CODE_MAP.md + 40_OPERATIONS_AND_RELEASE.md).
fn check_family_counts_drift(
    manifest: &Value,
    repo_root: &Path,
    current_dir: &Path,
    out: &mut Vec<StructuralWarning>,
) {
    let stored = manifest
        .get("family_counts")
        .and_then(|v| v.as_object())
        .cloned()
        .unwrap_or_default();
    if stored.is_empty() {
        return;
    }
    let live = resolve_family_counts(repo_root, current_dir);
    // Iterate in BTreeSet order over the union of keys so output is stable.
    let mut keys: BTreeSet<String> = BTreeSet::new();
    for k in stored.keys() {
        keys.insert(k.clone());
    }
    for k in live.keys() {
        keys.insert(k.clone());
    }
    for k in keys {
        let stored_n = stored.get(&k).and_then(|v| v.as_u64()).unwrap_or(0);
        let live_n = live.get(&k).copied().unwrap_or(0) as u64;
        if stored_n == live_n {
            continue;
        }
        out.push(StructuralWarning {
            kind: "FAMILY_COUNT_DRIFT".to_string(),
            message: format!(
                "family_counts drift for '{k}': manifest says {stored_n}, live resolves to {live_n} (delta {delta})",
                delta = (live_n as i64) - (stored_n as i64)
            ),
            affected_pack_files: vec![
                "20_CODE_MAP.md".to_string(),
                "40_OPERATIONS_AND_RELEASE.md".to_string(),
            ],
        });
    }
}

/// P2 check 2: cross-check pack prose numeric claims against live authoritative
/// counts resolved from globs. Emits one `DECLARED_COUNT_DRIFT` warning per
/// stale (noun, count) tuple, naming every pack file+line that parrots the
/// stale number.
///
/// The authoritative source is live `family_counts` (re-resolved against
/// disk): for each noun we heuristically find the glob whose pattern
/// mentions that noun (e.g. noun "scripts" -> pattern "scripts/run_*.py").
/// When prose says N and the matched glob resolves to M, every prose line
/// saying N is reported as stale.
///
/// Nouns without a matching glob pattern fall back to comparing live prose
/// against the manifest's sealed `declared_counts` for the same noun — if
/// they disagree, we name the stale (older-seal) lines so the reviewer can
/// pick the authoritative answer.
fn check_declared_counts_drift(
    manifest: &Value,
    repo_root: &Path,
    current_dir: &Path,
    out: &mut Vec<StructuralWarning>,
) {
    let live_prose = extract_declared_counts(current_dir);
    if live_prose.is_empty() {
        return;
    }
    let stored_prose = manifest
        .get("declared_counts")
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();

    // Authoritative counts: re-resolve globs NOW. This is what "the repo
    // actually looks like" at verify time. Keyed by the glob pattern.
    let live_families = resolve_family_counts(repo_root, current_dir);

    // Map each noun to an authoritative count when a glob pattern references
    // the noun word. Matching is case-insensitive and checks for the noun or
    // its singular form in the pattern text (e.g. "scripts" -> "scripts/").
    fn noun_matches_pattern(noun: &str, pattern: &str) -> bool {
        let p = pattern.to_ascii_lowercase();
        let n = noun.to_ascii_lowercase();
        if p.contains(&n) {
            return true;
        }
        // Try the singular form (strip trailing "s"/"es").
        let trimmed = n.trim_end_matches('s');
        if trimmed.len() < n.len() && !trimmed.is_empty() && p.contains(trimmed) {
            return true;
        }
        false
    }

    let resolve_auth_count = |noun: &str| -> Option<u64> {
        for (pattern, count) in &live_families {
            if noun_matches_pattern(noun, pattern) {
                return Some(*count as u64);
            }
        }
        None
    };

    // Walk live prose claims: for each (noun, count, file, line), check
    // whether an authoritative answer exists and disagrees.
    use std::collections::BTreeMap;
    // (noun, stale_count, authoritative_count) -> [file:line, ...].
    let mut stale_tuples: BTreeMap<(String, u64, u64), Vec<String>> = BTreeMap::new();

    for v in &live_prose {
        let noun = match v.get("noun").and_then(|x| x.as_str()) {
            Some(s) => s.to_string(),
            None => continue,
        };
        let count = v.get("count").and_then(|x| x.as_u64()).unwrap_or(0);
        let file = v
            .get("file")
            .and_then(|x| x.as_str())
            .unwrap_or("")
            .to_string();
        let line = v.get("line").and_then(|x| x.as_u64()).unwrap_or(0);

        // Prefer the authoritative answer. Fall back to manifest sealed prose.
        let auth = resolve_auth_count(&noun).or_else(|| {
            stored_prose.iter().find_map(|sv| {
                let sv_noun = sv.get("noun").and_then(|x| x.as_str()).unwrap_or("");
                if sv_noun.eq_ignore_ascii_case(&noun) {
                    sv.get("count").and_then(|x| x.as_u64())
                } else {
                    None
                }
            })
        });
        if let Some(auth_count) = auth {
            if auth_count != count {
                stale_tuples
                    .entry((noun.clone(), count, auth_count))
                    .or_default()
                    .push(format!("{}:{}", file, line));
            }
        }
    }

    for ((noun, stale_count, auth_count), occurrences) in stale_tuples {
        let mut affected: BTreeSet<String> = BTreeSet::new();
        for occ in &occurrences {
            if let Some(f) = occ.split(':').next() {
                affected.insert(f.to_string());
            }
        }
        let affected_vec: Vec<String> = affected.into_iter().collect();
        out.push(StructuralWarning {
            kind: "DECLARED_COUNT_DRIFT".to_string(),
            message: format!(
                "declared_counts drift: pack prose says '{stale_count} {noun}' but authoritative count is {auth_count}; stale at {occurrences}",
                occurrences = occurrences.join(", ")
            ),
            affected_pack_files: affected_vec,
        });
    }
}

/// P2 check 3: re-parse `verification_shortcuts` signatures from the repo and
/// compare to manifest. Emits one `SIGNATURE_DRIFT` warning per function whose
/// signature changed (including renames — the old key disappears, a new key
/// appears; we emit one warning per side so the reviewer can spot renames).
fn check_shortcut_signatures_drift(
    manifest: &Value,
    repo_root: &Path,
    current_dir: &Path,
    out: &mut Vec<StructuralWarning>,
) {
    let stored_map = manifest
        .get("shortcut_signatures")
        .and_then(|v| v.as_object());
    let Some(stored_map) = stored_map else { return };
    let live = parse_shortcut_signatures(repo_root, current_dir);

    let stored_keys: BTreeSet<String> = stored_map.keys().cloned().collect();
    let live_keys: BTreeSet<String> = live.keys().cloned().collect();

    // Changed (same key, different signature).
    for key in stored_keys.intersection(&live_keys) {
        let stored = stored_map.get(key).and_then(|v| v.as_str()).unwrap_or("");
        let live_sig = live.get(key).map(|s| s.as_str()).unwrap_or("");
        if stored != live_sig {
            out.push(StructuralWarning {
                kind: "SIGNATURE_DRIFT".to_string(),
                message: format!(
                    "SIGNATURE_DRIFT: {key}: manifest='{stored}' vs live='{live_sig}'"
                ),
                affected_pack_files: vec![
                    "20_CODE_MAP.md".to_string(),
                    "search_scope.json".to_string(),
                ],
            });
        }
    }
    // Removed (possibly renamed — the old name is gone).
    for key in stored_keys.difference(&live_keys) {
        out.push(StructuralWarning {
            kind: "SIGNATURE_DRIFT".to_string(),
            message: format!(
                "SIGNATURE_DRIFT: {key} present in manifest but missing from source (rename or deletion)"
            ),
            affected_pack_files: vec![
                "20_CODE_MAP.md".to_string(),
                "search_scope.json".to_string(),
            ],
        });
    }
    // Added (possibly renamed — a new name appeared).
    for key in live_keys.difference(&stored_keys) {
        out.push(StructuralWarning {
            kind: "SIGNATURE_DRIFT".to_string(),
            message: format!(
                "SIGNATURE_DRIFT: {key} present in source but not in manifest (new function or rename)"
            ),
            affected_pack_files: vec![
                "20_CODE_MAP.md".to_string(),
                "search_scope.json".to_string(),
            ],
        });
    }
}

/// P2 check 4: re-hash dependency files and compare to manifest. Points the
/// reviewer at `40_OPERATIONS_AND_RELEASE.md` which typically owns the deps
/// narrative.
fn check_dependencies_drift(
    manifest: &Value,
    repo_root: &Path,
    out: &mut Vec<StructuralWarning>,
) {
    let stored = manifest
        .get("dependencies_snapshot")
        .and_then(|v| v.as_object())
        .cloned()
        .unwrap_or_default();
    if stored.is_empty() {
        return;
    }
    let live = compute_dependencies_snapshot(repo_root);
    let mut keys: BTreeSet<String> = BTreeSet::new();
    for k in stored.keys() {
        keys.insert(k.clone());
    }
    for k in live.keys() {
        keys.insert(k.clone());
    }
    for key in keys {
        let stored_hash = stored.get(&key).and_then(|v| v.as_str()).unwrap_or("");
        let live_hash = live.get(&key).map(|s| s.as_str()).unwrap_or("");
        if stored_hash != live_hash {
            out.push(StructuralWarning {
                kind: "DEPENDENCIES_DRIFT".to_string(),
                message: format!(
                    "dependencies_snapshot drift for '{key}': manifest hash differs from on-disk file; review {file}",
                    file = "40_OPERATIONS_AND_RELEASE.md"
                ),
                affected_pack_files: vec!["40_OPERATIONS_AND_RELEASE.md".to_string()],
            });
        }
    }
}

/// P2 — run every structural check and return their collected warnings. Pure
/// function over manifest JSON + filesystem. Missing pack configs cause
/// individual checks to no-op; the caller treats warnings as non-fatal.
fn run_structural_checks(
    manifest: &Value,
    repo_root: &Path,
    current_dir: &Path,
) -> Vec<StructuralWarning> {
    let mut out: Vec<StructuralWarning> = Vec::new();
    check_template_markers(current_dir, &mut out);
    check_contract_files_exist(repo_root, current_dir, &mut out);
    check_verification_shortcuts_look_for(repo_root, current_dir, &mut out);
    check_routing_files(repo_root, &mut out);
    check_family_counts_drift(manifest, repo_root, current_dir, &mut out);
    check_declared_counts_drift(manifest, repo_root, current_dir, &mut out);
    check_shortcut_signatures_drift(manifest, repo_root, current_dir, &mut out);
    check_dependencies_drift(manifest, repo_root, &mut out);
    check_verified_acceptance_anchors(repo_root, current_dir, &mut out);
    out
}

// ============================================================================
// P12 / F41 — Verified acceptance tests
//
// `acceptance_tests.md` gains an optional `verified: true|false` flag and an
// `anchors: [{file, line, line_contains}]` list per test. When a test is
// `verified: true`, each anchor's `line_contains` must literally appear at the
// named line in the named file (±3 lines tolerance). A mismatch is a hard
// structural warning — the test can only claim "verified" if the pointer
// still resolves.
//
// Schema notes:
// - `anchors` may be specified as `anchors: [file:line: substring]` bullet
//   form (compact, human-friendly) OR as a YAML-lite block:
//       anchors:
//         - file: src/lib.rs
//           line: 42
//           line_contains: "fn compute"
// - The compact form is what the team_skills fixtures use and is preserved
//   across seals; we parse it as `<path>:<line>: <substring>` (first colon
//   ends the path, second colon ends the line number).
// - Fewer than 2 of N tests verified → emit a non-fatal `warning` (warn
//   kind `VERIFIED_COUNT_LOW`) so pack authors see the signal without the
//   pack being blocked from shipping.
// ============================================================================

/// One parsed acceptance-test record. `None` variants mean the block was
/// parsed but the author omitted the field.
#[derive(Debug, Clone)]
struct ParsedAcceptanceTest {
    id: String,
    verified: bool,
    anchors: Vec<ParsedAnchor>,
}

#[derive(Debug, Clone)]
struct ParsedAnchor {
    file: String,
    line: usize,
    line_contains: String,
}

/// Parse `acceptance_tests.md` into a structured list of tests. Skips blocks
/// without an `### test:` header (or alt spellings); gracefully returns an
/// empty vec when the file is missing, empty, or unparseable — callers treat
/// that as "no verified-anchor contract to enforce".
fn parse_acceptance_tests_with_anchors(current_dir: &Path) -> Vec<ParsedAcceptanceTest> {
    let path = current_dir.join("acceptance_tests.md");
    let Ok(raw) = fs::read_to_string(&path) else {
        return Vec::new();
    };
    let mut out: Vec<ParsedAcceptanceTest> = Vec::new();
    let mut current: Option<ParsedAcceptanceTest> = None;
    let mut in_anchors_block = false;
    let mut pending_anchor: Option<ParsedAnchor> = None;

    // Inline "flush the pending YAML-lite anchor into the current test" step;
    // written as a direct block rather than a closure because the borrow
    // checker dislikes re-borrowing `current` through a closure while we also
    // hold `t = current.as_mut()` in the same loop iteration.
    macro_rules! flush_pending_anchor {
        () => {{
            if let (Some(t), Some(a)) = (current.as_mut(), pending_anchor.take()) {
                if !a.file.is_empty() && !a.line_contains.is_empty() && a.line > 0 {
                    t.anchors.push(a);
                }
            }
        }};
    }

    for line in raw.lines() {
        let trimmed = line.trim();
        // New test header. Finalize any pending anchor + test first.
        if let Some(rest) = trimmed
            .strip_prefix("### test:")
            .or_else(|| trimmed.strip_prefix("### Test:"))
            .or_else(|| trimmed.strip_prefix("###test:"))
        {
            flush_pending_anchor!();
            if let Some(t) = current.take() {
                out.push(t);
            }
            current = Some(ParsedAcceptanceTest {
                id: rest.trim().to_string(),
                verified: false,
                anchors: Vec::new(),
            });
            in_anchors_block = false;
            continue;
        }

        if current.is_none() {
            continue;
        }

        // `verified: true`. Accept bullet or bare form.
        let body = trimmed.trim_start_matches(['-', ' ', '*']);
        if let Some(rest) = body
            .strip_prefix("verified:")
            .or_else(|| body.strip_prefix("verified :"))
        {
            if let Some(t) = current.as_mut() {
                t.verified = matches!(rest.trim().to_ascii_lowercase().as_str(), "true" | "yes" | "1");
            }
            continue;
        }

        // Anchors block start.
        if body.starts_with("anchors:") {
            flush_pending_anchor!();
            in_anchors_block = true;
            // Inline compact form: `anchors: file.rs:42: fn foo`
            let after = body.trim_start_matches("anchors:").trim();
            if !after.is_empty() {
                if let Some(anchor) = parse_anchor_compact(after) {
                    if let Some(t) = current.as_mut() {
                        t.anchors.push(anchor);
                    }
                }
            }
            continue;
        }

        if in_anchors_block {
            // Bullet list under `anchors:` — supports either compact or YAML-lite.
            if let Some(stripped) = trimmed.strip_prefix('-') {
                flush_pending_anchor!();
                let s = stripped.trim();
                if let Some(anchor) = parse_anchor_compact(s) {
                    if let Some(t) = current.as_mut() {
                        t.anchors.push(anchor);
                    }
                    continue;
                }
                // Treat as start of a YAML-lite block. Expect `file: X` on this line.
                if let Some(rest) = s.strip_prefix("file:") {
                    pending_anchor = Some(ParsedAnchor {
                        file: rest.trim().to_string(),
                        line: 0,
                        line_contains: String::new(),
                    });
                }
                continue;
            }
            // Continuation field for an open pending_anchor.
            if let Some(pending) = pending_anchor.as_mut() {
                if let Some(rest) = body.strip_prefix("line:") {
                    pending.line = rest.trim().parse().unwrap_or(0);
                    continue;
                }
                if let Some(rest) = body.strip_prefix("line_contains:") {
                    pending.line_contains = rest
                        .trim()
                        .trim_matches(|c: char| c == '"' || c == '\'')
                        .to_string();
                    continue;
                }
            }
            // Non-anchor content terminates the block.
            if trimmed.is_empty() || trimmed.starts_with("### ") {
                flush_pending_anchor!();
                in_anchors_block = false;
            }
        }
    }
    flush_pending_anchor!();
    if let Some(t) = current.take() {
        out.push(t);
    }
    out
}

/// Parse the compact anchor form `path:line: substring` into an anchor.
/// Returns `None` when the path, line, or substring portion is missing.
fn parse_anchor_compact(s: &str) -> Option<ParsedAnchor> {
    // First `:` ends the path (no colons in paths are supported here — good
    // enough for source files, which never have colons on Unix).
    let first = s.find(':')?;
    let (path_part, rest1) = s.split_at(first);
    let rest1 = &rest1[1..]; // drop colon
    // Second `:` ends the line number.
    let second = rest1.find(':')?;
    let (line_part, rest2) = rest1.split_at(second);
    let rest2 = &rest2[1..];
    let path = path_part.trim().to_string();
    let line: usize = line_part.trim().parse().ok()?;
    let line_contains = rest2
        .trim()
        .trim_matches(|c: char| c == '"' || c == '\'')
        .to_string();
    if path.is_empty() || line == 0 || line_contains.is_empty() {
        return None;
    }
    Some(ParsedAnchor { file: path, line, line_contains })
}

/// P12 / F41: for every acceptance test with `verified: true`, each anchor's
/// `line_contains` must literally appear at the named line (±3 lines). Emit
/// `VERIFIED_ANCHOR_MISS` when a pointer fails to resolve and
/// `VERIFIED_COUNT_LOW` when fewer than 2 tests claim `verified: true`.
fn check_verified_acceptance_anchors(
    repo_root: &Path,
    current_dir: &Path,
    out: &mut Vec<StructuralWarning>,
) {
    let tests = parse_acceptance_tests_with_anchors(current_dir);
    if tests.is_empty() {
        return;
    }
    let tolerance: isize = 3;
    let mut verified_count = 0;
    for test in &tests {
        if !test.verified {
            continue;
        }
        verified_count += 1;
        for anchor in &test.anchors {
            let path = repo_root.join(&anchor.file);
            let raw = match fs::read_to_string(&path) {
                Ok(s) => s,
                Err(_) => {
                    out.push(StructuralWarning {
                        kind: "VERIFIED_ANCHOR_MISS".to_string(),
                        message: format!(
                            "acceptance_tests.md `{}`: anchor file missing or unreadable: {}",
                            test.id, anchor.file
                        ),
                        affected_pack_files: vec!["acceptance_tests.md".to_string()],
                    });
                    continue;
                }
            };
            let lines: Vec<&str> = raw.lines().collect();
            let mid = anchor.line.saturating_sub(1) as isize;
            let lo = (mid - tolerance).max(0) as usize;
            let hi = ((mid + tolerance) as usize).min(lines.len().saturating_sub(1));
            let mut found = false;
            for idx in lo..=hi {
                if let Some(line) = lines.get(idx) {
                    if line.contains(&anchor.line_contains) {
                        found = true;
                        break;
                    }
                }
            }
            if !found {
                out.push(StructuralWarning {
                    kind: "VERIFIED_ANCHOR_MISS".to_string(),
                    message: format!(
                        "acceptance_tests.md `{}`: anchor `{}` not found within \u{00b1}3 lines of {}:{}",
                        test.id, anchor.line_contains, anchor.file, anchor.line
                    ),
                    affected_pack_files: vec!["acceptance_tests.md".to_string()],
                });
            }
        }
    }

    // Ship-quality bar: at least 2 of 4 (or more) tests should be verified.
    // We warn below that, but only when there are at least 2 tests in the
    // file — otherwise the pack just hasn't fleshed out acceptance tests yet.
    if tests.len() >= 2 && verified_count < 2 {
        out.push(StructuralWarning {
            kind: "VERIFIED_COUNT_LOW".to_string(),
            message: format!(
                "acceptance_tests.md: only {} of {} tests are `verified: true` (ship-quality bar is \u{2265}2)",
                verified_count,
                tests.len()
            ),
            affected_pack_files: vec!["acceptance_tests.md".to_string()],
        });
    }
}

/// Serialize a `StructuralWarning` list into the CI JSON array shape defined
/// by the P2 plan: `[{kind, message, affected_pack_files[]}]`. Always emits an
/// array (possibly empty) so downstream consumers can rely on the key.
fn structural_warnings_as_json(warnings: &[StructuralWarning]) -> Value {
    Value::Array(
        warnings
            .iter()
            .map(|w| {
                json!({
                    "kind": w.kind,
                    "message": w.message,
                    "affected_pack_files": w.affected_pack_files,
                })
            })
            .collect(),
    )
}

/// P11-drift / F38 — serialize the tool-integrity report into the CI JSON
/// shape `{status, mismatches: [{file, reason}], extra_files: [..]}`. The
/// `status` field is `"pass"` when no mismatches were found (even if extras
/// exist — those are informational) and `"fail"` otherwise, so CI can gate
/// without parsing the mismatch array.
fn tool_integrity_as_json(report: &ToolIntegrityReport) -> Value {
    let status = if report.mismatches.is_empty() {
        "pass"
    } else {
        "fail"
    };
    json!({
        "status": status,
        "mismatches": report
            .mismatches
            .iter()
            .map(|m| json!({"file": m.file, "reason": m.reason}))
            .collect::<Vec<_>>(),
        "extra_files": report.extra_files,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;
    use std::fs;
    use std::path::PathBuf;

    fn test_dir(name: &str) -> PathBuf {
        let dir = env::temp_dir().join(format!("chorus_cp_test_{}", name));
        // Clean up from any previous run (idempotent)
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).expect("create test dir");
        dir
    }

    // --- upsert_context_pack_block tests ---

    #[test]
    fn upsert_creates_file_when_missing() {
        let dir = test_dir("upsert_creates");
        let file = dir.join("CLAUDE.md");
        let block = "## Context Pack\n\nRead the pack.";
        let marker = "agent-chorus:context-pack:claude";

        upsert_context_pack_block(&file, block, marker).unwrap();

        let content = fs::read_to_string(&file).unwrap();
        assert!(content.contains("<!-- agent-chorus:context-pack:claude:start -->"));
        assert!(content.contains("## Context Pack"));
        assert!(content.contains("<!-- agent-chorus:context-pack:claude:end -->"));

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn upsert_prepends_when_no_markers() {
        let dir = test_dir("upsert_prepends");
        let file = dir.join("CLAUDE.md");
        fs::write(&file, "# Existing Content\n\nSome instructions.\n").unwrap();

        let block = "## Context Pack\n\nRead the pack.";
        let marker = "agent-chorus:context-pack:claude";

        upsert_context_pack_block(&file, block, marker).unwrap();

        let content = fs::read_to_string(&file).unwrap();
        // Managed block should come before existing content
        let block_pos = content.find("<!-- agent-chorus:context-pack:claude:start -->").unwrap();
        let existing_pos = content.find("# Existing Content").unwrap();
        assert!(block_pos < existing_pos, "managed block should be prepended");
        // Existing content preserved
        assert!(content.contains("Some instructions."));

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn upsert_replaces_existing_block() {
        let dir = test_dir("upsert_replaces");
        let file = dir.join("CLAUDE.md");
        let marker = "agent-chorus:context-pack:claude";

        // Write initial content with a managed block in the middle
        let initial = "# Header\n\n\
            <!-- agent-chorus:context-pack:claude:start -->\n\
            ## Old Block\n\
            <!-- agent-chorus:context-pack:claude:end -->\n\n\
            # Footer\n";
        fs::write(&file, initial).unwrap();

        let new_block = "## New Block\n\nUpdated instructions.";
        upsert_context_pack_block(&file, new_block, marker).unwrap();

        let content = fs::read_to_string(&file).unwrap();
        assert!(!content.contains("Old Block"), "old block should be replaced");
        assert!(content.contains("New Block"), "new block should be present");
        assert!(content.contains("Updated instructions."));
        // Surrounding content preserved
        assert!(content.contains("# Header"));
        assert!(content.contains("# Footer"));

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn upsert_is_idempotent() {
        let dir = test_dir("upsert_idempotent");
        let file = dir.join("CLAUDE.md");
        let block = "## Context Pack\n\nRead the pack.";
        let marker = "agent-chorus:context-pack:claude";

        upsert_context_pack_block(&file, block, marker).unwrap();
        let first = fs::read_to_string(&file).unwrap();

        upsert_context_pack_block(&file, block, marker).unwrap();
        let second = fs::read_to_string(&file).unwrap();

        assert_eq!(first, second, "running upsert twice should produce identical content");

        let _ = fs::remove_dir_all(&dir);
    }

    // --- update_start_here_snapshot tests ---

    #[test]
    fn snapshot_replaces_three_metadata_lines() {
        let dir = test_dir("snapshot_metadata");
        let start_here = dir.join("00_START_HERE.md");

        let initial = "# Context Pack: Start Here\n\n\
            ## Snapshot\n\
            - Repo: `my-repo`\n\
            - Branch at generation: `main`\n\
            - HEAD commit: `abc1234`\n\
            - Generated at: `2026-01-01T00:00:00Z`\n\n\
            ## Read Order\n\
            1. 10_SYSTEM_OVERVIEW.md\n";
        fs::write(&start_here, initial).unwrap();

        update_start_here_snapshot(&dir, "feature-x", Some("def5678"), "2026-03-23T12:00:00Z")
            .unwrap();

        let content = fs::read_to_string(&start_here).unwrap();
        assert!(content.contains("- Branch at generation: `feature-x`"));
        assert!(content.contains("- HEAD commit: `def5678`"));
        assert!(content.contains("- Generated at: `2026-03-23T12:00:00Z`"));
        // Repo line and rest of file preserved
        assert!(content.contains("- Repo: `my-repo`"));
        assert!(content.contains("## Read Order"));
        assert!(content.contains("1. 10_SYSTEM_OVERVIEW.md"));

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn snapshot_noop_when_file_missing() {
        let dir = test_dir("snapshot_noop");
        // Don't create 00_START_HERE.md — it should be a no-op
        let result =
            update_start_here_snapshot(&dir, "main", Some("abc1234"), "2026-03-23T12:00:00Z");
        assert!(result.is_ok(), "should return Ok when file is missing");
        assert!(
            !dir.join("00_START_HERE.md").exists(),
            "should not create the file"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    // --- P11 / F34: manifest.schema_version enforcement ---

    #[test]
    fn schema_version_missing_passes_with_deprecation_warning() {
        let manifest = json!({ "files": [] });
        match check_schema_version(&manifest) {
            SchemaVersionCheck::Ok { warning } => {
                let msg = warning.expect("missing schema_version should produce a warning");
                assert!(
                    msg.contains("no schema_version"),
                    "warning should name the missing field, got: {msg}"
                );
            }
            SchemaVersionCheck::TooNew { .. } => {
                panic!("missing schema_version must be treated as v1, not newer-than-tool");
            }
        }
    }

    #[test]
    fn schema_version_matching_passes_clean() {
        let manifest = json!({ "schema_version": CURRENT_SCHEMA_VERSION, "files": [] });
        match check_schema_version(&manifest) {
            SchemaVersionCheck::Ok { warning } => {
                assert!(
                    warning.is_none(),
                    "matching schema_version must not emit a warning, got: {warning:?}"
                );
            }
            SchemaVersionCheck::TooNew { message } => {
                panic!("matching schema_version rejected as too-new: {message}");
            }
        }
    }

    #[test]
    fn schema_version_older_passes_with_upgrade_recommendation() {
        // This test only exercises the "older" branch when the tool has
        // advanced past v1. Until that happens, exercise it by forging a
        // version one step below the tool's current version when the tool
        // is >1; otherwise assert the current-version branch explicitly.
        if CURRENT_SCHEMA_VERSION <= 1 {
            // No "older" version is representable yet; exercise the
            // matching branch as a sanity check and return.
            let manifest = json!({ "schema_version": CURRENT_SCHEMA_VERSION, "files": [] });
            assert!(matches!(
                check_schema_version(&manifest),
                SchemaVersionCheck::Ok { warning: None }
            ));
            return;
        }
        let older = CURRENT_SCHEMA_VERSION - 1;
        let manifest = json!({ "schema_version": older, "files": [] });
        match check_schema_version(&manifest) {
            SchemaVersionCheck::Ok { warning } => {
                let msg = warning.expect("older schema_version should produce a warning");
                assert!(
                    msg.contains("Re-seal"),
                    "warning should recommend re-sealing, got: {msg}"
                );
            }
            SchemaVersionCheck::TooNew { message } => {
                panic!("older schema_version rejected as too-new: {message}");
            }
        }
    }

    #[test]
    fn schema_version_newer_fails_loudly() {
        let forged = CURRENT_SCHEMA_VERSION + 1;
        let manifest = json!({ "schema_version": forged, "files": [] });
        match check_schema_version(&manifest) {
            SchemaVersionCheck::Ok { .. } => {
                panic!(
                    "schema_version {forged} > tool {CURRENT_SCHEMA_VERSION} must fail loudly, not pass"
                );
            }
            SchemaVersionCheck::TooNew { message } => {
                assert!(
                    message.contains("Upgrade chorus"),
                    "error message should direct the user to upgrade, got: {message}"
                );
                assert!(
                    message.contains(&format!("v{forged}")),
                    "error message should name the offending version, got: {message}"
                );
            }
        }
    }

    // --- P11 / F36: seal records current chorus_version in manifest ---

    #[test]
    fn build_manifest_records_current_chorus_version() {
        let files_meta: Vec<FileMeta> = Vec::new();
        let baseline = SemanticBaseline::default();
        let tool_hashes: std::collections::BTreeMap<String, String> =
            std::collections::BTreeMap::new();
        let bundle = build_manifest(
            "2026-04-17T00:00:00Z",
            Path::new("/tmp/unused"),
            "fixture-repo",
            "main",
            false,
            Some("abcd1234"),
            "test-seal",
            None,
            &Vec::new(),
            &files_meta,
            &baseline,
            &tool_hashes,
        );

        let schema = bundle
            .value
            .get("schema_version")
            .and_then(|v| v.as_u64())
            .expect("schema_version should be present and numeric");
        assert_eq!(
            schema, CURRENT_SCHEMA_VERSION,
            "sealed manifest must record the current schema version"
        );

        let chorus_version = bundle
            .value
            .get("chorus_version")
            .and_then(|v| v.as_str())
            .expect("chorus_version should be a string");
        assert_eq!(
            chorus_version,
            env!("CARGO_PKG_VERSION"),
            "chorus_version must match the package version baked into the binary"
        );

        // skill_version and verifier_sha256 must be present as additive fields.
        assert!(
            bundle.value.get("skill_version").is_some(),
            "skill_version field must be present (null is fine)"
        );
        let verifier = bundle
            .value
            .get("verifier_sha256")
            .expect("verifier_sha256 field must be present");
        assert!(
            verifier.is_string() || verifier.is_null(),
            "verifier_sha256 must be a hex string or null, got: {verifier}"
        );
    }

    // -----------------------------------------------------------------------
    // P3 — Zone map + suggest-patches tests.
    // Pure-helper tests (load_zone_map, resolve_affected_sections) do not need
    // a git repo; freshness + validation tests bootstrap one on the fly.
    // -----------------------------------------------------------------------

    /// Write a relevance.json with the given content under `repo_root`.
    fn write_relevance_json(repo_root: &Path, content: &str) {
        let dir = repo_root.join(".agent-context");
        fs::create_dir_all(&dir).expect("create .agent-context dir");
        fs::write(dir.join("relevance.json"), content).expect("write relevance.json");
    }

    /// Initialize a minimal git repo with one committed file so HEAD~1-style
    /// diffs don't trip the initial-commit guard. Returns the repo root.
    fn init_repo_with_commits(name: &str) -> PathBuf {
        let dir = test_dir(name);
        let run = |args: &[&str]| -> std::process::Output {
            Command::new("git")
                .args(args)
                .current_dir(&dir)
                .output()
                .expect("git command runs")
        };
        run(&["init", "-q"]);
        run(&["config", "user.email", "test@example.com"]);
        run(&["config", "user.name", "P3 Test"]);
        run(&["config", "commit.gpgsign", "false"]);
        fs::write(dir.join("README.md"), "initial\n").unwrap();
        run(&["add", "README.md"]);
        run(&["commit", "-q", "-m", "initial"]);
        // Second commit so HEAD~1 exists for diff fallback.
        fs::write(dir.join("README.md"), "initial\nv2\n").unwrap();
        run(&["add", "README.md"]);
        run(&["commit", "-q", "-m", "second"]);
        dir
    }

    /// Stage + commit a file under the repo so it becomes tracked for
    /// `git ls-files` and shows up in diffs.
    fn commit_file(repo: &Path, rel: &str, content: &str) {
        let full = repo.join(rel);
        if let Some(parent) = full.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        fs::write(&full, content).unwrap();
        Command::new("git")
            .args(["add", rel])
            .current_dir(repo)
            .output()
            .unwrap();
        Command::new("git")
            .args(["commit", "-q", "-m", &format!("add {rel}")])
            .current_dir(repo)
            .output()
            .unwrap();
    }

    #[test]
    fn p3_load_zone_map_reads_valid_file() {
        let dir = test_dir("p3_load_zone_map");
        write_relevance_json(
            &dir,
            r#"{
              "zones": [
                {"paths": ["study/**"], "affects": ["10_SYSTEM_OVERVIEW.md"]},
                {"paths": ["src/**"], "affects": ["20_CODE_MAP.md", "30_BEHAVIORAL_INVARIANTS.md"]}
              ]
            }"#,
        );

        let zones = load_zone_map(&dir).expect("zone map should load");
        assert_eq!(zones.len(), 2, "both zones should load");
        assert_eq!(zones[0].paths, vec!["study/**".to_string()]);
        assert_eq!(zones[0].affects, vec!["10_SYSTEM_OVERVIEW.md".to_string()]);
        assert_eq!(zones[1].paths, vec!["src/**".to_string()]);
        assert_eq!(
            zones[1].affects,
            vec![
                "20_CODE_MAP.md".to_string(),
                "30_BEHAVIORAL_INVARIANTS.md".to_string()
            ]
        );
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p3_load_zone_map_missing_returns_none() {
        let dir = test_dir("p3_load_zone_map_missing");
        let zones = load_zone_map(&dir);
        assert!(zones.is_none(), "no file → None");
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p3_load_zone_map_legacy_only_returns_none() {
        let dir = test_dir("p3_load_zone_map_legacy");
        // A legacy file without `zones` should yield None; the caller
        // (check_freshness_with_zones) falls back to hardcoded relevance.
        write_relevance_json(
            &dir,
            r#"{"include": ["**"], "exclude": [".git/**"]}"#,
        );
        assert!(load_zone_map(&dir).is_none(), "no zones key → None");
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p3_resolve_affected_sections_maps_changed_file_to_zone() {
        let zones = vec![
            Zone {
                paths: vec!["study/**".to_string(), "docs/methodology/**".to_string()],
                affects: vec![
                    "10_SYSTEM_OVERVIEW.md".to_string(),
                    "30_BEHAVIORAL_INVARIANTS.md".to_string(),
                ],
            },
            Zone {
                paths: vec!["src/brand_lift/**".to_string()],
                affects: vec!["20_CODE_MAP.md".to_string()],
            },
        ];
        let sections = resolve_affected_sections("study/methodology/intro.md", &zones);
        // The first zone matches; only its affects should appear (sorted/dedup).
        assert_eq!(
            sections,
            vec![
                "10_SYSTEM_OVERVIEW.md".to_string(),
                "30_BEHAVIORAL_INVARIANTS.md".to_string(),
            ]
        );

        let none = resolve_affected_sections("unrelated/thing.txt", &zones);
        assert!(none.is_empty(), "no zone matches → empty");

        let multi = resolve_affected_sections("src/brand_lift/mod.rs", &zones);
        assert_eq!(multi, vec!["20_CODE_MAP.md".to_string()]);
    }

    #[test]
    fn p3_check_freshness_with_zones_returns_affected_sections() {
        // Full end-to-end: a real git repo with a study/ file committed, a
        // zone map referencing study/**, and a subsequent commit that edits
        // the study/ file. The zone-aware freshness check must surface the
        // affected_sections list.
        let repo = init_repo_with_commits("p3_freshness_zones");
        // Commit a file in study/ so the zone path resolves.
        commit_file(&repo, "study/intro.md", "hello\n");
        // Write zone map after the commit so load_zone_map sees it.
        write_relevance_json(
            &repo,
            r#"{
              "zones": [
                {"paths": ["study/**"], "affects": ["10_SYSTEM_OVERVIEW.md"]}
              ]
            }"#,
        );
        // Make a change that touches study/ (relative to HEAD~1).
        commit_file(&repo, "study/intro.md", "hello world\n");

        let result = check_freshness_with_zones("origin/nonexistent", &repo)
            .expect("freshness should succeed");
        assert_eq!(result.status, "warn", "study/ change without pack update → warn");
        assert!(
            result.changed_files.iter().any(|f| f == "study/intro.md"),
            "study/intro.md must appear in changed_files, got {:?}",
            result.changed_files
        );
        assert!(
            result.affected_sections.iter().any(|s| s == "10_SYSTEM_OVERVIEW.md"),
            "affected_sections must include 10_SYSTEM_OVERVIEW.md, got {:?}",
            result.affected_sections
        );
        let _ = fs::remove_dir_all(&repo);
    }

    #[test]
    fn p3_zone_referencing_zero_files_fails_verify() {
        // A zone whose `paths` match no tracked file is an authoring bug; the
        // freshness check must surface this as a `fail` so verify fails loudly.
        let repo = init_repo_with_commits("p3_zone_zero_files");
        // Zone points at a dir that does not exist in the repo.
        write_relevance_json(
            &repo,
            r#"{
              "zones": [
                {"paths": ["study/**"], "affects": ["10_SYSTEM_OVERVIEW.md"]}
              ]
            }"#,
        );
        let result = check_freshness_with_zones("origin/nonexistent", &repo)
            .expect("freshness should return a result, not an error");
        assert_eq!(
            result.status, "fail",
            "zone resolving to zero tracked files must fail"
        );
        let reason = result.skipped_reason.unwrap_or_default();
        assert!(
            reason.contains("study/**") && reason.contains("zero tracked files"),
            "fail reason must name the offending zone path, got: {reason}"
        );
        let _ = fs::remove_dir_all(&repo);
    }

    #[test]
    fn p3_fallback_when_relevance_json_absent() {
        // No relevance.json → fall through to Pass-0 `is_context_relevant`
        // hardcoded rules; affected_sections remains empty.
        let repo = init_repo_with_commits("p3_fallback_no_relevance");
        // Commit a "context-relevant" file per Pass-0 hardcoded rules
        // (cli/src/** is explicitly included in is_context_relevant).
        commit_file(&repo, "cli/src/foo.rs", "fn a() {}\n");
        // Change it again to produce a HEAD~1 diff hit.
        commit_file(&repo, "cli/src/foo.rs", "fn a() -> i32 { 0 }\n");

        let result = check_freshness_with_zones("origin/nonexistent", &repo)
            .expect("freshness ok");
        assert_eq!(
            result.status, "warn",
            "Pass-0 fallback must treat cli/src changes as context-relevant"
        );
        assert!(
            result.changed_files.iter().any(|f| f == "cli/src/foo.rs"),
            "cli/src/foo.rs must be in changed_files, got {:?}",
            result.changed_files
        );
        assert!(
            result.affected_sections.is_empty(),
            "fallback mode → affected_sections stays empty"
        );
        let _ = fs::remove_dir_all(&repo);
    }

    #[test]
    fn p3_suggest_patches_emits_expected_shape() {
        // End-to-end: wire suggest_patches() into a repo with a zone map and
        // a changed file; assert the JSON payload has all four required fields
        // and that baseline_drift is [] (reserved for P2).
        let repo = init_repo_with_commits("p3_suggest_patches_shape");
        commit_file(&repo, "study/intro.md", "hello\n");
        write_relevance_json(
            &repo,
            r#"{
              "zones": [
                {"paths": ["study/**"], "affects": ["10_SYSTEM_OVERVIEW.md"]}
              ]
            }"#,
        );
        commit_file(&repo, "study/intro.md", "hello world\n");

        let payload = suggest_patches("origin/nonexistent", &repo)
            .expect("suggest_patches should succeed");
        let obj = payload.as_object().expect("payload must be an object");
        for key in [
            "changed_files",
            "pack_sections_to_update",
            "diff_excerpt",
            "baseline_drift",
        ] {
            assert!(obj.contains_key(key), "payload missing '{key}'");
        }
        // baseline_drift is reserved for P2 → always empty array.
        assert_eq!(
            obj["baseline_drift"].as_array().unwrap().len(),
            0,
            "baseline_drift must be [] until P2 wires drift detection"
        );
        // pack_sections_to_update must include the zone's affects.
        let sections: Vec<String> = obj["pack_sections_to_update"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str().map(String::from))
            .collect();
        assert!(
            sections.iter().any(|s| s == "10_SYSTEM_OVERVIEW.md"),
            "pack_sections_to_update must contain 10_SYSTEM_OVERVIEW.md, got {:?}",
            sections
        );
        // diff_excerpt must be a string (possibly empty).
        assert!(
            obj["diff_excerpt"].is_string(),
            "diff_excerpt must be a string"
        );
        let _ = fs::remove_dir_all(&repo);
    }

    #[test]
    fn p3_default_relevance_json_contains_zones() {
        // Regression guard: the shipped default must carry a zones[] array so
        // new repos get zone-aware freshness out of the box.
        let json_str = default_relevance_json();
        let parsed: Value =
            serde_json::from_str(&json_str).expect("default relevance.json must parse");
        let zones = parsed
            .get("zones")
            .and_then(|v| v.as_array())
            .expect("zones[] must be present");
        assert!(!zones.is_empty(), "default must ship at least one zone");
        // Legacy include/exclude stays for back-compat.
        assert!(parsed.get("include").is_some(), "include[] preserved");
        assert!(parsed.get("exclude").is_some(), "exclude[] preserved");
    }


    // --- P1 tests (restored post-integration) ---

    fn init_git(root: &Path) {
        let _ = std::process::Command::new("git")
            .args(["init", "--quiet"])
            .current_dir(root)
            .output();
        let _ = std::process::Command::new("git")
            .args(["config", "user.email", "p1@test"])
            .current_dir(root)
            .output();
        let _ = std::process::Command::new("git")
            .args(["config", "user.name", "P1 Tester"])
            .current_dir(root)
            .output();
    }

    #[test]
    fn extract_declared_counts_catches_prose_numbers_and_respects_ignore() {
        let dir = test_dir("p1_declared_counts");
        let current = dir.join(".agent-context").join("current");
        fs::create_dir_all(&current).unwrap();
        let body = "\
# Overview\n\
\n\
The repo ships 6 files and 12 scripts.\n\
\n\
<!-- count-claim: ignore -->\n\
Historic note: we once had 42 tests.\n\
<!-- count-claim: end -->\n\
\n\
After cleanup there are 32 tests today.\n\
";
        fs::write(current.join("10_SYSTEM_OVERVIEW.md"), body).unwrap();

        let out = extract_declared_counts(&current);
        // Collect (noun, count) pairs for easier assertions.
        let pairs: Vec<(String, u64)> = out
            .iter()
            .map(|v| {
                (
                    v.get("noun").and_then(|x| x.as_str()).unwrap().to_string(),
                    v.get("count").and_then(|x| x.as_u64()).unwrap(),
                )
            })
            .collect();
        assert!(
            pairs.contains(&("files".to_string(), 6)),
            "expected '6 files' claim in {pairs:?}"
        );
        assert!(
            pairs.contains(&("scripts".to_string(), 12)),
            "expected '12 scripts' claim in {pairs:?}"
        );
        assert!(
            pairs.contains(&("tests".to_string(), 32)),
            "expected '32 tests' claim in {pairs:?}"
        );
        assert!(
            !pairs.contains(&("tests".to_string(), 42)),
            "'42 tests' inside <!-- count-claim: ignore --> must be skipped, got {pairs:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn resolve_family_counts_empty_when_config_absent() {
        let dir = test_dir("p1_family_counts_absent");
        init_git(&dir);
        let current = dir.join(".agent-context").join("current");
        fs::create_dir_all(&current).unwrap();

        let counts = resolve_family_counts(&dir, &current);
        assert!(
            counts.is_empty(),
            "expected empty family_counts when configs absent, got {counts:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn post_commit_reconcile_updates_post_commit_sha() {
        let dir = test_dir("p1_post_commit");
        init_git(&dir);
        // Seed a single commit so HEAD resolves.
        fs::write(dir.join("README.md"), "hi\n").unwrap();
        let _ = std::process::Command::new("git")
            .args(["add", "README.md"])
            .current_dir(&dir)
            .output();
        let _ = std::process::Command::new("git")
            .args(["commit", "-m", "seed", "--quiet"])
            .current_dir(&dir)
            .output();

        // Minimal manifest that we expect the reconcile to stamp.
        let current = dir.join(".agent-context").join("current");
        fs::create_dir_all(&current).unwrap();
        let initial = json!({
            "schema_version": CURRENT_SCHEMA_VERSION,
            "head_sha": "deadbeef",
            "head_sha_at_seal": "deadbeef",
            "post_commit_sha": null,
            "files": [],
        });
        fs::write(
            current.join("manifest.json"),
            format!("{}\n", serde_json::to_string_pretty(&initial).unwrap()),
        )
        .unwrap();

        post_commit_reconcile(Some(dir.to_str().unwrap()), None).expect("reconcile must succeed");

        let raw = fs::read_to_string(current.join("manifest.json")).unwrap();
        let manifest: serde_json::Value = serde_json::from_str(&raw).unwrap();
        let post_sha = manifest
            .get("post_commit_sha")
            .and_then(|v| v.as_str())
            .expect("post_commit_sha must be a string after reconcile");
        // SHA-1 hex = 40 chars; accept any non-empty string from git rev-parse.
        assert!(!post_sha.is_empty());
        // Invariant: seal-time head_sha_at_seal must be preserved.
        assert_eq!(
            manifest.get("head_sha_at_seal").and_then(|v| v.as_str()),
            Some("deadbeef"),
            "reconcile must never mutate head_sha_at_seal"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn compute_dependencies_snapshot_hashes_pyproject_and_npm() {
        let dir = test_dir("p1_deps");
        fs::write(dir.join("pyproject.toml"), "[tool.poetry]\nname=\"demo\"\n").unwrap();
        fs::write(dir.join("package.json"), "{\"name\":\"demo\"}\n").unwrap();

        let snap = compute_dependencies_snapshot(&dir);
        assert!(
            snap.get("pyproject").map(|s| s.len() == 64).unwrap_or(false),
            "pyproject hash should be 64-char hex, got {snap:?}"
        );
        assert!(
            snap.get("npm").map(|s| s.len() == 64).unwrap_or(false),
            "npm hash should be 64-char hex, got {snap:?}"
        );
        assert!(
            !snap.contains_key("cargo"),
            "cargo must be absent when Cargo.toml missing, got {snap:?}"
        );

        // Sanity: value changes when the file changes.
        fs::write(dir.join("pyproject.toml"), "[tool.poetry]\nname=\"demo2\"\n").unwrap();
        let snap2 = compute_dependencies_snapshot(&dir);
        assert_ne!(
            snap.get("pyproject"),
            snap2.get("pyproject"),
            "pyproject hash must change when the file content changes"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn build_manifest_emits_all_p1_fields() {
        let files_meta: Vec<FileMeta> = Vec::new();
        let mut baseline = SemanticBaseline::default();
        baseline
            .family_counts
            .insert("scripts/run_*.py".to_string(), 12);
        baseline.declared_counts.push(json!({
            "noun": "files",
            "count": 6,
            "file": "10_SYSTEM_OVERVIEW.md",
            "line": 3,
        }));
        baseline
            .shortcut_signatures
            .insert("calc.py::compute_lift".to_string(), "def compute_lift(data)".to_string());
        baseline
            .dependencies_snapshot
            .insert("pyproject".to_string(), "a".repeat(64));

        let tool_hashes: std::collections::BTreeMap<String, String> =
            std::collections::BTreeMap::new();
        let bundle = build_manifest(
            "2026-04-17T00:00:00Z",
            Path::new("/tmp/unused"),
            "fixture-repo",
            "main",
            false,
            Some("abcd1234"),
            "test-seal",
            None,
            &Vec::new(),
            &files_meta,
            &baseline,
            &tool_hashes,
        );

        // All four semantic fields must be present with the right shape.
        assert_eq!(
            bundle
                .value
                .get("family_counts")
                .and_then(|v| v.get("scripts/run_*.py"))
                .and_then(|v| v.as_u64()),
            Some(12),
            "family_counts missing the expected entry"
        );
        let declared = bundle
            .value
            .get("declared_counts")
            .and_then(|v| v.as_array())
            .expect("declared_counts must be an array");
        assert_eq!(declared.len(), 1, "declared_counts should contain one entry");
        assert!(
            bundle
                .value
                .get("shortcut_signatures")
                .and_then(|v| v.get("calc.py::compute_lift"))
                .and_then(|v| v.as_str())
                .map(|s| s.contains("compute_lift"))
                .unwrap_or(false),
            "shortcut_signatures missing the expected entry"
        );
        assert!(
            bundle
                .value
                .get("dependencies_snapshot")
                .and_then(|v| v.get("pyproject"))
                .and_then(|v| v.as_str())
                .map(|s| s.len() == 64)
                .unwrap_or(false),
            "dependencies_snapshot missing pyproject entry"
        );

        // head_sha_at_seal is the new canonical field; head_sha remains populated.
        assert_eq!(
            bundle
                .value
                .get("head_sha_at_seal")
                .and_then(|v| v.as_str()),
            Some("abcd1234"),
            "head_sha_at_seal should mirror the seal-time HEAD"
        );
        assert_eq!(
            bundle.value.get("head_sha").and_then(|v| v.as_str()),
            Some("abcd1234"),
            "head_sha must stay populated for back-compat"
        );
        assert!(
            bundle.value.get("post_commit_sha").map(|v| v.is_null()).unwrap_or(false),
            "post_commit_sha must be null at seal time"
        );
    }

    #[test]
    fn resolve_family_counts_resolves_run_scripts_glob() {
        let dir = test_dir("p1_family_counts");
        init_git(&dir);
        let scripts = dir.join("scripts");
        fs::create_dir_all(&scripts).unwrap();
        for i in 0..12 {
            fs::write(scripts.join(format!("run_{i}.py")), "# runner\n").unwrap();
        }
        let current = dir.join(".agent-context").join("current");
        fs::create_dir_all(&current).unwrap();
        // Author a minimal completeness_contract.json that declares the glob.
        fs::write(
            current.join("completeness_contract.json"),
            r#"{
              "task_families": {
                "lookup": {"required_file_families": ["scripts/run_*.py"]}
              }
            }"#,
        )
        .unwrap();

        let counts = resolve_family_counts(&dir, &current);
        assert_eq!(
            counts.get("scripts/run_*.py").copied(),
            Some(12),
            "expected 12 scripts under scripts/run_*.py, got {counts:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn parse_shortcut_signatures_for_python_and_rust_and_ts() {
        let dir = test_dir("p1_sigs");
        init_git(&dir);
        // Python
        fs::write(
            dir.join("calc.py"),
            "def compute_lift(data, *, alpha=0.05):\n    return 1\n\n\
             async def fetch(url: str) -> str:\n    return url\n",
        )
        .unwrap();
        // Rust
        fs::write(
            dir.join("lib.rs"),
            "pub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n\n\
             fn internal() {\n}\n",
        )
        .unwrap();
        // TypeScript
        fs::write(
            dir.join("helpers.ts"),
            "export function shout(msg: string): string {\n  return msg.toUpperCase();\n}\n\n\
             export const greet = (name: string): string => `hi ${name}`;\n",
        )
        .unwrap();

        let py_sigs = parse_shortcut_signatures_for_file(&dir.join("calc.py"), "calc.py");
        assert!(
            py_sigs
                .get("calc.py::compute_lift")
                .map(|s| s.contains("compute_lift"))
                .unwrap_or(false),
            "expected python compute_lift signature, got {py_sigs:?}"
        );
        assert!(
            py_sigs.contains_key("calc.py::fetch"),
            "expected python fetch signature, got {py_sigs:?}"
        );

        let rs_sigs = parse_shortcut_signatures_for_file(&dir.join("lib.rs"), "lib.rs");
        assert!(
            rs_sigs
                .get("lib.rs::add")
                .map(|s| s.contains("-> i32"))
                .unwrap_or(false),
            "expected rust add signature with return type, got {rs_sigs:?}"
        );
        assert!(
            rs_sigs.contains_key("lib.rs::internal"),
            "expected rust internal signature, got {rs_sigs:?}"
        );

        let ts_sigs = parse_shortcut_signatures_for_file(&dir.join("helpers.ts"), "helpers.ts");
        assert!(
            ts_sigs.contains_key("helpers.ts::shout"),
            "expected ts shout signature, got {ts_sigs:?}"
        );
        assert!(
            ts_sigs.contains_key("helpers.ts::greet"),
            "expected ts greet arrow signature, got {ts_sigs:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    // --- P2 structural verifier tests ---

    /// Build a minimal `.agent-context/current/` at `<dir>` and return the
    /// `current_dir` PathBuf. Used by several P2 tests.
    fn p2_init_pack(dir: &Path) -> PathBuf {
        let current = dir.join(".agent-context").join("current");
        fs::create_dir_all(&current).unwrap();
        current
    }

    #[test]
    fn p2_template_marker_in_pack_json_is_flagged() {
        let dir = test_dir("p2_template_markers");
        let current = p2_init_pack(&dir);
        fs::write(
            current.join("routes.json"),
            r#"{"task_routes": {"lookup": {"pack_read_order": ["{name}/00_START_HERE.md"]}}}"#,
        )
        .unwrap();

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_template_markers(&current, &mut out);

        assert!(
            out.iter().any(|w| w.kind == "TEMPLATE_MARKER"
                && w.message.contains("routes.json")
                && w.message.contains("{name}")),
            "expected TEMPLATE_MARKER warning naming routes.json + {{name}}, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p2_contract_glob_existence_flags_empty_matches() {
        let dir = test_dir("p2_contract_glob");
        init_git(&dir);
        let current = p2_init_pack(&dir);
        fs::write(
            current.join("completeness_contract.json"),
            r#"{
              "task_families": {
                "lookup": {
                  "contractually_required_files": ["nonexistent/plan.md"],
                  "required_file_families": ["scripts/run_*.py"]
                }
              }
            }"#,
        )
        .unwrap();

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_contract_files_exist(&dir, &current, &mut out);

        // Both checks must fire: missing file AND empty glob.
        assert!(
            out.iter().any(|w| w.kind == "CONTRACT_REQUIRED_FILE_MISSING"
                && w.message.contains("nonexistent/plan.md")),
            "expected CONTRACT_REQUIRED_FILE_MISSING naming nonexistent/plan.md, got {out:?}"
        );
        assert!(
            out.iter().any(|w| w.kind == "CONTRACT_GLOB_MISS"
                && w.message.contains("scripts/run_*.py")),
            "expected CONTRACT_GLOB_MISS naming scripts/run_*.py, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p2_look_for_substring_check_flags_missing_string() {
        let dir = test_dir("p2_look_for");
        init_git(&dir);
        let current = p2_init_pack(&dir);
        // Repo file that does NOT contain the look_for string.
        fs::write(
            dir.join("calc.py"),
            "def compute_lift(data):\n    return 1\n",
        )
        .unwrap();
        // team_skills-shape verification_shortcuts (array of {file, look_for}).
        fs::write(
            current.join("search_scope.json"),
            r#"{
              "task_families": {
                "lookup": {
                  "verification_shortcuts": [
                    {"file": "calc.py", "look_for": "MIN_CELL_SIZE = 30"}
                  ]
                }
              }
            }"#,
        )
        .unwrap();

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_verification_shortcuts_look_for(&dir, &current, &mut out);

        assert!(
            out.iter().any(|w| w.kind == "LOOK_FOR_MISSING"
                && w.message.contains("calc.py")
                && w.message.contains("MIN_CELL_SIZE = 30")),
            "expected LOOK_FOR_MISSING naming calc.py + MIN_CELL_SIZE, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    // --- P12 / F40 — comment-aware look_for tests ---

    /// When the only occurrence of the look_for substring is inside a Python
    /// line comment, the comment-strip pass turns "match" into "no match"
    /// and the warning must explicitly name the comment-only case.
    #[test]
    fn p12_look_for_rejects_comment_only_match_python() {
        let dir = test_dir("p12_look_for_comment_py");
        init_git(&dir);
        let current = p2_init_pack(&dir);
        // Python file where the string appears ONLY in a comment.
        fs::write(
            dir.join("calc.py"),
            "# MIN_CELL_SIZE = 30 (stale doc comment)\n\
             def compute_lift(data):\n    return 1\n",
        )
        .unwrap();
        fs::write(
            current.join("search_scope.json"),
            r#"{
              "task_families": {
                "lookup": {
                  "verification_shortcuts": [
                    {"file": "calc.py", "look_for": "MIN_CELL_SIZE = 30"}
                  ]
                }
              }
            }"#,
        )
        .unwrap();

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_verification_shortcuts_look_for(&dir, &current, &mut out);

        assert!(
            out.iter().any(|w| w.kind == "LOOK_FOR_MISSING"
                && w.message.contains("matches only comments")
                && w.message.contains("calc.py")),
            "expected comment-only LOOK_FOR_MISSING, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    /// Rust variant — `//` line comments and `/* */` blocks must be stripped
    /// so a look_for that only appears inside them is flagged, matching the
    /// Python behavior.
    #[test]
    fn p12_look_for_rejects_comment_only_match_rust() {
        let dir = test_dir("p12_look_for_comment_rs");
        init_git(&dir);
        let current = p2_init_pack(&dir);
        fs::write(
            dir.join("lib.rs"),
            "// MIN_CELL_SIZE = 30 is a stale doc hint\n\
             /* block: MIN_CELL_SIZE = 30 */\n\
             pub fn add(a: i32, b: i32) -> i32 { a + b }\n",
        )
        .unwrap();
        fs::write(
            current.join("search_scope.json"),
            r#"{
              "task_families": {
                "lookup": {
                  "verification_shortcuts": [
                    {"file": "lib.rs", "look_for": "MIN_CELL_SIZE = 30"}
                  ]
                }
              }
            }"#,
        )
        .unwrap();

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_verification_shortcuts_look_for(&dir, &current, &mut out);

        assert!(
            out.iter().any(|w| w.kind == "LOOK_FOR_MISSING"
                && w.message.contains("matches only comments")
                && w.message.contains("lib.rs")),
            "expected comment-only LOOK_FOR_MISSING for rust, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    /// TypeScript variant using `//` line comments.
    #[test]
    fn p12_look_for_rejects_comment_only_match_ts() {
        let dir = test_dir("p12_look_for_comment_ts");
        init_git(&dir);
        let current = p2_init_pack(&dir);
        fs::write(
            dir.join("svc.ts"),
            "// MIN_CELL_SIZE = 30 (stale JSDoc hint)\n\
             export function svc(): number { return 1; }\n",
        )
        .unwrap();
        fs::write(
            current.join("search_scope.json"),
            r#"{
              "task_families": {
                "lookup": {
                  "verification_shortcuts": [
                    {"file": "svc.ts", "look_for": "MIN_CELL_SIZE = 30"}
                  ]
                }
              }
            }"#,
        )
        .unwrap();

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_verification_shortcuts_look_for(&dir, &current, &mut out);

        assert!(
            out.iter().any(|w| w.kind == "LOOK_FOR_MISSING"
                && w.message.contains("matches only comments")
                && w.message.contains("svc.ts")),
            "expected comment-only LOOK_FOR_MISSING for ts, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    /// When the look_for substring is present in real code (not just in a
    /// comment) after comment-stripping, no warning should fire — the
    /// comment-strip must not drop legitimate matches.
    #[test]
    fn p12_look_for_accepts_match_in_real_code() {
        let dir = test_dir("p12_look_for_real_code");
        init_git(&dir);
        let current = p2_init_pack(&dir);
        fs::write(
            dir.join("calc.py"),
            "MIN_CELL_SIZE = 30\n\
             def compute_lift(data):\n    return MIN_CELL_SIZE\n",
        )
        .unwrap();
        fs::write(
            current.join("search_scope.json"),
            r#"{
              "task_families": {
                "lookup": {
                  "verification_shortcuts": [
                    {"file": "calc.py", "look_for": "MIN_CELL_SIZE = 30"}
                  ]
                }
              }
            }"#,
        )
        .unwrap();

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_verification_shortcuts_look_for(&dir, &current, &mut out);

        assert!(
            out.is_empty(),
            "no warning expected when look_for appears in real code, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    /// `look_for_regex` takes precedence over `look_for` when both are
    /// present, and uses regex semantics against the comment-stripped source.
    #[test]
    fn p12_look_for_regex_matches_after_comment_strip() {
        let dir = test_dir("p12_look_for_regex");
        init_git(&dir);
        let current = p2_init_pack(&dir);
        fs::write(
            dir.join("calc.py"),
            "# MIN_CELL_SIZE = 30\n\
             MIN_CELL_SIZE = 42\n",
        )
        .unwrap();
        fs::write(
            current.join("search_scope.json"),
            r#"{
              "task_families": {
                "lookup": {
                  "verification_shortcuts": [
                    {"file": "calc.py", "look_for_regex": "MIN_CELL_SIZE = \\d+"}
                  ]
                }
              }
            }"#,
        )
        .unwrap();

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_verification_shortcuts_look_for(&dir, &current, &mut out);

        assert!(
            out.is_empty(),
            "look_for_regex should match MIN_CELL_SIZE = 42 after comment-strip, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    /// When the regex does not match, the warning must name the regex field
    /// so authors can tell whether to update the pattern or the pack pointer.
    #[test]
    fn p12_look_for_regex_reports_no_match() {
        let dir = test_dir("p12_look_for_regex_miss");
        init_git(&dir);
        let current = p2_init_pack(&dir);
        fs::write(
            dir.join("calc.py"),
            "def compute_lift(data):\n    return 1\n",
        )
        .unwrap();
        fs::write(
            current.join("search_scope.json"),
            r#"{
              "task_families": {
                "lookup": {
                  "verification_shortcuts": [
                    {"file": "calc.py", "look_for_regex": "MAX_BATCH_SIZE = \\d+"}
                  ]
                }
              }
            }"#,
        )
        .unwrap();

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_verification_shortcuts_look_for(&dir, &current, &mut out);

        assert!(
            out.iter().any(|w| w.kind == "LOOK_FOR_MISSING"
                && w.message.contains("look_for_regex did not match")),
            "expected look_for_regex no-match warning, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    /// Direct unit test for the comment-strip helper — covers docstrings
    /// (triple-quoted) and line comments for Python.
    #[test]
    fn p12_strip_python_comments_removes_docstrings() {
        let src = "\"\"\"module docstring with MIN_CELL_SIZE = 30\"\"\"\n\
                   # MIN_CELL_SIZE = 30 also here\n\
                   def f():\n    pass\n";
        let stripped = strip_source_comments(src, "py");
        assert!(
            !stripped.contains("MIN_CELL_SIZE = 30"),
            "docstring and comment must be stripped, got {stripped:?}"
        );
        assert!(stripped.contains("def f()"), "code must remain after strip");
    }

    /// Unit test for the C-style strip covering both `//` and `/* */` cases.
    #[test]
    fn p12_strip_c_style_comments_removes_both_forms() {
        let src = "// comment with TOKEN\n/* block TOKEN */\nfn real() {}\n";
        let stripped = strip_source_comments(src, "rs");
        assert!(
            !stripped.contains("TOKEN"),
            "both comment forms must be stripped, got {stripped:?}"
        );
        assert!(stripped.contains("fn real()"));
    }

    // --- P12 / F41 — verified acceptance tests + anchor tolerance ---

    /// A `verified: true` test whose anchor points at an exact line that
    /// contains the declared substring must pass the check.
    #[test]
    fn p12_verified_acceptance_anchor_passes_when_line_matches() {
        let dir = test_dir("p12_verified_anchor_ok");
        let current = p2_init_pack(&dir);
        fs::write(
            dir.join("lib.rs"),
            "line 1\nline 2\npub fn compute(x: i32) -> i32 { x }\nline 4\n",
        )
        .unwrap();
        fs::write(
            current.join("acceptance_tests.md"),
            "### test: smoke\n\
             - verified: true\n\
             - anchors: lib.rs:3: pub fn compute\n\n\
             ### test: other\n\
             - verified: true\n\
             - anchors: lib.rs:3: pub fn compute\n",
        )
        .unwrap();

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_verified_acceptance_anchors(&dir, &current, &mut out);
        assert!(
            out.is_empty(),
            "anchor at the exact line must pass, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    /// Anchor tolerance is ±3 lines — a line_contains string found 2 lines
    /// below the declared line still passes.
    #[test]
    fn p12_verified_acceptance_anchor_tolerates_small_drift() {
        let dir = test_dir("p12_verified_anchor_tolerance");
        let current = p2_init_pack(&dir);
        fs::write(
            dir.join("lib.rs"),
            "l1\nl2\nl3\nl4\npub fn compute(x: i32) -> i32 { x }\nl6\n",
        )
        .unwrap();
        // Declared line is 3, actual is 5 → within ±3 tolerance.
        fs::write(
            current.join("acceptance_tests.md"),
            "### test: smoke\n\
             - verified: true\n\
             - anchors: lib.rs:3: pub fn compute\n\n\
             ### test: other\n\
             - verified: true\n\
             - anchors: lib.rs:3: pub fn compute\n",
        )
        .unwrap();

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_verified_acceptance_anchors(&dir, &current, &mut out);
        assert!(
            out.is_empty(),
            "2-line drift must be tolerated, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    /// Anchor outside the ±3 tolerance window must fail with
    /// VERIFIED_ANCHOR_MISS naming the line_contains substring.
    #[test]
    fn p12_verified_acceptance_anchor_fails_when_drift_exceeds_tolerance() {
        let dir = test_dir("p12_verified_anchor_miss");
        let current = p2_init_pack(&dir);
        fs::write(
            dir.join("lib.rs"),
            "l1\nl2\nl3\nl4\nl5\nl6\nl7\nl8\npub fn compute(x: i32) -> i32 { x }\n",
        )
        .unwrap();
        // Declared at line 2, actual at line 9 → exceeds ±3.
        fs::write(
            current.join("acceptance_tests.md"),
            "### test: smoke\n\
             - verified: true\n\
             - anchors: lib.rs:2: pub fn compute\n\n\
             ### test: other\n\
             - verified: true\n\
             - anchors: lib.rs:2: pub fn compute\n",
        )
        .unwrap();

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_verified_acceptance_anchors(&dir, &current, &mut out);
        assert!(
            out.iter().any(|w| w.kind == "VERIFIED_ANCHOR_MISS"
                && w.message.contains("pub fn compute")),
            "expected VERIFIED_ANCHOR_MISS, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    /// Fewer than 2 `verified: true` tests → emit VERIFIED_COUNT_LOW warning
    /// but do NOT emit VERIFIED_ANCHOR_MISS (anchors are still intact on the
    /// verified test that exists). The warning is advisory.
    #[test]
    fn p12_verified_count_low_emits_warning_not_failure() {
        let dir = test_dir("p12_verified_count_low");
        let current = p2_init_pack(&dir);
        fs::write(
            dir.join("lib.rs"),
            "pub fn compute(x: i32) -> i32 { x }\n",
        )
        .unwrap();
        // 4 tests, only 1 is verified.
        fs::write(
            current.join("acceptance_tests.md"),
            "### test: a\n\
             - verified: true\n\
             - anchors: lib.rs:1: pub fn compute\n\n\
             ### test: b\n- verified: false\n\n\
             ### test: c\n- verified: false\n\n\
             ### test: d\n- verified: false\n",
        )
        .unwrap();

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_verified_acceptance_anchors(&dir, &current, &mut out);
        assert!(
            out.iter().any(|w| w.kind == "VERIFIED_COUNT_LOW"
                && w.message.contains("1 of 4")),
            "expected VERIFIED_COUNT_LOW naming 1 of 4, got {out:?}"
        );
        assert!(
            !out.iter().any(|w| w.kind == "VERIFIED_ANCHOR_MISS"),
            "anchor was valid; no MISS expected, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    // --- P12 / F42 — history.jsonl audit trail tests ---

    /// On first seal (no previous snapshot), the audit helper must return
    /// an empty diff vector rather than panic.
    #[test]
    fn p12_compute_prose_diff_sections_first_seal_is_empty() {
        let dir = test_dir("p12_prose_diff_first");
        let snapshots = dir.join("snapshots");
        fs::create_dir_all(&snapshots).unwrap();
        let current = dir.join("current");
        fs::create_dir_all(&current).unwrap();
        fs::write(current.join("20_CODE_MAP.md"), "## Intro\nhello\n").unwrap();

        let diff = compute_prose_diff_sections(&snapshots, &current);
        assert!(diff.is_empty(), "first seal must have empty diff, got {diff:?}");
        let _ = fs::remove_dir_all(&dir);
    }

    /// When an H2 section body changes between snapshots, the helper must
    /// report `<file>#<heading>` for that section.
    #[test]
    fn p12_compute_prose_diff_sections_names_changed_h2() {
        let dir = test_dir("p12_prose_diff_h2");
        let snapshots = dir.join("snapshots");
        let prev = snapshots.join("20260101T000000Z_abc");
        fs::create_dir_all(&prev).unwrap();
        fs::write(prev.join("20_CODE_MAP.md"), "## Intro\nold body\n## Other\nunchanged\n")
            .unwrap();
        let current = dir.join("current");
        fs::create_dir_all(&current).unwrap();
        fs::write(
            current.join("20_CODE_MAP.md"),
            "## Intro\nnew body\n## Other\nunchanged\n",
        )
        .unwrap();

        let diff = compute_prose_diff_sections(&snapshots, &current);
        assert!(
            diff.iter().any(|s| s == "20_CODE_MAP.md#Intro"),
            "expected 20_CODE_MAP.md#Intro in diff, got {diff:?}"
        );
        assert!(
            !diff.iter().any(|s| s == "20_CODE_MAP.md#Other"),
            "unchanged section must not appear, got {diff:?}"
        );
        let _ = fs::remove_dir_all(&dir);
    }

    /// `git_committer_identity` must return "name <email>" when both are
    /// configured. The test sets env locally (not the global user config).
    #[test]
    fn p12_git_committer_identity_formats_name_and_email() {
        let dir = test_dir("p12_committer");
        init_git(&dir);
        // Local git config for this repo only.
        let _ = Command::new("git")
            .args(["config", "user.name", "Test User"])
            .current_dir(&dir)
            .output();
        let _ = Command::new("git")
            .args(["config", "user.email", "test@example.com"])
            .current_dir(&dir)
            .output();

        let ident = git_committer_identity(&dir);
        assert_eq!(ident, "Test User <test@example.com>");
        let _ = fs::remove_dir_all(&dir);
    }

    /// End-to-end: a full seal writes the new audit-trail fields on
    /// `history.jsonl`. Verifies the JSON structure so readers can rely
    /// on sealed_by/prose_diff_sections/seal_reason.
    #[test]
    fn p12_seal_history_entry_has_audit_trail_fields() {
        let dir = test_dir("p12_seal_audit");
        init_git(&dir);
        // Local git identity so `sealed_by` is populated.
        let _ = Command::new("git")
            .args(["config", "user.name", "Audit Bot"])
            .current_dir(&dir)
            .output();
        let _ = Command::new("git")
            .args(["config", "user.email", "audit@example.com"])
            .current_dir(&dir)
            .output();

        // Minimal pack with all required files.
        let current = dir.join(".agent-context").join("current");
        fs::create_dir_all(&current).unwrap();
        for f in REQUIRED_FILES {
            fs::write(current.join(f), format!("## Intro\n{f} body\n")).unwrap();
        }

        // Commit so HEAD exists.
        let _ = Command::new("git").args(["add", "-A"]).current_dir(&dir).output();
        let _ = Command::new("git")
            .args(["commit", "-m", "init"])
            .current_dir(&dir)
            .output();

        seal(SealOptions {
            reason: Some("p12-audit-seal".to_string()),
            base: None,
            head: None,
            pack_dir: None,
            cwd: Some(dir.display().to_string()),
            force: false,
            force_snapshot: false,
            follow_symlinks: false,
        })
        .expect("seal should succeed");

        let history = fs::read_to_string(dir.join(".agent-context/history.jsonl"))
            .expect("history.jsonl present after seal");
        let last_line = history.lines().last().expect("history has at least one line");
        let entry: Value = serde_json::from_str(last_line).expect("valid jsonl entry");
        assert_eq!(
            entry.get("seal_reason").and_then(|v| v.as_str()),
            Some("p12-audit-seal"),
            "seal_reason must be populated"
        );
        assert!(
            entry
                .get("sealed_by")
                .and_then(|v| v.as_str())
                .map(|s| s.contains("Audit Bot") && s.contains("audit@example.com"))
                .unwrap_or(false),
            "sealed_by must reflect git identity, got {entry:?}"
        );
        assert!(
            entry.get("prose_diff_sections").and_then(|v| v.as_array()).is_some(),
            "prose_diff_sections must be an array, got {entry:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    /// Acceptance tests without anchors (or without `verified: true`) must
    /// not produce any warnings — the feature is opt-in.
    #[test]
    fn p12_verified_acceptance_no_verified_tests_is_silent() {
        let dir = test_dir("p12_verified_silent");
        let current = p2_init_pack(&dir);
        fs::write(
            current.join("acceptance_tests.md"),
            "### test: a\n- invalidated_by: compute\n",
        )
        .unwrap();
        let mut out: Vec<StructuralWarning> = Vec::new();
        check_verified_acceptance_anchors(&dir, &current, &mut out);
        assert!(
            out.is_empty(),
            "un-verified tests must not produce warnings, got {out:?}"
        );
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p2_routing_file_check_flags_missing_canonical_refs() {
        let dir = test_dir("p2_routing");
        // CLAUDE.md exists but does not reference the canonical pack paths.
        fs::write(dir.join("CLAUDE.md"), "# empty routing block\n").unwrap();
        // GEMINI.md missing entirely — must not produce a warning.

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_routing_files(&dir, &mut out);

        assert!(
            out.iter().any(|w| w.kind == "ROUTING_MISSING_REF"
                && w.message.contains("CLAUDE.md")),
            "expected ROUTING_MISSING_REF naming CLAUDE.md, got {out:?}"
        );
        assert!(
            !out.iter().any(|w| w.message.contains("GEMINI.md")),
            "missing GEMINI.md must not trigger a warning, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p2_family_count_delta_after_adding_a_script() {
        // Fixture: 12 scripts at seal, manifest says 12, add a 13th → warn.
        let dir = test_dir("p2_family_count_delta");
        init_git(&dir);
        let scripts = dir.join("scripts");
        fs::create_dir_all(&scripts).unwrap();
        for i in 0..12 {
            fs::write(scripts.join(format!("run_{i}.py")), "# runner\n").unwrap();
        }
        let current = p2_init_pack(&dir);
        fs::write(
            current.join("completeness_contract.json"),
            r#"{"task_families": {"lookup": {"required_file_families": ["scripts/run_*.py"]}}}"#,
        )
        .unwrap();

        // Manifest records 12 (seal-time count).
        let manifest = json!({
            "family_counts": {"scripts/run_*.py": 12},
        });

        // No drift yet.
        let mut out: Vec<StructuralWarning> = Vec::new();
        check_family_counts_drift(&manifest, &dir, &current, &mut out);
        assert!(
            out.is_empty(),
            "no drift expected before adding the 13th file, got {out:?}"
        );

        // Add a 13th script — live count becomes 13.
        fs::write(scripts.join("run_12.py"), "# 13th\n").unwrap();
        let mut out: Vec<StructuralWarning> = Vec::new();
        check_family_counts_drift(&manifest, &dir, &current, &mut out);
        assert!(
            out.iter().any(|w| w.kind == "FAMILY_COUNT_DRIFT"
                && w.message.contains("scripts/run_*.py")
                && w.message.contains("12")
                && w.message.contains("13")),
            "expected FAMILY_COUNT_DRIFT naming 12 vs 13, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p2_declared_count_drift_names_every_stale_file() {
        // Fixture: prose in two pack files says "12 scripts" but the repo
        // actually has 13 matching scripts on disk. The authoritative count
        // comes from the live resolution of the glob pattern that mentions
        // "scripts" in its path.
        let dir = test_dir("p2_declared_count_delta");
        init_git(&dir);
        // Create 13 scripts on disk — the authoritative answer.
        let scripts = dir.join("scripts");
        fs::create_dir_all(&scripts).unwrap();
        for i in 0..13 {
            fs::write(scripts.join(format!("run_{i}.py")), "# runner\n").unwrap();
        }
        let current = p2_init_pack(&dir);
        // Declare the glob so resolve_family_counts returns 13 for it.
        fs::write(
            current.join("completeness_contract.json"),
            r#"{"task_families": {"lookup": {"required_file_families": ["scripts/run_*.py"]}}}"#,
        )
        .unwrap();
        // Two pack files both claim "12 scripts" — both must be reported.
        fs::write(
            current.join("10_SYSTEM_OVERVIEW.md"),
            "# Overview\n\nThe repo has 12 scripts.\n",
        )
        .unwrap();
        fs::write(
            current.join("20_CODE_MAP.md"),
            "# Code Map\n\nWe maintain 12 scripts total.\n",
        )
        .unwrap();

        // Manifest is intentionally the seal-time snapshot; the authoritative
        // signal is the live family_counts (which now resolves to 13).
        let manifest = json!({
            "declared_counts": [
                {"noun": "scripts", "count": 12, "file": "10_SYSTEM_OVERVIEW.md", "line": 3},
                {"noun": "scripts", "count": 12, "file": "20_CODE_MAP.md", "line": 3},
            ],
        });

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_declared_counts_drift(&manifest, &dir, &current, &mut out);

        assert!(
            out.iter().any(|w| w.kind == "DECLARED_COUNT_DRIFT"
                && w.message.contains("12 scripts")
                && w.message.contains("13")),
            "expected DECLARED_COUNT_DRIFT naming '12 scripts' stale vs auth 13, got {out:?}"
        );
        // affected_pack_files should include both stale file names so the
        // reviewer can see every repetition.
        let affected: Vec<String> = out
            .iter()
            .filter(|w| w.kind == "DECLARED_COUNT_DRIFT")
            .flat_map(|w| w.affected_pack_files.clone())
            .collect();
        assert!(
            affected.iter().any(|f| f == "10_SYSTEM_OVERVIEW.md"),
            "expected 10_SYSTEM_OVERVIEW.md in affected_pack_files, got {affected:?}"
        );
        assert!(
            affected.iter().any(|f| f == "20_CODE_MAP.md"),
            "expected 20_CODE_MAP.md in affected_pack_files, got {affected:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p2_signature_drift_fires_on_rename() {
        // Manifest has "calc.py::compute_lift_with_ci". Rename the function
        // in the repo file → drift check must surface both sides (old missing,
        // new added).
        let dir = test_dir("p2_sig_drift");
        let current = p2_init_pack(&dir);
        fs::write(
            dir.join("calc.py"),
            "def compute_lift(data):\n    return 1\n",
        )
        .unwrap();
        // search_scope points at calc.py so parse_shortcut_signatures picks it up.
        fs::write(
            current.join("search_scope.json"),
            r#"{
              "task_families": {
                "lookup": {
                  "verification_shortcuts": {"calc.py": "compute_lift"}
                }
              }
            }"#,
        )
        .unwrap();

        let manifest = json!({
            "shortcut_signatures": {
                "calc.py::compute_lift_with_ci": "def compute_lift_with_ci(data)"
            }
        });

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_shortcut_signatures_drift(&manifest, &dir, &current, &mut out);

        // Old key gone:
        assert!(
            out.iter().any(|w| w.kind == "SIGNATURE_DRIFT"
                && w.message.contains("compute_lift_with_ci")
                && w.message.contains("rename or deletion")),
            "expected SIGNATURE_DRIFT for renamed-out compute_lift_with_ci, got {out:?}"
        );
        // New key present:
        assert!(
            out.iter().any(|w| w.kind == "SIGNATURE_DRIFT"
                && w.message.contains("calc.py::compute_lift")
                && w.message.contains("new function or rename")),
            "expected SIGNATURE_DRIFT for added compute_lift, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p2_dependencies_drift_fires_on_pyproject_change() {
        let dir = test_dir("p2_deps_drift");
        fs::write(dir.join("pyproject.toml"), "[tool.poetry]\nname=\"v1\"\n").unwrap();
        // Seal-time hash is the hash of the *old* file contents; compute it
        // against a different string so drift is guaranteed to fire.
        let manifest = json!({
            "dependencies_snapshot": {"pyproject": "a".repeat(64)}
        });

        let mut out: Vec<StructuralWarning> = Vec::new();
        check_dependencies_drift(&manifest, &dir, &mut out);

        assert!(
            out.iter().any(|w| w.kind == "DEPENDENCIES_DRIFT"
                && w.message.contains("pyproject")
                && w.message.contains("40_OPERATIONS_AND_RELEASE.md")),
            "expected DEPENDENCIES_DRIFT naming pyproject + 40_OPERATIONS_AND_RELEASE.md, got {out:?}"
        );
        // affected_pack_files points at operations md.
        assert!(
            out.iter().any(|w| w
                .affected_pack_files
                .iter()
                .any(|f| f == "40_OPERATIONS_AND_RELEASE.md")),
            "expected affected_pack_files to include 40_OPERATIONS_AND_RELEASE.md, got {out:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p2_structural_warnings_as_json_shape_is_stable() {
        // CI JSON contract: each warning has {kind, message, affected_pack_files}.
        let warnings = vec![StructuralWarning {
            kind: "TEMPLATE_MARKER".to_string(),
            message: "marker found".to_string(),
            affected_pack_files: vec!["routes.json".to_string()],
        }];
        let v = structural_warnings_as_json(&warnings);
        let arr = v.as_array().expect("must be an array");
        assert_eq!(arr.len(), 1);
        let obj = arr[0].as_object().expect("must be an object");
        assert_eq!(obj.get("kind").and_then(|v| v.as_str()), Some("TEMPLATE_MARKER"));
        assert_eq!(
            obj.get("message").and_then(|v| v.as_str()),
            Some("marker found")
        );
        let affected = obj
            .get("affected_pack_files")
            .and_then(|v| v.as_array())
            .expect("affected_pack_files must be an array");
        assert_eq!(affected.len(), 1);
        assert_eq!(affected[0].as_str(), Some("routes.json"));
    }

    // --- P4 tests (restored post-integration) ---


    #[test]
    fn p4_install_settings_template_is_idempotent() {
        let dir = test_dir("p4_install_idempotent");
        install_settings_template(dir.to_str().unwrap(), false).unwrap();
        let first = fs::read_to_string(dir.join(".claude/settings.json")).unwrap();

        install_settings_template(dir.to_str().unwrap(), false).unwrap();
        let second = fs::read_to_string(dir.join(".claude/settings.json")).unwrap();
        assert_eq!(
            first, second,
            "running install-settings-template twice must produce identical bytes"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p4_acceptance_tests_graceful_degrade_when_p2_absent() {
        // No signature_drift.json on disk → every test stays `ready`
        // regardless of its invalidated_by entries. This is the graceful
        // degradation contract documented inline on evaluate_acceptance_tests.
        let dir = test_dir("p4_acceptance_no_p2");
        let current = dir.join(".agent-context/current");
        fs::create_dir_all(&current).unwrap();
        fs::write(
            current.join("acceptance_tests.md"),
            "### test: t1\n- invalidated_by: fn_a, fn_b\n",
        )
        .unwrap();

        let drifted = collect_signature_drift(&current);
        assert!(
            drifted.is_empty(),
            "absence of signature_drift.json must yield an empty set, got {drifted:?}"
        );

        let tests = evaluate_acceptance_tests(&current, &drifted);
        assert_eq!(tests.len(), 1);
        assert_eq!(
            tests[0]["status"].as_str(),
            Some("ready"),
            "without P2 drift signal, tests stay ready even when invalidated_by is populated"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p4_install_settings_template_creates_new_file() {
        let dir = test_dir("p4_install_new");
        install_settings_template(dir.to_str().unwrap(), false)
            .expect("install must succeed when no settings.json exists");

        let path = dir.join(".claude/settings.json");
        assert!(path.exists(), "settings.json must be created");
        let raw = fs::read_to_string(&path).unwrap();
        let parsed: Value = serde_json::from_str(&raw).expect("must be valid JSON");
        let pre_tool_use = parsed
            .get("hooks")
            .and_then(|h| h.get("PreToolUse"))
            .and_then(|v| v.as_array())
            .expect("hooks.PreToolUse must be present");
        assert!(
            pre_tool_use.len() >= 2,
            "shipped template should install both Edit|Write and Bash matchers, got {} entries",
            pre_tool_use.len()
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p4_install_settings_template_preserves_existing_keys() {
        let dir = test_dir("p4_install_merge");
        let claude_dir = dir.join(".claude");
        fs::create_dir_all(&claude_dir).unwrap();
        let existing = json!({
            "permissions": {"allow": ["Bash(ls:*)"]},
            "env": {"DEBUG": "false"},
            "hooks": {
                "PostToolUse": [
                    {"matcher": "Edit", "hooks": [{"type": "command", "command": "echo hi"}]}
                ]
            }
        });
        fs::write(
            claude_dir.join("settings.json"),
            format!("{}\n", serde_json::to_string_pretty(&existing).unwrap()),
        )
        .unwrap();

        install_settings_template(dir.to_str().unwrap(), false)
            .expect("merge must succeed when settings.json exists");

        let raw = fs::read_to_string(claude_dir.join("settings.json")).unwrap();
        let parsed: Value = serde_json::from_str(&raw).unwrap();

        // Existing non-hooks keys preserved verbatim.
        assert_eq!(
            parsed
                .get("permissions")
                .and_then(|p| p.get("allow"))
                .and_then(|a| a.as_array())
                .map(|a| a.len()),
            Some(1),
            "permissions.allow must be preserved, got {parsed}"
        );
        assert_eq!(
            parsed.get("env").and_then(|e| e.get("DEBUG")).and_then(|v| v.as_str()),
            Some("false"),
            "env.DEBUG must be preserved"
        );
        // Existing hooks.PostToolUse stays intact.
        assert!(
            parsed
                .get("hooks")
                .and_then(|h| h.get("PostToolUse"))
                .and_then(|v| v.as_array())
                .map(|a| !a.is_empty())
                .unwrap_or(false),
            "existing hooks.PostToolUse must survive the merge"
        );
        // PreToolUse entries added by the template are now present.
        let pre = parsed
            .get("hooks")
            .and_then(|h| h.get("PreToolUse"))
            .and_then(|v| v.as_array())
            .expect("hooks.PreToolUse must be inserted");
        assert!(
            pre.iter().any(|e| e.get("matcher").and_then(|v| v.as_str()) == Some("Edit|Write")),
            "Edit|Write matcher must be present after merge, got {pre:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p4_merge_settings_template_dedupes_existing_command() {
        // A user who already added the chorus PreToolUse hook (e.g. by hand)
        // should see no duplicate on subsequent install.
        let template: Value = serde_json::from_str(SETTINGS_TEMPLATE_JSON).unwrap();
        let existing = template.clone();
        let merged = merge_settings_template(&existing, &template);
        let pre = merged
            .get("hooks")
            .and_then(|h| h.get("PreToolUse"))
            .and_then(|v| v.as_array())
            .expect("PreToolUse must be present");
        let template_pre_len = template
            .get("hooks")
            .and_then(|h| h.get("PreToolUse"))
            .and_then(|v| v.as_array())
            .map(|a| a.len())
            .unwrap();
        assert_eq!(
            pre.len(),
            template_pre_len,
            "merging the template with itself must not double entries"
        );
    }

    #[test]
    fn p4_acceptance_tests_missing_file_returns_empty() {
        let dir = test_dir("p4_acceptance_missing");
        let current = dir.join(".agent-context/current");
        fs::create_dir_all(&current).unwrap();
        let drifted = std::collections::BTreeSet::new();
        let tests = evaluate_acceptance_tests(&current, &drifted);
        assert!(tests.is_empty(), "missing file → empty list");
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p4_acceptance_tests_needs_revalidation_on_drift() {
        // acceptance_tests.md declares two tests; one lists `compute_lift`
        // in its invalidated_by field. When signature_drift.json names
        // `compute_lift`, the corresponding test must flip to
        // `needs_revalidation`; the other test must stay `ready`.
        let dir = test_dir("p4_acceptance_drift");
        let current = dir.join(".agent-context/current");
        fs::create_dir_all(&current).unwrap();

        fs::write(
            current.join("acceptance_tests.md"),
            "# Acceptance Tests\n\n\
             ### test: verify_lift_calculation\n\
             - invalidated_by: compute_lift, format_result\n\n\
             ### test: verify_summary_shape\n\
             - invalidated_by: render_summary\n",
        )
        .unwrap();

        // P2 drift signal — only compute_lift drifted.
        fs::write(
            current.join("signature_drift.json"),
            r#"{"drifted_functions": ["compute_lift"]}"#,
        )
        .unwrap();

        let drifted = collect_signature_drift(&current);
        assert!(drifted.contains("compute_lift"));

        let tests = evaluate_acceptance_tests(&current, &drifted);
        assert_eq!(tests.len(), 2, "both tests must be surfaced, got {tests:?}");

        let lift = tests
            .iter()
            .find(|t| t["id"] == "verify_lift_calculation")
            .expect("lift test must be present");
        assert_eq!(
            lift["status"].as_str(),
            Some("needs_revalidation"),
            "drift in compute_lift must flip verify_lift_calculation to needs_revalidation"
        );
        let matched: Vec<&str> = lift["invalidated_by_matched"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str())
            .collect();
        assert_eq!(matched, vec!["compute_lift"]);

        let summary = tests
            .iter()
            .find(|t| t["id"] == "verify_summary_shape")
            .expect("summary test must be present");
        assert_eq!(
            summary["status"].as_str(),
            Some("ready"),
            "no overlap with drifted set → stays ready"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    // --- P6 tests (restored post-integration) ---


    /// A PR range with only pure-code and only pure-pack commits passes the
    /// separate-commit gate. A commit that mixes the two is reported with the
    /// `commit <sha> mixes pack + non-pack changes` message.
    #[test]
    fn p6_check_separate_commits_reports_mixed_commit() {
        let repo = init_repo_with_commits("p6_separate_commits_mixed");
        // Capture the pre-range tip — we'll diff against it as `base`.
        let base = run_git(&["rev-parse", "HEAD"], &repo, true)
            .expect("base sha")
            .trim()
            .to_string();

        // Pure-code commit (passes).
        commit_file(&repo, "src/pure_code.rs", "fn a() {}\n");

        // Pure-pack commit (passes).
        fs::create_dir_all(repo.join(".agent-context/current")).unwrap();
        commit_file(
            &repo,
            ".agent-context/current/20_CODE_MAP.md",
            "# pack content\n",
        );

        // Mixed commit: touches both a pack file and a non-pack file in a
        // single commit. This is the case the gate must flag.
        fs::write(repo.join("src/pure_code.rs"), "fn a() -> i32 { 0 }\n").unwrap();
        fs::write(
            repo.join(".agent-context/current/20_CODE_MAP.md"),
            "# pack content v2\n",
        )
        .unwrap();
        let run = |args: &[&str]| {
            Command::new("git")
                .args(args)
                .current_dir(&repo)
                .output()
                .expect("git")
        };
        run(&["add", "-A"]);
        run(&["commit", "-q", "-m", "mix: pack and code in one commit"]);
        let mixed_sha = run_git(&["rev-parse", "HEAD"], &repo, true)
            .expect("mixed sha")
            .trim()
            .to_string();

        let errors = check_separate_commits(&base, &repo).expect("separate-commit check runs");
        assert_eq!(errors.len(), 1, "only one commit should fail the gate: {errors:?}");
        let msg = &errors[0];
        assert!(
            msg.contains(&mixed_sha),
            "error message must name the offending sha ({mixed_sha}), got {msg:?}"
        );
        assert!(
            msg.contains("mixes pack + non-pack changes"),
            "error message must use the documented phrase, got {msg:?}"
        );

        let _ = fs::remove_dir_all(&repo);
    }

    /// `write_last_freshness_state` creates the state file with the three
    /// documented keys when the pack directory exists, and silently
    /// no-ops when it does not. Covers the "warn → state file" path.
    #[test]
    fn p6_write_last_freshness_state_emits_payload() {
        let dir = test_dir("p6_write_state");
        let current = dir.join(".agent-context").join("current");
        fs::create_dir_all(&current).expect("create pack dir");

        let result = FreshnessResult {
            status: "warn".to_string(),
            changed_files: vec!["src/foo.rs".to_string(), "src/bar.rs".to_string()],
            pack_updated: false,
            skipped_reason: None,
            affected_sections: vec![
                "20_CODE_MAP.md".to_string(),
                "30_BEHAVIORAL_INVARIANTS.md".to_string(),
            ],
        };
        write_last_freshness_state(&dir, &result);

        let state_path = current.join(".last_freshness.json");
        assert!(state_path.exists(), "state file must be created");
        let body = fs::read_to_string(&state_path).expect("read state file");
        let parsed: Value = serde_json::from_str(&body).expect("state file must be valid json");
        let changed = parsed
            .get("changed_files")
            .and_then(|v| v.as_array())
            .expect("changed_files array");
        assert_eq!(changed.len(), 2);
        let sections = parsed
            .get("affected_sections")
            .and_then(|v| v.as_array())
            .expect("affected_sections array");
        assert_eq!(sections.len(), 2);
        assert!(
            parsed.get("timestamp").and_then(|v| v.as_u64()).is_some(),
            "timestamp must be a number"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    /// The generated pre-push hook must include the P6 pack-only skip
    /// shortcut so users who inspect the hook can see the behavior. We
    /// pin on the sentinel strings the hook's callers expect rather than
    /// the whole body, so surrounding hook content can evolve.
    #[test]
    fn p6_hook_section_contains_pack_only_skip_logic() {
        let hook = build_pre_push_hook_section();
        assert!(
            hook.contains("pack_only_skip"),
            "hook must define pack_only_skip helper, got:\n{hook}"
        );
        assert!(
            hook.contains("pack-only push, skipping freshness check"),
            "hook must announce the skip when triggered, got:\n{hook}"
        );
        assert!(
            hook.contains(".agent-context/current/.last_freshness.json"),
            "hook must read the last-freshness state file, got:\n{hook}"
        );
        assert!(
            hook.contains("warning appears addressed"),
            "hook must report when prior warning is addressed, got:\n{hook}"
        );
    }

    /// P12 / F44 — shell hygiene for the generated pre-push hook.
    /// Verifies `set -u`, quoted env interpolations, and the `--` separator
    /// in the git diff call. Uses `bash -n` to syntax-check the generated
    /// body so authoring regressions land as test failures rather than at
    /// install time.
    #[test]
    fn p12_pre_push_hook_has_set_u_quoted_interpolations_and_dashdash() {
        let hook = build_pre_push_hook_section();
        assert!(
            hook.starts_with("set -u"),
            "hook must start with `set -u` for fail-fast semantics, got:\n{hook}"
        );
        // `$remote_name`/`$remote_url` appear inside a double-quoted echo
        // string, so they are implicitly quoted. Guard against a regression
        // where someone unquotes them by looking for the classic bare-var
        // form outside of quotes.
        assert!(
            !hook.contains(" $remote_name ") && !hook.contains(" $remote_url "),
            "hook must not leave `$remote_name`/`$remote_url` unquoted bare, got:\n{hook}"
        );
        assert!(
            hook.contains("git diff --name-only \"${base}..${head}\" --"),
            "hook must pass `--` to git diff, got:\n{hook}"
        );
        // Also guard against regressions that might drop quotes around
        // `$base` / `$head` (the classic `$var` → injection pitfall).
        assert!(
            !hook.contains("git diff --name-only $base..$head"),
            "hook must not leave `$base..$head` unquoted"
        );

        // Static `bash -n` syntax check — fails loudly on quoting
        // regressions that the substring asserts above would miss.
        // Skip silently if `bash` is not on PATH (e.g. exotic CI).
        if Command::new("bash").arg("--version").output().is_ok() {
            let mut body = String::from("#!/usr/bin/env bash\n");
            body.push_str(&hook);
            body.push('\n');
            let tmp = env::temp_dir().join("chorus_p12_hook_syntax_check.sh");
            fs::write(&tmp, &body).unwrap();
            let out = Command::new("bash")
                .args(["-n"])
                .arg(&tmp)
                .output()
                .expect("bash -n should run");
            assert!(
                out.status.success(),
                "generated hook must parse cleanly under `bash -n`: stderr:\n{}",
                String::from_utf8_lossy(&out.stderr)
            );
            let _ = fs::remove_file(&tmp);
        }
    }

    /// `check_separate_commits` is a no-op when the directory is not a git
    /// repo. The surrounding verify flow already reports that case, so this
    /// check simply must not error out.
    #[test]
    fn p6_check_separate_commits_non_git_is_empty() {
        let dir = test_dir("p6_separate_commits_non_git");
        let errors = check_separate_commits("origin/main", &dir)
            .expect("must not fail on non-git directory");
        assert!(
            errors.is_empty(),
            "non-git directory must return empty error list, got {errors:?}"
        );
        let _ = fs::remove_dir_all(&dir);
    }

    /// Non-warn results must not overwrite an existing state file; only a
    /// warn is authoritative. The public API reflects this by guarding the
    /// write behind `result.status == "warn"`.
    #[test]
    fn p6_write_last_freshness_state_skips_non_warn() {
        let dir = test_dir("p6_write_state_noop");
        let current = dir.join(".agent-context").join("current");
        fs::create_dir_all(&current).expect("create pack dir");

        let result = FreshnessResult {
            status: "pass".to_string(),
            changed_files: vec![],
            pack_updated: false,
            skipped_reason: None,
            affected_sections: vec![],
        };
        write_last_freshness_state(&dir, &result);

        let state_path = current.join(".last_freshness.json");
        assert!(
            !state_path.exists(),
            "non-warn result must not write the state file"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    /// When the pack directory does not exist yet, `write_last_freshness_state`
    /// must silently no-op rather than panic. This guards the pre-init case
    /// where a freshness helper fires before the pack has been scaffolded.
    #[test]
    fn p6_write_last_freshness_state_no_pack_dir_is_noop() {
        let dir = test_dir("p6_write_state_no_pack");
        let result = FreshnessResult {
            status: "warn".to_string(),
            changed_files: vec!["src/foo.rs".to_string()],
            pack_updated: false,
            skipped_reason: None,
            affected_sections: vec!["20_CODE_MAP.md".to_string()],
        };
        // Should not panic.
        write_last_freshness_state(&dir, &result);
        assert!(
            !dir.join(".agent-context/current/.last_freshness.json").exists(),
            "state file must not be written when pack dir is absent"
        );
        let _ = fs::remove_dir_all(&dir);
    }

    /// A range where no commit mixes pack + non-pack returns an empty error
    /// list so the CI gate stays silent.
    #[test]
    fn p6_check_separate_commits_clean_range_passes() {
        let repo = init_repo_with_commits("p6_separate_commits_clean");
        let base = run_git(&["rev-parse", "HEAD"], &repo, true)
            .expect("base sha")
            .trim()
            .to_string();

        // Alternating but separate commits — each is either pure code or
        // pure pack, so the gate must not flag them.
        commit_file(&repo, "src/code_a.rs", "fn a() {}\n");
        fs::create_dir_all(repo.join(".agent-context/current")).unwrap();
        commit_file(
            &repo,
            ".agent-context/current/20_CODE_MAP.md",
            "# pack content\n",
        );
        commit_file(&repo, "src/code_b.rs", "fn b() {}\n");

        let errors = check_separate_commits(&base, &repo).expect("separate-commit check runs");
        assert!(
            errors.is_empty(),
            "clean range must produce no errors, got {errors:?}"
        );

        let _ = fs::remove_dir_all(&repo);
    }

    // --- P5 tests (restored post-integration) ---


    #[test]
    fn extract_numeric_claims_flags_stale_prose() {
        let mut authoritative = std::collections::HashMap::new();
        authoritative.insert("files".to_string(), 7usize);
        let content = "We have 6 files in the repo.";
        let mismatches = extract_numeric_claims(content, &authoritative, "10_SYSTEM_OVERVIEW.md");
        assert_eq!(mismatches.len(), 1, "expected one mismatch, got {mismatches:?}");
        let m = &mismatches[0];
        assert_eq!(m.claimed_count, 6);
        assert_eq!(m.authoritative_count, 7);
        assert_eq!(m.noun, "files");
        assert_eq!(m.line, 1);
    }

    #[test]
    fn slug_for_count_key_normalizes_globs() {
        assert_eq!(slug_for_count_key("scripts/run_*.py"), "scripts_run_py");
        assert_eq!(
            slug_for_count_key("src/brand_lift/*.py"),
            "src_brand_lift_py"
        );
        assert_eq!(slug_for_count_key("tests/**/*.py"), "tests_py");
        assert_eq!(slug_for_count_key("docs/methodology"), "docs_methodology");
    }

    #[test]
    fn extract_numeric_claims_respects_ignore_region() {
        let mut authoritative = std::collections::HashMap::new();
        authoritative.insert("files".to_string(), 7usize);
        let content = "\
<!-- count-claim: ignore -->\n\
Historic note: we once shipped 6 files.\n\
<!-- count-claim: end -->\n\
Current count is {{counts.files_py}}.\n";
        let mismatches = extract_numeric_claims(content, &authoritative, "doc.md");
        assert!(
            mismatches.is_empty(),
            "claim inside ignore region must not be flagged, got {mismatches:?}"
        );
    }

    #[test]
    fn extract_numeric_claims_passes_matching_prose() {
        let mut authoritative = std::collections::HashMap::new();
        authoritative.insert("tests".to_string(), 32usize);
        let content = "Currently 32 tests pass.";
        let mismatches = extract_numeric_claims(content, &authoritative, "doc.md");
        assert!(
            mismatches.is_empty(),
            "prose that matches authoritative must not be flagged, got {mismatches:?}"
        );
    }

    #[test]
    fn seal_fails_on_stale_prose_number_without_force() {
        let dir = test_dir("p5_seal_stale");
        init_git(&dir);
        let scripts = dir.join("scripts");
        fs::create_dir_all(&scripts).unwrap();
        for i in 0..12 {
            fs::write(scripts.join(format!("run_{i}.py")), "# runner\n").unwrap();
        }
        let current = dir.join(".agent-context").join("current");
        fs::create_dir_all(&current).unwrap();
        // Required files (P1 schema — 5 markdown).
        fs::write(
            current.join("00_START_HERE.md"),
            "- Branch at generation: `main`\n- HEAD commit: `unknown`\n- Generated at: `never`\n",
        )
        .unwrap();
        fs::write(
            current.join("10_SYSTEM_OVERVIEW.md"),
            "The repo ships 6 scripts today.\n",
        )
        .unwrap();
        fs::write(current.join("20_CODE_MAP.md"), "map\n").unwrap();
        fs::write(current.join("30_BEHAVIORAL_INVARIANTS.md"), "rules\n").unwrap();
        fs::write(current.join("40_OPERATIONS_AND_RELEASE.md"), "ops\n").unwrap();
        // Completeness contract declares the scripts/run_*.py family so P1
        // family_counts resolves to 12 at seal time.
        fs::write(
            current.join("completeness_contract.json"),
            r#"{
              "task_families": {
                "lookup": {"required_file_families": ["scripts/run_*.py"]}
              }
            }"#,
        )
        .unwrap();

        // Seed a commit so HEAD resolves.
        let _ = std::process::Command::new("git")
            .args(["add", "-A"])
            .current_dir(&dir)
            .output();
        let _ = std::process::Command::new("git")
            .args(["commit", "-m", "seed", "--quiet"])
            .current_dir(&dir)
            .output();

        let err = seal(SealOptions {
            reason: None,
            base: None,
            head: None,
            pack_dir: None,
            cwd: Some(dir.to_string_lossy().to_string()),
            force: false,
            force_snapshot: false,
            follow_symlinks: false,
        })
        .expect_err("seal must fail when prose count disagrees with authoritative");
        let msg = format!("{err}");
        assert!(
            msg.contains("disagree")
                || msg.contains("claim")
                || msg.contains("numeric claims"),
            "expected a count-claim failure message, got: {msg}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn expand_count_handlebars_substitutes_known_slugs() {
        let mut counts = std::collections::HashMap::new();
        counts.insert("scripts_run".to_string(), 12usize);
        counts.insert("src_brand_lift".to_string(), 6usize);
        let content = "The repo has {{counts.scripts_run}} scripts and {{counts.src_brand_lift}} brand-lift modules.";
        let expanded = expand_count_handlebars(content, &counts);
        assert_eq!(
            expanded,
            "The repo has 12 scripts and 6 brand-lift modules.",
            "handlebars must expand to authoritative values"
        );
    }

    #[test]
    fn expand_count_handlebars_leaves_unknown_slugs_literal() {
        let counts = std::collections::HashMap::new();
        let content = "No mapping for {{counts.bogus_slug}} here.";
        let expanded = expand_count_handlebars(content, &counts);
        assert_eq!(
            expanded,
            "No mapping for {{counts.bogus_slug}} here.",
            "unknown slugs must remain literal to surface author mistakes"
        );
    }

    #[test]
    fn seal_force_downgrades_stale_prose_to_warning() {
        let dir = test_dir("p5_seal_force");
        init_git(&dir);
        let scripts = dir.join("scripts");
        fs::create_dir_all(&scripts).unwrap();
        for i in 0..12 {
            fs::write(scripts.join(format!("run_{i}.py")), "# runner\n").unwrap();
        }
        let current = dir.join(".agent-context").join("current");
        fs::create_dir_all(&current).unwrap();
        fs::write(
            current.join("00_START_HERE.md"),
            "- Branch at generation: `main`\n- HEAD commit: `unknown`\n- Generated at: `never`\n",
        )
        .unwrap();
        fs::write(
            current.join("10_SYSTEM_OVERVIEW.md"),
            "Legacy prose: 6 scripts ship today.\n",
        )
        .unwrap();
        fs::write(current.join("20_CODE_MAP.md"), "map\n").unwrap();
        fs::write(current.join("30_BEHAVIORAL_INVARIANTS.md"), "rules\n").unwrap();
        fs::write(current.join("40_OPERATIONS_AND_RELEASE.md"), "ops\n").unwrap();
        fs::write(
            current.join("completeness_contract.json"),
            r#"{
              "task_families": {
                "lookup": {"required_file_families": ["scripts/run_*.py"]}
              }
            }"#,
        )
        .unwrap();

        let _ = std::process::Command::new("git")
            .args(["add", "-A"])
            .current_dir(&dir)
            .output();
        let _ = std::process::Command::new("git")
            .args(["commit", "-m", "seed", "--quiet"])
            .current_dir(&dir)
            .output();

        // --force must succeed (mismatches downgraded to warnings).
        seal(SealOptions {
            reason: None,
            base: None,
            head: None,
            pack_dir: None,
            cwd: Some(dir.to_string_lossy().to_string()),
            force: true,
            force_snapshot: false,
            follow_symlinks: false,
        })
        .expect("seal must succeed under --force even with stale prose");

        // Manifest should now exist with family_counts populated.
        let manifest: serde_json::Value = serde_json::from_str(
            &fs::read_to_string(current.join("manifest.json")).unwrap(),
        )
        .unwrap();
        assert_eq!(
            manifest
                .get("family_counts")
                .and_then(|v| v.get("scripts/run_*.py"))
                .and_then(|v| v.as_u64()),
            Some(12),
            "family_counts must be sealed correctly even under --force"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn derive_count_maps_splits_by_slug_and_noun() {
        let mut family = std::collections::BTreeMap::new();
        family.insert("scripts/run_*.py".to_string(), 12usize);
        family.insert("src/brand_lift/*.py".to_string(), 6usize);
        let (slugs, nouns) = derive_count_maps(&family);
        assert_eq!(slugs.get("scripts_run_py").copied(), Some(12));
        assert_eq!(slugs.get("src_brand_lift_py").copied(), Some(6));
        // "scripts" noun sums every slug whose tokens include "scripts"
        // — only scripts/run_*.py does, so authoritative is 12.
        assert_eq!(nouns.get("scripts").copied(), Some(12));
        // "brands" noun would match only `brand` / `brands` slug tokens; the
        // brand_lift slug contains "brand" so it's included via singular.
        assert_eq!(nouns.get("brand").copied(), Some(6));
    }

    #[test]
    fn seal_expands_handlebars_into_sealed_pack() {
        let dir = test_dir("p5_seal_expand");
        init_git(&dir);
        let scripts = dir.join("scripts");
        fs::create_dir_all(&scripts).unwrap();
        for i in 0..12 {
            fs::write(scripts.join(format!("run_{i}.py")), "# runner\n").unwrap();
        }
        let current = dir.join(".agent-context").join("current");
        fs::create_dir_all(&current).unwrap();
        fs::write(
            current.join("00_START_HERE.md"),
            "- Branch at generation: `main`\n- HEAD commit: `unknown`\n- Generated at: `never`\n",
        )
        .unwrap();
        fs::write(
            current.join("10_SYSTEM_OVERVIEW.md"),
            "We ship {{counts.scripts_run_py}} scripts.\n",
        )
        .unwrap();
        fs::write(current.join("20_CODE_MAP.md"), "map\n").unwrap();
        fs::write(current.join("30_BEHAVIORAL_INVARIANTS.md"), "rules\n").unwrap();
        fs::write(current.join("40_OPERATIONS_AND_RELEASE.md"), "ops\n").unwrap();
        fs::write(
            current.join("completeness_contract.json"),
            r#"{
              "task_families": {
                "lookup": {"required_file_families": ["scripts/run_*.py"]}
              }
            }"#,
        )
        .unwrap();

        let _ = std::process::Command::new("git")
            .args(["add", "-A"])
            .current_dir(&dir)
            .output();
        let _ = std::process::Command::new("git")
            .args(["commit", "-m", "seed", "--quiet"])
            .current_dir(&dir)
            .output();

        seal(SealOptions {
            reason: None,
            base: None,
            head: None,
            pack_dir: None,
            cwd: Some(dir.to_string_lossy().to_string()),
            force: false,
            force_snapshot: false,
            follow_symlinks: false,
        })
        .expect("seal with expanded handlebars must succeed");

        let expanded_body =
            fs::read_to_string(current.join("10_SYSTEM_OVERVIEW.md")).unwrap();
        assert!(
            expanded_body.contains("We ship 12 scripts."),
            "handlebar must be expanded in the sealed file body, got: {expanded_body:?}"
        );
        assert!(
            !expanded_body.contains("{{counts."),
            "no handlebar should survive seal, got: {expanded_body:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }


    #[test]
    fn extract_numeric_claims_skips_expanded_counts_matches() {
        let mut authoritative = std::collections::HashMap::new();
        authoritative.insert("scripts".to_string(), 12usize);
        let mut slug_counts = std::collections::HashMap::new();
        slug_counts.insert("scripts_run".to_string(), 12usize);
        // Author wrote the handlebar; after expansion the number is the
        // authoritative 12, which must not be flagged as a mismatch.
        let authored = "We ship {{counts.scripts_run}} scripts.";
        let expanded = expand_count_handlebars(authored, &slug_counts);
        assert_eq!(expanded, "We ship 12 scripts.");
        let mismatches = extract_numeric_claims(&expanded, &authoritative, "doc.md");
        assert!(
            mismatches.is_empty(),
            "expanded handlebar value must match authoritative without being flagged, got {mismatches:?}"
        );
    }

    // --- P7 tests (restored post-integration) ---

    fn commit_and_sha(root: &Path, rel: &str, content: &str) -> String {
        commit_file(root, rel, content);
        let out = std::process::Command::new("git")
            .args(["rev-parse", "HEAD"])
            .current_dir(root)
            .output()
            .unwrap();
        String::from_utf8_lossy(&out.stdout).trim().to_string()
    }

    fn seed_manifest(root: &Path, head_sha_at_seal: &str) {
        let current = root.join(".agent-context").join("current");
        fs::create_dir_all(&current).unwrap();
        let manifest = json!({
            "schema_version": CURRENT_SCHEMA_VERSION,
            "head_sha": head_sha_at_seal,
            "head_sha_at_seal": head_sha_at_seal,
            "post_commit_sha": null,
            "files": [],
        });
        fs::write(
            current.join("manifest.json"),
            format!("{}\n", serde_json::to_string_pretty(&manifest).unwrap()),
        )
        .unwrap();
    }



    #[test]
    fn diff_since_seal_returns_empty_zones_when_no_changes() {
        let dir = test_dir("p7_no_changes");
        init_git(&dir);
        // Commit an initial seed + a second commit so HEAD~1 resolves; the
        // seal baseline points at the LATEST commit so the diff is empty.
        commit_file(&dir, "seed.txt", "one");
        commit_file(&dir, "second.txt", "two");
        write_relevance_json(
            &dir,
            r#"{"zones":[{"paths":["src/**"],"affects":["20_CODE_MAP.md"]}]}"#,
        );
        // A `src/` file must exist so zone validation can pass when loaded
        // elsewhere; diff_since_seal itself doesn't validate zones here.
        commit_file(&dir, "src/lib.rs", "fn a() {}\n");
        // Re-seal baseline to THIS commit so there's nothing newer to diff.
        let latest = run_git(&["rev-parse", "HEAD"], &dir, true).unwrap();
        seed_manifest(&dir, &latest);

        let out = diff_since_seal(&dir, None).expect("diff_since_seal must succeed");
        let zones = out
            .value
            .get("zones")
            .and_then(|v| v.as_array())
            .expect("zones array");
        assert!(zones.is_empty(), "no changes must yield empty zones, got {zones:?}");
        assert!(
            out.acceptance_tests_invalidated.is_empty(),
            "no drifts -> empty acceptance_tests_invalidated"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn parse_acceptance_invalidated_by_reads_list_markers() {
        let dir = test_dir("p7_parse_invalidated");
        let current = dir.join(".agent-context").join("current");
        fs::create_dir_all(&current).unwrap();
        let body = "\
# Acceptance tests\n\
\n\
## Q1: lookup the release flow\n\
- invalidated_by: publish_release\n\
\n\
## Q2: rename impact\n\
  invalidated_by: compute_lift_with_ci, resolve_sample\n\
\n\
## Q3: unrelated\n\
(no invalidated_by marker)\n\
";
        fs::write(current.join("acceptance_tests.md"), body).unwrap();

        let entries = parse_acceptance_invalidated_by(&current);
        assert_eq!(entries.len(), 2, "expected 2 bindings, got {entries:?}");
        let q1 = &entries[0];
        assert_eq!(
            q1.get("test_id").and_then(|v| v.as_str()),
            Some("Q1: lookup the release flow")
        );
        let q1_fns: Vec<String> = q1
            .get("invalidated_by")
            .and_then(|v| v.as_array())
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str().map(String::from))
            .collect();
        assert_eq!(q1_fns, vec!["publish_release"]);
        let q2 = &entries[1];
        let q2_fns: Vec<String> = q2
            .get("invalidated_by")
            .and_then(|v| v.as_array())
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str().map(String::from))
            .collect();
        assert_eq!(q2_fns, vec!["compute_lift_with_ci", "resolve_sample"]);

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn diff_since_seal_groups_changes_by_zone() {
        let dir = test_dir("p7_zone_grouping");
        init_git(&dir);
        // Seed commit so HEAD~1 is available and baseline has a predecessor.
        commit_file(&dir, "seed.txt", "seed");
        // Seed a file in the src/ zone so the zone map validates downstream.
        let baseline_sha = commit_and_sha(&dir, "src/existing.rs", "fn a() {}\n");

        write_relevance_json(
            &dir,
            r#"{"zones":[
              {"paths":["src/**"],"affects":["20_CODE_MAP.md"]},
              {"paths":["docs/**"],"affects":["10_SYSTEM_OVERVIEW.md"]}
            ]}"#,
        );
        seed_manifest(&dir, &baseline_sha);

        // Add a src file and a docs file AFTER seal.
        commit_file(&dir, "src/new_module.rs", "fn b() {}\n");
        commit_file(&dir, "docs/intro.md", "# Intro\n");

        let out = diff_since_seal(&dir, None).expect("diff_since_seal must succeed");
        let zones = out
            .value
            .get("zones")
            .and_then(|v| v.as_array())
            .expect("zones array");
        assert_eq!(zones.len(), 2, "expected 2 populated zones, got {zones:?}");

        // Check each zone carries its own changed_files + affects.
        let find_zone = |affect: &str| -> &Value {
            zones
                .iter()
                .find(|z| {
                    z.get("affects")
                        .and_then(|v| v.as_array())
                        .map(|a| a.iter().any(|s| s.as_str() == Some(affect)))
                        .unwrap_or(false)
                })
                .expect("zone present")
        };
        let src_zone = find_zone("20_CODE_MAP.md");
        let src_changed: Vec<String> = src_zone
            .get("changed_files")
            .and_then(|v| v.as_array())
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str().map(String::from))
            .collect();
        assert!(
            src_changed.iter().any(|f| f == "src/new_module.rs"),
            "src zone must include src/new_module.rs, got {src_changed:?}"
        );
        let docs_zone = find_zone("10_SYSTEM_OVERVIEW.md");
        let docs_changed: Vec<String> = docs_zone
            .get("changed_files")
            .and_then(|v| v.as_array())
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str().map(String::from))
            .collect();
        assert_eq!(docs_changed, vec!["docs/intro.md"]);

        // P2 degradation: empty drift/count/delete arrays present and typed.
        for zone in zones {
            for field in ["signature_drifts", "count_deltas", "deleted_files"] {
                assert!(
                    zone.get(field).and_then(|v| v.as_array()).is_some(),
                    "zone must emit {field} as an array (possibly empty), zone={zone:?}"
                );
            }
        }

        // Recommended actions should include a re-seal reminder since pack
        // wasn't updated in this diff.
        let actions: Vec<String> = out
            .value
            .get("recommended_reconciliation_actions")
            .and_then(|v| v.as_array())
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str().map(String::from))
            .collect();
        assert!(
            actions.iter().any(|s| s.contains("Re-seal")),
            "expected a re-seal reminder in actions, got {actions:?}"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn verify_ci_fails_when_acceptance_test_invalidated_and_pack_stale() {
        // End-to-end: signature drift in a function listed by
        // acceptance_tests.md `invalidated_by` should make `verify --ci`
        // exit non-zero when the pack wasn't updated. We prove this by
        // inspecting the computed diff payload directly — going through the
        // actual `verify` would std::process::exit, which we can't capture
        // in a unit test.
        let dir = test_dir("p7_ci_gate");
        init_git(&dir);
        commit_file(&dir, "seed.txt", "s");
        // Seed a file in the src zone so the initial zone passes validation.
        let baseline_sha = commit_and_sha(&dir, "src/lib.rs", "fn compute_lift_with_ci() {}\n");
        write_relevance_json(
            &dir,
            r#"{"zones":[{"paths":["src/**"],"affects":["20_CODE_MAP.md"]}]}"#,
        );

        // Manually craft a drift payload inside the manifest so we simulate
        // what a P2-integrated pipeline would produce. diff_since_seal doesn't
        // (yet) produce drifts itself (TODO(P2-integration)), so we exercise
        // the matcher + gate directly.
        let bindings = vec![json!({
            "test_id": "Q: rename impact",
            "invalidated_by": ["compute_lift_with_ci"],
        })];
        let drifts = vec![json!({"fn": "compute_lift_with_ci"})];
        let invalidated = match_invalidated_tests(&drifts, &bindings);
        assert_eq!(
            invalidated.len(),
            1,
            "matcher must flag the drifted acceptance test"
        );

        // And confirm diff_since_seal returns pack_updated=false in this
        // scenario (pack wasn't touched after seal).
        seed_manifest(&dir, &baseline_sha);
        commit_file(&dir, "src/new.rs", "fn b() {}\n");
        let out = diff_since_seal(&dir, None).expect("diff_since_seal must succeed");
        assert_eq!(
            out.value.get("pack_updated").and_then(|v| v.as_bool()),
            Some(false),
            "expected pack_updated=false when only code changed"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn resolve_seal_baseline_sha_prefers_post_commit_sha() {
        let with_post = json!({
            "head_sha_at_seal": "aaaa",
            "post_commit_sha": "bbbb",
        });
        assert_eq!(resolve_seal_baseline_sha(&with_post).as_deref(), Some("bbbb"));

        let only_seal = json!({
            "head_sha_at_seal": "aaaa",
            "post_commit_sha": null,
        });
        assert_eq!(resolve_seal_baseline_sha(&only_seal).as_deref(), Some("aaaa"));

        let neither = json!({"foo": "bar"});
        assert!(resolve_seal_baseline_sha(&neither).is_none());
    }

    #[test]
    fn match_invalidated_tests_surfaces_drifted_function() {
        // Simulate P4 acceptance-test schema + a P2-style drift payload.
        let bindings = vec![json!({
            "test_id": "Q2: what changes for a rename?",
            "invalidated_by": ["compute_lift_with_ci", "resolve_sample"],
        })];
        // Drift shape 1: {file, fn}
        let drifts_obj = vec![json!({"file": "src/lib.rs", "fn": "compute_lift_with_ci"})];
        let hits = match_invalidated_tests(&drifts_obj, &bindings);
        assert_eq!(hits.len(), 1, "expected one matched test, got {hits:?}");
        let matched: Vec<String> = hits[0]
            .get("matched_drifts")
            .and_then(|v| v.as_array())
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str().map(String::from))
            .collect();
        assert!(matched.contains(&"compute_lift_with_ci".to_string()));

        // Drift shape 2: bare string. Still matches.
        let drifts_str = vec![json!("resolve_sample")];
        let hits2 = match_invalidated_tests(&drifts_str, &bindings);
        assert_eq!(hits2.len(), 1);

        // Non-matching drift yields no invalidation.
        let drifts_none = vec![json!("unrelated_fn")];
        let hits3 = match_invalidated_tests(&drifts_none, &bindings);
        assert!(hits3.is_empty(), "unrelated drift must not match, got {hits3:?}");
    }

    // -------------------------------------------------------------------------
    // P13 — Authoring ergonomics & lifecycle
    // -------------------------------------------------------------------------

    /// Bootstrap a minimal git repo so `init()` (which requires git) succeeds
    /// under test. Returns the repo root path.
    fn init_bare_git_repo(name: &str) -> PathBuf {
        let dir = test_dir(name);
        let run = |args: &[&str]| {
            Command::new("git")
                .args(args)
                .current_dir(&dir)
                .output()
                .expect("git runs in test");
        };
        run(&["init", "-q"]);
        run(&["config", "user.email", "test@example.com"]);
        run(&["config", "user.name", "P13 Test"]);
        run(&["config", "commit.gpgsign", "false"]);
        fs::write(dir.join("README.md"), "initial\n").unwrap();
        run(&["add", "README.md"]);
        run(&["commit", "-q", "-m", "initial"]);
        dir
    }

    #[test]
    fn p13_init_tier_one_scaffolds_minimal_pack() {
        let repo = init_bare_git_repo("p13_init_tier_one");
        init(InitOptions {
            pack_dir: None,
            cwd: Some(repo.display().to_string()),
            force: false,
            follow_symlinks: false,
            tier: InitTier::One,
        })
        .expect("tier-1 init succeeds");

        let current = repo.join(".agent-context").join("current");
        assert!(current.join("20_CODE_MAP.md").exists(), "tier 1 ships CODE_MAP");
        assert!(current.join("routes.json").exists(), "tier 1 ships routes.json");

        // Files intentionally excluded from tier 1.
        for name in [
            "00_START_HERE.md",
            "10_SYSTEM_OVERVIEW.md",
            "30_BEHAVIORAL_INVARIANTS.md",
            "40_OPERATIONS_AND_RELEASE.md",
            "completeness_contract.json",
            "reporting_rules.json",
            "search_scope.json",
        ] {
            assert!(
                !current.join(name).exists(),
                "tier 1 should NOT ship {name}"
            );
        }
        let _ = fs::remove_dir_all(&repo);
    }

    #[test]
    fn p13_init_tier_two_scaffolds_tier_one_plus_invariants_and_contract() {
        let repo = init_bare_git_repo("p13_init_tier_two");
        init(InitOptions {
            pack_dir: None,
            cwd: Some(repo.display().to_string()),
            force: false,
            follow_symlinks: false,
            tier: InitTier::Two,
        })
        .expect("tier-2 init succeeds");

        let current = repo.join(".agent-context").join("current");
        // Tier 1 core.
        assert!(current.join("20_CODE_MAP.md").exists());
        assert!(current.join("routes.json").exists());
        // Tier 2 additions.
        assert!(current.join("30_BEHAVIORAL_INVARIANTS.md").exists());
        assert!(current.join("completeness_contract.json").exists());
        // Still excluded in tier 2.
        for name in [
            "00_START_HERE.md",
            "10_SYSTEM_OVERVIEW.md",
            "40_OPERATIONS_AND_RELEASE.md",
            "reporting_rules.json",
            "search_scope.json",
        ] {
            assert!(
                !current.join(name).exists(),
                "tier 2 should NOT ship {name}"
            );
        }
        let _ = fs::remove_dir_all(&repo);
    }

    #[test]
    fn p13_init_tier_three_scaffolds_full_pack() {
        let repo = init_bare_git_repo("p13_init_tier_three");
        init(InitOptions {
            pack_dir: None,
            cwd: Some(repo.display().to_string()),
            force: false,
            follow_symlinks: false,
            tier: InitTier::Three,
        })
        .expect("tier-3 init succeeds");

        let current = repo.join(".agent-context").join("current");
        // Tier 3 must equal the pre-P13 full pack.
        for name in [
            "00_START_HERE.md",
            "10_SYSTEM_OVERVIEW.md",
            "20_CODE_MAP.md",
            "30_BEHAVIORAL_INVARIANTS.md",
            "40_OPERATIONS_AND_RELEASE.md",
            "routes.json",
            "completeness_contract.json",
            "reporting_rules.json",
            "search_scope.json",
        ] {
            assert!(
                current.join(name).exists(),
                "tier 3 must ship {name} to match legacy behavior"
            );
        }
        let _ = fs::remove_dir_all(&repo);
    }

    #[test]
    fn p13_routing_templates_include_session_start_freshness_gate() {
        // F47 — every shipped routing block must start with the mandatory
        // manifest/HEAD divergence check so agents cannot reason on a stale
        // pack.
        for agent in ["claude", "codex", "gemini"] {
            let block = build_context_pack_routing_block(agent);
            assert!(
                block.contains("head_sha_at_seal"),
                "{agent} routing block missing `head_sha_at_seal` reference: {block}"
            );
            assert!(
                block.contains("git rev-parse HEAD"),
                "{agent} routing block missing `git rev-parse HEAD` reference"
            );
            assert!(
                block.contains("Before any reasoning"),
                "{agent} routing block missing the freshness preamble"
            );
            assert!(
                block.contains("warn the user"),
                "{agent} routing block missing the `warn the user` escalation clause"
            );
        }
    }

    #[test]
    fn p13_verify_accepts_aliased_pack_filename() {
        // F50 — when the manifest carries an `aliases` map and the canonical
        // file is missing but its alias exists on disk, resolve_file_with_alias
        // should walk the alias.
        let dir = test_dir("p13_verify_alias");
        let current = dir.join("current");
        fs::create_dir_all(&current).unwrap();
        // Only the aliased on-disk file exists.
        fs::write(current.join("20_architecture.md"), "aliased\n").unwrap();

        let manifest = json!({
            "aliases": { "20_CODE_MAP.md": "20_architecture.md" },
        });
        let aliases = resolve_pack_aliases(&manifest);
        assert_eq!(
            aliases.get("20_CODE_MAP.md"),
            Some(&"20_architecture.md".to_string()),
            "alias map must be parsed from manifest"
        );

        let (resolved, alias_used) =
            resolve_file_with_alias(&current, "20_CODE_MAP.md", &aliases);
        assert!(
            resolved.ends_with("20_architecture.md"),
            "resolved path must point at the alias target, got {resolved:?}"
        );
        assert_eq!(alias_used.as_deref(), Some("20_architecture.md"));

        // Without an alias, a missing file falls back to the canonical path.
        let empty_aliases = std::collections::BTreeMap::new();
        let (fallback, no_alias) =
            resolve_file_with_alias(&current, "40_OPERATIONS_AND_RELEASE.md", &empty_aliases);
        assert!(fallback.ends_with("40_OPERATIONS_AND_RELEASE.md"));
        assert!(no_alias.is_none());

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p13_update_last_known_good_writes_sha_into_manifest() {
        // F58 — the verify --ci success path promotes head_sha_at_seal into
        // last_known_good_sha via this helper. The write must be atomic and
        // must not clobber the rest of the manifest.
        let dir = test_dir("p13_update_last_known_good");
        let manifest_path = dir.join("manifest.json");
        let initial = json!({
            "schema_version": 1,
            "head_sha_at_seal": "abc123",
            "last_known_good_sha": null,
            "aliases": {},
            "files": [],
        });
        fs::write(
            &manifest_path,
            serde_json::to_string_pretty(&initial).unwrap(),
        )
        .unwrap();

        update_last_known_good(&manifest_path, "abc123").expect("update succeeds");

        let after: Value =
            serde_json::from_str(&fs::read_to_string(&manifest_path).unwrap()).unwrap();
        assert_eq!(
            after.get("last_known_good_sha").and_then(|v| v.as_str()),
            Some("abc123"),
            "last_known_good_sha must be promoted"
        );
        assert_eq!(
            after.get("head_sha_at_seal").and_then(|v| v.as_str()),
            Some("abc123"),
            "unrelated fields must be preserved"
        );
        assert!(
            after.get("files").and_then(|v| v.as_array()).is_some(),
            "files array must be preserved"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p13_rollback_latest_good_targets_matching_snapshot() {
        // F58 — rollback --latest-good looks up the snapshot whose
        // history.jsonl entry's head_sha matches the manifest's
        // last_known_good_sha.
        let dir = test_dir("p13_rollback_latest_good");
        let pack_root = dir.join(".agent-context");
        fs::create_dir_all(&pack_root).unwrap();

        let history = pack_root.join("history.jsonl");
        // Two snapshots: one with the "good" sha, a newer one with a different sha.
        let good_entry = json!({
            "snapshot_id": "20260401T120000Z_abc1234",
            "generated_at": "2026-04-01T12:00:00Z",
            "head_sha": "abc1234deadbeef",
        });
        let newer_entry = json!({
            "snapshot_id": "20260402T120000Z_xyz5678",
            "generated_at": "2026-04-02T12:00:00Z",
            "head_sha": "xyz5678cafebabe",
        });
        fs::write(
            &history,
            format!(
                "{}\n{}\n",
                serde_json::to_string(&good_entry).unwrap(),
                serde_json::to_string(&newer_entry).unwrap()
            ),
        )
        .unwrap();

        let resolved =
            find_snapshot_for_head_sha(&pack_root, "abc1234deadbeef").expect("scan succeeds");
        assert_eq!(
            resolved.as_deref(),
            Some("20260401T120000Z_abc1234"),
            "latest-good lookup must return the snapshot matching the sha"
        );

        // Unknown sha → None, caller is expected to fail loudly.
        let missing =
            find_snapshot_for_head_sha(&pack_root, "0000000000000000").expect("scan succeeds");
        assert!(missing.is_none());

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn p13_manifest_carries_last_known_good_and_aliases_fields() {
        // Ensure build_manifest emits the two new P13 fields with their
        // default shapes so downstream consumers can rely on the schema.
        let files_meta: Vec<FileMeta> = Vec::new();
        let baseline = SemanticBaseline::default();
        let bundle = build_manifest(
            "2026-04-17T00:00:00Z",
            Path::new("/tmp/unused"),
            "fixture-repo",
            "main",
            false,
            Some("abcd1234"),
            "test-seal",
            None,
            &Vec::new(),
            &files_meta,
            &baseline,
            &std::collections::BTreeMap::new(),
        );

        assert!(
            bundle.value.get("last_known_good_sha").is_some(),
            "manifest must emit last_known_good_sha (null is fine)"
        );
        assert!(
            bundle
                .value
                .get("last_known_good_sha")
                .map(|v| v.is_null())
                .unwrap_or(false),
            "last_known_good_sha defaults to null at seal time"
        );
        assert!(
            bundle
                .value
                .get("aliases")
                .and_then(|v| v.as_object())
                .map(|m| m.is_empty())
                .unwrap_or(false),
            "aliases defaults to empty object"
        );
    }
}