Skip to content

Commit e60d474

Browse files
committed
fix(ui): preserve roster intent when operator identity arrives
1 parent 0140d87 commit e60d474

2 files changed

Lines changed: 99 additions & 68 deletions

File tree

‎ui/src/lib/sessions/connection-hydration.test.ts‎

Lines changed: 92 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ describe("session connection hydration", () => {
189189
expect(request.mock.calls.filter(([method]) => method === "sessions.list")).toHaveLength(0);
190190
expect(sessions.state.result).toBeNull();
191191
const queuedRefresh = sessions.refresh({ agentId: "other", search: "queued", force: true });
192+
snapshot = { ...snapshot, selfUser: { id: "collaborator", name: "Collaborator" } };
193+
gatewayListener?.(snapshot);
192194

193195
bootstrap.resolve({ subscribed: true, list: roster });
194196
await waitForFast(() =>
@@ -260,77 +262,100 @@ describe("session connection hydration", () => {
260262
sessions.dispose();
261263
});
262264

263-
it("rehydrates owner sessions when identity arrives on the same connection", async () => {
264-
let resolveList: (result: SessionsListResult) => void = () => undefined;
265-
const pendingList = new Promise<SessionsListResult>((resolve) => {
266-
resolveList = resolve;
267-
});
268-
let listCalls = 0;
269-
const result: SessionsListResult = {
270-
ts: 1,
271-
path: "(multiple)",
272-
count: 0,
273-
defaults: { modelProvider: null, model: null, contextTokens: null },
274-
sessions: [],
275-
};
276-
const request = vi.fn(async (method: string, _params?: Record<string, unknown>) => {
277-
if (method === "sessions.subscribe") {
278-
return { subscribed: true };
279-
}
280-
if (method === "sessions.list") {
281-
listCalls += 1;
282-
return listCalls === 1 ? await pendingList : result;
283-
}
284-
throw new Error(`Unexpected request: ${method}`);
285-
});
286-
const client = { request } as unknown as GatewayBrowserClient;
287-
let snapshot = {
288-
client: null as GatewayBrowserClient | null,
289-
phase: "reconnecting" as "connected" | "reconnecting",
290-
sessionKey: "agent:main:main",
291-
assistantAgentId: "main" as string | null,
292-
hello: null as GatewayHelloOk | null,
293-
canvasPluginSurfaceUrl: null as string | null,
294-
selfUser: null as { id: string; name?: string } | null,
295-
};
296-
let gatewayListener: ((next: typeof snapshot) => void) | undefined;
297-
const sessions = createSessionCapability({
298-
get snapshot() {
299-
return snapshot;
300-
},
301-
subscribe(listener) {
302-
gatewayListener = listener;
303-
return () => undefined;
304-
},
305-
subscribeEvents: () => () => undefined,
306-
});
265+
it.each(["main", "work"])(
266+
"rehydrates the current %s roster when identity arrives on the same connection",
267+
async (agentId) => {
268+
let resolveList: (result: SessionsListResult) => void = () => undefined;
269+
const pendingList = new Promise<SessionsListResult>((resolve) => {
270+
resolveList = resolve;
271+
});
272+
let listCalls = 0;
273+
const result: SessionsListResult = {
274+
ts: 1,
275+
path: "(multiple)",
276+
count: 0,
277+
defaults: { modelProvider: null, model: null, contextTokens: null },
278+
sessions: [],
279+
};
280+
const request = vi.fn(async (method: string, _params?: Record<string, unknown>) => {
281+
if (method === "sessions.subscribe") {
282+
return { subscribed: true };
283+
}
284+
if (method === "sessions.list") {
285+
listCalls += 1;
286+
return listCalls === 1 ? await pendingList : result;
287+
}
288+
throw new Error(`Unexpected request: ${method}`);
289+
});
290+
const client = { request } as unknown as GatewayBrowserClient;
291+
let snapshot = {
292+
client: null as GatewayBrowserClient | null,
293+
phase: "reconnecting" as "connected" | "reconnecting",
294+
sessionKey: "agent:main:main",
295+
assistantAgentId: "main" as string | null,
296+
hello: null as GatewayHelloOk | null,
297+
canvasPluginSurfaceUrl: null as string | null,
298+
selfUser: null as { id: string; name?: string } | null,
299+
};
300+
let gatewayListener: ((next: typeof snapshot) => void) | undefined;
301+
const sessions = createSessionCapability({
302+
get snapshot() {
303+
return snapshot;
304+
},
305+
subscribe(listener) {
306+
gatewayListener = listener;
307+
return () => undefined;
308+
},
309+
subscribeEvents: () => () => undefined,
310+
});
307311

308-
snapshot = { ...snapshot, client, phase: "connected" };
309-
gatewayListener?.(snapshot);
310-
await waitForFast(() => expect(listCalls).toBe(1));
312+
snapshot = { ...snapshot, client, phase: "connected" };
313+
gatewayListener?.(snapshot);
314+
await waitForFast(() => expect(listCalls).toBe(1));
311315

312-
snapshot = { ...snapshot, canvasPluginSurfaceUrl: "https://gateway.example.test/canvas" };
313-
gatewayListener?.(snapshot);
314-
await Promise.resolve();
315-
expect(listCalls).toBe(1);
316+
snapshot = { ...snapshot, canvasPluginSurfaceUrl: "https://gateway.example.test/canvas" };
317+
gatewayListener?.(snapshot);
318+
await Promise.resolve();
319+
expect(listCalls).toBe(1);
320+
321+
if (agentId === "work") {
322+
resolveList(result);
323+
await waitForFast(() => expect(sessions.state.result).toBe(result));
324+
// The global route remains unchanged while the picker selects another roster.
325+
snapshot = { ...snapshot, sessionKey: "global" };
326+
await sessions.refresh({ agentId, search: "selected", force: true });
327+
expect(sessions.state.agentId).toBe(agentId);
328+
}
329+
snapshot = { ...snapshot, selfUser: { id: "operator", name: "Operator" } };
330+
gatewayListener?.(snapshot);
331+
resolveList(result);
332+
await waitForFast(() => expect(listCalls).toBe(agentId === "work" ? 3 : 2));
316333

317-
snapshot = { ...snapshot, selfUser: { id: "operator", name: "Operator" } };
318-
gatewayListener?.(snapshot);
319-
resolveList(result);
320-
await waitForFast(() => expect(listCalls).toBe(2));
334+
expect(
335+
request.mock.calls
336+
.filter(([method]) => method === "sessions.list")
337+
.map(([, params]) => params),
338+
).toEqual([
339+
expect.not.objectContaining({ ownerFirst: expect.anything() }),
340+
...(agentId === "work" ? [expect.objectContaining({ agentId, search: "selected" })] : []),
341+
expect.objectContaining({
342+
agentId,
343+
limit: SIDEBAR_SESSION_ROSTER_LIMIT,
344+
...(agentId === "work" ? { search: "selected" } : { ownerFirst: true }),
345+
}),
346+
]);
347+
await waitForFast(() => expect(sessions.state.agentId).toBe(agentId));
348+
expect(sessions.state.result?.sessions).toEqual([]);
321349

322-
expect(
323-
request.mock.calls
324-
.filter(([method]) => method === "sessions.list")
325-
.map(([, params]) => params),
326-
).toEqual([
327-
expect.not.objectContaining({ ownerFirst: expect.anything() }),
328-
expect.objectContaining({ ownerFirst: true, limit: SIDEBAR_SESSION_ROSTER_LIMIT }),
329-
]);
330-
expect(sessions.state.result?.sessions).toEqual([]);
331-
expect(sessions.state.agentId).toBe("main");
332-
sessions.dispose();
333-
});
350+
// A new connection still derives scope from the current gateway route.
351+
snapshot = { ...snapshot, phase: "reconnecting" };
352+
gatewayListener?.(snapshot);
353+
snapshot = { ...snapshot, phase: "connected", sessionKey: "agent:main:main" };
354+
gatewayListener?.(snapshot);
355+
await waitForFast(() => expect(sessions.state.agentId).toBe("main"));
356+
sessions.dispose();
357+
},
358+
);
334359

335360
it("hydrates again after the current client reconnects", async () => {
336361
let listCalls = 0;

‎ui/src/lib/sessions/index.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,19 @@ export function createSessionCapability(gateway: SessionGateway): SessionCapabil
486486
});
487487
return;
488488
}
489-
if (hydratedClient !== next.client || hydratedSelfUserId !== selfUserId) {
489+
const hydrateConnection = hydratedClient !== next.client;
490+
if (hydrateConnection || hydratedSelfUserId !== selfUserId) {
490491
const scope = connection.capture();
491492
if (!scope) {
492493
return;
493494
}
494495
hydratedClient = scope.client;
495496
hydratedSelfUserId = selfUserId;
497+
if (!hydrateConnection) {
498+
// Identity updates refresh the current roster without displacing queued picker intent.
499+
roster.scheduleEvent();
500+
return;
501+
}
496502
void (async () => {
497503
if (connection.isCurrent(scope)) {
498504
const sessionKey = gateway.snapshot.sessionKey?.trim();

0 commit comments

Comments
 (0)