Skip to content

Commit ad8c5a4

Browse files
fix(desktop): agent tabs use a background window only while T3 is hidden
An agent tab that appears while the app window is on screen stays in a panel webview, so Pick element, Capture, DevTools, Find, picture-in-picture and direct input keep working. Only tabs that first appear while the window is hidden or minimized, whose webview would never paint, get a never-shown window. The choice is fixed per tab, since moving a page between hosts would replace it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a623078 commit ad8c5a4

7 files changed

Lines changed: 93 additions & 40 deletions

File tree

‎apps/desktop/src/ipc/DesktopIpcHandlers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
getPreferredSystemLanguages,
4343
getPrimaryPasteEnabled,
4444
getWindowFullscreenState,
45+
getWindowShown,
4546
openExternal,
4647
readMedia,
4748
openSystemSettings,
@@ -102,6 +103,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
102103
yield* ipc.handleSync(getPreferredSystemLanguages);
103104
yield* ipc.handleSync(getPrimaryPasteEnabled);
104105
yield* ipc.handleSync(getWindowFullscreenState);
106+
yield* ipc.handleSync(getWindowShown);
105107
yield* ipc.handleSync(getLocalEnvironmentBootstraps);
106108
yield* ipc.handleSync(getLocalEnvironmentEnabled);
107109
yield* ipc.handle(setLocalEnvironmentEnabled);

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const TRACKPAD_SCROLL_BEGIN_CHANNEL = "desktop:trackpad-scroll-begin";
1919
export const TRACKPAD_SCROLL_END_CHANNEL = "desktop:trackpad-scroll-end";
2020
export const GET_WINDOW_FULLSCREEN_STATE_CHANNEL = "desktop:get-window-fullscreen-state";
2121
export const WINDOW_FULLSCREEN_STATE_CHANNEL = "desktop:window-fullscreen-state";
22+
export const GET_WINDOW_SHOWN_CHANNEL = "desktop:get-window-shown";
2223
export const SYNC_HTML_FULLSCREEN_CHANNEL = "desktop:sync-html-fullscreen";
2324
export const DESKTOP_APP_ACTIVATION_READY_CHANNEL = "desktop:app-activation-ready";
2425
export const DESKTOP_APP_ACTIVATION_COMPLETE_CHANNEL = "desktop:app-activation-complete";

‎apps/desktop/src/ipc/methods/window.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ export const getWindowFullscreenState = DesktopIpc.makeSyncIpcMethod({
135135
}),
136136
});
137137

138+
export const getWindowShown = DesktopIpc.makeSyncIpcMethod({
139+
channel: IpcChannels.GET_WINDOW_SHOWN_CHANNEL,
140+
result: Schema.Boolean,
141+
handler: Effect.fn("desktop.ipc.window.getWindowShown")(function* () {
142+
const electronWindow = yield* ElectronWindow.ElectronWindow;
143+
const window = yield* electronWindow.currentMainOrFirst;
144+
return Option.isSome(window) && window.value.isVisible() && !window.value.isMinimized();
145+
}),
146+
});
147+
138148
export const getLocalEnvironmentBootstraps = DesktopIpc.makeSyncIpcMethod({
139149
channel: IpcChannels.GET_LOCAL_ENVIRONMENT_BOOTSTRAPS_CHANNEL,
140150
result: Schema.Array(DesktopEnvironmentBootstrapSchema),

‎apps/desktop/src/preload.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
304304
},
305305
getWindowFullscreenState: () =>
306306
ipcRenderer.sendSync(IpcChannels.GET_WINDOW_FULLSCREEN_STATE_CHANNEL) === true,
307+
getWindowShown: () => ipcRenderer.sendSync(IpcChannels.GET_WINDOW_SHOWN_CHANNEL) !== false,
307308
getWindowZoomFactor: () => webFrame.getZoomFactor(),
308309
onWindowFullscreenStateChange: (listener) => {
309310
const wrappedListener = (_event: Electron.IpcRendererEvent, fullscreen: unknown) => {

‎apps/web/src/browser/ElectronBrowserHost.browser.test.tsx‎

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ function setup(): Disposable {
102102
};
103103
}
104104

105+
function stubDesktopWindowShown(shown: boolean): Disposable {
106+
window.desktopBridge = { getWindowShown: () => shown, getWindowZoomFactor: () => 1 } as never;
107+
return {
108+
[Symbol.dispose]() {
109+
delete window.desktopBridge;
110+
},
111+
};
112+
}
113+
105114
async function renderHost() {
106115
await using stack = new AsyncDisposableStack();
107116
const screen = stack.use(
@@ -170,33 +179,46 @@ describe("ElectronBrowserHost", () => {
170179
expect(after.every((node, index) => node.isSameNode(before[index]!))).toBe(true);
171180
});
172181

173-
it("hosts an agent's tab in a background window instead of a panel webview", async () => {
174-
using _state = setup();
175-
reconcilePreviewServerSessions(threadRef, {
176-
sessions: [
177-
{
178-
...snapshot("tab_agent", "2026-01-01T00:00:01.000Z"),
179-
runtime: "server",
180-
automationOwner: "agent-session",
181-
},
182-
],
183-
serverEpoch: "server-a",
184-
revision: 1,
185-
});
186-
await using screen = await renderHost();
182+
it.each([
183+
{ shown: false, tabId: "tab_agent_hidden" },
184+
{ shown: true, tabId: "tab_agent_shown" },
185+
])(
186+
"hosts an agent's tab in a background window only if the app window is hidden (shown $shown)",
187+
async ({ shown, tabId }) => {
188+
using _state = setup();
189+
using _bridge = stubDesktopWindowShown(shown);
190+
reconcilePreviewServerSessions(threadRef, {
191+
sessions: [
192+
{
193+
...snapshot(tabId, "2026-01-01T00:00:01.000Z"),
194+
runtime: "server",
195+
automationOwner: "agent-session",
196+
},
197+
],
198+
serverEpoch: "server-a",
199+
revision: 1,
200+
});
201+
await using screen = await renderHost();
187202

188-
await vi.waitFor(() =>
189-
expect(mocks.createTab).toHaveBeenCalledWith(
190-
expect.any(String),
191-
expect.objectContaining({
192-
serverTab: { threadId: threadRef.threadId, tabId: "tab_agent" },
193-
background: expect.objectContaining({
194-
environmentId: threadRef.environmentId,
195-
url: "https://example.com/tab_agent",
203+
await vi.waitFor(() =>
204+
expect(mocks.createTab).toHaveBeenCalledWith(
205+
expect.any(String),
206+
expect.objectContaining({
207+
serverTab: { threadId: threadRef.threadId, tabId },
196208
}),
197-
}),
198-
),
199-
);
200-
expect(screen.container.querySelectorAll("webview")).toHaveLength(0);
201-
});
209+
),
210+
);
211+
const [, defaults] = mocks.createTab.mock.calls[0]!;
212+
if (shown) {
213+
expect(defaults?.background).toBeUndefined();
214+
expect(screen.container.querySelectorAll("webview")).toHaveLength(1);
215+
} else {
216+
expect(defaults?.background).toMatchObject({
217+
environmentId: threadRef.environmentId,
218+
url: `https://example.com/${tabId}`,
219+
});
220+
expect(screen.container.querySelectorAll("webview")).toHaveLength(0);
221+
}
222+
},
223+
);
202224
});

‎apps/web/src/browser/previewRuntime.ts‎

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ export function usePreviewAvailable(environmentId: EnvironmentId | null): boolea
4141
return isPreviewSupportedInRuntime() || serverBrowser;
4242
}
4343

44-
type RuntimeSnapshot = Pick<PreviewSessionSnapshot, "runtime" | "headless" | "automationOwner">;
44+
type RuntimeSnapshot = Pick<
45+
PreviewSessionSnapshot,
46+
"threadId" | "tabId" | "runtime" | "headless" | "automationOwner"
47+
>;
4548

4649
/** The desktop app hosts this server tab, in a panel `<webview>` or a background window. */
4750
function desktopHostsServerTab(
@@ -58,30 +61,42 @@ function desktopHostsServerTab(
5861
);
5962
}
6063

61-
export function rendersServerTabNatively(
64+
// Fixed when a tab first appears: moving a page between hosts would replace it.
65+
const backgroundHostedTabs = new Map<string, boolean>();
66+
67+
/**
68+
* A panel `<webview>` created while the app window is hidden or minimized never paints, so an
69+
* agent tab that appears then runs in a never-shown desktop window and the panel streams it.
70+
*/
71+
export function hostsServerTabInBackground(
6272
environmentId: EnvironmentId,
6373
primaryEnvironmentId: EnvironmentId | null,
6474
snapshot: RuntimeSnapshot | null | undefined,
6575
): boolean {
66-
return (
67-
desktopHostsServerTab(environmentId, primaryEnvironmentId, snapshot) &&
68-
snapshot?.automationOwner === undefined
69-
);
76+
if (
77+
!snapshot ||
78+
snapshot.automationOwner === undefined ||
79+
!desktopHostsServerTab(environmentId, primaryEnvironmentId, snapshot)
80+
) {
81+
return false;
82+
}
83+
const key = `${environmentId}\u0000${snapshot.threadId}\u0000${snapshot.tabId}`;
84+
let background = backgroundHostedTabs.get(key);
85+
if (background === undefined) {
86+
background = window.desktopBridge?.getWindowShown?.() === false;
87+
backgroundHostedTabs.set(key, background);
88+
}
89+
return background;
7090
}
7191

72-
/**
73-
* Agent tabs run in a never-shown desktop window, because a panel `<webview>`
74-
* created while the app is hidden never paints and agents open tabs whenever
75-
* they like. The panel streams them like any other server tab.
76-
*/
77-
export function hostsServerTabInBackground(
92+
export function rendersServerTabNatively(
7893
environmentId: EnvironmentId,
7994
primaryEnvironmentId: EnvironmentId | null,
8095
snapshot: RuntimeSnapshot | null | undefined,
8196
): boolean {
8297
return (
8398
desktopHostsServerTab(environmentId, primaryEnvironmentId, snapshot) &&
84-
snapshot?.automationOwner !== undefined
99+
!hostsServerTabInBackground(environmentId, primaryEnvironmentId, snapshot)
85100
);
86101
}
87102

‎packages/contracts/src/ipc.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,8 @@ export interface DesktopBridge {
11651165
onSnapShotEvent: (listener: (event: DesktopSnapShotEvent) => void) => () => void;
11661166
onQuitShortcut: (listener: (event: QuitShortcutHintEvent) => void) => () => void;
11671167
getWindowFullscreenState: () => boolean;
1168+
/** Whether the app window is on screen: not hidden and not minimized. */
1169+
getWindowShown: () => boolean;
11681170
onWindowFullscreenStateChange: (listener: (fullscreen: boolean) => void) => () => void;
11691171
getWindowZoomFactor: () => number;
11701172
getUpdateState: () => Promise<DesktopUpdateState>;

0 commit comments

Comments
 (0)