mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
## Why Resuming a thread rereads persisted history even when a preloaded snapshot is unchanged. Reusing that snapshot must still account for writes committed before writer ownership is acquired. ## What changed - Carry optional, non-serialized revisions from stored history and model context through thread and agent resume paths. - Validate the revision under writer ownership and reuse the shared snapshot when it matches. Reload stored history when the revision is missing or changed, while preserving explicit history overrides. - Resolve the selected rollout before loading context, validate compressed snapshots before materialization, and enforce archived-thread restrictions on canonical paths. ## Testing Extend coverage for snapshot reuse and late-write refresh in legacy and paginated histories, compressed rollout reuse, archived rollout symlinks, and cold child restoration that includes edits made after preloading history. GitOrigin-RevId: e1fd7aee6ef2b30f41951e6b7e6b01c3e1420cab
211 lines
8.0 KiB
Rust
211 lines
8.0 KiB
Rust
use codex_protocol::ThreadId;
|
|
use codex_protocol::protocol::ThreadHistoryMode;
|
|
use codex_rollout::RolloutConfig;
|
|
use codex_rollout::RolloutRecorder;
|
|
use codex_rollout::RolloutRecorderParams;
|
|
|
|
use super::LocalThreadStore;
|
|
use super::paginated_fork;
|
|
use super::thread_rollout_resolver;
|
|
use crate::ForkBoundary;
|
|
use crate::RevertThreadParams;
|
|
use crate::ThreadStoreError;
|
|
use crate::ThreadStoreResult;
|
|
|
|
/// Revert an unloaded paginated thread by creating a new immutable rollout file.
|
|
///
|
|
/// Old rollouts stay intact. The new file references the retained prefix, and the only mutable
|
|
/// cutover is the existing SQLite rollout-path pointer for the thread.
|
|
pub(super) async fn revert(
|
|
store: &LocalThreadStore,
|
|
params: RevertThreadParams,
|
|
) -> ThreadStoreResult<()> {
|
|
let RevertThreadParams {
|
|
thread_id,
|
|
before_turn_id,
|
|
multi_agent_version,
|
|
} = params;
|
|
let state_db = store
|
|
.state_db()
|
|
.await
|
|
.ok_or(ThreadStoreError::Unsupported {
|
|
operation: "revert_thread",
|
|
})?;
|
|
let _lifecycle_guard = store.live_writer_locks.lock_lifecycle(thread_id).await;
|
|
let _live_writer_guard = store.live_writer_locks.lock(thread_id).await;
|
|
store.ensure_live_recorder_absent(thread_id).await?;
|
|
let writer_lock = store.acquire_writer_lock(thread_id)?;
|
|
|
|
// Resolution may return a compressed sibling. Keep SQLite's exact stored path for the CAS.
|
|
let stored_metadata = state_db
|
|
.get_thread(thread_id)
|
|
.await
|
|
.map_err(|err| ThreadStoreError::Internal {
|
|
message: format!("failed to read thread metadata for {thread_id}: {err}"),
|
|
})?
|
|
.ok_or(ThreadStoreError::ThreadNotFound { thread_id })?;
|
|
let expected_sqlite_path = stored_metadata.rollout_path;
|
|
let current_rollout = thread_rollout_resolver::resolve_current(store, thread_id)
|
|
.await?
|
|
.ok_or(ThreadStoreError::ThreadNotFound { thread_id })?;
|
|
let source_path = current_rollout.path;
|
|
let mut source_meta = codex_rollout::read_session_meta_line(source_path.as_path())
|
|
.await
|
|
.map_err(|err| ThreadStoreError::Internal {
|
|
message: format!(
|
|
"failed to read current paginated rollout {}: {err}",
|
|
source_path.display()
|
|
),
|
|
})?
|
|
.meta;
|
|
if source_meta.id != current_rollout.thread_id {
|
|
return Err(ThreadStoreError::InvalidRequest {
|
|
message: format!("current rollout for {thread_id} belongs to another thread"),
|
|
});
|
|
}
|
|
if source_meta.history_mode != ThreadHistoryMode::Paginated {
|
|
return Err(ThreadStoreError::InvalidRequest {
|
|
message: format!("thread {thread_id} does not use paginated history"),
|
|
});
|
|
}
|
|
|
|
// Older binaries can omit creator fields when replacing a rollout during revert.
|
|
source_meta.creator_user_id = source_meta
|
|
.creator_user_id
|
|
.or(stored_metadata.creator_user_id);
|
|
source_meta.creator_account_id = source_meta
|
|
.creator_account_id
|
|
.or(stored_metadata.creator_account_id);
|
|
|
|
// Preserve old-reader compatibility when introducing the first reference to a standalone
|
|
// source. Already-shared ancestors stay read-only; their offsets address decoded JSONL bytes.
|
|
let mut lineage = store
|
|
.resolve_rollout_lineage(thread_id, /*initial_path*/ None)
|
|
.await?;
|
|
for segment in &mut lineage.segments {
|
|
if segment.rollout_id() == current_rollout.rollout_id && source_meta.history_base.is_none()
|
|
{
|
|
segment.rollout_path =
|
|
codex_rollout::materialize_rollout_for_reference(segment.rollout_path.as_path())
|
|
.await
|
|
.map_err(|err| ThreadStoreError::Internal {
|
|
message: format!(
|
|
"failed to materialize rollout {} for revert: {err}",
|
|
segment.rollout_path.display()
|
|
),
|
|
})?;
|
|
}
|
|
super::thread_history_materialization::materialize_to_sqlite(
|
|
store,
|
|
segment.rollout_id(),
|
|
segment.rollout_path.as_path(),
|
|
)
|
|
.await?;
|
|
}
|
|
let history_base = paginated_fork::history_base_at_boundary(
|
|
store,
|
|
thread_id,
|
|
ForkBoundary::BeforeTurn(before_turn_id),
|
|
&lineage,
|
|
)
|
|
.await?;
|
|
|
|
let forked_from_ordinal_exclusive =
|
|
codex_rollout::forked_from_ordinal_exclusive(&source_meta, Some(source_path.as_path()))
|
|
.map(|cutoff| {
|
|
// Reverting into inherited history can shrink, but never grow, the parent prefix.
|
|
cutoff.min(history_base.map_or(0, |base| base.end_ordinal_exclusive))
|
|
});
|
|
|
|
source_meta.multi_agent_version = multi_agent_version.or(source_meta.multi_agent_version);
|
|
let rollout_id = ThreadId::new();
|
|
let recorder = create_replacement_recorder(
|
|
store,
|
|
source_meta,
|
|
rollout_id,
|
|
history_base,
|
|
forked_from_ordinal_exclusive,
|
|
writer_lock,
|
|
)
|
|
.await?;
|
|
let replacement_path = recorder.rollout_path().to_path_buf();
|
|
recorder.persist().await.map_err(thread_store_io_error)?;
|
|
recorder.shutdown().await.map_err(thread_store_io_error)?;
|
|
|
|
let replaced = state_db
|
|
.replace_rollout_path_if_current(
|
|
thread_id,
|
|
expected_sqlite_path.as_path(),
|
|
replacement_path.as_path(),
|
|
)
|
|
.await
|
|
.map_err(|err| ThreadStoreError::Internal {
|
|
message: format!("failed to switch thread {thread_id} to reverted rollout: {err}"),
|
|
})?;
|
|
if !replaced {
|
|
let _ = tokio::fs::remove_file(replacement_path.as_path()).await;
|
|
return Err(ThreadStoreError::Conflict {
|
|
message: format!("thread {thread_id} changed while it was being reverted"),
|
|
});
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
async fn create_replacement_recorder(
|
|
store: &LocalThreadStore,
|
|
source_meta: codex_rollout::SessionMeta,
|
|
rollout_id: ThreadId,
|
|
history_base: Option<codex_protocol::protocol::HistoryPosition>,
|
|
forked_from_ordinal_exclusive: Option<u64>,
|
|
writer_lock: super::WriterLockGuard,
|
|
) -> ThreadStoreResult<RolloutRecorder> {
|
|
let config = RolloutConfig {
|
|
codex_home: store.config.codex_home.clone(),
|
|
sqlite: store.config.sqlite.clone(),
|
|
cwd: source_meta.cwd.clone(),
|
|
model_provider_id: source_meta
|
|
.model_provider
|
|
.clone()
|
|
.unwrap_or_else(|| store.config.default_model_provider_id.clone()),
|
|
generate_memories: source_meta.memory_mode.as_deref() != Some("disabled"),
|
|
};
|
|
let mut params = RolloutRecorderParams::new(
|
|
source_meta.id,
|
|
source_meta.forked_from_id,
|
|
source_meta.parent_thread_id,
|
|
source_meta.source,
|
|
source_meta.thread_source,
|
|
source_meta.originator,
|
|
source_meta.base_instructions.unwrap_or_default(),
|
|
source_meta.dynamic_tools.unwrap_or_default(),
|
|
)
|
|
.with_creator(source_meta.creator_user_id, source_meta.creator_account_id)
|
|
.with_session_id(source_meta.session_id)
|
|
.with_rollout_id(rollout_id)
|
|
.with_selected_capability_roots(source_meta.selected_capability_roots)
|
|
.with_runtime_workspace_roots(source_meta.runtime_workspace_roots)
|
|
.with_multi_agent_version(source_meta.multi_agent_version)
|
|
.with_history_mode(ThreadHistoryMode::Paginated)
|
|
.with_history_base(history_base)
|
|
.with_forked_from_ordinal_exclusive(forked_from_ordinal_exclusive)
|
|
.with_subagent_history_start_ordinal(source_meta.subagent_history_start_ordinal);
|
|
if let Some(context_window) = source_meta.context_window {
|
|
params = params.with_initial_window_id(context_window.window_id);
|
|
}
|
|
RolloutRecorder::new_with_writer_lock(&config, params, writer_lock)
|
|
.await
|
|
.map_err(|err| ThreadStoreError::Internal {
|
|
message: format!("failed to create reverted rollout: {err}"),
|
|
})
|
|
}
|
|
|
|
fn thread_store_io_error(err: std::io::Error) -> ThreadStoreError {
|
|
ThreadStoreError::Internal {
|
|
message: err.to_string(),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "revert_thread_tests.rs"]
|
|
mod tests;
|