fix(preview): use the visible browser for new agent sessions (#13064)
This commit is contained in:
1 parent
d4cd7d5c33
commit
894d33419d
5 files changed
+371
-14
No files matched your search
@@ -685,6 +685,7 @@ it.effect("pins a provider session to its initial host despite later focus chang
|
||||
environmentId: scope.environmentId,
|
||||
connectionId: "connection-stale",
|
||||
focused: true,
|
||||
liveTabs: [{ threadId: scope.threadId, tabId: PreviewTabId.make("stale-tab") }],
|
||||
});
|
||||
expect(yield* broker.invoke<string>({ scope, operation: "status", input: {} })).toBe(
|
||||
"second",
|
||||
@@ -725,6 +726,130 @@ it.effect("pins a provider session to its initial host despite later focus chang
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("prefers the live tab owner for new sessions without moving existing leases", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const broker = yield* makeBroker;
|
||||
const connections = new Map<string, string>();
|
||||
for (const clientId of ["owner", "other"]) {
|
||||
const requests = requestsFrom(
|
||||
yield* broker.connect(makeHost({ clientId })),
|
||||
(connectionId) => connections.set(clientId, connectionId),
|
||||
);
|
||||
yield* Stream.runForEach(requests, (request) =>
|
||||
broker.respond({
|
||||
clientId,
|
||||
connectionId: request.connectionId,
|
||||
requestId: request.requestId,
|
||||
ok: true,
|
||||
result: clientId,
|
||||
}),
|
||||
).pipe(Effect.forkScoped);
|
||||
}
|
||||
yield* Effect.yieldNow;
|
||||
yield* broker.focusHost({
|
||||
clientId: "owner",
|
||||
environmentId: scope.environmentId,
|
||||
connectionId: connections.get("owner")!,
|
||||
focused: false,
|
||||
liveTabs: [
|
||||
{ threadId: scope.threadId, tabId: PreviewTabId.make("signed-in"), visible: true },
|
||||
],
|
||||
});
|
||||
yield* broker.focusHost({
|
||||
clientId: "other",
|
||||
environmentId: scope.environmentId,
|
||||
connectionId: connections.get("other")!,
|
||||
focused: true,
|
||||
liveTabs: [
|
||||
{ threadId: scope.threadId, tabId: PreviewTabId.make("signed-in"), visible: false },
|
||||
{
|
||||
threadId: ThreadId.make("another-thread"),
|
||||
tabId: PreviewTabId.make("different-tab"),
|
||||
visible: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(yield* broker.invoke<string>({ scope, operation: "evaluate", input: {} })).toBe(
|
||||
"owner",
|
||||
);
|
||||
expect(
|
||||
yield* broker.invoke<string>({
|
||||
scope: { ...scope, providerSessionId: "explicit-owner" },
|
||||
tabId: PreviewTabId.make("signed-in"),
|
||||
operation: "snapshot",
|
||||
input: {},
|
||||
}),
|
||||
).toBe("owner");
|
||||
expect(
|
||||
yield* broker.invoke<string>({
|
||||
scope: { ...scope, providerSessionId: "other-tab" },
|
||||
tabId: PreviewTabId.make("different-tab"),
|
||||
operation: "evaluate",
|
||||
input: {},
|
||||
}),
|
||||
).toBe("other");
|
||||
|
||||
yield* broker.focusHost({
|
||||
clientId: "owner",
|
||||
environmentId: scope.environmentId,
|
||||
connectionId: connections.get("owner")!,
|
||||
focused: false,
|
||||
liveTabs: [],
|
||||
});
|
||||
expect(yield* broker.invoke<string>({ scope, operation: "evaluate", input: {} })).toBe(
|
||||
"owner",
|
||||
);
|
||||
expect(
|
||||
yield* broker.invoke<string>({
|
||||
scope: { ...scope, providerSessionId: "after-tab-closed" },
|
||||
operation: "evaluate",
|
||||
input: {},
|
||||
}),
|
||||
).toBe("other");
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("prefers a focused host over unrelated extra capabilities for a new session", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const broker = yield* makeBroker;
|
||||
let focusedConnectionId = "";
|
||||
for (const [clientId, supportedOperations] of [
|
||||
["focused", ["status"]],
|
||||
["background", ["status", "resize"]],
|
||||
] as const) {
|
||||
const requests = requestsFrom(
|
||||
yield* broker.connect(makeHost({ clientId, supportedOperations })),
|
||||
(connectionId) => {
|
||||
if (clientId === "focused") focusedConnectionId = connectionId;
|
||||
},
|
||||
);
|
||||
yield* Stream.runForEach(requests, (request) =>
|
||||
broker.respond({
|
||||
clientId,
|
||||
connectionId: request.connectionId,
|
||||
requestId: request.requestId,
|
||||
ok: true,
|
||||
result: clientId,
|
||||
}),
|
||||
).pipe(Effect.forkScoped);
|
||||
}
|
||||
yield* Effect.yieldNow;
|
||||
yield* broker.focusHost({
|
||||
clientId: "focused",
|
||||
environmentId: scope.environmentId,
|
||||
connectionId: focusedConnectionId,
|
||||
focused: true,
|
||||
});
|
||||
expect(yield* broker.invoke<string>({ scope, operation: "status", input: {} })).toBe(
|
||||
"focused",
|
||||
);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("does not route new operations to legacy hosts that did not advertise support", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
@@ -921,6 +1046,7 @@ it.effect("fails over a pinned provider session only after its host disconnects"
|
||||
environmentId: scope.environmentId,
|
||||
connectionId: firstConnectionId,
|
||||
focused: true,
|
||||
liveTabs: [{ threadId: scope.threadId, tabId: firstTabId }],
|
||||
});
|
||||
expect(yield* broker.invoke({ scope, operation: "open", input: {} })).toEqual({
|
||||
host: "first",
|
||||
|
||||
@@ -73,6 +73,7 @@ interface ClientConnection {
|
||||
readonly environmentId: PreviewAutomationHost["environmentId"];
|
||||
readonly supportedOperations: ReadonlySet<PreviewAutomationOperation>;
|
||||
readonly focused: boolean;
|
||||
readonly liveTabs: NonNullable<PreviewAutomationHostFocus["liveTabs"]>;
|
||||
readonly focusOrder: number;
|
||||
readonly queue: Queue.Queue<PreviewAutomationStreamEvent, Cause.Done>;
|
||||
}
|
||||
@@ -376,6 +377,7 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
|
||||
environmentId: host.environmentId,
|
||||
supportedOperations: new Set(host.supportedOperations ?? PREVIEW_AUTOMATION_V1_OPERATIONS),
|
||||
focused: false,
|
||||
liveTabs: [],
|
||||
focusOrder: 0,
|
||||
queue,
|
||||
};
|
||||
@@ -432,6 +434,7 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
|
||||
clients.set(host.clientId, {
|
||||
...currentHost,
|
||||
focused: host.focused,
|
||||
liveTabs: host.liveTabs ?? currentHost.liveTabs,
|
||||
focusOrder: host.focused ? focusSequence : currentHost.focusOrder,
|
||||
});
|
||||
return { ...current, clients, focusSequence };
|
||||
@@ -492,6 +495,13 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
|
||||
// operation is not silently moved to a newer client: the caller gets a
|
||||
// capability failure and can deliberately start a fresh provider
|
||||
// session. A dead lease is pruned above and may fail over.
|
||||
const ownsTargetTab = (host: ClientConnection, visibleOnly = false) =>
|
||||
host.liveTabs.some(
|
||||
(tab) =>
|
||||
tab.threadId === input.scope.threadId &&
|
||||
(!visibleOnly || tab.visible === true) &&
|
||||
(input.tabId === undefined || tab.tabId === input.tabId),
|
||||
);
|
||||
const connection =
|
||||
hasLiveAssignment && supportsOperation(assignedConnection, input.operation)
|
||||
? assignedConnection
|
||||
@@ -505,7 +515,8 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
|
||||
)
|
||||
.sort(
|
||||
(left, right) =>
|
||||
right.supportedOperations.size - left.supportedOperations.size ||
|
||||
Number(ownsTargetTab(right, true)) - Number(ownsTargetTab(left, true)) ||
|
||||
Number(ownsTargetTab(right)) - Number(ownsTargetTab(left)) ||
|
||||
Number(right.focused) - Number(left.focused) ||
|
||||
right.focusOrder - left.focusOrder,
|
||||
)[0];
|
||||
|
||||
Reference in new issue
Block a user