Skip to content
TypeScript

perf(server): bounded snapshots of threads with large messages skip a full encode (#20064)

Julius Marmingeb437ec1ab256
diff --git a/apps/server/src/orchestration-v2/threadHistoryPaging.test.ts b/apps/server/src/orchestration-v2/threadHistoryPaging.test.ts
index 0b58163bbdf..2e96b480588 100644
--- a/apps/server/src/orchestration-v2/threadHistoryPaging.test.ts
+++ b/apps/server/src/orchestration-v2/threadHistoryPaging.test.ts
@@ -7,7 +7,7 @@ import {
type OrchestrationV2ProjectedTurnItem,
type OrchestrationV2TurnItem,
} from "@t3tools/contracts";
-import { describe, expect, it } from "@effect/vitest";
+import { describe, expect, it, vi } from "@effect/vitest";
import * as DateTime from "effect/DateTime";
import {
@@ -110,6 +110,27 @@ function interruptRow(
};
}
+function assistantRow(
+ text: string,
+ visibility: "local" | "inherited" = "local",
+): OrchestrationV2ProjectedTurnItem {
+ const row = makeRow(0);
+ const sourceThreadId = visibility === "local" ? THREAD : ThreadId.make("parent-thread");
+ return {
+ ...row,
+ visibility,
+ sourceThreadId,
+ item: {
+ ...row.item,
+ threadId: sourceThreadId,
+ type: "assistant_message",
+ messageId: MessageId.make("answer-0"),
+ text,
+ streaming: false,
+ },
+ } as OrchestrationV2ProjectedTurnItem;
+}
+
function makeProjection(visibleTurnItems: OrchestrationV2ProjectedTurnItem[]) {
return {
thread: {
@@ -718,6 +739,43 @@ describe("threadHistoryPaging", () => {
expect(bounded.projection.visibleTurnItems).toEqual(actionable.visibleTurnItems);
});
+ it("reports overflow from timeline text alone without serializing the projection", () => {
+ // A local item is encoded twice (visible row and turnItems): 2 x 20k chars > 30k bytes.
+ const projection = makeProjection([assistantRow("a".repeat(20_000))]);
+ const stringify = vi.spyOn(JSON, "stringify");
+ try {
+ const bounded = buildBoundedThreadProjection({
+ projection,
+ snapshotSequence: 1,
+ policy: { maxItems: 75, maxEncodedBytes: 30_000 },
+ });
+ expect(bounded.payloadBudgetExceeded).toBe(true);
+ // Skipping this encode is the shortcut's only observable effect.
+ expect(stringify).not.toHaveBeenCalledWith(bounded.projection);
+ } finally {
+ stringify.mockRestore();
+ }
+ });
+
+ it("flips overflow at the exact encoded size when timeline text fits the budget", () => {
+ // Inherited rows are encoded once, local rows twice; neither may overflow early.
+ for (const visibility of ["local", "inherited"] as const) {
+ const projection = makeProjection([assistantRow("a".repeat(20_000), visibility)]);
+ const build = (maxEncodedBytes: number) =>
+ buildBoundedThreadProjection({
+ projection,
+ snapshotSequence: 1,
+ policy: { maxItems: 75, maxEncodedBytes },
+ });
+ const bytes = Buffer.byteLength(
+ JSON.stringify(build(Number.MAX_SAFE_INTEGER).projection),
+ "utf8",
+ );
+ expect(build(bytes).payloadBudgetExceeded).toBe(false);
+ expect(build(bytes - 1).payloadBudgetExceeded).toBe(true);
+ }
+ });
+
it("keeps agent-only turns visible when checkpoint file lists fill the byte budget", () => {
const items = Array.from({ length: 3 }, (_, turn) => {
const first = makeRow(turn * 5);
diff --git a/apps/server/src/orchestration-v2/threadHistoryPaging.ts b/apps/server/src/orchestration-v2/threadHistoryPaging.ts
index ee675856477..5f466bdbb15 100644
--- a/apps/server/src/orchestration-v2/threadHistoryPaging.ts
+++ b/apps/server/src/orchestration-v2/threadHistoryPaging.ts
@@ -79,6 +79,27 @@ function bytesOfJson(value: unknown): number {
return Buffer.byteLength(JSON.stringify(value), "utf8");
}
+/**
+ * Proves overflow without serializing: a string's UTF-16 length never exceeds
+ * its UTF-8 JSON bytes, and local items are encoded once per array they sit in.
+ */
+function timelineTextExceedsBudget(
+ projection: OrchestrationV2ThreadProjection,
+ budget: number,
+): boolean {
+ let length = 0;
+ const exceeds = (item: OrchestrationV2TurnItem): boolean => {
+ if ("text" in item) {
+ length += item.text.length;
+ }
+ return length > budget;
+ };
+ return (
+ projection.visibleTurnItems.some((row) => exceeds(row.item)) ||
+ projection.turnItems.some(exceeds)
+ );
+}
+
export function projectedRowEncodedBytes(row: HistoryRow): number {
return bytesOfJson(row);
}
@@ -562,6 +583,8 @@ export function buildBoundedThreadProjection(input: {
hasMoreHistory: window.hasMoreHistory,
latestLocalTurnOrdinal,
payloadBudgetExceeded:
- controlBytes > policy.maxEncodedBytes || bytesOfJson(projection) > policy.maxEncodedBytes,
+ controlBytes > policy.maxEncodedBytes ||
+ timelineTextExceedsBudget(projection, policy.maxEncodedBytes) ||
+ bytesOfJson(projection) > policy.maxEncodedBytes,
};
}