diff --git a/src/daemon/router.ts b/src/daemon/router.ts index ddbb158..d0ca2f8 100644 --- a/src/daemon/router.ts +++ b/src/daemon/router.ts @@ -261,11 +261,15 @@ export class CommandRouter { const input: { cwd: string; alias?: string; trust?: boolean } = { cwd: requireString(params, "cwd"), }; - const alias = optionalString(params, "alias"); + // `name` is the web dialog's field; it is an alias when `alias` is absent. + // A whitespace-only value is dropped, exactly like an empty `alias`. + const alias = optionalString(params, "alias") ?? optionalString(params, "name"); if (alias !== undefined) input.alias = alias; if (typeof params.trust === "boolean") input.trust = params.trust; const workspace = await this.workspaces.add(input); - return { accepted: true, workspace }; + // `workspace.workspaceId` is the documented location; the top-level + // copy saves the web client a round trip on its fallback path. + return { accepted: true, workspaceId: workspace.workspaceId, workspace }; } case "workspace.update": { const workspaceId = requireString(params, "workspaceId"); diff --git a/src/workspaces/index.ts b/src/workspaces/index.ts index a70e3f2..ccd6f2f 100644 --- a/src/workspaces/index.ts +++ b/src/workspaces/index.ts @@ -14,7 +14,7 @@ import { accessSync, constants, realpathSync, statSync } from "node:fs"; import { homedir } from "node:os"; import { basename, isAbsolute, join, relative, resolve, sep } from "node:path"; import type { InstanceManager, WorkspaceStore as WorkspaceStoreContract } from "../core/interfaces.js"; -import { dataDir, realpathSafe, workspaceIdForCwd } from "../core/paths.js"; +import { dataDir, expandTilde, realpathSafe, workspaceIdForCwd } from "../core/paths.js"; import { ERROR_CODES } from "../protocol/errors.js"; import type { InstanceState, @@ -62,6 +62,11 @@ export interface WorkspaceStoreOptions { env?: WorkspaceEnv; /** Clock for the `PI2_WORKSPACE_STALE_DAYS` comparison; defaults to `Date.now`. */ now?: () => number; + /** + * Home for `~`/`~/…` expansion in `add()` and the default allowlist root; + * injectable so tests never touch the real home directory. + */ + home?: string; } export interface WorkspaceStoreHandle extends WorkspaceStoreContract { @@ -83,11 +88,13 @@ class WorkspaceStore implements WorkspaceStoreHandle { private readonly roots: readonly string[] | undefined; private readonly env: WorkspaceEnv; private readonly now: () => number; + private readonly home: string; constructor(options: WorkspaceStoreOptions = {}) { this.logger = options.logger; this.env = options.env ?? process.env; this.now = options.now ?? Date.now; + this.home = options.home ?? homedir(); this.discovery = options.discovery ?? createDiscovery({ ...(options.logger ? { logger: options.logger } : {}) }); this.registry = options.registry ?? @@ -141,8 +148,18 @@ class WorkspaceStore implements WorkspaceStoreHandle { if (raw.includes("\0")) { throw new WorkspaceError(ERROR_CODES.BAD_REQUEST, "cwd contains a NUL byte"); } - if (!isAbsolute(raw)) { - throw new WorkspaceError(ERROR_CODES.BAD_REQUEST, `cwd must be absolute: ${raw}`); + // `~` and `~/…` are shell shorthand the web dialog advertises; expand before + // the absolute-path check. `~user/…` is deliberately left alone: resolving + // another user's home is not something the daemon should guess at. + const expanded = expandTilde(raw, this.home); + if (expanded === undefined) { + throw new WorkspaceError( + ERROR_CODES.BAD_REQUEST, + `cwd must be an absolute path (or start with ~/): ~user is not expanded: ${raw}`, + ); + } + if (!isAbsolute(expanded)) { + throw new WorkspaceError(ERROR_CODES.BAD_REQUEST, `cwd must be an absolute path (or start with ~/): ${raw}`); } if (input.alias !== undefined && typeof input.alias !== "string") { throw new WorkspaceError(ERROR_CODES.BAD_REQUEST, "alias must be a string"); @@ -154,9 +171,9 @@ class WorkspaceStore implements WorkspaceStoreHandle { const roots = this.allowlist(); let real: string; try { - real = realpathSync(raw); + real = realpathSync(expanded); } catch (error) { - throw mapFsError(error, raw); + throw mapFsError(error, expanded); } // Check the canonical path: symlinks inside the allowlist must not escape it. this.assertAllowed(real, roots); @@ -408,7 +425,7 @@ class WorkspaceStore implements WorkspaceStoreHandle { if (configured !== undefined) { return configured.map((root) => canonicalRoot(root)).filter((root): root is string => root.length > 0); } - return rootsFromEnv(this.env.PI2_WORKSPACE_ROOTS); + return rootsFromEnv(this.env.PI2_WORKSPACE_ROOTS, this.home); } private assertAllowed(candidate: string, roots: readonly string[]): void { diff --git a/test/daemon/router.test.ts b/test/daemon/router.test.ts index 5e1acc7..1e8eb31 100644 --- a/test/daemon/router.test.ts +++ b/test/daemon/router.test.ts @@ -102,6 +102,26 @@ describe("CommandRouter", () => { expect(workspaces.refreshCalls).toEqual(["ws-1"]); }); + it("accepts name as the alias when alias is absent", async () => { + const added = await router.dispatch({ + method: "workspace.add", + params: { cwd: "/repo/named", name: " Named " }, + }); + + expect(added.status).toBe(202); + expect(workspaces.addCalls).toEqual([{ cwd: "/repo/named", alias: " Named " }]); + // The response carries the id both where the protocol documents it and at + // the top level (the web client's shortcut). + expect(added.body).toMatchObject({ accepted: true, workspaceId: "ws-2", workspace: { workspaceId: "ws-2" } }); + }); + + it("prefers alias over name and drops a whitespace-only name", async () => { + await router.dispatch({ method: "workspace.add", params: { cwd: "/repo/both", alias: "A", name: "N" } }); + await router.dispatch({ method: "workspace.add", params: { cwd: "/repo/blank", name: " " } }); + + expect(workspaces.addCalls).toEqual([{ cwd: "/repo/both", alias: "A" }, { cwd: "/repo/blank" }]); + }); + it("routes instance and system methods to the instance manager", async () => { const ensured = await router.dispatch({ method: "instance.ensure", params: { workspaceId: "ws-1" } }); expect(ensured.status).toBe(202); diff --git a/test/workspaces/store.test.ts b/test/workspaces/store.test.ts index 8e688c3..3020ffb 100644 --- a/test/workspaces/store.test.ts +++ b/test/workspaces/store.test.ts @@ -281,6 +281,37 @@ describe("workspace store", () => { } }); + it("expands ~ and ~/… against the store's home before the allowlist check", async () => { + const home = join(allowRoot, "home"); + const project = join(home, "project"); + mkdirSync(project, { recursive: true }); + const { store, registry } = setup({ env: { PI2_WORKSPACE_HIDE_TEMP: "0" }, home }); + + const root = await store.add({ cwd: "~" }); + expect(root.workspaceId).toBe(workspaceIdForCwd(home)); + expect(root.cwd).toBe(home); + expect(root.alias).toBeNull(); + + const child = await store.add({ cwd: "~/project", alias: "Project" }); + expect(child.workspaceId).toBe(workspaceIdForCwd(project)); + expect(child.cwd).toBe(project); + expect(child.alias).toBe("Project"); + expect(registry.list()).toHaveLength(2); + }); + + it("rejects ~user/… and names the accepted forms in the message", async () => { + const { store } = setup({ home: allowRoot }); + + await expect(store.add({ cwd: "~alice/project" })).rejects.toMatchObject({ + code: "E_BAD_REQUEST", + message: expect.stringContaining("cwd must be an absolute path (or start with ~/)"), + }); + await expect(store.add({ cwd: "relative/project" })).rejects.toMatchObject({ + code: "E_BAD_REQUEST", + message: expect.stringContaining("cwd must be an absolute path (or start with ~/)"), + }); + }); + it("validates relative, missing and non-directory paths", async () => { const file = join(allowRoot, "file.txt"); writeFileSync(file, "x");