mahbot 0.9.0

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

use crate::agent::{run_agent, run_default_agent};
use crate::prompt::{load_prompt, load_prompt_sections, substitute};
use crate::tools::Tool;
use crate::{Agent, ChatMessage, DEFAULT_MAX_TOKENS, Role, Workspace};
use anyhow::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::fmt::Write as _;
use std::time::Duration;

// ── Constants (module-local defaults) ────────────────────────────────────

/// Number of decorrelated parallel analysts spawned per analyze round (durable and
/// sync pipelines share the same batch width).
const PARALLEL_ANALYST_COUNT: usize = 2;

/// Controls sub-agent dispatch behaviour.
///
/// [`Sync`](DispatchMode::Sync) blocks the caller until the sub-agent completes.
/// [`Async`](DispatchMode::Async) dispatches the sub-agent in a background task
/// and injects the result via the caller's agent queue.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum DispatchMode {
    Sync,
    Async,
}

impl DispatchMode {
    /// Returns `true` when this dispatch mode is [`Async`](DispatchMode::Async).
    #[must_use]
    pub const fn is_async(self) -> bool {
        matches!(self, Self::Async)
    }
}

/// How deep an `analyze` call goes: `quick` is one round of parallel analysts;
/// `deep` is the multi-round deep investigation, whose report arrives hours
/// later. The order of [`Self::ALL`] is the interface order — the narrowest
/// kind is first.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum AnalyzeDepth {
    Quick,
    Deep,
}

impl AnalyzeDepth {
    /// The argument naming the choice.
    pub const ARG: &'static str = "depth";

    /// Every depth, in interface order (narrowest first).
    pub const ALL: [Self; 2] = [Self::Quick, Self::Deep];

    /// The wire spellings of [`Self::ALL`], in the same order.
    pub const STRS: &'static [&'static str] = &[Self::Quick.as_str(), Self::Deep.as_str()];

    /// The wire spelling of this depth.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Quick => "quick",
            Self::Deep => "deep",
        }
    }

    /// Parse a wire spelling.
    #[must_use]
    pub fn parse(value: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|depth| depth.as_str() == value)
    }
}

/// The cancel action's argument spellings — the interface's second axis,
/// declared only where an asynchronous quick round can be dispatched
/// ([`AnalyzeTool::cancels`]).
pub(crate) const ACTION_ARG: &str = "action";
/// The round the cancel action addresses — the id the dispatch acknowledgement
/// carried.
pub(crate) const JOB_ID_ARG: &str = "job_id";
/// The `action` value that selects the cancel action.
const ACTION_CANCEL: &str = "cancel";

pub struct AnalyzeTool {
    /// Controls how the sub-agents are dispatched.
    /// - [`DispatchMode::Sync`] — blocks the caller until the sub-agents complete.
    /// - [`DispatchMode::Async`] — dispatches in a background task, result
    ///   delivered via the caller's agent queue.
    dispatch_mode: DispatchMode,
    /// The role of the calling agent. Used to route async results to the
    /// correct agent channel (Manager → manager_{ws}, Assistant → direct_{...}).
    pub caller_role: Role,
    /// The depths this holder may call, in interface order. A single depth is
    /// never declared as a choice argument.
    kinds: Vec<AnalyzeDepth>,
    /// Whether the optional `place` argument is accepted — the admin Assistant
    /// and any custom role holding `quick`. Every other holder keeps the
    /// argument-less interface.
    accepts_place: bool,
}

impl AnalyzeTool {
    /// The quick-only, no-`place` variant: Engineer, Manager, Maintainer and a
    /// guest Assistant.
    #[must_use]
    pub fn new(dispatch_mode: DispatchMode, caller_role: Role) -> Self {
        Self {
            dispatch_mode,
            caller_role,
            kinds: vec![AnalyzeDepth::Quick],
            accepts_place: false,
        }
    }

    /// The admin Assistant's variant: both depths and the optional `place`
    /// argument (honoured for `quick` only).
    ///
    /// Always the async dispatch — a place can only be honoured by a durable
    /// background round, which records the directory it runs in for resume.
    #[must_use]
    pub fn admin(caller_role: Role) -> Self {
        Self {
            dispatch_mode: DispatchMode::Async,
            caller_role,
            kinds: vec![AnalyzeDepth::Quick, AnalyzeDepth::Deep],
            accepts_place: true,
        }
    }

    /// A custom role's variant: exactly the granted depths, always async, with
    /// `place` offered iff `quick` is held (deep ignores it). Never empty — a
    /// grant entry that named no kind takes the narrowest kind (`quick`).
    #[must_use]
    pub fn granted(kinds: &[AnalyzeDepth]) -> Self {
        let kinds = if kinds.is_empty() {
            vec![AnalyzeDepth::Quick]
        } else {
            kinds.to_vec()
        };
        let accepts_place = kinds.contains(&AnalyzeDepth::Quick);
        Self {
            dispatch_mode: DispatchMode::Async,
            caller_role: Role::Custom,
            kinds,
            accepts_place,
        }
    }

    /// Whether this holder is offered the cancel action. Only a holder that can
    /// dispatch an asynchronous `quick` round can — the Manager, the Assistant
    /// and a custom role granted `quick`. A sync holder (Engineer, Maintainer)
    /// and a `deep`-only holder dispatch work this action deliberately does not
    /// stop, so it is not offered to them.
    #[must_use]
    fn cancels(&self) -> bool {
        self.dispatch_mode.is_async() && self.kinds.contains(&AnalyzeDepth::Quick)
    }
}

#[async_trait]
impl Tool for AnalyzeTool {
    fn name(&self) -> &'static str {
        "analyze"
    }

    /// Compose the description from shared fragments: the base text, the quick
    /// note when `quick` is held, the depth note for what this holder can call,
    /// the optional-`place` note, the async note, and — only where the cancel
    /// action is offered — the cancel note. The deep note always carries the
    /// long-running warning and the scope-confirmation requirement.
    fn description(&self) -> String {
        let mut parts = vec![load_prompt("tool/analyze.md")];
        if self.kinds.contains(&AnalyzeDepth::Quick) {
            parts.push(load_prompt("tool/analyze_quick.md"));
        }
        if self.kinds.contains(&AnalyzeDepth::Deep) {
            let note = if self.kinds.contains(&AnalyzeDepth::Quick) {
                "tool/analyze_depth.md"
            } else {
                "tool/analyze_deep.md"
            };
            parts.push(load_prompt(note));
        }
        if self.accepts_place {
            parts.push(load_prompt("tool/analyze_place.md"));
        }
        if self.dispatch_mode.is_async() {
            parts.push(load_prompt("tool/analyze_async.md"));
        }
        if self.cancels() {
            parts.push(load_prompt("tool/analyze_cancel.md"));
        }
        parts.join("\n\n")
    }

    fn parameters_schema(&self) -> serde_json::Value {
        let mut properties = json!({
            "message": {
                "type": "string",
                "description": "The question for the analysts"
            }
        });
        let mut dispatch = vec!["message"];
        if self.kinds.len() > 1 {
            let choices: Vec<&str> = self.kinds.iter().map(|depth| depth.as_str()).collect();
            properties[AnalyzeDepth::ARG] = json!({
                "type": "string",
                "enum": choices,
                "description": "How deep to go; the matching note says what each value does."
            });
            dispatch.push(AnalyzeDepth::ARG);
        }
        if self.accepts_place {
            // The interface declares the per-choice requirement — `deep` never
            // takes the folder — so only a holder that has the `depth` argument
            // is told about it. How the folder is spelled lives in the note.
            let mut description = "Optional folder this round works in.".to_string();
            if self.kinds.contains(&AnalyzeDepth::Deep) {
                description.push_str(" `deep` ignores it.");
            }
            properties["place"] = json!({
                "type": "string",
                "description": description
            });
        }
        if !self.cancels() {
            return super::tool_params_schema(&properties, &dispatch);
        }
        // The cancel action is a second axis, not a second tool: it is declared
        // beside the dispatch arguments and validated on the call. A cancel call
        // carries no dispatch argument, so the requirement is a disjunction —
        // `message` (+ `depth`) to dispatch, `action` + `job_id` to cancel —
        // which the flat `required` envelope cannot express (built like
        // `web_search`'s two-branch schema).
        properties[ACTION_ARG] = json!({
            "type": "string",
            "enum": [ACTION_CANCEL],
            "description": "Set to `cancel` to stop one of the rounds you dispatched, named by `job_id`, instead of dispatching a new one."
        });
        properties[JOB_ID_ARG] = json!({
            "type": "string",
            "description": "The round to cancel — the id from its dispatch acknowledgement."
        });
        let mut schema = super::tool_params_schema(&properties, &[]);
        schema["oneOf"] = json!([
            { "required": dispatch },
            { "required": [ACTION_ARG, JOB_ID_ARG] },
        ]);
        schema
    }

    /// A dispatch itself spends nothing the workspace can observe — the analysts
    /// are read-only (see `Role::tools()` for when even the read-only shell is
    /// withheld), and this classification is coupled to it: if Analyst ever gains
    /// side-effecting tools, this must be reconsidered. A cancel call does delete
    /// the round's rows, but it can only name a round whose acknowledgement an
    /// EARLIER tool round returned, so it never acts on a round dispatched beside
    /// it — staying read-only keeps several dispatches of one tool round running
    /// in parallel.
    fn side_effects(&self) -> bool {
        false
    }

    /// The consolidated analysis (grouped claims + optional verification
    /// section) must reach the calling agent in full — sandwich-truncating it
    /// would silently drop findings and verification verdicts. The analyze
    /// output is bounded by the analyst round itself, never by the shared
    /// tool-output budget.
    fn preserve_full_output(&self) -> bool {
        true
    }

    async fn execute(&self, ws: &Workspace, args: serde_json::Value) -> Result<String> {
        // The cancel action is read before anything else: it takes neither the
        // message, nor the depth, nor the place, so a cancel is never refused
        // for a dispatch argument it does not carry.
        if self.cancels() {
            match super::get_opt_str_strict(&args, ACTION_ARG)? {
                None => {}
                Some(action) if action == ACTION_CANCEL => {
                    let job_id = super::get_str(&args, JOB_ID_ARG)?.trim();
                    anyhow::ensure!(
                        !job_id.is_empty(),
                        "usage: `{JOB_ID_ARG}` must name the round to cancel"
                    );
                    // Ownership is the calling SESSION: the round's own delivery
                    // address is compared against it, and the user is not part of
                    // it — a workspace-scoped Manager session is driven by
                    // pipeline ticket notifications, whose turns carry no user at
                    // all (`tool_identity` would refuse exactly those turns).
                    let caller = crate::agent::tool_agent_id()?;
                    let outcome = crate::analyze_cancel::cancel_round(job_id, &caller).await?;
                    return Ok(crate::analyze_cancel::answer(&outcome, job_id));
                }
                Some(action) => {
                    return Err(super::refuse_choice(ACTION_ARG, action, &[ACTION_CANCEL]));
                }
            }
        }
        let message = super::get_dispatch_message(&args)?;
        let depth = super::chosen_kind(
            &args,
            AnalyzeDepth::ARG,
            &self.kinds,
            AnalyzeDepth::as_str,
            AnalyzeDepth::parse,
        )?;

        match depth {
            AnalyzeDepth::Quick => {
                // The optional `place` only affects a quick round, and only a
                // durable background round can honour it — resolved here, where
                // it is used. A refused place dispatches nothing and never falls
                // back to the caller's own workspace.
                let place = if self.accepts_place {
                    super::delegation::resolve(super::delegation::DelegationKind::Analyze, &args)
                        .await?
                } else {
                    None
                };
                if self.dispatch_mode.is_async() {
                    let job_id = crate::generate_id();
                    super::SyncDurableCore::Analyze.spawn_dispatch(
                        ws,
                        place.as_ref(),
                        message,
                        self.caller_role,
                        job_id.clone(),
                    );
                    return Ok(format!(
                        "Analysis dispatched (job {job_id}). The consolidated answer follows \
                         as an <analyze-tool-result> message unless you cancel the round first."
                    ));
                }
                // Sync path — blocks the caller until the analysts complete.
                // (a place is impossible here: `admin()`/`granted()` are
                // async-only.)
                super::SyncDurableCore::Analyze
                    .run_sync_dispatch(ws, message, self.caller_role)
                    .await
            }
            AnalyzeDepth::Deep => Ok(super::research::dispatch_deep_analysis(
                ws,
                message,
                self.caller_role,
            )),
        }
    }
}

/// One durable analyze-round roster slot (pre-generated agent id + final task).
struct AnalyzeSlot {
    agent_id: String,
    task: String,
}

/// Spawn the analyze job + roster (one tx), run the analysts, checkpoint per-agent
/// outcomes, consolidate. `pre_done` carries slots already completed (resume)
/// with their stored raw-response outcomes.
#[expect(clippy::too_many_lines)]
pub(crate) async fn run_analyze_with_job(
    ws: &Workspace,
    message: &str,
    args: crate::tools::CoreJobArgs<'_>,
) -> anyhow::Result<crate::tools::SyncCoreOutcome> {
    let crate::tools::CoreJobArgs {
        job_id,
        caller_role,
        user_name,
        channel,
        spawn,
        caller_agent_id,
        fail_on_checkpoint_error,
    } = args;
    let resume = spawn.is_none();
    let deadline = std::time::Instant::now() + round_timeout();
    // The signal may have fired before the round started: the caller was already
    // answered and the round's rows swept, so stop before writing anything.
    if crate::round_cancel::is_cancelled(job_id) {
        return Ok(crate::tools::SyncCoreOutcome::Cancelled);
    }

    let (slots, pre_done) = match &spawn {
        // Resume: reuse the stored roster (agent ids + final tasks) — never
        // regenerate ids (the PK would conflict AND the new ids would not match
        // the stored roster rows).
        None => {
            let rows =
                crate::jobs::list_agents_for_job(&crate::session::store().conn, job_id).await?;
            let slots: Vec<AnalyzeSlot> = rows
                .iter()
                .map(|r| AnalyzeSlot {
                    agent_id: r.agent_id.clone(),
                    task: r.task.clone(),
                })
                .collect();
            // Done slots are reconstructed from their stored outcomes; the rest
            // re-run with their stored tasks.
            let pre_done: Vec<(String, String)> = rows
                .iter()
                .filter(|r| r.status == crate::jobs::RowStatus::Done.as_str())
                .filter_map(|r| r.outcome.clone().map(|o| (r.agent_id.clone(), o)))
                .collect();
            (slots, pre_done)
        }
        // Fresh dispatch: build the roster and spawn the job BEFORE any analyst
        // session write (caller identity persisted on the job row so a later
        // resume delivers to the ORIGINAL caller).
        Some(spawn) => {
            let suffix = crate::generate_suffix();
            let angles = load_analyst_angles();
            let mut slots: Vec<AnalyzeSlot> = Vec::with_capacity(PARALLEL_ANALYST_COUNT);
            let mut agents: Vec<crate::jobs::NewAgent> = Vec::with_capacity(PARALLEL_ANALYST_COUNT);
            for i in 0..PARALLEL_ANALYST_COUNT {
                let slot = analyst_slot(ws, &angles, &suffix, i, message);
                agents.push(crate::jobs::NewAgent {
                    agent_id: slot.agent_id.clone(),
                    kind: crate::jobs::AgentKind::Analyst,
                    idx: Some(i64::try_from(i).unwrap_or(i64::MAX)),
                    task: slot.task.clone(),
                });
                slots.push(slot);
            }
            crate::jobs::spawn_job(
                &crate::session::store().conn,
                job_id,
                message,
                spawn.delivery_name,
                user_name,
                channel,
                caller_role,
                &agents,
                &crate::jobs::SpawnChild::Analyze,
                caller_agent_id,
                // The round's execution directory (the caller's place, when one
                // was asked for) so a restart resumes into the same directory.
                spawn.exec_dir,
                // The calling custom role's launch, when there is one, so a
                // resume rebuilds an envelope addressed to its branch.
                spawn.custom_launch,
            )
            .await?;
            // The cancel can have landed while the roster was built, or between
            // that and this INSERT's commit. The gate inside `spawn_job`'s tx
            // refuses the row in that case, so this only turns the stop into the
            // round's outcome — a cancelled round is never resumed either way.
            if crate::round_cancel::is_cancelled(job_id) {
                return Ok(crate::tools::SyncCoreOutcome::Cancelled);
            }
            (slots, Vec::new())
        }
    };

    // Run the launched slots; reconstruct done slots from stored outcomes.
    let fresh_slots: Vec<&AnalyzeSlot> = slots
        .iter()
        .filter(|s| !pre_done.iter().any(|(id, _)| id == &s.agent_id))
        .collect();
    // Round key = the durable job_id — stable across resume so a resumed
    // round's analysts and consolidation call group under the same key.
    let fresh_runs = run_analyze_slots(ws, &fresh_slots, deadline, resume, job_id, message).await;

    // The cancel landed while the analysts ran: they were cancelled as a group,
    // so their runs already carry the stopped outcome — the partial findings are
    // never turned into a result, with no checkpoint write, extraction or
    // consolidation.
    if crate::round_cancel::is_cancelled(job_id) {
        return Ok(crate::tools::SyncCoreOutcome::Cancelled);
    }

    // Assemble runs in slot order.
    let mut runs: Vec<AnalyzeRun> = Vec::with_capacity(slots.len());
    let mut fresh_iter = fresh_runs.into_iter();
    for slot in &slots {
        if let Some((_, raw)) = pre_done.iter().find(|(id, _)| id == &slot.agent_id) {
            // Done slot — reconstruct the run from the stored response. The
            // fresh agent pre-loads the analyst's persisted session so
            // extract_findings keeps KV-cache compatibility.
            let mut agent = crate::Agent::new(
                slot.agent_id.clone(),
                crate::Role::Analyst,
                ws,
                None,
                String::new(),
                String::new(),
                false,
                Some(crate::agent::registry::ParentKey::AnalyzeRound(
                    job_id.to_string(),
                )),
                Some(message.to_string()),
                None,
            );
            let _ = agent.session.init(&slot.agent_id).await;
            runs.push(AnalyzeRun::settle(agent, Some(raw.clone())));
        } else if let Some(run) = fresh_iter.next() {
            runs.push(run);
        } else {
            unreachable!("every fresh slot resolves to exactly one run (1:1 with fresh_slots)");
        }
    }

    // CHECKPOINT: per-agent outcomes (raw response or collapsed reason).
    let conn = &crate::session::store().conn;
    for (slot, run) in slots.iter().zip(&runs) {
        let (status, outcome) = match run {
            AnalyzeRun::Completed { response, .. } => (crate::jobs::RowStatus::Done, response),
            AnalyzeRun::Failed { reason } => (crate::jobs::RowStatus::Failed, reason),
        };
        if let Err(e) = crate::jobs::write_agent_outcome(
            conn,
            job_id,
            &slot.agent_id,
            status,
            Some(outcome.as_str()),
        )
        .await
        {
            // Sync calls fail the tool call on a checkpoint DB error so the
            // model retries; async/boot-resume warn-and-continue (the outcomes
            // are recomputable on the next resume).
            if fail_on_checkpoint_error {
                return Err(e.context("failed to checkpoint analyze outcome"));
            }
            tracing::warn!(job = %job_id, error = %e, "Failed to checkpoint analyze outcome");
        }
    }

    // Drain/shutdown cut the round mid-flight: skip the consolidate LLM call
    // (no new LLM work during the drain) — the checkpointed outcomes are
    // reused by the next boot's resume; the caller leaves the job launched.
    if crate::shutdown::aborting() {
        return Ok(crate::tools::SyncCoreOutcome::DrainCut);
    }

    // The consolidation owns the round's last model calls, so a cancel landing
    // anywhere inside it stops the round instead of yielding a result.
    Ok(
        match consolidate_analyst_runs(ws, message, runs, deadline, job_id).await {
            Ok(Some(text)) => crate::tools::SyncCoreOutcome::Terminal(Ok(text)),
            Ok(None) => crate::tools::SyncCoreOutcome::Cancelled,
            Err(e) => crate::tools::SyncCoreOutcome::Terminal(Err(e)),
        },
    )
}

/// Run the given analyst slots concurrently (deadline-bounded).
///
/// `round_key` is the analyze round's grouping key (the durable `job_id` on the
/// durable path, a fresh suffix on the sync path) — every analyst of the round
/// registers with `AnalyzeRound(round_key)` so the Running Agents view groups them
/// with the round's consolidation call.
async fn run_analyze_slots(
    ws: &Workspace,
    slots: &[&AnalyzeSlot],
    deadline: std::time::Instant,
    resume: bool,
    round_key: &str,
    question: &str,
) -> Vec<AnalyzeRun> {
    let members: Vec<_> = slots
        .iter()
        .map(|slot| {
            let ws = ws.clone();
            let agent_id = slot.agent_id.clone();
            let task = slot.task.clone();
            let round_key = round_key.to_string();
            let question = question.to_string();
            move |round| async move {
                // Session-non-emptiness discriminator: a resumed slot whose
                // session already contains the task continues with an empty
                // message (no duplicate task-prompt append); a missing/empty
                // session dispatches fresh with the stored task.
                let has_session = resume && crate::session::store().has_content(&agent_id).await;
                // Dynamic-resume path (resume + conditional message) — not a default dispatch.
                run_agent(
                    agent_id,
                    crate::Role::Analyst,
                    &ws,
                    None,
                    if has_session { "" } else { task.as_str() },
                    String::new(),
                    String::new(),
                    false,
                    None,
                    resume,
                    Some(round),
                    Some(crate::agent::registry::ParentKey::AnalyzeRound(round_key)),
                    Some(question),
                    None,
                )
                .await
            }
        })
        .collect();
    let handles = crate::agent::spawn_staggered_round(members, resume).await;
    await_round_members(handles, deadline)
        .await
        .into_iter()
        .map(|m| match m {
            RoundMember::Done((agent, response)) => AnalyzeRun::settle(agent, response),
            RoundMember::TimedOut => AnalyzeRun::Failed {
                reason: "timeout: analyst still running when the round deadline expired"
                    .to_string(),
            },
            RoundMember::Panicked => AnalyzeRun::Failed {
                reason: "panic: analyst task panicked".to_string(),
            },
            RoundMember::Cancelled => AnalyzeRun::Failed {
                reason: "cancelled: analyst task was cancelled".to_string(),
            },
        })
        .collect()
}

/// Boot-resume a durable analyze round — thin wrapper over
/// [`crate::tools::SyncDurableCore::resume_durable_round`]. `exec_dir` is the
/// recorded place directory the round ran in, when it had one.
pub(crate) async fn resume_analyze_round(job_id: &str, ws: &Workspace, exec_dir: Option<&str>) {
    crate::tools::SyncDurableCore::Analyze
        .resume_durable_round(job_id, ws, exec_dir)
        .await;
}

// ── Structured claim-level findings ──────────────────────────────────────

/// A single claim with source and confidence, extracted from an analyst's
/// response. Shared with the deep investigation (`research.rs`).
#[expect(clippy::struct_field_names)] // field name matches the JSON schema key
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct Claim {
    pub claim: String,
    pub source: String,
    pub confidence: String,
    pub contradictions: Vec<String>,
}

/// Structured claim-level findings extracted from an analyst's response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct AnalystFindings {
    pub claims: Vec<Claim>,
    pub unanswered: Vec<String>,
}

/// Per-analyst outcome after findings extraction. Three mutually-exclusive
/// states mirroring `pipeline::ParallelVerdict` — the type system guarantees
/// "no response" and "parse failure" cannot be confused.
enum AnalystOutcome {
    /// Agent failed to produce a response (crashed, cancelled, empty output).
    /// Carries the collapsed failure reason (scrubbed).
    NoResponse(String),
    /// Agent responded and structured extraction succeeded.
    Findings {
        raw: String,
        findings: AnalystFindings,
    },
    /// Agent responded but structured extraction failed after retries; the
    /// raw response is preserved for the fail-open dump.
    ParseFailed { raw: String, failure: String },
}

/// Outcome of one member of a parallel round awaited with a deadline.
#[derive(Debug)]
pub(crate) enum RoundMember<T> {
    Done(T),
    /// Member was still running when the round deadline expired — aborted.
    TimedOut,
    /// Member task panicked — surfaced, never silent.
    Panicked,
    /// Member task was cancelled externally (e.g. runtime shutdown), not by
    /// the round deadline — distinct from [`RoundMember::TimedOut`] so the
    /// surfaced reason stays accurate.
    Cancelled,
}

/// Await all round tasks with a single overall deadline. Members still
/// pending at the deadline are aborted and reported [`RoundMember::TimedOut`]
/// (warn-logged); panicked members are reported [`RoundMember::Panicked`] with
/// their panic message logged. Completed members keep their results, so a
/// stuck analyst never blocks the others' delivery.
///
/// Note: members are `tokio::spawn`ed (panic isolation), so if this future is
/// dropped mid-await the member tasks run detached to self-termination rather
/// than being cancelled — reachable only during process shutdown (tools run
/// to completion on agent cancel), where the runtime ends anyway; the
/// deadline covers the live-await path.
pub(crate) async fn await_round_members<T: Send + 'static>(
    handles: Vec<tokio::task::JoinHandle<T>>,
    deadline: std::time::Instant,
) -> Vec<RoundMember<T>> {
    use futures_util::StreamExt;
    use futures_util::stream::FuturesUnordered;

    let start = std::time::Instant::now();
    let cancel = tokio_util::sync::CancellationToken::new();
    let mut pending: FuturesUnordered<_> = handles
        .into_iter()
        .enumerate()
        .map(|(i, mut handle)| {
            let cancel = cancel.clone();
            async move {
                tokio::select! {
                    biased;
                    r = &mut handle => (i, match r {
                        Ok(v) => RoundMember::Done(v),
                        Err(e) if e.is_panic() => {
                            let panic = crate::util::panic_message(&*e.into_panic());
                            tracing::warn!(member = i, %panic, "round member task panicked");
                            RoundMember::Panicked
                        }
                        Err(_) => {
                            tracing::warn!(member = i, "round member task cancelled externally");
                            RoundMember::Cancelled
                        }
                    }),
                    () = cancel.cancelled() => {
                        handle.abort();
                        (i, RoundMember::TimedOut)
                    }
                }
            }
        })
        .collect();

    let mut out: Vec<Option<RoundMember<T>>> = (0..pending.len()).map(|_| None).collect();
    let deadline_expired = loop {
        if pending.is_empty() {
            break false;
        }
        let remaining = deadline.saturating_duration_since(std::time::Instant::now());
        if remaining.is_zero() {
            break true;
        }
        match tokio::time::timeout(remaining, pending.next()).await {
            Ok(Some((i, member))) => out[i] = Some(member),
            Ok(None) => break false,
            Err(_) => break true,
        }
    };
    if deadline_expired {
        cancel.cancel();
        while let Some((i, member)) = pending.next().await {
            out[i] = Some(member);
        }
        // After the drain every slot is filled; a `TimedOut` slot is exactly a
        // member that was still running at the deadline (aborted), while
        // members that finished just before it resolve to their real outcome
        // (biased select prefers the ready handle) — an accurate count.
        let aborted = out
            .iter()
            .filter(|m| matches!(m, Some(RoundMember::TimedOut)))
            .count();
        tracing::warn!(
            members_aborted = aborted,
            elapsed_secs = start.elapsed().as_secs_f64(),
            "round consolidation deadline expired — aborting stuck members"
        );
    }
    // Every loop-exit path (pending empty, deadline-drain, Ok(None)) fills
    // every slot exactly once — the invariant is enforced by construction.
    out.into_iter()
        .map(|m| m.expect("every round member resolves exactly once"))
        .collect()
}

/// Default bound on a parallel round's consolidation wait. Hours-scale: only
/// pathological stalls (a stuck analyst never completing) hit it; normal
/// analyst work finishes in minutes.
const DEFAULT_ROUND_TIMEOUT_SECS: u64 = 3 * 60 * 60;

/// Round consolidation bound for [`await_round_members`]. Overridable via env
/// for tuning.
pub(crate) fn round_timeout() -> Duration {
    crate::util::env_duration_secs("MAHBOT_ROUND_TIMEOUT_SECS", DEFAULT_ROUND_TIMEOUT_SECS)
}

/// One analyst's outcome in a parallel round. `Completed` always carries the
/// analyst's text; every way of producing none — stuck past the round deadline,
/// panicked, cancelled, or finished without a response — is spelled
/// [`Self::Failed`] with its reason surfaced, never silent.
#[expect(clippy::large_enum_variant)] // Agent is inherently large; the enum is transient
enum AnalyzeRun {
    Completed { agent: Agent, response: String },
    Failed { reason: String },
}

impl AnalyzeRun {
    /// Settle one finished slot. A member that finished without text is a
    /// [`Self::Failed`] carrying the agent's own recorded reason, so the real
    /// error chain (not a bare literal) reaches the caller.
    fn settle(agent: Agent, response: Option<String>) -> Self {
        match response.filter(|text| !text.trim().is_empty()) {
            Some(response) => Self::Completed { agent, response },
            None => Self::Failed {
                reason: agent.failure_reason("no-response: analyst produced no response"),
            },
        }
    }
}

/// Verdict of a single targeted claim verification, dispatched via
/// [`dispatch_claim_verifiers`] (the deep investigation and the analyze
/// dispute-verification round).
#[derive(Debug, Clone, Serialize, Deserialize)]
struct VerificationVerdict {
    pub verdict: String,
    pub evidence: String,
}

/// Verification outcome of one targeted claim. On the deep research path it is
/// merged into the run summary (the telemetry fields feed its query ledger);
/// on the analyze path it feeds the annotation-only `## Verification` section.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct VerificationResult {
    pub claim: String,
    pub verdict: String,
    pub evidence: String,
    pub tool_calls: usize,
    pub searches: usize,
    pub queries: Vec<String>,
}

impl VerificationResult {
    /// Fail-open verifier outcome: the claim could not be verified (deadline,
    /// panic, no/empty response, extraction failure, budget exhaustion).
    /// Canonical home of the "unresolved" verdict string.
    pub(crate) fn unresolved(
        claim: &str,
        evidence: impl Into<String>,
        tool_calls: usize,
        searches: usize,
        queries: Vec<String>,
    ) -> Self {
        Self {
            claim: claim.to_string(),
            verdict: "unresolved".to_string(),
            evidence: evidence.into(),
            tool_calls,
            searches,
            queries,
        }
    }
}

/// Reject verification verdicts outside the accepted vocabulary — fail-closed
/// inside the extraction retry loop.
fn validate_verification_verdict(v: &VerificationVerdict) -> Result<(), String> {
    if matches!(
        v.verdict.as_str(),
        "supported" | "contradicted" | "unresolved"
    ) {
        Ok(())
    } else {
        Err(format!(
            "verification verdict '{}' not in [supported, contradicted, unresolved]",
            v.verdict
        ))
    }
}

/// Cap on verification analysts spawned by a verification gate (exactly one
/// pass, bounded). The research tool caps at this; the analyze round needs at
/// most 2 (see [`verification_units`]) so the cap is never reached there.
/// Bounds the shared dispatch — never a per-run analyst budget.
pub(crate) const VERIFY_MAX_ANALYSTS: usize = 4;

/// Load the decorrelation angles asset — the research angles dealt out across
/// the parallel analyst cohort ([`crate::prompt::deal_sections`]).
pub(crate) fn load_analyst_angles() -> Vec<String> {
    load_prompt_sections("analyze/angles.md")
}

/// Compose an analyst's roster slot: the analyze with the slot's share of the
/// decorrelation angles appended ([`crate::prompt::deal_sections`]).
/// KV-cache discipline: vary ONLY the user message (the research
/// angle) — never per-analyst model/effort/tools.
fn analyst_slot(
    ws: &Workspace,
    angles: &[String],
    suffix: &str,
    i: usize,
    analyze: &str,
) -> AnalyzeSlot {
    let assigned = crate::prompt::deal_sections(angles, PARALLEL_ANALYST_COUNT, i);
    let task = if assigned.is_empty() {
        analyze.to_string()
    } else {
        format!("{analyze}\n\n{assigned}")
    };
    AnalyzeSlot {
        agent_id: format!("analyze_{}_{}_{}_analyst", ws.name, suffix, i),
        task,
    }
}

/// Consolidate analyst runs: 0 valid → error, 1 valid → raw passthrough,
/// ≥2 valid → extract findings and run the shared grouping pass (≥2 with
/// parseable claims group; a single parseable source skips grouping). A failed
/// member is the single spelling of "no response" (see [`AnalyzeRun::settle`]),
/// with its reason surfaced in the error. `Ok(None)` is a round that was
/// cancelled before this phase's first model call: no text was produced and the
/// caller receives nothing.
async fn consolidate_analyst_runs(
    ws: &Workspace,
    analyze: &str,
    runs: Vec<AnalyzeRun>,
    deadline: std::time::Instant,
    round_key: &str,
) -> Result<Option<String>> {
    // The cancel may have landed while the analysts ran: nothing new is started
    // for a cancelled round, so the extraction model calls never happen and the
    // partial findings are never turned into a result.
    if crate::round_cancel::is_cancelled(round_key) {
        return Ok(None);
    }
    // Drain-watch coverage for this phase: extraction runs while the analyst
    // Agents are still registered (their registry entries keep the drain-watch
    // from firing); the grouping pass runs with zero live agents and is
    // covered by the guard INSIDE the shared consensus core
    // (`run_grouping_repair`) — the single tracker for the consolidation call.
    // `consolidate_findings` re-checks the drain flag before the grouping call,
    // closing the sync-only window between extraction end and the core guard's
    // registration (documented accepted-residual-window contract, jobs.rs). No
    // phase guard here: it would duplicate the core's row and register a
    // phantom row on 0/1-valid-response rounds where no grouping call runs.
    let valid_count = runs
        .iter()
        .filter(|r| matches!(r, AnalyzeRun::Completed { .. }))
        .count();
    match valid_count {
        0 => {
            // Every run is a failure here (a completed run always carries text).
            let reasons: Vec<&str> = runs
                .iter()
                .filter_map(|r| match r {
                    AnalyzeRun::Failed { reason } => Some(reason.as_str()),
                    AnalyzeRun::Completed { .. } => None,
                })
                .collect();
            Err(super::internal_fault(&format!(
                "all parallel analysts failed to produce a response ({})",
                reasons.join("; ")
            )))
        }
        1 => Ok(Some(
            runs.into_iter()
                .find_map(|r| match r {
                    AnalyzeRun::Completed { response, .. } => Some(response),
                    AnalyzeRun::Failed { .. } => None,
                })
                .expect("exactly one valid response"),
        )),
        _ => {
            let outcomes = extract_findings(runs, deadline, round_key).await;
            consolidate_findings(ws, analyze, outcomes, round_key, deadline).await
        }
    }
}

/// Extract structured claim-level findings from each valid analyst response
/// while the agent is still alive (see [`Agent::extract_verdict`] for the
/// KV-cache rationale). Fail-open: parse failures keep the raw response and
/// are never silently dropped. The extraction wait shares the round-wide
/// `deadline` — a stuck extraction is reported as no-response, not awaited
/// forever — and `round_key` is the round whose cancel signal abandons each
/// extraction instead of asking again.
async fn extract_findings(
    runs: Vec<AnalyzeRun>,
    deadline: std::time::Instant,
    round_key: &str,
) -> Vec<AnalystOutcome> {
    let extraction_prompt = load_prompt("extraction/findings.md");
    let handles: Vec<_> = runs
        .into_iter()
        .map(|run| {
            let extraction_prompt = extraction_prompt.clone();
            let round_key = round_key.to_string();
            tokio::spawn(async move {
                let (agent, raw) = match run {
                    AnalyzeRun::Completed { agent, response } => (agent, response),
                    AnalyzeRun::Failed { reason } => {
                        return AnalystOutcome::NoResponse(crate::util::scrub_credentials(&reason));
                    }
                };
                // Nothing new starts for a round the caller has cancelled: the
                // extraction loop re-asks on a parse failure, so the whole call
                // chain is abandoned at the signal instead of asking again. The
                // caller has already been told the round was stopped, and the
                // outcome here is discarded by the phase gate either way. The
                // signal is polled first (`biased`), so it wins over an
                // extraction that is ready to return.
                let extracted = tokio::select! {
                    biased;
                    () = crate::round_cancel::cancelled(&round_key) => {
                        return AnalystOutcome::NoResponse(
                            "cancelled: findings extraction task was cancelled".to_string(),
                        );
                    }
                    result = agent
                        .extract_verdict::<AnalystFindings>(&extraction_prompt, None, None) => result,
                };
                match extracted {
                    Ok(findings) => AnalystOutcome::Findings { raw, findings },
                    Err(e) => AnalystOutcome::ParseFailed {
                        raw,
                        failure: e.to_string(),
                    },
                }
            })
        })
        .collect();
    await_round_members(handles, deadline)
        .await
        .into_iter()
        .map(|m| match m {
            RoundMember::Done(outcome) => outcome,
            RoundMember::TimedOut => AnalystOutcome::NoResponse(
                "timeout: findings extraction still running when the round deadline expired"
                    .to_string(),
            ),
            RoundMember::Panicked => {
                AnalystOutcome::NoResponse("panic: findings extraction task panicked".to_string())
            }
            RoundMember::Cancelled => AnalystOutcome::NoResponse(
                "cancelled: findings extraction task was cancelled".to_string(),
            ),
        })
        .collect()
}

/// Per-agent claim texts keyed by the ORIGINAL outcome index. A failed agent's
/// slot stays empty so the id space matches the input material. Per-agent
/// duplicates are NOT deduped: two identical claims from one agent are two
/// distinct ids, and the model places each exactly once.
#[must_use]
fn claims_per_agent(outcomes: &[AnalystOutcome]) -> Vec<Vec<String>> {
    outcomes
        .iter()
        .map(|o| match o {
            AnalystOutcome::Findings { findings, .. } => {
                findings.claims.iter().map(|c| c.claim.clone()).collect()
            }
            _ => Vec::new(),
        })
        .collect()
}

/// Render the fail-open analyst deliverable: the flat numbered claim list
/// plus the raw analyst dumps, headed by an explicit `marker` naming why
/// consolidation produced no groups. The marker is head-placed so it stays
/// prominently visible in the delivered output. Every no-groups path with at
/// least one valid claim list goes through this helper; the
/// total-extraction-failure path (`n_valid == 0`) is a deliberate exception
/// that renders extraction failures in place of the flat claim list.
#[must_use]
fn render_unconsolidated_fallback(
    marker: &str,
    items_by_agent: &[Vec<String>],
    outcomes: &[AnalystOutcome],
) -> String {
    let flat = render_flat_claim_list(items_by_agent);
    let raw_dump = render_raw_analyst_dump(outcomes);
    format!("## Analyst Reports\n\n({marker})\n\n{flat}\n\n{raw_dump}")
}

/// Consolidate extracted outcomes: build the per-agent claim lists and run the
/// shared repair-mode grouping pass (semantic grouping + contradiction
/// judgment, frozen groups + deterministic remainder). ≥2 valid responses go
/// through grouping; the output contract is summary + groups + optional
/// ungrouped list, with brackets computed from distinct cited agent ids.
/// Partial success (≥1 frozen group) is delivered as-is; only an exhaustion
/// with zero groups ever frozen keeps the fail-open flat claim list plus raw
/// analyst dumps with an explicit marker — never a fabricated consensus.
/// `Ok(None)` is a round cancelled before this phase's first model call or one
/// that was cancelled while it ran: no model call is started and the caller
/// receives nothing.
async fn consolidate_findings(
    ws: &Workspace,
    analyze: &str,
    outcomes: Vec<AnalystOutcome>,
    round_key: &str,
    deadline: std::time::Instant,
) -> Result<Option<String>> {
    // A round cancelled while its analysts ran produces no text at all — not
    // even the fail-open claim list below, which would render a partial result
    // for a round whose caller was already answered that it was stopped.
    if crate::round_cancel::is_cancelled(round_key) {
        return Ok(None);
    }
    let items_by_agent = claims_per_agent(&outcomes);
    let n_valid = items_by_agent.iter().filter(|l| !l.is_empty()).count();
    if n_valid == 0 {
        // No valid response produced parseable claims — fail open with raw
        // reports and the per-analyst reasons (nothing silently lost). The
        // marker distinguishes extraction failures from zero-claim reports.
        let has_parse_failures = outcomes
            .iter()
            .any(|o| matches!(o, AnalystOutcome::ParseFailed { .. }));
        let raw_dump = render_raw_analyst_dump(&outcomes);
        let failures = render_extraction_failures(&outcomes);
        let marker = if has_parse_failures {
            "unconsolidated — findings extraction failed"
        } else {
            "unconsolidated — no extractable claims from any analyst"
        };
        return Ok(Some(format!(
            "## Analyst Reports\n\n({marker})\n\n{failures}\n\n{raw_dump}"
        )));
    }
    if n_valid == 1 {
        // Only one analyst produced parseable claims — a grouping pass over a
        // single source cannot produce agreement brackets or contradictions,
        // so skip the provider call and deliver the flat claim list + raw
        // dumps with an explicit marker.
        tracing::info!("Single-analyst consolidation — skipping grouping pass");
        return Ok(Some(render_unconsolidated_fallback(
            "only one analyst produced parseable claims — grouping skipped",
            &items_by_agent,
            &outcomes,
        )));
    }
    // User material: global flat ids across ALL agents (each claim exactly one
    // id, in (agent, claim) order) — the schema's `id` field matches.
    let material = crate::consensus::numbered_items_material(&items_by_agent);
    let user =
        format!("# Original Question\n\n{analyze}\n\n# Agent Claims (id-numbered)\n\n{material}");

    let derived = crate::agent::role_chat_params(Role::Analyst);
    let system = format!(
        "{}\n\n{}",
        load_prompt("synthesis/analyst.md"),
        load_prompt("synthesis/grouping_contradictions.md"),
    );
    let request = crate::consensus::grouping_request(
        ws,
        "consolidate",
        &system,
        &user,
        derived.model,
        derived.reasoning_effort,
        derived.provider_order,
        Some(DEFAULT_MAX_TOKENS),
    );

    let table = crate::consensus::ItemTable::new(&items_by_agent);
    let parent = Some(crate::agent::registry::ParentKey::AnalyzeRound(
        round_key.to_string(),
    ));
    // Drain fired in the extraction-end → grouping-start gap (the analyst
    // agents have dropped, the consensus core's guard is not yet registered):
    // never start a new LLM call once the drain begins — deliver the fail-open
    // raw claim list instead (the same deliverable as a Fallback, without the
    // doomed call). The extraction itself is covered by the live analyst cards.
    if crate::shutdown::aborting() {
        tracing::warn!(
            "Analyst consolidation skipped — shutdown/drain in progress; delivering raw claim list"
        );
        return Ok(Some(render_unconsolidated_fallback(
            "unconsolidated — shutdown during consolidation",
            &items_by_agent,
            &outcomes,
        )));
    }
    // The same stop at the phase's own model calls: a cancel that lands after
    // the entry gate abandons the pass instead of letting it start another
    // repair round (or write its fail-open fallback and Warn for a round the
    // caller was already told was stopped). The signal is polled first and
    // without the random branch choice (`biased`), so a fired signal always wins
    // over a pass that is ready to continue.
    let outcome = tokio::select! {
        biased;
        () = crate::round_cancel::cancelled(round_key) => return Ok(None),
        outcome = crate::consensus::run_grouping_repair(
            ws,
            "consolidate",
            request,
            &items_by_agent,
            parent,
            Some(analyze.to_string()),
        ) => outcome,
    };
    match outcome {
        crate::consensus::RepairOutcome::Repaired { output, references } => {
            // Annotation-only verification of disputed groups: fresh analysts
            // re-check the contested findings, appended after the grouped
            // analysis. The main grouped analysis is never re-run or re-rendered.
            let verification = verify_disputed_groups(
                ws, analyze, &output, &table, &outcomes, round_key, deadline,
            )
            .await;
            Ok(Some(render_analyze_groups(
                &output,
                &references,
                &table,
                n_valid,
                &outcomes,
                &verification,
            )))
        }
        crate::consensus::RepairOutcome::Fallback => {
            tracing::warn!("Analyst consolidation failed — delivering raw claim list");
            Ok(Some(render_unconsolidated_fallback(
                "unconsolidated — consolidation failed",
                &items_by_agent,
                &outcomes,
            )))
        }
    }
}

/// Render the analyze output contract: summary + groups (heading, contradiction
/// flag, members citing item ids) + ungrouped remainder + DISPUTED
/// cross-references from remainder items to frozen groups. Brackets [n/N]
/// come from distinct cited agent ids; DISPUTED appears only when the group's
/// contradiction flag is true. Self-reported caveats of a cited claim render
/// as member metadata, never as contradictions.
///
/// `verification` is the annotation-only `## Verification` section (empty when
/// no disputed group was verified) — inserted after the grouped analysis so the
/// main grouped analysis is never disturbed.
#[must_use]
fn render_analyze_groups(
    output: &crate::consensus::GroupingOutput,
    references: &[crate::consensus::GroupingReference],
    table: &crate::consensus::ItemTable<'_>,
    n_valid: usize,
    outcomes: &[AnalystOutcome],
    verification: &str,
) -> String {
    let mut out = String::new();
    let summary = output.summary.trim();
    if summary.is_empty() {
        let _ = writeln!(
            out,
            "LLM summary unavailable — deterministic member render."
        );
    } else {
        let _ = writeln!(out, "{summary}");
    }
    for group in &output.groups {
        let n = crate::consensus::distinct_agents(group, table).len();
        let bracket = crate::consensus::bracket_label(n, n_valid, group.contradiction);
        let _ = write!(out, "\n\n**{}** {bracket}", group.heading);
        for member in &group.members {
            let caveat = member_caveat(member, table, outcomes);
            let _ = write!(
                out,
                "\n- {}",
                crate::consensus::render_member_line(member, table)
            );
            if let Some(c) = caveat {
                let _ = write!(out, " — caveat: {c}");
            }
        }
    }
    out.push_str(&crate::consensus::render_ungrouped_section(
        output,
        references,
        |member, disputed| {
            let mut line = crate::consensus::render_member_line(member, table);
            line.push_str(disputed);
            if let Some(c) = member_caveat(member, table, outcomes) {
                let _ = write!(line, " — caveat: {c}");
            }
            line
        },
    ));
    if !verification.is_empty() {
        let _ = write!(out, "\n\n{verification}");
    }
    out
}

/// Look up a cited member's self-reported caveats (the claim's own
/// `contradictions` field — analyst self-report, NOT a group contradiction).
/// The claim is resolved via the item table's (agent, per-agent index) — no
/// text equality.
fn member_caveat(
    member: &crate::consensus::GroupingMember,
    table: &crate::consensus::ItemTable<'_>,
    outcomes: &[AnalystOutcome],
) -> Option<String> {
    let (agent, item) = table.resolve_index(member.id)?;
    let AnalystOutcome::Findings { findings, .. } = outcomes.get(agent)? else {
        return None;
    };
    let claim = findings.claims.get(item)?;
    if claim.contradictions.is_empty() {
        None
    } else {
        Some(claim.contradictions.join("; "))
    }
}

/// Render the flat numbered claim list (the grouping-pass input) for the
/// fail-open deliverable.
#[must_use]
fn render_flat_claim_list(items_by_agent: &[Vec<String>]) -> String {
    let mut out = String::new();
    let _ = writeln!(out, "### Claims (grouping input)");
    for (agent_idx, claims) in items_by_agent.iter().enumerate() {
        for (i, c) in claims.iter().enumerate() {
            let _ = writeln!(out, "{}. [Agent {agent_idx}] {c}", i + 1);
        }
    }
    out
}

/// Normalize a claim for deterministic comparisons (QueryLedger, unanswered
/// dedup). Shared with the deep investigation.
pub(crate) fn normalize_claim(s: &str) -> String {
    s.split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
        .to_lowercase()
}

/// Max of two confidence tiers ("low" < "medium" < "high"). Shared with the
/// deep investigation's cross-round claim merge (`research.rs`).
pub(crate) fn max_confidence(a: &str, b: &str) -> String {
    let rank = |c: &str| match c {
        "high" => 2,
        "medium" => 1,
        _ => 0,
    };
    if rank(b) > rank(a) {
        b.to_string()
    } else {
        a.to_string()
    }
}

/// One claim targeted for verification by a verification gate (deep research
/// tool's, or the analyze dispute-verification round), plus the material fed
/// into the fresh analyst's task.
pub(crate) struct VerificationTarget {
    pub(crate) claim: String,
    pub(crate) sources: String,
    pub(crate) contradictions: String,
}

impl VerificationTarget {
    pub(crate) fn new(claim: &str, sources: &str, contradictions: &str) -> Self {
        Self {
            claim: claim.to_string(),
            sources: sources.to_string(),
            contradictions: contradictions.to_string(),
        }
    }
}

/// Extract search/web_search query strings + tool-call counts from session
/// history messages (for the query ledger and the run summary). Shared with
/// the deep investigation (`research.rs`), which also feeds it the persisted
/// history of deadline-aborted analysts (wrap-up stage).
pub(crate) fn extract_query_telemetry_from_history(
    history: &[ChatMessage],
) -> (usize, usize, Vec<String>) {
    let mut tool_calls = 0usize;
    let mut searches = 0usize;
    let mut queries = Vec::new();
    for msg in history {
        let Some(calls) = crate::session::decoded_tool_calls(msg) else {
            continue;
        };
        tool_calls += calls.len();
        for call in calls {
            if matches!(call.name.as_str(), "web_search" | "search") {
                searches += 1;
                if let Some(q) = call.arguments.get("query").and_then(|v| v.as_str()) {
                    queries.push(q.to_string());
                }
            }
        }
    }
    (tool_calls, searches, queries)
}

/// Extract search/web_search query strings + tool-call counts from an
/// agent's session history (for the query ledger and the run summary).
/// Shared with the deep investigation (`research.rs`).
pub(crate) fn extract_query_telemetry(agent: &Agent) -> (usize, usize, Vec<String>) {
    extract_query_telemetry_from_history(agent.session.history())
}

/// Dispatch one fresh Analyst per verification target (bounded by
/// [`VERIFY_MAX_ANALYSTS`]) in parallel. `id_prefix` seeds the per-target
/// agent IDs; `task_extra` is appended to every task (e.g. the query
/// ledger). The wait shares the round-wide `deadline`. Shared with the deep
/// research tool's verification gate.
///
/// `parent_key` names the parent invocation the verifiers group under
/// (research verifiers pass [`ParentKey::Research`]; analyze verifiers pass
/// [`ParentKey::AnalyzeRound`]) — the running-agents view and research-cancel
/// sweep use it, so it must be supplied by the caller, never hardcoded.
///
/// Returns the results PLUS the dispatched agent IDs (the deep research
/// sanitizer needs them at dispatch time — successful writers never appear in
/// wrap-up snapshots; the ids ride the return value so the shared helper
/// keeps no write-only out-parameter).
#[expect(clippy::too_many_arguments)]
pub(crate) async fn dispatch_claim_verifiers(
    ws: &Workspace,
    id_prefix: &str,
    targets: &[VerificationTarget],
    task_extra: &str,
    deadline: std::time::Instant,
    resume: bool,
    parent_key: Option<crate::agent::registry::ParentKey>,
    question: &str,
) -> (Vec<VerificationResult>, Vec<String>) {
    let task_template = load_prompt("analyze/verify.md");
    let extraction_prompt = load_prompt("extraction/verify.md");
    let suffix = crate::generate_suffix();
    let mut dispatched: Vec<String> = Vec::new();
    let members: Vec<_> = targets
        .iter()
        .take(VERIFY_MAX_ANALYSTS)
        .enumerate()
        .map(|(i, t)| {
            let ws = ws.clone();
            let agent_id = format!("{id_prefix}_{suffix}_{i}");
            dispatched.push(agent_id.clone());
            let mut task = substitute(
                &task_template,
                &[
                    ("{{claim}}", &t.claim),
                    ("{{sources}}", &t.sources),
                    ("{{contradictions}}", &t.contradictions),
                ],
            );
            task.push_str(task_extra);
            let extraction_prompt = extraction_prompt.clone();
            let claim_text = t.claim.clone();
            let parent_key = parent_key.clone();
            let question = question.to_string();
            move |round| async move {
                run_claim_verifier(
                    &ws,
                    &agent_id,
                    &claim_text,
                    &task,
                    &extraction_prompt,
                    round,
                    parent_key,
                    &question,
                )
                .await
            }
        })
        .collect();
    let handles = crate::agent::spawn_staggered_round(members, resume).await;
    let results = await_round_members(handles, deadline)
        .await
        .into_iter()
        .enumerate()
        .map(|(i, m)| match m {
            RoundMember::Done(v) => v,
            RoundMember::TimedOut | RoundMember::Panicked | RoundMember::Cancelled => {
                VerificationResult::unresolved(
                    &targets[i].claim,
                    "verifier never completed (round deadline or panic)",
                    0,
                    0,
                    Vec::new(),
                )
            }
        })
        .collect();
    (results, dispatched)
}

/// Run one claim verifier: a fresh Analyst researches the claim and returns
/// its structured verdict. Any failure yields "unresolved" — fail-open.
///
/// `question` is the run's question — threaded as the run group's header
/// label (purely presentational). `parent_key` is supplied by the dispatcher
/// so the verifier groups under the correct parent invocation (research run /
/// analyze round).
#[expect(clippy::too_many_arguments)]
async fn run_claim_verifier(
    ws: &Workspace,
    agent_id: &str,
    target_claim: &str,
    task: &str,
    extraction_prompt: &str,
    round: crate::agent::RoundOpts,
    parent_key: Option<crate::agent::registry::ParentKey>,
    question: &str,
) -> VerificationResult {
    let (agent, response) = run_default_agent(
        agent_id,
        Role::Analyst,
        ws,
        task,
        false,
        Some(round),
        parent_key,
        Some(question.to_string()),
    )
    .await;
    // The telemetry fields ride every VerificationResult regardless of caller:
    // the deep-research path consumes them for its run-summary/query ledger,
    // while the analyze path only renders claim/verdict/evidence in the
    // annotation section (the extra scan is cheap and keeps one shared shape).
    let (tool_calls, searches, queries) = extract_query_telemetry(&agent);
    let Some(raw) = response else {
        return VerificationResult::unresolved(
            target_claim,
            "verifier failed to produce a response",
            tool_calls,
            searches,
            queries,
        );
    };
    if raw.trim().is_empty() {
        return VerificationResult::unresolved(
            target_claim,
            "verifier produced an empty response",
            tool_calls,
            searches,
            queries,
        );
    }
    match agent
        .extract_verdict::<VerificationVerdict>(
            extraction_prompt,
            Some(&validate_verification_verdict),
            None,
        )
        .await
    {
        Ok(v) => VerificationResult {
            // Key by the target claim (not the verifier's possibly-rephrased
            // restatement) so report reconciliation matches exactly.
            claim: target_claim.to_string(),
            verdict: v.verdict,
            evidence: v.evidence,
            tool_calls,
            searches,
            queries,
        },
        Err(e) => VerificationResult::unresolved(
            target_claim,
            format!("verification extraction failed: {e}"),
            tool_calls,
            searches,
            queries,
        ),
    }
}

/// Escape triple-backtick code fences to prevent markdown-structure
/// corruption in the consolidated output.
pub(crate) fn escape_fences(s: &str) -> String {
    s.replace("```", "\\`\\`\\`")
}

// ── Analyze dispute-verification round ──────────────────────────────────

/// Split the disputed groups into verification units.
///
/// Exactly one disputed group → a single unit (→ 1 verifier). More than one →
/// two roughly-equal parts **by group count**, the FIRST part taking the extra
/// group when the count is odd (→ 2 verifiers). Each unit is verified by
/// exactly one fresh analyst, so the verification cost is bounded by the rule
/// above — no per-run analyst budget.
fn verification_units<'a>(
    disputed: &[&'a crate::consensus::GroupingGroup],
) -> Vec<Vec<&'a crate::consensus::GroupingGroup>> {
    if disputed.len() <= 1 {
        return vec![disputed.to_vec()];
    }
    // ceil(n / 2) — the first part takes the extra group on an odd count.
    let first_count = disputed.len().div_ceil(2);
    let (first, second) = disputed.split_at(first_count);
    vec![first.to_vec(), second.to_vec()]
}

/// Synthesize ONE verifiable proposition for a verification unit (a disputed
/// group, or — for a split unit — several groups treated as a single unit).
///
/// The claim is a single statement built from the group's heading + its member
/// claims; member sources and self-reported contradictions are aggregated into
/// the [`VerificationTarget`] fields so the verifier sees both the assertion
/// and the evidence pointing both ways. A group with no resolvable member
/// claims degrades to its bare heading (never silent).
fn synthesize_verification_target(
    unit: &[&crate::consensus::GroupingGroup],
    table: &crate::consensus::ItemTable<'_>,
    outcomes: &[AnalystOutcome],
) -> VerificationTarget {
    let mut claim_parts = Vec::new();
    let mut sources = Vec::new();
    let mut contradictions = Vec::new();
    for group in unit {
        let mut group_claims = Vec::new();
        for member in &group.members {
            if let Some((_, text)) = table.resolve(member.id) {
                group_claims.push(text.to_string());
            }
            if let Some((agent_idx, item_idx)) = table.resolve_index(member.id)
                && let Some(AnalystOutcome::Findings { findings, .. }) = outcomes.get(agent_idx)
                && let Some(claim) = findings.claims.get(item_idx)
            {
                if !claim.source.is_empty() {
                    sources.push(claim.source.clone());
                }
                contradictions.extend(claim.contradictions.iter().cloned());
            }
        }
        let claim = if group_claims.is_empty() {
            group.heading.clone()
        } else {
            format!("{}: {}", group.heading, group_claims.join("; "))
        };
        claim_parts.push(claim);
    }
    VerificationTarget::new(
        &claim_parts.join("\n"),
        &sources.join("; "),
        &contradictions.join("; "),
    )
}

/// Render the annotation-only `## Verification` section from verifier results.
/// Empty when no verifier produced a verdict. Each verdict line carries the
/// synthesized claim, the verdict, and the supporting evidence. Composite
/// (multi-group) claims AND multi-line evidence render their newlines as
/// semicolons so the bullet stays on one line.
fn render_verification_section(results: &[VerificationResult]) -> String {
    if results.is_empty() {
        return String::new();
    }
    let mut out = String::from("## Verification");
    for v in results {
        let claim_line = escape_fences(&v.claim).replace('\n', "; ");
        let evidence_line = escape_fences(&v.evidence).replace('\n', "; ");
        let _ = write!(
            out,
            "\n- {claim_line} → **{}** — {evidence_line}",
            v.verdict,
        );
    }
    out
}

/// (Deadline agreement) Verification runs only while ≤ half of the round
/// window has elapsed; past that, it is skipped entirely (nothing appended)
/// rather than forced all-unresolved. The round window is
/// [`round_timeout`] — the same bound the analyst round shares.
fn verification_window_open(deadline: std::time::Instant) -> bool {
    let Some(anchor) = deadline.checked_sub(round_timeout() / 2) else {
        return false;
    };
    std::time::Instant::now() <= anchor
}

/// Annotation-only verification of disputed consensus groups.
///
/// Detects groups flagged `contradiction:true` (those rendered `[n/N ·
/// DISPUTED]`), and — only when at least one exists — dispatches fresh
/// verification analysts (1 for a single disputed group, 2 over two split
/// parts for more) to re-check them. Results are appended as a
/// `## Verification` section; the main grouped analysis is never modified and
/// grouping is never re-run. No per-run analyst budget is introduced, and the
/// round is best-effort (never checkpointed).
async fn verify_disputed_groups(
    ws: &Workspace,
    analyze: &str,
    output: &crate::consensus::GroupingOutput,
    table: &crate::consensus::ItemTable<'_>,
    outcomes: &[AnalystOutcome],
    round_key: &str,
    deadline: std::time::Instant,
) -> String {
    // A cancelled round starts nothing new AND raises no complaint about itself:
    // this gate is the phase's first statement, so neither skip warning below can
    // fire for a round whose result is discarded. Logged at info, like the
    // consolidation phase's gate.
    if crate::round_cancel::is_cancelled(round_key) {
        tracing::info!(
            round = %round_key,
            "Analyze verification skipped — the round was cancelled"
        );
        return String::new();
    }
    let disputed: Vec<&crate::consensus::GroupingGroup> =
        output.groups.iter().filter(|g| g.contradiction).collect();
    if disputed.is_empty() {
        // No contested finding — the fast single-round path is preserved.
        return String::new();
    }
    if !verification_window_open(deadline) {
        tracing::warn!(
            disputed_groups = disputed.len(),
            "Analyze verification skipped — more than half the round window elapsed"
        );
        return String::new();
    }
    if crate::shutdown::aborting() {
        tracing::warn!(
            disputed_groups = disputed.len(),
            "Analyze verification skipped — shutdown/drain in progress"
        );
        return String::new();
    }
    let units = verification_units(&disputed);
    let targets: Vec<VerificationTarget> = units
        .iter()
        .map(|unit| synthesize_verification_target(unit, table, outcomes))
        .collect();
    tracing::info!(
        disputed_groups = disputed.len(),
        verifiers = units.len(),
        "Analyze verification round dispatching fresh analysts"
    );
    let prefix = format!("analyze_{}_verify", ws.name);
    let (results, _dispatched) = dispatch_claim_verifiers(
        ws,
        &prefix,
        &targets,
        "",
        deadline,
        false,
        Some(crate::agent::registry::ParentKey::AnalyzeRound(
            round_key.to_string(),
        )),
        analyze,
    )
    .await;
    render_verification_section(&results)
}

/// Render the fail-open raw dump: the raw response of every valid analyst
/// (extraction success or failure) in `### Report from Analyst N` sections.
/// No-response analysts render with their reason so nothing is silently lost.
fn render_raw_analyst_dump(outcomes: &[AnalystOutcome]) -> String {
    let sections: Vec<String> = outcomes
        .iter()
        .enumerate()
        .map(|(i, o)| {
            let body = match o {
                AnalystOutcome::Findings { raw, .. } | AnalystOutcome::ParseFailed { raw, .. } => {
                    escape_fences(raw)
                }
                AnalystOutcome::NoResponse(reason) => format!("no response: {reason}"),
            };
            format!("### Report from Analyst {}\n{body}", i + 1)
        })
        .collect();
    sections.join("\n\n")
}

/// Render extraction-failure reasons for the fail-open marker (which analysts
/// produced no parseable findings and why).
#[must_use]
fn render_extraction_failures(outcomes: &[AnalystOutcome]) -> String {
    let mut out = String::new();
    for (i, o) in outcomes.iter().enumerate() {
        match o {
            AnalystOutcome::ParseFailed { failure, .. } => {
                let _ = writeln!(
                    out,
                    "- Analyst {}: findings extraction failed ({failure})",
                    i + 1
                );
            }
            AnalystOutcome::NoResponse(reason) => {
                let _ = writeln!(out, "- Analyst {}: no response ({reason})", i + 1);
            }
            AnalystOutcome::Findings { .. } => {}
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::util::UnwrapPoison as _;
    use crate::util::test::{FakeProvider, install_fake_provider};
    use crate::workspace::test_ws;
    use serde_json::json;
    use std::sync::Arc;

    #[tokio::test]
    async fn test_analyze_missing_args() {
        let tool = AnalyzeTool::new(DispatchMode::Sync, Role::Engineer);
        let ws = test_ws("/tmp/test_ws");

        // Missing message
        let result = tool.execute(&ws, json!({})).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("usage: missing required argument \"message\""),
            "Should mention missing message"
        );
    }

    /// Tests that analyze dispatches analysts and returns the consolidated result.
    /// Requires an LLM provider to be configured. The sync path now runs through
    /// the durable analyze core, so the stores must be initialized (like the
    /// resume test).
    #[tokio::test]
    #[ignore = "requires LLM provider; runs only when explicitly invoked"]
    async fn test_analyze_analyst() {
        crate::util::test::init_management_test_stores().await;
        let tool = AnalyzeTool::new(DispatchMode::Sync, Role::Engineer);
        let ws = test_ws("/tmp/test_ws");
        let args = json!({"message": "Say 'hello analyst' and nothing else."});
        let result = tool.execute(&ws, args).await.expect("execute");
        assert!(
            result.contains("hello"),
            "output should contain hello: {result}"
        );
    }

    // ── Consolidation edge-case tests (no LLM provider needed) ──

    /// Build a bare analyst run for pipeline tests: a real `Agent` (no stores
    /// touched) settled from an optional raw response.
    fn test_analyst_run(ws: &Workspace, response: Option<&str>) -> AnalyzeRun {
        let agent = Agent::new(
            format!("analyze_test_{}", crate::generate_suffix()),
            Role::Analyst,
            ws,
            None,
            String::new(),
            String::new(),
            false,
            None,
            None,
            None,
        );
        AnalyzeRun::settle(agent, response.map(ToString::to_string))
    }

    /// A failed round member (stuck past the deadline, panicked, cancelled).
    fn test_failed_run(reason: &str) -> AnalyzeRun {
        AnalyzeRun::Failed {
            reason: reason.to_string(),
        }
    }

    fn findings(claims: Vec<(&str, &str, &str)>) -> AnalystFindings {
        AnalystFindings {
            claims: claims
                .into_iter()
                .map(|(claim, source, confidence)| Claim {
                    claim: claim.to_string(),
                    source: source.to_string(),
                    confidence: confidence.to_string(),
                    contradictions: Vec::new(),
                })
                .collect(),
            unanswered: Vec::new(),
        }
    }

    #[tokio::test]
    async fn test_consolidate_zero_responses_surfaces_failure_reasons() {
        let ws = test_ws("/tmp/test_ws");
        let runs = vec![
            test_failed_run("analyst 0 still running when the round deadline expired"),
            test_failed_run("analyst 1 task panicked"),
            test_analyst_run(&ws, None),
        ];
        let result = consolidate_analyst_runs(
            &ws,
            "test question",
            runs,
            std::time::Instant::now() + Duration::from_mins(1),
            "test_round",
        )
        .await;
        let err = result.expect_err("0 valid responses should error");
        assert!(
            err.to_string()
                .contains("internal: all parallel analysts failed"),
            "error should mention analyst failure: {err}"
        );
        assert!(
            err.to_string().contains("deadline expired"),
            "error should surface the stuck-analyst reason: {err}"
        );
        assert!(
            err.to_string().contains("panicked"),
            "error should surface the panicked-analyst reason: {err}"
        );
        assert!(
            err.to_string().contains("analyst produced no response"),
            "error should surface the no-response reason: {err}"
        );
    }

    /// The round wait is bounded: a member that never completes is aborted at
    /// the deadline and reported [`RoundMember::TimedOut`], while completed
    /// members keep their results.
    #[tokio::test]
    async fn await_round_members_bounds_stuck_member() {
        struct AbortDetector(std::sync::Arc<std::sync::atomic::AtomicBool>);
        impl Drop for AbortDetector {
            fn drop(&mut self) {
                self.0.store(true, std::sync::atomic::Ordering::SeqCst);
            }
        }

        let aborted = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        let flag = aborted.clone();
        let stuck = tokio::spawn(async move {
            let _detector = AbortDetector(flag);
            loop {
                tokio::time::sleep(Duration::from_hours(1)).await;
            }
        });
        let done = tokio::spawn(async { 42u32 });
        let deadline = std::time::Instant::now() + Duration::from_millis(300);
        let members = await_round_members(vec![stuck, done], deadline).await;
        assert!(matches!(members[0], RoundMember::TimedOut), "{members:?}");
        assert!(matches!(members[1], RoundMember::Done(42)), "{members:?}");
        // Aborting the task drops its locals — the detector fires.
        tokio::time::sleep(Duration::from_millis(100)).await;
        assert!(
            aborted.load(std::sync::atomic::Ordering::SeqCst),
            "stuck member task should have been aborted at the deadline"
        );
    }

    /// A panicking member is surfaced as [`RoundMember::Panicked`], never
    /// silently dropped.
    #[tokio::test]
    async fn await_round_members_surfaces_panics() {
        let panicking = tokio::spawn(async { panic!("boom") });
        let deadline = std::time::Instant::now() + Duration::from_secs(5);
        let members = await_round_members(vec![panicking], deadline).await;
        assert!(matches!(members[0], RoundMember::Panicked), "{members:?}");
    }

    #[tokio::test]
    async fn test_consolidate_one_response_returned_directly() {
        let ws = test_ws("/tmp/test_ws");
        let runs = vec![
            test_analyst_run(&ws, Some("only answer")),
            test_analyst_run(&ws, None),
            test_analyst_run(&ws, None),
        ];
        let result = consolidate_analyst_runs(
            &ws,
            "test question",
            runs,
            std::time::Instant::now() + Duration::from_mins(1),
            "test_round",
        )
        .await;
        assert!(result.is_ok(), "1 response should succeed");
        assert_eq!(
            result.unwrap().as_deref(),
            Some("only answer"),
            "Should return the single response directly without consolidation"
        );
    }

    #[tokio::test]
    async fn test_consolidate_empty_responses_filtered() {
        // Empty/whitespace-only strings should be filtered the same as None
        let ws = test_ws("/tmp/test_ws");
        let runs = vec![
            test_analyst_run(&ws, Some("valid")),
            test_analyst_run(&ws, Some("")),
            test_analyst_run(&ws, Some("   ")),
        ];
        let result = consolidate_analyst_runs(
            &ws,
            "test question",
            runs,
            std::time::Instant::now() + Duration::from_mins(1),
            "test_round",
        )
        .await;
        assert!(
            result.is_ok(),
            "1 valid after filtering empty should succeed"
        );
        assert_eq!(
            result.unwrap().as_deref(),
            Some("valid"),
            "Should return the only non-empty response"
        );
    }

    // ── Analyze grouping renderer ────────────────────────────────────────────

    #[test]
    fn render_analyze_groups_computes_brackets_from_distinct_agents() {
        let outcomes = vec![
            AnalystOutcome::Findings {
                raw: "r1".into(),
                findings: findings(vec![
                    ("alpha is true", "url1", "high"),
                    ("beta is true", "url2", "medium"),
                ]),
            },
            AnalystOutcome::Findings {
                raw: "r2".into(),
                findings: findings(vec![
                    ("alpha is true", "url1", "high"),
                    ("gamma is true", "url3", "low"),
                ]),
            },
            AnalystOutcome::NoResponse("analyst produced no response".into()),
        ];
        // Global flat ids: 0 = a0 "alpha is true", 1 = a0 "beta is true",
        // 2 = a1 "alpha is true", 3 = a1 "gamma is true".
        let items = claims_per_agent(&outcomes);
        let table = crate::consensus::ItemTable::new(&items);
        let output = crate::consensus::GroupingOutput {
            summary: "Two facts, one solo finding.".into(),
            groups: vec![
                crate::consensus::GroupingGroup {
                    heading: "Alpha".into(),
                    contradiction: false,
                    members: vec![
                        crate::consensus::GroupingMember {
                            id: 0,
                            ..Default::default()
                        },
                        crate::consensus::GroupingMember {
                            id: 2,
                            ..Default::default()
                        },
                    ],
                },
                crate::consensus::GroupingGroup {
                    heading: "Beta".into(),
                    contradiction: false,
                    members: vec![crate::consensus::GroupingMember {
                        id: 1,
                        ..Default::default()
                    }],
                },
            ],
            ungrouped: vec![crate::consensus::GroupingMember {
                id: 3,
                ..Default::default()
            }],
        };
        let text = render_analyze_groups(&output, &[], &table, 2, &outcomes, "");
        assert!(
            text.contains("**Alpha** [2/2]"),
            "consensus group renders [2/2] from distinct cited agents: {text}"
        );
        assert!(
            text.contains("**Beta** [1/2]"),
            "solo group renders [1/2] without DISPUTED: {text}"
        );
        assert!(text.contains("**Other**"), "ungrouped list renders: {text}");
        assert!(
            text.contains("- Agent 1: gamma is true"),
            "ungrouped member attributes its source: {text}"
        );
        assert!(
            !text.contains("DISPUTED"),
            "no contradiction flag means no DISPUTED anywhere: {text}"
        );
    }

    #[test]
    fn render_analyze_groups_disputed_only_on_contradiction() {
        let outcomes = vec![
            AnalystOutcome::Findings {
                raw: "r1".into(),
                findings: findings(vec![("alpha is true", "url1", "high")]),
            },
            AnalystOutcome::Findings {
                raw: "r2".into(),
                findings: findings(vec![("alpha is false", "url2", "high")]),
            },
        ];
        let items = claims_per_agent(&outcomes);
        let table = crate::consensus::ItemTable::new(&items);
        let output = crate::consensus::GroupingOutput {
            summary: "Agents disagree on alpha.".into(),
            groups: vec![crate::consensus::GroupingGroup {
                heading: "Alpha".into(),
                contradiction: true,
                members: vec![
                    crate::consensus::GroupingMember {
                        id: 0,
                        ..Default::default()
                    },
                    crate::consensus::GroupingMember {
                        id: 1,
                        ..Default::default()
                    },
                ],
            }],
            ungrouped: vec![],
        };
        let text = render_analyze_groups(&output, &[], &table, 2, &outcomes, "");
        assert!(
            text.contains("**Alpha** [2/2 · DISPUTED]"),
            "contradiction group renders [2/2 · DISPUTED]: {text}"
        );
    }

    #[test]
    fn render_analyze_groups_surfaces_member_caveats_as_metadata() {
        let mut claims = findings(vec![("alpha is true", "url1", "high")]);
        claims.claims[0].contradictions = vec!["source B says alpha may be false".into()];
        let outcomes = vec![
            AnalystOutcome::Findings {
                raw: "r1".into(),
                findings: claims,
            },
            AnalystOutcome::Findings {
                raw: "r2".into(),
                findings: findings(vec![("alpha is true", "url1", "high")]),
            },
        ];
        let items = claims_per_agent(&outcomes);
        let table = crate::consensus::ItemTable::new(&items);
        let output = crate::consensus::GroupingOutput {
            summary: "alpha is agreed.".into(),
            groups: vec![crate::consensus::GroupingGroup {
                heading: "Alpha".into(),
                contradiction: false,
                members: vec![
                    crate::consensus::GroupingMember {
                        id: 0,
                        ..Default::default()
                    },
                    crate::consensus::GroupingMember {
                        id: 1,
                        ..Default::default()
                    },
                ],
            }],
            ungrouped: vec![],
        };
        let text = render_analyze_groups(&output, &[], &table, 2, &outcomes, "");
        assert!(
            text.contains("caveat: source B says alpha may be false"),
            "self-reported caveat renders as member metadata: {text}"
        );
        assert!(
            !text.contains("DISPUTED"),
            "a self-reported caveat is NOT a contradiction: {text}"
        );
    }

    // ── consolidation fail-open ────────────────────────────────────────

    /// Helper: run consolidation over the given extracted outcomes with the
    /// given fake provider outcomes scripted.
    async fn consolidate_with_script(
        outcomes: Vec<AnalystOutcome>,
        fake: FakeProvider,
    ) -> anyhow::Result<String> {
        let provider: Arc<dyn crate::Provider> = Arc::new(fake);
        let _guard = install_fake_provider(provider);
        let ws = test_ws("/tmp/test_ws");
        consolidate_findings(
            &ws,
            "test question",
            outcomes,
            "test_round",
            std::time::Instant::now() + std::time::Duration::from_mins(60),
        )
        .await
        .map(|text| text.expect("a live round delivers its consolidated text"))
    }

    /// Two analysts agreeing on one claim, plus a no-response member.
    fn agreed_outcomes(raw_a: &str, raw_b: &str) -> Vec<AnalystOutcome> {
        vec![
            AnalystOutcome::Findings {
                raw: raw_a.to_string(),
                findings: findings(vec![("alpha is true", "url1", "high")]),
            },
            AnalystOutcome::Findings {
                raw: raw_b.to_string(),
                findings: findings(vec![("alpha is true", "url1", "high")]),
            },
            AnalystOutcome::NoResponse("analyst produced no response".into()),
        ]
    }

    #[tokio::test]
    #[serial_test::serial(provider, drain)] // serializes the process-global fake provider (providers::PROVIDER) + shutdown drain flag
    async fn test_consolidation_fail_open_delivers_raw_reports() {
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        // Synthesis retries (transport failures) exhaust → fail open.
        let fake = FakeProvider::new()
            .err(
                crate::retry::FailureClass::Transport,
                "error reading response body",
            )
            .err(
                crate::retry::FailureClass::Transport,
                "error reading response body",
            )
            .err(
                crate::retry::FailureClass::Transport,
                "error reading response body",
            );
        let result =
            consolidate_with_script(agreed_outcomes("report one", "report two"), fake).await;
        let text = result.expect("fail-open must succeed with raw reports");
        assert!(
            text.contains("unconsolidated — consolidation failed"),
            "must carry the unconsolidated marker: {text}"
        );
        assert!(text.contains("## Analyst Reports"), "{text}");
        assert!(text.contains("### Report from Analyst 1"), "{text}");
        assert!(text.contains("report one"), "{text}");
        assert!(text.contains("### Report from Analyst 2"), "{text}");
        assert!(text.contains("report two"), "{text}");
        assert!(
            text.contains("no response: analyst produced no response"),
            "no-response analyst renders its reason — nothing silently lost: {text}"
        );
    }

    #[tokio::test]
    #[serial_test::serial(provider, drain)] // serializes the process-global fake provider (providers::PROVIDER) + shutdown drain flag
    async fn test_consolidation_non_retryable_fails_open() {
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        // Even an immediate non-retryable error fails open.
        let fake = FakeProvider::new().err(
            crate::retry::FailureClass::NonRetryable,
            "insufficient balance",
        );
        let outcomes = vec![
            AnalystOutcome::Findings {
                raw: "alpha".into(),
                findings: findings(vec![("a", "u1", "low")]),
            },
            AnalystOutcome::Findings {
                raw: "beta".into(),
                findings: findings(vec![("a", "u1", "low")]),
            },
            AnalystOutcome::Findings {
                raw: "gamma".into(),
                findings: findings(vec![("a", "u1", "low")]),
            },
        ];
        let result = consolidate_with_script(outcomes, fake).await;
        let text = result.expect("non-retryable consolidation failure fails open");
        assert!(
            text.contains("unconsolidated — consolidation failed"),
            "{text}"
        );
        assert!(text.contains("alpha"), "{text}");
        assert!(text.contains("beta"), "{text}");
        assert!(text.contains("gamma"), "{text}");
    }

    #[tokio::test]
    #[serial_test::serial(provider, drain)] // serializes the process-global fake provider (providers::PROVIDER) + shutdown drain flag
    async fn test_consolidation_success_synthesizes() {
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        // The grouping pass parses a strict id-based GroupingOutput; a
        // faithful model groups the two agreed claims and leaves nothing
        // ungrouped.
        let fake = FakeProvider::new().ok(
            r#"{"summary":"alpha is true.","groups":[{"heading":"Alpha","contradiction":false,"members":[{"id":0},{"id":1}]}],"ungrouped":[]}"#,
        );
        let result = consolidate_with_script(agreed_outcomes("report a", "report b"), fake).await;
        let text = result.expect("success");
        assert!(text.contains("alpha is true"), "{text}");
        assert!(
            !text.contains("unconsolidated"),
            "successful consolidation must not hit the fail-open marker: {text}"
        );
    }

    #[tokio::test]
    #[serial_test::serial(provider, drain)] // serializes the process-global fake provider (providers::PROVIDER) + shutdown drain flag
    async fn test_consolidation_single_parseable_source_skips_grouping() {
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        // ≥2 valid responses but only one produced parseable claims: a
        // grouping pass over a single source can't yield agreement brackets
        // or contradictions — the flat claim list + raw dumps are delivered
        // without calling the provider.
        let fake = FakeProvider::new(); // no script — any provider call would fail
        let provider: Arc<dyn crate::Provider> = Arc::new(fake);
        let _provider_guard = install_fake_provider(provider);
        let ws = test_ws("/tmp/test_ws");
        let outcomes = vec![
            AnalystOutcome::Findings {
                raw: "sole report".into(),
                findings: findings(vec![("alpha is true", "url1", "high")]),
            },
            AnalystOutcome::ParseFailed {
                raw: "unparseable report".into(),
                failure: "parse failed".into(),
            },
        ];
        let result = consolidate_findings(
            &ws,
            "test question",
            outcomes,
            "test_round",
            std::time::Instant::now() + std::time::Duration::from_mins(60),
        )
        .await;
        let text = result
            .expect("single parseable source succeeds")
            .expect("a live round delivers its text");
        assert!(
            text.contains("only one analyst produced parseable claims — grouping skipped"),
            "skip marker must be present: {text}"
        );
        assert!(text.contains("alpha is true"), "{text}");
        assert!(text.contains("### Report from Analyst 2"), "{text}");
        assert!(text.contains("unparseable report"), "{text}");
    }

    #[tokio::test]
    #[serial_test::serial(provider, drain)] // serializes the process-global fake provider (providers::PROVIDER) + shutdown drain flag
    async fn test_consolidation_fail_open_mixed_failure_classes() {
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        // Mixed transport + empty-response failures exercise both failure
        // arms of the grouping retry loop; exhaustion still fails open.
        // Script: transport error, then two empty responses.
        let fake = FakeProvider::new()
            .err(
                crate::retry::FailureClass::Transport,
                "error reading response body",
            )
            .ok("")
            .ok("");
        let result = consolidate_with_script(agreed_outcomes("only usable", "second"), fake).await;
        let text = result.expect("mixed failures still fail open");
        assert!(
            text.contains("unconsolidated — consolidation failed"),
            "{text}"
        );
        assert!(text.contains("only usable"), "{text}");
        assert!(text.contains("second"), "{text}");
    }

    /// Extraction fails for every valid analyst → the raw reports are
    /// delivered with the extraction-failure marker (fail-open at the
    /// extraction step, not the synthesis step).
    #[tokio::test]
    #[serial_test::serial(provider, drain)] // serializes the process-global fake provider (providers::PROVIDER) + shutdown drain flag
    async fn test_extraction_fail_open_delivers_raw_reports() {
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        let fake = FakeProvider::new()
            .err(
                crate::retry::FailureClass::Transport,
                "error reading response body",
            )
            .err(
                crate::retry::FailureClass::Transport,
                "error reading response body",
            )
            .err(
                crate::retry::FailureClass::Transport,
                "error reading response body",
            )
            .err(
                crate::retry::FailureClass::Transport,
                "error reading response body",
            )
            .err(
                crate::retry::FailureClass::Transport,
                "error reading response body",
            )
            .err(
                crate::retry::FailureClass::Transport,
                "error reading response body",
            );
        let provider: Arc<dyn crate::Provider> = Arc::new(fake);
        let _provider_guard = install_fake_provider(provider);
        let ws = test_ws("/tmp/test_ws");
        let runs = vec![
            test_analyst_run(&ws, Some("report one")),
            test_analyst_run(&ws, Some("report two")),
            test_analyst_run(&ws, None),
        ];
        let result = consolidate_analyst_runs(
            &ws,
            "test question",
            runs,
            std::time::Instant::now() + Duration::from_mins(1),
            "test_round",
        )
        .await;
        let text = result
            .expect("extraction fail-open must deliver raw reports")
            .expect("a live round delivers its text");
        assert!(
            text.contains("unconsolidated — findings extraction failed"),
            "{text}"
        );
        assert!(text.contains("### Report from Analyst 1"), "{text}");
        assert!(text.contains("report one"), "{text}");
        assert!(text.contains("### Report from Analyst 2"), "{text}");
        assert!(text.contains("report two"), "{text}");
    }

    #[tokio::test]
    #[serial_test::serial(provider, drain)] // serializes the process-global fake provider (providers::PROVIDER) + shutdown drain flag
    async fn test_consolidation_async_envelope_carries_marker() {
        // The async dispatch path (tokio::spawn in AnalyzeTool::execute) builds
        // its envelope via SyncDurableCore::Analyze.build_async_message — this
        // test drives the REAL fail-open consolidation result through that
        // production builder (not a manual re-wrap), asserting the exact
        // envelope + marker shape that reaches the caller's agent channel.
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        let fake = FakeProvider::new()
            .err(crate::retry::FailureClass::Transport, "down")
            .err(crate::retry::FailureClass::Transport, "down")
            .err(crate::retry::FailureClass::Transport, "down");
        let result =
            consolidate_with_script(agreed_outcomes("raw report", "raw report 2"), fake).await;
        let envelope = crate::tools::SyncDurableCore::Analyze.build_async_message(
            "job123",
            &result,
            Some("/tmp/round-place"),
            &crate::workspace::test_ws("/tmp/round-delivery"),
        );
        assert!(envelope.contains("<analyze-tool-result>"), "{envelope}");
        assert!(
            envelope.contains("unconsolidated — consolidation failed"),
            "{envelope}"
        );
        assert!(envelope.contains("raw report"), "{envelope}");
        assert!(
            envelope.contains("\n\nJob: job123\n\nDirectory: /tmp/round-place\n\n"),
            "the place directory must follow the job id: {envelope}"
        );
        assert!(
            envelope.ends_with("</analyze-tool-result>"),
            "envelope must close: {envelope}"
        );
    }

    #[tokio::test]
    async fn async_envelope_wraps_sub_agent_errors() {
        // The async dispatch path's error branch: a failed sub-agent is
        // wrapped in the same envelope with the error text. The delivery
        // directory line is absent when the round ran in the caller's own
        // workspace.
        let envelope = crate::tools::SyncDurableCore::Analyze.build_async_message(
            "job123",
            &Err(anyhow::anyhow!("sub-agent exploded")),
            None,
            &crate::workspace::test_ws("/tmp/round-delivery"),
        );
        assert!(envelope.contains("<analyze-tool-result>"), "{envelope}");
        assert!(
            envelope.contains("An error occurred: sub-agent exploded"),
            "{envelope}"
        );
        assert!(
            !envelope.contains("Directory:"),
            "no directory line without a place: {envelope}"
        );
        assert!(
            envelope.ends_with("</analyze-tool-result>"),
            "envelope must close: {envelope}"
        );
    }

    /// The sync analyze path preserves tool output in full — the analyze tool
    /// overrides `preserve_full_output` so the consolidated analysis (and any
    /// appended verification section) is never sandwich-truncated.
    #[tokio::test]
    #[serial_test::serial(provider, drain)] // serializes the process-global fake provider (providers::PROVIDER) + shutdown drain flag
    async fn analyze_output_preserved_full_no_sandwich_truncation() {
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        let fake = FakeProvider::new()
            .err(crate::retry::FailureClass::Transport, "down")
            .err(crate::retry::FailureClass::Transport, "down")
            .err(crate::retry::FailureClass::Transport, "down");
        // A long raw report would overflow the shared 5 KB budget if truncated.
        let long_report = "lorem ipsum ".repeat(2_000);
        let result = consolidate_with_script(agreed_outcomes(&long_report, "second"), fake).await;
        let text = result.expect("fail-open must succeed");
        // The analyze tool's format_output returns the output verbatim — no
        // head/tail sandwich, so the marker and all findings reach the caller.
        let tool = AnalyzeTool::new(DispatchMode::Sync, Role::Engineer);
        let formatted = tool.format_output(&text);
        assert_eq!(
            formatted, text,
            "analyze output must be preserved in full (no sandwich truncation)"
        );
        assert!(
            formatted.contains("unconsolidated — consolidation failed"),
            "fail-open marker present: {formatted}"
        );
    }

    #[test]
    fn render_analyze_groups_renders_disputed_cross_references() {
        // A remainder item that contradicts a frozen group renders with
        // DISPUTED + a code-computed cross-reference (id-resolved — no text
        // equality).
        let outcomes = vec![
            AnalystOutcome::Findings {
                raw: "r1".into(),
                findings: findings(vec![("safe", "url1", "high")]),
            },
            AnalystOutcome::Findings {
                raw: "r2".into(),
                findings: findings(vec![("actually unsafe", "url2", "high")]),
            },
        ];
        let items = claims_per_agent(&outcomes);
        let table = crate::consensus::ItemTable::new(&items);
        let output = crate::consensus::GroupingOutput {
            summary: "One consensus, one dispute.".into(),
            groups: vec![crate::consensus::GroupingGroup {
                heading: "Safety".into(),
                contradiction: false,
                members: vec![crate::consensus::GroupingMember {
                    id: 0,
                    ..Default::default()
                }],
            }],
            ungrouped: vec![crate::consensus::GroupingMember {
                id: 1,
                ..Default::default()
            }],
        };
        let references = vec![crate::consensus::GroupingReference {
            group: 0,
            member: crate::consensus::GroupingMember {
                id: 1,
                ..Default::default()
            },
        }];
        let text = render_analyze_groups(&output, &references, &table, 2, &outcomes, "");
        assert!(
            text.contains("Agent 1: actually unsafe [DISPUTED — contradicts group 0 \"Safety\"]"),
            "reference must render with DISPUTED + cross-ref: {text}"
        );
    }

    #[tokio::test]
    #[serial_test::serial(provider, drain)] // serializes the process-global fake provider (providers::PROVIDER) + shutdown drain flag
    async fn test_consolidation_partial_success_with_remainder() {
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        // Round 1 freezes one group and leaves one item ungrouped; round 2's
        // delta leaves that item ungrouped → zero-progress stop. The frozen
        // group + deterministic remainder are delivered (partial success —
        // never the raw-dump fallback), and the round-1 summary is final.
        let outcomes = vec![
            AnalystOutcome::Findings {
                raw: "r1".into(),
                findings: findings(vec![
                    ("alpha is true", "url1", "high"),
                    ("beta is true", "url2", "medium"),
                ]),
            },
            AnalystOutcome::Findings {
                raw: "r2".into(),
                findings: findings(vec![("alpha is true", "url1", "high")]),
            },
            AnalystOutcome::NoResponse("analyst produced no response".into()),
        ];
        // ids: 0 = a0 "alpha is true", 1 = a0 "beta is true", 2 = a1 "alpha is true".
        let fake = FakeProvider::new()
            .ok(
                r#"{"summary":"alpha agreed, beta solo.","groups":[{"heading":"Alpha","contradiction":false,"members":[{"id":0},{"id":2}]}],"ungrouped":[{"id":1}]}"#,
            )
            .ok(r#"{"groups":[],"ungrouped":[{"id":1}]}"#);
        let result = consolidate_with_script(outcomes, fake).await;
        let text = result.expect("partial success must be delivered");
        assert!(
            text.contains("**Alpha** [2/2]"),
            "frozen group with code-computed bracket renders: {text}"
        );
        assert!(
            text.contains("**Other**") && text.contains("- Agent 0: beta is true"),
            "deterministic remainder renders in the ungrouped section: {text}"
        );
        assert!(
            text.contains("alpha agreed, beta solo."),
            "round-1 summary is final: {text}"
        );
        assert!(
            !text.contains("unconsolidated"),
            "partial success replaces the raw-dump fallback: {text}"
        );
    }

    /// Boot resume of a crashed async analyze must REUSE the existing job + roster
    /// (never re-spawn — the jobs.id PK would conflict AND freshly generated
    /// analyst ids would not match the stored roster rows, so outcomes would
    /// be lost). A single done slot reconstructs from its stored outcome
    /// (raw passthrough — no provider needed).
    ///
    /// Serialized with the drain-flag writers: `resume_analyze_round` consults the
    /// process-global drain flag and aborts early while it is set.
    #[tokio::test]
    #[serial_test::serial(drain)] // serializes the process-global drain flag
    async fn resume_analyze_round_reuses_existing_job() {
        crate::util::test::init_management_test_stores().await;
        let ws = test_ws("/tmp/test_ws_resume_analyze");
        let job_id = "analyze_job_resume_1";
        let agent_id = format!("analyze_{}_rs_0_analyst", ws.name);
        let conn = &crate::session::store().conn;
        // Pre-create the job row exactly as a crashed dispatch leaves it
        // (caller identity persisted on the job row; one done slot with a
        // stored outcome).
        crate::jobs::spawn_job(
            conn,
            job_id,
            "question?",
            &ws.name,
            "caller-user",
            "telegram",
            crate::Role::Assistant,
            &[crate::jobs::NewAgent {
                agent_id: agent_id.clone(),
                kind: crate::jobs::AgentKind::Analyst,
                idx: Some(0),
                task: "question?".to_string(),
            }],
            &crate::jobs::SpawnChild::Analyze,
            None,
            None,
            None,
        )
        .await
        .unwrap();
        crate::jobs::write_agent_outcome(
            conn,
            job_id,
            &agent_id,
            crate::jobs::RowStatus::Done,
            Some("completed analyst response"),
        )
        .await
        .unwrap();

        // Resume: must NOT hit the jobs.id PK conflict, must reuse the stored
        // roster slot, then terminalize into a durable envelope delivered to
        // the ORIGINAL caller (Assistant, not Manager).
        resume_analyze_round(job_id, &ws, None).await;

        let job_rows = conn
            .query(
                "SELECT id FROM jobs WHERE id = ?1",
                crate::db::params![job_id],
            )
            .await
            .unwrap();
        assert_eq!(job_rows.len(), 0, "resumed job must be terminalized");
        let pending = conn
            .query(
                "SELECT envelope FROM pending_jobs WHERE id = ?1",
                crate::db::params![job_id],
            )
            .await
            .unwrap();
        assert_eq!(pending.len(), 1, "envelope persisted with the job id");
        let envelope: crate::agent::message_router::AgentJob =
            serde_json::from_str(&pending[0].get::<String>(0).unwrap()).unwrap();
        assert_eq!(
            envelope.role,
            crate::Role::Assistant,
            "resumed envelope routes to the original caller role, not Manager"
        );
        assert_eq!(envelope.user_name, "caller-user");
    }

    /// A place round whose recorded directory is gone must END with a plain
    /// failure — the job terminalizes and the caller receives the reason, never
    /// left launched and silent forever.
    #[tokio::test]
    #[serial_test::serial(drain)] // serializes the process-global drain flag
    async fn resume_place_round_with_a_vanished_directory_fails_loudly() {
        crate::util::test::init_management_test_stores().await;
        let ws = test_ws("/tmp/test_ws_resume_place");
        let job_id = "analyze_job_resume_place";
        let place = "/definitely/not/here/analyze-place";
        let conn = &crate::session::store().conn;
        crate::jobs::spawn_job(
            conn,
            job_id,
            "question?",
            &ws.name,
            "caller-user",
            "telegram",
            crate::Role::Assistant,
            &[crate::jobs::NewAgent {
                agent_id: format!("{job_id}_analyst"),
                kind: crate::jobs::AgentKind::Analyst,
                idx: Some(0),
                task: "question?".to_string(),
            }],
            &crate::jobs::SpawnChild::Analyze,
            None,
            Some(place),
            None,
        )
        .await
        .unwrap();

        resume_analyze_round(job_id, &ws, Some(place)).await;

        let job_rows = conn
            .query(
                "SELECT id FROM jobs WHERE id = ?1",
                crate::db::params![job_id],
            )
            .await
            .unwrap();
        assert_eq!(job_rows.len(), 0, "the round must terminalize, not hang");
        let pending = conn
            .query(
                "SELECT envelope FROM pending_jobs WHERE id = ?1",
                crate::db::params![job_id],
            )
            .await
            .unwrap();
        assert_eq!(pending.len(), 1, "the failure is delivered to the caller");
        let envelope: crate::agent::message_router::AgentJob =
            serde_json::from_str(&pending[0].get::<String>(0).unwrap()).unwrap();
        assert_eq!(
            envelope.workspace_name, ws.name,
            "the delivery key stays the caller's own workspace, never the place"
        );
        assert!(
            envelope.content.contains("no longer available"),
            "{}",
            envelope.content
        );
        assert!(
            envelope.content.contains(&format!("Directory: {place}")),
            "{}",
            envelope.content
        );
    }

    /// A provider that parks every chat call until the test opens the gate, so
    /// the round's durable state can be observed while it is still running.
    /// The Assistant consumer the delivered envelope routes into is parked
    /// indefinitely: it would otherwise reclaim (delete) the pending row before
    /// the test can read it.
    struct GatedProvider {
        calls: std::sync::Mutex<Vec<String>>,
        open: tokio::sync::watch::Receiver<bool>,
    }

    #[async_trait::async_trait]
    impl crate::Provider for GatedProvider {
        async fn chat_scoped(
            &self,
            request: crate::ChatRequest,
        ) -> Result<crate::ChatResponse, crate::providers::ScopedCallError> {
            self.calls.lock().unwrap_poison().push(
                request
                    .messages
                    .iter()
                    .map(|m| format!("{m:?}"))
                    .collect::<Vec<_>>()
                    .join("\n"),
            );
            if request.meta.as_ref().is_some_and(|m| m.role == "assistant") {
                std::future::pending::<()>().await;
            }
            let mut open = self.open.clone();
            while !*open.borrow_and_update() {
                if open.changed().await.is_err() {
                    break;
                }
            }
            Ok(crate::ChatResponse {
                text: Some("ANALYST FINDING".to_string()),
                ..Default::default()
            })
        }
    }

    /// End-to-end for an ANALYST place round: the analysts run in the place
    /// directory's synthetic workspace while the durable job and the delivered
    /// envelope stay keyed to the caller's own workspace, and `jobs.exec_dir`
    /// records the directory really used (so a restart resumes into it). The
    /// gated provider parks the round until the durable row has been observed.
    #[tokio::test]
    #[serial_test::serial(provider, drain)]
    async fn place_round_records_exec_dir_and_delivers_to_the_caller() {
        crate::util::test::init_management_test_stores().await;
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        let (gate_tx, gate_rx) = tokio::sync::watch::channel(false);
        let provider = Arc::new(GatedProvider {
            calls: std::sync::Mutex::new(Vec::new()),
            open: gate_rx,
        });
        let _provider_guard = install_fake_provider(provider.clone());

        let dir = tempfile::tempdir().unwrap();
        // The spelling a resolved place stores (see `stored_workspace_path`):
        // canonical, without the platform's verbatim prefix.
        let place_path =
            crate::util::strip_verbatim_prefix(&std::fs::canonicalize(dir.path()).unwrap())
                .to_str()
                .unwrap()
                .to_string();
        let caller = test_ws("/tmp/test_ws_analyze_place");

        // The admin Assistant's tool path: a place-bearing analyze call (the
        // caller identity the tool reads from its task-locals).
        let ack = crate::agent::CURRENT_TOOL_USER_NAME
            .scope("admin".to_string(), async {
                crate::agent::CURRENT_TOOL_CHANNEL
                    .scope("gui".to_string(), async {
                        AnalyzeTool::admin(Role::Assistant)
                            .execute(
                                &caller,
                                json!({ "message": "what is here", "place": place_path }),
                            )
                            .await
                    })
                    .await
            })
            .await
            .expect("dispatch acked");
        let job_id = ack
            .split("job ")
            .nth(1)
            .and_then(|rest| rest.split(')').next())
            .expect("the ack names the job id")
            .to_string();

        // The round parks on the gated provider: the job row exists and records
        // the place directory while the durable delivery key stays the caller's.
        let conn = &crate::session::store().conn;
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        let (workspace_name, exec_dir) = loop {
            let rows = conn
                .query(
                    "SELECT workspace_name, exec_dir FROM jobs WHERE id = ?1",
                    crate::db::params![job_id.clone()],
                )
                .await
                .unwrap();
            if let Some(row) = rows.first() {
                break (
                    row.get::<String>(0).unwrap(),
                    row.get::<Option<String>>(1).unwrap(),
                );
            }
            assert!(
                std::time::Instant::now() < deadline,
                "the parked round's job row never appeared"
            );
            tokio::time::sleep(std::time::Duration::from_millis(25)).await;
        };
        assert_eq!(
            workspace_name, caller.name,
            "the durable delivery key never moves"
        );
        assert_eq!(
            exec_dir.as_deref(),
            Some(place_path.as_str()),
            "jobs.exec_dir records the directory the round really ran in"
        );

        gate_tx.send(true).unwrap();

        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
        let envelope = loop {
            let rows = conn
                .query(
                    "SELECT envelope FROM pending_jobs WHERE id = ?1",
                    crate::db::params![job_id.clone()],
                )
                .await
                .unwrap();
            if let Some(row) = rows.first() {
                break serde_json::from_str::<crate::agent::message_router::AgentJob>(
                    &row.get::<String>(0).unwrap(),
                )
                .unwrap();
            }
            assert!(
                std::time::Instant::now() < deadline,
                "the place round never delivered its envelope"
            );
            tokio::time::sleep(std::time::Duration::from_millis(2)).await;
        };
        assert_eq!(envelope.workspace_name, caller.name);
        assert!(
            envelope.content.contains("ANALYST FINDING"),
            "{}",
            envelope.content
        );
        assert!(
            envelope
                .content
                .contains(&format!("Directory: {place_path}")),
            "{}",
            envelope.content
        );

        // The analysts really saw the place directory: its path is in their
        // system prompt, and their sessions are keyed to its synthetic
        // workspace — never the caller's.
        let calls = provider.calls.lock().unwrap_poison().join("\n");
        assert!(
            calls.contains(&place_path),
            "the analyst system prompt carries the execution workspace's path"
        );
        let place_name = crate::tools::delegation::workspace_at_dir(&place_path)
            .await
            .unwrap()
            .name;
        assert!(place_name.starts_with("dir-"), "got {place_name}");
        let place_sessions = conn
            .query(
                "SELECT agent_id FROM session_metadata WHERE workspace_name = ?1",
                crate::db::params![place_name],
            )
            .await
            .unwrap();
        assert!(
            !place_sessions.is_empty(),
            "the analysts' sessions are keyed to the place's synthetic workspace"
        );
        let caller_sessions = conn
            .query(
                "SELECT agent_id FROM session_metadata WHERE workspace_name = ?1",
                crate::db::params![caller.name],
            )
            .await
            .unwrap();
        assert!(
            caller_sessions.is_empty(),
            "nothing ran in the caller's own workspace"
        );
    }

    /// A `place` is inert for every holder but the admin's Assistant: the
    /// guest's tool keeps today's interface (no new error, even for a value the
    /// admin's tool refuses) and still runs the round in the caller's own
    /// workspace, even when the value names a real directory.
    #[tokio::test]
    #[serial_test::serial(provider, drain)]
    async fn a_place_is_inert_for_a_guest_assistant() {
        crate::util::test::init_management_test_stores().await;
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        let _provider_guard =
            install_fake_provider(Arc::new(FakeProvider::new().ok("ANALYST FINDING")));
        let caller = test_ws("/tmp/test_ws_guest_inert_place");
        let dir = tempfile::tempdir().unwrap();
        let place_path =
            crate::util::strip_verbatim_prefix(&std::fs::canonicalize(dir.path()).unwrap())
                .to_str()
                .unwrap()
                .to_string();

        let tool = AnalyzeTool::new(DispatchMode::Async, Role::Assistant);
        // A value the admin's tool refuses (a bogus name) must not become a new
        // error for the guest.
        for place in ["not_a_managed_workspace", place_path.as_str()] {
            let ack = tool
                .execute(&caller, json!({ "message": "q", "place": place }))
                .await
                .expect("an inert place must not surface a new error");
            assert!(ack.contains("dispatched"), "{ack}");
        }

        // Both rounds run in the caller's own workspace — never the place.
        let conn = &crate::session::store().conn;
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        loop {
            let rows = conn
                .query(
                    "SELECT COUNT(*) FROM session_metadata WHERE workspace_name = ?1",
                    crate::db::params![caller.name.clone()],
                )
                .await
                .unwrap();
            if rows[0].get::<i64>(0).unwrap() > 0 {
                break;
            }
            assert!(
                std::time::Instant::now() < deadline,
                "the guest's round never ran in the caller's own workspace"
            );
            tokio::time::sleep(std::time::Duration::from_millis(25)).await;
        }

        let place_name = crate::tools::delegation::workspace_at_dir(&place_path)
            .await
            .unwrap()
            .name;
        let place_sessions = conn
            .query(
                "SELECT agent_id FROM session_metadata WHERE workspace_name = ?1",
                crate::db::params![place_name],
            )
            .await
            .unwrap();
        assert!(
            place_sessions.is_empty(),
            "the inert place directory never ran anything"
        );
    }

    /// Run a dispatch tool the way the agent loop does — with the caller
    /// identity the tool reads from its task-locals (the same scoping the send
    /// harness uses).
    async fn dispatch_analyze(
        tool: &AnalyzeTool,
        ws: &Workspace,
        args: serde_json::Value,
    ) -> anyhow::Result<String> {
        crate::agent::CURRENT_TOOL_USER_NAME
            .scope("admin".to_string(), async {
                crate::agent::CURRENT_TOOL_CHANNEL
                    .scope("gui".to_string(), async { tool.execute(ws, args).await })
                    .await
            })
            .await
    }

    /// The `(kind, exec_dir)` a durable round's job row records, within a
    /// bounded wait — the spawned dispatch writes the row asynchronously.
    async fn round_row(job_id: &str) -> (String, Option<String>) {
        let conn = &crate::session::store().conn;
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        loop {
            let rows = conn
                .query(
                    "SELECT kind, exec_dir FROM jobs WHERE id = ?1",
                    crate::db::params![job_id],
                )
                .await
                .unwrap();
            if let Some(row) = rows.first() {
                return (
                    row.get::<String>(0).unwrap(),
                    row.get::<Option<String>>(1).unwrap(),
                );
            }
            assert!(
                std::time::Instant::now() < deadline,
                "no job row for {job_id} — the round was never created"
            );
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        }
    }

    /// Ruling 1: a folder name may never refuse a `deep` call. The `place` the
    /// admin's Assistant may name for `quick` is completely inert for `deep` —
    /// never a refusal, never a different execution root — and a deep-only
    /// holder, which is not offered `place` at all, is not tripped by one
    /// either: the round keeps the caller's own workspace.
    #[tokio::test]
    #[serial_test::serial(provider, channel_registry, drain)] // serializes the process-global fake provider + channel registry + shutdown drain flag
    async fn a_place_never_touches_a_deep_round() {
        use std::sync::Arc;

        crate::util::test::init_management_test_stores().await;
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        let _provider_guard =
            install_fake_provider(Arc::new(FakeProvider::new().ok("ANALYST FINDING")));
        crate::CHANNEL_REGISTRY.get_or_init(crate::ChannelRegistry::default);

        let home = tempfile::tempdir().unwrap();
        let caller = crate::workspace::test_ws_named(
            home.path().to_str().unwrap(),
            &crate::users::personal_workspace_name(crate::users::ADMIN_USER_NAME),
        );
        // The deep report lands in the asking Assistant's session; register the
        // consumer exactly as the running service's bootstrap leaves it.
        let assistant = crate::session::assistant_agent_id(crate::users::ADMIN_USER_NAME);
        let _assistant_rx = crate::agent::message_router::register_agent(&assistant);

        // A `place` the `quick` depth refuses: it is not a directory at all.
        let bogus = "/definitely/not/a/directory/xyz";
        let admin = AnalyzeTool::admin(Role::Assistant);
        let err = dispatch_analyze(
            &admin,
            &caller,
            json!({ "message": "quick q", "depth": "quick", "place": bogus }),
        )
        .await
        .expect_err("a quick round refuses a place that is not a directory");
        assert!(err.to_string().contains("does not exist"), "{err}");

        // The same argument is completely inert for `deep`: the round is
        // dispatched, so the value never turns into a refusal.
        let ack = dispatch_analyze(
            &admin,
            &caller,
            json!({ "message": "deep q", "depth": "deep", "place": bogus }),
        )
        .await
        .expect("a deep round is never refused for a place");
        assert!(ack.contains("Deep analysis dispatched"), "{ack}");
        let job_id = ack
            .split("job ")
            .nth(1)
            .and_then(|rest| rest.split(')').next())
            .expect("the deep ack names its job")
            .to_string();
        let (kind, exec_dir) = round_row(&job_id).await;
        assert_eq!(kind, "research", "a deep round is a research job");
        assert_eq!(
            exec_dir, None,
            "a deep round ignores the place: no exec_dir is recorded"
        );
        // The deep investigation is long-running and its completion is never
        // required: the round this test started is stopped the way the Running
        // Agents view stops one, so it never outlives the test.
        crate::research_cancel::cancel_research_run(&job_id).await;

        // A deep-only granted holder is not offered `place` at all; a caller
        // passing one still succeeds and the round keeps the caller's own
        // workspace as its execution root.
        let deep_only = AnalyzeTool::granted(&[AnalyzeDepth::Deep]);
        assert!(
            deep_only.parameters_schema()["properties"]
                .get("place")
                .is_none(),
            "a deep-only holder never declares a place"
        );
        let ack = dispatch_analyze(
            &deep_only,
            &caller,
            json!({ "message": "deep one", "place": bogus }),
        )
        .await
        .expect("a deep-only holder is not tripped by a place it never declares");
        assert!(ack.contains("Deep analysis dispatched"), "{ack}");
        let job_id = ack
            .split("job ")
            .nth(1)
            .and_then(|rest| rest.split(')').next())
            .expect("the deep ack names its job")
            .to_string();
        let (kind, exec_dir) = round_row(&job_id).await;
        assert_eq!(kind, "research");
        assert_eq!(
            exec_dir, None,
            "the round runs in the caller's own workspace, not the named folder"
        );
        crate::research_cancel::cancel_research_run(&job_id).await;
    }

    // ── Analyze cancel action ─────────────────────────────────────────────

    // The cancel tests' caller: a guest Assistant's async quick variant under
    // its own user/workspace identity. The message router and the shared test
    // store are process-global, so this identity is deliberately one no other
    // test routes to — a restart's replay of the store cannot leak another
    // test's message into this caller's receiver.
    const CANCEL_TEST_USER: &str = "cancel_test_user";
    const CANCEL_TEST_WS: &str = "cancel_test_ws";

    // The sibling-round test's own identity: its delivered result stays in the
    // shared store's `pending_jobs` for the rest of the process, so it must not
    // be one the other cancel tests' replay assertions observe.
    const CANCEL_SIBLING_USER: &str = "cancel_sibling_user";
    const CANCEL_SIBLING_WS: &str = "cancel_sibling_ws";

    /// The agent address of [`CANCEL_SIBLING_USER`]'s Assistant session.
    fn cancel_sibling_caller_id() -> String {
        crate::session::resolve_agent_id(
            CANCEL_SIBLING_USER,
            Role::Assistant.as_str(),
            CANCEL_SIBLING_WS,
        )
    }

    /// The agent address of [`CANCEL_TEST_USER`]'s Assistant session — the id
    /// these tests register as a consumer and the one a round's `jobs` row
    /// derives to.
    fn cancel_test_caller_id() -> String {
        crate::session::resolve_agent_id(CANCEL_TEST_USER, Role::Assistant.as_str(), CANCEL_TEST_WS)
    }

    /// Whether the store holds a row with this `id` in `table` — the cancel
    /// tests' window into the durable state a round leaves behind.
    async fn cancel_row_exists(table: &str, id: &str) -> bool {
        !crate::session::store()
            .conn
            .query(
                &format!("SELECT id FROM {table} WHERE id = ?1"),
                crate::db::params![id.to_string()],
            )
            .await
            .unwrap()
            .is_empty()
    }

    /// Run the cancel tests' tool the way the agent loop does — with the caller
    /// identity the tool reads from its task-locals, including the calling agent
    /// id the cancel action proves ownership with. The caller's workspace is the
    /// one the round's address derives from, so the session that dispatched the
    /// round is the one cancelling it.
    async fn run_as_caller(
        tool: &AnalyzeTool,
        ws: &Workspace,
        user: &str,
        caller_id: &str,
        args: serde_json::Value,
    ) -> anyhow::Result<String> {
        crate::agent::CURRENT_TOOL_USER_NAME
            .scope(user.to_string(), async {
                crate::agent::CURRENT_TOOL_CHANNEL
                    .scope("gui".to_string(), async {
                        crate::agent::CURRENT_TOOL_AGENT_ID
                            .scope(Some(caller_id.to_string()), async {
                                tool.execute(ws, args).await
                            })
                            .await
                    })
                    .await
            })
            .await
    }

    /// [`run_as_caller`] for [`CANCEL_TEST_USER`]'s own session.
    async fn run_as_cancel_test_caller(
        tool: &AnalyzeTool,
        ws: &Workspace,
        args: serde_json::Value,
    ) -> anyhow::Result<String> {
        run_as_caller(tool, ws, CANCEL_TEST_USER, &cancel_test_caller_id(), args).await
    }

    /// Records the level of every event emitted on the thread it is installed
    /// for, so a test can assert a path left no complaint behind.
    #[derive(Clone, Default)]
    struct LevelRecorder {
        levels: std::sync::Arc<std::sync::Mutex<Vec<tracing::Level>>>,
    }

    impl LevelRecorder {
        fn levels(&self) -> Vec<tracing::Level> {
            self.levels.lock().unwrap_poison().clone()
        }
    }

    impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for LevelRecorder {
        fn on_event(
            &self,
            event: &tracing::Event<'_>,
            _ctx: tracing_subscriber::layer::Context<'_, S>,
        ) {
            self.levels
                .lock()
                .unwrap_poison()
                .push(*event.metadata().level());
        }
    }

    /// A cancel stops a parked quick round: its `jobs` row and its queued result
    /// go, the fired signal stays observable while the round unwinds, the
    /// cancelled round delivers nothing to the caller, the analysts' own session
    /// records survive, and a start does not re-arm the round.
    ///
    /// `current_thread` on purpose: the WARN/ERROR capture below is
    /// thread-scoped, and a single-threaded runtime keeps the awaited cancel on
    /// the thread the capture was installed for.
    #[tokio::test(flavor = "current_thread")]
    #[serial_test::serial(provider, drain, recover_from_restart)]
    async fn cancel_stops_a_running_round_and_delivers_nothing() {
        crate::util::test::init_management_test_stores().await;
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        let (gate_tx, gate_rx) = tokio::sync::watch::channel(false);
        let _provider_guard = install_fake_provider(Arc::new(GatedProvider {
            calls: std::sync::Mutex::new(Vec::new()),
            open: gate_rx,
        }));
        let caller_id = cancel_test_caller_id();
        let mut rx = crate::agent::message_router::register_agent(&caller_id);
        let caller = crate::workspace::test_ws_named("/tmp/test_ws_analyze_cancel", CANCEL_TEST_WS);
        let tool = AnalyzeTool::new(DispatchMode::Async, Role::Assistant);

        let ack = run_as_cancel_test_caller(&tool, &caller, json!({ "message": "what changed" }))
            .await
            .expect("dispatch acked");
        let job_id = ack
            .split("job ")
            .nth(1)
            .and_then(|rest| rest.split(')').next())
            .expect("the ack names the job id")
            .to_string();
        assert!(ack.contains(&job_id), "{ack}");

        // The round parks on the gated provider: the jobs row and its analyst
        // roster are written while nothing has been delivered.
        let (kind, _exec_dir) = round_row(&job_id).await;
        assert_eq!(kind, "analyze", "a quick round is an analyze job");
        let conn = &crate::session::store().conn;
        let roster: Vec<String> = conn
            .query(
                "SELECT agent_id FROM agents WHERE job_id = ?1",
                crate::db::params![job_id.clone()],
            )
            .await
            .unwrap()
            .iter()
            .map(|r| r.get::<String>(0).unwrap())
            .collect();
        assert!(!roster.is_empty(), "the round has an analyst roster");

        // A synthetic call card belonging to the round, exactly as the round's
        // consolidation call registers one: the cancel must remove it so the
        // cancelled round stops being shown as running work.
        let _call_card = crate::agent::registry::NON_AGENT_CALLS.register(
            "consolidate",
            CANCEL_TEST_WS,
            Some(crate::agent::registry::ParentKey::AnalyzeRound(
                job_id.clone(),
            )),
            false,
            None,
        );

        let recorder = LevelRecorder::default();
        let answer = {
            use tracing_subscriber::layer::SubscriberExt as _;
            let _capture = tracing::subscriber::set_default(
                tracing_subscriber::registry().with(recorder.clone()),
            );
            run_as_cancel_test_caller(
                &tool,
                &caller,
                json!({ "action": "cancel", "job_id": job_id }),
            )
            .await
            .expect("the cancel is answered")
        };
        assert!(answer.contains("Cancelled the analysis round"), "{answer}");
        assert!(answer.contains(&job_id), "{answer}");
        // The cancel leaves no complaint about itself behind: nothing it emits
        // is a WARN or an ERROR (its own record is the INFO line plus the tool
        // call in the caller's session). `tracing::Level` orders by VERBOSITY
        // (ERROR < WARN < INFO < …), so "above WARN" means quieter than a
        // warning. The test runs on a single-threaded runtime so the capture
        // installed for this thread sees every event the awaited cancel emits.
        // The capture must have seen the path's own INFO record — an empty
        // capture could only make the assertion below pass vacuously.
        assert!(
            recorder.levels().contains(&tracing::Level::INFO),
            "the capture must observe the cancel's own record: {:?}",
            recorder.levels()
        );
        assert!(
            recorder
                .levels()
                .iter()
                .all(|level| *level > tracing::Level::WARN),
            "the cancel must leave no complaint behind: {:?}",
            recorder.levels()
        );

        // The stop is already durable while the round is still parked: the rows
        // are gone and the fired signal is still observable on the live entry.
        assert!(
            !cancel_row_exists("jobs", &job_id).await,
            "the cancelled round's jobs row must be gone"
        );
        assert!(
            !cancel_row_exists("pending_jobs", &job_id).await,
            "the cancelled round must leave no queued result"
        );
        assert!(
            crate::round_cancel::is_cancelled(&job_id),
            "the fired signal must be observable while the round unwinds"
        );
        assert!(
            !crate::agent::registry::NON_AGENT_CALLS
                .list()
                .iter()
                .any(|card| card.parent_key
                    == Some(crate::agent::registry::ParentKey::AnalyzeRound(
                        job_id.clone()
                    ))),
            "the cancelled round must stop being shown as running work"
        );

        // The round really is still running (its entry is registered until its
        // invocation exits), so its stop is still in force and a second cancel
        // of the same id is answered as stopped again.
        let answer = run_as_cancel_test_caller(
            &tool,
            &caller,
            json!({ "action": "cancel", "job_id": job_id }),
        )
        .await
        .expect("the repeat cancel is answered");
        assert!(answer.contains("Cancelled the analysis round"), "{answer}");

        // Let the parked round unwind, then assert nothing it might still do
        // resurrects its rows or reaches the caller.
        gate_tx.send(true).unwrap();
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
        while crate::round_cancel::is_registered(&job_id) {
            assert!(
                std::time::Instant::now() < deadline,
                "the cancelled round never unwound after its gate opened"
            );
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        }
        assert!(
            !cancel_row_exists("jobs", &job_id).await,
            "the unwound round must not resurrect its jobs row"
        );
        assert!(
            !cancel_row_exists("pending_jobs", &job_id).await,
            "the unwound round must not deliver a result"
        );
        assert!(
            rx.try_recv().is_err(),
            "a cancelled round must deliver nothing to the caller"
        );

        // The analysts' own records are not the round's to remove.
        for agent_id in &roster {
            assert_eq!(
                conn.query(
                    "SELECT agent_id FROM session_metadata WHERE agent_id = ?1",
                    crate::db::params![agent_id.clone()],
                )
                .await
                .unwrap()
                .len(),
                1,
                "the analyst {agent_id}'s session record must survive the cancel"
            );
        }

        // A start replays pending_jobs and re-arms resumable jobs: this round is
        // neither, and the replay delivers nothing for it.
        let resumable = crate::jobs::recover_from_restart().await.unwrap();
        assert!(
            !resumable.iter().any(|job| matches!(
                job,
                crate::jobs::ResumableJob::Analyze { job_id: id, .. } if id == &job_id
            )),
            "a cancelled round must not be re-armed by a start"
        );
        assert!(
            rx.try_recv().is_err(),
            "the pending replay must deliver nothing for the cancelled round"
        );
    }

    /// A cancel discards an analyze result already produced but not yet
    /// delivered: its queued row goes, nothing reaches the caller, the discard
    /// survives a start's replay, and a second cancel of the same id answers
    /// exactly like an id that never existed.
    #[tokio::test]
    #[serial_test::serial(provider, drain, recover_from_restart)]
    async fn cancel_discards_a_produced_result_and_survives_a_restart() {
        crate::util::test::init_management_test_stores().await;
        let caller_id = cancel_test_caller_id();
        let mut rx = crate::agent::message_router::register_agent(&caller_id);
        let caller =
            crate::workspace::test_ws_named("/tmp/test_ws_analyze_cancel_discard", CANCEL_TEST_WS);
        let tool = AnalyzeTool::new(DispatchMode::Async, Role::Assistant);
        let job_id = "analyze_cancel_discard_job";

        // A produced result with no round row left: the delivery is all that
        // remains, and it waits in `pending_jobs`.
        crate::jobs::complete_durable_job(
            job_id,
            format!("<analyze-tool-result>\n\nJob: {job_id}\n\nfindings"),
            crate::agent::message_router::MessageKind::AnalyzeToolResult,
            crate::Role::Assistant,
            CANCEL_TEST_USER,
            "gui",
            &caller.name,
            None,
        )
        .await;
        assert!(
            cancel_row_exists("pending_jobs", job_id).await,
            "the produced result waits in pending_jobs"
        );

        let answer = run_as_cancel_test_caller(
            &tool,
            &caller,
            json!({ "action": "cancel", "job_id": job_id }),
        )
        .await
        .expect("the cancel is answered");
        assert!(
            answer.contains("had already produced its result"),
            "{answer}"
        );
        assert!(answer.contains("discarded"), "{answer}");
        assert!(answer.contains(job_id), "{answer}");
        assert!(
            !cancel_row_exists("pending_jobs", job_id).await,
            "the discarded result's queued row is gone"
        );
        assert!(
            rx.try_recv().is_err(),
            "a discarded result must not be delivered"
        );

        // A start replays pending_jobs — the discarded row must not come back
        // and the replay must deliver nothing for it.
        crate::jobs::recover_from_restart().await.unwrap();
        assert!(
            !cancel_row_exists("pending_jobs", job_id).await,
            "the discard must survive a restart's pending replay"
        );
        assert!(
            rx.try_recv().is_err(),
            "the pending replay of a discarded result delivers nothing"
        );

        // The row is gone for good: a second cancel answers like an id that
        // never existed.
        let answer = run_as_cancel_test_caller(
            &tool,
            &caller,
            json!({ "action": "cancel", "job_id": job_id }),
        )
        .await
        .expect("the second cancel is answered");
        assert!(
            answer.contains("No analysis round of this session to cancel"),
            "{answer}"
        );
    }

    /// A cancelled round leaves no resumable row: a dispatch that observes the
    /// cancel at its entry returns before writing anything, delivers nothing, and
    /// releases the synthetic place's engine it would have held. (The write that
    /// WOULD have happened — a cancel landing inside the dispatch's own row
    /// transaction — is the gate `jobs::spawn_job` covers, pinned beside it in
    /// `spawn_job_refuses_a_cancelled_rounds_row`.)
    #[tokio::test]
    #[serial_test::serial(provider, drain)]
    async fn a_cancelled_dispatch_leaves_no_resumable_row() {
        crate::util::test::init_management_test_stores().await;
        let ws = test_ws("/tmp/test_ws_analyze_cancel_dispatch");
        let job_id = "analyze_cancel_dispatch_job";

        // The synthetic `dir-…` delegation place: the cancelled dispatch must
        // release the picker/watcher it created for it.
        let place_dir = std::env::temp_dir().join("mahbot_analyze_cancel_place");
        std::fs::create_dir_all(&place_dir).unwrap();
        let place =
            crate::workspace::test_ws_named(place_dir.to_str().unwrap(), "dir-cancel-place-test");
        crate::workspace_index::get_or_init_engine(&place.name, std::path::Path::new(&place.path))
            .unwrap();
        assert!(
            crate::workspace_index::get_engine_by_name(&place.name).is_some(),
            "the place's search engine must be registered before the dispatch"
        );

        let _cancel_guard = crate::round_cancel::register(job_id, None);
        crate::round_cancel::cancel(job_id);

        let delivered = crate::tools::SyncDurableCore::Analyze
            .dispatch_durable(
                &ws,
                Some(&place),
                "task",
                Role::Assistant,
                "user",
                "gui",
                job_id,
                None,
            )
            .await;
        assert!(delivered.is_none(), "a cancelled round delivers nothing");
        assert!(
            !cancel_row_exists("jobs", job_id).await,
            "the cancelled dispatch must write no row for a start to resume"
        );
        assert!(
            crate::workspace_index::get_engine_by_name(&place.name).is_none(),
            "a cancelled round must not leave its picker/watcher held"
        );
    }

    /// A cancel that lands between the boot scan which picked a round and the
    /// resume's own cancel-signal registration still stops that round: the mark
    /// the cancel left reaches the registration, so the resume runs nothing and
    /// a caller answered as stopped is never delivered to.
    #[tokio::test]
    #[serial_test::serial(drain, recover_from_restart)] // the resume consults the drain flag
    async fn a_cancel_before_a_resume_registers_still_stops_that_resume() {
        crate::util::test::init_management_test_stores().await;
        let ws = test_ws("/tmp/test_ws_analyze_cancel_before_resume");
        let job_id = "analyze_cancel_before_resume_job";
        let agent_id = format!("analyze_{}_rs_0_analyst", ws.name);
        let conn = &crate::session::store().conn;
        // A round a crashed dispatch left launched, with its stored outcome
        // (reconstructed by its roster slot — no provider needed): without the
        // cancel, this resume runs and delivers that outcome.
        crate::jobs::spawn_job(
            conn,
            job_id,
            "question?",
            &ws.name,
            "caller-user",
            "telegram",
            Role::Assistant,
            &[crate::jobs::NewAgent {
                agent_id: agent_id.clone(),
                kind: crate::jobs::AgentKind::Analyst,
                idx: Some(0),
                task: "question?".to_string(),
            }],
            &crate::jobs::SpawnChild::Analyze,
            None,
            None,
            None,
        )
        .await
        .unwrap();
        crate::jobs::write_agent_outcome(
            conn,
            job_id,
            &agent_id,
            crate::jobs::RowStatus::Done,
            Some("completed analyst response"),
        )
        .await
        .unwrap();

        // The cancel fires while nothing is registered for the round, and its
        // row removal has not landed yet: the boot scan has already listed this
        // row, so the resume below is the one that must observe the cancel.
        assert!(!crate::round_cancel::is_registered(job_id));
        crate::round_cancel::cancel(job_id);
        resume_analyze_round(job_id, &ws, None).await;

        // Asserted BEFORE the cancel action's own row removal below: an
        // uncancelled resume of this stored round writes exactly this row.
        let pending = conn
            .query(
                "SELECT id FROM pending_jobs WHERE id = ?1",
                crate::db::params![job_id],
            )
            .await
            .unwrap();
        assert_eq!(
            pending.len(),
            0,
            "a round cancelled before its resume must deliver nothing"
        );

        // The cancel action then removes the round's own rows, as it always
        // does, so the next start cannot re-arm it.
        crate::jobs::delete_round_rows(conn, job_id).await.unwrap();
        assert!(
            !cancel_row_exists("jobs", job_id).await,
            "the cancelled round must not be re-armed for the next start"
        );
    }

    /// Seed one launched analyze round row exactly as a dispatch writes it — the
    /// address shapes the ownership cases below need.
    async fn seed_round_row(
        ws: &Workspace,
        job_id: &str,
        user: &str,
        role: Role,
        launch: Option<&crate::Launch>,
    ) {
        crate::jobs::spawn_job(
            &crate::session::store().conn,
            job_id,
            "question?",
            &ws.name,
            user,
            "gui",
            role,
            &[],
            &crate::jobs::SpawnChild::Analyze,
            None,
            None,
            launch,
        )
        .await
        .unwrap();
    }

    /// Ownership is the calling SESSION, resolved through the crate's ONE
    /// delivery-address derivation ([`crate::jobs::target_agent_id`]) — so the two
    /// addresses that are NOT (user, role, workspace) are both covered: the
    /// workspace's shared Manager session, cancelling in a pipeline-driven turn
    /// that carries NO user at all, and a custom-role branch, whose address comes
    /// from the launch its row carries.
    #[tokio::test]
    #[serial_test::serial(drain, recover_from_restart)]
    async fn the_dispatchers_session_cancels_its_round_whatever_its_address_shape() {
        crate::util::test::init_management_test_stores().await;
        let ws = test_ws("/tmp/test_ws_analyze_cancel_addresses");

        // A Manager round, cancelled by the workspace's manager session in a turn
        // with no user (pipeline ticket notifications).
        let manager_job = "analyze_cancel_manager_job";
        seed_round_row(&ws, manager_job, "asking-user", Role::Manager, None).await;
        let manager_tool = AnalyzeTool::new(DispatchMode::Async, Role::Manager);
        let answer = run_as_caller(
            &manager_tool,
            &ws,
            "",
            &crate::session::manager_agent_id(&ws.name),
            json!({ "action": "cancel", "job_id": manager_job }),
        )
        .await
        .expect("the cancel is answered");
        assert!(answer.contains("Cancelled the analysis round"), "{answer}");
        assert!(
            !cancel_row_exists("jobs", manager_job).await,
            "the manager's own round is stopped"
        );

        // A custom-role branch's round, cancelled by that branch.
        let branch_job = "analyze_cancel_custom_job";
        let branch = "role_audit:1";
        let launch = crate::Launch {
            role: Role::Custom,
            name: "audit".to_string(),
            identifier: Some("1".to_string()),
            prompt: String::new(),
            tools: Vec::new(),
            agent_id: branch.to_string(),
            reply_to: None,
        };
        seed_round_row(&ws, branch_job, "branch-user", Role::Custom, Some(&launch)).await;
        let answer = run_as_caller(
            &AnalyzeTool::new(DispatchMode::Async, Role::Assistant),
            &ws,
            "branch-user",
            branch,
            json!({ "action": "cancel", "job_id": branch_job }),
        )
        .await
        .expect("the cancel is answered");
        assert!(answer.contains("Cancelled the analysis round"), "{answer}");
        assert!(
            !cancel_row_exists("jobs", branch_job).await,
            "the branch's own round is stopped"
        );
    }

    /// A placed round holds its synthetic `dir-…` search engine for the resume
    /// that reuses it — a round cut by drain has already returned, so its own
    /// exit released nothing. A cancel means there is no resume, so the cancel
    /// action releases the engine from the directory its row records — even once
    /// that directory itself is gone, since the engine is NAMED by it and the
    /// round's rows are swept.
    #[tokio::test]
    #[serial_test::serial(drain, recover_from_restart)]
    async fn a_cancel_releases_a_placed_rounds_workspace_index() {
        crate::util::test::init_management_test_stores().await;
        let caller = crate::workspace::test_ws_named(
            "/tmp/test_ws_analyze_cancel_place_release",
            CANCEL_TEST_WS,
        );
        let dir = tempfile::tempdir().unwrap();
        let place_path =
            crate::util::strip_verbatim_prefix(&std::fs::canonicalize(dir.path()).unwrap())
                .to_str()
                .unwrap()
                .to_string();
        // The engine the round left behind, exactly as a drain-cut leaves it.
        let place = crate::tools::delegation::workspace_at_dir(&place_path)
            .await
            .unwrap();
        crate::workspace_index::get_or_init_engine(&place.name, std::path::Path::new(&place.path))
            .unwrap();
        assert!(
            crate::workspace_index::get_engine_by_name(&place.name).is_some(),
            "the placed round's engine must be registered before the cancel"
        );
        let job_id = "analyze_cancel_place_release_job";
        crate::jobs::spawn_job(
            &crate::session::store().conn,
            job_id,
            "analyze task",
            CANCEL_TEST_WS,
            CANCEL_TEST_USER,
            "gui",
            Role::Assistant,
            &[],
            &crate::jobs::SpawnChild::Analyze,
            None,
            Some(&place_path),
            None,
        )
        .await
        .unwrap();
        // The directory is gone by the time the cancel lands — exactly the state
        // a drain-cut round leaves: it has already returned, its rows are swept,
        // and the engine's name is a derivation of the recorded path, not a
        // lookup that needs the directory to exist.
        dir.close().unwrap();
        assert!(
            !std::path::Path::new(&place_path).exists(),
            "the fixture's directory must be gone before the cancel"
        );

        let answer = run_as_cancel_test_caller(
            &AnalyzeTool::new(DispatchMode::Async, Role::Assistant),
            &caller,
            json!({ "action": "cancel", "job_id": job_id }),
        )
        .await
        .expect("the cancel is answered");
        assert!(answer.contains("Cancelled the analysis round"), "{answer}");
        assert!(
            crate::workspace_index::get_engine_by_name(&place.name).is_none(),
            "a cancelled round must not leave its picker/watcher held for a resume that cannot happen"
        );
        assert!(!cancel_row_exists("jobs", job_id).await);
    }

    /// A cancelled round starts no model call on its way out: both the findings
    /// extraction and the grouping pass stop at the fired signal, so the round
    /// reports its cancellation instead of a result — no extraction call and no
    /// re-registered `consolidate` call card.
    #[tokio::test]
    #[serial_test::serial(provider, drain)]
    async fn a_cancelled_round_starts_no_consolidation_call() {
        crate::util::test::init_management_test_stores().await;
        let ws = test_ws("/tmp/test_ws_analyze_cancel_consolidate");
        let round = "analyze_cancel_consolidate_round";
        // Every scripted call fails: a call the cancelled round started anyway
        // would surface as a fail-open deliverable instead of a cancellation.
        let fake =
            FakeProvider::new().err(crate::retry::FailureClass::Transport, "no call expected");
        let provider: Arc<dyn crate::Provider> = Arc::new(fake);
        let _provider_guard = install_fake_provider(provider);
        // The round's cancel signal, fired before either phase: the entry stays
        // alive for the duration of the test.
        let _cancel_guard = crate::round_cancel::register(round, None);

        // Extraction phase: the gate sits ahead of the analyst extraction calls.
        let runs = vec![
            test_analyst_run(&ws, Some("report one")),
            test_analyst_run(&ws, Some("report two")),
        ];
        crate::round_cancel::cancel(round);
        let result = consolidate_analyst_runs(
            &ws,
            "test question",
            runs,
            std::time::Instant::now() + Duration::from_mins(1),
            round,
        )
        .await
        .expect("a cancelled round is not a failure");
        assert!(
            result.is_none(),
            "a cancelled round produces no text: {result:?}"
        );

        // Grouping pass: the gate sits ahead of the shared grouping call.
        let result = consolidate_findings(
            &ws,
            "test question",
            agreed_outcomes("report one", "report two"),
            round,
            std::time::Instant::now() + Duration::from_mins(1),
        )
        .await
        .expect("a cancelled round is not a failure");
        assert!(
            result.is_none(),
            "a cancelled round starts no grouping call: {result:?}"
        );
    }

    /// A cancel landing AFTER a phase's entry gate stops that phase's own model
    /// calls instead of letting the retry loop re-ask: both the findings
    /// extraction and the consolidation pass are abandoned at the fired signal,
    /// so no attempt starts after the caller was answered.
    #[tokio::test]
    #[serial_test::serial(provider, drain)]
    async fn a_cancelled_round_asks_no_further_model_call() {
        // Parks the canceller deterministically at the phase's in-flight call
        // instead of sleeping blindly: waits (bounded) until the provider has
        // recorded `expected` calls.
        async fn wait_for_calls(provider: &GatedProvider, expected: usize) {
            let mut waited = 0;
            while provider.calls.lock().unwrap_poison().len() < expected {
                assert!(waited < 400, "the provider never recorded {expected} calls");
                waited += 1;
                tokio::time::sleep(std::time::Duration::from_millis(5)).await;
            }
        }

        /// The phase is already over: a retry loop that ignored the signal would
        /// re-ask right away, so watching the count across a settle window
        /// catches it wherever in the window it lands.
        async fn assert_no_further_calls(provider: &GatedProvider, expected: usize) {
            for _ in 0..20 {
                assert_eq!(
                    provider.calls.lock().unwrap_poison().len(),
                    expected,
                    "no further attempt may be asked after the cancel"
                );
                tokio::time::sleep(std::time::Duration::from_millis(5)).await;
            }
        }

        crate::util::test::init_management_test_stores().await;
        let _policy_guard =
            crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
        // Every call parks until the gate opens, then answers with a text that
        // never parses: were the phase not abandoned at the cancel, the retry
        // loop would re-ask within the test window.
        let (gate_tx, gate_rx) = tokio::sync::watch::channel(false);
        let provider = Arc::new(GatedProvider {
            calls: std::sync::Mutex::new(Vec::new()),
            open: gate_rx,
        });
        let _provider_guard = install_fake_provider(provider.clone() as Arc<dyn crate::Provider>);
        let ws = test_ws("/tmp/test_ws_analyze_cancel_no_further_call");
        let round = "analyze_cancel_no_further_call";
        let _cancel_guard = crate::round_cancel::register(round, None);

        // Part 1 — findings extraction: the two extraction calls are in flight
        // when the cancel lands, so neither may re-ask once the gate opens.
        let runs = vec![
            test_analyst_run(&ws, Some("report one")),
            test_analyst_run(&ws, Some("report two")),
        ];
        let (result, ()) = tokio::join!(
            consolidate_analyst_runs(
                &ws,
                "test question",
                runs,
                std::time::Instant::now() + Duration::from_mins(1),
                round,
            ),
            async {
                wait_for_calls(&provider, 2).await;
                crate::round_cancel::cancel(round);
                // Unblock the parked calls too: were the phase not abandoned at
                // the signal, the retry loop would re-ask and the count below
                // would grow.
                gate_tx.send(true).unwrap();
            },
        );
        assert!(
            result
                .expect("a cancelled round is not a failure")
                .is_none(),
            "a cancelled round produces no text"
        );
        assert_no_further_calls(&provider, 2).await;

        // Part 2 — consolidation: the grouping call is in flight when the cancel
        // lands, so no repair round may start. A fresh round key is registered —
        // the already-fired signal would be caught by the entry gate before any
        // call — and the gate is re-armed so that grouping call parks.
        let round2 = "analyze_cancel_no_further_call_consolidate";
        let _cancel_guard2 = crate::round_cancel::register(round2, None);
        gate_tx.send(false).unwrap();
        let (result, ()) = tokio::join!(
            consolidate_findings(
                &ws,
                "test question",
                agreed_outcomes("report one", "report two"),
                round2,
                std::time::Instant::now() + Duration::from_mins(1),
            ),
            async {
                wait_for_calls(&provider, 3).await;
                crate::round_cancel::cancel(round2);
                // Same as Part 1: the phase must stop at the signal, not after
                // the unblocked call returns.
                gate_tx.send(true).unwrap();
            },
        );
        assert!(
            result
                .expect("a cancelled round is not a failure")
                .is_none(),
            "a cancelled round starts no grouping call"
        );
        assert_no_further_calls(&provider, 3).await;
    }

    /// A cancelled round raises no warning about itself: the verification
    /// phase's cancel gate is its first statement, so the elapsed-window WARN —
    /// the very state the expired deadline below sets up — cannot fire for a
    /// round whose result is discarded.
    #[tokio::test(flavor = "current_thread")]
    #[expect(clippy::unchecked_time_subtraction)] // the expired deadline is this fixture's point
    async fn a_cancelled_round_raises_no_verification_warning() {
        let ws = test_ws("/tmp/test_ws_analyze_cancel_verify_warning");
        let round_key = "analyze_cancel_verify_warning_job";
        let _cancel_guard = crate::round_cancel::register(round_key, None);
        crate::round_cancel::cancel(round_key);

        let items = vec![
            vec!["the cache is warm".to_string()],
            vec!["the cache is cold".to_string()],
        ];
        let table = crate::consensus::ItemTable::new(&items);
        let output = crate::consensus::GroupingOutput {
            summary: "contested".to_string(),
            groups: vec![crate::consensus::GroupingGroup {
                heading: "cache state".to_string(),
                contradiction: true,
                members: Vec::new(),
            }],
            ungrouped: Vec::new(),
        };
        let recorder = LevelRecorder::default();
        let section = {
            use tracing_subscriber::layer::SubscriberExt as _;
            let _capture = tracing::subscriber::set_default(
                tracing_subscriber::registry().with(recorder.clone()),
            );
            verify_disputed_groups(
                &ws,
                "test question",
                &output,
                &table,
                &[],
                round_key,
                std::time::Instant::now() - round_timeout(),
            )
            .await
        };
        assert!(
            section.is_empty(),
            "a cancelled round renders no verification: {section}"
        );
        // The capture saw this phase's own record (the gate's INFO), so the
        // absence of a WARN below is not a vacuous pass.
        assert!(
            recorder.levels().contains(&tracing::Level::INFO),
            "the capture must observe the gate's own record: {:?}",
            recorder.levels()
        );
        assert!(
            !recorder.levels().contains(&tracing::Level::WARN),
            "a cancelled round must not warn about itself: {:?}",
            recorder.levels()
        );
    }

    /// Cancelling one round leaves the caller's other in-flight rounds alone: a
    /// second round of the same session keeps its produced result, its signal is
    /// never fired, and that result is still delivered by a start's replay.
    #[tokio::test]
    #[serial_test::serial(provider, drain, recover_from_restart)]
    async fn a_cancel_leaves_the_callers_other_rounds_delivering() {
        crate::util::test::init_management_test_stores().await;
        let conn = &crate::session::store().conn;
        let caller_id = cancel_sibling_caller_id();
        let mut rx = crate::agent::message_router::register_agent(&caller_id);
        let caller = crate::workspace::test_ws_named(
            "/tmp/test_ws_analyze_cancel_siblings",
            CANCEL_SIBLING_WS,
        );
        let tool = AnalyzeTool::new(DispatchMode::Async, Role::Assistant);

        // The caller's other round, already produced and waiting.
        let sibling = "analyze_cancel_sibling_job";
        crate::jobs::complete_durable_job(
            sibling,
            format!("<analyze-tool-result>\n\nJob: {sibling}\n\nsibling findings"),
            crate::agent::message_router::MessageKind::AnalyzeToolResult,
            crate::Role::Assistant,
            CANCEL_SIBLING_USER,
            "gui",
            &caller.name,
            None,
        )
        .await;

        // A live round of the same session, cancelled by the action.
        crate::jobs::spawn_job(
            conn,
            "analyze_cancel_first_job",
            "first question",
            &caller.name,
            CANCEL_SIBLING_USER,
            "gui",
            crate::Role::Assistant,
            &[],
            &crate::jobs::SpawnChild::Analyze,
            None,
            None,
            None,
        )
        .await
        .unwrap();
        let answer = run_as_caller(
            &tool,
            &caller,
            CANCEL_SIBLING_USER,
            &caller_id,
            json!({ "action": "cancel", "job_id": "analyze_cancel_first_job" }),
        )
        .await
        .expect("the cancel is answered");
        assert!(answer.contains("Cancelled the analysis round"), "{answer}");

        // The sibling round is untouched: its result still waits and its signal
        // was never fired.
        assert!(
            !crate::round_cancel::is_cancelled(sibling),
            "cancelling one round must not fire another round's signal"
        );
        assert!(
            cancel_row_exists("pending_jobs", sibling).await,
            "the sibling round's queued result must survive"
        );

        // …and it still reaches the caller on the start replay.
        crate::jobs::recover_from_restart().await.unwrap();
        let mut delivered = Vec::new();
        while let Ok(job) = rx.try_recv() {
            delivered.push(job.content);
        }
        assert!(
            delivered.iter().any(|content| content.contains(sibling)),
            "the sibling round's result is still delivered: {delivered:?}"
        );
        // The delivered row is not reclaimed by the replay: clear it so it
        // cannot reach another test's assertions.
        crate::jobs::delete_pending_job(conn, sibling)
            .await
            .unwrap();
    }

    /// The cancel action refuses everything that is not the caller's own live
    /// quick round — an unknown id, a `deep` investigation the caller owns, a
    /// deep investigation's queued report, a worker round's queued report, and
    /// another session's analyze round — and leaves each completely alone: no
    /// signal is fired and every refused row survives. The caller's own live
    /// quick row is stopped and deleted, after which the same id answers as
    /// nothing of the caller's.
    #[tokio::test]
    #[serial_test::serial(provider, drain, recover_from_restart)]
    async fn a_cancel_refuses_everything_that_is_not_the_callers_live_quick_round() {
        crate::util::test::init_management_test_stores().await;
        let conn = &crate::session::store().conn;
        let caller_id = cancel_sibling_caller_id();
        let _rx = crate::agent::message_router::register_agent(&caller_id);
        let caller = crate::workspace::test_ws_named(
            "/tmp/test_ws_analyze_cancel_refuse",
            CANCEL_SIBLING_WS,
        );
        let tool = AnalyzeTool::new(DispatchMode::Async, Role::Assistant);

        // An id that names nothing is answered without touching anything.
        let answer = run_as_caller(
            &tool,
            &caller,
            CANCEL_SIBLING_USER,
            &caller_id,
            json!({ "action": "cancel", "job_id": "no_such_cancel_round" }),
        )
        .await
        .expect("an unknown id is answered");
        assert!(
            answer.contains("No analysis round of this session to cancel"),
            "{answer}"
        );
        assert!(
            !crate::round_cancel::is_cancelled("no_such_cancel_round"),
            "a refused cancel must not fire a signal"
        );

        // A `deep` investigation is a research-kind row — out of the action's
        // scope even when the caller's own identity owns it.
        crate::jobs::spawn_job(
            conn,
            "deep_cancel_round",
            "deep question",
            &caller.name,
            CANCEL_SIBLING_USER,
            "gui",
            crate::Role::Assistant,
            &[],
            &crate::jobs::SpawnChild::Research,
            None,
            None,
            None,
        )
        .await
        .unwrap();
        let answer = run_as_caller(
            &tool,
            &caller,
            CANCEL_SIBLING_USER,
            &caller_id,
            json!({ "action": "cancel", "job_id": "deep_cancel_round" }),
        )
        .await
        .expect("a deep round is answered");
        assert!(
            answer.contains("No analysis round of this session to cancel"),
            "{answer}"
        );
        assert!(
            !crate::round_cancel::is_cancelled("deep_cancel_round"),
            "a refused cancel must not fire a signal"
        );
        assert!(
            cancel_row_exists("jobs", "deep_cancel_round").await,
            "a refused deep round is left in place"
        );

        // Another session's analyze round is refused and untouched.
        crate::jobs::spawn_job(
            conn,
            "someone_elses_analyze",
            "their question",
            "mahbot",
            "someone-else",
            "gui",
            crate::Role::Engineer,
            &[],
            &crate::jobs::SpawnChild::Analyze,
            None,
            None,
            None,
        )
        .await
        .unwrap();
        let answer = run_as_caller(
            &tool,
            &caller,
            CANCEL_SIBLING_USER,
            &caller_id,
            json!({ "action": "cancel", "job_id": "someone_elses_analyze" }),
        )
        .await
        .expect("another session's round is answered");
        assert!(
            answer.contains("No analysis round of this session to cancel"),
            "{answer}"
        );
        assert!(
            !crate::round_cancel::is_cancelled("someone_elses_analyze"),
            "a refused cancel must not fire a signal"
        );
        assert!(
            cancel_row_exists("jobs", "someone_elses_analyze").await,
            "another session's round is left in place"
        );

        // Reports queued for THIS caller: only the round's own kind
        // distinguishes them from a quick round's result, and a deep
        // investigation's report and a worker round's report are not this
        // action's to destroy.
        for (job_id, kind) in [
            (
                "cancel_refuses_deep_report",
                crate::agent::message_router::MessageKind::ResearchResult,
            ),
            (
                "cancel_refuses_worker_report",
                crate::agent::message_router::MessageKind::ImplementResult,
            ),
        ] {
            crate::jobs::complete_durable_job(
                job_id,
                format!("<report>\n\nJob: {job_id}\n\nreport"),
                kind,
                crate::Role::Assistant,
                CANCEL_SIBLING_USER,
                "gui",
                &caller.name,
                None,
            )
            .await;
            let answer = run_as_caller(
                &tool,
                &caller,
                CANCEL_SIBLING_USER,
                &caller_id,
                json!({ "action": "cancel", "job_id": job_id }),
            )
            .await
            .expect("other queued work is answered");
            assert!(
                answer.contains("No analysis round of this session to cancel"),
                "{answer}"
            );
            assert!(
                !crate::round_cancel::is_cancelled(job_id),
                "a refused cancel must not fire a signal"
            );
            assert!(
                cancel_row_exists("pending_jobs", job_id).await,
                "{kind:?} queued for the caller must survive"
            );
        }

        // The caller's own live quick round: a live round holds its
        // cancel-signal entry for the duration of its invocation, so register
        // one the way a dispatch would. The first cancel stops it, fires its
        // signal and deletes the row; once the invocation exits (the guard
        // drops) the same id answers like one that never existed.
        crate::jobs::spawn_job(
            conn,
            "mine_cancel_round",
            "my question",
            &caller.name,
            CANCEL_SIBLING_USER,
            "gui",
            crate::Role::Assistant,
            &[],
            &crate::jobs::SpawnChild::Analyze,
            None,
            None,
            None,
        )
        .await
        .unwrap();
        let mine_round = crate::round_cancel::register("mine_cancel_round", Some(&caller_id));
        let answer = run_as_caller(
            &tool,
            &caller,
            CANCEL_SIBLING_USER,
            &caller_id,
            json!({ "action": "cancel", "job_id": "mine_cancel_round" }),
        )
        .await
        .expect("the caller's own round is answered");
        assert!(answer.contains("Cancelled the analysis round"), "{answer}");
        assert!(
            !cancel_row_exists("jobs", "mine_cancel_round").await,
            "the caller's own cancelled round is deleted"
        );
        assert!(
            crate::round_cancel::is_cancelled("mine_cancel_round"),
            "the caller's own live round has its signal fired"
        );
        drop(mine_round);

        let answer = run_as_caller(
            &tool,
            &caller,
            CANCEL_SIBLING_USER,
            &caller_id,
            json!({ "action": "cancel", "job_id": "mine_cancel_round" }),
        )
        .await
        .expect("the second cancel is answered");
        assert!(
            answer.contains("No analysis round of this session to cancel"),
            "{answer}"
        );

        // Leave nothing behind for a sibling test's `recover_from_restart`
        // replay: every queued report and every launched row this test created
        // goes.
        for report in ["cancel_refuses_deep_report", "cancel_refuses_worker_report"] {
            crate::jobs::delete_pending_job(conn, report).await.unwrap();
        }
        for launched in ["deep_cancel_round", "someone_elses_analyze"] {
            crate::jobs::terminalize_job(conn, launched).await.unwrap();
        }
    }

    /// Nothing new is started for a cancelled round: an analyst that registers
    /// AFTER the round's cancel signal fired is pre-cancelled at registration.
    /// The late-register race is reachable for an analyze round because its
    /// verification analysts are dispatched after the analysts it started with.
    #[tokio::test]
    async fn a_late_registering_analyst_of_a_cancelled_round_is_pre_cancelled() {
        crate::util::test::init_management_test_stores().await;
        let ws = test_ws("/tmp/test_ws_analyze_cancel_late_register");
        let job_id = "analyze_late_register_round";
        let round = crate::round_cancel::register(job_id, None);
        let analyst = |id: &str| {
            crate::Agent::new(
                id.to_string(),
                Role::Analyst,
                &ws,
                None,
                String::new(),
                String::new(),
                false,
                Some(crate::agent::registry::ParentKey::AnalyzeRound(
                    job_id.to_string(),
                )),
                Some("round question".to_string()),
                None,
            )
        };

        assert!(
            !analyst("analyze_late_register_first").is_cancelled(),
            "a round's analyst runs while its round is live"
        );
        crate::round_cancel::cancel(job_id);
        assert!(
            analyst("analyze_late_register_late").is_cancelled(),
            "an analyst the cancelled round dispatches afterwards must be pre-cancelled"
        );

        // The entry belongs to the round's own invocation: once that invocation
        // exits, a fresh round is not affected by the gone round's signal.
        drop(round);
        assert!(
            !analyst("analyze_late_register_fresh").is_cancelled(),
            "a fresh round's analyst is not pre-cancelled by a gone round's signal"
        );
    }

    /// A dangling cancel frame is re-executed, never bound to an unrelated
    /// analyze job: the core resolution refuses any `analyze` call carrying an
    /// `action`, while the plain dispatch form still resolves to the quick
    /// round.
    #[test]
    fn a_cancel_frame_is_never_resolved_to_a_dispatch() {
        assert!(
            crate::tools::SyncDurableCore::from_call(
                "analyze",
                &json!({ "action": "cancel", "job_id": "abc" }),
            )
            .is_none(),
            "a dangling cancel frame must be re-executed, never resolved to a dispatch"
        );
        assert!(
            matches!(
                crate::tools::SyncDurableCore::from_call("analyze", &json!({ "message": "q" })),
                Some(crate::tools::SyncDurableCore::Analyze)
            ),
            "a plain analyze call still resolves to its quick round"
        );
    }

    /// The cancel action is offered exactly where a quick async round can be
    /// dispatched: its `action`/`job_id` properties and the cancel note appear
    /// on the admin Assistant, the async Manager and a quick-granted holder, and
    /// nowhere else.
    #[test]
    fn the_cancel_action_is_offered_only_where_a_quick_round_can_be_dispatched() {
        let note = load_prompt("tool/analyze_cancel.md");
        for tool in [
            AnalyzeTool::admin(Role::Assistant),
            AnalyzeTool::new(DispatchMode::Async, Role::Manager),
            AnalyzeTool::granted(&[AnalyzeDepth::Quick]),
        ] {
            let description = tool.description();
            let schema = tool.parameters_schema();
            assert!(
                schema["properties"]
                    .get(crate::tools::analyze::ACTION_ARG)
                    .is_some(),
                "a holder that can dispatch a quick round must offer the cancel action: \
                 {description}"
            );
            assert!(
                schema["properties"]
                    .get(crate::tools::analyze::JOB_ID_ARG)
                    .is_some(),
                "a cancelling holder must declare the round to cancel: {description}"
            );
            assert!(
                description.contains(&note),
                "the cancel note must be declared where the action is: {description}"
            );
            // A cancel call carries no dispatch argument, so the requirement is
            // per-branch: `message` (+ `depth`) to dispatch, `action` + `job_id`
            // to cancel — never a flat `required` list that would demand both.
            let branches = schema["oneOf"]
                .as_array()
                .expect("a cancelling holder's schema states the two call forms");
            assert!(
                branches[0]["required"]
                    .as_array()
                    .is_some_and(|required| required.iter().any(|field| field == "message")),
                "the dispatch branch requires `message`: {schema}"
            );
            assert_eq!(
                branches[1]["required"],
                json!([
                    crate::tools::analyze::ACTION_ARG,
                    crate::tools::analyze::JOB_ID_ARG
                ]),
                "the cancel branch requires the action and the round to cancel: {schema}"
            );
            assert!(
                schema.get("required").is_none(),
                "a cancelling holder must not also state a flat required list: {schema}"
            );
        }
        for tool in [
            AnalyzeTool::new(DispatchMode::Sync, Role::Engineer),
            AnalyzeTool::new(DispatchMode::Sync, Role::Maintainer),
            AnalyzeTool::granted(&[AnalyzeDepth::Deep]),
        ] {
            let description = tool.description();
            let schema = tool.parameters_schema();
            assert!(
                schema["properties"]
                    .get(crate::tools::analyze::ACTION_ARG)
                    .is_none(),
                "a holder that cannot cancel must not declare the action: {description}"
            );
            assert!(
                schema["properties"]
                    .get(crate::tools::analyze::JOB_ID_ARG)
                    .is_none(),
                "a holder that cannot cancel must not declare a job id: {description}"
            );
            assert!(
                !description.contains(&note),
                "the cancel note must not be declared where the action is not: {description}"
            );
            assert!(
                schema.get("oneOf").is_none() && schema["required"].is_array(),
                "a holder that cannot cancel keeps the plain required list: {schema}"
            );
        }
    }

    /// The same class `send`'s sweep guards, for the depth choice: a holder is
    /// never told about a depth it does not have (its own note may name the one
    /// it holds), the `depth` argument exists only where a choice is declared,
    /// and `place` exists only where it means something. Swept over every holder,
    /// so a third depth joins the sweep by existing.
    #[test]
    fn no_holder_reads_a_depth_it_does_not_have() {
        for mask in 1u32..(1 << AnalyzeDepth::ALL.len()) {
            let kinds: Vec<AnalyzeDepth> = AnalyzeDepth::ALL
                .iter()
                .enumerate()
                .filter(|(bit, _)| mask & (1 << bit) != 0)
                .map(|(_, depth)| *depth)
                .collect();
            let tool = AnalyzeTool::granted(&kinds);
            let declared = kinds.len() > 1;
            let schema = tool.parameters_schema();
            let text = format!("{}\n{schema}", tool.description());
            for depth in AnalyzeDepth::ALL {
                let labelled = format!("`{}`", depth.as_str());
                assert!(
                    !text.contains(&labelled) || kinds.contains(&depth),
                    "{kinds:?} is told about {labelled}, which it does not have: {text}"
                );
            }
            assert_eq!(
                schema["properties"].get(AnalyzeDepth::ARG).is_some(),
                declared,
                "{kinds:?}"
            );
            // The folder belongs to the quick depth alone.
            assert_eq!(
                schema["properties"].get("place").is_some(),
                kinds.contains(&AnalyzeDepth::Quick),
                "{kinds:?}"
            );
        }
    }

    /// Seed a caller-owned analyze job with a single roster slot. `outcome`
    /// `Some` marks the slot Done with that stored outcome (reconstructable);
    /// `None` leaves it launched.
    async fn seed_analyze_job(ws: &Workspace, job_id: &str, pin: &str, outcome: Option<&str>) {
        let conn = &crate::session::store().conn;
        let analyst_id = format!("{job_id}_analyst");
        crate::jobs::spawn_job(
            conn,
            job_id,
            "analyze task",
            &ws.name,
            "caller-user",
            "telegram",
            crate::Role::Engineer,
            &[crate::jobs::NewAgent {
                agent_id: analyst_id.clone(),
                kind: crate::jobs::AgentKind::Analyst,
                idx: Some(0),
                task: "analyze task".to_string(),
            }],
            &crate::jobs::SpawnChild::Analyze,
            Some(pin),
            None,
            None,
        )
        .await
        .unwrap();
        if let Some(outcome) = outcome {
            crate::jobs::write_agent_outcome(
                conn,
                job_id,
                &analyst_id,
                crate::jobs::RowStatus::Done,
                Some(outcome),
            )
            .await
            .unwrap();
        }
    }

    /// (k-extra) `find_owned_launched_jobs` returns only the caller's
    /// launched analyze/implement jobs — a research-kind and a ticket-phase-kind
    /// job owned by the same pin are ignored — newest first.
    #[tokio::test]
    async fn find_owned_launched_jobs_ignores_other_kinds() {
        crate::util::test::init_management_test_stores().await;
        let ws = test_ws("/tmp/test_ws_find_owned");
        let pin = "sync_analyze_find_pin";
        let conn = &crate::session::store().conn;

        // A research-kind job owned by the pin.
        crate::jobs::spawn_job(
            conn,
            "research_job",
            "research task",
            &ws.name,
            "caller",
            "telegram",
            crate::Role::Analyst,
            &[crate::jobs::NewAgent {
                agent_id: "research_a".to_string(),
                kind: crate::jobs::AgentKind::Analyst,
                idx: Some(0),
                task: "r".to_string(),
            }],
            &crate::jobs::SpawnChild::Research,
            Some(pin),
            None,
            None,
        )
        .await
        .unwrap();
        // A ticket-phase-kind job owned by the pin (requires a real ticket for
        // the FK).
        let ticket_id = crate::util::test::make_ticket(
            crate::pipeline::board::store(),
            &ws,
            "FindOwnedJob",
            crate::pipeline::board::TicketPhase::Analysis,
        )
        .await;
        crate::jobs::spawn_job(
            conn,
            "phase_job",
            "phase task",
            &ws.name,
            "caller",
            "telegram",
            crate::Role::Engineer,
            &[crate::jobs::NewAgent {
                agent_id: "phase_a".to_string(),
                kind: crate::jobs::AgentKind::Engineer,
                idx: Some(0),
                task: "p".to_string(),
            }],
            &crate::jobs::SpawnChild::Phase {
                phase: crate::pipeline::board::TicketPhase::Analysis,
                ticket_id: ticket_id.clone(),
            },
            Some(pin),
            None,
            None,
        )
        .await
        .unwrap();

        seed_analyze_job(&ws, "analyze_newer", pin, None).await;
        seed_analyze_job(&ws, "analyze_older", pin, None).await;
        // Backdate `analyze_older` so the newest-first assertion is
        // deterministic at `created_at` second precision.
        conn.execute(
            "UPDATE jobs SET created_at = ?1 WHERE id = ?2",
            crate::db::params!["2023-01-02T00:00:00+00:00", "analyze_older"],
        )
        .await
        .unwrap();

        let owned = crate::jobs::find_owned_launched_jobs(conn, pin)
            .await
            .unwrap();
        let ids: Vec<&str> = owned.iter().map(|j| j.id.as_str()).collect();
        assert_eq!(
            ids,
            vec!["analyze_newer", "analyze_older"],
            "only analyze jobs returned, newest first"
        );
    }

    // ── Analyze dispute-verification round helpers ────────────────────────

    /// Build a disputed consensus group over the given flat item ids.
    fn disputed_group(heading: &str, members: &[usize]) -> crate::consensus::GroupingGroup {
        crate::consensus::GroupingGroup {
            heading: heading.to_string(),
            contradiction: true,
            members: members
                .iter()
                .map(|id| crate::consensus::GroupingMember {
                    id: *id,
                    ..Default::default()
                })
                .collect(),
        }
    }

    #[test]
    fn verification_units_single_group_is_one_unit() {
        let groups = [disputed_group("Alpha", &[0, 1])];
        let units = verification_units(&groups.iter().collect::<Vec<_>>());
        assert_eq!(units.len(), 1, "exactly one disputed group → one unit");
        assert_eq!(units[0].len(), 1);
        assert_eq!(units[0][0].heading, "Alpha");
    }

    #[test]
    fn verification_units_splits_by_group_count_first_takes_extra() {
        let group = |h: &str| disputed_group(h, &[]);
        // 3 groups → 2 units: first takes the extra (2), second gets 1.
        let groups = [group("A"), group("B"), group("C")];
        let refs = groups.iter().collect::<Vec<_>>();
        let units = verification_units(&refs);
        assert_eq!(units.len(), 2);
        assert_eq!(units[0].len(), 2, "first part takes the extra group");
        assert_eq!(units[1].len(), 1);
        assert_eq!(units[0][1].heading, "B");
        assert_eq!(units[1][0].heading, "C");

        // 4 groups → 2 units of 2/2.
        let groups = [group("A"), group("B"), group("C"), group("D")];
        let refs = groups.iter().collect::<Vec<_>>();
        let units = verification_units(&refs);
        assert_eq!(units[0].len(), 2);
        assert_eq!(units[1].len(), 2);
    }

    #[test]
    fn synthesize_verification_target_merges_heading_claims_sources_contradictions() {
        // Two disputed groups, each with member claims carrying a source and a
        // self-reported contradiction (member caveat).
        let mut f1 = findings(vec![("alpha is true", "url1", "high")]);
        f1.claims[0].contradictions = vec!["src says alpha may be false".into()];
        let f2 = findings(vec![("alpha is false", "url2", "high")]);
        let mut f3 = findings(vec![("beta is true", "url3", "medium")]);
        f3.claims[0].contradictions = vec!["src says beta unknown".into()];
        let f4 = findings(vec![("beta is false", "url4", "medium")]);
        let outcomes = vec![
            AnalystOutcome::Findings {
                raw: "r1".into(),
                findings: f1,
            },
            AnalystOutcome::Findings {
                raw: "r2".into(),
                findings: f2,
            },
            AnalystOutcome::Findings {
                raw: "r3".into(),
                findings: f3,
            },
            AnalystOutcome::Findings {
                raw: "r4".into(),
                findings: f4,
            },
        ];
        let items = claims_per_agent(&outcomes);
        let table = crate::consensus::ItemTable::new(&items);
        let group = |heading: &str, ids: &[usize]| disputed_group(heading, ids);
        // Unit = one disputed group (Alpha over ids 0 and 1).
        let groups = [group("Alpha", &[0, 1])];
        let unit = groups.iter().collect::<Vec<_>>();
        let target = synthesize_verification_target(&unit, &table, &outcomes);
        assert!(
            target
                .claim
                .contains("Alpha: alpha is true; alpha is false"),
            "claim synthesizes heading + member claims: {}",
            target.claim
        );
        assert!(
            target.sources.contains("url1") && target.sources.contains("url2"),
            "sources aggregate member sources: {}",
            target.sources
        );
        assert!(
            target
                .contradictions
                .contains("src says alpha may be false"),
            "member self-reported contradictions aggregate: {}",
            target.contradictions
        );
    }

    #[test]
    fn render_verification_section_formats_verdicts() {
        let results = vec![
            VerificationResult {
                claim: "Alpha: x".into(),
                verdict: "supported".into(),
                evidence: "primary source confirms".into(),
                tool_calls: 1,
                searches: 2,
                queries: vec!["q".into()],
            },
            VerificationResult {
                claim: "Beta: y".into(),
                verdict: "unresolved".into(),
                evidence: "no evidence found".into(),
                tool_calls: 0,
                searches: 1,
                queries: vec![],
            },
        ];
        let text = render_verification_section(&results);
        assert_eq!(
            text,
            "## Verification\n- Alpha: x → **supported** — primary source confirms\n- Beta: y → **unresolved** — no evidence found",
            "verdicts render as annotation lines"
        );
        assert!(
            render_verification_section(&[]).is_empty(),
            "empty results render an empty section"
        );
    }

    #[test]
    #[expect(clippy::unchecked_time_subtraction)] // fixture deliberately constructs a past deadline from monotonic arithmetic
    fn verification_window_open_guards_half_round_window() {
        // A fresh round (deadline = now + window) is open: verification runs.
        let open = verification_window_open(std::time::Instant::now() + round_timeout());
        assert!(
            open,
            "verification runs while ≤ half the window has elapsed"
        );
        // An elapsed round (deadline already in the past) is closed: skip.
        let closed = verification_window_open(std::time::Instant::now() - round_timeout() * 2);
        assert!(
            !closed,
            "verification skipped when more than half the window elapsed"
        );
    }

    // ── Emission-time durability: drain-cut + resume-completion ───────────

    /// (e) With drain active, a sync analyze dispatch surfaces the
    /// [`CallSuspended`] carrier and leaves the caller-owned job `launched` —
    /// never terminalized.
    #[tokio::test]
    #[serial_test::serial(drain)] // serializes the process-global drain flag
    async fn sync_analyze_draincut_returns_call_suspended_error() {
        crate::util::test::init_management_test_stores().await;
        let ws = test_ws("/tmp/test_ws_sync_analyze_draincut");
        let pin = "sync_analyze_draincut_pin";
        let conn = &crate::session::store().conn;

        crate::shutdown::drain_begin();
        let tool = AnalyzeTool::new(DispatchMode::Sync, crate::Role::Engineer);
        let res = crate::agent::CURRENT_TOOL_AGENT_ID
            .scope(Some(pin.to_string()), async {
                tool.execute(&ws, json!({"message": "analyze task"})).await
            })
            .await;
        crate::shutdown::drain_clear();

        let err = res.expect_err("drain must cut the sync analyze dispatch");
        assert!(
            err.downcast_ref::<crate::tools::CallSuspended>().is_some(),
            "CallSuspended carrier expected: {err:#}"
        );

        // The durable job stays launched, caller-owned by the pin.
        let rows = conn
            .query(
                "SELECT status, caller_agent_id FROM jobs WHERE caller_agent_id = ?1 AND kind = 'analyze'",
                crate::db::params![pin],
            )
            .await
            .unwrap();
        assert_eq!(rows.len(), 1, "one launched analyze job");
        assert_eq!(rows[0].get::<String>(0).unwrap(), "launched");
        assert_eq!(
            rows[0].get::<String>(1).unwrap(),
            pin,
            "job is caller-owned by the session pin"
        );
    }

    /// (f) The resume-completion path resumes a durable analyze job whose
    /// roster is all-Done: the Done slot is reconstructed (zero LLM calls),
    /// the consolidated result settles contiguously after the frame, and the
    /// job is terminalized.
    // Joins the `provider` serial group: the seam swaps the process-global
    // provider, so the test must not interleave with the other provider-group
    // tests (same exclusion the pipeline/retry tests rely on).
    #[tokio::test]
    #[serial_test::serial(provider, drain)] // serializes the process-global fake provider (providers::PROVIDER) + shutdown drain flag
    async fn resume_hook_completion_resumes_durable_analyze() {
        let fake = std::sync::Arc::new(FakeProvider::new());
        let _seam = crate::util::test::install_retry_seam_dyn(fake.clone());
        crate::util::test::init_management_test_stores().await;
        let ws = test_ws("/tmp/test_ws_analyze_resume_hook");
        let pin = "sync_analyze_resume_pin";
        let conn = &crate::session::store().conn;
        let job_id = "sync_analyze_resume_job";

        // Caller session [user, assistant frame with an analyze tool call].
        crate::util::test::seed_session_row(conn, pin, "user", "analyze task").await;
        let frame = crate::providers::reasoning::assistant_replay_payload(
            Some(""),
            &[crate::ToolCall {
                id: "call_analyze_f".to_string(),
                name: "analyze".to_string(),
                arguments: json!({"message": "analyze task"}),
            }],
            None,
        )
        .to_string();
        crate::util::test::seed_session_row(conn, pin, "assistant", &frame).await;
        // Owned analyze job with an all-Done roster (outcome RESULT_ONE).
        seed_analyze_job(&ws, job_id, pin, Some("RESULT_ONE")).await;

        // Load the caller session and run the resume+settle path.
        let mut session = crate::session::Session::default();
        session.init(pin).await.unwrap();
        let pending = session
            .pending_tool_frame()
            .expect("dangling analyze call")
            .calls;
        assert_eq!(pending.len(), 1);
        assert_eq!(pending[0].id, "call_analyze_f");

        let outcome = crate::tools::SyncDurableCore::Analyze
            .resume_sync_core(&ws, job_id, true)
            .await
            .unwrap();
        let crate::jobs::SyncResumeOutcome::Terminal(_, _, result) = outcome else {
            panic!("expected a terminal resume outcome");
        };
        let text = result.expect("resumed analyze result");
        assert_eq!(text, "RESULT_ONE");
        crate::jobs::terminalize_job(conn, job_id).await.unwrap();

        session
            .settle_tool_results(pin, &[("call_analyze_f".to_string(), text.clone())], &[])
            .await
            .unwrap();

        // The Done slot was reconstructed — the seam saw zero LLM calls.
        assert!(
            fake.request_fingerprints.lock().unwrap().is_empty(),
            "resume of an all-Done roster must not call the model"
        );

        // Job terminalized.
        let jobs = conn
            .query(
                "SELECT id FROM jobs WHERE id = ?1",
                crate::db::params![job_id],
            )
            .await
            .unwrap();
        assert!(jobs.is_empty(), "resumed job must be terminalized");

        // Tool result row contiguous after the frame; pending_jobs empty.
        let rows = conn
            .query(
                "SELECT id, role, content FROM sessions WHERE agent_id = ?1 ORDER BY id",
                crate::db::params![pin],
            )
            .await
            .unwrap();
        assert_eq!(rows.len(), 3);
        assert_eq!(rows[1].get::<String>(1).unwrap(), "assistant");
        assert_eq!(rows[2].get::<String>(1).unwrap(), "tool");
        let tool_content = rows[2].get::<String>(2).unwrap();
        let payload: crate::ToolResultPayload = serde_json::from_str(&tool_content).unwrap();
        assert_eq!(payload.tool_call_id, "call_analyze_f");
        assert_eq!(payload.content, "RESULT_ONE");
        let pending_jobs = conn
            .query(
                "SELECT id FROM pending_jobs WHERE id = ?1",
                crate::db::params![job_id],
            )
            .await
            .unwrap();
        assert!(pending_jobs.is_empty(), "no envelope pending after resume");
    }
}