From 7c1b64c5df07ae124c70a9a0094811cd3e23fcb5 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Mon, 12 Oct 2026 17:42:49 +0000 Subject: [PATCH] Stop identifying Apple Terminal from `TERM_SESSION_ID` (#53438) ## Why `TERM_SESSION_ID` is not specific to Apple Terminal. Treating it as an Apple Terminal identifier can mask Windows Terminal detection and the `TERM` fallback. ## What changed Remove the `TERM_SESSION_ID` detection heuristic. Apple Terminal remains detectable through `TERM_PROGRAM=Apple_Terminal`. ## Testing Update tests to cover Windows Terminal detection and the `TERM` fallback when `TERM_SESSION_ID` is present, and Apple Terminal detection with its version and user-agent token. GitOrigin-RevId: 27db11cb7bcca8f94888f9e21a6cdfcf31561b1b --- codex-rs/terminal-detection/src/lib.rs | 11 ++---- .../terminal-detection/src/terminal_tests.rs | 36 +++++++------------ 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/codex-rs/terminal-detection/src/lib.rs b/codex-rs/terminal-detection/src/lib.rs index 2c172ee778..823a180ab2 100644 --- a/codex-rs/terminal-detection/src/lib.rs +++ b/codex-rs/terminal-detection/src/lib.rs @@ -248,7 +248,8 @@ pub fn terminal_info() -> TerminalInfo { /// - `TERM_PROGRAM` (plus `TERM_PROGRAM_VERSION`) drives the detected terminal name, /// except when it identifies tmux rather than the underlying terminal. /// This means `TERM_PROGRAM` can mask later probes (for example `WT_SESSION`). -/// - Next, terminal-specific variables (WEZTERM, iTerm2, Apple Terminal, kitty, etc.) are checked. +/// - Next, terminal-specific variables (WEZTERM, iTerm2, kitty, etc.) are checked. +/// `TERM_SESSION_ID` is not terminal-specific and cannot identify Apple Terminal. /// - Finally, `TERM` is used as the capability fallback. fn detect_terminal_info_from_env(env: &dyn Environment) -> TerminalInfo { let multiplexer = detect_multiplexer(env); @@ -274,14 +275,6 @@ fn detect_terminal_info_from_env(env: &dyn Environment) -> TerminalInfo { return TerminalInfo::from_name(TerminalName::Iterm2, /*version*/ None, multiplexer); } - if env.has("TERM_SESSION_ID") { - return TerminalInfo::from_name( - TerminalName::AppleTerminal, - /*version*/ None, - multiplexer, - ); - } - if env.has("KITTY_WINDOW_ID") || env .var("TERM") diff --git a/codex-rs/terminal-detection/src/terminal_tests.rs b/codex-rs/terminal-detection/src/terminal_tests.rs index b761a767cc..49d5d1583e 100644 --- a/codex-rs/terminal-detection/src/terminal_tests.rs +++ b/codex-rs/terminal-detection/src/terminal_tests.rs @@ -153,14 +153,17 @@ fn detects_iterm2() { #[test] fn detects_apple_terminal() { - let env = FakeEnvironment::new().with_var("TERM_PROGRAM", "Apple_Terminal"); + let env = FakeEnvironment::new() + .with_var("TERM_PROGRAM", "Apple_Terminal") + .with_var("TERM_PROGRAM_VERSION", "455") + .with_var("TERM_SESSION_ID", "A1B2C3"); let terminal = detect_terminal_info_from_env(&env); assert_eq!( terminal, terminal_info( TerminalName::AppleTerminal, Some("Apple_Terminal"), - /*version*/ None, + Some("455"), /*term*/ None, /*multiplexer*/ None, ), @@ -168,28 +171,9 @@ fn detects_apple_terminal() { ); assert_eq!( terminal.user_agent_token(), - "Apple_Terminal", + "Apple_Terminal/455", "apple_term_program_user_agent" ); - - let env = FakeEnvironment::new().with_var("TERM_SESSION_ID", "A1B2C3"); - let terminal = detect_terminal_info_from_env(&env); - assert_eq!( - terminal, - terminal_info( - TerminalName::AppleTerminal, - /*term_program*/ None, - /*version*/ None, - /*term*/ None, - /*multiplexer*/ None - ), - "apple_term_session_id_info" - ); - assert_eq!( - terminal.user_agent_token(), - "Apple_Terminal", - "apple_term_session_id_user_agent" - ); } #[test] @@ -735,7 +719,9 @@ fn detects_vte() { #[test] fn detects_windows_terminal() { - let env = FakeEnvironment::new().with_var("WT_SESSION", "1"); + let env = FakeEnvironment::new() + .with_var("WT_SESSION", "1") + .with_var("TERM_SESSION_ID", "A1B2C3"); let terminal = detect_terminal_info_from_env(&env); assert_eq!( terminal, @@ -778,7 +764,9 @@ fn detects_windows_terminal() { #[test] fn detects_term_fallbacks() { - let env = FakeEnvironment::new().with_var("TERM", "xterm-256color"); + let env = FakeEnvironment::new() + .with_var("TERM", "xterm-256color") + .with_var("TERM_SESSION_ID", "A1B2C3"); let terminal = detect_terminal_info_from_env(&env); assert_eq!( terminal,