Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: keep turns over the message budget whole

ogsamrat9e0fd2077a90
diff --git a/apps/server/src/session-upload-observer-big-turn.test.ts b/apps/server/src/session-upload-observer-big-turn.test.ts
new file mode 100644
index 00000000..72c60bf6
--- /dev/null
+++ b/apps/server/src/session-upload-observer-big-turn.test.ts
@@ -0,0 +1,100 @@
+import { afterEach, describe, expect, test } from "bun:test";
+import { createServer, type Server } from "node:http";
+
+import { projectArchiveEngineReads, slimEngineMessage, type EngineTarget } from "./session-upload-observer.js";
+import { completedTurnCount, promptTurnAt } from "./session-archive/lifecycle.js";
+
+// A turn over the observer's 8 MiB message budget (a package install: the
+// user message's diff summary carries every .venv / node_modules file) keeps
+// its turn states: the prompt is read whole, the dependency files leave the
+// diff summary, and a diff still too large loses its texts with a mark.
+
+const MiB = 1024 * 1024;
+const blob = (bytes: number, seed: string) => `${seed}:`.padEnd(bytes, "x");
+
+type Message = { info: Record<string, unknown> & { id: string }; parts: unknown[] };
+
+function bigPrompt(id: string, text: string, files: Array<{ file: string; bytes: number }>): Message {
+ return {
+ info: {
+ id,
+ sessionID: "ses_big",
+ role: "user",
+ time: { created: 1 },
+ summary: { diffs: files.map(({ file, bytes }) => ({ file, before: "", after: blob(bytes, file), additions: 1, deletions: 0, status: "added" })) },
+ },
+ parts: [{ type: "text", text }],
+ };
+}
+
+const reply = (id: string): Message => ({ info: { id, role: "assistant", finish: "stop", time: { created: 2, completed: 3 } }, parts: [{ type: "text", text: "installed" }, { type: "step-finish" }] });
+
+let server: Server | null = null;
+afterEach(() => {
+ server?.close();
+ server = null;
+});
+
+/** The engine's paged message list: newest last, `limit`/`before`, x-next-cursor = the oldest id of the page. */
+async function pagedEngine(messages: Message[]): Promise<EngineTarget> {
+ server = createServer((req, res) => {
+ const url = new URL(req.url ?? "/", "http://engine");
+ const headers: Record<string, string> = { "content-type": "application/json" };
+ if (!url.pathname.endsWith("/message")) {
+ res.writeHead(200, headers);
+ res.end(JSON.stringify({ id: "ses_big" }));
+ return;
+ }
+ const limit = Number(url.searchParams.get("limit") ?? messages.length);
+ const before = url.searchParams.get("before");
+ const end = before ? messages.findIndex((message) => message.info.id === before) : messages.length;
+ const page = messages.slice(Math.max(0, end - limit), end);
+ if (end - limit > 0) headers["x-next-cursor"] = page[0]!.info.id;
+ res.writeHead(200, headers);
+ res.end(JSON.stringify(page));
+ });
+ await new Promise<void>((resolve) => server!.listen(0, "127.0.0.1", resolve));
+ const address = server.address();
+ const port = typeof address === "object" && address ? address.port : 0;
+ return { baseUrl: `http://127.0.0.1:${port}`, headers: [], search: "", engine: "v1" } as EngineTarget;
+}
+
+describe("a turn over the message budget", () => {
+ test("a package install turn over 8 MiB keeps its prompt and its turn count", async () => {
+ const prompt = bigPrompt("u2", "install the deps", [
+ { file: "src/app.py", bytes: 200 },
+ ...Array.from({ length: 12 }, (_, index) => ({ file: `.venv/lib/python3.11/site-packages/pkg${index}/core.py`, bytes: MiB })),
+ { file: "web/node_modules/left-pad/index.js", bytes: MiB },
+ ]);
+ expect(Buffer.byteLength(JSON.stringify(prompt))).toBeGreaterThan(12 * MiB);
+ const target = await pagedEngine([
+ { info: { id: "u1", role: "user", time: { created: 0 } }, parts: [{ type: "text", text: "hello" }] },
+ reply("a1"),
+ prompt,
+ reply("a2"),
+ ]);
+ const outline = (await projectArchiveEngineReads(target, "ses_big").messages()) as Message[];
+ expect(completedTurnCount(outline)).toBe(2);
+ expect(outline[2]!.info.role).toBe("user");
+ expect(promptTurnAt(outline, 5)).toBe(2);
+ });
+
+ test("a diff summary still over the budget loses its largest texts, marked, and keeps the rest", () => {
+ const message = bigPrompt("u1", "generate data", [
+ { file: "data/big.csv", bytes: 9 * MiB },
+ { file: "src/small.ts", bytes: 100 },
+ { file: "node_modules/x/index.js", bytes: 10 },
+ ]);
+ const slim = slimEngineMessage(message) as Message & { info: { summary: { diffs: Array<Record<string, unknown>>; diffs_truncated?: number; diffs_omitted?: number } } };
+ expect(Buffer.byteLength(JSON.stringify(slim))).toBeLessThanOrEqual(8 * MiB);
+ const diffs = slim.info.summary.diffs;
+ expect(diffs.map((entry) => entry.file)).toEqual(["data/big.csv", "src/small.ts"]);
+ expect(diffs[0]!.text_omitted).toBe(true);
+ expect(diffs[0]!.after).toBeUndefined();
+ expect((diffs[1]!.after as string).length).toBe(100);
+ expect(slim.info.summary.diffs_truncated).toBe(1);
+ expect(slim.info.summary.diffs_omitted).toBe(1);
+ const small = bigPrompt("u3", "hi", [{ file: "a.txt", bytes: 10 }]);
+ expect(slimEngineMessage(small)).toBe(small);
+ });
+});
diff --git a/apps/server/src/session-upload-observer.ts b/apps/server/src/session-upload-observer.ts
index b499864d..c4937ca6 100644
--- a/apps/server/src/session-upload-observer.ts
+++ b/apps/server/src/session-upload-observer.ts
@@ -241,6 +241,8 @@ const MAX_TURN_MESSAGE_BYTES = 8 * 1024 * 1024;
* turns the observer never saw settle, goes out over several flushes.
*/
const MAX_BACKLOG_MESSAGE_BYTES = 4 * MAX_TURN_MESSAGE_BYTES;
+/** A message over the read cap is asked for alone and read up to this, then slimmed (slimEngineMessage). */
+const MAX_SINGLE_MESSAGE_READ_BYTES = 64 * 1024 * 1024;
/** The longest an observer follows one turn: a safety bound far past any real turn. */
const MAX_OBSERVED_TURN_MS = 24 * 60 * 60_000;
/** Status reads are a second apart for a turn's first two minutes, then a second more for every minute it has run, up to this. */
@@ -480,6 +482,93 @@ function leadingMessageInfo(chunks: Uint8Array[]): Record<string, unknown> | nul
return null;
}
+/**
+ * The info of a message whose info itself did not fit the bytes read (a
+ * user message's diff summary of many files): its leading id, session and
+ * role, enough to count it as the turn's prompt. Null without a role.
+ */
+function leadingInfoFields(chunks: Uint8Array[]): Record<string, unknown> | null {
+ let head = "";
+ for (const chunk of chunks) {
+ head += Buffer.from(chunk).toString("utf8");
+ if (head.length >= 65_536) break;
+ }
+ const info = /^\s*\[\s*\{\s*"info"\s*:\s*\{/.exec(head);
+ if (!info) return null;
+ const rest = head.slice(info[0].length);
+ const field = (key: string) => new RegExp(`"${key}"\\s*:\\s*"([^"\\\\]{1,200})"`).exec(rest)?.[1];
+ const role = field("role");
+ if (role !== "user" && role !== "assistant") return null;
+ const id = field("id");
+ const sessionID = field("sessionID");
+ return { ...(id ? { id } : {}), ...(sessionID ? { sessionID } : {}), role, truncated: true };
+}
+
+/**
+ * Dependency and cache folders: a user message's diff summary leaves out
+ * the files in them (.venv, node_modules, ...), which the install log and
+ * the workspace snapshots carry. Build output folders stay.
+ */
+const DIFF_DEPENDENCY_DIRS = new Set([
+ ".git", "node_modules", ".venv", "venv", "__pycache__", ".tox", ".nox", ".mypy_cache", ".pytest_cache", ".ruff_cache",
+ ".turbo", ".parcel-cache", ".cache", ".gradle", ".dart_tool", "Pods", "vendor", "bower_components", "site-packages",
+]);
+
+function diffEntryPath(entry: unknown): string | null {
+ if (!isRecord(entry)) return null;
+ for (const key of ["file", "filePath", "path"]) if (typeof entry[key] === "string") return entry[key] as string;
+ return null;
+}
+
+function inDiffDependencyDir(path: string): boolean {
+ return path.replace(/\\/g, "/").split("/").slice(0, -1).some((part) => DIFF_DEPENDENCY_DIRS.has(part));
+}
+
+/**
+ * A message as the observer keeps it: a user message's diff summary
+ * (`info.summary.diffs`, whole-file texts per changed file) without the
+ * files in dependency and cache folders (`summary.diffs_omitted` counts
+ * them); and, for a message still over `maxBytes` (null: no size check),
+ * the largest diff entries lose their texts until it fits
+ * (`summary.diffs_truncated` counts them). The prompt, the role and the ids
+ * stay, so a turn that installed packages reads as any other.
+ */
+export function slimEngineMessage(message: unknown, maxBytes: number | null = MAX_TURN_MESSAGE_BYTES): unknown {
+ if (!isRecord(message) || !isRecord(message.info) || !isRecord(message.info.summary) || !Array.isArray(message.info.summary.diffs)) return message;
+ const record = message;
+ const info = message.info;
+ const summary = message.info.summary;
+ const diffs = summary.diffs as unknown[];
+ const kept = diffs.filter((entry) => {
+ const path = diffEntryPath(entry);
+ return path === null || !inDiffDependencyDir(path);
+ });
+ const omitted = diffs.length - kept.length;
+ const slim = (entries: unknown[], extra: Record<string, unknown>) => ({
+ ...record,
+ info: { ...info, summary: { ...summary, diffs: entries, ...(omitted > 0 ? { diffs_omitted: omitted } : {}), ...extra } },
+ });
+ let result: Record<string, unknown> = omitted > 0 ? slim(kept, {}) : message;
+ if (maxBytes === null) return result;
+ let bytes = Buffer.byteLength(JSON.stringify(result));
+ if (bytes <= maxBytes) return result;
+ const entries = [...kept];
+ const order = entries.map((entry, index) => ({ index, size: Buffer.byteLength(JSON.stringify(entry ?? null)) })).sort((a, b) => b.size - a.size);
+ let truncated = 0;
+ for (const { index, size } of order) {
+ if (bytes <= maxBytes) break;
+ const entry = entries[index];
+ if (!isRecord(entry)) continue;
+ const { before: _before, after: _after, patch: _patch, diff: _diff, ...rest } = entry;
+ const stub = { ...rest, text_omitted: true };
+ entries[index] = stub;
+ bytes -= size - Buffer.byteLength(JSON.stringify(stub));
+ truncated += 1;
+ }
+ result = slim(entries, truncated > 0 ? { diffs_truncated: truncated } : {});
+ return result;
+}
+
/** A message read from the engine; `whole` is false for one known only by its info (too large to read). */
type EngineMessage = { message: unknown; whole: boolean };
@@ -496,7 +585,7 @@ async function* engineMessages(fetchEngine: EngineFetch, path: string, paged: bo
const payload = await readEngineJson(fetchEngine, path);
const list = isRecord(payload) && "data" in payload ? payload.data : payload;
if (!Array.isArray(list)) throw new Error("the engine messages could not be read");
- for (const message of list.reverse()) yield { message, whole: true };
+ for (const message of list.reverse()) yield { message: slimEngineMessage(message, null), whole: true };
return;
}
let ceiling = MESSAGE_PAGE_SIZE;
@@ -511,9 +600,11 @@ async function* engineMessages(fetchEngine: EngineFetch, path: string, paged: bo
const cursor = response.headers.get("x-next-cursor");
let page: EngineMessage[];
try {
- const items = await readObservedResponse(response);
+ // A message alone may be larger than a page: it is read whole, then slimmed under the turn's budget.
+ const alone = limit === 1;
+ const items = await readObservedResponse(response, alone ? MAX_SINGLE_MESSAGE_READ_BYTES : MAX_ENGINE_READ_BYTES);
if (!Array.isArray(items)) throw new Error("the engine messages could not be read");
- page = items.map((message) => ({ message, whole: true }));
+ page = items.map((message) => ({ message: slimEngineMessage(message, alone ? MAX_TURN_MESSAGE_BYTES : null), whole: true }));
} catch (error) {
if (!(error instanceof OversizedEngineResponse)) throw error;
if (limit > 1) {
@@ -521,7 +612,7 @@ async function* engineMessages(fetchEngine: EngineFetch, path: string, paged: bo
limit = 1;
continue;
}
- page = [{ message: { info: leadingMessageInfo(error.chunks) ?? {}, parts: [] }, whole: false }];
+ page = [{ message: { info: leadingMessageInfo(error.chunks) ?? leadingInfoFields(error.chunks) ?? {}, parts: [] }, whole: false }];
}
for (const entry of page.reverse()) yield entry;
if (!cursor || cursor === before) return;