Keep desktop browser tabs stable across focus and automation (#2907)

* fix(desktop): preserve browser tabs across focus changes

Electron can replace a guest WebContents when a webview is reparented, which reloads the page and can race automation against a destroyed target. Keep each browser guest on one permanent paintable surface and make user and agent resizing share one canonical viewport.

* fix(desktop): recover browser surfaces after lifecycle changes

* fix(desktop): preserve resident browser lifecycle state

* fix(desktop): seed inactive browser viewport
This commit is contained in:
Mohamed Boudra
2026-08-06 00:05:21 +02:00
committed by GitHub
parent 7f7e8923eb
commit eb95a6eea6
12 changed files with 929 additions and 84 deletions
@@ -74,6 +74,7 @@ interface WebContentsDebugger {
event: "message",
listener: (event: unknown, method: string, params?: Record<string, unknown>) => void,
): void;
on?(event: "detach", listener: () => void): void;
}
interface ConsoleMessageEmitter {
@@ -188,6 +189,7 @@ function getDialogMonitor(
class DialogMonitor {
private enabled = false;
private listenerRegistered = false;
private detachGeneration = 0;
private readonly activeCollectors: DialogCollector[] = [];
public constructor(
@@ -200,10 +202,14 @@ class DialogMonitor {
task: () => Promise<T>,
): Promise<{ result: T; dialogs: BrowserAutomationDialogEvent[] }> {
const collector: DialogCollector = { dialogs: [] };
const setupDetachGeneration = this.detachGeneration;
try {
await this.enable();
await this.installPromptShim();
} catch (error) {
if (this.contents.isDestroyed() || this.detachGeneration !== setupDetachGeneration) {
throw error;
}
console.warn("[browser-automation] Dialog capture unavailable; running command without it", {
contentsId: this.contentsId,
error,
@@ -244,6 +250,10 @@ class DialogMonitor {
}
void this.handleOpening(params ?? {});
});
this.contents.debugger.on("detach", () => {
this.enabled = false;
this.detachGeneration += 1;
});
}
await this.sendDebugCommand("Page.enable");
this.enabled = true;