Skip to content

Commit 9ed1bc2

Browse files
committed
fix(server): show the page a person opened from an agent browser tab
Google results use target=_blank. On an agent-owned tab that popup stayed hidden, so the click never reached a visible tab. A person holding the pointer now gets the page revealed and switched to.
1 parent ed0f7b9 commit 9ed1bc2

2 files changed

Lines changed: 41 additions & 8 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,34 @@ it.live("a popup becomes the agent's own tab and keeps its opener page", () =>
762762
).pipe(Effect.provide(layer)),
763763
);
764764

765+
it.live("a person's click on an agent tab shows the popup instead of leaving it hidden", () =>
766+
Effect.scoped(
767+
Effect.gen(function* () {
768+
const { browser, tabId } = yield* ready;
769+
const manager = yield* Manager.PreviewManager;
770+
const watcher = yield* browser.attachViewer(viewerInput(tabId, false));
771+
const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
772+
yield* viewer.input({ type: "takeControl" });
773+
const opener = contexts[0]!.page;
774+
const popup = makeContext();
775+
opener.emit("popup", popup.page);
776+
let shown = yield* Queue.take(viewer.output);
777+
while (shown._tag !== "popup") shown = yield* Queue.take(viewer.output);
778+
const sessions = (yield* manager.list({ threadId: scope.thread.threadId })).sessions;
779+
const popupTab = sessions.find((session) => session.tabId === shown.tabId);
780+
const opened = sessions.find((session) => session.tabId === tabId)!;
781+
expect(popupTab).toMatchObject({
782+
automationOwner: opened.automationOwner,
783+
reveal: true,
784+
});
785+
expect((yield* Queue.clear(watcher.output)).some((item) => item._tag === "popup")).toBe(
786+
false,
787+
);
788+
expect(opener.close).not.toHaveBeenCalled();
789+
}),
790+
).pipe(Effect.provide(layer)),
791+
);
792+
765793
it.live("a tab opened to a file the browser cannot show reports the file to download", () =>
766794
Effect.scoped(
767795
Effect.gen(function* () {

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,26 +1143,32 @@ const make = Effect.gen(function* () {
11431143

11441144
const adoptPopup = async (opener: ServerTab, popup: Page) => {
11451145
const agentId = opener.control.agentId;
1146+
// Whoever holds the pointer clicked. Google results use target=_blank, so the
1147+
// page they asked for only exists as this popup. Hide it and the click looks dead.
1148+
const humanId = opener.control.controller;
11461149
// A popup past an agent's limit closes; its page sees window.open return a closed window.
1147-
if (opener.closing || (agentId !== null && atTabLimit(agentId))) {
1150+
// A person driving the tab is not spending that hidden-tab budget.
1151+
if (opener.closing || (agentId !== null && humanId === null && atTabLimit(agentId))) {
11481152
await popup.close().catch(constVoid);
11491153
return;
11501154
}
11511155
const url = popup.url();
11521156
// Captured now: the person whose click opened the popup gets switched to it.
1153-
const controller = [...opener.viewers].find(
1154-
(viewer) => viewer.id === opener.control.controller,
1155-
);
1157+
const controller = [...opener.viewers].find((viewer) => viewer.id === humanId);
11561158
await Effect.runPromise(
11571159
manager.open({
11581160
threadId: opener.threadId,
11591161
...(/^https?:/i.test(url) ? { url } : {}),
11601162
runtime: "server",
11611163
...(opener.profileId === undefined ? {} : { profileId: opener.profileId }),
11621164
// Agent popups stay with the agent and only float when it asks, like its own opens.
1163-
...(opener.control.agentId === null
1165+
// A person's click on that same tab has to come forward, or the link never shows.
1166+
...(agentId === null
11641167
? {}
1165-
: { automationOwner: opener.control.agentId, reveal: false }),
1168+
: {
1169+
automationOwner: agentId,
1170+
...(humanId === null ? { reveal: false } : { reveal: true }),
1171+
}),
11661172
beforePublish: (snapshot) =>
11671173
adoptedPages.set(tabKey(snapshot.threadId, snapshot.tabId), {
11681174
page: popup,
@@ -1172,8 +1178,7 @@ const make = Effect.gen(function* () {
11721178
).then(
11731179
(snapshot) => {
11741180
// The opener stays open behind it, so `window.opener` can still report back.
1175-
if (opener.control.agentId === null)
1176-
controller?.push({ _tag: "popup", tabId: snapshot.tabId });
1181+
if (humanId !== null) controller?.push({ _tag: "popup", tabId: snapshot.tabId });
11771182
},
11781183
async () => {
11791184
await popup.close().catch(constVoid);

0 commit comments

Comments
 (0)