diff --git a/apps/desktop/src/preview/DesktopBrowserHost.test.ts b/apps/desktop/src/preview/DesktopBrowserHost.test.ts index 8e96129293fc..27fade3eefa1 100644 --- a/apps/desktop/src/preview/DesktopBrowserHost.test.ts +++ b/apps/desktop/src/preview/DesktopBrowserHost.test.ts @@ -1,6 +1,6 @@ // @effect-diagnostics nodeBuiltinImport:off - Stands in for an Electron debugger. import { describe, expect, it, vi } from "@effect/vitest"; -import { DesktopBrowserEvent } from "@t3tools/contracts"; +import { DesktopBrowserEvent, PREVIEW_TITLE_WATCH_OBJECT_GROUP } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; import * as Queue from "effect/Queue"; @@ -36,12 +36,13 @@ const command = ( host: DesktopBrowserHost.DesktopBrowserHost["Service"], id: number, method: string, + params: Record = {}, ) => host.handleCommandLine( encodeJson({ type: "cdp", ...key, - message: encodeJson({ id, method, params: { id }, sessionId: "t3-preview-page" }), + message: encodeJson({ id, method, params: { ...params, id }, sessionId: "t3-preview-page" }), }), ); @@ -440,6 +441,24 @@ describe("DesktopBrowserHost", () => { }), ); + it.effect("neither focuses nor paints a guest for the server's pending title watch", () => + Effect.gen(function* () { + const host = yield* makeDrawnHost; + const debuggee = makeDebuggee(); + host.attach(key, debuggee.tab); + const painting = yield* watchPainting(host); + + yield* command(host, 1, "Runtime.evaluate", { + objectGroup: PREVIEW_TITLE_WATCH_OBJECT_GROUP, + }); + yield* settle; + yield* TestClock.adjust("1 minute"); + expect(debuggee.sent).toEqual([1]); + expect(debuggee.focusEmulation).toEqual([]); + expect(painting).toEqual([]); + }).pipe(Effect.scoped), + ); + it.effect("keeps a guest focused while a command it is running is still pending", () => Effect.gen(function* () { const host = yield* makeDrawnHost; @@ -780,6 +799,36 @@ describe("DesktopBrowserHost", () => { }), ); + it.effect("keeps painting while the agent's turn is using the tab", () => + Effect.gen(function* () { + const host = yield* DesktopBrowserHost.make; + const debuggee = makeDebuggee(); + host.attach(key, debuggee.tab); + const painting = yield* watchPainting(host); + const turn = (active: boolean) => + host.handleCommandLine(encodeJson({ type: "turn", threadId: key.threadId, active })); + + yield* turn(true); + // Playwright's actionability check waits for the page's next frame. + yield* command(host, 1, "Runtime.callFunctionOn"); + yield* settle; + expect(painting).toEqual([true]); + + yield* turn(false); + yield* settle; + expect(painting).toEqual([true, false]); + + // Outside a turn the guest paints until the agent goes idle. + yield* command(host, 2, "Input.dispatchMouseEvent"); + yield* settle; + expect(painting).toEqual([true, false, true]); + debuggee.release(); + yield* settle; + yield* TestClock.adjust("1 minute"); + expect(painting).toEqual([true, false, true, false]); + }).pipe(Effect.scoped), + ); + it.effect("lets a guest throttle when Chromium refuses a viewer's screencast", () => Effect.gen(function* () { const host = yield* makeDrawnHost; diff --git a/apps/desktop/src/preview/DesktopBrowserHost.ts b/apps/desktop/src/preview/DesktopBrowserHost.ts index ce5d7074a786..e03dc8c008ac 100644 --- a/apps/desktop/src/preview/DesktopBrowserHost.ts +++ b/apps/desktop/src/preview/DesktopBrowserHost.ts @@ -4,6 +4,7 @@ import { DesktopBrowserCommand, DesktopBrowserEvent, type DesktopBrowserEvent as DesktopBrowserEventType, + PREVIEW_TITLE_WATCH_OBJECT_GROUP, type PreviewAutomationLoadError, } from "@t3tools/contracts"; import * as Context from "effect/Context"; @@ -237,20 +238,22 @@ export const make = Effect.gen(function* () { // The agent pauses to think between steps; its tab stays focused until the turn ends. if (tab.agentUsed && agentTurns.has(tab.key.threadId)) return; tab.agentUsed = false; + syncPainting(tab); if (tabs.get(keyOf(tab.key)) === tab) setFocusEmulation(tab, false); }), ); }; /** - * Electron composites no offscreen guest, so its screenshot or screencast waits for a frame - * that never comes. The client paints the guest behind the app while one is pending; the - * capture then finishes on its own, and the guest goes back offscreen once none is. + * Electron composites no offscreen guest, so a capture, or agent input that waits on the + * next frame, waits forever. The client paints the guest behind the app while one is pending + * or the agent drives it, and the guest goes back offscreen once neither holds. */ const syncPainting = (tab: AttachedTab) => { const active = tabs.get(keyOf(tab.key)) === tab && - (tab.screencasts.size > 0 || + (tab.agentUsed || + tab.screencasts.size > 0 || tab.capturing.size > 0 || tab.waitingScreenshots.some((waiting) => waiting.live())); if (tab.painting === active) return; @@ -382,11 +385,19 @@ export const make = Effect.gen(function* () { syncPainting(tab); if (tab.screencasts.size > 0) return Promise.resolve({}); } + // Pending until the title changes, it would hold focus and painting for as long as the page lives. + if ( + method === "Runtime.evaluate" && + params["objectGroup"] === PREVIEW_TITLE_WATCH_OBJECT_GROUP + ) { + return send(method, params, sessionId); + } // A person's input through a viewer, or Playwright's own housekeeping, is not the agent's. if (AGENT_INPUT_METHOD.test(method)) { if (owner === undefined || !streamingOwners.has(owner)) { tab.agentUsed = true; tab.agentLedAt = performance.now(); + syncPainting(tab); } else if (method.startsWith("Input.") && !POINTER_TRAVEL.has(params["type"])) { tab.agentLedAt = Number.NEGATIVE_INFINITY; } diff --git a/apps/server/src/preview/ServerBrowser.ts b/apps/server/src/preview/ServerBrowser.ts index 9fd94232a5e2..ca0c2e755b15 100644 --- a/apps/server/src/preview/ServerBrowser.ts +++ b/apps/server/src/preview/ServerBrowser.ts @@ -44,6 +44,7 @@ import { type PreviewNavStatus, type PreviewSessionSnapshot, type PreviewViewportSetting, + PREVIEW_TITLE_WATCH_OBJECT_GROUP, ThreadId, SERVER_BROWSER_AUTOMATION_CLIENT_ID, type PreviewAppearancePreference, @@ -808,6 +809,7 @@ const make = Effect.gen(function* () { contextId: watch.contextId, expression: titleChange(title), awaitPromise: true, + objectGroup: PREVIEW_TITLE_WATCH_OBJECT_GROUP, }) .then( ({ exceptionDetails }) => exceptionDetails === undefined, diff --git a/apps/web/src/browser/HostedBrowserWebview.tsx b/apps/web/src/browser/HostedBrowserWebview.tsx index 766f94b59790..edfc5ac5d099 100644 --- a/apps/web/src/browser/HostedBrowserWebview.tsx +++ b/apps/web/src/browser/HostedBrowserWebview.tsx @@ -14,7 +14,7 @@ import { import { usePreviewBridge } from "~/components/preview/usePreviewBridge"; import { useClientSettingsHydrated } from "~/hooks/useSettings"; import { useViewportSize } from "~/hooks/useViewportSize"; -import { cn, isMacPlatform } from "~/lib/utils"; +import { cn } from "~/lib/utils"; import { resolveBrowserSurfacePanelRect, useBrowserSurfaceStore } from "./browserSurfaceStore"; import { useActiveBrowserRecordingTabIds } from "./browserRecording"; @@ -352,8 +352,6 @@ export function HostedBrowserWebview(props: { const wrapperStyle = resolveHostedBrowserWebviewWrapperStyle({ active, renderingActive, - // Electron 43 can permanently blank a macOS webview after `visibility: hidden`. - keepPaintableWhenInactive: isMacPlatform(navigator.platform), cornerRadius: presentation.cornerRadius, zIndex: presentation.zIndex, rect: lastRect, diff --git a/apps/web/src/browser/hostedBrowserWebviewStyle.ts b/apps/web/src/browser/hostedBrowserWebviewStyle.ts index f37c222b08e9..628ff302e1d3 100644 --- a/apps/web/src/browser/hostedBrowserWebviewStyle.ts +++ b/apps/web/src/browser/hostedBrowserWebviewStyle.ts @@ -21,21 +21,12 @@ const HIDDEN_BROWSER_WEBVIEW_OFFSET = -100_000; export function resolveHostedBrowserWebviewWrapperStyle(input: { readonly active: boolean; readonly renderingActive: boolean; - readonly keepPaintableWhenInactive?: boolean; readonly cornerRadius?: number; readonly zIndex?: number; readonly rect: BrowserSurfaceRect | null; readonly hiddenSize: HostedBrowserWebviewSize; }): HostedBrowserWebviewWrapperStyle { - const { - active, - cornerRadius = 0, - hiddenSize, - keepPaintableWhenInactive = false, - rect, - renderingActive, - zIndex = 30, - } = input; + const { active, cornerRadius = 0, hiddenSize, rect, renderingActive, zIndex = 30 } = input; if (active && rect) { return { left: rect.x, @@ -68,6 +59,6 @@ export function resolveHostedBrowserWebviewWrapperStyle(input: { height: hiddenSize.height, zIndex: -1, pointerEvents: "none", - visibility: keepPaintableWhenInactive ? "visible" : "hidden", + visibility: "hidden", }; } diff --git a/packages/contracts/src/desktopBrowser.ts b/packages/contracts/src/desktopBrowser.ts index 1fa572ca662a..346ed05f0741 100644 --- a/packages/contracts/src/desktopBrowser.ts +++ b/packages/contracts/src/desktopBrowser.ts @@ -28,6 +28,12 @@ export const DesktopBrowserEvent = Schema.Union([ ]); export type DesktopBrowserEvent = typeof DesktopBrowserEvent.Type; +/** + * The `objectGroup` of the server's title watch, a `Runtime.evaluate` that stays pending until + * the page's title changes. It only waits, so the desktop host neither focuses nor paints for it. + */ +export const PREVIEW_TITLE_WATCH_OBJECT_GROUP = "t3-preview-title-watch"; + export const DesktopBrowserCommand = Schema.Union([ Schema.Struct({ type: Schema.Literal("cdp"), ...TabKey, message: Schema.String }), Schema.Struct({ type: Schema.Literal("release"), ...TabKey }), diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index 962cfd089dd2..2e2256455bcc 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -619,7 +619,7 @@ export interface DesktopPreviewTabState { /** Chromium reports playback even when audioMuted is true. */ audible: boolean; controller: "human" | "agent" | "none"; - /** A screenshot or stream of the guest is pending, so the client keeps it painting while hidden. */ + /** A screenshot, stream, or agent command needs the guest, so the client keeps it painting while hidden. */ capturing: boolean; favicon?: DesktopPreviewFavicon; updatedAt: string;