Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(browser): bound screenshot hangs and recover CDP session

ZxlDragonDoctor24babed9d76c
diff --git a/apps/desktop/electron/main/browser-host.ts b/apps/desktop/electron/main/browser-host.ts
index 1644dcd05..268859db2 100644
--- a/apps/desktop/electron/main/browser-host.ts
+++ b/apps/desktop/electron/main/browser-host.ts
@@ -1,4 +1,4 @@
-import type { BrowserWindow } from "electron";
+import type { BrowserWindow, WebContents } from "electron";
import type { BrowserState } from "@pi-desktop/shared";
import type { BrowserPane } from "./browser-view";
import { BrowserCdp } from "./browser-cdp";
@@ -8,6 +8,7 @@ import { join } from "node:path";
export const BROWSER_PLUGIN_ID = "pi.browser";
export const BROWSER_VIEW_ID = "browser";
const BROWSER_REVEAL_TIMEOUT_MS = 5_000;
+const BROWSER_SCREENSHOT_TIMEOUT_MS = 30_000;
export type BrowserRect = {
x: number;
@@ -91,6 +92,31 @@ type BrowserPage = {
navigating: boolean;
};
+function captureScreenshotWithDeadline<T>(
+ page: BrowserPage,
+ capture: (wc: WebContents) => Promise<T>,
+): Promise<T> {
+ return page.pane.captureScreenshot((wc) => {
+ let timeout: ReturnType<typeof setTimeout> | undefined;
+ const timedOut = new Promise<never>((_resolve, reject) => {
+ timeout = setTimeout(() => {
+ const error = Object.assign(
+ new Error(`browser screenshot timed out after ${BROWSER_SCREENSHOT_TIMEOUT_MS}ms`),
+ { code: "TIMEOUT" },
+ );
+ reject(error);
+ try {
+ page.cdp.detach(wc);
+ } catch {
+ // Preserve the timeout result if the guest is already shutting down.
+ }
+ }, BROWSER_SCREENSHOT_TIMEOUT_MS);
+ });
+ return Promise.race([Promise.resolve().then(() => capture(wc)), timedOut])
+ .finally(() => { if (timeout) clearTimeout(timeout); });
+ });
+}
+
/** Host-owned pages, keyed by conversation and resource tab. */
export class BrowserHost {
private readonly deps: BrowserHostDeps;
@@ -430,7 +456,7 @@ export class BrowserHost {
async screenshot(input: { fullPage?: boolean } = {}, sessionId?: string): Promise<{ mimeType: string; data: string; path?: string }> {
const page = this.currentPage();
- const shot = await page.pane.captureScreenshot((wc) => page.cdp.screenshot(wc, input));
+ const shot = await captureScreenshotWithDeadline(page, (wc) => page.cdp.screenshot(wc, input));
const scratch = this.deps.getScratchDir?.(sessionId ?? page.sessionId);
if (!scratch) return shot;
try {
@@ -454,7 +480,7 @@ export class BrowserHost {
async cdpCommand(method: string, params?: unknown): Promise<unknown> {
const page = this.currentPage();
if (method === "Page.captureScreenshot") {
- return page.pane.captureScreenshot((wc) => page.cdp.send(wc, method, params));
+ return captureScreenshotWithDeadline(page, (wc) => page.cdp.send(wc, method, params));
}
return page.cdp.send(this.requireWebContents(), method, params);
}
diff --git a/apps/desktop/test/browser-capture-resize.test.mjs b/apps/desktop/test/browser-capture-resize.test.mjs
index d0fd1e44d..0e0ad2fc5 100644
--- a/apps/desktop/test/browser-capture-resize.test.mjs
+++ b/apps/desktop/test/browser-capture-resize.test.mjs
@@ -172,6 +172,42 @@ test("a failed capture releases resizing and does not poison the next capture",
assert.equal((await next).data, "fixture-image");
});
+for (const raw of [false, true]) {
+ test(`${raw ? "raw CDP" : "browser screenshot"} timeout releases the queue for the next capture`, async (t) => {
+ const { host, wc } = await harness(t);
+ const capture = () => raw
+ ? host.cdpCommand("Page.captureScreenshot", { format: "png" })
+ : host.screenshot();
+ const setTimeout = globalThis.setTimeout;
+ let expire;
+ globalThis.setTimeout = (callback, delay, ...args) => {
+ assert.equal(delay, 30_000);
+ expire = () => callback(...args);
+ return 1;
+ };
+ t.after(() => { globalThis.setTimeout = setTimeout; });
+ const failed = assert.rejects(
+ capture(),
+ (error) => {
+ assert.match(error.message, /browser screenshot timed out after 30000ms/);
+ assert.equal(error.code, "TIMEOUT");
+ return true;
+ },
+ );
+ await once(wc, "capture-start");
+ expire();
+
+ await failed;
+ assert.equal(wc.debugger.isAttached(), false, "detach the stalled CDP session");
+ wc.captures[0].reject(new Error("debugger detached"));
+ const nextStarted = once(wc, "capture-start");
+ const next = capture();
+ await nextStarted;
+ wc.captures[1].resolve();
+ assert.equal((await next).data, "fixture-image");
+ });
+}
+
test("overlapping capture APIs serialize per page, not across sibling tabs", async (t) => {
const { host, resize, wc } = await harness(t);
const started = once(wc, "capture-start");
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index e9d68a16e..cd3e214ae 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -4550,6 +4550,9 @@ window; opening a normal panel afterward must still work.
queued capture to another page. A destroyed guest leaves no orphan native
view when it is recreated or disposed. No screenshot is repeated to repair
layout.
+- **Timeout recovery**: When either screenshot API does not complete within
+ 30 seconds, it fails with `TIMEOUT`, detaches the stalled CDP session, and
+ releases the page queue; the next capture reattaches and can run.
- **Status**: Native Electron capture/resize and pending window-transition
placement path automated by
`node scripts/e2e-browser-capture-resize.mjs` (artifacts retained). The
@@ -9690,6 +9693,7 @@ must keep splitting are covered by `markdown-blocks.test.mjs`.
| A / C — Unicode stdio framing | E2E-RPC-unicode-separators |
| C / G / Quality — Plugins navigation | E2E-NAV-plugins-button-goes-back |
| C / D / Quality — Sidebar row states | E2E-LAYOUT-sidebar-row-states |
+| E / Quality — Browser screenshot timeout recovery | E2E-BROWSER-capture-resize |
| A / C / Quality — Sidebar material and settings return | E2E-LAYOUT-sidebar-settings |
| C / Quality — Destination loading and focus | E2E-087b |
| A / H / Quality — Renderer process crash recovery | E2E-RUNTIME-renderer-crash-recovery |
@@ -9762,7 +9766,7 @@ must keep splitting are covered by `markdown-blocks.test.mjs`.
| M2 | E2E-004, E2E-005, E2E-006, E2E-007, E2E-008, E2E-008d, E2E-008e, E2E-009, E2E-010, E2E-011, E2E-011a, E2E-011b, E2E-011d, E2E-011e, E2E-011g, E2E-020, E2E-021, E2E-021a, E2E-027, E2E-031, E2E-042, E2E-087, E2E-088, E2E-088b, E2E-089, E2E-090, E2E-COMPOSER-narrow-controls, E2E-144, E2E-005J, E2E-201, E2E-202, E2E-207, E2E-206 |
| M3 | E2E-012, E2E-013, E2E-014, E2E-015, E2E-016, E2E-017, E2E-018, E2E-019, E2E-040 |
| M4 | E2E-022, E2E-023, E2E-024, E2E-025, E2E-026, E2E-030, E2E-038 |
-| M5 | E2E-CHAT-long-history-stream-keeps-responsive, E2E-CHAT-running-status-survives-output-pauses, E2E-008a, E2E-032, E2E-033, E2E-034, E2E-039, E2E-043, E2E-044, E2E-045, E2E-046, E2E-047, E2E-048, E2E-048A, E2E-049, E2E-050, E2E-051, E2E-052, E2E-053, E2E-054, E2E-055, E2E-056, E2E-057, E2E-058, E2E-059, E2E-060, E2E-061, E2E-062, E2E-063, E2E-064, E2E-065, E2E-066, E2E-067, E2E-068, E2E-069, E2E-070, E2E-071, E2E-072, E2E-073, E2E-074, E2E-075, E2E-076, E2E-077, E2E-078, E2E-079, E2E-080, E2E-081, E2E-082, E2E-083, E2E-084, E2E-085, E2E-086, E2E-092, E2E-093, E2E-096, E2E-097, E2E-098, E2E-099, E2E-100, E2E-101, E2E-102, E2E-102a, E2E-102b, E2E-102c, E2E-102d, E2E-102e, E2E-AGENTS-001, E2E-059a, E2E-060b, E2E-060c, E2E-061a, E2E-073a, E2E-094, E2E-095, E2E-143, E2E-145, E2E-146, E2E-146a, E2E-147, E2E-177, E2E-178, E2E-180, E2E-181, E2E-182, E2E-183, E2E-186, E2E-187, E2E-194, E2E-195, E2E-204, E2E-208, E2E-CHAT-content-width-handles, E2E-250, E2E-252, E2E-102i |
+| M5 | E2E-CHAT-long-history-stream-keeps-responsive, E2E-CHAT-running-status-survives-output-pauses, E2E-008a, E2E-032, E2E-033, E2E-034, E2E-039, E2E-043, E2E-044, E2E-045, E2E-046, E2E-047, E2E-048, E2E-048A, E2E-049, E2E-050, E2E-051, E2E-052, E2E-053, E2E-054, E2E-055, E2E-056, E2E-057, E2E-058, E2E-059, E2E-060, E2E-061, E2E-062, E2E-063, E2E-064, E2E-065, E2E-066, E2E-067, E2E-068, E2E-069, E2E-070, E2E-071, E2E-072, E2E-073, E2E-074, E2E-075, E2E-076, E2E-077, E2E-078, E2E-079, E2E-080, E2E-081, E2E-082, E2E-083, E2E-084, E2E-085, E2E-086, E2E-092, E2E-093, E2E-096, E2E-097, E2E-098, E2E-099, E2E-100, E2E-101, E2E-102, E2E-102a, E2E-102b, E2E-102c, E2E-102d, E2E-102e, E2E-AGENTS-001, E2E-059a, E2E-060b, E2E-060c, E2E-061a, E2E-073a, E2E-094, E2E-095, E2E-143, E2E-145, E2E-146, E2E-146a, E2E-147, E2E-177, E2E-178, E2E-180, E2E-181, E2E-182, E2E-183, E2E-186, E2E-187, E2E-194, E2E-195, E2E-204, E2E-208, E2E-CHAT-content-width-handles, E2E-250, E2E-252, E2E-102i, E2E-BROWSER-capture-resize |
| M5 (project ordering) | E2E-253 |
| M2 (IME slash alias) | E2E-255 |
| M5 (Skill residency) | E2E-254 |
diff --git a/docs/spec/07-plugins/03-plugin-api.md b/docs/spec/07-plugins/03-plugin-api.md
index 0be6b6dbf..5418b3c63 100644
--- a/docs/spec/07-plugins/03-plugin-api.md
+++ b/docs/spec/07-plugins/03-plugin-api.md
@@ -842,6 +842,12 @@ session never takes focus; if it cannot become visible, `reveal()` fails with
`UNAVAILABLE`. The bundled Browser agent tool calls `reveal()` before each
operation. `BrowserPreview` remains the live-reloading workspace-file preview.
+Screenshot captures, including raw `Page.captureScreenshot` CDP calls, have a
+30-second host-side deadline. A capture that exceeds it fails with `TIMEOUT`;
+the host detaches the stalled CDP session and releases that page's capture
+queue so a later capture can reattach and proceed. Other CDP methods retain
+their existing timeout behavior.
+
`getHistory` returns newest-first entries explicitly recorded by the host, with
text and images interleaved in capture order. Content written through
`writeText` and content supplied by the Composer's user-initiated `paste` event