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

+3
View File
@@ -195,6 +195,7 @@ SELECT
threads.tokens_used,
threads.first_user_message,
threads.archived_at,
threads.is_pinned,
threads.git_sha,
threads.git_branch,
threads.git_origin_url
@@ -208,6 +209,7 @@ FROM threads
allowed_sources,
model_providers: None,
cwd_filters: None,
is_pinned: None,
anchor: None,
sort_key: SortKey::UpdatedAt,
sort_direction: SortDirection::Desc,
@@ -569,6 +571,7 @@ SELECT
threads.tokens_used,
threads.first_user_message,
threads.archived_at,
threads.is_pinned,
threads.git_sha,
threads.git_branch,
threads.git_origin_url
@@ -69,6 +69,7 @@ pub(super) fn test_thread_metadata(
tokens_used: 0,
first_user_message: Some("hello".to_string()),
archived_at: None,
is_pinned: false,
git_sha: None,
git_branch: None,
git_origin_url: None,
+208 -2
View File
@@ -33,6 +33,7 @@ SELECT
threads.tokens_used,
threads.first_user_message,
threads.archived_at,
threads.is_pinned,
threads.git_sha,
threads.git_branch,
threads.git_origin_url
@@ -372,6 +373,7 @@ ON CONFLICT(child_thread_id) DO NOTHING
allowed_sources,
model_providers,
cwd_filters: None,
is_pinned: None,
anchor: None,
sort_key: crate::SortKey::UpdatedAt,
sort_direction: SortDirection::Desc,
@@ -496,6 +498,7 @@ ON CONFLICT(child_thread_id) DO NOTHING
allowed_sources,
model_providers,
cwd_filters: None,
is_pinned: None,
anchor,
sort_key,
sort_direction: SortDirection::Desc,
@@ -565,11 +568,12 @@ INSERT INTO threads (
first_user_message,
archived,
archived_at,
is_pinned,
git_sha,
git_branch,
git_origin_url,
memory_mode
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO NOTHING
"#,
)
@@ -611,6 +615,7 @@ ON CONFLICT(id) DO NOTHING
.bind(metadata.first_user_message.as_deref().unwrap_or_default())
.bind(metadata.archived_at.is_some())
.bind(metadata.archived_at.map(datetime_to_epoch_seconds))
.bind(metadata.is_pinned)
.bind(metadata.git_sha.as_deref())
.bind(metadata.git_branch.as_deref())
.bind(metadata.git_origin_url.as_deref())
@@ -661,6 +666,20 @@ ON CONFLICT(id) DO NOTHING
Ok(result.rows_affected() > 0)
}
/// Update the SQLite-owned pinned state without changing other thread metadata.
pub async fn update_thread_pin(
&self,
thread_id: ThreadId,
is_pinned: bool,
) -> anyhow::Result<bool> {
let result = sqlx::query("UPDATE threads SET is_pinned = ? WHERE id = ?")
.bind(is_pinned)
.bind(thread_id.to_string())
.execute(self.pool.as_ref())
.await?;
Ok(result.rows_affected() > 0)
}
pub async fn touch_thread_updated_at(
&self,
thread_id: ThreadId,
@@ -834,11 +853,12 @@ INSERT INTO threads (
first_user_message,
archived,
archived_at,
is_pinned,
git_sha,
git_branch,
git_origin_url,
memory_mode
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
rollout_path = excluded.rollout_path,
created_at = excluded.created_at,
@@ -909,6 +929,7 @@ ON CONFLICT(id) DO UPDATE SET
.bind(metadata.first_user_message.as_deref().unwrap_or_default())
.bind(metadata.archived_at.is_some())
.bind(metadata.archived_at.map(datetime_to_epoch_seconds))
.bind(metadata.is_pinned)
.bind(metadata.git_sha.as_deref())
.bind(metadata.git_branch.as_deref())
.bind(metadata.git_origin_url.as_deref())
@@ -1190,6 +1211,7 @@ SELECT
threads.tokens_used,
threads.first_user_message,
threads.archived_at,
threads.is_pinned,
threads.git_sha,
threads.git_branch,
threads.git_origin_url
@@ -1222,6 +1244,7 @@ pub struct ThreadFilterOptions<'a> {
pub allowed_sources: &'a [String],
pub model_providers: Option<&'a [String]>,
pub cwd_filters: Option<&'a [PathBuf]>,
pub is_pinned: Option<bool>,
pub anchor: Option<&'a crate::Anchor>,
pub sort_key: SortKey,
pub sort_direction: SortDirection,
@@ -1238,6 +1261,7 @@ pub(super) fn push_thread_filters<'a>(
allowed_sources,
model_providers,
cwd_filters,
is_pinned,
anchor,
sort_key,
sort_direction,
@@ -1250,6 +1274,10 @@ pub(super) fn push_thread_filters<'a>(
builder.push(" AND threads.archived = 0");
}
builder.push(" AND threads.preview <> ''");
if let Some(is_pinned) = is_pinned {
builder.push(" AND threads.is_pinned = ");
builder.push_bind(is_pinned);
}
if !allowed_sources.is_empty() {
builder.push(" AND threads.source IN (");
let mut separated = builder.separated(", ");
@@ -1455,6 +1483,173 @@ mod tests {
assert_eq!(metadata.history_mode, ThreadHistoryMode::Paginated);
}
#[tokio::test]
async fn thread_pin_updates_round_trip_and_survive_rollout_reconciliation() {
let codex_home = unique_temp_dir();
let runtime = StateRuntime::init(codex_home.clone(), "test-provider".to_string())
.await
.expect("state db should initialize");
let thread_id = ThreadId::new();
let metadata = test_thread_metadata(&codex_home, thread_id, codex_home.clone());
runtime
.upsert_thread(&metadata)
.await
.expect("thread insert should succeed");
assert!(
!runtime
.get_thread(thread_id)
.await
.unwrap()
.unwrap()
.is_pinned
);
assert!(
runtime
.update_thread_pin(thread_id, /*is_pinned*/ true)
.await
.unwrap()
);
assert!(
runtime
.get_thread(thread_id)
.await
.unwrap()
.unwrap()
.is_pinned
);
runtime
.upsert_thread(&metadata)
.await
.expect("stale rollout metadata should reconcile");
assert!(
runtime
.get_thread(thread_id)
.await
.unwrap()
.unwrap()
.is_pinned
);
assert!(
runtime
.update_thread_pin(thread_id, /*is_pinned*/ false)
.await
.unwrap()
);
assert!(
!runtime
.get_thread(thread_id)
.await
.unwrap()
.unwrap()
.is_pinned
);
assert!(
!runtime
.update_thread_pin(ThreadId::new(), /*is_pinned*/ true)
.await
.unwrap()
);
}
#[tokio::test]
async fn list_threads_filters_pins_before_recency_pagination_and_uses_index() {
let codex_home = unique_temp_dir();
let runtime = StateRuntime::init(codex_home.clone(), "test-provider".to_string())
.await
.expect("state db should initialize");
let oldest_pinned = ThreadId::from_string("00000000-0000-0000-0000-000000000041").unwrap();
let newest_unpinned =
ThreadId::from_string("00000000-0000-0000-0000-000000000042").unwrap();
let newest_pinned = ThreadId::from_string("00000000-0000-0000-0000-000000000043").unwrap();
let oldest_unpinned =
ThreadId::from_string("00000000-0000-0000-0000-000000000044").unwrap();
for (thread_id, recency_at, is_pinned) in [
(oldest_pinned, 1_700_000_001, true),
(newest_unpinned, 1_700_000_003, false),
(newest_pinned, 1_700_000_002, true),
(oldest_unpinned, 1_700_000_000, false),
] {
let mut metadata = test_thread_metadata(&codex_home, thread_id, codex_home.clone());
metadata.recency_at = DateTime::<Utc>::from_timestamp(recency_at, 0).unwrap();
metadata.is_pinned = is_pinned;
runtime.upsert_thread(&metadata).await.unwrap();
}
let filters = |anchor, is_pinned| ThreadFilterOptions {
archived_only: false,
allowed_sources: &[],
model_providers: None,
cwd_filters: None,
is_pinned: Some(is_pinned),
anchor,
sort_key: SortKey::RecencyAt,
sort_direction: SortDirection::Desc,
search_term: None,
};
let first_page = runtime
.list_threads(/*page_size*/ 1, filters(None, true))
.await
.unwrap();
assert_eq!(first_page.items.len(), 1);
assert_eq!(first_page.items[0].id, newest_pinned);
assert!(first_page.items[0].is_pinned);
let second_page = runtime
.list_threads(
/*page_size*/ 1,
filters(first_page.next_anchor.as_ref(), true),
)
.await
.unwrap();
assert_eq!(second_page.items.len(), 1);
assert_eq!(second_page.items[0].id, oldest_pinned);
assert_eq!(second_page.next_anchor, None);
let unpinned_page = runtime
.list_threads(/*page_size*/ 10, filters(None, false))
.await
.unwrap();
assert_eq!(
unpinned_page
.items
.iter()
.map(|thread| thread.id)
.collect::<Vec<_>>(),
vec![newest_unpinned, oldest_unpinned]
);
let mut builder = QueryBuilder::<Sqlite>::new("EXPLAIN QUERY PLAN ");
push_list_threads_query(
&mut builder,
filters(None, true),
/*relation_filter*/ None,
/*limit*/ 2,
);
let plan_details = builder
.build()
.fetch_all(runtime.pool.as_ref())
.await
.unwrap()
.into_iter()
.map(|row| row.get::<String, _>("detail"))
.collect::<Vec<_>>();
assert!(
plan_details
.iter()
.any(|detail| detail.contains("idx_threads_pinned_recency_at_ms")),
"pinned listing did not use its selective recency index: {plan_details:?}"
);
assert!(
!plan_details
.iter()
.any(|detail| detail.contains("TEMP B-TREE")),
"pinned listing unexpectedly sorted outside its index: {plan_details:?}"
);
}
#[tokio::test]
async fn delete_thread_cleans_associated_state() -> Result<()> {
let codex_home = unique_temp_dir();
@@ -1628,6 +1823,7 @@ mod tests {
allowed_sources: &[],
model_providers: Some(&model_providers),
cwd_filters: None,
is_pinned: None,
anchor: Some(&anchor),
sort_key: SortKey::UpdatedAt,
sort_direction: SortDirection::Asc,
@@ -1656,6 +1852,7 @@ mod tests {
allowed_sources: &[],
model_providers: Some(&model_providers),
cwd_filters: None,
is_pinned: None,
anchor: page.next_anchor.as_ref(),
sort_key: SortKey::UpdatedAt,
sort_direction: SortDirection::Asc,
@@ -1709,6 +1906,7 @@ mod tests {
allowed_sources: &[],
model_providers: None,
cwd_filters: Some(cwd_filters.as_slice()),
is_pinned: None,
anchor: None,
sort_key: SortKey::UpdatedAt,
sort_direction: SortDirection::Desc,
@@ -1741,6 +1939,7 @@ mod tests {
allowed_sources: &[],
model_providers: None,
cwd_filters: Some(cwd_filters.as_slice()),
is_pinned: None,
anchor: first_page.next_anchor.as_ref(),
sort_key: SortKey::UpdatedAt,
sort_direction: SortDirection::Desc,
@@ -1766,6 +1965,7 @@ mod tests {
allowed_sources: &[],
model_providers: None,
cwd_filters: Some(&[]),
is_pinned: None,
anchor: None,
sort_key: SortKey::UpdatedAt,
sort_direction: SortDirection::Desc,
@@ -1830,6 +2030,7 @@ mod tests {
allowed_sources: &[],
model_providers: Some(&model_providers),
cwd_filters,
is_pinned: None,
anchor,
sort_key,
sort_direction: SortDirection::Desc,
@@ -1923,6 +2124,7 @@ mod tests {
allowed_sources: &[],
model_providers: None,
cwd_filters: None,
is_pinned: None,
anchor: None,
sort_key: SortKey::CreatedAt,
sort_direction: SortDirection::Desc,
@@ -1951,6 +2153,7 @@ mod tests {
allowed_sources: &[],
model_providers: None,
cwd_filters: None,
is_pinned: None,
anchor,
sort_key: SortKey::CreatedAt,
sort_direction: SortDirection::Desc,
@@ -2595,6 +2798,7 @@ mod tests {
allowed_sources: &[],
model_providers: None,
cwd_filters: None,
is_pinned: None,
anchor: None,
sort_key: SortKey::RecencyAt,
sort_direction: SortDirection::Desc,
@@ -2627,6 +2831,7 @@ mod tests {
allowed_sources: &[],
model_providers: None,
cwd_filters: None,
is_pinned: None,
anchor: first_page.next_anchor.as_ref(),
sort_key: SortKey::RecencyAt,
sort_direction: SortDirection::Desc,
@@ -2659,6 +2864,7 @@ mod tests {
allowed_sources: &[],
model_providers: None,
cwd_filters: None,
is_pinned: None,
anchor: second_page.next_anchor.as_ref(),
sort_key: SortKey::RecencyAt,
sort_direction: SortDirection::Desc,