Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Resume model context from the latest compaction boundary (#47365)

Owen Line7bbc79f482a
diff --git a/codex-rs/app-server/tests/suite/v2/thread_resume.rs b/codex-rs/app-server/tests/suite/v2/thread_resume.rs
index c0cbedd9ce..c6ea68f725 100644
--- a/codex-rs/app-server/tests/suite/v2/thread_resume.rs
+++ b/codex-rs/app-server/tests/suite/v2/thread_resume.rs
@@ -187,11 +187,6 @@ async fn thread_resume_paginated_model_context_preserves_original_metadata() ->
"collaboration_mode": { "mode": "default", "settings": { "model": "gpt-5.4" } },
},
}))?;
- append_rollout_item_to_path(
- &path,
- &RolloutItem::EventMsg(EventMsg::ThreadSettingsApplied(settings)),
- )
- .await?;
append_rollout_item_to_path(
&path,
&RolloutItem::Compacted(CompactedItem {
@@ -210,6 +205,11 @@ async fn thread_resume_paginated_model_context_preserves_original_metadata() ->
}),
)
.await?;
+ append_rollout_item_to_path(
+ &path,
+ &RolloutItem::EventMsg(EventMsg::ThreadSettingsApplied(settings)),
+ )
+ .await?;
let mut primary = TestAppServer::builder()
.with_codex_home(codex_home.path())
diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs
index 2965bac15d..579644823d 100644
--- a/codex-rs/core/src/session/mod.rs
+++ b/codex-rs/core/src/session/mod.rs
@@ -1706,6 +1706,7 @@ impl Session {
mut history,
retained_context,
guardian_history,
+ last_started_turn_id,
previous_turn_settings,
reference_context_item,
world_state_baseline,
@@ -1767,6 +1768,7 @@ impl Session {
guardian_history.as_ref(),
reviewer_compaction_hash.as_deref(),
);
+ state.last_started_turn_id = last_started_turn_id;
if let Some(world_state) = world_state_baseline {
state.history.set_world_state_baseline(world_state);
}
diff --git a/codex-rs/core/src/session/rollout_reconstruction.rs b/codex-rs/core/src/session/rollout_reconstruction.rs
index 8860d4c36a..473cd17bc7 100644
--- a/codex-rs/core/src/session/rollout_reconstruction.rs
+++ b/codex-rs/core/src/session/rollout_reconstruction.rs
@@ -4,15 +4,17 @@ use crate::context::world_state::WorldStateSnapshot;
use crate::context_manager::is_user_turn_boundary;
use codex_history::ResponseItemEnvelope;
use codex_protocol::protocol::SessionContextWindow;
+use codex_protocol::protocol::ThreadHistoryMode;
use uuid::Uuid;
// Return value of `Session::reconstruct_history_from_rollout`, bundling the rebuilt history with
// the resume/fork hydration metadata derived from the same replay.
-#[derive(Debug)]
+#[derive(Debug, PartialEq)]
pub(super) struct RolloutReconstruction {
pub(super) history: Vec<ResponseItemEnvelope>,
pub(super) retained_context: codex_history::RetainedContext,
pub(super) guardian_history: Option<codex_history::GuardianHistoryCheckpoint>,
+ pub(super) last_started_turn_id: Option<String>,
pub(super) previous_turn_settings: Option<PreviousTurnSettings>,
pub(super) reference_context_item: Option<TurnContextItem>,
pub(super) world_state_baseline: Option<WorldStateSnapshot>,
@@ -47,20 +49,54 @@ enum TurnReferenceContextItem {
}
#[derive(Debug, Clone, Copy)]
-// The selected checkpoint and its replay tail must belong to the same surviving segment.
+// The selected compaction and its replay tail must belong to the same surviving segment.
struct ReplayCheckpoint<'a> {
compacted: &'a CompactedItem,
suffix: &'a [RolloutItem],
}
+/// Selects the newest compaction that can safely bound replay.
+///
+/// Returns `None` when reconstruction must replay all supplied items, either because there is no
+/// compaction or the newest compaction cannot bound replay.
+fn select_input_compaction(
+ rollout_items: &[RolloutItem],
+ history_mode: ThreadHistoryMode,
+) -> Option<ReplayCheckpoint<'_>> {
+ // Only the newest compaction can bound replay. If it is incomplete, an older compaction
+ // cannot replace the history or window state that the newer one may have changed.
+ let (index, compacted) = rollout_items
+ .iter()
+ .enumerate()
+ .rev()
+ .find_map(|(index, item)| match item {
+ RolloutItem::Compacted(compacted) => Some((index, compacted)),
+ _ => None,
+ })?;
+ // Paginated histories always honor this boundary. Other histories only do so when resume
+ // metadata identifies a compaction written under the newer resume contract.
+ if compacted.replacement_history.is_none()
+ || compacted.window_number.is_none()
+ || (compacted.resume_metadata.is_none()
+ && !matches!(history_mode, ThreadHistoryMode::Paginated))
+ {
+ return None;
+ }
+ Some(ReplayCheckpoint {
+ compacted,
+ suffix: &rollout_items[index + 1..],
+ })
+}
+
#[derive(Debug, Default)]
struct ActiveReplaySegment<'a> {
turn_id: Option<String>,
+ turn_completed: bool,
counts_as_user_turn: bool,
previous_turn_settings: Option<PreviousTurnSettings>,
reference_context_item: TurnReferenceContextItem,
world_state_replay: Vec<&'a RolloutItem>,
- base_compaction: Option<ReplayCheckpoint<'a>>,
+ history_checkpoint: Option<ReplayCheckpoint<'a>>,
window: Option<ReconstructedWindow>,
}
@@ -71,7 +107,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_compaction: &mut Option<ReplayCheckpoint<'a>>,
+ history_checkpoint: &mut Option<ReplayCheckpoint<'a>>,
previous_turn_settings: &mut Option<PreviousTurnSettings>,
reference_context_item: &mut TurnReferenceContextItem,
world_state_replay: &mut Vec<&'a RolloutItem>,
@@ -100,12 +136,12 @@ fn finalize_active_segment<'a>(
.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
+ // A surviving replacement-history compaction is a complete history base. Once we
// know the newest surviving one, older rollout items do not affect rebuilt history.
- if base_compaction.is_none()
- && let Some(segment_base_compaction) = active_segment.base_compaction
+ if history_checkpoint.is_none()
+ && let Some(segment_history_checkpoint) = active_segment.history_checkpoint
{
- *base_compaction = Some(segment_base_compaction);
+ *history_checkpoint = Some(segment_history_checkpoint);
}
if window.is_none() {
@@ -136,11 +172,7 @@ impl Session {
turn_context: &TurnContext,
rollout_items: &[RolloutItem],
) -> RolloutReconstruction {
- // Replay metadata should already match the shape of the future lazy reverse loader, even
- // while history materialization still uses an eager bridge. Scan newest-to-oldest,
- // stopping once a surviving replacement-history checkpoint and the required resume metadata
- // are both known; then replay only the buffered surviving tail forward to preserve exact
- // history semantics.
+ // Select the compaction and suffix that can affect reconstruction.
let has_legacy_compaction_without_window_number =
rollout_items.iter().any(|item| {
matches!(item, RolloutItem::Compacted(compacted) if compacted.window_number.is_none())
@@ -157,11 +189,38 @@ impl Session {
_ => None,
})
};
- let mut base_compaction = None;
+ let input_checkpoint = select_input_compaction(rollout_items, turn_context.history_mode);
+ let replay_items = input_checkpoint.map_or(rollout_items, |checkpoint| checkpoint.suffix);
+ let resume_metadata =
+ input_checkpoint.and_then(|checkpoint| checkpoint.compacted.resume_metadata.as_ref());
+ let mut history_checkpoint = input_checkpoint;
+ let mut window = input_checkpoint
+ .and_then(|checkpoint| reconstructed_window_from_compaction(checkpoint.compacted));
+
+ // Scan the selected items backward to find the newest surviving turn state.
+ let last_started_turn_id = replay_items
+ .iter()
+ .rev()
+ .find_map(|item| match item {
+ RolloutItem::EventMsg(EventMsg::TurnStarted(event)) => Some(event.turn_id.clone()),
+ RolloutItem::SessionMeta(_)
+ | RolloutItem::ResponseItem(_)
+ | RolloutItem::InterAgentCommunication(_)
+ | RolloutItem::InterAgentCommunicationMetadata { .. }
+ | RolloutItem::TurnContext(_)
+ | RolloutItem::WorldState(_)
+ | RolloutItem::RetainedContext(_)
+ | RolloutItem::SecurityRiskScore(_)
+ | RolloutItem::TokenUsageRecord(_)
+ | RolloutItem::RealtimeItem(_)
+ | RolloutItem::Compacted(_)
+ | RolloutItem::EventMsg(_) => None,
+ })
+ .or_else(|| resume_metadata.and_then(|metadata| metadata.last_started_turn_id.clone()));
+
let mut previous_turn_settings = None;
let mut reference_context_item = TurnReferenceContextItem::NeverSet;
let mut world_state_replay = Vec::new();
- let mut window = None;
// 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;
@@ -169,24 +228,17 @@ impl Session {
// we hit its matching `TurnStarted`, at which point the segment can be finalized.
let mut active_segment: Option<ActiveReplaySegment<'_>> = None;
- for (index, item) in rollout_items.iter().enumerate().rev() {
+ for (index, item) in replay_items.iter().enumerate().rev() {
match item {
RolloutItem::Compacted(compacted) => {
let active_segment =
active_segment.get_or_insert_with(ActiveReplaySegment::default);
active_segment.world_state_replay.push(item);
if active_segment.window.is_none()
- && let Some(window_number) = compacted.window_number
+ && let Some(compaction_window) =
+ reconstructed_window_from_compaction(compacted)
{
- active_segment.window = Some(ReconstructedWindow {
- number: window_number,
- first_id: compacted.first_window_id.as_deref().and_then(parse_uuid_v7),
- previous_id: compacted
- .previous_window_id
- .as_deref()
- .and_then(parse_uuid_v7),
- id: compacted.window_id.as_deref().and_then(parse_uuid_v7),
- });
+ active_segment.window = Some(compaction_window);
}
// Looking backward, compaction clears any older baseline unless a newer
// `TurnContextItem` in this same segment has already re-established it.
@@ -196,12 +248,12 @@ impl Session {
) {
active_segment.reference_context_item = TurnReferenceContextItem::Cleared;
}
- if active_segment.base_compaction.is_none()
+ if active_segment.history_checkpoint.is_none()
&& compacted.replacement_history.is_some()
{
- active_segment.base_compaction = Some(ReplayCheckpoint {
+ active_segment.history_checkpoint = Some(ReplayCheckpoint {
compacted,
- suffix: &rollout_items[index + 1..],
+ suffix: &replay_items[index + 1..],
});
}
}
@@ -212,6 +264,7 @@ impl Session {
RolloutItem::EventMsg(EventMsg::TurnComplete(event)) => {
let active_segment =
active_segment.get_or_insert_with(ActiveReplaySegment::default);
+ active_segment.turn_completed = true;
// Reverse replay often sees `TurnComplete` before any turn-scoped metadata.
// Capture the turn id early so later `TurnContext` / abort items can match it.
if active_segment.turn_id.is_none() {
@@ -279,7 +332,7 @@ impl Session {
{
finalize_active_segment(
active_segment,
- &mut base_compaction,
+ &mut history_checkpoint,
&mut previous_turn_settings,
&mut reference_context_item,
&mut world_state_replay,
@@ -307,22 +360,17 @@ impl Session {
| RolloutItem::TokenUsageRecord(_)
| RolloutItem::InterAgentCommunicationMetadata { .. } => {}
}
-
- if base_compaction.is_some()
- && previous_turn_settings.is_some()
- && !matches!(reference_context_item, TurnReferenceContextItem::NeverSet)
- {
- // At this point we have both eager resume metadata values and the replacement-
- // history base for the surviving tail, so older rollout items cannot affect this
- // result.
- break;
- }
}
- if let Some(active_segment) = active_segment.take() {
+ if let Some(mut active_segment) = active_segment.take() {
+ // A companion turn context only restores the context baseline. Once that turn
+ // completes, its settings are newer than the compaction metadata.
+ if resume_metadata.is_some() && !active_segment.turn_completed {
+ active_segment.previous_turn_settings = None;
+ }
finalize_active_segment(
active_segment,
- &mut base_compaction,
+ &mut history_checkpoint,
&mut previous_turn_settings,
&mut reference_context_item,
&mut world_state_replay,
@@ -331,6 +379,11 @@ impl Session {
);
}
+ if previous_turn_settings.is_none() {
+ previous_turn_settings =
+ resume_metadata.and_then(|metadata| metadata.previous_turn_settings.clone());
+ }
+
let fallback_window_number = u64::try_from(
rollout_items
.iter()
@@ -339,12 +392,13 @@ impl Session {
)
.unwrap_or(u64::MAX);
+ // Build model-visible history from the selected compaction and its newer suffix.
let mut history = ContextManager::with_guardian_context_mode(
self.guardian_context_mode,
&turn_context.session_source,
);
let mut saw_legacy_compaction_without_replacement_history = false;
- if let Some(checkpoint) = base_compaction
+ if let Some(checkpoint) = history_checkpoint
&& let Some(items) = &checkpoint.compacted.replacement_history
{
history.replace_annotated(items.clone());
@@ -356,10 +410,8 @@ impl Session {
None,
);
}
- // 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);
+ let rollout_suffix =
+ history_checkpoint.map_or(rollout_items, |checkpoint| checkpoint.suffix);
for item in rollout_suffix {
match item {
RolloutItem::RetainedContext(event) => {
@@ -380,8 +432,8 @@ impl Session {
}
RolloutItem::InterAgentCommunicationMetadata { .. } => {}
RolloutItem::Compacted(compacted) => {
- // Reverse replay already chose the newest surviving checkpoint. Any newer
- // replacement checkpoint belongs to a rolled-back turn; replay its original
+ // Reverse replay already chose the newest surviving compaction. Any newer
+ // replacement compaction 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;
@@ -438,8 +490,8 @@ impl Session {
reference_context_item
};
- // Segments and their contents were collected newest-first; replay the surviving records
- // chronologically so compaction resets and merge patches have their original meaning.
+ // Replay the collected world-state records chronologically so compaction resets and merge
+ // patches keep their original meaning.
world_state_replay.reverse();
let mut world_state_baseline: Option<WorldStateSnapshot> = None;
for item in world_state_replay {
@@ -479,6 +531,7 @@ impl Session {
RolloutReconstruction {
retained_context: history.retained_context().clone(),
guardian_history: history.guardian_history_checkpoint(),
+ last_started_turn_id,
history: history.into_annotated_items(),
previous_turn_settings,
reference_context_item,
@@ -497,6 +550,18 @@ fn parse_uuid_v7(value: &str) -> Option<Uuid> {
.filter(|uuid| uuid.get_version_num() == 7)
}
+fn reconstructed_window_from_compaction(compacted: &CompactedItem) -> Option<ReconstructedWindow> {
+ Some(ReconstructedWindow {
+ number: compacted.window_number?,
+ first_id: compacted.first_window_id.as_deref().and_then(parse_uuid_v7),
+ previous_id: compacted
+ .previous_window_id
+ .as_deref()
+ .and_then(parse_uuid_v7),
+ id: compacted.window_id.as_deref().and_then(parse_uuid_v7),
+ })
+}
+
fn reconstructed_window_from_session_context_window(
context_window: &SessionContextWindow,
) -> Option<ReconstructedWindow> {
diff --git a/codex-rs/core/src/session/rollout_reconstruction_tests.rs b/codex-rs/core/src/session/rollout_reconstruction_tests.rs
index e8841f4a26..e69c0fb585 100644
--- a/codex-rs/core/src/session/rollout_reconstruction_tests.rs
+++ b/codex-rs/core/src/session/rollout_reconstruction_tests.rs
@@ -1485,9 +1485,20 @@ async fn reconstruct_history_replays_world_state_from_latest_compaction_window()
);
}
+#[derive(Clone, Copy)]
+enum CompactionFormat {
+ Current,
+ Legacy,
+}
+
+#[test_case(CompactionFormat::Current; "current compactions")]
+#[test_case(CompactionFormat::Legacy; "legacy compactions")]
#[tokio::test]
-async fn bounded_replay_matches_full_replay_after_empty_turn_compactions() {
- let (session, turn_context) = make_session_and_context().await;
+async fn bounded_replay_matches_full_replay_after_empty_turn_compactions(
+ compaction_format: CompactionFormat,
+) {
+ let (session, mut turn_context) = make_session_and_context().await;
+ turn_context.history_mode = ThreadHistoryMode::Paginated;
let session_meta = SessionMetaLine {
meta: SessionMeta {
history_mode: ThreadHistoryMode::Paginated,
@@ -1504,7 +1515,8 @@ async fn bounded_replay_matches_full_replay_after_empty_turn_compactions() {
)],
));
let window_ids = [Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7()];
- let mut latest_wake_start = 0;
+ let current = matches!(compaction_format, CompactionFormat::Current);
+ let mut latest_compaction_index = 0;
for window_number in 1..=2 {
let mut context = initial_context.clone();
context.turn_id = Some(format!("wake-{window_number}"));
@@ -1532,7 +1544,15 @@ async fn bounded_replay_matches_full_replay_after_empty_turn_compactions() {
window_id: Some(window_ids[window_number].to_string()),
compaction_response_id: None,
latest_token_usage_record: None,
- resume_metadata: None,
+ resume_metadata: current.then(|| codex_history::CompactionResumeMetadata {
+ multi_agent_version: None,
+ last_started_turn_id: Some(format!("wake-{window_number}")),
+ previous_turn_settings: Some(PreviousTurnSettings {
+ model: format!("metadata-model-{window_number}"),
+ comp_hash: Some(format!("metadata-hash-{window_number}")),
+ realtime_active: Some(false),
+ }),
+ }),
}),
RolloutItem::WorldState(WorldStateItem::full(object!({
"environment": {"window": window_number, "status": "starting"}
@@ -1560,7 +1580,11 @@ async fn bounded_replay_matches_full_replay_after_empty_turn_compactions() {
duration_ms: None,
},
)));
- latest_wake_start = rollout_items.len();
+ latest_compaction_index = rollout_items.len()
+ + wake_items
+ .iter()
+ .position(|item| matches!(item, RolloutItem::Compacted(_)))
+ .expect("wake compaction");
rollout_items.extend(wake_items);
}
@@ -1572,8 +1596,9 @@ async fn bounded_replay_matches_full_replay_after_empty_turn_compactions() {
.find_map(|(index, item)| {
matches!(scan.push(item.clone()), ModelContextScanProgress::Complete).then_some(index)
});
- assert_eq!(cutoff, Some(latest_wake_start));
- let bounded_items = scan.finish(session_meta);
+ assert_eq!(cutoff, Some(latest_compaction_index));
+ let mut bounded_items = scan.finish();
+ bounded_items.insert(0, RolloutItem::SessionMeta(session_meta));
let full = session
.reconstruct_history_from_rollout(&turn_context, &rollout_items)
.await;
@@ -1587,30 +1612,92 @@ async fn bounded_replay_matches_full_replay_after_empty_turn_compactions() {
assistant_message("continued working"),
])),
);
+ if current {
+ assert_eq!(bounded.last_started_turn_id.as_deref(), Some("wake-2"));
+ assert_eq!(
+ bounded
+ .previous_turn_settings
+ .as_ref()
+ .map(|settings| settings.model.as_str()),
+ Some("metadata-model-2")
+ );
+ } else {
+ assert_eq!(bounded.last_started_turn_id, None);
+ }
+ assert_eq!(bounded, full);
+}
+
+#[tokio::test]
+async fn paginated_compaction_does_not_restore_missing_companions_from_older_history() {
+ let (session, mut turn_context) = make_session_and_context().await;
+ turn_context.history_mode = ThreadHistoryMode::Paginated;
+
+ for resume_metadata in [None, Some(object!({}))] {
+ let mut compacted: CompactedItem = object!({
+ "message": "summary",
+ "replacement_history": [assistant_message("summary")],
+ "window_number": 1
+ });
+ compacted.resume_metadata = resume_metadata;
+ let rollout_items = vec![
+ RolloutItem::TurnContext(turn_context.to_turn_context_item()),
+ RolloutItem::WorldState(WorldStateItem::full(object!({"older": true}))),
+ RolloutItem::Compacted(compacted),
+ ];
+
+ let reconstructed = session
+ .reconstruct_history_from_rollout(&turn_context, &rollout_items)
+ .await;
+
+ assert_eq!(reconstructed.previous_turn_settings, None);
+ assert_eq!(reconstructed.reference_context_item, None);
+ assert_eq!(reconstructed.world_state_baseline, None);
+ }
+}
+
+#[tokio::test]
+async fn completed_turn_suffix_after_compaction_overrides_resume_metadata() {
+ let (session, turn_context) = make_session_and_context().await;
+ let mut newer_context = turn_context.to_turn_context_item();
+ newer_context.turn_id = Some("newer-turn".to_string());
+ newer_context.model = "newer-model".to_string();
+ newer_context.comp_hash = Some("newer-hash".to_string());
+ let expected_settings = PreviousTurnSettings {
+ model: newer_context.model.clone(),
+ comp_hash: newer_context.comp_hash.clone(),
+ realtime_active: newer_context.realtime_active,
+ };
+ let mut rollout_items = vec![
+ RolloutItem::Compacted(object!({
+ "message": "summary",
+ "replacement_history": [user_message("seed"), assistant_message("summary")],
+ "window_number": 1,
+ "resume_metadata": {
+ "previous_turn_settings": {
+ "model": "metadata-model",
+ "comp_hash": "metadata-hash"
+ }
+ }
+ })),
+ RolloutItem::WorldState(WorldStateItem::full(object!({}))),
+ ];
+ // The bounded suffix starts at the compaction, so the turn start and user input are already in
+ // replacement history. Its context and completion still make its settings authoritative.
+ rollout_items.extend(
+ completed_user_turn_rollout(newer_context.clone(), Vec::new())
+ .into_iter()
+ .skip(2),
+ );
+
+ let reconstructed = session
+ .reconstruct_history_from_rollout(&turn_context, &rollout_items)
+ .await;
+
assert_eq!(
- (
- bounded.guardian_history,
- bounded.history,
- bounded.previous_turn_settings,
- bounded.reference_context_item,
- bounded.world_state_baseline,
- bounded.window_number,
- bounded.first_window_id,
- bounded.previous_window_id,
- bounded.window_id,
- ),
- (
- full.guardian_history,
- full.history,
- full.previous_turn_settings,
- full.reference_context_item,
- full.world_state_baseline,
- full.window_number,
- full.first_window_id,
- full.previous_window_id,
- full.window_id,
- ),
+ reconstructed.previous_turn_settings,
+ Some(expected_settings)
);
+ assert_eq!(reconstructed.reference_context_item, Some(newer_context));
}
#[tokio::test]
diff --git a/codex-rs/core/src/session/session.rs b/codex-rs/core/src/session/session.rs
index 21c68aad6f..bec92fcaf4 100644
--- a/codex-rs/core/src/session/session.rs
+++ b/codex-rs/core/src/session/session.rs
@@ -1477,12 +1477,6 @@ impl Session {
&session_configuration.session_source,
),
);
- state.last_started_turn_id = initial_history.get_rollout_items().iter().rev().find_map(|item| {
- match item {
- RolloutItem::EventMsg(EventMsg::TurnStarted(event)) => Some(event.turn_id.clone()),
- _ => None,
- }
- });
state.base_instructions_provenance = base_instructions_provenance.clone();
state.active_disabled_plugin_ids = session_configuration.disabled_plugin_ids.clone();
let managed_network_requirements_configured = config
diff --git a/codex-rs/core/tests/suite/compact.rs b/codex-rs/core/tests/suite/compact.rs
index cfbcefc9d8..76d8354a32 100644
--- a/codex-rs/core/tests/suite/compact.rs
+++ b/codex-rs/core/tests/suite/compact.rs
@@ -4378,6 +4378,172 @@ async fn auto_compact_allows_multiple_attempts_when_interleaved_with_other_turn_
);
}
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn paginated_compaction_cold_resume_from_bounded_suffix() -> Result<()> {
+ skip_if_no_network!(Ok(()));
+ let server = start_mock_server().await;
+ let request_log = mount_sse_sequence(
+ &server,
+ vec![
+ sse(vec![
+ ev_function_call(DUMMY_CALL_ID, DUMMY_FUNCTION_NAME, "{}"),
+ ev_completed_with_tokens("before-compact", /*total_tokens*/ 96),
+ ]),
+ sse(vec![
+ ev_assistant_message("summary", &auto_summary(AUTO_SUMMARY_TEXT)),
+ ev_completed_with_tokens("compact", /*total_tokens*/ 10),
+ ]),
+ sse(vec![
+ ev_assistant_message("continued", "continued"),
+ ev_completed_with_tokens("continued", /*total_tokens*/ 10),
+ ]),
+ sse(vec![ev_completed_with_tokens(
+ "resumed", /*total_tokens*/ 10,
+ )]),
+ ],
+ )
+ .await;
+ let provider = non_openai_model_provider(&server);
+ let test = test_codex()
+ .with_model_info_override("gpt-5.4", |model| {
+ model.multi_agent_version = Some(codex_protocol::protocol::MultiAgentVersion::V2);
+ })
+ .with_history_mode(codex_protocol::protocol::ThreadHistoryMode::Paginated)
+ .with_config(move |config| {
+ config
+ .features
+ .disable(Feature::MultiAgentV2)
+ .expect("disable override");
+ config.agents_enabled = true;
+ config.model_provider = provider;
+ config.include_environment_context = false;
+ set_test_compact_prompt(config);
+ config.model_context_window = Some(100);
+ config.model_auto_compact_token_limit = Some(90);
+ })
+ .build_with_auto_env(&server)
+ .await?;
+ core_test_support::submit_thread_settings(
+ &test.codex,
+ ThreadSettingsOverrides {
+ collaboration_mode: Some(CollaborationMode {
+ mode: ModeKind::Plan,
+ settings: Settings {
+ model: test.session_configured.model.clone(),
+ reasoning_effort: test.config.model_reasoning_effort.clone(),
+ developer_instructions: Some(
+ "Preserve the compaction's plan instructions.".into(),
+ ),
+ },
+ }),
+ ..Default::default()
+ },
+ )
+ .await?;
+ let codex_core::TurnInputSubmission::Started { turn_id } = test
+ .codex
+ .start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
+ text: FUNCTION_CALL_LIMIT_MSG.into(),
+ text_elements: Vec::new(),
+ }]))
+ .await?
+ else {
+ panic!("initial turn did not start");
+ };
+ wait_for_event(&test.codex, |event| {
+ matches!(event, EventMsg::TurnComplete(_))
+ })
+ .await;
+ let expected_settings = test.codex.thread_settings_snapshot().await;
+ let path = test.codex.rollout_path().expect("rollout path");
+ test.codex.shutdown_and_wait().await?;
+ test.thread_manager
+ .remove_thread(&test.session_configured.thread_id)
+ .await;
+
+ let context = test
+ .thread_store
+ .load_latest_model_context(codex_thread_store::LoadThreadHistoryParams {
+ thread_id: test.session_configured.thread_id,
+ include_archived: false,
+ })
+ .await?;
+ let [
+ RolloutItem::SessionMeta(meta),
+ RolloutItem::Compacted(compacted),
+ ..,
+ ] = context.items.as_slice()
+ else {
+ panic!("bounded resume should start with session metadata and the compaction");
+ };
+ assert_eq!(meta.meta.multi_agent_version, None);
+ assert!(compacted.resume_metadata.is_some());
+ let replacement_len = compacted
+ .replacement_history
+ .as_ref()
+ .expect("replacement history")
+ .len();
+
+ let auth_manager = codex_core::test_support::auth_manager_from_auth_with_home(
+ CodexAuth::from_api_key("test"),
+ test.config.codex_home.to_path_buf(),
+ );
+ let resumed = test
+ .thread_manager
+ .resume_thread_with_history(
+ test.config.clone(),
+ codex_history::InitialHistory::Resumed(codex_history::ResumedHistory {
+ conversation_id: context.thread_id,
+ history: Arc::new(context.items),
+ rollout_path: Some(path),
+ }),
+ auth_manager,
+ /*parent_trace*/ None,
+ codex_protocol::mcp::ClientMcpExtensions::default(),
+ )
+ .await?;
+ assert_eq!(
+ resumed.thread.thread_settings_snapshot().await,
+ expected_settings
+ );
+ assert_eq!(
+ resumed.thread.multi_agent_version(),
+ Some(codex_protocol::protocol::MultiAgentVersion::V2)
+ );
+ let submission = resumed
+ .thread
+ .continue_turn_if_idle(
+ TurnInputRequest::new(codex_core::TurnInput::ResponseItem(
+ core_test_support::responses::user_message_item("continue after cold resume"),
+ )),
+ turn_id,
+ )
+ .await?;
+ assert!(matches!(
+ submission,
+ codex_core::TurnInputSubmission::Started { .. }
+ ));
+ wait_for_event(&resumed.thread, |event| {
+ matches!(event, EventMsg::TurnComplete(_))
+ })
+ .await;
+
+ let requests = request_log.requests();
+ assert_eq!(requests.len(), 4);
+ assert_eq!(
+ &requests[3].input()[..replacement_len],
+ &requests[2].input()[..replacement_len]
+ );
+ insta::assert_snapshot!(
+ "compaction_cold_resume_after_completion",
+ format_history_snapshot(
+ "Real paginated mid-turn compaction and cold resume from the bounded suffix.",
+ &requests,
+ )
+ );
+ Ok(())
+}
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn snapshot_request_shape_mid_turn_continuation_compaction() {
skip_if_no_network!();
diff --git a/codex-rs/core/tests/suite/guardian_history.rs b/codex-rs/core/tests/suite/guardian_history.rs
index ea614451fa..f85e5ce4d3 100644
--- a/codex-rs/core/tests/suite/guardian_history.rs
+++ b/codex-rs/core/tests/suite/guardian_history.rs
@@ -1,4 +1,4 @@
-//! Exercises retained review history through compaction, resume, fork, eviction, and legacy rollback replay.
+//! Exercises retained review history through compaction, resume, fork, and eviction.
use anyhow::Result;
use base64::Engine;
@@ -18,7 +18,6 @@ use codex_protocol::protocol::AskForApproval;
use codex_protocol::protocol::EventMsg;
use codex_protocol::protocol::Op;
use codex_protocol::protocol::ThreadHistoryMode;
-use codex_protocol::protocol::ThreadRolledBackEvent;
use codex_protocol::request_user_input::RequestUserInputAnswer;
use codex_protocol::request_user_input::RequestUserInputResponse;
use codex_protocol::user_input::UserInput;
@@ -312,14 +311,14 @@ async fn guardian_history_uses_deltas_between_eviction_batches() -> Result<()> {
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
-async fn guardian_answers_survive_compaction_and_eviction_but_not_rollback() -> Result<()> {
+async fn guardian_answers_survive_compaction_and_eviction() -> Result<()> {
skip_if_no_network!(Ok(()));
skip_if_wine_exec!(
Ok(()),
"Guardian approval actions require host-native paths"
);
let server = start_mock_server().await;
- let mut test = test_codex()
+ let test = test_codex()
.with_config(|config| {
config.features.enable(Feature::TokenBudget).unwrap();
config
@@ -415,130 +414,82 @@ async fn guardian_answers_survive_compaction_and_eviction_but_not_rollback() ->
);
assert!(image_url.len() > 4 * 1024 * 1024);
let command = r#"{"cmd":"echo publish","sandbox_permissions":"require_escalated","justification":"Publish the inspected change."}"#;
- for (prompt, retained) in [
- ("Do not publish the attached image.", true),
- ("Inspect a different repository.", false),
- ] {
- let review = mount_sse_sequence(
- &server,
- vec![
- sse(vec![
- ev_function_call("publish", "exec_command", command),
- ev_completed("publish"),
- ]),
- sse(vec![
- ev_assistant_message("review", r#"{"outcome":"deny"}"#),
- ev_completed("review"),
- ]),
- sse(vec![ev_completed("publish-done")]),
- ],
- )
- .await;
- test.codex
- .start_or_steer_turn(TurnInputRequest::user_input(vec![
- UserInput::Text {
- text: prompt.to_owned(),
- text_elements: Vec::new(),
- },
- UserInput::Image {
- image: ImageReference::Inline {
- image_url: image_url.clone(),
- },
- detail: None,
- },
- ]))
- .await?;
- wait_for_event(&test.codex, |event| {
- matches!(event, EventMsg::TurnComplete(_))
- })
- .await;
- let requests = review.requests();
- assert!(
- requests[0]
- .input()
- .iter()
- .filter_map(|item| item["content"].as_array())
- .flatten()
- .any(|item| item["image_url"]
- .as_str()
- .is_some_and(|url| url.len() > 4 * 1024 * 1024))
- );
- let guardian = requests
+ let prompt = "Do not publish the attached image.";
+ let review = mount_sse_sequence(
+ &server,
+ vec![
+ sse(vec![
+ ev_function_call("publish", "exec_command", command),
+ ev_completed("publish"),
+ ]),
+ sse(vec![
+ ev_assistant_message("review", r#"{"outcome":"deny"}"#),
+ ev_completed("review"),
+ ]),
+ sse(vec![ev_completed("publish-done")]),
+ ],
+ )
+ .await;
+ test.codex
+ .start_or_steer_turn(TurnInputRequest::user_input(vec![
+ UserInput::Text {
+ text: prompt.to_owned(),
+ text_elements: Vec::new(),
+ },
+ UserInput::Image {
+ image: ImageReference::Inline { image_url },
+ detail: None,
+ },
+ ]))
+ .await?;
+ wait_for_event(&test.codex, |event| {
+ matches!(event, EventMsg::TurnComplete(_))
+ })
+ .await;
+ let requests = review.requests();
+ assert!(
+ requests[0]
+ .input()
.iter()
- .find(|request| {
- request.body_json()["client_metadata"]["x-openai-subagent"] == "guardian"
- })
- .expect("Guardian request");
- let transcript = serde_json::to_string(&guardian.input())?;
- assert!(transcript.contains(prompt));
- if retained {
- let trusted_answers = transcript
- .split_once(">>> TRUSTED USER ANSWERS START")
- .expect("trusted answers survive compaction")
- .1
- .split_once(">>> TRUSTED USER ANSWERS END")
- .expect("trusted answers end marker")
- .0;
- assert!(trusted_answers.contains("user: Do not publish anything."));
- let positions = [
- "Only publish to a private repository.",
- "Do not publish the attached image.",
- ]
- .map(|text| {
- transcript
- .find(text)
- .unwrap_or_else(|| panic!("missing {text}: {transcript}"))
- });
- assert!(!transcript.contains("tool update_plan call"));
- assert!(!transcript.contains("tool update_plan result"));
- let mut ordered = positions;
- ordered.sort();
- assert_eq!(positions, ordered);
- assert!(
- requests[0]
- .input()
- .iter()
- .all(|item| item["call_id"] != "inspect-0"
- && item["call_id"] != "confirm-publish")
- );
- test.codex.ensure_rollout_materialized().await;
- test.codex
- .append_rollout_items(&[RolloutItem::EventMsg(EventMsg::ThreadRolledBack(
- ThreadRolledBackEvent { num_turns: 2 },
- ))])
- .await?;
- test.codex.shutdown_and_wait().await?;
- let thread_id = test.session_configured.thread_id;
- test.thread_manager.remove_thread(&thread_id).await;
- let model_context = test
- .thread_store
- .load_latest_model_context(LoadThreadHistoryParams {
- thread_id,
- include_archived: false,
- })
- .await?;
- test.codex = test
- .thread_manager
- .resume_thread_with_history(
- test.config.clone(),
- InitialHistory::Resumed(ResumedHistory {
- conversation_id: thread_id,
- history: Arc::new(model_context.items),
- rollout_path: None,
- }),
- test.thread_manager.auth_manager(),
- /*parent_trace*/ None,
- ClientMcpExtensions::default(),
- )
- .await?
- .thread;
- } else {
- assert!(!transcript.contains(">>> TRUSTED USER ANSWERS START"));
- assert!(!transcript.contains("Do not publish anything."));
- assert!(!transcript.contains("Only publish to a private repository."));
- assert!(!transcript.contains("tool update_plan call"));
- assert!(!transcript.contains("tool update_plan result"));
- }
- }
+ .filter_map(|item| item["content"].as_array())
+ .flatten()
+ .any(|item| item["image_url"]
+ .as_str()
+ .is_some_and(|url| url.len() > 4 * 1024 * 1024))
+ );
+ let guardian = requests
+ .iter()
+ .find(|request| request.body_json()["client_metadata"]["x-openai-subagent"] == "guardian")
+ .expect("Guardian request");
+ let transcript = serde_json::to_string(&guardian.input())?;
+ assert!(transcript.contains(prompt));
+ let trusted_answers = transcript
+ .split_once(">>> TRUSTED USER ANSWERS START")
+ .expect("trusted answers survive compaction")
+ .1
+ .split_once(">>> TRUSTED USER ANSWERS END")
+ .expect("trusted answers end marker")
+ .0;
+ assert!(trusted_answers.contains("user: Do not publish anything."));
+ let positions = [
+ "Only publish to a private repository.",
+ "Do not publish the attached image.",
+ ]
+ .map(|text| {
+ transcript
+ .find(text)
+ .unwrap_or_else(|| panic!("missing {text}: {transcript}"))
+ });
+ assert!(!transcript.contains("tool update_plan call"));
+ assert!(!transcript.contains("tool update_plan result"));
+ let mut ordered = positions;
+ ordered.sort();
+ assert_eq!(positions, ordered);
+ assert!(
+ requests[0]
+ .input()
+ .iter()
+ .all(|item| item["call_id"] != "inspect-0" && item["call_id"] != "confirm-publish")
+ );
Ok(())
}
diff --git a/codex-rs/core/tests/suite/guardian_retained_context.rs b/codex-rs/core/tests/suite/guardian_retained_context.rs
index 53274954cd..42a355af28 100644
--- a/codex-rs/core/tests/suite/guardian_retained_context.rs
+++ b/codex-rs/core/tests/suite/guardian_retained_context.rs
@@ -1,6 +1,5 @@
//! Retained-instruction and answer lifecycles through real sessions and durable checkpoints.
-//! Real user input covers steering and compaction; legacy rollback replay fixtures use
-//! the public rollout append API. Resume and child forks use production paths.
+//! Real user input covers steering and compaction. Resume and child forks use production paths.
use std::collections::HashMap;
use std::sync::Arc;
@@ -38,7 +37,6 @@ use codex_protocol::models::ResponseItem;
use codex_protocol::protocol::EventMsg;
use codex_protocol::protocol::Op;
use codex_protocol::protocol::ThreadHistoryMode;
-use codex_protocol::protocol::ThreadRolledBackEvent;
use codex_protocol::request_user_input::RequestUserInputAnswer;
use codex_protocol::request_user_input::RequestUserInputResponse;
use codex_protocol::user_input::UserInput;
@@ -266,15 +264,11 @@ enum InstructionSize {
Oversized,
}
-#[test_case(ThreadHistoryMode::Legacy, true, InstructionSize::Normal; "enabled legacy rollback replay")]
-#[test_case(ThreadHistoryMode::Paginated, true, InstructionSize::Normal; "enabled paginated resume")]
-#[test_case(ThreadHistoryMode::Legacy, false, InstructionSize::Normal; "disabled legacy rollback replay")]
-#[test_case(ThreadHistoryMode::Paginated, false, InstructionSize::Normal; "disabled paginated resume")]
-#[test_case(ThreadHistoryMode::Legacy, true, InstructionSize::Oversized; "oversized instruction rollback replay")]
-#[test_case(ThreadHistoryMode::Paginated, true, InstructionSize::Oversized; "oversized instruction resume")]
+#[test_case(true, InstructionSize::Normal; "enabled paginated resume")]
+#[test_case(false, InstructionSize::Normal; "disabled paginated resume")]
+#[test_case(true, InstructionSize::Oversized; "oversized instruction resume")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn retained_instructions_keep_identity_across_compaction_and_resume(
- history_mode: ThreadHistoryMode,
thread_context_enabled: bool,
instruction_size: InstructionSize,
) -> Result<()> {
@@ -288,14 +282,9 @@ async fn retained_instructions_keep_identity_across_compaction_and_resume(
"Project detail. ".repeat(2_000)
),
};
- // Legacy rollouts can contain rollback markers; paginated histories use checkpoint resume.
- let rollback_counts: &[usize] = match history_mode {
- ThreadHistoryMode::Legacy => &[1, 0],
- ThreadHistoryMode::Paginated => &[],
- };
let server = start_mock_server().await;
let test = test_codex()
- .with_history_mode(history_mode)
+ .with_history_mode(ThreadHistoryMode::Paginated)
.with_config(move |config| {
config.experimental_thread_store = ThreadStoreConfig::Local;
config
@@ -340,12 +329,10 @@ async fn retained_instructions_keep_identity_across_compaction_and_resume(
})
.collect::<Vec<_>>();
responses.push(sse(vec![ev_completed("done")]));
- responses.extend((0..=rollback_counts.len()).map(|index| {
- sse(vec![
- ev_assistant_message(&format!("summary-{index}"), "Compacted inspection context."),
- ev_completed(&format!("compact-{index}")),
- ])
- }));
+ responses.push(sse(vec![
+ ev_assistant_message("summary-0", "Compacted inspection context."),
+ ev_completed("compact-0"),
+ ]));
let response_mock = mount_sse_sequence(&server, responses).await;
test.codex
.start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
@@ -500,45 +487,7 @@ async fn retained_instructions_keep_identity_across_compaction_and_resume(
"only thread-owned context preserves original user-message identity"
);
}
- let mut thread = thread;
- for &remaining in rollback_counts {
- let remaining = if thread_context_enabled { remaining } else { 0 };
- thread = resume(&test, &thread).await?;
- assert_eq!(
- serde_json::to_value(
- thread
- .conversation_history_snapshot()
- .await
- .retained_context()
- )?,
- if thread_context_enabled {
- expected.clone()
- } else {
- serde_json::Value::Null
- }
- );
- thread
- .append_rollout_items(&[RolloutItem::EventMsg(EventMsg::ThreadRolledBack(
- ThreadRolledBackEvent { num_turns: 1 },
- ))])
- .await?;
- thread = resume(&test, &thread).await?;
- expected["user_messages"]
- .as_array_mut()
- .expect("expected retained user messages")
- .truncate(remaining);
- expected["verified_answers"]
- .as_array_mut()
- .expect("expected retained verified answers")
- .clear();
- // Rollback replay discards the prior compaction's position; its replacement
- // summary consumes that position again, leaving the same next_order.
- assert_eq!(
- serde_json::to_value(compact_and_assert_answers(&test, &thread, &[]).await?)?,
- expected
- );
- }
- thread = resume(&test, &thread).await?;
+ let thread = resume(&test, &thread).await?;
assert_eq!(
serde_json::to_value(
thread
@@ -554,17 +503,8 @@ async fn retained_instructions_keep_identity_across_compaction_and_resume(
);
thread.shutdown_and_wait().await?;
let requests = response_mock.requests();
- assert_eq!(requests.len(), 4 + rollback_counts.len());
+ assert_eq!(requests.len(), 4);
assert!(requests[3].has_message_with_input_texts("user", |texts| texts == [STEER]));
- if history_mode == ThreadHistoryMode::Legacy {
- assert!(
- requests[4].has_message_with_input_texts("user", |texts| texts == [initial.as_str()])
- );
- assert!(!requests[4].has_message_with_input_texts("user", |texts| texts == [STEER]));
- assert!(
- !requests[5].has_message_with_input_texts("user", |texts| texts == [initial.as_str()])
- );
- }
Ok(())
}
@@ -796,162 +736,6 @@ async fn disabled_capture_stays_incomplete_after_compaction_and_enabled_resume()
Ok(())
}
-#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
-async fn legacy_rollback_replay_retains_only_surviving_steered_answers() -> Result<()> {
- skip_if_no_network!(Ok(()));
- let server = start_mock_server().await;
- let test = test_codex()
- .with_history_mode(ThreadHistoryMode::Legacy)
- .with_config(|config| {
- config.experimental_thread_store = ThreadStoreConfig::Local;
- // Legacy saved answers use their source calls, not acceptance-order metadata.
- config
- .features
- .disable(Feature::GuardianThreadContext)
- .expect("legacy evidence fixture");
- for feature in [Feature::TokenBudget, Feature::DefaultModeRequestUserInput] {
- config
- .features
- .enable(feature)
- .expect("enable test feature");
- }
- })
- .build_with_auto_env(&server)
- .await?;
- let questions = [
- ("before-steer", "Publish?", "Only privately."),
- ("after-steer", "Publish the README?", "Do not publish it."),
- ];
- let mut responses = questions
- .iter()
- .map(|(call_id, question, _)| {
- sse(vec![
- ev_function_call(
- call_id,
- "request_user_input",
- &json!({"questions": [{
- "id": "publish", "header": "Publish", "question": question,
- "options": [
- {"label": "Yes", "description": "Publish privately."},
- {"label": "No", "description": "Keep local."}
- ]
- }]})
- .to_string(),
- ),
- ev_completed(call_id),
- ])
- })
- .collect::<Vec<_>>();
- responses.push(sse(vec![ev_completed("done")]));
- let response_mock = mount_sse_sequence(&server, responses).await;
- test.codex
- .start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
- text: "Check whether to publish.".to_owned(),
- text_elements: Vec::new(),
- }]))
- .await?;
- let mut answers = Vec::new();
- for (call_id, question, answer) in questions {
- let request = wait_for_event_match(&test.codex, |event| match event {
- EventMsg::RequestUserInput(request) => Some(request.clone()),
- _ => None,
- })
- .await;
- if answers.is_empty() {
- test.codex
- .start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
- text: "Also inspect the README.".to_owned(),
- text_elements: Vec::new(),
- }]))
- .await?;
- }
- test.codex
- .submit(Op::UserInputAnswer {
- id: request.turn_id.clone(),
- response: RequestUserInputResponse {
- answers: HashMap::from([(
- "publish".to_owned(),
- RequestUserInputAnswer {
- answers: vec![answer.to_owned()],
- },
- )]),
- },
- })
- .await?;
- answers.push(VerifiedAnswer {
- turn_id: request.turn_id,
- call_id: call_id.to_owned(),
- questions: vec![VerifiedQuestionAnswer {
- question: question.to_owned(),
- answer: answer.to_owned(),
- }],
- });
- }
- wait_for_event(&test.codex, |event| {
- matches!(event, EventMsg::TurnComplete(_))
- })
- .await;
- assert_eq!(answers[0].turn_id, answers[1].turn_id);
- let requests = response_mock.requests();
- assert_eq!(requests.len(), 3);
- assert!(
- requests[0].has_message_with_input_texts("user", |texts| {
- texts == ["Check whether to publish."]
- })
- );
- for (index, request) in requests.iter().skip(1).enumerate() {
- assert!(request.has_message_with_input_texts("user", |texts| {
- texts == ["Also inspect the README."]
- }));
- for answer in &answers[..=index] {
- let (output, _) = request
- .function_call_output_content_and_success(&answer.call_id)
- .context("answer tool output")?;
- let output: serde_json::Value =
- serde_json::from_str(&output.context("answer tool output content")?)?;
- assert_eq!(
- output,
- json!({"answers": {"publish": {"answers": [answer.questions[0].answer]}}})
- );
- }
- }
- test.codex
- .append_rollout_items(
- &answers
- .iter()
- .cloned()
- .map(|answer| {
- RolloutItem::RetainedContext(RetainedContextEvent::VerifiedAnswer {
- answer,
- acceptance_order: None,
- })
- })
- .collect::<Vec<_>>(),
- )
- .await?;
- let mut thread = resume(&test, &test.codex).await?;
- compact_and_assert_answers(&test, &thread, &answers).await?;
- for expected in [&answers[..1], &[]] {
- thread
- .append_rollout_items(&[RolloutItem::EventMsg(EventMsg::ThreadRolledBack(
- ThreadRolledBackEvent { num_turns: 1 },
- ))])
- .await?;
- thread = resume(&test, &thread).await?;
- compact_and_assert_answers(&test, &thread, expected).await?;
- thread = resume(&test, &thread).await?;
- compact_and_assert_answers(&test, &thread, expected).await?;
- }
- thread.shutdown_and_wait().await?;
- Ok(())
-}
-
-#[derive(Clone, Copy)]
-enum LifecycleBoundary {
- LegacyRollbackReplay,
- ChildFork,
-}
-
// Cover live copied history and a truncated referenced checkpoint. Parent-answer
// omission is already exercised by retained_answers_cross_real_session_boundaries.
#[test_case(ThreadHistoryMode::Legacy; "copied worker history")]
@@ -1360,13 +1144,11 @@ async fn forked_parent_instructions_do_not_become_local_authorization(
Ok(())
}
-#[test_case(ThreadHistoryMode::Legacy, LifecycleBoundary::LegacyRollbackReplay; "legacy rollback replay")]
-#[test_case(ThreadHistoryMode::Legacy, LifecycleBoundary::ChildFork; "legacy child fork")]
-#[test_case(ThreadHistoryMode::Paginated, LifecycleBoundary::ChildFork; "paginated child fork")]
+#[test_case(ThreadHistoryMode::Legacy; "legacy child fork")]
+#[test_case(ThreadHistoryMode::Paginated; "paginated child fork")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn retained_answers_cross_real_session_boundaries(
history_mode: ThreadHistoryMode,
- boundary: LifecycleBoundary,
) -> Result<()> {
skip_if_no_network!(Ok(()));
let server = start_mock_server().await;
@@ -1444,71 +1226,51 @@ async fn retained_answers_cross_real_session_boundaries(
let thread = resume(&test, &thread).await?;
let expected = [before.clone(), after];
- match boundary {
- LifecycleBoundary::LegacyRollbackReplay => {
- // First remove the suffix answer, then the source retained only in the checkpoint.
- let mut thread = thread;
- for expected in [std::slice::from_ref(&before), &[]] {
- thread
- .append_rollout_items(&[RolloutItem::EventMsg(EventMsg::ThreadRolledBack(
- ThreadRolledBackEvent { num_turns: 1 },
- ))])
- .await?;
- thread = resume(&test, &thread).await?;
- compact_and_assert_answers(&test, &thread, expected).await?;
- thread = resume(&test, &thread).await?;
- compact_and_assert_answers(&test, &thread, expected).await?;
- }
- thread.shutdown_and_wait().await?;
- }
- LifecycleBoundary::ChildFork => {
- // Full-history delegation must not turn parent-local answers into child-local facts.
- let mut created = test.thread_manager.subscribe_thread_created();
- let arguments = json!({
- "task_name": "worker",
- "message": "Inspect without publishing.",
- "fork_turns": "all",
- });
- mount_sse_sequence(
- &server,
- vec![
- sse(vec![
- ev_function_call_with_namespace(
- "spawn",
- "collaboration",
- "spawn_agent",
- &arguments.to_string(),
- ),
- ev_completed("spawn-response"),
- ]),
- sse(vec![ev_completed("first-fork-completion")]),
- sse(vec![ev_completed("second-fork-completion")]),
- ],
- )
- .await;
- thread
- .start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
- text: "Delegate an inspection.".to_owned(),
- text_elements: Vec::new(),
- }]))
- .await?;
- wait_for_event(&thread, |event| matches!(event, EventMsg::TurnComplete(_))).await;
- let child = test.thread_manager.get_thread(created.try_recv()?).await?;
- wait_for_event(&child, |event| matches!(event, EventMsg::TurnComplete(_))).await;
- child.flush_rollout().await?;
- let child_history = load_context(&test, &child).await?;
- assert!(
- serde_json::to_string(&child_history)?.contains("Delegate an inspection."),
- "the child must actually inherit parent conversation context"
- );
- compact_and_assert_answers(&test, &child, &[]).await?;
- let child = resume(&test, &child).await?;
- compact_and_assert_answers(&test, &child, &[]).await?;
- child.shutdown_and_wait().await?;
- compact_and_assert_answers(&test, &thread, &expected).await?;
+ // Full-history delegation must not turn parent-local answers into child-local facts.
+ let mut created = test.thread_manager.subscribe_thread_created();
+ let arguments = json!({
+ "task_name": "worker",
+ "message": "Inspect without publishing.",
+ "fork_turns": "all",
+ });
+ mount_sse_sequence(
+ &server,
+ vec![
+ sse(vec![
+ ev_function_call_with_namespace(
+ "spawn",
+ "collaboration",
+ "spawn_agent",
+ &arguments.to_string(),
+ ),
+ ev_completed("spawn-response"),
+ ]),
+ sse(vec![ev_completed("first-fork-completion")]),
+ sse(vec![ev_completed("second-fork-completion")]),
+ ],
+ )
+ .await;
+ thread
+ .start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
+ text: "Delegate an inspection.".to_owned(),
+ text_elements: Vec::new(),
+ }]))
+ .await?;
+ wait_for_event(&thread, |event| matches!(event, EventMsg::TurnComplete(_))).await;
+ let child = test.thread_manager.get_thread(created.try_recv()?).await?;
+ wait_for_event(&child, |event| matches!(event, EventMsg::TurnComplete(_))).await;
+ child.flush_rollout().await?;
+ let child_history = load_context(&test, &child).await?;
+ assert!(
+ serde_json::to_string(&child_history)?.contains("Delegate an inspection."),
+ "the child must actually inherit parent conversation context"
+ );
+ compact_and_assert_answers(&test, &child, &[]).await?;
+ let child = resume(&test, &child).await?;
+ compact_and_assert_answers(&test, &child, &[]).await?;
+ child.shutdown_and_wait().await?;
+ compact_and_assert_answers(&test, &thread, &expected).await?;
- thread.shutdown_and_wait().await?;
- }
- }
+ thread.shutdown_and_wait().await?;
Ok(())
}
diff --git a/codex-rs/core/tests/suite/snapshots/all__suite__compact__compaction_cold_resume_after_completion.snap b/codex-rs/core/tests/suite/snapshots/all__suite__compact__compaction_cold_resume_after_completion.snap
new file mode 100644
index 0000000000..7fae463115
--- /dev/null
+++ b/codex-rs/core/tests/suite/snapshots/all__suite__compact__compaction_cold_resume_after_completion.snap
@@ -0,0 +1,102 @@
+---
+source: core/tests/suite/compact.rs
+assertion_line: 4537
+expression: "format_history_snapshot(\"Real paginated mid-turn compaction and cold resume from the bounded suffix.\",\n&requests,)"
+---
+Scenario: Real paginated mid-turn compaction and cold resume from the bounded suffix.
+
+## Window 1
+-- request 1 (turn) --
+00:message/developer[2]:
+ [01] <PERMISSIONS_INSTRUCTIONS>
+ [02] <COLLABORATION_MODE>
+01:message/developer:
+ You are `/root`, the primary agent in a team of agents collaborating to fulfill the user's goals.
+
+ At the start of your turn, you are the active agent.
+ You can spawn sub-agents to handle subtasks, and those sub-agents can spawn their own sub-agents.
+ All agents in the team, including the agents that you can assign tasks to, are equally intelligent and capable, and have access to the same set of tools.
+
+ You can use `spawn_agent` to create a new agent, `followup_task` to give an existing agent a new task an...message to a running agent without triggering a turn. [hash=7d40ff8b58977202]
+ Child agents can also spawn their own sub-agents.
+ <OMITTED 16 LINES; ~234 TOKENS; hash=b2e94283a8c22327>
+ - All agents use the same current working directory.
+ - As a result, edits made by one agent are immediately visible to all other agents.
+
+ When calling `wait_agent`, prefer longer waits (minutes) to avoid busy polling.
+
+ There are 4 available concurrency slots, meaning that up to 4 agents can be active at once, including you.
+
+ Full-history forks (`fork_turns` omitted or `"all"`) inherit the parent model and reasoning effort and d...fork_turns` to `"none"` or a positive integer string. [hash=db3545796947884f]
+02:message/developer:
+ <MULTI_AGENT_MODE>
+03:message/user:
+ function call limit push
+
+## Window 2 (after request 1: settings changed (parallel_tool_calls, tools))
+-- request 2 (compaction) --
+00:message/developer[2]:
+ [01] <PERMISSIONS_INSTRUCTIONS>
+ [02] <COLLABORATION_MODE>
+01:message/developer:
+ You are `/root`, the primary agent in a team of agents collaborating to fulfill the user's goals.
+
+ At the start of your turn, you are the active agent.
+ You can spawn sub-agents to handle subtasks, and those sub-agents can spawn their own sub-agents.
+ All agents in the team, including the agents that you can assign tasks to, are equally intelligent and capable, and have access to the same set of tools.
+
+ You can use `spawn_agent` to create a new agent, `followup_task` to give an existing agent a new task an...message to a running agent without triggering a turn. [hash=7d40ff8b58977202]
+ Child agents can also spawn their own sub-agents.
+ <OMITTED 16 LINES; ~234 TOKENS; hash=b2e94283a8c22327>
+ - All agents use the same current working directory.
+ - As a result, edits made by one agent are immediately visible to all other agents.
+
+ When calling `wait_agent`, prefer longer waits (minutes) to avoid busy polling.
+
+ There are 4 available concurrency slots, meaning that up to 4 agents can be active at once, including you.
+
+ Full-history forks (`fork_turns` omitted or `"all"`) inherit the parent model and reasoning effort and d...fork_turns` to `"none"` or a positive integer string. [hash=db3545796947884f]
+02:message/developer:
+ <MULTI_AGENT_MODE>
+03:message/user:
+ function call limit push
+04:function_call/test_tool:{}
+05:function_call_output:unsupported call: test_tool
+06:message/user:
+ <SUMMARIZATION_PROMPT>
+
+## Window 3 (after request 2: input diverged at item 04, settings changed (parallel_tool_calls, tools))
+-- request 3 (turn) --
+00:message/developer[2]:
+ [01] <PERMISSIONS_INSTRUCTIONS>
+ [02] <COLLABORATION_MODE>
+01:message/developer:
+ You are `/root`, the primary agent in a team of agents collaborating to fulfill the user's goals.
+
+ At the start of your turn, you are the active agent.
+ You can spawn sub-agents to handle subtasks, and those sub-agents can spawn their own sub-agents.
+ All agents in the team, including the agents that you can assign tasks to, are equally intelligent and capable, and have access to the same set of tools.
+
+ You can use `spawn_agent` to create a new agent, `followup_task` to give an existing agent a new task an...message to a running agent without triggering a turn. [hash=7d40ff8b58977202]
+ Child agents can also spawn their own sub-agents.
+ <OMITTED 16 LINES; ~234 TOKENS; hash=b2e94283a8c22327>
+ - All agents use the same current working directory.
+ - As a result, edits made by one agent are immediately visible to all other agents.
+
+ When calling `wait_agent`, prefer longer waits (minutes) to avoid busy polling.
+
+ There are 4 available concurrency slots, meaning that up to 4 agents can be active at once, including you.
+
+ Full-history forks (`fork_turns` omitted or `"all"`) inherit the parent model and reasoning effort and d...fork_turns` to `"none"` or a positive integer string. [hash=db3545796947884f]
+02:message/developer:
+ <MULTI_AGENT_MODE>
+03:message/user:
+ function call limit push
+04:message/user:
+ <COMPACTION_SUMMARY>
+ AUTO_SUMMARY
+-- request 4 (turn) --
+05:message/assistant:
+ continued
+06:message/user:
+ continue after cold resume
diff --git a/codex-rs/history/src/compaction_resume_metadata.rs b/codex-rs/history/src/compaction_resume_metadata.rs
index 19c6603623..1a70f8108d 100644
--- a/codex-rs/history/src/compaction_resume_metadata.rs
+++ b/codex-rs/history/src/compaction_resume_metadata.rs
@@ -1,5 +1,6 @@
//! Defines resume metadata stored directly on a compaction.
+use crate::RolloutItem;
use codex_protocol::protocol::MultiAgentVersion;
use schemars::JsonSchema;
use serde::Deserialize;
@@ -25,3 +26,24 @@ pub struct PreviousTurnSettings {
pub comp_hash: Option<String>,
pub realtime_active: Option<bool>,
}
+
+/// Returns the runtime version stored in a turn context or compaction resume metadata.
+pub fn resume_multi_agent_version(item: &RolloutItem) -> Option<MultiAgentVersion> {
+ match item {
+ RolloutItem::TurnContext(context) => context.multi_agent_version,
+ RolloutItem::Compacted(compacted) => compacted
+ .resume_metadata
+ .as_ref()
+ .and_then(|metadata| metadata.multi_agent_version),
+ RolloutItem::SessionMeta(_)
+ | RolloutItem::ResponseItem(_)
+ | RolloutItem::InterAgentCommunication(_)
+ | RolloutItem::InterAgentCommunicationMetadata { .. }
+ | RolloutItem::TokenUsageRecord(_)
+ | RolloutItem::WorldState(_)
+ | RolloutItem::RetainedContext(_)
+ | RolloutItem::SecurityRiskScore(_)
+ | RolloutItem::RealtimeItem(_)
+ | RolloutItem::EventMsg(_) => None,
+ }
+}
diff --git a/codex-rs/history/src/lib.rs b/codex-rs/history/src/lib.rs
index 3e82da9f94..a0cd404cb6 100644
--- a/codex-rs/history/src/lib.rs
+++ b/codex-rs/history/src/lib.rs
@@ -3,6 +3,7 @@
mod compaction_resume_metadata;
pub use compaction_resume_metadata::CompactionResumeMetadata;
pub use compaction_resume_metadata::PreviousTurnSettings;
+pub use compaction_resume_metadata::resume_multi_agent_version;
mod compaction_checkpoint;
pub use compaction_checkpoint::CompactionCheckpoint;
@@ -541,22 +542,7 @@ fn multi_agent_version_from_items(
_ => None,
});
- session_meta_version.or_else(|| {
- items.iter().rev().find_map(|item| match item {
- RolloutItem::TurnContext(turn_context) => turn_context.multi_agent_version,
- RolloutItem::SessionMeta(_)
- | RolloutItem::ResponseItem(_)
- | RolloutItem::InterAgentCommunication(_)
- | RolloutItem::InterAgentCommunicationMetadata { .. }
- | RolloutItem::Compacted(_)
- | RolloutItem::TokenUsageRecord(_)
- | RolloutItem::WorldState(_)
- | RolloutItem::RetainedContext(_)
- | RolloutItem::SecurityRiskScore(_)
- | RolloutItem::RealtimeItem(_)
- | RolloutItem::EventMsg(_) => None,
- })
- })
+ session_meta_version.or_else(|| items.iter().rev().find_map(resume_multi_agent_version))
}
#[cfg(test)]
diff --git a/codex-rs/history/src/tests.rs b/codex-rs/history/src/tests.rs
index e5f9d097b6..c414bacb77 100644
--- a/codex-rs/history/src/tests.rs
+++ b/codex-rs/history/src/tests.rs
@@ -889,3 +889,19 @@ fn multi_agent_version_uses_newest_present_session_meta_value() -> Result<()> {
);
Ok(())
}
+
+#[test]
+fn multi_agent_version_uses_compaction_metadata_without_turn_context() -> Result<()> {
+ let compacted = serde_json::from_value::<CompactedItem>(json!({
+ "message": "summary",
+ "replacement_history": [],
+ "window_number": 1,
+ "resume_metadata": {"multi_agent_version": "v2"}
+ }))?;
+
+ assert_eq!(
+ InitialHistory::Forked(vec![RolloutItem::Compacted(compacted)]).get_multi_agent_version(),
+ Some(MultiAgentVersion::V2)
+ );
+ Ok(())
+}
diff --git a/codex-rs/rollout/src/lib.rs b/codex-rs/rollout/src/lib.rs
index 78d4d1290a..6bcbf57fde 100644
--- a/codex-rs/rollout/src/lib.rs
+++ b/codex-rs/rollout/src/lib.rs
@@ -36,6 +36,7 @@ pub use codex_history::RetainedContextEvent;
pub use codex_history::RetainedInputSource;
pub use codex_history::RolloutItem;
pub use codex_history::RolloutLine;
+pub use codex_history::resume_multi_agent_version;
pub(crate) use codex_protocol::protocol;
/// Decodes a persisted rollout record without Serde's flattened-envelope buffering.
diff --git a/codex-rs/rollout/src/model_context.rs b/codex-rs/rollout/src/model_context.rs
index ba09120cdd..3a5854165d 100644
--- a/codex-rs/rollout/src/model_context.rs
+++ b/codex-rs/rollout/src/model_context.rs
@@ -1,10 +1,10 @@
-use crate::ResponseItemEnvelope;
+//! Collects persisted rollout items needed to reconstruct the most recent context window.
+//!
+//! Storage readers feed items newest-to-oldest. The scan stops at the newest compaction that has
+//! both replacement history and a window number, or at the beginning of the rollout when no such
+//! compaction exists. Items are returned in chronological order.
+
use crate::RolloutItem;
-use codex_protocol::items::TurnItem;
-use codex_protocol::models::ResponseItem;
-use codex_protocol::protocol::EventMsg;
-use codex_protocol::protocol::InterAgentCommunication;
-use codex_protocol::protocol::SessionMetaLine;
/// Whether a reverse model-context scan needs more rollout items.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -15,197 +15,45 @@ pub enum ModelContextScanProgress {
Complete,
}
-/// Accumulates newest-to-oldest rollout items until they are sufficient to reconstruct the latest
-/// model context.
-///
-/// Storage implementations own how they fetch older items. Local JSONL readers and future
-/// reverse-paged cloud readers can both feed their items through this scan to share the cutoff
-/// rules and chronological replay assembly.
-///
-/// The scan stops once it has both:
-///
-/// - `saw_compaction`: a `CompactedItem` with `replacement_history` and `window_number`;
-/// - `saw_completed_turn_context`: a completed turn with a compatible `TurnContextItem` and a
-/// durable context baseline.
-///
-/// If the scan reaches the beginning before finding a bounded cutoff, it has already collected
-/// the complete replay and so we can return that directly.
-///
-/// A turn establishes a context baseline with a user-turn boundary (a paginated
-/// `ItemCompleted(UserMessage)` marker, agent message, or inter-agent message), or a full
-/// `WorldState` snapshot newer than that turn's latest compaction. The snapshot also lets turns
-/// with empty input supply resume metadata, matching rollout reconstruction. Without either,
-/// the scan continues to older turns. A raw `role=user` response item is not sufficient because
-/// contextual user fragments use that role but do not count as turn boundaries during reconstruction.
-/// The compaction restores model-visible items; the turn context restores previous settings
-/// (`model`, `comp_hash`, and `realtime_active`) and the reference baseline.
+/// Finds a bounded suffix for reconstructing the most recent context window.
///
-/// These paginated shapes disable the bounded cutoff:
-///
-/// - compaction without `replacement_history` or `window_number`;
-/// - rollback markers;
-///
-/// When one appears, the scanner continues to the beginning and returns the complete replay.
+/// A compaction with replacement history and a window number is a complete conversation-history
+/// boundary. Records after it provide any companion state that was persisted. Older compactions
+/// missing either field require the complete rollout so reconstruction can rebuild their history
+/// or window number.
#[derive(Debug, Default)]
pub struct ModelContextScan {
items_newest_first: Vec<RolloutItem>,
- saw_compaction: bool,
- saw_completed_turn_context: bool,
- must_scan_to_start: bool,
- active_segment: ActiveTurnSegment,
+ requires_full_replay: bool,
}
impl ModelContextScan {
/// Adds the next newest-to-oldest rollout item and reports whether the reader can stop.
pub fn push(&mut self, item: RolloutItem) -> ModelContextScanProgress {
- let progress = self.observe(&item);
+ let progress = if self.requires_full_replay {
+ ModelContextScanProgress::Continue
+ } else if let RolloutItem::Compacted(compacted) = &item {
+ if compacted.replacement_history.is_some() && compacted.window_number.is_some() {
+ ModelContextScanProgress::Complete
+ } else {
+ // This compaction cannot be reconstructed from a bounded suffix. Do not stop at
+ // an older compaction because this newer one still affects the surviving history.
+ self.requires_full_replay = true;
+ ModelContextScanProgress::Continue
+ }
+ } else {
+ ModelContextScanProgress::Continue
+ };
self.items_newest_first.push(item);
progress
}
- /// Returns the collected items in chronological order with canonical head metadata.
+ /// Returns the collected items in chronological order.
///
/// Call this after the reader reaches the beginning of its source or after [`Self::push`]
/// returns [`ModelContextScanProgress::Complete`].
- pub fn finish(mut self, session_meta: SessionMetaLine) -> Vec<RolloutItem> {
+ pub fn finish(mut self) -> Vec<RolloutItem> {
self.items_newest_first.reverse();
- if self.has_bounded_cutoff() {
- // A bounded scan stops before reaching the head. Prepend the separately loaded head
- // SessionMeta, which remains canonical when copied fork history contains later
- // metadata.
- self.items_newest_first
- .insert(0, RolloutItem::SessionMeta(session_meta));
- }
self.items_newest_first
}
-
- fn observe(&mut self, item: &RolloutItem) -> ModelContextScanProgress {
- if self.must_scan_to_start {
- return ModelContextScanProgress::Continue;
- }
-
- match item {
- RolloutItem::Compacted(compacted)
- if compacted.replacement_history.is_none() || compacted.window_number.is_none() =>
- {
- self.must_scan_to_start = true;
- }
- RolloutItem::Compacted(_) => {
- self.saw_compaction = true;
- self.active_segment.saw_compaction = true;
- }
- RolloutItem::EventMsg(EventMsg::ThreadRolledBack(_)) => {
- // Rollback markers only occur in legacy history. Keep old rollouts correct rather than
- // duplicating rollback survival semantics in this bounded selector.
- self.must_scan_to_start = true;
- }
- RolloutItem::EventMsg(EventMsg::ItemCompleted(event)) => {
- if self.active_segment.turn_id.is_none() {
- self.active_segment.turn_id = Some(event.turn_id.clone());
- }
- if turn_ids_are_compatible(
- self.active_segment.turn_id.as_deref(),
- Some(event.turn_id.as_str()),
- ) {
- self.active_segment.has_user_turn |=
- matches!(&event.item, TurnItem::UserMessage(_));
- }
- }
- RolloutItem::EventMsg(EventMsg::TurnComplete(event)) => {
- self.active_segment
- .turn_id
- .get_or_insert_with(|| event.turn_id.clone());
- }
- RolloutItem::EventMsg(EventMsg::TurnAborted(event)) => {
- if let Some(turn_id) = &event.turn_id {
- self.active_segment
- .turn_id
- .get_or_insert_with(|| turn_id.clone());
- }
- }
- RolloutItem::EventMsg(EventMsg::TurnStarted(event)) => {
- if turn_ids_are_compatible(
- self.active_segment.turn_id.as_deref(),
- Some(event.turn_id.as_str()),
- ) {
- self.finalize_active_segment();
- }
- }
- RolloutItem::TurnContext(context) => {
- if self.active_segment.turn_id.is_none() {
- self.active_segment.turn_id = context.turn_id.clone();
- }
- if turn_ids_are_compatible(
- self.active_segment.turn_id.as_deref(),
- context.turn_id.as_deref(),
- ) {
- self.active_segment.has_turn_context = true;
- }
- }
- RolloutItem::ResponseItem(response_item) => {
- self.active_segment.has_user_turn |=
- response_item_counts_as_user_turn(response_item);
- }
- RolloutItem::InterAgentCommunication(_) => {
- self.active_segment.has_user_turn = true;
- }
- RolloutItem::EventMsg(EventMsg::UserMessage(_)) => {
- self.active_segment.has_user_turn = true;
- }
- RolloutItem::WorldState(state) => {
- self.active_segment.has_full_world_state |=
- state.full && !self.active_segment.saw_compaction;
- }
- RolloutItem::EventMsg(_)
- | RolloutItem::SessionMeta(_)
- | RolloutItem::InterAgentCommunicationMetadata { .. }
- | RolloutItem::RealtimeItem(_)
- | RolloutItem::RetainedContext(_)
- | RolloutItem::SecurityRiskScore(_)
- | RolloutItem::TokenUsageRecord(_) => {}
- }
-
- if self.has_bounded_cutoff() {
- ModelContextScanProgress::Complete
- } else {
- ModelContextScanProgress::Continue
- }
- }
-
- fn finalize_active_segment(&mut self) {
- if self.active_segment.has_turn_context
- && (self.active_segment.has_user_turn || self.active_segment.has_full_world_state)
- {
- self.saw_completed_turn_context = true;
- }
- self.active_segment = ActiveTurnSegment::default();
- }
-
- fn has_bounded_cutoff(&self) -> bool {
- !self.must_scan_to_start && self.saw_compaction && self.saw_completed_turn_context
- }
-}
-
-#[derive(Debug, Default)]
-struct ActiveTurnSegment {
- turn_id: Option<String>,
- has_user_turn: bool,
- has_turn_context: bool,
- has_full_world_state: bool,
- saw_compaction: bool,
-}
-
-fn turn_ids_are_compatible(active_turn_id: Option<&str>, item_turn_id: Option<&str>) -> bool {
- active_turn_id
- .is_none_or(|turn_id| item_turn_id.is_none_or(|item_turn_id| item_turn_id == turn_id))
-}
-
-fn response_item_counts_as_user_turn(response_item: &ResponseItemEnvelope) -> bool {
- match &response_item.item {
- ResponseItem::AgentMessage { .. } => true,
- ResponseItem::Message { role, content, .. } => {
- role == "assistant" && InterAgentCommunication::is_message_content(content)
- }
- _ => false,
- }
}
diff --git a/codex-rs/thread-store/src/local/model_context.rs b/codex-rs/thread-store/src/local/model_context.rs
index c96883747d..7e898add6d 100644
--- a/codex-rs/thread-store/src/local/model_context.rs
+++ b/codex-rs/thread-store/src/local/model_context.rs
@@ -26,11 +26,9 @@ mod tests;
/// Loads rollout items needed to reconstruct the latest model-visible context.
///
-/// Paginated JSONL rollouts use a reverse scan. When it finds both a usable replacement-
-/// history checkpoint and the completed user-turn context needed for resume metadata, the returned
-/// replay starts with the canonical `SessionMeta` followed by that newest suffix. When no
-/// bounded cutoff is available, the scan continues to the beginning and returns the complete
-/// replay it already accumulated.
+/// Paginated JSONL rollouts use a reverse scan. It stops at the newest `CompactedItem` with both
+/// replacement history and a window number, and returns that compaction plus its newer suffix. If
+/// the newest compaction lacks either field, the scan continues to the beginning of the rollout.
///
/// Compressed segments are decoded before applying their original JSONL offsets. Legacy rollouts
/// keep the existing full-history path.
@@ -115,9 +113,7 @@ pub(super) async fn load_for_fork(
let ScanOutcome::Parsed(line) = outcome else {
continue;
};
- if let RolloutItem::TurnContext(context) = &line.item
- && let Some(version) = context.multi_agent_version
- {
+ if let Some(version) = codex_rollout::resume_multi_agent_version(&line.item) {
return Ok(Some(version));
}
// Ancestor metadata does not describe the immediate source's runtime.
@@ -191,10 +187,7 @@ fn scan_model_context_from_lineage_blocking(
}
}
- let canonical_meta = session_meta.clone();
- let mut items = scan.finish(session_meta);
- if !matches!(items.first(), Some(RolloutItem::SessionMeta(_))) {
- items.insert(0, RolloutItem::SessionMeta(canonical_meta));
- }
+ let mut items = scan.finish();
+ items.insert(0, RolloutItem::SessionMeta(session_meta));
Ok(items)
}
diff --git a/codex-rs/thread-store/src/local/model_context_tests.rs b/codex-rs/thread-store/src/local/model_context_tests.rs
index 00cb1e9ce8..eeef222d75 100644
--- a/codex-rs/thread-store/src/local/model_context_tests.rs
+++ b/codex-rs/thread-store/src/local/model_context_tests.rs
@@ -7,7 +7,6 @@ use codex_protocol::ThreadId;
use codex_protocol::config_types::ReasoningSummary;
use codex_protocol::items::TurnItem;
use codex_protocol::items::UserMessageItem;
-use codex_protocol::models::AgentMessageInputContent;
use codex_protocol::models::ContentItem;
use codex_protocol::models::ResponseItem;
use codex_protocol::protocol::AskForApproval;
@@ -35,29 +34,28 @@ use crate::local::test_support::test_config;
use crate::local::test_support::write_session_file_with_history_mode;
#[tokio::test]
-async fn loads_latest_checkpoint_with_required_turn_metadata() {
+async fn stops_at_newest_usable_compaction_and_keeps_companions() {
let home = TempDir::new().expect("temp dir");
let uuid = Uuid::from_u128(/*v*/ 1001);
- let thread_id = codex_protocol::ThreadId::from_string(&uuid.to_string()).expect("thread id");
- write_paginated_rollout(
+ let thread_id = ThreadId::from_string(&uuid.to_string()).expect("thread id");
+ let world_state = RolloutItem::WorldState(WorldStateItem::full(Default::default()));
+ let turn_context = turn_context(home.path(), "companion");
+ let path = write_paginated_rollout(
home.path(),
"2025-01-03T13-00-00",
uuid,
[
- turn_started("turn-1"),
- user_message("older turn"),
- completed_user_message("turn-1", "older turn"),
- turn_context(home.path(), "turn-1"),
- compacted("older checkpoint", Some(Vec::new())),
- turn_complete("turn-1"),
- turn_started("turn-2"),
- user_message("latest turn"),
- completed_user_message("turn-2", "latest turn"),
- turn_context(home.path(), "turn-2"),
- compacted("latest checkpoint", Some(Vec::new())),
- turn_complete("turn-2"),
+ user_message("older history"),
+ compacted("older compaction", Some(Vec::new())),
+ user_message("superseded suffix"),
+ compacted("latest compaction", Some(Vec::new())),
+ world_state.clone(),
+ turn_context.clone(),
],
);
+ let session_meta = codex_rollout::read_session_meta_line(path.as_path())
+ .await
+ .expect("read session metadata");
let store = LocalThreadStore::new(test_config(home.path()), /*state_db*/ None);
let context = store
@@ -68,19 +66,16 @@ async fn loads_latest_checkpoint_with_required_turn_metadata() {
.await
.expect("load model context");
- assert!(matches!(
- context.items.first(),
- Some(RolloutItem::SessionMeta(_))
- ));
- assert!(context.items.iter().any(|item| {
- matches!(item, RolloutItem::Compacted(compacted) if compacted.message == "latest checkpoint")
- }));
- assert!(!context.items.iter().any(|item| {
- matches!(item, RolloutItem::Compacted(compacted) if compacted.message == "older checkpoint")
- }));
- assert!(context.items.iter().any(|item| {
- matches!(item, RolloutItem::TurnContext(context) if context.turn_id.as_deref() == Some("turn-2"))
- }));
+ assert_eq!(
+ serde_json::to_value(context.items).expect("serialize context"),
+ serde_json::to_value(vec![
+ RolloutItem::SessionMeta(session_meta),
+ compacted("latest compaction", Some(Vec::new())),
+ world_state,
+ turn_context,
+ ])
+ .expect("serialize expected context")
+ );
}
#[tokio::test]
@@ -104,10 +99,10 @@ async fn loads_recent_context_after_many_empty_wake_turns() {
for index in 0..32 {
let turn_id = format!("wake-{index}");
let mut items = vec![turn_started(&turn_id)];
- if index % 8 == 0 {
- expected_suffix.clear();
+ let starts_new_window = index % 8 == 0;
+ if starts_new_window {
items.extend([
- compacted(&format!("checkpoint-{index}"), Some(Vec::new())),
+ compacted(&format!("compaction-{index}"), Some(Vec::new())),
RolloutItem::WorldState(WorldStateItem::full(Default::default())),
]);
}
@@ -117,7 +112,11 @@ async fn loads_recent_context_after_many_empty_wake_turns() {
turn_complete(&turn_id),
]);
append_items(&path, items.clone());
- expected_suffix.extend(items);
+ if starts_new_window {
+ expected_suffix = items.into_iter().skip(1).collect();
+ } else {
+ expected_suffix.extend(items);
+ }
}
let session_meta = codex_rollout::read_session_meta_line(&path)
.await
@@ -138,59 +137,6 @@ async fn loads_recent_context_after_many_empty_wake_turns() {
);
}
-#[tokio::test]
-async fn empty_wake_requires_surviving_full_world_state_and_matching_context() {
- enum MissingBaseline {
- SnapshotBeforeCompaction,
- PatchOnly,
- MissingContext,
- IncompatibleContext,
- }
- for baseline in [
- MissingBaseline::SnapshotBeforeCompaction,
- MissingBaseline::PatchOnly,
- MissingBaseline::MissingContext,
- MissingBaseline::IncompatibleContext,
- ] {
- let home = TempDir::new().expect("temp dir");
- let path = write_paginated_rollout(
- home.path(),
- "2025-01-03T13-00-08",
- Uuid::from_u128(/*v*/ 1009),
- [
- turn_started("user-turn"),
- completed_user_message("user-turn", "keep working"),
- turn_context(home.path(), "user-turn"),
- turn_complete("user-turn"),
- turn_started("wake"),
- ],
- );
- let full = RolloutItem::WorldState(WorldStateItem::full(Default::default()));
- let checkpoint = compacted("checkpoint", Some(Vec::new()));
- let context = turn_context(home.path(), "wake");
- let items = match baseline {
- MissingBaseline::SnapshotBeforeCompaction => vec![full, checkpoint, context],
- MissingBaseline::PatchOnly => vec![
- checkpoint,
- RolloutItem::WorldState(WorldStateItem::patch(Default::default())),
- context,
- ],
- MissingBaseline::MissingContext => vec![checkpoint, full],
- MissingBaseline::IncompatibleContext => {
- vec![
- checkpoint,
- full,
- turn_context(home.path(), "different-turn"),
- ]
- }
- };
- append_items(&path, items);
- append_items(&path, [turn_complete("wake")]);
-
- assert_reverse_scan_matches_full_history(home.path(), &path).await;
- }
-}
-
#[tokio::test]
async fn fork_context_excludes_items_after_frozen_cutoff() {
let home = TempDir::new().expect("temp dir");
@@ -265,15 +211,20 @@ async fn fork_version_stops_before_older_segments_once_resolved() {
),
)
.expect("write child metadata");
- let RolloutItem::TurnContext(mut context) = turn_context(home.path(), "version-turn")
+ let RolloutItem::Compacted(mut compacted) =
+ compacted("version compaction", Some(Vec::new()))
else {
unreachable!();
};
- context.multi_agent_version = Some(MultiAgentVersion::V2);
+ compacted.resume_metadata = Some(codex_rollout::CompactionResumeMetadata {
+ multi_agent_version: Some(MultiAgentVersion::V2),
+ last_started_turn_id: None,
+ previous_turn_settings: None,
+ });
append_items(
&child_path,
[
- RolloutItem::TurnContext(context),
+ RolloutItem::Compacted(compacted),
turn_context(home.path(), "unset-turn"),
],
);
@@ -282,7 +233,7 @@ async fn fork_version_stops_before_older_segments_once_resolved() {
.resolve_rollout_lineage(child_id)
.await
.expect("resolve source lineage");
- // No checkpoint exists. A full model-context read would reach this missing segment.
+ // The usable compaction should stop the scan before it reaches this missing segment.
std::fs::remove_file(root_path).expect("remove older segment after resolving lineage");
let context = load_for_fork(lineage, /*history_base*/ None)
@@ -349,73 +300,40 @@ async fn fork_version_respects_inherited_segment_cutoffs() {
);
}
-#[tokio::test]
-async fn loads_turn_metadata_across_an_older_checkpoint() {
- let home = TempDir::new().expect("temp dir");
- let uuid = Uuid::from_u128(/*v*/ 1006);
- let thread_id = codex_protocol::ThreadId::from_string(&uuid.to_string()).expect("thread id");
- write_paginated_rollout(
- home.path(),
- "2025-01-03T13-00-05",
- uuid,
- [
- turn_started("turn-0"),
- user_message("oldest turn"),
- completed_user_message("turn-0", "oldest turn"),
- turn_context(home.path(), "turn-0"),
- turn_complete("turn-0"),
- turn_started("turn-1"),
- user_message("metadata turn"),
- completed_user_message("turn-1", "metadata turn"),
- turn_context(home.path(), "turn-1"),
- compacted("older checkpoint", Some(Vec::new())),
- turn_complete("turn-1"),
- turn_started("turn-2"),
- compacted("latest checkpoint", Some(Vec::new())),
- turn_complete("turn-2"),
- ],
- );
- let store = LocalThreadStore::new(test_config(home.path()), /*state_db*/ None);
-
- let context = store
- .load_latest_model_context(LoadThreadHistoryParams {
- thread_id,
- include_archived: false,
- })
- .await
- .expect("load model context");
-
- assert!(context.items.iter().any(|item| {
- matches!(item, RolloutItem::Compacted(compacted) if compacted.message == "latest checkpoint")
- }));
- assert!(context.items.iter().any(|item| {
- matches!(item, RolloutItem::TurnContext(context) if context.turn_id.as_deref() == Some("turn-1"))
- }));
- assert!(!context.items.iter().any(|item| {
- matches!(item, RolloutItem::TurnContext(context) if context.turn_id.as_deref() == Some("turn-0"))
- }));
-}
-
#[tokio::test]
async fn returns_scanned_full_history_for_unsupported_compaction() {
- let home = TempDir::new().expect("temp dir");
- let uuid = Uuid::from_u128(/*v*/ 1002);
- let path = write_paginated_rollout(
- home.path(),
- "2025-01-03T13-00-01",
- uuid,
- [
- turn_started("turn-1"),
- user_message("turn"),
- completed_user_message("turn-1", "turn"),
- turn_context(home.path(), "turn-1"),
- compacted("usable checkpoint", Some(Vec::new())),
- compacted("legacy checkpoint", /*replacement_history*/ None),
- turn_complete("turn-1"),
- ],
- );
+ enum MissingField {
+ ReplacementHistory,
+ WindowNumber,
+ }
- assert_reverse_scan_matches_full_history(home.path(), path.as_path()).await;
+ for (index, missing_field) in [MissingField::ReplacementHistory, MissingField::WindowNumber]
+ .into_iter()
+ .enumerate()
+ {
+ let home = TempDir::new().expect("temp dir");
+ let uuid = Uuid::from_u128(1002 + index as u128);
+ let mut unsupported = compacted("unsupported compaction", Some(Vec::new()));
+ let RolloutItem::Compacted(unsupported_compaction) = &mut unsupported else {
+ unreachable!("compacted helper returns a compaction");
+ };
+ match missing_field {
+ MissingField::ReplacementHistory => unsupported_compaction.replacement_history = None,
+ MissingField::WindowNumber => unsupported_compaction.window_number = None,
+ }
+ let path = write_paginated_rollout(
+ home.path(),
+ "2025-01-03T13-00-01",
+ uuid,
+ [
+ user_message("older history"),
+ compacted("older usable compaction", Some(Vec::new())),
+ unsupported,
+ ],
+ );
+
+ assert_reverse_scan_matches_full_history(home.path(), path.as_path()).await;
+ }
}
#[tokio::test]
@@ -438,83 +356,6 @@ async fn returns_scanned_full_history_at_bof_without_checkpoint() {
assert_reverse_scan_matches_full_history(home.path(), path.as_path()).await;
}
-#[tokio::test]
-async fn uses_agent_message_turn_context_without_scanning_older_turn() {
- let home = TempDir::new().expect("temp dir");
- let uuid = Uuid::from_u128(/*v*/ 1004);
- let thread_id = codex_protocol::ThreadId::from_string(&uuid.to_string()).expect("thread id");
- write_paginated_rollout(
- home.path(),
- "2025-01-03T13-00-03",
- uuid,
- [
- turn_started("turn-1"),
- user_message("older turn"),
- completed_user_message("turn-1", "older turn"),
- turn_context(home.path(), "turn-1"),
- compacted("checkpoint", Some(Vec::new())),
- turn_complete("turn-1"),
- turn_started("turn-2"),
- turn_context(home.path(), "turn-2"),
- agent_message("child done"),
- turn_complete("turn-2"),
- ],
- );
- let store = LocalThreadStore::new(test_config(home.path()), /*state_db*/ None);
-
- let context = store
- .load_latest_model_context(LoadThreadHistoryParams {
- thread_id,
- include_archived: false,
- })
- .await
- .expect("load model context");
-
- assert!(context.items.iter().any(|item| {
- matches!(item, RolloutItem::TurnContext(context) if context.turn_id.as_deref() == Some("turn-2"))
- }));
- assert!(!context.items.iter().any(|item| {
- matches!(item, RolloutItem::TurnContext(context) if context.turn_id.as_deref() == Some("turn-1"))
- }));
-}
-
-#[tokio::test]
-async fn ignores_contextual_user_messages_when_selecting_turn_context() {
- let home = TempDir::new().expect("temp dir");
- let uuid = Uuid::from_u128(/*v*/ 1005);
- let thread_id = codex_protocol::ThreadId::from_string(&uuid.to_string()).expect("thread id");
- write_paginated_rollout(
- home.path(),
- "2025-01-03T13-00-04",
- uuid,
- [
- turn_started("turn-1"),
- user_message("real user turn"),
- completed_user_message("turn-1", "real user turn"),
- turn_context(home.path(), "turn-1"),
- compacted("checkpoint", Some(Vec::new())),
- turn_complete("turn-1"),
- turn_started("turn-2"),
- contextual_user_message(),
- turn_context(home.path(), "turn-2"),
- turn_complete("turn-2"),
- ],
- );
- let store = LocalThreadStore::new(test_config(home.path()), /*state_db*/ None);
-
- let context = store
- .load_latest_model_context(LoadThreadHistoryParams {
- thread_id,
- include_archived: false,
- })
- .await
- .expect("load model context");
-
- assert!(context.items.iter().any(|item| {
- matches!(item, RolloutItem::TurnContext(context) if context.turn_id.as_deref() == Some("turn-1"))
- }));
-}
-
#[tokio::test]
async fn replays_nested_archived_lineage_from_frozen_prefix() {
let home = TempDir::new().expect("temp dir");
@@ -833,21 +674,6 @@ fn completed_user_message(turn_id: &str, message: &str) -> RolloutItem {
}))
}
-fn agent_message(message: &str) -> RolloutItem {
- RolloutItem::ResponseItem(
- ResponseItem::AgentMessage {
- id: None,
- author: "worker".to_string(),
- recipient: "root".to_string(),
- content: vec![AgentMessageInputContent::InputText {
- text: message.to_string(),
- }],
- internal_chat_message_metadata_passthrough: None,
- }
- .into(),
- )
-}
-
fn turn_context(root: &Path, turn_id: &str) -> RolloutItem {
RolloutItem::TurnContext(TurnContextItem {
turn_id: Some(turn_id.to_string()),
diff --git a/codex-rs/thread-store/src/local/rollout_migration.rs b/codex-rs/thread-store/src/local/rollout_migration.rs
index ef7d9aec43..c259a6f034 100644
--- a/codex-rs/thread-store/src/local/rollout_migration.rs
+++ b/codex-rs/thread-store/src/local/rollout_migration.rs
@@ -713,14 +713,7 @@ impl LocalThreadStore {
// phase. Keep the individual operations readable and tag the phase once if it fails.
let conversion_result = async {
let bounded_subagent_context = if kind == RolloutMigrationKind::Subagent {
- let RolloutItem::SessionMeta(session_meta) = &canonical_session_meta.item else {
- return Err(migration_error("canonical session metadata is missing"));
- };
- let context = subagent::select_bounded_context(
- source_path.to_path_buf(),
- session_meta.clone(),
- )
- .await?;
+ let context = subagent::select_bounded_context(source_path.to_path_buf()).await?;
limiter.account(source_metadata.len()).await;
context
} else {
diff --git a/codex-rs/thread-store/src/local/rollout_migration/subagent.rs b/codex-rs/thread-store/src/local/rollout_migration/subagent.rs
index 775b6f447d..1adce87530 100644
--- a/codex-rs/thread-store/src/local/rollout_migration/subagent.rs
+++ b/codex-rs/thread-store/src/local/rollout_migration/subagent.rs
@@ -4,15 +4,13 @@
//! files verbatim can preserve gigabytes of duplicated history even though a resumed subagent only
//! needs its latest bounded model context.
//!
-//! This module reverse-scans the legacy rollout for a safe model-context checkpoint and returns the
-//! smallest replay needed to resume from there. Malformed reverse-scanned records are skipped. If
-//! we cannot prove that a bounded replay is safe, we return `None` and let the caller fall back to
-//! full tolerant replay.
+//! This module reverse-scans the legacy rollout for the newest compaction with replacement history
+//! and a window number. Malformed records are skipped. If no such compaction exists, the caller
+//! falls back to full tolerant replay.
use std::fs::File;
use std::path::PathBuf;
-use codex_protocol::protocol::SessionMetaLine;
use codex_rollout::ModelContextScan;
use codex_rollout::ModelContextScanProgress;
use codex_rollout::ReverseJsonlScanner;
@@ -24,11 +22,9 @@ use super::line_parser;
use super::migration_error;
use crate::ThreadStoreResult;
-/// Returns a bounded replay when the legacy child has enough durable context to reconstruct from
-/// a suffix. Without that proof, the caller streams the complete legacy replay instead.
+/// Returns the suffix starting at the newest usable compaction, if one exists.
pub(super) async fn select_bounded_context(
rollout_path: PathBuf,
- session_meta: SessionMetaLine,
) -> ThreadStoreResult<Option<Vec<RolloutItem>>> {
tokio::task::spawn_blocking(move || {
let file = File::open(rollout_path).map_err(migration_error)?;
@@ -46,7 +42,7 @@ pub(super) async fn select_bounded_context(
continue;
};
if scan.push(line.item) == ModelContextScanProgress::Complete {
- let mut items = scan.finish(session_meta);
+ let mut items = scan.finish();
items.retain(|item| !matches!(item, RolloutItem::SessionMeta(_)));
return Ok(Some(items));
}