From 21709812bb3dbb3191f9d5cda2f342b628eb4071 Mon Sep 17 00:00:00 2001 From: Rock Date: Thu, 12 Nov 2026 13:02:10 -0800 Subject: [PATCH] fix(codex): retry an oversized history page with smaller pages (#9432) thread/items/list asks for 100 items a page. On image-heavy threads a page can exceed the app-server's line cap, and the history stopped at the first such page with a "too large to load" warning, dropping every later item even when each one fits. The reader now halves the page and retries the same cursor, and shows the warning only when a single item cannot fit. Co-authored-by: Claude Opus 5.5 --- .../providers/codex/thread-history.test.ts | 11 ++++++-- .../agent/providers/codex/thread-history.ts | 27 ++++++++++++++----- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/packages/server/src/server/agent/providers/codex/thread-history.test.ts b/packages/server/src/server/agent/providers/codex/thread-history.test.ts index 3feb737aa..3f0e35801 100644 --- a/packages/server/src/server/agent/providers/codex/thread-history.test.ts +++ b/packages/server/src/server/agent/providers/codex/thread-history.test.ts @@ -147,7 +147,7 @@ describe("Codex thread history", () => { appServer.assertNoErrors(); }); - test("a page too big to send shows a warning row and stops loading", async () => { + test("a page too big to send retries smaller pages, then warns at the item that cannot fit", async () => { const answers = Array.from({ length: 120 }, (_, index) => agentItem(`agent-${index}`, `Answer ${index}`), ); @@ -163,11 +163,12 @@ describe("Codex thread history", () => { ); const loaded = [ { type: "user_message", text: "One", messageId: "user-1" }, - ...answers.slice(0, 99).map((answer) => ({ + ...answers.map((answer) => ({ type: "assistant_message", text: answer.text, messageId: answer.id, })), + { type: "user_message", text: "Two", messageId: "user-2" }, { type: "notification", level: "warning", @@ -176,6 +177,12 @@ describe("Codex thread history", () => { ]; try { expect(timelineItems(await readHistory(session))).toEqual(loaded); + // Each oversized page retries the same cursor at half the size until it fits. + expect( + historyRequests(appServer) + .filter((request) => request.method === "thread/items/list") + .map((request) => (request.params as { limit: number }).limit), + ).toEqual([100, 100, 50, 25, 12, 12, 6, 6, 3, 3, 1, 1]); // The oversized reply failed only its own request; the session still answers. expect(timelineItems(await readHistory(session))).toEqual(loaded); } finally { diff --git a/packages/server/src/server/agent/providers/codex/thread-history.ts b/packages/server/src/server/agent/providers/codex/thread-history.ts index bf7461deb..557d92150 100644 --- a/packages/server/src/server/agent/providers/codex/thread-history.ts +++ b/packages/server/src/server/agent/providers/codex/thread-history.ts @@ -124,15 +124,28 @@ async function* readPaginatedHistory( ): AsyncGenerator { const seenCursors = new Set(); let cursor: string | undefined; + let limit = PAGE_SIZE; for (;;) { - const page = CodexItemsPageSchema.parse( - await requestHistory( - client, - "thread/items/list", - { threadId, limit: PAGE_SIZE, sortDirection: "asc", cursor }, + let response: unknown; + try { + response = await client.request("thread/items/list", { + threadId, + limit, + sortDirection: "asc", + cursor, + }); + } catch (error) { + if (!(error instanceof CodexAppServerResponseTooLargeError)) throw error; + // Image-heavy items can push a full page past the line cap; retry the same cursor smaller. + if (limit > 1) { + limit = Math.max(1, Math.floor(limit / 2)); + continue; + } + throw new CodexThreadHistoryTooLargeError( "The rest of this Codex thread's history is too large to load.", - ), - ); + ); + } + const page = CodexItemsPageSchema.parse(response); for (const row of page.data) { yield { item: row.item,