Skip to content

Commit da3a091

Browse files
Yuruzuucodex
andcommitted
perf(server): save large agent recordings faster
Co-authored-by: Codex <[email protected]>
1 parent 20c3dd3 commit da3a091

3 files changed

Lines changed: 131 additions & 34 deletions

File tree

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ vi.mock("./ServerBrowserContexts.ts", () => ({
6262
return context as unknown as BrowserContext;
6363
}
6464
async scratchPage() {
65-
return makeEncoderPage() as unknown as Page;
65+
return encoderPageOverride ?? (makeEncoderPage() as unknown as Page);
6666
}
6767
async connectDesktopPage(endpoint: string) {
6868
const context = makeContext(undefined, desktopPageUrl);
@@ -297,6 +297,7 @@ function makeEncoderPage() {
297297
}
298298

299299
const contexts: ReturnType<typeof makeContext>[] = [];
300+
let encoderPageOverride: Page | undefined;
300301
const encoderStreams: Array<{ frameRate: number | undefined; bitsPerSecond: number }> = [];
301302
const contextRequests: Array<{ profileId: string; isolated: boolean }> = [];
302303
let contextGate: PromiseWithResolvers<void> | null = null;
@@ -474,6 +475,7 @@ const openDesktopViewer = Effect.gen(function* () {
474475

475476
beforeEach(() => {
476477
contexts.length = 0;
478+
encoderPageOverride = undefined;
477479
encoderStreams.length = 0;
478480
contextRequests.length = 0;
479481
contextGate = null;
@@ -818,6 +820,71 @@ it.live.each([
818820
).pipe(Effect.provide(layer)),
819821
);
820822

823+
it.live.each([
824+
{ bytes: 5, truncated: false },
825+
{ bytes: 8 * 1024 * 1024, truncated: false },
826+
{ bytes: 8 * 1024 * 1024, truncated: true },
827+
])(
828+
"saves a recording of $bytes bytes and cleans up a truncated download ($truncated)",
829+
({ bytes, truncated }) =>
830+
Effect.scoped(
831+
Effect.gen(function* () {
832+
const { broker, tabId } = yield* ready;
833+
const fs = yield* FileSystem.FileSystem;
834+
const payload = Buffer.alloc(bytes, 0x5a);
835+
const large = bytes > 5;
836+
let stagedPath: string | undefined;
837+
const download = Promise.withResolvers<{ saveAs: (path: string) => Promise<void> }>();
838+
const sandbox = NodeVM.createContext({
839+
__t3Recorder: {
840+
frame: async () => true,
841+
stop: async () => ({ mimeType: "video/webm", count: 1, bytes }),
842+
chunk: async () => {
843+
if (large) throw new Error("Large recording used Base64 export");
844+
return payload.toString("base64");
845+
},
846+
download: () =>
847+
download.resolve({
848+
saveAs: async (path) => {
849+
stagedPath = path;
850+
await NodeFSP.writeFile(path, truncated ? payload.subarray(1) : payload);
851+
},
852+
}),
853+
},
854+
});
855+
encoderPageOverride = {
856+
...makeContext().page,
857+
evaluate: async (script: string | ((arg: unknown) => unknown), arg?: unknown) =>
858+
typeof script === "function"
859+
? NodeVM.runInContext(`(${script.toString()})`, sandbox)(arg)
860+
: undefined,
861+
waitForEvent: () => download.promise,
862+
} as unknown as Page;
863+
864+
yield* broker.invoke({ scope, tabId, operation: "recordingStart", input: {} });
865+
const stop = broker.invoke<{ path: string; sizeBytes: number }>({
866+
scope,
867+
tabId,
868+
operation: "recordingStop",
869+
input: {},
870+
});
871+
if (truncated) {
872+
expect(yield* stop.pipe(Effect.flip)).toMatchObject({
873+
_tag: "PreviewAutomationExecutionError",
874+
});
875+
} else {
876+
const artifact = yield* stop;
877+
expect(artifact.sizeBytes).toBe(bytes);
878+
expect(Buffer.from(yield* fs.readFile(artifact.path)).equals(payload)).toBe(true);
879+
}
880+
if (large) {
881+
expect(stagedPath).toBeDefined();
882+
expect(yield* fs.exists(stagedPath!)).toBe(false);
883+
}
884+
}),
885+
).pipe(Effect.provide(layer)),
886+
);
887+
821888
it.live("enforces provider ownership and explicit targets when a session has multiple tabs", () =>
822889
Effect.scoped(
823890
Effect.gen(function* () {

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

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ const HOST_RECONNECT_DELAY = "1 second";
100100
const DESKTOP_ATTACH_TIMEOUT = "10 seconds";
101101
const VIEWER_OUTPUT_LIMIT = 64;
102102
const RECORDING_SCREENCAST = { format: "jpeg", quality: 90, everyNthFrame: 1 } as const;
103+
const RECORDING_BINARY_EXPORT_MIN_BYTES = 8 * 1024 * 1024;
104+
const RECORDING_DOWNLOAD_TIMEOUT_MS = 5_000;
103105
const decodeViewportSetting = Schema.decodeUnknownSync(PreviewViewportSettingSchema);
104106
const AGENT_CURSOR_MOVE_MS = 160;
105107
const AGENT_CURSOR_CLICK_LEAD_MS = 40;
@@ -279,6 +281,7 @@ interface EncoderWindow {
279281
frame(data: string, width: number): Promise<boolean>;
280282
stop(): Promise<{ mimeType: string | null; count: number; bytes: number }>;
281283
chunk(index: number): Promise<string>;
284+
download(): void;
282285
};
283286
}
284287

@@ -1818,56 +1821,76 @@ const make = Effect.gen(function* () {
18181821
message: "No recording is active for this tab.",
18191822
});
18201823
}
1821-
let mimeType: string | null;
1822-
const chunks: Array<Buffer> = [];
18231824
try {
18241825
await recording.session.send("Page.stopScreencast").catch(constVoid);
18251826
await Promise.all(recording.framesInFlight);
18261827
await recording.session.detach().catch(constVoid);
1827-
const stopped = await recording.encoder.evaluate(() =>
1828+
const { mimeType, count, bytes } = await recording.encoder.evaluate(() =>
18281829
(globalThis as unknown as EncoderWindow).__t3Recorder.stop(),
18291830
);
1830-
mimeType = stopped.mimeType;
18311831
// Checked before the transfer so an oversized video never lands in this process.
1832-
if (stopped.bytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES) {
1832+
if (bytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES) {
18331833
throw new ServerBrowserPage.ServerBrowserOperationError({
18341834
tag: "PreviewAutomationRecordingTooLargeError",
18351835
message: "The recording is larger than the attachment limit.",
18361836
});
18371837
}
1838-
for (let index = 0; index < stopped.count; index += 1) {
1839-
const chunk = await recording.encoder.evaluate(
1840-
(chunkIndex) => (globalThis as unknown as EncoderWindow).__t3Recorder.chunk(chunkIndex),
1841-
index,
1842-
);
1843-
chunks.push(Buffer.from(chunk, "base64"));
1838+
if (!mimeType || bytes === 0) {
1839+
throw new ServerBrowserPage.ServerBrowserOperationError({
1840+
tag: "PreviewAutomationExecutionError",
1841+
message: "The recording captured no frames.",
1842+
});
18441843
}
1844+
const extension = mimeType.startsWith("video/mp4") ? "mp4" : "webm";
1845+
const pendingId = `${PENDING_ATTACHMENT_THREAD_SEGMENT}-${NodeCrypto.randomUUID()}-${extension}`;
1846+
const path = NodePath.join(config.attachmentsDir, `${pendingId}.${extension}`);
1847+
await NodeFSP.mkdir(config.attachmentsDir, { recursive: true });
1848+
if (bytes >= RECORDING_BINARY_EXPORT_MIN_BYTES) {
1849+
const stagedPath = `${path}.partial`;
1850+
try {
1851+
const [download] = await Promise.all([
1852+
recording.encoder.waitForEvent("download", {
1853+
timeout: RECORDING_DOWNLOAD_TIMEOUT_MS,
1854+
}),
1855+
recording.encoder.evaluate(() =>
1856+
(globalThis as unknown as EncoderWindow).__t3Recorder.download(),
1857+
),
1858+
]);
1859+
await download.saveAs(stagedPath);
1860+
if ((await NodeFSP.stat(stagedPath)).size !== bytes) {
1861+
throw new Error("The recording download is incomplete.");
1862+
}
1863+
await NodeFSP.rename(stagedPath, path);
1864+
} finally {
1865+
await NodeFSP.rm(stagedPath, { force: true }).catch(constVoid);
1866+
}
1867+
} else {
1868+
const chunks: Array<Buffer> = [];
1869+
for (let index = 0; index < count; index += 1) {
1870+
const chunk = await recording.encoder.evaluate(
1871+
(chunkIndex) =>
1872+
(globalThis as unknown as EncoderWindow).__t3Recorder.chunk(chunkIndex),
1873+
index,
1874+
);
1875+
chunks.push(Buffer.from(chunk, "base64"));
1876+
}
1877+
const data = Buffer.concat(chunks);
1878+
if (data.byteLength !== bytes) throw new Error("The recording transfer is incomplete.");
1879+
await NodeFSP.writeFile(path, data);
1880+
}
1881+
return {
1882+
id: pendingId,
1883+
tabId: tab.tabId,
1884+
path,
1885+
mimeType: mimeType.split(";")[0]!,
1886+
sizeBytes: bytes,
1887+
createdAt: new Date().toISOString(),
1888+
uploadedAttachmentId: pendingId,
1889+
};
18451890
} finally {
18461891
await recording.encoder.close().catch(constVoid);
18471892
tab.recording = null;
18481893
}
1849-
const data = Buffer.concat(chunks);
1850-
if (!mimeType || data.byteLength === 0) {
1851-
throw new ServerBrowserPage.ServerBrowserOperationError({
1852-
tag: "PreviewAutomationExecutionError",
1853-
message: "The recording captured no frames.",
1854-
});
1855-
}
1856-
1857-
const extension = mimeType.startsWith("video/mp4") ? "mp4" : "webm";
1858-
const pendingId = `${PENDING_ATTACHMENT_THREAD_SEGMENT}-${NodeCrypto.randomUUID()}-${extension}`;
1859-
const path = NodePath.join(config.attachmentsDir, `${pendingId}.${extension}`);
1860-
await NodeFSP.mkdir(config.attachmentsDir, { recursive: true });
1861-
await NodeFSP.writeFile(path, data);
1862-
return {
1863-
id: pendingId,
1864-
tabId: tab.tabId,
1865-
path,
1866-
mimeType: mimeType.split(";")[0]!,
1867-
sizeBytes: data.byteLength,
1868-
createdAt: new Date().toISOString(),
1869-
uploadedAttachmentId: pendingId,
1870-
};
18711894
});
18721895

18731896
let pointerSequence = 0;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,11 +1138,18 @@ export const recordingEncoderScript = (frameRate: BrowserRecordingFrameRate) =>
11381138
},
11391139
async chunk(index) {
11401140
const bytes = new Uint8Array(await chunks[index].arrayBuffer());
1141+
if (typeof bytes.toBase64 === "function") return bytes.toBase64();
11411142
let binary = "";
11421143
for (let offset = 0; offset < bytes.length; offset += 0x8000) {
11431144
binary += String.fromCharCode(...bytes.subarray(offset, offset + 0x8000));
11441145
}
11451146
return btoa(binary);
11461147
},
1148+
download() {
1149+
const link = document.createElement("a");
1150+
link.href = URL.createObjectURL(new Blob(chunks, { type: recorder.mimeType }));
1151+
link.download = "recording";
1152+
link.click();
1153+
},
11471154
};
11481155
})()`;

0 commit comments

Comments
 (0)