Skip to content

Commit 9ef037f

Browse files
shivamhwpclaude
andcommitted
fix(desktop): agents can drive the popup windows their desktop tabs open
A window.open popup from an agent's desktop-rendered preview tab opened as a native window the agent could not see: the CDP relay exposed only the tab's own page, so Playwright never reported the popup and the server never adopted it. The relay now carries the tab's popup windows as extra targets, announced after the page with it as their opener, each routed to its own debugger. The desktop host attaches a popup's debugger only once the server asks for popups, which it does for agent-owned tabs alone, and keeps per-popup focus, screenshot, download and stream state. The server adopts these popups through adoptPopup, including ones already open when it connects, as native tabs that share the opener's connection, and publishes them streamed from their first snapshot so the desktop never loads them a second time. Refs #30384 Implemented by Claude Opus 5.5 in Claude Code. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7c22e24 commit 9ef037f

11 files changed

Lines changed: 954 additions & 252 deletions

File tree

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

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,164 @@ describe("CDP relay extra sessions", () => {
170170
});
171171
});
172172

173+
describe("CDP relay popups", () => {
174+
const makePopup = (
175+
targetId: string,
176+
send: CdpRelayTarget["send"],
177+
close: () => void = () => {},
178+
setDownloadDirectory: CdpRelayTarget["setDownloadDirectory"] = () => {},
179+
): CdpRelayTarget => ({
180+
...makeTarget(send, setDownloadDirectory),
181+
targetId: async () => targetId,
182+
url: () => `http://localhost:4719/${targetId}`,
183+
close,
184+
});
185+
const resultOf = (written: Array<Record<string, unknown>>, id: number) =>
186+
written.find((message) => message["id"] === id)!;
187+
188+
it("announces the page's popups after it, as its popups, and routes them separately", async () => {
189+
const pageSend = vi.fn(async () => ({}));
190+
const earlySend = vi.fn(async () => ({}));
191+
const lateSend = vi.fn(async () => ({}));
192+
const closeLate = vi.fn();
193+
const directories: Array<string | null> = [];
194+
const written: Array<Record<string, unknown>> = [];
195+
const relay = makeRelay(makeTarget(pageSend), (raw) => written.push(JSON.parse(raw)));
196+
const early = relay.addPopup(makePopup("EARLY", earlySend));
197+
relay.receive(
198+
JSON.stringify({
199+
id: 1,
200+
method: "Browser.setDownloadBehavior",
201+
params: { behavior: "allowAndName", downloadPath: "/srv/artifacts", eventsEnabled: true },
202+
}),
203+
);
204+
relay.receive(JSON.stringify({ id: 2, method: "Target.setAutoAttach", params: {} }));
205+
await settle();
206+
const late = relay.addPopup(
207+
makePopup("LATE", lateSend, closeLate, (directory) => directories.push(directory)),
208+
);
209+
await settle();
210+
211+
const announced = written.filter((m) => m["method"] === "Target.attachedToTarget");
212+
expect(announced.map((m) => m["params"])).toEqual([
213+
expect.objectContaining({
214+
sessionId: "t3-preview-page",
215+
targetInfo: expect.not.objectContaining({ openerId: expect.anything() }),
216+
}),
217+
expect.objectContaining({
218+
targetInfo: expect.objectContaining({ targetId: "EARLY", openerId: "GUEST-TARGET" }),
219+
}),
220+
expect.objectContaining({
221+
targetInfo: expect.objectContaining({ targetId: "LATE", openerId: "GUEST-TARGET" }),
222+
}),
223+
]);
224+
const [, earlySession, lateSession] = announced.map(
225+
(m) => (m["params"] as { sessionId: string }).sessionId,
226+
);
227+
// A popup opened after the server chose where downloads go saves there too.
228+
expect(directories).toEqual(["/srv/artifacts"]);
229+
expect(lateSend).toHaveBeenCalledWith(
230+
"Page.setDownloadBehavior",
231+
{ behavior: "default" },
232+
undefined,
233+
);
234+
235+
relay.receive(JSON.stringify({ id: 3, method: "Target.getTargets" }));
236+
relay.receive(JSON.stringify({ id: 4, method: "Target.attachToBrowserTarget" }));
237+
await settle();
238+
expect(
239+
(resultOf(written, 3)["result"] as { targetInfos: Array<{ targetId: string }> }).targetInfos,
240+
).toEqual([
241+
expect.objectContaining({ targetId: "GUEST-TARGET" }),
242+
expect.objectContaining({ targetId: "EARLY" }),
243+
expect.objectContaining({ targetId: "LATE" }),
244+
]);
245+
const browserSession = (resultOf(written, 4)["result"] as { sessionId: string }).sessionId;
246+
relay.receive(
247+
JSON.stringify({
248+
id: 5,
249+
method: "Target.attachToTarget",
250+
params: { targetId: "LATE", flatten: true },
251+
sessionId: browserSession,
252+
}),
253+
);
254+
relay.receive(
255+
JSON.stringify({ id: 6, method: "Target.closeTarget", params: { targetId: "GUEST-TARGET" } }),
256+
);
257+
relay.receive(
258+
JSON.stringify({ id: 7, method: "Target.attachToTarget", params: { targetId: "OTHER" } }),
259+
);
260+
await settle();
261+
const extra = (resultOf(written, 5)["result"] as { sessionId: string }).sessionId;
262+
expect(resultOf(written, 6)).toHaveProperty("error");
263+
expect(resultOf(written, 7)).toHaveProperty("error");
264+
265+
relay.receive(JSON.stringify({ id: 8, method: "Page.enable", sessionId: earlySession }));
266+
relay.receive(JSON.stringify({ id: 9, method: "Page.startScreencast", sessionId: extra }));
267+
relay.receive(JSON.stringify({ id: 10, method: "Page.enable", sessionId: "t3-preview-page" }));
268+
await settle();
269+
expect(earlySend.mock.calls.at(-1)).toEqual(["Page.enable", {}, undefined, undefined]);
270+
expect(lateSend.mock.calls.at(-1)).toEqual(["Page.startScreencast", {}, undefined, extra]);
271+
expect(pageSend.mock.calls.at(-1)).toEqual(["Page.enable", {}, undefined, undefined]);
272+
273+
// A frame's session belongs to the popup whose debugger attached it.
274+
late.event(
275+
"Target.attachedToTarget",
276+
{ sessionId: "late-frame", targetInfo: { type: "iframe" } },
277+
"",
278+
);
279+
relay.receive(JSON.stringify({ id: 11, method: "DOM.enable", sessionId: "late-frame" }));
280+
await settle();
281+
expect(lateSend.mock.calls.at(-1)).toEqual(["DOM.enable", {}, "late-frame", undefined]);
282+
283+
written.length = 0;
284+
early.event("Page.loadEventFired", { timestamp: 1 }, "");
285+
late.event("Page.downloadWillBegin", { guid: "g1", frameId: "LATE" }, "");
286+
expect(written).toEqual([
287+
{ method: "Page.loadEventFired", params: { timestamp: 1 }, sessionId: earlySession },
288+
{ method: "Browser.downloadWillBegin", params: { guid: "g1", frameId: "LATE" } },
289+
]);
290+
291+
relay.receive(
292+
JSON.stringify({ id: 12, method: "Target.closeTarget", params: { targetId: "LATE" } }),
293+
);
294+
await settle();
295+
expect(closeLate).toHaveBeenCalledOnce();
296+
written.length = 0;
297+
late.remove();
298+
late.event("Page.loadEventFired", {}, "");
299+
expect(written).toEqual([
300+
{
301+
method: "Target.detachedFromTarget",
302+
params: { sessionId: lateSession, targetId: "LATE" },
303+
},
304+
{
305+
method: "Target.detachedFromTarget",
306+
params: { sessionId: extra, targetId: "LATE" },
307+
sessionId: browserSession,
308+
},
309+
]);
310+
relay.receive(JSON.stringify({ id: 13, method: "Target.getTargets" }));
311+
await settle();
312+
expect(
313+
(resultOf(written, 13)["result"] as { targetInfos: Array<{ targetId: string }> }).targetInfos,
314+
).toHaveLength(2);
315+
});
316+
317+
it("never announces a popup that closed before the page was", async () => {
318+
const written: Array<Record<string, unknown>> = [];
319+
const relay = makeRelay(makeTarget(vi.fn()), (raw) => written.push(JSON.parse(raw)));
320+
relay.addPopup(makePopup("GONE", vi.fn())).remove();
321+
relay.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach", params: {} }));
322+
await settle();
323+
expect(written.filter((m) => m["method"] !== undefined)).toEqual([
324+
expect.objectContaining({
325+
params: expect.objectContaining({ sessionId: "t3-preview-page" }),
326+
}),
327+
]);
328+
});
329+
});
330+
173331
describe("CDP relay downloads", () => {
174332
it("captures preview downloads without changing the default browser context", async () => {
175333
const send = vi.fn(async () => ({}));

0 commit comments

Comments
 (0)