@devries-cameron20 / t3code-1 / commits / 719707c354
Fork 0

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

perf(server): save large agent recordings faster (#33930)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Andrei committed Nov 13, 2026 at 05:14 UTC 719707c3547c2e5910f15ce3f128160be7915429
3 files changed +124 -34
apps/server/src/preview/ServerBrowser.test.ts
+61 -1
@@ -63,7 +63,7 @@ vi.mock("./ServerBrowserContexts.ts", () => ({
63 return context as unknown as BrowserContext;
64 }
65 async scratchPage() {
66 - return makeEncoderPage() as unknown as Page;
66 + return encoderPageOverride ?? (makeEncoderPage() as unknown as Page);
67 }
68 async connectDesktopPage(endpoint: string) {
69 const context = makeContext(undefined, desktopPageUrl);
@@ -377,6 +377,7 @@ function makeEncoderPage() {
377 }
378
379 const contexts: ReturnType<typeof makeContext>[] = [];
380 +let encoderPageOverride: Page | undefined;
381 const encoderStreams: Array<{ frameRate: number | undefined; bitsPerSecond: number }> = [];
382 const contextRequests: Array<{ profileId: string; isolated: boolean }> = [];
383 let contextGate: PromiseWithResolvers<void> | null = null;
@@ -560,6 +561,7 @@ const openDesktopViewer = Effect.gen(function* () {
561
562 beforeEach(() => {
563 contexts.length = 0;
564 + encoderPageOverride = undefined;
565 encoderStreams.length = 0;
566 contextRequests.length = 0;
567 contextGate = null;
@@ -936,6 +938,64 @@ it.live.each([
938 ).pipe(Effect.provide(layer)),
939 );
940
941 +it.live.each([{ truncated: false }, { truncated: true }])(
942 + "saves a large recording as a binary download and removes its staged file (truncated: $truncated)",
943 + ({ truncated }) =>
944 + Effect.scoped(
945 + Effect.gen(function* () {
946 + const { broker, tabId } = yield* ready;
947 + const fs = yield* FileSystem.FileSystem;
948 + const bytes = 8 * 1024 * 1024;
949 + const payload = Buffer.alloc(bytes, 0x5a);
950 + let stagedPath: string | undefined;
951 + const download = Promise.withResolvers<{ saveAs: (path: string) => Promise<void> }>();
952 + const sandbox = NodeVM.createContext({
953 + __t3Recorder: {
954 + frame: async () => true,
955 + stop: async () => ({ mimeType: "video/webm", count: 1, bytes }),
956 + chunk: async () => {
957 + throw new Error("Large recording used Base64 export");
958 + },
959 + download: () =>
960 + download.resolve({
961 + saveAs: async (path) => {
962 + stagedPath = path;
963 + await NodeFSP.writeFile(path, truncated ? payload.subarray(1) : payload);
964 + },
965 + }),
966 + },
967 + });
968 + encoderPageOverride = {
969 + ...makeContext().page,
970 + evaluate: async (script: string | ((arg: unknown) => unknown), arg?: unknown) =>
971 + typeof script === "function"
972 + ? NodeVM.runInContext(`(${script.toString()})`, sandbox)(arg)
973 + : undefined,
974 + waitForEvent: () => download.promise,
975 + } as unknown as Page;
976 +
977 + yield* broker.invoke({ scope, tabId, operation: "recordingStart", input: {} });
978 + const stop = broker.invoke<{ path: string; sizeBytes: number }>({
979 + scope,
980 + tabId,
981 + operation: "recordingStop",
982 + input: {},
983 + });
984 + if (truncated) {
985 + expect(yield* stop.pipe(Effect.flip)).toMatchObject({
986 + _tag: "PreviewAutomationExecutionError",
987 + });
988 + } else {
989 + const artifact = yield* stop;
990 + expect(artifact.sizeBytes).toBe(bytes);
991 + expect(Buffer.from(yield* fs.readFile(artifact.path)).equals(payload)).toBe(true);
992 + }
993 + expect(stagedPath).toBeDefined();
994 + expect(yield* fs.exists(stagedPath!)).toBe(false);
995 + }),
996 + ).pipe(Effect.provide(layer)),
997 +);
998 +
999 it.live.each([
1000 { refreshFails: false, expected: "final frame" },
1001 { refreshFails: true, expected: "frame" },
apps/server/src/preview/ServerBrowser.ts
+56 -33
@@ -101,6 +101,8 @@ const HOST_RECONNECT_DELAY = "1 second";
101 const DESKTOP_ATTACH_TIMEOUT = "10 seconds";
102 const VIEWER_OUTPUT_LIMIT = 64;
103 const RECORDING_SCREENCAST = { format: "jpeg", quality: 90, everyNthFrame: 1 } as const;
104 +const RECORDING_BINARY_EXPORT_MIN_BYTES = 8 * 1024 * 1024;
105 +const RECORDING_DOWNLOAD_TIMEOUT_MS = 5_000;
106 const decodeViewportSetting = Schema.decodeUnknownSync(PreviewViewportSettingSchema);
107 const AGENT_CURSOR_MOVE_MS = 160;
108 const AGENT_CURSOR_CLICK_LEAD_MS = 40;
@@ -294,6 +296,7 @@ interface EncoderWindow {
296 frame(data: string, width: number): Promise<boolean>;
297 stop(): Promise<{ mimeType: string | null; count: number; bytes: number }>;
298 chunk(index: number): Promise<string>;
299 + download(): void;
300 };
301 }
302
@@ -1920,8 +1923,6 @@ const make = Effect.gen(function* () {
1923 message: "No recording is active for this tab.",
1924 });
1925 }
1923 - let mimeType: string | null;
1924 - const chunks: Array<Buffer> = [];
1926 try {
1927 await recording.session.send("Page.stopScreencast").catch(constVoid);
1928 await Promise.all(recording.framesInFlight);
@@ -1945,50 +1946,72 @@ const make = Effect.gen(function* () {
1946 .catch(constVoid);
1947 }
1948 }
1948 - const stopped = await recording.encoder.evaluate(() =>
1949 + const { mimeType, count, bytes } = await recording.encoder.evaluate(() =>
1950 (globalThis as unknown as EncoderWindow).__t3Recorder.stop(),
1951 );
1951 - mimeType = stopped.mimeType;
1952 // Checked before the transfer so an oversized video never lands in this process.
1953 - if (stopped.bytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES) {
1953 + if (bytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES) {
1954 throw new ServerBrowserPage.ServerBrowserOperationError({
1955 tag: "PreviewAutomationRecordingTooLargeError",
1956 message: "The recording is larger than the attachment limit.",
1957 });
1958 }
1959 - for (let index = 0; index < stopped.count; index += 1) {
1960 - const chunk = await recording.encoder.evaluate(
1961 - (chunkIndex) => (globalThis as unknown as EncoderWindow).__t3Recorder.chunk(chunkIndex),
1962 - index,
1963 - );
1964 - chunks.push(Buffer.from(chunk, "base64"));
1959 + if (!mimeType || bytes === 0) {
1960 + throw new ServerBrowserPage.ServerBrowserOperationError({
1961 + tag: "PreviewAutomationExecutionError",
1962 + message: "The recording captured no frames.",
1963 + });
1964 }
1965 + const extension = mimeType.startsWith("video/mp4") ? "mp4" : "webm";
1966 + const pendingId = `${PENDING_ATTACHMENT_THREAD_SEGMENT}-${NodeCrypto.randomUUID()}-${extension}`;
1967 + const path = NodePath.join(config.attachmentsDir, `${pendingId}.${extension}`);
1968 + await NodeFSP.mkdir(config.attachmentsDir, { recursive: true });
1969 + if (bytes >= RECORDING_BINARY_EXPORT_MIN_BYTES) {
1970 + const stagedPath = `${path}.partial`;
1971 + try {
1972 + const [download] = await Promise.all([
1973 + recording.encoder.waitForEvent("download", {
1974 + timeout: RECORDING_DOWNLOAD_TIMEOUT_MS,
1975 + }),
1976 + recording.encoder.evaluate(() =>
1977 + (globalThis as unknown as EncoderWindow).__t3Recorder.download(),
1978 + ),
1979 + ]);
1980 + await download.saveAs(stagedPath);
1981 + if ((await NodeFSP.stat(stagedPath)).size !== bytes) {
1982 + throw new Error("The recording download is incomplete.");
1983 + }
1984 + await NodeFSP.rename(stagedPath, path);
1985 + } finally {
1986 + await NodeFSP.rm(stagedPath, { force: true }).catch(constVoid);
1987 + }
1988 + } else {
1989 + const chunks: Array<Buffer> = [];
1990 + for (let index = 0; index < count; index += 1) {
1991 + const chunk = await recording.encoder.evaluate(
1992 + (chunkIndex) =>
1993 + (globalThis as unknown as EncoderWindow).__t3Recorder.chunk(chunkIndex),
1994 + index,
1995 + );
1996 + chunks.push(Buffer.from(chunk, "base64"));
1997 + }
1998 + const data = Buffer.concat(chunks);
1999 + if (data.byteLength !== bytes) throw new Error("The recording transfer is incomplete.");
2000 + await NodeFSP.writeFile(path, data);
2001 + }
2002 + return {
2003 + id: pendingId,
2004 + tabId: tab.tabId,
2005 + path,
2006 + mimeType: mimeType.split(";")[0]!,
2007 + sizeBytes: bytes,
2008 + createdAt: new Date().toISOString(),
2009 + uploadedAttachmentId: pendingId,
2010 + };
2011 } finally {
2012 await recording.encoder.close().catch(constVoid);
2013 tab.recording = null;
2014 }
1970 - const data = Buffer.concat(chunks);
1971 - if (!mimeType || data.byteLength === 0) {
1972 - throw new ServerBrowserPage.ServerBrowserOperationError({
1973 - tag: "PreviewAutomationExecutionError",
1974 - message: "The recording captured no frames.",
1975 - });
1976 - }
1977 -
1978 - const extension = mimeType.startsWith("video/mp4") ? "mp4" : "webm";
1979 - const pendingId = `${PENDING_ATTACHMENT_THREAD_SEGMENT}-${NodeCrypto.randomUUID()}-${extension}`;
1980 - const path = NodePath.join(config.attachmentsDir, `${pendingId}.${extension}`);
1981 - await NodeFSP.mkdir(config.attachmentsDir, { recursive: true });
1982 - await NodeFSP.writeFile(path, data);
1983 - return {
1984 - id: pendingId,
1985 - tabId: tab.tabId,
1986 - path,
1987 - mimeType: mimeType.split(";")[0]!,
1988 - sizeBytes: data.byteLength,
1989 - createdAt: new Date().toISOString(),
1990 - uploadedAttachmentId: pendingId,
1991 - };
2015 });
2016
2017 let pointerSequence = 0;
apps/server/src/preview/ServerBrowserPage.ts
+7
@@ -1165,11 +1165,18 @@ export const recordingEncoderScript = (frameRate: BrowserRecordingFrameRate) =>
1165 },
1166 async chunk(index) {
1167 const bytes = new Uint8Array(await chunks[index].arrayBuffer());
1168 + if (typeof bytes.toBase64 === "function") return bytes.toBase64();
1169 let binary = "";
1170 for (let offset = 0; offset < bytes.length; offset += 0x8000) {
1171 binary += String.fromCharCode(...bytes.subarray(offset, offset + 0x8000));
1172 }
1173 return btoa(binary);
1174 },
1175 + download() {
1176 + const link = document.createElement("a");
1177 + link.href = URL.createObjectURL(new Blob(chunks, { type: recorder.mimeType }));
1178 + link.download = "recording";
1179 + link.click();
1180 + },
1181 };
1182 })()`;