End the agent's process group and the consoles it opened on every quit

AI Assisted
This commit is contained in:
Seth committed 2026-12-05 00:10:44 -08:00
1 parent 98bdbc97c3
commit 6a93395148
8 files changed
+1363 -58

No files matched your search

+31
View File
@@ -19,6 +19,9 @@
//! leaves the folder)
//! ENV <name> print the variable as `ENV <name>=<value>`
//! HANG say `HANGING`, then go silent for good (no more lines read)
//! STRAY (macOS and Linux) start a child that ignores hangup and is never
//! waited for, and print its process id as `STRAY <n>`; elsewhere
//! say `STRAY unavailable`
//! <other> echoed back as `echo: <line>`
//!
//! A change in terminal size is reported on its own as `SIZE <cols>x<rows>`.
@@ -56,6 +59,8 @@ pub enum Command {
File(String, String),
/// `HANG`: go silent, to model an agent that stops answering.
Hang,
/// `STRAY`: leave a hangup-ignoring child behind in the agent's process group.
Stray,
Echo(String),
}
@@ -134,6 +139,24 @@ fn update_state(
std::fs::write(&temp, text).is_ok() && std::fs::rename(&temp, &file).is_ok()
}
/// A child that ignores hangup, in this process's group, never waited for.
#[cfg(unix)]
fn start_stray() -> Option<u32> {
std::process::Command::new("/bin/sh")
.args(["-c", "trap '' HUP; exec sleep 300"])
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.ok()
.map(|child| child.id())
}
#[cfg(not(unix))]
fn start_stray() -> Option<u32> {
None
}
pub fn parse(line: &str) -> Command {
let line = line.trim();
if let Some(rest) = line.strip_prefix("STEP ") {
@@ -176,6 +199,9 @@ pub fn parse(line: &str) -> Command {
if line == "HANG" {
return Command::Hang;
}
if line == "STRAY" {
return Command::Stray;
}
Command::Echo(line.to_string())
}
@@ -274,6 +300,10 @@ fn main() {
thread::sleep(Duration::from_secs(3600));
}
}
Command::Stray => match start_stray() {
Some(pid) => say(&format!("STRAY {pid}")),
None => say("STRAY unavailable"),
},
Command::Echo(text) => say(&format!("echo: {text}")),
}
}
@@ -303,6 +333,7 @@ mod tests {
Command::Report("{\"phase\":2}".into())
);
assert_eq!(parse("HANG\r\n"), Command::Hang);
assert_eq!(parse("STRAY"), Command::Stray);
}
#[test]