Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(agent-runtime): collapse dot and duplicate segments in path-lock keys (#1935)
xpeng5278-web2bee85715a01
diff --git a/packages/agent-runtime/src/path-lock.test.ts b/packages/agent-runtime/src/path-lock.test.tsindex ff64621f0..f554c1f23 100644--- a/packages/agent-runtime/src/path-lock.test.ts+++ b/packages/agent-runtime/src/path-lock.test.ts@@ -18,6 +18,35 @@ describe("normalizeLockPath", () => {expect(normalizeLockPath("src/app.tsx/")).toBe("src/app.tsx");expect(normalizeLockPath(undefined)).toBe("");});++ it("collapses dot, duplicate, and parent segments", () => {+ for (const spelling of [+ "src/a.ts",+ "././src/a.ts",+ "src/./a.ts",+ "src//a.ts",+ "src/foo/../a.ts",+ "src\\.\\a.ts",+ ]) {+ expect(normalizeLockPath(spelling)).toBe("src/a.ts");+ }+ // Drive letters stay, including when `..` would otherwise pop them.+ expect(normalizeLockPath("C:\\Src\\.\\A.ts")).toBe("c:/src/a.ts");+ expect(normalizeLockPath("C:/src/foo/../A.ts")).toBe("c:/src/a.ts");+ expect(normalizeLockPath("C:/../src/a.ts")).toBe("c:/src/a.ts");+ // Delegate lock keys are `${worktreeKey}\0${path}` (see pathLockKey).+ // `..` must not climb out of the path half into the worktree id.+ expect(normalizeLockPath("Wt\0src/foo/../A.ts")).toBe("wt\0src/a.ts");+ expect(normalizeLockPath("wt\0foo/../../a.ts")).toBe("wt\0../a.ts");+ });++ it("keeps a relative parent and an absolute root", () => {+ expect(normalizeLockPath("../x.ts")).toBe("../x.ts");+ expect(normalizeLockPath("../X.ts")).toBe("../x.ts");+ expect(normalizeLockPath("/abs/a.ts")).toBe("/abs/a.ts");+ expect(normalizeLockPath("/Abs/A.ts")).toBe("/abs/a.ts");+ expect(normalizeLockPath("/../src/a.ts")).toBe("/src/a.ts");+ });});describe("PathMutex", () => {@@ -84,6 +113,63 @@ describe("PathMutex", () => {await expect(first).resolves.toBe(1);});+ it("queues a write behind another spelling of the same path", async () => {+ const mutex = new PathMutex();+ const gate = deferred();+ let secondStarted = false;++ const first = mutex.run("src/./a.ts", async () => {+ await gate.promise;+ });+ const second = mutex.run("src/a.ts", async () => {+ secondStarted = true;+ });++ expect(secondStarted).toBe(false);+ gate.resolve();+ await Promise.all([first, second]);+ expect(secondStarted).toBe(true);+ expect(mutex.busy).toBe(false);+ });++ it("does not overlap a read with a write of an equivalent path", async () => {+ const mutex = new PathMutex();+ const gate = deferred();+ let readStarted = false;++ const write = mutex.run("src/foo/../a.ts", async () => {+ await gate.promise;+ });+ const read = mutex.read("src//a.ts", async () => {+ readStarted = true;+ });++ expect(readStarted).toBe(false);+ gate.resolve();+ await Promise.all([write, read]);+ expect(readStarted).toBe(true);+ expect(mutex.busy).toBe(false);+ });++ it("does not create a second lock when an absolute path climbs above root", async () => {+ const mutex = new PathMutex();+ const gate = deferred();+ let secondStarted = false;++ const first = mutex.run("/../src/a.ts", async () => {+ await gate.promise;+ });+ const second = mutex.run("/src/a.ts", async () => {+ secondStarted = true;+ });++ expect(secondStarted).toBe(false);+ gate.resolve();+ await Promise.all([first, second]);+ expect(secondStarted).toBe(true);+ expect(mutex.busy).toBe(false);+ });+it("allows concurrent reads on the same path while serializing reads against writes", async () => {const mutex = new PathMutex();const order: string[] = [];diff --git a/packages/agent-runtime/src/path-lock.ts b/packages/agent-runtime/src/path-lock.tsindex 9a1c9930b..8caea9216 100644--- a/packages/agent-runtime/src/path-lock.ts+++ b/packages/agent-runtime/src/path-lock.ts@@ -15,13 +15,48 @@* parallel except where the order matters.*/-/** Compare-equal spelling of a path: forward slashes, no `./` prefix. */+/**+ * Compare-equal lock key. Slashes are forward. `.` and empty segments drop,+ * and `..` pops the previous segment when there is one to pop — otherwise the+ * `..` stays, so a relative path that climbs past its root (`../x.ts`) is not+ * eaten. A leading `/` and a Windows drive prefix stay. The result is+ * lowercased.+ *+ * Delegate keys are `${worktreeKey}\0${path}` (`pathLockKey`). Only the path+ * half is collapsed, so `..` cannot pop the worktree id out of the key.+ * Lexical only: the lock takes its key synchronously, and the file may not+ * exist yet.+ */export function normalizeLockPath(path: unknown): string {- return String(path ?? "")- .replaceAll("\\", "/")- .replace(/^\.\//, "")- .replace(/\/+$/, "")- .toLowerCase();+ const raw = String(path ?? "").replaceAll("\\", "/");+ const separator = raw.indexOf("\0");+ if (separator >= 0) {+ const namespace = raw.slice(0, separator);+ const filePath = collapseLockPath(raw.slice(separator + 1));+ return `${namespace}\0${filePath}`.toLowerCase();+ }+ return collapseLockPath(raw).toLowerCase();+}++function collapseLockPath(path: string): string {+ const drive = /^[A-Za-z]:/.exec(path)?.[0] ?? "";+ const body = drive ? path.slice(drive.length) : path;+ const absolute = body.startsWith("/");+ const stack: string[] = [];+ for (const segment of body.split("/")) {+ if (segment === "" || segment === ".") continue;+ if (segment === "..") {+ if (stack.length > 0 && stack[stack.length - 1] !== "..") {+ stack.pop();+ } else if (!absolute) {+ stack.push("..");+ }+ continue;+ }+ stack.push(segment);+ }+ const joined = stack.join("/");+ return absolute ? `${drive}/${joined}` : `${drive}${joined}`;}type PathQueueEntry = {