@devries-cameron20 / t3code-1 / commits / 6739dd7294
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(server): thread snapshots skip serializing a row past the row limit (#29103)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Andrei committed Nov 3, 2026 at 09:37 UTC 6739dd729431d4e009e4795eeb2b26186348dcc3
2 files changed +81 -2
apps/server/src/orchestration-v2/threadHistoryPaging.test.ts
+79
@@ -66,6 +66,26 @@ function makeRow(
66 };
67 }
68
69 +function makeTurnStart(
70 + index: number,
71 + createdBy: "user" | "agent",
72 +): OrchestrationV2ProjectedTurnItem {
73 + const row = makeRow(index);
74 + return {
75 + ...row,
76 + item: {
77 + ...row.item,
78 + type: "user_message",
79 + createdBy,
80 + creationSource: "provider",
81 + inputIntent: "turn_start",
82 + messageId: MessageId.make(`prompt-${index}`),
83 + text: `Prompt ${index}`,
84 + attachments: [],
85 + },
86 + } as OrchestrationV2ProjectedTurnItem;
87 +}
88 +
89 function interruptItem(
90 id: string,
91 type: "run_interrupt_request" | "run_interrupt_result",
@@ -450,6 +470,65 @@ describe("threadHistoryPaging", () => {
470 }
471 });
472
473 + it.each([
474 + {
475 + name: "item ceiling",
476 + items: Array.from({ length: 5 }, (_, index) => makeRow(index)),
477 + policy: { maxItems: 3, maxEncodedBytes: 1_000 },
478 + admitted: 3,
479 + measured: 3,
480 + },
481 + {
482 + name: "user turn ceiling",
483 + items: [makeRow(0), makeTurnStart(1, "user"), makeRow(2)],
484 + policy: { maxUserTurns: 1, maxItems: 100, maxEncodedBytes: 1_000 },
485 + admitted: 2,
486 + measured: 2,
487 + },
488 + {
489 + name: "raw turn ceiling",
490 + items: [
491 + makeTurnStart(0, "user"),
492 + ...Array.from({ length: 151 }, (_, index) => makeTurnStart(index + 1, "agent")),
493 + ],
494 + policy: { maxUserTurns: 10, maxItems: 1_000, maxEncodedBytes: 1_000_000 },
495 + admitted: 150,
496 + measured: 150,
497 + },
498 + {
499 + name: "byte budget, which still admits an exact fit",
500 + items: Array.from({ length: 5 }, (_, index) => makeRow(index)),
501 + policy: { maxItems: 100, maxEncodedBytes: 20 },
502 + admitted: 2,
503 + measured: 3,
504 + },
505 + {
506 + name: "byte budget, which still admits an oversized first row",
507 + items: Array.from({ length: 5 }, (_, index) => makeRow(index)),
508 + policy: { maxItems: 100, maxEncodedBytes: 5 },
509 + admitted: 1,
510 + measured: 2,
511 + },
512 + ])("measures only the rows needed to stop at the $name", (testCase) => {
513 + const measured: OrchestrationV2ProjectedTurnItem[] = [];
514 + const page = selectRecentTimelineWindow({
515 + items: testCase.items,
516 + snapshotSequence: 1,
517 + policy: testCase.policy,
518 + rowEncodedBytes: (row) => {
519 + measured.push(row);
520 + return 10;
521 + },
522 + });
523 +
524 + expect(page.items.map((row) => row.sourceItemId)).toEqual(
525 + testCase.items.slice(-testCase.admitted).map((row) => row.sourceItemId),
526 + );
527 + expect(measured).toHaveLength(testCase.measured);
528 + expect(page.hasMoreHistory).toBe(true);
529 + expect(decodeThreadHistoryCursor(page.nextCursor!).si).toBe(page.items[0]?.sourceItemId);
530 + });
531 +
532 it("recovers identity-miss cursors when position is past a shrunken timeline", () => {
533 const items = Array.from({ length: 5 }, (_, index) => makeRow(index));
534 const cursor = encodeThreadHistoryCursor({
apps/server/src/orchestration-v2/threadHistoryPaging.ts
+2 -2
@@ -331,16 +331,16 @@ function selectOlderTimelinePage<Row extends HistoryRow>(input: {
331 turnLimit === undefined ? policy.maxItems : (policy.maxTurnItems ?? policy.maxItems);
332 for (let index = end - 1; index >= 0; index -= 1) {
333 const row = input.items[index]!;
334 - const rowBytes = rowCost(row);
334 if (
335 selected.length > 0 &&
336 (selected.length >= maxItems ||
338 - encodedBytes + rowBytes > policy.maxEncodedBytes ||
337 (turnLimit !== undefined &&
338 (userTurns >= turnLimit || rawTurns >= THREAD_HISTORY_MAX_RAW_TURNS)))
339 ) {
340 break;
341 }
342 + const rowBytes = rowCost(row);
343 + if (selected.length > 0 && encodedBytes + rowBytes > policy.maxEncodedBytes) break;
344 selected.push(row);
345 encodedBytes += rowBytes;
346 if (isThreadHistoryUserTurn(row.item)) userTurns += 1;