Skip to content
TypeScript

fix(server): agent recordings honor the browser frame rate setting (#23895)

Julius Marminge70bd96282cbb
diff --git a/apps/server/src/preview/ServerBrowser.test.ts b/apps/server/src/preview/ServerBrowser.test.ts
index ac4a785f207..2b33f3b3708 100644
--- a/apps/server/src/preview/ServerBrowser.test.ts
+++ b/apps/server/src/preview/ServerBrowser.test.ts
@@ -1,4 +1,5 @@
import * as NodeEvents from "node:events";
+import * as NodeVM from "node:vm";
// @effect-diagnostics-next-line nodeBuiltinImport:off - Playwright download mocks perform real file operations outside the Effect runtime.
import * as NodeFSP from "node:fs/promises";
@@ -6,6 +7,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { it } from "@effect/vitest";
import {
+ type BrowserRecordingFrameRate,
EnvironmentId,
PreviewTabId,
ProviderInstanceId,
@@ -49,7 +51,7 @@ vi.mock("./ServerBrowserContexts.ts", () => ({
return context as unknown as BrowserContext;
}
async scratchPage() {
- return makeContext().page as unknown as Page;
+ return makeEncoderPage() as unknown as Page;
}
async connectDesktopPage(endpoint: string) {
const context = makeContext();
@@ -212,7 +214,50 @@ function makeContext(onClose?: (context: BrowserContext) => void) {
return context;
}
+// Runs the real encoder script against fake canvas and MediaRecorder globals.
+function makeEncoderPage() {
+ const canvas = {
+ width: 0,
+ height: 0,
+ getContext: () => ({ drawImage() {}, fillRect() {} }),
+ captureStream: (frameRate?: number) => ({ frameRate, getTracks: () => [] }),
+ };
+ class FakeMediaRecorder {
+ static isTypeSupported = (type: string) => type === "video/webm";
+ onstart: (() => void) | null = null;
+ constructor(stream: { frameRate?: number }, options: { videoBitsPerSecond: number }) {
+ encoderStreams.push({
+ frameRate: stream.frameRate,
+ bitsPerSecond: options.videoBitsPerSecond,
+ });
+ }
+ start() {
+ this.onstart?.();
+ }
+ }
+ const sandbox: Record<string, unknown> = {
+ document: { createElement: () => canvas },
+ Path2D: function Path2D() {},
+ MediaRecorder: FakeMediaRecorder,
+ createImageBitmap: async () => ({ width: 1280, height: 800, close() {} }),
+ atob,
+ Blob,
+ performance,
+ };
+ sandbox.window = sandbox;
+ const context = NodeVM.createContext(sandbox);
+ return {
+ ...makeContext().page,
+ evaluate: vi.fn(async (script: string | ((arg: unknown) => unknown), arg?: unknown) =>
+ typeof script === "string"
+ ? NodeVM.runInContext(script, context)
+ : NodeVM.runInContext(`(${script.toString()})`, context)(arg),
+ ),
+ };
+}
+
const contexts: ReturnType<typeof makeContext>[] = [];
+const encoderStreams: Array<{ frameRate: number | undefined; bitsPerSecond: number }> = [];
const contextRequests: Array<{ profileId: string; isolated: boolean }> = [];
let contextGate: PromiseWithResolvers<void> | null = null;
let pageCopy = "";
@@ -320,6 +365,7 @@ const viewerInput = (tabId: string, canOperate: boolean) => ({
beforeEach(() => {
contexts.length = 0;
+ encoderStreams.length = 0;
contextRequests.length = 0;
contextGate = null;
contextFailure = null;
@@ -418,6 +464,36 @@ it.live.each([
).pipe(Effect.provide(layer)),
);
+// The fake encoder decodes every frame at 1280x800, so 30 fps hits the bitrate floor.
+it.live.each([
+ { reports: [], frameRate: 30, bitsPerSecond: 2.5e6 },
+ { reports: [30], frameRate: 30, bitsPerSecond: 2.5e6 },
+ { reports: [60], frameRate: 60, bitsPerSecond: 1280 * 800 * 60 * 0.05 },
+ { reports: [60, undefined], frameRate: 60, bitsPerSecond: 1280 * 800 * 60 * 0.05 },
+ { reports: [60, 30], frameRate: 30, bitsPerSecond: 2.5e6 },
+] satisfies ReadonlyArray<{
+ reports: ReadonlyArray<BrowserRecordingFrameRate | undefined>;
+ frameRate: number;
+ bitsPerSecond: number;
+}>)(
+ "agent recordings encode at the last reported frame rate ($reports)",
+ ({ reports, frameRate, bitsPerSecond }) =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const { browser, broker, tabId } = yield* ready;
+ for (const recordingFrameRate of reports) {
+ yield* browser.reportProfiles({
+ profiles: [],
+ defaultProfileId: "default",
+ ...(recordingFrameRate === undefined ? {} : { recordingFrameRate }),
+ });
+ }
+ yield* broker.invoke({ scope, tabId, operation: "recordingStart", input: {} });
+ expect(encoderStreams).toEqual([{ frameRate, bitsPerSecond }]);
+ }),
+ ).pipe(Effect.provide(layer)),
+);
+
it.live("enforces provider ownership and explicit targets when a session has multiple tabs", () =>
Effect.scoped(
Effect.gen(function* () {
diff --git a/apps/server/src/preview/ServerBrowser.ts b/apps/server/src/preview/ServerBrowser.ts
index 5b386e5f079..3544cd172a0 100644
--- a/apps/server/src/preview/ServerBrowser.ts
+++ b/apps/server/src/preview/ServerBrowser.ts
@@ -4,6 +4,7 @@
import {
BUILT_IN_BROWSER_PROFILES,
DEFAULT_BROWSER_PROFILE_ID,
+ DEFAULT_BROWSER_RECORDING_FRAME_RATE,
FILL_PREVIEW_VIEWPORT,
findBrowserProfile,
INCOGNITO_BROWSER_PROFILE_ID,
@@ -1313,9 +1314,13 @@ const make = Effect.gen(function* () {
readonly profiles: ReadonlyArray<BrowserProfile>;
readonly defaultProfileId: string;
} | null = null;
+ // Agent recordings encode here, so they need the client's Browser recording frame rate. The
+ // last report outlives the client, which lets agents record while no client is connected.
+ let recordingFrameRate = DEFAULT_BROWSER_RECORDING_FRAME_RATE;
const reportProfiles: ServerBrowser["Service"]["reportProfiles"] = (input) =>
Effect.sync(() => {
+ if (input.recordingFrameRate !== undefined) recordingFrameRate = input.recordingFrameRate;
const profiles = resolveBrowserProfiles(input.profiles);
reportedProfiles = {
profiles,
@@ -1472,7 +1477,7 @@ const make = Effect.gen(function* () {
const encoder = await contexts.scratchPage();
let session: CDPSession | null = null;
try {
- await encoder.evaluate(ServerBrowserPage.RECORDING_ENCODER_SCRIPT);
+ await encoder.evaluate(ServerBrowserPage.recordingEncoderScript(recordingFrameRate));
// Seed idle pages before returning so an immediate stop has a frame to encode.
const firstFrame = await ServerBrowserPage.captureViewport(tab.page, tab.cdp, {
format: "jpeg",
diff --git a/apps/server/src/preview/ServerBrowserPage.ts b/apps/server/src/preview/ServerBrowserPage.ts
index 02089cca320..23550292583 100644
--- a/apps/server/src/preview/ServerBrowserPage.ts
+++ b/apps/server/src/preview/ServerBrowserPage.ts
@@ -1,6 +1,7 @@
// @effect-diagnostics nodeBuiltinImport:off globalDate:off globalTimers:off - Playwright callbacks run outside the Effect runtime.
import {
+ type BrowserRecordingFrameRate,
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
type PreviewAutomationClickInput,
type PreviewAutomationConsoleEntry,
@@ -922,7 +923,7 @@ export const parseNetError = (errorText: string) => {
return { description, code: NET_ERROR_CODES[description] ?? -2 };
};
-export const RECORDING_ENCODER_SCRIPT = `(() => {
+export const recordingEncoderScript = (frameRate: BrowserRecordingFrameRate) => `(() => {
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
const chunks = [];
@@ -991,8 +992,8 @@ export const RECORDING_ENCODER_SCRIPT = `(() => {
canvas.width = bitmap.width;
canvas.height = bitmap.height;
const mimeType = ["video/mp4;codecs=avc1.640033", "video/webm;codecs=vp9", "video/webm"].find((type) => MediaRecorder.isTypeSupported(type));
- const bitsPerSecond = Math.min(50e6, Math.max(2.5e6, bitmap.width * bitmap.height * 30 * 0.05));
- recorder = new MediaRecorder(canvas.captureStream(30), { mimeType, videoBitsPerSecond: bitsPerSecond });
+ const bitsPerSecond = Math.min(50e6, Math.max(2.5e6, bitmap.width * bitmap.height * ${frameRate} * 0.05));
+ recorder = new MediaRecorder(canvas.captureStream(${frameRate}), { mimeType, videoBitsPerSecond: bitsPerSecond });
started = new Promise((resolve, reject) => {
recorder.onstart = resolve;
recorder.onerror = (event) => reject(event.error);
diff --git a/apps/web/src/browser/BrowserProfileReporter.tsx b/apps/web/src/browser/BrowserProfileReporter.tsx
index deef484bf49..1839a35aac2 100644
--- a/apps/web/src/browser/BrowserProfileReporter.tsx
+++ b/apps/web/src/browser/BrowserProfileReporter.tsx
@@ -1,4 +1,8 @@
-import { AuthPreviewOperateScope, type BrowserProfile } from "@t3tools/contracts";
+import {
+ AuthPreviewOperateScope,
+ type BrowserProfile,
+ type BrowserRecordingFrameRate,
+} from "@t3tools/contracts";
import { useEffect } from "react";
import { useClientSettings, useClientSettingsHydrated } from "~/hooks/useSettings";
@@ -12,11 +16,15 @@ const selectProfiles = (settings: { readonly browserProfiles: ReadonlyArray<Brow
settings.browserProfiles;
const selectDefaultProfileId = (settings: { readonly browserDefaultProfileId: string }) =>
settings.browserDefaultProfileId;
+const selectRecordingFrameRate = (settings: {
+ readonly browserRecordingFrameRate: BrowserRecordingFrameRate;
+}) => settings.browserRecordingFrameRate;
export function BrowserProfileReporter() {
const hydrated = useClientSettingsHydrated();
const profiles = useClientSettings(selectProfiles);
const defaultProfileId = useClientSettings(selectDefaultProfileId);
+ const recordingFrameRate = useClientSettings(selectRecordingFrameRate);
const { environments } = useEnvironments();
const serverConfigs = useServerConfigs();
const operable = useEnvironmentsWithScope(environments, AuthPreviewOperateScope);
@@ -26,9 +34,9 @@ export function BrowserProfileReporter() {
if (!hydrated) return;
for (const [environmentId, config] of serverConfigs) {
if (!config.environment.capabilities.serverBrowser || !operable.has(environmentId)) continue;
- void report({ environmentId, input: { profiles, defaultProfileId } });
+ void report({ environmentId, input: { profiles, defaultProfileId, recordingFrameRate } });
}
- }, [defaultProfileId, hydrated, operable, profiles, report, serverConfigs]);
+ }, [defaultProfileId, hydrated, operable, profiles, recordingFrameRate, report, serverConfigs]);
return null;
}
diff --git a/packages/contracts/src/preview.ts b/packages/contracts/src/preview.ts
index 455663155cb..ec34f0ead85 100644
--- a/packages/contracts/src/preview.ts
+++ b/packages/contracts/src/preview.ts
@@ -241,9 +241,16 @@ export const PreviewCloseInput = Schema.Struct({
});
export type PreviewCloseInput = typeof PreviewCloseInput.Type;
+export const BROWSER_RECORDING_FRAME_RATES = [30, 60] as const;
+export const BrowserRecordingFrameRate = Schema.Literals(BROWSER_RECORDING_FRAME_RATES);
+export type BrowserRecordingFrameRate = typeof BrowserRecordingFrameRate.Type;
+export const DEFAULT_BROWSER_RECORDING_FRAME_RATE: BrowserRecordingFrameRate = 30;
+
export const PreviewReportProfilesInput = Schema.Struct({
profiles: Schema.Array(BrowserProfile).check(Schema.isMaxLength(BROWSER_PROFILE_MAX_COUNT)),
defaultProfileId: BrowserProfileId,
+ /** Optional so older clients keep reporting; omitting it keeps the last reported rate. */
+ recordingFrameRate: Schema.optional(BrowserRecordingFrameRate),
});
export type PreviewReportProfilesInput = typeof PreviewReportProfilesInput.Type;
diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts
index 4d55e0e038f..7923879541a 100644
--- a/packages/contracts/src/settings.ts
+++ b/packages/contracts/src/settings.ts
@@ -33,6 +33,8 @@ import { ProjectScript, type ProjectMutation } from "./project.ts";
import { DEFAULT_RUNTIME_MODE, RuntimeMode } from "./providerPolicy.ts";
import { BrowserProfile, BrowserProfileId, DEFAULT_BROWSER_PROFILE_ID } from "./browserProfile.ts";
import {
+ BrowserRecordingFrameRate,
+ DEFAULT_BROWSER_RECORDING_FRAME_RATE,
DEFAULT_PREVIEW_APPEARANCE,
DEFAULT_PREVIEW_ZOOM_FACTOR,
FILL_PREVIEW_VIEWPORT,
@@ -257,10 +259,11 @@ export type DefaultThemePreference = typeof DefaultThemePreference.Type;
export const DEFAULT_BROWSER_VIEWPORT: PreviewViewportSetting = FILL_PREVIEW_VIEWPORT;
export const DEFAULT_BROWSER_AUTO_SHOW_FLOATING_PREVIEW = true;
-export const BROWSER_RECORDING_FRAME_RATES = [30, 60] as const;
-export const BrowserRecordingFrameRate = Schema.Literals(BROWSER_RECORDING_FRAME_RATES);
-export type BrowserRecordingFrameRate = typeof BrowserRecordingFrameRate.Type;
-export const DEFAULT_BROWSER_RECORDING_FRAME_RATE: BrowserRecordingFrameRate = 30;
+export {
+ BROWSER_RECORDING_FRAME_RATES,
+ BrowserRecordingFrameRate,
+ DEFAULT_BROWSER_RECORDING_FRAME_RATE,
+} from "./preview.ts";
export const BrowserLinkTarget = Schema.Literals(["system", "app"]);
export type BrowserLinkTarget = typeof BrowserLinkTarget.Type;
export const DEFAULT_BROWSER_LINK_TARGET: BrowserLinkTarget = "system";