shared-context-engineering 0.2.0-pre-alpha-v2

Shared Context Engineering CLI
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
use anyhow::{bail, Context, Result};
use std::collections::{BTreeMap, HashSet};
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::Instant;

use crate::services::agent_trace::{
    build_trace_payload, AgentTraceContributor, AgentTraceConversation, AgentTraceFile,
    AgentTraceRange, AgentTraceRecord, AgentTraceVcs, ContributorInput, ContributorType,
    ConversationInput, FileAttributionInput, QualityStatus, RangeInput, RewriteInfo,
    TraceAdapterInput, METADATA_IDEMPOTENCY_KEY, METADATA_QUALITY_STATUS, TRACE_CONTENT_TYPE,
    TRACE_VERSION, VCS_TYPE_GIT,
};
use crate::services::default_paths;
use crate::services::local_db::ensure_agent_trace_local_db_ready_blocking;

pub const NAME: &str = "hooks";
pub const CANONICAL_SCE_COAUTHOR_TRAILER: &str = "Co-authored-by: SCE <sce@crocoder.dev>";
pub const POST_COMMIT_PARENT_SHA_METADATA_KEY: &str = "dev.crocoder.sce.parent_revision";
const CLAUDE_CODE_HARNESS_TYPE: &str = "claude_code";
const MAX_PROMPT_BYTES: usize = 10 * 1024;
const MODEL_ID_ENV_KEYS: [&str; 5] = [
    "SCE_MODEL_ID",
    "CLAUDE_MODEL",
    "CLAUDE_CODE_MODEL",
    "ANTHROPIC_MODEL",
    "MODEL_ID",
];
const RETRY_QUEUE_MAX_ITEMS_PER_RUN: usize = 16;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum HookSubcommand {
    PreCommit,
    CommitMsg { message_file: PathBuf },
    PostCommit,
    PostRewrite { rewrite_method: String },
}

pub fn run_hooks_subcommand(subcommand: HookSubcommand) -> Result<String> {
    match subcommand {
        HookSubcommand::PreCommit => run_pre_commit_subcommand(),
        HookSubcommand::CommitMsg { message_file } => run_commit_msg_subcommand(&message_file),
        HookSubcommand::PostCommit => run_post_commit_subcommand(),
        HookSubcommand::PostRewrite { rewrite_method } => {
            run_post_rewrite_subcommand(&rewrite_method)
        }
    }
}

fn run_pre_commit_subcommand() -> Result<String> {
    let repository_root = std::env::current_dir()
        .context("Failed to determine current directory for pre-commit runtime invocation.")?;
    run_pre_commit_subcommand_in_repo(&repository_root)
}

#[allow(clippy::unnecessary_wraps)]
fn run_pre_commit_subcommand_in_repo(repository_root: &Path) -> Result<String> {
    let runtime = resolve_pre_commit_runtime_state(repository_root);

    if runtime.sce_disabled || !runtime.cli_available || runtime.is_bare_repo {
        let reason = if runtime.sce_disabled {
            PreCommitNoOpReason::Disabled
        } else if !runtime.cli_available {
            PreCommitNoOpReason::CliUnavailable
        } else {
            PreCommitNoOpReason::BareRepository
        };

        return Ok(format!(
            "pre-commit hook executed with no-op runtime state: {reason:?}"
        ));
    }

    let anchors = match capture_pre_commit_tree_anchors(repository_root) {
        Ok(anchors) => anchors,
        Err(error) => {
            return Ok(format!(
                "pre-commit hook skipped checkpoint finalization: failed to capture git anchors ({error})"
            ));
        }
    };

    let pending = match collect_pending_checkpoint(repository_root) {
        Ok(pending) => pending,
        Err(error) => {
            return Ok(format!(
                "pre-commit hook skipped checkpoint finalization: failed to collect staged attribution ({error})"
            ));
        }
    };

    let outcome = finalize_pre_commit_checkpoint(&runtime, anchors, pending);

    let message = match outcome {
        PreCommitFinalization::NoOp(reason) => {
            format!("pre-commit hook executed with no-op runtime state: {reason:?}")
        }
        PreCommitFinalization::Finalized(checkpoint) => {
            if let Err(error) = write_finalized_checkpoint(repository_root, &checkpoint) {
                return Ok(format!(
                    "pre-commit hook finalized staged checkpoint for {} file(s) but failed to persist handoff artifact ({error})",
                    checkpoint.files.len()
                ));
            }
            format!(
                "pre-commit hook executed and finalized staged checkpoint for {} file(s).",
                checkpoint.files.len()
            )
        }
    };

    Ok(message)
}

fn resolve_pre_commit_runtime_state(repository_root: &Path) -> PreCommitRuntimeState {
    PreCommitRuntimeState {
        sce_disabled: env_flag_is_truthy("SCE_DISABLED"),
        cli_available: git_command_success(repository_root, &["--version"]),
        is_bare_repo: git_command_output(repository_root, &["rev-parse", "--is-bare-repository"])
            .is_some_and(|output| output == "true"),
    }
}

fn env_flag_is_truthy(name: &str) -> bool {
    std::env::var(name)
        .ok()
        .is_some_and(|value| env_value_is_truthy(&value))
}

fn env_flag_is_enabled_by_default(name: &str) -> bool {
    match std::env::var(name) {
        Ok(value) => env_value_is_truthy(&value),
        Err(_) => true,
    }
}

fn env_value_is_truthy(value: &str) -> bool {
    matches!(
        value.trim().to_ascii_lowercase().as_str(),
        "1" | "true" | "yes" | "on"
    )
}

fn git_command_success(repository_root: &Path, args: &[&str]) -> bool {
    Command::new("git")
        .args(args)
        .current_dir(repository_root)
        .output()
        .map(|output| output.status.success())
        .unwrap_or(false)
}

fn git_command_output(repository_root: &Path, args: &[&str]) -> Option<String> {
    let output = Command::new("git")
        .args(args)
        .current_dir(repository_root)
        .output()
        .ok()?;

    if !output.status.success() {
        return None;
    }

    let stdout = String::from_utf8(output.stdout).ok()?;
    Some(stdout.trim().to_string())
}

fn run_git_command(repository_root: &Path, args: &[&str], context_message: &str) -> Result<String> {
    let output = Command::new("git")
        .args(args)
        .current_dir(repository_root)
        .output()
        .with_context(|| {
            format!(
                "{} (directory: '{}')",
                context_message,
                repository_root.display()
            )
        })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
        let diagnostic = if stderr.is_empty() {
            String::from("git command exited with a non-zero status")
        } else {
            stderr
        };
        bail!("{context_message} {diagnostic}");
    }

    String::from_utf8(output.stdout)
        .context("git command output contained invalid UTF-8")
        .map(|stdout| stdout.trim().to_string())
}

fn run_git_command_allow_empty(
    repository_root: &Path,
    args: &[&str],
    context_message: &str,
) -> Result<String> {
    let output = Command::new("git")
        .args(args)
        .current_dir(repository_root)
        .output()
        .with_context(|| {
            format!(
                "{} (directory: '{}')",
                context_message,
                repository_root.display()
            )
        })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
        let diagnostic = if stderr.is_empty() {
            String::from("git command exited with a non-zero status")
        } else {
            stderr
        };
        bail!("{context_message} {diagnostic}");
    }

    String::from_utf8(output.stdout).context("git command output contained invalid UTF-8")
}

fn capture_pre_commit_tree_anchors(repository_root: &Path) -> Result<PreCommitTreeAnchors> {
    let index_tree = run_git_command(
        repository_root,
        &["write-tree"],
        "Failed to capture index tree anchor for pre-commit checkpoint.",
    )?;
    let head_tree = git_command_output(repository_root, &["rev-parse", "--verify", "HEAD^{tree}"]);

    Ok(PreCommitTreeAnchors {
        index_tree,
        head_tree,
    })
}

fn collect_pending_checkpoint(repository_root: &Path) -> Result<PendingCheckpoint> {
    let staged_diff = run_git_command_allow_empty(
        repository_root,
        &[
            "diff",
            "--cached",
            "--unified=0",
            "--no-color",
            "--no-ext-diff",
        ],
        "Failed to collect staged diff for pre-commit attribution.",
    )?;
    let unstaged_diff = run_git_command_allow_empty(
        repository_root,
        &["diff", "--unified=0", "--no-color", "--no-ext-diff"],
        "Failed to collect unstaged diff for pre-commit attribution.",
    )?;

    let staged_ranges = parse_unified_zero_diff_ranges(&staged_diff)?;
    let unstaged_ranges = parse_unified_zero_diff_ranges(&unstaged_diff)?;

    let mut all_paths = HashSet::new();
    for path in staged_ranges.keys() {
        // Clone required: HashSet::insert requires ownership of the key
        all_paths.insert(path.clone());
    }
    for path in unstaged_ranges.keys() {
        // Clone required: HashSet::insert requires ownership of the key
        all_paths.insert(path.clone());
    }

    // TODO(0.3.0): Replace with attribution-aware producer.
    // Currently defaults to true for all staged files, which means all commits
    // will receive the SCE co-author trailer when the policy gate passes.
    // Future versions will require explicit attribution marking from a
    // separate producer that validates staged ranges came from SCE contributions.
    let files = all_paths
        .iter()
        .map(|path| PendingFileCheckpoint {
            path: path.clone(),
            has_sce_attribution: true,
            staged_ranges: staged_ranges.get(path).cloned().unwrap_or_default(),
            unstaged_ranges: unstaged_ranges.get(path).cloned().unwrap_or_default(),
        })
        .collect();

    let git_branch = resolve_pre_commit_git_branch(repository_root)?;
    let model_id = resolve_pre_commit_model_id();
    let prompts = load_pending_prompts(repository_root)?;

    Ok(PendingCheckpoint {
        files,
        harness_type: CLAUDE_CODE_HARNESS_TYPE.to_string(),
        git_branch,
        model_id,
        prompts,
    })
}

fn resolve_pre_commit_git_branch(repository_root: &Path) -> Result<Option<String>> {
    let branch = run_git_command_allow_empty(
        repository_root,
        &["branch", "--show-current"],
        "Failed to resolve git branch for pre-commit checkpoint.",
    )?;

    let trimmed = branch.trim();
    if trimmed.is_empty() {
        return Ok(None);
    }

    Ok(Some(trimmed.to_string()))
}

fn resolve_pre_commit_model_id() -> Option<String> {
    MODEL_ID_ENV_KEYS
        .iter()
        .filter_map(|name| std::env::var(name).ok())
        .map(|value| value.trim().to_string())
        .find(|value| !value.is_empty())
}

fn load_pending_prompts(repository_root: &Path) -> Result<Vec<PendingPromptCheckpoint>> {
    let prompt_capture_path =
        resolve_git_path(repository_root, default_paths::git_relative_path::PROMPTS)?;
    let payload = match fs::read_to_string(&prompt_capture_path) {
        Ok(payload) => payload,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
        Err(error) => {
            bail!(
                "Failed to read prompt capture file '{}': {}",
                prompt_capture_path.display(),
                error
            )
        }
    };

    let mut prompts = Vec::new();
    let mut seen_entries = HashSet::new();
    let mut last_known_cwd: Option<String> = None;

    for line in payload.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }

        let Ok(json) = serde_json::from_str::<serde_json::Value>(trimmed) else {
            continue;
        };

        let Some(prompt_text) = json.get("prompt").and_then(serde_json::Value::as_str) else {
            continue;
        };
        let Some(captured_at) = json.get("timestamp").and_then(serde_json::Value::as_str) else {
            continue;
        };

        let dedupe_key = format!("{prompt_text}\u{001f}{captured_at}");
        if !seen_entries.insert(dedupe_key) {
            continue;
        }

        let Ok(turn_number) = u32::try_from(prompts.len() + 1) else {
            break;
        };

        let cwd = json
            .get("cwd")
            .and_then(serde_json::Value::as_str)
            .map(str::trim)
            .filter(|value| !value.is_empty())
            .map(ToOwned::to_owned)
            .or_else(|| last_known_cwd.clone());

        if let Some(cwd_value) = &cwd {
            last_known_cwd = Some(cwd_value.clone());
        }

        prompts.push(PendingPromptCheckpoint {
            turn_number,
            prompt_text: prompt_text.to_string(),
            prompt_length: prompt_text.len(),
            is_truncated: false,
            cwd,
            transcript_path: json
                .get("transcript_path")
                .and_then(serde_json::Value::as_str)
                .map(str::trim)
                .filter(|value| !value.is_empty())
                .map(ToOwned::to_owned),
            captured_at: captured_at.to_string(),
        });
    }

    Ok(prompts)
}

fn parse_unified_zero_diff_ranges(
    contents: &str,
) -> Result<BTreeMap<String, Vec<PendingLineRange>>> {
    let mut ranges_by_path: BTreeMap<String, Vec<PendingLineRange>> = BTreeMap::new();
    let mut current_path: Option<String> = None;

    for line in contents.lines() {
        if let Some(path) = line.strip_prefix("+++ b/") {
            current_path = Some(path.to_string());
            continue;
        }

        if line.starts_with("+++") {
            current_path = None;
            continue;
        }

        if !line.starts_with("@@") {
            continue;
        }

        let Some(path) = current_path.clone() else {
            continue;
        };

        if let Some(range) = parse_hunk_new_range(line)? {
            ranges_by_path.entry(path).or_default().push(range);
        }
    }

    Ok(ranges_by_path)
}

fn parse_hunk_new_range(header_line: &str) -> Result<Option<PendingLineRange>> {
    let mut fields = header_line.split_whitespace();
    let _ = fields.next();
    let _ = fields.next();
    let Some(new_range_field) = fields.next() else {
        bail!("Invalid unified diff hunk header '{header_line}': missing new-range field");
    };

    let Some(range_body) = new_range_field.strip_prefix('+') else {
        bail!("Invalid unified diff hunk header '{header_line}': malformed new-range field");
    };

    let mut parts = range_body.split(',');
    let start_line: u32 = parts
        .next()
        .context("Unified diff hunk is missing start line")?
        .parse()
        .with_context(|| format!("Invalid hunk start line in '{header_line}': expected integer"))?;
    let line_count: u32 = parts
        .next()
        .map(str::parse)
        .transpose()
        .with_context(|| format!("Invalid hunk line count in '{header_line}': expected integer"))?
        .unwrap_or(1);

    if line_count == 0 {
        return Ok(None);
    }

    Ok(Some(PendingLineRange {
        start_line,
        end_line: start_line + line_count - 1,
    }))
}

fn resolve_pre_commit_checkpoint_path(repository_root: &Path) -> Result<PathBuf> {
    let resolved = run_git_command(
        repository_root,
        &[
            "rev-parse",
            "--git-path",
            default_paths::git_relative_path::PRE_COMMIT_CHECKPOINT,
        ],
        "Failed to resolve pre-commit checkpoint handoff path.",
    )?;
    let path = PathBuf::from(resolved);

    if path.is_absolute() {
        return Ok(path);
    }

    Ok(repository_root.join(path))
}

fn write_finalized_checkpoint(
    repository_root: &Path,
    checkpoint: &FinalizedCheckpoint,
) -> Result<()> {
    let checkpoint_path = resolve_pre_commit_checkpoint_path(repository_root)?;
    let parent = checkpoint_path
        .parent()
        .context("Resolved pre-commit checkpoint path has no parent directory")?;
    fs::create_dir_all(parent).with_context(|| {
        format!(
            "Failed to create pre-commit checkpoint directory '{}'.",
            parent.display()
        )
    })?;

    let mut files = Vec::new();
    for file in &checkpoint.files {
        let mut ranges = Vec::new();
        for range in &file.ranges {
            ranges.push(serde_json::json!({
                "start_line": range.start_line,
                "end_line": range.end_line,
            }));
        }
        files.push(serde_json::json!({
            "path": file.path,
            "has_sce_attribution": file.has_sce_attribution,
            "ranges": ranges,
        }));
    }

    let mut prompts = Vec::new();
    for prompt in &checkpoint.prompts {
        prompts.push(serde_json::json!({
            "turn_number": prompt.turn_number,
            "prompt_text": prompt.prompt_text,
            "prompt_length": prompt.prompt_length,
            "is_truncated": prompt.is_truncated,
            "cwd": prompt.cwd,
            "transcript_path": prompt.transcript_path,
            "captured_at": prompt.captured_at,
        }));
    }

    let payload = serde_json::json!({
        "version": 1,
        "anchors": {
            "index_tree": checkpoint.anchors.index_tree.clone(),
            "head_tree": checkpoint.anchors.head_tree.clone(),
        },
        "harness_type": checkpoint.harness_type,
        "git_branch": checkpoint.git_branch,
        "model_id": checkpoint.model_id,
        "files": files,
        "prompts": prompts,
    });

    let serialized = serde_json::to_vec_pretty(&payload)
        .context("Failed to serialize pre-commit checkpoint artifact")?;
    fs::write(&checkpoint_path, serialized).with_context(|| {
        format!(
            "Failed to persist pre-commit checkpoint artifact '{}'.",
            checkpoint_path.display()
        )
    })
}

fn run_commit_msg_subcommand(message_file: &Path) -> Result<String> {
    let repository_root = std::env::current_dir()
        .context("Failed to determine current directory for commit-msg runtime invocation.")?;
    run_commit_msg_subcommand_in_repo(&repository_root, message_file)
}

fn run_commit_msg_subcommand_in_repo(
    repository_root: &Path,
    message_file: &Path,
) -> Result<String> {
    let metadata = fs::metadata(message_file).with_context(|| {
        format!(
            "Invalid commit message file '{}': file does not exist or is not readable.",
            message_file.display()
        )
    })?;

    if !metadata.is_file() {
        bail!(
            "Invalid commit message file '{}': expected a regular file path.",
            message_file.display()
        );
    }

    let runtime = resolve_commit_msg_runtime_state(repository_root);
    let original = fs::read_to_string(message_file).with_context(|| {
        format!(
            "Invalid commit message file '{}': failed to read UTF-8 content.",
            message_file.display()
        )
    })?;

    let transformed = apply_commit_msg_coauthor_policy(&runtime, &original);
    let gate_passed =
        !runtime.sce_disabled && runtime.sce_coauthor_enabled && runtime.has_staged_sce_attribution;
    let trailer_applied = gate_passed && transformed != original;

    if trailer_applied {
        fs::write(message_file, transformed.as_bytes()).with_context(|| {
            format!(
                "Failed to update commit message file '{}' with canonical co-author trailer.",
                message_file.display()
            )
        })?;
    }

    Ok(format!(
        "commit-msg hook processed message file '{}' (policy_gate_passed={}, trailer_applied={}).",
        message_file.display(),
        gate_passed,
        trailer_applied
    ))
}

fn resolve_commit_msg_runtime_state(repository_root: &Path) -> CommitMsgRuntimeState {
    CommitMsgRuntimeState {
        sce_disabled: env_flag_is_truthy("SCE_DISABLED"),
        sce_coauthor_enabled: env_flag_is_enabled_by_default("SCE_COAUTHOR_ENABLED"),
        has_staged_sce_attribution: staged_sce_attribution_present(repository_root),
    }
}

fn staged_sce_attribution_present(repository_root: &Path) -> bool {
    let Ok(checkpoint_path) = resolve_pre_commit_checkpoint_path(repository_root) else {
        return false;
    };

    let Ok(payload) = fs::read_to_string(&checkpoint_path) else {
        return false;
    };
    let Ok(json) = serde_json::from_str::<serde_json::Value>(&payload) else {
        return false;
    };

    checkpoint_has_explicit_sce_attribution(&json)
}

fn checkpoint_has_explicit_sce_attribution(json: &serde_json::Value) -> bool {
    json.get("files")
        .and_then(serde_json::Value::as_array)
        .is_some_and(|files| {
            files.iter().any(|file| {
                let has_sce_attribution = file
                    .get("has_sce_attribution")
                    .and_then(serde_json::Value::as_bool)
                    .unwrap_or(false);

                if !has_sce_attribution {
                    return false;
                }

                file.get("ranges")
                    .and_then(serde_json::Value::as_array)
                    .is_some_and(|ranges| !ranges.is_empty())
            })
        })
}

fn run_post_commit_subcommand() -> Result<String> {
    let repository_root = std::env::current_dir()
        .context("Failed to determine current directory for post-commit runtime invocation.")?;
    run_post_commit_subcommand_in_repo(&repository_root)
}

#[allow(clippy::unnecessary_wraps)]
fn run_post_commit_subcommand_in_repo(repository_root: &Path) -> Result<String> {
    let runtime = resolve_post_commit_runtime_state(repository_root);

    if runtime.sce_disabled || !runtime.cli_available || runtime.is_bare_repo {
        let reason = if runtime.sce_disabled {
            PostCommitNoOpReason::Disabled
        } else if !runtime.cli_available {
            PostCommitNoOpReason::CliUnavailable
        } else {
            PostCommitNoOpReason::BareRepository
        };

        return Ok(format!(
            "post-commit hook executed with no-op runtime state: {reason:?}"
        ));
    }

    let runtime_paths = match resolve_post_commit_runtime_paths(repository_root) {
        Ok(paths) => paths,
        Err(error) => {
            return Ok(format!(
                "post-commit hook skipped trace finalization: failed to resolve persistence targets ({error})"
            ));
        }
    };

    let input = match build_post_commit_input(repository_root) {
        Ok(input) => input,
        Err(error) => {
            return Ok(format!(
                "post-commit hook skipped trace finalization: failed to collect commit attribution input ({error})"
            ));
        }
    };

    let mut notes_writer = GitNotesTraceWriter {
        repository_root: repository_root.to_path_buf(),
    };
    let mut record_store = LocalDbTraceRecordStore {
        repository_root: repository_root.to_path_buf(),
        db_path: runtime_paths.local_db_path,
    };
    let mut retry_queue = JsonFileTraceRetryQueue {
        path: runtime_paths.retry_queue_path,
    };
    let mut emission_ledger = FileTraceEmissionLedger {
        path: runtime_paths.emission_ledger_path,
    };

    let outcome = match finalize_post_commit_trace(
        &runtime,
        input,
        &mut notes_writer,
        &mut record_store,
        &mut retry_queue,
        &mut emission_ledger,
    ) {
        Ok(outcome) => outcome,
        Err(error) => {
            return Ok(format!(
                "post-commit hook skipped trace finalization: finalizer execution failed ({error})"
            ));
        }
    };

    let retry_report =
        match process_runtime_retry_queue(&mut retry_queue, &mut notes_writer, &mut record_store) {
            Ok(report) => report,
            Err(error) => {
                return Ok(format!(
                "post-commit hook completed trace finalization but retry replay failed ({error})"
            ));
            }
        };

    let message = match outcome {
        PostCommitFinalization::NoOp(reason) => {
            format!("post-commit hook executed with no-op runtime state: {reason:?}")
        }
        PostCommitFinalization::Persisted(persisted) => format!(
            "post-commit hook finalized trace for commit '{}' (trace_id='{}', notes={:?}, database={:?}) {}.",
            persisted.commit_sha, persisted.trace_id, persisted.notes, persisted.database
            , retry_report.summary_text()
        ),
        PostCommitFinalization::QueuedFallback(queued) => format!(
            "post-commit hook enqueued fallback for commit '{}' (trace_id='{}', failed_targets={:?}) {}.",
            queued.commit_sha,
            queued.trace_id,
            queued.failed_targets,
            retry_report.summary_text()
        ),
    };

    Ok(message)
}

fn resolve_post_commit_runtime_state(repository_root: &Path) -> PostCommitRuntimeState {
    PostCommitRuntimeState {
        sce_disabled: env_flag_is_truthy("SCE_DISABLED"),
        cli_available: git_command_success(repository_root, &["--version"]),
        is_bare_repo: git_command_output(repository_root, &["rev-parse", "--is-bare-repository"])
            .is_some_and(|output| output == "true"),
    }
}

#[allow(clippy::struct_field_names)]
struct PostCommitRuntimePaths {
    local_db_path: PathBuf,
    retry_queue_path: PathBuf,
    emission_ledger_path: PathBuf,
}

fn resolve_post_commit_runtime_paths(repository_root: &Path) -> Result<PostCommitRuntimePaths> {
    let local_db_path = ensure_agent_trace_local_db_ready_blocking()?;
    let retry_queue_path = resolve_git_path(
        repository_root,
        default_paths::git_relative_path::TRACE_RETRY_QUEUE,
    )?;
    let emission_ledger_path = resolve_git_path(
        repository_root,
        default_paths::git_relative_path::TRACE_EMISSION_LEDGER,
    )?;

    Ok(PostCommitRuntimePaths {
        local_db_path,
        retry_queue_path,
        emission_ledger_path,
    })
}

fn resolve_git_path(repository_root: &Path, git_path: &str) -> Result<PathBuf> {
    let resolved = run_git_command(
        repository_root,
        &["rev-parse", "--git-path", git_path],
        "Failed to resolve git persistence path.",
    )?;
    let path = PathBuf::from(resolved);
    if path.is_absolute() {
        return Ok(path);
    }

    Ok(repository_root.join(path))
}

fn build_post_commit_input(repository_root: &Path) -> Result<PostCommitInput> {
    let commit_sha = run_git_command(
        repository_root,
        &["rev-parse", "--verify", "HEAD"],
        "Failed to resolve post-commit HEAD SHA.",
    )?;
    let parent_sha = git_command_output(repository_root, &["rev-parse", "--verify", "HEAD^"]);
    let timestamp_rfc3339 = run_git_command(
        repository_root,
        &["show", "-s", "--format=%cI", "HEAD"],
        "Failed to resolve post-commit timestamp.",
    )?;
    let committed_at_unix_ms = run_git_command(
        repository_root,
        &["show", "-s", "--format=%ct", "HEAD"],
        "Failed to resolve post-commit timestamp seconds.",
    )?
    .parse::<i64>()
    .context("Failed to parse post-commit timestamp seconds as integer")?
        * 1_000;
    let files = collect_post_commit_file_attribution(repository_root)?;
    let prompts =
        load_post_commit_prompt_records(repository_root, committed_at_unix_ms, &timestamp_rfc3339)?;
    let idempotency_key = format!("post-commit:{commit_sha}");
    let record_id = deterministic_uuid_v4_from_seed(&format!("{commit_sha}:{timestamp_rfc3339}"));

    Ok(PostCommitInput {
        record_id,
        timestamp_rfc3339,
        committed_at_unix_ms,
        commit_sha,
        parent_sha,
        idempotency_key,
        files,
        prompts,
    })
}

fn collect_post_commit_file_attribution(
    repository_root: &Path,
) -> Result<Vec<FileAttributionInput>> {
    let checkpoint_files = load_post_commit_checkpoint_files(repository_root)?;
    if !checkpoint_files.is_empty() {
        return Ok(checkpoint_files);
    }

    collect_commit_file_attribution(
        repository_root,
        "HEAD",
        "https://crocoder.dev/sce/local-hooks/post-commit",
    )
}

fn collect_commit_file_attribution(
    repository_root: &Path,
    revision: &str,
    conversation_url: &str,
) -> Result<Vec<FileAttributionInput>> {
    let changed_paths = run_git_command_allow_empty(
        repository_root,
        &["show", "--pretty=format:", "--name-only", revision],
        "Failed to resolve changed files for commit attribution.",
    )?;

    let mut files = Vec::new();
    for line in changed_paths.lines() {
        let path = line.trim();
        if path.is_empty() {
            continue;
        }

        files.push(FileAttributionInput {
            path: path.to_string(),
            conversations: vec![ConversationInput {
                url: conversation_url.to_string(),
                related: Vec::new(),
                ranges: vec![RangeInput {
                    start_line: 1,
                    end_line: 1,
                    contributor: ContributorInput {
                        kind: ContributorType::Unknown,
                        model_id: None,
                    },
                }],
            }],
        });
    }

    Ok(files)
}

fn load_post_commit_checkpoint_files(repository_root: &Path) -> Result<Vec<FileAttributionInput>> {
    let checkpoint_path = resolve_pre_commit_checkpoint_path(repository_root)?;
    let payload = match fs::read_to_string(&checkpoint_path) {
        Ok(payload) => payload,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
        Err(error) => {
            bail!(
                "Failed to read pre-commit checkpoint '{}' for post-commit finalization: {}",
                checkpoint_path.display(),
                error
            )
        }
    };

    let checkpoint = serde_json::from_str::<serde_json::Value>(&payload).with_context(|| {
        format!(
            "Failed to parse pre-commit checkpoint '{}' as JSON.",
            checkpoint_path.display()
        )
    })?;

    let Some(files_json) = checkpoint
        .get("files")
        .and_then(serde_json::Value::as_array)
    else {
        return Ok(Vec::new());
    };

    let mut files = Vec::new();
    for file_json in files_json {
        let Some(path) = file_json.get("path").and_then(serde_json::Value::as_str) else {
            continue;
        };

        let ranges = file_json
            .get("ranges")
            .and_then(serde_json::Value::as_array)
            .map(|ranges| {
                ranges
                    .iter()
                    .filter_map(|range_json| {
                        let start_line = range_json
                            .get("start_line")
                            .and_then(serde_json::Value::as_u64)
                            .and_then(|value| u32::try_from(value).ok())?;
                        let end_line = range_json
                            .get("end_line")
                            .and_then(serde_json::Value::as_u64)
                            .and_then(|value| u32::try_from(value).ok())?;

                        Some(RangeInput {
                            start_line,
                            end_line,
                            contributor: ContributorInput {
                                kind: ContributorType::Unknown,
                                model_id: None,
                            },
                        })
                    })
                    .collect::<Vec<_>>()
            })
            .unwrap_or_default();

        if ranges.is_empty() {
            continue;
        }

        files.push(FileAttributionInput {
            path: path.to_string(),
            conversations: vec![ConversationInput {
                url: String::from("https://crocoder.dev/sce/local-hooks/pre-commit-checkpoint"),
                related: Vec::new(),
                ranges,
            }],
        });
    }

    Ok(files)
}

fn load_post_commit_prompt_records(
    repository_root: &Path,
    committed_at_unix_ms: i64,
    committed_at_rfc3339: &str,
) -> Result<Vec<PersistedPromptRecord>> {
    let checkpoint_path = resolve_pre_commit_checkpoint_path(repository_root)?;
    let payload = match fs::read_to_string(&checkpoint_path) {
        Ok(payload) => payload,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
        Err(error) => {
            bail!(
                "Failed to read pre-commit checkpoint '{}' for post-commit prompts: {}",
                checkpoint_path.display(),
                error
            )
        }
    };

    let checkpoint = serde_json::from_str::<serde_json::Value>(&payload).with_context(|| {
        format!(
            "Failed to parse pre-commit checkpoint '{}' as JSON for prompt persistence.",
            checkpoint_path.display()
        )
    })?;

    let harness_type = checkpoint
        .get("harness_type")
        .and_then(serde_json::Value::as_str)
        .unwrap_or(CLAUDE_CODE_HARNESS_TYPE)
        .to_string();
    let git_branch = checkpoint
        .get("git_branch")
        .and_then(serde_json::Value::as_str)
        .map(ToOwned::to_owned);
    let model_id = checkpoint
        .get("model_id")
        .and_then(serde_json::Value::as_str)
        .map(ToOwned::to_owned);
    let Some(prompts_json) = checkpoint
        .get("prompts")
        .and_then(serde_json::Value::as_array)
    else {
        return Ok(Vec::new());
    };

    let mut prompt_windows = Vec::new();
    for (index, prompt_json) in prompts_json.iter().enumerate() {
        let turn_number = prompt_json
            .get("turn_number")
            .and_then(serde_json::Value::as_u64)
            .and_then(|value| u32::try_from(value).ok())
            .unwrap_or_else(|| u32::try_from(index + 1).unwrap_or(u32::MAX));
        let Some(prompt_text) = prompt_json
            .get("prompt_text")
            .and_then(serde_json::Value::as_str)
            .map(ToOwned::to_owned)
        else {
            continue;
        };
        let captured_at = prompt_json
            .get("captured_at")
            .and_then(serde_json::Value::as_str)
            .map_or_else(|| committed_at_rfc3339.to_string(), ToOwned::to_owned);
        let Some(captured_at_unix_ms) = parse_utc_rfc3339_to_unix_ms(&captured_at) else {
            continue;
        };
        let next_captured_at_unix_ms = prompts_json
            .get(index + 1)
            .and_then(|next| next.get("captured_at"))
            .and_then(serde_json::Value::as_str)
            .and_then(parse_utc_rfc3339_to_unix_ms)
            .unwrap_or(committed_at_unix_ms);
        let duration_ms = (next_captured_at_unix_ms - captured_at_unix_ms).max(0);
        let transcript_path = prompt_json
            .get("transcript_path")
            .and_then(serde_json::Value::as_str)
            .filter(|value| !value.trim().is_empty());
        let tool_call_count = transcript_path
            .map(|path| {
                count_tool_uses_in_transcript(path, captured_at_unix_ms, next_captured_at_unix_ms)
            })
            .transpose()?
            .unwrap_or(0);
        let cwd = prompt_json
            .get("cwd")
            .and_then(serde_json::Value::as_str)
            .map(ToOwned::to_owned);
        let (prompt_text, is_truncated, prompt_length) = truncate_prompt(&prompt_text);

        prompt_windows.push(PersistedPromptRecord {
            turn_number,
            prompt_text,
            prompt_length,
            is_truncated,
            harness_type: harness_type.clone(),
            model_id: model_id.clone(),
            cwd,
            git_branch: git_branch.clone(),
            tool_call_count,
            duration_ms,
            captured_at,
        });
    }

    Ok(prompt_windows)
}

fn count_tool_uses_in_transcript(
    transcript_path: &str,
    captured_at_unix_ms: i64,
    next_captured_at_unix_ms: i64,
) -> Result<u32> {
    let payload = fs::read_to_string(transcript_path).with_context(|| {
        format!("Failed to read Claude transcript '{transcript_path}' for prompt metrics.")
    })?;
    let mut count = 0_u32;

    for line in payload.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }

        let Ok(json) = serde_json::from_str::<serde_json::Value>(trimmed) else {
            continue;
        };

        if json.get("type").and_then(serde_json::Value::as_str) != Some("assistant") {
            continue;
        }

        let Some(timestamp) = json.get("timestamp").and_then(serde_json::Value::as_str) else {
            continue;
        };
        let Some(event_unix_ms) = parse_utc_rfc3339_to_unix_ms(timestamp) else {
            continue;
        };
        if event_unix_ms < captured_at_unix_ms || event_unix_ms >= next_captured_at_unix_ms {
            continue;
        }

        let Some(contents) = json
            .get("message")
            .and_then(|message| message.get("content"))
            .and_then(serde_json::Value::as_array)
        else {
            continue;
        };

        for content in contents {
            if content.get("type").and_then(serde_json::Value::as_str) == Some("tool_use") {
                count = count.saturating_add(1);
            }
        }
    }

    Ok(count)
}

fn truncate_prompt(text: &str) -> (String, bool, usize) {
    let original_length = text.len();
    if original_length <= MAX_PROMPT_BYTES {
        return (text.to_string(), false, original_length);
    }

    let mut end = 0;
    for (index, _) in text.char_indices() {
        if index > MAX_PROMPT_BYTES {
            break;
        }
        end = index;
    }
    if end == 0 {
        end = text
            .char_indices()
            .find(|(index, _)| *index >= MAX_PROMPT_BYTES)
            .map_or(text.len(), |(index, _)| index);
    }

    (text[..end].to_string(), true, original_length)
}

fn parse_utc_rfc3339_to_unix_ms(value: &str) -> Option<i64> {
    let value = value.trim();
    let core = value.strip_suffix('Z')?;
    let (date_part, time_part) = core.split_once('T')?;
    let mut date_fields = date_part.split('-');
    let year = date_fields.next()?.parse::<i32>().ok()?;
    let month = date_fields.next()?.parse::<u32>().ok()?;
    let day = date_fields.next()?.parse::<u32>().ok()?;
    if date_fields.next().is_some() {
        return None;
    }

    let (time_main, fractional) = match time_part.split_once('.') {
        Some((time_main, fractional)) => (time_main, fractional),
        None => (time_part, ""),
    };
    let mut time_fields = time_main.split(':');
    let hour = time_fields.next()?.parse::<u32>().ok()?;
    let minute = time_fields.next()?.parse::<u32>().ok()?;
    let second = time_fields.next()?.parse::<u32>().ok()?;
    if time_fields.next().is_some() {
        return None;
    }

    let millis = parse_fractional_millis(fractional)?;
    let days = days_from_civil(year, month, day)?;
    let seconds = days
        .checked_mul(86_400)?
        .checked_add(i64::from(hour) * 3_600)?
        .checked_add(i64::from(minute) * 60)?
        .checked_add(i64::from(second))?;
    seconds.checked_mul(1_000)?.checked_add(i64::from(millis))
}

fn parse_fractional_millis(value: &str) -> Option<u32> {
    if value.is_empty() {
        return Some(0);
    }

    let digits = value
        .chars()
        .take_while(char::is_ascii_digit)
        .collect::<String>();
    if digits.is_empty() {
        return Some(0);
    }

    let mut normalized = digits;
    while normalized.len() < 3 {
        normalized.push('0');
    }
    normalized.truncate(3);
    normalized.parse::<u32>().ok()
}

fn days_from_civil(year: i32, month: u32, day: u32) -> Option<i64> {
    if !(1..=12).contains(&month) || day == 0 || day > 31 {
        return None;
    }

    let year = i64::from(year) - i64::from(month <= 2);
    let era = if year >= 0 { year } else { year - 399 } / 400;
    let year_of_era = year - era * 400;
    let month = i64::from(month);
    let day = i64::from(day);
    let day_of_year = (153 * (month + if month > 2 { -3 } else { 9 }) + 2) / 5 + day - 1;
    let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
    Some(era * 146_097 + day_of_era - 719_468)
}

fn prompt_to_json(prompt: &PersistedPromptRecord) -> serde_json::Value {
    serde_json::json!({
        "turn_number": prompt.turn_number,
        "prompt_text": prompt.prompt_text,
        "prompt_length": prompt.prompt_length,
        "is_truncated": prompt.is_truncated,
        "harness_type": prompt.harness_type,
        "model_id": prompt.model_id,
        "cwd": prompt.cwd,
        "git_branch": prompt.git_branch,
        "tool_call_count": prompt.tool_call_count,
        "duration_ms": prompt.duration_ms,
        "captured_at": prompt.captured_at,
    })
}

fn prompt_from_json(value: &serde_json::Value) -> Result<PersistedPromptRecord> {
    Ok(PersistedPromptRecord {
        turn_number: value
            .get("turn_number")
            .and_then(serde_json::Value::as_u64)
            .and_then(|raw| u32::try_from(raw).ok())
            .context("Prompt payload missing 'turn_number' integer")?,
        prompt_text: value
            .get("prompt_text")
            .and_then(serde_json::Value::as_str)
            .context("Prompt payload missing 'prompt_text' string")?
            .to_string(),
        prompt_length: value
            .get("prompt_length")
            .and_then(serde_json::Value::as_u64)
            .and_then(|raw| usize::try_from(raw).ok())
            .context("Prompt payload missing 'prompt_length' integer")?,
        is_truncated: value
            .get("is_truncated")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false),
        harness_type: value
            .get("harness_type")
            .and_then(serde_json::Value::as_str)
            .context("Prompt payload missing 'harness_type' string")?
            .to_string(),
        model_id: value
            .get("model_id")
            .and_then(serde_json::Value::as_str)
            .map(ToOwned::to_owned),
        cwd: value
            .get("cwd")
            .and_then(serde_json::Value::as_str)
            .map(ToOwned::to_owned),
        git_branch: value
            .get("git_branch")
            .and_then(serde_json::Value::as_str)
            .map(ToOwned::to_owned),
        tool_call_count: value
            .get("tool_call_count")
            .and_then(serde_json::Value::as_u64)
            .and_then(|raw| u32::try_from(raw).ok())
            .unwrap_or(0),
        duration_ms: value
            .get("duration_ms")
            .and_then(serde_json::Value::as_i64)
            .unwrap_or(0),
        captured_at: value
            .get("captured_at")
            .and_then(serde_json::Value::as_str)
            .context("Prompt payload missing 'captured_at' string")?
            .to_string(),
    })
}

fn deterministic_uuid_v4_from_seed(seed: &str) -> String {
    use sha2::{Digest, Sha256};

    let digest = Sha256::digest(seed.as_bytes());
    let mut bytes = [0_u8; 16];
    bytes.copy_from_slice(&digest[..16]);

    bytes[6] = (bytes[6] & 0x0f) | 0x40;
    bytes[8] = (bytes[8] & 0x3f) | 0x80;

    format!(
        "{:08x}-{:04x}-{:04x}-{:04x}-{:012x}",
        u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]),
        u16::from_be_bytes([bytes[4], bytes[5]]),
        u16::from_be_bytes([bytes[6], bytes[7]]),
        u16::from_be_bytes([bytes[8], bytes[9]]),
        u64::from_be_bytes([
            0, 0, bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]
        ])
    )
}

fn run_post_rewrite_subcommand(rewrite_method: &str) -> Result<String> {
    let repository_root = std::env::current_dir()
        .context("Failed to determine current directory for post-rewrite runtime invocation.")?;
    let stdin = std::io::read_to_string(std::io::stdin())
        .context("Failed to read post-rewrite pair input from STDIN")?;

    run_post_rewrite_subcommand_in_repo(&repository_root, rewrite_method, &stdin)
}

#[allow(clippy::unnecessary_wraps)]
fn run_post_rewrite_subcommand_in_repo(
    repository_root: &Path,
    rewrite_method: &str,
    pairs_file_contents: &str,
) -> Result<String> {
    let runtime = resolve_post_rewrite_runtime_state(repository_root);

    if runtime.sce_disabled || !runtime.cli_available || runtime.is_bare_repo {
        let reason = if runtime.sce_disabled {
            PostRewriteNoOpReason::Disabled
        } else if !runtime.cli_available {
            PostRewriteNoOpReason::CliUnavailable
        } else {
            PostRewriteNoOpReason::BareRepository
        };

        return Ok(format!(
            "post-rewrite hook executed with no-op runtime state: {reason:?}"
        ));
    }

    let runtime_paths = match resolve_post_commit_runtime_paths(repository_root) {
        Ok(paths) => paths,
        Err(error) => {
            return Ok(format!(
                "post-rewrite hook skipped rewrite finalization: failed to resolve persistence targets ({error})"
            ));
        }
    };

    let mut ingestion = LocalDbRewriteRemapIngestion {
        repository_root: repository_root.to_path_buf(),
        db_path: runtime_paths.local_db_path.clone(),
        accepted_requests: Vec::new(),
    };

    let outcome = match finalize_post_rewrite_remap(
        &runtime,
        rewrite_method,
        pairs_file_contents,
        &mut ingestion,
    ) {
        Ok(outcome) => outcome,
        Err(error) => {
            return Ok(format!(
                "post-rewrite hook skipped rewrite finalization: remap ingestion failed ({error})"
            ));
        }
    };

    let mut notes_writer = GitNotesTraceWriter {
        repository_root: repository_root.to_path_buf(),
    };
    let mut record_store = LocalDbTraceRecordStore {
        repository_root: repository_root.to_path_buf(),
        db_path: runtime_paths.local_db_path,
    };
    let mut retry_queue = JsonFileTraceRetryQueue {
        path: runtime_paths.retry_queue_path,
    };
    let mut emission_ledger = FileTraceEmissionLedger {
        path: runtime_paths.emission_ledger_path,
    };

    let mut rewrite_persisted = 0_usize;
    let mut rewrite_queued = 0_usize;
    let mut rewrite_noops = 0_usize;
    let mut rewrite_failures = 0_usize;

    for request in &ingestion.accepted_requests {
        let Ok(input) = build_rewrite_trace_input(repository_root, request) else {
            rewrite_failures += 1;
            continue;
        };

        match finalize_rewrite_trace(
            &runtime,
            input,
            &mut notes_writer,
            &mut record_store,
            &mut retry_queue,
            &mut emission_ledger,
        ) {
            Ok(RewriteTraceFinalization::Persisted(_)) => rewrite_persisted += 1,
            Ok(RewriteTraceFinalization::QueuedFallback(_)) => rewrite_queued += 1,
            Ok(RewriteTraceFinalization::NoOp(_)) => rewrite_noops += 1,
            Err(_) => rewrite_failures += 1,
        }
    }

    let retry_report =
        match process_runtime_retry_queue(&mut retry_queue, &mut notes_writer, &mut record_store) {
            Ok(report) => report,
            Err(error) => {
                return Ok(format!(
                "post-rewrite hook completed rewrite finalization but retry replay failed ({error})"
            ));
            }
        };

    match outcome {
        PostRewriteFinalization::NoOp(reason) => Ok(format!(
            "post-rewrite hook executed with no-op runtime state: {reason:?}"
        )),
        PostRewriteFinalization::Ingested(ingested) => Ok(format!(
            "post-rewrite hook ingested {} pair(s), skipped {} duplicate pair(s), method='{}', rewrite_traces=(persisted={}, queued={}, no_op={}, failed={}) {}.",
            ingested.ingested_pairs,
            ingested.skipped_pairs,
            ingested.rewrite_method.canonical_label(),
            rewrite_persisted,
            rewrite_queued,
            rewrite_noops,
            rewrite_failures,
            retry_report.summary_text()
        )),
    }
}

#[derive(Default)]
struct InMemoryRetryMetricsSink {
    events: Vec<RetryProcessingMetric>,
}

impl RetryMetricsSink for InMemoryRetryMetricsSink {
    fn record_retry_metric(&mut self, metric: RetryProcessingMetric) {
        self.events.push(metric);
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct RuntimeRetryReport {
    summary: RetryQueueProcessSummary,
    transient_failures: usize,
    permanent_failures: usize,
}

impl RuntimeRetryReport {
    fn summary_text(&self) -> String {
        format!(
            "retry_queue=(attempted={}, recovered={}, requeued={}, transient_failures={}, permanent_failures={})",
            self.summary.attempted,
            self.summary.recovered,
            self.summary.requeued,
            self.transient_failures,
            self.permanent_failures
        )
    }
}

fn process_runtime_retry_queue(
    retry_queue: &mut impl TraceRetryQueue,
    notes_writer: &mut impl TraceNotesWriter,
    record_store: &mut impl TraceRecordStore,
) -> Result<RuntimeRetryReport> {
    let mut metrics_sink = InMemoryRetryMetricsSink::default();
    let summary = process_trace_retry_queue(
        retry_queue,
        notes_writer,
        record_store,
        &mut metrics_sink,
        RETRY_QUEUE_MAX_ITEMS_PER_RUN,
    )?;

    let mut transient_failures = 0_usize;
    let mut permanent_failures = 0_usize;

    for metric in metrics_sink.events {
        match metric.error_class {
            Some(PersistenceErrorClass::Transient) => transient_failures += 1,
            Some(PersistenceErrorClass::Permanent) => permanent_failures += 1,
            None => {}
        }
    }

    Ok(RuntimeRetryReport {
        summary,
        transient_failures,
        permanent_failures,
    })
}

fn resolve_post_rewrite_runtime_state(repository_root: &Path) -> PostRewriteRuntimeState {
    PostRewriteRuntimeState {
        sce_disabled: env_flag_is_truthy("SCE_DISABLED"),
        cli_available: git_command_success(repository_root, &["--version"]),
        is_bare_repo: git_command_output(repository_root, &["rev-parse", "--is-bare-repository"])
            .is_some_and(|output| output == "true"),
    }
}

fn build_rewrite_trace_input(
    repository_root: &Path,
    request: &RewriteRemapRequest,
) -> Result<RewriteTraceInput> {
    let timestamp_rfc3339 = run_git_command(
        repository_root,
        &["show", "-s", "--format=%cI", request.new_sha.as_str()],
        "Failed to resolve rewritten commit timestamp.",
    )?;
    let files = collect_commit_file_attribution(
        repository_root,
        request.new_sha.as_str(),
        "https://crocoder.dev/sce/local-hooks/post-rewrite",
    )?;

    Ok(RewriteTraceInput {
        record_id: deterministic_uuid_v4_from_seed(&format!(
            "{}:{}",
            request.idempotency_key, timestamp_rfc3339
        )),
        timestamp_rfc3339,
        rewritten_commit_sha: request.new_sha.clone(),
        rewrite_from_sha: request.old_sha.clone(),
        rewrite_method: request.rewrite_method.clone(),
        rewrite_confidence: 1.0,
        idempotency_key: request.idempotency_key.clone(),
        files,
    })
}

struct LocalDbRewriteRemapIngestion {
    repository_root: PathBuf,
    db_path: PathBuf,
    accepted_requests: Vec<RewriteRemapRequest>,
}

impl RewriteRemapIngestion for LocalDbRewriteRemapIngestion {
    fn ingest(&mut self, request: RewriteRemapRequest) -> Result<bool> {
        let runtime = tokio::runtime::Builder::new_current_thread().build()?;
        let accepted = runtime.block_on(ingest_rewrite_mapping_to_local_db(
            &self.repository_root,
            &self.db_path,
            &request,
        ))?;
        if accepted {
            self.accepted_requests.push(request);
        }
        Ok(accepted)
    }
}

async fn ingest_rewrite_mapping_to_local_db(
    repository_root: &Path,
    db_path: &Path,
    request: &RewriteRemapRequest,
) -> Result<bool> {
    let location = db_path.to_str().ok_or_else(|| {
        anyhow::anyhow!("Local DB path must be valid UTF-8: {}", db_path.display())
    })?;
    let db = turso::Builder::new_local(location).build().await?;
    let conn = db.connect()?;
    conn.execute("PRAGMA foreign_keys = ON", ()).await?;

    let canonical_root = repository_root
        .canonicalize()
        .unwrap_or_else(|_| repository_root.to_path_buf())
        .to_string_lossy()
        .to_string();

    conn.execute(
        "INSERT OR IGNORE INTO repositories (canonical_root) VALUES (?1)",
        [canonical_root.as_str()],
    )
    .await?;

    let repository_id = {
        let mut rows = conn
            .query(
                "SELECT id FROM repositories WHERE canonical_root = ?1 LIMIT 1",
                [canonical_root.as_str()],
            )
            .await?;
        let row = rows
            .next()
            .await?
            .ok_or_else(|| anyhow::anyhow!("repository id query returned no rows"))?;
        let value = row.get_value(0)?;
        value
            .as_integer()
            .copied()
            .ok_or_else(|| anyhow::anyhow!("repository id query returned non-integer"))?
    };

    let existing = {
        let mut rows = conn
            .query(
                "SELECT COUNT(*) FROM rewrite_mappings WHERE repository_id = ?1 AND idempotency_key = ?2",
                (repository_id, request.idempotency_key.as_str()),
            )
            .await?;
        let row = rows
            .next()
            .await?
            .ok_or_else(|| anyhow::anyhow!("rewrite mapping count query returned no rows"))?;
        let value = row.get_value(0)?;
        value
            .as_integer()
            .copied()
            .ok_or_else(|| anyhow::anyhow!("rewrite mapping count query returned non-integer"))?
    };
    if existing > 0 {
        return Ok(false);
    }

    let reconciliation_key = format!(
        "local-post-rewrite:{}:{}",
        request.rewrite_method.canonical_label(),
        request.new_sha
    );
    conn.execute(
        "INSERT OR IGNORE INTO reconciliation_runs (repository_id, provider, idempotency_key, status) VALUES (?1, ?2, ?3, ?4)",
        (repository_id, "local-hook", reconciliation_key.as_str(), "completed"),
    )
    .await?;

    let run_id = {
        let mut rows = conn
            .query(
                "SELECT id FROM reconciliation_runs WHERE repository_id = ?1 AND idempotency_key = ?2 LIMIT 1",
                (repository_id, reconciliation_key.as_str()),
            )
            .await?;
        let row = rows
            .next()
            .await?
            .ok_or_else(|| anyhow::anyhow!("reconciliation run id query returned no rows"))?;
        let value = row.get_value(0)?;
        value
            .as_integer()
            .copied()
            .ok_or_else(|| anyhow::anyhow!("reconciliation run id query returned non-integer"))?
    };

    conn.execute(
        "INSERT INTO rewrite_mappings (reconciliation_run_id, repository_id, old_commit_sha, new_commit_sha, mapping_status, confidence, idempotency_key) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
        (
            run_id,
            repository_id,
            request.old_sha.as_str(),
            request.new_sha.as_str(),
            "mapped",
            1.0_f64,
            request.idempotency_key.as_str(),
        ),
    )
    .await?;

    Ok(true)
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PreCommitRuntimeState {
    pub sce_disabled: bool,
    pub cli_available: bool,
    pub is_bare_repo: bool,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PreCommitTreeAnchors {
    pub index_tree: String,
    pub head_tree: Option<String>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PendingLineRange {
    pub start_line: u32,
    pub end_line: u32,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PendingFileCheckpoint {
    pub path: String,
    pub has_sce_attribution: bool,
    pub staged_ranges: Vec<PendingLineRange>,
    pub unstaged_ranges: Vec<PendingLineRange>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PendingCheckpoint {
    pub files: Vec<PendingFileCheckpoint>,
    pub harness_type: String,
    pub git_branch: Option<String>,
    pub model_id: Option<String>,
    pub prompts: Vec<PendingPromptCheckpoint>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FinalizedFileCheckpoint {
    pub path: String,
    pub has_sce_attribution: bool,
    pub ranges: Vec<PendingLineRange>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PendingPromptCheckpoint {
    pub turn_number: u32,
    pub prompt_text: String,
    pub prompt_length: usize,
    pub is_truncated: bool,
    pub cwd: Option<String>,
    pub transcript_path: Option<String>,
    pub captured_at: String,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FinalizedPromptCheckpoint {
    pub turn_number: u32,
    pub prompt_text: String,
    pub prompt_length: usize,
    pub is_truncated: bool,
    pub cwd: Option<String>,
    pub transcript_path: Option<String>,
    pub captured_at: String,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FinalizedCheckpoint {
    pub anchors: PreCommitTreeAnchors,
    pub harness_type: String,
    pub git_branch: Option<String>,
    pub model_id: Option<String>,
    pub files: Vec<FinalizedFileCheckpoint>,
    pub prompts: Vec<FinalizedPromptCheckpoint>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PreCommitNoOpReason {
    Disabled,
    CliUnavailable,
    BareRepository,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PreCommitFinalization {
    NoOp(PreCommitNoOpReason),
    Finalized(FinalizedCheckpoint),
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CommitMsgRuntimeState {
    pub sce_disabled: bool,
    pub sce_coauthor_enabled: bool,
    pub has_staged_sce_attribution: bool,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PostCommitRuntimeState {
    pub sce_disabled: bool,
    pub cli_available: bool,
    pub is_bare_repo: bool,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PostCommitInput {
    pub record_id: String,
    pub timestamp_rfc3339: String,
    pub committed_at_unix_ms: i64,
    pub commit_sha: String,
    pub parent_sha: Option<String>,
    pub idempotency_key: String,
    pub files: Vec<FileAttributionInput>,
    pub prompts: Vec<PersistedPromptRecord>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PersistedPromptRecord {
    pub turn_number: u32,
    pub prompt_text: String,
    pub prompt_length: usize,
    pub is_truncated: bool,
    pub harness_type: String,
    pub model_id: Option<String>,
    pub cwd: Option<String>,
    pub git_branch: Option<String>,
    pub tool_call_count: u32,
    pub duration_ms: i64,
    pub captured_at: String,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TraceNote {
    pub notes_ref: String,
    pub commit_sha: String,
    pub content_type: String,
    pub record: AgentTraceRecord,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PersistedTraceRecord {
    pub commit_sha: String,
    pub idempotency_key: String,
    pub content_type: String,
    pub notes_ref: String,
    pub record: AgentTraceRecord,
    pub prompts: Vec<PersistedPromptRecord>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PersistenceErrorClass {
    Transient,
    Permanent,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PersistenceFailure {
    pub class: PersistenceErrorClass,
    pub message: String,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PersistenceWriteResult {
    Written,
    AlreadyExists,
    Failed(PersistenceFailure),
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PersistenceTarget {
    Notes,
    Database,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TraceRetryQueueEntry {
    pub commit_sha: String,
    pub failed_targets: Vec<PersistenceTarget>,
    pub content_type: String,
    pub notes_ref: String,
    pub record: AgentTraceRecord,
    pub prompts: Vec<PersistedPromptRecord>,
}

pub trait TraceNotesWriter {
    fn write_note(&mut self, note: TraceNote) -> PersistenceWriteResult;
}

pub trait TraceRecordStore {
    fn write_trace_record(&mut self, record: PersistedTraceRecord) -> PersistenceWriteResult;
}

pub trait TraceRetryQueue {
    fn enqueue(&mut self, entry: TraceRetryQueueEntry) -> Result<()>;
    fn dequeue_next(&mut self) -> Result<Option<TraceRetryQueueEntry>>;
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RetryProcessingMetric {
    pub commit_sha: String,
    pub trace_id: String,
    pub runtime_ms: u128,
    pub error_class: Option<PersistenceErrorClass>,
    pub failed_targets: Vec<PersistenceTarget>,
}

pub trait RetryMetricsSink {
    fn record_retry_metric(&mut self, metric: RetryProcessingMetric);
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RetryQueueProcessSummary {
    pub attempted: usize,
    pub recovered: usize,
    pub requeued: usize,
}

pub trait TraceEmissionLedger {
    fn has_emitted(&self, commit_sha: &str) -> bool;
    fn mark_emitted(&mut self, commit_sha: &str);
}

struct GitNotesTraceWriter {
    repository_root: PathBuf,
}

impl TraceNotesWriter for GitNotesTraceWriter {
    fn write_note(&mut self, note: TraceNote) -> PersistenceWriteResult {
        let payload = match serialize_note_payload(&note) {
            Ok(payload) => payload,
            Err(error) => {
                return PersistenceWriteResult::Failed(PersistenceFailure {
                    class: PersistenceErrorClass::Permanent,
                    message: format!("failed to serialize trace note payload: {error}"),
                });
            }
        };

        let existing = Command::new("git")
            .args([
                "notes",
                "--ref",
                note.notes_ref.as_str(),
                "show",
                note.commit_sha.as_str(),
            ])
            .current_dir(&self.repository_root)
            .output();
        if let Ok(output) = &existing {
            if output.status.success() {
                let existing_payload = String::from_utf8_lossy(&output.stdout).trim().to_string();
                if existing_payload == payload {
                    return PersistenceWriteResult::AlreadyExists;
                }
            }
        }

        match Command::new("git")
            .args([
                "notes",
                "--ref",
                note.notes_ref.as_str(),
                "add",
                "-f",
                "-m",
                payload.as_str(),
                note.commit_sha.as_str(),
            ])
            .current_dir(&self.repository_root)
            .output()
        {
            Ok(output) if output.status.success() => PersistenceWriteResult::Written,
            Ok(output) => PersistenceWriteResult::Failed(PersistenceFailure {
                class: classify_persistence_error_class_from_stderr(&String::from_utf8_lossy(
                    &output.stderr,
                )),
                message: format!(
                    "failed to write git note for commit '{}': {}",
                    note.commit_sha,
                    String::from_utf8_lossy(&output.stderr).trim()
                ),
            }),
            Err(error) => PersistenceWriteResult::Failed(PersistenceFailure {
                class: classify_persistence_error_class_from_io(&error),
                message: format!(
                    "failed to execute git notes command for commit '{}': {}",
                    note.commit_sha, error
                ),
            }),
        }
    }
}

struct LocalDbTraceRecordStore {
    repository_root: PathBuf,
    db_path: PathBuf,
}

impl TraceRecordStore for LocalDbTraceRecordStore {
    fn write_trace_record(&mut self, record: PersistedTraceRecord) -> PersistenceWriteResult {
        let runtime = match tokio::runtime::Builder::new_current_thread().build() {
            Ok(runtime) => runtime,
            Err(error) => {
                return PersistenceWriteResult::Failed(PersistenceFailure {
                    class: PersistenceErrorClass::Permanent,
                    message: format!("failed to initialize local DB runtime: {error}"),
                })
            }
        };

        match runtime.block_on(write_trace_record_to_local_db(
            &self.db_path,
            &self.repository_root,
            &record,
        )) {
            Ok(written) => {
                if written {
                    PersistenceWriteResult::Written
                } else {
                    PersistenceWriteResult::AlreadyExists
                }
            }
            Err(error) => PersistenceWriteResult::Failed(PersistenceFailure {
                class: classify_persistence_error_class_from_message(&error.to_string()),
                message: format!(
                    "failed to persist trace record in local DB '{}': {error}",
                    self.db_path.display()
                ),
            }),
        }
    }
}

#[allow(clippy::too_many_lines)]
async fn write_trace_record_to_local_db(
    db_path: &Path,
    repository_root: &Path,
    record: &PersistedTraceRecord,
) -> Result<bool> {
    let location = db_path.to_str().ok_or_else(|| {
        anyhow::anyhow!("Local DB path must be valid UTF-8: {}", db_path.display())
    })?;
    let db = turso::Builder::new_local(location).build().await?;
    let conn = db.connect()?;
    conn.execute("PRAGMA foreign_keys = ON", ()).await?;

    let canonical_root = repository_root
        .canonicalize()
        .unwrap_or_else(|_| repository_root.to_path_buf())
        .to_string_lossy()
        .to_string();

    conn.execute(
        "INSERT OR IGNORE INTO repositories (canonical_root) VALUES (?1)",
        [canonical_root.as_str()],
    )
    .await?;

    let repository_id = {
        let mut rows = conn
            .query(
                "SELECT id FROM repositories WHERE canonical_root = ?1 LIMIT 1",
                [canonical_root.as_str()],
            )
            .await?;
        let row = rows
            .next()
            .await?
            .ok_or_else(|| anyhow::anyhow!("repository id query returned no rows"))?;
        let value = row.get_value(0)?;
        value
            .as_integer()
            .copied()
            .ok_or_else(|| anyhow::anyhow!("repository id query returned non-integer"))?
    };

    conn.execute(
        "INSERT OR IGNORE INTO commits (repository_id, commit_sha, idempotency_key) VALUES (?1, ?2, ?3)",
        (
            repository_id,
            record.commit_sha.as_str(),
            record.idempotency_key.as_str(),
        ),
    )
    .await?;

    let commit_id = {
        let mut rows = conn
            .query(
                "SELECT id FROM commits WHERE repository_id = ?1 AND commit_sha = ?2 LIMIT 1",
                (repository_id, record.commit_sha.as_str()),
            )
            .await?;
        let row = rows
            .next()
            .await?
            .ok_or_else(|| anyhow::anyhow!("commit id query returned no rows"))?;
        let value = row.get_value(0)?;
        value
            .as_integer()
            .copied()
            .ok_or_else(|| anyhow::anyhow!("commit id query returned non-integer"))?
    };

    let existing = {
        let mut rows = conn
            .query(
                "SELECT COUNT(*) FROM trace_records WHERE repository_id = ?1 AND (commit_id = ?2 OR idempotency_key = ?3)",
                (repository_id, commit_id, record.idempotency_key.as_str()),
            )
            .await?;
        let row = rows
            .next()
            .await?
            .ok_or_else(|| anyhow::anyhow!("existing trace count query returned no rows"))?;
        let value = row.get_value(0)?;
        value
            .as_integer()
            .copied()
            .ok_or_else(|| anyhow::anyhow!("existing trace count query returned non-integer"))?
    };
    if existing > 0 {
        return Ok(false);
    }

    let payload_json = serde_json::to_string(&trace_record_to_json(&record.record))
        .context("failed to serialize trace record JSON payload")?;
    let quality_status = record
        .record
        .metadata
        .get(METADATA_QUALITY_STATUS)
        .cloned()
        .unwrap_or_else(|| String::from("final"));

    conn.execute(
        "INSERT INTO trace_records (repository_id, commit_id, trace_id, version, content_type, notes_ref, payload_json, quality_status, idempotency_key, recorded_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
        (
            repository_id,
            commit_id,
            record.record.id.as_str(),
            record.record.version.as_str(),
            record.content_type.as_str(),
            record.notes_ref.as_str(),
            payload_json.as_str(),
            quality_status.as_str(),
            record.idempotency_key.as_str(),
            record.record.timestamp.as_str(),
        ),
    )
    .await?;

    let trace_record_id = {
        let mut rows = conn
            .query(
                "SELECT id FROM trace_records WHERE trace_id = ?1 LIMIT 1",
                [record.record.id.as_str()],
            )
            .await?;
        let row = rows
            .next()
            .await?
            .ok_or_else(|| anyhow::anyhow!("trace record id query returned no rows"))?;
        let value = row.get_value(0)?;
        value
            .as_integer()
            .copied()
            .ok_or_else(|| anyhow::anyhow!("trace record id query returned non-integer"))?
    };

    for file in &record.record.files {
        for conversation in &file.conversations {
            for range in &conversation.ranges {
                conn.execute(
                    "INSERT INTO trace_ranges (trace_record_id, file_path, conversation_url, start_line, end_line, contributor_type, contributor_model_id) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
                    (
                        trace_record_id,
                        file.path.as_str(),
                        conversation.url.as_str(),
                        i64::from(range.start_line),
                        i64::from(range.end_line),
                        range.contributor.r#type.as_str(),
                        range.contributor.model_id.as_deref(),
                    ),
                )
                .await?;
            }
        }
    }

    for prompt in &record.prompts {
        conn.execute(
            "INSERT INTO prompts (commit_id, prompt_text, prompt_length, is_truncated, turn_number, harness_type, model_id, cwd, git_branch, tool_call_count, duration_ms, captured_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)",
            (
                commit_id,
                prompt.prompt_text.as_str(),
                i64::try_from(prompt.prompt_length)
                    .context("Prompt length exceeded supported SQLite integer range")?,
                prompt.is_truncated,
                i64::from(prompt.turn_number),
                prompt.harness_type.as_str(),
                prompt.model_id.as_deref(),
                prompt.cwd.as_deref(),
                prompt.git_branch.as_deref(),
                i64::from(prompt.tool_call_count),
                prompt.duration_ms,
                prompt.captured_at.as_str(),
            ),
        )
        .await?;
    }

    Ok(true)
}

struct JsonFileTraceRetryQueue {
    path: PathBuf,
}

impl TraceRetryQueue for JsonFileTraceRetryQueue {
    fn enqueue(&mut self, entry: TraceRetryQueueEntry) -> Result<()> {
        if let Some(parent) = self.path.parent() {
            fs::create_dir_all(parent).with_context(|| {
                format!(
                    "Failed to create retry queue directory '{}'",
                    parent.display()
                )
            })?;
        }

        let line = serde_json::json!({
            "commit_sha": entry.commit_sha,
            "failed_targets": entry
                .failed_targets
                .iter()
                .copied()
                .map(persistence_target_label)
                .collect::<Vec<_>>(),
            "content_type": entry.content_type,
            "notes_ref": entry.notes_ref,
            "record": trace_record_to_json(&entry.record),
            "prompts": entry.prompts.iter().map(prompt_to_json).collect::<Vec<_>>(),
        })
        .to_string();
        append_jsonl_line(&self.path, &line)?;

        Ok(())
    }

    fn dequeue_next(&mut self) -> Result<Option<TraceRetryQueueEntry>> {
        if !self.path.exists() {
            return Ok(None);
        }

        let payload = fs::read_to_string(&self.path).with_context(|| {
            format!(
                "Failed to read retry queue file '{}' for dequeue.",
                self.path.display()
            )
        })?;

        let mut lines = payload.lines();
        let Some(first_line) = lines.next() else {
            return Ok(None);
        };

        let mut remaining = String::new();
        for line in lines {
            remaining.push_str(line);
            remaining.push('\n');
        }
        fs::write(&self.path, remaining).with_context(|| {
            format!(
                "Failed to rewrite retry queue file '{}' after dequeue.",
                self.path.display()
            )
        })?;

        let parsed = serde_json::from_str::<serde_json::Value>(first_line)
            .context("Failed to parse retry queue entry JSON during dequeue")?;
        let commit_sha = parsed
            .get("commit_sha")
            .and_then(serde_json::Value::as_str)
            .context("Retry queue entry missing 'commit_sha' string")?
            .to_string();
        let content_type = parsed
            .get("content_type")
            .and_then(serde_json::Value::as_str)
            .context("Retry queue entry missing 'content_type' string")?
            .to_string();
        let notes_ref = parsed
            .get("notes_ref")
            .and_then(serde_json::Value::as_str)
            .context("Retry queue entry missing 'notes_ref' string")?
            .to_string();
        let record = trace_record_from_json(
            parsed
                .get("record")
                .context("Retry queue entry missing 'record' object")?,
        )?;

        let failed_targets = parsed
            .get("failed_targets")
            .and_then(serde_json::Value::as_array)
            .into_iter()
            .flatten()
            .filter_map(|value| value.as_str())
            .filter_map(persistence_target_from_label)
            .collect::<Vec<_>>();
        let prompts = parsed
            .get("prompts")
            .and_then(serde_json::Value::as_array)
            .into_iter()
            .flatten()
            .map(prompt_from_json)
            .collect::<Result<Vec<_>>>()?;

        Ok(Some(TraceRetryQueueEntry {
            commit_sha,
            failed_targets,
            content_type,
            notes_ref,
            record,
            prompts,
        }))
    }
}

struct FileTraceEmissionLedger {
    path: PathBuf,
}

impl TraceEmissionLedger for FileTraceEmissionLedger {
    fn has_emitted(&self, commit_sha: &str) -> bool {
        fs::read_to_string(&self.path)
            .ok()
            .is_some_and(|contents| contents.lines().any(|line| line.trim() == commit_sha))
    }

    fn mark_emitted(&mut self, commit_sha: &str) {
        if self.has_emitted(commit_sha) {
            return;
        }

        if let Some(parent) = self.path.parent() {
            if fs::create_dir_all(parent).is_err() {
                return;
            }
        }

        if let Ok(mut file) = fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&self.path)
        {
            // Best-effort append to notes; errors are logged but don't fail the commit
            if let Err(e) = writeln!(file, "{commit_sha}") {
                eprintln!("Warning: Failed to append commit SHA to notes: {e}");
            }
        }
    }
}

fn append_jsonl_line(path: &Path, line: &str) -> std::io::Result<()> {
    let mut file = fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)?;
    writeln!(file, "{line}")?;
    Ok(())
}

fn classify_persistence_error_class_from_io(error: &std::io::Error) -> PersistenceErrorClass {
    match error.kind() {
        std::io::ErrorKind::Interrupted
        | std::io::ErrorKind::WouldBlock
        | std::io::ErrorKind::TimedOut
        | std::io::ErrorKind::ConnectionRefused
        | std::io::ErrorKind::ConnectionReset
        | std::io::ErrorKind::ConnectionAborted
        | std::io::ErrorKind::NotConnected => PersistenceErrorClass::Transient,
        _ => PersistenceErrorClass::Permanent,
    }
}

fn classify_persistence_error_class_from_stderr(stderr: &str) -> PersistenceErrorClass {
    let lowered = stderr.to_ascii_lowercase();
    if lowered.contains("timed out")
        || lowered.contains("temporar")
        || lowered.contains("try again")
        || lowered.contains("index.lock")
    {
        return PersistenceErrorClass::Transient;
    }

    PersistenceErrorClass::Permanent
}

fn classify_persistence_error_class_from_message(message: &str) -> PersistenceErrorClass {
    let lowered = message.to_ascii_lowercase();
    if lowered.contains("locked")
        || lowered.contains("timed out")
        || lowered.contains("temporar")
        || lowered.contains("try again")
    {
        return PersistenceErrorClass::Transient;
    }

    PersistenceErrorClass::Permanent
}

fn persistence_target_label(target: PersistenceTarget) -> &'static str {
    match target {
        PersistenceTarget::Notes => "notes",
        PersistenceTarget::Database => "database",
    }
}

fn persistence_target_from_label(label: &str) -> Option<PersistenceTarget> {
    match label {
        "notes" => Some(PersistenceTarget::Notes),
        "database" => Some(PersistenceTarget::Database),
        _ => None,
    }
}

fn serialize_note_payload(note: &TraceNote) -> Result<String> {
    serde_json::to_string_pretty(&serde_json::json!({
        "content_type": note.content_type,
        "record": trace_record_to_json(&note.record),
    }))
    .context("Failed to serialize trace note payload")
}

fn trace_record_to_json(record: &AgentTraceRecord) -> serde_json::Value {
    serde_json::json!({
        "version": record.version,
        "id": record.id,
        "timestamp": record.timestamp,
        "vcs": {
            "type": record.vcs.r#type,
            "revision": record.vcs.revision,
        },
        "files": record.files.iter().map(|file| {
            serde_json::json!({
                "path": file.path,
                "conversations": file.conversations.iter().map(|conversation| {
                    serde_json::json!({
                        "url": conversation.url,
                        "related": conversation.related,
                        "ranges": conversation.ranges.iter().map(|range| {
                            serde_json::json!({
                                "start_line": range.start_line,
                                "end_line": range.end_line,
                                "contributor": {
                                    "type": range.contributor.r#type,
                                    "model_id": range.contributor.model_id,
                                },
                            })
                        }).collect::<Vec<_>>(),
                    })
                }).collect::<Vec<_>>(),
            })
        }).collect::<Vec<_>>(),
        "metadata": record.metadata,
    })
}

#[allow(clippy::too_many_lines)]
fn trace_record_from_json(value: &serde_json::Value) -> Result<AgentTraceRecord> {
    let version = value
        .get("version")
        .and_then(serde_json::Value::as_str)
        .unwrap_or(TRACE_VERSION)
        .to_string();
    let id = value
        .get("id")
        .and_then(serde_json::Value::as_str)
        .context("trace record JSON missing id")?
        .to_string();
    let timestamp = value
        .get("timestamp")
        .and_then(serde_json::Value::as_str)
        .context("trace record JSON missing timestamp")?
        .to_string();

    let vcs = value
        .get("vcs")
        .and_then(serde_json::Value::as_object)
        .context("trace record JSON missing vcs object")?;
    let vcs_type = vcs
        .get("type")
        .and_then(serde_json::Value::as_str)
        .unwrap_or(VCS_TYPE_GIT)
        .to_string();
    let vcs_revision = vcs
        .get("revision")
        .and_then(serde_json::Value::as_str)
        .context("trace record JSON missing vcs.revision")?
        .to_string();

    let files_json = value
        .get("files")
        .and_then(serde_json::Value::as_array)
        .cloned()
        .unwrap_or_default();
    let mut files = Vec::new();
    for file in files_json {
        let Some(path) = file.get("path").and_then(serde_json::Value::as_str) else {
            continue;
        };
        let mut conversations = Vec::new();
        for conversation in file
            .get("conversations")
            .and_then(serde_json::Value::as_array)
            .into_iter()
            .flatten()
        {
            let Some(url) = conversation.get("url").and_then(serde_json::Value::as_str) else {
                continue;
            };
            let related = conversation
                .get("related")
                .and_then(serde_json::Value::as_array)
                .into_iter()
                .flatten()
                .filter_map(|value| value.as_str().map(ToString::to_string))
                .collect::<Vec<_>>();
            let mut ranges = Vec::new();
            for range in conversation
                .get("ranges")
                .and_then(serde_json::Value::as_array)
                .into_iter()
                .flatten()
            {
                let Some(start_line) = range
                    .get("start_line")
                    .and_then(serde_json::Value::as_u64)
                    .and_then(|value| u32::try_from(value).ok())
                else {
                    continue;
                };
                let Some(end_line) = range
                    .get("end_line")
                    .and_then(serde_json::Value::as_u64)
                    .and_then(|value| u32::try_from(value).ok())
                else {
                    continue;
                };
                let contributor = range
                    .get("contributor")
                    .and_then(serde_json::Value::as_object)
                    .cloned()
                    .unwrap_or_default();
                ranges.push(AgentTraceRange {
                    start_line,
                    end_line,
                    contributor: AgentTraceContributor {
                        r#type: contributor
                            .get("type")
                            .and_then(serde_json::Value::as_str)
                            .unwrap_or("unknown")
                            .to_string(),
                        model_id: contributor
                            .get("model_id")
                            .and_then(serde_json::Value::as_str)
                            .map(ToString::to_string),
                    },
                });
            }

            conversations.push(AgentTraceConversation {
                url: url.to_string(),
                related,
                ranges,
            });
        }

        files.push(AgentTraceFile {
            path: path.to_string(),
            conversations,
        });
    }

    let metadata = value
        .get("metadata")
        .and_then(serde_json::Value::as_object)
        .map(|map| {
            map.iter()
                .filter_map(|(key, value)| {
                    value.as_str().map(|value| (key.clone(), value.to_string()))
                })
                .collect::<BTreeMap<_, _>>()
        })
        .unwrap_or_default();

    Ok(AgentTraceRecord {
        version,
        id,
        timestamp,
        vcs: AgentTraceVcs {
            r#type: vcs_type,
            revision: vcs_revision,
        },
        files,
        metadata,
    })
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PostCommitNoOpReason {
    Disabled,
    CliUnavailable,
    BareRepository,
    AlreadyFinalized,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PostCommitPersisted {
    pub commit_sha: String,
    pub notes: PersistenceWriteResult,
    pub database: PersistenceWriteResult,
    pub trace_id: String,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PostCommitQueuedFallback {
    pub commit_sha: String,
    pub failed_targets: Vec<PersistenceTarget>,
    pub trace_id: String,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PostCommitFinalization {
    NoOp(PostCommitNoOpReason),
    Persisted(PostCommitPersisted),
    QueuedFallback(PostCommitQueuedFallback),
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PostRewriteRuntimeState {
    pub sce_disabled: bool,
    pub cli_available: bool,
    pub is_bare_repo: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub struct RewriteTraceInput {
    pub record_id: String,
    pub timestamp_rfc3339: String,
    pub rewritten_commit_sha: String,
    pub rewrite_from_sha: String,
    pub rewrite_method: RewriteMethod,
    pub rewrite_confidence: f32,
    pub idempotency_key: String,
    pub files: Vec<FileAttributionInput>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RewriteTraceNoOpReason {
    Disabled,
    CliUnavailable,
    BareRepository,
    AlreadyFinalized,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RewriteTracePersisted {
    pub commit_sha: String,
    pub trace_id: String,
    pub quality_status: QualityStatus,
    pub notes: PersistenceWriteResult,
    pub database: PersistenceWriteResult,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RewriteTraceQueuedFallback {
    pub commit_sha: String,
    pub trace_id: String,
    pub quality_status: QualityStatus,
    pub failed_targets: Vec<PersistenceTarget>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RewriteTraceFinalization {
    NoOp(RewriteTraceNoOpReason),
    Persisted(RewriteTracePersisted),
    QueuedFallback(RewriteTraceQueuedFallback),
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PostRewriteNoOpReason {
    Disabled,
    CliUnavailable,
    BareRepository,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RewriteMethod {
    Amend,
    Rebase,
    Other(String),
}

impl RewriteMethod {
    fn canonical_label(&self) -> &str {
        match self {
            RewriteMethod::Amend => "amend",
            RewriteMethod::Rebase => "rebase",
            RewriteMethod::Other(method) => method.as_str(),
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RewritePair {
    pub old_sha: String,
    pub new_sha: String,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RewriteRemapRequest {
    pub rewrite_method: RewriteMethod,
    pub old_sha: String,
    pub new_sha: String,
    pub idempotency_key: String,
}

pub trait RewriteRemapIngestion {
    fn ingest(&mut self, request: RewriteRemapRequest) -> Result<bool>;
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PostRewriteIngested {
    pub rewrite_method: RewriteMethod,
    pub total_pairs: usize,
    pub ingested_pairs: usize,
    pub skipped_pairs: usize,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PostRewriteFinalization {
    NoOp(PostRewriteNoOpReason),
    Ingested(PostRewriteIngested),
}

pub fn finalize_post_rewrite_remap(
    runtime: &PostRewriteRuntimeState,
    rewrite_method: &str,
    pairs_file_contents: &str,
    remap_ingestion: &mut impl RewriteRemapIngestion,
) -> Result<PostRewriteFinalization> {
    if runtime.sce_disabled {
        return Ok(PostRewriteFinalization::NoOp(
            PostRewriteNoOpReason::Disabled,
        ));
    }

    if !runtime.cli_available {
        return Ok(PostRewriteFinalization::NoOp(
            PostRewriteNoOpReason::CliUnavailable,
        ));
    }

    if runtime.is_bare_repo {
        return Ok(PostRewriteFinalization::NoOp(
            PostRewriteNoOpReason::BareRepository,
        ));
    }

    let method = normalize_rewrite_method(rewrite_method);
    let pairs = parse_post_rewrite_pairs(pairs_file_contents)?;

    let mut ingested_pairs = 0_usize;
    for pair in &pairs {
        let idempotency_key = format!(
            "post-rewrite:{}:{}:{}",
            method.canonical_label(),
            pair.old_sha,
            pair.new_sha
        );
        let accepted = remap_ingestion.ingest(RewriteRemapRequest {
            rewrite_method: method.clone(),
            old_sha: pair.old_sha.clone(),
            new_sha: pair.new_sha.clone(),
            idempotency_key,
        })?;
        if accepted {
            ingested_pairs += 1;
        }
    }

    let total_pairs = pairs.len();
    Ok(PostRewriteFinalization::Ingested(PostRewriteIngested {
        rewrite_method: method,
        total_pairs,
        ingested_pairs,
        skipped_pairs: total_pairs.saturating_sub(ingested_pairs),
    }))
}

pub fn finalize_rewrite_trace(
    runtime: &PostRewriteRuntimeState,
    input: RewriteTraceInput,
    notes_writer: &mut impl TraceNotesWriter,
    record_store: &mut impl TraceRecordStore,
    retry_queue: &mut impl TraceRetryQueue,
    emission_ledger: &mut impl TraceEmissionLedger,
) -> Result<RewriteTraceFinalization> {
    if runtime.sce_disabled {
        return Ok(RewriteTraceFinalization::NoOp(
            RewriteTraceNoOpReason::Disabled,
        ));
    }

    if !runtime.cli_available {
        return Ok(RewriteTraceFinalization::NoOp(
            RewriteTraceNoOpReason::CliUnavailable,
        ));
    }

    if runtime.is_bare_repo {
        return Ok(RewriteTraceFinalization::NoOp(
            RewriteTraceNoOpReason::BareRepository,
        ));
    }

    if emission_ledger.has_emitted(&input.rewritten_commit_sha) {
        return Ok(RewriteTraceFinalization::NoOp(
            RewriteTraceNoOpReason::AlreadyFinalized,
        ));
    }

    let confidence = normalize_rewrite_confidence(input.rewrite_confidence)?;
    let quality_status = quality_status_for_confidence(input.rewrite_confidence);
    let record = build_trace_payload(TraceAdapterInput {
        record_id: input.record_id,
        timestamp_rfc3339: input.timestamp_rfc3339,
        commit_sha: input.rewritten_commit_sha.clone(),
        files: input.files,
        quality_status,
        rewrite: Some(RewriteInfo {
            from_sha: input.rewrite_from_sha,
            method: input.rewrite_method.canonical_label().to_string(),
            confidence,
        }),
        idempotency_key: Some(input.idempotency_key.clone()),
    });

    let note = TraceNote {
        notes_ref: crate::services::agent_trace::NOTES_REF.to_string(),
        commit_sha: input.rewritten_commit_sha.clone(),
        content_type: TRACE_CONTENT_TYPE.to_string(),
        record: record.clone(),
    };
    let persisted = PersistedTraceRecord {
        commit_sha: input.rewritten_commit_sha.clone(),
        idempotency_key: input.idempotency_key,
        content_type: TRACE_CONTENT_TYPE.to_string(),
        notes_ref: crate::services::agent_trace::NOTES_REF.to_string(),
        record: record.clone(),
        prompts: Vec::new(),
    };

    let notes_result = notes_writer.write_note(note);
    let database_result = record_store.write_trace_record(persisted);

    let failed_targets = collect_failed_targets(&notes_result, &database_result);
    if failed_targets.is_empty() {
        emission_ledger.mark_emitted(&input.rewritten_commit_sha);
        return Ok(RewriteTraceFinalization::Persisted(RewriteTracePersisted {
            commit_sha: input.rewritten_commit_sha,
            trace_id: record.id,
            quality_status,
            notes: notes_result,
            database: database_result,
        }));
    }

    retry_queue.enqueue(TraceRetryQueueEntry {
        commit_sha: input.rewritten_commit_sha.clone(),
        failed_targets: failed_targets.clone(),
        content_type: TRACE_CONTENT_TYPE.to_string(),
        notes_ref: crate::services::agent_trace::NOTES_REF.to_string(),
        record: record.clone(),
        prompts: Vec::new(),
    })?;

    Ok(RewriteTraceFinalization::QueuedFallback(
        RewriteTraceQueuedFallback {
            commit_sha: input.rewritten_commit_sha,
            trace_id: record.id,
            quality_status,
            failed_targets,
        },
    ))
}

fn normalize_rewrite_confidence(confidence: f32) -> Result<String> {
    if !confidence.is_finite() {
        anyhow::bail!("rewrite confidence must be finite")
    }

    if !(0.0..=1.0).contains(&confidence) {
        anyhow::bail!("rewrite confidence must be within [0.0, 1.0]")
    }

    Ok(format!("{confidence:.2}"))
}

fn quality_status_for_confidence(confidence: f32) -> QualityStatus {
    if confidence >= 0.90 {
        return QualityStatus::Final;
    }

    if confidence >= 0.60 {
        return QualityStatus::Partial;
    }

    QualityStatus::NeedsReview
}

fn parse_post_rewrite_pairs(contents: &str) -> Result<Vec<RewritePair>> {
    let mut pairs = Vec::new();

    for (line_index, line) in contents.lines().enumerate() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }

        let mut fields = trimmed.split_whitespace();
        let Some(old_sha) = fields.next() else {
            continue;
        };
        let Some(new_sha) = fields.next() else {
            anyhow::bail!(
                "Invalid post-rewrite pair format on line {}: expected '<old_sha> <new_sha>'",
                line_index + 1
            );
        };

        if fields.next().is_some() {
            anyhow::bail!(
                "Invalid post-rewrite pair format on line {}: expected exactly two fields",
                line_index + 1
            );
        }

        if old_sha == new_sha {
            continue;
        }

        pairs.push(RewritePair {
            old_sha: old_sha.to_string(),
            new_sha: new_sha.to_string(),
        });
    }

    Ok(pairs)
}

fn normalize_rewrite_method(method: &str) -> RewriteMethod {
    match method.trim().to_ascii_lowercase().as_str() {
        "amend" => RewriteMethod::Amend,
        "rebase" => RewriteMethod::Rebase,
        other => RewriteMethod::Other(other.to_string()),
    }
}

pub fn finalize_post_commit_trace(
    runtime: &PostCommitRuntimeState,
    input: PostCommitInput,
    notes_writer: &mut impl TraceNotesWriter,
    record_store: &mut impl TraceRecordStore,
    retry_queue: &mut impl TraceRetryQueue,
    emission_ledger: &mut impl TraceEmissionLedger,
) -> Result<PostCommitFinalization> {
    if runtime.sce_disabled {
        return Ok(PostCommitFinalization::NoOp(PostCommitNoOpReason::Disabled));
    }

    if !runtime.cli_available {
        return Ok(PostCommitFinalization::NoOp(
            PostCommitNoOpReason::CliUnavailable,
        ));
    }

    if runtime.is_bare_repo {
        return Ok(PostCommitFinalization::NoOp(
            PostCommitNoOpReason::BareRepository,
        ));
    }

    if emission_ledger.has_emitted(&input.commit_sha) {
        return Ok(PostCommitFinalization::NoOp(
            PostCommitNoOpReason::AlreadyFinalized,
        ));
    }

    let PostCommitInput {
        record_id,
        timestamp_rfc3339,
        committed_at_unix_ms: _,
        commit_sha,
        parent_sha,
        idempotency_key,
        files,
        prompts,
    } = input;

    let mut record = build_trace_payload(TraceAdapterInput {
        record_id,
        timestamp_rfc3339,
        commit_sha: commit_sha.clone(),
        files,
        quality_status: QualityStatus::Final,
        rewrite: None,
        idempotency_key: Some(idempotency_key.clone()),
    });

    if let Some(parent_sha) = parent_sha {
        record
            .metadata
            .insert(POST_COMMIT_PARENT_SHA_METADATA_KEY.to_string(), parent_sha);
    }

    let note = TraceNote {
        notes_ref: crate::services::agent_trace::NOTES_REF.to_string(),
        commit_sha: commit_sha.clone(),
        content_type: TRACE_CONTENT_TYPE.to_string(),
        record: record.clone(),
    };
    let persisted = PersistedTraceRecord {
        commit_sha: commit_sha.clone(),
        idempotency_key,
        content_type: TRACE_CONTENT_TYPE.to_string(),
        notes_ref: crate::services::agent_trace::NOTES_REF.to_string(),
        record: record.clone(),
        prompts: prompts.clone(),
    };

    let notes_result = notes_writer.write_note(note);
    let database_result = record_store.write_trace_record(persisted);

    let failed_targets = collect_failed_targets(&notes_result, &database_result);
    if failed_targets.is_empty() {
        emission_ledger.mark_emitted(&commit_sha);
        return Ok(PostCommitFinalization::Persisted(PostCommitPersisted {
            commit_sha,
            notes: notes_result,
            database: database_result,
            trace_id: record.id,
        }));
    }

    retry_queue.enqueue(TraceRetryQueueEntry {
        commit_sha: commit_sha.clone(),
        failed_targets: failed_targets.clone(),
        content_type: TRACE_CONTENT_TYPE.to_string(),
        notes_ref: crate::services::agent_trace::NOTES_REF.to_string(),
        record: record.clone(),
        prompts,
    })?;

    Ok(PostCommitFinalization::QueuedFallback(
        PostCommitQueuedFallback {
            commit_sha,
            failed_targets,
            trace_id: record.id,
        },
    ))
}

fn collect_failed_targets(
    notes_result: &PersistenceWriteResult,
    database_result: &PersistenceWriteResult,
) -> Vec<PersistenceTarget> {
    let mut failed_targets = Vec::new();
    if matches!(notes_result, PersistenceWriteResult::Failed(_)) {
        failed_targets.push(PersistenceTarget::Notes);
    }
    if matches!(database_result, PersistenceWriteResult::Failed(_)) {
        failed_targets.push(PersistenceTarget::Database);
    }
    failed_targets
}

pub fn process_trace_retry_queue(
    retry_queue: &mut impl TraceRetryQueue,
    notes_writer: &mut impl TraceNotesWriter,
    record_store: &mut impl TraceRecordStore,
    metrics_sink: &mut impl RetryMetricsSink,
    max_items: usize,
) -> Result<RetryQueueProcessSummary> {
    let mut processed_trace_ids = HashSet::new();
    let mut summary = RetryQueueProcessSummary {
        attempted: 0,
        recovered: 0,
        requeued: 0,
    };

    for _ in 0..max_items {
        let Some(entry) = retry_queue.dequeue_next()? else {
            break;
        };

        if !processed_trace_ids.insert(entry.record.id.clone()) {
            retry_queue.enqueue(entry)?;
            break;
        }

        summary.attempted += 1;
        let started = Instant::now();

        let notes_result = if entry.failed_targets.contains(&PersistenceTarget::Notes) {
            notes_writer.write_note(TraceNote {
                notes_ref: entry.notes_ref.clone(),
                commit_sha: entry.commit_sha.clone(),
                content_type: entry.content_type.clone(),
                record: entry.record.clone(),
            })
        } else {
            PersistenceWriteResult::AlreadyExists
        };

        let database_result = if entry.failed_targets.contains(&PersistenceTarget::Database) {
            let idempotency_key = entry
                .record
                .metadata
                .get(METADATA_IDEMPOTENCY_KEY)
                .cloned()
                .unwrap_or_else(|| format!("retry:{}:{}", entry.commit_sha, entry.record.id));
            record_store.write_trace_record(PersistedTraceRecord {
                commit_sha: entry.commit_sha.clone(),
                idempotency_key,
                content_type: entry.content_type.clone(),
                notes_ref: entry.notes_ref.clone(),
                record: entry.record.clone(),
                prompts: entry.prompts.clone(),
            })
        } else {
            PersistenceWriteResult::AlreadyExists
        };

        let failed_targets = collect_failed_targets(&notes_result, &database_result);
        let error_class = first_failure_class(&notes_result, &database_result);

        metrics_sink.record_retry_metric(RetryProcessingMetric {
            commit_sha: entry.commit_sha.clone(),
            trace_id: entry.record.id.clone(),
            runtime_ms: started.elapsed().as_millis(),
            error_class,
            failed_targets: failed_targets.clone(),
        });

        if failed_targets.is_empty() {
            summary.recovered += 1;
            continue;
        }

        summary.requeued += 1;
        retry_queue.enqueue(TraceRetryQueueEntry {
            commit_sha: entry.commit_sha,
            failed_targets,
            content_type: entry.content_type,
            notes_ref: entry.notes_ref,
            record: entry.record,
            prompts: entry.prompts,
        })?;
    }

    Ok(summary)
}

fn first_failure_class(
    notes_result: &PersistenceWriteResult,
    database_result: &PersistenceWriteResult,
) -> Option<PersistenceErrorClass> {
    match notes_result {
        PersistenceWriteResult::Failed(failure) => return Some(failure.class.clone()),
        PersistenceWriteResult::Written | PersistenceWriteResult::AlreadyExists => {}
    }

    match database_result {
        PersistenceWriteResult::Failed(failure) => Some(failure.class.clone()),
        PersistenceWriteResult::Written | PersistenceWriteResult::AlreadyExists => None,
    }
}

pub fn apply_commit_msg_coauthor_policy(
    runtime: &CommitMsgRuntimeState,
    commit_message: &str,
) -> String {
    if runtime.sce_disabled || !runtime.sce_coauthor_enabled || !runtime.has_staged_sce_attribution
    {
        return commit_message.to_string();
    }

    let mut lines: Vec<&str> = commit_message.lines().collect();
    lines.retain(|line| *line != CANONICAL_SCE_COAUTHOR_TRAILER);

    if !lines.is_empty() && !lines.last().is_some_and(|line| line.is_empty()) {
        lines.push("");
    }
    lines.push(CANONICAL_SCE_COAUTHOR_TRAILER);

    let mut normalized = lines.join("\n");
    if commit_message.ends_with('\n') {
        normalized.push('\n');
    }

    normalized
}

pub fn finalize_pre_commit_checkpoint(
    runtime: &PreCommitRuntimeState,
    anchors: PreCommitTreeAnchors,
    pending: PendingCheckpoint,
) -> PreCommitFinalization {
    if runtime.sce_disabled {
        return PreCommitFinalization::NoOp(PreCommitNoOpReason::Disabled);
    }

    if !runtime.cli_available {
        return PreCommitFinalization::NoOp(PreCommitNoOpReason::CliUnavailable);
    }

    if runtime.is_bare_repo {
        return PreCommitFinalization::NoOp(PreCommitNoOpReason::BareRepository);
    }

    let PendingCheckpoint {
        files: pending_files,
        harness_type,
        git_branch,
        model_id,
        prompts: pending_prompts,
    } = pending;

    let files = pending_files
        .into_iter()
        .filter_map(|file| {
            if file.staged_ranges.is_empty() {
                return None;
            }

            Some(FinalizedFileCheckpoint {
                path: file.path,
                has_sce_attribution: file.has_sce_attribution,
                ranges: file.staged_ranges,
            })
        })
        .collect();

    let prompts = pending_prompts
        .into_iter()
        .map(|prompt| FinalizedPromptCheckpoint {
            turn_number: prompt.turn_number,
            prompt_text: prompt.prompt_text,
            prompt_length: prompt.prompt_length,
            is_truncated: prompt.is_truncated,
            cwd: prompt.cwd,
            transcript_path: prompt.transcript_path,
            captured_at: prompt.captured_at,
        })
        .collect();

    PreCommitFinalization::Finalized(FinalizedCheckpoint {
        anchors,
        harness_type,
        git_branch,
        model_id,
        files,
        prompts,
    })
}