Preserve context baselines across nested agent forks (#41424)

## What changed

- Treat a surviving full world-state snapshot as a context baseline when a fork removes the associated user message.
- Restore the previous turn settings and reference context from that baseline, without treating the segment as a user turn for rollback.
- Ignore partial snapshots and full snapshots superseded by compaction.

## Testing

- Cover resumed rollout reconstruction with removed task messages, partial snapshots, and compaction.
- Verify nested agents inherit developer instructions and environment context exactly once across history modes and compacted histories.

GitOrigin-RevId: 9f9f6992d7ea8cff9ac82a7fcdf0c8fb764db81c
This commit is contained in:
jif authored and copyberry committed 2026-08-28 21:53:14 +00:00
1 parent 0918cd2c08
commit f9cdc90c2c
3 files changed
+333 -9

No files matched your search

@@ -78,6 +78,16 @@ fn finalize_active_segment<'a>(
return;
}
// Full world-state snapshots are persisted after installing initial context. They still
// establish a baseline when a child fork removes the parent turn's agent message. Do not
// count these context-only segments as user turns for rollback, or use a snapshot from
// before the segment's latest compaction.
let has_context_baseline = active_segment.counts_as_user_turn
|| active_segment
.world_state_replay
.iter()
.take_while(|item| !matches!(item, RolloutItem::Compacted(_)))
.any(|item| matches!(item, RolloutItem::WorldState(state) if state.full));
world_state_replay.extend(active_segment.world_state_replay);
// A surviving replacement-history checkpoint is a complete history base. Once we
@@ -92,15 +102,15 @@ fn finalize_active_segment<'a>(
*window = active_segment.window;
}
// `previous_turn_settings` come from the newest surviving user turn that established them.
if previous_turn_settings.is_none() && active_segment.counts_as_user_turn {
// Restore settings from the newest surviving context baseline.
if previous_turn_settings.is_none() && has_context_baseline {
*previous_turn_settings = active_segment.previous_turn_settings;
}
// `reference_context_item` comes from the newest surviving user turn baseline, or
// `reference_context_item` comes from the newest surviving context baseline, or
// from a surviving compaction that explicitly cleared that baseline.
if matches!(reference_context_item, TurnReferenceContextItem::NeverSet)
&& (active_segment.counts_as_user_turn
&& (has_context_baseline
|| matches!(
active_segment.reference_context_item,
TurnReferenceContextItem::Cleared