Persist names for paginated threads (#34229)

## Why

Paginated threads need explicit user-facing names to remain distinct from
derived titles and previews without requiring rollout metadata writes.

## What changed

- Add a nullable `name` column to persisted thread metadata and use it as the
  canonical name for paginated threads.
- Route paginated name updates through the state database while retaining the
  name index as best-effort compatibility data and preserving legacy thread
  behavior.
- Return persisted names when reading, listing, searching, and resuming
  paginated threads, and include them in state-database search matching.

## Testing

- Cover paginated name updates, reads, list/search results, and metadata-only
  resume behavior, including stale or unavailable compatibility data.

GitOrigin-RevId: 5ac4dfa2a83682b43d59ecc87e0f515580164dca
This commit is contained in:
Brent Traut authored and copyberry committed 2026-07-19 21:13:13 +00:00
1 parent 7844386e3d
commit 5a208c1fc3
13 files changed
+483 -148

No files matched your search

@@ -54,30 +54,55 @@ pub(super) async fn update_thread_metadata(
.await;
}
let needs_rollout_compat = needs_rollout_compatibility_update(&patch);
if needs_rollout_compat {
// These explicit patches still write legacy rollout/name-index state after the
// SQLite update. Paginated threads must fail before either side is mutated.
let thread = read_thread::read_thread(
store,
ReadThreadParams {
thread_id,
include_archived: params.include_archived,
include_history: false,
},
let requires_rollout_compat = requires_rollout_compatibility_update(&patch);
let history_mode = if patch.name.is_some() || requires_rollout_compat {
Some(
read_thread::read_thread(
store,
ReadThreadParams {
thread_id,
include_archived: params.include_archived,
include_history: false,
},
)
.await?
.history_mode,
)
.await?;
reject_paginated_history_mode(thread.history_mode)?;
} else {
None
};
if requires_rollout_compat {
// Explicit patches that require legacy rollout state must fail before either
// persistence layer is mutated.
if let Some(history_mode) = history_mode {
reject_paginated_history_mode(history_mode)?;
}
}
let require_sqlite_write = sqlite_write_failure_should_block(&patch);
let paginated_name =
patch.name.is_some() && matches!(history_mode, Some(ThreadHistoryMode::Paginated));
let needs_rollout_compat = requires_rollout_compat || patch.name.is_some() && !paginated_name;
let require_sqlite_write = sqlite_write_failure_should_block(&patch) || paginated_name;
let updated = apply_metadata_update(
store,
thread_id,
patch.clone(),
params.include_archived,
require_sqlite_write,
history_mode,
)
.await?;
if paginated_name && let Some(name) = patch.name.as_ref() {
if let Err(err) = append_thread_name(
store.config.codex_home.as_path(),
thread_id,
name.as_deref().unwrap_or_default(),
)
.await
{
warn!("failed to index paginated thread name for {thread_id}: {err}");
}
return Ok(updated);
}
if !needs_rollout_compat {
return Ok(updated);
}
@@ -108,7 +133,15 @@ pub(super) async fn update_thread_metadata(
.await;
if let Some(name) = name {
apply_thread_name(store, thread_id, name.unwrap_or_default()).await?;
append_thread_name(
store.config.codex_home.as_path(),
thread_id,
&name.unwrap_or_default(),
)
.await
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to index thread name: {err}"),
})?;
}
let resolved_git_info = match git_info {
@@ -203,6 +236,7 @@ async fn apply_metadata_update(
patch: ThreadMetadataPatch,
include_archived: bool,
require_sqlite_write: bool,
history_mode: Option<ThreadHistoryMode>,
) -> ThreadStoreResult<StoredThread> {
let live_rollout_path = live_writer::rollout_path(store, thread_id).await.ok();
let mut rollout_path = patch.rollout_path.clone().or(live_rollout_path);
@@ -253,9 +287,6 @@ async fn apply_metadata_update(
if let Some(preview) = patch.preview {
metadata.preview = Some(preview);
}
if let Some(name) = patch.name {
metadata.title = name.unwrap_or_default();
}
if let Some(title) = patch.title {
metadata.title = title;
}
@@ -329,6 +360,35 @@ async fn apply_metadata_update(
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to update thread metadata for {thread_id}: {err}"),
})?;
if let Some(name) = patch.name.as_ref() {
let history_mode = history_mode.ok_or_else(|| ThreadStoreError::Internal {
message: format!(
"thread history mode unavailable before name update: {thread_id}"
),
})?;
let updated = match history_mode {
ThreadHistoryMode::Legacy => {
state_db
.update_thread_title(thread_id, name.as_deref().unwrap_or_default())
.await
}
ThreadHistoryMode::Paginated => {
state_db
.update_thread_name(thread_id, name.as_deref())
.await
}
}
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to set thread name: {err}"),
})?;
if !updated {
return Err(ThreadStoreError::Internal {
message: format!(
"thread metadata unavailable before name update: {thread_id}"
),
});
}
}
if existing.is_some()
&& let Some(recency_at) = advance_recency_at
{
@@ -352,22 +412,19 @@ async fn apply_metadata_update(
Ok(())
}
.await
} else if require_sqlite_write {
Err(ThreadStoreError::Internal {
message: format!("sqlite state db unavailable for thread {thread_id}"),
})
} else {
Ok(())
};
match (state_db.is_some(), sqlite_write_result) {
(true, Ok(())) => {}
(true, Err(err)) if require_sqlite_write || !sqlite_write_error_is_best_effort(&err) => {
match sqlite_write_result {
Ok(()) => {}
Err(err) if require_sqlite_write || !sqlite_write_error_is_best_effort(&err) => {
return Err(err);
}
(true, Err(err)) => {
warn!("state db update_thread_metadata failed for {thread_id}: {err}");
}
(false, Ok(())) => {}
(false, Err(err)) if require_sqlite_write || !sqlite_write_error_is_best_effort(&err) => {
return Err(err);
}
(false, Err(err)) => {
Err(err) => {
warn!("state db update_thread_metadata failed for {thread_id}: {err}");
}
}
@@ -455,10 +512,7 @@ async fn canonical_history_mode(
Ok(session_meta.meta.history_mode)
}
fn needs_rollout_compatibility_update(patch: &ThreadMetadataPatch) -> bool {
if patch.name.is_some() {
return true;
}
fn requires_rollout_compatibility_update(patch: &ThreadMetadataPatch) -> bool {
if patch.memory_mode.is_none() && patch.git_info.is_none() {
return false;
}
@@ -597,32 +651,6 @@ async fn apply_thread_git_info_to_rollout(
})
}
async fn apply_thread_name(
store: &LocalThreadStore,
thread_id: ThreadId,
name: String,
) -> ThreadStoreResult<()> {
if let Some(state_db) = store.state_db().await {
let updated = state_db
.update_thread_title(thread_id, &name)
.await
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to set thread name: {err}"),
})?;
if !updated {
return Err(ThreadStoreError::Internal {
message: format!("thread metadata unavailable before name update: {thread_id}"),
});
}
}
append_thread_name(store.config.codex_home.as_path(), thread_id, &name)
.await
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to index thread name: {err}"),
})
}
async fn apply_thread_memory_mode(
rollout_path: &Path,
thread_id: ThreadId,
@@ -767,6 +795,104 @@ mod tests {
assert_eq!(latest_name.as_deref(), Some("A sharper name"));
}
#[tokio::test]
async fn paginated_name_updates_use_sqlite_without_rollout_writes() {
let home = TempDir::new().expect("temp dir");
let config = test_config(home.path());
let uuid = Uuid::from_u128(318);
let thread_id = ThreadId::from_string(&uuid.to_string()).expect("valid thread id");
let path = write_session_file_with_history_mode(
home.path(),
"2025-01-03T14-15-00",
uuid,
ThreadHistoryMode::Paginated,
)
.expect("session file");
let original_rollout = std::fs::read_to_string(&path).expect("read rollout");
let runtime = codex_state::StateRuntime::init(
home.path().to_path_buf(),
config.default_model_provider_id.clone(),
)
.await
.expect("state db should initialize");
let store = LocalThreadStore::new(config, Some(runtime.clone()));
let thread = store
.update_thread_metadata(UpdateThreadMetadataParams {
thread_id,
patch: ThreadMetadataPatch {
name: Some(Some("Canonical paginated name".to_string())),
..Default::default()
},
include_archived: false,
})
.await
.expect("set paginated thread name");
assert_eq!(thread.name.as_deref(), Some("Canonical paginated name"));
let metadata = runtime
.get_thread(thread_id)
.await
.expect("read metadata")
.expect("thread metadata");
assert_eq!(metadata.name.as_deref(), Some("Canonical paginated name"));
assert!(metadata.title.is_empty());
assert_eq!(
codex_rollout::find_thread_name_by_id(home.path(), &thread_id)
.await
.expect("find thread name")
.as_deref(),
Some("Canonical paginated name")
);
let thread = store
.update_thread_metadata(UpdateThreadMetadataParams {
thread_id,
patch: ThreadMetadataPatch {
title: Some("Derived first message".to_string()),
preview: Some("Derived first message".to_string()),
..Default::default()
},
include_archived: false,
})
.await
.expect("apply derived paginated metadata");
assert_eq!(thread.name.as_deref(), Some("Canonical paginated name"));
let session_index_path = home.path().join("session_index.jsonl");
std::fs::remove_file(&session_index_path).expect("remove session index");
std::fs::create_dir(&session_index_path).expect("block session index writes");
let thread = store
.update_thread_metadata(UpdateThreadMetadataParams {
thread_id,
patch: ThreadMetadataPatch {
name: Some(Some("Updated SQLite name".to_string())),
..Default::default()
},
include_archived: false,
})
.await
.expect("set paginated thread name with unavailable index");
assert_eq!(thread.name.as_deref(), Some("Updated SQLite name"));
let err = LocalThreadStore::new(test_config(home.path()), /*state_db*/ None)
.update_thread_metadata(UpdateThreadMetadataParams {
thread_id,
patch: ThreadMetadataPatch {
name: Some(Some("Unpersistable name".to_string())),
..Default::default()
},
include_archived: false,
})
.await
.expect_err("paginated name update without sqlite should fail");
assert!(matches!(err, ThreadStoreError::Internal { .. }));
assert_eq!(
std::fs::read_to_string(&path).expect("read rollout"),
original_rollout
);
}
#[tokio::test]
async fn update_thread_metadata_sets_memory_mode_on_active_rollout() {
let home = TempDir::new().expect("temp dir");
@@ -834,6 +960,7 @@ mod tests {
.update_thread_metadata(UpdateThreadMetadataParams {
thread_id,
patch: ThreadMetadataPatch {
name: Some(Some("Must not persist".to_string())),
memory_mode: Some(ThreadMemoryMode::Disabled),
..Default::default()
},
@@ -858,6 +985,21 @@ mod tests {
.as_deref(),
Some("enabled")
);
assert_eq!(
runtime
.get_thread(thread_id)
.await
.expect("thread metadata should be readable")
.expect("thread metadata")
.name,
None
);
assert_eq!(
codex_rollout::find_thread_name_by_id(home.path(), &thread_id)
.await
.expect("find thread name"),
None
);
}
#[tokio::test]