cflx 0.6.327

Conflux – a spec-driven parallel coding orchestrator that runs AI agents on git worktrees
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
//! Acceptance test output parsing module.
//!
//! This module provides functions to parse acceptance test output
//! and determine pass/fail status with findings.
//!
//! Verdict contract (post `adopt-json-acceptance-verdict`):
//!
//! - **Primary**: a strict JSON verdict object of the form
//!   `{"acceptance":"pass|fail|continue|gated","findings":[...]}` emitted as
//!   the final machine-readable verdict payload. JSON verdicts may appear
//!   directly as a line on stdout, or wrapped inside a supported JSONL event
//!   text payload. In either case the runtime unwraps the payload and evaluates
//!   the JSON verdict.
//! - **Fallback**: legacy plain-text standalone verdict markers of the form
//!   `ACCEPTANCE: PASS|FAIL|CONTINUE|GATED` remain supported for backward
//!   compatibility, but JSON takes priority whenever both are present.

/// Supported explicit blocker categories for a validated stalled hold.
///
/// The category is always supplied explicitly by the acceptance reviewer. The
/// runtime never derives it by searching narrative prose for words such as
/// `credential`, `token`, or `auth`: an unsupported or absent category is a
/// protocol error, not an invitation to guess.
pub const SUPPORTED_BLOCKER_CATEGORIES: &[&str] = &[
    "credential",
    "external_approval",
    "policy",
    "external_service",
    "pending_verification",
    "infrastructure",
    "schema_incompatibility",
    "human_decision",
];

/// Structured, validated external blocker payload accompanying a `gated`
/// compatibility verdict.
///
/// Every field here is authored by the acceptance reviewer. A payload that
/// satisfies [`validate_acceptance_blocker`] is the *only* input that may enter
/// the in-memory `stalled` hold; anything weaker stays on the bounded
/// protocol-error path.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AcceptanceBlocker {
    /// Explicit category drawn from [`SUPPORTED_BLOCKER_CATEGORIES`].
    pub category: String,
    /// Concrete non-empty evidence for the external prerequisite.
    pub evidence: Vec<String>,
    /// Verifiable condition whose satisfaction clears the wait.
    ///
    /// Distinct from [`Self::next_action`]: the condition is what an observer
    /// can check, the action is what someone does about it. The orchestrator
    /// requires both before it will classify the report as external `blocked`.
    pub unblock_condition: String,
    /// Operator-facing action that can unblock the hold.
    pub next_action: String,
    /// Whether acceptance can resume once the prerequisite is satisfied.
    pub resumable: bool,
    /// Optional owning team/role for the prerequisite.
    pub prerequisite_owner: Option<String>,
    /// Optional stable identifiers for the evidence entries.
    pub evidence_ids: Vec<String>,
}

impl AcceptanceBlocker {
    /// Reported blocker facts for lifecycle events and status displays.
    ///
    /// Every field is copied verbatim; nothing is re-derived from prose. This is
    /// the single projection every frontend shares, so equivalent blockers
    /// reach the orchestrator's classifier as equivalent facts — and therefore
    /// receive the same lifecycle classification in both modes.
    pub fn to_stalled_blocker(&self) -> crate::events::StalledBlocker {
        crate::events::StalledBlocker {
            category: self.category.clone(),
            phase: "acceptance".to_string(),
            gate: "acceptance".to_string(),
            error_summary: format!(
                "validated external acceptance blocker ({}): {}",
                self.category,
                self.evidence.join(" | ")
            ),
            evidence: self.evidence.clone(),
            unblock_condition: Some(self.unblock_condition.clone()),
            prerequisite_owner: self.prerequisite_owner.clone(),
            next_action: self.next_action.clone(),
            resumable: self.resumable,
            worktree_preserved: true,
        }
    }
}

/// Why a `gated`/legacy-`blocked` verdict failed to qualify as a validated
/// stalled blocker. Every variant routes to the bounded protocol-error path.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BlockerRejection {
    /// No `blocker` payload accompanied the compatibility verdict at all
    /// (bare `{"acceptance":"gated"}` or plain-text `ACCEPTANCE: GATED`).
    Missing,
    /// The `blocker` field was present but not a JSON object.
    NotAnObject,
    /// `category` was absent or not a string.
    MissingCategory,
    /// `category` was present but is not in [`SUPPORTED_BLOCKER_CATEGORIES`].
    UnsupportedCategory(String),
    /// `evidence` was absent, not an array, or contained no non-blank entry.
    EmptyEvidence,
    /// `unblock_condition` was absent or blank.
    MissingUnblockCondition,
    /// `next_action` was absent or blank.
    MissingNextAction,
    /// `resumable` was absent or not a boolean.
    MissingResumable,
}

impl BlockerRejection {
    /// Short operator-facing reason, used in protocol-retry diagnostics.
    pub fn reason(&self) -> String {
        match self {
            BlockerRejection::Missing => {
                "no structured blocker payload accompanied the gated verdict".to_string()
            }
            BlockerRejection::NotAnObject => "blocker payload is not a JSON object".to_string(),
            BlockerRejection::MissingCategory => {
                "blocker payload has no explicit category".to_string()
            }
            BlockerRejection::UnsupportedCategory(category) => format!(
                "blocker category '{category}' is not one of: {}",
                SUPPORTED_BLOCKER_CATEGORIES.join(", ")
            ),
            BlockerRejection::EmptyEvidence => {
                "blocker payload has no concrete evidence entries".to_string()
            }
            BlockerRejection::MissingUnblockCondition => {
                "blocker payload has no verifiable unblock_condition".to_string()
            }
            BlockerRejection::MissingNextAction => "blocker payload has no next_action".to_string(),
            BlockerRejection::MissingResumable => {
                "blocker payload has no boolean resumable field".to_string()
            }
        }
    }
}

/// Validate a raw `blocker` payload from a `gated` verdict object.
///
/// Validation is purely structural and explicit: the category must be one the
/// runtime supports, evidence must be concrete, and the reviewer must state a
/// verifiable unblock condition, a next action, and resumability. Nothing is
/// inferred from prose, and a payload missing any required field routes to
/// bounded protocol correction rather than to an inferred external wait.
pub fn validate_acceptance_blocker(
    payload: Option<&serde_json::Value>,
) -> std::result::Result<AcceptanceBlocker, BlockerRejection> {
    let Some(payload) = payload else {
        return Err(BlockerRejection::Missing);
    };
    if payload.is_null() {
        return Err(BlockerRejection::Missing);
    }
    let object = payload.as_object().ok_or(BlockerRejection::NotAnObject)?;

    let category = object
        .get("category")
        .and_then(|value| value.as_str())
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .ok_or(BlockerRejection::MissingCategory)?;
    if !SUPPORTED_BLOCKER_CATEGORIES.contains(&category) {
        return Err(BlockerRejection::UnsupportedCategory(category.to_string()));
    }

    let evidence = string_list(object.get("evidence"));
    if evidence.is_empty() {
        return Err(BlockerRejection::EmptyEvidence);
    }

    let unblock_condition = object
        .get("unblock_condition")
        .and_then(|value| value.as_str())
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .ok_or(BlockerRejection::MissingUnblockCondition)?;

    let next_action = object
        .get("next_action")
        .and_then(|value| value.as_str())
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .ok_or(BlockerRejection::MissingNextAction)?;

    let resumable = object
        .get("resumable")
        .and_then(serde_json::Value::as_bool)
        .ok_or(BlockerRejection::MissingResumable)?;

    Ok(AcceptanceBlocker {
        category: category.to_string(),
        evidence,
        unblock_condition: unblock_condition.to_string(),
        next_action: next_action.to_string(),
        resumable,
        prerequisite_owner: object
            .get("prerequisite_owner")
            .and_then(|value| value.as_str())
            .map(str::trim)
            .filter(|value| !value.is_empty())
            .map(str::to_string),
        evidence_ids: string_list(object.get("evidence_ids")),
    })
}

/// Severity of a structured repository finding.
///
/// Both severities block PASS. The distinction exists only so operators can
/// triage; it never changes routing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FindingSeverity {
    /// Reviewer-declared major defect.
    Major,
    /// Reviewer-declared minor defect. Still blocks PASS.
    Minor,
}

impl FindingSeverity {
    /// Canonical lowercase token used in the verdict schema.
    pub fn as_str(self) -> &'static str {
        match self {
            FindingSeverity::Major => "major",
            FindingSeverity::Minor => "minor",
        }
    }

    fn parse(value: &str) -> Option<Self> {
        match value.trim().to_ascii_lowercase().as_str() {
            "major" => Some(FindingSeverity::Major),
            "minor" => Some(FindingSeverity::Minor),
            _ => None,
        }
    }
}

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

/// One declared repository expectation: a file plus what must become true there.
///
/// Used for both `required_changes` (implementation) and `verification`
/// (proof). `file` is always a normalized repository-relative path.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FindingFileExpectation {
    /// Normalized repository-relative path.
    pub file: String,
    /// Expected behavior or proof at that path.
    pub description: String,
}

/// A structured, actionable repository finding.
///
/// `id` is the authoritative retry identity. It is stable for the same defect
/// across attempts and is never derived from the mutable `summary`, evidence
/// prose, or line numbers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RepositoryFinding {
    /// Stable reviewer-authored identity for this defect.
    pub id: String,
    /// `major` or `minor`; both block PASS.
    pub severity: FindingSeverity,
    /// One-line human-facing summary.
    pub summary: String,
    /// Concrete observed evidence.
    pub evidence: Vec<String>,
    /// Implementation files that must change, with expected behavior.
    pub required_changes: Vec<FindingFileExpectation>,
    /// Verification files that must change, with expected proof.
    pub verification: Vec<FindingFileExpectation>,
}

impl RepositoryFinding {
    /// Declared implementation paths.
    pub fn required_files(&self) -> Vec<String> {
        self.required_changes
            .iter()
            .map(|change| change.file.clone())
            .collect()
    }

    /// Declared verification paths.
    pub fn verification_files(&self) -> Vec<String> {
        self.verification
            .iter()
            .map(|change| change.file.clone())
            .collect()
    }

    /// Machine-readable payload handed to Apply, verbatim and complete.
    pub fn to_json(&self) -> serde_json::Value {
        let expectations = |items: &[FindingFileExpectation]| {
            items
                .iter()
                .map(|item| serde_json::json!({"file": item.file, "description": item.description}))
                .collect::<Vec<_>>()
        };
        serde_json::json!({
            "id": self.id,
            "severity": self.severity.as_str(),
            "summary": self.summary,
            "evidence": self.evidence,
            "required_changes": expectations(&self.required_changes),
            "verification": expectations(&self.verification),
        })
    }

    /// Single-line rendering that keeps every actionable field.
    ///
    /// Runtime-owned follow-up records are line-oriented, so the complete
    /// payload has to survive as one line without losing evidence, required
    /// changes, or verification expectations.
    pub fn to_single_line(&self) -> String {
        let render = |items: &[FindingFileExpectation]| {
            items
                .iter()
                .map(|item| format!("{} — {}", item.file, item.description))
                .collect::<Vec<_>>()
                .join("; ")
        };
        format!(
            "[{}] ({}) {} | evidence: {} | required_changes: {} | verification: {}",
            self.id,
            self.severity,
            self.summary,
            self.evidence.join("; "),
            render(&self.required_changes),
            render(&self.verification),
        )
    }
}

/// Why an object entry in `findings` failed to qualify as a structured
/// repository finding. Every variant routes to bounded protocol handling: none
/// of them may degrade into a normalized path-only repair instruction.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FindingRejection {
    /// The entry was neither a legacy string nor a JSON object.
    NotStringOrObject,
    /// `id` was absent or blank.
    MissingId,
    /// Two findings in the same verdict claimed the same `id`.
    DuplicateId(String),
    /// `severity` was absent or not `major`/`minor`.
    UnsupportedSeverity(String),
    /// `summary` was absent or blank.
    MissingSummary,
    /// `evidence` was absent, not an array, or had no concrete entry.
    EmptyEvidence,
    /// `required_changes` was absent or contained no valid entry.
    EmptyRequiredChanges,
    /// `verification` was absent or contained no valid entry.
    EmptyVerification,
    /// A declared path was absolute, escaped the workspace, or was blank.
    InvalidPath { field: &'static str, path: String },
    /// A declared entry had a path but no description.
    MissingDescription { field: &'static str, path: String },
}

impl FindingRejection {
    /// Short operator-facing reason, used in protocol-retry diagnostics.
    pub fn reason(&self) -> String {
        match self {
            FindingRejection::NotStringOrObject => {
                "finding entry is neither a legacy string nor a structured object".to_string()
            }
            FindingRejection::MissingId => {
                "structured finding has no stable non-empty id".to_string()
            }
            FindingRejection::DuplicateId(id) => {
                format!("structured finding id '{id}' appears more than once in one verdict")
            }
            FindingRejection::UnsupportedSeverity(severity) => {
                format!("structured finding severity '{severity}' is not 'major' or 'minor'")
            }
            FindingRejection::MissingSummary => {
                "structured finding has no non-empty summary".to_string()
            }
            FindingRejection::EmptyEvidence => {
                "structured finding has no concrete evidence entries".to_string()
            }
            FindingRejection::EmptyRequiredChanges => {
                "structured finding declares no required_changes entry".to_string()
            }
            FindingRejection::EmptyVerification => {
                "structured finding declares no verification entry".to_string()
            }
            FindingRejection::InvalidPath { field, path } => format!(
                "structured finding {field} path '{path}' is not a repository-relative path inside \
                 the workspace"
            ),
            FindingRejection::MissingDescription { field, path } => format!(
                "structured finding {field} entry for '{path}' has no description of the expected \
                 behavior or proof"
            ),
        }
    }
}

/// Normalize a declared path to a repository-relative form.
///
/// Returns `None` for absolute paths, Windows drive prefixes, blank input, and
/// anything containing a `..` component: a finding may never point outside the
/// workspace.
pub fn normalize_repository_path(raw: &str) -> Option<String> {
    let candidate = raw.trim().replace('\\', "/");
    if candidate.is_empty() || candidate.starts_with('/') || candidate.starts_with('~') {
        return None;
    }
    // Reject `C:/...` style absolute paths without rejecting `src/a.rs:10`.
    if candidate.len() >= 2 && candidate.as_bytes()[1] == b':' {
        return None;
    }
    let mut components = Vec::new();
    for component in candidate.split('/') {
        match component {
            "" | "." => continue,
            ".." => return None,
            other => components.push(other),
        }
    }
    (!components.is_empty()).then(|| components.join("/"))
}

fn parse_expectations(
    value: Option<&serde_json::Value>,
    field: &'static str,
) -> std::result::Result<Vec<FindingFileExpectation>, FindingRejection> {
    let entries = value
        .and_then(|value| value.as_array())
        .ok_or(match field {
            "required_changes" => FindingRejection::EmptyRequiredChanges,
            _ => FindingRejection::EmptyVerification,
        })?;
    let mut expectations = Vec::new();
    for entry in entries {
        let object = entry.as_object().ok_or(FindingRejection::InvalidPath {
            field,
            path: entry.to_string(),
        })?;
        let raw_path = object
            .get("file")
            .and_then(|value| value.as_str())
            .unwrap_or_default();
        let file =
            normalize_repository_path(raw_path).ok_or_else(|| FindingRejection::InvalidPath {
                field,
                path: raw_path.to_string(),
            })?;
        let description = object
            .get("description")
            .and_then(|value| value.as_str())
            .map(str::trim)
            .filter(|value| !value.is_empty())
            .ok_or_else(|| FindingRejection::MissingDescription {
                field,
                path: file.clone(),
            })?;
        expectations.push(FindingFileExpectation {
            file,
            description: description.to_string(),
        });
    }
    if expectations.is_empty() {
        return Err(match field {
            "required_changes" => FindingRejection::EmptyRequiredChanges,
            _ => FindingRejection::EmptyVerification,
        });
    }
    Ok(expectations)
}

/// Validate one object entry of a FAIL `findings` array.
///
/// Validation is purely structural. Nothing is inferred from prose, and a
/// failure never degrades into a lossy path-only instruction.
pub fn validate_repository_finding(
    value: &serde_json::Value,
) -> std::result::Result<RepositoryFinding, FindingRejection> {
    let object = value
        .as_object()
        .ok_or(FindingRejection::NotStringOrObject)?;

    let id = object
        .get("id")
        .and_then(|value| value.as_str())
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .ok_or(FindingRejection::MissingId)?;

    let raw_severity = object
        .get("severity")
        .and_then(|value| value.as_str())
        .map(str::trim)
        .unwrap_or_default();
    let severity = FindingSeverity::parse(raw_severity)
        .ok_or_else(|| FindingRejection::UnsupportedSeverity(raw_severity.to_string()))?;

    let summary = object
        .get("summary")
        .and_then(|value| value.as_str())
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .ok_or(FindingRejection::MissingSummary)?;

    let evidence = string_list(object.get("evidence"));
    if evidence.is_empty() {
        return Err(FindingRejection::EmptyEvidence);
    }

    Ok(RepositoryFinding {
        id: id.to_string(),
        severity,
        summary: summary.to_string(),
        evidence,
        required_changes: parse_expectations(object.get("required_changes"), "required_changes")?,
        verification: parse_expectations(object.get("verification"), "verification")?,
    })
}

/// One acceptance finding as it travels through history, follow-up state,
/// prompts, and retry accounting.
///
/// This is the single shared representation. A structured finding keeps its
/// complete payload; a legacy string finding keeps its complete original text.
/// Neither is ever replaced by its normalized comparison identity.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AcceptanceFinding {
    text: String,
    structured: Option<RepositoryFinding>,
}

impl AcceptanceFinding {
    /// Wrap a legacy string finding, preserving its complete original text.
    pub fn legacy(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            structured: None,
        }
    }

    /// Wrap a validated structured finding.
    pub fn structured(finding: RepositoryFinding) -> Self {
        Self {
            text: finding.to_single_line(),
            structured: Some(finding),
        }
    }

    /// Complete actionable text. Never a normalized identity.
    pub fn text(&self) -> &str {
        &self.text
    }

    /// Structured payload, when the reviewer supplied one.
    pub fn structured_payload(&self) -> Option<&RepositoryFinding> {
        self.structured.as_ref()
    }

    /// Stable reviewer-authored ID, when the finding is structured.
    pub fn id(&self) -> Option<&str> {
        self.structured.as_ref().map(|finding| finding.id.as_str())
    }

    /// Whether this finding declares required implementation/verification paths
    /// that strict diff coverage can enforce.
    pub fn declares_paths(&self) -> bool {
        self.structured.is_some()
    }

    /// Machine-readable payload for the Apply repair prompt.
    pub fn to_json(&self) -> serde_json::Value {
        match &self.structured {
            Some(finding) => finding.to_json(),
            None => serde_json::json!({"finding": self.text}),
        }
    }
}

impl From<String> for AcceptanceFinding {
    fn from(value: String) -> Self {
        Self::legacy(value)
    }
}

impl From<&str> for AcceptanceFinding {
    fn from(value: &str) -> Self {
        Self::legacy(value)
    }
}

impl std::ops::Deref for AcceptanceFinding {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.text
    }
}

impl AsRef<str> for AcceptanceFinding {
    fn as_ref(&self) -> &str {
        &self.text
    }
}

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

// Text-equality conveniences. Comparing against a plain string checks the
// complete finding text; it never compares a normalized identity.
impl PartialEq<str> for AcceptanceFinding {
    fn eq(&self, other: &str) -> bool {
        self.text == other
    }
}

impl PartialEq<&str> for AcceptanceFinding {
    fn eq(&self, other: &&str) -> bool {
        self.text == *other
    }
}

impl PartialEq<String> for AcceptanceFinding {
    fn eq(&self, other: &String) -> bool {
        &self.text == other
    }
}

/// Complete texts of a finding list, in order.
pub fn finding_texts(findings: &[AcceptanceFinding]) -> Vec<String> {
    findings
        .iter()
        .map(|finding| finding.text().to_string())
        .collect()
}

/// Wrap legacy string findings without changing their text.
pub fn legacy_findings<I, S>(findings: I) -> Vec<AcceptanceFinding>
where
    I: IntoIterator<Item = S>,
    S: Into<String>,
{
    findings
        .into_iter()
        .map(|finding| AcceptanceFinding::legacy(finding))
        .collect()
}

/// Parse one `findings` array into shared findings.
///
/// Strings stay legacy. Objects must validate; a malformed object is a protocol
/// error rather than a lossy degradation. Duplicate structured IDs in one
/// verdict are rejected because retry accounting keys on the ID.
pub fn parse_finding_entries(
    entries: &[serde_json::Value],
) -> std::result::Result<Vec<AcceptanceFinding>, FindingRejection> {
    let mut findings = Vec::new();
    let mut seen_ids = std::collections::HashSet::new();
    for entry in entries {
        if let Some(text) = entry.as_str() {
            let trimmed = text.trim();
            if trimmed.is_empty() {
                continue;
            }
            findings.push(AcceptanceFinding::legacy(trimmed));
            continue;
        }
        let structured = validate_repository_finding(entry)?;
        if !seen_ids.insert(structured.id.clone()) {
            return Err(FindingRejection::DuplicateId(structured.id));
        }
        findings.push(AcceptanceFinding::structured(structured));
    }
    Ok(findings)
}

/// Collect the non-blank string entries of a JSON array field.
fn string_list(value: Option<&serde_json::Value>) -> Vec<String> {
    value
        .and_then(|value| value.as_array())
        .map(|entries| {
            entries
                .iter()
                .filter_map(|entry| entry.as_str())
                .map(str::trim)
                .filter(|entry| !entry.is_empty())
                .map(str::to_string)
                .collect()
        })
        .unwrap_or_default()
}

/// Result of parsing acceptance output
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AcceptanceResult {
    /// Acceptance passed
    Pass,
    /// Acceptance failed with findings
    Fail { findings: Vec<AcceptanceFinding> },
    /// Acceptance requires more investigation - continue later
    Continue,
    /// A FAIL verdict carried a `findings` entry that claimed to be structured
    /// but did not validate.
    ///
    /// Like [`AcceptanceResult::BareBlocker`] this is a bounded protocol error:
    /// the runtime asks for a corrected verdict instead of dispatching ambiguous
    /// path-only repair work.
    MalformedFinding { rejection: FindingRejection },
    /// A `gated` (or legacy `blocked`) compatibility verdict arrived without a
    /// payload that qualifies as a validated external blocker.
    ///
    /// This is an acceptance protocol error, not a stalled hold: it creates no
    /// lifecycle transition, no blocker category, and no durable record. The
    /// runtime retries acceptance within a bounded budget and then reports a
    /// terminal protocol error.
    BareBlocker { rejection: BlockerRejection },
    /// A `gated` compatibility verdict carrying a validated structured external
    /// blocker. Only this variant may enter the durable `stalled` hold.
    Stalled { blocker: AcceptanceBlocker },
    /// No canonical verdict was found in the output. The acceptance command
    /// completed without emitting the machine-readable verdict it owes
    /// (for example a status-only or waiting narrative), which is a protocol
    /// failure distinct from an intentional [`AcceptanceResult::Continue`].
    MissingVerdict,
}

/// Canonical plain-text verdict variants. These remain supported as a
/// backward-compatible fallback when no JSON verdict object is present.
///
/// The runtime requires an unwrapped standalone line that exactly equals one
/// of these markers (after stripping tolerated markdown decorations).
/// Trailing-text concatenation such as `ACCEPTANCE: PASSAll ...` or
/// `ACCEPTANCE: PASS## ...` does NOT satisfy the fallback contract and falls
/// through to the missing-verdict protocol failure.
pub(crate) const CANONICAL_VERDICTS: &[(&str, &str)] = &[
    ("ACCEPTANCE: PASS", "pass"),
    ("ACCEPTANCE: FAIL", "fail"),
    ("ACCEPTANCE: CONTINUE", "continue"),
    ("ACCEPTANCE: GATED", "gated"),
    // Legacy backward-compatibility marker during migration.
    ("ACCEPTANCE: BLOCKED", "gated"),
];

/// A strict JSON acceptance verdict object parsed from one line/text payload.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct JsonVerdict {
    /// Canonical verdict kind: `pass`/`fail`/`continue`/`gated`.
    pub kind: &'static str,
    /// Raw `findings` array entries, when present. Validation happens in
    /// [`acceptance_result_from_json`] so a malformed structured entry can be
    /// reported as a protocol error instead of being silently dropped.
    pub findings: Vec<serde_json::Value>,
    /// Raw `blocker` payload, when present. Validated separately so the parser
    /// can report *why* a compatibility verdict failed to qualify as a stall.
    pub blocker: Option<serde_json::Value>,
}

/// Attempt to parse a single line/text payload as a strict JSON acceptance
/// verdict object. Returns `Some(verdict)` when the trimmed content is a JSON
/// object with an `acceptance` field equal to one of
/// `pass`/`fail`/`continue`/`gated` (case-insensitive).
/// Legacy `blocked` input is accepted as backward-compatible alias.
///
/// Accepted shapes:
///
/// ```json
/// {"acceptance":"pass"}
/// {"acceptance":"fail","findings":["src/foo.rs:10 issue"]}
/// {"acceptance":"gated","blocker":{"category":"credential",
///   "evidence":["STAGING_API_KEY is unset in the verification environment"],
///   "unblock_condition":"STAGING_API_KEY is present in the verification environment",
///   "next_action":"provision STAGING_API_KEY then retry acceptance",
///   "resumable":true}}
/// ```
pub(crate) fn parse_json_verdict(text: &str) -> Option<JsonVerdict> {
    let trimmed = text.trim();
    if !trimmed.starts_with('{') || !trimmed.ends_with('}') {
        return None;
    }
    let value: serde_json::Value = serde_json::from_str(trimmed).ok()?;
    let obj = value.as_object()?;
    let raw_kind = obj.get("acceptance")?.as_str()?;
    let kind = match raw_kind.trim().to_ascii_lowercase().as_str() {
        "pass" => "pass",
        "fail" => "fail",
        "continue" => "continue",
        "gated" => "gated",
        "blocked" => "gated",
        _ => return None,
    };
    let findings = obj
        .get("findings")
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();
    Some(JsonVerdict {
        kind,
        findings,
        blocker: obj.get("blocker").cloned(),
    })
}

/// Detect a canonical verdict kind in a single output line.
///
/// Preference order:
///
/// 1. Strict JSON verdict object on the line itself (primary contract).
/// 2. Strict JSON verdict object inside an unwrapped supported agent JSONL
///    event text payload.
/// 3. Legacy plain-text standalone canonical marker on the line itself, or
///    inside the unwrapped event payload (backward-compatible fallback).
///
/// Returns `None` when no verdict is recognized. This helper is used both for
/// streaming verdict detection (grace-period trigger) and for full-output
/// parsing fallback.
/// Returns the canonical verdict kind for a single output line, or `None` when
/// the line is not a standalone canonical verdict.
///
/// Matching is strict: the line must equal one of [`CANONICAL_VERDICTS`] markers
/// exactly after trimming whitespace and stripping defensively-tolerated markdown
/// decorations (bold/italic/underline + leading heading/blockquote/bullet
/// prefixes). Trailing-text concatenation onto the marker is rejected.
pub(crate) fn canonical_verdict_kind(line: &str) -> Option<&'static str> {
    let normalized = strip_markdown_decorations(line.trim());
    CANONICAL_VERDICTS
        .iter()
        .find(|(marker, _)| normalized == *marker)
        .map(|(_, kind)| *kind)
}

#[cfg(test)]
fn detect_verdict_in_line(line: &str) -> Option<&'static str> {
    VerdictStreamDetector::default().detect(line)
}

#[derive(Default)]
pub(crate) struct VerdictStreamDetector {
    code_fence: Option<(char, usize)>,
}

impl VerdictStreamDetector {
    pub(crate) fn detect(&mut self, line: &str) -> Option<&'static str> {
        if let Some(text) = crate::stream_json_textifier::extract_text_from_stream_json(line.trim())
        {
            for inner in text.lines() {
                if let Some(kind) = self.detect_unwrapped(inner) {
                    return Some(kind);
                }
            }
            return None;
        }
        self.detect_unwrapped(line)
    }

    fn detect_unwrapped(&mut self, line: &str) -> Option<&'static str> {
        let trimmed = line.trim();
        if let Some((marker, length, closing)) = verdict_markdown_fence(trimmed, self.code_fence) {
            if closing {
                self.code_fence = None;
            } else if self.code_fence.is_none() {
                self.code_fence = Some((marker, length));
            }
            return None;
        }
        if self.code_fence.is_some() {
            return None;
        }
        parse_json_verdict(trimmed)
            .map(|verdict| verdict.kind)
            .or_else(|| canonical_verdict_kind(trimmed))
    }
}

fn verdict_markdown_fence(line: &str, open: Option<(char, usize)>) -> Option<(char, usize, bool)> {
    let marker = line.chars().next()?;
    if marker != '`' && marker != '~' {
        return None;
    }
    let length = line
        .chars()
        .take_while(|character| *character == marker)
        .count();
    if length < 3 {
        return None;
    }
    let remainder = &line[length..];
    let closing = open.is_some_and(|(open_marker, open_length)| {
        marker == open_marker && length >= open_length && remainder.trim().is_empty()
    });
    Some((marker, length, closing))
}

/// Parse acceptance output text and determine pass/fail/continue/gated status.
///
/// Contract (JSON-primary, text-fallback):
///
/// - Primary: a strict JSON verdict object
///   `{"acceptance":"pass|fail|continue|gated","findings":[...]}` emitted
///   either directly as a line, or wrapped inside a supported JSONL event text
///   payload unwrapped by the runtime.
///   The first JSON verdict encountered wins, regardless of any earlier text
///   marker.
/// - Fallback: a standalone legacy line equal to one of `ACCEPTANCE: PASS`,
///   `ACCEPTANCE: FAIL`, `ACCEPTANCE: CONTINUE`, `ACCEPTANCE: GATED`,
///   or (legacy-compatibility) `ACCEPTANCE: BLOCKED`.
///   Bold/italic/underline wrappers and leading heading/blockquote/bullet
///   prefixes are tolerated defensively, but trailing-text concatenation onto
///   the marker (for example `ACCEPTANCE: PASSAll ...` or
///   `ACCEPTANCE: PASS## ...`) is NOT a canonical marker. When no JSON verdict
///   is found, the first canonical fallback line wins.
///
/// If neither is observed, the result is [`AcceptanceResult::MissingVerdict`]:
/// the acceptance command completed without emitting a canonical verdict. This
/// is an explicit protocol failure and is intentionally distinct from an
/// agent-emitted canonical `CONTINUE`, so a premature agent exit (for example a
/// status-only "waiting for verification" narrative) cannot masquerade as an
/// intentional continuation request.
pub fn parse_acceptance_output(output: &str) -> AcceptanceResult {
    let mut fallback_kind: Option<&'static str> = None;
    let mut fallback_findings = Vec::new();
    let mut collecting_findings = false;
    let mut detector = VerdictStreamDetector::default();

    for raw_line in output.lines() {
        let unwrapped = crate::stream_json_textifier::extract_text_from_stream_json(raw_line)
            .unwrap_or_else(|| raw_line.to_string());
        for line in unwrapped.lines() {
            let candidate = line.trim();
            let fence = verdict_markdown_fence(candidate, detector.code_fence);
            let detected = detector.detect_unwrapped(candidate);
            if fence.is_some() || detector.code_fence.is_some() {
                continue;
            }

            if let Some(verdict) = parse_json_verdict(candidate) {
                return acceptance_result_from_json(verdict);
            }

            if fallback_kind.is_none() {
                if let Some(kind) = detected {
                    fallback_kind = Some(kind);
                    collecting_findings = false;
                }
                continue;
            }

            if fallback_kind == Some("fail") {
                if candidate == "FINDINGS:" {
                    collecting_findings = true;
                } else if collecting_findings {
                    if let Some(finding) = candidate.strip_prefix("- ") {
                        fallback_findings.push(finding.to_string());
                    } else if !candidate.is_empty() {
                        collecting_findings = false;
                    }
                }
            }
        }
    }

    match fallback_kind {
        Some("pass") => AcceptanceResult::Pass,
        Some("continue") => AcceptanceResult::Continue,
        // The plain-text fallback carries no structured payload, so a legacy
        // `ACCEPTANCE: GATED`/`ACCEPTANCE: BLOCKED` line is always bare.
        Some("gated") => AcceptanceResult::BareBlocker {
            rejection: BlockerRejection::Missing,
        },
        Some("fail") => AcceptanceResult::Fail {
            findings: legacy_findings(fallback_findings),
        },
        // No canonical verdict anywhere in the output: explicit protocol
        // failure, never an implicit CONTINUE.
        _ => AcceptanceResult::MissingVerdict,
    }
}

fn acceptance_result_from_json(verdict: JsonVerdict) -> AcceptanceResult {
    match verdict.kind {
        "pass" => AcceptanceResult::Pass,
        // A structured finding that fails validation is a protocol error. It is
        // never converted into a normalized path-only repair instruction.
        "fail" => match parse_finding_entries(&verdict.findings) {
            Ok(findings) => AcceptanceResult::Fail { findings },
            Err(rejection) => AcceptanceResult::MalformedFinding { rejection },
        },
        "continue" => AcceptanceResult::Continue,
        // Compatibility verdict: only a validated structured payload becomes a
        // stall. Everything else is a bounded protocol error.
        "gated" => match validate_acceptance_blocker(verdict.blocker.as_ref()) {
            Ok(blocker) => AcceptanceResult::Stalled { blocker },
            Err(rejection) => AcceptanceResult::BareBlocker { rejection },
        },
        _ => AcceptanceResult::MissingVerdict,
    }
}

/// Strip markdown decorations from a string.
/// Removes bold (**), italic (*), underline (_), heading prefixes (#),
/// blockquote prefixes (>), and bullet prefixes (-) that LLMs commonly
/// add around verdict markers.
///
/// This is a defensive measure — the canonical contract forbids these
/// wrappers, but the parser tolerates them to prevent a missing-verdict
/// protocol failure when an agent drifts.
pub(crate) fn strip_markdown_decorations(text: &str) -> String {
    let mut s = text.to_string();
    // Remove bold pairs first to avoid leaving stray * characters
    s = s.replace("**", "");
    // Remove remaining italic/underline characters
    s = s.replace(['*', '_'], "");
    // Trim whitespace, then strip leading markdown block-level prefixes
    let trimmed = s.trim();
    // Strip leading heading markers (e.g., "## ", "### ")
    let trimmed = trimmed.trim_start_matches('#');
    // Strip leading blockquote markers (e.g., "> ")
    let trimmed = trimmed.trim_start_matches('>');
    // Strip leading bullet markers (e.g., "- ")
    let trimmed = trimmed.trim_start_matches('-');
    trimmed.trim().to_string()
}

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

    #[test]
    fn test_parse_pass() {
        let output = "ACCEPTANCE: PASS\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn acceptance_append_prompt_text_is_not_parsed_as_output_verdict() {
        let prompt = crate::agent::append_optional_prompt(
            "generated acceptance prompt".to_string(),
            Some("optional guidance mentioning ACCEPTANCE: FAIL and {change_id}"),
        );
        assert!(prompt.ends_with("optional guidance mentioning ACCEPTANCE: FAIL and {change_id}"));
        assert_eq!(
            parse_acceptance_output("ACCEPTANCE: PASS\n"),
            AcceptanceResult::Pass
        );
    }

    #[test]
    fn test_parse_pass_with_extra_output() {
        let output = "Some debug output\nACCEPTANCE: PASS\nMore output\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_fail_with_findings() {
        let output = "ACCEPTANCE: FAIL\nFINDINGS:\n- Issue 1\n- Issue 2\n";
        match parse_acceptance_output(output) {
            AcceptanceResult::Fail { findings } => {
                assert_eq!(findings.len(), 2);
                assert_eq!(findings[0], "Issue 1");
                assert_eq!(findings[1], "Issue 2");
            }
            _ => panic!("Expected Fail"),
        }
    }

    #[test]
    fn test_parse_fail_with_no_findings() {
        let output = "ACCEPTANCE: FAIL\n";
        match parse_acceptance_output(output) {
            AcceptanceResult::Fail { findings } => {
                assert_eq!(findings.len(), 0);
            }
            _ => panic!("Expected Fail"),
        }
    }

    #[test]
    fn test_parse_fail_with_multiline_findings() {
        let output = r#"ACCEPTANCE: FAIL
FINDINGS:
- Task 1.3 is not completed
- Missing unit tests for new feature
- Code does not handle error case X
"#;
        match parse_acceptance_output(output) {
            AcceptanceResult::Fail { findings } => {
                assert_eq!(findings.len(), 3);
                assert_eq!(findings[0], "Task 1.3 is not completed");
                assert_eq!(findings[1], "Missing unit tests for new feature");
                assert_eq!(findings[2], "Code does not handle error case X");
            }
            _ => panic!("Expected Fail"),
        }
    }

    #[test]
    fn test_parse_no_status() {
        let output = "Some random output\n";
        // When no explicit marker is present, this is a missing-verdict
        // protocol failure — not an implicit CONTINUE.
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict
        );
    }

    #[test]
    fn test_parse_no_marker_is_missing_verdict() {
        // Empty output is a missing-verdict protocol failure
        assert_eq!(
            parse_acceptance_output(""),
            AcceptanceResult::MissingVerdict
        );

        // Output with no acceptance marker is a missing-verdict protocol failure
        let output = "Some debug output\nNo marker here\n";
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict
        );

        // Output with findings but no marker is a missing-verdict protocol failure
        let output = "FINDINGS:\n- Issue 1\n- Issue 2\n";
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict
        );
    }

    #[test]
    fn test_parse_status_only_waiting_output_is_missing_verdict_not_continue() {
        // Regression: an acceptance agent that starts verification, reports it
        // is waiting for a completion notification, and exits without a
        // canonical verdict must be classified as a missing-verdict protocol
        // failure — never as an intentional CONTINUE.
        let output = "Started long-running verification job.\n\
                      Monitoring verification; I will evaluate the evidence and \
                      emit the final verdict once the completion notification arrives.\n";
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict
        );
        assert_ne!(
            parse_acceptance_output(output),
            AcceptanceResult::Continue,
            "status-only exit must remain distinguishable from explicit CONTINUE"
        );
    }

    /// Parser routing matrix: each canonical verdict keeps its own meaning and
    /// only verdict-free output becomes the missing-verdict protocol failure.
    /// The parser never sees command failure — a non-zero exit is classified by
    /// the runtime before parsing — so nothing here can turn into a protocol
    /// retry by accident.
    #[test]
    fn acceptance_parser_routing_matrix_is_stable() {
        let cases: [(&str, AcceptanceResult); 10] = [
            ("{\"acceptance\":\"pass\"}\n", AcceptanceResult::Pass),
            ("ACCEPTANCE: PASS\n", AcceptanceResult::Pass),
            (
                "{\"acceptance\":\"fail\",\"findings\":[\"src/a.rs:1 issue\"]}\n",
                AcceptanceResult::Fail {
                    findings: vec!["src/a.rs:1 issue".to_string().into()],
                },
            ),
            (
                "ACCEPTANCE: FAIL\nFINDINGS:\n- src/a.rs:1 issue\n",
                AcceptanceResult::Fail {
                    findings: vec!["src/a.rs:1 issue".to_string().into()],
                },
            ),
            (
                "{\"acceptance\":\"continue\"}\n",
                AcceptanceResult::Continue,
            ),
            ("ACCEPTANCE: CONTINUE\n", AcceptanceResult::Continue),
            (
                "{\"acceptance\":\"gated\"}\n",
                AcceptanceResult::BareBlocker {
                    rejection: BlockerRejection::Missing,
                },
            ),
            (
                "ACCEPTANCE: BLOCKED\n",
                AcceptanceResult::BareBlocker {
                    rejection: BlockerRejection::Missing,
                },
            ),
            (
                "Monitoring verification; waiting for the job to finish\n",
                AcceptanceResult::MissingVerdict,
            ),
            (
                "ACCEPTANCE: PASSAll criteria verified\n",
                AcceptanceResult::MissingVerdict,
            ),
        ];

        for (output, expected) in cases {
            assert_eq!(
                parse_acceptance_output(output),
                expected,
                "routing drifted for output: {output:?}"
            );
        }
    }

    #[test]
    fn test_explicit_continue_remains_distinct_from_missing_verdict() {
        // Canonical CONTINUE — via JSON and via legacy marker — must keep its
        // intentional-continuation semantics.
        assert_eq!(
            parse_acceptance_output("{\"acceptance\":\"continue\"}\n"),
            AcceptanceResult::Continue
        );
        assert_eq!(
            parse_acceptance_output("ACCEPTANCE: CONTINUE\n"),
            AcceptanceResult::Continue
        );
        // Whereas verdict-free output is a protocol failure.
        assert_eq!(
            parse_acceptance_output("waiting for checks to finish\n"),
            AcceptanceResult::MissingVerdict
        );
    }

    #[test]
    fn test_parse_findings_with_trailing_content() {
        let output = r#"ACCEPTANCE: FAIL
FINDINGS:
- Issue 1
- Issue 2

Additional output here
"#;
        match parse_acceptance_output(output) {
            AcceptanceResult::Fail { findings } => {
                assert_eq!(findings.len(), 2);
                assert_eq!(findings[0], "Issue 1");
                assert_eq!(findings[1], "Issue 2");
            }
            _ => panic!("Expected Fail"),
        }
    }

    #[test]
    fn test_parse_continue() {
        let output = "ACCEPTANCE: CONTINUE\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Continue);
    }

    #[test]
    fn test_parse_continue_with_extra_output() {
        let output = "Some debug output\nACCEPTANCE: CONTINUE\nMore output\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Continue);
    }

    #[test]
    fn test_parse_pass_with_bold_decoration() {
        let output = "**ACCEPTANCE: PASS**\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_pass_with_bold_decoration_and_extra_output() {
        let output = "Some debug output\n**ACCEPTANCE: PASS**\nMore output\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_fail_with_bold_decoration() {
        let output = "**ACCEPTANCE: FAIL**\nFINDINGS:\n- Issue 1\n- Issue 2\n";
        match parse_acceptance_output(output) {
            AcceptanceResult::Fail { findings } => {
                assert_eq!(findings.len(), 2);
                assert_eq!(findings[0], "Issue 1");
                assert_eq!(findings[1], "Issue 2");
            }
            _ => panic!("Expected Fail"),
        }
    }

    #[test]
    fn test_parse_continue_with_bold_decoration() {
        let output = "**ACCEPTANCE: CONTINUE**\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Continue);
    }

    #[test]
    fn test_parse_pass_with_italic_decoration() {
        let output = "*ACCEPTANCE: PASS*\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_pass_with_mixed_decorations() {
        let output = "**_ACCEPTANCE: PASS_**\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_strip_markdown_decorations() {
        assert_eq!(
            strip_markdown_decorations("**ACCEPTANCE: PASS**"),
            "ACCEPTANCE: PASS"
        );
        assert_eq!(
            strip_markdown_decorations("*ACCEPTANCE: PASS*"),
            "ACCEPTANCE: PASS"
        );
        assert_eq!(
            strip_markdown_decorations("_ACCEPTANCE: PASS_"),
            "ACCEPTANCE: PASS"
        );
        assert_eq!(
            strip_markdown_decorations("**_ACCEPTANCE: PASS_**"),
            "ACCEPTANCE: PASS"
        );
        assert_eq!(
            strip_markdown_decorations("ACCEPTANCE: PASS"),
            "ACCEPTANCE: PASS"
        );
    }

    #[test]
    fn test_parse_ignores_acceptance_in_code_blocks() {
        // Code block with FAIL should be ignored, actual PASS should be detected
        let output = r#"
Example output:
```
ACCEPTANCE: FAIL
FINDINGS:
- Issue 1
```

Actual result:
ACCEPTANCE: PASS
"#;
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_ignores_multiple_code_blocks() {
        // Multiple code blocks with different statuses should be ignored
        let output = r#"
First example:
```
ACCEPTANCE: FAIL
```

Second example:
```
ACCEPTANCE: CONTINUE
```

Actual result:
ACCEPTANCE: PASS
"#;
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_code_block_with_language_specifier() {
        // Code blocks with language specifier should also be ignored
        let output = r#"
Example:
```bash
ACCEPTANCE: FAIL
```

Result:
ACCEPTANCE: PASS
"#;
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_ignores_tilde_fenced_verdicts() {
        let output =
            "~~~json\n{\"acceptance\":\"pass\"}\n~~~\nACCEPTANCE: FAIL\nFINDINGS:\n- real failure";

        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::Fail {
                findings: vec!["real failure".to_string().into()]
            }
        );
    }

    #[test]
    fn test_parse_does_not_close_fence_with_info_string() {
        let output = "```text\n```json\n{\"acceptance\":\"pass\"}\nACCEPTANCE: FAIL";

        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict
        );
    }

    #[test]
    fn verdict_stream_detector_ignores_fenced_verdicts() {
        let mut detector = VerdictStreamDetector::default();

        assert_eq!(detector.detect("~~~json"), None);
        assert_eq!(detector.detect(r#"{\"acceptance\":\"pass\"}"#), None);
        assert_eq!(detector.detect("~~~"), None);
        assert_eq!(detector.detect("ACCEPTANCE: FAIL"), Some("fail"));

        let mut detector = VerdictStreamDetector::default();
        assert_eq!(detector.detect("```json"), None);
        assert_eq!(detector.detect(r#"{"acceptance":"pass"}"#), None);
        assert_eq!(detector.detect("```"), None);
    }

    #[test]
    fn test_parse_unclosed_code_block() {
        // If code block is not closed, everything after opening should be skipped
        let output = r#"
Example:
```
ACCEPTANCE: FAIL
ACCEPTANCE: PASS
"#;
        // Both are inside unclosed code block, so no canonical verdict exists.
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict
        );
    }

    // Trailing-text concatenation is NOT canonical. The runtime parser
    // rejects malformed verdicts and falls through to the missing-verdict
    // protocol failure instead of locking in a bogus PASS.

    #[test]
    fn test_parse_pass_with_trailing_text_is_not_canonical() {
        // Regression: real log produced "ACCEPTANCE: PASSAll acceptance criteria verified:"
        // which previously satisfied PASS via starts_with. Strict canonical
        // matching rejects it.
        let output = "ACCEPTANCE: PASSAll acceptance criteria verified:\n";
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict
        );
    }

    #[test]
    fn test_parse_pass_with_trailing_heading_is_not_canonical() {
        // Real log: "ACCEPTANCE: PASS## Acceptance Review Summary"
        let output = "ACCEPTANCE: PASS## Acceptance Review Summary\n";
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict
        );
    }

    #[test]
    fn test_parse_pass_with_trailing_text_in_context_is_not_canonical() {
        let output = r#"Some prior output
ACCEPTANCE: PASSAll acceptance criteria verified:

1. Git working tree is clean
"#;
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict
        );
    }

    #[test]
    fn test_parse_pass_with_trailing_text_falls_through_to_canonical_pass() {
        // If a malformed verdict appears first but a canonical standalone
        // verdict follows on a later line, the canonical line wins.
        let output = "ACCEPTANCE: PASSAll bad form\nACCEPTANCE: PASS\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_fail_with_trailing_text_is_not_canonical() {
        let output = "ACCEPTANCE: FAILSome additional context\nFINDINGS:\n- Issue 1\n";
        // Trailing-text FAIL is malformed; falls through to MissingVerdict.
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict
        );
    }

    #[test]
    fn test_parse_continue_with_trailing_text_is_not_canonical() {
        let output = "ACCEPTANCE: CONTINUENeeds further investigation\n";
        // Malformed verdict — falls through to the missing-verdict protocol
        // failure rather than being read as an intentional CONTINUE.
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict
        );
    }

    #[test]
    fn test_parse_blocked_with_trailing_text_is_not_canonical() {
        let output = "ACCEPTANCE: BLOCKEDWaiting for dependency\n";
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict
        );
    }

    #[test]
    fn test_parse_passed_word_boundary_is_not_canonical() {
        // "ACCEPTANCE: PASSED" must not match canonical PASS.
        let output = "ACCEPTANCE: PASSED\n";
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict
        );
    }

    #[test]
    fn test_canonical_verdict_kind_strict_match() {
        assert_eq!(canonical_verdict_kind("ACCEPTANCE: PASS"), Some("pass"));
        assert_eq!(canonical_verdict_kind("**ACCEPTANCE: PASS**"), Some("pass"));
        assert_eq!(canonical_verdict_kind("## ACCEPTANCE: PASS"), Some("pass"));
        assert_eq!(canonical_verdict_kind("> ACCEPTANCE: FAIL"), Some("fail"));
        assert_eq!(canonical_verdict_kind("ACCEPTANCE: PASSAll bad"), None);
        assert_eq!(canonical_verdict_kind("ACCEPTANCE: PASS## heading"), None);
        assert_eq!(canonical_verdict_kind("ACCEPTANCE: PASSED"), None);
        assert_eq!(canonical_verdict_kind("not a verdict"), None);
    }

    #[test]
    fn test_parse_blocked() {
        let output = "ACCEPTANCE: BLOCKED\n";
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::BareBlocker {
                rejection: BlockerRejection::Missing
            }
        );
    }

    #[test]
    fn test_parse_blocked_with_extra_output() {
        let output = "Some debug output\nACCEPTANCE: BLOCKED\nMore output\n";
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::BareBlocker {
                rejection: BlockerRejection::Missing
            }
        );
    }

    #[test]
    fn test_parse_blocked_with_bold_decoration() {
        let output = "**ACCEPTANCE: BLOCKED**\n";
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::BareBlocker {
                rejection: BlockerRejection::Missing
            }
        );
    }

    // Characterization tests: document the exact contract that
    // src/orchestration/acceptance.rs relies on after the refactor.

    #[test]
    fn test_parse_fail_findings_excludes_preamble() {
        // parse_acceptance_output for FAIL extracts only items from the
        // FINDINGS section — preamble lines before the marker are NOT included.
        let output =
            "preamble line\nACCEPTANCE: FAIL\nFINDINGS:\n- Finding 1\n- Finding 2\npostamble";
        match parse_acceptance_output(output) {
            AcceptanceResult::Fail { findings } => {
                assert_eq!(findings, vec!["Finding 1", "Finding 2"]);
                assert!(!findings.iter().any(|f| f.contains("preamble")));
            }
            _ => panic!("Expected Fail"),
        }
    }

    #[test]
    fn test_parse_fail_findings_from_findings_section_only() {
        // Findings come exclusively from lines prefixed with "- " after
        // "FINDINGS:". This is the single authoritative source used in
        // AcceptanceResult::Fail after the refactor.
        let output =
            "ACCEPTANCE: FAIL\nFINDINGS:\n- src/foo.rs:10 missing test\n- src/bar.rs:5 dead code\n";
        match parse_acceptance_output(output) {
            AcceptanceResult::Fail { findings } => {
                assert_eq!(findings.len(), 2);
                assert_eq!(findings[0], "src/foo.rs:10 missing test");
                assert_eq!(findings[1], "src/bar.rs:5 dead code");
            }
            _ => panic!("Expected Fail"),
        }
    }

    // --- Markdown drift tolerance tests ---
    // These tests verify that the parser defensively handles common LLM
    // formatting drift (headings, blockquotes, bullets) even though the
    // canonical contract forbids these wrappers.

    #[test]
    fn test_parse_pass_with_heading_prefix() {
        let output = "## ACCEPTANCE: PASS\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_pass_with_heading_h3_prefix() {
        let output = "### ACCEPTANCE: PASS\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_fail_with_heading_prefix() {
        let output = "## ACCEPTANCE: FAIL\nFINDINGS:\n- Issue 1\n";
        match parse_acceptance_output(output) {
            AcceptanceResult::Fail { findings } => {
                assert_eq!(findings.len(), 1);
                assert_eq!(findings[0], "Issue 1");
            }
            _ => panic!("Expected Fail"),
        }
    }

    #[test]
    fn test_parse_pass_with_blockquote_prefix() {
        let output = "> ACCEPTANCE: PASS\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_fail_with_blockquote_prefix() {
        let output = "> ACCEPTANCE: FAIL\nFINDINGS:\n- Issue 1\n";
        match parse_acceptance_output(output) {
            AcceptanceResult::Fail { findings } => {
                assert_eq!(findings.len(), 1);
            }
            _ => panic!("Expected Fail"),
        }
    }

    #[test]
    fn test_parse_pass_with_bullet_prefix() {
        let output = "- ACCEPTANCE: PASS\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_continue_with_heading_prefix() {
        let output = "## ACCEPTANCE: CONTINUE\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Continue);
    }

    #[test]
    fn test_parse_blocked_with_heading_prefix() {
        let output = "## ACCEPTANCE: BLOCKED\n";
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::BareBlocker {
                rejection: BlockerRejection::Missing
            }
        );
    }

    #[test]
    fn test_parse_pass_with_heading_and_bold() {
        // Combined drift: heading + bold
        let output = "## **ACCEPTANCE: PASS**\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_strip_markdown_decorations_heading() {
        assert_eq!(
            strip_markdown_decorations("## ACCEPTANCE: PASS"),
            "ACCEPTANCE: PASS"
        );
    }

    #[test]
    fn test_strip_markdown_decorations_blockquote() {
        assert_eq!(
            strip_markdown_decorations("> ACCEPTANCE: PASS"),
            "ACCEPTANCE: PASS"
        );
    }

    #[test]
    fn test_strip_markdown_decorations_bullet() {
        assert_eq!(
            strip_markdown_decorations("- ACCEPTANCE: PASS"),
            "ACCEPTANCE: PASS"
        );
    }

    #[test]
    fn test_strip_markdown_decorations_heading_and_bold() {
        assert_eq!(
            strip_markdown_decorations("## **ACCEPTANCE: PASS**"),
            "ACCEPTANCE: PASS"
        );
    }

    #[test]
    fn test_parse_fail_empty_findings_when_no_section() {
        // When ACCEPTANCE: FAIL appears without a FINDINGS section,
        // parse_acceptance_output returns an empty findings vec.
        let output = "ACCEPTANCE: FAIL\nSome explanation without a FINDINGS: header\n";
        match parse_acceptance_output(output) {
            AcceptanceResult::Fail { findings } => {
                assert!(findings.is_empty());
            }
            _ => panic!("Expected Fail"),
        }
    }

    // --- Marker contract consistency tests ---
    // These tests verify that the marker detection used in orchestration/acceptance.rs
    // streaming code is consistent with the parser contract. The streaming code uses
    // strip_markdown_decorations + starts_with to detect markers for the grace period,
    // and the parser uses the same function. Both must agree.

    #[test]
    fn test_marker_detection_consistency_with_parser() {
        // Streaming verdict detection (src/orchestration/acceptance.rs and
        // src/parallel/executor.rs) and the parser MUST agree on the canonical
        // contract: standalone exact match after stripping tolerated markdown
        // decorations. This test pins both surfaces to canonical_verdict_kind.
        let drift_cases: &[(&str, &str)] = &[
            ("ACCEPTANCE: PASS", "pass"),
            ("**ACCEPTANCE: PASS**", "pass"),
            ("## ACCEPTANCE: PASS", "pass"),
            ("> ACCEPTANCE: PASS", "pass"),
            ("- ACCEPTANCE: PASS", "pass"),
            ("### **ACCEPTANCE: FAIL**", "fail"),
            ("ACCEPTANCE: CONTINUE", "continue"),
            ("ACCEPTANCE: GATED", "gated"),
            // Legacy compatibility during migration.
            ("ACCEPTANCE: BLOCKED", "gated"),
            ("## ACCEPTANCE: BLOCKED", "gated"),
            ("> ACCEPTANCE: FAIL", "fail"),
        ];

        for (case, expected_kind) in drift_cases {
            assert_eq!(
                canonical_verdict_kind(case),
                Some(*expected_kind),
                "canonical_verdict_kind must detect '{}' as kind '{}'",
                case,
                expected_kind
            );

            let full_output = format!("{}\n", case);
            let result = parse_acceptance_output(&full_output);
            let result_kind = match &result {
                AcceptanceResult::Pass => "pass",
                AcceptanceResult::Fail { .. } => "fail",
                AcceptanceResult::Continue => "continue",
                AcceptanceResult::BareBlocker { .. } => "gated",
                AcceptanceResult::Stalled { .. } => "stalled",
                AcceptanceResult::MalformedFinding { .. } => "malformed-finding",
                AcceptanceResult::MissingVerdict => "missing-verdict",
            };
            assert_eq!(
                result_kind, *expected_kind,
                "parse_acceptance_output returned '{}' but expected '{}' for input '{}'",
                result_kind, expected_kind, case
            );
        }
    }

    #[test]
    fn test_marker_detection_rejects_trailing_text_uniformly() {
        // Both surfaces (parser + streaming detection) must reject trailing-text
        // verdicts so they cannot accidentally finalize an acceptance run early.
        let malformed: &[&str] = &[
            "ACCEPTANCE: PASSAll checks completed",
            "ACCEPTANCE: PASS## Acceptance Review Summary",
            "ACCEPTANCE: PASSED",
            "ACCEPTANCE: FAILSome explanation",
            "ACCEPTANCE: CONTINUEMore work",
            "ACCEPTANCE: BLOCKEDWaiting",
        ];

        for case in malformed {
            assert!(
                canonical_verdict_kind(case).is_none(),
                "canonical_verdict_kind must NOT detect malformed verdict '{}'",
                case
            );
        }
    }

    #[test]
    fn test_code_fence_markers_rejected_by_both_parser_and_detection() {
        // Markers inside code fences must NOT be detected by either the parser
        // or the streaming marker detection (which operates line-by-line and
        // does not track code fence state, but the parser does).
        let output = "```\nACCEPTANCE: PASS\n```\n";
        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::MissingVerdict,
            "Parser must not match markers inside code fences"
        );
    }

    // --- JSON-primary verdict contract tests ---

    fn verdict_kind(line: &str) -> Option<&'static str> {
        parse_json_verdict(line).map(|verdict| verdict.kind)
    }

    #[test]
    fn test_parse_json_verdict_pass() {
        let verdict = parse_json_verdict(r#"{"acceptance":"pass"}"#).expect("strict JSON verdict");
        assert_eq!(verdict.kind, "pass");
        assert!(verdict.findings.is_empty());
        assert_eq!(verdict.blocker, None);
    }

    #[test]
    fn test_parse_json_verdict_fail_with_findings() {
        let line = r#"{"acceptance":"fail","findings":["src/a.rs:1 bad","src/b.rs:2 worse"]}"#;
        let verdict = parse_json_verdict(line).expect("strict JSON verdict");
        assert_eq!(verdict.kind, "fail");
        assert_eq!(verdict.findings, vec!["src/a.rs:1 bad", "src/b.rs:2 worse"]);
    }

    #[test]
    fn test_parse_json_verdict_continue_gated_and_legacy_blocked() {
        assert_eq!(
            verdict_kind(r#"{"acceptance":"continue"}"#),
            Some("continue")
        );
        assert_eq!(verdict_kind(r#"{"acceptance":"gated"}"#), Some("gated"));
        assert_eq!(verdict_kind(r#"{"acceptance":"blocked"}"#), Some("gated"));
    }

    #[test]
    fn test_parse_json_verdict_case_insensitive_value() {
        assert_eq!(verdict_kind(r#"{"acceptance":"PASS"}"#), Some("pass"));
        assert_eq!(verdict_kind(r#"{"acceptance":"Fail"}"#), Some("fail"));
    }

    #[test]
    fn test_parse_json_verdict_rejects_non_object_and_unknown_kind() {
        assert_eq!(verdict_kind("pass"), None);
        assert_eq!(verdict_kind(r#"["pass"]"#), None);
        assert_eq!(verdict_kind(r#"{"acceptance":"maybe"}"#), None);
        assert_eq!(verdict_kind(r#"{"other":"pass"}"#), None);
        assert_eq!(verdict_kind("not json"), None);
        assert_eq!(verdict_kind(""), None);
    }

    #[test]
    fn test_parse_acceptance_output_json_pass_single_line() {
        // Primary contract: the final machine-readable verdict is a strict JSON
        // object emitted as its own stdout line.
        let output = r#"{"acceptance":"pass"}
"#;
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_acceptance_output_json_fail_findings_preferred_over_text_section() {
        // JSON findings array is the source of truth when JSON verdict wins.
        let output = r#"preamble
{"acceptance":"fail","findings":["x","y"]}
"#;
        match parse_acceptance_output(output) {
            AcceptanceResult::Fail { findings } => {
                assert_eq!(findings, vec!["x", "y"]);
            }
            other => panic!("expected Fail, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_acceptance_output_json_beats_text_fallback_regardless_of_order() {
        // JSON-primary: when both a text marker and a JSON verdict appear, the
        // JSON verdict wins even if the text marker came first.
        let output = "ACCEPTANCE: CONTINUE\n{\"acceptance\":\"pass\"}\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_acceptance_output_text_fallback_when_no_json() {
        // Backward compatibility: absent a JSON verdict, the legacy standalone
        // text marker remains accepted as canonical.
        let output = "ACCEPTANCE: PASS\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_acceptance_output_json_inside_agent_assistant_event() {
        // Some agent JSONL formats wrap final text in an assistant event.
        // The parser must unwrap the text payload and still find the JSON verdict.
        let event = r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"{\"acceptance\":\"pass\"}"}]}}"#;
        let output = format!("{}\n", event);
        assert_eq!(parse_acceptance_output(&output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_parse_acceptance_output_ignores_fenced_verdict_inside_agent_event() {
        let event = serde_json::json!({
            "type": "assistant",
            "message": {
                "role": "assistant",
                "content": [{
                    "type": "text",
                    "text": "~~~json\n{\"acceptance\":\"pass\"}\n~~~\nACCEPTANCE: FAIL\nFINDINGS:\n- real"
                }]
            }
        })
        .to_string();

        assert_eq!(
            parse_acceptance_output(&event),
            AcceptanceResult::Fail {
                findings: vec!["real".to_string().into()]
            }
        );
    }

    #[test]
    fn legacy_fail_ignores_findings_before_verdict_and_inside_fences() {
        let output = "FINDINGS:\n- stale\n```text\nFINDINGS:\n- injected\n```\nACCEPTANCE: FAIL\nFINDINGS:\n- canonical";

        assert_eq!(
            parse_acceptance_output(output),
            AcceptanceResult::Fail {
                findings: vec!["canonical".to_string().into()]
            }
        );
    }

    #[test]
    fn test_parse_acceptance_output_json_inside_agent_result_event() {
        // Some agent JSONL formats carry the assistant's final text in `result`.
        // Parser must unwrap and accept the JSON verdict.
        let event = r#"{"type":"result","subtype":"success","result":"{\"acceptance\":\"fail\",\"findings\":[\"a\"]}","is_error":false}"#;
        let output = format!("{}\n", event);
        match parse_acceptance_output(&output) {
            AcceptanceResult::Fail { findings } => {
                assert_eq!(findings, vec!["a".to_string()]);
            }
            other => panic!("expected Fail, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_acceptance_output_json_inside_codex_item_completed_event() {
        let event = r#"{"type":"item.completed","item":{"id":"item_0","type":"agent_message","text":"{\"acceptance\":\"pass\"}"}}"#;
        let output = format!("{}\n", event);
        assert_eq!(parse_acceptance_output(&output), AcceptanceResult::Pass);
    }

    #[test]
    fn test_detect_verdict_in_line_json_direct() {
        assert_eq!(
            detect_verdict_in_line(r#"{"acceptance":"pass"}"#),
            Some("pass")
        );
        assert_eq!(
            detect_verdict_in_line(r#"{"acceptance":"gated"}"#),
            Some("gated")
        );
        assert_eq!(
            detect_verdict_in_line(r#"{"acceptance":"blocked"}"#),
            Some("gated")
        );
    }

    #[test]
    fn test_detect_verdict_in_line_json_inside_assistant_event() {
        let event = r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"{\"acceptance\":\"pass\"}"}]}}"#;
        assert_eq!(detect_verdict_in_line(event), Some("pass"));
    }

    #[test]
    fn test_detect_verdict_in_line_text_fallback() {
        assert_eq!(detect_verdict_in_line("ACCEPTANCE: PASS"), Some("pass"));
        assert_eq!(detect_verdict_in_line("**ACCEPTANCE: FAIL**"), Some("fail"));
        assert_eq!(
            detect_verdict_in_line("ACCEPTANCE: PASSAll checks done"),
            None,
            "trailing-text PASS must not satisfy even the text fallback"
        );
    }

    #[test]
    fn test_detect_verdict_in_line_text_inside_assistant_event() {
        // When the agent produces the legacy text marker but wrapped in a
        // stream-json assistant event, the detector MUST still recognize the
        // fallback so acceptance does not stall waiting for JSON.
        let event = r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"ACCEPTANCE: PASS"}]}}"#;
        assert_eq!(detect_verdict_in_line(event), Some("pass"));
    }

    #[test]
    fn test_detect_verdict_in_line_unrelated_events_return_none() {
        assert_eq!(
            detect_verdict_in_line(r#"{"type":"system","subtype":"init"}"#),
            None
        );
        assert_eq!(detect_verdict_in_line("plain log line"), None);
        assert_eq!(detect_verdict_in_line(""), None);
    }

    // --- Structured stalled-blocker contract tests ---

    fn structured_gated(blocker: &str) -> String {
        format!("{{\"acceptance\":\"gated\",\"blocker\":{blocker}}}\n")
    }

    const VALID_BLOCKER: &str = r#"{"category":"credential","evidence":["STAGING_API_KEY unset in the verification environment"],"unblock_condition":"STAGING_API_KEY is present in the verification environment","next_action":"provision STAGING_API_KEY then retry acceptance","resumable":true}"#;

    #[test]
    fn structured_blocker_becomes_validated_stall_with_explicit_category() {
        match parse_acceptance_output(&structured_gated(VALID_BLOCKER)) {
            AcceptanceResult::Stalled { blocker } => {
                assert_eq!(blocker.category, "credential");
                assert_eq!(
                    blocker.evidence,
                    ["STAGING_API_KEY unset in the verification environment"]
                );
                assert_eq!(
                    blocker.unblock_condition,
                    "STAGING_API_KEY is present in the verification environment"
                );
                assert_eq!(
                    blocker.next_action,
                    "provision STAGING_API_KEY then retry acceptance"
                );
                assert!(blocker.resumable);
                assert_eq!(blocker.prerequisite_owner, None);
                assert!(blocker.evidence_ids.is_empty());
            }
            other => panic!("expected validated stall, got {other:?}"),
        }
    }

    #[test]
    fn structured_blocker_preserves_optional_owner_and_evidence_ids() {
        let blocker = r#"{"category":"external_approval","evidence":["change board ticket CB-42 is awaiting sign-off"],"unblock_condition":"CB-42 is approved","next_action":"await CB-42 approval then retry acceptance","resumable":true,"prerequisite_owner":"release-management","evidence_ids":["CB-42",""," "]}"#;
        match parse_acceptance_output(&structured_gated(blocker)) {
            AcceptanceResult::Stalled { blocker } => {
                assert_eq!(blocker.category, "external_approval");
                assert_eq!(
                    blocker.prerequisite_owner.as_deref(),
                    Some("release-management")
                );
                assert_eq!(blocker.evidence_ids, ["CB-42"]);
            }
            other => panic!("expected validated stall, got {other:?}"),
        }
    }

    #[test]
    fn every_supported_category_is_accepted_verbatim() {
        for category in SUPPORTED_BLOCKER_CATEGORIES {
            let blocker = format!(
                r#"{{"category":"{category}","evidence":["concrete evidence"],"unblock_condition":"the prerequisite is satisfied","next_action":"resolve then retry","resumable":false}}"#
            );
            match parse_acceptance_output(&structured_gated(&blocker)) {
                AcceptanceResult::Stalled { blocker } => {
                    assert_eq!(&blocker.category, category);
                    assert!(!blocker.resumable);
                }
                other => panic!("category {category} must validate, got {other:?}"),
            }
        }
    }

    /// Table-driven rejection matrix: every incomplete or malformed blocker
    /// payload falls back to the bounded protocol-error path, never a stall.
    #[test]
    fn invalid_blocker_payloads_fall_back_to_bare_protocol_error() {
        let cases: &[(&str, BlockerRejection)] = &[
            (r#"null"#, BlockerRejection::Missing),
            (r#""credential""#, BlockerRejection::NotAnObject),
            (r#"["credential"]"#, BlockerRejection::NotAnObject),
            (
                r#"{"evidence":["e"],"unblock_condition":"c","next_action":"a","resumable":true}"#,
                BlockerRejection::MissingCategory,
            ),
            (
                r#"{"category":"   ","evidence":["e"],"unblock_condition":"c","next_action":"a","resumable":true}"#,
                BlockerRejection::MissingCategory,
            ),
            (
                r#"{"category":"flaky_test","evidence":["e"],"unblock_condition":"c","next_action":"a","resumable":true}"#,
                BlockerRejection::UnsupportedCategory("flaky_test".to_string()),
            ),
            (
                r#"{"category":"credential","evidence":[],"unblock_condition":"c","next_action":"a","resumable":true}"#,
                BlockerRejection::EmptyEvidence,
            ),
            (
                r#"{"category":"credential","evidence":["  "],"unblock_condition":"c","next_action":"a","resumable":true}"#,
                BlockerRejection::EmptyEvidence,
            ),
            (
                r#"{"category":"credential","unblock_condition":"c","next_action":"a","resumable":true}"#,
                BlockerRejection::EmptyEvidence,
            ),
            (
                r#"{"category":"credential","evidence":["e"],"next_action":"a","resumable":true}"#,
                BlockerRejection::MissingUnblockCondition,
            ),
            (
                r#"{"category":"credential","evidence":["e"],"unblock_condition":"  ","next_action":"a","resumable":true}"#,
                BlockerRejection::MissingUnblockCondition,
            ),
            (
                r#"{"category":"credential","evidence":["e"],"unblock_condition":"c","resumable":true}"#,
                BlockerRejection::MissingNextAction,
            ),
            (
                r#"{"category":"credential","evidence":["e"],"unblock_condition":"c","next_action":"  ","resumable":true}"#,
                BlockerRejection::MissingNextAction,
            ),
            (
                r#"{"category":"credential","evidence":["e"],"unblock_condition":"c","next_action":"a"}"#,
                BlockerRejection::MissingResumable,
            ),
            (
                r#"{"category":"credential","evidence":["e"],"unblock_condition":"c","next_action":"a","resumable":"yes"}"#,
                BlockerRejection::MissingResumable,
            ),
        ];

        for (payload, expected) in cases {
            assert_eq!(
                parse_acceptance_output(&structured_gated(payload)),
                AcceptanceResult::BareBlocker {
                    rejection: expected.clone()
                },
                "payload must not become a stall: {payload}"
            );
        }
    }

    /// Prose containing credential/token/auth words must never be promoted to a
    /// category. Only an explicit supported `category` field can do that.
    #[test]
    fn credential_prose_never_infers_a_blocker_category() {
        let outputs = [
            "{\"acceptance\":\"gated\",\"findings\":[\"missing credential token for auth\"]}\n"
                .to_string(),
            structured_gated(
                r#"{"evidence":["missing credential token for auth"],"next_action":"provision it","resumable":true}"#,
            ),
            "ACCEPTANCE: GATED\nThe deploy credential token could not be read (auth failure)\n"
                .to_string(),
        ];
        for output in outputs {
            match parse_acceptance_output(&output) {
                AcceptanceResult::BareBlocker { .. } => {}
                other => panic!("prose must not create a category, got {other:?}"),
            }
        }
    }

    /// Bare and legacy compatibility inputs share one protocol-error path.
    #[test]
    fn bare_and_legacy_blocker_inputs_share_the_protocol_error_path() {
        let bare = AcceptanceResult::BareBlocker {
            rejection: BlockerRejection::Missing,
        };
        for output in [
            "{\"acceptance\":\"gated\"}\n",
            "{\"acceptance\":\"blocked\"}\n",
            "ACCEPTANCE: GATED\n",
            "ACCEPTANCE: BLOCKED\n",
            "**ACCEPTANCE: GATED**\n",
        ] {
            assert_eq!(
                parse_acceptance_output(output),
                bare,
                "drifted for {output:?}"
            );
        }
    }

    #[test]
    fn blocker_rejection_reasons_are_operator_readable() {
        assert!(BlockerRejection::Missing
            .reason()
            .contains("no structured blocker"));
        assert!(BlockerRejection::UnsupportedCategory("nope".to_string())
            .reason()
            .contains("credential"));
        assert!(BlockerRejection::EmptyEvidence
            .reason()
            .contains("evidence"));
        assert!(BlockerRejection::MissingNextAction
            .reason()
            .contains("next_action"));
        assert!(BlockerRejection::MissingResumable
            .reason()
            .contains("resumable"));
        assert!(BlockerRejection::NotAnObject
            .reason()
            .contains("JSON object"));
        assert!(BlockerRejection::MissingCategory
            .reason()
            .contains("category"));
    }

    #[test]
    fn structured_blocker_survives_agent_event_wrapping() {
        let text = format!("{{\"acceptance\":\"gated\",\"blocker\":{VALID_BLOCKER}}}");
        let event = serde_json::json!({
            "type": "assistant",
            "message": {"role": "assistant", "content": [{"type": "text", "text": text}]}
        })
        .to_string();
        assert!(matches!(
            parse_acceptance_output(&event),
            AcceptanceResult::Stalled { .. }
        ));
    }

    /// Regression for the malformed trailing-text real-log case
    /// (`ACCEPTANCE: PASS# ...`): with the legacy text-only contract, the
    /// parser returned CONTINUE and the acceptance loop retried. Under the new
    /// JSON-primary contract the agent emits a strict JSON verdict on a later
    /// line, and that JSON verdict MUST win regardless of the earlier drift.
    #[test]
    fn test_regression_malformed_text_then_json_pass() {
        let output = "ACCEPTANCE: PASS# Acceptance Review Summary\n{\"acceptance\":\"pass\"}\n";
        assert_eq!(parse_acceptance_output(output), AcceptanceResult::Pass);
    }
    // --- Structured repository finding contract ---

    fn secret_value_finding_json() -> String {
        serde_json::json!({
            "acceptance": "fail",
            "findings": [{
                "id": "acceptance-secret-value-scan",
                "severity": "minor",
                "summary": "Challenge and proof leakage is not tested by value",
                "evidence": ["tests/support/relay.ts exposes counts but not issued values"],
                "required_changes": [{
                    "file": "tests/support/relay.ts",
                    "description": "Expose issued challenge and presented proof values to tests"
                }],
                "verification": [{
                    "file": "runtime/recovery.integration.test.ts",
                    "description": "Assert recorded values are absent from serialized audit output"
                }]
            }]
        })
        .to_string()
    }

    fn fail_findings(output: &str) -> Vec<AcceptanceFinding> {
        match parse_acceptance_output(output) {
            AcceptanceResult::Fail { findings } => findings,
            other => panic!("expected Fail, got {other:?}"),
        }
    }

    fn rejection(output: &str) -> FindingRejection {
        match parse_acceptance_output(output) {
            AcceptanceResult::MalformedFinding { rejection } => rejection,
            other => panic!("expected MalformedFinding, got {other:?}"),
        }
    }

    #[test]
    fn acceptance_structured_finding_reaches_callers_without_lossy_conversion() {
        let findings = fail_findings(&secret_value_finding_json());
        assert_eq!(findings.len(), 1);
        let structured = findings[0]
            .structured_payload()
            .expect("structured payload must survive parsing");

        assert_eq!(structured.id, "acceptance-secret-value-scan");
        assert_eq!(structured.severity, FindingSeverity::Minor);
        assert_eq!(
            structured.summary,
            "Challenge and proof leakage is not tested by value"
        );
        assert_eq!(
            structured.evidence,
            ["tests/support/relay.ts exposes counts but not issued values"]
        );
        assert_eq!(structured.required_files(), ["tests/support/relay.ts"]);
        assert_eq!(
            structured.verification_files(),
            ["runtime/recovery.integration.test.ts"]
        );

        // The single-line rendering keeps every actionable field, so no surface
        // that only carries text can silently reduce this to a path hint.
        let text = findings[0].text();
        assert!(text.contains("acceptance-secret-value-scan"), "{text}");
        assert!(
            text.contains("exposes counts but not issued values"),
            "{text}"
        );
        assert!(text.contains("tests/support/relay.ts"), "{text}");
        assert!(
            text.contains("runtime/recovery.integration.test.ts"),
            "{text}"
        );
    }

    #[test]
    fn acceptance_structured_finding_accepts_both_severities_and_both_block_pass() {
        for severity in ["major", "minor"] {
            let output = serde_json::json!({
                "acceptance": "fail",
                "findings": [{
                    "id": format!("id-{severity}"),
                    "severity": severity,
                    "summary": "s",
                    "evidence": ["e"],
                    "required_changes": [{"file": "src/a.rs", "description": "d"}],
                    "verification": [{"file": "tests/a.rs", "description": "d"}]
                }]
            })
            .to_string();
            let result = parse_acceptance_output(&output);
            assert!(
                matches!(result, AcceptanceResult::Fail { .. }),
                "{severity} must block PASS, got {result:?}"
            );
        }
    }

    #[test]
    fn acceptance_structured_finding_rejection_matrix_is_bounded_protocol_handling() {
        let base = serde_json::json!({
            "id": "stable-id",
            "severity": "major",
            "summary": "s",
            "evidence": ["e"],
            "required_changes": [{"file": "src/a.rs", "description": "d"}],
            "verification": [{"file": "tests/a.rs", "description": "d"}]
        });
        let mutate = |key: &str, value: serde_json::Value| {
            let mut object = base.as_object().unwrap().clone();
            if value.is_null() {
                object.remove(key);
            } else {
                object.insert(key.to_string(), value);
            }
            serde_json::json!({"acceptance": "fail", "findings": [object]}).to_string()
        };

        let cases: Vec<(String, FindingRejection)> = vec![
            (
                mutate("id", serde_json::Value::Null),
                FindingRejection::MissingId,
            ),
            (
                mutate("id", serde_json::json!("   ")),
                FindingRejection::MissingId,
            ),
            (
                mutate("severity", serde_json::json!("blocker")),
                FindingRejection::UnsupportedSeverity("blocker".to_string()),
            ),
            (
                mutate("summary", serde_json::Value::Null),
                FindingRejection::MissingSummary,
            ),
            (
                mutate("evidence", serde_json::json!([])),
                FindingRejection::EmptyEvidence,
            ),
            (
                mutate("required_changes", serde_json::Value::Null),
                FindingRejection::EmptyRequiredChanges,
            ),
            (
                mutate("verification", serde_json::json!([])),
                FindingRejection::EmptyVerification,
            ),
            (
                mutate(
                    "required_changes",
                    serde_json::json!([{"file": "../../etc/passwd", "description": "d"}]),
                ),
                FindingRejection::InvalidPath {
                    field: "required_changes",
                    path: "../../etc/passwd".to_string(),
                },
            ),
            (
                mutate(
                    "verification",
                    serde_json::json!([{"file": "/abs/path.rs", "description": "d"}]),
                ),
                FindingRejection::InvalidPath {
                    field: "verification",
                    path: "/abs/path.rs".to_string(),
                },
            ),
            (
                mutate(
                    "verification",
                    serde_json::json!([{"file": "tests/a.rs", "description": "  "}]),
                ),
                FindingRejection::MissingDescription {
                    field: "verification",
                    path: "tests/a.rs".to_string(),
                },
            ),
        ];

        for (output, expected) in cases {
            assert_eq!(rejection(&output), expected, "routing drifted for {output}");
        }
    }

    #[test]
    fn acceptance_duplicate_structured_ids_are_a_protocol_error() {
        let finding = serde_json::json!({
            "id": "same-id",
            "severity": "major",
            "summary": "s",
            "evidence": ["e"],
            "required_changes": [{"file": "src/a.rs", "description": "d"}],
            "verification": [{"file": "tests/a.rs", "description": "d"}]
        });
        let output =
            serde_json::json!({"acceptance": "fail", "findings": [finding.clone(), finding]})
                .to_string();
        assert_eq!(
            rejection(&output),
            FindingRejection::DuplicateId("same-id".to_string())
        );
    }

    #[test]
    fn acceptance_non_object_non_string_finding_entry_is_a_protocol_error() {
        let output = r#"{"acceptance":"fail","findings":[42]}"#;
        assert_eq!(rejection(output), FindingRejection::NotStringOrObject);
    }

    #[test]
    fn acceptance_malformed_structured_finding_never_becomes_path_only_work() {
        // Regression shape: an object naming a file but omitting verification
        // must not degrade into "fix src/a.rs" style repair instructions.
        let output = serde_json::json!({
            "acceptance": "fail",
            "findings": [{
                "id": "partial",
                "severity": "major",
                "summary": "s",
                "evidence": ["e"],
                "required_changes": [{"file": "src/a.rs", "description": "d"}]
            }]
        })
        .to_string();
        let result = parse_acceptance_output(&output);
        assert!(
            matches!(result, AcceptanceResult::MalformedFinding { .. }),
            "got {result:?}"
        );
        if let AcceptanceResult::Fail { findings } = result {
            panic!("malformed structured finding degraded into {findings:?}");
        }
    }

    #[test]
    fn acceptance_mixed_legacy_and_structured_findings_both_survive() {
        let output = serde_json::json!({
            "acceptance": "fail",
            "findings": [
                "src/legacy.rs:10 missing regression coverage",
                {
                    "id": "structured-id",
                    "severity": "major",
                    "summary": "s",
                    "evidence": ["e"],
                    "required_changes": [{"file": "src/a.rs", "description": "d"}],
                    "verification": [{"file": "tests/a.rs", "description": "d"}]
                }
            ]
        })
        .to_string();

        let findings = fail_findings(&output);
        assert_eq!(findings.len(), 2);
        assert_eq!(findings[0].id(), None);
        assert_eq!(
            findings[0].text(),
            "src/legacy.rs:10 missing regression coverage"
        );
        assert_eq!(findings[1].id(), Some("structured-id"));
    }

    #[test]
    fn acceptance_legacy_verdict_syntax_is_unchanged() {
        // Both legacy shapes keep their complete original text and gain no
        // structured payload.
        for output in [
            "ACCEPTANCE: FAIL\nFINDINGS:\n- src/a.rs:1 issue\n",
            r#"{"acceptance":"fail","findings":["src/a.rs:1 issue"]}"#,
        ] {
            let findings = fail_findings(output);
            assert_eq!(findings.len(), 1, "{output}");
            assert_eq!(findings[0].text(), "src/a.rs:1 issue", "{output}");
            assert!(findings[0].structured_payload().is_none(), "{output}");
            assert!(!findings[0].declares_paths(), "{output}");
        }
    }

    #[test]
    fn acceptance_repository_path_normalization_rejects_escapes() {
        assert_eq!(
            normalize_repository_path("./src//a.rs"),
            Some("src/a.rs".to_string())
        );
        assert_eq!(
            normalize_repository_path("src\\a.rs"),
            Some("src/a.rs".to_string())
        );
        for escape in [
            "",
            "  ",
            "/etc/passwd",
            "~/secrets",
            "C:/x",
            "../up",
            "a/../../b",
        ] {
            assert_eq!(normalize_repository_path(escape), None, "{escape}");
        }
    }
}