clt-rs 0.6.19

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
use std::{
    ffi::OsString,
    fs,
    io::{self, Read, Write},
    path::{Path, PathBuf},
    process::{Child, Command, ExitStatus, Stdio},
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
    thread,
    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};

use anyhow::{Context, Result};

use crate::{
    agent::{
        self, AgentSessionControlAction, AgentSessionControlState, ensure_agent_state_dir,
        ensure_agent_state_dir_at, open_agent_store_at, with_agent_store_at,
    },
    application::{
        AgentRunJob, AgentTaskSelection, INTERACTIVE_LEASE_GENERATION, new_agent_shutdown_signal,
    },
    platform::{
        InteractiveTerminalForeground, agent_process_group_exists,
        automated_agent_process_group_is_running, configure_agent_child_command,
        configure_interactive_child_command, interactive_child_exited_without_reaping,
        interactive_terminal_input, local_process_is_running,
        restore_interactive_terminal_before_handoff,
        restore_parent_terminal_after_interactive_guardian, stop_interactive_child_process,
    },
    runner::{
        CodexAgentRunner, agent_codex_command, agent_timestamp, agent_timestamp_after,
        agent_timestamp_seconds, automated_exec_gate_is_released,
        configure_agent_provider_credential, configure_automated_exec_gate_inheritance,
    },
    scheduler::{
        agent_failure_backoff, agent_lease_holder, agent_lease_is_reclaimable,
        agent_lease_renew_interval, agent_lease_timeout, agent_max_global_jobs,
        reconcile_stale_agent_session_controls, scan_agent_project, task_status_for_codex_session,
    },
    session_recovery::ensure_orphaned_session_supervision,
    task::{
        TaskEntry, TaskSource, TaskStatus, get_tasks_dir, read_task_entries,
        recoverable_codex_session_id_from_task_content,
    },
    tui::{
        TUI_LEASE_RELEASE_ATTEMPTS, TUI_LEASE_RELEASE_RETRY_MILLIS,
        TUI_SESSION_HANDOFF_TIMEOUT_SECONDS, TUI_SESSION_RESUME_WORKER_RETRY_MILLIS,
    },
    worker::{
        blocked_task_snapshots, completed_task_contents, print_agent_run_completion, run_agent_job,
    },
};

pub(super) mod planning;

#[cfg(all(unix, test))]
use crate::application::TEST_INTERACTIVE_EXEC_GATE_ENV;
#[cfg(unix)]
use std::{
    os::fd::{AsRawFd, FromRawFd},
    os::unix::{net::UnixStream, process::CommandExt},
};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum InteractiveCodexResumeMode {
    ResumeExec,
    WritableIdle,
    WritableShared,
}

impl InteractiveCodexResumeMode {
    pub(super) fn resumes_exec(self) -> bool {
        self == Self::ResumeExec
    }

    pub(super) fn shares_project(self) -> bool {
        self == Self::WritableShared
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum InteractiveGuardianDisposition {
    ResumeExec,
    PreserveIdleSession,
    RestoreStopped,
    PreserveSharedSession,
    RestoreStoppedShared,
}

impl InteractiveGuardianDisposition {
    pub(super) fn from_handoff(mode: InteractiveCodexResumeMode, from_holder: &str) -> Self {
        if mode.resumes_exec() {
            Self::ResumeExec
        } else if mode.shares_project() {
            if is_stopped_shared_interactive_holder(from_holder) {
                Self::RestoreStoppedShared
            } else {
                Self::PreserveSharedSession
            }
        } else if from_holder.starts_with("clt-stopped-interactive-") {
            Self::RestoreStopped
        } else {
            Self::PreserveIdleSession
        }
    }

    pub(super) fn holds_project_lease(self) -> bool {
        !matches!(
            self,
            Self::PreserveSharedSession | Self::RestoreStoppedShared
        )
    }

    pub(super) fn guardian_holder_prefix(self) -> &'static str {
        match self {
            Self::ResumeExec => "clt-interactive-worker",
            // Keep the established holder prefixes so a newer CLT can recover
            // guardians started by an older binary.
            Self::PreserveIdleSession => "clt-idle-interactive-worker",
            Self::RestoreStopped => "clt-stopped-interactive-worker",
            Self::PreserveSharedSession => "clt-shared-interactive-worker",
            Self::RestoreStoppedShared => "clt-stopped-shared-interactive-worker",
        }
    }

    pub(super) fn from_guardian_holder(holder: &str) -> Option<Self> {
        // Recognize guardians left by the brief read-only implementation so a
        // newer CLT can still recover their persisted session controls.
        if holder.starts_with("clt-stopped-readonly-interactive-worker-") {
            return Some(Self::RestoreStoppedShared);
        }
        if holder.starts_with("clt-readonly-interactive-worker-") {
            return Some(Self::PreserveSharedSession);
        }
        [
            Self::ResumeExec,
            Self::PreserveIdleSession,
            Self::RestoreStopped,
            Self::PreserveSharedSession,
            Self::RestoreStoppedShared,
        ]
        .into_iter()
        .find(|disposition| holder.starts_with(disposition.guardian_holder_prefix()))
    }

    pub(super) fn guardian_process_is_proven_dead(holder: &str) -> bool {
        Self::guardian_process_id(holder)
            .is_some_and(|pid| local_process_is_running(pid) == Some(false))
    }

    pub(super) fn guardian_process_id(holder: &str) -> Option<u32> {
        let disposition = Self::from_guardian_holder(holder)?;
        holder
            .strip_prefix(disposition.guardian_holder_prefix())
            .and_then(|suffix| suffix.strip_prefix('-'))
            .and_then(|suffix| suffix.split('-').next())
            .and_then(|pid| pid.parse::<u32>().ok())
    }
}

pub(super) fn is_stopped_shared_interactive_holder(holder: &str) -> bool {
    holder.starts_with("clt-stopped-shared-interactive-")
        || holder.starts_with("clt-stopped-readonly-interactive-")
}

pub(super) fn automated_session_control_action_for_generation(
    control: &agent::AgentSessionControlRecord,
    child_pid: u32,
    run_token: &str,
) -> Option<AgentSessionControlAction> {
    if control.run_token.as_deref() != Some(run_token) {
        return None;
    }
    if control.child_pid == Some(child_pid) {
        return control.state.requested_action();
    }
    match control.state {
        AgentSessionControlState::Stopped => Some(AgentSessionControlAction::Stop),
        AgentSessionControlState::ReadyInteractive => Some(AgentSessionControlAction::Interrupt),
        _ => None,
    }
}

pub(super) fn configure_interactive_codex_resume_command(
    command: &mut Command,
    project_root: &Path,
    session_id: &str,
) {
    command
        .arg("resume")
        .arg("--include-non-interactive")
        .arg("--sandbox")
        .arg("workspace-write")
        .arg("--ask-for-approval")
        .arg("on-request")
        .arg("-C")
        .arg(project_root)
        .arg(session_id)
        .current_dir(project_root);
}

#[cfg(unix)]
pub(super) fn set_descriptor_close_on_exec(fd: libc::c_int, close_on_exec: bool) -> io::Result<()> {
    // SAFETY: fcntl only inspects or updates descriptor flags for the supplied
    // live descriptor.
    let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
    if flags < 0 {
        return Err(io::Error::last_os_error());
    }
    let updated = if close_on_exec {
        flags | libc::FD_CLOEXEC
    } else {
        flags & !libc::FD_CLOEXEC
    };
    // SAFETY: `updated` is derived from the descriptor's current flag set.
    if unsafe { libc::fcntl(fd, libc::F_SETFD, updated) } < 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(())
}

#[cfg(unix)]
pub(super) fn configure_inherited_child_control(
    command: &mut Command,
) -> Result<(i32, UnixStream)> {
    let (child_control, parent_control) =
        UnixStream::pair().context("Failed to create the interactive guardian control channel")?;
    set_descriptor_close_on_exec(child_control.as_raw_fd(), true)
        .context("Failed to protect the child end of the interactive control channel")?;
    set_descriptor_close_on_exec(parent_control.as_raw_fd(), true)
        .context("Failed to protect the parent end of the interactive control channel")?;
    let control_fd = child_control.as_raw_fd();

    // SAFETY: the closure only performs async-signal-safe fcntl calls. It owns
    // `child_control`, keeping that exact descriptor allocated through fork,
    // and clears CLOEXEC only in the child that was explicitly given its
    // numeric descriptor on the command line.
    unsafe {
        command.pre_exec(move || {
            let child_fd = child_control.as_raw_fd();
            let flags = libc::fcntl(child_fd, libc::F_GETFD);
            if flags < 0 {
                return Err(io::Error::last_os_error());
            }
            if libc::fcntl(child_fd, libc::F_SETFD, flags & !libc::FD_CLOEXEC) < 0 {
                return Err(io::Error::last_os_error());
            }
            Ok(())
        });
    }

    Ok((control_fd, parent_control))
}

#[cfg(unix)]
pub(super) fn inherited_child_control_reader(control_fd: Option<i32>) -> Result<fs::File> {
    let control_fd = control_fd.context("Interactive helper did not receive its control FD")?;
    if control_fd <= libc::STDERR_FILENO {
        anyhow::bail!("Interactive helper received an invalid control FD {control_fd}");
    }
    // SAFETY: the hidden helper receives this descriptor from the parent that
    // kept it allocated across exec. Taking ownership here ensures it closes
    // when the helper finishes reading the channel.
    let control = unsafe { fs::File::from_raw_fd(control_fd) };
    set_descriptor_close_on_exec(control.as_raw_fd(), true)
        .context("Failed to contain the inherited interactive control FD")?;
    Ok(control)
}

#[cfg(unix)]
pub(super) fn run_interactive_exec_gate(
    control_fd: Option<i32>,
    program: &Path,
    arguments: &[OsString],
) -> Result<()> {
    let mut reader = inherited_child_control_reader(control_fd)?;
    if !automated_exec_gate_is_released(&mut reader)
        .context("Failed to read interactive Codex launch gate")?
    {
        return Ok(());
    }
    drop(reader);

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

#[cfg(test)]
mod tests;

#[cfg(not(unix))]
pub(super) fn run_interactive_exec_gate(
    _control_fd: Option<i32>,
    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 interactive Codex launch gate")?
    {
        return Ok(());
    }

    let terminal_input = interactive_terminal_input()?;
    let status = Command::new(program)
        .args(arguments)
        .stdin(Stdio::from(terminal_input))
        .status()
        .with_context(|| {
            format!(
                "Failed to start gated interactive Codex command {}",
                program.display()
            )
        })?;
    if status.success() {
        Ok(())
    } else {
        anyhow::bail!("Interactive Codex exited with status {status}")
    }
}

pub(super) struct InteractiveExecGateCommand {
    pub(super) command: Command,
    pub(super) launch_gate: Option<Box<dyn Write>>,
}

impl InteractiveExecGateCommand {
    pub(super) fn command_mut(&mut self) -> &mut Command {
        &mut self.command
    }

    pub(super) fn spawn(mut self) -> Result<(Child, Box<dyn Write>)> {
        let mut child = self.command.spawn()?;
        let launch_gate = match self.launch_gate.take() {
            Some(launch_gate) => launch_gate,
            None => match child.stdin.take() {
                Some(launch_gate) => Box::new(launch_gate),
                None => {
                    let _ = child.kill();
                    let _ = child.wait();
                    anyhow::bail!("Interactive Codex launch gate did not open its release pipe");
                }
            },
        };
        Ok((child, launch_gate))
    }
}

#[cfg(all(unix, not(test)))]
pub(super) fn interactive_exec_gate_command(
    target: &Command,
) -> Result<InteractiveExecGateCommand> {
    let executable = std::env::current_exe()
        .context("Failed to resolve the CLT executable for the interactive Codex launch gate")?;
    let mut gate = Command::new(executable);
    configure_automated_exec_gate_inheritance(&mut gate, target);
    gate.stdin(Stdio::inherit());
    let (control_fd, launch_gate) = configure_inherited_child_control(&mut gate)?;
    gate.arg("--local")
        .arg("agent")
        .arg("interactive-exec-gate")
        .arg("--control-fd")
        .arg(control_fd.to_string())
        .arg("--")
        .arg(target.get_program())
        .args(target.get_args());
    Ok(InteractiveExecGateCommand {
        command: gate,
        launch_gate: Some(Box::new(launch_gate)),
    })
}

#[cfg(all(unix, test))]
pub(super) fn interactive_exec_gate_command(
    target: &Command,
) -> Result<InteractiveExecGateCommand> {
    // A test binary is driven by libtest rather than the Clap entry point. Run
    // one exact helper test so launch-phase tests exercise the real FD reader.
    let executable = std::env::current_exe()
        .context("Failed to resolve the CLT interactive exec-gate test helper")?;
    let mut gate = Command::new(executable);
    configure_automated_exec_gate_inheritance(&mut gate, target);
    gate.stdin(Stdio::inherit());
    let (control_fd, launch_gate) = configure_inherited_child_control(&mut gate)?;
    gate.arg("--exact")
        .arg("runner::tests::interactive_exec_gate_process_entry")
        .arg("--nocapture")
        .env(TEST_INTERACTIVE_EXEC_GATE_ENV, "1")
        .env(
            "CLT_TEST_INTERACTIVE_GATE_CONTROL_FD",
            control_fd.to_string(),
        )
        .env("CLT_TEST_INTERACTIVE_GATE_PROGRAM", target.get_program())
        .env(
            "CLT_TEST_INTERACTIVE_GATE_ARGUMENT_COUNT",
            target.get_args().count().to_string(),
        );
    for (index, argument) in target.get_args().enumerate() {
        gate.env(
            format!("CLT_TEST_INTERACTIVE_GATE_ARGUMENT_{index}"),
            argument,
        );
    }
    Ok(InteractiveExecGateCommand {
        command: gate,
        launch_gate: Some(Box::new(launch_gate)),
    })
}

#[cfg(not(unix))]
pub(super) fn interactive_exec_gate_command(
    target: &Command,
) -> Result<InteractiveExecGateCommand> {
    let executable = std::env::current_exe()
        .context("Failed to resolve the CLT executable for the interactive Codex launch gate")?;
    let mut gate = Command::new(executable);
    gate.arg("--local")
        .arg("agent")
        .arg("interactive-exec-gate")
        .arg("--")
        .arg(target.get_program())
        .args(target.get_args());
    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);
            }
        }
    }
    gate.stdin(Stdio::piped());
    Ok(InteractiveExecGateCommand {
        command: gate,
        launch_gate: None,
    })
}

pub(super) struct InteractiveAgentLease {
    pub(super) state_dir: PathBuf,
    pub(super) project_id: i64,
    pub(super) holder: String,
    pub(super) released: bool,
}

pub(super) struct PendingInteractiveHandoff {
    state_dir: PathBuf,
    project_id: i64,
    session_id: String,
    holder: String,
    armed: bool,
}

impl PendingInteractiveHandoff {
    pub(super) fn new(state_dir: &Path, project_id: i64, session_id: &str, holder: &str) -> Self {
        Self {
            state_dir: state_dir.to_path_buf(),
            project_id,
            session_id: session_id.to_string(),
            holder: holder.to_string(),
            armed: true,
        }
    }

    pub(super) fn disarm(&mut self) {
        self.armed = false;
    }
}

impl Drop for PendingInteractiveHandoff {
    fn drop(&mut self) {
        if !self.armed {
            return;
        }
        let _ = with_agent_store_at(&self.state_dir, |store| {
            let cancel_result = store.cancel_session_interrupt_handoff_blocking(
                self.project_id,
                &self.session_id,
                &self.holder,
            );
            let release_result = store.release_lease_blocking(self.project_id, &self.holder);
            cancel_result?;
            release_result.map(|_| ())
        });
    }
}

impl InteractiveAgentLease {
    pub(super) fn holder_for_current_process_with_prefix(prefix: &str) -> String {
        let generation = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos();
        let sequence = INTERACTIVE_LEASE_GENERATION.fetch_add(1, Ordering::Relaxed);
        format!("{prefix}-{}-{generation}-{sequence}", std::process::id())
    }

    pub(super) fn holder_for_current_process() -> String {
        Self::holder_for_current_process_with_prefix("clt-interactive")
    }

    pub(super) fn holder_for_idle_session() -> String {
        Self::holder_for_current_process_with_prefix("clt-idle-interactive")
    }

    pub(super) fn holder_for_stopped_session() -> String {
        Self::holder_for_current_process_with_prefix("clt-stopped-interactive")
    }

    pub(super) fn holder_for_shared_session(restore_stopped: bool) -> String {
        let prefix = if restore_stopped {
            "clt-stopped-shared-interactive"
        } else {
            "clt-shared-interactive"
        };
        Self::holder_for_current_process_with_prefix(prefix)
    }

    pub(super) fn try_acquire_idle(project_id: i64, restore_stopped: bool) -> Result<Option<Self>> {
        let state_dir = ensure_agent_state_dir()?;
        let timeout_seconds = TUI_SESSION_HANDOFF_TIMEOUT_SECONDS.max(60);
        let holder = if restore_stopped {
            Self::holder_for_stopped_session()
        } else {
            Self::holder_for_idle_session()
        };
        Self::try_acquire_with_holder_at(&state_dir, project_id, &holder, timeout_seconds)
    }

    #[cfg(test)]
    pub(super) fn try_acquire_at(
        state_dir: &Path,
        project_id: i64,
        timeout_seconds: u64,
    ) -> Result<Option<Self>> {
        let holder = Self::holder_for_current_process();
        Self::try_acquire_with_holder_at(state_dir, project_id, &holder, timeout_seconds)
    }

    pub(super) fn try_acquire_with_holder_at(
        state_dir: &Path,
        project_id: i64,
        holder: &str,
        timeout_seconds: u64,
    ) -> Result<Option<Self>> {
        ensure_agent_state_dir_at(state_dir)?;
        let acquired_at = agent_timestamp();
        let expires_at = agent_timestamp_after(timeout_seconds);
        let acquired = with_agent_store_at(state_dir, |store| {
            store.try_acquire_lease_blocking(project_id, holder, &acquired_at, &expires_at)
        })?;

        Ok(acquired.then(|| Self {
            state_dir: state_dir.to_path_buf(),
            project_id,
            holder: holder.to_string(),
            released: false,
        }))
    }

    pub(super) fn adopt_at(
        state_dir: &Path,
        project_id: i64,
        holder: &str,
    ) -> Result<Option<Self>> {
        let lease = with_agent_store_at(state_dir, |store| {
            store.lease_for_project_blocking(project_id)
        })?;
        Ok(lease.filter(|lease| lease.holder == holder).map(|_| Self {
            state_dir: state_dir.to_path_buf(),
            project_id,
            holder: holder.to_string(),
            released: false,
        }))
    }

    pub(super) fn release(mut self) -> Result<()> {
        let mut last_error = None;
        for attempt in 0..TUI_LEASE_RELEASE_ATTEMPTS {
            match with_agent_store_at(&self.state_dir, |store| {
                store.release_lease_blocking(self.project_id, &self.holder)
            }) {
                Ok(true) => {
                    self.released = true;
                    return Ok(());
                }
                Ok(false) => {
                    let lease = with_agent_store_at(&self.state_dir, |store| {
                        store.lease_for_project_blocking(self.project_id)
                    })?;
                    if lease
                        .as_ref()
                        .is_none_or(|lease| lease.holder != self.holder)
                    {
                        self.released = true;
                        return Ok(());
                    }
                    last_error = Some(anyhow::anyhow!(
                        "Interactive Codex lease is still held after its release request"
                    ));
                }
                Err(error) => last_error = Some(error),
            }
            if attempt + 1 < TUI_LEASE_RELEASE_ATTEMPTS {
                thread::sleep(Duration::from_millis(TUI_LEASE_RELEASE_RETRY_MILLIS));
            }
        }
        Err(last_error.unwrap_or_else(|| anyhow::anyhow!("Failed to release interactive lease")))
    }
}

impl Drop for InteractiveAgentLease {
    fn drop(&mut self) {
        if self.released {
            return;
        }
        let _ = with_agent_store_at(&self.state_dir, |store| {
            store
                .release_lease_blocking(self.project_id, &self.holder)
                .map(|_| ())
        });
    }
}

pub(super) fn resume_codex_session_interactively(
    project_root: &Path,
    project_id: i64,
    session_id: &str,
    from_holder: &str,
    mode: InteractiveCodexResumeMode,
) -> Result<ExitStatus> {
    let executable = std::env::current_exe().context("Failed to resolve the CLT executable")?;
    let state_dir = ensure_agent_state_dir()?;
    let store = open_agent_store_at(&state_dir)?;
    let mut command = Command::new(&executable);
    command
        .arg("--local")
        .arg("agent")
        .arg("interactive-session-worker")
        .arg("--project-id")
        .arg(project_id.to_string())
        .arg("--session-id")
        .arg(session_id)
        .arg("--from-holder")
        .arg(from_holder)
        .current_dir(project_root);
    if mode.resumes_exec() {
        command.arg("--resume-exec");
    }
    if mode.shares_project() {
        command.arg("--shared-project");
    }
    #[cfg(unix)]
    let (control_fd, guardian_lifeline) = configure_inherited_child_control(&mut command)?;
    #[cfg(unix)]
    command.arg("--control-fd").arg(control_fd.to_string());
    #[cfg(not(unix))]
    command.stdin(Stdio::piped());
    let mut guardian = command.spawn().with_context(|| {
        format!(
            "Failed to start the interactive Codex guardian with {} in {}",
            executable.display(),
            project_root.display()
        )
    })?;
    drop(command);
    #[cfg(unix)]
    let lifeline: Box<dyn Write> = Box::new(guardian_lifeline);
    #[cfg(not(unix))]
    let Some(lifeline) = guardian.stdin.take() else {
        let _ = guardian.kill();
        let _ = guardian.wait();
        anyhow::bail!("Interactive Codex guardian did not open its lifeline");
    };
    let mut lifeline = Some(lifeline);
    let start_result = lifeline
        .as_mut()
        .expect("interactive guardian lifeline is present before launch")
        .write_all(&[1])
        .context("Failed to start the interactive Codex guardian")
        .and_then(|_| {
            lifeline
                .as_mut()
                .expect("interactive guardian lifeline is present while launching")
                .flush()
                .context("Failed to flush the interactive Codex guardian lifeline")
        });
    if let Err(error) = start_result {
        drop(lifeline);
        let _ = guardian.wait();
        return Err(error);
    }
    let guardian_pid = guardian.id();
    let status_result = loop {
        match guardian.try_wait() {
            Ok(Some(status)) => break Ok(status),
            Ok(None) => {}
            Err(error) => {
                break Err(error).context("Failed to wait for the interactive Codex guardian");
            }
        }

        if lifeline.is_some()
            && store
                .session_control_blocking(project_id, session_id)
                .is_ok_and(|control| {
                    control.is_some_and(|control| {
                        interactive_guardian_stop_requested(&control, guardian_pid)
                    })
                })
        {
            // Closing the database-free lifeline asks the child-owning guardian
            // to stop and reap its exact Codex process group. The parent TUI is
            // the only process that owns this writer, so another TUI can request
            // a safe stop without ever signaling a numeric PID itself.
            drop(lifeline.take());
        }
        thread::sleep(Duration::from_millis(100));
    };
    drop(lifeline);
    let foreground_result = restore_parent_terminal_after_interactive_guardian();
    match status_result {
        Ok(status) => {
            foreground_result?;
            Ok(status)
        }
        Err(error) => {
            let _ = guardian.wait();
            match foreground_result {
                Ok(()) => Err(error),
                Err(foreground_error) => Err(error.context(format!(
                    "restoring the parent terminal foreground also failed: {foreground_error:#}"
                ))),
            }
        }
    }
}

pub(super) fn interactive_guardian_stop_requested(
    control: &agent::AgentSessionControlRecord,
    guardian_pid: u32,
) -> bool {
    control.state == AgentSessionControlState::StopRequested
        && control.interactive_holder.as_deref().is_some_and(|holder| {
            InteractiveGuardianDisposition::guardian_process_id(holder) == Some(guardian_pid)
        })
}

pub(super) fn run_agent_session_resume_worker(project_id: i64, session_id: &str) -> Result<()> {
    if session_id.is_empty()
        || !session_id
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
    {
        anyhow::bail!("Invalid Codex session ID for exact-session resume");
    }

    let state_dir = ensure_agent_state_dir()?;
    let store = open_agent_store_at(&state_dir)?;
    let project = store
        .list_projects_blocking()?
        .into_iter()
        .find(|project| project.id == project_id)
        .with_context(|| format!("Registered project {project_id} no longer exists"))?;
    let scan = scan_agent_project(&project.path);
    let blocked_task_count_before = scan.blocked_task_count();
    let done_task_contents_before = completed_task_contents(&project.path).unwrap_or_default();
    let blocked_task_snapshots_before = blocked_task_snapshots(&project.path).unwrap_or_default();
    let runner = CodexAgentRunner::new(state_dir.clone())?;
    let holder = agent_lease_holder();
    let lease_timeout = agent_lease_timeout()?;
    loop {
        let control = store.session_control_blocking(project_id, session_id)?;
        match control {
            Some(control) if control.state == AgentSessionControlState::ResumeRequested => {}
            Some(control) if control.state == AgentSessionControlState::Running => {
                let lease = store.lease_for_project_blocking(project_id)?;
                if lease.as_ref().is_some_and(|lease| {
                    !agent_lease_is_reclaimable(lease, false, agent_timestamp_seconds())
                }) {
                    thread::sleep(Duration::from_millis(
                        TUI_SESSION_RESUME_WORKER_RETRY_MILLIS,
                    ));
                    continue;
                }
                if !control.child_pid.is_some_and(|child_pid| {
                    automated_agent_process_group_is_running(child_pid) == Some(false)
                }) {
                    thread::sleep(Duration::from_millis(
                        TUI_SESSION_RESUME_WORKER_RETRY_MILLIS,
                    ));
                    continue;
                }
                store.recover_stale_automated_session_control_blocking(
                    project_id,
                    session_id,
                    AgentSessionControlState::Running,
                    AgentSessionControlState::ResumeRequested,
                    control.child_pid.expect("checked recorded child PID"),
                    control.run_token.as_deref(),
                )?;
                continue;
            }
            Some(_) | None => return Ok(()),
        }

        let acquired_at = agent_timestamp();
        let expires_at = agent_timestamp_after(lease_timeout.as_secs());
        if !store.try_acquire_lease_blocking(project_id, &holder, &acquired_at, &expires_at)? {
            if let Some(lease) = store.lease_for_project_blocking(project_id)?
                && agent_lease_is_reclaimable(&lease, false, agent_timestamp_seconds())
            {
                store.release_lease_blocking(project_id, &lease.holder)?;
                continue;
            }
            thread::sleep(Duration::from_millis(
                TUI_SESSION_RESUME_WORKER_RETRY_MILLIS,
            ));
            continue;
        }

        let completion = match run_agent_job(
            AgentRunJob {
                state_dir: state_dir.clone(),
                project: project.clone(),
                holder: holder.clone(),
                worker_token: None,
                max_global_jobs: agent_max_global_jobs()?,
                task_selection: AgentTaskSelection::ResumeSession,
                resume_session_id: Some(session_id.to_string()),
                blocked_task_count_before,
                done_task_contents_before: done_task_contents_before.clone(),
                blocked_task_snapshots_before: blocked_task_snapshots_before.clone(),
            },
            &runner,
            &new_agent_shutdown_signal(),
        ) {
            Ok(completion) => completion,
            Err(error) => {
                eprintln!(
                    "Exact-session resume worker could not run Codex session {session_id}: {error:#}"
                );
                thread::sleep(Duration::from_millis(
                    TUI_SESSION_RESUME_WORKER_RETRY_MILLIS,
                ));
                continue;
            }
        };
        print_agent_run_completion(&completion)?;
        if matches!(completion.status, "failure" | "timeout") {
            thread::sleep(agent_failure_backoff()?);
            continue;
        }
        return Ok(());
    }
}

pub(super) fn run_agent_interactive_session_worker(
    project_id: i64,
    session_id: &str,
    from_holder: &str,
    mode: InteractiveCodexResumeMode,
    control_fd: Option<i32>,
) -> Result<()> {
    if session_id.is_empty()
        || !session_id
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
    {
        anyhow::bail!("Invalid Codex session ID for interactive guardian");
    }

    #[cfg(unix)]
    let mut parent_control: Box<dyn Read + Send> =
        Box::new(inherited_child_control_reader(control_fd)?);
    #[cfg(not(unix))]
    let mut parent_control: Box<dyn Read + Send> = Box::new(io::stdin());
    let mut startup_gate = [0_u8; 1];
    parent_control
        .read_exact(&mut startup_gate)
        .context("Interactive guardian parent disconnected before startup")?;
    let parent_connected = Arc::new(AtomicBool::new(true));
    let lifeline = Arc::clone(&parent_connected);
    thread::Builder::new()
        .name(format!("clt-interactive-lifeline-{project_id}"))
        .spawn(move || {
            let mut buffer = [0_u8; 1];
            loop {
                match parent_control.read(&mut buffer) {
                    Ok(0) | Err(_) => {
                        lifeline.store(false, Ordering::SeqCst);
                        break;
                    }
                    Ok(_) => {}
                }
            }
        })
        .context("Failed to start interactive guardian lifeline")?;

    let state_dir = ensure_agent_state_dir()?;
    let store = open_agent_store_at(&state_dir)?;
    let project = store
        .list_projects_blocking()?
        .into_iter()
        .find(|project| project.id == project_id)
        .with_context(|| format!("Registered project {project_id} no longer exists"))?;
    let terminal_input = interactive_terminal_input()?;
    let lease_timeout = agent_lease_timeout()?;
    let disposition = InteractiveGuardianDisposition::from_handoff(mode, from_holder);
    let guardian_holder = interactive_guardian_holder(disposition);
    if !store.adopt_interactive_guardian_blocking(
        project_id,
        Some(session_id),
        from_holder,
        &guardian_holder,
        lease_timeout.as_secs().max(60),
    )? {
        anyhow::bail!("Interactive handoff changed before its guardian could adopt it");
    }

    let interaction_result = run_guarded_interactive_codex(
        &store,
        &project,
        session_id,
        &guardian_holder,
        lease_timeout,
        terminal_input,
        &parent_connected,
    );
    let interaction_failure = match interaction_result {
        Ok(Some(status)) if !status.success() => Some(anyhow::anyhow!(
            "Interactive Codex session {session_id} exited with status {status}"
        )),
        Ok(_) => None,
        Err(error) => Some(error),
    };
    let resume_exec = finish_interactive_guardian_after_reap(
        &store,
        project_id,
        session_id,
        &guardian_holder,
        lease_timeout,
        disposition,
    )?;
    if resume_exec {
        spawn_agent_session_resume_worker(&project.path, project_id, session_id)?;
    }
    interaction_failure.map_or(Ok(()), Err)
}

pub(super) fn finish_interactive_guardian_after_reap(
    store: &agent::TursoAgentStore,
    project_id: i64,
    session_id: &str,
    guardian_holder: &str,
    lease_timeout: Duration,
    disposition: InteractiveGuardianDisposition,
) -> Result<bool> {
    let renewal_interval = agent_lease_renew_interval(lease_timeout);
    let mut last_renewal = Instant::now();
    let mut last_warning: Option<Instant> = None;

    loop {
        // The child has been reaped and terminal ownership restored. Exit so
        // this store releases its access lock and exclusive recovery can run.
        store.check_recovery_required()?;
        match store.finish_interactive_guardian_blocking(
            project_id,
            session_id,
            guardian_holder,
            disposition,
        ) {
            Ok(changed) => match store.session_control_blocking(project_id, session_id) {
                Ok(control) => {
                    if control.is_none() && disposition.holds_project_lease() {
                        match store.release_lease_blocking(project_id, guardian_holder) {
                            Ok(_) => {
                                anyhow::bail!(
                                    "Interactive Codex session {session_id} disappeared after its guarded child was reaped; CLT released the orphaned project reservation"
                                );
                            }
                            Err(error) => {
                                store.check_recovery_required()?;
                                let should_warn = last_warning.is_none_or(|warning| {
                                    warning.elapsed() >= Duration::from_secs(5)
                                });
                                if should_warn {
                                    eprintln!(
                                        "Interactive guardian is retrying orphaned lease cleanup after its session disappeared: {error:#}"
                                    );
                                    last_warning = Some(Instant::now());
                                }
                                continue;
                            }
                        }
                    }
                    let finalized = match disposition {
                        InteractiveGuardianDisposition::ResumeExec => {
                            control.as_ref().and_then(|control| {
                                if control.interactive_holder.as_deref() == Some(guardian_holder) {
                                    return None;
                                }
                                match control.state {
                                    AgentSessionControlState::ResumeRequested
                                    | AgentSessionControlState::Running => Some(true),
                                    AgentSessionControlState::Stopped => Some(false),
                                    _ => None,
                                }
                            })
                        }
                        InteractiveGuardianDisposition::PreserveIdleSession
                        | InteractiveGuardianDisposition::PreserveSharedSession
                        | InteractiveGuardianDisposition::RestoreStopped
                        | InteractiveGuardianDisposition::RestoreStoppedShared => control
                            .is_some_and(|control| {
                                control.state == AgentSessionControlState::Stopped
                                    && control.interactive_holder.is_none()
                            })
                            .then_some(false),
                    };
                    if let Some(resume_exec) = finalized {
                        return Ok(resume_exec);
                    }
                    if changed {
                        anyhow::bail!(
                            "Interactive guardian finalized its child but left an unexpected session state"
                        );
                    }
                    anyhow::bail!(
                        "Interactive guardian state changed before its reaped child could be finalized"
                    );
                }
                Err(error) => {
                    store.check_recovery_required()?;
                    let should_warn = last_warning
                        .is_none_or(|warning| warning.elapsed() >= Duration::from_secs(5));
                    if should_warn {
                        eprintln!(
                            "Interactive guardian is retrying its post-reap state check: {error:#}"
                        );
                        last_warning = Some(Instant::now());
                    }
                }
            },
            Err(error) => {
                store.check_recovery_required()?;
                let should_warn =
                    last_warning.is_none_or(|warning| warning.elapsed() >= Duration::from_secs(5));
                if should_warn {
                    eprintln!(
                        "Interactive guardian is retrying post-reap database finalization: {error:#}"
                    );
                    last_warning = Some(Instant::now());
                }
            }
        }

        if last_renewal.elapsed() >= renewal_interval {
            let expires_at = agent_timestamp_after(lease_timeout.as_secs().max(60));
            let _ = store.renew_lease_blocking(project_id, guardian_holder, &expires_at);
            last_renewal = Instant::now();
        }
        thread::sleep(Duration::from_millis(250));
    }
}

pub(super) fn interactive_guardian_holder(disposition: InteractiveGuardianDisposition) -> String {
    let generation = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos();
    let sequence = INTERACTIVE_LEASE_GENERATION.fetch_add(1, Ordering::Relaxed);
    let prefix = disposition.guardian_holder_prefix();
    format!("{prefix}-{}-{generation}-{sequence}", std::process::id())
}

pub(super) fn run_guarded_interactive_codex(
    store: &agent::TursoAgentStore,
    project: &agent::AgentProject,
    session_id: &str,
    guardian_holder: &str,
    lease_timeout: Duration,
    terminal_input: fs::File,
    parent_connected: &AtomicBool,
) -> Result<Option<ExitStatus>> {
    if !parent_connected.load(Ordering::SeqCst) {
        return Ok(None);
    }

    let disposition = InteractiveGuardianDisposition::from_guardian_holder(guardian_holder)
        .context("Interactive Codex guardian has an unrecognized holder")?;
    let mut terminal_foreground = InteractiveTerminalForeground::capture(&terminal_input)?;
    let codex_command = agent_codex_command();
    let mut target = Command::new(&codex_command);
    configure_interactive_codex_resume_command(&mut target, &project.path, session_id);
    if let Some(provider) = store.resolve_credential_provider_blocking(project)? {
        configure_agent_provider_credential(&mut target, store, &provider)?;
    }
    let mut command = interactive_exec_gate_command(&target)?;
    configure_interactive_child_command(command.command_mut());
    let (mut child, mut launch_gate) = match command.spawn() {
        Ok(child) => child,
        Err(error) => {
            return Err(error).with_context(|| {
                format!(
                    "Failed to start the gated interactive Codex session {session_id} with {} in {}",
                    codex_command.display(),
                    project.path.display()
                )
            });
        }
    };
    let child_pid = child.id();
    let registered = store.register_interactive_guardian_child_blocking(
        project.id,
        session_id,
        guardian_holder,
        child_pid,
        lease_timeout.as_secs().max(60),
    );
    match registered {
        Ok(true) => {}
        Ok(false) => {
            drop(launch_gate);
            let _ = child.wait();
            anyhow::bail!(
                "Interactive handoff changed before gated Codex child {child_pid} could be registered"
            );
        }
        Err(error) => {
            drop(launch_gate);
            let _ = child.wait();
            return Err(error).context("Failed to register gated interactive Codex before launch");
        }
    }
    if !parent_connected.load(Ordering::SeqCst) {
        drop(launch_gate);
        let _ = child.wait();
        return Ok(None);
    }
    if let Err(foreground_error) = terminal_foreground.give_to_child(&child) {
        drop(launch_gate);
        let _ = stop_interactive_child_until_reaped(
            &mut child,
            store,
            project.id,
            guardian_holder,
            lease_timeout,
            "terminal foreground handoff failed",
        );
        restore_interactive_terminal_before_handoff(
            &mut terminal_foreground,
            store,
            project.id,
            guardian_holder,
            lease_timeout,
            parent_connected,
        );
        return Err(foreground_error);
    }
    let release_result = launch_gate
        .write_all(b"x")
        .context("Failed to release the registered interactive Codex launch gate")
        .and_then(|_| {
            launch_gate
                .flush()
                .context("Failed to flush the interactive Codex launch gate")
        });
    drop(launch_gate);
    if let Err(error) = release_result {
        let _ = stop_interactive_child_until_reaped(
            &mut child,
            store,
            project.id,
            guardian_holder,
            lease_timeout,
            "interactive launch-gate release failed",
        );
        restore_interactive_terminal_before_handoff(
            &mut terminal_foreground,
            store,
            project.id,
            guardian_holder,
            lease_timeout,
            parent_connected,
        );
        return Err(error);
    }

    let renew_interval = agent_lease_renew_interval(lease_timeout);
    let mut last_renewal = Instant::now();

    let interaction_result = loop {
        #[cfg(unix)]
        match interactive_child_exited_without_reaping(&child) {
            Ok(true) => {
                let status = stop_interactive_child_until_reaped(
                    &mut child,
                    store,
                    project.id,
                    guardian_holder,
                    lease_timeout,
                    "the interactive Codex leader exited",
                );
                break Ok(status);
            }
            Ok(false) => {}
            Err(error) => {
                eprintln!("Interactive Codex polling failed: {error:#}");
                let status = stop_interactive_child_until_reaped(
                    &mut child,
                    store,
                    project.id,
                    guardian_holder,
                    lease_timeout,
                    "polling the interactive Codex child failed",
                );
                break Ok(status);
            }
        }
        #[cfg(not(unix))]
        match child.try_wait() {
            Ok(Some(status)) => {
                break Ok(Some(status));
            }
            Ok(None) => {}
            Err(error) => {
                eprintln!("Interactive Codex polling failed: {error}");
                let status = stop_interactive_child_until_reaped(
                    &mut child,
                    store,
                    project.id,
                    guardian_holder,
                    lease_timeout,
                    "polling the interactive Codex child failed",
                );
                break Ok(status);
            }
        }
        if !parent_connected.load(Ordering::SeqCst) {
            let status = stop_interactive_child_until_reaped(
                &mut child,
                store,
                project.id,
                guardian_holder,
                lease_timeout,
                "the CLT parent disconnected",
            );
            break Ok(status);
        }
        if disposition.holds_project_lease() && last_renewal.elapsed() >= renew_interval {
            let expires_at = agent_timestamp_after(lease_timeout.as_secs().max(60));
            match store.renew_lease_blocking(project.id, guardian_holder, &expires_at) {
                Ok(true) => last_renewal = Instant::now(),
                Ok(false) => {
                    eprintln!("Interactive guardian no longer holds its project lease");
                    let status = stop_interactive_child_until_reaped(
                        &mut child,
                        store,
                        project.id,
                        guardian_holder,
                        lease_timeout,
                        "the interactive guardian lost its project lease",
                    );
                    break Ok(status);
                }
                Err(error) => {
                    eprintln!("Failed to renew interactive guardian lease: {error:#}");
                    let status = stop_interactive_child_until_reaped(
                        &mut child,
                        store,
                        project.id,
                        guardian_holder,
                        lease_timeout,
                        "renewing the interactive guardian lease failed",
                    );
                    break Ok(status);
                }
            }
        }
        thread::sleep(Duration::from_millis(250));
    };

    restore_interactive_terminal_before_handoff(
        &mut terminal_foreground,
        store,
        project.id,
        guardian_holder,
        lease_timeout,
        parent_connected,
    );
    interaction_result
}

#[cfg(unix)]
pub(super) fn stop_interactive_child_until_reaped(
    child: &mut Child,
    store: &agent::TursoAgentStore,
    project_id: i64,
    guardian_holder: &str,
    lease_timeout: Duration,
    reason: &str,
) -> Option<ExitStatus> {
    let renewal_interval = agent_lease_renew_interval(lease_timeout);
    let mut last_renewal = Instant::now();
    let mut last_warning: Option<Instant> = None;
    let child_process_label = child.id().to_string();
    let process_group = loop {
        match i32::try_from(child.id()).context("Interactive Codex process ID exceeded pid_t") {
            Ok(process_group) => break process_group,
            Err(error) => {
                let should_warn =
                    last_warning.is_none_or(|warning| warning.elapsed() >= Duration::from_secs(5));
                if should_warn {
                    eprintln!(
                        "Interactive guardian cannot identify the owned Codex process group after {reason}: {error:#}"
                    );
                    last_warning = Some(Instant::now());
                }
            }
        }
        renew_interactive_guardian_cleanup_lease(
            store,
            project_id,
            guardian_holder,
            lease_timeout,
            &child_process_label,
            &mut last_renewal,
            renewal_interval,
        );
        thread::sleep(Duration::from_millis(250));
    };
    let mut leader_status = None;

    loop {
        if let Some(status) = leader_status {
            match agent_process_group_exists(process_group) {
                Ok(false) => return Some(status),
                Ok(true) => {}
                Err(error) => {
                    let should_warn = last_warning
                        .is_none_or(|warning| warning.elapsed() >= Duration::from_secs(5));
                    if should_warn {
                        eprintln!(
                            "Interactive guardian cannot yet prove Codex process group {process_group} exited after {reason}: {error:#}"
                        );
                        last_warning = Some(Instant::now());
                    }
                }
            }
        } else {
            match stop_interactive_child_process(child) {
                Ok(Some(status)) => return Some(status),
                Ok(None) => {}
                Err(error) => {
                    let should_warn = last_warning
                        .is_none_or(|warning| warning.elapsed() >= Duration::from_secs(5));
                    if should_warn {
                        eprintln!(
                            "Interactive guardian is retaining its lease after {reason}; the owned Codex process group is not yet proven stopped: {error:#}"
                        );
                        last_warning = Some(Instant::now());
                    }
                    match child.try_wait() {
                        Ok(Some(status)) => leader_status = Some(status),
                        Ok(None) => {}
                        Err(poll_error) => {
                            let should_warn = last_warning
                                .is_none_or(|warning| warning.elapsed() >= Duration::from_secs(5));
                            if should_warn {
                                eprintln!(
                                    "Interactive guardian could not determine whether the Codex group leader was reaped: {poll_error:#}"
                                );
                                last_warning = Some(Instant::now());
                            }
                        }
                    }
                }
            }
        }

        renew_interactive_guardian_cleanup_lease(
            store,
            project_id,
            guardian_holder,
            lease_timeout,
            &child_process_label,
            &mut last_renewal,
            renewal_interval,
        );
        thread::sleep(Duration::from_millis(250));
    }
}

#[cfg(unix)]
pub(super) fn renew_interactive_guardian_cleanup_lease(
    store: &agent::TursoAgentStore,
    project_id: i64,
    guardian_holder: &str,
    lease_timeout: Duration,
    process_group: &str,
    last_renewal: &mut Instant,
    renewal_interval: Duration,
) {
    if InteractiveGuardianDisposition::from_guardian_holder(guardian_holder)
        .is_some_and(|disposition| !disposition.holds_project_lease())
    {
        return;
    }
    if last_renewal.elapsed() < renewal_interval {
        return;
    }
    let expires_at = agent_timestamp_after(lease_timeout.as_secs().max(60));
    match store.renew_lease_blocking(project_id, guardian_holder, &expires_at) {
        Ok(true) => {}
        Ok(false) => eprintln!(
            "Interactive guardian could not renew its lease while stopping Codex process group {process_group}"
        ),
        Err(error) => eprintln!(
            "Interactive guardian lease renewal failed while stopping Codex process group {process_group}: {error:#}"
        ),
    }
    *last_renewal = Instant::now();
}

#[cfg(not(unix))]
pub(super) fn stop_interactive_child_until_reaped(
    child: &mut Child,
    store: &agent::TursoAgentStore,
    project_id: i64,
    guardian_holder: &str,
    lease_timeout: Duration,
    reason: &str,
) -> Option<ExitStatus> {
    let holds_project_lease = InteractiveGuardianDisposition::from_guardian_holder(guardian_holder)
        .is_none_or(InteractiveGuardianDisposition::holds_project_lease);
    let renewal_interval = agent_lease_renew_interval(lease_timeout);
    let mut last_renewal = Instant::now();
    let mut last_warning: Option<Instant> = None;

    loop {
        match stop_interactive_child_process(child) {
            Ok(Some(status)) => return Some(status),
            Ok(None) => {}
            Err(error) => {
                let should_warn =
                    last_warning.is_none_or(|warning| warning.elapsed() >= Duration::from_secs(5));
                if should_warn {
                    eprintln!(
                        "Interactive guardian is retaining its lease after {reason}; the owned Codex child is not yet proven reaped: {error:#}"
                    );
                    last_warning = Some(Instant::now());
                }
            }
        }

        if holds_project_lease && last_renewal.elapsed() >= renewal_interval {
            let expires_at = agent_timestamp_after(lease_timeout.as_secs().max(60));
            match store.renew_lease_blocking(project_id, guardian_holder, &expires_at) {
                Ok(true) => {}
                Ok(false) => eprintln!(
                    "Interactive guardian could not renew its lease while waiting to reap its owned Codex child"
                ),
                Err(error) => eprintln!(
                    "Interactive guardian lease renewal failed while waiting to reap its owned Codex child: {error:#}"
                ),
            }
            last_renewal = Instant::now();
        }

        thread::sleep(Duration::from_millis(250));
    }
}

pub(super) fn task_supports_interactive_codex_resume(
    status: TaskStatus,
    _task: &TaskEntry,
) -> bool {
    matches!(
        status,
        TaskStatus::Todo | TaskStatus::Doing | TaskStatus::Done
    )
}

pub(super) fn codex_session_task_supports_interactive_resume(
    project_root: &Path,
    session_id: &str,
) -> Result<bool> {
    let mut matches = Vec::new();
    collect_codex_session_tasks_in_board(&get_tasks_dir(project_root), session_id, &mut matches)?;
    Ok(matches.len() == 1
        && matches
            .first()
            .is_some_and(|(status, task)| task_supports_interactive_codex_resume(*status, task)))
}

pub(super) fn collect_codex_session_tasks_in_board(
    board_dir: &Path,
    session_id: &str,
    matches: &mut Vec<(TaskStatus, TaskEntry)>,
) -> Result<()> {
    for status in TaskStatus::SESSION_SEARCH_ORDER {
        let tasks = read_task_entries(board_dir, status)?;
        for task in tasks {
            if recoverable_codex_session_id_from_task_content(&task.content) == Some(session_id) {
                matches.push((status, task.clone()));
            }
            if task.has_subtasks
                && let TaskSource::Path { path, is_dir: true } = &task.source
            {
                collect_codex_session_tasks_in_board(path, session_id, matches)?;
            }
        }
    }
    Ok(())
}

pub(super) fn codex_session_for_task(task: &TaskEntry) -> Option<String> {
    recoverable_codex_session_id_from_task_content(&task.content).map(str::to_string)
}

pub(super) fn toggle_tui_codex_session_stop(project_id: i64, session_id: &str) -> Result<String> {
    let state_dir = ensure_agent_state_dir()?;
    toggle_tui_codex_session_stop_at(&state_dir, project_id, session_id)
}

pub(super) fn toggle_tui_codex_session_stop_at(
    state_dir: &Path,
    project_id: i64,
    session_id: &str,
) -> Result<String> {
    let store = open_agent_store_at(state_dir)?;
    let Some(control) = store.session_control_blocking(project_id, session_id)? else {
        return Ok(
            "This task does not have a live or stopped Codex session to control.".to_string(),
        );
    };

    match control.state {
        AgentSessionControlState::Running => {
            let child_pid = control
                .child_pid
                .context("The Codex session is still registering its child process; try again")?;
            let run_token = control
                .run_token
                .as_deref()
                .context("The Codex session is still registering its run; try again")?;
            if store.request_session_stop_blocking(project_id, session_id, child_pid, run_token)? {
                if let Err(error) =
                    ensure_orphaned_session_supervision(state_dir, project_id, session_id)
                {
                    return Ok(format!(
                        "Stop requested; CLT could not reattach supervision: {error:#}. The session remains fenced until its process exits."
                    ));
                }
                Ok(
                    "Stopping this Codex task session; press s again once stopped to resume it."
                        .to_string(),
                )
            } else {
                Ok("The Codex session changed before it could be stopped; try again.".to_string())
            }
        }
        AgentSessionControlState::StopRequested => {
            ensure_orphaned_session_supervision(state_dir, project_id, session_id)?;
            Ok("This Codex task session is already stopping.".to_string())
        }
        AgentSessionControlState::Stopped => {
            if control.run_token.is_none()
                && let Some(project) = store
                    .list_projects_blocking()?
                    .into_iter()
                    .find(|project| project.id == project_id)
                && task_status_for_codex_session(&project.path, session_id)?
                    == Some(TaskStatus::Todo)
            {
                return Ok("This planning session has no automated run to resume; press c to continue the conversation. Todo automation can start the task normally.".to_string());
            }
            if store.request_stopped_session_resume_blocking(
                project_id,
                session_id,
                control.run_token.as_deref(),
            )? {
                Ok("Resuming this stopped Codex task session in automated exec mode.".to_string())
            } else {
                Ok(
                    "The stopped Codex session changed before it could be resumed; try again."
                        .to_string(),
                )
            }
        }
        AgentSessionControlState::Interactive => {
            let child_pid = control
                .child_pid
                .context("The interactive Codex session is still registering; try again")?;
            let interactive_holder = control.interactive_holder.as_deref().context(
                "The interactive Codex session has no guardian identity; leave it fenced",
            )?;
            if store.request_interactive_session_stop_blocking(
                project_id,
                session_id,
                child_pid,
                interactive_holder,
            )? {
                Ok(
                    "Stopping this interactive Codex session safely; CLT will reap it and release its reservation."
                        .to_string(),
                )
            } else {
                Ok(
                    "The interactive Codex session changed before it could be stopped; try again."
                        .to_string(),
                )
            }
        }
        AgentSessionControlState::InterruptRequested
        | AgentSessionControlState::ReadyInteractive => {
            Ok("This Codex session is being used for an interactive handoff.".to_string())
        }
        AgentSessionControlState::ResumeRequested => {
            Ok("This Codex task session is already queued to resume in exec mode.".to_string())
        }
    }
}

pub(super) fn prepare_tui_codex_session_interrupt(
    project_id: i64,
    session_id: &str,
) -> Result<InteractiveAgentLease> {
    let state_dir = ensure_agent_state_dir()?;
    let lease_timeout_seconds = TUI_SESSION_HANDOFF_TIMEOUT_SECONDS.max(60);
    prepare_tui_codex_session_interrupt_at(
        &state_dir,
        project_id,
        session_id,
        lease_timeout_seconds,
        Duration::from_secs(TUI_SESSION_HANDOFF_TIMEOUT_SECONDS),
    )
}

pub(super) fn prepare_tui_codex_session_interrupt_at(
    state_dir: &Path,
    project_id: i64,
    session_id: &str,
    lease_timeout_seconds: u64,
    handoff_timeout: Duration,
) -> Result<InteractiveAgentLease> {
    let store = open_agent_store_at(state_dir)?;
    let control = store
        .session_control_blocking(project_id, session_id)?
        .with_context(|| {
            format!("Codex session {session_id} is not registered as running or stopped")
        })?;
    let interactive_holder = InteractiveAgentLease::holder_for_current_process();

    match control.state {
        AgentSessionControlState::Running => {
            // Reattach before requesting handoff so failure does not leave a
            // phantom interrupt request waiting for a nonexistent supervisor.
            ensure_orphaned_session_supervision(state_dir, project_id, session_id)?;
            let child_pid = control
                .child_pid
                .context("The Codex session is still registering its child process; try again")?;
            let run_token = control
                .run_token
                .as_deref()
                .context("The Codex session is still registering its run; try again")?;
            if !store.request_session_interrupt_blocking(
                project_id,
                session_id,
                child_pid,
                run_token,
                &interactive_holder,
            )? {
                anyhow::bail!(
                    "The Codex session changed before it could be interrupted; try again"
                );
            }
        }
        AgentSessionControlState::Stopped => {
            let lease = InteractiveAgentLease::try_acquire_with_holder_at(
                state_dir,
                project_id,
                &interactive_holder,
                lease_timeout_seconds,
            )?
            .context("The project became busy before the stopped Codex session could open")?;
            if !store.begin_stopped_session_interactive_blocking(
                project_id,
                session_id,
                &interactive_holder,
                control.run_token.as_deref(),
            )? {
                anyhow::bail!(
                    "The stopped Codex session changed before it could open interactively"
                );
            }
            return Ok(lease);
        }
        AgentSessionControlState::StopRequested => {
            anyhow::bail!("This Codex session is still stopping; try again when it is stopped")
        }
        AgentSessionControlState::InterruptRequested
        | AgentSessionControlState::ReadyInteractive
        | AgentSessionControlState::Interactive => {
            anyhow::bail!("This Codex session already has an interactive handoff in progress")
        }
        AgentSessionControlState::ResumeRequested => {
            anyhow::bail!("This Codex session is already queued to resume in exec mode")
        }
    }

    let mut pending_handoff =
        PendingInteractiveHandoff::new(state_dir, project_id, session_id, &interactive_holder);
    let started = Instant::now();
    loop {
        let control = store
            .session_control_blocking(project_id, session_id)?
            .with_context(|| format!("Codex session {session_id} disappeared during handoff"))?;
        match control.state {
            AgentSessionControlState::ReadyInteractive
                if control.interactive_holder.as_deref() == Some(interactive_holder.as_str()) =>
            {
                let lease =
                    InteractiveAgentLease::adopt_at(state_dir, project_id, &interactive_holder)?
                        .context(
                            "The Codex runner did not transfer its project lease for handoff",
                        )?;
                pending_handoff.disarm();
                return Ok(lease);
            }
            AgentSessionControlState::InterruptRequested => {
                // A session may exit just before c/i is pressed, including
                // while the scheduler is stopped. Complete the exact handoff
                // here once the whole group is absent instead of waiting for
                // a supervisor that no longer needs to be started.
                if control
                    .child_pid
                    .is_some_and(|pid| automated_agent_process_group_is_running(pid) == Some(false))
                {
                    let lease = store.lease_for_project_blocking(project_id)?;
                    reconcile_stale_agent_session_controls(
                        state_dir,
                        project_id,
                        lease.as_ref(),
                        false,
                        agent_timestamp_seconds(),
                    )?;
                }
            }
            AgentSessionControlState::ResumeRequested => {
                anyhow::bail!("The automated runner could not complete the interactive handoff")
            }
            state => anyhow::bail!(
                "Codex session {session_id} entered unexpected state {} during handoff",
                state.database_value()
            ),
        }
        if started.elapsed() >= handoff_timeout {
            anyhow::bail!("Timed out waiting for the Codex runner to enter interactive mode");
        }
        thread::sleep(Duration::from_millis(50));
    }
}

pub(super) fn queue_tui_codex_session_exec_resume(
    project_id: i64,
    session_id: &str,
    interactive_holder: &str,
) -> Result<()> {
    let state_dir = ensure_agent_state_dir()?;
    let store = open_agent_store_at(&state_dir)?;
    if store.cancel_session_interrupt_handoff_blocking(
        project_id,
        session_id,
        interactive_holder,
    )? {
        return Ok(());
    }
    let control = store
        .session_control_blocking(project_id, session_id)?
        .with_context(|| format!("Codex session {session_id} disappeared before exec resume"))?;
    if matches!(
        control.state,
        AgentSessionControlState::ResumeRequested | AgentSessionControlState::Running
    ) {
        Ok(())
    } else {
        anyhow::bail!(
            "Codex session {session_id} changed to {} before exec resume could be queued",
            control.state.database_value()
        )
    }
}

pub(super) fn reserve_tui_idle_codex_session_interactive(
    project_id: i64,
    session_id: &str,
    interactive_holder: &str,
    expected_stopped_run_token: Option<&str>,
) -> Result<bool> {
    let state_dir = ensure_agent_state_dir()?;
    with_agent_store_at(&state_dir, |store| {
        store.reserve_idle_session_interactive_blocking(
            project_id,
            session_id,
            interactive_holder,
            expected_stopped_run_token,
        )
    })
}

pub(super) fn reserve_tui_shared_codex_session_interactive(
    project_id: i64,
    session_id: &str,
    interactive_holder: &str,
    expected_stopped_run_token: Option<&str>,
) -> Result<bool> {
    let state_dir = ensure_agent_state_dir()?;
    with_agent_store_at(&state_dir, |store| {
        store.reserve_shared_session_interactive_blocking(
            project_id,
            session_id,
            interactive_holder,
            expected_stopped_run_token,
        )
    })
}

pub(super) fn cancel_tui_idle_codex_session_interactive(
    project_id: i64,
    session_id: &str,
    interactive_holder: &str,
) -> Result<bool> {
    let state_dir = ensure_agent_state_dir()?;
    with_agent_store_at(&state_dir, |store| {
        if store.cancel_idle_session_interactive_blocking(
            project_id,
            session_id,
            interactive_holder,
        )? {
            return Ok(true);
        }
        let control = store.session_control_blocking(project_id, session_id)?;
        Ok(match control {
            None => true,
            Some(control) => {
                control.state == AgentSessionControlState::Stopped
                    && control.interactive_holder.is_none()
            }
        })
    })
}

pub(super) fn spawn_agent_session_resume_worker(
    project_root: &Path,
    project_id: i64,
    session_id: &str,
) -> Result<PathBuf> {
    let executable = std::env::current_exe().context("Failed to resolve the CLT executable")?;
    let state_dir = ensure_agent_state_dir()?;
    let log_dir = state_dir.join("resume-workers");
    fs::create_dir_all(&log_dir)
        .with_context(|| format!("Failed to create resume-worker log directory {log_dir:?}"))?;
    let log_path = agent_session_resume_worker_log_path(&state_dir, project_id, session_id);
    let stderr_file = fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&log_path)
        .with_context(|| format!("Failed to open resume-worker log {log_path:?}"))?;
    let stdout_file = stderr_file
        .try_clone()
        .with_context(|| format!("Failed to clone resume-worker log {log_path:?}"))?;
    let mut command = Command::new(&executable);
    command
        .arg("--local")
        .arg("agent")
        .arg("resume-session-worker")
        .arg("--project-id")
        .arg(project_id.to_string())
        .arg("--session-id")
        .arg(session_id)
        .current_dir(project_root)
        .stdin(Stdio::null())
        .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 CLT exact-session resume worker with {}",
            executable.display()
        )
    })?;
    thread::Builder::new()
        .name(format!("clt-resume-worker-{project_id}"))
        .spawn(move || {
            let _ = child.wait();
        })
        .context("Failed to start exact-session resume-worker reaper")?;
    Ok(log_path)
}

pub(super) fn agent_session_resume_worker_log_path(
    state_dir: &Path,
    project_id: i64,
    session_id: &str,
) -> PathBuf {
    state_dir
        .join("resume-workers")
        .join(format!("p{project_id}-{session_id}.log"))
}

pub(super) fn tui_inactive_codex_session_control(
    project_id: i64,
    session_id: &str,
) -> Result<Option<agent::AgentSessionControlRecord>> {
    let state_dir = ensure_agent_state_dir()?;
    with_agent_store_at(&state_dir, |store| {
        Ok(store
            .session_control_blocking(project_id, session_id)?
            .filter(|control| {
                matches!(
                    control.state,
                    AgentSessionControlState::Stopped | AgentSessionControlState::ResumeRequested
                ) && control.child_pid.is_none()
                    && control.interactive_holder.is_none()
            }))
    })
}