Restore thread cwd from owned settings snapshots (#41567)

## Why

Resuming a thread without an explicit `cwd` should restore that thread's latest
retained setting. Forked history can contain settings copied from another
thread, while compaction can move the latest setting outside the replay window.

## What changed

- Record the owning thread ID on new settings snapshots and only use snapshots
  owned by the resumed thread when restoring `cwd`. Older snapshots without an
  owner remain readable but do not override the startup `cwd`.
- Checkpoint the current settings after compaction and serialize checkpoints
  with settings updates so the retained history contains an accepted, current
  snapshot.
- Cover resume behavior across compaction, forks, reverts, legacy histories,
  and concurrent settings updates.

GitOrigin-RevId: 1efc9cf55472d6b88c465c4efe44672a232dbf64
This commit is contained in:
rhan-oai authored and copyberry committed 2026-08-29 19:57:06 +00:00
1 parent 4210c08def
commit f5636bb733
20 files changed
+462 -35

No files matched your search

+11 -6
View File
@@ -3634,6 +3634,9 @@ impl Session {
.map(|id| id.to_string()),
window_id: Some(metadata.window_ids.window_id.to_string()),
};
// Wait for accepted updates to finish persisting, then keep later updates from
// overtaking the current settings snapshot while its checkpoint is written.
let _settings_guard = thread_settings::acquire_persistence_lock(self).await;
// Compaction starts a new history window, so its WorldState baseline must be full.
let mut world_state_item = None;
{
@@ -3646,17 +3649,19 @@ impl Session {
}
}
self.persist_rollout_items(&[RolloutItem::Compacted(compacted_item)])
.await;
let mut rollout_items = vec![RolloutItem::Compacted(compacted_item)];
// Persist the baseline after the replacement history that established it.
if let Some(world_state_item) = world_state_item {
self.persist_rollout_items(&[RolloutItem::WorldState(world_state_item)])
.await;
rollout_items.push(RolloutItem::WorldState(world_state_item));
}
if let Some(turn_context_item) = reference_context_item {
self.persist_rollout_items(&[RolloutItem::TurnContext(turn_context_item)])
.await;
rollout_items.push(RolloutItem::TurnContext(turn_context_item));
}
// The frozen turn context must not override current settings in persisted metadata.
rollout_items.push(RolloutItem::EventMsg(
thread_settings::applied_event(self).await,
));
self.persist_rollout_items(&rollout_items).await;
{
let mut state = self.state.lock().await;
state.queue_pending_session_start_source(codex_hooks::SessionStartSource::Compact);