Skip to content

Commit 8ac9542

Browse files
authored
fix: clear inherited terminal and agent session identity (#4461)
refs #4104
1 parent 2836010 commit 8ac9542

2 files changed

Lines changed: 105 additions & 16 deletions

File tree

‎docs/next/website/src/content/docs/cli-reference.mdx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,17 @@ terminal size, and values smaller than the popup minimum are clamped. A popup
517517
is not a Herdr pane, does not export `HERDR_PANE_ID`, and does not participate
518518
in pane or agent APIs. Native non-terminal plugin panes are outside plugin v1.
519519

520+
New panes identify as `TERM_PROGRAM=herdr`, with `TERM_PROGRAM_VERSION` set to
521+
Herdr's version. They keep `TERM=xterm-256color` and `COLORTERM=truecolor`.
522+
Herdr removes inherited outer-terminal session markers (iTerm2, WezTerm, Kitty,
523+
Windows Terminal, tmux, screen, and Zellij), iTerm2's `LC_TERMINAL` identity,
524+
and known Claude Code, Codex, and OMP session markers before applying explicit
525+
launch environment values. This prevents new panes from claiming the terminal
526+
or agent session that originally started the server. Ordinary environment
527+
variables, including API keys and display settings, remain inherited rather than
528+
being replaced with the attaching client's environment. SSH-agent forwarding has
529+
its own reconnect handling. Existing pane processes are unchanged by this cleanup.
530+
520531
`--env KEY=VALUE` can be repeated on process-launching commands. It applies to the newly launched process only. Herdr-managed variables such as `HERDR_SOCKET_PATH`, `HERDR_BIN_PATH`, `HERDR_ENV`, `HERDR_WORKSPACE_ID`, `HERDR_TAB_ID`, `HERDR_PANE_ID`, `HERDR_PLUGIN_ID`, `HERDR_PLUGIN_ROOT`, `HERDR_PLUGIN_CONFIG_DIR`, `HERDR_PLUGIN_STATE_DIR`, `HERDR_PLUGIN_ENTRYPOINT_ID`, and `HERDR_PLUGIN_CONTEXT_JSON` stay authoritative when they conflict with caller-provided env.
521532

522533
## Read sources

‎src/pane.rs‎

Lines changed: 94 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,25 @@ fn apply_pane_terminal_env(cmd: &mut CommandBuilder) {
9696
// when the remote side lacks matching terminfo entries.
9797
cmd.env("TERM", PANE_TERM);
9898
cmd.env("COLORTERM", PANE_COLORTERM);
99-
cmd.env_remove("WT_SESSION");
99+
cmd.env("TERM_PROGRAM", "herdr");
100+
cmd.env("TERM_PROGRAM_VERSION", crate::build_info::version());
101+
// Host handles refer to the outer terminal, never to this pane.
102+
for key in [
103+
"ITERM_SESSION_ID",
104+
"LC_TERMINAL",
105+
"LC_TERMINAL_VERSION",
106+
"WEZTERM_PANE",
107+
"KITTY_WINDOW_ID",
108+
"WT_SESSION",
109+
"TMUX",
110+
"TMUX_PANE",
111+
"STY",
112+
"ZELLIJ",
113+
"ZELLIJ_SESSION_NAME",
114+
"ZELLIJ_PANE_ID",
115+
] {
116+
cmd.env_remove(key);
117+
}
100118
}
101119

102120
#[derive(Debug, Clone, Default, PartialEq, Eq)]
@@ -148,10 +166,18 @@ impl PaneLaunchEnv {
148166
fn apply_pane_launch_env(cmd: &mut CommandBuilder, launch_env: &PaneLaunchEnv) {
149167
#[cfg(unix)]
150168
crate::platform::ssh_agent::apply_pane_env(cmd);
151-
cmd.env_remove("CODEX_THREAD_ID");
152-
// OMP sets OMPCODE for shells it spawns. A pane launched from inside OMP
153-
// must not inherit it or its root agent would look like a nested session.
154-
cmd.env_remove("OMPCODE");
169+
// A new pane is not a child agent of the process that started the server.
170+
// Explicit launch env below can opt back into an intentional child session.
171+
for key in [
172+
"CODEX_THREAD_ID",
173+
"OMPCODE",
174+
"CLAUDECODE",
175+
"CLAUDE_CODE_CHILD_SESSION",
176+
"CLAUDE_CODE_SESSION_ID",
177+
"CLAUDE_CODE_MESSAGING_TOKEN",
178+
] {
179+
cmd.env_remove(key);
180+
}
155181
for (key, value) in &launch_env.extra {
156182
cmd.env(key, value);
157183
}
@@ -3715,6 +3741,7 @@ impl PaneRuntime {
37153741
#[cfg(test)]
37163742
mod tests {
37173743
use super::*;
3744+
use std::ffi::OsStr;
37183745

37193746
#[tokio::test]
37203747
async fn clear_pane_preserves_wrapped_input_and_unfinished_vt_sequence() {
@@ -3824,33 +3851,84 @@ mod tests {
38243851
}
38253852

38263853
#[test]
3827-
fn pane_launch_env_removes_outer_codex_thread_id() {
3854+
fn pane_launch_env_removes_outer_agent_identity() {
3855+
let keys = [
3856+
"CODEX_THREAD_ID",
3857+
"OMPCODE",
3858+
"CLAUDECODE",
3859+
"CLAUDE_CODE_CHILD_SESSION",
3860+
"CLAUDE_CODE_SESSION_ID",
3861+
"CLAUDE_CODE_MESSAGING_TOKEN",
3862+
];
38283863
let mut cmd = CommandBuilder::new("shell");
3829-
cmd.env("CODEX_THREAD_ID", "outer-session");
3864+
for key in keys {
3865+
cmd.env(key, "outer-session");
3866+
}
3867+
cmd.env("ANTHROPIC_API_KEY", "fake-api-key");
3868+
cmd.env("DISPLAY", ":42");
38303869

38313870
apply_pane_launch_env(&mut cmd, &PaneLaunchEnv::default());
38323871

3833-
assert!(cmd.get_env("CODEX_THREAD_ID").is_none());
3872+
for key in keys {
3873+
assert!(cmd.get_env(key).is_none(), "{key} must not leak into panes");
3874+
}
3875+
assert_eq!(
3876+
cmd.get_env("ANTHROPIC_API_KEY"),
3877+
Some(OsStr::new("fake-api-key"))
3878+
);
3879+
assert_eq!(cmd.get_env("DISPLAY"), Some(OsStr::new(":42")));
38343880
}
38353881

38363882
#[test]
3837-
fn pane_launch_env_removes_outer_ompcode_marker() {
3883+
fn pane_terminal_identity_removes_outer_terminal_identity() {
3884+
let keys = [
3885+
"ITERM_SESSION_ID",
3886+
"LC_TERMINAL",
3887+
"LC_TERMINAL_VERSION",
3888+
"WEZTERM_PANE",
3889+
"KITTY_WINDOW_ID",
3890+
"WT_SESSION",
3891+
"TMUX",
3892+
"TMUX_PANE",
3893+
"STY",
3894+
"ZELLIJ",
3895+
"ZELLIJ_SESSION_NAME",
3896+
"ZELLIJ_PANE_ID",
3897+
];
38383898
let mut cmd = CommandBuilder::new("shell");
3839-
cmd.env("OMPCODE", "1");
3899+
for key in keys {
3900+
cmd.env(key, "outer-session");
3901+
}
3902+
cmd.env("TERM_PROGRAM", "iTerm.app");
3903+
cmd.env("TERM_PROGRAM_VERSION", "outer-version");
38403904

3841-
apply_pane_launch_env(&mut cmd, &PaneLaunchEnv::default());
3905+
apply_pane_terminal_env(&mut cmd);
38423906

3843-
assert!(cmd.get_env("OMPCODE").is_none());
3907+
for key in keys {
3908+
assert!(cmd.get_env(key).is_none(), "{key} must not leak into panes");
3909+
}
3910+
assert_eq!(cmd.get_env("TERM_PROGRAM"), Some(OsStr::new("herdr")));
3911+
assert_eq!(
3912+
cmd.get_env("TERM_PROGRAM_VERSION"),
3913+
Some(OsStr::new(&crate::build_info::version()))
3914+
);
38443915
}
38453916

38463917
#[test]
3847-
fn pane_terminal_identity_removes_outer_windows_terminal_session() {
3918+
fn pane_launch_env_allows_explicit_session_identity() {
3919+
let extra = vec![
3920+
("CLAUDE_CODE_CHILD_SESSION".into(), "1".into()),
3921+
("CLAUDE_CODE_SESSION_ID".into(), "intentional-child".into()),
3922+
("CLAUDE_CODE_MESSAGING_TOKEN".into(), "fake-token".into()),
3923+
("ITERM_SESSION_ID".into(), "intentional-host".into()),
3924+
];
38483925
let mut cmd = CommandBuilder::new("shell");
3849-
cmd.env("WT_SESSION", "outer-session");
3850-
38513926
apply_pane_terminal_env(&mut cmd);
3927+
apply_pane_launch_env(&mut cmd, &PaneLaunchEnv::from_extra(extra.clone()));
38523928

3853-
assert!(cmd.get_env("WT_SESSION").is_none());
3929+
for (key, value) in extra {
3930+
assert_eq!(cmd.get_env(key), Some(OsStr::new(&value)));
3931+
}
38543932
}
38553933

38563934
#[tokio::test]

0 commit comments

Comments
 (0)