@devries-cameron20 / t3code Mirror · read-only / commits / b437ec1ab2
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 00:30 UTC

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

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

Julius Marminge committed Oct 19, 2026 at 19:52 UTC b437ec1ab256b108ea0b373cfb35d50f10e0427b
2 files changed +83 -2
apps/server/src/orchestration-v2/threadHistoryPaging.test.ts
+59 -1
@@ -7,7 +7,7 @@ import {
7 type OrchestrationV2ProjectedTurnItem,
8 type OrchestrationV2TurnItem,
9 } from "@t3tools/contracts";
10 -import { describe, expect, it } from "@effect/vitest";
10 +import { describe, expect, it, vi } from "@effect/vitest";
11 import * as DateTime from "effect/DateTime";
12
13 import {
@@ -110,6 +110,27 @@ function interruptRow(
110 };
111 }
112
113 +function assistantRow(
114 + text: string,
115 + visibility: "local" | "inherited" = "local",
116 +): OrchestrationV2ProjectedTurnItem {
117 + const row = makeRow(0);
118 + const sourceThreadId = visibility === "local" ? THREAD : ThreadId.make("parent-thread");
119 + return {
120 + ...row,
121 + visibility,
122 + sourceThreadId,
123 + item: {
124 + ...row.item,
125 + threadId: sourceThreadId,
126 + type: "assistant_message",
127 + messageId: MessageId.make("answer-0"),
128 + text,
129 + streaming: false,
130 + },
131 + } as OrchestrationV2ProjectedTurnItem;
132 +}
133 +
134 function makeProjection(visibleTurnItems: OrchestrationV2ProjectedTurnItem[]) {
135 return {
136 thread: {
@@ -718,6 +739,43 @@ describe("threadHistoryPaging", () => {
739 expect(bounded.projection.visibleTurnItems).toEqual(actionable.visibleTurnItems);
740 });
741
742 + it("reports overflow from timeline text alone without serializing the projection", () => {
743 + // A local item is encoded twice (visible row and turnItems): 2 x 20k chars > 30k bytes.
744 + const projection = makeProjection([assistantRow("a".repeat(20_000))]);
745 + const stringify = vi.spyOn(JSON, "stringify");
746 + try {
747 + const bounded = buildBoundedThreadProjection({
748 + projection,
749 + snapshotSequence: 1,
750 + policy: { maxItems: 75, maxEncodedBytes: 30_000 },
751 + });
752 + expect(bounded.payloadBudgetExceeded).toBe(true);
753 + // Skipping this encode is the shortcut's only observable effect.
754 + expect(stringify).not.toHaveBeenCalledWith(bounded.projection);
755 + } finally {
756 + stringify.mockRestore();
757 + }
758 + });
759 +
760 + it("flips overflow at the exact encoded size when timeline text fits the budget", () => {
761 + // Inherited rows are encoded once, local rows twice; neither may overflow early.
762 + for (const visibility of ["local", "inherited"] as const) {
763 + const projection = makeProjection([assistantRow("a".repeat(20_000), visibility)]);
764 + const build = (maxEncodedBytes: number) =>
765 + buildBoundedThreadProjection({
766 + projection,
767 + snapshotSequence: 1,
768 + policy: { maxItems: 75, maxEncodedBytes },
769 + });
770 + const bytes = Buffer.byteLength(
771 + JSON.stringify(build(Number.MAX_SAFE_INTEGER).projection),
772 + "utf8",
773 + );
774 + expect(build(bytes).payloadBudgetExceeded).toBe(false);
775 + expect(build(bytes - 1).payloadBudgetExceeded).toBe(true);
776 + }
777 + });
778 +
779 it("keeps agent-only turns visible when checkpoint file lists fill the byte budget", () => {
780 const items = Array.from({ length: 3 }, (_, turn) => {
781 const first = makeRow(turn * 5);
apps/server/src/orchestration-v2/threadHistoryPaging.ts
+24 -1
@@ -79,6 +79,27 @@ function bytesOfJson(value: unknown): number {
79 return Buffer.byteLength(JSON.stringify(value), "utf8");
80 }
81
82 +/**
83 + * Proves overflow without serializing: a string's UTF-16 length never exceeds
84 + * its UTF-8 JSON bytes, and local items are encoded once per array they sit in.
85 + */
86 +function timelineTextExceedsBudget(
87 + projection: OrchestrationV2ThreadProjection,
88 + budget: number,
89 +): boolean {
90 + let length = 0;
91 + const exceeds = (item: OrchestrationV2TurnItem): boolean => {
92 + if ("text" in item) {
93 + length += item.text.length;
94 + }
95 + return length > budget;
96 + };
97 + return (
98 + projection.visibleTurnItems.some((row) => exceeds(row.item)) ||
99 + projection.turnItems.some(exceeds)
100 + );
101 +}
102 +
103 export function projectedRowEncodedBytes(row: HistoryRow): number {
104 return bytesOfJson(row);
105 }
@@ -562,6 +583,8 @@ export function buildBoundedThreadProjection(input: {
583 hasMoreHistory: window.hasMoreHistory,
584 latestLocalTurnOrdinal,
585 payloadBudgetExceeded:
565 - controlBytes > policy.maxEncodedBytes || bytesOfJson(projection) > policy.maxEncodedBytes,
586 + controlBytes > policy.maxEncodedBytes ||
587 + timelineTextExceedsBudget(projection, policy.maxEncodedBytes) ||
588 + bytesOfJson(projection) > policy.maxEncodedBytes,
589 };
590 }