Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Console: let only the latest status check write, and test the socket's 4401 (CHOO-4224)

Simon Flackf33becad3e1c
diff --git a/console/apps/switch-console-desktop/src/main/core/user-changes/user-changes.test.ts b/console/apps/switch-console-desktop/src/main/core/user-changes/user-changes.test.ts
index 197a2855d..e53c8f7aa 100644
--- a/console/apps/switch-console-desktop/src/main/core/user-changes/user-changes.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/user-changes/user-changes.test.ts
@@ -5,6 +5,8 @@ import { createUserChangesService, type UserChangesDeps } from './user-changes';
vi.mock('@main/lib/events', () => ({ events: { emit: vi.fn() } }));
vi.mock('@main/lib/logger', () => ({ log: { debug: vi.fn() } }));
vi.mock('@main/core/switch-servers/gateway-client', () => ({ gatewaySocketTarget: vi.fn() }));
+const noteSessionEnded = vi.hoisted(() => vi.fn());
+vi.mock('@main/core/switch-servers/session-ended', () => ({ noteSessionEnded }));
vi.mock('@main/core/workspaces/workspace-session', () => ({
withReachableServerWorkspaceSession: vi.fn(),
}));
@@ -114,6 +116,26 @@ describe('user changes', () => {
expect(socket.sent).toEqual([{ type: 'pong' }]);
});
+ it('says the session ended when the gateway closes the socket with 4401', async () => {
+ // The gateway closes it so once the cookie it was opened with expires, or
+ // at the next ping after the account's sessions end (a password change).
+ const { service } = setup();
+ service.watch('s1');
+ await tick();
+ noteSessionEnded.mockClear();
+ FakeSocket.opened[0].fire('close', { code: 4401 });
+ expect(noteSessionEnded).toHaveBeenCalledExactlyOnceWith('s1');
+ });
+
+ it('says nothing about the session for any other close', async () => {
+ const { service } = setup();
+ service.watch('s1');
+ await tick();
+ noteSessionEnded.mockClear();
+ FakeSocket.opened[0].fire('close', { code: 1012 });
+ expect(noteSessionEnded).not.toHaveBeenCalled();
+ });
+
it('goes down on close and opens again while watched', async () => {
const { service, emitted, timers } = setup();
service.watch('s1');
diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-servers/switch-servers-store.test.ts b/console/apps/switch-console-desktop/src/renderer/features/switch-servers/switch-servers-store.test.ts
index 350c6d311..7cc4236d3 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/switch-servers/switch-servers-store.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-servers/switch-servers-store.test.ts
@@ -825,6 +825,50 @@ describe('a session the server has ended', () => {
expect(store.isConnected('a')).toBe(true);
});
+ it('keeps the answer of the latest check when an earlier one lands after it', async () => {
+ // A check that set out with the old cookie (refused) is overtaken by the
+ // one the account page starts once the new cookie is kept (accepted). The
+ // old one landing last must not show the server signed out again.
+ const store = newStore([server('a')]);
+ getConnectionStatus.mockResolvedValueOnce({ serverId: 'a', connected: true, user });
+ await store.refreshStatus('a');
+
+ const withOldCookie = deferred<unknown>();
+ const withNewCookie = deferred<unknown>();
+ getConnectionStatus.mockReturnValueOnce(withOldCookie.promise);
+ getConnectionStatus.mockReturnValueOnce(withNewCookie.promise);
+ const first = store.refreshStatus('a');
+ const second = store.refreshStatus('a');
+
+ withNewCookie.resolve({ serverId: 'a', connected: true, user });
+ await second;
+ expect(store.refreshing.has('a')).toBe(false);
+ withOldCookie.resolve({ serverId: 'a', connected: false, user: null });
+ await first;
+
+ expect(store.isConnected('a')).toBe(true);
+ expect(store.refreshing.has('a')).toBe(false);
+ });
+
+ it('keeps the running check marked as running when an earlier one lands first', async () => {
+ const store = newStore([server('a')]);
+ const earlier = deferred<unknown>();
+ const later = deferred<unknown>();
+ getConnectionStatus.mockReturnValueOnce(earlier.promise);
+ getConnectionStatus.mockReturnValueOnce(later.promise);
+ const first = store.refreshStatus('a');
+ const second = store.refreshStatus('a');
+
+ earlier.resolve({ serverId: 'a', connected: false, user: null });
+ await first;
+ expect(store.refreshing.has('a')).toBe(true);
+
+ later.resolve({ serverId: 'a', connected: true, user });
+ await second;
+ expect(store.refreshing.has('a')).toBe(false);
+ expect(store.isConnected('a')).toBe(true);
+ });
+
it('listens for the main process saying so', async () => {
expect(onSessionEnded).toBeDefined();
switchServersStore.servers = [server('a')];
diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-servers/switch-servers-store.ts b/console/apps/switch-console-desktop/src/renderer/features/switch-servers/switch-servers-store.ts
index acdf59483..908064665 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/switch-servers/switch-servers-store.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-servers/switch-servers-store.ts
@@ -70,6 +70,13 @@ export class SwitchServersStore {
loaded = false;
/** Server ids with an in-flight status refresh. */
readonly refreshing = new Set<string>();
+ /**
+ * The latest status check started for each server. Checks overlap (a
+ * session-ended hint, the account page after a password change, a manual
+ * refresh), and only the latest may write: one that set out with a cookie
+ * since replaced must not land after it and undo its answer. Nothing renders it.
+ */
+ private readonly statusGeneration = new Map<string, number>();
/** The sentence the page leads with. Never raw exception text. */
error: string | null = null;
/** Diagnostics for the same failure, rendered under `error` rather than in it. */
@@ -364,11 +371,15 @@ export class SwitchServersStore {
}
async refreshStatus(serverId: string): Promise<void> {
+ const generation = (this.statusGeneration.get(serverId) ?? 0) + 1;
+ this.statusGeneration.set(serverId, generation);
+ const latest = () => this.statusGeneration.get(serverId) === generation;
runInAction(() => {
this.refreshing.add(serverId);
});
try {
const status = await rpc.switchServers.getConnectionStatus(serverId);
+ if (!latest()) return;
runInAction(() => {
this.statuses.set(serverId, status);
this.statusUnreachable.delete(serverId);
@@ -380,14 +391,18 @@ export class SwitchServersStore {
// raise the page-level `error` banner: that field is global, so one
// unreachable server would paint an error over every server's view.
console.warn(`[switch-servers] could not read status for ${serverId}`, cause);
+ if (!latest()) return;
runInAction(() => {
this.statuses.set(serverId, { serverId, connected: false, user: null });
this.statusUnreachable.add(serverId);
});
} finally {
- runInAction(() => {
- this.refreshing.delete(serverId);
- });
+ // An overtaken check leaves the flag to the one still running.
+ if (latest()) {
+ runInAction(() => {
+ this.refreshing.delete(serverId);
+ });
+ }
}
}