beads_rust 0.5.7

Agent-first issue tracker (SQLite + JSONL)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
//! Close command implementation.

use crate::cli::CloseArgs as CliCloseArgs;
use crate::cli::commands::{
    acquire_routed_workspace_write_lock, auto_import_storage_ctx_if_stale,
    finalize_batched_blocked_cache_refresh, preserve_blocked_cache_on_error,
    report_auto_flush_failure, resolve_issue_ids, update_issues_atomically_with_recovery,
};
use crate::close_policy::{
    self, AttributionTier, AttributionValues, CloseEvidence, ClosePolicy, PolicyDocument,
    PolicyViolation,
};
use crate::config;
use crate::error::{BeadsError, Result};
use crate::format::sanitize_terminal_inline;
use crate::model::{Issue, IssueType, Status};
use crate::output::OutputContext;
use crate::storage::{EventAttribution, IssueUpdate, SqliteStorage};
use crate::util::id::{IdResolver, ResolverConfig};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};
use std::path::Path;

/// Internal arguments for the close command.
#[derive(Debug, Clone, Default)]
pub struct CloseArgs {
    /// Issue IDs to close
    pub ids: Vec<String>,
    /// Close reason
    pub reason: Option<String>,
    /// New comment committed atomically with the close transition.
    pub transition_comment: Option<String>,
    /// Force close even if blocked
    pub force: bool,
    /// Session ID for `closed_by_session` field
    pub session: Option<String>,
    /// Return newly unblocked issues (single ID only)
    pub suggest_next: bool,
    /// Tier 1 attribution: agent name (issue #274 Phase 1).
    pub agent_name: Option<String>,
    /// Tier 1 attribution: harness identifier.
    pub harness: Option<String>,
    /// Tier 1 attribution: model identifier.
    pub model: Option<String>,
    /// Bypass closure-time policy gates.
    pub bypass_policy: bool,
    /// Reason for bypass. Required when `bypass_policy = true`.
    pub bypass_reason: Option<String>,
}

impl From<&CliCloseArgs> for CloseArgs {
    fn from(cli: &CliCloseArgs) -> Self {
        Self {
            ids: cli.ids.clone(),
            reason: cli.reason.clone(),
            transition_comment: cli.transition_comment.clone(),
            force: cli.force,
            session: cli.session.clone(),
            suggest_next: cli.suggest_next,
            agent_name: cli.agent_name.clone(),
            harness: cli.harness.clone(),
            model: cli.model.clone(),
            bypass_policy: cli.bypass_policy,
            bypass_reason: cli.bypass_reason.clone(),
        }
    }
}

/// Aggregate of policy gates that fired for a single candidate close.
struct EvaluatedGates {
    violations: Vec<PolicyViolation>,
}

/// Validate the `--bypass-policy` / `--bypass-reason` flag pair before
/// touching storage. Mirrors the documented contract: bypass requires a
/// non-empty reason and is meaningless without `--bypass-policy`.
fn validate_bypass_args(args: &CloseArgs) -> Result<()> {
    if args.bypass_policy {
        let reason_present = args
            .bypass_reason
            .as_deref()
            .map(str::trim)
            .is_some_and(|s| !s.is_empty());
        if !reason_present {
            return Err(BeadsError::validation(
                "bypass-reason",
                "--bypass-policy requires --bypass-reason \"<text>\"",
            ));
        }
    } else if args
        .bypass_reason
        .as_deref()
        .map(str::trim)
        .is_some_and(|s| !s.is_empty())
    {
        return Err(BeadsError::validation(
            "bypass-policy",
            "--bypass-reason was set without --bypass-policy",
        ));
    }
    Ok(())
}

/// Resolve attribution values for the close. CLI flags take precedence over
/// env vars; both are ignored when the policy.yaml `attribution.tier` is
/// `off`. Tier 2/3 ("require"/"allowlist") are out of scope for Phase 1.
fn resolve_attribution_for_close(
    args: &CloseArgs,
    policy_doc: &PolicyDocument,
) -> AttributionValues {
    if policy_doc.close_policy.attribution.tier == AttributionTier::Off {
        return AttributionValues::default();
    }
    AttributionValues::resolve_from_env(
        args.agent_name.as_deref(),
        args.harness.as_deref(),
        args.model.as_deref(),
    )
}

/// Run every enabled gate against `issue` and produce the (possibly empty)
/// violation list. This includes the close-policy gates (issue #274) *and* the
/// workflow gate engine (issue #312, layer 2): closing an issue is a transition
/// into the `closed` state, so any `workflow.gates` rule guarding
/// `"<current> -> closed"` is enforced here too.
fn evaluate_close_policy(
    policy: &ClosePolicy,
    workflow: &crate::close_policy::Workflow,
    storage: &SqliteStorage,
    issue_id: &str,
    issue: &Issue,
    args: &CloseArgs,
    close_actor: &str,
) -> Result<EvaluatedGates> {
    // Look up the in_progress actor only when the gate is enabled — this
    // saves a query per close for repos that don't enable that specific
    // gate.
    let in_progress_actor = if policy.forbid_self_close_after_in_progress.enabled {
        storage.find_last_in_progress_actor(issue_id)?
    } else {
        None
    };

    let evidence = CloseEvidence {
        issue_id,
        close_reason: args.reason.as_deref(),
        description: issue.description.as_deref(),
        design: issue.design.as_deref(),
        acceptance_criteria: issue.acceptance_criteria.as_deref(),
        notes: issue.notes.as_deref(),
        close_actor,
        in_progress_actor: in_progress_actor.as_deref(),
    };

    let mut violations = close_policy::evaluate(policy, &evidence);

    // Deferred-dependents gate (beads_rust#303). Storage-backed, so it lives
    // here rather than in the pure `close_policy::evaluate`. We only query when
    // the gate is enabled to avoid a dependency lookup per close otherwise.
    if policy.forbid_close_with_deferred_dependents.enabled {
        let deferred_dependents = collect_deferred_blocks_dependents(storage, issue_id)?;
        if let Some(violation) =
            close_policy::deferred_dependents_violation(issue_id, &deferred_dependents)
        {
            violations.push(violation);
        }
    }

    // The storage transaction is the canonical gate/required-field chokepoint.
    // Pre-evaluate these rules only for an explicit bypass so the warning and
    // close metadata can name exactly what the operator bypassed without
    // introducing a read-before-write race on the ordinary path.
    if args.bypass_policy {
        let from = issue.status.as_str();
        let to = Status::Closed.as_str();
        violations.extend(close_policy::evaluate_transition_required_fields(
            workflow,
            issue_id,
            Some(from),
            to,
            issue.acceptance_criteria.as_deref(),
            args.transition_comment.as_deref(),
        ));
        if workflow.gates_enforced() && workflow.gate_rule_for(from, to).is_some() {
            let labels = storage.get_labels(issue_id)?;
            let results = storage.get_scoped_gate_results(issue_id, from, to)?;
            violations.extend(close_policy::evaluate_gates(
                workflow,
                issue_id,
                from,
                to,
                &labels,
                issue.priority.0,
                &results,
            ));
        }
    }

    Ok(EvaluatedGates { violations })
}

/// Collect the IDs of issues that have a `blocks` edge *from* `issue_id`
/// (i.e. depend on `issue_id` as a prerequisite) and are currently in
/// `deferred` status. Used by the `forbid_close_with_deferred_dependents`
/// gate (beads_rust#303).
///
/// Edge direction: a `blocks` dependency row
/// `(issue_id=DEP, depends_on_id=PREREQ)` means "PREREQ blocks DEP" / "DEP
/// depends on PREREQ". `get_dependents_with_metadata` returns the `DEP` rows
/// for a given `PREREQ`, which is exactly the set we filter.
fn collect_deferred_blocks_dependents(
    storage: &SqliteStorage,
    issue_id: &str,
) -> Result<Vec<String>> {
    let dependents = storage.get_dependents_with_metadata(issue_id)?;
    Ok(dependents
        .into_iter()
        .filter(|dependent| {
            dependent.dep_type == crate::model::DependencyType::Blocks.as_str()
                && dependent.status == Status::Deferred
        })
        .map(|dependent| dependent.id)
        .collect())
}

fn summarize_violations(violations: &[PolicyViolation]) -> String {
    if let [single] = violations {
        return single.message.clone();
    }
    let lines: Vec<String> = violations
        .iter()
        .map(|v| format!("- {}", v.message))
        .collect();
    format!("{} gates fired:\n{}", violations.len(), lines.join("\n"))
}

fn emit_bypass_warning(ctx: &OutputContext, issue_id: &str, violations: &[PolicyViolation]) {
    let summary = summarize_violations(violations);
    let id = sanitize_terminal_inline(issue_id);
    let summary = sanitize_terminal_inline(&summary);
    ctx.warning(&format!(
        "Closing {} despite policy violation(s) (--bypass-policy): {}",
        id.as_ref(),
        summary.as_ref()
    ));
}

/// Execute the close command from CLI args.
///
/// # Errors
///
/// Returns an error if database operations fail or IDs cannot be resolved.
pub fn execute_cli(
    cli_args: &CliCloseArgs,
    json: bool,
    cli: &config::CliOverrides,
    ctx: &OutputContext,
) -> Result<()> {
    let args = CloseArgs::from(cli_args);
    execute_with_args(&args, json, cli, ctx)
}

/// Result of a close operation for JSON output.
#[derive(Debug, Serialize, Deserialize)]
pub struct CloseResult {
    pub closed: Vec<ClosedIssue>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub skipped: Vec<SkippedIssue>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub warnings: Vec<crate::close_policy::WorkflowCapacityWarning>,
}

/// Result of closing with suggest-next.
#[derive(Debug, Serialize, Deserialize)]
pub struct CloseWithSuggestResult {
    pub closed: Vec<ClosedIssue>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub skipped: Vec<SkippedIssue>,
    pub unblocked: Vec<UnblockedIssue>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub warnings: Vec<crate::close_policy::WorkflowCapacityWarning>,
}

/// An issue that became unblocked after closing.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnblockedIssue {
    pub id: String,
    pub title: String,
    pub priority: i32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClosedIssue {
    pub id: String,
    pub title: String,
    pub status: String,
    pub closed_at: String,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub close_reason: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SkippedIssue {
    pub id: String,
    pub reason: String,
}

#[allow(dead_code)]
#[derive(Debug, Default)]
struct CloseExecution {
    closed: Vec<ClosedIssue>,
    skipped: Vec<SkippedIssue>,
    unblocked: Vec<UnblockedIssue>,
    ordered_outcomes: Vec<CloseOutcome>,
    capacity_warnings: Vec<crate::close_policy::WorkflowCapacityWarning>,
}

#[derive(Debug, Clone)]
enum CloseOutcome {
    Closed(ClosedIssue),
    Skipped(SkippedIssue),
}

fn build_close_json_payload(
    args: &CloseArgs,
    closed_issues: Vec<ClosedIssue>,
    skipped_issues: Vec<SkippedIssue>,
    unblocked_issues: Vec<UnblockedIssue>,
    capacity_warnings: Vec<crate::close_policy::WorkflowCapacityWarning>,
) -> Result<String> {
    let json = if args.suggest_next {
        // suggest_next is br-only, so always use the wrapped machine format.
        let result = CloseWithSuggestResult {
            closed: closed_issues,
            skipped: skipped_issues,
            unblocked: unblocked_issues,
            warnings: capacity_warnings,
        };
        serde_json::to_string_pretty(&result)?
    } else if skipped_issues.is_empty() && capacity_warnings.is_empty() {
        // Preserve bd-compatible array output for pure-success closes.
        serde_json::to_string_pretty(&closed_issues)?
    } else {
        // Once skips are present, a bare array loses machine-readable reasons.
        let result = CloseResult {
            closed: closed_issues,
            skipped: skipped_issues,
            warnings: capacity_warnings,
        };
        serde_json::to_string_pretty(&result)?
    };

    Ok(json)
}

fn render_close_json(
    args: &CloseArgs,
    closed_issues: Vec<ClosedIssue>,
    skipped_issues: Vec<SkippedIssue>,
    unblocked_issues: Vec<UnblockedIssue>,
    capacity_warnings: Vec<crate::close_policy::WorkflowCapacityWarning>,
) -> Result<()> {
    let json = build_close_json_payload(
        args,
        closed_issues,
        skipped_issues,
        unblocked_issues,
        capacity_warnings,
    )?;
    println!("{json}");
    Ok(())
}

fn emit_close_structured_output(
    args: &CloseArgs,
    closed_issues: Vec<ClosedIssue>,
    skipped_issues: Vec<SkippedIssue>,
    unblocked_issues: Vec<UnblockedIssue>,
    capacity_warnings: Vec<crate::close_policy::WorkflowCapacityWarning>,
    ctx: &OutputContext,
) -> Result<()> {
    if args.suggest_next {
        let result = CloseWithSuggestResult {
            closed: closed_issues,
            skipped: skipped_issues,
            unblocked: unblocked_issues,
            warnings: capacity_warnings,
        };
        if ctx.is_toon() {
            ctx.toon(&result);
        } else if ctx.is_json() {
            ctx.json_pretty(&result);
        } else {
            let json_ctx = OutputContext::from_flags(true, false, true);
            json_ctx.json_pretty(&result);
        }
        return Ok(());
    }

    if skipped_issues.is_empty() && capacity_warnings.is_empty() {
        if ctx.is_toon() {
            ctx.toon(&closed_issues);
        } else if ctx.is_json() {
            ctx.json_pretty(&closed_issues);
        } else {
            render_close_json(
                args,
                closed_issues,
                skipped_issues,
                unblocked_issues,
                capacity_warnings,
            )?;
        }
        return Ok(());
    }

    let result = CloseResult {
        closed: closed_issues,
        skipped: skipped_issues,
        warnings: capacity_warnings,
    };
    if ctx.is_toon() {
        ctx.toon(&result);
    } else if ctx.is_json() {
        ctx.json_pretty(&result);
    } else {
        let json_ctx = OutputContext::from_flags(true, false, true);
        json_ctx.json_pretty(&result);
    }
    Ok(())
}

fn close_human_message(closed: &ClosedIssue) -> String {
    let id = sanitize_terminal_inline(&closed.id);
    let title = sanitize_terminal_inline(&closed.title);
    let mut message = format!("Closed {}: {}", id.as_ref(), title.as_ref());
    if let Some(reason) = &closed.close_reason {
        let reason = sanitize_terminal_inline(reason);
        message.push_str(" (");
        message.push_str(reason.as_ref());
        message.push(')');
    }
    message
}

fn skipped_human_message(skipped: &SkippedIssue) -> String {
    let id = sanitize_terminal_inline(&skipped.id);
    let reason = sanitize_terminal_inline(&skipped.reason);
    format!("Skipped {}: {}", id.as_ref(), reason.as_ref())
}

fn unblocked_human_line(issue: &UnblockedIssue) -> String {
    let id = sanitize_terminal_inline(&issue.id);
    let title = sanitize_terminal_inline(&issue.title);
    format!("  {}: {}", id.as_ref(), title.as_ref())
}

fn issue_input_text(input: &str) -> String {
    sanitize_terminal_inline(input).into_owned()
}

fn reorder_routed_items_by_requested_inputs<T>(
    requested_inputs: &[String],
    routed_items: Vec<(Vec<String>, Vec<T>)>,
    context: &str,
) -> Result<Vec<T>> {
    let mut positions_by_input: HashMap<&str, VecDeque<usize>> = HashMap::new();
    for (index, input) in requested_inputs.iter().enumerate() {
        positions_by_input
            .entry(input.as_str())
            .or_default()
            .push_back(index);
    }

    let mut ordered_items: Vec<Option<T>> = (0..requested_inputs.len()).map(|_| None).collect();
    for (batch_inputs, batch_items) in routed_items {
        if batch_inputs.len() != batch_items.len() {
            return Err(BeadsError::internal(format!(
                "{context} produced mismatched issue/result counts"
            )));
        }

        for (input, item) in batch_inputs.into_iter().zip(batch_items) {
            let Some(index) = positions_by_input
                .get_mut(input.as_str())
                .and_then(VecDeque::pop_front)
            else {
                let input = issue_input_text(&input);
                return Err(BeadsError::internal(format!(
                    "{context} returned unexpected issue input {input}"
                )));
            };
            let Some(slot) = ordered_items.get_mut(index) else {
                let input = issue_input_text(&input);
                return Err(BeadsError::internal(format!(
                    "{context} returned out-of-range issue input {input}"
                )));
            };
            *slot = Some(item);
        }
    }

    ordered_items
        .into_iter()
        .enumerate()
        .map(|(index, item)| {
            item.ok_or_else(|| {
                let input = requested_inputs
                    .get(index)
                    .map(|input| issue_input_text(input))
                    .unwrap_or_else(|| "<unknown>".to_string());
                BeadsError::internal(format!("{context} did not produce a result for {input}"))
            })
        })
        .collect()
}

fn compute_batch_closable_ids(
    active_issue_ids: &HashSet<String>,
    internal_blockers_by_id: &HashMap<String, Vec<String>>,
    external_blockers_by_id: &HashMap<String, Vec<String>>,
) -> HashSet<String> {
    let mut closable: HashSet<String> = active_issue_ids
        .iter()
        .filter(|id| {
            external_blockers_by_id
                .get(*id)
                .is_none_or(std::vec::Vec::is_empty)
        })
        .cloned()
        .collect();

    loop {
        let to_remove: Vec<String> = closable
            .iter()
            .filter(|id| {
                internal_blockers_by_id
                    .get(*id)
                    .into_iter()
                    .flatten()
                    .any(|blocker_id| !closable.contains(blocker_id))
            })
            .cloned()
            .collect();

        if to_remove.is_empty() {
            break;
        }

        for id in to_remove {
            closable.remove(&id);
        }
    }

    closable
}

/// Execute the close command.
///
/// # Errors
///
/// Returns an error if database operations fail or IDs cannot be resolved.
pub fn execute(
    ids: Vec<String>,
    json: bool,
    cli: &config::CliOverrides,
    ctx: &OutputContext,
) -> Result<()> {
    let args = CloseArgs {
        ids,
        ..CloseArgs::default()
    };

    execute_with_args(&args, json, cli, ctx)
}

/// Execute the close command with full arguments.
///
/// # Errors
///
/// Returns an error if database operations fail or IDs cannot be resolved.
#[allow(clippy::too_many_lines)]
pub fn execute_with_args(
    args: &CloseArgs,
    use_json: bool,
    cli: &config::CliOverrides,
    ctx: &OutputContext,
) -> Result<()> {
    tracing::info!("Executing close command");
    let use_structured_output = use_json || ctx.is_json() || ctx.is_toon();

    // Up-front bypass argument-pair validation. Done before any storage IO so
    // a misuse of the bypass flag never silently slips past policy gates.
    validate_bypass_args(args)?;

    let beads_dir = config::discover_beads_dir_with_cli(cli)?;
    let mut target_inputs = args.ids.clone();
    if target_inputs.is_empty() {
        let last_touched = crate::util::get_last_touched_id(&beads_dir);
        if last_touched.is_empty() {
            return Err(BeadsError::validation(
                "ids",
                "no issue IDs provided and no last-touched issue",
            ));
        }
        target_inputs.push(last_touched);
    }

    if args.suggest_next && target_inputs.len() > 1 {
        return Err(BeadsError::validation(
            "suggest-next",
            "--suggest-next only works with a single issue ID",
        ));
    }
    let routed_batches = config::routing::group_issue_inputs_by_route(&target_inputs, &beads_dir)?;

    let mut closed_issues = Vec::new();
    let mut skipped_issues = Vec::new();
    let mut unblocked_issues = Vec::new();
    let mut capacity_warnings = Vec::new();

    if routed_batches.iter().any(|batch| batch.is_external) {
        let normalized_local_beads_dir =
            dunce::canonicalize(&beads_dir).unwrap_or_else(|_| beads_dir.clone());
        let mut routed_outcomes = Vec::new();

        for batch in routed_batches {
            let mut batch_args = args.clone();
            batch_args.ids.clone_from(&batch.issue_inputs);

            let normalized_batch_beads_dir =
                dunce::canonicalize(&batch.beads_dir).unwrap_or_else(|_| batch.beads_dir.clone());
            let mut batch_cli = cli.clone();
            batch_cli.db = if normalized_batch_beads_dir == normalized_local_beads_dir {
                cli.db.clone()
            } else {
                None
            };

            let execution = execute_route(
                &batch_args,
                &batch_cli,
                ctx,
                &batch.beads_dir,
                batch.is_external,
            )?;
            let CloseExecution {
                unblocked,
                ordered_outcomes,
                capacity_warnings: route_warnings,
                ..
            } = execution;
            routed_outcomes.push((batch.issue_inputs, ordered_outcomes));
            unblocked_issues.extend(unblocked);
            capacity_warnings.extend(route_warnings);
        }

        let ordered_outcomes = reorder_routed_items_by_requested_inputs(
            &target_inputs,
            routed_outcomes,
            "close routing",
        )?;
        for outcome in ordered_outcomes {
            match outcome {
                CloseOutcome::Closed(issue) => closed_issues.push(issue),
                CloseOutcome::Skipped(issue) => skipped_issues.push(issue),
            }
        }
    } else {
        let mut local_args = args.clone();
        local_args.ids = target_inputs;
        let execution = execute_route(&local_args, cli, ctx, &beads_dir, false)?;
        closed_issues = execution.closed;
        skipped_issues = execution.skipped;
        unblocked_issues = execution.unblocked;
        capacity_warnings = execution.capacity_warnings;
    }

    let closed_count = closed_issues.len();
    let skipped_count = skipped_issues.len();
    // Capture per-issue skip reasons BEFORE the vectors are moved into the
    // output emitters. When every issue is skipped, the terminal error must
    // carry the real reasons: a generic "all N skipped" used to imply
    // "already closed or not found" even when the skip was actually a
    // dependency block, sending operators down the wrong debugging path
    // (issue #380).
    let skip_summary = summarize_skip_reasons(&skipped_issues);

    if let Some(last_closed) = closed_issues.last() {
        crate::util::set_last_touched_id(&beads_dir, &last_closed.id);
    }

    if use_structured_output {
        emit_close_structured_output(
            args,
            closed_issues,
            skipped_issues,
            unblocked_issues,
            capacity_warnings,
            ctx,
        )?;
    } else if closed_issues.is_empty() && skipped_issues.is_empty() {
        ctx.info("No issues to close.");
    } else {
        for closed in &closed_issues {
            ctx.success(&close_human_message(closed));
        }
        for skipped in &skipped_issues {
            ctx.warning(&skipped_human_message(skipped));
        }
        for warning in &capacity_warnings {
            ctx.warning(&warning.to_string());
        }
        if !unblocked_issues.is_empty() {
            ctx.newline();
            ctx.info(&format!("Unblocked {} issue(s):", unblocked_issues.len()));
            for issue in &unblocked_issues {
                ctx.print_line(&unblocked_human_line(issue));
            }
        }
    }

    // Exit status must carry the outcome for EVERY requested id, not just for
    // the all-or-nothing case. Gating this on `closed_count == 0` meant a batch
    // that closed one issue and refused another exited 0 with the refusal
    // visible only as a warning on stderr — so `br close <blocked> <closeable>`
    // reported success while the blocked issue was untouched, and stdout showed
    // an unqualified "✓ Closed". `docs/agent/ERRORS.md` tells callers to parse
    // stdout precisely when the exit code is 0, which made that transcript
    // authoritative. A no-op must not be able to read as success.
    if skipped_count > 0 {
        return Err(if closed_count == 0 {
            BeadsError::NothingToDo {
                reason: format!("all {skipped_count} issue(s) skipped — {skip_summary}"),
            }
        } else {
            BeadsError::CloseIncomplete {
                closed: closed_count,
                skipped: skipped_count,
                summary: skip_summary,
            }
        });
    }

    Ok(())
}

/// Render the per-issue skip reasons for the terminal `NothingToDo` error.
///
/// Lists up to five `id: reason` pairs (sanitized for
/// terminal safety) so the error names WHY each issue was skipped instead of
/// leaving the operator to guess (issue #380). Longer batches get a
/// `+N more` suffix; JSON/robot callers still receive the full skip list in
/// the structured payload.
fn summarize_skip_reasons(skipped: &[SkippedIssue]) -> String {
    const SKIP_SUMMARY_PREVIEW: usize = 5;
    let mut parts: Vec<String> = skipped
        .iter()
        .take(SKIP_SUMMARY_PREVIEW)
        .map(|s| {
            format!(
                "{}: {}",
                sanitize_terminal_inline(&s.id),
                sanitize_terminal_inline(&s.reason)
            )
        })
        .collect();
    if skipped.len() > SKIP_SUMMARY_PREVIEW {
        parts.push(format!("+{} more", skipped.len() - SKIP_SUMMARY_PREVIEW));
    }
    parts.join("; ")
}

#[allow(clippy::too_many_lines)]
fn execute_route(
    args: &CloseArgs,
    cli: &config::CliOverrides,
    ctx: &OutputContext,
    beads_dir: &Path,
    auto_flush_external: bool,
) -> Result<CloseExecution> {
    let routed_write_lock =
        acquire_routed_workspace_write_lock(beads_dir, auto_flush_external, cli.lock_timeout)?;
    // Reuse the routed authority for the storage open below; acquiring the
    // same database-family lock from a second descriptor in this process
    // would self-deadlock until the lock timeout (#409 routed cluster).
    let mut route_cli = cli.clone();
    routed_write_lock.mark_cli_write_lock_held(&mut route_cli);
    let cli = &route_cli;
    let mut storage_ctx = config::open_storage_with_cli(beads_dir, cli)?;
    auto_import_storage_ctx_if_stale(&mut storage_ctx, cli)?;

    let config_layer = storage_ctx.load_config(cli)?;
    let actor = config::resolve_actor(&config_layer);
    let id_config = config::id_config_from_layer(&config_layer);
    let resolver = IdResolver::new(ResolverConfig::with_prefix(id_config.prefix));
    let resolved_ids = resolve_issue_ids(&storage_ctx.storage, &resolver, &args.ids)?;

    // Closure-time policy gates (issue #274 Phase 1). Loading happens once per
    // route; if the file is absent the doc is the all-off default.
    let policy_doc = close_policy::load_for_beads_dir(beads_dir)?;
    // Active when close-policy gates are enabled (issue #274) OR the workflow
    // gate engine is configured (issue #312, layer 2). The latter must also
    // trigger per-issue gate evaluation at close time.
    let policy_active = policy_doc.close_policy.is_active()
        || policy_doc.workflow.gates_enforced()
        || !policy_doc.workflow.required_fields.is_empty();
    let attribution = resolve_attribution_for_close(args, &policy_doc);
    if args.bypass_policy && !policy_doc.allow_bypass {
        return Err(BeadsError::validation(
            "bypass-policy",
            ".beads/policy.yaml has allow_bypass: false; --bypass-policy is disabled",
        ));
    }

    let epic_counts = storage_ctx.storage.get_epic_counts()?;
    let blocked_before: Vec<String> = if args.suggest_next {
        storage_ctx
            .storage
            .get_blocked_issues()?
            .into_iter()
            .map(|(i, _)| i.id)
            .collect()
    } else {
        Vec::new()
    };

    let requested_ids: HashSet<String> = resolved_ids.iter().cloned().collect();
    let mut open_issues: HashMap<String, crate::model::Issue> = HashMap::new();
    let mut internal_blockers_by_id: HashMap<String, Vec<String>> = HashMap::new();
    let mut external_blockers_by_id: HashMap<String, Vec<String>> = HashMap::new();
    let mut closed_issues: Vec<ClosedIssue> = Vec::new();
    let mut skipped_issues: Vec<SkippedIssue> = Vec::new();
    let mut ordered_outcomes = Vec::with_capacity(resolved_ids.len());
    let mut cache_dirty = false;
    let mut capacity_warnings = Vec::new();

    let mut atomic_updates = Vec::new();
    let mut planned_closes = Vec::new();
    for (outcome_index, id) in resolved_ids.iter().enumerate() {
        ordered_outcomes.push(None);
        tracing::info!(id = %id, "Closing issue");

        let issue_result = storage_ctx.storage.get_issue(id);
        let Some(issue) = preserve_blocked_cache_on_error(
            &mut storage_ctx.storage,
            cache_dirty,
            "close",
            issue_result,
        )?
        else {
            let skipped = SkippedIssue {
                id: id.clone(),
                reason: "issue not found".to_string(),
            };
            ordered_outcomes[outcome_index] = Some(CloseOutcome::Skipped(skipped.clone()));
            skipped_issues.push(skipped);
            continue;
        };

        if issue.status.is_terminal() {
            let skipped = SkippedIssue {
                id: id.clone(),
                reason: format!("already {}", issue.status.as_str()),
            };
            ordered_outcomes[outcome_index] = Some(CloseOutcome::Skipped(skipped.clone()));
            skipped_issues.push(skipped);
            continue;
        }

        if !args.force
            && let Some(&(total, closed)) = epic_counts.get(id)
            && closed < total
        {
            let label = if issue.issue_type == IssueType::Epic {
                "epic"
            } else {
                "parent issue"
            };
            let skipped = SkippedIssue {
                id: id.clone(),
                reason: format!(
                    "{label} has {}/{} open children (use --force to close anyway)",
                    total - closed,
                    total
                ),
            };
            ordered_outcomes[outcome_index] = Some(CloseOutcome::Skipped(skipped.clone()));
            skipped_issues.push(skipped);
            continue;
        }

        // Supplementary guard: catch dot-notation children (e.g. `epic.1`,
        // `epic.2`) that exist in the issues table without a formal
        // parent-child dep row. These slip past `epic_counts` because
        // get_epic_counts only scans the dependencies table. Missing-dep
        // children occur with legacy-bd migrations, bulk JSONL imports,
        // and hand-edited JSONL. Without this check, closing the parent
        // silently orphans the open children.
        let requested_dot_children = if args.force {
            Vec::new()
        } else {
            let open_dot_children = storage_ctx.storage.get_open_dot_notation_children(id)?;
            let (requested_children, unrequested_children): (Vec<String>, Vec<String>) =
                open_dot_children
                    .into_iter()
                    .partition(|child_id| requested_ids.contains(child_id));
            if !unrequested_children.is_empty() {
                let label = if issue.issue_type == IssueType::Epic {
                    "epic"
                } else {
                    "parent issue"
                };
                let preview: Vec<String> = unrequested_children.iter().take(5).cloned().collect();
                let suffix = if unrequested_children.len() > preview.len() {
                    format!(", +{} more", unrequested_children.len() - preview.len())
                } else {
                    String::new()
                };
                let skipped = SkippedIssue {
                    id: id.clone(),
                    reason: format!(
                        "{label} has {} open dot-notation child issue(s): {}{} (use --force to close anyway)",
                        unrequested_children.len(),
                        preview.join(", "),
                        suffix
                    ),
                };
                ordered_outcomes[outcome_index] = Some(CloseOutcome::Skipped(skipped.clone()));
                skipped_issues.push(skipped);
                continue;
            }
            requested_children
        };

        if args.force {
            open_issues.insert(id.clone(), issue);
            continue;
        }

        // Use *close* blockers, not generic blockers: a `parent-child` edge is
        // hierarchy, not a prerequisite from the parent to the child, so a
        // finished child must be closable even while its parent epic is itself
        // blocked or open (#355). `get_close_blockers` strips the propagated
        // `:parent-blocked` markers while retaining real prerequisite edges on
        // the child and the `:child-open` close-ordering rollup.
        let close_blockers_result = storage_ctx.storage.get_close_blockers(id);
        let mut blocker_ids = preserve_blocked_cache_on_error(
            &mut storage_ctx.storage,
            cache_dirty,
            "close",
            close_blockers_result,
        )?;
        blocker_ids.extend(requested_dot_children);
        blocker_ids.sort();
        blocker_ids.dedup();
        let (internal_blockers, external_blockers): (Vec<String>, Vec<String>) = blocker_ids
            .into_iter()
            .partition(|blocker_id| requested_ids.contains(blocker_id));
        internal_blockers_by_id.insert(id.clone(), internal_blockers);
        external_blockers_by_id.insert(id.clone(), external_blockers);
        open_issues.insert(id.clone(), issue);
    }

    let active_issue_ids: HashSet<String> = open_issues.keys().cloned().collect();
    let batch_closable_ids = if args.force {
        active_issue_ids
    } else {
        compute_batch_closable_ids(
            &active_issue_ids,
            &internal_blockers_by_id,
            &external_blockers_by_id,
        )
    };

    let mut policy_evaluations_by_id: HashMap<String, EvaluatedGates> = HashMap::new();
    if policy_active {
        for id in &resolved_ids {
            let Some(issue) = open_issues.get(id) else {
                continue;
            };
            if !args.force && !batch_closable_ids.contains(id) {
                continue;
            }

            let evaluated_gates = evaluate_close_policy(
                &policy_doc.close_policy,
                &policy_doc.workflow,
                &storage_ctx.storage,
                id,
                issue,
                args,
                &actor,
            )?;
            if !evaluated_gates.violations.is_empty() && !args.bypass_policy {
                let summary = summarize_violations(&evaluated_gates.violations);
                return Err(BeadsError::PolicyViolation {
                    issue_id: id.clone(),
                    summary,
                    violations: evaluated_gates.violations,
                });
            }
            policy_evaluations_by_id.insert(id.clone(), evaluated_gates);
        }
    }

    for (outcome_index, id) in resolved_ids.iter().enumerate() {
        let Some(issue) = open_issues.get(id) else {
            continue;
        };

        if !args.force && !batch_closable_ids.contains(id) {
            let mut blocker_ids = external_blockers_by_id.get(id).cloned().unwrap_or_default();
            if let Some(internal_blockers) = internal_blockers_by_id.get(id) {
                blocker_ids.extend(
                    internal_blockers
                        .iter()
                        .filter(|blocker_id| !batch_closable_ids.contains(*blocker_id))
                        .cloned(),
                );
            }
            blocker_ids.sort();
            blocker_ids.dedup();
            tracing::debug!(blocked_by = ?blocker_ids, "Issue remains blocked in batch close");
            // Name the open blockers AND the way out. Without the explicit
            // remediation this skip used to surface as a bare "all N issue(s)
            // skipped" error whose hint claimed the issue was "already closed
            // or not found" — flatly wrong for a dependency block (#380).
            let reason = if blocker_ids.is_empty() {
                "blocked by dependencies — close the open blocker(s) first, or use --force to close anyway".to_string()
            } else {
                format!(
                    "blocked by: {} — close the open blocker(s) first, or use --force to close anyway",
                    blocker_ids.join(", ")
                )
            };
            let skipped = SkippedIssue {
                id: id.clone(),
                reason,
            };
            ordered_outcomes[outcome_index] = Some(CloseOutcome::Skipped(skipped.clone()));
            skipped_issues.push(skipped);
            continue;
        }

        let gates_fired = if let Some(evaluated_gates) = policy_evaluations_by_id.get(id) {
            if !evaluated_gates.violations.is_empty() && args.bypass_policy {
                emit_bypass_warning(ctx, id, &evaluated_gates.violations);
            }
            evaluated_gates
                .violations
                .iter()
                .map(|v| v.gate.clone())
                .collect::<Vec<String>>()
        } else {
            Vec::new()
        };

        let now = Utc::now();
        let close_reason = args.reason.clone().unwrap_or_else(|| "done".to_string());
        let update = IssueUpdate {
            status: Some(Status::Closed),
            closed_at: Some(Some(now)),
            close_reason: Some(Some(close_reason.clone())),
            closed_by_session: args.session.clone().map(Some),
            transition_comment: args.transition_comment.clone(),
            workflow_policy_bypass_reason: if args.bypass_policy {
                args.bypass_reason.clone()
            } else {
                None
            },
            skip_cache_rebuild: true,
            ..Default::default()
        };

        atomic_updates.push((id.clone(), update));
        planned_closes.push((
            outcome_index,
            id.clone(),
            issue.clone(),
            gates_fired,
            now,
            close_reason,
        ));
    }

    if !atomic_updates.is_empty() {
        storage_ctx
            .storage
            .set_pending_event_attribution(EventAttribution::new(
                attribution.agent_name.as_deref(),
                attribution.harness.as_deref(),
                attribution.model.as_deref(),
                super::session_attribution_from_env().as_deref(),
            ));
        let update_result = update_issues_atomically_with_recovery(
            &mut storage_ctx,
            true,
            "close",
            &atomic_updates,
            &actor,
        );
        preserve_blocked_cache_on_error(&mut storage_ctx.storage, false, "close", update_result)?;
        capacity_warnings = storage_ctx.storage.take_capacity_warnings();
        cache_dirty = true;
    }

    for (outcome_index, id, issue, gates_fired, now, close_reason) in planned_closes {
        tracing::info!(id = %id, reason = ?args.reason, "Issue closed");

        if policy_active {
            // Best-effort persistence of attribution + bypass auditing. Failure
            // to record metadata never undoes a successful close: the close
            // already committed, and burning down a successful close because of
            // an optional auxiliary table would be a regression for users whose
            // schema could not migrate. We log and move on.
            let bypass_reason = if args.bypass_policy {
                args.bypass_reason.as_deref()
            } else {
                None
            };
            let metadata_result = storage_ctx.storage.record_close_metadata(
                &id,
                &attribution,
                args.bypass_policy && !gates_fired.is_empty(),
                bypass_reason,
                &gates_fired,
            );
            if let Err(error) = metadata_result {
                tracing::warn!(
                    issue_id = %id,
                    error = %error,
                    "failed to record closure-time policy metadata; close already committed"
                );
            }
        }

        let closed = ClosedIssue {
            id: id.clone(),
            title: issue.title.clone(),
            status: "closed".to_string(),
            closed_at: now.to_rfc3339(),
            close_reason: Some(close_reason),
        };
        ordered_outcomes[outcome_index] = Some(CloseOutcome::Closed(closed.clone()));
        closed_issues.push(closed);
    }

    let ordered_outcomes = ordered_outcomes
        .into_iter()
        .map(|outcome| {
            outcome.ok_or_else(|| BeadsError::internal("close batch outcome was not populated"))
        })
        .collect::<Result<Vec<_>>>()?;

    if cache_dirty {
        tracing::info!(
            "Rebuilding blocked cache after closing {} issues",
            closed_issues.len()
        );
        finalize_batched_blocked_cache_refresh(&mut storage_ctx.storage, cache_dirty, "close")?;
    }

    let unblocked_issues: Vec<UnblockedIssue> = if args.suggest_next && !closed_issues.is_empty() {
        let blocked_after_result = storage_ctx.storage.get_blocked_issues();
        let blocked_after = match preserve_blocked_cache_on_error(
            &mut storage_ctx.storage,
            cache_dirty,
            "close",
            blocked_after_result,
        ) {
            Ok(blocked_after) => Some(
                blocked_after
                    .into_iter()
                    .map(|(issue, _)| issue.id)
                    .collect::<Vec<_>>(),
            ),
            Err(error) => {
                tracing::warn!(
                    error = %error,
                    "Skipping suggest-next calculation after committed close because blocked-cache lookup failed"
                );
                None
            }
        };

        let Some(blocked_after) = blocked_after else {
            storage_ctx.flush_no_db_if_dirty()?;
            return Ok(CloseExecution {
                closed: closed_issues,
                skipped: skipped_issues,
                unblocked: Vec::new(),
                ordered_outcomes,
                capacity_warnings,
            });
        };

        let newly_unblocked: Vec<String> = blocked_before
            .into_iter()
            .filter(|id| !blocked_after.contains(id))
            .collect();

        tracing::debug!(unblocked = ?newly_unblocked, "Issues unblocked by close");

        let mut unblocked = Vec::new();
        for uid in newly_unblocked {
            let issue_result = storage_ctx.storage.get_issue(&uid);
            match preserve_blocked_cache_on_error(
                &mut storage_ctx.storage,
                cache_dirty,
                "close",
                issue_result,
            ) {
                Ok(Some(issue)) if issue.status.is_active() => {
                    unblocked.push(UnblockedIssue {
                        id: issue.id,
                        title: issue.title,
                        priority: issue.priority.0,
                    });
                }
                Ok(_) => {}
                Err(error) => {
                    tracing::warn!(
                        issue_id = %uid,
                        error = %error,
                        "Skipping suggest-next candidate after committed close because issue lookup failed"
                    );
                }
            }
        }
        unblocked
    } else {
        Vec::new()
    };

    storage_ctx.flush_no_db_if_dirty()?;
    if auto_flush_external && let Err(error) = storage_ctx.auto_flush_if_enabled() {
        report_auto_flush_failure(
            ctx,
            &storage_ctx.paths.beads_dir,
            &storage_ctx.paths.jsonl_path,
            &error,
        );
    }

    Ok(CloseExecution {
        closed: closed_issues,
        skipped: skipped_issues,
        unblocked: unblocked_issues,
        ordered_outcomes,
        capacity_warnings,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::commands;
    use crate::config::CliOverrides;
    use crate::error::StructuredError;
    use crate::model::{DependencyType, Issue, IssueType, Priority, Status};
    use crate::output::OutputContext;
    use crate::storage::SqliteStorage;
    use chrono::Utc;
    use std::env;
    use std::path::PathBuf;

    use tempfile::TempDir;

    struct DirGuard {
        previous: PathBuf,
    }

    impl DirGuard {
        fn new(target: &std::path::Path) -> Self {
            let previous = env::current_dir().unwrap_or_else(|_| PathBuf::from("/tmp"));
            env::set_current_dir(target).expect("set current dir");
            Self { previous }
        }
    }

    impl Drop for DirGuard {
        fn drop(&mut self) {
            let _ = env::set_current_dir(&self.previous);
        }
    }

    fn make_issue(id: &str, title: &str) -> Issue {
        let now = Utc::now();
        Issue {
            id: id.to_string(),
            title: title.to_string(),
            status: Status::Open,
            priority: Priority::MEDIUM,
            issue_type: IssueType::Task,
            created_at: now,
            updated_at: now,
            ..Issue::default()
        }
    }

    fn make_issue_with_status(id: &str, title: &str, status: Status) -> Issue {
        Issue {
            status,
            ..make_issue(id, title)
        }
    }

    // =========================================================================
    // CloseArgs tests
    // =========================================================================

    #[test]
    fn test_close_args_default() {
        let args = CloseArgs::default();
        assert!(args.ids.is_empty());
        assert!(args.reason.is_none());
        assert!(args.transition_comment.is_none());
        assert!(!args.force);
        assert!(args.session.is_none());
        assert!(!args.suggest_next);
    }

    #[test]
    fn test_close_args_with_all_fields() {
        let args = CloseArgs {
            ids: vec!["bd-abc".to_string(), "bd-xyz".to_string()],
            reason: Some("Fixed in PR #123".to_string()),
            transition_comment: Some("Verified in staging".to_string()),
            force: true,
            session: Some("session-456".to_string()),
            suggest_next: true,
            agent_name: Some("agent-1".to_string()),
            harness: Some("codex-cli".to_string()),
            model: Some("gpt-5".to_string()),
            bypass_policy: true,
            bypass_reason: Some("Manual override approved".to_string()),
        };
        assert_eq!(args.ids.len(), 2);
        assert_eq!(args.ids[0], "bd-abc");
        assert_eq!(args.reason.as_deref(), Some("Fixed in PR #123"));
        assert_eq!(
            args.transition_comment.as_deref(),
            Some("Verified in staging")
        );
        assert!(args.force);
        assert_eq!(args.session.as_deref(), Some("session-456"));
        assert!(args.suggest_next);
        assert_eq!(args.agent_name.as_deref(), Some("agent-1"));
        assert_eq!(args.harness.as_deref(), Some("codex-cli"));
        assert_eq!(args.model.as_deref(), Some("gpt-5"));
        assert!(args.bypass_policy);
        assert_eq!(
            args.bypass_reason.as_deref(),
            Some("Manual override approved")
        );
    }

    // =========================================================================
    // CloseResult serialization tests
    // =========================================================================

    #[test]
    fn test_close_result_serialization_empty_skipped_omitted() {
        let result = CloseResult {
            closed: vec![ClosedIssue {
                id: "bd-123".to_string(),
                title: "Test issue".to_string(),
                status: "closed".to_string(),
                closed_at: "2026-01-01T00:00:00Z".to_string(),
                close_reason: None,
            }],
            skipped: vec![],
            warnings: vec![],
        };
        let json = serde_json::to_string(&result).unwrap();
        // Empty skipped should be omitted due to skip_serializing_if
        assert!(!json.contains("\"skipped\""));
        assert!(json.contains("\"closed\""));
    }

    #[test]
    fn test_close_result_serialization_with_skipped() {
        let result = CloseResult {
            closed: vec![],
            skipped: vec![SkippedIssue {
                id: "bd-456".to_string(),
                reason: "already closed".to_string(),
            }],
            warnings: vec![],
        };
        let json = serde_json::to_string(&result).unwrap();
        assert!(json.contains("\"skipped\""));
        assert!(json.contains("\"reason\":\"already closed\""));
    }

    #[test]
    fn test_close_result_roundtrip() {
        let result = CloseResult {
            closed: vec![
                ClosedIssue {
                    id: "bd-a".to_string(),
                    title: "First".to_string(),
                    status: "closed".to_string(),
                    closed_at: "2026-01-01T00:00:00Z".to_string(),
                    close_reason: Some("Done".to_string()),
                },
                ClosedIssue {
                    id: "bd-b".to_string(),
                    title: "Second".to_string(),
                    status: "closed".to_string(),
                    closed_at: "2026-01-02T00:00:00Z".to_string(),
                    close_reason: None,
                },
            ],
            skipped: vec![SkippedIssue {
                id: "bd-c".to_string(),
                reason: "blocked by: bd-d".to_string(),
            }],
            warnings: vec![],
        };
        let json = serde_json::to_string(&result).unwrap();
        let parsed: CloseResult = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.closed.len(), 2);
        assert_eq!(parsed.skipped.len(), 1);
        assert_eq!(parsed.closed[0].id, "bd-a");
        assert_eq!(parsed.closed[0].close_reason.as_deref(), Some("Done"));
        assert!(parsed.closed[1].close_reason.is_none());
    }

    // =========================================================================
    // CloseWithSuggestResult serialization tests
    // =========================================================================

    #[test]
    fn test_close_with_suggest_result_serialization() {
        let result = CloseWithSuggestResult {
            closed: vec![ClosedIssue {
                id: "bd-parent".to_string(),
                title: "Parent task".to_string(),
                status: "closed".to_string(),
                closed_at: "2026-01-15T10:00:00Z".to_string(),
                close_reason: Some("Completed".to_string()),
            }],
            skipped: vec![],
            unblocked: vec![
                UnblockedIssue {
                    id: "bd-child1".to_string(),
                    title: "Child task 1".to_string(),
                    priority: 1,
                },
                UnblockedIssue {
                    id: "bd-child2".to_string(),
                    title: "Child task 2".to_string(),
                    priority: 2,
                },
            ],
            warnings: vec![],
        };
        let json = serde_json::to_string(&result).unwrap();
        assert!(json.contains("\"unblocked\""));
        assert!(json.contains("\"bd-child1\""));
        assert!(json.contains("\"bd-child2\""));
        assert!(json.contains("\"priority\":1"));
        assert!(json.contains("\"priority\":2"));
        // Empty skipped should be omitted
        assert!(!json.contains("\"skipped\""));
    }

    #[test]
    fn test_close_with_suggest_result_empty_unblocked() {
        let result = CloseWithSuggestResult {
            closed: vec![],
            skipped: vec![SkippedIssue {
                id: "bd-x".to_string(),
                reason: "not found".to_string(),
            }],
            unblocked: vec![],
            warnings: vec![],
        };
        let json = serde_json::to_string(&result).unwrap();
        // unblocked is not marked skip_serializing_if, so it should appear as empty array
        assert!(json.contains("\"unblocked\":[]"));
        assert!(json.contains("\"skipped\""));
    }

    // =========================================================================
    // ClosedIssue serialization tests
    // =========================================================================

    #[test]
    fn test_closed_issue_serialization_with_reason() {
        let issue = ClosedIssue {
            id: "bd-test".to_string(),
            title: "Test issue".to_string(),
            status: "closed".to_string(),
            closed_at: "2026-01-17T08:00:00Z".to_string(),
            close_reason: Some("Fixed in commit abc123".to_string()),
        };
        let json = serde_json::to_string(&issue).unwrap();
        assert!(json.contains("\"close_reason\":\"Fixed in commit abc123\""));
    }

    #[test]
    fn test_closed_issue_serialization_without_reason() {
        let issue = ClosedIssue {
            id: "bd-test".to_string(),
            title: "Test issue".to_string(),
            status: "closed".to_string(),
            closed_at: "2026-01-17T08:00:00Z".to_string(),
            close_reason: None,
        };
        let json = serde_json::to_string(&issue).unwrap();
        // close_reason should be omitted due to skip_serializing_if
        assert!(!json.contains("close_reason"));
    }

    #[test]
    fn test_closed_issue_all_fields() {
        let issue = ClosedIssue {
            id: "beads_rust-xyz".to_string(),
            title: "Multi-word title with special chars: <>&".to_string(),
            status: "closed".to_string(),
            closed_at: "2026-12-31T23:59:59Z".to_string(),
            close_reason: Some("End of year cleanup".to_string()),
        };
        let json = serde_json::to_string(&issue).unwrap();
        let parsed: ClosedIssue = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.id, "beads_rust-xyz");
        assert!(parsed.title.contains("<>&"));
        assert_eq!(parsed.status, "closed");
        assert!(parsed.closed_at.contains("2026-12-31"));
    }

    #[test]
    fn close_human_messages_sanitize_terminal_controls() {
        let closed = ClosedIssue {
            id: "bd-close\x1b[2J".to_string(),
            title: "bad\rtitle\x08".to_string(),
            status: "closed".to_string(),
            closed_at: "2026-12-31T23:59:59Z".to_string(),
            close_reason: Some("done\nnext\x07\u{9b}".to_string()),
        };
        let skipped = SkippedIssue {
            id: "bd-skip\x1b[2J".to_string(),
            reason: "blocked\rby\nterminal\x07".to_string(),
        };
        let unblocked = UnblockedIssue {
            id: "bd-unblock\x1b[2J".to_string(),
            title: "ready\nlater\x08".to_string(),
            priority: 1,
        };

        let close_message = close_human_message(&closed);
        let skipped_message = skipped_human_message(&skipped);
        let unblocked_line = unblocked_human_line(&unblocked);

        for text in [&close_message, &skipped_message, &unblocked_line] {
            assert!(!text.chars().any(char::is_control));
            assert!(text.contains("\\u{1b}[2J"));
        }
        assert!(close_message.contains("\\r"));
        assert!(close_message.contains("\\u{8}"));
        assert!(close_message.contains("\\n"));
        assert!(close_message.contains("\\u{7}"));
        assert!(close_message.contains("\\u{9b}"));
        assert!(skipped_message.contains("\\r"));
        assert!(skipped_message.contains("\\n"));
        assert!(skipped_message.contains("\\u{7}"));
        assert!(unblocked_line.contains("\\n"));
        assert!(unblocked_line.contains("\\u{8}"));
    }

    #[test]
    fn reorder_routed_items_sanitizes_missing_input_error() {
        let requested = vec!["bd-close\x1b[2J\nbad".to_string(), "bd-ok".to_string()];
        let routed_items = vec![(vec!["bd-ok".to_string()], vec!["ok"])];

        let err =
            reorder_routed_items_by_requested_inputs(&requested, routed_items, "close routing")
                .unwrap_err();

        assert!(
            matches!(err, BeadsError::Internal { .. }),
            "unexpected error: {err:?}"
        );
        if let BeadsError::Internal { message } = err {
            assert!(!message.chars().any(char::is_control));
            assert!(message.contains("\\u{1b}[2J"));
            assert!(message.contains("\\n"));
        }
    }

    #[test]
    fn reorder_routed_items_sanitizes_unexpected_input_error() {
        let requested = vec!["bd-ok".to_string()];
        let routed_items = vec![(vec!["bd-close\x1b[2J\nbad".to_string()], vec!["bad"])];

        let err =
            reorder_routed_items_by_requested_inputs(&requested, routed_items, "close routing")
                .unwrap_err();

        assert!(
            matches!(err, BeadsError::Internal { .. }),
            "unexpected error: {err:?}"
        );
        if let BeadsError::Internal { message } = err {
            assert!(!message.chars().any(char::is_control));
            assert!(message.contains("\\u{1b}[2J"));
            assert!(message.contains("\\n"));
        }
    }

    // =========================================================================
    // SkippedIssue serialization tests
    // =========================================================================

    #[test]
    fn test_skipped_issue_serialization() {
        let skipped = SkippedIssue {
            id: "bd-skip".to_string(),
            reason: "already closed".to_string(),
        };
        let json = serde_json::to_string(&skipped).unwrap();
        assert!(json.contains("\"id\":\"bd-skip\""));
        assert!(json.contains("\"reason\":\"already closed\""));
    }

    #[test]
    fn test_skipped_issue_blocked_reason() {
        let skipped = SkippedIssue {
            id: "bd-blocked".to_string(),
            reason: "blocked by: bd-dep1, bd-dep2".to_string(),
        };
        let json = serde_json::to_string(&skipped).unwrap();
        let parsed: SkippedIssue = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.id, "bd-blocked");
        assert!(parsed.reason.contains("bd-dep1"));
        assert!(parsed.reason.contains("bd-dep2"));
    }

    // =========================================================================
    // UnblockedIssue serialization tests
    // =========================================================================

    #[test]
    fn test_unblocked_issue_serialization() {
        let unblocked = UnblockedIssue {
            id: "bd-next".to_string(),
            title: "Next task".to_string(),
            priority: 1,
        };
        let json = serde_json::to_string(&unblocked).unwrap();
        assert!(json.contains("\"id\":\"bd-next\""));
        assert!(json.contains("\"title\":\"Next task\""));
        assert!(json.contains("\"priority\":1"));
    }

    #[test]
    fn test_unblocked_issue_priority_boundaries() {
        for priority in [0, 1, 2, 3, 4] {
            let unblocked = UnblockedIssue {
                id: format!("bd-p{priority}"),
                title: format!("Priority {priority} task"),
                priority,
            };
            let json = serde_json::to_string(&unblocked).unwrap();
            let parsed: UnblockedIssue = serde_json::from_str(&json).unwrap();
            assert_eq!(parsed.priority, priority);
        }
    }

    // =========================================================================
    // Edge case tests
    // =========================================================================

    #[test]
    fn test_close_result_multiple_closed_multiple_skipped() {
        let result = CloseResult {
            closed: vec![
                ClosedIssue {
                    id: "bd-1".to_string(),
                    title: "Task 1".to_string(),
                    status: "closed".to_string(),
                    closed_at: "2026-01-01T00:00:00Z".to_string(),
                    close_reason: None,
                },
                ClosedIssue {
                    id: "bd-2".to_string(),
                    title: "Task 2".to_string(),
                    status: "closed".to_string(),
                    closed_at: "2026-01-01T00:00:01Z".to_string(),
                    close_reason: Some("Batch close".to_string()),
                },
            ],
            skipped: vec![
                SkippedIssue {
                    id: "bd-3".to_string(),
                    reason: "issue not found".to_string(),
                },
                SkippedIssue {
                    id: "bd-4".to_string(),
                    reason: "already tombstone".to_string(),
                },
            ],
            warnings: vec![],
        };
        let json = serde_json::to_string_pretty(&result).unwrap();
        let parsed: CloseResult = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.closed.len(), 2);
        assert_eq!(parsed.skipped.len(), 2);
    }

    #[test]
    fn test_render_close_json_preserves_bare_array_for_pure_success() {
        let json = build_close_json_payload(
            &CloseArgs::default(),
            vec![ClosedIssue {
                id: "bd-1".to_string(),
                title: "Task 1".to_string(),
                status: "closed".to_string(),
                closed_at: "2026-01-01T00:00:00Z".to_string(),
                close_reason: Some("done".to_string()),
            }],
            vec![],
            vec![],
            vec![],
        )
        .unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert!(parsed.is_array());
    }

    #[test]
    fn test_close_result_shape_with_skipped_is_wrapped() {
        let json = build_close_json_payload(
            &CloseArgs::default(),
            vec![ClosedIssue {
                id: "bd-1".to_string(),
                title: "Task 1".to_string(),
                status: "closed".to_string(),
                closed_at: "2026-01-01T00:00:00Z".to_string(),
                close_reason: Some("done".to_string()),
            }],
            vec![SkippedIssue {
                id: "bd-2".to_string(),
                reason: "blocked by: bd-3".to_string(),
            }],
            vec![],
            vec![],
        )
        .unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert!(parsed.is_object());
        assert_eq!(parsed["closed"][0]["id"], "bd-1");
        assert_eq!(parsed["skipped"][0]["id"], "bd-2");
    }

    #[test]
    fn test_close_args_clone() {
        let args = CloseArgs {
            ids: vec!["bd-clone".to_string()],
            reason: Some("Clone test".to_string()),
            transition_comment: Some("Fresh transition evidence".to_string()),
            force: true,
            session: Some("sess".to_string()),
            suggest_next: true,
            agent_name: Some("agent-clone".to_string()),
            harness: Some("harness-clone".to_string()),
            model: Some("model-clone".to_string()),
            bypass_policy: true,
            bypass_reason: Some("Clone bypass reason".to_string()),
        };
        let cloned = args.clone();
        assert_eq!(cloned.ids, args.ids);
        assert_eq!(cloned.reason, args.reason);
        assert_eq!(cloned.transition_comment, args.transition_comment);
        assert_eq!(cloned.force, args.force);
        assert_eq!(cloned.session, args.session);
        assert_eq!(cloned.suggest_next, args.suggest_next);
        assert_eq!(cloned.agent_name, args.agent_name);
        assert_eq!(cloned.harness, args.harness);
        assert_eq!(cloned.model, args.model);
        assert_eq!(cloned.bypass_policy, args.bypass_policy);
        assert_eq!(cloned.bypass_reason, args.bypass_reason);
        assert_eq!(cloned.suggest_next, args.suggest_next);
    }

    #[test]
    fn test_close_args_debug_impl() {
        let args = CloseArgs::default();
        let debug_str = format!("{args:?}");
        assert!(debug_str.contains("CloseArgs"));
        assert!(debug_str.contains("ids"));
        assert!(debug_str.contains("reason"));
    }

    #[test]
    fn execute_with_args_closes_requested_blocker_chain_in_one_batch() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        let mut storage = SqliteStorage::open(&db_path).expect("storage");
        storage
            .create_issue(&make_issue("bd-blocker", "Batch blocker"), "tester")
            .expect("create blocker");
        storage
            .create_issue(&make_issue("bd-blocked", "Batch blocked"), "tester")
            .expect("create blocked");
        storage
            .add_dependency(
                "bd-blocked",
                "bd-blocker",
                DependencyType::Blocks.as_str(),
                "tester",
            )
            .expect("add dependency");
        storage.rebuild_blocked_cache(true).expect("rebuild cache");
        drop(storage);

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-blocked".to_string(), "bd-blocker".to_string()],
            ..CloseArgs::default()
        };
        execute_with_args(&args, false, &CliOverrides::default(), &ctx).expect("close batch");

        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        let blocker = storage
            .get_issue("bd-blocker")
            .expect("get blocker")
            .expect("blocker exists");
        let blocked_issue = storage
            .get_issue("bd-blocked")
            .expect("get blocked")
            .expect("blocked exists");

        assert_eq!(blocker.status, Status::Closed);
        assert_eq!(blocked_issue.status, Status::Closed);
    }

    #[test]
    fn execute_route_preserves_request_order_for_mixed_close_outcomes() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        let mut storage = SqliteStorage::open(&db_path).expect("storage");
        storage
            .create_issue(&make_issue("bd-close-first", "First"), "tester")
            .expect("create first");
        let mut skipped = make_issue_with_status("bd-close-skip", "Skip", Status::Closed);
        skipped.closed_at = Some(Utc::now());
        storage
            .create_issue(&skipped, "tester")
            .expect("create skipped");
        storage
            .create_issue(&make_issue("bd-close-last", "Last"), "tester")
            .expect("create last");
        drop(storage);

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec![
                "bd-close-first".to_string(),
                "bd-close-skip".to_string(),
                "bd-close-last".to_string(),
            ],
            ..CloseArgs::default()
        };
        let execution = execute_route(&args, &CliOverrides::default(), &ctx, &beads_dir, false)
            .expect("mixed close batch");

        assert!(matches!(
            &execution.ordered_outcomes[0],
            CloseOutcome::Closed(issue) if issue.id == "bd-close-first"
        ));
        assert!(matches!(
            &execution.ordered_outcomes[1],
            CloseOutcome::Skipped(issue) if issue.id == "bd-close-skip"
        ));
        assert!(matches!(
            &execution.ordered_outcomes[2],
            CloseOutcome::Closed(issue) if issue.id == "bd-close-last"
        ));
    }

    #[test]
    fn execute_with_args_closes_requested_dot_notation_child_with_parent() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        let mut storage = SqliteStorage::open(&db_path).expect("storage");
        storage
            .create_issue(&make_issue("bd-parent", "Legacy parent"), "tester")
            .expect("create parent");
        storage
            .create_issue(&make_issue("bd-parent.1", "Legacy child"), "tester")
            .expect("create dot child");
        drop(storage);

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-parent".to_string(), "bd-parent.1".to_string()],
            ..CloseArgs::default()
        };
        execute_with_args(&args, false, &CliOverrides::default(), &ctx)
            .expect("close parent and dot child in one batch");

        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        let parent = storage
            .get_issue("bd-parent")
            .expect("get parent")
            .expect("parent exists");
        let child = storage
            .get_issue("bd-parent.1")
            .expect("get child")
            .expect("child exists");

        assert_eq!(parent.status, Status::Closed);
        assert_eq!(child.status, Status::Closed);
    }

    #[test]
    fn execute_with_args_keeps_parent_blocked_by_unrequested_dot_notation_child() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        let mut storage = SqliteStorage::open(&db_path).expect("storage");
        storage
            .create_issue(&make_issue("bd-parent", "Legacy parent"), "tester")
            .expect("create parent");
        storage
            .create_issue(&make_issue("bd-parent.1", "Legacy child"), "tester")
            .expect("create dot child");
        drop(storage);

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-parent".to_string()],
            ..CloseArgs::default()
        };
        let err = execute_with_args(&args, true, &CliOverrides::default(), &ctx)
            .expect_err("parent-only close should remain blocked by dot child");
        assert!(matches!(err, BeadsError::NothingToDo { .. }));

        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        let parent = storage
            .get_issue("bd-parent")
            .expect("get parent")
            .expect("parent exists");
        let child = storage
            .get_issue("bd-parent.1")
            .expect("get child")
            .expect("child exists");

        assert_eq!(parent.status, Status::Open);
        assert_eq!(child.status, Status::Open);
    }

    #[test]
    fn execute_with_args_closes_child_even_when_parent_epic_is_blocked() {
        // Regression for #355: a finished child must be closable even while its
        // parent epic is itself blocked by an unrelated prerequisite. The
        // `parent-child` edge is hierarchy, not a prerequisite from parent to
        // child, so the propagated `:parent-blocked` marker must not gate the
        // child's own closure.
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        let mut storage = SqliteStorage::open(&db_path).expect("storage");
        // A deferred prerequisite that blocks the parent epic.
        storage
            .create_issue(
                &make_issue_with_status("bd-blocker", "Deferred blocker", Status::Deferred),
                "tester",
            )
            .expect("create blocker");
        // The parent epic, blocked by the deferred prerequisite.
        let mut parent = make_issue("bd-parent", "Parent epic");
        parent.issue_type = IssueType::Epic;
        storage
            .create_issue(&parent, "tester")
            .expect("create parent");
        storage
            .add_dependency(
                "bd-parent",
                "bd-blocker",
                DependencyType::Blocks.as_str(),
                "tester",
            )
            .expect("add blocks dependency");
        // A child of the parent epic, reviewed-complete and ready to close.
        storage
            .create_issue(&make_issue("bd-child", "Child task"), "tester")
            .expect("create child");
        storage
            .add_dependency(
                "bd-child",
                "bd-parent",
                DependencyType::ParentChild.as_str(),
                "tester",
            )
            .expect("add parent-child dependency");
        storage.rebuild_blocked_cache(true).expect("rebuild cache");
        // The child IS propagated `parent-blocked` in the readiness graph...
        assert!(
            storage
                .get_blockers("bd-child")
                .expect("get blockers")
                .contains(&"bd-parent".to_string()),
            "child should inherit a parent-blocked readiness marker"
        );
        // ...but it must NOT be a *close* blocker.
        assert!(
            storage
                .get_close_blockers("bd-child")
                .expect("get close blockers")
                .is_empty(),
            "a blocked parent must not gate the child's closure"
        );
        drop(storage);

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-child".to_string()],
            reason: Some("Child done".to_string()),
            ..CloseArgs::default()
        };
        execute_with_args(&args, false, &CliOverrides::default(), &ctx)
            .expect("child close should succeed despite blocked parent");

        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        let child = storage
            .get_issue("bd-child")
            .expect("get child")
            .expect("child exists");
        let parent = storage
            .get_issue("bd-parent")
            .expect("get parent")
            .expect("parent exists");
        assert_eq!(child.status, Status::Closed, "child should be closed");
        assert_eq!(
            parent.status,
            Status::Open,
            "parent epic should remain open/blocked"
        );
    }

    #[test]
    fn execute_with_args_returns_nothing_to_do_when_all_requested_issues_are_skipped() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        let mut storage = SqliteStorage::open(&db_path).expect("storage");
        let mut issue = make_issue("bd-closed", "Already closed");
        issue.status = Status::Closed;
        issue.closed_at = Some(Utc::now());
        storage
            .create_issue(&issue, "tester")
            .expect("create closed issue");

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-closed".to_string()],
            ..CloseArgs::default()
        };

        let err = execute_with_args(&args, true, &CliOverrides::default(), &ctx)
            .expect_err("all-skipped close should fail");
        assert!(matches!(err, BeadsError::NothingToDo { .. }));
    }

    #[test]
    fn execute_with_args_reports_partial_batch_as_error_not_success() {
        // A batch that closes one issue and refuses another used to return
        // `Ok(())`, because the terminal error was gated on `closed_count == 0`.
        // The refusal was then visible ONLY as a warning on stderr, while stdout
        // carried an unqualified "✓ Closed" and `$?` was 0 — and
        // `docs/agent/ERRORS.md` instructs callers to parse stdout precisely
        // when the exit code is 0. Any caller branching on exit status read the
        // untouched issue as closed.
        //
        // This is the direction that fires: under the old predicate this test
        // fails at `expect_err`. The all-succeed case is covered by
        // `execute_with_args_closes_requested_blocker_chain_in_one_batch`, which
        // still expects `Ok(())` — so the new predicate is proven in both
        // directions.
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        let mut storage = SqliteStorage::open(&db_path).expect("storage");
        storage
            .create_issue(&make_issue("bd-blocker", "Unrequested blocker"), "tester")
            .expect("create blocker");
        storage
            .create_issue(
                &make_issue("bd-blocked", "Blocked by unrequested"),
                "tester",
            )
            .expect("create blocked");
        storage
            .create_issue(&make_issue("bd-free", "Independently closeable"), "tester")
            .expect("create free");
        storage
            .add_dependency(
                "bd-blocked",
                "bd-blocker",
                DependencyType::Blocks.as_str(),
                "tester",
            )
            .expect("add dependency");
        storage.rebuild_blocked_cache(true).expect("rebuild cache");
        drop(storage);

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-blocked".to_string(), "bd-free".to_string()],
            ..CloseArgs::default()
        };

        let err = execute_with_args(&args, false, &CliOverrides::default(), &ctx)
            .expect_err("a partially applied batch must not report success");
        assert!(
            matches!(err, BeadsError::CloseIncomplete { .. }),
            "a partial batch must surface as CloseIncomplete, got: {err:?}"
        );
        // Destructure without a diverging arm: the fallback carries values that
        // fail every assertion below, so a wrong variant is still loud.
        let (closed, skipped, summary) = match &err {
            BeadsError::CloseIncomplete {
                closed,
                skipped,
                summary,
            } => (*closed, *skipped, summary.clone()),
            other => (0, 0, format!("wrong variant: {other:?}")),
        };
        assert_eq!(closed, 1, "exactly one issue should have closed");
        assert_eq!(skipped, 1, "exactly one issue should have been skipped");
        assert!(
            summary.contains("bd-blocked") && summary.contains("blocked by"),
            "summary must name the refused issue and the real reason, got: {summary}"
        );

        // The exit status now agrees with the record, which is the whole point.
        assert_ne!(
            StructuredError::from_error(&err).code.exit_code(),
            0,
            "a partial close must not exit 0"
        );

        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        assert_eq!(
            storage
                .get_issue("bd-free")
                .expect("get free")
                .expect("free exists")
                .status,
            Status::Closed,
            "the closeable issue should still have been closed"
        );
        assert_eq!(
            storage
                .get_issue("bd-blocked")
                .expect("get blocked")
                .expect("blocked exists")
                .status,
            Status::Open,
            "the blocked issue must remain open — the error is what reports that"
        );
    }

    #[test]
    fn execute_with_args_records_clean_policy_close_metadata() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        let mut storage = SqliteStorage::open(&db_path).expect("storage");
        storage
            .create_issue(&make_issue("bd-policy", "Policy governed"), "tester")
            .expect("create policy issue");
        drop(storage);

        std::fs::write(
            beads_dir.join(close_policy::POLICY_FILE_NAME),
            "close_policy:\n  require_close_reason:\n    enabled: true\n    min_length: 4\n",
        )
        .expect("write policy");

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-policy".to_string()],
            reason: Some("done cleanly".to_string()),
            ..CloseArgs::default()
        };
        execute_with_args(&args, false, &CliOverrides::default(), &ctx).expect("close issue");

        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        let metadata = storage
            .get_close_metadata("bd-policy")
            .expect("read close metadata")
            .expect("active policy should record metadata even when no gate fires");
        assert!(!metadata.bypassed_policy);
        assert!(metadata.bypass_reason.is_none());
        assert!(metadata.policy_gates_fired.is_empty());
        assert!(metadata.closed_by_agent_name.is_none());
        assert!(metadata.closed_by_harness.is_none());
        assert!(metadata.closed_by_model.is_none());
    }

    #[test]
    fn execute_with_args_policy_violation_in_batch_does_not_close_earlier_issue() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        let mut storage = SqliteStorage::open(&db_path).expect("storage");
        storage
            .create_issue(&make_issue("bd-clean", "Clean policy issue"), "tester")
            .expect("create clean issue");
        let mut failing_issue = make_issue("bd-policy-fail", "Policy failing issue");
        failing_issue.acceptance_criteria = Some("- [ ] Finish remaining work\n".to_string());
        storage
            .create_issue(&failing_issue, "tester")
            .expect("create failing issue");
        drop(storage);

        std::fs::write(
            beads_dir.join(close_policy::POLICY_FILE_NAME),
            "close_policy:\n  require_acceptance_criteria_satisfied:\n    enabled: true\n",
        )
        .expect("write policy");

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-clean".to_string(), "bd-policy-fail".to_string()],
            reason: Some("done".to_string()),
            ..CloseArgs::default()
        };
        let err = execute_with_args(&args, false, &CliOverrides::default(), &ctx)
            .expect_err("policy violation should abort the batch before mutation");
        assert!(
            matches!(err, BeadsError::PolicyViolation { .. }),
            "unexpected error: {err:?}"
        );

        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        let clean = storage
            .get_issue("bd-clean")
            .expect("read clean issue")
            .expect("clean issue exists");
        let failing = storage
            .get_issue("bd-policy-fail")
            .expect("read failing issue")
            .expect("failing issue exists");
        assert_eq!(clean.status, Status::Open);
        assert_eq!(failing.status, Status::Open);
        assert!(
            storage
                .get_close_metadata("bd-clean")
                .expect("read clean metadata")
                .is_none()
        );
    }

    #[test]
    fn execute_with_args_leaves_no_policy_close_metadata_invisible() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        let mut storage = SqliteStorage::open(&db_path).expect("storage");
        storage
            .create_issue(&make_issue("bd-no-policy", "No policy"), "tester")
            .expect("create no-policy issue");
        drop(storage);

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-no-policy".to_string()],
            reason: Some("done".to_string()),
            ..CloseArgs::default()
        };
        execute_with_args(&args, false, &CliOverrides::default(), &ctx).expect("close issue");

        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        assert!(
            storage
                .get_close_metadata("bd-no-policy")
                .expect("read close metadata")
                .is_none(),
            "repos without an active policy should retain the no-observable-change invariant"
        );
    }

    /// GitHub #399: `br close` must honor `workflow.transitions`. A policy
    /// whose map has no `open -> closed` edge has to refuse the close and
    /// leave the issue open; widening the map with the `any` wildcard has to
    /// let the same close through.
    #[test]
    fn execute_with_args_enforces_workflow_transitions_on_close() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        let mut storage = SqliteStorage::open(&db_path).expect("storage");
        storage
            .create_issue(&make_issue("bd-wf", "Workflow governed"), "tester")
            .expect("create workflow issue");
        drop(storage);

        let policy_path = beads_dir.join(close_policy::POLICY_FILE_NAME);
        std::fs::write(
            &policy_path,
            "workflow:\n  strict: true\n  statuses: [open, in_progress, closed]\n  \
             transitions:\n    open: [in_progress]\n    in_progress: [closed]\n",
        )
        .expect("write workflow policy");

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-wf".to_string()],
            reason: Some("done".to_string()),
            ..CloseArgs::default()
        };
        let err = execute_with_args(&args, false, &CliOverrides::default(), &ctx)
            .expect_err("open -> closed is not in the transitions map");
        let message = err.to_string();
        assert!(
            message.contains("workflow.transitions") && message.contains("'open'"),
            "error should name the rejected transition: {message}"
        );

        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        assert_eq!(
            storage
                .get_issue("bd-wf")
                .expect("read issue")
                .expect("issue exists")
                .status,
            Status::Open,
            "a refused close must leave the issue open"
        );
        drop(storage);

        // Widening the map with the `any` wildcard makes the same close legal.
        std::fs::write(
            &policy_path,
            "workflow:\n  strict: true\n  statuses: [open, in_progress, closed]\n  \
             transitions:\n    open: [in_progress]\n    any: [closed]\n",
        )
        .expect("rewrite workflow policy");
        execute_with_args(&args, false, &CliOverrides::default(), &ctx)
            .expect("wildcard `any: [closed]` permits the close");

        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        assert_eq!(
            storage
                .get_issue("bd-wf")
                .expect("read issue")
                .expect("issue exists")
                .status,
            Status::Closed
        );
    }

    // =========================================================================
    // forbid_close_with_deferred_dependents gate (beads_rust#303)
    // =========================================================================

    const DEFERRED_DEPENDENTS_POLICY: &str =
        "close_policy:\n  forbid_close_with_deferred_dependents:\n    enabled: true\n";

    /// Build a prereq bead `bd-prereq` and a dependent bead `bd-dep` with a
    /// `blocks` edge from the prereq (so `bd-dep` depends on `bd-prereq`),
    /// where the dependent has the supplied status.
    fn setup_prereq_and_dependent(
        beads_dir: &std::path::Path,
        db_path: &std::path::Path,
        dependent_status: Status,
    ) {
        let mut storage = SqliteStorage::open(db_path).expect("storage");
        storage
            .create_issue(&make_issue("bd-prereq", "Prerequisite"), "tester")
            .expect("create prereq");
        storage
            .create_issue(
                &make_issue_with_status("bd-dep", "Dependent", dependent_status),
                "tester",
            )
            .expect("create dependent");
        // Row (issue_id=bd-dep, depends_on_id=bd-prereq, type=blocks):
        // "bd-prereq blocks bd-dep" / "bd-dep depends on bd-prereq".
        storage
            .add_dependency(
                "bd-dep",
                "bd-prereq",
                DependencyType::Blocks.as_str(),
                "tester",
            )
            .expect("add dependency");
        storage.rebuild_blocked_cache(true).expect("rebuild cache");
        drop(storage);
        // No policy.yaml written by default — callers add it when needed.
        let _ = beads_dir;
    }

    #[test]
    fn deferred_dependents_gate_off_allows_close_by_default() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        // Deferred dependent present, but NO policy.yaml => gate is off.
        setup_prereq_and_dependent(&beads_dir, &db_path, Status::Deferred);

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-prereq".to_string()],
            reason: Some("done".to_string()),
            ..CloseArgs::default()
        };
        execute_with_args(&args, false, &CliOverrides::default(), &ctx)
            .expect("close should succeed when gate is off (backwards compatible)");

        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        let prereq = storage
            .get_issue("bd-prereq")
            .expect("get prereq")
            .expect("prereq exists");
        assert_eq!(prereq.status, Status::Closed);
    }

    #[test]
    fn deferred_dependents_gate_on_rejects_close_and_names_ids() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        setup_prereq_and_dependent(&beads_dir, &db_path, Status::Deferred);
        std::fs::write(
            beads_dir.join(close_policy::POLICY_FILE_NAME),
            DEFERRED_DEPENDENTS_POLICY,
        )
        .expect("write policy");

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-prereq".to_string()],
            reason: Some("done".to_string()),
            ..CloseArgs::default()
        };
        let err = execute_with_args(&args, false, &CliOverrides::default(), &ctx)
            .expect_err("close should be rejected by the deferred-dependents gate");

        match err {
            BeadsError::PolicyViolation {
                issue_id,
                summary,
                violations,
            } => {
                assert_eq!(issue_id, "bd-prereq");
                assert!(summary.contains("bd-dep"), "summary: {summary}");
                assert!(
                    violations.iter().any(|v| v.gate
                        == close_policy::GATE_FORBID_CLOSE_WITH_DEFERRED_DEPENDENTS),
                    "expected deferred-dependents gate to fire: {violations:?}"
                );
            }
            other => panic!("expected PolicyViolation, got {other:?}"),
        }

        // The prereq must NOT have been closed.
        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        let prereq = storage
            .get_issue("bd-prereq")
            .expect("get prereq")
            .expect("prereq exists");
        assert_ne!(prereq.status, Status::Closed);
    }

    #[test]
    fn deferred_dependents_gate_on_allows_when_dependent_not_deferred() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        // Dependent is open, not deferred => gate is satisfied.
        setup_prereq_and_dependent(&beads_dir, &db_path, Status::Open);
        std::fs::write(
            beads_dir.join(close_policy::POLICY_FILE_NAME),
            DEFERRED_DEPENDENTS_POLICY,
        )
        .expect("write policy");

        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-prereq".to_string()],
            reason: Some("done".to_string()),
            ..CloseArgs::default()
        };
        execute_with_args(&args, false, &CliOverrides::default(), &ctx)
            .expect("close should succeed when no dependent is deferred");

        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        let prereq = storage
            .get_issue("bd-prereq")
            .expect("get prereq")
            .expect("prereq exists");
        assert_eq!(prereq.status, Status::Closed);
    }

    #[test]
    fn deferred_dependents_gate_on_allows_after_reopening_dependent() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");

        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        setup_prereq_and_dependent(&beads_dir, &db_path, Status::Deferred);
        std::fs::write(
            beads_dir.join(close_policy::POLICY_FILE_NAME),
            DEFERRED_DEPENDENTS_POLICY,
        )
        .expect("write policy");

        // First attempt is rejected.
        {
            let _guard = DirGuard::new(temp.path());
            let args = CloseArgs {
                ids: vec!["bd-prereq".to_string()],
                reason: Some("done".to_string()),
                ..CloseArgs::default()
            };
            execute_with_args(&args, false, &CliOverrides::default(), &ctx)
                .expect_err("close should be rejected while dependent is deferred");
        }

        // Reopen the deferred dependent (`br update bd-dep --status=open`).
        {
            let mut storage = SqliteStorage::open(&db_path).expect("storage");
            let update = IssueUpdate {
                status: Some(Status::Open),
                ..Default::default()
            };
            storage
                .update_issue("bd-dep", &update, "tester")
                .expect("reopen dependent");
        }

        // Second attempt now succeeds.
        {
            let _guard = DirGuard::new(temp.path());
            let args = CloseArgs {
                ids: vec!["bd-prereq".to_string()],
                reason: Some("done".to_string()),
                ..CloseArgs::default()
            };
            execute_with_args(&args, false, &CliOverrides::default(), &ctx)
                .expect("close should succeed after the dependent is reopened");
        }

        let storage = SqliteStorage::open(&db_path).expect("reopen storage");
        let prereq = storage
            .get_issue("bd-prereq")
            .expect("get prereq")
            .expect("prereq exists");
        assert_eq!(prereq.status, Status::Closed);
    }

    // =========================================================================
    // Workflow gate enforcement at close (issue #312, layer 2 / beads_rust#319)
    // =========================================================================

    const GATE_POLICY_YAML: &str = r#"workflow:
  strict: true
  gates:
    "in_review -> closed":
      require_all:
        - ci_green
"#;

    fn setup_gate_repo(temp: &TempDir, status: Status) -> std::path::PathBuf {
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");
        let beads_dir = temp.path().join(".beads");
        std::fs::write(beads_dir.join("policy.yaml"), GATE_POLICY_YAML).expect("write policy");
        let db_path = beads_dir.join("beads.db");
        let mut storage = SqliteStorage::open(&db_path).expect("storage");
        storage
            .create_issue(&make_issue_with_status("bd-1", "Gated", status), "tester")
            .expect("create issue");
        drop(storage);
        db_path
    }

    #[test]
    fn close_blocked_when_required_gate_unsatisfied() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let db_path = setup_gate_repo(&temp, Status::Custom("in_review".to_string()));
        let _guard = DirGuard::new(temp.path());
        let ctx = OutputContext::from_flags(false, false, true);

        let args = CloseArgs {
            ids: vec!["bd-1".to_string()],
            reason: Some("done".to_string()),
            ..CloseArgs::default()
        };
        let err = execute_with_args(&args, false, &CliOverrides::default(), &ctx)
            .expect_err("close must be blocked by unsatisfied ci_green gate");
        assert!(
            matches!(err, BeadsError::PolicyViolation { .. }),
            "unexpected error: {err:?}"
        );

        // The issue must remain un-closed.
        let storage = SqliteStorage::open(&db_path).expect("reopen");
        assert_eq!(
            storage.get_issue("bd-1").unwrap().unwrap().status,
            Status::Custom("in_review".to_string())
        );
    }

    #[test]
    fn close_allowed_after_gate_passes() {
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let db_path = setup_gate_repo(&temp, Status::Custom("in_review".to_string()));
        {
            let storage = SqliteStorage::open(&db_path).expect("storage");
            storage
                .record_scoped_gate_result(
                    "bd-1",
                    "in_review",
                    0,
                    "closed",
                    "ci_green",
                    "ci",
                    true,
                    None,
                    "ci-bot",
                )
                .expect("record pass");
        }
        let _guard = DirGuard::new(temp.path());
        let ctx = OutputContext::from_flags(false, false, true);

        let args = CloseArgs {
            ids: vec!["bd-1".to_string()],
            reason: Some("done".to_string()),
            ..CloseArgs::default()
        };
        execute_with_args(&args, false, &CliOverrides::default(), &ctx)
            .expect("close should succeed once ci_green passes");

        let storage = SqliteStorage::open(&db_path).expect("reopen");
        assert_eq!(
            storage.get_issue("bd-1").unwrap().unwrap().status,
            Status::Closed
        );
    }

    #[test]
    fn close_unaffected_when_transition_not_gated() {
        // The gate only guards `in_review -> closed`; closing an `open` issue is
        // an `open -> closed` move with no rule, so it must proceed.
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let db_path = setup_gate_repo(&temp, Status::Open);
        let _guard = DirGuard::new(temp.path());
        let ctx = OutputContext::from_flags(false, false, true);

        let args = CloseArgs {
            ids: vec!["bd-1".to_string()],
            reason: Some("done".to_string()),
            ..CloseArgs::default()
        };
        execute_with_args(&args, false, &CliOverrides::default(), &ctx)
            .expect("open -> closed is not gated and must succeed");

        let storage = SqliteStorage::open(&db_path).expect("reopen");
        assert_eq!(
            storage.get_issue("bd-1").unwrap().unwrap().status,
            Status::Closed
        );
    }

    #[test]
    fn close_unaffected_with_no_policy_file() {
        // Backward-compat: no policy.yaml at all → close behaves as before.
        let _lock = crate::util::test_helpers::TEST_DIR_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let temp = TempDir::new().expect("tempdir");
        let ctx = OutputContext::from_flags(false, false, true);
        commands::init::execute(None, false, Some(temp.path()), &ctx).expect("init");
        let beads_dir = temp.path().join(".beads");
        let db_path = beads_dir.join("beads.db");
        {
            let mut storage = SqliteStorage::open(&db_path).expect("storage");
            storage
                .create_issue(
                    &make_issue_with_status(
                        "bd-1",
                        "Plain",
                        Status::Custom("in_review".to_string()),
                    ),
                    "tester",
                )
                .expect("create");
        }
        let _guard = DirGuard::new(temp.path());
        let args = CloseArgs {
            ids: vec!["bd-1".to_string()],
            reason: Some("done".to_string()),
            ..CloseArgs::default()
        };
        execute_with_args(&args, false, &CliOverrides::default(), &ctx)
            .expect("close must succeed with no policy file");
        let storage = SqliteStorage::open(&db_path).expect("reopen");
        assert_eq!(
            storage.get_issue("bd-1").unwrap().unwrap().status,
            Status::Closed
        );
    }
}