Skip to content

Commit 719707c

Browse files
perf(server): save large agent recordings faster (#33930)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 0d51d5b commit 719707c

3 files changed

Lines changed: 124 additions & 34 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ vi.mock("./ServerBrowserContexts.ts", () => ({
6363
return context as unknown as BrowserContext;
6464
}
6565
async scratchPage() {
66-
return makeEncoderPage() as unknown as Page;
66+
return encoderPageOverride ?? (makeEncoderPage() as unknown as Page);
6767
}
6868
async connectDesktopPage(endpoint: string) {
6969
const context = makeContext(undefined, desktopPageUrl);
@@ -377,6 +377,7 @@ function makeEncoderPage() {
377377
}
378378

379379
const contexts: ReturnType<typeof makeContext>[] = [];
380+
let encoderPageOverride: Page | undefined;
380381
const encoderStreams: Array<{ frameRate: number | undefined; bitsPerSecond: number }> = [];
381382
const contextRequests: Array<{ profileId: string; isolated: boolean }> = [];
382383
let contextGate: PromiseWithResolvers<void> | null = null;
@@ -560,6 +561,7 @@ const openDesktopViewer = Effect.gen(function* () {
560561

561562
beforeEach(() => {
562563
contexts.length = 0;
564+
encoderPageOverride = undefined;
563565
encoderStreams.length = 0;
564566
contextRequests.length = 0;
565567
contextGate = null;
@@ -936,6 +938,64 @@ it.live.each([
936938
).pipe(Effect.provide(layer)),
937939
);
938940

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+
939999
it.live.each([
9401000
{ refreshFails: false, expected: "final frame" },
9411001
{ refreshFails: true, expected: "frame" },

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ const HOST_RECONNECT_DELAY = "1 second";
101101
const DESKTOP_ATTACH_TIMEOUT = "10 seconds";
102102
const VIEWER_OUTPUT_LIMIT = 64;
103103
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;
104106
const decodeViewportSetting = Schema.decodeUnknownSync(PreviewViewportSettingSchema);
105107
const AGENT_CURSOR_MOVE_MS = 160;
106108
const AGENT_CURSOR_CLICK_LEAD_MS = 40;
@@ -294,6 +296,7 @@ interface EncoderWindow {
294296
frame(data: string, width: number): Promise<boolean>;
295297
stop(): Promise<{ mimeType: string | null; count: number; bytes: number }>;
296298
chunk(index: number): Promise<string>;
299+
download(): void;
297300
};
298301
}
299302

@@ -1920,8 +1923,6 @@ const make = Effect.gen(function* () {
19201923
message: "No recording is active for this tab.",
19211924
});
19221925
}
1923-
let mimeType: string | null;
1924-
const chunks: Array<Buffer> = [];
19251926
try {
19261927
await recording.session.send("Page.stopScreencast").catch(constVoid);
19271928
await Promise.all(recording.framesInFlight);
@@ -1945,50 +1946,72 @@ const make = Effect.gen(function* () {
19451946
.catch(constVoid);
19461947
}
19471948
}
1948-
const stopped = await recording.encoder.evaluate(() =>
1949+
const { mimeType, count, bytes } = await recording.encoder.evaluate(() =>
19491950
(globalThis as unknown as EncoderWindow).__t3Recorder.stop(),
19501951
);
1951-
mimeType = stopped.mimeType;
19521952
// 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) {
19541954
throw new ServerBrowserPage.ServerBrowserOperationError({
19551955
tag: "PreviewAutomationRecordingTooLargeError",
19561956
message: "The recording is larger than the attachment limit.",
19571957
});
19581958
}
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+
});
19651964
}
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+
};
19662011
} finally {
19672012
await recording.encoder.close().catch(constVoid);
19682013
tab.recording = null;
19692014
}
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-
};
19922015
});
19932016

19942017
let pointerSequence = 0;

‎apps/server/src/preview/ServerBrowserPage.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,11 +1165,18 @@ export const recordingEncoderScript = (frameRate: BrowserRecordingFrameRate) =>
11651165
},
11661166
async chunk(index) {
11671167
const bytes = new Uint8Array(await chunks[index].arrayBuffer());
1168+
if (typeof bytes.toBase64 === "function") return bytes.toBase64();
11681169
let binary = "";
11691170
for (let offset = 0; offset < bytes.length; offset += 0x8000) {
11701171
binary += String.fromCharCode(...bytes.subarray(offset, offset + 0x8000));
11711172
}
11721173
return btoa(binary);
11731174
},
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+
},
11741181
};
11751182
})()`;

0 commit comments

Comments
 (0)