fix(browser): never gate recovery commands on a wake frame

This commit is contained in:
Luke Parker 2026-11-17 10:07:28 +10:00
parent d44e103611
commit bd46fe5006
3 changed files with 30 additions and 9 deletions

View file

@ -1,5 +1,5 @@
import { expect, test } from "bun:test"
import { agentRendering } from "./agent-rendering"
import { agentRendering, needsWakeFrame } from "./agent-rendering"
test("only the last command starts the idle clock; wake before the next command and cancel at disposal", () => {
const lifetime = new AbortController()
@ -41,3 +41,17 @@ test("overlapping commands await the same first frame", async () => {
first[Symbol.dispose]()
second[Symbol.dispose]()
})
test("a missing frame never prevents acting; dialog and navigation bypass even a pending wake", async () => {
const render = agentRendering(new AbortController().signal, () => {}, () => () => {})
const frame = Promise.withResolvers<void>()
const shot = render.hold(() => frame.promise)
expect(needsWakeFrame("dialog", true, true)).toBe(false)
expect(needsWakeFrame("navigate", true, false)).toBe(false)
expect(needsWakeFrame("click", true, true)).toBe(false)
expect(needsWakeFrame("snapshot", false, false)).toBe(false)
expect(needsWakeFrame("evaluate", true, false)).toBe(true)
expect(render.hold().painted).toBeUndefined()
frame.reject(new Error("no frame"))
await expect(shot.painted).resolves.toBeUndefined()
})

View file

@ -1,3 +1,10 @@
import type { Browser } from "@opencode/plugin-browser/rpc"
/** A paused page cannot paint the frame that its dialog or a recovery navigation would unblock. */
export function needsWakeFrame(method: Browser.Method, loaded: boolean, dialog: boolean) {
return loaded && !dialog && ["snapshot", "find", "wait", "evaluate", "click", "check", "fill", "fill_form", "select", "press", "type", "hover", "drag", "scroll", "screenshot"].includes(method)
}
// Long enough to cover thinking between a snapshot and its follow-up; a forgotten tab doesn't animate indefinitely.
const idleMs = 30_000
@ -15,16 +22,15 @@ export function agentRendering(signal: AbortSignal, throttle: (enabled: boolean)
if (!awake) {
throttle(false)
paint = onWake?.()
// Later commands may retry a failed wake; overlapping holders await the same frame.
void paint?.then(() => { paint = undefined }, () => { awake = false; paint = undefined })
paint = onWake?.().catch(() => undefined)
void paint?.then(() => { paint = undefined })
}
awake = true
holds++
return {
painted: paint,
painted: onWake ? paint : undefined,
[Symbol.dispose]() {
if (--holds || signal.aborted) return
cancel = after(idleMs, () => {

View file

@ -3,7 +3,7 @@ import { scheduler } from "node:timers/promises"
import { navigationTurn, replacementNavigation } from "./navigation-turn"
import { accessibilitySnapshot } from "./snapshot"
import { documentRefs } from "./refs"
import { agentRendering } from "./agent-rendering"
import { agentRendering, needsWakeFrame } from "./agent-rendering"
import { extensionResource } from "./profile/extension-api/resource"
import { Browser } from "@opencode/plugin-browser/rpc"
import electron, { type BrowserWindow, type WebContents } from "electron"
@ -1144,12 +1144,13 @@ export function createBrowserPage(
async execute(command: Browser.Command, signal: AbortSignal, origin: "agent" | "user"): Promise<Browser.Result> {
using active = new DisposableStack()
const hidden = !view.getVisible() && !undrawable({ platform: process.platform, visible: win.isVisible(), minimized: win.isMinimized(), appHidden: process.platform === "darwin" && electron.app.isHidden() })
const hidden = !view.getVisible() && win.isVisible() && !win.isMinimized() && !(process.platform === "darwin" && electron.app.isHidden())
const shouldPaint = () => needsWakeFrame(command.action.type, documentState.status === "loaded" && !documentState.busy && !documentState.pending, !!selectOpenDialog(dialogGate))
const hold = origin === "agent" ? active.use(rendering.hold(hidden ? async () => {
const hold = origin === "agent" ? active.use(rendering.hold(hidden && shouldPaint() ? async () => {
await ready
if (closed() || agentRefusal({ type: "snapshot" }, mainDocument())) return
if (!shouldPaint() || agentRefusal({ type: "snapshot" }, mainDocument())) return
await withFrames(false, pageLifetime.signal, async stop => {
const frame = (await cdp.send("Page.getFrameTree")).frameTree.frame.id
const world = await cdp.send("Page.createIsolatedWorld", { frameId: frame, worldName: "opencode-browser-paint" })