mahbot 0.1.1

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
//! Ticket/board system — Turso-backed task management.

use crate::global_store;
use crate::turso::{self, Connection, TxGuard, Value, params_from_iter};
use anyhow::{Context, Result};
use chrono::{Duration, Utc};
use serde::Serialize;
use std::collections::HashMap;
use std::fmt::Write;
use std::path::Path;
use std::time::Duration as StdDuration;
use tracing::{debug, info, warn};

global_store! {
    /// Global board store.
    pub static BOARD: BoardStore,
    constructor = BoardStore::open,
}

/// Background task: auto-archive cancelled tickets older than 1 hour.
///
/// Runs every 5 minutes, respects the global shutdown token via
/// [`crate::shutdown::sleep_or_shutdown`] (same pattern as
/// [`crate::maintainer::run_maintainer_loop`]).
/// Logs per-ticket failures and continues — a ticket that was un-cancelled
/// between the SELECT and UPDATE is harmlessly skipped.
pub async fn run_archive_cancelled_loop() {
    let interval = StdDuration::from_mins(5);

    loop {
        if !crate::shutdown::sleep_or_shutdown(interval).await {
            break;
        }

        let Some(board) = BOARD.get() else {
            warn!("Archive cancelled loop: board not initialized");
            continue;
        };

        match board.archive_stale_cancelled(1).await {
            Ok(n) if n > 0 => info!(count = n, "Archived stale cancelled tickets"),
            Ok(_) => debug!("Archive cancelled loop: no stale tickets"),
            Err(e) => warn!(error = %e, "Archive cancelled loop failed"),
        }
    }
}

const SCHEMA: &str = "\
CREATE TABLE IF NOT EXISTS tickets (
    id              TEXT PRIMARY KEY,
    title           TEXT NOT NULL,
    description     TEXT NOT NULL,
    status          TEXT NOT NULL DEFAULT 'backlog',
    assigned_to     TEXT,
    workspace_name  TEXT NOT NULL,
    created_at      TEXT NOT NULL,
    updated_at      TEXT NOT NULL,
    prerequisites   TEXT NOT NULL DEFAULT '[]',
    supersedes      TEXT,
    superseded_by   TEXT,
    commit_hash     TEXT,
    lines_added     INTEGER,
    lines_removed   INTEGER,
    reporter        TEXT NOT NULL DEFAULT '',
    is_archived     INTEGER NOT NULL DEFAULT 0,
    embedding       BLOB,
    pipeline_reservation INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS ticket_comments (
    id          TEXT PRIMARY KEY,
    ticket_id   TEXT NOT NULL,
    role        TEXT NOT NULL,
    content     TEXT NOT NULL,
    created_at  TEXT NOT NULL,
    FOREIGN KEY (ticket_id) REFERENCES tickets(id)
);
CREATE INDEX IF NOT EXISTS idx_ticket_comments_ticket_id ON ticket_comments(ticket_id);
CREATE TABLE IF NOT EXISTS ticket_counters (
    workspace_name TEXT PRIMARY KEY,
    next_id        INTEGER NOT NULL DEFAULT 1
);";

const TICKETS_FTS_INDEX_NAME: &str = "idx_tickets_title_fts";
const TICKETS_FTS_INDEX_DDL: &str = "\
CREATE INDEX IF NOT EXISTS idx_tickets_title_fts ON tickets \
USING fts (title) WITH (tokenizer = 'ngram')";

/// Column list for ticket SELECT/RETURNING queries.
///
/// The column order here must match the positional indices defined in
/// [`COL_TICKET_ID`] through [`COL_TICKET_IS_ARCHIVED`], which are used in
/// [`BoardStore::ticket_from_row`].
const TICKET_COLUMNS: &str = "id, title, description, status, assigned_to, \
     workspace_name, created_at, updated_at, prerequisites, supersedes, \
     superseded_by, commit_hash, lines_added, lines_removed, reporter, is_archived, \
     pipeline_reservation";

/// Column-index constants for [`TICKET_COLUMNS`].
///
/// These replace hardcoded positional indices in [`BoardStore::ticket_from_row`].
/// With named constants, the compiler catches references to undefined
/// column constants — for instance, removing a constant but forgetting to
/// update a `row.get()` call produces a compile error rather than a silent
/// field mapping bug.
const COL_TICKET_ID: usize = 0;
const COL_TICKET_TITLE: usize = 1;
const COL_TICKET_DESCRIPTION: usize = 2;
const COL_TICKET_STATUS: usize = 3;
const COL_TICKET_ASSIGNED_TO: usize = 4;
const COL_TICKET_WORKSPACE_NAME: usize = 5;
const COL_TICKET_CREATED_AT: usize = 6;
const COL_TICKET_UPDATED_AT: usize = 7;
const COL_TICKET_PREREQUISITES: usize = 8;
const COL_TICKET_SUPERSEDES: usize = 9;
const COL_TICKET_SUPERSEDED_BY: usize = 10;
const COL_TICKET_COMMIT_HASH: usize = 11;
const COL_TICKET_LINES_ADDED: usize = 12;
const COL_TICKET_LINES_REMOVED: usize = 13;
const COL_TICKET_REPORTER: usize = 14;
const COL_TICKET_IS_ARCHIVED: usize = 15;
const COL_TICKET_PIPELINE_RESERVATION: usize = 16;

/// Column list for comment SELECT queries.
///
/// The column order here must match the positional indices defined in
/// [`COL_COMMENT_ROLE`] through [`COL_COMMENT_CREATED_AT`], which are used in
/// [`BoardStore::get_comments`] (see renumbered constants below).
const COMMENT_COLUMNS: &str = "role, content, created_at";

/// Column-index constants for [`COMMENT_COLUMNS`].
///
/// Note: `id` and `ticket_id` were removed from the SELECT (these fields
/// are dead — never consumed by any production code). The remaining columns
/// are renumbered from 0.
///
/// These replace hardcoded positional indices in [`BoardStore::get_comments`].
/// With named constants, the compiler catches references to undefined
/// column constants — for instance, removing a constant but forgetting to
/// update a `row.get()` call produces a compile error rather than a silent
/// field mapping bug.
const COL_COMMENT_ROLE: usize = 0;
const COL_COMMENT_CONTENT: usize = 1;
const COL_COMMENT_CREATED_AT: usize = 2;

/// Statuses where a ticket occupies the dev/review/QA pipeline.
///
/// Only one ticket at a time per workspace may be in this pipeline. Any ticket in one of these
/// statuses blocks new Engineer dispatches and suppresses maintainer investigation
/// for that workspace.
///
/// Note: [`BoardStore::reset_inflight_tickets`] (via [`BoardStore::RESET_TRANSITIONS`]) only resets a subset
/// of these (InDevelopment, InDiagnostics, InReview, InQa) plus Analysis — see its docs for
/// rationale. The remaining three phases ([`TRANSITORY_HANDOFF_PHASES`]) are transitory handoff
/// states intentionally excluded from reset. The `tests::test_pipeline_blockers_coverage` test
/// enforces that every non-transitory pipeline blocker has a corresponding reset transition.
const PIPELINE_BLOCKING_STATUSES: &[TicketPhase] = &[
    TicketPhase::InDevelopment,
    TicketPhase::InDiagnostics,
    TicketPhase::DiagnosticsDone,
    TicketPhase::InReview,
    TicketPhase::Reviewed,
    TicketPhase::InQa,
    TicketPhase::QaPassed,
];

/// Pipeline-blocking phases that are transitory handoff states — no agent is
/// mid-execution in these phases, so they don't need a reset transition. The
/// poller picks them up within seconds.
///
/// This is a subset of [`PIPELINE_BLOCKING_STATUSES`]. The relationship is
/// mechanically verified by `tests::test_pipeline_blockers_coverage`.
const TRANSITORY_HANDOFF_PHASES: &[TicketPhase] = &[
    TicketPhase::DiagnosticsDone,
    TicketPhase::Reviewed,
    TicketPhase::QaPassed,
];

/// Statuses that unblock dependent tickets.
///
/// When a ticket transitions to one of these statuses, any tickets that
/// depend on it become eligible for claiming (their prerequisite filter
/// no longer blocks them).
///
/// [`TicketPhase::Failed`] is intentionally excluded — a failed ticket
/// permanently blocks its dependents, requiring manual intervention.
///
/// [`TicketPhase::is_unblocking`] delegates to this constant to ensure
/// the unblocking set is always authoritative. If a new status is added
/// here, `is_unblocking()` automatically picks it up; if the set ever
/// needs to diverge from the unblocking set, this delegation must be
/// broken explicitly.
pub const UNBLOCKING_STATUSES: &[TicketPhase] = &[TicketPhase::Done, TicketPhase::Cancelled];

/// Produces an SQL fragment listing statuses as quoted, comma-separated
/// strings — e.g. `'done', 'cancelled'`.
///
/// # Precondition
///
/// The input slice must be non-empty. Passing an empty slice produces
/// `WHERE status IN ()` which is invalid SQL.
fn status_list_sql_fragment(statuses: &[TicketPhase]) -> String {
    statuses
        .iter()
        .map(|p| format!("'{p}'"))
        .collect::<Vec<_>>()
        .join(", ")
}

fn parse_prereqs(raw: &str) -> Result<Vec<String>> {
    serde_json::from_str(raw).with_context(|| {
        if raw.len() > 200 {
            format!(
                "Corrupt prerequisites JSON in database: {}",
                &raw[..raw.floor_char_boundary(200)]
            )
        } else {
            format!("Corrupt prerequisites JSON in database: {raw}")
        }
    })
}

/// The default phase assigned to newly created tickets.
pub const DEFAULT_TICKET_PHASE: TicketPhase = TicketPhase::Backlog;

#[derive(Debug, Clone, Serialize)]
pub struct TicketComment {
    pub role: String,
    pub content: String,
    pub created_at: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct Ticket {
    pub id: String,
    pub title: String,
    pub description: String,
    pub status: TicketPhase,
    pub assigned_to: Option<String>,
    pub workspace_name: String,
    pub created_at: String,
    pub updated_at: String,
    pub comments: Vec<TicketComment>,
    /// IDs of tickets that must be completed before this one can be claimed.
    pub prerequisites: Vec<String>,
    /// ID of the ticket this one supersedes (set when created via supersede).
    /// The superseded ticket is cancelled atomically during creation.
    pub supersedes: Option<String>,
    /// ID of the ticket that supersedes this one (set on the old ticket when
    /// it is superseded). Purely informational — never drives logic.
    pub superseded_by: Option<String>,
    /// Full commit SHA (40 hex chars), `None` if no commit recorded.
    pub commit_hash: Option<String>,
    /// Lines added (non-negative) from the associated commit.
    pub lines_added: Option<i64>,
    /// Lines removed (non-negative) from the associated commit.
    pub lines_removed: Option<i64>,
    /// Who created this ticket — set by the tool at construction time.
    /// Either "manager" or "maintainer"; "" for pre-migration tickets.
    pub reporter: String,
    /// Whether this ticket has been archived (hidden from normal listings).
    pub is_archived: bool,
    /// Whether this ticket holds a pipeline reservation (bounced back from
    /// review/QA/diagnostics and awaiting rework). When set, the ticket gets
    /// priority over other ReadyForDevelopment tickets during claim.
    pub pipeline_reservation: bool,
}

impl Ticket {
    /// Short single-line display for listing tickets in agent-facing output.
    ///
    /// Returns `"  [{reporter}] [{status}] {id}: {title}"` (note the leading
    /// two-space indent for alignment within a multi-line block). The trailing
    /// newline is omitted — callers add it via `writeln!` or equivalent.
    ///
    /// The `{status}` field uses the snake_case Display representation from
    /// [`TicketPhase`] (e.g. `"in_development"`), which is the canonical form
    /// for agent-facing output. For user-facing labels with spaces instead of
    /// underscores, use [`TicketPhase::display_name()`] directly.
    ///
    /// ## Related formatting (not duplicated here)
    ///
    /// - `crate::prompt::format_ticket_block` produces a Markdown
    ///   `<current-ticket>` block for system messages — intentionally different
    ///   format and should not be unified.
    /// - `search_archived_tickets` format omits the reporter field
    ///   (`"  [{status}] {id}: {title}"`) — intentionally different.
    #[must_use]
    pub fn short_display(&self) -> String {
        format!(
            "  [{}] [{}] {}: {}",
            self.reporter, self.status, self.id, self.title
        )
    }

    /// Produce a detailed multi-line display of the ticket, suitable for
    /// [`GetTicketTool`](crate::tools::ticket::GetTicketTool) and other agent-facing output.
    ///
    /// The output includes these fields (when present):
    ///
    /// - Ticket ID, Title, Description
    /// - Status (snake_case — e.g. `ready_for_development`)
    /// - Reporter, Workspace, Created, Updated
    /// - Supersedes, Superseded by, Prerequisites (conditionally when non-empty)
    /// - Archived flag (conditionally when `true`)
    /// - Comments block (via [`Self::format_comments`])
    ///
    /// ## Fields *not* displayed
    ///
    /// The following [`Ticket`] fields are deliberately omitted — they are
    /// available in the board UI but not meaningful for agent context:
    ///
    /// - `assigned_to`
    /// - `commit_hash`
    /// - `lines_added` / `lines_removed`
    ///
    /// ## Output size
    ///
    /// The returned string can be arbitrarily large (unbounded descriptions
    /// and comments). Callers that need truncation should apply their own
    /// limits (see `GetTicketTool::format_output` for an example that
    /// disables the default 5K truncation).
    ///
    /// ## Changing this output
    ///
    /// Because agent tool calls depend on this exact format, changes to
    /// displayed fields or layout must be kept in sync with
    /// [`crate::tools::ticket::GetTicketTool`] (the primary consumer). If new fields are
    /// added here, update the integration test `test_get_ticket_tool`
    /// to prevent silent divergence.
    #[must_use]
    pub fn detailed_display(&self) -> String {
        let mut out = format!(
            "Ticket: {id}\n\
             Title: {title}\n\
             Description: {description}\n\
             Status: {status}\n\
             Reporter: {reporter}\n\
             Workspace: {workspace}\n\
             Created: {created}\n\
             Updated: {updated}\n",
            id = self.id,
            title = self.title,
            description = self.description,
            status = self.status,
            reporter = self.reporter,
            workspace = self.workspace_name,
            created = self.created_at,
            updated = self.updated_at,
        );
        if let Some(ref s) = self.supersedes {
            let _ = writeln!(out, "Supersedes: {s}");
        }
        if let Some(ref s) = self.superseded_by {
            let _ = writeln!(out, "Superseded by: {s}");
        }
        if !self.prerequisites.is_empty() {
            let _ = writeln!(out, "Prerequisites: {}", self.prerequisites.join(", "));
        }
        if self.is_archived {
            out.push_str("Archived: yes\n");
        }
        out.push_str(&self.format_comments());
        out
    }

    /// Format comments as a `"Comments:"` block suitable for [`crate::tools::ticket::GetTicketTool`].
    ///
    /// Returns a string starting with `"Comments:"` followed by one line per
    /// comment in the format `"\n  [{role}] ({timestamp}): {content}"`, or
    /// `"Comments:\n  (no comments)"` if the comment list is empty.
    ///
    /// Timestamps are truncated to seconds (`[..19]` of the RFC 3339 string),
    /// with a defensive `min()` guard against abnormally short strings.
    #[must_use]
    pub fn format_comments(&self) -> String {
        let mut s = String::from("Comments:");
        if self.comments.is_empty() {
            s.push_str("\n  (no comments)");
        } else {
            for c in &self.comments {
                let end = 19.min(c.created_at.len());
                let ts = &c.created_at[..end];
                let _ = write!(s, "\n  [{}] ({}): {}", c.role, ts, c.content);
            }
        }
        s
    }
}
/// Lowercase snake_case strings matching the DB column values — no schema
/// migration needed. Mapping derived via `strum` (`Display`, `EnumString`,
/// `AsRefStr`, `EnumIter`).
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Serialize,
    strum::Display,
    strum::EnumString,
    strum::AsRefStr,
    strum::EnumIter,
)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum TicketPhase {
    Backlog,
    Analysis,
    Planning,
    ReadyForDevelopment,
    InDevelopment,
    InDiagnostics,
    DiagnosticsDone,
    InReview,
    Reviewed,
    InQa,
    QaPassed,
    Done,
    Cancelled,
    Failed,
    Paused,
}

impl TicketPhase {
    /// Returns `true` for transitory handoff phases — pipeline-blocking
    /// statuses where no agent is mid-execution.
    ///
    /// Delegates to `TRANSITORY_HANDOFF_PHASES` so the transitory handoff set can never
    /// accidentally diverge from the definition used in coverage tests.
    #[must_use]
    pub fn is_transitory_handoff(&self) -> bool {
        TRANSITORY_HANDOFF_PHASES.contains(self)
    }

    /// Returns `true` for phases that unblock dependent tickets.
    ///
    /// Delegates to [`UNBLOCKING_STATUSES`] so the unblocking set can never
    /// accidentally diverge from the prerequisite-unblocking set.
    /// [`TicketPhase::Failed`] is not in [`UNBLOCKING_STATUSES`] and is
    /// therefore not unblocking — a failed ticket permanently blocks its
    /// dependents and remains visible in active views for manual triage.
    #[must_use]
    pub fn is_unblocking(&self) -> bool {
        UNBLOCKING_STATUSES.contains(self)
    }

    /// Returns `true` if the ticket is in a pipeline-blocking phase.
    ///
    /// Tickets in these phases are actively being worked on by agents
    /// (development, diagnostics, review, QA). Automated tools (create,
    /// update, add_comment) should refuse to modify them to prevent
    /// race conditions with running agents.
    #[must_use]
    pub fn is_pipeline_blocking(&self) -> bool {
        PIPELINE_BLOCKING_STATUSES.contains(self)
    }

    /// Human-readable display label with spaces instead of underscores
    /// (e.g. `"in development"` from [`TicketPhase::InDevelopment`]).
    ///
    /// This is the presentation-oriented counterpart to `AsRefStr::as_ref`
    /// (which returns the machine-oriented `snake_case` form like
    /// `"in_development"`). Use `display_name()` for user-facing UI labels;
    /// keep `as_ref()` for tool output, SQL fragments, and agent-facing text
    /// where agents expect the snake_case status string.
    #[must_use]
    pub fn display_name(&self) -> String {
        self.as_ref().replace('_', " ")
    }
}

// No manual `as_str()`, `Display`, or `FromStr` — provided by strum derives.

impl BoardStore {
    /// Open (or create) the board database at `root/db/board.db`.
    pub async fn open(root: &Path) -> Result<Self> {
        let db_path = root.join("db/board.db");
        let conn = turso::open_with_schema(&db_path, SCHEMA).await?;

        crate::turso::ensure_fts_index(
            &conn,
            TICKETS_FTS_INDEX_NAME,
            "ngram",
            TICKETS_FTS_INDEX_DDL,
        )
        .await?;

        // Migration: add pipeline_reservation column.
        // Check current version first to avoid re-running migrations.
        let version: i64 = conn
            .query_row("PRAGMA user_version", turso::params![], |row| row.get(0))
            .await
            .unwrap_or(0);
        if version < 2 {
            // Use a silent ALTER TABLE ADD COLUMN since `IF NOT EXISTS` requires
            // SQLite 3.35+. If the ALTER fails (column already exists from a
            // concurrent fresh CREATE TABLE IF NOT EXISTS), the error is harmless.
            let _ = conn
                .execute(
                    "ALTER TABLE tickets ADD COLUMN pipeline_reservation \
                     INTEGER NOT NULL DEFAULT 0",
                    turso::params![],
                )
                .await;
            conn.execute("PRAGMA user_version = 2", turso::params![])
                .await
                .context("Failed to set PRAGMA user_version = 2")?;
        }

        Ok(Self { conn })
    }

    /// Shared INSERT logic for [`BoardStore::create_ticket`] and [`BoardStore::supersede_and_create`].
    ///
    /// Computes the timestamp and serializes prerequisites internally. Does NOT
    /// commit the transaction — the caller is responsible for calling
    /// `tx.commit()` after any additional writes.
    #[allow(clippy::too_many_arguments)]
    async fn insert_ticket_in_tx(
        tx: &TxGuard<'_>,
        id: &str,
        title: &str,
        description: &str,
        workspace_name: &str,
        phase: TicketPhase,
        prerequisites: &[String],
        supersedes: Option<&str>,
        reporter: &str,
        embedding: Option<&[u8]>,
    ) -> Result<()> {
        let now = turso::now();
        let prereqs_json = serde_json::to_string(prerequisites)?;
        tx.execute(
            "INSERT INTO tickets (id, title, description, status, workspace_name, \
             created_at, updated_at, prerequisites, supersedes, reporter, embedding) \
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)",
            turso::params![
                id,
                title,
                description,
                phase.as_ref(),
                workspace_name,
                now.as_str(),
                now.as_str(),
                prereqs_json.as_str(),
                supersedes,
                reporter,
                embedding,
            ],
        )
        .await?;
        Ok(())
    }

    /// Rewire dependents after supersede: tickets whose prerequisites mention
    /// `old_id` get updated to point to `new_id`. Queried and updated within
    /// the same transaction — no TOCTOU window between SELECT and UPDATE.
    ///
    /// Uses `json_each()` for exact prerequisite matching (consistent with
    /// [`claim_ticket_in_workspace`](Self::claim_ticket_in_workspace)).
    async fn rewire_dependents(
        tx: &TxGuard<'_>,
        old_id: &str,
        new_id: &str,
        workspace_name: &str,
    ) -> Result<()> {
        let dep_rows = tx
            .query(
                "SELECT DISTINCT t.id, t.prerequisites \
                 FROM tickets t, json_each(t.prerequisites) AS je \
                 WHERE je.value = ?1 AND t.workspace_name = ?2",
                turso::params![old_id, workspace_name],
            )
            .await?;

        for row in &dep_rows {
            let dep_id: String = row.get(0)?;
            let raw: String = row.get(1)?;
            let mut prereqs: Vec<String> = parse_prereqs(&raw)
                .with_context(|| format!("Failed to parse prerequisites for ticket {dep_id}"))?;
            let mut changed = false;
            for p in &mut prereqs {
                if *p == old_id {
                    *p = new_id.to_string();
                    changed = true;
                }
            }
            if changed {
                let new_json = serde_json::to_string(&prereqs)?;
                tx.execute(
                    "UPDATE tickets SET prerequisites = ?1 WHERE id = ?2",
                    turso::params![new_json, dep_id],
                )
                .await?;
            }
        }
        Ok(())
    }

    /// Begin a transaction, generate a ticket ID, and validate prerequisites.
    ///
    /// Performs the shared validation preamble used by both [`BoardStore::create_ticket`]
    /// and [`BoardStore::supersede_and_create`]: starts a transaction, generates a
    /// sequential ticket ID via counter upsert, checks that the new ID
    /// doesn't appear in its own prerequisites, then validates all
    /// prerequisites exist and belong to the same workspace.
    ///
    /// Callers must not call `self.conn` methods until the guard is dropped
    /// or committed — `TxGuard` holds a tokio mutex lock.
    ///
    /// # Safety (TOCTOU)
    ///
    /// Safety relies on both the tokio mutex inside `conn` (serializes
    /// Rust-level writes) and the SQLite transaction `tx` (provides
    /// database-level isolation) — no concurrent write can change
    /// prerequisite tickets between validation and the caller's INSERT.
    /// Validation runs inside the transaction via `tx.query()` (which
    /// uses the upstream connection through the MutexGuard, avoiding
    /// mutex deadlock with `self.conn.query()`).
    async fn begin_tx_and_validate_prerequisites(
        &self,
        workspace_name: &str,
        prerequisites: &[String],
    ) -> Result<(TxGuard<'_>, String)> {
        let tx = self.conn.begin_tx().await?;
        // Generate sequential ticket ID: {workspace_name}-{seq}
        let seq: i64 = tx
            .query_row(
                "INSERT INTO ticket_counters (workspace_name, next_id) VALUES (?1, 1) \
                 ON CONFLICT(workspace_name) DO UPDATE SET next_id = ticket_counters.next_id + 1 \
                 RETURNING next_id - 1",
                turso::params![workspace_name],
                |row| row.get(0),
            )
            .await?;
        let id = format!("{workspace_name}-{seq}");
        anyhow::ensure!(
            !prerequisites.contains(&id),
            "Ticket cannot depend on itself: {id}"
        );
        // Validate prerequisites using the transaction's query method —
        // tx.query() uses the upstream connection through the MutexGuard
        // so it doesn't deadlock with the mutex held by TxGuard.
        self.validate_prerequisites(&tx, prerequisites, workspace_name)
            .await?;
        Ok((tx, id))
    }

    /// Create a new ticket at the requested phase. Returns the ticket id.
    #[allow(clippy::too_many_arguments)]
    pub async fn create_ticket(
        &self,
        title: &str,
        description: &str,
        ws: &crate::Workspace,
        phase: TicketPhase,
        prerequisites: &[String],
        reporter: &str,
        embedding: Option<&[u8]>,
    ) -> Result<String> {
        let (tx, id) = self
            .begin_tx_and_validate_prerequisites(&ws.name, prerequisites)
            .await?;

        Self::insert_ticket_in_tx(
            &tx,
            &id,
            title,
            description,
            &ws.name,
            phase,
            prerequisites,
            None,
            reporter,
            embedding,
        )
        .await?;

        tx.commit().await?;
        Ok(id)
    }

    /// Create a new ticket that supersedes (replaces) an existing ticket.
    ///
    /// Atomically cancels `supersede_id`, creates the new ticket with a
    /// `supersedes` back-link, and rewires any dependent tickets' prerequisites
    /// to point to the new ID. All writes happen in a single transaction
    /// via `begin_tx()` + parameterized queries.
    ///
    /// After commit, any running agent on the superseded ticket is cancelled.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The superseded ticket does not exist
    /// - The superseded ticket is in a different workspace
    /// - A self-reference is detected (supersede ID in the new ticket's prerequisites)
    /// - Any prerequisite is invalid (doesn't exist or cross-workspace)
    #[allow(clippy::too_many_arguments)]
    pub async fn supersede_and_create(
        &self,
        supersede_id: &str,
        title: &str,
        description: &str,
        ws: &crate::Workspace,
        prerequisites: &[String],
        reporter: &str,
        embedding: Option<&[u8]>,
    ) -> Result<String> {
        // Self-reference: a ticket cannot supersede and depend on the same ticket.
        anyhow::ensure!(
            !prerequisites.iter().any(|p| p == supersede_id),
            "Ticket cannot supersede and depend on the same ticket: {supersede_id}"
        );

        // Begin transaction and validate prerequisites — counter upsert, old
        // cancel, new create, and dependent rewiring all happen atomically.
        let (tx, new_id) = self
            .begin_tx_and_validate_prerequisites(&ws.name, prerequisites)
            .await?;

        // Verify the superseded ticket exists and belongs to the same workspace.
        // This runs INSIDE the transaction (tx.query() uses the upstream
        // connection through the MutexGuard) to eliminate the TOCTOU race
        // between validation and cancellation.
        let rows = tx
            .query(
                "SELECT workspace_name, status FROM tickets WHERE id = ?1",
                turso::params![supersede_id],
            )
            .await?;
        let row = rows
            .into_iter()
            .next()
            .ok_or_else(|| anyhow::anyhow!("Superseded ticket not found: {supersede_id}"))?;
        let old_ws: String = row.get(0)?;
        anyhow::ensure!(
            old_ws == ws.name,
            "Superseded ticket {supersede_id} belongs to workspace '{old_ws}', \
             not the current workspace '{}'. \
             Cross-workspace supersede is not allowed.",
            ws.name,
        );
        let status_str: String = row.get(1)?;
        let old_status: TicketPhase = status_str.parse()?;

        // Cancel the old ticket and set the forward supersede link.
        let now = turso::now();
        let cancelled_rows = tx
            .execute(
                "UPDATE tickets SET status = ?1, updated_at = ?2, assigned_to = NULL, \
                 superseded_by = ?4, is_archived = 1 WHERE id = ?3",
                turso::params![
                    TicketPhase::Cancelled.as_ref(),
                    now,
                    supersede_id,
                    new_id.as_str(),
                ],
            )
            .await?;
        Self::ensure_ticket_found(cancelled_rows, supersede_id, "cancel superseded ticket")?;

        // Insert the new ticket.
        Self::insert_ticket_in_tx(
            &tx,
            &new_id,
            title,
            description,
            &ws.name,
            TicketPhase::Backlog,
            prerequisites,
            Some(supersede_id),
            reporter,
            embedding,
        )
        .await?;

        // Rewire dependents that reference the old ID.
        Self::rewire_dependents(&tx, supersede_id, &new_id, &ws.name).await?;

        tx.commit().await?;

        // Buffer the supersede transition after successful commit.
        crate::ticket_buffer::push(
            &ws.name,
            supersede_id,
            old_status.as_ref(),
            TicketPhase::Cancelled.as_ref(),
        );

        // Cancel any running agent on the superseded ticket (after commit).
        crate::registry::AGENT_REGISTRY.cancel_by_ticket_id(supersede_id);

        Ok(new_id)
    }

    /// Build a [`Ticket`] from a row returned by a
    /// [`TICKET_COLUMNS`] SELECT, optionally including its comments.
    async fn ticket_from_row(&self, row: &turso::Row, load_comments: bool) -> Result<Ticket> {
        let id: String = row.get(COL_TICKET_ID)?;
        let comments = if load_comments {
            self.get_comments(&id).await?
        } else {
            Vec::new()
        };
        let prerequisites_raw: String = row.get(COL_TICKET_PREREQUISITES)?;
        let prerequisites = parse_prereqs(&prerequisites_raw)
            .with_context(|| format!("Failed to parse prerequisites for ticket {id}"))?;
        Ok(Ticket {
            id,
            title: row.get(COL_TICKET_TITLE)?,
            description: row.get(COL_TICKET_DESCRIPTION)?,
            status: row
                .get::<String>(COL_TICKET_STATUS)?
                .parse::<TicketPhase>()?,
            assigned_to: row.get(COL_TICKET_ASSIGNED_TO)?,
            workspace_name: row.get(COL_TICKET_WORKSPACE_NAME)?,
            created_at: row.get(COL_TICKET_CREATED_AT)?,
            updated_at: row.get(COL_TICKET_UPDATED_AT)?,
            comments,
            prerequisites,
            supersedes: row.get(COL_TICKET_SUPERSEDES)?,
            superseded_by: row.get(COL_TICKET_SUPERSEDED_BY)?,
            commit_hash: row.get(COL_TICKET_COMMIT_HASH)?,
            lines_added: row.get(COL_TICKET_LINES_ADDED)?,
            lines_removed: row.get(COL_TICKET_LINES_REMOVED)?,
            reporter: row.get::<String>(COL_TICKET_REPORTER)?,
            is_archived: row.get::<i64>(COL_TICKET_IS_ARCHIVED)? != 0,
            pipeline_reservation: row.get::<i64>(COL_TICKET_PIPELINE_RESERVATION)? != 0,
        })
    }

    /// Claim a ticket scoped to a single workspace and transition it to
    /// `target_phase`. Always filters by `workspace_name` so only tickets from
    /// that workspace are eligible.
    ///
    /// Only tickets currently in `expected_phase` are eligible for claiming.
    /// The WHERE clause includes `t1.status = ?` bound to `expected_phase`,
    /// providing CAS-style atomicity for phase transitions — if no ticket
    /// matches the expected phase, the claim returns `None`.
    ///
    /// When `require_clear_pipeline` is `true`, the claim is rejected (returns `None`)
    /// if any pipeline-blocking ticket exists in the same workspace. The
    /// occupancy check is part of the same atomic SQL UPDATE statement (no
    /// separate SELECT + UPDATE window). Pipeline-blocking statuses are defined
    /// in [`PIPELINE_BLOCKING_STATUSES`].
    ///
    /// Note that a reserved ReadyForDevelopment ticket (one with
    /// `pipeline_reservation = 1`) is **not** treated as a pipeline blocker for
    /// the purpose of this claim. This
    /// asymmetry with [`has_pipeline_blocker_for_workspace`](Self::has_pipeline_blocker_for_workspace)
    /// (which does treat reserved ReadyForDevelopment as a blocker for the
    /// maintainer) is intentional: the claim subquery orders by
    /// `pipeline_reservation DESC` and clears reservation on claim, so a
    /// reserved ticket at ReadyForDevelopment will be claimed before any
    /// other ticket at the same phase — no pipeline blocking needed.
    ///
    /// When `require_clear_pipeline` is `false`, the claim uses a simple LIMIT 1
    /// subquery with no pipeline gating. This is used for phases that should
    /// not be blocked by in-flight pipeline tickets (e.g., analysis, review, and QA).
    ///
    /// The subquery orders by `pipeline_reservation DESC, created_at ASC` so that
    /// tickets bounced back from review/QA/diagnostics (reservation = 1) are claimed
    /// before fresh tickets at the same phase. Among tickets with equal reservation,
    /// the oldest ticket (earliest created_at) is claimed first.
    ///
    /// Note: the UPDATE sets `assigned_to = NULL` and `pipeline_reservation = 0` —
    /// this intentionally drops the previous claimant and clears any pipeline
    /// reservation so the cleared slot is available for other tickets.
    /// Callers that require agent-level assignment
    /// (single-owner dispatches like the Engineer) should call
    /// [`set_assigned_to`](Self::set_assigned_to) after claiming. Parallel-agent
    /// dispatches (analysts, verifiers) intentionally leave `assigned_to` NULL.
    pub(crate) async fn claim_ticket_in_workspace(
        &self,
        expected_phase: TicketPhase,
        target_phase: TicketPhase,
        workspace_name: &str,
        require_clear_pipeline: bool,
    ) -> Result<Option<Ticket>> {
        let now = turso::now();

        // Filter that excludes tickets with unmet prerequisites.
        let prereq_filter = format!(
            "AND NOT EXISTS ( \
               SELECT 1 FROM json_each(t1.prerequisites) AS je \
               JOIN tickets t_pre ON t_pre.id = je.value \
               WHERE t_pre.status NOT IN ({}) \
             )",
            status_list_sql_fragment(UNBLOCKING_STATUSES),
        );

        let pipeline_blocker_clause = if require_clear_pipeline {
            let blocker_sql = status_list_sql_fragment(PIPELINE_BLOCKING_STATUSES);
            format!(
                "AND NOT EXISTS (SELECT 1 FROM tickets t2 \
                 WHERE t2.workspace_name = t1.workspace_name \
                 AND t2.status IN ({blocker_sql}) \
                 AND t2.id != t1.id) "
            )
        } else {
            String::new()
        };

        let sql = format!(
            "UPDATE tickets SET status = ?1, assigned_to = NULL, updated_at = ?2, \
             pipeline_reservation = 0 \
             WHERE id = (SELECT t1.id FROM tickets t1 \
             WHERE t1.status = ?3 AND t1.assigned_to IS NULL AND t1.workspace_name = ?4 \
             {pipeline_blocker_clause}{prereq_filter} \
             ORDER BY t1.pipeline_reservation DESC, t1.created_at ASC LIMIT 1) \
             RETURNING {TICKET_COLUMNS}"
        );

        let rows = self
            .conn
            .query(
                &sql,
                turso::params![
                    target_phase.as_ref(),
                    now,
                    expected_phase.as_ref(),
                    workspace_name,
                ],
            )
            .await?;
        // Caller contract: query produces at most one row (subsequent rows silently ignored)
        match rows.into_iter().next() {
            Some(row) => Ok(Some(self.ticket_from_row(&row, true).await?)),
            None => Ok(None),
        }
    }

    /// Get a ticket by id.
    pub async fn get_ticket(&self, id: &str) -> Result<Option<Ticket>> {
        let sql = format!("SELECT {TICKET_COLUMNS} FROM tickets WHERE id = ?1");
        let rows = self.conn.query(&sql, turso::params![id]).await?;
        // Caller contract: query produces at most one row (subsequent rows silently ignored)
        match rows.into_iter().next() {
            Some(row) => Ok(Some(self.ticket_from_row(&row, true).await?)),
            None => Ok(None),
        }
    }

    /// Get a ticket's status phase by id — lightweight, no comments loaded.
    pub async fn get_ticket_status(&self, id: &str) -> Result<Option<TicketPhase>> {
        let sql = "SELECT status FROM tickets WHERE id = ?1";
        let rows = self.conn.query(sql, turso::params![id]).await?;
        match rows.into_iter().next() {
            Some(row) => {
                let status: String = row.get(0)?;
                Ok(Some(status.parse()?))
            }
            None => Ok(None),
        }
    }

    /// List all tickets in a given phase for a workspace, ordered by created_at
    /// descending. Does NOT load comments — lightweight enough for poll loops.
    ///
    /// Delegates to [`BoardStore::list_all_tickets`] with both filters set — this function
    /// is a convenience wrapper with non-optional parameters. Because it shares
    /// the same query path, future changes to `list_all_tickets` (sorting,
    /// filtering, comment loading) will affect this function too.
    pub(crate) async fn list_tickets_in_phase(
        &self,
        phase: TicketPhase,
        workspace_name: &str,
    ) -> Result<Vec<Ticket>> {
        self.list_all_tickets(Some(workspace_name), Some(phase))
            .await
    }

    /// Update the status of a ticket, optionally guarded by an expected phase.
    ///
    /// When `expected_phase` is [`Some`], the UPDATE includes `AND status = ?` in
    /// the WHERE clause — the call only succeeds when the ticket exists and its
    /// current phase matches. This provides CAS-style atomicity for phase
    /// transitions (e.g. Backlog→InDevelopment) and prevents
    /// concurrent-modification races.
    ///
    /// When `expected_phase` is [`None`], the transition is unconditional — any
    /// ticket matching the ID is updated, regardless of its current status.
    /// This is appropriate when the caller is the authority on the new status
    /// (e.g. GUI manual override).
    ///
    /// In both cases the method clears `assigned_to` (sets to `NULL`) and cancels
    /// any running agents on this ticket.
    ///
    /// # Note on [`pipeline_reservation`](Ticket::pipeline_reservation)
    ///
    /// This method does **not** change [`pipeline_reservation`](Ticket::pipeline_reservation).
    /// The reservation is deliberately left untouched so that:
    /// - Bounce-back transitions (diagnostics/review/QA failure → ReadyForDevelopment)
    ///   can set it atomically via [`transition_to_with_reservation`](Self::transition_to_with_reservation).
    /// - Manual transitions (GUI/UpdateTicketTool) to terminal phases
    ///   leave stale reservations inert — the claim and blocker queries filter by
    ///   status, so a cancelled/paused/failed ticket with reservation=1 is harmless.
    ///
    /// # Errors
    ///
    /// Returns an error when the UPDATE matched 0 rows (ticket does not exist, or
    /// exists but is not in `expected_phase` when a guard is active) or when a
    /// database error occurs.
    pub async fn transition_to(
        &self,
        id: &str,
        expected_phase: Option<TicketPhase>,
        target_phase: TicketPhase,
    ) -> Result<()> {
        self.transition_to_inner(id, expected_phase, target_phase, None)
            .await
    }

    /// Transition a ticket to a new phase, optionally setting `pipeline_reservation`.
    ///
    /// When `reservation` is `None` (the common case), does not touch the
    /// `pipeline_reservation` column.  When `Some(value)`, also sets
    /// `pipeline_reservation` to the given boolean (needed for crash/restart
    /// recovery and rework priority).
    async fn transition_to_inner(
        &self,
        id: &str,
        expected_phase: Option<TicketPhase>,
        target_phase: TicketPhase,
        reservation: Option<bool>,
    ) -> Result<()> {
        let now = turso::now();
        let guard: Option<&str> = expected_phase.as_ref().map(TicketPhase::as_ref);
        let action = match reservation {
            Some(v) => format!(
                "set status to {} (reservation={})",
                target_phase.as_ref(),
                v,
            ),
            None => format!("set status to {}", target_phase.as_ref()),
        };
        self.execute_and_cancel(
            "UPDATE tickets SET status = ?1, assigned_to = NULL, updated_at = ?2, \
             pipeline_reservation = COALESCE(?5, pipeline_reservation) \
             WHERE id = ?3 AND (?4 IS NULL OR status = ?4)",
            turso::params![target_phase.as_ref(), now, id, guard, reservation],
            id,
            &action,
        )
        .await
    }

    /// Verify that a mutation query affected at least one row, returning an
    /// error with a descriptive message if the ticket was not found.
    fn ensure_ticket_found(rows: u64, id: &str, action: &str) -> Result<()> {
        anyhow::ensure!(rows > 0, "Ticket {id} not found — cannot {action}");
        Ok(())
    }

    /// Execute a mutation SQL, verify it affected a row, then cancel any agent
    /// registered on the ticket.
    ///
    /// This is a convenience helper for single-ticket mutation methods that
    /// follow the pattern: execute UPDATE → `ensure_ticket_found` → cancel stale
    /// agent. Used by [`transition_to`](Self::transition_to),
    /// [`transition_to_inner`](Self::transition_to_inner),
    /// [`set_assigned_to`](Self::set_assigned_to), and
    /// [`set_archived`](Self::set_archived).
    ///
    /// # When NOT to use
    ///
    /// Do **not** use this helper for methods with different semantics:
    /// - **`set_commit_info`** — pure metadata, does not cancel agents.
    /// - **`claim_diagnostics`** — returns `Result<bool>` (conditional success),
    ///   only cancels on successful claim.
    /// - **`supersede_and_create`** — runs inside a transaction, cancels after
    ///   commit via a different pattern.
    /// - **`batch_set_archived`** — batch operation on many tickets, handles
    ///   zero-affected-rows gracefully (no error).
    async fn execute_and_cancel(
        &self,
        sql: &str,
        params: impl turso::IntoParams + Send + 'static,
        id: &str,
        action: &str,
    ) -> Result<()> {
        let rows = self.conn.execute(sql, params).await?;
        Self::ensure_ticket_found(rows, id, action)?;
        crate::registry::AGENT_REGISTRY.cancel_by_ticket_id(id);
        Ok(())
    }

    /// Set or clear the assignee for a ticket (does not change status).
    ///
    /// When `assigned_to` is `Some(value)`, sets the `assigned_to` column to that
    /// value. When `None`, clears the assignee (sets `assigned_to = NULL`).
    ///
    /// Cancels any running agents registered on this ticket as a safety-in-depth
    /// measure against stale assignments. For set operations, callers typically
    /// set the assignee before spawning an agent, so the cancel is normally a
    /// no-op. For clear operations, this ensures no stale agent remains bound to
    /// a now-unassigned ticket.
    pub async fn set_assigned_to(&self, id: &str, assigned_to: Option<&str>) -> Result<()> {
        let now = turso::now();
        let action = if assigned_to.is_some() {
            "set assigned_to"
        } else {
            "clear assigned_to"
        };
        self.execute_and_cancel(
            "UPDATE tickets SET assigned_to = ?1, updated_at = ?2 WHERE id = ?3",
            turso::params![assigned_to, now, id],
            id,
            action,
        )
        .await
    }

    /// Atomically claim a ticket for diagnostics execution.
    ///
    /// Sets `assigned_to = 'diagnostics'` only when the ticket is unassigned
    /// AND still in [`TicketPhase::InDiagnostics`] — a single atomic SQL guard
    /// that prevents the TOCTOU race between the poll listing pre-filter and the
    /// subsequent claim. The assignee set and phase check are fused into one
    /// UPDATE; callers do not need a separate `set_assigned_to` after claiming.
    ///
    /// Returns `Ok(true)` if a row was updated (claim succeeded), `Ok(false)`
    /// if no row matched (already claimed by another dispatch or ticket moved
    /// out of [`TicketPhase::InDiagnostics`]). On a successful claim, cancels
    /// any agent registered on this ticket as a safety-in-depth measure against
    /// stale dispatches.
    pub async fn claim_diagnostics(&self, id: &str) -> Result<bool> {
        let now = turso::now();
        let rows = self
            .conn
            .execute(
                "UPDATE tickets \
                 SET assigned_to = 'diagnostics', updated_at = ?1 \
                 WHERE id = ?2 \
                 AND assigned_to IS NULL \
                 AND status = ?3",
                turso::params![now, id, TicketPhase::InDiagnostics.as_ref()],
            )
            .await?;

        if rows > 0 {
            crate::registry::AGENT_REGISTRY.cancel_by_ticket_id(id);
        }

        Ok(rows > 0)
    }

    /// Record commit metadata on a ticket.
    ///
    /// This is pure metadata — it does NOT cancel running agents or check
    /// ticket phase (unlike `set_assigned_to`, which cancels running agents). Non-negative line counts are
    /// enforced by debug assertions; the caller is responsible for providing
    /// valid values in production.
    pub async fn set_commit_info(
        &self,
        id: &str,
        hash: &str,
        lines_added: i64,
        lines_removed: i64,
    ) -> Result<()> {
        debug_assert!(
            lines_added >= 0,
            "lines_added must be non-negative: {lines_added}"
        );
        debug_assert!(
            lines_removed >= 0,
            "lines_removed must be non-negative: {lines_removed}"
        );

        let now = turso::now();
        let rows = self
            .conn
            .execute(
                "UPDATE tickets SET commit_hash = ?1, lines_added = ?2, lines_removed = ?3, \
                 updated_at = ?4 WHERE id = ?5",
                turso::params![hash, lines_added, lines_removed, now, id],
            )
            .await?;

        Self::ensure_ticket_found(rows, id, "set commit info")?;

        Ok(())
    }

    /// Transition a ticket to `target_phase` while atomically setting the
    /// pipeline reservation to `reservation`.
    ///
    /// This is identical to [`transition_to`](Self::transition_to) except it
    /// also sets `pipeline_reservation` in the same UPDATE statement. Use this
    /// for bounce-back transitions (diagnostics/review/QA failure →
    /// ReadyForDevelopment) to avoid a race where a non-reserved ticket is
    /// claimed between the transition and the subsequent
    /// separate call to set the reservation.
    pub async fn transition_to_with_reservation(
        &self,
        id: &str,
        expected_phase: Option<TicketPhase>,
        target_phase: TicketPhase,
        reservation: bool,
    ) -> Result<()> {
        self.transition_to_inner(id, expected_phase, target_phase, Some(reservation))
            .await
    }

    /// Transition pairs for crash/restart recovery (extracted so tests can verify
    /// coverage against [`PIPELINE_BLOCKING_STATUSES`] without duplicating the pairs).
    ///
    /// Each entry maps a phase where an agent may have crashed mid-work back to the
    /// phase the ticket should resume in. Must be kept in sync with
    /// [`PIPELINE_BLOCKING_STATUSES`] — see `tests::test_pipeline_blockers_coverage`.
    ///
    /// Asymmetry: `Analysis → Backlog` is included (backlog analysts may crash mid-analysis),
    /// but `Analysis` is intentionally NOT in [`PIPELINE_BLOCKING_STATUSES`] (it's a pre-flight
    /// phase, not a pipeline blocker).
    ///
    /// Transitory handoff phases (see [`TicketPhase::is_transitory_handoff`]) are pipeline
    /// blocking but don't need a reset entry — the poller picks them up within seconds
    /// of restart, so no agent session is mid-execution in those states.
    const RESET_TRANSITIONS: &[(TicketPhase, TicketPhase, bool)] = &[
        (
            TicketPhase::InDevelopment,
            TicketPhase::ReadyForDevelopment,
            true,
        ),
        (
            TicketPhase::InDiagnostics,
            TicketPhase::ReadyForDevelopment,
            true,
        ),
        (TicketPhase::InQa, TicketPhase::Reviewed, false),
        (TicketPhase::InReview, TicketPhase::DiagnosticsDone, false),
        (TicketPhase::Analysis, TicketPhase::Backlog, false),
    ];
    /// Reset all in-flight tickets to their ready state (for crash/restart recovery).
    ///
    /// Resets:
    /// - 4 of the 7 `PIPELINE_BLOCKING_STATUSES` where agents may have been mid-work
    ///   (InDevelopment, InDiagnostics, InReview, InQa) — roll back to their pre-pipeline state
    /// - `Analysis` (not a pipeline blocker, but backlog analysts may crash mid-work)
    ///
    /// Tickets that are bounced to `ReadyForDevelopment` (InDevelopment and InDiagnostics)
    /// get `pipeline_reservation = 1` so they are claimed before any fresh
    /// `ReadyForDevelopment` ticket — this preserves the rework priority across restarts.
    ///
    /// Excludes `TRANSITORY_HANDOFF_PHASES` — these are transitory handoff states
    /// that the poller picks up within 2 seconds of restart.
    ///
    /// Uses `Self::RESET_TRANSITIONS` (extracted as an associated const so tests
    /// can verify coverage against `PIPELINE_BLOCKING_STATUSES`).
    pub async fn reset_inflight_tickets(&self) -> Result<()> {
        let now = turso::now();
        for (from, to, reserve) in Self::RESET_TRANSITIONS {
            self.conn
                .execute(
                    "UPDATE tickets SET status = ?1, assigned_to = NULL, updated_at = ?2, \
                 pipeline_reservation = ?4 WHERE status = ?3",
                    turso::params![to.as_ref(), now.clone(), from.as_ref(), i64::from(*reserve)],
                )
                .await?;
        }
        Ok(())
    }

    /// Returns true if the given workspace has any ticket with a pipeline-blocking
    /// status (dev/review/QA), OR any reserved ReadyForDevelopment ticket that
    /// was bounced back and is awaiting rework. Used by the maintainer to avoid
    /// scanning codebases that are actively being changed or about to be changed by rework.
    ///
    /// Excludes archived tickets — the only statuses that ever get archived are
    /// `Done` and `Cancelled`, neither of which appears in
    /// `PIPELINE_BLOCKING_STATUSES`, so this is a defensive consistency measure.
    pub async fn has_pipeline_blocker_for_workspace(&self, workspace_name: &str) -> Result<bool> {
        let blocker_sql = status_list_sql_fragment(PIPELINE_BLOCKING_STATUSES);
        let sql = format!(
            "SELECT 1 FROM tickets WHERE \
             (status IN ({blocker_sql}) OR \
              (status = '{}' AND pipeline_reservation = 1)) \
             AND workspace_name = ?1 AND is_archived = 0 LIMIT 1",
            TicketPhase::ReadyForDevelopment.as_ref()
        );
        let rows = self
            .conn
            .query(&sql, turso::params![workspace_name])
            .await?;
        Ok(!rows.is_empty())
    }

    /// Add a comment to a ticket (append-only).
    pub async fn add_comment(&self, id: &str, role: &str, content: &str) -> Result<()> {
        let comment_id = crate::generate_id();
        let now = turso::now();
        let tx = self.conn.begin_tx().await?;
        tx.execute(
            "INSERT INTO ticket_comments (id, ticket_id, role, content, created_at) VALUES (?1, ?2, ?3, ?4, ?5)",
            turso::params![comment_id, id, role, content, now.clone()],
        )
        .await?;
        tx.execute(
            "UPDATE tickets SET updated_at = ?1 WHERE id = ?2",
            turso::params![now, id],
        )
        .await?;
        tx.commit().await?;
        Ok(())
    }

    /// Get all comments for a ticket, ordered by creation time.
    pub async fn get_comments(&self, id: &str) -> Result<Vec<TicketComment>> {
        let sql = format!(
            "SELECT {COMMENT_COLUMNS} FROM ticket_comments WHERE ticket_id = ?1 ORDER BY created_at ASC"
        );
        let rows = self.conn.query(&sql, turso::params![id]).await?;
        let mut comments = Vec::new();
        for row in rows {
            comments.push(TicketComment {
                role: row.get(COL_COMMENT_ROLE)?,
                content: row.get(COL_COMMENT_CONTENT)?,
                created_at: row.get(COL_COMMENT_CREATED_AT)?,
            });
        }
        Ok(comments)
    }

    /// Validate prerequisites for a new ticket being created.
    ///
    /// Checks that every prerequisite ticket exists and belongs to the same
    /// workspace. Self-reference is checked separately by the caller (before
    /// this function is called, using the real ID generated within the transaction).
    ///
    /// At creation time, transitive cycles cannot exist because no existing
    /// ticket depends on the new ticket yet. Redundant prerequisites (e.g.,
    /// A and B where B already depends on A) are allowed — they do not form
    /// a cycle.
    async fn validate_prerequisites(
        &self,
        tx: &TxGuard<'_>,
        prerequisite_ids: &[String],
        workspace_name: &str,
    ) -> Result<()> {
        // Guard against empty list — SQLite rejects WHERE id IN ().
        if prerequisite_ids.is_empty() {
            return Ok(());
        }

        // Batch query: fetch id + workspace_name for all prerequisites in one
        // round trip. Uses tx.query() — the transaction's query method operates
        // on the upstream connection through the MutexGuard, avoiding mutex
        // deadlock with self.conn.query().
        let sql = format!(
            "SELECT id, workspace_name FROM tickets WHERE id IN ({})",
            turso::sql_in_placeholders(prerequisite_ids.len()),
        );
        let params: Vec<Value> = prerequisite_ids
            .iter()
            .map(|id| Value::Text(id.clone()))
            .collect();
        let rows = tx.query(&sql, params_from_iter(params)).await?;

        // Build a simple lookup Vec in a single pass over the result rows.
        let mut found: Vec<(String, String)> = Vec::new();
        for row in rows {
            let id: String = row.get(0)?;
            let ws_name: String = row.get(1)?;
            found.push((id, ws_name));
        }

        for pid in prerequisite_ids {
            let ws_name = found
                .iter()
                .find(|(i, _)| i == pid)
                .map(|(_, ws)| ws)
                .ok_or_else(|| anyhow::anyhow!("Prerequisite ticket not found: {pid}"))?;
            anyhow::ensure!(
                ws_name == workspace_name,
                "Prerequisite {pid} belongs to workspace '{ws_name}', \
                 not the ticket's workspace '{workspace_name}'. \
                 Cross-workspace prerequisites are not allowed.",
            );
        }

        Ok(())
    }

    /// List all tickets, optionally filtered by workspace and/or status.
    /// Used by the dashboard to show tickets across all workspaces.
    pub async fn list_all_tickets(
        &self,
        workspace_name: Option<&str>,
        status_filter: Option<TicketPhase>,
    ) -> Result<Vec<Ticket>> {
        let sql = format!(
            "SELECT {TICKET_COLUMNS} FROM tickets \
             WHERE (?1 IS NULL OR workspace_name = ?1) \
               AND (?2 IS NULL OR status = ?2) \
               AND is_archived = 0 \
             ORDER BY created_at DESC"
        );
        let status_str: Option<&str> = status_filter.as_ref().map(TicketPhase::as_ref);
        let rows = self
            .conn
            .query(&sql, turso::params![workspace_name, status_str])
            .await?;
        let mut tickets = Vec::new();
        for row in rows {
            tickets.push(self.ticket_from_row(&row, false).await?);
        }
        Ok(tickets)
    }

    /// Count how many tickets have the given status, optionally filtered by workspace.
    ///
    /// Excludes archived tickets to stay consistent with [`list_all_tickets`](Self::list_all_tickets)
    /// and most other read paths in this module. Currently unused for `Done` or `Cancelled`
    /// (the only statuses that ever get archived), so this is a defensive consistency fix —
    /// callers that pass a terminal status will not see archived tickets in the count.
    pub async fn count_by_status(
        &self,
        status: TicketPhase,
        workspace_name: Option<&str>,
    ) -> Result<i64> {
        self.conn
            .query_row(
                "SELECT COUNT(*) FROM tickets \
                 WHERE status = ?1 \
                   AND (?2 IS NULL OR workspace_name = ?2) \
                   AND is_archived = 0",
                turso::params![status.as_ref(), workspace_name],
                |row| row.get(0),
            )
            .await
            .map_err(Into::into)
    }

    /// Execute a SELECT query and collect the first column as candidate IDs
    /// for archival. All returned rows are included as-is; no status or phase
    /// validation is performed.
    async fn collect_archive_candidates(
        &self,
        sql: &str,
        params: impl turso::IntoParams + Send + 'static,
    ) -> Result<Vec<String>> {
        let rows = self.conn.query(sql, params).await?;
        let mut candidates = Vec::new();
        for row in rows {
            let id: String = row.get(0)?;
            candidates.push(id);
        }
        Ok(candidates)
    }

    /// Maximum IDs per batch to stay well under SQLite's
    /// `SQLITE_MAX_VARIABLE_NUMBER` limit (default 999), with one
    /// placeholder reserved for the timestamp.
    const ARCHIVE_CHUNK_SIZE: usize = 500;

    async fn batch_set_archived(&self, items: &[String]) -> Result<u64> {
        if items.is_empty() {
            return Ok(0);
        }
        let now = turso::now();
        let mut total: u64 = 0;
        for chunk in items.chunks(Self::ARCHIVE_CHUNK_SIZE) {
            let sql = format!(
                "UPDATE tickets SET is_archived = 1, updated_at = ? \
                 WHERE id IN ({}) AND assigned_to IS NULL",
                turso::sql_in_placeholders(chunk.len()),
            );
            let mut params: Vec<Value> = vec![Value::Text(now.clone())];
            params.extend(chunk.iter().map(|id| Value::Text(id.clone())));
            total += self
                .conn
                .execute(&sql, params_from_iter(params))
                .await
                .context("Failed to batch-archive tickets")?;
        }
        Ok(total)
    }

    /// Archive a single ticket by ID.
    ///
    /// Sets `is_archived = 1` and clears `assigned_to` (archived tickets should
    /// not remain assigned). Returns an error if the ticket does not exist.
    ///
    /// **Ordering constraint:** The caller must transition the ticket to a
    /// terminal state (`done` or `cancelled`) *before* calling this method.
    /// Unlike `batch_set_archived` there is no
    /// `assigned_to IS NULL` guard — [`transition_to`](Self::transition_to)
    /// already clears the assignee, and a single-ticket archive on an assigned
    /// ticket is intentionally allowed to resolve stale assignments.
    pub async fn set_archived(&self, id: &str) -> Result<()> {
        let now = turso::now();
        self.execute_and_cancel(
            "UPDATE tickets SET is_archived = 1, assigned_to = NULL, updated_at = ?1 \
             WHERE id = ?2",
            turso::params![now, id],
            id,
            "set archived",
        )
        .await
    }

    pub async fn archive_stale_cancelled(&self, hours: i64) -> Result<u64> {
        let cutoff = (Utc::now() - Duration::hours(hours)).to_rfc3339();
        let to_archive = self
            .collect_archive_candidates(
                "SELECT id FROM tickets \
             WHERE status = ?1 AND updated_at < ?2 AND assigned_to IS NULL \
             AND is_archived = 0",
                turso::params![TicketPhase::Cancelled.as_ref(), cutoff],
            )
            .await?;
        self.batch_set_archived(&to_archive).await
    }

    pub async fn archive_all_done_and_cancelled(
        &self,
        workspace_name: Option<&str>,
    ) -> Result<u64> {
        let done_cancelled = [TicketPhase::Done, TicketPhase::Cancelled];
        let select_sql = format!(
            "SELECT id FROM tickets WHERE status IN ({}) \
             AND assigned_to IS NULL AND is_archived = 0 \
             AND (?1 IS NULL OR workspace_name = ?1)",
            status_list_sql_fragment(&done_cancelled),
        );
        let to_archive = self
            .collect_archive_candidates(&select_sql, turso::params![workspace_name])
            .await?;
        self.batch_set_archived(&to_archive).await
    }

    // ── Archived ticket search methods ────────────────────────────────────
    //
    // These encapsulate the FTS and embedding SQL that
    // [`SearchArchivedTicketsTool`](crate::tools::search_archived_tickets)
    // previously ran directly against `self.conn`. The board owns the schema
    // (`ngram` tokenizer, FTS index name, blob format) and the tool layer
    // owns the hybrid RRF merge logic.

    /// Search archived tickets by FTS keyword match.
    ///
    /// Sanitizes the input query (strips non-alphanumeric characters) before
    /// matching against the `ngram`-tokenized FTS index on `title`.
    ///
    /// Returns up to `limit` `(id, fts_score)` pairs, highest score first.
    /// On SQL error (e.g. corrupt FTS index), logs a warning and returns an
    /// empty vec — the caller may fall through to vector search as a graceful
    /// degradation strategy.
    pub async fn search_archived_by_fts(
        &self,
        query: &str,
        limit: usize,
    ) -> Result<Vec<(String, f64)>> {
        let sanitized = crate::turso::sanitize_fts_query(query);
        if sanitized.is_empty() {
            return Ok(Vec::new());
        }

        let sql = format!(
            "SELECT t.id, fts_score(t.title, ?1) AS score \
             FROM tickets t \
             WHERE t.is_archived = 1 \
               AND t.title MATCH ?1 \
             ORDER BY score DESC LIMIT {limit}"
        );
        match self
            .conn
            .query_map(&sql, turso::params![sanitized.clone()], |row| {
                let id: String = row.get(0)?;
                let score: f64 = row.get(1)?;
                Ok::<_, anyhow::Error>((id, score))
            })
            .await
        {
            Ok(items) => {
                let mut results = Vec::new();
                for item in items {
                    results.push(item?);
                }
                Ok(results)
            }
            Err(e) => {
                tracing::warn!(
                    query = %sanitized,
                    error = %e,
                    "FTS search for archived tickets failed"
                );
                Ok(Vec::new())
            }
        }
    }

    /// List archived tickets with non-NULL embeddings, deserialized.
    ///
    /// Returns `(id, embedding)` pairs for all archived tickets that have
    /// a stored embedding blob. Embeddings are deserialized from the
    /// on-disk `[u8]` byte layout (4-byte little-endian `f32`) into
    /// `Vec<f32>` via [`crate::vector::bytes_to_vec`].
    ///
    /// This returns ALL archived tickets with embeddings — there is no
    /// LIMIT because the caller (the tool layer) needs all candidates for
    /// cosine-similarity ranking, and the archive size is bounded in practice
    /// by the total ticket volume of the installation.
    pub async fn list_archived_with_embeddings(&self) -> Result<Vec<(String, Vec<f32>)>> {
        let rows = self
            .conn
            .query(
                "SELECT id, embedding FROM tickets \
                 WHERE is_archived = 1 AND embedding IS NOT NULL",
                turso::params![],
            )
            .await?;

        let mut candidates: Vec<(String, Vec<f32>)> = Vec::new();
        for row in &rows {
            let id: String = row.get(0)?;
            let stored: Vec<u8> = row.get(1)?;
            let emb = crate::vector::bytes_to_vec(&stored);
            candidates.push((id, emb));
        }
        Ok(candidates)
    }

    /// Batch fetch minimal ticket info (id, title, status) by ID list.
    ///
    /// Results are returned in the same order as `ids`. Missing IDs are
    /// silently omitted (no error).
    ///
    /// This method exists specifically for the search-archived-ticket
    /// formatting path, which needs just `id`, `title`, and `status` for
    /// display purposes after hybrid ranking.
    pub async fn list_tickets_minimal(
        &self,
        ids: &[String],
    ) -> Result<Vec<(String, String, String)>> {
        if ids.is_empty() {
            return Ok(Vec::new());
        }
        let sql = format!(
            "SELECT id, title, status FROM tickets WHERE id IN ({})",
            turso::sql_in_placeholders(ids.len()),
        );
        let params: Vec<Value> = ids.iter().map(|id| Value::Text(id.clone())).collect();

        let rows = self.conn.query(&sql, params_from_iter(params)).await?;

        let mut map: HashMap<String, (String, String)> = HashMap::new();
        for row in &rows {
            let id: String = row.get(0)?;
            let title: String = row.get(1)?;
            let status: String = row.get(2)?;
            map.insert(id, (title, status));
        }

        let mut results = Vec::with_capacity(ids.len());
        for id in ids {
            if let Some((title, status)) = map.get(id) {
                results.push((id.clone(), title.clone(), status.clone()));
            }
        }
        Ok(results)
    }
}

#[derive(Clone, Debug)]
pub struct BoardStore {
    pub(crate) conn: Connection,
}

/// Returns a reference to the global [`BoardStore`] singleton.
///
/// Open a BoardStore in a fresh temp directory (no global CONFIG dependency).
///
/// Placed outside `mod tests` so that test code in sibling modules
/// (e.g., `tools::search_archived_tickets`) can reuse it without
/// duplicating the setup logic.
#[cfg(test)]
pub(crate) async fn open_test_store() -> (BoardStore, tempfile::TempDir) {
    let tmp = tempfile::TempDir::new().expect("temp dir");
    let store = BoardStore::open(tmp.path()).await.expect("open store");
    (store, tmp)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Role;
    use crate::Tool;
    use crate::util::test::expect_ticket;
    use crate::workspace::test_ws;
    use crate::workspace::test_ws_named;
    use strum::IntoEnumIterator;
    use tempfile::TempDir;

    /// Open a test store and create a default ticket.
    /// Returns (store, temp_dir, ticket_id).
    async fn setup() -> (BoardStore, TempDir, String) {
        let (store, tmp) = crate::board::open_test_store().await;
        let id = default_ticket(&store, "/ws", "ws", "test").await;
        (store, tmp, id)
    }

    /// Create a ticket with the most common test pattern:
    /// title="Test", desc="desc", DEFAULT_TICKET_PHASE, at a given workspace.
    async fn default_ticket(
        store: &BoardStore,
        workspace_path: &str,
        workspace_name: &str,
        reporter: &str,
    ) -> String {
        store
            .create_ticket(
                "Test",
                "desc",
                &test_ws_named(workspace_path, workspace_name),
                DEFAULT_TICKET_PHASE,
                &[],
                reporter,
                None,
            )
            .await
            .expect("default_ticket")
    }

    #[tokio::test]
    async fn test_create_and_get_ticket() {
        let (store, _tmp) = open_test_store().await;

        let id = store
            .create_ticket(
                "Test",
                "A test ticket",
                &crate::workspace::test_ws_named("/workspace", "workspace"),
                DEFAULT_TICKET_PHASE,
                &[],
                "test",
                None,
            )
            .await
            .expect("create");

        let ticket = crate::util::test::expect_ticket(&store, &id).await;
        assert_eq!(ticket.title, "Test");
        assert_eq!(ticket.description, "A test ticket");
        assert_eq!(ticket.status, TicketPhase::Backlog);
        assert!(ticket.assigned_to.is_none());
        assert!(ticket.comments.is_empty());
        assert!(
            ticket.commit_hash.is_none(),
            "new ticket should have no commit_hash"
        );
        assert!(
            ticket.lines_added.is_none(),
            "new ticket should have no lines_added"
        );
        assert!(
            ticket.lines_removed.is_none(),
            "new ticket should have no lines_removed"
        );
    }

    #[tokio::test]
    async fn test_get_ticket_status() {
        let (store, _tmp) = open_test_store().await;

        // Non-existent ticket returns None.
        assert!(
            store
                .get_ticket_status("nonexistent")
                .await
                .expect("query")
                .is_none()
        );

        let id = store
            .create_ticket(
                "Status Test",
                "Testing get_ticket_status",
                &crate::workspace::test_ws_named("/workspace", "workspace"),
                TicketPhase::Planning,
                &[],
                "test",
                None,
            )
            .await
            .expect("create");

        let status = crate::util::test::expect_ticket_status(&store, &id).await;
        assert_eq!(status, TicketPhase::Planning);

        // After transition, reflects new status.
        store
            .transition_to(&id, None, TicketPhase::ReadyForDevelopment)
            .await
            .expect("set");
        let status = crate::util::test::expect_ticket_status(&store, &id).await;
        assert_eq!(status, TicketPhase::ReadyForDevelopment);
    }

    #[tokio::test]
    async fn test_backlog_phases_roundtrip() {
        let (store, _tmp) = open_test_store().await;

        store
            .create_ticket(
                "Backlog Test",
                "A backlog ticket",
                &test_ws_named("/workspace", "workspace"),
                DEFAULT_TICKET_PHASE,
                &[],
                "test",
                None,
            )
            .await
            .expect("create");

        // Parse roundtrip — all variants
        let variants: &[(&str, TicketPhase)] = &[
            ("backlog", TicketPhase::Backlog),
            ("analysis", TicketPhase::Analysis),
            ("planning", TicketPhase::Planning),
            ("ready_for_development", TicketPhase::ReadyForDevelopment),
            ("in_development", TicketPhase::InDevelopment),
            ("in_diagnostics", TicketPhase::InDiagnostics),
            ("diagnostics_done", TicketPhase::DiagnosticsDone),
            ("in_review", TicketPhase::InReview),
            ("reviewed", TicketPhase::Reviewed),
            ("in_qa", TicketPhase::InQa),
            ("qa_passed", TicketPhase::QaPassed),
            ("done", TicketPhase::Done),
            ("cancelled", TicketPhase::Cancelled),
            ("failed", TicketPhase::Failed),
            ("paused", TicketPhase::Paused),
        ];
        for (s, expected) in variants {
            assert_eq!(&s.parse::<TicketPhase>().unwrap(), expected, "variant: {s}");
        }

        // Roundtrip: as_ref() -> parse() for every variant
        for v in TicketPhase::iter() {
            let parsed: TicketPhase = v.as_ref().parse().unwrap();
            assert_eq!(&parsed, &v, "roundtrip failed for {v}");
        }

        // Error case
        assert!("unknown_phase".parse::<TicketPhase>().is_err());

        let claimed = store
            .claim_ticket_in_workspace(
                TicketPhase::Backlog,
                TicketPhase::Analysis,
                "workspace",
                false,
            )
            .await
            .expect("claim");
        assert!(claimed.is_some());
        let claimed = claimed.unwrap();
        assert_eq!(claimed.status, TicketPhase::Analysis);
    }

    #[test]
    fn test_display_name_no_underscores() {
        // Every variant's display_name() must be underscore-free
        // and non-empty.
        for variant in TicketPhase::iter() {
            let name = variant.display_name();
            assert!(!name.is_empty(), "empty display_name for {variant}");
            assert!(
                !name.contains('_'),
                "display_name for {variant} still has underscore: {name}"
            );
        }
    }

    #[tokio::test]
    async fn test_get_nonexistent_ticket() {
        let (store, _tmp) = open_test_store().await;

        let ticket = store.get_ticket("nonexistent").await.expect("get");
        assert!(ticket.is_none());
    }

    #[tokio::test]
    async fn test_unconditional_transition_clears_assignment() {
        let (store, _tmp, id) = setup().await;

        // Claim the ticket (sets assigned_to to NULL by default)
        let claimed = store
            .claim_ticket_in_workspace(
                TicketPhase::Backlog,
                TicketPhase::InDevelopment,
                "ws",
                false,
            )
            .await
            .expect("claim")
            .expect("ticket exists");

        // Set assigned_to explicitly (matching production dispatch_engineer behavior)
        store
            .set_assigned_to(&claimed.id, Some(Role::Engineer.as_str()))
            .await
            .expect("set_assigned_to");
        let ticket = store
            .get_ticket(&id)
            .await
            .expect("get")
            .expect("should exist");
        assert!(
            ticket.assigned_to.is_some(),
            "assigned_to should be set after set_assigned_to"
        );

        // Update status — this should clear assigned_to
        store
            .transition_to(&id, None, TicketPhase::DiagnosticsDone)
            .await
            .expect("update");

        let ticket = crate::util::test::expect_ticket(&store, &id).await;
        assert_eq!(ticket.status, TicketPhase::DiagnosticsDone);
        assert!(
            ticket.assigned_to.is_none(),
            "assigned_to should be cleared after unconditional transition"
        );
    }

    #[tokio::test]
    async fn test_guarded_transition_with_wrong_phase_fails() {
        let (store, _tmp, id) = setup().await;

        // Ticket is in Backlog; guard expects Done — should fail.
        let result = store
            .transition_to(&id, Some(TicketPhase::Done), TicketPhase::InDevelopment)
            .await;
        assert!(
            result.is_err(),
            "guarded transition with wrong phase should fail"
        );

        // Ticket should still be in Backlog.
        let ticket = crate::util::test::expect_ticket(&store, &id).await;
        assert_eq!(ticket.status, TicketPhase::Backlog);
    }

    #[tokio::test]
    async fn test_guarded_transition_with_correct_phase_succeeds() {
        let (store, _tmp, id) = setup().await;

        // Ticket is in Backlog; guard expects Backlog — should succeed.
        store
            .transition_to(&id, Some(TicketPhase::Backlog), TicketPhase::InDevelopment)
            .await
            .expect("guarded transition with correct phase should succeed");

        let ticket = crate::util::test::expect_ticket(&store, &id).await;
        assert_eq!(ticket.status, TicketPhase::InDevelopment);
    }

    #[tokio::test]
    async fn test_add_comment() {
        let (store, _tmp, id) = setup().await;

        store
            .add_comment(&id, Role::Engineer.as_str(), "done!")
            .await
            .expect("add comment");

        let comments = store.get_comments(&id).await.expect("get comments");
        assert_eq!(comments.len(), 1);
        assert_eq!(comments[0].role, Role::Engineer.as_str());
        assert_eq!(comments[0].content, "done!");
        assert!(!comments[0].created_at.is_empty());

        // Verify updated_at was bumped
        let ticket = crate::util::test::expect_ticket(&store, &id).await;
        assert!(ticket.updated_at > ticket.created_at);
    }

    #[tokio::test]
    async fn test_list_tickets() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        store
            .create_ticket("A", "desc", &ws, DEFAULT_TICKET_PHASE, &[], "test", None)
            .await
            .expect("create");
        store
            .create_ticket("B", "desc", &ws, DEFAULT_TICKET_PHASE, &[], "test", None)
            .await
            .expect("create");
        store
            .create_ticket("C", "desc", &ws, DEFAULT_TICKET_PHASE, &[], "test", None)
            .await
            .expect("create");

        // All tickets for the workspace
        let tickets = store
            .list_all_tickets(Some("ws"), None)
            .await
            .expect("list");
        assert_eq!(tickets.len(), 3);

        // Filter by status (none match since all are Backlog)
        let tickets = store
            .list_all_tickets(Some("ws"), Some(TicketPhase::Done))
            .await
            .expect("list");
        assert_eq!(tickets.len(), 0);
    }

    #[tokio::test]
    async fn test_reset_inflight_tickets_new() {
        let (store, _tmp) = open_test_store().await;

        let ticket1 = default_ticket(&store, "/ws", "ws", "test").await;
        let ticket2 = default_ticket(&store, "/ws", "ws", "test").await;

        store.reset_inflight_tickets().await.expect("reset");

        // All new tickets stay Backlog (only in-flight tickets are affected by reset)
        for id in [&ticket1, &ticket2] {
            let t = store
                .get_ticket(id)
                .await
                .expect("get")
                .expect("should exist");
            assert_eq!(
                t.status,
                TicketPhase::Backlog,
                "new tickets stay backlog (not in any inflight phase)"
            );
            assert!(t.assigned_to.is_none(), "assigned_to should be cleared");
        }
    }

    #[tokio::test]
    async fn test_reset_inflight_tickets_sets_reservation() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        // Create tickets in InDevelopment and Analysis (both reset targets)
        let in_dev_id = store
            .create_ticket(
                "InDev",
                "desc",
                &ws,
                TicketPhase::InDevelopment,
                &[],
                "test",
                None,
            )
            .await
            .expect("create InDevelopment ticket");
        let analysis_id = store
            .create_ticket(
                "Analysis",
                "desc",
                &ws,
                TicketPhase::Analysis,
                &[],
                "test",
                None,
            )
            .await
            .expect("create Analysis ticket");

        store.reset_inflight_tickets().await.expect("reset");

        // InDevelopment → ReadyForDevelopment with reservation=1
        let t = expect_ticket(&store, &in_dev_id).await;
        assert_eq!(
            t.status,
            TicketPhase::ReadyForDevelopment,
            "InDevelopment should reset to ReadyForDevelopment"
        );
        assert!(
            t.pipeline_reservation,
            "InDevelopment reset should set pipeline_reservation = 1"
        );

        // Analysis → Backlog with reservation=0 (Analysis is not a pipeline blocker)
        let t = expect_ticket(&store, &analysis_id).await;
        assert_eq!(
            t.status,
            TicketPhase::Backlog,
            "Analysis should reset to Backlog"
        );
        assert!(
            !t.pipeline_reservation,
            "Analysis reset should NOT set pipeline_reservation"
        );
    }

    #[tokio::test]
    async fn test_claim_prefers_reserved_ticket() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        // Create two ReadyForDevelopment tickets
        let fresh_id = store
            .create_ticket(
                "Fresh",
                "desc",
                &ws,
                TicketPhase::ReadyForDevelopment,
                &[],
                "test",
                None,
            )
            .await
            .expect("create fresh ticket");
        let reserved_id = store
            .create_ticket(
                "Reserved",
                "desc",
                &ws,
                TicketPhase::ReadyForDevelopment,
                &[],
                "test",
                None,
            )
            .await
            .expect("create reserved ticket");

        // Set reservation on the second ticket
        store
            .transition_to_with_reservation(
                &reserved_id,
                Some(TicketPhase::ReadyForDevelopment),
                TicketPhase::ReadyForDevelopment,
                true,
            )
            .await
            .expect("set reservation");

        // When claiming with require_clear_pipeline, the reserved ticket should be picked first
        let claimed = store
            .claim_ticket_in_workspace(
                TicketPhase::ReadyForDevelopment,
                TicketPhase::InDevelopment,
                "ws",
                true,
            )
            .await
            .expect("claim")
            .expect("should claim a ticket");
        assert_eq!(
            claimed.id, reserved_id,
            "Reserved ticket should be claimed before fresh one"
        );
        assert!(
            !claimed.pipeline_reservation,
            "Claim should clear pipeline_reservation"
        );

        // Verify the cleared reservation is persisted in the DB
        // (the returned Ticket struct already reflects the DB state, but
        // a separate re-read explicitly tests persistence).
        let reserved_db = expect_ticket(&store, &reserved_id).await;
        assert!(
            !reserved_db.pipeline_reservation,
            "Reservation should be 0 in DB after claim"
        );

        // After the reserved ticket is claimed (now InDevelopment, pipeline-blocking),
        // the fresh ticket is still at ReadyForDevelopment but cannot be claimed
        // because the pipeline is blocked. Verify the fresh ticket remains untouched.
        let fresh = expect_ticket(&store, &fresh_id).await;
        assert_eq!(
            fresh.status,
            TicketPhase::ReadyForDevelopment,
            "Fresh ticket should still be at ReadyForDevelopment"
        );
        assert!(
            !fresh.pipeline_reservation,
            "Fresh ticket should have no reservation"
        );
    }

    #[tokio::test]
    async fn test_has_pipeline_blocker_reserved() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        // A fresh ReadyForDevelopment ticket should NOT be a blocker
        let id = store
            .create_ticket(
                "Fresh",
                "desc",
                &ws,
                TicketPhase::ReadyForDevelopment,
                &[],
                "test",
                None,
            )
            .await
            .expect("create");
        assert!(
            !store
                .has_pipeline_blocker_for_workspace("ws")
                .await
                .expect("check"),
            "Fresh ReadyForDevelopment ticket should not be a pipeline blocker"
        );

        // After setting reservation, it should be a blocker
        store
            .transition_to_with_reservation(
                &id,
                Some(TicketPhase::ReadyForDevelopment),
                TicketPhase::ReadyForDevelopment,
                true,
            )
            .await
            .expect("set reservation");
        assert!(
            store
                .has_pipeline_blocker_for_workspace("ws")
                .await
                .expect("check"),
            "Reserved ReadyForDevelopment ticket should be a pipeline blocker"
        );

        // After removing reservation, it should not be a blocker
        store
            .transition_to_with_reservation(
                &id,
                Some(TicketPhase::ReadyForDevelopment),
                TicketPhase::ReadyForDevelopment,
                false,
            )
            .await
            .expect("clear reservation");
        assert!(
            !store
                .has_pipeline_blocker_for_workspace("ws")
                .await
                .expect("check"),
            "Non-reserved ReadyForDevelopment ticket should not be a pipeline blocker again"
        );
    }

    /// Verify that every non-transitory pipeline-blocking phase has a reset transition.
    ///
    /// [`PIPELINE_BLOCKING_STATUSES`] defines 7 phases; 4 of them (InDevelopment,
    /// InDiagnostics, InReview, InQa) have entries in [`RESET_TRANSITIONS`]. The remaining
    /// 3 phases ([`TRANSITORY_HANDOFF_PHASES`]) are transitory handoff states that the
    /// poller picks up within seconds — no agent is mid-execution in those states,
    /// so they don't need reset entries.
    ///
    /// This test does NOT assert the reverse direction (reset → pipeline blocker),
    /// because [`RESET_TRANSITIONS`] also includes `Analysis → Backlog`, and `Analysis`
    /// is intentionally not a pipeline blocker (it's a pre-flight phase).
    ///
    /// It also mechanically verifies that [`TRANSITORY_HANDOFF_PHASES`] is a subset of
    /// [`PIPELINE_BLOCKING_STATUSES`], ensuring the two sets stay in sync.
    #[test]
    fn test_pipeline_blockers_coverage() {
        // Verify that every transitory handoff phase is a pipeline blocker.
        for phase in TRANSITORY_HANDOFF_PHASES {
            assert!(
                PIPELINE_BLOCKING_STATUSES.contains(phase),
                "\
TRANSITORY_HANDOFF_PHASES contains `{phase}` which is not in \
PIPELINE_BLOCKING_STATUSES. Every transitory handoff phase must also \
be a pipeline blocker.\
                ",
            );
        }

        // Collect all `from` phases from BoardStore::RESET_TRANSITIONS for easy lookup.
        let reset_from: Vec<TicketPhase> = BoardStore::RESET_TRANSITIONS
            .iter()
            .map(|(from, _, _)| *from)
            .collect();

        for phase in PIPELINE_BLOCKING_STATUSES {
            let has_reset = reset_from.contains(phase);
            assert!(
                has_reset || phase.is_transitory_handoff(),
                "\
PIPELINE_BLOCKING_STATUSES contains `{phase}` which has no corresponding \
entry in RESET_TRANSITIONS and is not a transitory handoff phase \
(see `TicketPhase::is_transitory_handoff`). Either add a reset transition to \
RESET_TRANSITIONS, or mark the phase as transitory handoff in that method \
with a comment explaining why no agent is mid-execution in that state.\
                ",
            );
        }
    }

    #[tokio::test]
    async fn test_claim_ticket_in_workspace() {
        let (store, _tmp) = open_test_store().await;

        // Create tickets in two different workspaces
        let ws_a = test_ws_named("/ws_a", "workspace_a");
        let ws_b = test_ws_named("/ws_b", "workspace_b");

        let id_a = store
            .create_ticket(
                "Ticket A",
                "desc",
                &ws_a,
                DEFAULT_TICKET_PHASE,
                &[],
                "test",
                None,
            )
            .await
            .expect("create ticket in ws_a");

        let id_b = store
            .create_ticket(
                "Ticket B",
                "desc",
                &ws_b,
                DEFAULT_TICKET_PHASE,
                &[],
                "test",
                None,
            )
            .await
            .expect("create ticket in ws_b");

        // Claim ticket from workspace A — should succeed
        let claimed_a = store
            .claim_ticket_in_workspace(
                TicketPhase::Backlog,
                TicketPhase::InDevelopment,
                "workspace_a",
                false,
            )
            .await
            .expect("claim in ws_a")
            .expect("should claim ticket from ws_a");
        assert_eq!(claimed_a.id, id_a);
        assert_eq!(claimed_a.workspace_name, "workspace_a");
        assert_eq!(claimed_a.status, TicketPhase::InDevelopment);
        assert!(claimed_a.assigned_to.is_none());

        // Claim from workspace A again — should return None (no more backlog tickets)
        assert!(
            store
                .claim_ticket_in_workspace(
                    TicketPhase::Backlog,
                    TicketPhase::InDevelopment,
                    "workspace_a",
                    false,
                )
                .await
                .expect("second claim in ws_a")
                .is_none(),
            "no more tickets to claim in ws_a"
        );

        // Claim ticket from workspace B — should still succeed (different workspace)
        let claimed_b = store
            .claim_ticket_in_workspace(
                TicketPhase::Backlog,
                TicketPhase::InDevelopment,
                "workspace_b",
                false,
            )
            .await
            .expect("claim in ws_b")
            .expect("should claim ticket from ws_b");
        assert_eq!(claimed_b.id, id_b);
        assert_eq!(claimed_b.workspace_name, "workspace_b");
    }

    /// Table-driven tests for `claim_ticket_in_workspace` with `require_clear_pipeline: true`.
    #[allow(clippy::too_many_lines)]
    #[tokio::test]
    async fn test_claim_ticket_in_workspace_if_pipeline_free() {
        /// The pipeline scenario for a single test case.
        enum Scenario {
            /// Blocker in the same workspace — claim should be blocked.
            SameWorkspace(TicketPhase),
            /// Blocker in a different workspace — claim should succeed.
            DifferentWorkspace(TicketPhase),
            /// No blocker — claim should succeed.
            NoBlocker,
        }

        struct Case {
            name: &'static str,
            /// Unique suffix for workspace names (isolates cases).
            suffix: &'static str,
            scenario: Scenario,
        }

        let cases = [
            Case {
                name: "blocked by same-workspace pipeline ticket",
                suffix: "blocked",
                scenario: Scenario::SameWorkspace(TicketPhase::InReview),
            },
            Case {
                name: "not blocked by cross-workspace pipeline ticket",
                suffix: "cross",
                scenario: Scenario::DifferentWorkspace(TicketPhase::InDevelopment),
            },
            Case {
                name: "no blocker succeeds",
                suffix: "none",
                scenario: Scenario::NoBlocker,
            },
        ];

        let (store, _tmp) = open_test_store().await;

        for case in &cases {
            let suffix = case.suffix;

            // Derive workspace names from the scenario.
            let (claim_ws_name, blocker_ws_name) = match &case.scenario {
                Scenario::DifferentWorkspace(_) => (
                    format!("ws_{suffix}_claimable"),
                    format!("ws_{suffix}_blocker"),
                ),
                // SameWorkspace and NoBlocker both use a single workspace name.
                Scenario::SameWorkspace(_) | Scenario::NoBlocker => {
                    let name = format!("ws_{suffix}");
                    (name.clone(), name)
                }
            };

            let expected_claim = !matches!(case.scenario, Scenario::SameWorkspace(_));

            let blocker_ws = test_ws_named(&format!("/{blocker_ws_name}"), &blocker_ws_name);
            let claimable_ws = test_ws_named(&format!("/{claim_ws_name}"), &claim_ws_name);

            // Create a pipeline blocker (if any)
            if let Scenario::SameWorkspace(phase) | Scenario::DifferentWorkspace(phase) =
                &case.scenario
            {
                // When blocker and claimable share a workspace, place the
                // blocker in the claimable's workspace (they are the same).
                let blocker_target = match &case.scenario {
                    Scenario::DifferentWorkspace(_) => &blocker_ws,
                    Scenario::SameWorkspace(_) => &claimable_ws,
                    // Not reachable: NoBlocker is guarded by the enclosing if-let.
                    Scenario::NoBlocker => unreachable!(),
                };
                store
                    .create_ticket(
                        "Blocker",
                        "already in pipeline",
                        blocker_target,
                        *phase,
                        &[],
                        "test",
                        None,
                    )
                    .await
                    .expect("create blocker");
            }

            // Create a claimable ticket
            let id = store
                .create_ticket(
                    "Claimable",
                    "ready for dev",
                    &claimable_ws,
                    TicketPhase::ReadyForDevelopment,
                    &[],
                    "test",
                    None,
                )
                .await
                .expect("create claimable");

            // Claim with require_clear_pipeline=true
            let claimed = store
                .claim_ticket_in_workspace(
                    TicketPhase::ReadyForDevelopment,
                    TicketPhase::InDevelopment,
                    &claim_ws_name,
                    true,
                )
                .await
                .expect("claim should not error");

            if expected_claim {
                let claimed = claimed.expect("should claim ticket");
                assert_eq!(claimed.id, id, "Case '{}': wrong ticket id", case.name);
                assert_eq!(
                    claimed.status,
                    TicketPhase::InDevelopment,
                    "Case '{}': wrong status after claim",
                    case.name
                );
            } else {
                assert!(
                    claimed.is_none(),
                    "Case '{}': claim should be blocked",
                    case.name
                );
            }
        }
    }

    // ── Prerequisites ────────────────────────────────────────────

    #[tokio::test]
    async fn test_create_ticket_with_prerequisites() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        // Create prerequisite tickets first
        let p1 = store
            .create_ticket(
                "P1",
                "prereq one",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[],
                "test",
                None,
            )
            .await
            .expect("create p1");
        let p2 = store
            .create_ticket(
                "P2",
                "prereq two",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[],
                "test",
                None,
            )
            .await
            .expect("create p2");

        // Create a ticket depending on both
        let deps = vec![p1.clone(), p2.clone()];
        let id = store
            .create_ticket(
                "Dependent",
                "needs both",
                &ws,
                DEFAULT_TICKET_PHASE,
                &deps,
                "test",
                None,
            )
            .await
            .expect("create dependent");

        let ticket = crate::util::test::expect_ticket(&store, &id).await;
        assert_eq!(ticket.prerequisites.len(), 2);
        assert!(ticket.prerequisites.contains(&p1));
        assert!(ticket.prerequisites.contains(&p2));
    }

    #[tokio::test]
    async fn test_create_ticket_nonexistent_prereq_error() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        let result = store
            .create_ticket(
                "Bad",
                "desc",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[String::from("nonexistent-1")],
                "test",
                None,
            )
            .await;

        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("not found"),
            "expected 'not found' in error, got: {err}"
        );
    }

    #[tokio::test]
    async fn test_create_ticket_self_reference_error() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        // Create a ticket so we have a counter row
        let _first = store
            .create_ticket("First", "any", &ws, DEFAULT_TICKET_PHASE, &[], "test", None)
            .await
            .expect("create first");

        // The next id would be ws-1, so pass that as a self-reference
        let result = store
            .create_ticket(
                "SelfReferencing",
                "desc",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[String::from("ws-1")],
                "test",
                None,
            )
            .await;

        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("cannot depend on itself"),
            "expected 'cannot depend on itself' in error, got: {err}"
        );
    }

    #[tokio::test]
    async fn test_create_ticket_cross_workspace_error() {
        let (store, _tmp) = open_test_store().await;
        let ws_a = test_ws_named("/ws_a", "workspace_a");
        let ws_b = test_ws_named("/ws_b", "workspace_b");

        let pa = store
            .create_ticket("PA", "in A", &ws_a, DEFAULT_TICKET_PHASE, &[], "test", None)
            .await
            .expect("create pa");

        let result = store
            .create_ticket(
                "InB",
                "depends on A",
                &ws_b,
                DEFAULT_TICKET_PHASE,
                std::slice::from_ref(&pa),
                "test",
                None,
            )
            .await;

        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("Cross-workspace"),
            "expected 'Cross-workspace' in error, got: {err}"
        );
    }

    #[tokio::test]
    async fn test_circular_dependency_rejected() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        // A depends on nothing
        let a = store
            .create_ticket("A", "first", &ws, DEFAULT_TICKET_PHASE, &[], "test", None)
            .await
            .expect("create a");

        // B depends on A
        let b = store
            .create_ticket(
                "B",
                "second",
                &ws,
                DEFAULT_TICKET_PHASE,
                std::slice::from_ref(&a),
                "test",
                None,
            )
            .await
            .expect("create b");

        // Verify that A→B chain works: creating a ticket with both A and B
        // as prerequisites is NOT a cycle (it's just redundant, since A is
        // already transitively required through B). This should succeed.
        let _c = store
            .create_ticket(
                "C",
                "depends on both",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[a.clone(), b.clone()],
                "test",
                None,
            )
            .await
            .expect("create c — A and B as prereqs is not a cycle");
    }

    #[tokio::test]
    async fn test_blocked_ticket_not_claimable() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        // P is a prerequisite
        let p = store
            .create_ticket("P", "prereq", &ws, DEFAULT_TICKET_PHASE, &[], "test", None)
            .await
            .expect("create p");

        // D depends on P
        let d_id = store
            .create_ticket(
                "D",
                "dependent",
                &ws,
                DEFAULT_TICKET_PHASE,
                std::slice::from_ref(&p),
                "test",
                None,
            )
            .await
            .expect("create d");

        // Verify D has a prerequisite
        let d_ticket = crate::util::test::expect_ticket(&store, &d_id).await;
        assert_eq!(d_ticket.prerequisites, vec![p.clone()]);

        // D should not be claimable because P is still in 'backlog'
        let claimed = store
            .claim_ticket_in_workspace(TicketPhase::Backlog, TicketPhase::Analysis, "ws", false)
            .await
            .expect("claim");
        assert!(claimed.is_some(), "should claim P (no unmet prereqs)");
        // claimed should NOT be D — it should be P (the only unblocked one)
        let claimed = claimed.unwrap();
        assert_eq!(
            claimed.id, p,
            "should have claimed the unblocked ticket P, not D"
        );

        // Now P is in Analysis — D should still be blocked
        let second = store
            .claim_ticket_in_workspace(TicketPhase::Backlog, TicketPhase::Analysis, "ws", false)
            .await
            .expect("claim");
        assert!(
            second.is_none(),
            "D should be blocked because P is in analysis, not done"
        );
    }

    #[tokio::test]
    async fn test_unblocked_after_prereq_done() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        // P is a prerequisite
        let p = store
            .create_ticket("P", "prereq", &ws, DEFAULT_TICKET_PHASE, &[], "test", None)
            .await
            .expect("create p");

        // D depends on P
        let d = store
            .create_ticket(
                "D",
                "dependent",
                &ws,
                DEFAULT_TICKET_PHASE,
                std::slice::from_ref(&p),
                "test",
                None,
            )
            .await
            .expect("create d");

        // D is blocked while P is in backlog
        let blocked = store
            .claim_ticket_in_workspace(TicketPhase::Backlog, TicketPhase::Analysis, "ws", false)
            .await
            .expect("claim");
        // Should have claimed P, not D
        assert_eq!(blocked.unwrap().id, p);

        // Move P to done
        store
            .transition_to(&p, None, TicketPhase::Done)
            .await
            .expect("set done");

        // Now D should be claimable
        let unblocked = store
            .claim_ticket_in_workspace(TicketPhase::Backlog, TicketPhase::Analysis, "ws", false)
            .await
            .expect("claim");
        assert!(unblocked.is_some(), "D should be claimable after P is done");
        assert_eq!(unblocked.unwrap().id, d);
    }

    #[tokio::test]
    async fn test_transitive_prerequisites_block() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        // A (no prereqs)

        let a = store
            .create_ticket("A", "leaf", &ws, DEFAULT_TICKET_PHASE, &[], "test", None)
            .await
            .expect("create a");

        // B depends on A
        let b = store
            .create_ticket(
                "B",
                "middle",
                &ws,
                DEFAULT_TICKET_PHASE,
                std::slice::from_ref(&a),
                "test",
                None,
            )
            .await
            .expect("create b");

        // C depends on B
        let c = store
            .create_ticket(
                "C",
                "top",
                &ws,
                DEFAULT_TICKET_PHASE,
                std::slice::from_ref(&b),
                "test",
                None,
            )
            .await
            .expect("create c");

        // C should be blocked even though B is done — A is still blocking
        // First claim: A is the only unblocked one
        let claimed = store
            .claim_ticket_in_workspace(TicketPhase::Backlog, TicketPhase::Analysis, "ws", false)
            .await
            .expect("claim")
            .expect("should claim A");
        assert_eq!(claimed.id, a);

        // Move A to done
        store
            .transition_to(&a, None, TicketPhase::Done)
            .await
            .expect("done a");

        // Now B should be claimable
        let claimed2 = store
            .claim_ticket_in_workspace(TicketPhase::Backlog, TicketPhase::Analysis, "ws", false)
            .await
            .expect("claim")
            .expect("should claim B");
        assert_eq!(claimed2.id, b);

        // Move B to done
        store
            .transition_to(&b, None, TicketPhase::Done)
            .await
            .expect("done b");

        // Now C should be claimable
        let claimed3 = store
            .claim_ticket_in_workspace(TicketPhase::Backlog, TicketPhase::Analysis, "ws", false)
            .await
            .expect("claim")
            .expect("should claim C");
        assert_eq!(claimed3.id, c);
    }

    #[tokio::test]
    async fn test_archive_stale_cancelled() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        // Ticket 1: cancelled, old (2h) → should be archived
        let old_cancelled_id = store
            .create_ticket(
                "old-cancelled",
                "desc",
                &ws,
                TicketPhase::Backlog,
                &[],
                "test",
                None,
            )
            .await
            .expect("create_ticket");

        let two_hours_ago = (Utc::now() - chrono::Duration::hours(2)).to_rfc3339();

        store
            .transition_to(&old_cancelled_id, None, TicketPhase::Cancelled)
            .await
            .expect("cancel");
        store
            .conn
            .execute(
                "UPDATE tickets SET updated_at = ?1 WHERE id = ?2",
                crate::turso::params![two_hours_ago.clone(), old_cancelled_id.clone()],
            )
            .await
            .expect("backdate");

        // Ticket 2: cancelled, fresh → should NOT be archived
        let fresh_cancelled_id = store
            .create_ticket(
                "fresh-cancelled",
                "desc",
                &ws,
                TicketPhase::Backlog,
                &[],
                "test",
                None,
            )
            .await
            .expect("create_ticket");
        store
            .transition_to(&fresh_cancelled_id, None, TicketPhase::Cancelled)
            .await
            .expect("cancel");
        // No backdating — updated_at is now.

        // Ticket 3: not cancelled (Backlog), old → should NOT be archived
        let old_backlog_id = store
            .create_ticket(
                "old-backlog",
                "desc",
                &ws,
                TicketPhase::Backlog,
                &[],
                "test",
                None,
            )
            .await
            .expect("create_ticket");
        store
            .conn
            .execute(
                "UPDATE tickets SET updated_at = ?1 WHERE id = ?2",
                crate::turso::params![two_hours_ago.clone(), old_backlog_id.clone()],
            )
            .await
            .expect("backdate");

        // Act
        let count = store
            .archive_stale_cancelled(1)
            .await
            .expect("archive_stale_cancelled");
        assert_eq!(count, 1, "should archive only the old cancelled ticket");

        // Assert
        let old_cancelled = crate::util::test::expect_ticket(&store, &old_cancelled_id).await;
        assert!(
            old_cancelled.is_archived,
            "old cancelled ticket should be archived"
        );
        assert_eq!(old_cancelled.status, TicketPhase::Cancelled);

        let fresh_cancelled = crate::util::test::expect_ticket(&store, &fresh_cancelled_id).await;
        assert!(
            !fresh_cancelled.is_archived,
            "fresh cancelled ticket should NOT be archived"
        );
        assert_eq!(fresh_cancelled.status, TicketPhase::Cancelled);

        let old_backlog = crate::util::test::expect_ticket(&store, &old_backlog_id).await;
        assert!(
            !old_backlog.is_archived,
            "old non-cancelled ticket should NOT be archived"
        );
        assert_eq!(old_backlog.status, TicketPhase::Backlog);
    }

    #[tokio::test]
    async fn test_archive_stale_cancelled_empty_db() {
        let (store, _tmp) = open_test_store().await;

        let count = store
            .archive_stale_cancelled(1)
            .await
            .expect("archive_stale_cancelled");
        assert_eq!(count, 0, "Empty DB should return 0");
    }

    #[tokio::test]
    async fn test_archive_all_done_and_cancelled() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        // Create three tickets: one Done, one Cancelled, one Backlog.
        let done_id = store
            .create_ticket("done", "desc", &ws, TicketPhase::Backlog, &[], "test", None)
            .await
            .expect("create_ticket");
        store
            .transition_to(&done_id, None, TicketPhase::Done)
            .await
            .expect("set done");

        let cancelled_id = store
            .create_ticket(
                "cancelled",
                "desc",
                &ws,
                TicketPhase::Backlog,
                &[],
                "test",
                None,
            )
            .await
            .expect("create_ticket");
        store
            .transition_to(&cancelled_id, None, TicketPhase::Cancelled)
            .await
            .expect("cancel");

        let backlog_id = store
            .create_ticket(
                "backlog",
                "desc",
                &ws,
                TicketPhase::Backlog,
                &[],
                "test",
                None,
            )
            .await
            .expect("create_ticket");
        // Leave in Backlog.

        // Act
        let count = store
            .archive_all_done_and_cancelled(None)
            .await
            .expect("archive");
        assert_eq!(count, 2, "should archive Done and Cancelled tickets");

        // Assert
        let done_ticket = crate::util::test::expect_ticket(&store, &done_id).await;
        assert!(done_ticket.is_archived, "Done ticket should be archived");
        assert_eq!(done_ticket.status, TicketPhase::Done);

        let cancelled_ticket = crate::util::test::expect_ticket(&store, &cancelled_id).await;
        assert!(
            cancelled_ticket.is_archived,
            "Cancelled ticket should be archived"
        );
        assert_eq!(cancelled_ticket.status, TicketPhase::Cancelled);

        let backlog_ticket = crate::util::test::expect_ticket(&store, &backlog_id).await;
        assert!(
            !backlog_ticket.is_archived,
            "Backlog ticket should NOT be archived"
        );
        assert_eq!(backlog_ticket.status, TicketPhase::Backlog);
    }

    #[tokio::test]
    async fn test_archive_all_done_and_cancelled_empty_db() {
        let (store, _tmp) = open_test_store().await;

        let count = store
            .archive_all_done_and_cancelled(None)
            .await
            .expect("archive_all_done_and_cancelled");
        assert_eq!(count, 0, "Empty DB should return 0");
    }

    #[tokio::test]
    async fn test_archive_all_done_and_cancelled_workspace_filter() {
        let (store, _tmp) = open_test_store().await;

        // Create a done ticket in ws1 and another in ws2.
        let id1 = default_ticket(&store, "/ws1", "ws1", "test").await;
        store
            .transition_to(&id1, None, TicketPhase::Done)
            .await
            .expect("set done");
        let id2 = default_ticket(&store, "/ws2", "ws2", "test").await;
        store
            .transition_to(&id2, None, TicketPhase::Done)
            .await
            .expect("set done");

        // Archive only ws1.
        let count = store
            .archive_all_done_and_cancelled(Some("ws1"))
            .await
            .expect("archive_all_done_and_cancelled");
        assert_eq!(count, 1, "Should archive only ws1 ticket");

        let ticket1 = crate::util::test::expect_ticket(&store, &id1).await;
        assert!(ticket1.is_archived, "ws1 ticket should be archived");
        assert_eq!(
            ticket1.status,
            TicketPhase::Done,
            "ws1 status should remain Done"
        );

        let ticket2 = crate::util::test::expect_ticket(&store, &id2).await;
        assert!(!ticket2.is_archived, "ws2 ticket should NOT be archived");
        assert_eq!(
            ticket2.status,
            TicketPhase::Done,
            "ws2 ticket should remain Done"
        );
    }

    #[tokio::test]
    async fn test_count_by_status_excludes_archived() {
        let (store, _tmp) = open_test_store().await;
        let _ws = test_ws_named("/ws", "ws");

        // Create a ticket and set it to Done.
        let id = default_ticket(&store, "/ws", "ws", "test").await;
        store
            .transition_to(&id, None, TicketPhase::Done)
            .await
            .expect("set done");

        // Before archiving, count includes the Done ticket.
        let count_before = store
            .count_by_status(TicketPhase::Done, None)
            .await
            .expect("count before");
        assert_eq!(count_before, 1, "Should count Done ticket before archive");

        // Archive done tickets.
        let archived = store
            .archive_all_done_and_cancelled(None)
            .await
            .expect("archive");
        assert_eq!(archived, 1, "Should have archived 1 ticket");

        // After archiving, count_by_status(Done) should return 0.
        let count_after = store
            .count_by_status(TicketPhase::Done, None)
            .await
            .expect("count after");
        assert_eq!(count_after, 0, "Should not count archived Done tickets");

        // Archived tickets with other statuses should also be excluded.
        let count_cancelled = store
            .count_by_status(TicketPhase::Cancelled, None)
            .await
            .expect("count cancelled");
        assert_eq!(count_cancelled, 0, "No Cancelled tickets exist");
    }

    #[tokio::test]
    async fn test_create_ticket_tool_with_prerequisites() {
        crate::util::test::init_test_stores().await;

        let store = crate::board::BOARD.get().unwrap();
        let ws = test_ws("/tmp/test_ws_tool_prereqs");

        // Create a prerequisite via the store directly
        let p_id = store
            .create_ticket(
                "Pre",
                "a prereq",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[],
                "test",
                None,
            )
            .await
            .expect("create prereq");

        let tool = crate::tools::CreateTicketTool::new("test");
        let args = serde_json::json!({
            "title": "Test with prereqs",
            "description": "depends on something",
            "prerequisites": [p_id],
        });
        let result = tool.execute(&ws, args).await.expect("execute");
        assert!(
            result.contains(&p_id),
            "Output should mention prerequisite ID"
        );
    }

    #[tokio::test]
    async fn test_supersede_and_create_basic() {
        let (store, _tmp, old_id) = setup().await;
        let ws = test_ws_named("/ws", "ws");

        // Existing ticket (created by setup)

        // Supersede it
        let new_id = store
            .supersede_and_create(&old_id, "New title", "New desc", &ws, &[], "test", None)
            .await
            .expect("supersede");

        // Old ticket is cancelled and points forward to the new ticket
        let old = store
            .get_ticket(&old_id)
            .await
            .expect("get old")
            .expect("old exists");
        assert_eq!(old.status, TicketPhase::Cancelled);
        assert!(old.assigned_to.is_none());
        assert_eq!(old.superseded_by.as_deref(), Some(new_id.as_str()));
        assert!(
            old.is_archived,
            "superseded ticket should be archived immediately"
        );

        // New ticket is in Backlog and links to old
        let new = store
            .get_ticket(&new_id)
            .await
            .expect("get new")
            .expect("new exists");
        assert_eq!(new.status, TicketPhase::Backlog);
        assert_eq!(new.supersedes.as_deref(), Some(old_id.as_str()));
        assert_eq!(new.title, "New title");
    }

    #[tokio::test]
    async fn test_supersede_rewires_only_matching_prerequisite() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        // Create ticket A (will be superseded) and ticket C (independent).
        let a_id = store
            .create_ticket("A", "old", &ws, DEFAULT_TICKET_PHASE, &[], "test", None)
            .await
            .expect("create A");
        let c_id = store
            .create_ticket("C", "other", &ws, DEFAULT_TICKET_PHASE, &[], "test", None)
            .await
            .expect("create C");

        // Create ticket B that depends on both A and C.
        let b_id = store
            .create_ticket(
                "B",
                "dep on A and C",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[a_id.clone(), c_id.clone()],
                "test",
                None,
            )
            .await
            .expect("create B");

        // Create ticket D with no prerequisites — should be untouched.
        let d_id = store
            .create_ticket("D", "no deps", &ws, DEFAULT_TICKET_PHASE, &[], "test", None)
            .await
            .expect("create D");

        // Supersede A → A2.
        let supersede_id = store
            .supersede_and_create(&a_id, "A2", "refined", &ws, &[], "test", None)
            .await
            .expect("supersede");

        // B's prerequisites: A→A2, C unchanged.
        let b = store
            .get_ticket(&b_id)
            .await
            .expect("get B")
            .expect("B exists");
        assert_eq!(b.prerequisites, vec![supersede_id.clone(), c_id.clone()]);

        // D untouched.
        let d = store
            .get_ticket(&d_id)
            .await
            .expect("get D")
            .expect("D exists");
        assert!(d.prerequisites.is_empty());
    }

    /// Table-driven tests for `supersede_and_create` with invalid inputs.
    #[tokio::test]
    async fn test_supersede_invalid_inputs() {
        enum Scenario {
            /// Supersede a nonexistent ticket.
            NonExistent,
            /// Supersede a ticket from a different workspace.
            CrossWorkspace,
            /// Supersede a ticket with a self-referencing prerequisite.
            SelfReference,
        }

        struct Case {
            name: &'static str,
            scenario: Scenario,
        }

        let cases = [
            Case {
                name: "nonexistent original",
                scenario: Scenario::NonExistent,
            },
            Case {
                name: "cross-workspace supersede",
                scenario: Scenario::CrossWorkspace,
            },
            Case {
                name: "self-referencing prerequisites",
                scenario: Scenario::SelfReference,
            },
        ];

        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");
        let ws_b = test_ws_named("/ws_b", "ws_b");

        for case in &cases {
            let expected_error = match case.scenario {
                Scenario::NonExistent => "not found",
                Scenario::CrossWorkspace => "Cross-workspace",
                Scenario::SelfReference => "supersede and depend",
            };

            let original_id = match case.scenario {
                Scenario::NonExistent => None,
                Scenario::CrossWorkspace | Scenario::SelfReference => {
                    let id = store
                        .create_ticket("A", "desc", &ws, DEFAULT_TICKET_PHASE, &[], "test", None)
                        .await
                        .expect("create original");
                    Some(id)
                }
            };

            let target_ws = match case.scenario {
                Scenario::CrossWorkspace => &ws_b,
                Scenario::NonExistent | Scenario::SelfReference => &ws,
            };
            let supersede_id: &str = original_id.as_deref().unwrap_or("nonexistent");
            // prereqs include the original id only for SelfReference.
            let prereqs: Vec<String> = match &case.scenario {
                Scenario::SelfReference => {
                    vec![
                        original_id
                            .clone()
                            .expect("original must exist for SelfReference"),
                    ]
                }
                Scenario::NonExistent | Scenario::CrossWorkspace => vec![],
            };

            let err = store
                .supersede_and_create(
                    supersede_id,
                    "New",
                    "desc",
                    target_ws,
                    &prereqs,
                    "test",
                    None,
                )
                .await
                .unwrap_err();
            assert!(
                err.to_string().contains(expected_error),
                "Case '{}': expected error containing '{}', got: {err}",
                case.name,
                expected_error
            );
        }
    }

    #[tokio::test]
    async fn test_supersede_tool() {
        crate::util::test::init_test_stores().await;

        let store = crate::board::BOARD.get().unwrap();
        let ws = test_ws("/tmp/test_ws_supersede_tool");

        // Create old ticket
        let old_id = store
            .create_ticket(
                "Old",
                "old desc",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[],
                "test",
                None,
            )
            .await
            .expect("create old");

        let tool = crate::tools::CreateTicketTool::new("test");
        let args = serde_json::json!({
            "title": "Refined",
            "description": "refined desc",
            "supersede": old_id,
        });
        let result = tool.execute(&ws, args).await.expect("execute");
        assert!(
            result.contains("Superseded"),
            "Output should say Superseded: {result}"
        );
        assert!(
            result.contains(&old_id),
            "Output should mention old ID: {result}"
        );

        // Verify old is cancelled
        let old = store
            .get_ticket(&old_id)
            .await
            .expect("get old")
            .expect("old exists");
        assert_eq!(old.status, TicketPhase::Cancelled);
        assert!(
            old.is_archived,
            "superseded ticket should be archived immediately"
        );
    }

    #[tokio::test]
    async fn test_supersede_already_cancelled() {
        let (store, _tmp, old_id) = setup().await;
        let ws = test_ws_named("/ws", "ws");

        // Cancel the ticket (created by setup)
        store
            .transition_to(&old_id, None, TicketPhase::Cancelled)
            .await
            .expect("cancel");

        // Superseding an already-cancelled ticket should work
        let new_id = store
            .supersede_and_create(&old_id, "Refined", "desc", &ws, &[], "test", None)
            .await
            .expect("supersede already-cancelled");

        let new = store
            .get_ticket(&new_id)
            .await
            .expect("get new")
            .expect("new exists");
        assert_eq!(new.supersedes.as_deref(), Some(old_id.as_str()));

        // Old ticket should also be archived immediately.
        let old = store
            .get_ticket(&old_id)
            .await
            .expect("get old")
            .expect("old exists");
        assert!(
            old.is_archived,
            "superseded ticket should be archived immediately"
        );
    }

    // ── set_commit_info ───────────────────────────────────────────────

    #[tokio::test]
    async fn test_set_commit_info() {
        // Successfully set commit info
        let (store, _tmp, id) = setup().await;

        store
            .set_commit_info(&id, "abcdef0123456789abcdef0123456789abcd0123", 10, 5)
            .await
            .expect("set commit info");

        let ticket = crate::util::test::expect_ticket(&store, &id).await;
        assert_eq!(
            ticket.commit_hash.as_deref(),
            Some("abcdef0123456789abcdef0123456789abcd0123")
        );
        assert_eq!(ticket.lines_added, Some(10));
        assert_eq!(ticket.lines_removed, Some(5));

        // Non-existent ticket fails with appropriate error
        let (store2, _tmp2) = open_test_store().await;
        let result = store2
            .set_commit_info(
                "nonexistent",
                "0000000000000000000000000000000000000000",
                0,
                0,
            )
            .await;

        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("nonexistent"),
            "error should mention ticket id: {msg}"
        );
    }

    // ── parse_prereqs unit tests ──

    #[test]
    fn test_parse_prereqs() {
        // ── Valid JSON cases ──
        let valid: &[(&str, &[&str])] = &[
            ("[]", &[] as &[&str]),
            (r#"["a","b","c"]"#, &["a", "b", "c"]),
        ];
        for (input, expected) in valid {
            let got = parse_prereqs(input).expect("should parse valid JSON");
            assert_eq!(got, *expected, "input: {input:?}");
        }

        // ── Invalid / corrupt JSON cases ──
        let invalid: &[&str] = &["", "not valid json {{{", r#"{"key":"value"}"#, "[1, 2, 3]"];
        for input in invalid {
            let err = parse_prereqs(input).unwrap_err();
            assert!(
                err.to_string().contains("Corrupt prerequisites JSON"),
                "input {input:?}: expected 'Corrupt prerequisites JSON' error, got: {err}",
            );
        }

        // ── Long ASCII input (>200 bytes) — preview truncated with ellipsis ──
        let long = format!(r#""{}...""#, "x".repeat(500));
        let msg = parse_prereqs(&long).unwrap_err().to_string();
        assert!(
            msg.contains(''),
            "long input should produce truncated preview: {msg}"
        );
        assert!(
            msg.len() < 500,
            "truncated message should be <500 chars, got len={}",
            msg.len()
        );

        // ── Multi-byte character straddling byte 200 — no panic on truncation ──
        // Without floor_char_boundary, `&raw[..200]` would panic on the mid-char slice.
        let raw = format!("{}éééééééééémore", "x".repeat(199));
        assert!(raw.len() > 200, "need raw longer than 200 chars");
        // Verify byte 200 is indeed within a multi-byte character (not a boundary).
        assert!(
            !raw.is_char_boundary(200),
            "byte 200 must be mid-character for this test to be meaningful"
        );
        let msg = parse_prereqs(&raw).unwrap_err().to_string();
        assert!(
            msg.contains(''),
            "multi-byte input should produce truncated preview: {msg}"
        );
        assert!(
            msg.len() < raw.len() + 50,
            "message too long after truncation: len={}, raw.len()={}",
            msg.len(),
            raw.len()
        );
        assert!(
            msg.contains("Corrupt prerequisites JSON"),
            "should mention corrupt JSON: {msg}"
        );
    }

    // ── Integration test: corrupt prerequisites in the database ──

    #[tokio::test]
    async fn corrupt_prerequisites_causes_get_ticket_error() {
        let (store, _tmp, id) = setup().await;

        // Directly corrupt the prerequisites column via raw SQL
        store
            .conn
            .execute(
                "UPDATE tickets SET prerequisites = ?1 WHERE id = ?2",
                crate::turso::params!["{not valid json}", id.clone()],
            )
            .await
            .expect("corrupt update");

        // get_ticket should now return an error
        let result = store.get_ticket(&id).await;
        assert!(
            result.is_err(),
            "get_ticket should fail when prerequisites are corrupt"
        );
        let err = result.unwrap_err();
        let msg = format!("{err:#}");
        assert!(
            msg.contains("Corrupt prerequisites JSON"),
            "error should mention corrupt JSON: {msg}"
        );
        assert!(
            msg.contains(&id),
            "error should include ticket ID {id}: {msg}"
        );
    }

    #[tokio::test]
    async fn corrupt_prerequisites_causes_list_all_tickets_error() {
        let (store, _tmp, id) = setup().await;
        // Corrupt its prerequisites
        store
            .conn
            .execute(
                "UPDATE tickets SET prerequisites = ?1 WHERE id = ?2",
                crate::turso::params!["garbage{{{", id.clone()],
            )
            .await
            .expect("corrupt update");

        // list_all_tickets should fail entirely
        let result = store.list_all_tickets(Some("ws"), None).await;
        assert!(
            result.is_err(),
            "list_all_tickets should fail when any ticket has corrupt prerequisites"
        );
    }

    // ── claim_diagnostics tests ──

    /// Table-driven tests for `claim_diagnostics` covering success,
    /// pre-assignment rejection, wrong-phase rejection, and idempotency.
    #[tokio::test]
    async fn test_claim_diagnostics() {
        #[allow(clippy::struct_excessive_bools)]
        struct Case {
            name: &'static str,
            move_to_diagnostics: bool,
            pre_assigned: bool,
            expected_claim: bool,
            check_idempotent: bool,
        }

        let cases = [
            Case {
                name: "unassigned in diagnostics succeeds",
                move_to_diagnostics: true,
                pre_assigned: false,
                expected_claim: true,
                check_idempotent: true,
            },
            Case {
                name: "already assigned fails",
                move_to_diagnostics: true,
                pre_assigned: true,
                expected_claim: false,
                check_idempotent: false,
            },
            Case {
                name: "wrong phase fails",
                move_to_diagnostics: false,
                pre_assigned: false,
                expected_claim: false,
                check_idempotent: false,
            },
        ];

        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        for (i, case) in cases.iter().enumerate() {
            let title = format!("claim-{i}");
            let id = store
                .create_ticket(&title, "desc", &ws, TicketPhase::Backlog, &[], "test", None)
                .await
                .expect("create_ticket");

            if case.move_to_diagnostics {
                store
                    .transition_to(&id, None, TicketPhase::InDiagnostics)
                    .await
                    .expect("transition to InDiagnostics");
            }
            if case.pre_assigned {
                store
                    .set_assigned_to(&id, Some("diagnostics"))
                    .await
                    .expect("set_assigned_to");
            }

            let claimed = store
                .claim_diagnostics(&id)
                .await
                .expect("claim_diagnostics");
            assert_eq!(
                claimed, case.expected_claim,
                "Case '{}': unexpected claim result",
                case.name
            );

            if case.expected_claim {
                let ticket = crate::util::test::expect_ticket(&store, &id).await;
                assert_eq!(
                    ticket.assigned_to.as_deref(),
                    Some("diagnostics"),
                    "Case '{}': assignee should be set",
                    case.name
                );
                assert_eq!(
                    ticket.status,
                    TicketPhase::InDiagnostics,
                    "Case '{}': status should remain InDiagnostics",
                    case.name
                );
            }

            if case.check_idempotent {
                let second = store.claim_diagnostics(&id).await.expect("second claim");
                assert!(
                    !second,
                    "Case '{}': second claim should return false (idempotent)",
                    case.name
                );
            }
        }
    }

    #[tokio::test]
    async fn test_set_assigned_to_none() {
        // Successfully clear an assigned assignee
        let (store, _tmp, id) = setup().await;

        store
            .set_assigned_to(&id, Some("diagnostics"))
            .await
            .expect("set_assigned_to");
        let ticket = crate::util::test::expect_ticket(&store, &id).await;
        assert_eq!(ticket.assigned_to.as_deref(), Some("diagnostics"));

        store
            .set_assigned_to(&id, None)
            .await
            .expect("set_assigned_to(None) should clear assignee");
        let ticket = crate::util::test::expect_ticket(&store, &id).await;
        assert!(ticket.assigned_to.is_none(), "assigned_to should be NULL");

        // Idempotent: clearing an already-None assignee succeeds
        store
            .set_assigned_to(&id, None)
            .await
            .expect("second set_assigned_to(None) should also succeed");

        // Non-existent ticket fails
        let (store2, _tmp2) = open_test_store().await;
        let result = store2.set_assigned_to("nonexistent", None).await;
        assert!(
            result.is_err(),
            "set_assigned_to(None) on nonexistent ticket should fail"
        );
    }

    /// Round-trip test that exercises ALL column-index constants in
    /// [`ticket_from_row`] by creating a ticket, setting every mutable field
    /// via public API, then verifying every [`Ticket`] field survives the
    /// SELECT → `ticket_from_row` deserialization path.
    ///
    /// Catches silent field mapping bugs where [`TICKET_COLUMNS`] column order
    /// drifts from the [`COL_TICKET_*`] integer constants — the same class of
    /// bug that previously caused the stale doc comment on this module.
    #[tokio::test]
    async fn test_ticket_roundtrip_all_fields() {
        let (store, _tmp) = open_test_store().await;
        let ws = crate::workspace::test_ws_named("/test_ws", "test_workspace");

        // Create ticket with known values for every TICKET_COLUMNS position.
        let id = store
            .create_ticket(
                "Roundtrip Title",
                "Roundtrip description",
                &ws,
                TicketPhase::Backlog,
                &[],
                "test_reporter",
                None,
            )
            .await
            .expect("create_ticket");

        // Set assigned_to (exercises COL_TICKET_ASSIGNED_TO with non-None value).
        store
            .set_assigned_to(&id, Some("test_assignee"))
            .await
            .expect("set_assigned_to");

        // Set commit_hash, lines_added, lines_removed with non-default values.
        store
            .set_commit_info(&id, "abcdef0123456789abcdef0123456789abcd0123", 42, 7)
            .await
            .expect("set_commit_info");

        // Read back BEFORE archiving (which clears assigned_to).
        let ticket = store
            .get_ticket(&id)
            .await
            .expect("get_ticket")
            .expect("ticket exists");

        // ── Assert every Ticket field round-trips ──────────────────────
        assert_eq!(ticket.id, id, "id mismatch");
        assert_eq!(ticket.title, "Roundtrip Title", "title mismatch");
        assert_eq!(
            ticket.description, "Roundtrip description",
            "description mismatch",
        );
        assert_eq!(ticket.status, TicketPhase::Backlog, "status mismatch");
        assert_eq!(
            ticket.assigned_to.as_deref(),
            Some("test_assignee"),
            "assigned_to should round-trip",
        );
        assert_eq!(ticket.workspace_name, "test_workspace");
        // Timestamps are auto-generated RFC 3339 — validate format, not value.
        assert!(
            ticket.created_at.contains('T'),
            "created_at should be RFC 3339: {}",
            ticket.created_at,
        );
        assert!(
            ticket.updated_at.contains('T'),
            "updated_at should be RFC 3339: {}",
            ticket.updated_at,
        );
        assert!(ticket.comments.is_empty(), "no comments expected");
        assert!(
            ticket.prerequisites.is_empty(),
            "prerequisites should round-trip as empty",
        );
        assert_eq!(
            ticket.commit_hash.as_deref(),
            Some("abcdef0123456789abcdef0123456789abcd0123"),
            "commit_hash mismatch",
        );
        assert_eq!(ticket.lines_added, Some(42), "lines_added mismatch");
        assert_eq!(ticket.lines_removed, Some(7), "lines_removed mismatch");
        assert_eq!(ticket.reporter, "test_reporter", "reporter mismatch");
        // Fields not set remain at their defaults.
        assert!(
            ticket.supersedes.is_none(),
            "supersedes should be None for simple ticket",
        );
        assert!(
            ticket.superseded_by.is_none(),
            "superseded_by should be None for simple ticket",
        );
        assert!(
            !ticket.is_archived,
            "is_archived should be false before archiving",
        );

        // ── Exercise is_archived i64→bool conversion ──────────────────
        // set_archived flips is_archived to 1 in SQL, which exercises the
        // conversion: row.get::<i64>()? != 0.
        store.set_archived(&id).await.expect("set_archived");

        let archived = store
            .get_ticket(&id)
            .await
            .expect("get_ticket")
            .expect("ticket exists after archive");
        assert!(
            archived.is_archived,
            "is_archived should be true after set_archived"
        );
        assert!(
            archived.assigned_to.is_none(),
            "assigned_to should be cleared after archive",
        );
    }

    // ── Archived ticket search methods ──────────────────────────────────

    /// Create an archived ticket with the given title in tests.
    async fn create_archived_ticket(
        store: &super::BoardStore,
        title: &str,
        workspace_name: &str,
    ) -> String {
        let ws = test_ws(workspace_name);
        let id = store
            .create_ticket(
                title,
                "desc",
                &ws,
                crate::board::TicketPhase::Done,
                &[],
                "test",
                None,
            )
            .await
            .expect("create_ticket");
        store.set_archived(&id).await.expect("set_archived");
        id
    }

    #[tokio::test]
    async fn test_search_archived_by_fts_finds_matching_title() {
        let (store, _tmp) = open_test_store().await;
        let id = create_archived_ticket(&store, "Fix network timeout bug", "ws1").await;

        let results = store
            .search_archived_by_fts("network timeout", 10)
            .await
            .expect("FTS search");
        assert!(!results.is_empty(), "should find the ticket");
        let ids: Vec<&str> = results.iter().map(|(id, _)| id.as_str()).collect();
        assert!(
            ids.contains(&id.as_str()),
            "result should contain our ticket"
        );
    }

    #[tokio::test]
    async fn test_search_archived_by_fts_excludes_non_archived() {
        let (store, _tmp) = open_test_store().await;
        // Create a non-archived ticket — should not appear in archived search
        let ws = test_ws("ws2");
        store
            .create_ticket(
                "Still active",
                "desc",
                &ws,
                crate::board::TicketPhase::Backlog,
                &[],
                "test",
                None,
            )
            .await
            .expect("create_ticket");

        let results = store
            .search_archived_by_fts("active", 10)
            .await
            .expect("FTS search");
        assert!(results.is_empty(), "non-archived ticket should not appear");
    }

    #[tokio::test]
    async fn test_search_archived_by_fts_sanitize_mangles_query() {
        let (store, _tmp) = open_test_store().await;
        let results = store
            .search_archived_by_fts("!@#$%", 10)
            .await
            .expect("FTS search");
        assert!(
            results.is_empty(),
            "query with only special chars becomes empty after sanitize"
        );
    }

    #[tokio::test]
    async fn test_list_archived_with_embeddings_empty_when_no_tickets() {
        let (store, _tmp) = open_test_store().await;
        let candidates = store.list_archived_with_embeddings().await.expect("list");
        assert!(candidates.is_empty(), "no tickets at all");
    }

    /// Tests for `list_tickets_minimal`: found by ID, nonexistent IDs omitted,
    /// empty-ids early-return guard, and input order preservation.
    #[tokio::test]
    async fn test_list_tickets_minimal() {
        let (store, _tmp) = open_test_store().await;

        // Create 3 archived tickets in a shared store.
        let id_a = create_archived_ticket(&store, "Alpha", "ws").await;
        let id_b = create_archived_ticket(&store, "Beta", "ws").await;
        let id_c = create_archived_ticket(&store, "Gamma", "ws").await;

        // 1. Found by ID.
        let rows = store
            .list_tickets_minimal(std::slice::from_ref(&id_a))
            .await
            .expect("list minimal");
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].0, id_a);
        assert_eq!(rows[0].1, "Alpha");
        assert_eq!(rows[0].2, "done");

        // 2. Nonexistent IDs omitted.
        let rows = store
            .list_tickets_minimal(&[id_a.clone(), "nonexistent".to_string()])
            .await
            .expect("list minimal");
        assert_eq!(rows.len(), 1, "nonexistent IDs should be omitted");
        assert_eq!(rows[0].0, id_a);

        // 3. Empty ids returns empty (early-return guard).
        let rows: Vec<(String, String, String)> = store
            .list_tickets_minimal(&[] as &[String])
            .await
            .expect("list minimal");
        assert!(rows.is_empty(), "empty ids should return empty results");

        // 4. Preserves input order.
        let rows = store
            .list_tickets_minimal(&[id_c.clone(), id_a.clone(), id_b.clone()])
            .await
            .expect("list minimal");
        assert_eq!(rows.len(), 3);
        assert_eq!(rows[0].0, id_c, "first result should be Gamma");
        assert_eq!(rows[1].0, id_a, "second result should be Alpha");
        assert_eq!(rows[2].0, id_b, "third result should be Beta");
    }

    /// Basic field layout of `detailed_display`: fields present, negative
    /// assertions for absent fields, and "(no comments)" when empty.
    #[tokio::test]
    async fn test_detailed_display_basic() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/test-workspace", "test-ws");

        let prereq_id = store
            .create_ticket(
                "Prereq",
                "prereq desc",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[],
                "test",
                None,
            )
            .await
            .expect("create prereq");

        let id = store
            .create_ticket(
                "Display Test Ticket",
                "A description for testing",
                &ws,
                TicketPhase::InDevelopment,
                std::slice::from_ref(&prereq_id),
                "manager",
                None,
            )
            .await
            .expect("create");

        let ticket = store.get_ticket(&id).await.expect("get").expect("exists");
        let display = ticket.detailed_display();

        assert!(
            display.contains(&format!("Ticket: {id}")),
            "should contain ticket id"
        );
        assert!(
            display.contains("Title: Display Test Ticket"),
            "should contain title"
        );
        assert!(
            display.contains("Description: A description for testing"),
            "should contain description"
        );
        assert!(
            display.contains("Status: in_development"),
            "should use snake_case status"
        );
        assert!(
            display.contains("Reporter: manager"),
            "should contain reporter"
        );
        assert!(
            display.contains("Workspace: test-ws"),
            "should contain workspace"
        );
        assert!(
            display.contains("Created:"),
            "should contain created timestamp"
        );
        assert!(
            display.contains("Updated:"),
            "should contain updated timestamp"
        );
        assert!(
            display.contains(&format!("Prerequisites: {prereq_id}")),
            "should show prerequisites"
        );
        assert!(
            display.contains("Comments:"),
            "should have comments section"
        );
        assert!(display.contains("(no comments)"), "should show no comments");

        // Fields that should NOT appear when unset
        assert!(
            !display.contains("Supersedes:"),
            "no supersedes when not set"
        );
        assert!(
            !display.contains("Superseded by:"),
            "no superseded_by when not set"
        );
        assert!(
            !display.contains("Archived:"),
            "no archived line when false"
        );
        assert!(
            !display.contains("assigned_to:"),
            "assigned_to should not be displayed"
        );
        assert!(
            !display.contains("commit_hash:"),
            "commit_hash should not be displayed"
        );
        assert!(
            !display.contains("lines_added:"),
            "lines_added should not be displayed"
        );
        assert!(
            !display.contains("lines_removed:"),
            "lines_removed should not be displayed"
        );
    }

    /// `detailed_display` with comments (role labels, content) and multiple
    /// prerequisites joined by comma+space.
    #[allow(clippy::too_many_lines)]
    #[tokio::test]
    async fn test_detailed_display_with_content() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/test-workspace", "test-ws");

        // ── Comment formatting: two comments with different roles ──

        let id = store
            .create_ticket(
                "Comment Test",
                "desc",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[],
                "test",
                None,
            )
            .await
            .expect("create");

        store
            .add_comment(&id, Role::Analyst.as_str(), "First comment")
            .await
            .expect("add_comment");
        store
            .add_comment(&id, Role::Reviewer.as_str(), "Second comment")
            .await
            .expect("add_comment");

        let ticket = store.get_ticket(&id).await.expect("get").expect("exists");
        let display = ticket.detailed_display();

        assert!(
            display.contains("Comments:"),
            "should have comments section"
        );
        assert!(display.contains("[analyst]"), "should show analyst role");
        assert!(display.contains("[reviewer]"), "should show reviewer role");
        assert!(
            display.contains("First comment"),
            "should show first comment"
        );
        assert!(
            display.contains("Second comment"),
            "should show second comment"
        );
        assert!(
            !display.contains("(no comments)"),
            "should not say 'no comments' when comments exist"
        );

        // ── Multiple prerequisites: all three joined by comma+space ──

        let pre_a = store
            .create_ticket(
                "Pre-A",
                "desc",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[],
                "test",
                None,
            )
            .await
            .expect("create pre-a");
        let pre_b = store
            .create_ticket(
                "Pre-B",
                "desc",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[],
                "test",
                None,
            )
            .await
            .expect("create pre-b");
        let pre_c = store
            .create_ticket(
                "Pre-C",
                "desc",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[],
                "test",
                None,
            )
            .await
            .expect("create pre-c");

        let multi_id = store
            .create_ticket(
                "Multi prereq",
                "desc",
                &ws,
                DEFAULT_TICKET_PHASE,
                &[pre_a.clone(), pre_b.clone(), pre_c.clone()],
                "test",
                None,
            )
            .await
            .expect("create");

        let ticket = store
            .get_ticket(&multi_id)
            .await
            .expect("get")
            .expect("exists");
        let display = ticket.detailed_display();

        assert!(
            display.contains(&format!("Prerequisites: {pre_a}, {pre_b}, {pre_c}")),
            "should show all prerequisites joined with comma+space"
        );
    }

    /// `detailed_display` for supersedes chains: new ticket shows Supersedes,
    /// old ticket shows Superseded by + Archived.
    #[tokio::test]
    async fn test_detailed_display_supersedes_chain() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws_named("/ws", "ws");

        // Create an old ticket first
        let old_id = store
            .create_ticket(
                "Old ticket",
                "old desc",
                &ws,
                TicketPhase::Backlog,
                &[],
                "test",
                None,
            )
            .await
            .expect("create old");

        // Supersede it — new ticket gets supersedes = old_id, old ticket gets
        // superseded_by = new_id and is archived.
        let new_id = store
            .supersede_and_create(&old_id, "New ticket", "new desc", &ws, &[], "test", None)
            .await
            .expect("supersede");

        // Check the new ticket shows Supersedes
        let new_ticket = store
            .get_ticket(&new_id)
            .await
            .expect("get")
            .expect("exists");
        let new_display = new_ticket.detailed_display();
        assert!(
            new_display.contains(&format!("Supersedes: {old_id}")),
            "new ticket should show Supersedes: old_id"
        );

        // Check the old ticket shows Superseded by + Archived
        let old_ticket = store
            .get_ticket(&old_id)
            .await
            .expect("get")
            .expect("exists");
        let old_display = old_ticket.detailed_display();
        assert!(
            old_display.contains(&format!("Superseded by: {new_id}")),
            "old ticket should show Superseded by: new_id"
        );
        assert!(
            old_display.contains("Archived: yes"),
            "old ticket should be archived"
        );
    }

    #[tokio::test]
    async fn test_list_archived_with_embeddings_returns_deserialized() {
        let (store, _tmp) = open_test_store().await;
        let ws = test_ws("ws");

        // Create a ticket with a known embedding blob (two small f32s)
        let embedding: Vec<f32> = vec![1.0, 2.0];
        let blob: Vec<u8> = embedding.iter().flat_map(|f| f.to_le_bytes()).collect();

        let id = store
            .create_ticket(
                "Embedded ticket",
                "desc",
                &ws,
                crate::board::TicketPhase::Done,
                &[],
                "test",
                Some(&blob),
            )
            .await
            .expect("create_ticket with embedding");
        store.set_archived(&id).await.expect("archive");

        let candidates = store.list_archived_with_embeddings().await.expect("list");
        assert_eq!(candidates.len(), 1);
        assert_eq!(candidates[0].0, id);
        assert_eq!(candidates[0].1, vec![1.0, 2.0]);
    }
}