Skip to content

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

Canary takes its data folder from SWITCH_CONSOLE_USER_DATA_DIR (#905)

Louis Amaudruzd5e8c0925cac
diff --git a/console/apps/switch-console-desktop/src/main/app/user-data-dir.test.ts b/console/apps/switch-console-desktop/src/main/app/user-data-dir.test.ts
new file mode 100644
index 000000000..887571215
--- /dev/null
+++ b/console/apps/switch-console-desktop/src/main/app/user-data-dir.test.ts
@@ -0,0 +1,42 @@
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+const identity = vi.hoisted(() => ({ IS_CANARY: false, USER_DATA_DIR_NAME: 'switchdash' }));
+vi.mock('@shared/app-identity', () => identity);
+
+const { checkedFolderName, userDataDirName } = await import('./user-data-dir');
+
+afterEach(() => {
+ vi.unstubAllEnvs();
+ identity.IS_CANARY = false;
+});
+
+describe('userDataDirName', () => {
+ it('lets Canary start in a data folder of its own', () => {
+ vi.stubEnv('DEV', false);
+ identity.IS_CANARY = true;
+ vi.stubEnv('SWITCH_CONSOLE_USER_DATA_DIR', 'switchdash-qa-1');
+ expect(userDataDirName()).toBe('switchdash-qa-1');
+ });
+
+ it('keeps stable on the folder it has always used', () => {
+ vi.stubEnv('DEV', false);
+ vi.stubEnv('SWITCH_CONSOLE_USER_DATA_DIR', 'switchdash-qa-1');
+ expect(userDataDirName()).toBe('switchdash');
+ });
+
+ it('uses the usual folder when nothing names another', () => {
+ identity.IS_CANARY = true;
+ vi.stubEnv('SWITCH_CONSOLE_USER_DATA_DIR', '');
+ expect(userDataDirName()).toBe('switchdash');
+ });
+});
+
+describe('checkedFolderName', () => {
+ it('takes a plain folder name', () => {
+ expect(checkedFolderName('switchdash-qa_1.2')).toBe('switchdash-qa_1.2');
+ });
+
+ it.each(['../elsewhere', '/tmp/x', 'a/b', '..', '.', 'a b'])('refuses %j', (name) => {
+ expect(() => checkedFolderName(name)).toThrow(/SWITCH_CONSOLE_USER_DATA_DIR/);
+ });
+});
diff --git a/console/apps/switch-console-desktop/src/main/app/user-data-dir.ts b/console/apps/switch-console-desktop/src/main/app/user-data-dir.ts
index a23d584f4..24889b5a9 100644
--- a/console/apps/switch-console-desktop/src/main/app/user-data-dir.ts
+++ b/console/apps/switch-console-desktop/src/main/app/user-data-dir.ts
@@ -1,13 +1,35 @@
-import { USER_DATA_DIR_NAME } from '@shared/app-identity';
+import { IS_CANARY, USER_DATA_DIR_NAME } from '@shared/app-identity';
+
+/** The variable that names another data folder, in the builds that read it. */
+export const USER_DATA_DIR_ENV = 'SWITCH_CONSOLE_USER_DATA_DIR';
/**
* The folder under the system's app-data directory this run keeps its data in.
*
- * Several dev builds can run side by side on one machine (one per worktree)
- * when each keeps its own: the server list, sessions, the local database and
- * the managed local server's files live there. Dev-only, so a packaged app
- * never reads it.
+ * Several Consoles can run side by side on one machine when each keeps its
+ * own: the server list, sessions, the local database and the managed local
+ * server's files live there, and the managed server's Docker project is named
+ * after it. Dev builds read SWITCH_CONSOLE_USER_DATA_DIR for one per worktree;
+ * Canary reads it too, so a tester can start as a brand-new install without
+ * touching the data their real one shares with stable. A stable build never
+ * reads it.
+ *
+ * The name is a folder, not a path: anything else is refused at start rather
+ * than resolved somewhere outside the app-data directory.
*/
export function userDataDirName(): string {
- return (import.meta.env.DEV && process.env.SWITCH_CONSOLE_USER_DATA_DIR) || USER_DATA_DIR_NAME;
+ const named = import.meta.env.DEV || IS_CANARY ? process.env[USER_DATA_DIR_ENV] : undefined;
+ if (!named) return USER_DATA_DIR_NAME;
+ return checkedFolderName(named);
+}
+
+/** `name` when it is one plain folder name; throws, naming the variable, otherwise. */
+export function checkedFolderName(name: string): string {
+ if (!/^[A-Za-z0-9._-]+$/.test(name) || name === '.' || name === '..') {
+ throw new Error(
+ `${USER_DATA_DIR_ENV} must be a folder name made of letters, digits, '.', '-' and '_', ` +
+ `not a path: got ${JSON.stringify(name)}.`
+ );
+ }
+ return name;
}
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/constants.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/constants.ts
index 1096390af..5a41f86a0 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/constants.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/constants.ts
@@ -30,8 +30,8 @@ export function stackProjectName(dataDirName: string, kind: 'local' | 'remote'):
* project name share `pgdata` and every one except the one that created it
* fails authentication. They also fight over the containers themselves,
* recreating each other's stack on start with their own ports. That covers
- * dev builds started side by side with SWITCH_CONSOLE_USER_DATA_DIR, not only
- * different builds. Stable keeps the original name, `switchdash-local`, so
+ * dev builds and Canary started side by side with SWITCH_CONSOLE_USER_DATA_DIR,
+ * not only different builds. Stable keeps the original name, `switchdash-local`, so
* existing installs stay attached to the stack they already have.
*/
export const LOCAL_SERVER_PROJECT_NAME = stackProjectName(userDataDirName(), 'local');