clt-rs 0.6.6

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

use anyhow::{Context, Result};
use chrono::{DateTime, Local, Utc};

use crate::{
    agent::{
        self, AGENT_STATE_DIR_ENV, AgentGitMode, AgentSessionControlAction, open_agent_store_at,
        with_agent_store_at,
    },
    application::{
        AGENT_PROJECT_ID_ENV, AGENT_RUN_TOKEN_ENV, AGENT_SESSION_CONTROL_POLL_MILLIS,
        AgentShutdownSignal, AgentTaskSelection, get_task_root_at,
    },
    managed_git::{
        AgentGitStartState, bind_agent_git_working_task_identity, configure_agent_git_identity,
        ensure_agent_git_index_preflight, ensure_agent_git_working_record,
        prepare_agent_git_start_state_for_run, verify_agent_git_start_state_unchanged,
    },
    platform::{
        agent_codex_path_env, agent_process_group_exists, configure_agent_child_command,
        interactive_child_exited_without_reaping, stop_agent_child_process,
    },
    scheduler::{
        agent_lease_renew_interval, agent_lease_timeout, agent_poll_interval, agent_run_timeout,
    },
    session_control::automated_session_control_action_for_generation,
    task::TaskStatus,
    worker::{
        attach_codex_session_to_active_task, automated_codex_session_to_resume,
        blocked_task_snapshots, print_agent_run_heartbeat, task_contents_for_status,
    },
};

#[cfg(test)]
use crate::application::AGENT_DEFAULT_POLL_INTERVAL_SECONDS;
#[cfg(unix)]
use crate::application::AGENT_SUPERVISOR_READY_TIMEOUT_SECONDS;
#[cfg(all(unix, test))]
use crate::application::TEST_AUTOMATED_SUPERVISOR_ENV;
#[cfg(not(test))]
use crate::worker::validate_agent_worker_token;
#[cfg(unix)]
use std::os::unix::process::CommandExt;

#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) struct AutomatedAgentChildContext {
    pub(super) project_id: i64,
    pub(super) run_token: String,
}

#[cfg(not(test))]
pub(super) fn automated_agent_child_context() -> Result<Option<AutomatedAgentChildContext>> {
    automated_agent_child_context_from_values(
        std::env::var_os(AGENT_PROJECT_ID_ENV),
        std::env::var_os(AGENT_RUN_TOKEN_ENV),
    )
}

#[cfg(test)]
pub(super) fn automated_agent_child_context() -> Result<Option<AutomatedAgentChildContext>> {
    Ok(None)
}

#[cfg(not(test))]
pub(super) fn automated_agent_child_context_from_values(
    project_id: Option<OsString>,
    run_token: Option<OsString>,
) -> Result<Option<AutomatedAgentChildContext>> {
    let (project_id, run_token) = match (project_id, run_token) {
        (None, None) => return Ok(None),
        (Some(project_id), Some(run_token)) => (project_id, run_token),
        _ => {
            anyhow::bail!(
                "Incomplete automated agent context: {AGENT_PROJECT_ID_ENV} and {AGENT_RUN_TOKEN_ENV} must be set together"
            )
        }
    };
    let project_id = project_id
        .to_str()
        .context("Automated agent project ID is not valid UTF-8")?
        .parse::<i64>()
        .with_context(|| format!("{AGENT_PROJECT_ID_ENV} must be a positive integer"))?;
    if project_id <= 0 {
        anyhow::bail!("{AGENT_PROJECT_ID_ENV} must be a positive integer");
    }
    let run_token = run_token
        .into_string()
        .map_err(|_| anyhow::anyhow!("{AGENT_RUN_TOKEN_ENV} is not valid UTF-8"))?;
    validate_agent_worker_token(&run_token)
        .with_context(|| format!("Invalid {AGENT_RUN_TOKEN_ENV}"))?;

    Ok(Some(AutomatedAgentChildContext {
        project_id,
        run_token,
    }))
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) struct AgentRunResult {
    pub(super) status: &'static str,
    pub(super) exit_code: Option<i64>,
    pub(super) log_dir: PathBuf,
    pub(super) stdout_path: PathBuf,
    pub(super) stderr_path: PathBuf,
    pub(super) summary: String,
    pub(super) codex_session_id: Option<String>,
    pub(super) session_run_token: Option<String>,
    pub(super) control_action: Option<AgentSessionControlAction>,
}

#[derive(Debug)]
pub(super) struct AgentChildTerminationUnproven(pub(super) String);

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

#[cfg(test)]
pub(crate) mod tests;

impl std::error::Error for AgentChildTerminationUnproven {}

pub(super) fn unproven_agent_child_termination(
    error: anyhow::Error,
    context: &str,
) -> anyhow::Error {
    anyhow::Error::new(AgentChildTerminationUnproven(format!(
        "{context}: {error:#}"
    )))
}

pub(super) trait AgentRunner: Send + Sync {
    fn run_project(&self, request: AgentRunRequest<'_>) -> Result<AgentRunResult>;
}

pub(super) struct AgentRunRequest<'a> {
    pub(super) project: &'a agent::AgentProject,
    pub(super) task_selection: AgentTaskSelection,
    pub(super) resume_session_id: Option<&'a str>,
    pub(super) lease_holder: &'a str,
    pub(super) run_token: Option<&'a str>,
    pub(super) shutdown: &'a AgentShutdownSignal,
}

pub(super) struct AgentSupervisionOutcomeRequest {
    pub(super) wait_result: AgentProcessWait,
    pub(super) requested_control: Option<AgentSessionControlAction>,
    pub(super) reported_no_tasks: bool,
    pub(super) timeout: Duration,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) struct AgentSupervisionOutcome {
    pub(super) status: &'static str,
    pub(super) exit_code: Option<i64>,
    pub(super) summary: String,
    pub(super) stderr_note: Option<String>,
}

pub(super) struct AgentRunnerLaunchRequest<'a> {
    pub(super) runner: &'a CodexAgentRunner,
    pub(super) store: &'a agent::TursoAgentStore,
    pub(super) project: &'a agent::AgentProject,
    pub(super) task_selection: AgentTaskSelection,
    pub(super) resume_session_id: Option<&'a str>,
    pub(super) lease_holder: &'a str,
    pub(super) run_file_stem: &'a str,
    pub(super) git_start_state: Option<&'a AgentGitStartState>,
}

pub(super) struct AgentRunnerLaunchedProcess {
    pub(super) child: Child,
    pub(super) child_pid: u32,
    pub(super) log_dir: PathBuf,
    pub(super) stdout_path: PathBuf,
    pub(super) stderr_path: PathBuf,
    pub(super) configured_session_id: Option<String>,
    #[cfg(unix)]
    pub(super) supervisor_control: Option<std::process::ChildStdin>,
    #[cfg(unix)]
    pub(super) supervisor_proof: BufReader<std::process::ChildStdout>,
}

pub(super) enum AgentRunnerLaunchResult {
    Launched(AgentRunnerLaunchedProcess),
    Failed(AgentRunResult),
}

pub(super) fn classify_agent_supervision_stage(
    request: AgentSupervisionOutcomeRequest,
) -> AgentSupervisionOutcome {
    let AgentSupervisionOutcomeRequest {
        wait_result,
        requested_control,
        reported_no_tasks,
        timeout,
    } = request;
    if let Some(action) = requested_control {
        let exit_code = match wait_result {
            AgentProcessWait::Exited(status) => status.code().map(i64::from),
            AgentProcessWait::TimedOut(status) | AgentProcessWait::Interrupted(status) => {
                status.and_then(|status| status.code().map(i64::from))
            }
        };
        return match action {
            AgentSessionControlAction::Stop => AgentSupervisionOutcome {
                status: "stopped",
                exit_code,
                summary: "Codex task session stopped and remains resumable.".to_string(),
                stderr_note: Some("Codex stopped by task-session control.".to_string()),
            },
            AgentSessionControlAction::Interrupt => AgentSupervisionOutcome {
                status: "handoff",
                exit_code,
                summary: "Codex task session is ready for interactive handoff.".to_string(),
                stderr_note: Some(
                    "Codex interrupted for an interactive session handoff.".to_string(),
                ),
            },
        };
    }

    match wait_result {
        AgentProcessWait::Exited(exit_status) => {
            let exit_code = exit_status.code().map(i64::from);
            if reported_no_tasks {
                AgentSupervisionOutcome {
                    status: "idle",
                    exit_code,
                    summary: "Codex reported no available tasks.".to_string(),
                    stderr_note: None,
                }
            } else if exit_status.success() {
                AgentSupervisionOutcome {
                    status: "success",
                    exit_code,
                    summary: "Codex run completed successfully.".to_string(),
                    stderr_note: None,
                }
            } else {
                AgentSupervisionOutcome {
                    status: "failure",
                    exit_code,
                    summary: format!("Codex exited with status {exit_status}."),
                    stderr_note: None,
                }
            }
        }
        AgentProcessWait::TimedOut(exit_status) => {
            let summary = format!("Codex timed out after {} seconds.", timeout.as_secs());
            AgentSupervisionOutcome {
                status: "timeout",
                exit_code: exit_status.and_then(|status| status.code().map(i64::from)),
                stderr_note: Some(summary.clone()),
                summary,
            }
        }
        AgentProcessWait::Interrupted(exit_status) => AgentSupervisionOutcome {
            status: "interrupted",
            exit_code: exit_status.and_then(|status| status.code().map(i64::from)),
            summary: "Codex stopped because the agent is shutting down.".to_string(),
            stderr_note: Some("Codex stopped because the agent is shutting down.".to_string()),
        },
    }
}

pub(super) fn launch_agent_runner_stage(
    request: AgentRunnerLaunchRequest<'_>,
) -> Result<AgentRunnerLaunchResult> {
    let AgentRunnerLaunchRequest {
        runner,
        store,
        project,
        task_selection,
        resume_session_id,
        lease_holder,
        run_file_stem,
        git_start_state,
    } = request;
    let log_dir = agent_project_run_log_dir(&runner.state_dir, project)?;
    fs::create_dir_all(&log_dir)
        .with_context(|| format!("Failed to create agent run log directory {:?}", log_dir))?;
    let stdout_path = log_dir.join(format!("{run_file_stem}.out"));
    let stderr_path = log_dir.join(format!("{run_file_stem}.err"));
    let stdout_file = fs::File::create(&stdout_path)
        .with_context(|| format!("Failed to create stdout log {:?}", stdout_path))?;
    fs::File::create(&stderr_path)
        .with_context(|| format!("Failed to create stderr log {:?}", stderr_path))?;
    let stderr_file = fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&stderr_path)
        .with_context(|| format!("Failed to reopen stderr log {:?}", stderr_path))?;

    let mut command = Command::new(&runner.command);
    command
        .arg("--sandbox")
        .arg("danger-full-access")
        .arg("--ask-for-approval")
        .arg("never")
        .arg("--enable")
        .arg("goals");
    let model_target = if let Some(model_id) = project.codex_model.as_ref() {
        agent::AgentModelDefaults {
            provider_id: Some(
                project
                    .codex_provider
                    .clone()
                    .unwrap_or_else(|| "openai".to_string()),
            ),
            model_id: Some(model_id.clone()),
        }
    } else {
        store.resolve_model_target_blocking(project)?
    };
    if let (Some(provider), Some(model)) = (
        model_target.provider_id.as_deref(),
        model_target.model_id.as_deref(),
    ) {
        command
            .arg("--config")
            .arg(format!("model_provider={provider:?}"));
        command.arg("--model").arg(model);
    }
    let model_reasoning_effort = if project.codex_reasoning_effort.is_none() {
        match (
            model_target.provider_id.as_deref(),
            model_target.model_id.as_deref(),
        ) {
            (Some(provider), Some(model)) => {
                store.model_target_reasoning_blocking(provider, model)?
            }
            _ => None,
        }
    } else {
        None
    };
    if let Some(reasoning_effort) = project
        .codex_reasoning_effort
        .as_deref()
        .or(model_reasoning_effort.as_deref())
    {
        command
            .arg("--config")
            .arg(format!("model_reasoning_effort=\"{reasoning_effort}\""));
    }
    if project.codex_fast_enabled {
        command
            .arg("--enable")
            .arg("fast_mode")
            .arg("--config")
            .arg("service_tier=\"fast\"");
    } else {
        command.arg("--disable").arg("fast_mode");
    }
    let configured_session_id = configure_automated_codex_subcommand(
        &mut command,
        project,
        task_selection,
        resume_session_id,
    )?;
    #[cfg(not(unix))]
    if let Some(session_id) = configured_session_id.as_deref() {
        anyhow::bail!(
            "Automated Codex session resume {session_id} is unsupported on this platform because CLT cannot register it before launch"
        );
    }
    command.current_dir(&project.path);
    configure_agent_git_identity(&mut command, project.git_mode);
    configure_automated_agent_child_context(
        &mut command,
        &runner.state_dir,
        project.id,
        run_file_stem,
    );

    let persist_git_launch_state = || -> Result<bool> {
        let Some(git_start_state) = git_start_state else {
            return Ok(false);
        };
        verify_agent_git_start_state_unchanged(&project.path, project.git_mode, git_start_state)?;
        store.record_git_launch_state_blocking(
            project.id,
            run_file_stem,
            project.git_mode,
            git_start_state,
            &agent_timestamp(),
        )
    };

    #[cfg(unix)]
    let spawn_result: Result<AgentRunnerLaunchedProcess> = (|| {
        drop(stdout_file);
        let AutomatedSupervisorChild {
            mut process,
            control,
            child_pid,
            mut proof,
        } = spawn_automated_session_supervisor(
            &command,
            AutomatedSupervisorSpec {
                state_dir: &runner.state_dir,
                project_id: project.id,
                run_token: run_file_stem,
                lease_holder,
                stdout_path: &stdout_path,
                stderr_path: &stderr_path,
            },
            stderr_file,
        )?;
        if let Err(error) = persist_git_launch_state() {
            drop(control);
            wait_for_automated_supervisor_reaped(&mut process, &mut proof).with_context(|| {
                format!(
                    "Persisting the prelaunch Git state failed ({error:#}), and the automated supervisor did not prove Codex stopped"
                )
            })?;
            return Err(error)
                .context("Failed to persist the prelaunch Git state behind the Codex launch gate");
        }
        Ok(AgentRunnerLaunchedProcess {
            child: process,
            child_pid,
            log_dir: log_dir.clone(),
            stdout_path: stdout_path.clone(),
            stderr_path: stderr_path.clone(),
            configured_session_id: configured_session_id.clone(),
            supervisor_control: Some(control),
            supervisor_proof: proof,
        })
    })();
    #[cfg(not(unix))]
    let spawn_result: Result<AgentRunnerLaunchedProcess> = (|| {
        let git_launch_state_was_created = persist_git_launch_state()?;
        command
            .stdout(Stdio::from(stdout_file))
            .stderr(Stdio::from(stderr_file));
        configure_agent_child_command(&mut command);
        match command.spawn() {
            Ok(child) => Ok(AgentRunnerLaunchedProcess {
                child_pid: child.id(),
                child,
                log_dir: log_dir.clone(),
                stdout_path: stdout_path.clone(),
                stderr_path: stderr_path.clone(),
                configured_session_id: configured_session_id.clone(),
            }),
            Err(error) => {
                if git_launch_state_was_created
                    && !store.delete_git_launch_state_blocking(project.id, run_file_stem)?
                {
                    anyhow::bail!(
                        "The Codex process failed to spawn and its exact Git launch boundary could not be deleted"
                    );
                }
                Err(error.into())
            }
        }
    })();

    match spawn_result {
        Ok(process) => Ok(AgentRunnerLaunchResult::Launched(process)),
        Err(error) => {
            let summary = format!(
                "Failed to start Codex command {} in {}: {error}",
                runner.command.display(),
                project.path.display()
            );
            append_agent_log_line(&stderr_path, &summary)?;
            Ok(AgentRunnerLaunchResult::Failed(AgentRunResult {
                status: "failure",
                exit_code: None,
                log_dir,
                stdout_path,
                stderr_path,
                summary,
                codex_session_id: configured_session_id,
                session_run_token: None,
                control_action: None,
            }))
        }
    }
}

pub(super) struct CodexAgentRunner {
    pub(super) state_dir: PathBuf,
    pub(super) timeout: Duration,
    pub(super) heartbeat_interval: Duration,
    pub(super) lease_timeout: Duration,
    pub(super) lease_renew_interval: Duration,
    pub(super) command: PathBuf,
    pub(super) worker_token: Option<String>,
}

pub(super) const AGENT_NO_TASKS_LEFT_MARKER: &str = "NO_TASKS_LEFT";
pub(super) const CLT_TASK_MANAGEMENT_SKILL_NAME: &str = "clt-task-management";
pub(super) const GIT_COMMIT_SKILL_NAME: &str = "git-commit";
pub(super) const EMBEDDED_CLT_TASK_MANAGEMENT_SKILL: &str =
    include_str!("../skills/clt-task-management/SKILL.md");
pub(super) const EMBEDDED_GIT_COMMIT_SKILL: &str = include_str!("../skills/git-commit/SKILL.md");

pub(super) const AGENT_CODEX_PROMPT_BASE: &str = r#"You are working in this repo.

Use the existing task-management CLI tooling: clt.

Your job for this run:

1. Inspect the task board using the task CLI.
2. Pick the next available unblocked TODO / ready task.
3. If there are no available tasks, say exactly: NO_TASKS_LEFT
4. If there is a task:
   - inspect its full content and applicable repository instructions before starting work
   - follow the applicable mode-specific pre-task Git boundary; when a Git appendix says CLT already prepared and froze the checkout, do not sync or switch it yourself
   - move it to doing only after that preparation
   - if the first non-whitespace token is exactly `/goal`, treat that as an explicit Goal mode request: remove that token, trim the remaining task content, create a persistent goal from the result without including `/goal` in the goal objective, and then work toward it
   - if `/goal` has no non-empty objective after it, add a concise `BLOCKED YYYY-MM-DD:` note explaining that the goal objective is missing and stop
   - do not create a goal when `/goal` appears anywhere except at the start of the task content
   - complete that task
   - run the relevant checks/tests
   - update the task using the task CLI
   - mark it done if completed
   - include a concise note with what changed and what commands/tests ran
5. Stop after that one task.
6. Do not start another task.
7. Exit when finished.

Safety rules:
- Do not overwrite unrelated user changes.
- Before making edits, inspect the current repo state.
- A dirty worktree is expected when people, interactive sessions, or independent workers share a repository; it is not a blocker by itself.
- Treat the initial status and diff as the baseline, preserve pre-existing changes, and continue with non-conflicting work.
- Another change in the same file is not automatically a blocker. Re-read the affected area and keep both changes when the intended combined result is clear.
- Stop for Git overlap only when the required edits genuinely conflict and the correct combined result cannot be determined safely.
- During normal TODO selection, skip tasks whose latest dated state note is `BLOCKED YYYY-MM-DD:`.
- Inspect task details when needed; a folder-backed task's list summary may not show its blocker notes.
- Classify failed checks before deciding the task is blocked. If the implementation satisfies the task's acceptance criteria and its relevant checks pass, an independently evidenced pre-existing or environment-only failure may be recorded as a separate follow-up. Reproduce it on the frozen starting revision in an isolated directory without switching or resetting this checkout; record the revision, commands, matching failure, and the remaining work. Ordinary code fixes are actionable tasks, not blockers. Use BLOCKED only when an unavailable dependency, permission, or input prevents starting that follow-up. If independence or acceptance remains uncertain, keep the original task blocked.
- For that independent failure, run `clt list doing`, then `clt follow-up doing <index> "Follow-up description" --evidence "Failure evidence, baseline comparison, and remaining work"`. This queues one linked Todo task for a fresh run without starting another session or changing the parent's identity. Add `--blocked "Unavailable dependency or input and what restores it"` only for a real obstacle; that follow-up stays blocked in Doing for later recovery. Do not copy the parent's codex marker onto it. Do not work on the follow-up in this run. Record its reference and the validation evidence in the parent's COMPLETED note and finish the original task. Report the original task as completed with a queued follow-up when appropriate; creating follow-up work is not a run failure.
- If the implementation is incomplete, a task-relevant check fails, or the task cannot be completed safely, update the original task with a concise `BLOCKED YYYY-MM-DD:` note instead of forcing it. A follow-up must not hide unfinished acceptance criteria or an unproven regression.
"#;
pub(super) const AGENT_GIT_COMMIT_PROMPT_APPENDIX: &str = r#"

Git commit:
- This finalization contract is authoritative for the automated run and overrides older installed skill guidance when they differ.
- Before this process was released, CLT completed the scheduler-owned startup preparation, using a safe fast-forward-only sync only when no older WORKING journal required preserving its history, then froze HEAD, the worktree baseline, branch, and upstream state and persisted that launch record. The selected task must already be committed exactly once on the board. Do not pull, fetch or otherwise synchronize, merge, rebase, switch branches, reset history, or reconfigure Git after release.
- Move the selected Todo task to Doing before implementation. CLT rechecks the frozen launch record and binds it to the session's durable WORKING journal at that transition; do not edit or commit implementation first.
- After completing and verifying the task, run all formatting, lint, signing, and hook checks that can mutate files before sealing. Add its dated COMPLETED note. Stage the implementation, any linked follow-up created for an independent failure, and the active Doing task, including its terminal `codex:<session-id>` marker, then inspect the staged diff.
- Run `clt done` only after that staged diff is complete. CLT seals its durable task manifest and makes the board move provisionally; it is not terminal completion by itself.
- Stage only the resulting board transition, inspect the complete staged diff again, then use the $git-commit skill to create exactly one normal git commit containing the sealed implementation, completion note, and complete task-board move, plus any linked follow-up.
- Give that commit one exact final message paragraph: `CLT-Task: codex:<session-id>`.
- If a commit hook changes files or fails after the seal, fix and stage the complete corrected payload, run `clt done done <index>` to reseal that provisional Done entry, inspect it, and retry the one commit.
- Pre-existing unstaged changes do not prevent a commit. Stage only this task's paths or hunks, verify the staged diff, and leave unrelated changes untouched.
- A Todo or other task-board edit added during the run may also remain unstaged. Preserve it and stage only the selected task's board transition and its explicitly linked follow-up; CLT's exact staged-tree proof keeps the concurrent edit outside the sealed commit.
- Do not require the worktree to be clean before committing.
- The scheduler supplies the isolated Git identity `CLT Agent <clt-agent@localhost>` for clear automated-commit attribution; do not change Git configuration.
- Do not exit merely because the task appears in Done. Inspect the created commit and keep working until CLT can prove the task-specific commit. If this is a resumed finalization, inspect existing Git state before committing and never duplicate an already-created task commit.
- Do not commit when there are no tasks left, the original task is still blocked, task-relevant checks fail, or the work cannot be completed safely. A proven independent failure recorded with `clt follow-up` does not prevent committing the verified original task; include that follow-up in the same sealed commit.
"#;
pub(super) const AGENT_GIT_PUSH_PROMPT_APPENDIX: &str = r#"

Git push:
- This project is configured for commit and push. CLT already froze the attached branch's single intended push URL and upstream before release.
- Do not run `git push`. After CLT proves the sealed local commit, its finalizer sends exactly that frozen OID to exactly the frozen URL and merge ref with an explicit non-force refspec, then independently proves the remote result.
- Exit after creating and inspecting the verified commit. The task remains PUSH-PENDING until CLT's bounded push and remote proof succeed.
- If the remote advanced and rejects publication, CLT leaves the task PUSH-PENDING for a later scheduler retry or explicit external recovery; do not pull, fetch, merge, rebase, amend, switch branches, or change the destination.
- Never force-push.
"#;
pub(super) const AGENT_RESUME_DOING_PROMPT_APPENDIX: &str = r#"

Interrupted task recovery:
- A previous agent run was interrupted after moving a task to doing.
- Resume and finish exactly one existing doing task.
- Do not pick or move a TODO task; this recovery instruction replaces steps 2-4 above.
- If there is no doing task to resume, say exactly: NO_TASKS_LEFT
"#;
pub(super) const AGENT_RECOVER_BLOCKED_PROMPT_APPENDIX: &str = r#"

Blocked-task monitor:
- The scheduler found at least one blocked task in todo or doing and is reconsidering blockers before starting fresh Todo work.
- Review the existing blocker notes and choose exactly one blocked task from todo or doing.
- Re-evaluate whether the recorded blocking conditions still exist instead of assuming the task remains blocked.
- If the selected task is in todo, move it to doing before working on it.
- Try to resolve that task's blocker and finish the task, including the relevant checks.
- Update the existing task; do not create a replacement task. The independent-failure follow-up procedure above is allowed only when this original task satisfies its acceptance criteria.
- If the task is completed, add its completion note and move it to done.
- If its blocker is resolved but the task should be retried through the normal workflow, add a newer `UNBLOCKED YYYY-MM-DD:` note and move that same task back to todo.
- If it still cannot be completed safely, update its blocked note with what you tried and what is still needed, and leave it in doing.
- Do not select backlog work. Stop after handling that one blocked task.
- These recovery instructions replace steps 2-4 above.
"#;
pub(super) const AGENT_RESUME_SESSION_PROMPT_APPENDIX: &str = r#"

Interactive handoff recovery:
- Resume the exact task and Codex session that CLT handed back from interactive mode.
- If CLT reports this task as FINALIZING, inspect the existing commit first and continue only the first unproven local step. A PUSH-PENDING task is scheduler-owned and must not resume Codex merely to publish. Never create a duplicate completion commit or move a successfully committed task back to Doing.
- Inspect the linked task, current project state, and any interactive instructions, then continue from the next unfinished substantive step in the conversation context.
- A prior assistant plan, progress message, draft, summary, or claimed completion is not proof that requested work finished.
- If the linked task or interactive instructions request project, file, code, configuration, or task-board changes, do not mark the task done until those durable changes actually exist and the relevant checks pass.
- For a response-only task that does not request durable changes, a completed response may be the deliverable.
- If the task is already complete, verify its recorded completion and any requested durable output, then exit without selecting another task.
- Otherwise finish or update that same task using the normal task workflow and relevant checks.
- Do not select another Todo or Backlog task. Stop after handling this one session.
- These recovery instructions replace steps 2-4 above.
"#;

pub(super) fn agent_codex_command() -> PathBuf {
    agent_codex_path_env().unwrap_or_else(|| PathBuf::from("codex"))
}

pub(super) fn configure_automated_codex_subcommand(
    command: &mut Command,
    project: &agent::AgentProject,
    task_selection: AgentTaskSelection,
    resume_session_id: Option<&str>,
) -> Result<Option<String>> {
    let session_id = match resume_session_id {
        Some(session_id) => Some(session_id.to_string()),
        None => automated_codex_session_to_resume(&project.path, task_selection)?,
    };
    command.arg("exec");
    if let Some(session_id) = session_id.as_deref() {
        command
            .arg("resume")
            .arg("--skip-git-repo-check")
            .arg(session_id);
    } else {
        command
            .arg("--skip-git-repo-check")
            .arg("-C")
            .arg(&project.path);
    }
    command.arg(agent_codex_prompt(project, task_selection));
    Ok(session_id)
}

pub(super) fn automated_exec_gate_is_released(reader: &mut impl Read) -> io::Result<bool> {
    let mut release = [0_u8; 1];
    loop {
        return match reader.read(&mut release) {
            Ok(0) => Ok(false),
            Ok(_) => Ok(release[0] == b'x'),
            Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
            Err(error) => Err(error),
        };
    }
}

#[cfg(unix)]
pub(super) fn run_automated_exec_gate(program: &Path, arguments: &[OsString]) -> Result<()> {
    let stdin = io::stdin();
    let mut reader = stdin.lock();
    if !automated_exec_gate_is_released(&mut reader)
        .context("Failed to read automated Codex launch gate")?
    {
        return Ok(());
    }

    let mut command = Command::new(program);
    command.args(arguments);
    let error = command.exec();
    Err(error).with_context(|| format!("Failed to exec gated Codex command {}", program.display()))
}

#[cfg(not(unix))]
pub(super) fn run_automated_exec_gate(_program: &Path, _arguments: &[OsString]) -> Result<()> {
    anyhow::bail!("The automated Codex exec gate is only supported on Unix")
}

#[cfg(all(unix, not(test)))]
pub(super) fn automated_exec_gate_command(target: &Command) -> Result<Command> {
    let executable = std::env::current_exe()
        .context("Failed to resolve the CLT executable for the automated Codex launch gate")?;
    let mut gate = Command::new(executable);
    gate.arg("--local")
        .arg("agent")
        .arg("automated-exec-gate")
        .arg("--")
        .arg(target.get_program())
        .args(target.get_args());
    configure_automated_exec_gate_inheritance(&mut gate, target);
    Ok(gate)
}

#[cfg(all(unix, test))]
pub(super) fn automated_exec_gate_command(target: &Command) -> Result<Command> {
    // Unit-test executables are owned by libtest rather than this binary's CLI.
    // A POSIX shell supplies the same read/EOF/exec behavior for runner tests.
    let mut gate = Command::new("/bin/sh");
    gate.arg("-c")
        .arg("gate=$(/bin/dd bs=1 count=1 2>/dev/null)\n[ \"$gate\" = x ] || exit 0\nexec \"$@\"")
        .arg("clt-automated-exec-gate")
        .arg(target.get_program())
        .args(target.get_args());
    configure_automated_exec_gate_inheritance(&mut gate, target);
    Ok(gate)
}

#[cfg(unix)]
pub(super) fn configure_automated_exec_gate_inheritance(gate: &mut Command, target: &Command) {
    if let Some(current_dir) = target.get_current_dir() {
        gate.current_dir(current_dir);
    }
    for (key, value) in target.get_envs() {
        match value {
            Some(value) => {
                gate.env(key, value);
            }
            None => {
                gate.env_remove(key);
            }
        }
    }
    // Stdio::piped gives the helper only the read end. The parent owns the only
    // writer, so a parent crash before registration is observed as EOF.
    gate.stdin(Stdio::piped());
}

#[cfg(unix)]
pub(super) const AUTOMATED_SUPERVISOR_CONNECTED: u64 = 0;
#[cfg(unix)]
pub(super) const AUTOMATED_SUPERVISOR_STOP_REQUESTED: u64 = 1;
#[cfg(unix)]
pub(super) const AUTOMATED_SUPERVISOR_PARENT_DISCONNECTED: u64 = 2;
#[cfg(unix)]
pub(super) const AUTOMATED_SUPERVISOR_READY_PREFIX: &str = "clt-automated-child-pid:";
#[cfg(unix)]
pub(super) const AUTOMATED_SUPERVISOR_REAPED_PREFIX: &str = "clt-automated-child-reaped:";

#[cfg(unix)]
pub(super) struct AutomatedSupervisorChild {
    pub(super) process: Child,
    pub(super) control: std::process::ChildStdin,
    pub(super) child_pid: u32,
    pub(super) proof: BufReader<std::process::ChildStdout>,
}

#[cfg(unix)]
#[derive(Clone, Copy)]
pub(super) struct AutomatedSupervisorSpec<'a> {
    pub(super) state_dir: &'a Path,
    pub(super) project_id: i64,
    pub(super) run_token: &'a str,
    pub(super) lease_holder: &'a str,
    pub(super) stdout_path: &'a Path,
    pub(super) stderr_path: &'a Path,
}

#[cfg(unix)]
pub(super) struct AutomatedSupervisorWaitHandles<'a> {
    pub(super) process: &'a mut Child,
    pub(super) control: &'a mut Option<std::process::ChildStdin>,
    pub(super) proof: &'a mut BufReader<std::process::ChildStdout>,
}

#[cfg(all(unix, not(test)))]
pub(super) fn automated_session_supervisor_command(
    target: &Command,
    spec: AutomatedSupervisorSpec<'_>,
) -> Result<Command> {
    let executable = std::env::current_exe()
        .context("Failed to resolve the CLT automated-session supervisor executable")?;
    let mut supervisor = Command::new(executable);
    supervisor
        .arg("--local")
        .arg("agent")
        .arg("automated-session-supervisor")
        .arg("--state-dir")
        .arg(spec.state_dir)
        .arg("--project-id")
        .arg(spec.project_id.to_string())
        .arg("--run-token")
        .arg(spec.run_token)
        .arg("--lease-holder")
        .arg(spec.lease_holder)
        .arg("--stdout-path")
        .arg(spec.stdout_path)
        .arg("--stderr-path")
        .arg(spec.stderr_path)
        .arg("--")
        .arg(target.get_program())
        .args(target.get_args());
    configure_automated_exec_gate_inheritance(&mut supervisor, target);
    Ok(supervisor)
}

#[cfg(all(unix, test))]
pub(super) fn automated_session_supervisor_command(
    target: &Command,
    spec: AutomatedSupervisorSpec<'_>,
) -> Result<Command> {
    // A test binary is driven by libtest rather than the Clap entry point. Run
    // one exact helper test and pass the real supervisor arguments through its
    // environment so runner tests exercise the same ownership loop.
    let executable = std::env::current_exe()
        .context("Failed to resolve the CLT automated-session test supervisor")?;
    let mut supervisor = Command::new(executable);
    supervisor
        .arg("--exact")
        .arg("runner::tests::automated_session_supervisor_process_entry")
        .arg("--nocapture")
        .env(TEST_AUTOMATED_SUPERVISOR_ENV, "1")
        .env("CLT_TEST_SUPERVISOR_STATE_DIR", spec.state_dir)
        .env(
            "CLT_TEST_SUPERVISOR_PROJECT_ID",
            spec.project_id.to_string(),
        )
        .env("CLT_TEST_SUPERVISOR_RUN_TOKEN", spec.run_token)
        .env("CLT_TEST_SUPERVISOR_LEASE_HOLDER", spec.lease_holder)
        .env("CLT_TEST_SUPERVISOR_STDOUT_PATH", spec.stdout_path)
        .env("CLT_TEST_SUPERVISOR_STDERR_PATH", spec.stderr_path)
        .env("CLT_TEST_SUPERVISOR_PROGRAM", target.get_program())
        .env(
            "CLT_TEST_SUPERVISOR_ARGUMENT_COUNT",
            target.get_args().count().to_string(),
        );
    for (index, argument) in target.get_args().enumerate() {
        supervisor.env(format!("CLT_TEST_SUPERVISOR_ARGUMENT_{index}"), argument);
    }
    configure_automated_exec_gate_inheritance(&mut supervisor, target);
    Ok(supervisor)
}

#[cfg(unix)]
pub(super) fn spawn_automated_session_supervisor(
    target: &Command,
    spec: AutomatedSupervisorSpec<'_>,
    supervisor_stderr: fs::File,
) -> Result<AutomatedSupervisorChild> {
    let mut command = automated_session_supervisor_command(target, spec)?;
    command
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::from(supervisor_stderr));
    let mut process = command
        .spawn()
        .context("Failed to start the automated Codex session supervisor")?;
    let control = process
        .stdin
        .take()
        .context("Automated Codex supervisor did not open its control pipe")?;
    let readiness = process
        .stdout
        .take()
        .context("Automated Codex supervisor did not open its readiness pipe")?;
    let (sender, receiver) = std::sync::mpsc::sync_channel(1);
    thread::Builder::new()
        .name(format!(
            "clt-automated-supervisor-ready-{}",
            spec.project_id
        ))
        .spawn(move || {
            let mut reader = BufReader::new(readiness);
            let result = loop {
                let mut line = String::new();
                match reader.read_line(&mut line) {
                    Ok(0) => {
                        break Err(anyhow::anyhow!(
                            "Automated Codex supervisor closed before reporting its child PID"
                        ));
                    }
                    Ok(_) => {
                        if let Some(pid) = line
                            .trim_end()
                            .strip_prefix(AUTOMATED_SUPERVISOR_READY_PREFIX)
                        {
                            break pid
                                .parse::<u32>()
                                .context("Automated supervisor reported an invalid Codex PID")
                                .map(|child_pid| (child_pid, reader));
                        }
                    }
                    Err(error) => {
                        break Err(error).context(
                            "Failed to read the automated Codex supervisor readiness pipe",
                        );
                    }
                }
            };
            let _ = sender.send(result);
        })
        .context("Failed to start the automated supervisor readiness reader")?;

    match receiver.recv_timeout(Duration::from_secs(AGENT_SUPERVISOR_READY_TIMEOUT_SECONDS)) {
        Ok(Ok((child_pid, proof))) => Ok(AutomatedSupervisorChild {
            process,
            control,
            child_pid,
            proof,
        }),
        Ok(Err(error)) => {
            drop(control);
            let _ = process.wait();
            Err(error)
        }
        Err(error) => {
            drop(control);
            let _ = process.wait();
            Err(error).context("Timed out waiting for the automated Codex supervisor to start")
        }
    }
}

#[cfg(unix)]
pub(super) fn run_automated_session_supervisor(
    spec: AutomatedSupervisorSpec<'_>,
    program: &Path,
    arguments: &[OsString],
) -> Result<i32> {
    let stdout_file = fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(spec.stdout_path)
        .with_context(|| {
            format!(
                "Failed to open supervised Codex stdout {:?}",
                spec.stdout_path
            )
        })?;
    let stderr_file = fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(spec.stderr_path)
        .with_context(|| {
            format!(
                "Failed to open supervised Codex stderr {:?}",
                spec.stderr_path
            )
        })?;
    let mut target = Command::new(program);
    target.args(arguments);
    let mut command = automated_exec_gate_command(&target)?;
    command
        .stdout(Stdio::from(stdout_file))
        .stderr(Stdio::from(stderr_file));
    configure_agent_child_command(&mut command);
    let mut child = command.spawn().with_context(|| {
        format!(
            "Failed to start supervised automated Codex command {}",
            program.display()
        )
    })?;
    let child_pid = child.id();
    let Some(mut launch_gate) = child.stdin.take() else {
        let _ = stop_supervised_automated_child_until_reaped(
            &mut child,
            "its launch-gate pipe was unavailable",
        );
        anyhow::bail!("Supervised automated Codex launch gate did not open its release pipe");
    };

    let readiness_result = (|| -> Result<()> {
        let mut output = stdout().lock();
        writeln!(output, "{AUTOMATED_SUPERVISOR_READY_PREFIX}{child_pid}")?;
        output.flush()?;
        Ok(())
    })();
    if let Err(error) = readiness_result {
        drop(launch_gate);
        let _status = stop_supervised_automated_child_until_reaped(
            &mut child,
            "its parent disconnected before readiness",
        );
        finalize_disconnected_automated_supervisor(
            spec.state_dir,
            spec.project_id,
            child_pid,
            spec.run_token,
            spec.lease_holder,
        );
        return Err(error).context("Failed to report the supervised Codex child PID");
    }

    let mut parent_input = io::stdin().lock();
    let parent_released = match automated_exec_gate_is_released(&mut parent_input) {
        Ok(released) => released,
        Err(error) => {
            drop(parent_input);
            drop(launch_gate);
            let status = stop_supervised_automated_child_until_reaped(
                &mut child,
                "its launch-release pipe failed",
            );
            finalize_disconnected_automated_supervisor(
                spec.state_dir,
                spec.project_id,
                child_pid,
                spec.run_token,
                spec.lease_holder,
            );
            eprintln!("Failed to read automated supervisor launch release: {error}");
            return report_automated_supervisor_reaped(status);
        }
    };
    if !parent_released {
        drop(parent_input);
        drop(launch_gate);
        let status = stop_supervised_automated_child_until_reaped(
            &mut child,
            "its parent disconnected before launch",
        );
        finalize_disconnected_automated_supervisor(
            spec.state_dir,
            spec.project_id,
            child_pid,
            spec.run_token,
            spec.lease_holder,
        );
        return report_automated_supervisor_reaped(status);
    }
    drop(parent_input);
    if let Err(error) = launch_gate
        .write_all(b"x")
        .and_then(|_| launch_gate.flush())
    {
        drop(launch_gate);
        let _ = stop_supervised_automated_child_until_reaped(
            &mut child,
            "its inner launch gate could not be released",
        );
        eprintln!("Failed to release supervised automated Codex launch gate: {error}");
        return report_automated_supervisor_reaped(None);
    }
    drop(launch_gate);

    let parent_state = Arc::new(AtomicU64::new(AUTOMATED_SUPERVISOR_CONNECTED));
    let lifeline_state = Arc::clone(&parent_state);
    let lifeline_result = thread::Builder::new()
        .name(format!(
            "clt-automated-supervisor-lifeline-{}",
            spec.project_id
        ))
        .spawn(move || {
            let mut input = io::stdin();
            let mut buffer = [0_u8; 1];
            loop {
                match input.read(&mut buffer) {
                    Ok(0) | Err(_) => {
                        lifeline_state
                            .store(AUTOMATED_SUPERVISOR_PARENT_DISCONNECTED, Ordering::SeqCst);
                        break;
                    }
                    Ok(_) if buffer[0] == b's' => {
                        let _ = lifeline_state.compare_exchange(
                            AUTOMATED_SUPERVISOR_CONNECTED,
                            AUTOMATED_SUPERVISOR_STOP_REQUESTED,
                            Ordering::SeqCst,
                            Ordering::SeqCst,
                        );
                    }
                    Ok(_) => {}
                }
            }
        });
    if let Err(error) = lifeline_result {
        let status = stop_supervised_automated_child_until_reaped(
            &mut child,
            "its parent lifeline could not start",
        );
        eprintln!("Failed to start automated supervisor parent lifeline: {error}");
        return report_automated_supervisor_reaped(status);
    }

    // The runner owns session-control polling while it is connected and sends a
    // stop byte through the supervisor lifeline when a control is requested.
    // Keeping the supervisor out of the multiprocess agent database prevents a
    // storage-engine panic here from orphaning the Codex process it alone can
    // reap. If the runner crashes, EOF on the same lifeline remains an
    // independent, database-free shutdown signal.
    let monitor_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        loop {
            #[cfg(test)]
            if spec.run_token.starts_with("panic-supervisor-after-launch-") {
                panic!("injected automated supervisor monitor panic");
            }

            match interactive_child_exited_without_reaping(&child) {
                Ok(true) => {
                    break stop_supervised_automated_child_until_reaped(
                        &mut child,
                        "the Codex group leader exited",
                    );
                }
                Ok(false) => {}
                Err(error) => {
                    eprintln!("Automated supervisor could not poll its Codex child: {error:#}");
                    break stop_supervised_automated_child_until_reaped(
                        &mut child,
                        "polling the Codex child failed",
                    );
                }
            }

            if parent_state.load(Ordering::SeqCst) != AUTOMATED_SUPERVISOR_CONNECTED {
                break stop_supervised_automated_child_until_reaped(
                    &mut child,
                    "its runner requested shutdown or disconnected",
                );
            }
            thread::sleep(Duration::from_millis(100));
        }
    }));
    let status = match monitor_result {
        Ok(status) => status,
        Err(_) => {
            eprintln!(
                "Automated supervisor monitor panicked; stopping its owned Codex process group"
            );
            stop_supervised_automated_child_until_reaped(
                &mut child,
                "its supervisor monitor panicked",
            )
        }
    };

    let parent_disconnected =
        parent_state.load(Ordering::SeqCst) == AUTOMATED_SUPERVISOR_PARENT_DISCONNECTED;
    // A connected runner owns durable worker finalization. It already has the
    // reaping proof and will transition the exact session generation before it
    // records the run. Releasing or transferring its lease here would fence
    // that outer worker between `run_project` and transactional finalization.
    // The supervisor takes over only when that owner actually disconnects.
    if parent_disconnected {
        finalize_disconnected_automated_supervisor(
            spec.state_dir,
            spec.project_id,
            child_pid,
            spec.run_token,
            spec.lease_holder,
        );
    }
    report_automated_supervisor_reaped(status)
}

#[cfg(unix)]
pub(super) fn automated_supervisor_exit_code(status: Option<ExitStatus>) -> i32 {
    status.and_then(|status| status.code()).unwrap_or(1)
}

#[cfg(unix)]
pub(super) fn report_automated_supervisor_reaped(status: Option<ExitStatus>) -> Result<i32> {
    let exit_code = automated_supervisor_exit_code(status);
    let mut output = stdout().lock();
    writeln!(output, "{AUTOMATED_SUPERVISOR_REAPED_PREFIX}{exit_code}")
        .context("Failed to report automated Codex process-group shutdown")?;
    output
        .flush()
        .context("Failed to flush automated Codex process-group shutdown proof")?;
    Ok(exit_code)
}

#[cfg(unix)]
pub(super) fn supervised_session_control(
    store: &agent::TursoAgentStore,
    project_id: i64,
    child_pid: u32,
    run_token: &str,
) -> Result<Option<agent::AgentSessionControlRecord>> {
    Ok(store
        .session_controls_for_project_blocking(project_id)?
        .into_iter()
        .find(|control| {
            control.child_pid == Some(child_pid) && control.run_token.as_deref() == Some(run_token)
        }))
}

#[cfg(unix)]
pub(super) fn finalize_reaped_unregistered_agent_worker(
    store: &agent::TursoAgentStore,
    project_id: i64,
    run_token: &str,
    lease_holder: &str,
) -> Result<bool> {
    if let Some(worker) = store
        .list_active_workers_blocking()?
        .into_iter()
        .find(|worker| worker.worker_token == run_token)
    {
        if worker.project_id != project_id || worker.lease_holder != lease_holder {
            anyhow::bail!(
                "Reaped supervisor worker {run_token} does not match its exact project and lease fence"
            );
        }
        let lease = store.lease_for_project_blocking(project_id)?;
        let permitted_successor_holder = lease
            .as_ref()
            .filter(|lease| lease.holder != worker.lease_holder)
            .map(|lease| lease.holder.as_str());
        return store.abandon_worker_blocking(agent::AgentWorkerAbandonment {
            worker_token: run_token,
            expected_state: &worker.state,
            expected_worker_pid: worker.worker_pid,
            expected_heartbeat_at: worker.heartbeat_at.as_deref(),
            finished_at: &agent_timestamp(),
            error: "Automated runner disconnected after its supervised Codex process group was proven reaped",
            permitted_successor_holder,
        });
    }

    if let Some(worker) = store
        .list_terminal_workers_blocking()?
        .into_iter()
        .find(|worker| worker.worker_token == run_token)
    {
        if worker.project_id != project_id || worker.lease_holder != lease_holder {
            anyhow::bail!(
                "Terminal supervisor worker {run_token} does not match its exact project and lease fence"
            );
        }
        store.release_lease_blocking(project_id, lease_holder)?;
        return Ok(true);
    }

    if store
        .git_launch_state_blocking(project_id, run_token)?
        .is_some()
    {
        anyhow::bail!(
            "Reaped Git launch {run_token} has no exact durable worker to finalize; preserving its launch boundary and lease"
        );
    }
    store.release_lease_blocking(project_id, lease_holder)?;
    Ok(true)
}

#[cfg(unix)]
pub(super) fn stop_supervised_automated_child_until_reaped(
    child: &mut Child,
    reason: &str,
) -> Option<ExitStatus> {
    let process_group = match i32::try_from(child.id()) {
        Ok(process_group) => process_group,
        Err(error) => {
            eprintln!(
                "Automated supervisor cannot identify its owned Codex process group after {reason}: {error}"
            );
            loop {
                thread::sleep(Duration::from_secs(1));
            }
        }
    };
    let mut leader_status = None;
    let mut last_warning: Option<Instant> = None;
    loop {
        if let Some(status) = leader_status {
            match agent_process_group_exists(process_group) {
                Ok(false) => return Some(status),
                // Once the leader is reaped, its numeric PGID is no longer
                // anchored against reuse. Keep the generation fenced and only
                // observe from here; signaling again could target a new group.
                Ok(true) => {}
                Err(error) => {
                    let should_warn = last_warning
                        .is_none_or(|warning| warning.elapsed() >= Duration::from_secs(5));
                    if should_warn {
                        eprintln!(
                            "Automated supervisor cannot yet prove Codex group {process_group} exited after {reason}: {error:#}"
                        );
                        last_warning = Some(Instant::now());
                    }
                }
            }
        } else {
            match stop_agent_child_process(child) {
                Ok(status) => return status,
                Err(error) => {
                    let should_warn = last_warning
                        .is_none_or(|warning| warning.elapsed() >= Duration::from_secs(5));
                    if should_warn {
                        eprintln!(
                            "Automated supervisor retains its owned Codex group after {reason}: {error:#}"
                        );
                        last_warning = Some(Instant::now());
                    }
                    match child.try_wait() {
                        Ok(Some(status)) => leader_status = Some(status),
                        Ok(None) => {}
                        Err(error) => eprintln!(
                            "Automated supervisor could not poll its Codex group leader: {error:#}"
                        ),
                    }
                }
            }
        }
        thread::sleep(Duration::from_millis(250));
    }
}

#[cfg(unix)]
pub(super) fn finalize_disconnected_automated_supervisor(
    state_dir: &Path,
    project_id: i64,
    child_pid: u32,
    run_token: &str,
    lease_holder: &str,
) {
    let mut last_warning: Option<Instant> = None;
    loop {
        // The exact child group is already gone. A recovery marker requires
        // exclusive maintenance, so leave its durable fences for recovery.
        if let Err(error) = agent::recovery::check_required(state_dir) {
            eprintln!("Automated supervisor stopped post-reap finalization: {error:#}");
            return;
        }
        let result = (|| -> Result<bool> {
            if with_agent_store_at(state_dir, |store| {
                store.finalize_reaped_automated_session_blocking(
                    project_id,
                    child_pid,
                    run_token,
                    lease_holder,
                    agent_lease_timeout()?.as_secs().max(60),
                )
            })? {
                return Ok(true);
            }
            if with_agent_store_at(state_dir, |store| {
                supervised_session_control(store, project_id, child_pid, run_token)
            })?
            .is_some()
            {
                return Ok(false);
            }
            with_agent_store_at(state_dir, |store| {
                finalize_reaped_unregistered_agent_worker(
                    store,
                    project_id,
                    run_token,
                    lease_holder,
                )
            })
        })();
        match result {
            Ok(true) => return,
            Ok(false) => {}
            Err(error) => {
                if agent::recovery::check_required(state_dir).is_err() {
                    eprintln!("Automated supervisor stopped post-reap finalization: {error:#}");
                    return;
                }
                let should_warn =
                    last_warning.is_none_or(|warning| warning.elapsed() >= Duration::from_secs(5));
                if should_warn {
                    eprintln!("Automated supervisor is retrying post-reap finalization: {error:#}");
                    last_warning = Some(Instant::now());
                }
            }
        }
        thread::sleep(Duration::from_millis(250));
    }
}

pub(super) fn format_agent_run_line(run: &agent::AgentRunRecord) -> String {
    format!(
        "run={} project={} {} status={} started_at={} finished_at={} exit_code={} summary={} stdout={} stderr={} path={}",
        run.id,
        run.project_id,
        run.project_name,
        run.status,
        format_agent_timestamp(&run.started_at),
        format_optional_agent_timestamp(run.finished_at.as_deref()),
        run.exit_code
            .map(|exit_code| exit_code.to_string())
            .unwrap_or_else(|| "-".to_string()),
        run.summary.as_deref().unwrap_or("-"),
        run.stdout_path.as_deref().unwrap_or("-"),
        run.stderr_path.as_deref().unwrap_or("-"),
        run.project_path.display()
    )
}

pub(super) fn print_agent_log_tail(label: &str, path: Option<&str>) -> Result<()> {
    print_agent_log_tail_with_limit(label, path, 20)
}

pub(super) fn print_agent_log_tail_with_limit(
    label: &str,
    path: Option<&str>,
    limit: usize,
) -> Result<()> {
    let Some(path) = path else {
        println!("{label}=<not recorded>");
        return Ok(());
    };
    let path = Path::new(path);
    println!("{label}={}", path.display());
    match fs::read_to_string(path) {
        Ok(content) => {
            let tail = tail_lines(&content, limit);
            if tail.is_empty() {
                println!("  <empty>");
            } else {
                for line in tail {
                    println!("  {line}");
                }
            }
        }
        Err(err) if err.kind() == io::ErrorKind::NotFound => {
            println!("  <missing>");
        }
        Err(err) => {
            return Err(err).with_context(|| format!("Failed to read agent log {:?}", path));
        }
    }

    Ok(())
}

pub(super) fn tail_lines(content: &str, limit: usize) -> Vec<&str> {
    let lines: Vec<&str> = content.lines().collect();
    let start = lines.len().saturating_sub(limit);
    lines[start..].to_vec()
}

impl CodexAgentRunner {
    pub(super) fn new(state_dir: PathBuf) -> Result<Self> {
        Self::new_with_worker_token(state_dir, None)
    }

    pub(super) fn new_with_worker_token(
        state_dir: PathBuf,
        worker_token: Option<String>,
    ) -> Result<Self> {
        let lease_timeout = agent_lease_timeout()?;
        Ok(Self {
            state_dir,
            timeout: agent_run_timeout()?,
            heartbeat_interval: agent_poll_interval()?,
            lease_timeout,
            lease_renew_interval: agent_lease_renew_interval(lease_timeout),
            command: agent_codex_command(),
            worker_token,
        })
    }

    #[cfg(test)]
    pub(super) fn with_command(state_dir: PathBuf, timeout: Duration, command: PathBuf) -> Self {
        let lease_timeout = Duration::from_secs(60);
        Self {
            state_dir,
            timeout,
            heartbeat_interval: Duration::from_secs(AGENT_DEFAULT_POLL_INTERVAL_SECONDS),
            lease_timeout,
            lease_renew_interval: agent_lease_renew_interval(lease_timeout),
            command,
            worker_token: None,
        }
    }
}

pub(super) fn agent_codex_prompt(
    project: &agent::AgentProject,
    task_selection: AgentTaskSelection,
) -> String {
    let clt_skill_available =
        agent_skill_is_available(&project.path, CLT_TASK_MANAGEMENT_SKILL_NAME);
    let git_skill_available = project.git_mode == AgentGitMode::Off
        || agent_skill_is_available(&project.path, GIT_COMMIT_SKILL_NAME);
    build_agent_codex_prompt(
        project,
        task_selection,
        clt_skill_available,
        git_skill_available,
    )
}

pub(super) fn effective_agent_git_mode(
    store: &agent::TursoAgentStore,
    project: &agent::AgentProject,
    resume_session_id: Option<&str>,
) -> Result<AgentGitMode> {
    let Some(session_id) = resume_session_id else {
        return Ok(project.git_mode);
    };
    let Some(finalization) = store.git_finalization_blocking(project.id, session_id)? else {
        return Ok(project.git_mode);
    };
    Ok(if finalization.state.is_terminal() {
        project.git_mode
    } else {
        finalization.git_mode
    })
}

pub(super) fn build_agent_codex_prompt(
    project: &agent::AgentProject,
    task_selection: AgentTaskSelection,
    clt_skill_available: bool,
    git_skill_available: bool,
) -> String {
    let mut prompt = AGENT_CODEX_PROMPT_BASE.to_string();
    if clt_skill_available {
        prompt.push_str(
            "\nTask workflow:\n- Use the $clt-task-management skill for the task-board workflow.\n",
        );
    }
    match task_selection {
        AgentTaskSelection::NextTodo => {}
        AgentTaskSelection::ResumeDoing => {
            prompt.push_str(AGENT_RESUME_DOING_PROMPT_APPENDIX);
        }
        AgentTaskSelection::RecoverBlocked => {
            prompt.push_str(AGENT_RECOVER_BLOCKED_PROMPT_APPENDIX);
        }
        AgentTaskSelection::ResumeSession => {
            prompt.push_str(AGENT_RESUME_SESSION_PROMPT_APPENDIX);
        }
    }
    match project.git_mode {
        AgentGitMode::Off => {}
        AgentGitMode::Commit => prompt.push_str(AGENT_GIT_COMMIT_PROMPT_APPENDIX),
        AgentGitMode::CommitAndPush => {
            prompt.push_str(AGENT_GIT_COMMIT_PROMPT_APPENDIX);
            prompt.push_str(AGENT_GIT_PUSH_PROMPT_APPENDIX);
        }
    }
    if !clt_skill_available {
        append_embedded_agent_skill(
            &mut prompt,
            CLT_TASK_MANAGEMENT_SKILL_NAME,
            EMBEDDED_CLT_TASK_MANAGEMENT_SKILL,
        );
    }
    if project.git_mode != AgentGitMode::Off && !git_skill_available {
        append_embedded_agent_skill(
            &mut prompt,
            GIT_COMMIT_SKILL_NAME,
            EMBEDDED_GIT_COMMIT_SKILL,
        );
    }
    prompt
}

pub(super) fn append_embedded_agent_skill(prompt: &mut String, name: &str, contents: &str) {
    prompt.push_str("\n\nEmbedded skill fallback:\n");
    prompt.push_str("- The $");
    prompt.push_str(name);
    prompt.push_str(
        " skill was not found in a standard Codex skill directory. Follow this bundled version for this run.\n\n<skill>\n<name>",
    );
    prompt.push_str(name);
    prompt.push_str("</name>\n<source>embedded in clt</source>\n");
    prompt.push_str(contents);
    if !contents.ends_with('\n') {
        prompt.push('\n');
    }
    prompt.push_str("</skill>");
}

pub(super) fn agent_skill_is_available(project_root: &Path, skill_name: &str) -> bool {
    agent_skill_search_roots(project_root)
        .iter()
        .any(|root| agent_skill_root_contains_name(root, skill_name))
}

pub(super) fn agent_skill_search_roots(project_root: &Path) -> Vec<PathBuf> {
    let repository_root =
        get_task_root_at(project_root, false).unwrap_or_else(|_| project_root.to_path_buf());
    let mut roots = Vec::new();
    let mut directory = project_root.to_path_buf();

    loop {
        roots.push(directory.join(".agents/skills"));
        if directory == repository_root
            || !directory.pop()
            || !directory.starts_with(&repository_root)
        {
            break;
        }
    }

    if let Some(home) = std::env::var_os("HOME") {
        roots.push(PathBuf::from(home).join(".agents/skills"));
    }
    roots.push(PathBuf::from("/etc/codex/skills"));
    roots
}

pub(super) fn agent_skill_root_contains_name(root: &Path, skill_name: &str) -> bool {
    let Ok(entries) = fs::read_dir(root) else {
        return false;
    };

    entries.filter_map(Result::ok).any(|entry| {
        fs::read_to_string(entry.path().join("SKILL.md"))
            .ok()
            .and_then(|contents| skill_frontmatter_name(&contents).map(str::to_string))
            .is_some_and(|name| name == skill_name)
    })
}

pub(super) fn skill_frontmatter_name(contents: &str) -> Option<&str> {
    let mut lines = contents.lines();
    if lines.next()?.trim() != "---" {
        return None;
    }

    for line in lines {
        let line = line.trim();
        if line == "---" {
            break;
        }
        if let Some(name) = line.strip_prefix("name:") {
            return Some(name.trim().trim_matches(['\"', '\'']));
        }
    }
    None
}

impl AgentRunner for CodexAgentRunner {
    fn run_project(&self, request: AgentRunRequest<'_>) -> Result<AgentRunResult> {
        Self::run_codex_project_stage(self, request)
    }
}

impl CodexAgentRunner {
    pub(super) fn run_codex_project_stage(
        &self,
        request: AgentRunRequest<'_>,
    ) -> Result<AgentRunResult> {
        let AgentRunRequest {
            project,
            task_selection,
            resume_session_id,
            lease_holder,
            run_token,
            shutdown,
        } = request;
        let runner = self;
        let store = open_agent_store_at(&runner.state_dir)?;
        let known_session_id = match resume_session_id {
            Some(session_id) => Some(session_id.to_string()),
            None => automated_codex_session_to_resume(&project.path, task_selection)?,
        };
        let mut effective_project = project.clone();
        effective_project.git_mode =
            effective_agent_git_mode(&store, project, known_session_id.as_deref())?;
        let project = &effective_project;
        let effective_worker_token = run_token
            .map(str::to_string)
            .or_else(|| runner.worker_token.clone());
        if run_token.is_some()
            && runner.worker_token.is_some()
            && run_token != runner.worker_token.as_deref()
        {
            anyhow::bail!("Agent runner received conflicting durable worker tokens");
        }
        let run_file_stem = effective_worker_token
            .clone()
            .unwrap_or_else(|| agent_log_file_stem(project.id));
        ensure_agent_git_index_preflight(project, known_session_id.is_some())?;
        let existing_git_finalization = known_session_id
            .as_deref()
            .map(|session_id| store.git_finalization_blocking(project.id, session_id))
            .transpose()?
            .flatten();
        let git_start_state = prepare_agent_git_start_state_for_run(
            &store,
            project,
            task_selection,
            known_session_id.is_some(),
            existing_git_finalization.is_some(),
            &run_file_stem,
        )?;
        let doing_task_contents_before =
            task_contents_for_status(&project.path, TaskStatus::Doing).unwrap_or_default();
        let blocked_task_snapshots_before =
            blocked_task_snapshots(&project.path).unwrap_or_default();
        let launched = match launch_agent_runner_stage(AgentRunnerLaunchRequest {
            runner,
            store: &store,
            project,
            task_selection,
            resume_session_id,
            lease_holder,
            run_file_stem: &run_file_stem,
            git_start_state: git_start_state.as_ref(),
        })? {
            AgentRunnerLaunchResult::Launched(process) => process,
            AgentRunnerLaunchResult::Failed(result) => return Ok(result),
        };
        let mut child = launched.child;
        let child_pid = launched.child_pid;
        let log_dir = launched.log_dir;
        let stdout_path = launched.stdout_path;
        let stderr_path = launched.stderr_path;
        let configured_session_id = launched.configured_session_id;
        #[cfg(unix)]
        let mut supervisor_control = launched.supervisor_control;
        #[cfg(unix)]
        let mut supervisor_proof = launched.supervisor_proof;
        let mut last_heartbeat_stderr_bytes = 0;
        let mut observed_session_id = configured_session_id;
        let mut session_linked = false;
        let mut session_registered = false;
        let mut session_link_error_logged = false;
        let mut last_session_control_poll: Option<Instant> = None;
        let mut last_lease_renewal = Instant::now();
        let requested_control_cell = Cell::new(None);
        if let Some(session_id) = observed_session_id.as_deref() {
            let registration_result = store.register_known_session_with_child_blocking(
                agent::AgentKnownSessionRegistration {
                    project_id: project.id,
                    codex_session_id: session_id,
                    child_pid,
                    run_token: &run_file_stem,
                    stdout_path: &stdout_path,
                    stderr_path: &stderr_path,
                    lease_holder,
                    lease_timeout_seconds: runner.lease_timeout.as_secs(),
                    claim_requested_resume: task_selection == AgentTaskSelection::ResumeSession
                        && resume_session_id == Some(session_id),
                },
            );
            let registration_error = match registration_result {
                Ok(true) => None,
                Ok(false) => Some(anyhow::anyhow!(
                    "Known-session control or its live project lease changed before Codex launch"
                )),
                Err(error) => Some(error),
            };
            if let Some(error) = registration_error {
                #[cfg(unix)]
                {
                    supervisor_control.take();
                    wait_for_automated_supervisor_reaped(
                        &mut child,
                        &mut supervisor_proof,
                    )
                    .with_context(|| {
                        format!(
                            "Known-session registration failed ({error:#}), and its supervisor could not prove Codex stopped"
                        )
                    })?;
                }
                #[cfg(not(unix))]
                stop_agent_child_process(&mut child).with_context(|| {
                    format!(
                        "Known-session registration failed ({error:#}), and CLT could not prove its spawned Codex process stopped"
                    )
                })?;
                return Err(error).context("Failed to register known Codex child before launch");
            }
            session_registered = true;
            ensure_agent_git_working_record(
                &store,
                project,
                session_id,
                &run_file_stem,
                git_start_state.as_ref(),
            )?;
            let _ =
                bind_agent_git_working_task_identity(&store, project, session_id, &run_file_stem)?;
        }
        #[cfg(unix)]
        if let Err(error) = supervisor_control
            .as_mut()
            .expect("Unix automated supervisor has a control pipe")
            .write_all(b"x")
            .and_then(|_| {
                supervisor_control
                    .as_mut()
                    .expect("Unix automated supervisor has a control pipe")
                    .flush()
            })
        {
            supervisor_control.take();
            wait_for_automated_supervisor_reaped(
                &mut child,
                &mut supervisor_proof,
            )
            .with_context(|| {
                format!(
                    "Automated supervisor launch release failed ({error}), and it could not prove Codex stopped"
                )
            })?;
            return Err(error).context("Failed to release supervised automated Codex launch gate");
        }
        #[cfg(unix)]
        let wait_result = wait_for_automated_supervisor_with_timeout_and_heartbeat(
            AutomatedSupervisorWaitHandles {
                process: &mut child,
                control: &mut supervisor_control,
                proof: &mut supervisor_proof,
            },
            runner.timeout,
            runner.heartbeat_interval,
            |elapsed| {
                print_agent_run_heartbeat(
                    project,
                    elapsed,
                    runner.timeout,
                    &stdout_path,
                    &stderr_path,
                    &mut last_heartbeat_stderr_bytes,
                )
            },
            || {
                if last_lease_renewal.elapsed() >= runner.lease_renew_interval {
                    let expires_at = agent_timestamp_after(runner.lease_timeout.as_secs());
                    let renewed = if let Some(worker_token) = effective_worker_token.as_deref() {
                        store.renew_worker_blocking(
                            worker_token,
                            std::process::id(),
                            &agent_timestamp(),
                            &expires_at,
                        )?
                    } else {
                        store.renew_lease_blocking(project.id, lease_holder, &expires_at)?
                    };
                    if !renewed {
                        anyhow::bail!(
                            "Automated Codex lease is no longer held for project {}",
                            project.id
                        );
                    }
                    last_lease_renewal = Instant::now();
                }
                if observed_session_id.is_none() {
                    observed_session_id = agent_codex_session_id_from_log(&stderr_path)?;
                }
                if let Some(session_id) = observed_session_id.as_deref()
                    && !session_registered
                {
                    if project.git_mode == AgentGitMode::Off {
                        store.mark_session_running_blocking(
                            project.id,
                            session_id,
                            child_pid,
                            &run_file_stem,
                            &stdout_path,
                            &stderr_path,
                        )?;
                    } else {
                        store.mark_session_running_with_git_finalization_blocking(
                            project.id,
                            session_id,
                            child_pid,
                            &run_file_stem,
                            &stdout_path,
                            &stderr_path,
                            project.git_mode,
                        )?;
                    }
                    session_registered = true;
                }
                if let Some(session_id) = observed_session_id.as_deref()
                    && !session_linked
                {
                    match attach_codex_session_to_active_task(
                        &project.path,
                        task_selection,
                        &doing_task_contents_before,
                        &blocked_task_snapshots_before,
                        session_id,
                    ) {
                        Ok(attached) => {
                            session_linked = attached;
                            if attached {
                                if store
                                    .git_finalization_blocking(project.id, session_id)?
                                    .is_none()
                                    && project.git_mode != AgentGitMode::Off
                                {
                                    ensure_agent_git_working_record(
                                        &store,
                                        project,
                                        session_id,
                                        &run_file_stem,
                                        git_start_state.as_ref(),
                                    )?;
                                }
                                let _ = bind_agent_git_working_task_identity(
                                    &store,
                                    project,
                                    session_id,
                                    &run_file_stem,
                                )?;
                            }
                        }
                        Err(error) if !session_link_error_logged => {
                            append_agent_log_line(
                                &stderr_path,
                                &format!(
                                    "Failed to attach the live Codex session to its task: {error:#}"
                                ),
                            )?;
                            session_link_error_logged = true;
                        }
                        Err(_) => {}
                    }
                }
                let should_poll_control = last_session_control_poll.is_none_or(|last_poll| {
                    last_poll.elapsed() >= Duration::from_millis(AGENT_SESSION_CONTROL_POLL_MILLIS)
                });
                if should_poll_control {
                    if let Some(session_id) = observed_session_id.as_deref()
                        && let Some(control) =
                            store.session_control_blocking(project.id, session_id)?
                        && let Some(action) = automated_session_control_action_for_generation(
                            &control,
                            child_pid,
                            &run_file_stem,
                        )
                    {
                        requested_control_cell.set(Some(action));
                    }
                    last_session_control_poll = Some(Instant::now());
                }
                Ok(())
            },
            || shutdown.load(Ordering::SeqCst) || requested_control_cell.get().is_some(),
        );
        #[cfg(not(unix))]
        let wait_result = wait_for_child_with_timeout_and_heartbeat(
            &mut child,
            runner.timeout,
            runner.heartbeat_interval,
            |elapsed| {
                print_agent_run_heartbeat(
                    project,
                    elapsed,
                    runner.timeout,
                    &stdout_path,
                    &stderr_path,
                    &mut last_heartbeat_stderr_bytes,
                )
            },
            || Ok(()),
            || shutdown.load(Ordering::SeqCst),
        );
        let wait_result = match wait_result {
            Ok(wait_result) => wait_result,
            Err(error) => {
                #[cfg(not(unix))]
                stop_agent_child_process(&mut child).with_context(|| {
                    format!(
                        "Codex run observation failed ({error:#}), and CLT could not prove its process stopped"
                    )
                })?;
                return Err(error).context("Failed while observing the Codex run");
            }
        };
        let stdout = fs::read_to_string(&stdout_path).unwrap_or_default();
        let codex_session_id = match observed_session_id {
            Some(session_id) => Some(session_id),
            None => agent_codex_session_id_from_log(&stderr_path)?,
        };
        if requested_control_cell.get().is_none()
            && let Some(session_id) = codex_session_id.as_deref()
            && let Some(control) = store.session_control_blocking(project.id, session_id)?
            && let Some(action) =
                automated_session_control_action_for_generation(&control, child_pid, &run_file_stem)
        {
            requested_control_cell.set(Some(action));
        }
        let requested_control = requested_control_cell.get();
        let supervision = classify_agent_supervision_stage(AgentSupervisionOutcomeRequest {
            wait_result,
            requested_control,
            reported_no_tasks: stdout.contains(AGENT_NO_TASKS_LEFT_MARKER),
            timeout: runner.timeout,
        });
        if let Some(note) = supervision.stderr_note.as_deref() {
            append_agent_log_line(&stderr_path, note)?;
        }

        Ok(AgentRunResult {
            status: supervision.status,
            exit_code: supervision.exit_code,
            log_dir,
            stdout_path,
            stderr_path,
            summary: supervision.summary,
            codex_session_id,
            session_run_token: session_registered.then_some(run_file_stem),
            control_action: requested_control,
        })
    }
}

#[cfg(test)]
impl CodexAgentRunner {
    pub(super) fn run_project(
        &self,
        project: &agent::AgentProject,
        task_selection: AgentTaskSelection,
        resume_session_id: Option<&str>,
        lease_holder: &str,
        run_token: Option<&str>,
        shutdown: &AgentShutdownSignal,
    ) -> Result<AgentRunResult> {
        <Self as AgentRunner>::run_project(
            self,
            AgentRunRequest {
                project,
                task_selection,
                resume_session_id,
                lease_holder,
                run_token,
                shutdown,
            },
        )
    }
}

pub(super) enum AgentProcessWait {
    Exited(ExitStatus),
    TimedOut(Option<ExitStatus>),
    Interrupted(Option<ExitStatus>),
}

pub(super) fn resolve_agent_project_root(
    path: Option<&Path>,
    local: bool,
    default_root: &Path,
) -> Result<PathBuf> {
    match path {
        Some(path) => get_task_root_at(path, local),
        None => canonicalize_existing_path(default_root),
    }
}

pub(super) fn canonicalize_existing_path(path: &Path) -> Result<PathBuf> {
    fs::canonicalize(path).with_context(|| format!("Failed to resolve project path {:?}", path))
}

pub(super) fn agent_timestamp() -> String {
    agent_timestamp_seconds().to_string()
}

pub(super) fn agent_timestamp_seconds() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|duration| duration.as_secs())
        .unwrap_or(0)
}

pub(super) fn agent_timestamp_after(seconds: u64) -> String {
    std::time::SystemTime::now()
        .checked_add(std::time::Duration::from_secs(seconds))
        .and_then(|time| time.duration_since(std::time::UNIX_EPOCH).ok())
        .map(|duration| duration.as_secs().to_string())
        .unwrap_or_else(agent_timestamp)
}

pub(super) fn format_agent_timestamp(raw: &str) -> String {
    let Ok(seconds) = raw.parse::<i64>() else {
        return raw.to_string();
    };

    let Some(utc) = DateTime::<Utc>::from_timestamp(seconds, 0) else {
        return raw.to_string();
    };

    utc.with_timezone(&Local)
        .format("%Y-%m-%d %H:%M:%S %Z")
        .to_string()
}

pub(super) fn format_optional_agent_timestamp(raw: Option<&str>) -> String {
    raw.map(format_agent_timestamp)
        .unwrap_or_else(|| "-".to_string())
}

pub(super) fn agent_project_run_log_dir(
    state_dir: &Path,
    project: &agent::AgentProject,
) -> Result<PathBuf> {
    let slug = agent_project_slug(project);
    Ok(state_dir.join("runs").join(slug))
}

pub(super) fn agent_project_slug(project: &agent::AgentProject) -> String {
    let mut slug = String::new();
    let mut last_was_separator = false;

    for ch in project.name.chars().flat_map(char::to_lowercase) {
        if ch.is_ascii_alphanumeric() {
            slug.push(ch);
            last_was_separator = false;
        } else if (ch == '-' || ch == '_' || ch.is_whitespace()) && !last_was_separator {
            slug.push('-');
            last_was_separator = true;
        }
    }

    let slug = slug.trim_matches('-');
    if slug.is_empty() {
        format!("project-{}", project.id)
    } else {
        format!("{}-{}", project.id, slug)
    }
}

pub(super) fn agent_log_file_stem(project_id: i64) -> String {
    let duration = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default();
    format!(
        "{}-{:03}-p{}-{}",
        duration.as_secs(),
        duration.subsec_millis(),
        project_id,
        std::process::id()
    )
}

#[cfg(any(not(unix), test))]
pub(super) fn wait_for_child_with_timeout_and_heartbeat(
    child: &mut Child,
    timeout: Duration,
    heartbeat_interval: Duration,
    mut heartbeat: impl FnMut(Duration) -> Result<()>,
    mut observe: impl FnMut() -> Result<()>,
    mut should_shutdown: impl FnMut() -> bool,
) -> Result<AgentProcessWait> {
    let heartbeat_interval = if heartbeat_interval.is_zero() {
        Duration::from_millis(250)
    } else {
        heartbeat_interval
    };
    let started = Instant::now();
    let mut last_heartbeat = started;

    loop {
        observe()?;

        if let Some(status) = child.try_wait().context("Failed to poll Codex process")? {
            return Ok(AgentProcessWait::Exited(status));
        }

        if should_shutdown() {
            let status = stop_agent_child_process(child)
                .context("Failed to stop Codex process during agent shutdown")?;
            return Ok(AgentProcessWait::Interrupted(status));
        }

        if started.elapsed() >= timeout {
            let status = stop_agent_child_process(child)
                .context("Failed to stop timed out Codex process")?;
            return Ok(AgentProcessWait::TimedOut(status));
        }

        if last_heartbeat.elapsed() >= heartbeat_interval {
            heartbeat(started.elapsed())?;
            last_heartbeat = Instant::now();
        }

        thread::sleep(std::cmp::min(
            Duration::from_millis(250),
            heartbeat_interval,
        ));
    }
}

#[cfg(unix)]
pub(super) fn wait_for_automated_supervisor_with_timeout_and_heartbeat(
    handles: AutomatedSupervisorWaitHandles<'_>,
    timeout: Duration,
    heartbeat_interval: Duration,
    mut heartbeat: impl FnMut(Duration) -> Result<()>,
    mut observe: impl FnMut() -> Result<()>,
    mut should_shutdown: impl FnMut() -> bool,
) -> Result<AgentProcessWait> {
    let AutomatedSupervisorWaitHandles {
        process: supervisor,
        control,
        proof,
    } = handles;
    let heartbeat_interval = if heartbeat_interval.is_zero() {
        Duration::from_millis(250)
    } else {
        heartbeat_interval
    };
    let started = Instant::now();
    let mut last_heartbeat = started;

    loop {
        if let Err(error) = observe() {
            request_automated_supervisor_stop(control);
            wait_for_automated_supervisor_reaped(supervisor, proof).with_context(|| {
                format!(
                    "Automated Codex observation failed ({error:#}); its supervisor did not prove the process group reaped"
                )
            })?;
            return Err(error);
        }

        if let Some(status) = supervisor
            .try_wait()
            .context("Failed to poll automated Codex supervisor")?
        {
            let status = verify_automated_supervisor_reaped(status, proof)?;
            control.take();
            return Ok(AgentProcessWait::Exited(status));
        }

        if should_shutdown() {
            request_automated_supervisor_stop(control);
            let status = wait_for_automated_supervisor_reaped(supervisor, proof)?;
            return Ok(AgentProcessWait::Interrupted(Some(status)));
        }

        if started.elapsed() >= timeout {
            request_automated_supervisor_stop(control);
            let status = wait_for_automated_supervisor_reaped(supervisor, proof)?;
            return Ok(AgentProcessWait::TimedOut(Some(status)));
        }

        if last_heartbeat.elapsed() >= heartbeat_interval {
            if let Err(error) = heartbeat(started.elapsed()) {
                request_automated_supervisor_stop(control);
                wait_for_automated_supervisor_reaped(supervisor, proof).with_context(|| {
                    format!(
                        "Automated Codex heartbeat failed ({error:#}); its supervisor did not prove the process group reaped"
                    )
                })?;
                return Err(error);
            }
            last_heartbeat = Instant::now();
        }
        thread::sleep(std::cmp::min(
            Duration::from_millis(250),
            heartbeat_interval,
        ));
    }
}

#[cfg(unix)]
pub(super) fn request_automated_supervisor_stop(control: &mut Option<std::process::ChildStdin>) {
    let write_result = control
        .as_mut()
        .context("Automated Codex supervisor control pipe is already closed")
        .and_then(|control| {
            control
                .write_all(b"s")
                .and_then(|_| control.flush())
                .context("Failed to request supervised Codex shutdown")
        });
    if let Err(error) = write_result {
        eprintln!(
            "Automated Codex supervisor stop request failed; closing its lifeline instead: {error:#}"
        );
        control.take();
    }
}

#[cfg(unix)]
pub(super) fn wait_for_automated_supervisor_reaped(
    supervisor: &mut Child,
    proof: &mut BufReader<std::process::ChildStdout>,
) -> Result<ExitStatus> {
    let status = supervisor
        .wait()
        .context("Failed to wait for automated Codex supervisor")?;
    verify_automated_supervisor_reaped(status, proof)
}

#[cfg(unix)]
pub(super) fn verify_automated_supervisor_reaped(
    supervisor_status: ExitStatus,
    proof: &mut BufReader<std::process::ChildStdout>,
) -> Result<ExitStatus> {
    let mut remainder = String::new();
    proof
        .read_to_string(&mut remainder)
        .context("Failed to read automated Codex supervisor shutdown proof")?;
    let reported_exit_code = remainder.lines().find_map(|line| {
        line.trim()
            .strip_prefix(AUTOMATED_SUPERVISOR_REAPED_PREFIX)
            .and_then(|code| code.parse::<i32>().ok())
    });
    if reported_exit_code.is_none() {
        return Err(unproven_agent_child_termination(
            anyhow::anyhow!("supervisor exited with {supervisor_status} without a reap marker"),
            "The automated Codex supervisor exited without proving its owned process group stopped",
        ));
    }
    Ok(supervisor_status)
}

pub(super) fn configure_automated_agent_child_context(
    command: &mut Command,
    state_dir: &Path,
    project_id: i64,
    run_token: &str,
) {
    command
        .env(AGENT_STATE_DIR_ENV, state_dir)
        .env(AGENT_PROJECT_ID_ENV, project_id.to_string())
        .env(AGENT_RUN_TOKEN_ENV, run_token);
}

pub(super) fn append_agent_log_line(path: &Path, line: &str) -> Result<()> {
    let mut file = fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)
        .with_context(|| format!("Failed to append to agent log {:?}", path))?;
    writeln!(file, "{line}").with_context(|| format!("Failed to write agent log {:?}", path))
}

pub(super) fn parse_agent_codex_session_id(line: &str) -> Option<String> {
    line.trim()
        .strip_prefix("session id:")
        .map(str::trim)
        .filter(|session_id| !session_id.is_empty())
        .map(str::to_string)
}

pub(super) fn agent_codex_session_id_from_log(path: &Path) -> Result<Option<String>> {
    let file = fs::File::open(path)
        .with_context(|| format!("Failed to open recorded agent output {path:?}"))?;

    for line in BufReader::new(file).lines().take(100) {
        if let Some(session_id) = parse_agent_codex_session_id(&line?) {
            return Ok(Some(session_id));
        }
    }

    Ok(None)
}

#[derive(Debug, Default, Eq, PartialEq)]
pub(super) struct AgentRunSettings {
    pub(super) model: Option<String>,
    pub(super) reasoning_effort: Option<String>,
}

pub(super) fn agent_run_settings_from_log(path: &Path) -> Result<AgentRunSettings> {
    let file = fs::File::open(path)
        .with_context(|| format!("Failed to open recorded agent output {path:?}"))?;
    let mut settings = AgentRunSettings::default();
    let mut saw_banner = false;
    let mut in_header = false;

    // Only trust Codex's startup header, never similarly named fields in task/output text.
    // Bound reads even when a live or older log has no complete header.
    for line in BufReader::new(file.take(16 * 1024)).lines().take(100) {
        let line = line?;
        let line = line.trim();
        if !saw_banner {
            saw_banner = line.starts_with("OpenAI Codex v");
            continue;
        }
        if line == "--------" {
            if in_header {
                return Ok(settings);
            }
            in_header = true;
        } else if in_header {
            let Some((key, value)) = line.split_once(':') else {
                continue;
            };
            let value = value.trim();
            if value.is_empty() {
                continue;
            }
            match key {
                "model" => settings.model = Some(value.to_string()),
                "reasoning effort" => settings.reasoning_effort = Some(value.to_string()),
                _ => {}
            }
        }
    }

    Ok(AgentRunSettings::default())
}

pub(super) fn latest_agent_log_path(log_dir: &Path, extension: &str) -> Result<Option<PathBuf>> {
    if !log_dir.exists() {
        return Ok(None);
    }

    let mut paths = Vec::new();
    for entry in fs::read_dir(log_dir)
        .with_context(|| format!("Failed to read agent log directory {:?}", log_dir))?
    {
        let entry = entry?;
        let path = entry.path();
        if entry.file_type()?.is_file() && path.extension() == Some(OsStr::new(extension)) {
            paths.push(path);
        }
    }
    paths.sort();
    Ok(paths.pop())
}

pub(super) fn preferred_recorded_agent_output_path(run: &agent::AgentRunRecord) -> Option<PathBuf> {
    let stdout_path = run.stdout_path.as_ref().map(PathBuf::from);
    let stdout_has_output = stdout_path
        .as_ref()
        .and_then(|path| fs::metadata(path).ok())
        .is_some_and(|metadata| metadata.len() > 0);

    if stdout_has_output {
        stdout_path
    } else {
        run.stderr_path.as_ref().map(PathBuf::from).or(stdout_path)
    }
}