From e850cd429232038fea9eca815d45facfcb4886f9 Mon Sep 17 00:00:00 2001 From: Savin Angel-Mario Date: Wed, 18 Nov 2026 22:18:47 +0200 Subject: [PATCH 1/2] Keep Claude Code alive when a Codex subagent starts The Codex helper reads the login shell's PATH with `$SHELL -l -i`, and it runs inside Claude Code's terminal. An interactive zsh with a controlling terminal takes over the terminal's foreground while it runs, about half a second on every Codex turn. Claude Code was a background job on its own terminal for that time, and when it touched the terminal then, it died, taking the helper with it. Since #124 this happened on any turn. The login shell now starts in a session of its own (setsid), without a controlling terminal, so it can't take the terminal from anyone. This also covers the --mcp-title server, which runs in the same terminal. --- src/platform.rs | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/platform.rs b/src/platform.rs index 0aed546..bcb5dd5 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -241,13 +241,10 @@ fn print_path_script() -> String { #[cfg(any(unix, test))] fn path_from_shell(shell: &OsStr, script: &str, wait: std::time::Duration) -> Option { use std::io::Read; - let mut child = Command::new(shell) - .args(["-l", "-i", "-c", script]) - .stdin(Stdio::null()) - .stdout(Stdio::piped()) - .stderr(Stdio::null()) - .spawn() - .ok()?; + let mut command = Command::new(shell); + command.args(["-l", "-i", "-c", script]).stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::null()); + without_terminal(&mut command); + let mut child = command.spawn().ok()?; let mut stdout = child.stdout.take()?; let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { @@ -262,6 +259,21 @@ fn path_from_shell(shell: &OsStr, script: &str, wait: std::time::Duration) -> Op path_between_markers(&output.ok()?).map(std::ffi::OsString::from) } +/// Start `command` in a session of its own, without a controlling terminal. An interactive shell (`-i`) that has one +/// takes over the terminal's foreground for as long as it runs. The Codex helper and the MCP server run inside Claude +/// Code's terminal, so a PATH read from there took the terminal from Claude Code, which then died. +#[cfg(unix)] +fn without_terminal(command: &mut Command) { + use std::os::unix::process::CommandExt; + // SAFETY: `setsid` is async-signal-safe, so it may run between fork and exec. + unsafe { + command.pre_exec(|| if libc::setsid() == -1 { Err(std::io::Error::last_os_error()) } else { Ok(()) }); + } +} + +#[cfg(not(unix))] +fn without_terminal(_command: &mut Command) {} + /// The text between the markers, ignoring whatever the shell's startup files printed around them. #[cfg(any(unix, test))] fn path_between_markers(output: &str) -> Option<&str> { @@ -716,6 +728,16 @@ bye assert_eq!(path_of(&with), Some("/nvm/bin:/usr/bin".into())); } + /// A shell left in this process's session would share its terminal, and an interactive one takes it over. + #[test] + #[cfg(unix)] + fn the_login_shell_runs_without_this_processs_terminal() { + let script = format!("printf '%s%s%s' '{PATH_BEGIN}' \"$(ps -o pgid= -p $$ | tr -d ' ')\" '{PATH_END}'"); + let group = path_from_shell(OsStr::new("/bin/sh"), &script, std::time::Duration::from_secs(20)).expect("the shell printed its group"); + let ours = unsafe { libc::getpgrp() }; + assert_ne!(group.to_string_lossy(), ours.to_string(), "the shell must run in a session of its own"); + } + #[test] fn a_shell_answers_with_its_path_and_a_hung_one_is_given_up_on() { let Some(bash) = posix_shell() else { return }; From 0b97814864f2333b1e1d36258025b2d24abe6110 Mon Sep 17 00:00:00 2001 From: Savin Angel-Mario Date: Wed, 18 Nov 2026 22:26:17 +0200 Subject: [PATCH 2/2] Build without_terminal on Windows only where path_from_shell is --- src/platform.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform.rs b/src/platform.rs index bcb5dd5..7201f51 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -271,7 +271,7 @@ fn without_terminal(command: &mut Command) { } } -#[cfg(not(unix))] +#[cfg(all(not(unix), test))] fn without_terminal(_command: &mut Command) {} /// The text between the markers, ignoring whatever the shell's startup files printed around them.