mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
Simplify local thread reads and rollout metadata resolution (#65332)
## What changed Use a shared history attachment step for reads by thread ID and rollout path, including the check that rejects full-history requests for paginated threads. Keep session metadata reads on the rollout header path, including when an archive move leaves SQLite pointing at the old location. ## Testing Extend the archive and unarchive test to exercise the public metadata read with a stale SQLite path and invalid UTF-8 after the rollout header in both legacy and paginated history modes. GitOrigin-RevId: 9f0c9008069770a6cc14066c362a2d30c4fa2513
This commit is contained in:
@@ -369,13 +369,16 @@ mod tests {
|
||||
assert!(updated.archived_at.is_some());
|
||||
assert_eq!(updated.recency_at, metadata.recency_at);
|
||||
let contents = std::fs::read_to_string(&archived_path).expect("archived rollout");
|
||||
let (header, history) = contents.split_once('\n').expect("header and history");
|
||||
let (header, _) = contents.split_once('\n').expect("header and history");
|
||||
let mut header: serde_json::Value = serde_json::from_str(header).expect("header");
|
||||
let mut metadata = metadata;
|
||||
for mode in [ThreadHistoryMode::Legacy, ThreadHistoryMode::Paginated] {
|
||||
metadata.history_mode = mode;
|
||||
header["payload"]["history_mode"] = serde_json::to_value(mode).expect("history mode");
|
||||
std::fs::write(&archived_path, format!("{header}\n{history}")).expect("update header");
|
||||
// A moved rollout's header must remain readable even if its history cannot be decoded.
|
||||
let mut contents = serde_json::to_vec(&header).expect("serialize header");
|
||||
contents.extend_from_slice(b"\n\xff");
|
||||
std::fs::write(&archived_path, contents).expect("update rollout");
|
||||
// The file moved, but SQLite still points to its old location.
|
||||
runtime
|
||||
.upsert_thread(&metadata)
|
||||
|
||||
@@ -34,13 +34,51 @@ pub(super) async fn read_session_meta(
|
||||
store: &LocalThreadStore,
|
||||
params: LoadThreadHistoryParams,
|
||||
) -> ThreadStoreResult<SessionMeta> {
|
||||
match session_meta_path(store, ¶ms).await? {
|
||||
(_, Some(meta)) => Ok(meta),
|
||||
(path, None) => read_session_meta_from_path(store, params, &path).await,
|
||||
let (mut path, cached_meta) = resolve_session_meta_source(store, ¶ms).await?;
|
||||
if let Some(meta) = cached_meta {
|
||||
return Ok(meta);
|
||||
}
|
||||
|
||||
let not_found = || ThreadStoreError::ThreadNotFound {
|
||||
thread_id: params.thread_id,
|
||||
};
|
||||
let meta = match read_session_meta_line(&path).await {
|
||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
|
||||
// Archive moves precede SQLite updates. Follow the selected immutable rollout.
|
||||
path = if let Some(rollout_id) = codex_rollout::rollout_id_from_path(&path) {
|
||||
codex_rollout::find_rollout_path_by_rollout_id(
|
||||
store.config.codex_home.as_path(),
|
||||
rollout_id,
|
||||
)
|
||||
.await
|
||||
.map_err(|err| ThreadStoreError::Internal {
|
||||
message: err.to_string(),
|
||||
})?
|
||||
.ok_or_else(not_found)?
|
||||
} else {
|
||||
let (path, cached_meta) = resolve_session_meta_source(store, ¶ms).await?;
|
||||
if let Some(meta) = cached_meta {
|
||||
return Ok(meta);
|
||||
}
|
||||
path
|
||||
};
|
||||
read_required_session_meta_line(&path).await?
|
||||
}
|
||||
result => result.map_err(|err| ThreadStoreError::Internal {
|
||||
message: err.to_string(),
|
||||
})?,
|
||||
}
|
||||
.meta;
|
||||
if meta.id != params.thread_id
|
||||
|| (!params.include_archived
|
||||
&& rollout_path_is_archived(store.config.codex_home.as_path(), &path))
|
||||
{
|
||||
return Err(not_found());
|
||||
}
|
||||
Ok(meta)
|
||||
}
|
||||
|
||||
async fn session_meta_path(
|
||||
async fn resolve_session_meta_source(
|
||||
store: &LocalThreadStore,
|
||||
params: &LoadThreadHistoryParams,
|
||||
) -> ThreadStoreResult<(std::path::PathBuf, Option<SessionMeta>)> {
|
||||
@@ -56,57 +94,20 @@ async fn session_meta_path(
|
||||
Ok((metadata.rollout_path, None))
|
||||
}
|
||||
|
||||
pub(super) async fn read_session_meta_from_path(
|
||||
store: &LocalThreadStore,
|
||||
params: LoadThreadHistoryParams,
|
||||
path: &std::path::Path,
|
||||
) -> ThreadStoreResult<SessionMeta> {
|
||||
let mut path = path.to_path_buf();
|
||||
let meta = match read_session_meta_line(&path).await {
|
||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
|
||||
// Archive moves precede SQLite updates; follow the selected immutable rollout.
|
||||
path = match codex_rollout::rollout_id_from_path(&path) {
|
||||
Some(rollout_id) => codex_rollout::find_rollout_path_by_rollout_id(
|
||||
store.config.codex_home.as_path(),
|
||||
rollout_id,
|
||||
)
|
||||
.await
|
||||
.map_err(|err| ThreadStoreError::Internal {
|
||||
message: err.to_string(),
|
||||
})?
|
||||
.ok_or(ThreadStoreError::ThreadNotFound {
|
||||
thread_id: params.thread_id,
|
||||
})?,
|
||||
None => match session_meta_path(store, ¶ms).await? {
|
||||
(_, Some(meta)) => return Ok(meta),
|
||||
(path, None) => path,
|
||||
},
|
||||
};
|
||||
read_required_session_meta_line(&path).await?
|
||||
}
|
||||
result => result.map_err(|err| ThreadStoreError::Internal {
|
||||
message: err.to_string(),
|
||||
})?,
|
||||
}
|
||||
.meta;
|
||||
if meta.id != params.thread_id
|
||||
|| (!params.include_archived
|
||||
&& rollout_path_is_archived(store.config.codex_home.as_path(), &path))
|
||||
{
|
||||
return Err(ThreadStoreError::ThreadNotFound {
|
||||
thread_id: params.thread_id,
|
||||
});
|
||||
}
|
||||
Ok(meta)
|
||||
}
|
||||
|
||||
pub(super) async fn read_thread(
|
||||
store: &LocalThreadStore,
|
||||
params: ReadThreadParams,
|
||||
) -> ThreadStoreResult<StoredThread> {
|
||||
let thread_id = params.thread_id;
|
||||
let ReadThreadParams {
|
||||
thread_id,
|
||||
include_archived,
|
||||
include_history,
|
||||
} = params;
|
||||
let missing_rollout = || ThreadStoreError::InvalidRequest {
|
||||
message: format!("no rollout found for thread id {thread_id}"),
|
||||
};
|
||||
let sqlite_metadata = read_sqlite_metadata(store, thread_id).await;
|
||||
if !params.include_history
|
||||
if !include_history
|
||||
&& let Some((path, meta)) =
|
||||
super::live_writer::unpersisted_session_meta(store, thread_id).await
|
||||
&& sqlite_metadata
|
||||
@@ -119,91 +120,79 @@ pub(super) async fn read_thread(
|
||||
Some(metadata) => Ok(stored_thread_from_metadata(store, metadata, Some(meta)).await),
|
||||
// Preserve the saved-thread contract used by fork/resume. A live but untouched
|
||||
// thread has no stored record yet; its absence is known without searching history.
|
||||
None => Err(ThreadStoreError::InvalidRequest {
|
||||
message: format!("no rollout found for thread id {thread_id}"),
|
||||
}),
|
||||
None => Err(missing_rollout()),
|
||||
};
|
||||
}
|
||||
let persisted_model_settings = sqlite_metadata
|
||||
.as_ref()
|
||||
.map(|metadata| (metadata.model.clone(), metadata.reasoning_effort.clone()));
|
||||
let daybreak_enabled = sqlite_metadata
|
||||
.as_ref()
|
||||
.and_then(|metadata| metadata.daybreak_enabled);
|
||||
if let Some(metadata) = sqlite_metadata
|
||||
&& (params.include_archived
|
||||
|| (metadata.archived_at.is_none()
|
||||
&& !rollout_path_is_archived(
|
||||
store.config.codex_home.as_path(),
|
||||
metadata.rollout_path.as_path(),
|
||||
)))
|
||||
&& (!params.include_history
|
||||
|| sqlite_rollout_path_can_load_history_for_thread(&metadata.rollout_path, thread_id)
|
||||
.await)
|
||||
{
|
||||
let metadata_sandbox_policy = metadata.sandbox_policy.clone();
|
||||
let mut thread = stored_thread_from_sqlite_metadata(store, metadata).await?;
|
||||
// Paginated history may contain only a suffix, so its display metadata lives in SQLite.
|
||||
// Legacy display metadata remains rollout-derived.
|
||||
if thread.history_mode == ThreadHistoryMode::Legacy
|
||||
&& !params.include_history
|
||||
&& let Some(rollout_path) = thread.rollout_path.clone()
|
||||
&& let Ok(mut rollout_thread) = read_thread_from_rollout_path(store, rollout_path).await
|
||||
&& rollout_thread.thread_id == thread_id
|
||||
&& (params.include_archived || rollout_thread.archived_at.is_none())
|
||||
&& !rollout_thread.preview.is_empty()
|
||||
|
||||
let mut thread = match sqlite_metadata {
|
||||
Some(metadata)
|
||||
if (include_archived
|
||||
|| (metadata.archived_at.is_none()
|
||||
&& !rollout_path_is_archived(
|
||||
store.config.codex_home.as_path(),
|
||||
&metadata.rollout_path,
|
||||
)))
|
||||
&& (!include_history
|
||||
|| sqlite_rollout_path_can_load_history_for_thread(
|
||||
&metadata.rollout_path,
|
||||
thread_id,
|
||||
)
|
||||
.await) =>
|
||||
{
|
||||
rollout_thread.recency_at = thread.recency_at;
|
||||
rollout_thread.section = thread.section;
|
||||
rollout_thread.section_position = thread.section_position;
|
||||
rollout_thread.section_entered_at = thread.section_entered_at;
|
||||
if !thread.cwd.as_os_str().is_empty() {
|
||||
rollout_thread.cwd = thread.cwd;
|
||||
let metadata_sandbox_policy = metadata.sandbox_policy.clone();
|
||||
let thread = stored_thread_from_sqlite_metadata(store, metadata).await?;
|
||||
// Paginated history may contain only a suffix, so its display metadata lives in SQLite.
|
||||
// Legacy display metadata remains rollout-derived.
|
||||
if thread.history_mode == ThreadHistoryMode::Legacy
|
||||
&& !include_history
|
||||
&& let Some(rollout_path) = thread.rollout_path.as_deref()
|
||||
&& let Ok(mut rollout_thread) =
|
||||
read_thread_from_rollout_path(store, rollout_path).await
|
||||
&& rollout_thread.thread_id == thread_id
|
||||
&& (include_archived || rollout_thread.archived_at.is_none())
|
||||
&& !rollout_thread.preview.is_empty()
|
||||
{
|
||||
rollout_thread.recency_at = thread.recency_at;
|
||||
rollout_thread.section = thread.section;
|
||||
rollout_thread.section_position = thread.section_position;
|
||||
rollout_thread.section_entered_at = thread.section_entered_at;
|
||||
if !thread.cwd.as_os_str().is_empty() {
|
||||
rollout_thread.cwd = thread.cwd;
|
||||
}
|
||||
rollout_thread.name = thread.name.or(rollout_thread.name);
|
||||
rollout_thread.project_id = thread.project_id;
|
||||
rollout_thread.daybreak_enabled = thread.daybreak_enabled;
|
||||
rollout_thread.model = thread.model;
|
||||
rollout_thread.reasoning_effort = thread.reasoning_effort;
|
||||
rollout_thread.git_info = thread.git_info;
|
||||
rollout_thread.permission_profile = permission_profile_from_metadata_value(
|
||||
&metadata_sandbox_policy,
|
||||
rollout_thread.cwd.as_path(),
|
||||
);
|
||||
rollout_thread
|
||||
} else {
|
||||
thread
|
||||
}
|
||||
if thread.name.is_some() {
|
||||
rollout_thread.name = thread.name;
|
||||
}
|
||||
rollout_thread.project_id = thread.project_id;
|
||||
rollout_thread.daybreak_enabled = thread.daybreak_enabled;
|
||||
rollout_thread.model = thread.model;
|
||||
rollout_thread.reasoning_effort = thread.reasoning_effort;
|
||||
rollout_thread.git_info = thread.git_info;
|
||||
rollout_thread.permission_profile = permission_profile_from_metadata_value(
|
||||
&metadata_sandbox_policy,
|
||||
rollout_thread.cwd.as_path(),
|
||||
);
|
||||
thread = rollout_thread;
|
||||
}
|
||||
reject_paginated_history(&thread, params.include_history)?;
|
||||
attach_history_if_requested(&mut thread, params.include_history).await?;
|
||||
return Ok(thread);
|
||||
}
|
||||
metadata => {
|
||||
let resolved = if include_archived {
|
||||
thread_rollout_resolver::resolve_current_including_archived(store, thread_id)
|
||||
.await?
|
||||
} else {
|
||||
thread_rollout_resolver::resolve_current(store, thread_id).await?
|
||||
}
|
||||
.ok_or_else(missing_rollout)?;
|
||||
|
||||
let resolved = if params.include_archived {
|
||||
thread_rollout_resolver::resolve_current_including_archived(store, thread_id).await?
|
||||
} else {
|
||||
thread_rollout_resolver::resolve_current(store, thread_id).await?
|
||||
let mut thread = read_thread_from_rollout_path(store, &resolved.path).await?;
|
||||
if let Some(metadata) = metadata {
|
||||
thread.daybreak_enabled = metadata.daybreak_enabled;
|
||||
thread.model = metadata.model;
|
||||
thread.reasoning_effort = metadata.reasoning_effort;
|
||||
}
|
||||
thread
|
||||
}
|
||||
};
|
||||
let path =
|
||||
resolved
|
||||
.map(|resolved| resolved.path)
|
||||
.ok_or_else(|| ThreadStoreError::InvalidRequest {
|
||||
message: format!("no rollout found for thread id {thread_id}"),
|
||||
})?;
|
||||
|
||||
let mut thread = read_thread_from_rollout_path(store, path).await?;
|
||||
thread.daybreak_enabled = daybreak_enabled;
|
||||
if let Some((model, reasoning_effort)) = persisted_model_settings {
|
||||
thread.model = model;
|
||||
thread.reasoning_effort = reasoning_effort;
|
||||
}
|
||||
if !params.include_archived && thread.archived_at.is_some() {
|
||||
return Err(ThreadStoreError::InvalidRequest {
|
||||
message: format!("thread {} is archived", thread.thread_id),
|
||||
});
|
||||
}
|
||||
reject_paginated_history(&thread, params.include_history)?;
|
||||
attach_history_if_requested(&mut thread, params.include_history).await?;
|
||||
attach_history_if_requested(&mut thread, include_history).await?;
|
||||
Ok(thread)
|
||||
}
|
||||
|
||||
@@ -229,7 +218,7 @@ pub(super) async fn read_thread_by_rollout_path(
|
||||
include_history: bool,
|
||||
) -> ThreadStoreResult<StoredThread> {
|
||||
let path = resolve_requested_rollout_path(store, rollout_path).await?;
|
||||
let mut thread = read_thread_from_rollout_path(store, path.clone()).await?;
|
||||
let mut thread = read_thread_from_rollout_path(store, &path).await?;
|
||||
if !include_archived && thread.archived_at.is_some() {
|
||||
return Err(ThreadStoreError::InvalidRequest {
|
||||
message: format!("thread {} is archived", thread.thread_id),
|
||||
@@ -251,7 +240,7 @@ pub(super) async fn read_thread_by_rollout_path(
|
||||
thread.model = metadata.model;
|
||||
thread.reasoning_effort = metadata.reasoning_effort;
|
||||
if !metadata.cwd.as_os_str().is_empty()
|
||||
&& resolve_requested_rollout_path(store, metadata.rollout_path.clone())
|
||||
&& resolve_requested_rollout_path(store, metadata.rollout_path)
|
||||
.await
|
||||
.is_ok_and(|metadata_rollout_path| metadata_rollout_path == path)
|
||||
{
|
||||
@@ -277,18 +266,10 @@ pub(super) async fn read_thread_by_rollout_path(
|
||||
);
|
||||
}
|
||||
}
|
||||
reject_paginated_history(&thread, include_history)?;
|
||||
attach_history_if_requested(&mut thread, include_history).await?;
|
||||
Ok(thread)
|
||||
}
|
||||
|
||||
fn reject_paginated_history(thread: &StoredThread, include_history: bool) -> ThreadStoreResult<()> {
|
||||
if include_history {
|
||||
reject_paginated_history_mode(thread.history_mode)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) async fn resolve_requested_rollout_path(
|
||||
store: &LocalThreadStore,
|
||||
rollout_path: std::path::PathBuf,
|
||||
@@ -298,57 +279,48 @@ pub(super) async fn resolve_requested_rollout_path(
|
||||
} else {
|
||||
rollout_path
|
||||
};
|
||||
match tokio::fs::metadata(path.as_path()).await {
|
||||
Ok(metadata) if metadata.is_dir() => {
|
||||
return Err(ThreadStoreError::InvalidRequest {
|
||||
message: format!(
|
||||
"failed to resolve rollout path `{}`: path is a directory",
|
||||
path.display()
|
||||
),
|
||||
});
|
||||
}
|
||||
Ok(metadata) if !metadata.is_file() => {
|
||||
return Err(ThreadStoreError::InvalidRequest {
|
||||
message: format!(
|
||||
"failed to resolve rollout path `{}`: path is not a file",
|
||||
path.display()
|
||||
),
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
if let Ok(metadata) = tokio::fs::metadata(&path).await
|
||||
&& !metadata.is_file()
|
||||
{
|
||||
let reason = if metadata.is_dir() {
|
||||
"path is a directory"
|
||||
} else {
|
||||
"path is not a file"
|
||||
};
|
||||
return Err(ThreadStoreError::InvalidRequest {
|
||||
message: format!(
|
||||
"failed to resolve rollout path `{}`: {reason}",
|
||||
path.display()
|
||||
),
|
||||
});
|
||||
}
|
||||
// Compression can replace the physical file after lookup, before canonicalization.
|
||||
// Resolve again from the requested path so either representation remains usable.
|
||||
let mut retries_remaining = 3;
|
||||
const MAX_ROLLOUT_RESOLUTION_RETRIES: usize = 3;
|
||||
let mut retries_remaining = MAX_ROLLOUT_RESOLUTION_RETRIES;
|
||||
loop {
|
||||
let resolved_path = match codex_rollout::existing_rollout_path(path.as_path()).await {
|
||||
Some(path) => path,
|
||||
None if retries_remaining > 0 => {
|
||||
retries_remaining -= 1;
|
||||
continue;
|
||||
}
|
||||
None => {
|
||||
return Err(ThreadStoreError::InvalidRequest {
|
||||
message: format!(
|
||||
"failed to resolve rollout path `{}`: file does not exist",
|
||||
path.display()
|
||||
),
|
||||
});
|
||||
}
|
||||
};
|
||||
match std::fs::canonicalize(&resolved_path) {
|
||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound && retries_remaining > 0 => {
|
||||
retries_remaining -= 1;
|
||||
}
|
||||
result => {
|
||||
return result.map_err(|err| ThreadStoreError::InvalidRequest {
|
||||
message: format!(
|
||||
"failed to resolve rollout path `{}`: {err}",
|
||||
resolved_path.display()
|
||||
),
|
||||
});
|
||||
if let Some(resolved_path) = codex_rollout::existing_rollout_path(&path).await {
|
||||
match std::fs::canonicalize(&resolved_path) {
|
||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound && retries_remaining > 0 => {
|
||||
}
|
||||
result => {
|
||||
return result.map_err(|err| ThreadStoreError::InvalidRequest {
|
||||
message: format!(
|
||||
"failed to resolve rollout path `{}`: {err}",
|
||||
resolved_path.display()
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
} else if retries_remaining == 0 {
|
||||
return Err(ThreadStoreError::InvalidRequest {
|
||||
message: format!(
|
||||
"failed to resolve rollout path `{}`: file does not exist",
|
||||
path.display()
|
||||
),
|
||||
});
|
||||
}
|
||||
retries_remaining -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -359,15 +331,17 @@ async fn attach_history_if_requested(
|
||||
if !include_history {
|
||||
return Ok(());
|
||||
}
|
||||
reject_paginated_history_mode(thread.history_mode)?;
|
||||
let thread_id = thread.thread_id;
|
||||
let Some(path) = thread.rollout_path.clone() else {
|
||||
return Err(ThreadStoreError::Internal {
|
||||
let path = thread
|
||||
.rollout_path
|
||||
.as_deref()
|
||||
.ok_or_else(|| ThreadStoreError::Internal {
|
||||
message: format!("failed to load thread history for thread {thread_id}"),
|
||||
});
|
||||
};
|
||||
let before = super::history_revision::read(&path).await;
|
||||
let items = load_history_items(&path).await?;
|
||||
let after = super::history_revision::read(&path).await;
|
||||
})?;
|
||||
let before = super::history_revision::read(path).await;
|
||||
let items = load_history_items(path).await?;
|
||||
let after = super::history_revision::read(path).await;
|
||||
thread.history = Some(StoredThreadHistory {
|
||||
revision: before.filter(|revision| Some(revision) == after.as_ref()),
|
||||
thread_id,
|
||||
@@ -378,12 +352,13 @@ async fn attach_history_if_requested(
|
||||
|
||||
async fn read_thread_from_rollout_path(
|
||||
store: &LocalThreadStore,
|
||||
path: std::path::PathBuf,
|
||||
path: &std::path::Path,
|
||||
) -> ThreadStoreResult<StoredThread> {
|
||||
let Some(item) = read_thread_item_from_rollout(path.clone()).await else {
|
||||
return stored_thread_from_session_meta(store, path).await;
|
||||
let archived = rollout_path_is_archived(store.config.codex_home.as_path(), path);
|
||||
let Some(item) = read_thread_item_from_rollout(path.to_path_buf()).await else {
|
||||
let header = read_required_session_meta_line(path).await?;
|
||||
return Ok(stored_thread_from_meta_line(store, header, path, archived));
|
||||
};
|
||||
let archived = rollout_path_is_archived(store.config.codex_home.as_path(), path.as_path());
|
||||
let mut thread = stored_thread_from_rollout_item(
|
||||
item,
|
||||
archived,
|
||||
@@ -392,18 +367,13 @@ async fn read_thread_from_rollout_path(
|
||||
.ok_or_else(|| ThreadStoreError::Internal {
|
||||
message: format!("failed to read thread id from {}", path.display()),
|
||||
})?;
|
||||
thread.rollout_path = Some(codex_rollout::plain_rollout_path(path.as_path()));
|
||||
let meta_line = read_required_session_meta_line(path.as_path()).await?;
|
||||
thread.creator_user_id = meta_line.meta.creator_user_id;
|
||||
thread.creator_account_id = meta_line.meta.creator_account_id;
|
||||
thread.forked_from_id = meta_line.meta.forked_from_id;
|
||||
thread.parent_thread_id = meta_line.meta.parent_thread_id;
|
||||
thread.history_mode = meta_line.meta.history_mode;
|
||||
if let Some(model_provider) = meta_line
|
||||
.meta
|
||||
.model_provider
|
||||
.filter(|provider| !provider.is_empty())
|
||||
{
|
||||
let meta = read_required_session_meta_line(path).await?.meta;
|
||||
thread.creator_user_id = meta.creator_user_id;
|
||||
thread.creator_account_id = meta.creator_account_id;
|
||||
thread.forked_from_id = meta.forked_from_id;
|
||||
thread.parent_thread_id = meta.parent_thread_id;
|
||||
thread.history_mode = meta.history_mode;
|
||||
if let Some(model_provider) = meta.model_provider.filter(|provider| !provider.is_empty()) {
|
||||
thread.model_provider = model_provider;
|
||||
}
|
||||
if thread.history_mode == ThreadHistoryMode::Legacy
|
||||
@@ -435,7 +405,7 @@ async fn read_sqlite_metadata(
|
||||
runtime.get_thread(thread_id).await.ok().flatten()
|
||||
}
|
||||
|
||||
pub(super) async fn stored_thread_from_sqlite_metadata(
|
||||
async fn stored_thread_from_sqlite_metadata(
|
||||
store: &LocalThreadStore,
|
||||
metadata: ThreadMetadata,
|
||||
) -> ThreadStoreResult<StoredThread> {
|
||||
@@ -476,7 +446,6 @@ async fn stored_thread_from_metadata(
|
||||
metadata: ThreadMetadata,
|
||||
session_meta: Option<SessionMeta>,
|
||||
) -> StoredThread {
|
||||
let forked_from_id = session_meta.as_ref().and_then(|meta| meta.forked_from_id);
|
||||
let parent_thread_id = session_meta.as_ref().and_then(|meta| meta.parent_thread_id);
|
||||
let history_mode = session_meta
|
||||
.as_ref()
|
||||
@@ -484,20 +453,14 @@ async fn stored_thread_from_metadata(
|
||||
.unwrap_or(metadata.history_mode);
|
||||
let name = thread_name_from_metadata(store, &metadata, history_mode).await;
|
||||
let mut thread = stored_thread_from_state_metadata(store, metadata, parent_thread_id);
|
||||
if let Some(meta) = &session_meta {
|
||||
thread.creator_user_id = thread
|
||||
.creator_user_id
|
||||
.or_else(|| meta.creator_user_id.clone());
|
||||
thread.creator_account_id = thread
|
||||
.creator_account_id
|
||||
.or_else(|| meta.creator_account_id.clone());
|
||||
if let Some(meta) = session_meta {
|
||||
thread.forked_from_id = meta.forked_from_id;
|
||||
thread.creator_user_id = thread.creator_user_id.or(meta.creator_user_id);
|
||||
thread.creator_account_id = thread.creator_account_id.or(meta.creator_account_id);
|
||||
thread.originator = thread
|
||||
.originator
|
||||
.or_else(|| (!meta.originator.is_empty()).then_some(meta.originator));
|
||||
}
|
||||
thread.originator = thread.originator.or_else(|| {
|
||||
session_meta
|
||||
.map(|meta| meta.originator)
|
||||
.filter(|originator| !originator.is_empty())
|
||||
});
|
||||
thread.forked_from_id = forked_from_id;
|
||||
thread.history_mode = history_mode;
|
||||
thread.name = name;
|
||||
thread
|
||||
@@ -549,7 +512,7 @@ pub(super) fn stored_thread_from_state_metadata(
|
||||
cwd: metadata.cwd,
|
||||
cli_version: metadata.cli_version,
|
||||
originator: metadata.originator,
|
||||
source: parse_session_source(&metadata.source),
|
||||
source: parse_or_default(&metadata.source, SessionSource::Unknown),
|
||||
history_mode: metadata.history_mode,
|
||||
thread_source: metadata.thread_source,
|
||||
agent_nickname: metadata.agent_nickname,
|
||||
@@ -591,17 +554,6 @@ async fn thread_name_from_metadata(
|
||||
}
|
||||
}
|
||||
|
||||
async fn stored_thread_from_session_meta(
|
||||
store: &LocalThreadStore,
|
||||
path: std::path::PathBuf,
|
||||
) -> ThreadStoreResult<StoredThread> {
|
||||
let meta_line = read_required_session_meta_line(path.as_path()).await?;
|
||||
let archived = rollout_path_is_archived(store.config.codex_home.as_path(), path.as_path());
|
||||
Ok(stored_thread_from_meta_line(
|
||||
store, meta_line, path, archived,
|
||||
))
|
||||
}
|
||||
|
||||
async fn read_required_session_meta_line(
|
||||
path: &std::path::Path,
|
||||
) -> ThreadStoreResult<SessionMetaLine> {
|
||||
@@ -615,32 +567,31 @@ async fn read_required_session_meta_line(
|
||||
fn stored_thread_from_meta_line(
|
||||
store: &LocalThreadStore,
|
||||
meta_line: SessionMetaLine,
|
||||
path: std::path::PathBuf,
|
||||
path: &std::path::Path,
|
||||
archived: bool,
|
||||
) -> StoredThread {
|
||||
let created_at = parse_rfc3339_non_optional(&meta_line.meta.timestamp).unwrap_or_else(Utc::now);
|
||||
let updated_at = std::fs::metadata(path.as_path())
|
||||
let SessionMetaLine { meta, git } = meta_line;
|
||||
let created_at = DateTime::parse_from_rfc3339(&meta.timestamp)
|
||||
.map(|time| time.with_timezone(&Utc))
|
||||
.unwrap_or_else(|_| Utc::now());
|
||||
let updated_at = std::fs::metadata(path)
|
||||
.ok()
|
||||
.and_then(|meta| meta.modified().ok())
|
||||
.map(DateTime::<Utc>::from)
|
||||
.unwrap_or(created_at);
|
||||
let rollout_path = codex_rollout::plain_rollout_path(path.as_path());
|
||||
let rollout_path = codex_rollout::plain_rollout_path(path);
|
||||
StoredThread {
|
||||
creator_user_id: meta_line.meta.creator_user_id,
|
||||
creator_account_id: meta_line.meta.creator_account_id,
|
||||
thread_id: meta_line.meta.id,
|
||||
creator_user_id: meta.creator_user_id,
|
||||
creator_account_id: meta.creator_account_id,
|
||||
thread_id: meta.id,
|
||||
extra_config: None,
|
||||
rollout_path: Some(rollout_path),
|
||||
attached_to_thread_id: meta_line
|
||||
.meta
|
||||
.forked_from_id
|
||||
.filter(|_| meta_line.meta.attached_thread),
|
||||
forked_from_id: meta_line.meta.forked_from_id,
|
||||
parent_thread_id: meta_line.meta.parent_thread_id,
|
||||
attached_to_thread_id: meta.forked_from_id.filter(|_| meta.attached_thread),
|
||||
forked_from_id: meta.forked_from_id,
|
||||
parent_thread_id: meta.parent_thread_id,
|
||||
preview: String::new(),
|
||||
name: None,
|
||||
model_provider: meta_line
|
||||
.meta
|
||||
model_provider: meta
|
||||
.model_provider
|
||||
.filter(|provider| !provider.is_empty())
|
||||
.unwrap_or_else(|| store.config.default_model_provider_id.clone()),
|
||||
@@ -655,16 +606,16 @@ fn stored_thread_from_meta_line(
|
||||
section_entered_at: None,
|
||||
project_id: None,
|
||||
daybreak_enabled: None,
|
||||
cwd: meta_line.meta.cwd,
|
||||
cli_version: meta_line.meta.cli_version,
|
||||
originator: (!meta_line.meta.originator.is_empty()).then_some(meta_line.meta.originator),
|
||||
source: meta_line.meta.source,
|
||||
history_mode: meta_line.meta.history_mode,
|
||||
thread_source: meta_line.meta.thread_source,
|
||||
agent_nickname: meta_line.meta.agent_nickname,
|
||||
agent_role: meta_line.meta.agent_role,
|
||||
agent_path: meta_line.meta.agent_path,
|
||||
git_info: meta_line.git,
|
||||
cwd: meta.cwd,
|
||||
cli_version: meta.cli_version,
|
||||
originator: (!meta.originator.is_empty()).then_some(meta.originator),
|
||||
source: meta.source,
|
||||
history_mode: meta.history_mode,
|
||||
thread_source: meta.thread_source,
|
||||
agent_nickname: meta.agent_nickname,
|
||||
agent_role: meta.agent_role,
|
||||
agent_path: meta.agent_path,
|
||||
git_info: git,
|
||||
approval_mode: AskForApproval::OnRequest,
|
||||
permission_profile: PermissionProfile::read_only(),
|
||||
token_usage: None,
|
||||
@@ -673,12 +624,6 @@ fn stored_thread_from_meta_line(
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_session_source(source: &str) -> SessionSource {
|
||||
serde_json::from_str(source)
|
||||
.or_else(|_| serde_json::from_value(serde_json::Value::String(source.to_string())))
|
||||
.unwrap_or(SessionSource::Unknown)
|
||||
}
|
||||
|
||||
fn parse_or_default<T>(value: &str, default: T) -> T
|
||||
where
|
||||
T: serde::de::DeserializeOwned,
|
||||
@@ -688,12 +633,6 @@ where
|
||||
.unwrap_or(default)
|
||||
}
|
||||
|
||||
fn parse_rfc3339_non_optional(value: &str) -> Option<DateTime<Utc>> {
|
||||
DateTime::parse_from_rfc3339(value)
|
||||
.ok()
|
||||
.map(|dt| dt.with_timezone(&Utc))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Write;
|
||||
@@ -1597,7 +1536,9 @@ mod tests {
|
||||
assert_eq!(thread.model_provider, "rollout-provider");
|
||||
assert_eq!(
|
||||
thread.created_at,
|
||||
parse_rfc3339_non_optional("2025-01-03T12:00:00Z").unwrap()
|
||||
DateTime::parse_from_rfc3339("2025-01-03T12:00:00Z")
|
||||
.expect("test rollout timestamp")
|
||||
.with_timezone(&Utc)
|
||||
);
|
||||
assert!(thread.updated_at >= thread.created_at);
|
||||
assert_eq!(thread.archived_at, None);
|
||||
|
||||
@@ -62,7 +62,6 @@ async fn empty_archived_reads_without_sqlite_preserve_source_and_file_time()
|
||||
include_history: false,
|
||||
})
|
||||
.await?;
|
||||
let stale_path = path.clone();
|
||||
let by_path = store
|
||||
.read_thread_by_rollout_path(
|
||||
path, /*include_archived*/ true, /*include_history*/ false,
|
||||
@@ -91,7 +90,7 @@ async fn empty_archived_reads_without_sqlite_preserve_source_and_file_time()
|
||||
);
|
||||
}
|
||||
let other = LocalThreadStore::new(test_config(home.path()), /*state_db*/ None);
|
||||
let moved = other
|
||||
other
|
||||
.unarchive_thread(ArchiveThreadParams { thread_id })
|
||||
.await?;
|
||||
let config = test_config(home.path());
|
||||
@@ -106,15 +105,9 @@ async fn empty_archived_reads_without_sqlite_preserve_source_and_file_time()
|
||||
include_archived: true,
|
||||
};
|
||||
assert!(state_db.get_thread(thread_id).await?.is_none());
|
||||
let meta = indexed_store.read_session_meta(params.clone()).await?;
|
||||
let meta = indexed_store.read_session_meta(params).await?;
|
||||
let expected = to_value(&line.item)?;
|
||||
assert_eq!(to_value(meta)?, expected["payload"]);
|
||||
assert!(state_db.get_thread(thread_id).await?.is_none());
|
||||
fs::write(
|
||||
moved.rollout_path.expect("unarchived rollout"),
|
||||
[serde_json::to_vec(&line)?.as_slice(), b"\n\xff"].concat(),
|
||||
)?;
|
||||
let meta = super::read_thread::read_session_meta_from_path(&store, params, &stale_path).await?;
|
||||
assert_eq!(to_value(meta)?, expected["payload"]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user