Enable paginated thread history in app-server (#33364)

## What changed

- Allow `thread/start` to select experimental `historyMode: "paginated"` when the thread store supports turn and item pagination.
- Resume paginated threads with bounded model context and require `excludeTurns: true` instead of loading full history.
- Return `turnsBackwardsCursor` and `itemsBackwardsCursor` from `thread/resume` so clients can hydrate durable history through `thread/turns/list` and `thread/items/list` while newer records arrive through live notifications.
- Reject operations that still require full history, including `initialTurnsPage`, `thread/read` with `includeTurns: true`, `thread/rollback`, and detached review.
- Make item cursors independent of the optional `turnId` filter.

## Testing

- Cover paginated thread start, cold and live resume, backwards cursor hydration, metadata reads, unsupported operations, and stores without pagination support.

GitOrigin-RevId: c42f9b268e29d049843330f474fa3ecde12f5b8a
This commit is contained in:
Owen Lin authored and copyberry committed 2026-07-15 15:29:13 +00:00
1 parent 1bbdb32789
commit da61f7d8e1
26 files changed
+758 -138

No files matched your search

@@ -18,7 +18,6 @@ use crate::ReadThreadParams;
use crate::ResumeThreadParams;
use crate::ThreadStoreError;
use crate::ThreadStoreResult;
use crate::error::reject_paginated_history_mode;
use crate::types::canonical_history_mode_from_rollout_items;
const ROLLOUT_SIZE_BYTES_METRIC: &str = "codex.rollout.size_bytes";
@@ -66,7 +65,6 @@ pub(super) async fn resume_thread(
.await?
.history_mode
};
reject_paginated_history_mode(history_mode)?;
let rollout_path = match (params.rollout_path, params.history) {
(Some(rollout_path), _history) => rollout_path,
(None, history) => {
+15 -13
View File
@@ -344,6 +344,10 @@ impl ThreadStore for LocalThreadStore {
Box::pin(async move { list_threads::list_threads(self, params).await })
}
fn supports_paginated_history_lists(&self) -> bool {
true
}
fn list_turns(&self, params: ListTurnsParams) -> ThreadStoreFuture<'_, TurnPage> {
Box::pin(LocalThreadStore::list_turns(self, params))
}
@@ -1244,7 +1248,7 @@ mod tests {
}
#[tokio::test]
async fn paginated_threads_allow_metadata_reads_and_reject_legacy_history_paths() {
async fn paginated_threads_allow_metadata_reads_and_resume_but_reject_legacy_history_paths() {
let home = TempDir::new().expect("temp dir");
let store = LocalThreadStore::new(test_config(home.path()), /*state_db*/ None);
let uuid = uuid::Uuid::from_u128(408);
@@ -1308,18 +1312,16 @@ mod tests {
.await
.expect_err("history load should fail"),
);
assert_paginated_threads_unsupported(
store
.resume_thread(ResumeThreadParams {
thread_id,
rollout_path: Some(rollout_path),
history: None,
include_archived: false,
metadata: thread_metadata(),
})
.await
.expect_err("resume should fail"),
);
store
.resume_thread(ResumeThreadParams {
thread_id,
rollout_path: Some(rollout_path),
history: None,
include_archived: false,
metadata: thread_metadata(),
})
.await
.expect("resume should succeed");
}
#[tokio::test]
@@ -89,7 +89,7 @@ pub(super) fn write_session_file_with_fork(
fs::create_dir_all(&day_dir)?;
let path = day_dir.join(format!("rollout-{ts}-{uuid}.jsonl"));
let mut file = fs::File::create(&path)?;
let meta = serde_json::json!({
let mut meta = serde_json::json!({
"timestamp": ts,
"type": "session_meta",
"payload": {
@@ -110,6 +110,9 @@ pub(super) fn write_session_file_with_fork(
}
},
});
if matches!(history_mode, ThreadHistoryMode::Paginated) {
meta["ordinal"] = serde_json::json!(0);
}
writeln!(file, "{meta}")?;
if matches!(history_mode, ThreadHistoryMode::Legacy) {
let user_event = serde_json::json!({
@@ -38,7 +38,7 @@ struct HistoryCursor {
#[serde(tag = "kind", rename_all = "camelCase")]
enum CursorScope {
Turns,
Items { turn_id: Option<String> },
Items,
}
struct StoredTurnRow {
@@ -148,9 +148,7 @@ pub(in crate::local) async fn list_items(
"list_items",
)
.await?;
let scope = CursorScope::Items {
turn_id: params.turn_id.clone(),
};
let scope = CursorScope::Items;
let cursor = parse_cursor(params.cursor.as_deref(), params.thread_id, &scope)?;
let pool = store.thread_history_db().await?;
let limit = page_limit(params.page_size)?;
@@ -184,6 +184,20 @@ async fn list_items_pages_whole_thread_and_per_turn_rows() {
.await
.expect("turn item page");
assert_eq!(item_ids(&turn_page), vec!["item-5", "item-4"]);
let whole_thread_from_turn_cursor = store
.list_items(item_params(
thread_id,
/*turn_id*/ None,
turn_page.backwards_cursor.clone(),
/*page_size*/ 2,
SortDirection::Desc,
))
.await
.expect("whole-thread page from turn cursor");
assert_eq!(
item_ids(&whole_thread_from_turn_cursor),
vec!["item-5", "item-4"]
);
let next_turn_page = store
.list_items(item_params(
thread_id,