The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 17:15 UTC
fix(server): agent recordings keep up with the page instead of the encoder (#36215)
Julius Marminge committed
Nov 13, 2026 at 08:49 UTC
759e649ea8b306ae3ea8f97ed14e69158204ef8f
3 files changed
+63
-23
apps/server/src/preview/ServerBrowser.test.ts
+32
-6
@@ -360,12 +360,10 @@ function makeEncoderPage() {
360
document: { createElement: (tag: string) => (tag === "video" ? video : canvas) },
361
Path2D: function Path2D() {},
362
MediaRecorder: FakeMediaRecorder,
363
- createImageBitmap: async (blob: Blob) => ({
364
- width: 1280,
365
- height: 800,
366
- pixels: await blob.text(),
367
- close() {},
368
- }),
363
+ createImageBitmap: async (blob: Blob) => {
364
+ await decodeGate?.promise;
365
+ return { width: 1280, height: 800, pixels: await blob.text(), close() {} };
366
+ },
367
atob,
368
btoa,
369
Blob,
@@ -386,6 +384,7 @@ function makeEncoderPage() {
384
const contexts: ReturnType<typeof makeContext>[] = [];
385
let encoderPageOverride: Page | undefined;
386
const encoderStreams: Array<{ frameRate: number | undefined; bitsPerSecond: number }> = [];
387
+let decodeGate: PromiseWithResolvers<void> | null = null;
388
const contextRequests: Array<{ profileId: string; isolated: boolean }> = [];
389
let contextGate: PromiseWithResolvers<void> | null = null;
390
let heldStill: (() => Promise<void>) | null = null;
@@ -571,6 +570,7 @@ beforeEach(() => {
570
contexts.length = 0;
571
encoderPageOverride = undefined;
572
encoderStreams.length = 0;
573
+ decodeGate = null;
574
contextRequests.length = 0;
575
contextGate = null;
576
heldStill = null;
@@ -1077,6 +1077,32 @@ it.effect.each([
1077
).pipe(Effect.provide(layer)),
1078
);
1079
1080
+// Chrome stops sending screencast frames while too many are unacked.
1081
+it.effect("recordings ack screencast frames without waiting for the encoder to decode them", () =>
1082
+ Effect.scoped(
1083
+ Effect.gen(function* () {
1084
+ const { broker, tabId } = yield* ready;
1085
+ yield* broker.invoke({ scope, tabId, operation: "recordingStart", input: {} });
1086
+ const session = contexts[0]!.sessions.find((candidate) =>
1087
+ candidate.send.mock.calls.some(([method]) => method === "Page.startScreencast"),
1088
+ )!;
1089
+ const gate = Promise.withResolvers<void>();
1090
+ decodeGate = gate;
1091
+ yield* Effect.addFinalizer(() => Effect.sync(() => gate.resolve()));
1092
+ for (const sessionId of [1, 2, 3]) {
1093
+ session.emit("Page.screencastFrame", {
1094
+ data: Buffer.from(`frame ${sessionId}`).toString("base64"),
1095
+ sessionId,
1096
+ metadata: { deviceWidth: 1280 },
1097
+ });
1098
+ }
1099
+ expect(
1100
+ session.send.mock.calls.filter(([method]) => method === "Page.screencastFrameAck"),
1101
+ ).toHaveLength(3);
1102
+ }),
1103
+ ).pipe(Effect.provide(layer)),
1104
+);
1105
+
1106
it.effect.each([{ truncated: false }, { truncated: true }])(
1107
"saves a large recording as a binary download and removes its staged file (truncated: $truncated)",
1108
({ truncated }) =>
apps/server/src/preview/ServerBrowser.ts
+29
-16
@@ -1986,24 +1986,37 @@ const make = Effect.gen(function* () {
1986
const opened = await tab.page.context().newCDPSession(tab.page);
1987
session = opened;
1988
const framesInFlight = new Set<Promise<void>>();
1989
+ // Chrome withholds frames while acks are outstanding, so ack on arrival and let a
1990
+ // slow encoder skip to the newest frame instead of throttling the capture.
1991
+ let pendingFrame: readonly [string, number] | null = null;
1992
+ const deliverPending = async () => {
1993
+ while (pendingFrame) {
1994
+ const next = pendingFrame;
1995
+ pendingFrame = null;
1996
+ const accepted = await encoder
1997
+ .evaluate(
1998
+ ([data, width]) =>
1999
+ (globalThis as unknown as EncoderWindow).__t3Recorder.frame(data, width),
2000
+ next,
2001
+ )
2002
+ .catch(() => true);
2003
+ if (!accepted) {
2004
+ pendingFrame = null;
2005
+ await opened.send("Page.stopScreencast").catch(constVoid);
2006
+ }
2007
+ }
2008
+ };
2009
opened.on("Page.screencastFrame", (frame) => {
2010
+ void opened
2011
+ .send("Page.screencastFrameAck", { sessionId: frame.sessionId })
2012
+ .catch(constVoid);
2013
const cssWidth = tab.page.viewportSize()?.width ?? frame.metadata.deviceWidth;
1991
- const delivered: Promise<void> = encoder
1992
- .evaluate(
1993
- ([data, width]) =>
1994
- (globalThis as unknown as EncoderWindow).__t3Recorder.frame(data, width),
1995
- [frame.data, cssWidth] as const,
1996
- )
1997
- .then(async (accepted) => {
1998
- if (!accepted) await opened.send("Page.stopScreencast");
1999
- })
2000
- .catch(constVoid)
2001
- .finally(() => {
2002
- framesInFlight.delete(delivered);
2003
- void opened
2004
- .send("Page.screencastFrameAck", { sessionId: frame.sessionId })
2005
- .catch(constVoid);
2006
- });
2014
+ const delivering = framesInFlight.size > 0;
2015
+ pendingFrame = [frame.data, cssWidth];
2016
+ if (delivering) return;
2017
+ const delivered: Promise<void> = deliverPending().finally(() =>
2018
+ framesInFlight.delete(delivered),
2019
+ );
2020
framesInFlight.add(delivered);
2021
});
2022
await opened.send("Page.startScreencast", RECORDING_SCREENCAST);
apps/server/src/preview/ServerBrowserPage.ts
+2
-1
@@ -1103,7 +1103,8 @@ export const recordingEncoderScript = (frameRate: BrowserRecordingFrameRate) =>
1103
window.__t3Recorder = {
1104
async frame(base64, cssWidth) {
1105
if (tooLarge) return false;
1106
- const bytes = Uint8Array.from(atob(base64), (char) => char.charCodeAt(0));
1106
+ // A per-character atob copy costs ~30 ms on a 3200x1800 frame and throttles recordings.
1107
+ const bytes = Uint8Array.fromBase64(base64);
1108
const bitmap = await createImageBitmap(new Blob([bytes], { type: "image/jpeg" }));
1109
if (tooLarge) { bitmap.close(); return false; }
1110
if (!recorder) {