Preserve Guardian history across thread reconstruction (#42065)

## Why

Guardian review evidence must remain available after a compacted thread is resumed or forked, while rolled-back or parent-local evidence must not grant authorization in a different context.

## What changed

- Store the bounded, model-invisible Guardian transcript alongside compacted rollout history and restore it from the newest surviving checkpoint during replay.
- Trim Guardian history at rollback boundaries, clearing it when the boundary has already been evicted.
- Preserve the checkpoint for user-initiated forks, but remove it when spawning a subagent so parent review evidence is not inherited.
- Keep the new rollout field optional for compatibility with existing compacted records and legacy readers.

## Testing

- Cover Guardian history across compaction, restart, paginated and pathless stores, user forks, rollback, bounded replay, serialization, and subagent forks.

GitOrigin-RevId: 6ab076d5e0c3aac4d6a41ebd50ba2b200c90213e
This commit is contained in:
jif authored and copyberry committed 2026-09-01 14:33:58 +00:00
1 parent 3a04482645
commit 28097e98eb
23 files changed
+369 -27

No files matched your search

@@ -10,6 +10,7 @@ use uuid::Uuid;
#[derive(Debug)]
pub(super) struct RolloutReconstruction {
pub(super) history: Vec<ResponseItemEnvelope>,
pub(super) guardian_history: Option<codex_history::GuardianHistoryCheckpoint>,
pub(super) previous_turn_settings: Option<PreviousTurnSettings>,
pub(super) reference_context_item: Option<TurnContextItem>,
pub(super) world_state_baseline: Option<WorldStateSnapshot>,
@@ -43,6 +44,13 @@ enum TurnReferenceContextItem {
Latest(Box<TurnContextItem>),
}
#[derive(Debug, Clone, Copy)]
// The selected checkpoint and its replay tail must belong to the same surviving segment.
struct ReplayCheckpoint<'a> {
compacted: &'a CompactedItem,
suffix: &'a [RolloutItem],
}
#[derive(Debug, Default)]
struct ActiveReplaySegment<'a> {
turn_id: Option<String>,
@@ -50,7 +58,7 @@ struct ActiveReplaySegment<'a> {
previous_turn_settings: Option<PreviousTurnSettings>,
reference_context_item: TurnReferenceContextItem,
world_state_replay: Vec<&'a RolloutItem>,
base_replacement_history: Option<&'a [ResponseItemEnvelope]>,
base_compaction: Option<ReplayCheckpoint<'a>>,
window: Option<ReconstructedWindow>,
}
@@ -61,7 +69,7 @@ fn turn_ids_are_compatible(active_turn_id: Option<&str>, item_turn_id: Option<&s
fn finalize_active_segment<'a>(
active_segment: ActiveReplaySegment<'a>,
base_replacement_history: &mut Option<&'a [ResponseItemEnvelope]>,
base_compaction: &mut Option<ReplayCheckpoint<'a>>,
previous_turn_settings: &mut Option<PreviousTurnSettings>,
reference_context_item: &mut TurnReferenceContextItem,
world_state_replay: &mut Vec<&'a RolloutItem>,
@@ -92,10 +100,10 @@ fn finalize_active_segment<'a>(
// A surviving replacement-history checkpoint is a complete history base. Once we
// know the newest surviving one, older rollout items do not affect rebuilt history.
if base_replacement_history.is_none()
&& let Some(segment_base_replacement_history) = active_segment.base_replacement_history
if base_compaction.is_none()
&& let Some(segment_base_compaction) = active_segment.base_compaction
{
*base_replacement_history = Some(segment_base_replacement_history);
*base_compaction = Some(segment_base_compaction);
}
if window.is_none() {
@@ -147,7 +155,7 @@ impl Session {
_ => None,
})
};
let mut base_replacement_history: Option<&[ResponseItemEnvelope]> = None;
let mut base_compaction = None;
let mut previous_turn_settings = None;
let mut reference_context_item = TurnReferenceContextItem::NeverSet;
let mut world_state_replay = Vec::new();
@@ -155,9 +163,6 @@ impl Session {
// Rollback is "drop the newest N user turns". While scanning in reverse, that becomes
// "skip the next N user-turn segments we finalize".
let mut pending_rollback_turns = 0usize;
// Borrowed suffix of rollout items newer than the newest surviving replacement-history
// checkpoint. If no such checkpoint exists, this remains the full rollout.
let mut rollout_suffix = rollout_items;
// Reverse replay accumulates rollout items into the newest in-progress turn segment until
// we hit its matching `TurnStarted`, at which point the segment can be finalized.
let mut active_segment: Option<ActiveReplaySegment<'_>> = None;
@@ -189,11 +194,13 @@ impl Session {
) {
active_segment.reference_context_item = TurnReferenceContextItem::Cleared;
}
if active_segment.base_replacement_history.is_none()
&& let Some(replacement_history) = &compacted.replacement_history
if active_segment.base_compaction.is_none()
&& compacted.replacement_history.is_some()
{
active_segment.base_replacement_history = Some(replacement_history);
rollout_suffix = &rollout_items[index + 1..];
active_segment.base_compaction = Some(ReplayCheckpoint {
compacted,
suffix: &rollout_items[index + 1..],
});
}
}
RolloutItem::EventMsg(EventMsg::ThreadRolledBack(rollback)) => {
@@ -270,7 +277,7 @@ impl Session {
{
finalize_active_segment(
active_segment,
&mut base_replacement_history,
&mut base_compaction,
&mut previous_turn_settings,
&mut reference_context_item,
&mut world_state_replay,
@@ -298,7 +305,7 @@ impl Session {
| RolloutItem::InterAgentCommunicationMetadata { .. } => {}
}
if base_replacement_history.is_some()
if base_compaction.is_some()
&& previous_turn_settings.is_some()
&& !matches!(reference_context_item, TurnReferenceContextItem::NeverSet)
{
@@ -312,7 +319,7 @@ impl Session {
if let Some(active_segment) = active_segment.take() {
finalize_active_segment(
active_segment,
&mut base_replacement_history,
&mut base_compaction,
&mut previous_turn_settings,
&mut reference_context_item,
&mut world_state_replay,
@@ -331,12 +338,16 @@ impl Session {
let mut history = ContextManager::new();
let mut saw_legacy_compaction_without_replacement_history = false;
if let Some(base_replacement_history) = base_replacement_history {
history.replace_annotated(base_replacement_history.to_vec());
if let Some(checkpoint) = base_compaction
&& let Some(items) = &checkpoint.compacted.replacement_history
{
history.replace_annotated(items.clone());
history.restore_guardian_history(checkpoint.compacted.guardian_history.as_ref());
}
// Materialize exact history semantics from the replay-derived suffix. The eventual lazy
// design should keep this same replay shape, but drive it from a resumable reverse source
// instead of an eagerly loaded `&[RolloutItem]`.
let rollout_suffix = base_compaction.map_or(rollout_items, |checkpoint| checkpoint.suffix);
for item in rollout_suffix {
match item {
RolloutItem::ResponseItem(response_item) => {
@@ -354,11 +365,10 @@ impl Session {
}
RolloutItem::InterAgentCommunicationMetadata { .. } => {}
RolloutItem::Compacted(compacted) => {
if let Some(replacement_history) = &compacted.replacement_history {
// This should actually never happen, because the reverse loop above (to build rollout_suffix)
// should stop before any compaction that has Some replacement_history
history.replace_annotated(replacement_history.clone());
} else {
// Reverse replay already chose the newest surviving checkpoint. Any newer
// replacement checkpoint belongs to a rolled-back turn; replay its original
// items so the rollback can still find the removed user boundary.
if compacted.replacement_history.is_none() {
saw_legacy_compaction_without_replacement_history = true;
// Legacy rollouts without `replacement_history` should rebuild the
// historical TurnContext at the correct insertion point from persisted
@@ -441,6 +451,7 @@ impl Session {
id: None,
});
RolloutReconstruction {
guardian_history: history.guardian_history_checkpoint(),
history: history.into_annotated_items(),
previous_turn_settings,
reference_context_item,