Add thread recencyAt for sidebar ordering (#27910)

## Summary

Add a server-owned `recencyAt` timestamp and `recency_at` thread-list
sort key for product recency ordering while preserving the existing
meaning of `updatedAt` as the latest persisted thread mutation.

This is the server-side alternative to #27697. Rather than narrowing
`updatedAt`, clients can sort the sidebar by `recency_at` and continue
treating `updatedAt` as mutation time.

Paired Codex Apps PR:
[openai/openai#1024599](https://github.com/openai/openai/pull/1024599)

## Contract

- `recencyAt` initializes when a thread is created.
- A turn start advances `recencyAt` monotonically.
- Commentary, agent output, tool results, token/accounting updates, turn
completion, archive, unarchive, resume, and generic metadata writes do
not advance it.
- `updatedAt` retains its existing behavior and continues to advance for
persisted thread mutations.
- Current servers populate `recencyAt`; the response field is optional
in generated TypeScript so clients connected to older servers can fall
back to `updatedAt`.
- Filesystem-only fallback uses existing updated/mtime ordering when
SQLite is unavailable.

## Persistence and compatibility

Migration 0038 adds second- and millisecond-precision recency columns,
backfills them from the existing updated timestamp, creates list
indexes, and includes an insert trigger so older binaries writing to a
migrated database seed recency without causing later mutations to
advance it.

Generic metadata upserts preserve existing recency values. Turn-start
updates use a dedicated monotonic touch, and process-local allocation
keeps millisecond cursor values unique. State DB list, search, read,
filtered-list repair, rollout fallback propagation, and app-server
conversions all carry the new field.

## API

`Thread` responses include:

```ts
recencyAt?: number
```

`thread/list` and `thread/search` accept:

```json
{ "sortKey": "recency_at" }
```

Generated TypeScript and JSON schemas are included.

## Validation

- `just test -p codex-state` — 146 passed
- `just test -p codex-rollout` — 69 passed
- `just test -p codex-thread-store` — 81 passed
- `just test -p codex-app-server-protocol` — 231 passed
- Focused app-server list ordering, response mapping, archive/unarchive,
and resume lifecycle tests passed
- Scoped `just fix` for state, rollout, thread-store,
app-server-protocol, and app-server
- `just fmt`
- `git diff --check`
- Independent correctness, simplicity, elegance, security, and
test-quality reviews; actionable ordering, lifecycle, query-projection,
and timestamp-uniqueness findings were addressed
This commit is contained in:
Jeremy Rose authored and GitHub committed 2026-06-16 17:06:22 -07:00
1 parent f0cb96bcb1
commit fac3158c2a
67 files changed
+1153 -99

No files matched your search

+61 -6
View File
@@ -543,6 +543,27 @@ impl RolloutRecorder {
)
.await;
}
if sort_key == ThreadSortKey::RecencyAt {
if let Some(repaired_db_page) = state_db::list_threads_db(
state_db_ctx.as_deref(),
codex_home,
page_size,
cursor,
sort_key,
sort_direction,
allowed_sources,
model_providers,
cwd_filters,
/*parent_thread_id*/ None,
archived,
search_term,
)
.await
{
return Ok(repaired_db_page.into());
}
return Ok(db_page.into());
}
codex_state::record_fallback(
"list_threads",
"metadata_filter",
@@ -985,6 +1006,11 @@ fn truncate_fs_page(
let cursor_token = match sort_key {
ThreadSortKey::CreatedAt => created_at.format(&Rfc3339).ok()?,
ThreadSortKey::UpdatedAt => item.updated_at.as_deref()?.to_string(),
ThreadSortKey::RecencyAt => item
.recency_at
.as_deref()
.or(item.updated_at.as_deref())?
.to_string(),
};
parse_cursor(cursor_token.as_str())
});
@@ -1049,6 +1075,7 @@ fn fill_missing_thread_item_metadata(item: &mut ThreadItem, state_item: ThreadIt
cli_version,
created_at,
updated_at,
recency_at,
} = state_item;
if item.first_user_message.is_none() {
@@ -1093,6 +1120,9 @@ fn fill_missing_thread_item_metadata(item: &mut ThreadItem, state_item: ThreadIt
if item.updated_at.is_none() {
item.updated_at = updated_at;
}
if recency_at.is_some() {
item.recency_at = recency_at;
}
}
#[allow(clippy::too_many_arguments)]
@@ -1269,9 +1299,20 @@ async fn list_threads_from_files_asc(
.collect::<Vec<_>>();
if let Some(cursor) = cursor {
let anchor = cursor.timestamp();
all_items
.retain(|item| thread_item_sort_key(item, sort_key).is_some_and(|key| key.0 > anchor));
let anchor = (
cursor.timestamp(),
cursor
.thread_id()
.and_then(|id| uuid::Uuid::parse_str(&id.to_string()).ok()),
);
all_items.retain(|item| {
thread_item_sort_key(item, sort_key).is_some_and(|key| match anchor.1 {
Some(anchor_id) if sort_key == ThreadSortKey::RecencyAt => {
key > (anchor.0, anchor_id)
}
_ => key.0 > anchor.0,
})
});
}
let more_matches_available = all_items.len() > page_size || reached_scan_cap;
@@ -1329,14 +1370,27 @@ fn thread_item_sort_key(
let updated_at = item.updated_at.as_deref().or(item.created_at.as_deref())?;
OffsetDateTime::parse(updated_at, &Rfc3339).ok()?
}
ThreadSortKey::RecencyAt => {
let recency_at = item
.recency_at
.as_deref()
.or(item.updated_at.as_deref())
.or(item.created_at.as_deref())?;
OffsetDateTime::parse(recency_at, &Rfc3339).ok()?
}
};
Some((timestamp, id))
}
fn cursor_from_thread_item(item: &ThreadItem, sort_key: ThreadSortKey) -> Option<Cursor> {
let (timestamp, _id) = thread_item_sort_key(item, sort_key)?;
let cursor_token = timestamp.format(&Rfc3339).ok()?;
parse_cursor(cursor_token.as_str())
let (timestamp, id) = thread_item_sort_key(item, sort_key)?;
match sort_key {
ThreadSortKey::RecencyAt => Some(Cursor::with_thread_id(
timestamp,
ThreadId::from_string(&id.to_string()).ok()?,
)),
ThreadSortKey::CreatedAt | ThreadSortKey::UpdatedAt => Some(Cursor::new(timestamp)),
}
}
struct LogFileInfo {
@@ -1725,6 +1779,7 @@ fn thread_item_from_state_metadata(item: codex_state::ThreadMetadata) -> ThreadI
cli_version: Some(item.cli_version),
created_at: Some(item.created_at.to_rfc3339_opts(SecondsFormat::Secs, true)),
updated_at: Some(item.updated_at.to_rfc3339_opts(SecondsFormat::Millis, true)),
recency_at: Some(item.recency_at.to_rfc3339_opts(SecondsFormat::Millis, true)),
}
}