From d718e78bf37fc15d7b38283ed575e66947f4433c Mon Sep 17 00:00:00 2001 From: 3xcalibur Date: Sat, 21 Nov 2026 13:15:25 +0300 Subject: [PATCH 1/3] fix(desktop): agent screenshots and clicks work while T3 Code is hidden or minimized A hidden or minimized window stops drawing its preview guests even with throttling off, so an agent's Page.captureScreenshot never resolves and a click that waits on the next frame times out. While the window draws nothing, a capture or command still pending after 250 ms now prods the guest with Electron's own capturePage until it settles. Co-Authored-By: Claude Opus 5.5 --- .../src/preview/DesktopBrowserHost.test.ts | 52 +++++++++++++++++++ .../desktop/src/preview/DesktopBrowserHost.ts | 46 ++++++++++++++-- apps/desktop/src/preview/Manager.ts | 5 ++ 3 files changed, 98 insertions(+), 5 deletions(-) diff --git a/apps/desktop/src/preview/DesktopBrowserHost.test.ts b/apps/desktop/src/preview/DesktopBrowserHost.test.ts index 8e96129293fc8..bd5abc561d388 100644 --- a/apps/desktop/src/preview/DesktopBrowserHost.test.ts +++ b/apps/desktop/src/preview/DesktopBrowserHost.test.ts @@ -50,6 +50,9 @@ const makeDebuggee = ( readonly deferFocus?: boolean; /** The guest is still loading its first page. */ readonly loading?: boolean; + /** Like a guest of a hidden window: it answers a capture only once Electron captures it. */ + readonly drawsOnlyWhenCaptured?: boolean; + readonly windowDrawing?: () => boolean; readonly refuse?: (method: string, sessionId: string | undefined) => boolean; } = {}, ) => { @@ -90,7 +93,13 @@ const makeDebuggee = ( }), }); let destroyed = false; + let nativeCaptures = 0; const webContents = Object.assign(new NodeEvents.EventEmitter(), { + capturePage: () => { + nativeCaptures += 1; + if (options.drawsOnlyWhenCaptured) pending.splice(0).forEach((resolve) => resolve()); + return Promise.resolve({}); + }, getURL: () => "http://localhost/", getTitle: () => "Page", getUserAgent: () => "Electron", @@ -101,6 +110,7 @@ const makeDebuggee = ( tab: { webContents: webContents as unknown as Electron.WebContents, debugger: debuggee as unknown as Electron.Debugger, + ...(options.windowDrawing ? { windowDrawing: options.windowDrawing } : {}), }, emit: (method: string, params: unknown) => emitter.emit("message", {}, method, params, ""), navigate: () => webContents.emit("did-navigate"), @@ -117,6 +127,7 @@ const makeDebuggee = ( /** Refuses the pending command the debugger received with this id. */ refuse: (id: number) => rejects[sent.indexOf(id)]?.(), focusEmulation, + nativeCaptures: () => nativeCaptures, nextSend: () => new Promise((resolve) => sendWaiters.push(resolve)), release: () => pending.splice(0).forEach((resolve) => resolve()), releaseFirst: () => pending.shift()?.(), @@ -364,6 +375,47 @@ describe("DesktopBrowserHost", () => { }).pipe(Effect.scoped), ); + it.effect("prods a guest its window stopped drawing until what waits on it settles", () => + Effect.gen(function* () { + let drawing = false; + const host = yield* makeDrawnHost; + const debuggee = makeDebuggee({ drawsOnlyWhenCaptured: true, windowDrawing: () => drawing }); + host.attach(key, debuggee.tab); + const replies = yield* watchReplies(host); + + yield* command(host, 1, "Page.captureScreenshot"); + yield* settle; + expect(debuggee.sent).toEqual([1]); + yield* TestClock.adjust("250 millis"); + expect(yield* Queue.take(replies)).toBe(1); + expect(debuggee.nativeCaptures()).toBe(1); + + // A click waits on the guest's next frame the same way. + yield* command(host, 2, "Input.dispatchMouseEvent"); + yield* settle; + yield* TestClock.adjust("250 millis"); + expect(yield* Queue.take(replies)).toBe(2); + expect(debuggee.nativeCaptures()).toBe(2); + + // A wait that settles within a frame is never prodded. + yield* command(host, 3, "Page.captureScreenshot"); + yield* settle; + debuggee.releaseFirst(); + expect(yield* Queue.take(replies)).toBe(3); + yield* TestClock.adjust("1 second"); + expect(debuggee.nativeCaptures()).toBe(2); + + // Nor is a guest whose window draws it, however long the wait. + drawing = true; + yield* command(host, 4, "Page.captureScreenshot"); + yield* settle; + yield* TestClock.adjust("5 seconds"); + expect(debuggee.nativeCaptures()).toBe(2); + debuggee.releaseFirst(); + expect(yield* Queue.take(replies)).toBe(4); + }).pipe(Effect.scoped), + ); + it.effect("stops waiting on a screenshot the page navigated away from", () => Effect.gen(function* () { const host = yield* makeDrawnHost; diff --git a/apps/desktop/src/preview/DesktopBrowserHost.ts b/apps/desktop/src/preview/DesktopBrowserHost.ts index ce5d7074a786c..f19ac2b9db9c4 100644 --- a/apps/desktop/src/preview/DesktopBrowserHost.ts +++ b/apps/desktop/src/preview/DesktopBrowserHost.ts @@ -32,6 +32,10 @@ const POINTER_TRAVEL = new Set(["mouseMoved", "mouseWheel"]); const FOCUS_IDLE = "5 seconds"; /** How long a screenshot waits for the client to draw its guest at full detail. */ const CAPTURE_DRAW_WAIT = "1 second"; +/** Most waits settle within a frame or two of this, so a quick one is never prodded. */ +const PAINT_NUDGE_INTERVAL = "250 millis"; +/** A cold guest can stall Electron's capture; the next prod goes out regardless. */ +const PAINT_NUDGE_TIMEOUT = "2 seconds"; /** Well inside the server's 10 s DESKTOP_ATTACH_TIMEOUT, for a first page that never reports it is up. */ const FIRST_PAGE_TIMEOUT = "5 seconds"; const ERR_ABORTED = -3; @@ -46,6 +50,8 @@ export interface DesktopBrowserTabDebugger { readonly debugger: Electron.Debugger; /** Runs after the server clears the tab's cookies, so state kept beside them goes too. */ readonly cookiesCleared?: () => Promise; + /** Whether the window hosting the guest draws it; one that cannot is prodded to. */ + readonly windowDrawing?: () => boolean; } const keyOf = ({ threadId, tabId }: DesktopBrowserTabKey) => `${threadId}\u0000${tabId}`; @@ -258,6 +264,33 @@ export const make = Effect.gen(function* () { runFork(PubSub.publish(painting, { key: tab.key, active })); }; + /** + * A hidden or minimized window stops drawing its guest even while unthrottled, so a + * capture, or a command that waits on a frame, never settles. Electron's own capture still + * draws one, so a wait that outlasts a frame prods the guest until it settles. + */ + const nudgePaint = ({ webContents, windowDrawing }: DesktopBrowserTabDebugger) => + Effect.sleep(PAINT_NUDGE_INTERVAL).pipe( + Effect.andThen( + Effect.suspend(() => + webContents.isDestroyed() || windowDrawing?.() === true + ? Effect.void + : Effect.promise(() => + webContents.capturePage().then( + () => undefined, + () => undefined, + ), + ).pipe(Effect.timeoutOption(PAINT_NUDGE_TIMEOUT)), + ), + ), + Effect.forever, + ); + + const nudgedUntilSettled = (tab: AttachedTab, work: () => Promise) => { + const nudging = runFork(nudgePaint(tab.debuggee)); + return work().finally(() => runFork(Fiber.interrupt(nudging))); + }; + /** A command may wait on the page's next frame, so the guest stays focused until it settles. */ const holdFocus = (tab: AttachedTab, owner: string | undefined, work: () => Promise) => { const pending = {}; @@ -265,7 +298,7 @@ export const make = Effect.gen(function* () { tab.focusUses += 1; setFocusEmulation(tab, true); releaseFocusWhenIdle(tab); - return work().finally(() => { + return nudgedUntilSettled(tab, work).finally(() => { if (!tab.focusPending.delete(pending)) return; tab.focusUses += 1; releaseFocusWhenIdle(tab); @@ -296,11 +329,14 @@ export const make = Effect.gen(function* () { const sent = {}; tab.capturing.add(sent); syncPainting(tab); + const sendNudged = () => nudgedUntilSettled(tab, send); // Sent at once when already drawn, so it keeps its place among the commands after it. - return (drawnTabs.has(keyOf(tab.key)) ? send() : whenDrawn(tab).then(send)).finally(() => { - tab.capturing.delete(sent); - syncPainting(tab); - }); + return (drawnTabs.has(keyOf(tab.key)) ? sendNudged() : whenDrawn(tab).then(sendNudged)).finally( + () => { + tab.capturing.delete(sent); + syncPainting(tab); + }, + ); }; /** diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts index 531e269a07f11..ad25e4551a5d7 100644 --- a/apps/desktop/src/preview/Manager.ts +++ b/apps/desktop/src/preview/Manager.ts @@ -2579,6 +2579,11 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function webContents: wc, debugger: control.debugger, cookiesCleared: () => runPromise(browserSession.cookiesCleared(wc.session)), + windowDrawing: () => { + const host = wc.hostWebContents; + const window = host && !host.isDestroyed() ? BrowserWindow.fromWebContents(host) : null; + return window !== null && window.isVisible() && !window.isMinimized(); + }, }); // CDP cannot recover a failure that happened before it attached. const { navStatus } = afterAttach; From b2fc8e23941c735af85fe21bf198e857c1722f03 Mon Sep 17 00:00:00 2001 From: 3xcalibur Date: Sat, 21 Nov 2026 16:32:51 +0300 Subject: [PATCH 2/3] fix(desktop): also prod agent captures when T3 Code is covered A window covered by another app stops drawing its guests too, and Electron still reports it as visible, so the isVisible/isMinimized gate skipped exactly the Windows case in #23139. Prod any wait that outlasts a frame, backing off from 250 ms to 2 s so a long wait costs a capture every 2 s at most. Co-Authored-By: Claude Opus 5.5 --- .../src/preview/DesktopBrowserHost.test.ts | 30 ++++++++++++------- .../desktop/src/preview/DesktopBrowserHost.ts | 27 ++++++++++------- apps/desktop/src/preview/Manager.ts | 5 ---- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/apps/desktop/src/preview/DesktopBrowserHost.test.ts b/apps/desktop/src/preview/DesktopBrowserHost.test.ts index bd5abc561d388..252a8e4ee298f 100644 --- a/apps/desktop/src/preview/DesktopBrowserHost.test.ts +++ b/apps/desktop/src/preview/DesktopBrowserHost.test.ts @@ -51,8 +51,7 @@ const makeDebuggee = ( /** The guest is still loading its first page. */ readonly loading?: boolean; /** Like a guest of a hidden window: it answers a capture only once Electron captures it. */ - readonly drawsOnlyWhenCaptured?: boolean; - readonly windowDrawing?: () => boolean; + readonly drawsOnlyWhenCaptured?: () => boolean; readonly refuse?: (method: string, sessionId: string | undefined) => boolean; } = {}, ) => { @@ -97,7 +96,7 @@ const makeDebuggee = ( const webContents = Object.assign(new NodeEvents.EventEmitter(), { capturePage: () => { nativeCaptures += 1; - if (options.drawsOnlyWhenCaptured) pending.splice(0).forEach((resolve) => resolve()); + if (options.drawsOnlyWhenCaptured?.()) pending.splice(0).forEach((resolve) => resolve()); return Promise.resolve({}); }, getURL: () => "http://localhost/", @@ -110,7 +109,6 @@ const makeDebuggee = ( tab: { webContents: webContents as unknown as Electron.WebContents, debugger: debuggee as unknown as Electron.Debugger, - ...(options.windowDrawing ? { windowDrawing: options.windowDrawing } : {}), }, emit: (method: string, params: unknown) => emitter.emit("message", {}, method, params, ""), navigate: () => webContents.emit("did-navigate"), @@ -377,9 +375,9 @@ describe("DesktopBrowserHost", () => { it.effect("prods a guest its window stopped drawing until what waits on it settles", () => Effect.gen(function* () { - let drawing = false; + let drawsWhenCaptured = true; const host = yield* makeDrawnHost; - const debuggee = makeDebuggee({ drawsOnlyWhenCaptured: true, windowDrawing: () => drawing }); + const debuggee = makeDebuggee({ drawsOnlyWhenCaptured: () => drawsWhenCaptured }); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -405,14 +403,24 @@ describe("DesktopBrowserHost", () => { yield* TestClock.adjust("1 second"); expect(debuggee.nativeCaptures()).toBe(2); - // Nor is a guest whose window draws it, however long the wait. - drawing = true; - yield* command(host, 4, "Page.captureScreenshot"); + // A long wait, such as a page awaiting a timer, is prodded less often as it goes on. + drawsWhenCaptured = false; + yield* command(host, 4, "Runtime.evaluate"); yield* settle; - yield* TestClock.adjust("5 seconds"); - expect(debuggee.nativeCaptures()).toBe(2); + for (const [wait, prods] of [ + ["250 millis", 3], + ["500 millis", 4], + ["1 second", 5], + ["2 seconds", 6], + ["2 seconds", 7], + ] as const) { + yield* TestClock.adjust(wait); + expect(debuggee.nativeCaptures()).toBe(prods); + } debuggee.releaseFirst(); expect(yield* Queue.take(replies)).toBe(4); + yield* TestClock.adjust("10 seconds"); + expect(debuggee.nativeCaptures()).toBe(7); }).pipe(Effect.scoped), ); diff --git a/apps/desktop/src/preview/DesktopBrowserHost.ts b/apps/desktop/src/preview/DesktopBrowserHost.ts index f19ac2b9db9c4..431006d22d651 100644 --- a/apps/desktop/src/preview/DesktopBrowserHost.ts +++ b/apps/desktop/src/preview/DesktopBrowserHost.ts @@ -33,7 +33,9 @@ const FOCUS_IDLE = "5 seconds"; /** How long a screenshot waits for the client to draw its guest at full detail. */ const CAPTURE_DRAW_WAIT = "1 second"; /** Most waits settle within a frame or two of this, so a quick one is never prodded. */ -const PAINT_NUDGE_INTERVAL = "250 millis"; +const PAINT_NUDGE_FIRST_MS = 250; +/** A long wait, such as a page awaiting a timer, is prodded less often as it goes on. */ +const PAINT_NUDGE_LONGEST_MS = 2_000; /** A cold guest can stall Electron's capture; the next prod goes out regardless. */ const PAINT_NUDGE_TIMEOUT = "2 seconds"; /** Well inside the server's 10 s DESKTOP_ATTACH_TIMEOUT, for a first page that never reports it is up. */ @@ -50,8 +52,6 @@ export interface DesktopBrowserTabDebugger { readonly debugger: Electron.Debugger; /** Runs after the server clears the tab's cookies, so state kept beside them goes too. */ readonly cookiesCleared?: () => Promise; - /** Whether the window hosting the guest draws it; one that cannot is prodded to. */ - readonly windowDrawing?: () => boolean; } const keyOf = ({ threadId, tabId }: DesktopBrowserTabKey) => `${threadId}\u0000${tabId}`; @@ -265,15 +265,16 @@ export const make = Effect.gen(function* () { }; /** - * A hidden or minimized window stops drawing its guest even while unthrottled, so a - * capture, or a command that waits on a frame, never settles. Electron's own capture still - * draws one, so a wait that outlasts a frame prods the guest until it settles. + * A hidden, minimized or covered window stops drawing its guest even while unthrottled, and + * Electron reports a covered one as visible, so a capture, or a command that waits on a frame, + * can wait forever. Electron's own capture still draws one, so a wait that outlasts a frame + * prods the guest, less often as it goes on. */ - const nudgePaint = ({ webContents, windowDrawing }: DesktopBrowserTabDebugger) => - Effect.sleep(PAINT_NUDGE_INTERVAL).pipe( + const nudgePaint = (webContents: Electron.WebContents, delayMs: number): Effect.Effect => + Effect.sleep(delayMs).pipe( Effect.andThen( Effect.suspend(() => - webContents.isDestroyed() || windowDrawing?.() === true + webContents.isDestroyed() ? Effect.void : Effect.promise(() => webContents.capturePage().then( @@ -283,11 +284,15 @@ export const make = Effect.gen(function* () { ).pipe(Effect.timeoutOption(PAINT_NUDGE_TIMEOUT)), ), ), - Effect.forever, + Effect.andThen( + Effect.suspend(() => + nudgePaint(webContents, Math.min(delayMs * 2, PAINT_NUDGE_LONGEST_MS)), + ), + ), ); const nudgedUntilSettled = (tab: AttachedTab, work: () => Promise) => { - const nudging = runFork(nudgePaint(tab.debuggee)); + const nudging = runFork(nudgePaint(tab.debuggee.webContents, PAINT_NUDGE_FIRST_MS)); return work().finally(() => runFork(Fiber.interrupt(nudging))); }; diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts index ad25e4551a5d7..531e269a07f11 100644 --- a/apps/desktop/src/preview/Manager.ts +++ b/apps/desktop/src/preview/Manager.ts @@ -2579,11 +2579,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function webContents: wc, debugger: control.debugger, cookiesCleared: () => runPromise(browserSession.cookiesCleared(wc.session)), - windowDrawing: () => { - const host = wc.hostWebContents; - const window = host && !host.isDestroyed() ? BrowserWindow.fromWebContents(host) : null; - return window !== null && window.isVisible() && !window.isMinimized(); - }, }); // CDP cannot recover a failure that happened before it attached. const { navStatus } = afterAttach; From df95d5b92e8ae3fe656061cf0e1c0bd359aeee62 Mon Sep 17 00:00:00 2001 From: 3xcalibur Date: Sat, 21 Nov 2026 16:46:43 +0300 Subject: [PATCH 3/3] fix(desktop): prod a tab's waits from one loop that stops when they are dropped Review follow-ups: one nudge loop per tab serves all of its waits, so a held screenshot is no longer prodded twice per tick. A wait stops being prodded once its focus hold or capture is dropped (turn end, new document, relay drop) or the guest detaches. At most two native captures stay in flight per guest, counted across reattaches, since Electron gives a capture no deadline. Co-Authored-By: Claude Opus 5.5 --- .../src/preview/DesktopBrowserHost.test.ts | 45 +++++++++++- .../desktop/src/preview/DesktopBrowserHost.ts | 72 +++++++++++++------ 2 files changed, 91 insertions(+), 26 deletions(-) diff --git a/apps/desktop/src/preview/DesktopBrowserHost.test.ts b/apps/desktop/src/preview/DesktopBrowserHost.test.ts index 252a8e4ee298f..acd255e1e4625 100644 --- a/apps/desktop/src/preview/DesktopBrowserHost.test.ts +++ b/apps/desktop/src/preview/DesktopBrowserHost.test.ts @@ -52,6 +52,8 @@ const makeDebuggee = ( readonly loading?: boolean; /** Like a guest of a hidden window: it answers a capture only once Electron captures it. */ readonly drawsOnlyWhenCaptured?: () => boolean; + /** Electron's capture of this guest never settles. */ + readonly capturesStall?: () => boolean; readonly refuse?: (method: string, sessionId: string | undefined) => boolean; } = {}, ) => { @@ -96,6 +98,7 @@ const makeDebuggee = ( const webContents = Object.assign(new NodeEvents.EventEmitter(), { capturePage: () => { nativeCaptures += 1; + if (options.capturesStall?.()) return new Promise(() => {}); if (options.drawsOnlyWhenCaptured?.()) pending.splice(0).forEach((resolve) => resolve()); return Promise.resolve({}); }, @@ -376,8 +379,12 @@ describe("DesktopBrowserHost", () => { it.effect("prods a guest its window stopped drawing until what waits on it settles", () => Effect.gen(function* () { let drawsWhenCaptured = true; + let capturesStall = false; const host = yield* makeDrawnHost; - const debuggee = makeDebuggee({ drawsOnlyWhenCaptured: () => drawsWhenCaptured }); + const debuggee = makeDebuggee({ + drawsOnlyWhenCaptured: () => drawsWhenCaptured, + capturesStall: () => capturesStall, + }); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -403,9 +410,9 @@ describe("DesktopBrowserHost", () => { yield* TestClock.adjust("1 second"); expect(debuggee.nativeCaptures()).toBe(2); - // A long wait, such as a page awaiting a timer, is prodded less often as it goes on. + // A long wait is prodded less often as it goes on, once per tick though it holds focus too. drawsWhenCaptured = false; - yield* command(host, 4, "Runtime.evaluate"); + yield* command(host, 4, "Page.captureScreenshot"); yield* settle; for (const [wait, prods] of [ ["250 millis", 3], @@ -421,6 +428,38 @@ describe("DesktopBrowserHost", () => { expect(yield* Queue.take(replies)).toBe(4); yield* TestClock.adjust("10 seconds"); expect(debuggee.nativeCaptures()).toBe(7); + + // A wait the agent's turn gave up on is no longer prodded. + yield* command(host, 5, "Runtime.evaluate"); + yield* settle; + yield* TestClock.adjust("250 millis"); + expect(debuggee.nativeCaptures()).toBe(8); + yield* host.handleCommandLine( + encodeJson({ type: "turn", threadId: key.threadId, active: false }), + ); + yield* TestClock.adjust("10 seconds"); + expect(debuggee.nativeCaptures()).toBe(8); + debuggee.releaseFirst(); + expect(yield* Queue.take(replies)).toBe(5); + + // Captures Electron never settles pile up no further than two. + capturesStall = true; + yield* command(host, 6, "Runtime.evaluate"); + yield* settle; + yield* TestClock.adjust("10 seconds"); + expect(debuggee.nativeCaptures()).toBe(10); + debuggee.releaseFirst(); + expect(yield* Queue.take(replies)).toBe(6); + // Even across a reattach of the same guest, as when DevTools closes. + host.detach(key); + host.attach(key, debuggee.tab); + const reattached = yield* watchReplies(host); + yield* command(host, 7, "Runtime.evaluate"); + yield* settle; + yield* TestClock.adjust("10 seconds"); + expect(debuggee.nativeCaptures()).toBe(10); + debuggee.releaseFirst(); + expect(yield* Queue.take(reattached)).toBe(7); }).pipe(Effect.scoped), ); diff --git a/apps/desktop/src/preview/DesktopBrowserHost.ts b/apps/desktop/src/preview/DesktopBrowserHost.ts index 431006d22d651..e23dcee7ccfe9 100644 --- a/apps/desktop/src/preview/DesktopBrowserHost.ts +++ b/apps/desktop/src/preview/DesktopBrowserHost.ts @@ -36,8 +36,8 @@ const CAPTURE_DRAW_WAIT = "1 second"; const PAINT_NUDGE_FIRST_MS = 250; /** A long wait, such as a page awaiting a timer, is prodded less often as it goes on. */ const PAINT_NUDGE_LONGEST_MS = 2_000; -/** A cold guest can stall Electron's capture; the next prod goes out regardless. */ -const PAINT_NUDGE_TIMEOUT = "2 seconds"; +/** Electron's capture has no deadline and a cold guest can stall one, so a second may follow. */ +const PAINT_NUDGE_IN_FLIGHT = 2; /** Well inside the server's 10 s DESKTOP_ATTACH_TIMEOUT, for a first page that never reports it is up. */ const FIRST_PAGE_TIMEOUT = "5 seconds"; const ERR_ABORTED = -3; @@ -79,6 +79,9 @@ interface AttachedTab { readonly focusPending: Map; /** Screenshots Chromium was sent and has not answered; each keeps its wake lock until then. */ readonly capturing: Set; + /** Each wait on the guest's next frame, as whether it still waits; the guest is prodded until none do. */ + readonly frameWaits: Set<() => boolean>; + nudging: Fiber.Fiber | null; /** Whether the client keeps the guest painting, as published on `painting`. */ painting: boolean; /** @@ -270,30 +273,50 @@ export const make = Effect.gen(function* () { * can wait forever. Electron's own capture still draws one, so a wait that outlasts a frame * prods the guest, less often as it goes on. */ - const nudgePaint = (webContents: Electron.WebContents, delayMs: number): Effect.Effect => + /** By guest rather than attachment, since reopening DevTools reattaches the same guest. */ + const nativeCaptures = new WeakMap(); + + const nudgePaint = (tab: AttachedTab, delayMs: number): Effect.Effect => Effect.sleep(delayMs).pipe( Effect.andThen( - Effect.suspend(() => - webContents.isDestroyed() - ? Effect.void - : Effect.promise(() => - webContents.capturePage().then( - () => undefined, - () => undefined, - ), - ).pipe(Effect.timeoutOption(PAINT_NUDGE_TIMEOUT)), - ), - ), - Effect.andThen( - Effect.suspend(() => - nudgePaint(webContents, Math.min(delayMs * 2, PAINT_NUDGE_LONGEST_MS)), - ), + Effect.suspend(() => { + const { webContents } = tab.debuggee; + for (const waits of tab.frameWaits) if (!waits()) tab.frameWaits.delete(waits); + if ( + tab.frameWaits.size === 0 || + tabs.get(keyOf(tab.key)) !== tab || + webContents.isDestroyed() + ) { + tab.nudging = null; + return Effect.void; + } + const inFlight = nativeCaptures.get(webContents) ?? 0; + if (inFlight < PAINT_NUDGE_IN_FLIGHT) { + nativeCaptures.set(webContents, inFlight + 1); + const settled = () => { + nativeCaptures.set(webContents, (nativeCaptures.get(webContents) ?? 1) - 1); + }; + webContents.capturePage().then(settled, settled); + } + return nudgePaint(tab, Math.min(delayMs * 2, PAINT_NUDGE_LONGEST_MS)); + }), ), ); - const nudgedUntilSettled = (tab: AttachedTab, work: () => Promise) => { - const nudging = runFork(nudgePaint(tab.debuggee.webContents, PAINT_NUDGE_FIRST_MS)); - return work().finally(() => runFork(Fiber.interrupt(nudging))); + /** Prods the guest while `work` is pending and `waits` holds; one loop serves all of a tab's waits. */ + const nudgedUntilSettled = ( + tab: AttachedTab, + waits: () => boolean, + work: () => Promise, + ) => { + tab.frameWaits.add(waits); + tab.nudging ??= runFork(nudgePaint(tab, PAINT_NUDGE_FIRST_MS)); + return work().finally(() => { + tab.frameWaits.delete(waits); + if (tab.frameWaits.size > 0 || tab.nudging === null) return; + runFork(Fiber.interrupt(tab.nudging)); + tab.nudging = null; + }); }; /** A command may wait on the page's next frame, so the guest stays focused until it settles. */ @@ -303,7 +326,8 @@ export const make = Effect.gen(function* () { tab.focusUses += 1; setFocusEmulation(tab, true); releaseFocusWhenIdle(tab); - return nudgedUntilSettled(tab, work).finally(() => { + // Dropping the hold, at a turn's end or a new document, stops the prods too. + return nudgedUntilSettled(tab, () => tab.focusPending.has(pending), work).finally(() => { if (!tab.focusPending.delete(pending)) return; tab.focusUses += 1; releaseFocusWhenIdle(tab); @@ -334,7 +358,7 @@ export const make = Effect.gen(function* () { const sent = {}; tab.capturing.add(sent); syncPainting(tab); - const sendNudged = () => nudgedUntilSettled(tab, send); + const sendNudged = () => nudgedUntilSettled(tab, () => tab.capturing.has(sent), send); // Sent at once when already drawn, so it keeps its place among the commands after it. return (drawnTabs.has(keyOf(tab.key)) ? sendNudged() : whenDrawn(tab).then(sendNudged)).finally( () => { @@ -578,6 +602,8 @@ export const make = Effect.gen(function* () { focusUses: 0, focusPending: new Map(), capturing: new Set(), + frameWaits: new Set(), + nudging: null, painting: false, agentUsed: false, focusReleasing: false,