Skip to content

Commit 894d334

Browse files
authored
fix(preview): use the visible browser for new agent sessions (#13064)
1 parent d4cd7d5 commit 894d334

5 files changed

Lines changed: 371 additions & 14 deletions

File tree

‎apps/server/src/mcp/PreviewAutomationBroker.test.ts‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ it.effect("pins a provider session to its initial host despite later focus chang
685685
environmentId: scope.environmentId,
686686
connectionId: "connection-stale",
687687
focused: true,
688+
liveTabs: [{ threadId: scope.threadId, tabId: PreviewTabId.make("stale-tab") }],
688689
});
689690
expect(yield* broker.invoke<string>({ scope, operation: "status", input: {} })).toBe(
690691
"second",
@@ -725,6 +726,130 @@ it.effect("pins a provider session to its initial host despite later focus chang
725726
),
726727
);
727728

729+
it.effect("prefers the live tab owner for new sessions without moving existing leases", () =>
730+
Effect.scoped(
731+
Effect.gen(function* () {
732+
const broker = yield* makeBroker;
733+
const connections = new Map<string, string>();
734+
for (const clientId of ["owner", "other"]) {
735+
const requests = requestsFrom(
736+
yield* broker.connect(makeHost({ clientId })),
737+
(connectionId) => connections.set(clientId, connectionId),
738+
);
739+
yield* Stream.runForEach(requests, (request) =>
740+
broker.respond({
741+
clientId,
742+
connectionId: request.connectionId,
743+
requestId: request.requestId,
744+
ok: true,
745+
result: clientId,
746+
}),
747+
).pipe(Effect.forkScoped);
748+
}
749+
yield* Effect.yieldNow;
750+
yield* broker.focusHost({
751+
clientId: "owner",
752+
environmentId: scope.environmentId,
753+
connectionId: connections.get("owner")!,
754+
focused: false,
755+
liveTabs: [
756+
{ threadId: scope.threadId, tabId: PreviewTabId.make("signed-in"), visible: true },
757+
],
758+
});
759+
yield* broker.focusHost({
760+
clientId: "other",
761+
environmentId: scope.environmentId,
762+
connectionId: connections.get("other")!,
763+
focused: true,
764+
liveTabs: [
765+
{ threadId: scope.threadId, tabId: PreviewTabId.make("signed-in"), visible: false },
766+
{
767+
threadId: ThreadId.make("another-thread"),
768+
tabId: PreviewTabId.make("different-tab"),
769+
visible: true,
770+
},
771+
],
772+
});
773+
expect(yield* broker.invoke<string>({ scope, operation: "evaluate", input: {} })).toBe(
774+
"owner",
775+
);
776+
expect(
777+
yield* broker.invoke<string>({
778+
scope: { ...scope, providerSessionId: "explicit-owner" },
779+
tabId: PreviewTabId.make("signed-in"),
780+
operation: "snapshot",
781+
input: {},
782+
}),
783+
).toBe("owner");
784+
expect(
785+
yield* broker.invoke<string>({
786+
scope: { ...scope, providerSessionId: "other-tab" },
787+
tabId: PreviewTabId.make("different-tab"),
788+
operation: "evaluate",
789+
input: {},
790+
}),
791+
).toBe("other");
792+
793+
yield* broker.focusHost({
794+
clientId: "owner",
795+
environmentId: scope.environmentId,
796+
connectionId: connections.get("owner")!,
797+
focused: false,
798+
liveTabs: [],
799+
});
800+
expect(yield* broker.invoke<string>({ scope, operation: "evaluate", input: {} })).toBe(
801+
"owner",
802+
);
803+
expect(
804+
yield* broker.invoke<string>({
805+
scope: { ...scope, providerSessionId: "after-tab-closed" },
806+
operation: "evaluate",
807+
input: {},
808+
}),
809+
).toBe("other");
810+
}),
811+
),
812+
);
813+
814+
it.effect("prefers a focused host over unrelated extra capabilities for a new session", () =>
815+
Effect.scoped(
816+
Effect.gen(function* () {
817+
const broker = yield* makeBroker;
818+
let focusedConnectionId = "";
819+
for (const [clientId, supportedOperations] of [
820+
["focused", ["status"]],
821+
["background", ["status", "resize"]],
822+
] as const) {
823+
const requests = requestsFrom(
824+
yield* broker.connect(makeHost({ clientId, supportedOperations })),
825+
(connectionId) => {
826+
if (clientId === "focused") focusedConnectionId = connectionId;
827+
},
828+
);
829+
yield* Stream.runForEach(requests, (request) =>
830+
broker.respond({
831+
clientId,
832+
connectionId: request.connectionId,
833+
requestId: request.requestId,
834+
ok: true,
835+
result: clientId,
836+
}),
837+
).pipe(Effect.forkScoped);
838+
}
839+
yield* Effect.yieldNow;
840+
yield* broker.focusHost({
841+
clientId: "focused",
842+
environmentId: scope.environmentId,
843+
connectionId: focusedConnectionId,
844+
focused: true,
845+
});
846+
expect(yield* broker.invoke<string>({ scope, operation: "status", input: {} })).toBe(
847+
"focused",
848+
);
849+
}),
850+
),
851+
);
852+
728853
it.effect("does not route new operations to legacy hosts that did not advertise support", () =>
729854
Effect.scoped(
730855
Effect.gen(function* () {
@@ -921,6 +1046,7 @@ it.effect("fails over a pinned provider session only after its host disconnects"
9211046
environmentId: scope.environmentId,
9221047
connectionId: firstConnectionId,
9231048
focused: true,
1049+
liveTabs: [{ threadId: scope.threadId, tabId: firstTabId }],
9241050
});
9251051
expect(yield* broker.invoke({ scope, operation: "open", input: {} })).toEqual({
9261052
host: "first",

‎apps/server/src/mcp/PreviewAutomationBroker.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ interface ClientConnection {
7373
readonly environmentId: PreviewAutomationHost["environmentId"];
7474
readonly supportedOperations: ReadonlySet<PreviewAutomationOperation>;
7575
readonly focused: boolean;
76+
readonly liveTabs: NonNullable<PreviewAutomationHostFocus["liveTabs"]>;
7677
readonly focusOrder: number;
7778
readonly queue: Queue.Queue<PreviewAutomationStreamEvent, Cause.Done>;
7879
}
@@ -376,6 +377,7 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
376377
environmentId: host.environmentId,
377378
supportedOperations: new Set(host.supportedOperations ?? PREVIEW_AUTOMATION_V1_OPERATIONS),
378379
focused: false,
380+
liveTabs: [],
379381
focusOrder: 0,
380382
queue,
381383
};
@@ -432,6 +434,7 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
432434
clients.set(host.clientId, {
433435
...currentHost,
434436
focused: host.focused,
437+
liveTabs: host.liveTabs ?? currentHost.liveTabs,
435438
focusOrder: host.focused ? focusSequence : currentHost.focusOrder,
436439
});
437440
return { ...current, clients, focusSequence };
@@ -492,6 +495,13 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
492495
// operation is not silently moved to a newer client: the caller gets a
493496
// capability failure and can deliberately start a fresh provider
494497
// session. A dead lease is pruned above and may fail over.
498+
const ownsTargetTab = (host: ClientConnection, visibleOnly = false) =>
499+
host.liveTabs.some(
500+
(tab) =>
501+
tab.threadId === input.scope.threadId &&
502+
(!visibleOnly || tab.visible === true) &&
503+
(input.tabId === undefined || tab.tabId === input.tabId),
504+
);
495505
const connection =
496506
hasLiveAssignment && supportsOperation(assignedConnection, input.operation)
497507
? assignedConnection
@@ -505,7 +515,8 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
505515
)
506516
.sort(
507517
(left, right) =>
508-
right.supportedOperations.size - left.supportedOperations.size ||
518+
Number(ownsTargetTab(right, true)) - Number(ownsTargetTab(left, true)) ||
519+
Number(ownsTargetTab(right)) - Number(ownsTargetTab(left)) ||
509520
Number(right.focused) - Number(left.focused) ||
510521
right.focusOrder - left.focusOrder,
511522
)[0];

0 commit comments

Comments
 (0)