fix(server): skip home directory in workspace prefix matching (#563)

Skip the home directory in workspace prefix matching so child paths under the home workspace get their own workspace records.

Closes #562
This commit is contained in:
hissin
2026-04-27 20:28:46 +08:00
committed by GitHub
parent ee5a23d962
commit 34626e3b8f
2 changed files with 25 additions and 1 deletions
+2
View File
@@ -6257,8 +6257,10 @@ export class Session {
return exact.workspaceId;
}
const userHome = homedir();
let bestMatch: PersistedWorkspaceRecord | null = null;
for (const workspace of workspaces) {
if (workspace.cwd === userHome) continue;
const prefix = workspace.cwd.endsWith(sep) ? workspace.cwd : `${workspace.cwd}${sep}`;
if (!normalizedCwd.startsWith(prefix)) {
continue;
@@ -1,6 +1,6 @@
import { execSync } from "node:child_process";
import { mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { homedir, tmpdir } from "node:os";
import path from "node:path";
import { expect, test, vi } from "vitest";
import { Session } from "./session.js";
@@ -51,6 +51,10 @@ interface SessionTestAccess {
getAgentPayloadById(agentId: string): Promise<unknown>;
buildProjectPlacementForCwd(cwd: string): Promise<unknown>;
buildProjectPlacement(cwd: string): Promise<unknown>;
resolveRegisteredWorkspaceIdForCwd(
cwd: string,
workspaces: ReturnType<typeof createPersistedWorkspaceRecord>[],
): string;
buildWorkspaceDescriptorMap(...args: unknown[]): Promise<Map<string, unknown>>;
describeWorkspaceRecord(...args: unknown[]): Promise<unknown>;
describeWorkspaceRecordWithGitData(...args: unknown[]): Promise<unknown>;
@@ -3747,3 +3751,21 @@ test("subscribed fetch_workspaces includes git enrichment in the initial snapsho
expect(workspaceUpdates).toEqual([]);
expect(session.describeWorkspaceRecordWithGitData).toHaveBeenCalledWith(gitWorkspace, gitProject);
});
test("resolveRegisteredWorkspaceIdForCwd does not match home directory as a prefix", () => {
const session = createSessionForWorkspaceTests();
const home = homedir();
const childCwd = path.join(home, "projects/new-app");
const homeWorkspace = createPersistedWorkspaceRecord({
workspaceId: home,
projectId: "proj-home",
cwd: home,
kind: "directory",
displayName: "home",
createdAt: "2026-03-01T12:00:00.000Z",
updatedAt: "2026-03-01T12:00:00.000Z",
});
expect(session.resolveRegisteredWorkspaceIdForCwd(childCwd, [homeWorkspace])).toBe(childCwd);
expect(session.resolveRegisteredWorkspaceIdForCwd(home, [homeWorkspace])).toBe(home);
});