Skip to content

Commit 1489543

Browse files
roboclaw-botsteipete
authored andcommitted
fix(ui): keep pinned sidebar pages through reconnect
1 parent e1e2818 commit 1489543

16 files changed

Lines changed: 109 additions & 9 deletions

‎ui/src/app/gateway-store.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,19 @@ describe("createApplicationGateway connection phase", () => {
423423
expect(gateway.snapshot.phase).toBe("connecting");
424424
});
425425

426+
it("advances the connection revision only when credentials change", () => {
427+
const { gateway, current } = createStore();
428+
gateway.start();
429+
current().opts.onHello?.(HELLO);
430+
431+
expect(gateway.connectionRevision).toBe(0);
432+
gateway.connect({ sessionKey: "agent:main:other" });
433+
expect(gateway.connectionRevision).toBe(0);
434+
435+
gateway.connect({ token: "replacement-token" });
436+
expect(gateway.connectionRevision).toBe(1);
437+
});
438+
426439
it("keeps a newly selected Gateway's first retry at the login gate", () => {
427440
const { gateway, current } = createStore();
428441
gateway.start();

‎ui/src/app/gateway-store.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export function createApplicationGateway(
9191
...(options.bootstrapProfile ? { bootstrapProfile: options.bootstrapProfile } : {}),
9292
password: initialPassword,
9393
};
94+
let connectionRevision = 0;
9495
let snapshot: ApplicationGatewaySnapshot = {
9596
client: null,
9697
phase: "stopped",
@@ -293,6 +294,7 @@ export function createApplicationGateway(
293294
nextConnection.bootstrapToken !== connection.bootstrapToken ||
294295
nextConnection.bootstrapProfile !== connection.bootstrapProfile;
295296
if (credentialsChanged) {
297+
connectionRevision += 1;
296298
void clearStoredChatSnapshots();
297299
}
298300
const hasRequestedSessionKey = requestedSessionKey !== undefined;
@@ -542,6 +544,9 @@ export function createApplicationGateway(
542544
get connection() {
543545
return connection;
544546
},
547+
get connectionRevision() {
548+
return connectionRevision;
549+
},
545550
get eventLog() {
546551
return eventLog;
547552
},

‎ui/src/app/gateway.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type ApplicationGatewayConnectOptions = Partial<ApplicationGatewayConnect
4141
export type ApplicationGateway = {
4242
readonly snapshot: ApplicationGatewaySnapshot;
4343
readonly connection: ApplicationGatewayConnection;
44+
readonly connectionRevision: number;
4445
readonly eventLog: readonly EventLogEntry[];
4546
connect: (connection?: ApplicationGatewayConnectOptions) => void;
4647
setSessionKey: (sessionKey: string) => void;

‎ui/src/app/overlays-access.test-support.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export function createGatewayHarness(
5454
return snapshot;
5555
},
5656
connection: { gatewayUrl: "ws://gateway.test", password: "", token: "", bootstrapToken: "" },
57+
connectionRevision: 0,
5758
eventLog: [],
5859
connect,
5960
setSessionKey() {},

‎ui/src/components/command-palette.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function createGateway(
4949
return snapshot;
5050
},
5151
connection: { gatewayUrl: "ws://localhost", token: "", bootstrapToken: "", password: "" },
52+
connectionRevision: 0,
5253
eventLog: [],
5354
connect: () => undefined,
5455
setSessionKey: () => undefined,

‎ui/src/components/session-data-controller.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export class SessionDataController implements ReactiveController, SessionCatalog
9595
private activeSessionLineageRetryTimer: ReturnType<typeof globalThis.setTimeout> | null = null;
9696
private reconnectListRevision: number | null = null;
9797
private gatewaySource: ApplicationContext<RouteId>["gateway"] | null = null;
98+
private gatewayConnectionRevision = 0;
9899
private gatewayClient: GatewayBrowserClient | null = null;
99100
private gatewayConnected = false;
100101
// Bind mutation completions to one epoch so stale failures cannot cross reconnects.
@@ -422,19 +423,18 @@ export class SessionDataController implements ReactiveController, SessionCatalog
422423
return;
423424
}
424425
const gateway = this.context?.gateway;
425-
const sameClientDisconnected =
426+
const sameGatewayDisconnected =
426427
gateway !== undefined &&
427428
gateway === this.gatewaySource &&
428429
gateway.snapshot.client !== null &&
429-
gateway.snapshot.client === this.gatewayClient &&
430430
gateway.snapshot.phase !== "connected";
431-
if (sameClientDisconnected && this.reconnectListRevision === null) {
431+
if (sameGatewayDisconnected && this.reconnectListRevision === null) {
432432
this.reconnectListRevision = sessions.canonicalListRevision + 1;
433433
}
434434
const waitingForReconnectList =
435435
this.reconnectListRevision !== null &&
436436
sessions.canonicalListRevision < this.reconnectListRevision;
437-
if (!sameClientDisconnected && !waitingForReconnectList) {
437+
if (!sameGatewayDisconnected && !waitingForReconnectList) {
438438
// Keep the result and agent scope paired until the first canonical list
439439
// after reconnect; chat startup may publish a partial reconciliation first.
440440
this.reconnectListRevision = null;
@@ -465,9 +465,12 @@ export class SessionDataController implements ReactiveController, SessionCatalog
465465
private synchronizeGateway(gateway: ApplicationContext<RouteId>["gateway"]): void {
466466
const client = gateway.snapshot.client;
467467
const connected = gateway.snapshot.phase === "connected";
468+
const sessionSourceChanged =
469+
gateway !== this.gatewaySource ||
470+
gateway.connectionRevision !== this.gatewayConnectionRevision;
468471
const clientChanged = client !== this.gatewayClient;
469472
const connectedStarted = connected && !this.gatewayConnected;
470-
const sourceOrClientChanged = gateway !== this.gatewaySource || client !== this.gatewayClient;
473+
const sourceOrClientChanged = sessionSourceChanged || clientChanged;
471474
const connectionChanged = connected !== this.gatewayConnected;
472475
// Presence and auth snapshots must not retire this client's in-flight
473476
// native or catalog pages unless its connection phase actually changes.
@@ -476,6 +479,7 @@ export class SessionDataController implements ReactiveController, SessionCatalog
476479
}
477480
this.invalidateSessionMutations();
478481
this.gatewaySource = gateway;
482+
this.gatewayConnectionRevision = gateway.connectionRevision;
479483
this.gatewayClient = client;
480484
this.gatewayConnected = connected;
481485
this.presenceInstanceId = client?.instanceId;
@@ -493,7 +497,10 @@ export class SessionDataController implements ReactiveController, SessionCatalog
493497
}
494498
return;
495499
}
496-
this.clearSessionCache();
500+
// Session rows belong to the logical Gateway, not one replaceable socket client.
501+
if (sessionSourceChanged) {
502+
this.clearSessionCache();
503+
}
497504
this.resetSessionCatalogConnection();
498505
if (connected && this.sessionsSource && this.host.sidebarSessionStatusFilter() !== "active") {
499506
void this.refreshSidebarSessions();

‎ui/src/e2e/session-management.sidebar.e2e.test.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ suite.define(() => {
487487
}
488488
});
489489

490-
it("keeps sidebar sessions visible through a same-client Gateway reconnect", async () => {
490+
it("keeps sidebar sessions visible through transport and client replacement reconnects", async () => {
491491
const context = await suite.browser.newContext({
492492
locale: "en-US",
493493
serviceWorkers: "block",
@@ -499,7 +499,10 @@ suite.define(() => {
499499
const gateway = await installMockGateway(page, {
500500
methodResponses: {
501501
"sessions.list": sessionsListResponse([
502-
sessionRow(sessionKey, "Disconnect proof", Date.parse("2026-07-01T16:00:00.000Z")),
502+
sessionRow(sessionKey, "Disconnect proof", Date.parse("2026-07-01T16:00:00.000Z"), {
503+
pinned: true,
504+
pinnedAt: Date.parse("2026-07-01T16:00:00.000Z"),
505+
}),
503506
sessionRow(otherSessionKeys[0], "Other A", Date.parse("2026-07-01T15:59:00.000Z")),
504507
sessionRow(otherSessionKeys[1], "Other B", Date.parse("2026-07-01T15:58:00.000Z")),
505508
]),
@@ -510,7 +513,9 @@ suite.define(() => {
510513
try {
511514
await page.goto(`${suite.server.baseUrl}chat`);
512515
const sidebarRow = page.locator(`.sidebar-recent-session[data-session-key="${sessionKey}"]`);
516+
const pinnedEntry = page.locator(`[data-sidebar-entry="session:${sessionKey}"]`);
513517
await sidebarRow.waitFor({ state: "visible", timeout: 10_000 });
518+
await expect.poll(() => pinnedEntry.count()).toBe(1);
514519
const sidebarRows = page.locator(".sidebar-recent-session");
515520
await expect.poll(() => sidebarRows.count()).toBe(3);
516521
const initialListCount = (await gateway.getRequests("sessions.list")).length;
@@ -530,6 +535,15 @@ suite.define(() => {
530535
await expect
531536
.poll(() => gateway.getSocketCount(), { timeout: 15_000 })
532537
.toBe(socketsBefore + 1);
538+
await page.locator(".sidebar-identity-card").click();
539+
const retryConnection = page.locator(
540+
'wa-dropdown.sidebar-identity-menu wa-dropdown-item[value="command:retry-connect"]',
541+
);
542+
await retryConnection.waitFor({ state: "visible", timeout: 10_000 });
543+
await retryConnection.click();
544+
await expect.poll(() => pinnedEntry.count()).toBe(1);
545+
await captureUiProof(page, "sidebar-sessions-during-client-replacement.png");
546+
533547
await gateway.deferNext("sessions.list", { includeLastMessage: true });
534548
await gateway.setOnline(true);
535549
await waitForControlUiGatewayReady(page);

‎ui/src/lib/session-pull-requests.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function createGatewayHarness() {
5353
return snapshot;
5454
},
5555
connection: { gatewayUrl: "ws://example.test", token: "", bootstrapToken: "", password: "" },
56+
connectionRevision: 0,
5657
eventLog: [],
5758
subscribe: subscribeSnapshots,
5859
subscribeEvents,

‎ui/src/lib/session-viewer-presence.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function createGatewayHarness() {
3636
return snapshot;
3737
},
3838
connection: { gatewayUrl: "ws://example.test", token: "", bootstrapToken: "", password: "" },
39+
connectionRevision: 0,
3940
eventLog: [],
4041
subscribe: vi.fn((listener: (value: ApplicationGatewaySnapshot) => void) => {
4142
listeners.add(listener);

‎ui/src/pages/chat/chat-page.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ function setViewerPresenceContext(page: ChatPage) {
187187
lastErrorCode: null,
188188
},
189189
connection: { gatewayUrl: "ws://example.test", token: "", bootstrapToken: "", password: "" },
190+
connectionRevision: 0,
190191
eventLog: [],
191192
connect: vi.fn(),
192193
setSessionKey: vi.fn(),

0 commit comments

Comments
 (0)