From 33aab1f2c6d14a2caf30450a73e5f4cbbf2117ba Mon Sep 17 00:00:00 2001 From: jif Date: Tue, 1 Dec 2026 13:31:20 +0000 Subject: [PATCH] Always track retained command sessions in world state (#64993) ## What changed Track retained command sessions on every turn and mark `command_sessions_world_state` as a removed compatibility flag. When `write_stdin` is unavailable, keep reporting retained sessions and indicate that they cannot be polled. Omit the command session section when there are no sessions and no prior section. Still emit an empty update when needed to clear stale handles. ## Testing Update the Code Mode scenario to exercise retained command session tracking without enabling the flag. Its snapshot omits the empty section before any commands are retained. GitOrigin-RevId: fd953750b06f0098ae6382ce3d3115a1263fd305 --- .../context/world_state/command_sessions.rs | 38 +++++-------------- codex-rs/core/src/context/world_state/mod.rs | 1 - codex-rs/core/src/session/world_state.rs | 29 +++----------- .../suite/scenarios_command_sessions_tests.rs | 1 - ...essions__command_sessions_world_state.snap | 16 ++------ codex-rs/features/src/lib.rs | 4 +- 6 files changed, 22 insertions(+), 67 deletions(-) diff --git a/codex-rs/core/src/context/world_state/command_sessions.rs b/codex-rs/core/src/context/world_state/command_sessions.rs index b7e662a821..95cccb942f 100644 --- a/codex-rs/core/src/context/world_state/command_sessions.rs +++ b/codex-rs/core/src/context/world_state/command_sessions.rs @@ -58,45 +58,27 @@ impl WorldStateSection for CommandSessionsState { true } + fn matches_legacy_fragment(role: &str, text: &str) -> bool { + Self::matches_retained_fragment(role, text) + } + fn matches_retained_fragment(role: &str, text: &str) -> bool { role == "developer" && Self::matches_text(text) } - fn render_diff( - &self, - previous: PreviousSectionState<'_, Self::Snapshot>, - ) -> SectionTransition { - if matches!(previous, PreviousSectionState::Known(previous) if previous == self) { - return (None, Vec::new()); - } - ( - Some(self.clone()), - vec![WorldStateUpdate::fragment(self.clone())], - ) - } -} - -/// Clears stale handles when disabled, or preserves live handles when polling is unavailable. -#[derive(Default)] -pub(crate) struct DisabledCommandSessionsState(pub(crate) CommandSessionsState); - -impl WorldStateSection for DisabledCommandSessionsState { - const ID: &'static str = CommandSessionsState::ID; - type Snapshot = CommandSessionsState; - - fn matches_legacy_fragment(role: &str, text: &str) -> bool { - CommandSessionsState::matches_retained_fragment(role, text) - } - fn render_diff( &self, previous: PreviousSectionState<'_, Self::Snapshot>, ) -> SectionTransition { match previous { - PreviousSectionState::Absent if self.0.sessions.is_empty() => (None, Vec::new()), + PreviousSectionState::Absent if self.sessions.is_empty() => (None, Vec::new()), + PreviousSectionState::Known(previous) if previous == self => (None, Vec::new()), PreviousSectionState::Absent | PreviousSectionState::Unknown - | PreviousSectionState::Known(_) => self.0.render_diff(previous), + | PreviousSectionState::Known(_) => ( + Some(self.clone()), + vec![WorldStateUpdate::fragment(self.clone())], + ), } } } diff --git a/codex-rs/core/src/context/world_state/mod.rs b/codex-rs/core/src/context/world_state/mod.rs index e3d64da9d8..f1922d045f 100644 --- a/codex-rs/core/src/context/world_state/mod.rs +++ b/codex-rs/core/src/context/world_state/mod.rs @@ -49,7 +49,6 @@ pub(crate) use collaboration_mode::CollaborationModeState; pub(crate) use command_sessions::CommandSession; pub(crate) use command_sessions::CommandSessionStatus; pub(crate) use command_sessions::CommandSessionsState; -pub(crate) use command_sessions::DisabledCommandSessionsState; pub(crate) use compact_permissions::CompactPermissionsState; pub(crate) use context_window_guidance::ContextWindowGuidanceState; pub(crate) use environment::EnvironmentsState; diff --git a/codex-rs/core/src/session/world_state.rs b/codex-rs/core/src/session/world_state.rs index 3b18c0ea3d..bed6c9f0c8 100644 --- a/codex-rs/core/src/session/world_state.rs +++ b/codex-rs/core/src/session/world_state.rs @@ -211,29 +211,12 @@ impl Session { String::new() }; let mut world_state = WorldState::default(); - if turn_context - .config - .features - .enabled(Feature::CommandSessionsWorldState) - { - let mut sessions = - crate::unified_exec::command_sessions_state(&self.services.unified_exec_manager) - .await; - if step_context - .tool_router - .exposes_tool(&ToolName::plain("write_stdin")) - { - world_state.add_section(sessions); - } else { - sessions.polling_unavailable = true; - world_state.add_section(crate::context::world_state::DisabledCommandSessionsState( - sessions, - )); - } - } else { - world_state - .add_section(crate::context::world_state::DisabledCommandSessionsState::default()); - } + let mut sessions = + crate::unified_exec::command_sessions_state(&self.services.unified_exec_manager).await; + sessions.polling_unavailable = !step_context + .tool_router + .exposes_tool(&ToolName::plain("write_stdin")); + world_state.add_section(sessions); let extension_metrics = super::extension_metrics::from_session_telemetry( step_context.session_telemetry.clone(), ); diff --git a/codex-rs/core/tests/suite/scenarios_command_sessions_tests.rs b/codex-rs/core/tests/suite/scenarios_command_sessions_tests.rs index 21496b6e9b..d581f53c9f 100644 --- a/codex-rs/core/tests/suite/scenarios_command_sessions_tests.rs +++ b/codex-rs/core/tests/suite/scenarios_command_sessions_tests.rs @@ -36,7 +36,6 @@ async fn command_sessions_world_state_tracks_retained_processes() -> Result<()> Feature::CodeMode, Feature::CodeModeOnly, Feature::CodeModeHost, - Feature::CommandSessionsWorldState, ] { config .features diff --git a/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__command_sessions__command_sessions_world_state.snap b/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__command_sessions__command_sessions_world_state.snap index eab992915d..ce928ae13b 100644 --- a/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__command_sessions__command_sessions_world_state.snap +++ b/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__command_sessions__command_sessions_world_state.snap @@ -23,12 +23,8 @@ Scenario: A Code Mode script drops its command handle. WorldState retains it whi - function/wait_agent 01:message/developer: -02:message/developer[2]: - [01] - Current command sessions for this thread (supersedes earlier snapshots). Exited sessions retain results ... busy with a tool interaction or result finalization. [hash=e247ea1c0c96e976] - None. - - [02] +02:message/developer: + 03:message/developer: 04:message/developer: @@ -74,12 +70,8 @@ Scenario: A Code Mode script drops its command handle. WorldState retains it whi - function/wait_agent 01:message/developer: -02:message/developer[2]: - [01] - Current command sessions for this thread (supersedes earlier snapshots). Exited sessions retain results ... busy with a tool interaction or result finalization. [hash=e247ea1c0c96e976] - None. - - [02] +02:message/developer: + 03:message/developer: 04:message/developer: diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index 5ff59b4251..ce1aaa78dd 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -330,7 +330,7 @@ pub enum Feature { ToolSearchAlwaysDeferMcpTools, /// Describe deferred tool namespaces in the model-visible world state. DeferredToolWorldState, - /// Publish retained command sessions through incremental world state. + /// Removed compatibility flag. World state always tracks retained command sessions. CommandSessionsWorldState, /// Track top-level tool definitions in world state and emit incremental context updates. IncrementalTools, @@ -1972,7 +1972,7 @@ pub const FEATURES: &[FeatureSpec] = &[ FeatureSpec { id: Feature::CommandSessionsWorldState, key: "command_sessions_world_state", - stage: Stage::UnderDevelopment, + stage: Stage::Removed, default_enabled: false, }, FeatureSpec {