Skip to content

Commit 6e64358

Browse files
Yuruzuucodex
andcommitted
fix(server): keep the last page state in agent recordings
Co-authored-by: Codex <[email protected]>
1 parent 20c3dd3 commit 6e64358

3 files changed

Lines changed: 90 additions & 4 deletions

File tree

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

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,31 +256,64 @@ function makeContext(onClose?: (context: BrowserContext) => void, initialUrl = "
256256

257257
// Runs the real encoder script against fake canvas and MediaRecorder globals.
258258
function makeEncoderPage() {
259+
let paintedFrame = "";
259260
const canvas = {
260261
width: 0,
261262
height: 0,
262-
getContext: () => ({ drawImage() {}, fillRect() {} }),
263-
captureStream: (frameRate?: number) => ({ frameRate, getTracks: () => [] }),
263+
getContext: () => ({
264+
drawImage(bitmap: { pixels: string }) {
265+
paintedFrame = bitmap.pixels;
266+
},
267+
fillRect() {},
268+
}),
269+
captureStream: (frameRate?: number) => ({
270+
frameRate,
271+
getTracks: () => [],
272+
getVideoTracks: () => [{ requestFrame() {} }],
273+
}),
274+
};
275+
const video = {
276+
play: async () => {},
277+
pause() {},
278+
requestVideoFrameCallback: (callback: () => void) => queueMicrotask(callback),
264279
};
265280
class FakeMediaRecorder {
266281
static isTypeSupported = (type: string) => type === "video/webm";
282+
mimeType = "video/webm";
283+
state = "inactive";
267284
onstart: (() => void) | null = null;
285+
onstop: (() => void) | null = null;
286+
ondataavailable: ((event: { data: Blob }) => void) | null = null;
287+
readonly stream: { frameRate?: number };
268288
constructor(stream: { frameRate?: number }, options: { videoBitsPerSecond: number }) {
289+
this.stream = stream;
269290
encoderStreams.push({
270291
frameRate: stream.frameRate,
271292
bitsPerSecond: options.videoBitsPerSecond,
272293
});
273294
}
274295
start() {
296+
this.state = "recording";
275297
this.onstart?.();
276298
}
299+
stop() {
300+
this.state = "inactive";
301+
this.ondataavailable?.({ data: new Blob([paintedFrame]) });
302+
this.onstop?.();
303+
}
277304
}
278305
const sandbox: Record<string, unknown> = {
279-
document: { createElement: () => canvas },
306+
document: { createElement: (tag: string) => (tag === "video" ? video : canvas) },
280307
Path2D: function Path2D() {},
281308
MediaRecorder: FakeMediaRecorder,
282-
createImageBitmap: async () => ({ width: 1280, height: 800, close() {} }),
309+
createImageBitmap: async (blob: Blob) => ({
310+
width: 1280,
311+
height: 800,
312+
pixels: await blob.text(),
313+
close() {},
314+
}),
283315
atob,
316+
btoa,
284317
Blob,
285318
performance,
286319
};
@@ -818,6 +851,37 @@ it.live.each([
818851
).pipe(Effect.provide(layer)),
819852
);
820853

854+
it.live.each([
855+
{ refreshFails: false, expected: "final frame" },
856+
{ refreshFails: true, expected: "frame" },
857+
])(
858+
"saves the last page state when stopping a recording ($refreshFails)",
859+
({ refreshFails, expected }) =>
860+
Effect.scoped(
861+
Effect.gen(function* () {
862+
const { broker, tabId } = yield* ready;
863+
const fs = yield* FileSystem.FileSystem;
864+
yield* broker.invoke({ scope, tabId, operation: "recordingStart", input: {} });
865+
866+
const cdp = contexts[0]!.sessions[0]!;
867+
const send = cdp.send.getMockImplementation()!;
868+
cdp.send.mockImplementation((method, input) => {
869+
if (method !== "Page.captureScreenshot") return send(method, input);
870+
return refreshFails
871+
? Promise.reject(new Error("Page cannot capture a frame"))
872+
: Promise.resolve({ data: Buffer.from("final frame").toString("base64") });
873+
});
874+
const artifact = yield* broker.invoke<{ path: string }>({
875+
scope,
876+
tabId,
877+
operation: "recordingStop",
878+
input: {},
879+
});
880+
expect(yield* fs.readFileString(artifact.path)).toBe(expected);
881+
}),
882+
).pipe(Effect.provide(layer)),
883+
);
884+
821885
it.live("enforces provider ownership and explicit targets when a session has multiple tabs", () =>
822886
Effect.scoped(
823887
Effect.gen(function* () {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,25 @@ const make = Effect.gen(function* () {
18241824
await recording.session.send("Page.stopScreencast").catch(constVoid);
18251825
await Promise.all(recording.framesInFlight);
18261826
await recording.session.detach().catch(constVoid);
1827+
// A screencast can miss the last paint when the page goes idle.
1828+
if (!tab.closing && !tab.dialog) {
1829+
const finalFrame = await ServerBrowserPage.captureFinalRecordingFrame(
1830+
tab.cdp,
1831+
RECORDING_SCREENCAST.quality,
1832+
);
1833+
if (finalFrame !== undefined) {
1834+
await recording.encoder
1835+
.evaluate(
1836+
([data, width]) =>
1837+
(globalThis as unknown as EncoderWindow).__t3Recorder.frame(data, width),
1838+
[
1839+
finalFrame,
1840+
tab.page.viewportSize()?.width ?? UNATTACHED_FILL_VIEWPORT.width,
1841+
] as const,
1842+
)
1843+
.catch(constVoid);
1844+
}
1845+
}
18271846
const stopped = await recording.encoder.evaluate(() =>
18281847
(globalThis as unknown as EncoderWindow).__t3Recorder.stop(),
18291848
);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,9 @@ export const captureViewport = async (
477477
return data;
478478
};
479479

480+
export const captureFinalRecordingFrame = (cdp: CDPSession, quality: number) =>
481+
answered(captureViewport(cdp, { format: "jpeg", quality }));
482+
480483
/** Runs in the page's own isolated world, as the server has no image library. */
481484
const SHRINK_PNG = `async function (png, width, height) {
482485
const bytes = Uint8Array.from(atob(png), (char) => char.charCodeAt(0));

0 commit comments

Comments
 (0)