fix(browser): park webviews permanently paintable

Delete the renderer capture prep handshake and keep screenshots on the serialized invalidate/retry path. The capture harness now defaults to the production P1 attach-off parking check.
This commit is contained in:
Mohamed Boudra
2026-07-03 11:34:48 +02:00
parent cc21262130
commit ab62a023fd
15 changed files with 358 additions and 1652 deletions
@@ -7,12 +7,7 @@ import type {
BrowserAutomationExecuteRequest,
} from "@getpaseo/protocol/browser-automation/rpc-schemas";
import { BrowserSnapshotEngine } from "./snapshot-engine.js";
import type {
BrowserRegistry,
TabContents,
TabImage,
TabPixelCapturePreparation,
} from "./service.js";
import type { BrowserRegistry, TabContents, TabImage } from "./service.js";
import { executeAutomationCommand } from "./service.js";
const BROWSER_A = "11111111-1111-4111-8111-111111111111";
@@ -41,7 +36,6 @@ class FakeTab implements TabContents {
public readonly actions: string[] = [];
public readonly capturedViewports: Array<{ stayHidden?: boolean }> = [];
public readonly debugCommands: Array<{ command: string; params?: Record<string, unknown> }> = [];
public readonly restoredPixelCaptureTokens: string[] = [];
private readonly captureStartWaiters: Array<() => void> = [];
private readonly deferredCaptures: Array<(image: TabImage) => void> = [];
@@ -56,8 +50,6 @@ class FakeTab implements TabContents {
public captureErrorMessage = "capture failed";
public viewportCaptureFailuresBeforeSuccess = 0;
public deferCaptures = false;
public prepareNeverAcks = false;
public prepareErrorMessage: string | null = null;
public fullPageScreenshotThrows = false;
public fullPageScreenshotErrorMessage = "UnknownVizError";
public fullPageCaptureFailuresBeforeSuccess = 0;
@@ -68,8 +60,6 @@ class FakeTab implements TabContents {
public fullPageScreenshotData = "fullPagePng";
public documentNodeId = 1;
public queriedNodeId = 2;
public backgroundThrottlingAllowed = true;
private nextPixelCapturePreparationId = 0;
public constructor(
public readonly id: number,
@@ -153,36 +143,10 @@ class FakeTab implements TabContents {
return new FakeImage();
}
public async prepareForPixelCapture(): Promise<TabPixelCapturePreparation> {
this.actions.push("prepare");
if (this.prepareErrorMessage) {
throw new Error(this.prepareErrorMessage);
}
if (this.prepareNeverAcks) {
return new Promise<never>(() => {});
}
const token = `capture-${++this.nextPixelCapturePreparationId}`;
return { token };
}
public async restorePixelCapture(preparation: TabPixelCapturePreparation): Promise<void> {
this.actions.push(`restore:${preparation.token}`);
this.restoredPixelCaptureTokens.push(preparation.token);
}
public invalidate(): void {
this.actions.push("invalidate");
}
public isBackgroundThrottlingAllowed(): boolean {
return this.backgroundThrottlingAllowed;
}
public setBackgroundThrottling(allowed: boolean): void {
this.backgroundThrottlingAllowed = allowed;
this.actions.push(`background:${allowed}`);
}
public getConsoleMessages(): BrowserAutomationConsoleLogEntry[] {
return this.consoleMessages;
}
@@ -913,7 +877,7 @@ describe("executeAutomationCommand", () => {
});
});
test("screenshot serializes the painted viewport and restores throttling", async () => {
test("screenshot captures the painted viewport", async () => {
const browser = new BrowserAutomationHarness();
const result = await browser.execute({
@@ -934,14 +898,7 @@ describe("executeAutomationCommand", () => {
},
});
expect(browser.tab.capturedViewports).toEqual([{ stayHidden: false }]);
expect(browser.tab.actions).toEqual([
"prepare",
"background:false",
"invalidate",
"capture",
"restore:capture-1",
"background:true",
]);
expect(browser.tab.actions).toEqual(["invalidate", "capture"]);
});
test("screenshot returns no-frame when the viewport never paints", async () => {
@@ -961,25 +918,17 @@ describe("executeAutomationCommand", () => {
ok: false,
error: {
code: "screenshot_no_frame",
message:
"The browser tab has no painted frame. Focus the tab in the app, then try again.",
retryable: false,
message: "The tab has not painted yet. Retry the screenshot.",
retryable: true,
},
});
expect(browser.tab.actions).toEqual([
"prepare",
"background:false",
"invalidate",
"capture",
"restore:capture-1",
"background:true",
]);
expect(browser.tab.actions).toEqual(["invalidate", "capture"]);
} finally {
vi.useRealTimers();
}
});
test("screenshot restores capture preparation when viewport capture fails with an ordinary error", async () => {
test("screenshot surfaces ordinary viewport capture errors", async () => {
const browser = new BrowserAutomationHarness();
browser.tab.captureThrows = true;
@@ -990,14 +939,7 @@ describe("executeAutomationCommand", () => {
}),
).rejects.toThrow("capture failed");
expect(browser.tab.actions).toEqual([
"prepare",
"background:false",
"invalidate",
"capture",
"restore:capture-1",
"background:true",
]);
expect(browser.tab.actions).toEqual(["invalidate", "capture"]);
});
test("screenshot retries UnknownVizError until the first viewport frame appears", async () => {
@@ -1027,16 +969,12 @@ describe("executeAutomationCommand", () => {
});
expect(browser.tab.capturedViewports).toHaveLength(3);
expect(browser.tab.actions).toEqual([
"prepare",
"background:false",
"invalidate",
"capture",
"invalidate",
"capture",
"invalidate",
"capture",
"restore:capture-1",
"background:true",
]);
} finally {
vi.useRealTimers();
@@ -1061,74 +999,20 @@ describe("executeAutomationCommand", () => {
ok: false,
error: {
code: "screenshot_no_frame",
message:
"The browser tab has no painted frame. Focus the tab in the app, then try again.",
retryable: false,
message: "The tab has not painted yet. Retry the screenshot.",
retryable: true,
},
});
expect(browser.tab.capturedViewports.length).toBeGreaterThan(1);
expect(browser.tab.actions.filter((action) => action === "invalidate")).toHaveLength(
browser.tab.capturedViewports.length,
);
expect(browser.tab.restoredPixelCaptureTokens).toEqual(["capture-1"]);
expect(browser.tab.actions.at(-2)).toBe("restore:capture-1");
expect(browser.tab.actions.at(-1)).toBe("background:true");
} finally {
vi.useRealTimers();
}
});
test("screenshot returns no-frame when capture preparation does not ack", async () => {
vi.useFakeTimers();
try {
const browser = new BrowserAutomationHarness();
browser.tab.prepareNeverAcks = true;
const resultPromise = browser.execute({
command: "screenshot",
args: { browserId: BROWSER_A },
});
await vi.advanceTimersByTimeAsync(5_000);
await expect(resultPromise).resolves.toEqual({
requestId: "req-screenshot",
ok: false,
error: {
code: "screenshot_no_frame",
message:
"Browser screenshot prep_unavailable: the app renderer did not acknowledge capture preparation before the timeout.",
retryable: false,
},
});
expect(browser.tab.actions).toEqual(["prepare", "background:true"]);
} finally {
vi.useRealTimers();
}
});
test("screenshot returns prep_unavailable and does not capture when preparation cannot be honored", async () => {
const browser = new BrowserAutomationHarness();
browser.tab.prepareErrorMessage =
"Browser screenshot prep_unavailable: Browser host renderer is not available.";
const result = await browser.execute({
command: "screenshot",
args: { browserId: BROWSER_A },
});
expect(result).toEqual({
requestId: "req-screenshot",
ok: false,
error: {
code: "screenshot_no_frame",
message: "Browser screenshot prep_unavailable: Browser host renderer is not available.",
retryable: false,
},
});
expect(browser.tab.actions).toEqual(["prepare", "background:true"]);
});
test("overlapping screenshots serialize capture preparation and restore", async () => {
test("overlapping screenshots serialize through the shared capture queue", async () => {
const browser = new BrowserAutomationHarness();
browser.tab.deferCaptures = true;
@@ -1147,44 +1031,20 @@ describe("executeAutomationCommand", () => {
);
await Promise.resolve();
expect(browser.tab.actions).toEqual(["prepare", "background:false", "invalidate", "capture"]);
expect(browser.tab.actions).toEqual(["invalidate", "capture"]);
browser.tab.finishNextCapture();
await browser.tab.waitForCaptureStart(2);
expect(browser.tab.actions).toEqual([
"prepare",
"background:false",
"invalidate",
"capture",
"restore:capture-1",
"background:true",
"prepare",
"background:false",
"invalidate",
"capture",
]);
expect(browser.tab.actions).toEqual(["invalidate", "capture", "invalidate", "capture"]);
browser.tab.finishNextCapture();
await expect(first).resolves.toMatchObject({ requestId: "req-screenshot", ok: true });
await expect(second).resolves.toMatchObject({ requestId: "req-screenshot-2", ok: true });
expect(browser.tab.actions).toEqual([
"prepare",
"background:false",
"invalidate",
"capture",
"restore:capture-1",
"background:true",
"prepare",
"background:false",
"invalidate",
"capture",
"restore:capture-2",
"background:true",
]);
expect(browser.tab.actions).toEqual(["invalidate", "capture", "invalidate", "capture"]);
});
test("overlapping screenshots across browser tabs serialize capture preparation and restore", async () => {
test("overlapping screenshots across browser tabs serialize through the shared capture queue", async () => {
const registry = new FakeRegistry();
const firstTab = new FakeTab(1, "https://a.test", "A");
const secondTab = new FakeTab(2, "https://b.test", "B");
@@ -1214,33 +1074,19 @@ describe("executeAutomationCommand", () => {
);
await Promise.resolve();
expect(firstTab.actions).toEqual(["prepare", "background:false", "invalidate", "capture"]);
expect(firstTab.actions).toEqual(["invalidate", "capture"]);
expect(secondTab.actions).toEqual([]);
firstTab.finishNextCapture();
await secondTab.waitForCaptureStart(1);
expect(firstTab.actions).toEqual([
"prepare",
"background:false",
"invalidate",
"capture",
"restore:capture-1",
"background:true",
]);
expect(secondTab.actions).toEqual(["prepare", "background:false", "invalidate", "capture"]);
expect(firstTab.actions).toEqual(["invalidate", "capture"]);
expect(secondTab.actions).toEqual(["invalidate", "capture"]);
secondTab.finishNextCapture();
await expect(first).resolves.toMatchObject({ requestId: "req-screenshot", ok: true });
await expect(second).resolves.toMatchObject({ requestId: "req-screenshot-2", ok: true });
expect(secondTab.actions).toEqual([
"prepare",
"background:false",
"invalidate",
"capture",
"restore:capture-1",
"background:true",
]);
expect(secondTab.actions).toEqual(["invalidate", "capture"]);
});
test("screenshot with fullPage captures the page content area through CDP", async () => {
@@ -1275,17 +1121,13 @@ describe("executeAutomationCommand", () => {
},
]);
expect(browser.tab.actions).toEqual([
"prepare",
"background:false",
"invalidate",
"debug:Page.getLayoutMetrics",
"debug:Page.captureScreenshot",
"restore:capture-1",
"background:true",
]);
});
test("screenshot with fullPage restores capture preparation when CDP returns no image", async () => {
test("screenshot with fullPage returns unsupported when CDP returns no image", async () => {
const browser = new BrowserAutomationHarness();
browser.tab.fullPageScreenshotData = "";
@@ -1304,13 +1146,9 @@ describe("executeAutomationCommand", () => {
},
});
expect(browser.tab.actions).toEqual([
"prepare",
"background:false",
"invalidate",
"debug:Page.getLayoutMetrics",
"debug:Page.captureScreenshot",
"restore:capture-1",
"background:true",
]);
});
@@ -1339,23 +1177,19 @@ describe("executeAutomationCommand", () => {
},
});
expect(browser.tab.actions).toEqual([
"prepare",
"background:false",
"invalidate",
"debug:Page.getLayoutMetrics",
"debug:Page.captureScreenshot",
"invalidate",
"debug:Page.getLayoutMetrics",
"debug:Page.captureScreenshot",
"restore:capture-1",
"background:true",
]);
} finally {
vi.useRealTimers();
}
});
test("screenshot with fullPage restores capture preparation when CDP capture fails with an ordinary error", async () => {
test("screenshot with fullPage surfaces ordinary CDP capture errors", async () => {
const browser = new BrowserAutomationHarness();
browser.tab.fullPageScreenshotThrows = true;
browser.tab.fullPageScreenshotErrorMessage = "Debugger detached";
@@ -1367,13 +1201,9 @@ describe("executeAutomationCommand", () => {
}),
).rejects.toThrow("Debugger detached");
expect(browser.tab.actions).toEqual([
"prepare",
"background:false",
"invalidate",
"debug:Page.getLayoutMetrics",
"debug:Page.captureScreenshot",
"restore:capture-1",
"background:true",
]);
});