@devries-cameron20 / t3code Mirror · read-only / commits / 4c924ff4e6
Fork 0

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 22, 00:45 UTC

fix(desktop): new browser tabs no longer lose waits, clicks and snapshots for good (#32861)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 10, 2026 at 04:35 UTC 4c924ff4e6d0828e0a5810336ec7d7827d752645
2 files changed +131 -5
apps/desktop/src/preview/DesktopBrowserHost.test.ts
+76
@@ -34,6 +34,8 @@ const command = (
34 const makeDebuggee = (
35 options: {
36 readonly deferFocus?: boolean;
37 + /** The guest is still loading its first page. */
38 + readonly loading?: boolean;
39 readonly refuse?: (method: string, sessionId: string | undefined) => boolean;
40 } = {},
41 ) => {
@@ -69,10 +71,13 @@ const makeDebuggee = (
71 sendWaiters.shift()?.();
72 }),
73 });
74 + let destroyed = false;
75 const webContents = Object.assign(new NodeEvents.EventEmitter(), {
76 getURL: () => "http://localhost/",
77 getTitle: () => "Page",
78 getUserAgent: () => "Electron",
79 + isLoadingMainFrame: () => options.loading === true,
80 + isDestroyed: () => destroyed,
81 });
82 return {
83 tab: {
@@ -81,6 +86,12 @@ const makeDebuggee = (
86 },
87 emit: (method: string, params: unknown) => emitter.emit("message", {}, method, params, ""),
88 navigate: () => webContents.emit("did-navigate"),
89 + domReady: () => webContents.emit("dom-ready"),
90 + destroy: () => {
91 + destroyed = true;
92 + },
93 + failLoad: (code: number) =>
94 + webContents.emit("did-fail-load", {}, code, "Failed", "http://localhost/", true),
95 /** Ids of the commands the debugger received, in order. */
96 sent,
97 focusEmulation,
@@ -144,6 +155,71 @@ describe("DesktopBrowserHost", () => {
155 }),
156 );
157
158 + it.effect("announces a guest still loading its first page only once that page is up", () =>
159 + Effect.gen(function* () {
160 + const host = yield* DesktopBrowserHost.make;
161 + const loading = makeDebuggee({ loading: true });
162 + const other = { ...key, tabId: "tab-2" };
163 + const reader = yield* takeEvents(host, 3).pipe(Effect.forkScoped);
164 +
165 + yield* Effect.yieldNow;
166 + host.attach(key, loading.tab);
167 + host.setShown(key, true);
168 + host.attach(other, makeDebuggee().tab);
169 + loading.domReady();
170 +
171 + expect(yield* Fiber.join(reader)).toEqual([
172 + { type: "attached", ...other },
173 + { type: "attached", ...key },
174 + { type: "shown", ...key, shown: true },
175 + ]);
176 + }),
177 + );
178 +
179 + it.effect(
180 + "keeps a loading guest unannounced across a replaced navigation and a new backend",
181 + () =>
182 + Effect.gen(function* () {
183 + const host = yield* DesktopBrowserHost.make;
184 + const loading = makeDebuggee({ loading: true });
185 + const other = { ...key, tabId: "tab-2" };
186 + host.attach(key, loading.tab);
187 + loading.failLoad(-3);
188 + const reader = yield* takeEvents(host, 2).pipe(Effect.forkScoped);
189 +
190 + yield* Effect.yieldNow;
191 + host.attach(other, makeDebuggee().tab);
192 + loading.domReady();
193 +
194 + expect(yield* Fiber.join(reader)).toEqual([
195 + { type: "attached", ...other },
196 + { type: "attached", ...key },
197 + ]);
198 + }),
199 + );
200 +
201 + it.effect(
202 + "announces a first page that never comes up, but never a tab closed or destroyed first",
203 + () =>
204 + Effect.gen(function* () {
205 + const host = yield* DesktopBrowserHost.make;
206 + const closed = { ...key, tabId: "tab-2" };
207 + const destroyed = { ...key, tabId: "tab-3" };
208 + const destroyedGuest = makeDebuggee({ loading: true });
209 + const reader = yield* takeEvents(host, 1).pipe(Effect.forkScoped);
210 +
211 + yield* Effect.yieldNow;
212 + host.attach(closed, makeDebuggee({ loading: true }).tab);
213 + host.detach(closed);
214 + host.attach(destroyed, destroyedGuest.tab);
215 + destroyedGuest.destroy();
216 + host.attach(key, makeDebuggee({ loading: true }).tab);
217 + yield* TestClock.adjust("5 seconds");
218 +
219 + expect(yield* Fiber.join(reader)).toEqual([{ type: "attached", ...key }]);
220 + }),
221 + );
222 +
223 it.effect("re-sends shown to a backend that starts later and when a tab attaches again", () =>
224 Effect.gen(function* () {
225 const host = yield* DesktopBrowserHost.make;
apps/desktop/src/preview/DesktopBrowserHost.ts
+55 -5
@@ -8,6 +8,7 @@ import {
8 import * as Context from "effect/Context";
9 import * as NodePath from "node:path";
10 import * as Effect from "effect/Effect";
11 +import * as Fiber from "effect/Fiber";
12 import * as Layer from "effect/Layer";
13 import * as Option from "effect/Option";
14 import * as PubSub from "effect/PubSub";
@@ -26,6 +27,9 @@ const AGENT_INPUT_COMMAND =
27 const AGENT_INPUT_METHOD = /^(?:Input\.|Page\.navigate$|Runtime\.(?:evaluate|callFunctionOn)$)/;
28 const AGENT_DOWNLOAD_WINDOW_MS = 5_000;
29 const FOCUS_IDLE = "5 seconds";
30 +/** Well inside the server's 10 s DESKTOP_ATTACH_TIMEOUT, for a first page that never reports it is up. */
31 +const FIRST_PAGE_TIMEOUT = "5 seconds";
32 +const ERR_ABORTED = -3;
33
34 export interface DesktopBrowserTabKey {
35 readonly threadId: string;
@@ -78,6 +82,8 @@ interface AttachedTab {
82 sessionId: string,
83 ) => void;
84 readonly onNewDocument: () => void;
85 + /** Stops waiting for the first page; set until the tab is announced. */
86 + stopWaiting: (() => void) | null;
87 }
88
89 export class DesktopBrowserHost extends Context.Service<
@@ -413,11 +419,13 @@ export const make = Effect.gen(function* () {
419 const tab = tabs.get(id);
420 if (!tab) return;
421 tabs.delete(id);
422 + const announced = tab.stopWaiting === null;
423 + tab.stopWaiting?.();
424 tab.relay = null;
425 tab.debuggee.debugger.off("message", tab.onMessage);
426 tab.debuggee.webContents.off("did-navigate", tab.onNewDocument);
427 syncPainting(tab);
420 - emit({ type: "detached", ...key });
428 + if (announced) emit({ type: "detached", ...key });
429 };
430
431 const attach = (key: DesktopBrowserTabKey, debuggee: DesktopBrowserTabDebugger) => {
@@ -455,13 +463,53 @@ export const make = Effect.gen(function* () {
463 dropFocusHolds(tab);
464 if (tab.screenshotTurn !== null) nextScreenshot(tab);
465 },
466 + stopWaiting: null,
467 };
468 tabs.set(id, tab);
469 debuggee.debugger.on("message", tab.onMessage);
470 debuggee.webContents.on("did-navigate", tab.onNewDocument);
462 - emit({ type: "attached", ...key });
463 - const shown = shownTabs.get(id);
464 - if (shown !== undefined) emit({ type: "shown", ...key, shown });
471 + const announce = () => {
472 + emit({ type: "attached", ...key });
473 + const shown = shownTabs.get(id);
474 + if (shown !== undefined) emit({ type: "shown", ...key, shown });
475 + };
476 + if (!debuggee.webContents.isLoadingMainFrame()) {
477 + announce();
478 + return;
479 + }
480 + // Electron does not pause a guest for the debugger, so Playwright connecting during its first
481 + // commit builds its utility world in the discarded document and every wait on it hangs.
482 + const { webContents } = debuggee;
483 + // ERR_ABORTED is a newer navigation replacing this one, whose commit is still to come.
484 + const onFailLoad = (
485 + _event: Electron.Event,
486 + code: number,
487 + _description: string,
488 + _url: string,
489 + isMainFrame: boolean,
490 + ) => {
491 + if (isMainFrame && code !== ERR_ABORTED) ready();
492 + };
493 + const ready = () => {
494 + if (tab.stopWaiting === null) return;
495 + // The manager's cleanup for a destroyed guest can still be queued behind another tab's attach.
496 + if (webContents.isDestroyed()) return detach(key);
497 + tab.stopWaiting();
498 + announce();
499 + };
500 + const timeout = runFork(
501 + Effect.sleep(FIRST_PAGE_TIMEOUT).pipe(Effect.andThen(Effect.sync(ready))),
502 + );
503 + tab.stopWaiting = () => {
504 + tab.stopWaiting = null;
505 + webContents.off("dom-ready", ready);
506 + webContents.off("did-stop-loading", ready);
507 + webContents.off("did-fail-load", onFailLoad);
508 + runFork(Fiber.interrupt(timeout));
509 + };
510 + webContents.on("dom-ready", ready);
511 + webContents.on("did-stop-loading", ready);
512 + webContents.on("did-fail-load", onFailLoad);
513 };
514
515 const setShown = (key: DesktopBrowserTabKey, shown: boolean | undefined) => {
@@ -472,7 +520,7 @@ export const make = Effect.gen(function* () {
520 }
521 if (shownTabs.get(id) === shown) return;
522 shownTabs.set(id, shown);
475 - if (tabs.has(id)) emit({ type: "shown", ...key, shown });
523 + if (tabs.get(id)?.stopWaiting === null) emit({ type: "shown", ...key, shown });
524 };
525
526 const handleCommandLine = (line: string) =>
@@ -511,6 +559,8 @@ export const make = Effect.gen(function* () {
559 [...tabs.values()],
560 (tab) => {
561 dropRelay(tab);
562 + // Announced once its first page is up.
563 + if (tab.stopWaiting !== null) return Effect.void;
564 const shown = shownTabs.get(keyOf(tab.key));
565 return PubSub.publish(outbox, { type: "attached", ...tab.key }).pipe(
566 Effect.andThen(