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
This commit is contained in:
jif
2026-12-01 13:42:02 +00:00
committed by copyberry
parent 9a8b299471
commit 33aab1f2c6
6 changed files with 22 additions and 67 deletions
@@ -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<Self::Snapshot> {
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<Self::Snapshot> {
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())],
),
}
}
}
@@ -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;
+6 -23
View File
@@ -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(),
);
@@ -36,7 +36,6 @@ async fn command_sessions_world_state_tracks_retained_processes() -> Result<()>
Feature::CodeMode,
Feature::CodeModeOnly,
Feature::CodeModeHost,
Feature::CommandSessionsWorldState,
] {
config
.features
@@ -23,12 +23,8 @@ Scenario: A Code Mode script drops its command handle. WorldState retains it whi
- function/wait_agent
01:message/developer:
<MODEL_INSTRUCTIONS>
02:message/developer[2]:
[01] <command_sessions>
Current command sessions for this thread (supersedes earlier snapshots). Exited sessions retain results ... busy with a tool interaction or result finalization. [hash=e247ea1c0c96e976]
None.
</command_sessions>
[02] <PERMISSIONS_INSTRUCTIONS>
02:message/developer:
<PERMISSIONS_INSTRUCTIONS>
03:message/developer:
<MULTI_AGENT_ROLE>
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:
<MODEL_INSTRUCTIONS>
02:message/developer[2]:
[01] <command_sessions>
Current command sessions for this thread (supersedes earlier snapshots). Exited sessions retain results ... busy with a tool interaction or result finalization. [hash=e247ea1c0c96e976]
None.
</command_sessions>
[02] <PERMISSIONS_INSTRUCTIONS>
02:message/developer:
<PERMISSIONS_INSTRUCTIONS>
03:message/developer:
<MULTI_AGENT_ROLE>
04:message/developer:
+2 -2
View File
@@ -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 {