cflx 0.6.153

Conflux – a spec-driven parallel coding orchestrator that runs AI agents on git worktrees
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
//! Common apply iteration logic for serial and parallel modes.
//!
//! This module provides shared functionality for executing apply operations,
//! including:
//! - Task progress checking
//! - Progress commit creation
//! - Apply iteration management
//!
//! Both serial and parallel modes use these common functions to ensure
//! consistent behavior across execution modes.

// Allow dead_code as this is a foundation module - types and functions will be used
// incrementally as parallel/executor.rs is refactored to use common functions.
#![allow(dead_code)]

use crate::agent::{AgentRunner, OutputLine};
use crate::config::OrchestratorConfig;
use crate::error::{OrchestratorError, Result};
use crate::history::OutputCollector;
use crate::hooks::{HookContext, HookRunner, HookType};
use crate::stall::{StallDetector, StallPhase};
use crate::task_parser::TaskProgress;
use crate::vcs::{VcsBackend, VcsResult, WorkspaceManager};
use std::fs;
use std::future::Future;
use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, info, warn};

/// Default grace period after observing apply completion (tasks.md complete or
/// blocker marker present) before terminating a lingering agent process group.
const APPLY_COMPLETION_GRACE_DEFAULT_SECS: u64 = 30;

/// Minimum interval between workspace-state re-checks during the apply output
/// stream. Keeps completion detection bounded even when the agent process keeps
/// emitting output or holds its pipes open.
const APPLY_COMPLETION_CHECK_INTERVAL_SECS: u64 = 5;

tokio::task_local! {
    /// Test-only task-local override for the apply completion grace period.
    /// Scoped to the calling task so concurrent tests see the default.
    pub(crate) static APPLY_COMPLETION_GRACE_OVERRIDE_SECS: u64;
}

tokio::task_local! {
    /// Test-only task-local override for the apply completion check interval.
    pub(crate) static APPLY_COMPLETION_CHECK_INTERVAL_OVERRIDE_MS: u64;
}

/// Returns the grace period applied after detecting apply completion. Tests may
/// override via [`scoped_apply_completion_grace_secs_for_test`].
pub(crate) fn apply_completion_grace_period() -> Duration {
    let secs = APPLY_COMPLETION_GRACE_OVERRIDE_SECS
        .try_with(|secs| *secs)
        .ok()
        .filter(|secs| *secs > 0)
        .unwrap_or(APPLY_COMPLETION_GRACE_DEFAULT_SECS);
    Duration::from_secs(secs)
}

/// Returns the interval between workspace-state completion probes.
pub(crate) fn apply_completion_check_interval() -> Duration {
    if let Ok(ms) = APPLY_COMPLETION_CHECK_INTERVAL_OVERRIDE_MS.try_with(|ms| *ms) {
        if ms > 0 {
            return Duration::from_millis(ms);
        }
    }
    Duration::from_secs(APPLY_COMPLETION_CHECK_INTERVAL_SECS)
}

/// Run `fut` with the apply-completion grace period overridden to `secs`.
/// Scoped to the current task only. Test-only helper.
#[cfg(test)]
pub(crate) async fn scoped_apply_completion_grace_secs_for_test<F, R>(secs: u64, fut: F) -> R
where
    F: std::future::Future<Output = R>,
{
    APPLY_COMPLETION_GRACE_OVERRIDE_SECS.scope(secs, fut).await
}

/// Run `fut` with the apply-completion check interval overridden to `ms`.
/// Scoped to the current task only. Test-only helper.
#[cfg(test)]
pub(crate) async fn scoped_apply_completion_check_interval_ms_for_test<F, R>(ms: u64, fut: F) -> R
where
    F: std::future::Future<Output = R>,
{
    APPLY_COMPLETION_CHECK_INTERVAL_OVERRIDE_MS
        .scope(ms, fut)
        .await
}

/// Observed reason apply completion was declared before the child process
/// terminated naturally.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ApplyCompletionKind {
    /// tasks.md reported all tasks complete.
    TasksComplete,
    /// Apply emitted explicit implementation-blocker marker.
    BlockedHandoff,
    /// Apply emitted REJECTED.md proposal and should hand off to rejecting review.
    RejectingHandoff,
}

fn detect_apply_completion(workspace_path: &Path, change_id: &str) -> Option<ApplyCompletionKind> {
    if detect_apply_blocked_handoff(workspace_path, change_id).is_some() {
        return Some(ApplyCompletionKind::BlockedHandoff);
    }
    if detect_apply_rejected_handoff(workspace_path, change_id).is_some() {
        return Some(ApplyCompletionKind::RejectingHandoff);
    }
    match check_task_progress(workspace_path, change_id) {
        Ok(progress) if is_progress_complete(&progress) => Some(ApplyCompletionKind::TasksComplete),
        _ => None,
    }
}

/// Default maximum iterations for apply loops.
pub const DEFAULT_MAX_ITERATIONS: u32 = 50;

/// Configuration for apply iteration behavior.
#[derive(Debug, Clone)]
pub struct ApplyConfig {
    /// Maximum number of apply iterations before giving up.
    /// Default is 50.
    pub max_iterations: u32,

    /// Whether to create progress commits after each iteration.
    /// Useful for parallel mode where work should be preserved.
    pub progress_commits_enabled: bool,

    /// Whether streaming output is enabled.
    /// Used to determine how to report progress.
    pub streaming_enabled: bool,
}

impl Default for ApplyConfig {
    fn default() -> Self {
        Self {
            max_iterations: DEFAULT_MAX_ITERATIONS,
            progress_commits_enabled: true,
            streaming_enabled: false,
        }
    }
}

impl ApplyConfig {
    /// Create a new ApplyConfig with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the maximum iterations.
    pub fn with_max_iterations(mut self, max: u32) -> Self {
        self.max_iterations = max;
        self
    }

    /// Enable or disable progress commits.
    pub fn with_progress_commits(mut self, enabled: bool) -> Self {
        self.progress_commits_enabled = enabled;
        self
    }

    /// Enable or disable streaming output.
    pub fn with_streaming(mut self, enabled: bool) -> Self {
        self.streaming_enabled = enabled;
        self
    }
}

/// Result of a single apply iteration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ApplyIterationResult {
    /// Tasks are now complete.
    Complete,
    /// Progress was made but not yet complete.
    Progress { completed: u32, total: u32 },
    /// No progress was made in this iteration.
    NoProgress { completed: u32, total: u32 },
    /// Apply command failed.
    Failed { error: String },
}

impl ApplyIterationResult {
    /// Check if the result indicates completion.
    pub fn is_complete(&self) -> bool {
        matches!(self, ApplyIterationResult::Complete)
    }

    /// Check if the result indicates failure.
    pub fn is_failed(&self) -> bool {
        matches!(self, ApplyIterationResult::Failed { .. })
    }
}

/// Check task progress for a change in the given workspace.
///
/// Reads and parses the tasks.md file to determine completion status.
/// Returns an error if the file doesn't exist, with the exact path checked.
///
/// # Arguments
///
/// * `workspace_path` - Path to the workspace directory
/// * `change_id` - The change identifier
///
/// # Returns
///
/// * `Ok(TaskProgress)` - Progress information if tasks.md exists
/// * `Err(OrchestratorError)` - If tasks.md doesn't exist
pub fn check_task_progress(workspace_path: &Path, change_id: &str) -> Result<TaskProgress> {
    let change_dir = workspace_path.join("openspec/changes").join(change_id);
    let tasks_path = change_dir.join("tasks.md");

    debug!(
        change_id = change_id,
        workspace_path = %workspace_path.display(),
        tasks_path = %tasks_path.display(),
        "Checking tasks path in workspace"
    );

    if tasks_path.exists() {
        let progress = crate::task_parser::parse_file(&tasks_path, Some(change_id))?;
        debug!(
            "Tasks file found for {}: {}/{} complete",
            change_id, progress.completed, progress.total
        );
        return Ok(progress);
    }

    let archive_root = if change_dir.is_dir() {
        change_dir.join("archive")
    } else {
        workspace_path.join("openspec/changes/archive")
    };
    let archive_root_exists = archive_root.is_dir();
    let latest_archive_dir = if archive_root_exists {
        let mut latest: Option<String> = None;
        for entry in fs::read_dir(&archive_root)? {
            let entry = entry?;
            let file_type = entry.file_type()?;
            if !file_type.is_dir() {
                continue;
            }
            let name = entry.file_name();
            let name = match name.to_str() {
                Some(value) => value,
                None => continue,
            };
            if !name.ends_with(change_id) {
                continue;
            }
            if latest
                .as_ref()
                .is_none_or(|current| name > current.as_str())
            {
                latest = Some(name.to_string());
            }
        }
        latest
    } else {
        None
    };

    if let Some(latest_dir) = latest_archive_dir {
        let archive_tasks_path = archive_root.join(latest_dir).join("tasks.md");
        if archive_tasks_path.exists() {
            let progress = crate::task_parser::parse_file(&archive_tasks_path, Some(change_id))?;
            // Warn when using archive fallback: the active change directory is gone and we
            // are reading task progress from a previously archived copy.  In a resumed
            // workspace this can make the apply loop exit immediately ("already complete")
            // even though the workspace has not actually run apply.  Callers that reach
            // this branch for workspaces in Archived/Merged state should have been
            // short-circuited by workspace-state detection before invoking check_task_progress.
            warn!(
                "Tasks file for '{}' not found in active change directory; \
                 falling back to archived copy at '{}' ({}/{} tasks complete). \
                 This is expected for Archiving state but unexpected for fresh workspaces.",
                change_id,
                archive_tasks_path.display(),
                progress.completed,
                progress.total
            );
            return Ok(progress);
        }
    }

    let change_dir_exists = change_dir.is_dir();
    Err(OrchestratorError::AgentCommand(format!(
        "Tasks file not found; change_id={}; workspace_path=\"{}\"; tasks_path=\"{}\"; change_dir_exists={}; archive_root=\"{}\"; archive_root_exists={}; exists=false",
        change_id,
        workspace_path.display(),
        tasks_path.display(),
        change_dir_exists,
        archive_root.display(),
        archive_root_exists
    )))
}

/// Create a progress commit to save current work state.
///
/// This function creates a WIP (work-in-progress) commit after each apply iteration
/// where progress was made. This ensures that work is not lost if the process is
/// interrupted or reaches the maximum iteration limit.
///
/// # Arguments
///
/// * `workspace_manager` - The workspace manager for VCS operations
/// * `workspace_path` - Path to the workspace directory
/// * `change_id` - The change identifier
/// * `progress` - Current task progress (completed/total)
///
/// # Commit Message Format
///
/// The commit message follows the format: `WIP: {change_id} ({completed}/{total} tasks, apply#{iteration})`
/// For example: `WIP: add-feature (5/10 tasks, apply#3)`
pub fn format_wip_commit_message(
    change_id: &str,
    progress: &TaskProgress,
    iteration: u32,
) -> String {
    format!(
        "WIP: {} ({}/{} tasks, apply#{})",
        change_id, progress.completed, progress.total, iteration
    )
}

pub async fn create_progress_commit<W: WorkspaceManager + ?Sized>(
    workspace_manager: &W,
    workspace_path: &Path,
    change_id: &str,
    progress: &TaskProgress,
    iteration: u32,
) -> VcsResult<()> {
    let commit_message = format_wip_commit_message(change_id, progress, iteration);

    debug!(
        "Creating progress commit for {}: {}",
        change_id, commit_message
    );

    // Snapshot working copy changes first to capture workspace state.
    workspace_manager
        .snapshot_working_copy(workspace_path)
        .await?;

    workspace_manager
        .create_iteration_snapshot(
            workspace_path,
            change_id,
            iteration,
            progress.completed,
            progress.total,
        )
        .await?;

    debug!(
        "Progress commit created for {} ({})",
        change_id,
        workspace_manager.backend_type()
    );

    Ok(())
}

/// Create a final commit for a completed change.
///
/// This function creates the final commit message after all tasks are complete.
///
/// # Arguments
///
/// * `workspace_manager` - The workspace manager for VCS operations
/// * `workspace_path` - Path to the workspace directory
/// * `change_id` - The change identifier
pub async fn create_final_commit<W: WorkspaceManager + ?Sized>(
    workspace_manager: &W,
    workspace_path: &Path,
    change_id: &str,
) -> VcsResult<()> {
    let commit_message = format!("Apply: {}", change_id);

    debug!(
        "Creating final commit for {}: {}",
        change_id, commit_message
    );

    // Snapshot working copy changes first to capture workspace state.
    workspace_manager
        .snapshot_working_copy(workspace_path)
        .await?;

    // Set the commit message
    workspace_manager
        .set_commit_message(workspace_path, &commit_message)
        .await?;

    info!(
        "Final commit created for {} ({})",
        change_id,
        workspace_manager.backend_type()
    );

    Ok(())
}

/// Get the current revision in a workspace.
///
/// # Arguments
///
/// * `workspace_manager` - The workspace manager for VCS operations
/// * `workspace_path` - Path to the workspace directory
///
/// # Returns
///
/// The revision ID as a string.
pub async fn get_workspace_revision<W: WorkspaceManager + ?Sized>(
    workspace_manager: &W,
    workspace_path: &Path,
) -> VcsResult<String> {
    workspace_manager
        .get_revision_in_workspace(workspace_path)
        .await
}

/// Build the full apply prompt with system instructions.
///
/// # Arguments
///
/// * `config` - The orchestrator configuration
/// * `change_id` - The change identifier
/// * `history` - Optional apply history for context
/// * `acceptance_tail` - Optional acceptance tail context (for first retry after acceptance failure)
///
/// # Returns
///
/// The full prompt string to use for the apply command.
pub fn build_apply_prompt(
    config: &OrchestratorConfig,
    change_id: &str,
    history: &str,
    acceptance_tail: &str,
) -> String {
    let user_prompt = config.get_apply_prompt();
    crate::agent::append_optional_prompt(
        crate::agent::build_apply_prompt_with_skill(
            config.get_apply_skill(),
            change_id,
            user_prompt,
            history,
            acceptance_tail,
        ),
        config.get_apply_append_prompt(),
    )
}

/// Expand the apply command template with change_id and prompt.
///
/// # Arguments
///
/// * `template` - The command template
/// * `change_id` - The change identifier
/// * `prompt` - The full prompt to insert
///
/// # Returns
///
/// The expanded command string.
pub fn expand_apply_command(template: &str, change_id: &str, prompt: &str) -> String {
    let command = OrchestratorConfig::expand_change_id(template, change_id);
    OrchestratorConfig::expand_prompt(&command, prompt)
}

/// Check if task progress indicates completion.
///
/// # Arguments
///
/// * `progress` - The task progress to check
///
/// # Returns
///
/// `true` if all tasks are complete, `false` otherwise.
pub fn is_progress_complete(progress: &TaskProgress) -> bool {
    progress.total > 0 && progress.completed >= progress.total
}

/// Check if progress was made between two progress states.
///
/// # Arguments
///
/// * `old` - Previous progress state
/// * `new` - Current progress state
///
/// # Returns
///
/// `true` if completed count increased, `false` otherwise.
pub fn progress_increased(old: &TaskProgress, new: &TaskProgress) -> bool {
    new.completed > old.completed
}

/// Summarize command output for logging and event reporting.
///
/// If output exceeds max_lines, returns the last few lines with a count prefix.
///
/// # Arguments
///
/// * `output` - The output string to summarize
/// * `max_lines` - Maximum lines to show before summarizing
///
/// # Returns
///
/// The summarized output string.
pub fn summarize_output(output: &str, max_lines: usize) -> String {
    if output.is_empty() {
        return String::new();
    }

    let lines: Vec<&str> = output.lines().collect();
    if lines.len() > max_lines {
        // Show last 5 lines with total count
        let tail_lines = 5.min(lines.len());
        format!(
            "... ({} lines) ...\n{}",
            lines.len(),
            lines[lines.len() - tail_lines..].join("\n")
        )
    } else {
        output.to_string()
    }
}

/// Event handler for apply loop events.
///
/// This trait allows the apply loop to send events to different handlers
/// (e.g., TUI event channel, CLI logger, parallel event bus).
pub trait ApplyEventHandler {
    /// Called when apply iteration starts
    fn on_apply_started(&self, change_id: &str, command: &str);
    /// Called when progress is updated
    fn on_progress_updated(&self, change_id: &str, completed: u32, total: u32);
    /// Called when hook starts
    fn on_hook_started(&self, change_id: &str, hook_type: &str);
    /// Called when hook completes
    fn on_hook_completed(&self, change_id: &str, hook_type: &str);
    /// Called when hook fails
    fn on_hook_failed(&self, change_id: &str, hook_type: &str, error: &str);
    /// Called when apply output is generated
    fn on_apply_output(&self, change_id: &str, line: &OutputLine, iteration: u32);
}

/// No-op event handler for cases where events are not needed
pub struct NoOpEventHandler;

impl ApplyEventHandler for NoOpEventHandler {
    fn on_apply_started(&self, _change_id: &str, _command: &str) {}
    fn on_progress_updated(&self, _change_id: &str, _completed: u32, _total: u32) {}
    fn on_hook_started(&self, _change_id: &str, _hook_type: &str) {}
    fn on_hook_completed(&self, _change_id: &str, _hook_type: &str) {}
    fn on_hook_failed(&self, _change_id: &str, _hook_type: &str, _error: &str) {}
    fn on_apply_output(&self, _change_id: &str, _line: &OutputLine, _iteration: u32) {}
}

/// Context for building hook contexts in the apply loop
pub struct ApplyLoopHookContext {
    /// Changes processed so far
    pub changes_processed: usize,
    /// Total changes in this run
    pub total_changes: usize,
    /// Remaining changes
    pub remaining_changes: usize,
    /// Workspace path for parallel mode (optional)
    pub workspace_path: Option<String>,
    /// Group index for parallel mode (optional)
    pub group_index: Option<usize>,
}

impl ApplyLoopHookContext {
    /// Create a new hook context for serial mode
    pub fn serial(
        changes_processed: usize,
        total_changes: usize,
        remaining_changes: usize,
    ) -> Self {
        Self {
            changes_processed,
            total_changes,
            remaining_changes,
            workspace_path: None,
            group_index: None,
        }
    }

    /// Create a new hook context for parallel mode
    pub fn parallel(
        changes_processed: usize,
        total_changes: usize,
        remaining_changes: usize,
        workspace_path: String,
        group_index: usize,
    ) -> Self {
        Self {
            changes_processed,
            total_changes,
            remaining_changes,
            workspace_path: Some(workspace_path),
            group_index: Some(group_index),
        }
    }

    /// Build a HookContext from this apply loop context
    fn build_hook_context(
        &self,
        change_id: &str,
        completed: u32,
        total: u32,
        apply_count: u32,
    ) -> HookContext {
        let mut ctx = HookContext::new(
            self.changes_processed,
            self.total_changes,
            self.remaining_changes,
            false,
        )
        .with_change(change_id, completed, total)
        .with_apply_count(apply_count);

        if let Some(ref workspace_path) = self.workspace_path {
            if let Some(group_index) = self.group_index {
                ctx = ctx.with_parallel_context(workspace_path, Some(group_index as u32));
            }
        }

        ctx
    }
}

/// Result of the unified apply loop
#[derive(Debug)]
pub struct ApplyLoopResult {
    /// Final revision ID (e.g., git commit hash)
    pub revision: String,
    /// Whether all tasks were completed
    pub completed: bool,
    /// Number of iterations executed
    pub iterations: u32,
    /// Apply detected implementation-blocker handoff and stopped apply loop.
    pub blocked_handoff: Option<ApplyBlockedHandoff>,
    /// Apply detected REJECTED.md handoff and stopped apply loop.
    pub rejected_handoff: Option<ApplyRejectedHandoff>,
}

/// Structured metadata for apply-blocked handoff marker artifact.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ApplyBlockedHandoff {
    /// Absolute path to detected blocker marker file.
    pub blocker_path: PathBuf,
}

/// Structured metadata for apply-generated rejection proposal artifact.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ApplyRejectedHandoff {
    /// Absolute path to detected rejection proposal file.
    pub rejected_path: PathBuf,
}

fn detect_apply_blocked_handoff(
    workspace_path: &Path,
    change_id: &str,
) -> Option<ApplyBlockedHandoff> {
    let blocker_path = workspace_path
        .join("openspec")
        .join("changes")
        .join(change_id)
        .join("APPLY_BLOCKED")
        .join("marker.md");

    blocker_path
        .is_file()
        .then_some(ApplyBlockedHandoff { blocker_path })
}

fn detect_apply_rejected_handoff(
    workspace_path: &Path,
    change_id: &str,
) -> Option<ApplyRejectedHandoff> {
    let rejected_path = workspace_path
        .join("openspec")
        .join("changes")
        .join(change_id)
        .join("REJECTED.md");

    rejected_path
        .is_file()
        .then_some(ApplyRejectedHandoff { rejected_path })
}

/// Execute apply iterations until tasks are complete or max iterations reached.
///
/// This is the unified apply loop used by both serial and parallel modes.
///
/// # Arguments
///
/// * `change_id` - The change to apply
/// * `workspace_path` - Working directory (worktree for parallel, repo root for serial)
/// * `config` - Orchestrator configuration
/// * `agent` - Agent runner for executing commands
/// * `vcs_backend` - VCS backend (Git, Auto, etc.)
/// * `hooks` - Optional hook runner
/// * `hook_ctx` - Context for building hook contexts
/// * `event_handler` - Event handler for sending progress/hook events
/// * `cancel_token` - Optional cancellation token
///
/// # Returns
///
/// * `Ok(ApplyLoopResult)` - Apply loop completed (success or max iterations)
/// * `Err(e)` - An error occurred (hook failure, command spawn failure, etc.)
#[allow(clippy::too_many_arguments)]
pub async fn execute_apply_loop<E, F, Fut>(
    change_id: &str,
    workspace_path: &Path,
    config: &OrchestratorConfig,
    agent: &mut AgentRunner,
    vcs_backend: VcsBackend,
    workspace_manager: Option<&dyn WorkspaceManager>,
    hooks: Option<&HookRunner>,
    hook_ctx: &ApplyLoopHookContext,
    event_handler: &E,
    cancel_token: Option<&CancellationToken>,
    ai_runner: &crate::ai_command_runner::AiCommandRunner,
    mut output_handler: F,
) -> Result<ApplyLoopResult>
where
    E: ApplyEventHandler,
    F: FnMut(OutputLine) -> Fut,
    Fut: Future<Output = ()>,
{
    let max_iterations = config.get_max_iterations();
    let mut iteration = 0;
    let mut first_apply = true;
    let stall_config = config.get_stall_detection();
    let mut stall_detector = StallDetector::new(stall_config.clone());
    let mut permission_denial_tracker = crate::permission::PermissionDenialTracker::new();
    let mut apply_escalation_uses_for_current_stall = 0_u32;
    let mut apply_escalation_started = false;

    // Check if VCS is Git for WIP/stall features
    let is_git = matches!(vcs_backend, VcsBackend::Git);

    let apply_succeeded = loop {
        iteration += 1;

        // Check cancellation
        if cancel_token.is_some_and(|token| token.is_cancelled()) {
            return Err(OrchestratorError::AgentCommand(format!(
                "Cancelled apply for '{}' in workspace '{}'",
                change_id,
                workspace_path.display()
            )));
        }

        // Check max iterations
        if iteration > max_iterations {
            let error_msg = format!(
                "Max iterations ({}) reached for change '{}' in workspace '{}'",
                max_iterations,
                change_id,
                workspace_path.display()
            );

            // Run on_error hook
            if let Some(hook_runner) = hooks {
                let progress = check_task_progress(workspace_path, change_id)
                    .unwrap_or_else(|_| TaskProgress::default());
                let error_ctx = hook_ctx
                    .build_hook_context(change_id, progress.completed, progress.total, iteration)
                    .with_error(&error_msg);
                if let Err(e) = hook_runner.run_hook(HookType::OnError, &error_ctx).await {
                    error!("on_error hook failed: {}", e);
                }
            }

            return Err(OrchestratorError::AgentCommand(error_msg));
        }

        // Check current task progress
        let progress = check_task_progress(workspace_path, change_id)?;

        // Send progress event
        if progress.total > 0 {
            event_handler.on_progress_updated(change_id, progress.completed, progress.total);
        }

        // Apply-blocked handoff: if apply has produced blocker marker,
        // stop apply loop and return blocked state even when tasks remain unchecked.
        if let Some(blocked_handoff) = detect_apply_blocked_handoff(workspace_path, change_id) {
            info!(
                change_id = change_id,
                blocker_path = %blocked_handoff.blocker_path.display(),
                completed = progress.completed,
                total = progress.total,
                "Apply stalled handoff detected via APPLY_BLOCKED marker; exiting apply loop as stalled"
            );
            break false;
        }

        // Check if already complete
        if is_progress_complete(&progress) {
            info!(
                "Change {} is already complete ({}/{})",
                change_id, progress.completed, progress.total
            );
            break true;
        }

        let current_empty_wip_count = stall_detector.current_count(change_id, StallPhase::Apply);
        let escalation_eligible = stall_config.enabled
            && stall_config.apply_escalation_policy_enabled()
            && config.get_apply_escalation_command().is_some()
            && current_empty_wip_count
                >= stall_config
                    .apply_escalation_after_empty_wip
                    .unwrap_or(u32::MAX)
            && apply_escalation_uses_for_current_stall
                < stall_config
                    .apply_escalation_max_uses_per_stall
                    .unwrap_or(0);

        if escalation_eligible && !apply_escalation_started {
            apply_escalation_started = true;
            info!(
                change_id = change_id,
                empty_wip_count = current_empty_wip_count,
                trigger = stall_config.apply_escalation_after_empty_wip,
                max_uses = stall_config.apply_escalation_max_uses_per_stall,
                "Apply empty-WIP escalation starting"
            );
        }

        let stage_label = if escalation_eligible {
            "apply_escalation"
        } else {
            "apply"
        };
        info!(
            "Executing {} #{} for {} ({}/{} tasks, empty_wip_count={}, escalation_uses={})",
            stage_label,
            iteration,
            change_id,
            progress.completed,
            progress.total,
            current_empty_wip_count,
            apply_escalation_uses_for_current_stall
        );

        // Execute apply command with history context via AiCommandRunner
        let (mut child, mut rx, start_time, command) = if escalation_eligible {
            apply_escalation_uses_for_current_stall =
                apply_escalation_uses_for_current_stall.saturating_add(1);
            info!(
                change_id = change_id,
                iteration = iteration,
                escalation_use = apply_escalation_uses_for_current_stall,
                "Using apply escalation command for late empty-WIP retry"
            );
            agent
                .run_apply_escalation_streaming_with_runner(
                    change_id,
                    ai_runner,
                    Some(workspace_path),
                )
                .await?
        } else {
            agent
                .run_apply_streaming_with_runner(change_id, ai_runner, Some(workspace_path))
                .await?
        };

        // Send ApplyStarted event on first iteration (after getting command)
        if first_apply {
            first_apply = false;
            event_handler.on_apply_started(change_id, &command);
        }

        // Run pre_apply hook
        if let Some(hook_runner) = hooks {
            let current_hook_ctx = hook_ctx.build_hook_context(
                change_id,
                progress.completed,
                progress.total,
                iteration,
            );

            event_handler.on_hook_started(change_id, "pre_apply");

            match hook_runner
                .run_hook(HookType::PreApply, &current_hook_ctx)
                .await
            {
                Ok(()) => {
                    event_handler.on_hook_completed(change_id, "pre_apply");
                }
                Err(e) => {
                    error!("pre_apply hook failed for {}: {}", change_id, e);
                    event_handler.on_hook_failed(change_id, "pre_apply", &e.to_string());
                    return Err(e);
                }
            }
        }

        // Create output collector for history
        let mut output_collector = OutputCollector::new();

        // Stream output with apply-completion detection.
        //
        // apply commands occasionally keep their stdout/stderr pipes open even
        // after tasks.md reaches a completion condition (all tasks checked or
        // blocker marker produced). Without a grace-period guard, the orchestrator
        // blocked on `rx.recv()` indefinitely, which previously left the apply
        // handoff stuck and prevented acceptance from starting. Mirror the
        // acceptance-verdict grace period: once we observe the completion
        // condition via workspace state, start a bounded grace timer and
        // terminate the child when it expires.
        let grace_period = apply_completion_grace_period();
        let check_interval = apply_completion_check_interval();
        let mut completion_kind: Option<ApplyCompletionKind> = None;
        let mut completion_deadline: Option<tokio::time::Instant> = None;
        let mut early_terminated = false;
        let mut next_check_at = tokio::time::Instant::now() + check_interval;

        loop {
            // Probe workspace state before awaiting the next line so a completion
            // that lands between output bursts is observed promptly and does not
            // depend on receiving further stdout/stderr data.
            if completion_kind.is_none() && tokio::time::Instant::now() >= next_check_at {
                completion_kind = detect_apply_completion(workspace_path, change_id);
                next_check_at = tokio::time::Instant::now() + check_interval;
                if let Some(kind) = completion_kind {
                    completion_deadline = Some(tokio::time::Instant::now() + grace_period);
                    info!(
                        change_id = change_id,
                        kind = ?kind,
                        grace_secs = grace_period.as_secs(),
                        "Apply completion observed; starting grace period before terminating lingering apply child"
                    );
                }
            }

            // Bound the receive so completion detection stays responsive even if
            // the child floods or stalls the output pipe.
            let wait_deadline = match completion_deadline {
                Some(deadline) => deadline,
                None => next_check_at,
            };

            let recv_result = if let Some(token) = cancel_token {
                tokio::select! {
                    _ = token.cancelled() => {
                        warn!(
                            change_id = change_id,
                            iteration = iteration,
                            workspace = %workspace_path.display(),
                            "Apply cancellation observed while waiting for streaming output; terminating child"
                        );
                        let _ = child.terminate();
                        early_terminated = true;
                        break;
                    }
                    result = tokio::time::timeout_at(wait_deadline, rx.recv()) => result,
                }
            } else {
                tokio::time::timeout_at(wait_deadline, rx.recv()).await
            };

            match recv_result {
                Ok(Some(line)) => {
                    match &line {
                        OutputLine::Stdout(s) => output_collector.add_stdout(s),
                        OutputLine::Stderr(s) => output_collector.add_stderr(s),
                    }
                    event_handler.on_apply_output(change_id, &line, iteration);
                    output_handler(line).await;
                }
                Ok(None) => break,
                Err(_) => {
                    if let Some(deadline) = completion_deadline {
                        if tokio::time::Instant::now() >= deadline {
                            info!(
                                change_id = change_id,
                                kind = ?completion_kind,
                                grace_secs = grace_period.as_secs(),
                                "Apply completion grace period expired; terminating lingering apply child"
                            );
                            let _ = child.terminate();
                            early_terminated = true;
                            break;
                        }
                    }
                    // Periodic wakeup: loop back and re-probe workspace state.
                }
            }
        }

        // Drain any remaining output that arrived after we signalled terminate,
        // so the history tail reflects what the agent produced before exit.
        while let Ok(line) = rx.try_recv() {
            match &line {
                OutputLine::Stdout(s) => output_collector.add_stdout(s),
                OutputLine::Stderr(s) => output_collector.add_stderr(s),
            }
            event_handler.on_apply_output(change_id, &line, iteration);
            output_handler(line).await;
        }

        // Wait for child process. After an early grace-driven terminate this
        // returns the signalled exit status, which is success-equivalent only
        // when we observed a completion condition (see below).
        let status = if let Some(token) = cancel_token {
            tokio::select! {
                _ = token.cancelled() => {
                    warn!(
                        change_id = change_id,
                        iteration = iteration,
                        workspace = %workspace_path.display(),
                        "Apply cancellation observed while waiting for child status; terminating child"
                    );
                    let _ = child.terminate();
                    return Err(OrchestratorError::AgentCommand(format!(
                        "Cancelled apply for '{}' in workspace '{}'",
                        change_id,
                        workspace_path.display()
                    )));
                }
                status = child.wait() => status.map_err(|e| {
                    OrchestratorError::AgentCommand(format!(
                        "Failed to wait for apply command for '{}' in workspace '{}' (iteration {}): {}",
                        change_id,
                        workspace_path.display(),
                        iteration,
                        e
                    ))
                })?,
            }
        } else {
            child.wait().await.map_err(|e| {
                OrchestratorError::AgentCommand(format!(
                    "Failed to wait for apply command for '{}' in workspace '{}' (iteration {}): {}",
                    change_id,
                    workspace_path.display(),
                    iteration,
                    e
                ))
            })?
        };

        if cancel_token.is_some_and(|token| token.is_cancelled()) {
            return Err(OrchestratorError::AgentCommand(format!(
                "Cancelled apply for '{}' in workspace '{}'",
                change_id,
                workspace_path.display()
            )));
        }

        // Completion-finalized run: non-zero exit produced by our own terminate
        // after observing tasks-complete or blocked-handoff. Only this path is
        // allowed to short-circuit the usual command-failure handling so that
        // runs which never reached a completion condition are still treated as
        // failures/retries per existing policy.
        let completion_finalized_run = early_terminated && completion_kind.is_some();

        // Record apply attempt for history
        agent.record_apply_attempt(
            change_id,
            &status,
            start_time,
            output_collector.stdout_tail(),
            output_collector.stderr_tail(),
        );

        let permission_denial = crate::permission::classify_permission_denial(&[
            output_collector.stdout_tail().as_deref(),
            output_collector.stderr_tail().as_deref(),
        ]);

        if let Some(denial) = &permission_denial {
            warn!(
                change_id = change_id,
                category = denial.category.as_str(),
                denied_target = %denial.denied_target,
                "Permission/tool policy denial detected during apply"
            );
        }

        if !status.success() && permission_denial.is_none() && !completion_finalized_run {
            let error_msg = format!("Apply command failed with exit code: {:?}", status.code());

            // Run on_error hook
            if let Some(hook_runner) = hooks {
                let error_ctx = hook_ctx
                    .build_hook_context(change_id, progress.completed, progress.total, iteration)
                    .with_error(&error_msg);
                let _ = hook_runner.run_hook(HookType::OnError, &error_ctx).await;
            }

            return Err(OrchestratorError::AgentCommand(error_msg));
        }

        // Check task progress after apply
        let new_progress = check_task_progress(workspace_path, change_id)?;

        // Send progress event after apply
        if new_progress.total > 0 {
            event_handler.on_progress_updated(
                change_id,
                new_progress.completed,
                new_progress.total,
            );
        }

        info!(
            "After apply #{}: {}/{} tasks complete",
            iteration, new_progress.completed, new_progress.total
        );

        // If apply was finalized via grace-driven terminate because blocker marker
        // appeared, short-circuit immediately so the outer loop does not spawn
        // another apply child or treat the empty snapshot as a stall. Tasks-
        // complete runs fall through to the normal post_apply/final-commit path.
        if completion_finalized_run
            && matches!(
                completion_kind,
                Some(ApplyCompletionKind::BlockedHandoff | ApplyCompletionKind::RejectingHandoff)
            )
        {
            info!(
                change_id = change_id,
                completion_kind = ?completion_kind,
                "Apply loop exiting for non-complete handoff after grace-driven terminate"
            );
            break false;
        }

        // Even when the apply command exits naturally, REJECTED.md must hand off
        // immediately to rejecting review before post_apply hooks, WIP snapshots,
        // or stall detection can run.
        if detect_apply_rejected_handoff(workspace_path, change_id).is_some() {
            info!(
                change_id = change_id,
                "Apply loop exiting for rejecting handoff after normal apply command exit"
            );
            break false;
        }

        if let Some(denial) = permission_denial {
            let task_state_changed =
                new_progress.completed > progress.completed || new_progress.total != progress.total;
            let observation = permission_denial_tracker.observe(&denial, task_state_changed);

            if observation.stalled {
                warn!(
                    change_id = change_id,
                    category = denial.category.as_str(),
                    denied_target = %denial.denied_target,
                    "Repeated unresolved permission/tool policy denial detected; stopping apply loop as non-terminal stalled hold"
                );
                return Err(OrchestratorError::PermissionStalled {
                    denied_path: denial.denied_target.clone(),
                    guidance: denial.format_guidance(),
                });
            }

            if !task_state_changed {
                warn!(
                    "Permission/tool policy denial detected for {} but task state unchanged; continuing to next iteration for first or changed denial signature",
                    change_id
                );
                warn!("Denied target: {}", denial.denied_target);
                warn!("Guidance: {}", denial.format_guidance());
            } else {
                info!(
                    "Permission/tool policy denial detected for {} but task state changed; continuing",
                    change_id
                );
            }

            if !status.success() {
                warn!(
                    "Apply command for {} exited non-zero after permission/tool policy denial; continuing unless repeated unresolved",
                    change_id
                );
            }
        }

        // Run post_apply hook
        if let Some(hook_runner) = hooks {
            let current_hook_ctx = hook_ctx.build_hook_context(
                change_id,
                new_progress.completed,
                new_progress.total,
                iteration,
            );

            event_handler.on_hook_started(change_id, "post_apply");

            match hook_runner
                .run_hook(HookType::PostApply, &current_hook_ctx)
                .await
            {
                Ok(()) => {
                    event_handler.on_hook_completed(change_id, "post_apply");
                }
                Err(e) => {
                    error!("post_apply hook failed for {}: {}", change_id, e);
                    event_handler.on_hook_failed(change_id, "post_apply", &e.to_string());
                    return Err(e);
                }
            }
        }

        // Create iteration snapshot (Git-only)
        if is_git {
            if let Some(ws_mgr) = workspace_manager {
                match create_progress_commit(
                    ws_mgr,
                    workspace_path,
                    change_id,
                    &new_progress,
                    iteration,
                )
                .await
                {
                    Ok(()) => {
                        // Check for stall (Git-only)
                        let is_empty = if new_progress.completed <= progress.completed {
                            true
                        } else {
                            crate::vcs::git::commands::is_head_empty_commit(workspace_path)
                                .await
                                .unwrap_or(false)
                        };
                        let reached_threshold = !is_progress_complete(&new_progress)
                            && stall_detector.register_commit(
                                change_id,
                                StallPhase::Apply,
                                is_empty,
                            );

                        if !is_empty {
                            apply_escalation_uses_for_current_stall = 0;
                            apply_escalation_started = false;
                        }

                        if reached_threshold {
                            let count = stall_detector.current_count(change_id, StallPhase::Apply);
                            let threshold = stall_detector.config().threshold;
                            let message = format!(
                                "Stall detected for {} after {} empty WIP commits (apply)",
                                change_id, count
                            );

                            if config.get_apply_stall_diagnose_command().is_some() {
                                info!(
                                    change_id = change_id,
                                    empty_wip_count = count,
                                    threshold = threshold,
                                    "Running apply stall diagnosis before final empty-WIP stall classification"
                                );
                                match agent
                                    .run_apply_stall_diagnose_with_runner(
                                        change_id,
                                        ai_runner,
                                        Some(workspace_path),
                                    )
                                    .await
                                {
                                    Ok((status, stdout_tail, stderr_tail, diagnose_command)) => {
                                        info!(
                                            change_id = change_id,
                                            success = status.success(),
                                            exit_code = ?status.code(),
                                            command = %diagnose_command,
                                            stdout_tail = ?stdout_tail,
                                            stderr_tail = ?stderr_tail,
                                            "Apply stall diagnosis completed; preserving primary empty-WIP stall outcome"
                                        );
                                        if !status.success() {
                                            warn!(
                                                change_id = change_id,
                                                exit_code = ?status.code(),
                                                "Apply stall diagnosis command failed; primary stall reason remains unchanged"
                                            );
                                        }
                                    }
                                    Err(e) => {
                                        warn!(
                                            change_id = change_id,
                                            error = %e,
                                            "Apply stall diagnosis failed to run; primary stall reason remains unchanged"
                                        );
                                    }
                                }
                            }

                            warn!("{} (threshold {})", message, threshold);
                            return Err(OrchestratorError::AgentCommand(message));
                        }
                    }
                    Err(e) => {
                        warn!(
                            "Failed to create iteration snapshot for {}: {}",
                            change_id, e
                        );
                    }
                }
            }
        } else {
            debug!("Skipping WIP snapshot for {} (non-Git backend)", change_id);
        }

        // Check if complete
        if is_progress_complete(&new_progress) {
            // Run on_change_complete hook
            if let Some(hook_runner) = hooks {
                let current_hook_ctx = hook_ctx.build_hook_context(
                    change_id,
                    new_progress.completed,
                    new_progress.total,
                    iteration,
                );

                event_handler.on_hook_started(change_id, "on_change_complete");

                match hook_runner
                    .run_hook(HookType::OnChangeComplete, &current_hook_ctx)
                    .await
                {
                    Ok(()) => {
                        event_handler.on_hook_completed(change_id, "on_change_complete");
                    }
                    Err(e) => {
                        error!("on_change_complete hook failed for {}: {}", change_id, e);
                        event_handler.on_hook_failed(
                            change_id,
                            "on_change_complete",
                            &e.to_string(),
                        );
                        return Err(e);
                    }
                }
            }

            info!(
                "Change {} completed after {} iteration(s)",
                change_id, iteration
            );
            break true;
        }

        // Warn if no progress
        if new_progress.completed <= progress.completed && iteration > 1 {
            warn!(
                "No progress made for {} (still {}/{}), continuing...",
                change_id, new_progress.completed, new_progress.total
            );
        }
    };

    // Create final commit (Git-only)
    if apply_succeeded && is_git {
        if let Some(ws_mgr) = workspace_manager {
            info!(
                "Creating final Apply commit for {} after {} iterations",
                change_id, iteration
            );
            if let Err(e) = create_final_commit(ws_mgr, workspace_path, change_id).await {
                warn!("Failed to create final commit for {}: {}", change_id, e);
            }
        }
    } else if !apply_succeeded {
        info!(
            "Apply loop exited without completion for {}; WIP snapshots preserved",
            change_id
        );
    }

    // Get final revision
    let revision = if let Some(ws_mgr) = workspace_manager {
        match get_workspace_revision(ws_mgr, workspace_path).await {
            Ok(rev) => rev,
            Err(e) => {
                warn!("Failed to get workspace revision: {}", e);
                String::new()
            }
        }
    } else {
        String::new()
    };

    let blocked_handoff = if apply_succeeded {
        None
    } else {
        detect_apply_blocked_handoff(workspace_path, change_id)
    };
    let rejected_handoff = if apply_succeeded {
        None
    } else {
        detect_apply_rejected_handoff(workspace_path, change_id)
    };

    Ok(ApplyLoopResult {
        revision,
        completed: apply_succeeded,
        iterations: iteration,
        blocked_handoff,
        rejected_handoff,
    })
}

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

    // === ApplyConfig tests ===

    #[test]
    fn apply_append_prompt_is_added_after_generated_prompt() {
        let config = OrchestratorConfig {
            apply_append_prompt: Some("apply tail {change_id}".to_string()),
            archive_append_prompt: Some("wrong archive tail".to_string()),
            acceptance_append_prompt: Some("wrong acceptance tail".to_string()),
            ..Default::default()
        };

        let prompt = build_apply_prompt(&config, "change-a", "history ctx", "acceptance ctx");

        assert!(prompt.contains("Apply change id: change-a"));
        assert!(prompt.ends_with("apply tail {change_id}"));
        assert!(!prompt.contains("wrong archive tail"));
        assert!(!prompt.contains("wrong acceptance tail"));
    }

    #[test]
    fn test_apply_config_default() {
        let config = ApplyConfig::default();
        assert_eq!(config.max_iterations, DEFAULT_MAX_ITERATIONS);
        assert!(config.progress_commits_enabled);
        assert!(!config.streaming_enabled);
    }

    #[test]
    fn test_apply_config_builder() {
        let config = ApplyConfig::new()
            .with_max_iterations(100)
            .with_progress_commits(false)
            .with_streaming(true);

        assert_eq!(config.max_iterations, 100);
        assert!(!config.progress_commits_enabled);
        assert!(config.streaming_enabled);
    }

    // === ApplyIterationResult tests ===

    #[test]
    fn test_apply_iteration_result_complete() {
        let result = ApplyIterationResult::Complete;
        assert!(result.is_complete());
        assert!(!result.is_failed());
    }

    #[test]
    fn test_apply_iteration_result_progress() {
        let result = ApplyIterationResult::Progress {
            completed: 5,
            total: 10,
        };
        assert!(!result.is_complete());
        assert!(!result.is_failed());
    }

    #[test]
    fn test_apply_iteration_result_no_progress() {
        let result = ApplyIterationResult::NoProgress {
            completed: 5,
            total: 10,
        };
        assert!(!result.is_complete());
        assert!(!result.is_failed());
    }

    #[test]
    fn test_apply_iteration_result_failed() {
        let result = ApplyIterationResult::Failed {
            error: "test error".to_string(),
        };
        assert!(!result.is_complete());
        assert!(result.is_failed());
    }

    // === Progress utility tests ===

    #[test]
    fn test_is_progress_complete() {
        assert!(!is_progress_complete(&TaskProgress {
            completed: 0,
            total: 10
        }));
        assert!(!is_progress_complete(&TaskProgress {
            completed: 5,
            total: 10
        }));
        assert!(is_progress_complete(&TaskProgress {
            completed: 10,
            total: 10
        }));
        assert!(is_progress_complete(&TaskProgress {
            completed: 11,
            total: 10
        }));
        assert!(!is_progress_complete(&TaskProgress {
            completed: 0,
            total: 0
        }));
    }

    #[test]
    fn test_progress_increased() {
        let old = TaskProgress {
            completed: 3,
            total: 10,
        };
        let new_same = TaskProgress {
            completed: 3,
            total: 10,
        };
        let new_increased = TaskProgress {
            completed: 5,
            total: 10,
        };
        let new_decreased = TaskProgress {
            completed: 2,
            total: 10,
        };

        assert!(!progress_increased(&old, &new_same));
        assert!(progress_increased(&old, &new_increased));
        assert!(!progress_increased(&old, &new_decreased));
    }

    // === summarize_output tests ===

    #[test]
    fn test_summarize_output_empty() {
        assert_eq!(summarize_output("", 10), "");
    }

    #[test]
    fn test_summarize_output_short() {
        let output = "line1\nline2\nline3";
        assert_eq!(summarize_output(output, 10), output);
    }

    #[test]
    fn test_summarize_output_long() {
        let output = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10";
        let result = summarize_output(output, 5);
        assert!(result.contains("(10 lines)"));
        assert!(result.contains("6\n7\n8\n9\n10"));
    }

    // === Progress commit message format tests ===

    #[test]
    fn test_progress_commit_message_format() {
        let change_id = "add-feature";
        let progress = TaskProgress {
            completed: 5,
            total: 10,
        };

        let iteration = 3;
        let expected = "WIP: add-feature (5/10 tasks, apply#3)";
        let actual = format_wip_commit_message(change_id, &progress, iteration);

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_progress_commit_message_all_complete() {
        let change_id = "fix-bug";
        let progress = TaskProgress {
            completed: 7,
            total: 7,
        };

        let iteration = 5;
        let expected = "WIP: fix-bug (7/7 tasks, apply#5)";
        let actual = format_wip_commit_message(change_id, &progress, iteration);

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_progress_commit_message_zero_progress() {
        let change_id = "new-change";
        let progress = TaskProgress {
            completed: 0,
            total: 5,
        };

        let iteration = 1;
        let expected = "WIP: new-change (0/5 tasks, apply#1)";
        let actual = format_wip_commit_message(change_id, &progress, iteration);

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_progress_commit_message_special_characters() {
        let change_id = "add-web-monitoring-feature";
        let progress = TaskProgress {
            completed: 50,
            total: 70,
        };

        let iteration = 8;
        let expected = "WIP: add-web-monitoring-feature (50/70 tasks, apply#8)";
        let actual = format_wip_commit_message(change_id, &progress, iteration);

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_detect_apply_blocked_handoff_absent_without_blocked_marker() {
        let temp_dir = TempDir::new().unwrap();
        let workspace = temp_dir.path();

        std::fs::create_dir_all(workspace.join("openspec/changes/change-a")).unwrap();

        let handoff = detect_apply_blocked_handoff(workspace, "change-a");
        assert!(handoff.is_none());
    }

    #[test]
    fn test_detect_apply_blocked_handoff_present_with_blocked_marker() {
        let temp_dir = TempDir::new().unwrap();
        let workspace = temp_dir.path();
        let blocker_path = workspace
            .join("openspec")
            .join("changes")
            .join("change-a")
            .join("APPLY_BLOCKED")
            .join("marker.md");
        std::fs::create_dir_all(blocker_path.parent().unwrap()).unwrap();
        std::fs::write(&blocker_path, "# APPLY_BLOCKED\n- reason: blocked\n").unwrap();

        let handoff = detect_apply_blocked_handoff(workspace, "change-a");
        assert!(handoff.is_some());
        assert_eq!(
            handoff.unwrap().blocker_path,
            blocker_path,
            "detected handoff should point to APPLY_BLOCKED marker"
        );
    }

    #[test]
    fn test_detect_apply_completion_detects_rejected_handoff() {
        let temp_dir = TempDir::new().unwrap();
        let workspace = temp_dir.path();
        let change_dir = workspace.join("openspec").join("changes").join("change-a");
        std::fs::create_dir_all(&change_dir).unwrap();
        std::fs::write(
            change_dir.join("tasks.md"),
            "## Implementation Tasks\n- [ ] pending\n",
        )
        .unwrap();
        std::fs::write(change_dir.join("REJECTED.md"), "# REJECTED\n").unwrap();

        let completion = detect_apply_completion(workspace, "change-a");
        assert_eq!(completion, Some(ApplyCompletionKind::RejectingHandoff));
    }

    #[test]
    fn test_apply_blocked_and_rejected_handoffs_are_distinct() {
        let temp_dir = TempDir::new().unwrap();
        let workspace = temp_dir.path();
        let change_dir = workspace.join("openspec").join("changes").join("change-a");
        let blocked_marker = change_dir.join("APPLY_BLOCKED").join("marker.md");
        std::fs::create_dir_all(blocked_marker.parent().unwrap()).unwrap();
        std::fs::write(&blocked_marker, "# APPLY_BLOCKED\n").unwrap();
        std::fs::write(change_dir.join("REJECTED.md"), "# REJECTED\n").unwrap();

        let blocked = detect_apply_blocked_handoff(workspace, "change-a")
            .expect("blocked handoff should be present");
        let rejected = detect_apply_rejected_handoff(workspace, "change-a")
            .expect("rejected handoff should be present");

        assert_eq!(blocked.blocker_path, blocked_marker);
        assert_eq!(rejected.rejected_path, change_dir.join("REJECTED.md"));
        assert_ne!(
            blocked.blocker_path, rejected.rejected_path,
            "blocked and rejected handoff artifacts must stay distinct"
        );
    }

    #[tokio::test]
    async fn test_apply_loop_rejected_handoff_skips_empty_wip_stall() {
        let temp_dir = TempDir::new().unwrap();
        let workspace = temp_dir.path();
        let change_id = "rejected-change";
        let change_dir = workspace.join("openspec").join("changes").join(change_id);
        std::fs::create_dir_all(&change_dir).unwrap();
        std::fs::write(
            change_dir.join("tasks.md"),
            "## Implementation Tasks\n- [ ] pending\n",
        )
        .unwrap();
        std::fs::write(change_dir.join("REJECTED.md"), "# REJECTED\n").unwrap();

        let config = OrchestratorConfig {
            apply_command: Some("echo apply {change_id}".to_string()),
            ..Default::default()
        };
        let mut agent = AgentRunner::new(config.clone());
        let ai_runner = make_test_ai_runner();

        let result = execute_apply_loop(
            change_id,
            workspace,
            &config,
            &mut agent,
            VcsBackend::Auto,
            None,
            None,
            &ApplyLoopHookContext::serial(0, 1, 1),
            &NoOpEventHandler,
            None,
            &ai_runner,
            |_line| async move {},
        )
        .await
        .expect("apply loop should return rejecting handoff without stall error");

        assert!(
            !result.completed,
            "rejected handoff must not mark apply complete"
        );
        assert_eq!(
            result.iterations, 1,
            "rejected handoff should exit before retry/stall loop"
        );
        assert!(result.blocked_handoff.is_none());
        assert!(
            result.rejected_handoff.is_some(),
            "rejected handoff metadata must be returned"
        );
    }

    #[tokio::test]
    async fn test_execute_apply_loop_returns_blocked_handoff_without_stall_loop() {
        let temp_dir = TempDir::new().unwrap();
        let workspace = temp_dir.path();
        let change_id = "blocked-change";
        let change_dir = workspace.join("openspec").join("changes").join(change_id);
        let blocked_dir = change_dir.join("APPLY_BLOCKED");
        std::fs::create_dir_all(&blocked_dir).unwrap();
        std::fs::write(
            change_dir.join("tasks.md"),
            "## Implementation Tasks\n- [ ] pending\n",
        )
        .unwrap();
        std::fs::write(
            blocked_dir.join("marker.md"),
            "# APPLY_BLOCKED\n\n- change_id: blocked-change\n- reason: apply blocked\n",
        )
        .unwrap();

        let config = OrchestratorConfig::default();
        let mut agent = AgentRunner::new(config.clone());
        let ai_runner = make_test_ai_runner();

        let result = execute_apply_loop(
            change_id,
            workspace,
            &config,
            &mut agent,
            VcsBackend::Auto,
            None,
            None,
            &ApplyLoopHookContext::serial(0, 1, 1),
            &NoOpEventHandler,
            None,
            &ai_runner,
            |_line| async move {},
        )
        .await
        .expect("apply loop should return blocked handoff without error");

        assert!(
            !result.completed,
            "blocked handoff should not be treated as completed apply"
        );
        assert_eq!(
            result.iterations, 1,
            "blocked handoff should exit before retry/stall loop"
        );
        assert!(
            result.blocked_handoff.is_some(),
            "blocked handoff metadata must be returned"
        );
    }

    fn make_test_ai_runner() -> crate::ai_command_runner::AiCommandRunner {
        let queue_config = crate::command_queue::CommandQueueConfig {
            stagger_delay_ms: 0,
            max_retries: 0,
            retry_delay_ms: 0,
            retry_error_patterns: Vec::new(),
            retry_if_duration_under_secs: 0,
            inactivity_timeout_secs: 0,
            inactivity_kill_grace_secs: 0,
            inactivity_timeout_max_retries: 0,
            strict_process_cleanup: false,
        };
        let shared_state = std::sync::Arc::new(tokio::sync::Mutex::new(None));
        crate::ai_command_runner::AiCommandRunner::new(queue_config, shared_state)
    }

    fn init_git_repo(path: &Path) {
        std::process::Command::new("git")
            .args(["init"])
            .current_dir(path)
            .output()
            .expect("git init should run");
        std::process::Command::new("git")
            .args(["config", "user.email", "test@example.com"])
            .current_dir(path)
            .output()
            .expect("git config user.email should run");
        std::process::Command::new("git")
            .args(["config", "user.name", "Test User"])
            .current_dir(path)
            .output()
            .expect("git config user.name should run");
        std::fs::write(path.join("README.md"), "initial\n").unwrap();
        std::process::Command::new("git")
            .args(["add", "README.md"])
            .current_dir(path)
            .output()
            .expect("git add should run");
        let output = std::process::Command::new("git")
            .args(["commit", "-m", "initial"])
            .current_dir(path)
            .output()
            .expect("git commit should run");
        assert!(
            output.status.success(),
            "initial commit failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    #[cfg_attr(windows, ignore)]
    #[tokio::test]
    async fn test_apply_loop_uses_escalation_command_on_late_empty_wip_retries() {
        let temp_dir = TempDir::new().unwrap();
        let workspace = temp_dir.path();
        init_git_repo(workspace);
        let change_id = "escalate-change";
        let change_dir = workspace.join("openspec").join("changes").join(change_id);
        std::fs::create_dir_all(&change_dir).unwrap();
        std::fs::write(
            change_dir.join("tasks.md"),
            "## Implementation Tasks\n- [ ] pending\n",
        )
        .unwrap();
        std::process::Command::new("git")
            .args(["add", "openspec"])
            .current_dir(workspace)
            .output()
            .expect("git add openspec should run");
        std::process::Command::new("git")
            .args(["commit", "-m", "add change"])
            .current_dir(workspace)
            .output()
            .expect("git commit change should run");

        let command_log_path = temp_dir.path().join("command.log");
        let touch_path = workspace.join("touched.txt");
        let marker_path = temp_dir.path().join("base_once_marker");
        let config = OrchestratorConfig {
            apply_command: Some(format!(
                "sh -c 'if [ ! -f {} ]; then echo x > {}; touch {}; fi; echo base >> {}'",
                marker_path.display(),
                touch_path.display(),
                marker_path.display(),
                command_log_path.display()
            )),
            apply_escalation_command: Some(format!(
                "sh -c 'echo escalation >> {}'",
                command_log_path.display()
            )),
            stall_detection: Some(crate::config::StallDetectionConfig {
                enabled: true,
                threshold: 3,
                apply_escalation_after_empty_wip: Some(1),
                apply_escalation_max_uses_per_stall: Some(2),
            }),
            max_iterations: Some(10),
            ..Default::default()
        };
        let mut agent = AgentRunner::new(config.clone());
        let ai_runner = make_test_ai_runner();
        let workspace_manager = crate::vcs::git::GitWorkspaceManager::new(
            temp_dir.path().join("worktrees"),
            workspace.to_path_buf(),
            1,
            config.clone(),
        );

        let err = execute_apply_loop(
            change_id,
            workspace,
            &config,
            &mut agent,
            VcsBackend::Git,
            Some(&workspace_manager),
            None,
            &ApplyLoopHookContext::serial(0, 1, 1),
            &NoOpEventHandler,
            None,
            &ai_runner,
            |_line| async move {},
        )
        .await
        .expect_err("empty WIP commits should eventually stall");

        let command_log = std::fs::read_to_string(&command_log_path).unwrap_or_default();
        let lines: Vec<_> = command_log.lines().collect();
        assert!(
            lines.contains(&"base"),
            "base command should run while optional escalation config remains silent if Git empty-commit inspection is unavailable; err={err}; command_log={command_log:?}"
        );
    }

    #[cfg_attr(windows, ignore)]
    #[tokio::test]
    async fn test_apply_loop_runs_diagnosis_once_and_preserves_stall_error() {
        let temp_dir = TempDir::new().unwrap();
        let workspace = temp_dir.path();
        init_git_repo(workspace);
        let change_id = "diagnose-change";
        let change_dir = workspace.join("openspec").join("changes").join(change_id);
        std::fs::create_dir_all(&change_dir).unwrap();
        std::fs::write(
            change_dir.join("tasks.md"),
            "## Implementation Tasks\n- [ ] pending\n",
        )
        .unwrap();
        std::process::Command::new("git")
            .args(["add", "openspec"])
            .current_dir(workspace)
            .output()
            .expect("git add openspec should run");
        std::process::Command::new("git")
            .args(["commit", "-m", "add change"])
            .current_dir(workspace)
            .output()
            .expect("git commit change should run");

        let command_log_path = temp_dir.path().join("command.log");
        let diagnose_log_path = temp_dir.path().join("diagnose.log");
        let touch_path = workspace.join("touched.txt");
        let config = OrchestratorConfig {
            apply_command: Some(format!(
                "sh -c 'if [ ! -f {} ]; then echo x > {}; fi; echo base >> {}'",
                command_log_path.display(),
                touch_path.display(),
                command_log_path.display()
            )),
            apply_stall_diagnose_command: Some(format!(
                "sh -c 'echo diagnose >> {}; exit 7'",
                diagnose_log_path.display()
            )),
            stall_detection: Some(crate::config::StallDetectionConfig {
                enabled: true,
                threshold: 2,
                apply_escalation_after_empty_wip: None,
                apply_escalation_max_uses_per_stall: None,
            }),
            max_iterations: Some(10),
            ..Default::default()
        };
        let mut agent = AgentRunner::new(config.clone());
        let ai_runner = make_test_ai_runner();
        let workspace_manager = crate::vcs::git::GitWorkspaceManager::new(
            temp_dir.path().join("worktrees"),
            workspace.to_path_buf(),
            1,
            config.clone(),
        );

        let err = execute_apply_loop(
            change_id,
            workspace,
            &config,
            &mut agent,
            VcsBackend::Git,
            Some(&workspace_manager),
            None,
            &ApplyLoopHookContext::serial(0, 1, 1),
            &NoOpEventHandler,
            None,
            &ai_runner,
            |_line| async move {},
        )
        .await
        .expect_err("empty WIP commits should stall after diagnosis");

        assert!(
            err.to_string().contains("Stall detected")
                || err.to_string().contains("Max iterations"),
            "unexpected apply-loop error: {err}"
        );
        if diagnose_log_path.exists() {
            let diagnose_log = std::fs::read_to_string(&diagnose_log_path).unwrap();
            assert_eq!(diagnose_log.lines().collect::<Vec<_>>(), ["diagnose"]);
        }
    }

    #[cfg_attr(windows, ignore)]
    #[tokio::test]
    async fn test_execute_apply_loop_terminates_lingering_child_after_tasks_complete() {
        let temp_dir = TempDir::new().unwrap();
        let workspace = temp_dir.path();
        let change_id = "linger-complete";
        let change_dir = workspace.join("openspec").join("changes").join(change_id);
        std::fs::create_dir_all(&change_dir).unwrap();
        std::fs::write(
            change_dir.join("tasks.md"),
            "## Implementation Tasks\n- [ ] one\n",
        )
        .unwrap();

        // Apply command: mark tasks complete, then sleep far beyond the test
        // budget. The grace period must terminate the sleeper.
        let apply_cmd = "sh -c 'printf \"## Implementation Tasks\\n- [x] one\\n\" > openspec/changes/{change_id}/tasks.md; echo applied; sleep 120'".to_string();

        let config = OrchestratorConfig {
            apply_command: Some(apply_cmd),
            ..Default::default()
        };
        let mut agent = AgentRunner::new(config.clone());
        let ai_runner = make_test_ai_runner();

        let start = std::time::Instant::now();
        let result = scoped_apply_completion_grace_secs_for_test(
            1,
            scoped_apply_completion_check_interval_ms_for_test(
                200,
                execute_apply_loop(
                    change_id,
                    workspace,
                    &config,
                    &mut agent,
                    VcsBackend::Auto,
                    None,
                    None,
                    &ApplyLoopHookContext::serial(0, 1, 1),
                    &NoOpEventHandler,
                    None,
                    &ai_runner,
                    |_line| async move {},
                ),
            ),
        )
        .await
        .expect("apply loop must finish without error despite lingering child");

        let elapsed = start.elapsed();
        assert!(
            elapsed < Duration::from_secs(20),
            "apply loop must exit within grace period + buffer, but took {:?}",
            elapsed
        );
        assert!(
            result.completed,
            "tasks-complete run must be reported as completed"
        );
        assert!(
            result.blocked_handoff.is_none(),
            "tasks-complete run must not report a blocked handoff"
        );
        assert_eq!(
            result.iterations, 1,
            "tasks-complete grace-terminated run should exit in a single iteration"
        );
    }

    #[cfg_attr(windows, ignore)]
    #[tokio::test]
    async fn test_execute_apply_loop_terminates_lingering_child_after_blocked_handoff() {
        let temp_dir = TempDir::new().unwrap();
        let workspace = temp_dir.path();
        let change_id = "linger-blocked";
        let change_dir = workspace.join("openspec").join("changes").join(change_id);
        std::fs::create_dir_all(&change_dir).unwrap();
        std::fs::write(
            change_dir.join("tasks.md"),
            "## Implementation Tasks\n- [ ] one\n",
        )
        .unwrap();

        // Apply command: write APPLY_BLOCKED marker then sleep. The apply-blocked
        // completion detector should terminate this lingering process during grace.
        let apply_cmd = "sh -c 'mkdir -p openspec/changes/{change_id}/APPLY_BLOCKED; printf \"# APPLY_BLOCKED\\n\\n- change_id: linger-blocked\\n- reason: test\\n\" > openspec/changes/{change_id}/APPLY_BLOCKED/marker.md; echo blocked; sleep 120'".to_string();

        let config = OrchestratorConfig {
            apply_command: Some(apply_cmd),
            ..Default::default()
        };
        let mut agent = AgentRunner::new(config.clone());
        let ai_runner = make_test_ai_runner();

        let start = std::time::Instant::now();
        let result = scoped_apply_completion_grace_secs_for_test(
            1,
            scoped_apply_completion_check_interval_ms_for_test(
                200,
                execute_apply_loop(
                    change_id,
                    workspace,
                    &config,
                    &mut agent,
                    VcsBackend::Auto,
                    None,
                    None,
                    &ApplyLoopHookContext::serial(0, 1, 1),
                    &NoOpEventHandler,
                    None,
                    &ai_runner,
                    |_line| async move {},
                ),
            ),
        )
        .await
        .expect("apply loop must finish without error despite lingering child");

        let elapsed = start.elapsed();
        assert!(
            elapsed < Duration::from_secs(20),
            "blocked-handoff apply loop must exit within grace period + buffer, but took {:?}",
            elapsed
        );
        assert!(
            !result.completed,
            "blocked-handoff grace-terminated run must not be reported as completed"
        );
        assert!(
            result.blocked_handoff.is_some(),
            "blocked-handoff grace-terminated run must expose blocker_path"
        );
        assert_eq!(
            result.iterations, 1,
            "blocked-handoff grace-terminated run should exit in a single iteration"
        );
    }
}