From e977eaff6411e47a31f871cbc1d0dd8a74312565 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 24 Nov 2026 06:06:20 +0000 Subject: [PATCH] fix(server): server preview tabs keep rendering when the desktop window stops painting On Wayland a window on a hidden workspace gets no frame callbacks, so a server tab drawn by the desktop's webview never answers a capture: preview_snapshot failed and remote viewers got no frames. A desktop tab whose capture times out now reopens at the same page in the server's headless browser, as a tab the desktop never attached to already does. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/preview/ServerBrowser.test.ts | 40 +++++++++++++ apps/server/src/preview/ServerBrowser.ts | 57 ++++++++++++++++++- apps/server/src/preview/ServerBrowserPage.ts | 9 ++- 3 files changed, 102 insertions(+), 4 deletions(-) diff --git a/apps/server/src/preview/ServerBrowser.test.ts b/apps/server/src/preview/ServerBrowser.test.ts index f0841ca2556d3..8da0df14061a8 100644 --- a/apps/server/src/preview/ServerBrowser.test.ts +++ b/apps/server/src/preview/ServerBrowser.test.ts @@ -978,6 +978,46 @@ it.effect("a tab the desktop does not draw is marked headless, so the desktop st ).pipe(Effect.provide(layer)), ); +it.effect("a desktop tab whose window stops painting moves to headless at the same page", () => + Effect.scoped( + Effect.gen(function* () { + const browser = yield* ServerBrowser.ServerBrowser; + const manager = yield* Manager.PreviewManager; + yield* ready; + desktopRendersNext = true; + desktopPageUrl = "https://example.com/account"; + const { tabId } = yield* manager.open({ + threadId: scope.thread.threadId, + runtime: "server", + url: desktopPageUrl, + }); + yield* manager.reportStatus({ + threadId: scope.thread.threadId, + tabId, + navStatus: { _tag: "Success", url: desktopPageUrl, title: "Account" }, + canGoBack: false, + canGoForward: false, + serverControlled: true, + }); + // A Wayland window on a hidden workspace answers no capture at all. + heldStill = () => new Promise(() => {}); + const stalled = yield* browser.attachViewer(viewerInput(tabId, true)); + let end = yield* Queue.take(stalled.output); + while (end._tag !== "reconnect") end = yield* Queue.take(stalled.output); + heldStill = null; + const { sessions } = yield* manager.list({ threadId: scope.thread.threadId }); + expect(sessions.find((session) => session.tabId === tabId)?.headless).toBe(true); + + yield* browser.attachViewer(viewerInput(tabId, true)); + expect(desktopConnections).toHaveLength(1); + expect(contexts.at(-1)!.page.goto).toHaveBeenCalledWith( + "https://example.com/account", + expect.objectContaining({ waitUntil: "commit" }), + ); + }), + ).pipe(Effect.provide(layer)), +); + it.effect("a server started without the desktop browser channel marks its tabs headless", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/server/src/preview/ServerBrowser.ts b/apps/server/src/preview/ServerBrowser.ts index 8adba6ba3e478..582cd517644ec 100644 --- a/apps/server/src/preview/ServerBrowser.ts +++ b/apps/server/src/preview/ServerBrowser.ts @@ -687,6 +687,46 @@ const make = Effect.gen(function* () { await reopening.catch(constVoid); }; + // A window that stops painting never answers a capture: Wayland sends a window on a hidden + // workspace no frame callbacks, and Electron's own capture cannot wake it. Such a tab moves + // to a headless page, as a tab the desktop never attached to does. + const moveToHeadless = async (tab: ServerTab) => { + const key = tabKey(tab.threadId, tab.tabId); + if (!tab.desktop || tabs.get(key) !== tab || pendingTabs.has(key)) return; + const url = tab.page.url(); + // Dropped before the desktop hears of the move, so its webview closing cannot close the session. + dropTab(tab, false, true); + const moving = (async () => { + await runPromise( + manager.markHeadless({ threadId: tab.threadId, tabId: tab.tabId }).pipe(Effect.ignore), + ); + const { sessions } = await runPromise(manager.list({ threadId: tab.threadId })); + const snapshot = sessions.find( + (session) => session.tabId === tab.tabId && session.runtime === "server", + ); + if (!snapshot) { + throw new ServerBrowserTabNotFoundError({ threadId: tab.threadId, tabId: tab.tabId }); + } + const moved = await createTab(snapshot); + // A new headless page only loads a session that is still loading. + if (moved.initialNavigation === null && moved.crash === undefined && url !== "about:blank") { + moved.initialNavigation = moved.page + .goto(url, { waitUntil: "commit", timeout: NAVIGATION_TIMEOUT_MS }) + .then(constVoid, (cause: unknown) => { + throw ServerBrowserPage.toOperationError(cause, NAVIGATION_TIMEOUT_MS); + }); + if (moved.control.agentId !== null) moved.control.track(moved.initialNavigation); + else void moved.initialNavigation.catch(constVoid); + } + return moved; + })().finally(() => { + pendingTabs.delete(key); + closedPendingTabs.delete(key); + }); + pendingTabs.set(key, { agentId: tab.control.agentId, promise: moving }); + await moving.catch(constVoid); + }; + const dialogStatus = (tab: ServerTab) => tab.dialog ? { @@ -2914,7 +2954,15 @@ const make = Effect.gen(function* () { snapshotInput?.pixelsRequired === false ? ServerBrowserPage.snapshotMetadata(rendering) : ServerBrowserPage.snapshot(rendering), - ); + ).catch((cause: unknown) => { + if (!tab.desktop || !ServerBrowserPage.isNotPainting(cause)) throw cause; + void moveToHeadless(tab); + throw new ServerBrowserPage.ServerBrowserOperationError({ + tag: "PreviewAutomationExecutionError", + message: + "The T3 Code window is not drawing this tab, so it reopened in the server's own browser. Take the snapshot again.", + }); + }); const loadError = loadErrorOf(tab); return loadError ? { ...snapshot, url: loadError.url, loadError } : snapshot; } @@ -3343,9 +3391,14 @@ const make = Effect.gen(function* () { const pushStill = async () => { const data = await withCaptureLock(tab, async () => { let timer: ReturnType | undefined; + const capture = ServerBrowserPage.captureViewport(session, { format: "jpeg", quality }); + // Outlives the race below: the viewer would otherwise wait on a window that never paints. + capture.catch((cause: unknown) => { + if (tab.desktop && ServerBrowserPage.isNotPainting(cause)) void moveToHeadless(tab); + }); try { return await Promise.race([ - ServerBrowserPage.captureViewport(session, { format: "jpeg", quality }), + capture, new Promise((resolve) => { timer = setTimeout(() => resolve(null), VIEWER_STILL_TIMEOUT_MS); }), diff --git a/apps/server/src/preview/ServerBrowserPage.ts b/apps/server/src/preview/ServerBrowserPage.ts index a633f4328d163..76441c4586b6f 100644 --- a/apps/server/src/preview/ServerBrowserPage.ts +++ b/apps/server/src/preview/ServerBrowserPage.ts @@ -548,6 +548,12 @@ const readPageMetrics = async (cdp: CDPSession) => { ); }; +const NOT_PAINTING_MESSAGE = + "The browser is not producing screenshot frames. For a desktop tab, show or restore the T3 Code window and try again."; + +export const isNotPainting = (cause: unknown) => + isServerBrowserOperationError(cause) && cause.message === NOT_PAINTING_MESSAGE; + // Never pass a clip scale: Chromium mirrors the top-left of the page into a `backdrop-filter` // in any capture it shrinks. export const captureViewport = async ( @@ -565,8 +571,7 @@ export const captureViewport = async ( reject( new ServerBrowserOperationError({ tag: "PreviewAutomationExecutionError", - message: - "The browser is not producing screenshot frames. For a desktop tab, show or restore the T3 Code window and try again.", + message: NOT_PAINTING_MESSAGE, }), ), SCREENSHOT_TIMEOUT_MS,