Skip to content

Commit 70ee122

Browse files
PR Batch Testerclaude
andcommitted
fix(desktop): keep shown state right across hydration, reloads and crashes
- Report shown only after client settings hydrate, so the desktop tab exists when the report arrives. - Clear shown when a tab's guest webview is destroyed (window reload or guest crash) and report again once a new guest registers. - Tests cover hiding, picking the shown tab without a tabId, and not replaying a hidden tab to a reconnected backend. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8f9bef1 commit 70ee122

4 files changed

Lines changed: 58 additions & 5 deletions

File tree

‎apps/desktop/src/preview/DesktopBrowserHost.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ describe("DesktopBrowserHost", () => {
6565
it.effect("tells a backend that starts later which tabs are on screen", () =>
6666
Effect.gen(function* () {
6767
const host = yield* DesktopBrowserHost.make;
68+
const other = { threadId: "thread-1", tabId: "tab-2" };
6869
host.setShown(key, true);
70+
host.setShown(other, true);
71+
host.setShown(key, false);
6972

70-
expect(yield* takeEvents(host, 1)).toEqual([{ type: "shown", ...key, shown: true }]);
73+
expect(yield* takeEvents(host, 1)).toEqual([{ type: "shown", ...other, shown: true }]);
7174
}),
7275
);
7376

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
19851985
yield* rendererHistory.register(wc, { surface: "preview" });
19861986
const webContentsId = wc.id;
19871987
wc.once("destroyed", () => {
1988+
runFork(hideDestroyedGuest(webContentsId));
19881989
runFork(detachControlSession(webContentsId));
19891990
});
19901991
yield* ensureControlSession(wc).pipe(
@@ -2455,6 +2456,16 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
24552456
if (serverTab) browserHost.setShown(serverTab, shown);
24562457
});
24572458

2459+
// A window reload or guest crash destroys the guest without the renderer reporting it hidden.
2460+
// The renderer reports again once a new guest registers.
2461+
const hideDestroyedGuest = Effect.fnUntraced(function* (webContentsId: number) {
2462+
for (const tab of (yield* SynchronizedRef.get(tabsRef)).values()) {
2463+
if (tab.webContentsId === webContentsId && tab.serverTab) {
2464+
browserHost.setShown(tab.serverTab, false);
2465+
}
2466+
}
2467+
});
2468+
24582469
const setAudioMuted = Effect.fn("PreviewManager.setAudioMuted")(function* (
24592470
tabId: string,
24602471
audioMuted: boolean,

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,41 @@ it.live("a tab the desktop draws is visible only while the desktop shows it", ()
10261026
visible: true,
10271027
tabs: [expect.objectContaining({ tabId, visible: true })],
10281028
});
1029+
1030+
shownDesktopTabs.delete(tabId);
1031+
expect(yield* status).toMatchObject({ visible: false });
1032+
}),
1033+
).pipe(Effect.provide(layer)),
1034+
);
1035+
1036+
it.live("agents without a tab of their own get the tab the desktop shows", () =>
1037+
Effect.scoped(
1038+
Effect.gen(function* () {
1039+
const broker = yield* Broker.PreviewAutomationBroker;
1040+
const manager = yield* Manager.PreviewManager;
1041+
yield* ServerBrowser.ServerBrowser;
1042+
yield* Effect.yieldNow;
1043+
const openDesktopTab = Effect.fn("openDesktopTab")(function* (url: string) {
1044+
desktopRendersNext = true;
1045+
const opened = yield* manager.open({
1046+
threadId: scope.thread.threadId,
1047+
url,
1048+
runtime: "server",
1049+
});
1050+
const tabId = PreviewTabId.make(opened.tabId);
1051+
yield* broker.invoke({ scope, tabId, operation: "status", input: {} });
1052+
return tabId;
1053+
});
1054+
const shownTab = yield* openDesktopTab("http://localhost:5173/shown");
1055+
yield* openDesktopTab("http://localhost:5173/newer");
1056+
1057+
shownDesktopTabs.add(shownTab);
1058+
const status = yield* broker.invoke<PreviewAutomationStatus>({
1059+
scope: asSession("agent-b"),
1060+
operation: "status",
1061+
input: {},
1062+
});
1063+
expect(status.tabId).toBe(shownTab);
10291064
}),
10301065
).pipe(Effect.provide(layer)),
10311066
);

‎apps/web/src/browser/HostedBrowserWebview.tsx‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ export function HostedBrowserWebview(props: {
135135

136136
const [webviewGeneration, setWebviewGeneration] = useState(0);
137137
const [recoverySrc, setRecoverySrc] = useState(initialSrc);
138+
const active = presentation.visible && presentation.rect !== null;
139+
const shown = active || pictureInPicture;
140+
const reportedShown = useEffectEvent(() => serverDriven && shown);
138141
const remountCrashedWebview = useEffectEvent(() => {
139142
setRecoverySrc(initialUrl ?? initialSrc);
140143
setWebviewGeneration((generation) => generation + 1);
@@ -163,6 +166,9 @@ export function HostedBrowserWebview(props: {
163166
const webContentsId = webview.getWebContentsId();
164167
if (Number.isInteger(webContentsId) && webContentsId > 0) {
165168
await bridge.registerWebview(runtimeTabId, webContentsId);
169+
if (reportedShown()) {
170+
withDesktopTab(runtimeTabId, () => bridge.setShown(runtimeTabId, true));
171+
}
166172
}
167173
} catch {}
168174
})();
@@ -206,7 +212,6 @@ export function HostedBrowserWebview(props: {
206212
webviewGeneration,
207213
]);
208214

209-
const active = presentation.visible && presentation.rect !== null;
210215
const lastRect = presentation.rect;
211216
const layoutZoomFactor = useBrowserLayoutZoomFactor(zoomFactor);
212217
const viewportWidth = viewport._tag === "fill" ? null : viewport.width;
@@ -291,13 +296,12 @@ export function HostedBrowserWebview(props: {
291296
viewportWidth,
292297
]);
293298

294-
const shown = active || pictureInPicture;
295299
useEffect(() => {
296300
const bridge = window.desktopBridge?.preview;
297-
if (!bridge || !serverDriven || !shown) return;
301+
if (!clientSettingsHydrated || !bridge || !serverDriven || !shown) return;
298302
withDesktopTab(runtimeTabId, () => bridge.setShown(runtimeTabId, true));
299303
return () => withDesktopTab(runtimeTabId, () => bridge.setShown(runtimeTabId, false));
300-
}, [runtimeTabId, serverDriven, shown]);
304+
}, [clientSettingsHydrated, runtimeTabId, serverDriven, shown]);
301305

302306
if (!clientSettingsHydrated || !config) return null;
303307

0 commit comments

Comments
 (0)