mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
Add item anchors to thread/items/list pagination (#48151)
## What changed
Allow `cursor` to accept an item anchor such as `{"type":"item","itemId":"item-123"}` with a non-empty `turnId`. Ascending pagination returns items after the anchor; descending pagination returns items before it. The anchor itself is excluded, and subsequent pages use the returned opaque string cursor.
Resolve anchors within the requested turn's visible history, including inherited fork history. Reject missing turn IDs and empty, unknown, or out-of-scope item IDs with invalid params (`-32602`). Preserve existing string and null cursor behavior, and update the protocol schemas, TypeScript and Python types, and API documentation.
## Testing
Add protocol round-trip and invalid-shape tests, app-server coverage for anchored pagination and errors, and thread-store coverage for growing turns, reverse pagination, page boundaries, fork visibility, and anchor scope validation.
GitOrigin-RevId: a74cea190d3ff1b44865429f8b0650b793eb688d
This commit is contained in:
@@ -454,3 +454,19 @@ credential discovery/signing, are disabled while restrictions apply. Supported
|
||||
HTTP, WebSocket, and code-mode gRPC requests use the shared destination checks.
|
||||
User-directed Git, SSH, shell, and other subprocess traffic retain their existing
|
||||
execution and sandbox policies.
|
||||
|
||||
# Item history anchors
|
||||
|
||||
`thread/items/list` accepts an optional nullable `cursor`: either an opaque string
|
||||
from a previous response or an item anchor such as
|
||||
`{"type":"item","itemId":"item-123"}`. An item anchor resumes exclusively after
|
||||
that item in the requested pagination order: ascending (the default) returns newer
|
||||
items, and descending returns older items. It requires a non-empty `turnId`;
|
||||
otherwise the request returns invalid params (`-32602`) with
|
||||
`turnId is required when cursor is an item anchor`.
|
||||
The item must belong to that turn in the thread's visible history. Empty, unknown,
|
||||
and out-of-scope item IDs return invalid params (`-32602`) with
|
||||
`cursor.itemId does not identify an item in the requested history scope`.
|
||||
Omitted or null cursors preserve normal first-page behavior. Continue anchored
|
||||
pages with the returned opaque string `nextCursor`; response fields and
|
||||
`backwardsCursor` semantics are unchanged.
|
||||
|
||||
@@ -485,6 +485,7 @@ use codex_thread_store::DeleteThreadsParams as StoreDeleteThreadsParams;
|
||||
use codex_thread_store::GitInfoPatch as StoreGitInfoPatch;
|
||||
use codex_thread_store::ItemSortKey as StoreItemSortKey;
|
||||
use codex_thread_store::ListItemsParams as StoreListItemsParams;
|
||||
use codex_thread_store::ListItemsPosition as StoreListItemsPosition;
|
||||
use codex_thread_store::ListThreadsParams as StoreListThreadsParams;
|
||||
use codex_thread_store::ListTimelineParams as StoreListTimelineParams;
|
||||
use codex_thread_store::ListTurnsParams as StoreListTurnsParams;
|
||||
|
||||
@@ -14,6 +14,8 @@ use super::*;
|
||||
use crate::error_code::method_not_found;
|
||||
use codex_app_server_protocol::SelectedCapabilityRoot;
|
||||
use codex_app_server_protocol::ThreadHistoryMode as ApiThreadHistoryMode;
|
||||
use codex_app_server_protocol::ThreadItemsListAnchor;
|
||||
use codex_app_server_protocol::ThreadItemsListCursor;
|
||||
use codex_app_server_protocol::ThreadRevertParams;
|
||||
use codex_app_server_protocol::ThreadRevertResponse;
|
||||
use codex_app_server_protocol::ThreadRevertedNotification;
|
||||
@@ -3289,7 +3291,7 @@ impl ThreadRequestProcessor {
|
||||
thread_id,
|
||||
turn_id: Some(turn_id.to_string()),
|
||||
include_archived: true,
|
||||
cursor: cursor.clone(),
|
||||
position: cursor.clone().map(StoreListItemsPosition::Cursor),
|
||||
page_size: THREAD_ITEMS_MAX_LIMIT,
|
||||
sort_direction: StoreSortDirection::Asc,
|
||||
sort_key: StoreItemSortKey::CreatedAtOrdinal,
|
||||
@@ -3405,7 +3407,7 @@ impl ThreadRequestProcessor {
|
||||
thread_id,
|
||||
turn_id: None,
|
||||
include_archived: true,
|
||||
cursor: None,
|
||||
position: None,
|
||||
page_size: 1,
|
||||
sort_direction: StoreSortDirection::Desc,
|
||||
sort_key: StoreItemSortKey::CreatedAtOrdinal,
|
||||
@@ -3427,6 +3429,18 @@ impl ThreadRequestProcessor {
|
||||
limit,
|
||||
sort_direction,
|
||||
} = params;
|
||||
let position = cursor.map(|cursor| match cursor {
|
||||
ThreadItemsListCursor::Opaque(cursor) => StoreListItemsPosition::Cursor(cursor),
|
||||
ThreadItemsListCursor::Anchor(ThreadItemsListAnchor::Item { item_id }) => {
|
||||
StoreListItemsPosition::ItemAnchor { item_id }
|
||||
}
|
||||
});
|
||||
let has_anchor = matches!(position, Some(StoreListItemsPosition::ItemAnchor { .. }));
|
||||
if has_anchor && turn_id.as_deref().is_none_or(str::is_empty) {
|
||||
return Err(invalid_params(
|
||||
"turnId is required when cursor is an item anchor",
|
||||
));
|
||||
}
|
||||
let thread_id = ThreadId::from_string(&thread_id)
|
||||
.map_err(|err| invalid_request(format!("invalid thread id: {err}")))?;
|
||||
let page_size = limit
|
||||
@@ -3439,7 +3453,7 @@ impl ThreadRequestProcessor {
|
||||
thread_id,
|
||||
turn_id,
|
||||
include_archived: true,
|
||||
cursor,
|
||||
position,
|
||||
page_size,
|
||||
sort_direction: match sort_direction.unwrap_or(SortDirection::Asc) {
|
||||
SortDirection::Asc => StoreSortDirection::Asc,
|
||||
@@ -3450,6 +3464,9 @@ impl ThreadRequestProcessor {
|
||||
})
|
||||
.await
|
||||
.map_err(|err| match err {
|
||||
ThreadStoreError::InvalidRequest { message } if has_anchor => {
|
||||
invalid_params(message)
|
||||
}
|
||||
ThreadStoreError::InvalidRequest { message } => invalid_request(message),
|
||||
ThreadStoreError::Unsupported { .. } => {
|
||||
method_not_found("thread/items/list is not supported yet")
|
||||
|
||||
@@ -25,6 +25,8 @@ use codex_app_server_protocol::ThreadForkResponse;
|
||||
use codex_app_server_protocol::ThreadHistoryMode;
|
||||
use codex_app_server_protocol::ThreadItem;
|
||||
use codex_app_server_protocol::ThreadItemEntry;
|
||||
use codex_app_server_protocol::ThreadItemsListAnchor;
|
||||
use codex_app_server_protocol::ThreadItemsListCursor;
|
||||
use codex_app_server_protocol::ThreadItemsListParams;
|
||||
use codex_app_server_protocol::ThreadItemsListResponse;
|
||||
use codex_app_server_protocol::ThreadListParams;
|
||||
@@ -2052,6 +2054,84 @@ async fn paginated_history_lists_and_legacy_reads_use_projected_turns_and_items(
|
||||
assert_eq!(third_items_page.data[1].turn_id, "turn-2");
|
||||
assert_eq!(third_items_page.data[1].item.id(), "user-2");
|
||||
|
||||
let anchor_params = ThreadItemsListParams {
|
||||
thread_id: thread_id.to_string(),
|
||||
turn_id: Some("turn-1".to_string()),
|
||||
cursor: None,
|
||||
limit: Some(1),
|
||||
sort_direction: None,
|
||||
};
|
||||
for direction in [None, Some(SortDirection::Desc)] {
|
||||
let request = mcp
|
||||
.send_thread_items_list_request(ThreadItemsListParams {
|
||||
limit: Some(10),
|
||||
sort_direction: direction,
|
||||
..anchor_params.clone()
|
||||
})
|
||||
.await?;
|
||||
let ordinary: ThreadItemsListResponse =
|
||||
timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(request)).await??;
|
||||
assert_eq!(ordinary.data.len(), 3);
|
||||
let request = mcp
|
||||
.send_thread_items_list_request(ThreadItemsListParams {
|
||||
cursor: Some(ThreadItemsListCursor::Anchor(ThreadItemsListAnchor::Item {
|
||||
item_id: ordinary.data[0].item.id().to_string(),
|
||||
})),
|
||||
sort_direction: direction,
|
||||
..anchor_params.clone()
|
||||
})
|
||||
.await?;
|
||||
let anchored: ThreadItemsListResponse =
|
||||
timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(request)).await??;
|
||||
assert_eq!(anchored.data, ordinary.data[1..2]);
|
||||
let request = mcp
|
||||
.send_thread_items_list_request(ThreadItemsListParams {
|
||||
cursor: Some(ThreadItemsListCursor::Opaque(
|
||||
anchored
|
||||
.next_cursor
|
||||
.expect("anchored page has a continuation"),
|
||||
)),
|
||||
sort_direction: direction,
|
||||
..anchor_params.clone()
|
||||
})
|
||||
.await?;
|
||||
let continued: ThreadItemsListResponse =
|
||||
timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(request)).await??;
|
||||
assert_eq!(continued.data, ordinary.data[2..]);
|
||||
assert_eq!(continued.next_cursor, None);
|
||||
}
|
||||
for (turn_id, anchor, message) in [
|
||||
(
|
||||
None,
|
||||
"steer-1",
|
||||
"turnId is required when cursor is an item anchor",
|
||||
),
|
||||
(
|
||||
Some("turn-1"),
|
||||
"missing",
|
||||
"cursor.itemId does not identify an item in the requested history scope",
|
||||
),
|
||||
] {
|
||||
let request = mcp
|
||||
.send_thread_items_list_request(ThreadItemsListParams {
|
||||
turn_id: turn_id.map(str::to_string),
|
||||
cursor: Some(ThreadItemsListCursor::Anchor(ThreadItemsListAnchor::Item {
|
||||
item_id: anchor.to_string(),
|
||||
})),
|
||||
..anchor_params.clone()
|
||||
})
|
||||
.await?;
|
||||
let error = timeout(
|
||||
DEFAULT_READ_TIMEOUT,
|
||||
mcp.read_stream_until_error_message(RequestId::Integer(request)),
|
||||
)
|
||||
.await??;
|
||||
assert_eq!(
|
||||
(error.error.code, error.error.message.as_str()),
|
||||
(-32602, message)
|
||||
);
|
||||
}
|
||||
|
||||
let turn_start_id = mcp
|
||||
.send_turn_start_request(TurnStartParams {
|
||||
thread_id: thread_id.to_string(),
|
||||
@@ -2299,7 +2379,7 @@ async fn read_items_page(
|
||||
.send_thread_items_list_request(ThreadItemsListParams {
|
||||
thread_id: thread_id.to_string(),
|
||||
turn_id: turn_id.map(str::to_string),
|
||||
cursor,
|
||||
cursor: cursor.map(ThreadItemsListCursor::Opaque),
|
||||
limit,
|
||||
sort_direction: Some(sort_direction),
|
||||
})
|
||||
|
||||
@@ -17,6 +17,7 @@ use codex_app_server_protocol::SortDirection;
|
||||
use codex_app_server_protocol::ThreadForkParams;
|
||||
use codex_app_server_protocol::ThreadForkResponse;
|
||||
use codex_app_server_protocol::ThreadHistoryMode;
|
||||
use codex_app_server_protocol::ThreadItemsListCursor;
|
||||
use codex_app_server_protocol::ThreadItemsListParams;
|
||||
use codex_app_server_protocol::ThreadItemsListResponse;
|
||||
use codex_app_server_protocol::ThreadResumeParams;
|
||||
@@ -490,7 +491,7 @@ async fn thread_revert_replaces_paginated_history_before_turn() -> Result<()> {
|
||||
params: ThreadItemsListParams {
|
||||
thread_id: thread.id.clone(),
|
||||
turn_id: None,
|
||||
cursor: items_backwards_cursor,
|
||||
cursor: items_backwards_cursor.map(ThreadItemsListCursor::Opaque),
|
||||
limit: None,
|
||||
sort_direction: None,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user