Track context windows per agent (#37347)

## Why

Forked subagents can inherit a parent's compacted history, but their context-window metadata needs to identify the child and start a distinct window lineage.

## What changed

- Always identify context windows by agent name and remove the `features.token_budget.mode` setting.
- Persist the agent path in world state and emit context-window metadata as a standalone developer message when the identity changes.
- Reset inherited compaction metadata to the forked subagent's initial context window and restore that window in session state.

## Testing

Extend the full-history subagent test to verify parent and child world-state snapshots, agent identities, distinct window IDs, and the reset child checkpoint.

GitOrigin-RevId: 6584201e57b3d2fba0ce626b89a8b3b0b3c752db
This commit is contained in:
Boyang Niu authored and copyberry committed 2026-08-07 01:05:33 +00:00
1 parent 270d93268c
commit 69b6152c17
15 files changed
+195 -111

No files matched your search

+20
View File
@@ -595,6 +595,20 @@ impl Session {
}
});
let initial_auto_compact_window_ids = AutoCompactWindowIds::new_initial();
let restore_child_window = matches!(&initial_history, InitialHistory::Forked(_))
&& session_configuration.session_source.is_non_root_agent()
&& config.features.enabled(Feature::TokenBudget);
if restore_child_window && let InitialHistory::Forked(items) = &mut initial_history {
let child_window_id = initial_auto_compact_window_ids.window_id.to_string();
for item in items {
if let RolloutItem::Compacted(checkpoint) = item {
checkpoint.window_number = Some(0);
checkpoint.first_window_id = Some(child_window_id.clone());
checkpoint.previous_window_id = None;
checkpoint.window_id = Some(child_window_id.clone());
}
}
}
let agent_control = agent_control.with_session_id(
session_id,
config
@@ -1313,6 +1327,12 @@ impl Session {
// record_initial_history can emit events. We record only after the SessionConfiguredEvent is emitted.
Box::pin(sess.record_initial_history(initial_history)).await;
if restore_child_window {
sess.state.lock().await.restore_auto_compact_window(
/*window_number*/ 0,
initial_auto_compact_window_ids,
);
}
if matches!(&sess.fork_persistence, ForkPersistence::Referenced { .. }) {
// Keep the source reserved until the child's history reference is durable.
sess.try_ensure_rollout_materialized().await?;