mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Resume model context from the latest compaction boundary (#47365)
## Why Resuming a compacted thread should use the compaction and its newer records without depending on older turn contexts or restoring stale state from before the compaction. ## What changed - Stop paginated model-context scans at the newest compaction with `replacement_history` and `window_number`. Fall back to full replay if the newest compaction lacks either field. - Align reconstruction with that boundary and restore `last_started_turn_id`, previous turn settings, and the multi-agent version from compaction resume metadata. Allow newer completed turns to override the saved settings. - Keep canonical session metadata alongside the bounded replay and use the same scan boundary for subagent rollout migration. ## Testing Add coverage for cold resume after mid-turn compaction, bounded/full replay equivalence, missing companion state, settings precedence, and incomplete-compaction fallback. GitOrigin-RevId: f836464bb5cadd9be4a4e05b5d1679223713fc87
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -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"),
|
||||
])),
|
||||
);
|
||||
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,
|
||||
),
|
||||
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!(
|
||||
reconstructed.previous_turn_settings,
|
||||
Some(expected_settings)
|
||||
);
|
||||
assert_eq!(reconstructed.reference_context_item, Some(newer_context));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user