Skip to content

Commit 3d7deb2

Browse files
committed
fix(preview): scope popup handoffs to the clicking viewer
1 parent 8befb42 commit 3d7deb2

7 files changed

Lines changed: 142 additions & 151 deletions

File tree

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

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,17 @@ const ready = Effect.gen(function* () {
365365
const tabId = PreviewTabId.make(opened.tabId!);
366366
return { browser, broker, tabId };
367367
});
368-
const viewerInput = (tabId: string, canOperate: boolean, sessionId?: string) => ({
368+
const viewerInput = (
369+
tabId: string,
370+
canOperate: boolean,
371+
sessionId?: string,
372+
handoffId?: string,
373+
) => ({
369374
threadId: scope.thread.threadId,
370375
tabId,
371376
canOperate,
372377
...(sessionId === undefined ? {} : { sessionId }),
378+
...(handoffId === undefined ? {} : { handoffId }),
373379
maxWidth: 1280,
374380
maxHeight: 800,
375381
quality: 70,
@@ -949,6 +955,7 @@ it.live("an adopted popup that is still loading is not reported as successful ea
949955
}
950956

951957
yield* browser.attachViewer(viewerInput(opened.snapshot.tabId, false));
958+
popup.page.emit("framenavigated", popup.page);
952959
expect(popup.page.waitForLoadState).toHaveBeenCalledWith("load");
953960
// reportLoaded reads the title first, so an early report would have called it by now.
954961
expect(popup.page.title).not.toHaveBeenCalled();
@@ -1008,7 +1015,6 @@ it.live("a tab opened to a file the browser cannot show reports the file to down
10081015
const browser = yield* ServerBrowser.ServerBrowser;
10091016
const manager = yield* Manager.PreviewManager;
10101017
const events = yield* manager.subscribeEvents;
1011-
yield* Effect.yieldNow;
10121018
const opened = yield* manager.open({ threadId: scope.thread.threadId, runtime: "server" });
10131019
const viewer = yield* browser.attachViewer(viewerInput(opened.tabId, true));
10141020
const page = contexts[0]!.page;
@@ -1138,10 +1144,11 @@ it.live("the person who opens a popup from an agent tab controls the popup", ()
11381144
const events = yield* manager.subscribeEvents;
11391145
const opener = contexts[0]!.page;
11401146
opener.emit("popup", popup.page);
1141-
const shown = yield* Fiber.join(waitingForPopup);
1142-
const popupTabId = PreviewTabId.make(shown.tabId);
11431147
let opened = yield* PubSub.take(events);
11441148
while (opened.type !== "opened") opened = yield* PubSub.take(events);
1149+
expect(opened.snapshot.reveal).toBeUndefined();
1150+
const shown = yield* Fiber.join(waitingForPopup);
1151+
const popupTabId = PreviewTabId.make(shown.tabId);
11451152
const openerTab = (yield* manager.list({ threadId: scope.thread.threadId })).sessions.find(
11461153
(session) => session.tabId === tabId,
11471154
);
@@ -1152,7 +1159,9 @@ it.live("the person who opens a popup from an agent tab controls the popup", ()
11521159
});
11531160
expect(opened.snapshot.reveal).toBeUndefined();
11541161

1155-
const popupViewer = yield* browser.attachViewer(viewerInput(popupTabId, true, sessionId));
1162+
const popupViewer = yield* browser.attachViewer(
1163+
viewerInput(popupTabId, true, sessionId, shown.handoffId),
1164+
);
11561165
let control = yield* Queue.take(popupViewer.output);
11571166
while (control._tag !== "control") control = yield* Queue.take(popupViewer.output);
11581167
expect(control).toMatchObject({ controller: "you" });
@@ -1200,20 +1209,29 @@ it.live("a watcher from the handoff session does not consume the popup handoff",
12001209
while (control._tag !== "control") control = yield* Queue.take(watcher.output);
12011210
expect(control).toMatchObject({ controller: "agent" });
12021211

1212+
const sameSession = yield* browser.attachViewer(viewerInput(shown.tabId, true, sessionId));
1213+
control = yield* Queue.take(sameSession.output);
1214+
while (control._tag !== "control") control = yield* Queue.take(sameSession.output);
1215+
expect(control).toMatchObject({ controller: "agent" });
1216+
12031217
const otherSession = yield* browser.attachViewer(
12041218
viewerInput(shown.tabId, true, "other-session"),
12051219
);
12061220
control = yield* Queue.take(otherSession.output);
12071221
while (control._tag !== "control") control = yield* Queue.take(otherSession.output);
12081222
expect(control).toMatchObject({ controller: "agent" });
12091223

1210-
const operator = yield* browser.attachViewer(viewerInput(shown.tabId, true, sessionId));
1224+
const operator = yield* browser.attachViewer(
1225+
viewerInput(shown.tabId, true, sessionId, shown.handoffId),
1226+
);
12111227
control = yield* Queue.take(operator.output);
12121228
while (control._tag !== "control") control = yield* Queue.take(operator.output);
12131229
expect(control).toMatchObject({ controller: "you" });
12141230

12151231
yield* operator.input({ type: "releaseControl" });
1216-
const later = yield* browser.attachViewer(viewerInput(shown.tabId, true, sessionId));
1232+
const later = yield* browser.attachViewer(
1233+
viewerInput(shown.tabId, true, sessionId, shown.handoffId),
1234+
);
12171235
control = yield* Queue.take(later.output);
12181236
while (control._tag !== "control") control = yield* Queue.take(later.output);
12191237
expect(control).toMatchObject({ controller: "agent" });
@@ -1236,7 +1254,9 @@ it.live(
12361254
const shown = yield* Fiber.join(waitingForPopup);
12371255

12381256
yield* opener.input({ type: "releaseControl" });
1239-
const popupViewer = yield* browser.attachViewer(viewerInput(shown.tabId, true, sessionId));
1257+
const popupViewer = yield* browser.attachViewer(
1258+
viewerInput(shown.tabId, true, sessionId, shown.handoffId),
1259+
);
12401260
let control = yield* Queue.take(popupViewer.output);
12411261
while (control._tag !== "control") control = yield* Queue.take(popupViewer.output);
12421262
expect(control).toMatchObject({ controller: "agent" });
@@ -1275,10 +1295,42 @@ it.live("releasing the opener while its popup is still starting lets the agent k
12751295

12761296
yield* opener.input({ type: "releaseControl" });
12771297
setUp.resolve();
1278-
const popupViewer = yield* browser.attachViewer(viewerInput(shown.tabId, true, sessionId));
1298+
const popupViewer = yield* browser.attachViewer(
1299+
viewerInput(shown.tabId, true, sessionId, shown.handoffId),
1300+
);
12791301
let control = yield* Queue.take(popupViewer.output);
12801302
while (control._tag !== "control") control = yield* Queue.take(popupViewer.output);
12811303
expect(control).toMatchObject({ controller: "agent" });
1304+
1305+
yield* opener.input({ type: "takeControl" });
1306+
const manager = yield* Manager.PreviewManager;
1307+
const events = yield* manager.subscribeEvents;
1308+
const registering = Promise.withResolvers<void>();
1309+
const register = Promise.withResolvers<void>();
1310+
const registered = Promise.withResolvers<void>();
1311+
const open = manager.open;
1312+
using _open = vi.spyOn(manager, "open").mockImplementationOnce((input) =>
1313+
Effect.promise(() => {
1314+
registering.resolve();
1315+
return register.promise;
1316+
}).pipe(
1317+
Effect.andThen(open(input)),
1318+
Effect.tap(() => Effect.sync(() => registered.resolve())),
1319+
),
1320+
);
1321+
contexts[0]!.page.emit("popup", makeContext().page);
1322+
yield* Effect.promise(() => registering.promise);
1323+
yield* opener.input({ type: "releaseControl" });
1324+
register.resolve();
1325+
yield* Effect.promise(() => registered.promise);
1326+
let opened = yield* PubSub.take(events);
1327+
while (opened.type !== "opened") opened = yield* PubSub.take(events);
1328+
const lateViewer = yield* browser.attachViewer(
1329+
viewerInput(opened.snapshot.tabId, true, sessionId),
1330+
);
1331+
control = yield* Queue.take(lateViewer.output);
1332+
while (control._tag !== "control") control = yield* Queue.take(lateViewer.output);
1333+
expect(control).toMatchObject({ controller: "agent" });
12821334
}),
12831335
).pipe(Effect.provide(layer)),
12841336
);

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

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export type ServerBrowserViewerOutput =
193193
readonly accept: string;
194194
}
195195
| { readonly _tag: "fileChooserClosed"; readonly id: string }
196-
| { readonly _tag: "popup"; readonly tabId: string }
196+
| { readonly _tag: "popup"; readonly tabId: string; readonly handoffId?: string }
197197
| {
198198
readonly _tag: "download";
199199
readonly id: string;
@@ -219,6 +219,7 @@ export class ServerBrowser extends Context.Service<
219219
readonly quality: number;
220220
readonly canOperate: boolean;
221221
readonly sessionId?: string;
222+
readonly handoffId?: string | undefined;
222223
}) => Effect.Effect<
223224
ServerBrowserViewer,
224225
ServerBrowserTabNotFoundError | ServerBrowserLaunchError,
@@ -247,10 +248,16 @@ export class ServerBrowser extends Context.Service<
247248
}
248249
>()("t3/preview/ServerBrowser") {}
249250

251+
interface PopupHandoff {
252+
readonly id: string;
253+
readonly sessionId: string | undefined;
254+
active: boolean;
255+
}
256+
250257
interface ViewerState {
251258
readonly id: string;
252259
readonly canOperate: boolean;
253-
readonly sessionId: string | undefined;
260+
handoff: PopupHandoff;
254261
readonly pressedKeys: Map<string, { key: string; code: string }>;
255262
readonly pressedButtons: Map<"left" | "middle" | "right", { x: number; y: number }>;
256263
readonly push: (output: ServerBrowserViewerOutput) => void;
@@ -293,8 +300,7 @@ interface ServerTab {
293300
agentOperating: boolean;
294301
fileChooserIntercepted: boolean;
295302
readonly onFileChooser: (chooser: FileChooser) => void;
296-
// Only the session that opened this popup can take its initial human handoff.
297-
handoffSessionId: string | null;
303+
handoff: PopupHandoff | null;
298304

299305
readonly isolatedContext: boolean;
300306

@@ -477,7 +483,7 @@ const make = Effect.gen(function* () {
477483
{
478484
readonly page: Page;
479485
readonly openerTabId: string;
480-
handoffSessionId: string | null;
486+
readonly handoff: PopupHandoff | null;
481487
}
482488
>();
483489
const closedSessions = new Set<string>();
@@ -735,6 +741,7 @@ const make = Effect.gen(function* () {
735741

736742
const createTab = async (snapshot: PreviewSessionSnapshot): Promise<ServerTab> => {
737743
const adopted = adoptedPages.get(tabKey(snapshot.threadId, snapshot.tabId));
744+
adoptedPages.delete(tabKey(snapshot.threadId, snapshot.tabId));
738745
const desktop =
739746
adopted === undefined && (await desktopRenders(snapshot))
740747
? await connectDesktop(snapshot)
@@ -789,7 +796,7 @@ const make = Effect.gen(function* () {
789796
agentOperating: false,
790797
fileChooserIntercepted: false,
791798
onFileChooser: (chooser) => void offerFileChooser(tab, chooser),
792-
handoffSessionId: adopted?.handoffSessionId ?? null,
799+
handoff: adopted?.handoff ?? null,
793800
isolatedContext,
794801
desktop: desktop === null ? null : { close: desktop.close },
795802
profileId: snapshot.profileId,
@@ -800,7 +807,7 @@ const make = Effect.gen(function* () {
800807
setting: snapshot.viewport ?? FILL_PREVIEW_VIEWPORT,
801808
colorScheme: "system",
802809
zoomFactor: 1,
803-
loading: false,
810+
loading: adopted !== undefined && snapshot.navStatus._tag === "Loading",
804811
closing: false,
805812
recording: null,
806813
recordingStart: null,
@@ -914,8 +921,6 @@ const make = Effect.gen(function* () {
914921
}
915922
page.on("close", () => dropTab(tab, true));
916923
page.on("crash", () => dropTab(tab, true));
917-
// The opener's controller may have released during setup.
918-
tab.handoffSessionId = adopted?.handoffSessionId ?? null;
919924
tabs.set(key, tab);
920925
reportLiveTabs();
921926
// Existing desktop and popup PDFs can start loading before interception attaches.
@@ -935,16 +940,19 @@ const make = Effect.gen(function* () {
935940
if (tab.control.agentId !== null) tab.control.track(tab.initialNavigation);
936941
else void tab.initialNavigation.catch(constVoid);
937942
} else if (adopted) {
938-
// Popup navigation can finish during setup, before this tab's listeners exist.
939-
// A load after setup settles this wait too, and the `load` listener reports that one.
943+
// An adopted page can finish loading before its listeners attach.
940944
let loadReported = false;
941945
page.once("load", () => {
942946
loadReported = true;
943947
});
948+
const generation = tab.navigationGeneration;
944949
void page
945950
.waitForLoadState("load")
946951
.then(() => {
947-
if (!loadReported) return reportLoaded(tab);
952+
if (!loadReported && tab.navigationGeneration === generation) {
953+
tab.loading = false;
954+
return reportLoaded(tab);
955+
}
948956
})
949957
.catch(constVoid);
950958
}
@@ -1234,6 +1242,7 @@ const make = Effect.gen(function* () {
12341242
return;
12351243
}
12361244
const url = popup.url();
1245+
const handoff = controller?.handoff ?? null;
12371246

12381247
await runPromise(
12391248
manager.open({
@@ -1252,12 +1261,13 @@ const make = Effect.gen(function* () {
12521261
adoptedPages.set(tabKey(snapshot.threadId, snapshot.tabId), {
12531262
page: popup,
12541263
openerTabId: opener.tabId,
1255-
handoffSessionId: controller?.sessionId ?? null,
1264+
handoff,
12561265
}),
12571266
}),
12581267
).then(
12591268
(snapshot) => {
1260-
if (controller !== undefined) controller.push({ _tag: "popup", tabId: snapshot.tabId });
1269+
if (handoff?.active)
1270+
controller?.push({ _tag: "popup", tabId: snapshot.tabId, handoffId: handoff.id });
12611271
},
12621272
async () => {
12631273
await popup.close().catch(constVoid);
@@ -1284,7 +1294,6 @@ const make = Effect.gen(function* () {
12841294
.finally(() => {
12851295
pendingTabs.delete(key);
12861296
closedPendingTabs.delete(key);
1287-
adoptedPages.delete(key);
12881297
});
12891298
pendingTabs.set(key, opening);
12901299
return opening;
@@ -2341,7 +2350,7 @@ const make = Effect.gen(function* () {
23412350
const viewer: ViewerState = {
23422351
id: NodeCrypto.randomUUID(),
23432352
canOperate: input.canOperate,
2344-
sessionId: input.sessionId,
2353+
handoff: { id: NodeCrypto.randomUUID(), sessionId: input.sessionId, active: true },
23452354
pressedKeys: new Map(),
23462355
pressedButtons: new Map(),
23472356
push: (next) => {
@@ -2386,9 +2395,13 @@ const make = Effect.gen(function* () {
23862395
if (
23872396
input.canOperate &&
23882397
tab.control.controller === null &&
2389-
(tab.control.agentId === null || input.sessionId === tab.handoffSessionId)
2398+
(tab.control.agentId === null ||
2399+
(tab.handoff?.active &&
2400+
input.handoffId === tab.handoff.id &&
2401+
input.sessionId !== undefined &&
2402+
input.sessionId === tab.handoff.sessionId))
23902403
) {
2391-
tab.handoffSessionId = null;
2404+
tab.handoff = null;
23922405
yield* Effect.promise(() => tab.control.take(viewer.id));
23932406
}
23942407
broadcastControl(tab);
@@ -2465,29 +2478,19 @@ const make = Effect.gen(function* () {
24652478
try {
24662479
if (message.type === "takeControl") {
24672480
const taking = tab.control.take(viewer.id);
2481+
if (!viewer.handoff.active) {
2482+
viewer.handoff = {
2483+
id: NodeCrypto.randomUUID(),
2484+
sessionId: input.sessionId,
2485+
active: true,
2486+
};
2487+
}
24682488
broadcastControl(tab);
24692489
await taking;
24702490
pushFileChooser(tab);
24712491
} else if (message.type === "releaseControl") {
2472-
// Do not disarm on disconnect: the opener stream may drop while switching tabs.
2473-
if (viewer.id === tab.control.controller) {
2474-
for (const candidate of tabs.values()) {
2475-
if (
2476-
candidate.openerTabId === tab.tabId &&
2477-
candidate.handoffSessionId === viewer.sessionId
2478-
) {
2479-
candidate.handoffSessionId = null;
2480-
}
2481-
}
2482-
for (const pending of adoptedPages.values()) {
2483-
if (
2484-
pending.openerTabId === tab.tabId &&
2485-
pending.handoffSessionId === viewer.sessionId
2486-
) {
2487-
pending.handoffSessionId = null;
2488-
}
2489-
}
2490-
}
2492+
// A stream disconnect during the popup switch must preserve the grant.
2493+
if (viewer.id === tab.control.controller) viewer.handoff.active = false;
24912494
const releasing = tab.control.release(viewer.id, () =>
24922495
releaseViewerInput(viewer, session),
24932496
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ it.effect.each([
111111
);
112112
const server = Context.get(services, HttpServer.HttpServer);
113113
const origin = HttpServer.formatAddress(server.address).replace(/^http/, "ws");
114-
const resource = `/api/preview-stream/ws?threadId=thread&tabId=tab&wsTicket=one-use-ticket${interactive ? "" : "&interactive=false"}`;
114+
const resource = `/api/preview-stream/ws?threadId=thread&tabId=tab&handoffId=popup-claim&wsTicket=one-use-ticket${interactive ? "" : "&interactive=false"}`;
115115
const received = Promise.withResolvers<void>();
116116
const socket = yield* Effect.acquireRelease(
117117
Effect.sync(() => new WebSocket(`${origin}${resource}`)),
@@ -147,6 +147,7 @@ it.effect.each([
147147
quality: 70,
148148
canOperate,
149149
sessionId: "stream-test",
150+
handoffId: "popup-claim",
150151
},
151152
]);
152153
expect(auth.requests).toEqual([resource]);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const makeHandler = Effect.fnUntraced(function* (browser: ServerBrowser.ServerBr
8686
tabId,
8787
canOperate,
8888
sessionId: session.sessionId,
89+
handoffId: params.get("handoffId") ?? undefined,
8990
maxWidth: intParam(params, "maxWidth", 1280, 7680),
9091
maxHeight: intParam(params, "maxHeight", 800, 4320),
9192
quality: intParam(params, "quality", DEFAULT_QUALITY, 100),

0 commit comments

Comments
 (0)