Skip to content

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

fix(console): reach the workspace reconcile from the paths that skipped it

Petr Bauchdf05fbffc423
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline-matrix-migration.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline-matrix-migration.test.ts
index c6ee45465..d566da845 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline-matrix-migration.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline-matrix-migration.test.ts
@@ -60,6 +60,10 @@ vi.mock('@main/core/switch-servers/servers-store', () => ({
ensureManagedServer: () => Promise.resolve({ id: 'srv-1' }),
setActiveServerId: vi.fn(),
}));
+// Reads this install's own workspace rows, and through them the database client.
+vi.mock('@main/core/workspaces/reconcile-workspaces', () => ({
+ reconcileServerWorkspaces: () => Promise.resolve(),
+}));
vi.mock('@main/core/switch-servers/auth', () => ({
passwordLogin: () => Promise.resolve({ success: true }),
}));
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.test.ts
index 135275606..c791a56f1 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.test.ts
@@ -66,6 +66,10 @@ vi.mock('@main/core/switch-servers/servers-store', () => ({
ensureManagedServer: () => Promise.resolve({ id: 'srv-1' }),
setActiveServerId: setActiveServerIdMock,
}));
+// Reads this install's own workspace rows, and through them the database client.
+vi.mock('@main/core/workspaces/reconcile-workspaces', () => ({
+ reconcileServerWorkspaces: () => Promise.resolve(),
+}));
vi.mock('@main/core/switch-servers/auth', () => ({
passwordLogin: () => Promise.resolve({ success: true }),
}));
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.ts
index 51ffc8064..dc0753f6b 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/pipeline.ts
@@ -1,5 +1,6 @@
import { passwordLogin } from '@main/core/switch-servers/auth';
import { ensureManagedServer, setActiveServerId } from '@main/core/switch-servers/servers-store';
+import { reconcileServerWorkspaces } from '@main/core/workspaces/reconcile-workspaces';
import { log } from '@main/lib/logger';
import { COMPATIBLE_SWITCH_VERSION, RELEASE_REPO_OWNER } from '@shared/app-identity';
import {
@@ -269,6 +270,16 @@ export async function startStack(opts: StartStackOptions): Promise<StartLocalSer
log.warn('managed-switch-server: auto sign-in failed; server will show a sign-in prompt', {
error: login.error,
});
+ } else {
+ // The user never sees a login form for a managed stack, so this is the only
+ // sign-in it will ever have — without matching the workspaces here, its
+ // placeholder one would stay unmatched until some later launch happened to.
+ await reconcileServerWorkspaces(server.id).catch((error: unknown) => {
+ log.warn('managed-switch-server: signed in, but could not read the account’s workspaces', {
+ server: server.id,
+ error: String(error),
+ });
+ });
}
if (upgrade) await finishUpgrade(host);
diff --git a/console/apps/switch-console-desktop/src/main/core/switch-servers/servers-store.db.test.ts b/console/apps/switch-console-desktop/src/main/core/switch-servers/servers-store.db.test.ts
index 2474f01b4..86e9935b9 100644
--- a/console/apps/switch-console-desktop/src/main/core/switch-servers/servers-store.db.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/switch-servers/servers-store.db.test.ts
@@ -37,8 +37,14 @@ vi.mock('@main/core/telemetry/telemetry-service', () => ({
}));
// Imported after the mocks so the module binds to the mocked db + secrets store.
-const { addServer, ensureManagedServer, removeServer, renameServer } =
- await import('./servers-store');
+const {
+ addServer,
+ ensureManagedServer,
+ getActiveServerId,
+ removeServer,
+ renameServer,
+ setActiveServerId,
+} = await import('./servers-store');
describe('servers-store: rename & delete', () => {
let fixture: Awaited<ReturnType<typeof openFixture>>;
@@ -279,3 +285,67 @@ describe('servers-store: rename & delete', () => {
});
});
});
+
+describe('selecting a server', () => {
+ let fixture: Awaited<ReturnType<typeof openFixture>>;
+
+ beforeEach(async () => {
+ fixture = await openFixture('empty');
+ mocks.db = fixture.db;
+ fixture.sqlite.pragma('foreign_keys = OFF');
+ await fixture.db.insert(switchServers).values({
+ id: 'srv-1',
+ name: 'Local dev',
+ gatewayUrl: 'https://srv-1.example.com',
+ apiUrl: 'https://api-srv-1.example.com',
+ });
+ });
+
+ afterEach(() => {
+ fixture.close();
+ mocks.db = undefined;
+ });
+
+ async function seedWorkspace(id: string, tenantId: string | null): Promise<void> {
+ await fixture.db.insert(workspaces).values({ id, serverId: 'srv-1', name: id, tenantId });
+ }
+
+ it('selects the one workspace a server has', async () => {
+ await seedWorkspace('ws-1', 't-1');
+
+ await setActiveServerId('srv-1');
+
+ expect(await getActiveServerId()).toBe('srv-1');
+ });
+
+ /**
+ * Refusing would fail the managed stack start this runs inside, taking a
+ * healthy server down over a question about which of its workspaces to show.
+ */
+ it('picks one rather than refusing when the account has several there', async () => {
+ await seedWorkspace('ws-1', 't-1');
+ await seedWorkspace('ws-2', 't-2');
+
+ await setActiveServerId('srv-1');
+
+ expect(await getActiveServerId()).toBe('srv-1');
+ });
+
+ // Re-selecting the server the user is already in must not move them to
+ // another of its workspaces; a managed stack start does exactly that.
+ it('leaves the selection alone when it is already on that server', async () => {
+ await seedWorkspace('ws-1', 't-1');
+ await seedWorkspace('ws-2', 't-2');
+ await setActiveServerId('srv-1');
+ await fixture.db.update(kv).set({ value: 'ws-2' }).where(eq(kv.key, 'activeWorkspaceId'));
+
+ await setActiveServerId('srv-1');
+
+ const [pointer] = await fixture.db.select().from(kv).where(eq(kv.key, 'activeWorkspaceId'));
+ expect(pointer!.value).toBe('ws-2');
+ });
+
+ it('refuses a server with no workspace at all', async () => {
+ await expect(setActiveServerId('srv-1')).rejects.toThrow('no workspace');
+ });
+});
diff --git a/console/apps/switch-console-desktop/src/main/core/switch-servers/servers-store.ts b/console/apps/switch-console-desktop/src/main/core/switch-servers/servers-store.ts
index e94b945c5..004fc1738 100644
--- a/console/apps/switch-console-desktop/src/main/core/switch-servers/servers-store.ts
+++ b/console/apps/switch-console-desktop/src/main/core/switch-servers/servers-store.ts
@@ -9,8 +9,8 @@ import {
ensureServerWorkspace,
getActiveWorkspaceId,
insertServerWorkspace,
+ listWorkspacesForServer,
renameServerWorkspaces,
- requireSoleWorkspaceForServer,
serverIdForWorkspace,
setActiveWorkspaceId,
} from '@main/core/workspaces/workspaces-store';
@@ -285,11 +285,24 @@ export async function getActiveServerId(): Promise<string | null> {
return serverIdForWorkspace(activeWorkspaceId);
}
-/** Select a server by selecting its workspace. */
+/**
+ * Select a server by selecting one of its workspaces.
+ *
+ * Picks the first when the account turns out to belong to several on that
+ * server, rather than refusing the way the paths that attach an agent do. The
+ * risks are not the same: showing the wrong workspace is visible and one click
+ * from being corrected, while attaching an agent to it is neither. Refusing
+ * here would instead fail the managed stack start this runs inside, taking a
+ * healthy server down over a question about which of its workspaces to show.
+ */
export async function setActiveServerId(id: string): Promise<void> {
const server = await getServer(id);
if (!server) throw new Error(`No Switch server with id ${id}`);
- const workspace = await requireSoleWorkspaceForServer(id);
+ const found = await listWorkspacesForServer(id);
+ const workspace = found[0];
+ if (!workspace) throw new Error(`Switch server ${id} has no workspace`);
+ const active = await getActiveWorkspaceId();
+ if (found.some((candidate) => candidate.id === active)) return;
await setActiveWorkspaceId(workspace.id);
}
diff --git a/console/apps/switch-console-desktop/src/main/index.ts b/console/apps/switch-console-desktop/src/main/index.ts
index d5b8fb0a6..3e697c980 100644
--- a/console/apps/switch-console-desktop/src/main/index.ts
+++ b/console/apps/switch-console-desktop/src/main/index.ts
@@ -159,20 +159,25 @@ void app.whenReady().then(async () => {
void reconcileResourceSampler();
- // A server registered before its account's memberships were known carries one
- // tenant-less workspace; this is what gives it its tenant, and what notices a
- // membership added or withdrawn since the last launch. Unawaited: it talks to
- // every signed-in server, and the window must not wait on any of them.
- void reconcileAllWorkspaces().catch((error: unknown) => {
- log.error('Workspace reconcile could not run; every server was left as it was:', error);
- });
-
// Reflect a managed local Switch stack that survived the last quit, so the UI
// shows it running without the user restarting it.
- void localServerService.initialize();
+ const localServerReady = localServerService.initialize();
// Re-establish desktop-side forwards for remote-managed stacks that survived
// the last quit, restoring their reachability from this machine.
- void remoteServerService.initialize();
+ const remoteServerReady = remoteServerService.initialize();
+
+ // A server registered before its account's memberships were known carries one
+ // tenant-less workspace; this is what gives it its tenant, and what notices a
+ // membership added or withdrawn since the last launch. It runs after the two
+ // services above because they are what make a stack that survived the last
+ // quit report itself as running — before them every managed server still
+ // looks stopped and would be passed over. Unawaited: it talks to every
+ // signed-in server, and the window must not wait on any of them.
+ void Promise.allSettled([localServerReady, remoteServerReady])
+ .then(reconcileAllWorkspaces)
+ .catch((error: unknown) => {
+ log.error('Workspace reconcile could not run; every server was left as it was:', error);
+ });
const dependenciesReady = localDependencyManager.probeAll().catch((e: unknown) => {
log.error('Failed to probe dependencies:', e);
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 25c96500a..943b65a27 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
@@ -463,7 +463,9 @@ export class SwitchServersStore {
});
return false;
}
- await this.refreshStatus(serverId);
+ // Signing in is when the server first says which workspaces the account
+ // belongs to, so the list this app holds is stale the moment it returns.
+ await Promise.all([this.refreshStatus(serverId), workspacesStore.refresh()]);
return true;
}
@@ -480,7 +482,7 @@ export class SwitchServersStore {
}
return false;
}
- await this.refreshStatus(serverId);
+ await Promise.all([this.refreshStatus(serverId), workspacesStore.refresh()]);
return true;
}