Add persisted thread pinning to the app server (#34840)

## What changed

- Add `isPinned` to thread responses and allow `thread/metadata/update` to pin or unpin stored threads.
- Add an `isPinned` filter to `thread/list`, including cursor-based pagination and combinations with relationship filters.
- Persist pin state in SQLite without modifying rollout files, default existing threads to unpinned, and preserve pins through reconciliation and archive transitions.

## Testing

- Cover protocol serialization, database migration and indexing, pin updates, filtered pagination, rollout reconciliation, and archive/unarchive behavior.

GitOrigin-RevId: c2eacabd6dae465e2ae6ce8a169e00740960cdb7
This commit is contained in:
joeytrasatti-openai authored and copyberry committed 2026-07-22 22:54:28 +00:00
1 parent d7e8f4c3dc
commit 400ee190c3
71 files changed
+1058 -60

No files matched your search

@@ -264,7 +264,7 @@ async fn apply_metadata_update(
let sqlite_write_result: ThreadStoreResult<()> = if let Some(state_db) = state_db.as_ref() {
let patch = patch.clone();
async {
let existing =
let mut existing =
state_db
.get_thread(thread_id)
.await
@@ -277,6 +277,30 @@ async fn apply_metadata_update(
rollout_path_archived = resolved.archived;
rollout_path = Some(resolved.path);
}
if existing.is_none()
&& patch.is_pinned.is_some()
&& let Some(path) = rollout_path.as_deref()
&& let Some(existing_rollout_path) =
codex_rollout::existing_rollout_path(path).await
{
codex_rollout::state_db::reconcile_rollout(
Some(state_db.as_ref()),
existing_rollout_path.as_path(),
store.config.default_model_provider_id.as_str(),
/*builder*/ None,
&[],
/*archived_only*/ Some(rollout_path_archived),
/*new_thread_memory_mode*/ None,
)
.await;
existing = state_db.get_thread(thread_id).await.map_err(|err| {
ThreadStoreError::Internal {
message: format!(
"failed to read reconciled thread metadata for {thread_id}: {err}"
),
}
})?;
}
let mut metadata = match existing.clone() {
Some(metadata) => metadata,
None => {
@@ -365,6 +389,9 @@ async fn apply_metadata_update(
if let Some(first_user_message) = patch.first_user_message {
metadata.first_user_message = Some(first_user_message);
}
if let Some(is_pinned) = patch.is_pinned {
metadata.is_pinned = is_pinned;
}
if let Some(git_info) = patch.git_info {
let existing_git_info = git_info_from_parts(
metadata.git_sha.clone(),
@@ -382,6 +409,23 @@ async fn apply_metadata_update(
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to update thread metadata for {thread_id}: {err}"),
})?;
if let Some(is_pinned) = patch.is_pinned {
let updated = state_db
.update_thread_pin(thread_id, is_pinned)
.await
.map_err(|err| ThreadStoreError::Internal {
message: format!(
"failed to update pin state for thread {thread_id}: {err}"
),
})?;
if !updated {
return Err(ThreadStoreError::Internal {
message: format!(
"thread metadata unavailable before pin update: {thread_id}"
),
});
}
}
if let Some(name) = patch.name.as_ref() {
let history_mode = history_mode.ok_or_else(|| ThreadStoreError::Internal {
message: format!(
@@ -546,8 +590,9 @@ fn sqlite_write_failure_should_block(patch: &ThreadMetadataPatch) -> bool {
// transcript-derived metadata, thread names, and memory-mode indexing were log-only. Keep that
// failure isolation so a corrupted optional state DB does not make JSONL transcript durability
// look broken. Explicit git-only updates still require SQLite because partial git patches need
// the existing SQLite value to preserve unspecified fields.
patch.git_info.is_some() && !has_observed_metadata_facts(patch)
// the existing SQLite value to preserve unspecified fields. User-selected pin state is
// SQLite-only, so losing its write must also fail the explicit metadata update.
patch.is_pinned.is_some() || (patch.git_info.is_some() && !has_observed_metadata_facts(patch))
}
fn sqlite_write_error_is_best_effort(err: &ThreadStoreError) -> bool {
@@ -845,6 +890,110 @@ mod tests {
assert_eq!(latest_name.as_deref(), Some("A sharper name"));
}
#[tokio::test]
async fn pin_only_metadata_updates_persist_in_sqlite_without_changing_the_rollout() {
let home = TempDir::new().expect("temp dir");
let config = test_config(home.path());
let uuid = Uuid::from_u128(320);
let thread_id = ThreadId::from_string(&uuid.to_string()).expect("valid thread id");
let rollout_path =
write_session_file(home.path(), "2025-01-03T14-20-00", uuid).expect("session file");
let original_rollout = std::fs::read_to_string(&rollout_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 pinned = store
.update_thread_metadata(UpdateThreadMetadataParams {
thread_id,
patch: ThreadMetadataPatch {
is_pinned: Some(true),
..Default::default()
},
include_archived: false,
})
.await
.expect("pin thread");
assert!(pinned.is_pinned);
let pinned_metadata = runtime
.get_thread(thread_id)
.await
.expect("read pinned metadata")
.expect("pinned metadata");
assert!(pinned_metadata.is_pinned);
assert_eq!(pinned_metadata.preview.as_deref(), Some("Hello from user"));
assert_eq!(pinned_metadata.source, "cli");
let pinned_page = store
.list_threads(ListThreadsParams {
page_size: 10,
cursor: None,
sort_key: ThreadSortKey::RecencyAt,
sort_direction: SortDirection::Desc,
allowed_sources: Vec::new(),
model_providers: None,
cwd_filters: None,
is_pinned: Some(true),
archived: false,
search_term: None,
relation_filter: None,
use_state_db_only: true,
})
.await
.expect("list pinned thread");
assert_eq!(
pinned_page
.items
.iter()
.map(|thread| thread.thread_id)
.collect::<Vec<_>>(),
vec![thread_id]
);
let read_by_path = store
.read_thread_by_rollout_path(
rollout_path.clone(),
/*include_archived*/ false,
/*include_history*/ false,
)
.await
.expect("read pinned thread by rollout path");
assert!(read_by_path.is_pinned);
assert_eq!(
std::fs::read_to_string(&rollout_path).expect("read rollout"),
original_rollout
);
let unpinned = store
.update_thread_metadata(UpdateThreadMetadataParams {
thread_id,
patch: ThreadMetadataPatch {
is_pinned: Some(false),
..Default::default()
},
include_archived: false,
})
.await
.expect("unpin thread");
assert!(!unpinned.is_pinned);
assert!(
!runtime
.get_thread(thread_id)
.await
.expect("read unpinned metadata")
.expect("unpinned metadata")
.is_pinned
);
assert_eq!(
std::fs::read_to_string(&rollout_path).expect("read rollout"),
original_rollout
);
}
#[tokio::test]
async fn paginated_name_updates_use_sqlite_without_rollout_writes() {
let home = TempDir::new().expect("temp dir");
@@ -1647,6 +1796,14 @@ mod tests {
}));
}
#[test]
fn sqlite_failures_block_for_explicit_pin_updates() {
assert!(sqlite_write_failure_should_block(&ThreadMetadataPatch {
is_pinned: Some(false),
..Default::default()
}));
}
#[tokio::test]
async fn metadata_patch_applies_title_over_existing_name() {
let home = TempDir::new().expect("temp dir");
@@ -1920,6 +2077,7 @@ mod tests {
allowed_sources: Vec::new(),
model_providers: Some(Vec::new()),
cwd_filters: Some(vec![workspace]),
is_pinned: None,
archived: false,
search_term: None,
relation_filter: None,