diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6e50589df7..8791165a2e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -299,10 +299,7 @@ jobs: run: npm run test:integration --workspace=@getpaseo/server env: *server_test_env - # native-recursive.ts serves darwin and win32, but windows-latest above is the only - # runner that exercises it. macOS — the platform the reported watcher bug was on — - # otherwise has no server-test coverage. Keep this job scoped to regressions - # reported on macOS. + # Keep this job scoped to regressions reported on macOS. server-tests-macos: name: server-tests (macos-latest, file observation) needs: changes @@ -330,6 +327,13 @@ jobs: src/server/workspace-git-service.observation.integration.test.ts \ --bail=1 + - name: Verify path casing on macOS + run: | + npm run test:unit --workspace=@getpaseo/server -- \ + src/server/agent/agent-storage.test.ts \ + src/utils/directory-suggestions.test.ts \ + --bail=1 + - name: Verify plugin compiler children are reaped run: | npm run test:unit --workspace=@getpaseo/server -- \ diff --git a/packages/server/src/server/agent/agent-storage.test.ts b/packages/server/src/server/agent/agent-storage.test.ts index ba8e797d4b..b29f4522b0 100644 --- a/packages/server/src/server/agent/agent-storage.test.ts +++ b/packages/server/src/server/agent/agent-storage.test.ts @@ -156,6 +156,60 @@ describe("AgentStorage", () => { rmSync(tmpDir, { recursive: true, force: true }); }); + test("preserves records after saving and reloading a directory with different capitalization", async () => { + const cwd = path.join(tmpDir, "Work"); + const alternateCwd = path.join(tmpDir, "work"); + await fs.mkdir(cwd); + const needsAlias = !existsSync(alternateCwd); + if (needsAlias) await fs.symlink(cwd, alternateCwd, "junction"); + const timestamp = "2026-01-01T00:00:00.000Z"; + const first = { + id: "first", + provider: "codex", + cwd, + workspaceId: "one", + createdAt: timestamp, + updatedAt: timestamp, + labels: {}, + }; + const second = { ...first, id: "second", workspaceId: "two" }; + await storage.upsert(first); + await storage.upsert(second); + + // An existing record can use one cwd spelling in JSON and another in its path. + const recordPath = findRecordFile(storagePath, first.id); + if (needsAlias) { + // Exercise the same on-disk alias on case-sensitive CI filesystems. + const recordDir = path.dirname(recordPath); + await fs.symlink(recordDir, recordDir.replace(/Work$/, "work"), "junction"); + } + await fs.writeFile(recordPath, JSON.stringify({ ...first, cwd: alternateCwd })); + const loaded = new AgentStorage(storagePath, logger); + const saved = { ...first, cwd: alternateCwd, title: "Updated conversation" }; + await loaded.upsert(saved); + + const restarted = new AgentStorage(storagePath, logger); + await expect(restarted.listByWorkspace("one")).resolves.toMatchObject([saved]); + await expect(restarted.listByWorkspace("two")).resolves.toMatchObject([second]); + expect((await fs.readdir(path.dirname(recordPath))).sort()).toEqual([ + `${first.id}.json`, + `${second.id}.json`, + ]); + }); + + test("removes the previous record file when the cwd moves to a distinct directory", async () => { + const id = "moved-agent"; + await storage.applySnapshot(createManagedAgent({ id, cwd: "/tmp/first-project" })); + const previousPath = findRecordFile(storagePath, id); + await storage.applySnapshot(createManagedAgent({ id, cwd: "/tmp/second-project" })); + + expect(existsSync(previousPath)).toBe(false); + await expect(new AgentStorage(storagePath, logger).get(id)).resolves.toMatchObject({ + id, + cwd: "/tmp/second-project", + }); + }); + test("workspace and active queries follow updates, removal, and reload", async () => { const base = { provider: "codex", diff --git a/packages/server/src/server/agent/agent-storage.ts b/packages/server/src/server/agent/agent-storage.ts index 84767ce2d7..5bcdde5942 100644 --- a/packages/server/src/server/agent/agent-storage.ts +++ b/packages/server/src/server/agent/agent-storage.ts @@ -340,7 +340,15 @@ export class AgentStorage { if (previousPath && previousPath !== nextPath) { try { - await fs.unlink(previousPath); + // Compare the entries after the atomic replacement. On a case-insensitive + // volume both spellings can now address the file we just wrote. + const [previous, next] = await Promise.all([ + fs.lstat(previousPath, { bigint: true }), + fs.lstat(nextPath, { bigint: true }), + ]); + if (previous.dev !== next.dev || previous.ino !== next.ino) { + await fs.unlink(previousPath); + } } catch { // ignore cleanup errors } diff --git a/packages/server/src/utils/directory-suggestions.test.ts b/packages/server/src/utils/directory-suggestions.test.ts index 72c77a4ce2..e60a85a02d 100644 --- a/packages/server/src/utils/directory-suggestions.test.ts +++ b/packages/server/src/utils/directory-suggestions.test.ts @@ -1,6 +1,7 @@ import { createHook } from "node:async_hooks"; import { execFileSync, spawn, type ChildProcess } from "node:child_process"; import { + existsSync, mkdtempSync, mkdirSync, realpathSync, @@ -780,6 +781,50 @@ describe("rooted queries with a typed parent", () => { ).resolves.toEqual([path.join(homeDir, "Developer", "pasta")]); }); + it.for(["~/developer/pas", "~/developer/PASTA"])( + "returns the on-disk spelling exactly once for %s", + async (query, { skip }) => { + if (!existsSync(path.join(homeDir, "developer"))) { + skip("Requires a case-insensitive filesystem"); + } + await expect(searchAbsoluteDirectoryPaths({ homeDir, query })).resolves.toEqual([ + path.join(homeDir, "Developer", "pasta"), + ]); + }, + ); + + it.for(["~/developer/pas", "~/developer/pasta", "~/developer/"])( + "keeps the visible spelling of a symlink for %s", + async (query, { skip }) => { + const link = path.join(homeDir, "developer"); + if (existsSync(link)) skip("Requires a case-sensitive filesystem"); + symlinkSync(path.join(homeDir, "Developer"), link, "junction"); + + const suggestions = await searchAbsoluteDirectoryPaths({ homeDir, query }); + expect(suggestions).toContain(path.join(link, "pasta")); + expect(suggestions).not.toContain(path.join(homeDir, "Developer", "pasta")); + }, + ); + + it("keeps a symlink under a parent whose spelling differs from the target's parent", async ({ + skip, + }) => { + const parent = path.join(homeDir, "developer"); + if (existsSync(parent)) skip("Requires a case-sensitive filesystem"); + const target = path.join(homeDir, "Developer", "pasta"); + const link = path.join(parent, "pasta"); + mkdirSync(parent); + mkdirSync(path.join(target, "children")); + symlinkSync(target, link, "junction"); + + await expect( + searchAbsoluteDirectoryPaths({ homeDir, query: "~/developer/pasta", limit: 1 }), + ).resolves.toEqual([link]); + await expect( + searchAbsoluteDirectoryPaths({ homeDir, query: "~/developer/pasta/ch" }), + ).resolves.toEqual([path.join(link, "children")]); + }); + it("matches the whole tree when the typed parent does not exist", async () => { await expect(searchAbsoluteDirectoryPaths({ homeDir, query: "~/Devel/pas" })).resolves.toEqual([ path.join(homeDir, "Developer", "pasta"), diff --git a/packages/server/src/utils/directory-suggestions.ts b/packages/server/src/utils/directory-suggestions.ts index e731ccef0f..e9ebd84aa3 100644 --- a/packages/server/src/utils/directory-suggestions.ts +++ b/packages/server/src/utils/directory-suggestions.ts @@ -233,7 +233,30 @@ async function resolveTypedParent(input: SearchInput): Promise const resolvedPath = await realpath(visiblePath).catch(() => null); if (!resolvedPath || !isPathInsideRoot(input.root, resolvedPath)) return null; const info = await stat(resolvedPath).catch(() => null); - return info?.isDirectory() ? { visiblePath, resolvedPath } : null; + if (!info?.isDirectory()) return null; + return { + visiblePath: await withResolvedSpelling(visiblePath, resolvedPath), + resolvedPath, + }; +} + +async function withResolvedSpelling(visiblePath: string, resolvedPath: string): Promise { + if (visiblePath.toLowerCase() !== resolvedPath.toLowerCase()) return visiblePath; + + // Check the entire changed suffix. A symlink can keep its basename while redirecting + // into a parent directory that differs only in case. + let visible = visiblePath; + let resolved = resolvedPath; + while (visible !== resolved && visible !== path.parse(visible).root) { + const entryPath = visible; + const entry = await sharedFilesystemCall(`lstat:${entryPath}`, () => fs.lstat(entryPath)).catch( + () => null, + ); + if (!entry || entry.isSymbolicLink()) return visiblePath; + visible = path.dirname(visible); + resolved = path.dirname(resolved); + } + return resolvedPath; } async function resolveExistingPaths(paths: readonly string[]): Promise> { @@ -296,7 +319,8 @@ async function findExactEntry(input: SearchInput): Promise { - const visibleParent = path.resolve(input.root, input.plan.parentPart || "."); - const parent = await realpath(visibleParent).catch(() => null); + const typedParent = path.resolve(input.root, input.plan.parentPart || "."); + const parent = await realpath(typedParent).catch(() => null); if (!parent || !isPathInsideRoot(input.root, parent)) return []; if (isGitIgnoredPath(parent, input)) return []; + const visibleParent = await withResolvedSpelling(typedParent, parent); // Browsing inside an excluded path is explicit: the caller typed it, so list what is there. const discoveryInput = isInsideExcludedPath(parent, input) ? { ...input, excludedDiscoveryPaths: new Set() }