Skip to content

Commit 6739dd7

Browse files
Yuruzuuclaude
andauthored
perf(server): thread snapshots skip serializing a row past the row limit (#29103)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 448c1ac commit 6739dd7

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

‎apps/server/src/orchestration-v2/threadHistoryPaging.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ function makeRow(
6666
};
6767
}
6868

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+
6989
function interruptItem(
7090
id: string,
7191
type: "run_interrupt_request" | "run_interrupt_result",
@@ -450,6 +470,65 @@ describe("threadHistoryPaging", () => {
450470
}
451471
});
452472

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+
453532
it("recovers identity-miss cursors when position is past a shrunken timeline", () => {
454533
const items = Array.from({ length: 5 }, (_, index) => makeRow(index));
455534
const cursor = encodeThreadHistoryCursor({

‎apps/server/src/orchestration-v2/threadHistoryPaging.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,16 +331,16 @@ function selectOlderTimelinePage<Row extends HistoryRow>(input: {
331331
turnLimit === undefined ? policy.maxItems : (policy.maxTurnItems ?? policy.maxItems);
332332
for (let index = end - 1; index >= 0; index -= 1) {
333333
const row = input.items[index]!;
334-
const rowBytes = rowCost(row);
335334
if (
336335
selected.length > 0 &&
337336
(selected.length >= maxItems ||
338-
encodedBytes + rowBytes > policy.maxEncodedBytes ||
339337
(turnLimit !== undefined &&
340338
(userTurns >= turnLimit || rawTurns >= THREAD_HISTORY_MAX_RAW_TURNS)))
341339
) {
342340
break;
343341
}
342+
const rowBytes = rowCost(row);
343+
if (selected.length > 0 && encodedBytes + rowBytes > policy.maxEncodedBytes) break;
344344
selected.push(row);
345345
encodedBytes += rowBytes;
346346
if (isThreadHistoryUserTurn(row.item)) userTurns += 1;

0 commit comments

Comments
 (0)