mirror of
https://github.com/openai/codex.git
synced 2026-12-05 01:00:54 +00:00
## Why Callers that only need canonical session metadata should be able to read it without loading the full rollout history. ## What changed Add `ThreadStore::read_session_meta`. The default implementation looks for the session header in the latest model context and falls back to history if the header is absent or model context reads are unsupported. The local store reads only the selected rollout's header and skips SQLite read repair, which can parse the full rollout. If archiving or unarchiving moves the file before SQLite updates its path, the lookup can follow the rollout to its new location while respecting `include_archived`. ## Testing Cover paginated metadata reads, the history fallback for a saved child thread, and metadata reads with missing or stale SQLite entries. Verify that a valid header can still be read when the rest of the rollout contains invalid bytes. GitOrigin-RevId: 08790b2db80d35e67f5a101dc38d760987b2d710
266 lines
8.9 KiB
Rust
266 lines
8.9 KiB
Rust
//! Resolves a thread ID to the rollout file the thread currently uses.
|
|
//!
|
|
//! Most threads have one rollout file. `thread/revert` keeps the thread ID stable while switching
|
|
//! the thread to a new rollout file, so callers cannot infer the selected path from the thread ID
|
|
//! alone. This module centralizes live-writer, SQLite, and filesystem fallback resolution.
|
|
//! Session metadata reads skip read repair to avoid parsing the rest of the rollout.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use codex_protocol::ThreadId;
|
|
use codex_protocol::protocol::SessionMeta;
|
|
use codex_protocol::protocol::ThreadHistoryMode;
|
|
use codex_rollout::find_archived_thread_path_by_id_str;
|
|
use codex_rollout::find_thread_path_by_id_str;
|
|
|
|
use super::LocalThreadStore;
|
|
use super::helpers::rollout_path_is_archived;
|
|
use super::live_writer;
|
|
use crate::LoadThreadHistoryParams;
|
|
use crate::ThreadStoreError;
|
|
use crate::ThreadStoreResult;
|
|
|
|
/// One thread resolved to the concrete rollout file it currently uses.
|
|
///
|
|
/// For ordinary threads, `thread_id` and `rollout_id` are the same. After `thread/revert`,
|
|
/// `thread_id` stays stable while `rollout_id` identifies the new immutable rollout file.
|
|
#[derive(Debug)]
|
|
pub(super) struct ResolvedThreadRollout {
|
|
pub(super) thread_id: ThreadId,
|
|
pub(super) rollout_id: ThreadId,
|
|
pub(super) path: PathBuf,
|
|
pub(super) location: RolloutLocation,
|
|
/// Header already read to verify the SQLite path belongs to this thread.
|
|
pub(super) session_meta: Option<SessionMeta>,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub(super) enum RolloutLocation {
|
|
Unarchived,
|
|
Archived,
|
|
}
|
|
|
|
pub(super) async fn resolve_current(
|
|
store: &LocalThreadStore,
|
|
thread_id: ThreadId,
|
|
) -> ThreadStoreResult<Option<ResolvedThreadRollout>> {
|
|
resolve(
|
|
store,
|
|
thread_id,
|
|
LookupScope::ExcludeArchived,
|
|
ReadRepair::Enabled,
|
|
)
|
|
.await
|
|
}
|
|
|
|
pub(super) async fn resolve_current_including_archived(
|
|
store: &LocalThreadStore,
|
|
thread_id: ThreadId,
|
|
) -> ThreadStoreResult<Option<ResolvedThreadRollout>> {
|
|
resolve(
|
|
store,
|
|
thread_id,
|
|
LookupScope::IncludeArchived,
|
|
ReadRepair::Enabled,
|
|
)
|
|
.await
|
|
}
|
|
|
|
pub(super) async fn resolve_for_session_meta(
|
|
store: &LocalThreadStore,
|
|
params: &LoadThreadHistoryParams,
|
|
) -> ThreadStoreResult<Option<ResolvedThreadRollout>> {
|
|
let scope = if params.include_archived {
|
|
LookupScope::IncludeArchived
|
|
} else {
|
|
LookupScope::ExcludeArchived
|
|
};
|
|
resolve(store, params.thread_id, scope, ReadRepair::Disabled).await
|
|
}
|
|
|
|
#[derive(Clone, Copy)]
|
|
enum LookupScope {
|
|
ExcludeArchived,
|
|
IncludeArchived,
|
|
}
|
|
|
|
impl LookupScope {
|
|
fn accepts(self, location: RolloutLocation) -> bool {
|
|
match self {
|
|
Self::ExcludeArchived => location == RolloutLocation::Unarchived,
|
|
Self::IncludeArchived => true,
|
|
}
|
|
}
|
|
}
|
|
|
|
enum ReadRepair {
|
|
Enabled,
|
|
Disabled,
|
|
}
|
|
|
|
/// Resolves a thread's selected rollout in this order:
|
|
///
|
|
/// 1. The live writer, when one exists.
|
|
/// 2. SQLite's selected rollout path.
|
|
/// 3. Filesystem fallback for threads SQLite does not identify as paginated.
|
|
/// 4. Archived filesystem fallback, when requested.
|
|
async fn resolve(
|
|
store: &LocalThreadStore,
|
|
thread_id: ThreadId,
|
|
scope: LookupScope,
|
|
read_repair: ReadRepair,
|
|
) -> ThreadStoreResult<Option<ResolvedThreadRollout>> {
|
|
if let Ok(path) = live_writer::rollout_path(store, thread_id).await
|
|
&& codex_rollout::existing_rollout_path(path.as_path())
|
|
.await
|
|
.is_some()
|
|
&& let Some(resolved) =
|
|
resolve_path_in_scope(store, thread_id, path, scope, /*session_meta*/ None).await?
|
|
{
|
|
return Ok(Some(resolved));
|
|
}
|
|
|
|
let state_db_ctx = store.state_db().await;
|
|
if let Some(state_db_ctx) = state_db_ctx.as_deref() {
|
|
match state_db_ctx.get_thread(thread_id).await {
|
|
Ok(Some(metadata)) => {
|
|
// Once SQLite identifies a thread as paginated, its rollout path is
|
|
// authoritative: after `thread/revert`, a scan could find an older immutable
|
|
// rollout for the same thread. Filesystem fallback remains available when SQLite
|
|
// has no row or identifies the thread as legacy.
|
|
if let Some(path) =
|
|
codex_rollout::existing_rollout_path(metadata.rollout_path.as_path()).await
|
|
{
|
|
let session_meta = codex_rollout::read_session_meta_line(&path)
|
|
.await
|
|
.ok()
|
|
.map(|line| line.meta);
|
|
if session_meta
|
|
.as_ref()
|
|
.is_none_or(|meta| meta.id == thread_id)
|
|
{
|
|
return resolve_path_in_scope(store, thread_id, path, scope, session_meta)
|
|
.await;
|
|
}
|
|
}
|
|
if metadata.history_mode == ThreadHistoryMode::Paginated {
|
|
return Ok(None);
|
|
}
|
|
}
|
|
Ok(None) => {}
|
|
Err(err) => {
|
|
return Err(ThreadStoreError::Internal {
|
|
message: format!("failed to read thread metadata for {thread_id}: {err}"),
|
|
});
|
|
}
|
|
}
|
|
}
|
|
// The fallback can deserialize the full rollout while reseeding a missing SQLite row.
|
|
let fallback_state_db = match read_repair {
|
|
ReadRepair::Enabled => state_db_ctx.as_deref(),
|
|
ReadRepair::Disabled => None,
|
|
};
|
|
if let Some(path) = find_thread_path_by_id_str(
|
|
store.config.codex_home.as_path(),
|
|
&thread_id.to_string(),
|
|
fallback_state_db,
|
|
)
|
|
.await
|
|
.map_err(|err| ThreadStoreError::InvalidRequest {
|
|
message: format!("failed to locate thread id {thread_id}: {err}"),
|
|
})? && let Some(resolved) =
|
|
resolve_path_in_scope(store, thread_id, path, scope, /*session_meta*/ None).await?
|
|
{
|
|
return Ok(Some(resolved));
|
|
}
|
|
if !scope.accepts(RolloutLocation::Archived) {
|
|
return Ok(None);
|
|
}
|
|
let path = find_archived_thread_path_by_id_str(
|
|
store.config.codex_home.as_path(),
|
|
&thread_id.to_string(),
|
|
fallback_state_db,
|
|
)
|
|
.await
|
|
.map_err(|err| ThreadStoreError::InvalidRequest {
|
|
message: format!("failed to locate archived thread id {thread_id}: {err}"),
|
|
})?;
|
|
match path {
|
|
Some(path) => {
|
|
resolve_path_in_scope(store, thread_id, path, scope, /*session_meta*/ None).await
|
|
}
|
|
None => Ok(None),
|
|
}
|
|
}
|
|
|
|
async fn resolve_path_in_scope(
|
|
store: &LocalThreadStore,
|
|
thread_id: ThreadId,
|
|
path: PathBuf,
|
|
scope: LookupScope,
|
|
session_meta: Option<SessionMeta>,
|
|
) -> ThreadStoreResult<Option<ResolvedThreadRollout>> {
|
|
let location = location_for_path(store, path.as_path());
|
|
if !scope.accepts(location) {
|
|
return Ok(None);
|
|
}
|
|
let rollout_id = match codex_rollout::rollout_id_from_path(path.as_path()) {
|
|
Some(rollout_id) => rollout_id,
|
|
None => {
|
|
let history_mode = match &session_meta {
|
|
Some(meta) => meta.history_mode,
|
|
None => {
|
|
codex_rollout::read_session_meta_line(&path)
|
|
.await
|
|
.map_err(|err| ThreadStoreError::Internal {
|
|
message: format!(
|
|
"failed to read session metadata {}: {err}",
|
|
path.display()
|
|
),
|
|
})?
|
|
.meta
|
|
.history_mode
|
|
}
|
|
};
|
|
rollout_id_from_path_or_legacy_thread_id(path.as_path(), thread_id, history_mode)?
|
|
}
|
|
};
|
|
Ok(Some(ResolvedThreadRollout {
|
|
thread_id,
|
|
rollout_id,
|
|
path,
|
|
location,
|
|
session_meta,
|
|
}))
|
|
}
|
|
|
|
fn location_for_path(store: &LocalThreadStore, path: &std::path::Path) -> RolloutLocation {
|
|
if rollout_path_is_archived(store.config.codex_home.as_path(), path) {
|
|
RolloutLocation::Archived
|
|
} else {
|
|
RolloutLocation::Unarchived
|
|
}
|
|
}
|
|
|
|
/// Returns the immutable rollout ID for a path while preserving legacy noncanonical filenames.
|
|
pub(super) fn rollout_id_from_path_or_legacy_thread_id(
|
|
path: &std::path::Path,
|
|
thread_id: ThreadId,
|
|
history_mode: ThreadHistoryMode,
|
|
) -> ThreadStoreResult<ThreadId> {
|
|
Ok(match codex_rollout::rollout_id_from_path(path) {
|
|
Some(rollout_id) => rollout_id,
|
|
None => {
|
|
if history_mode == ThreadHistoryMode::Paginated {
|
|
return Err(ThreadStoreError::InvalidRequest {
|
|
message: format!(
|
|
"paginated rollout path `{}` does not have a canonical rollout filename",
|
|
path.display()
|
|
),
|
|
});
|
|
}
|
|
thread_id
|
|
}
|
|
})
|
|
}
|