Expose retained command sessions in incremental world state (#59633)

## Why

A Code Mode script can start a command without returning its session handle. Make retained sessions visible to the model so it can find running commands and collect exited results with `write_stdin`.

## What changed

- Add the disabled-by-default `command_sessions_world_state` feature.
- Publish snapshots of up to 16 retained sessions with their IDs, running or exited status, and whether they are in use. Report omitted sessions and suppress unchanged updates.
- Preserve session visibility when `write_stdin` is unavailable and refresh snapshots after compaction. Clear stale session handles after restart or when the feature is disabled.

## Testing

Add an end-to-end scenario and model-visible history snapshot covering a dropped command handle, unchanged state, unavailable polling, compaction, exit and result collection, and stale-handle cleanup after restart.

GitOrigin-RevId: 45704d8c6403149997231aafb8496c12879aff23
This commit is contained in:
jif
2026-11-05 11:14:26 +00:00
committed by copyberry
parent c0100ad4ec
commit d4902cedbf
12 changed files with 675 additions and 1 deletions
+3 -1
View File
@@ -2033,7 +2033,9 @@ impl Session {
state.last_started_turn_id = last_started_turn_id;
state.turn_attribution = turn_attribution;
state.restored_turn_routing = restored_turn_routing;
if let Some(world_state) = world_state_baseline {
if let Some(mut world_state) = world_state_baseline {
// Handles belong to this runtime; a saved checkpoint can also lag its fragment.
world_state.invalidate_section("command_sessions");
state.history.set_world_state_baseline(world_state);
}
let fallback_ids = state.auto_compact_window_ids();
+23
View File
@@ -171,6 +171,29 @@ 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 extension_metrics = super::extension_metrics::from_session_telemetry(
step_context.session_telemetry.clone(),
);