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

+129
View File
@@ -0,0 +1,129 @@
use std::borrow::Cow;
use sqlx::Row;
use sqlx::migrate::Migrator;
use sqlx::sqlite::SqlitePoolOptions;
use super::STATE_MIGRATOR;
fn migrator_through(version: i64) -> Migrator {
Migrator {
migrations: Cow::Owned(
STATE_MIGRATOR
.migrations
.iter()
.filter(|migration| migration.version <= version)
.cloned()
.collect(),
),
ignore_missing: STATE_MIGRATOR.ignore_missing,
locking: STATE_MIGRATOR.locking,
table_name: STATE_MIGRATOR.table_name.clone(),
create_schemas: STATE_MIGRATOR.create_schemas.clone(),
no_tx: STATE_MIGRATOR.no_tx,
}
}
#[tokio::test]
async fn recency_migration_backfills_and_seeds_old_binary_inserts() {
let pool = SqlitePoolOptions::new()
.max_connections(1)
.connect("sqlite::memory:")
.await
.expect("in-memory database should open");
migrator_through(/*version*/ 37)
.run(&pool)
.await
.expect("pre-recency migrations should apply");
sqlx::query(
r#"
INSERT INTO threads (
id,
rollout_path,
created_at,
updated_at,
created_at_ms,
updated_at_ms,
source,
model_provider,
cwd,
title,
sandbox_policy,
approval_mode
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"#,
)
.bind("00000000-0000-0000-0000-000000000001")
.bind("/tmp/first.jsonl")
.bind(1_700_000_000_i64)
.bind(1_700_000_100_i64)
.bind(1_700_000_000_123_i64)
.bind(1_700_000_100_456_i64)
.bind("cli")
.bind("openai")
.bind("/tmp")
.bind("")
.bind("read-only")
.bind("on-request")
.execute(&pool)
.await
.expect("legacy row should insert");
STATE_MIGRATOR
.run(&pool)
.await
.expect("recency migration should apply");
let backfilled = sqlx::query(
"SELECT updated_at, updated_at_ms, recency_at, recency_at_ms FROM threads WHERE id = ?",
)
.bind("00000000-0000-0000-0000-000000000001")
.fetch_one(&pool)
.await
.expect("backfilled row should load");
assert_eq!(backfilled.get::<i64, _>("recency_at"), 1_700_000_100);
assert_eq!(backfilled.get::<i64, _>("recency_at_ms"), 1_700_000_100_456);
sqlx::query(
r#"
INSERT INTO threads (
id,
rollout_path,
created_at,
updated_at,
created_at_ms,
updated_at_ms,
source,
model_provider,
cwd,
title,
sandbox_policy,
approval_mode
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"#,
)
.bind("00000000-0000-0000-0000-000000000002")
.bind("/tmp/second.jsonl")
.bind(1_700_000_200_i64)
.bind(1_700_000_300_i64)
.bind(1_700_000_200_123_i64)
.bind(1_700_000_300_456_i64)
.bind("cli")
.bind("openai")
.bind("/tmp")
.bind("")
.bind("read-only")
.bind("on-request")
.execute(&pool)
.await
.expect("old-binary row should insert");
let seeded = sqlx::query("SELECT recency_at, recency_at_ms FROM threads WHERE id = ?")
.bind("00000000-0000-0000-0000-000000000002")
.fetch_one(&pool)
.await
.expect("old-binary row should load");
assert_eq!(seeded.get::<i64, _>("recency_at"), 1_700_000_300);
assert_eq!(seeded.get::<i64, _>("recency_at_ms"), 1_700_000_300_456);
}