@devries-cameron20 / t3code-1 / commits / 2ed69de64e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(desktop): browser snapshots work after relay reconnects (#24829)

Julius Marminge committed Nov 11, 2026 at 16:05 UTC 2ed69de64e25e1599cf1653096cc15719c8c3645
3 files changed +58 -1
apps/desktop/src/preview/CdpRelay.test.ts
+45 -1
@@ -1,6 +1,10 @@
1 import { describe, expect, it, vi } from "vite-plus/test";
2
3 -import { createCdpRelayConnection, type CdpRelayTarget } from "./CdpRelay.ts";
3 +import {
4 + createCdpRelayConnection,
5 + type CdpRelayConnection,
6 + type CdpRelayTarget,
7 +} from "./CdpRelay.ts";
8
9 const makeTarget = (
10 send: CdpRelayTarget["send"],
@@ -354,3 +358,43 @@ describe("CDP relay payload budget", () => {
358 expect(written.map((message) => message["method"])).toEqual([]);
359 });
360 });
361 +
362 +describe("CDP relay reconnects", () => {
363 + it("reports the page's default execution context to every connection", async () => {
364 + // Electron keeps one debugger attached, so Runtime stays enabled after a client drops,
365 + // and the native Runtime.enable announces contexts again only after Runtime.disable.
366 + let runtimeEnabled = false;
367 + let current: CdpRelayConnection | undefined;
368 + const context = {
369 + context: { id: 1, origin: "null", name: "", auxData: { isDefault: true, frameId: "F" } },
370 + };
371 + const send = vi.fn(async (method: string) => {
372 + if (method === "Runtime.disable") runtimeEnabled = false;
373 + if (method === "Runtime.enable" && !runtimeEnabled) {
374 + runtimeEnabled = true;
375 + current!.event("Runtime.executionContextCreated", context, "");
376 + }
377 + return {};
378 + });
379 + const connect = async () => {
380 + const written: Array<Record<string, unknown>> = [];
381 + current = makeRelay(makeTarget(send), (raw) => written.push(JSON.parse(raw)));
382 + current.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach", params: {} }));
383 + await settle();
384 + current.receive(
385 + JSON.stringify({ id: 2, method: "Runtime.enable", sessionId: "t3-preview-page" }),
386 + );
387 + await settle();
388 + return written.filter((m) => m["method"] === "Runtime.executionContextCreated");
389 + };
390 + const expected = [
391 + {
392 + method: "Runtime.executionContextCreated",
393 + params: context,
394 + sessionId: "t3-preview-page",
395 + },
396 + ];
397 + expect(await connect()).toEqual(expected);
398 + expect(await connect()).toEqual(expected);
399 + });
400 +});
apps/desktop/src/preview/CdpRelay.ts
+10
@@ -188,6 +188,8 @@ export function createCdpRelayConnection(
188 }
189 };
190
191 + let runtimeReset: Promise<unknown> | undefined;
192 +
193 const pageCommand = (command: CdpCommand): Promise<unknown> => {
194 const child = sessions.get(command.sessionId!) === "page" ? undefined : command.sessionId;
195 const owner =
@@ -198,6 +200,14 @@ export function createCdpRelayConnection(
200 }
201 // Playwright asks for this on every page; the host emulates focus only while it is in use.
202 if (command.method === "Emulation.setFocusEmulationEnabled") return Promise.resolve({});
203 + // The shared debugger keeps Runtime enabled after an earlier connection, and a repeated
204 + // enable reports no existing contexts; Playwright waits for them, so reset once per connection.
205 + if (command.method === "Runtime.enable" && child === undefined) {
206 + runtimeReset ??= target.send("Runtime.disable", {}, undefined, owner).catch(() => undefined);
207 + return runtimeReset.then(() =>
208 + target.send(command.method, command.params ?? {}, undefined, owner),
209 + );
210 + }
211 return target.send(command.method, command.params ?? {}, child, owner);
212 };
213
apps/desktop/src/preview/DesktopBrowserHost.test.ts
+3
@@ -491,6 +491,9 @@ describe("DesktopBrowserHost", () => {
491 // Playwright re-enabling a domain after a reload is not the agent using the tab.
492 yield* command(host, 1, "Runtime.enable");
493 yield* settle;
494 + // The relay resets Runtime before a connection's first enable: answer the disable, then the enable.
495 + debuggee.release();
496 + yield* settle;
497 debuggee.release();
498 yield* settle;
499 yield* TestClock.adjust("1 minute");