A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
fix(coding-agent): canonicalize ACP cwd comparison (#628)
Seth Karten2afb88553119
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.mdindex cfc934e786..1c446a98ac 100644--- a/packages/coding-agent/CHANGELOG.md+++ b/packages/coding-agent/CHANGELOG.md@@ -9,6 +9,7 @@- Fixed `stop` and `rename` becoming prompts when `--daemon-socket` precedes the command ([#622](https://github.com/PrimeIntellect-ai/prime-agent/issues/622)).- Fixed subagent terminal notices arriving as anonymous follow-up prompts instead of attributed agent messages, so a parent can now tell which child reported completion, failure, or cancellation, and a busy parent is steered at the next turn boundary rather than waiting to go idle ([#617](https://github.com/PrimeIntellect-ai/prime-agent/issues/617)).- Fixed ACP mode reporting a failed turn as a clean `end_turn`. A provider error, expired auth, or unusable model left `session/prompt` resolving with no updates at all, which reads to a client as a successful but empty turn; the turn now fails with the underlying error instead.+- Fixed ACP cwd mismatch metadata treating symlink aliases such as macOS `/var` and `/private/var` as different directories ([#623](https://github.com/PrimeIntellect-ai/prime-agent/issues/623)).## [0.6.0] - 2026-08-04diff --git a/packages/coding-agent/src/modes/acp/acp-mode.ts b/packages/coding-agent/src/modes/acp/acp-mode.tsindex fc7423da43..eed228ae08 100644--- a/packages/coding-agent/src/modes/acp/acp-mode.ts+++ b/packages/coding-agent/src/modes/acp/acp-mode.ts@@ -1,4 +1,5 @@import { randomUUID } from "node:crypto";+import { realpathSync, statSync } from "node:fs";import { resolve } from "node:path";import { Readable } from "node:stream";import * as acp from "@agentclientprotocol/sdk";@@ -46,6 +47,43 @@ function rawStdoutSink(): WritableStream<Uint8Array> {});}+function normalizeWindowsDriveLetter(path: string): string {+ if (process.platform !== "win32" || !/^[A-Z]:/i.test(path)) return path;+ return path.slice(0, 1).toLowerCase() + path.slice(1);+}++function canonicalCwd(path: string): string {+ const resolved = resolve(path);+ let canonical: string;+ try {+ canonical = realpathSync(resolved);+ } catch {+ // Preserve the previous lexical comparison when a path is missing or inaccessible.+ canonical = resolved;+ }+ return normalizeWindowsDriveLetter(canonical);+}++function sameCwd(left: string, right: string): boolean {+ const canonicalLeft = canonicalCwd(left);+ const canonicalRight = canonicalCwd(right);+ if (canonicalLeft === canonicalRight) return true;++ try {+ const leftStat = statSync(canonicalLeft, { bigint: true });+ const rightStat = statSync(canonicalRight, { bigint: true });+ // Either half being zero makes the pair untrustworthy: Windows path-based+ // stat can report dev 0 with a real ino, and comparing ino alone would match+ // distinct directories on different volumes, since file IDs are volume-local.+ const leftIdentityMissing = leftStat.dev === 0n || leftStat.ino === 0n;+ const rightIdentityMissing = rightStat.dev === 0n || rightStat.ino === 0n;+ if (leftIdentityMissing || rightIdentityMissing) return false;+ return leftStat.dev === rightStat.dev && leftStat.ino === rightStat.ino;+ } catch {+ return false;+ }+}+export interface AcpModeOptions {/** Bind headless extensions once the connection is live (in-process mode). */bindHeadlessExtensions?: () => Promise<void>;@@ -255,7 +293,7 @@ export async function runAcpModeWithConnection(.getState().then((state) => state.cwd).catch(() => undefined);- if (actual && resolve(requestedCwd) !== resolve(actual)) {+ if (actual && !sameCwd(requestedCwd, actual)) {cwdMismatch = { requested: requestedCwd, actual };}}diff --git a/packages/coding-agent/test/suite/regressions/623-acp-canonical-cwd.test.ts b/packages/coding-agent/test/suite/regressions/623-acp-canonical-cwd.test.tsnew file mode 100644index 0000000000..c975a5a116--- /dev/null+++ b/packages/coding-agent/test/suite/regressions/623-acp-canonical-cwd.test.ts@@ -0,0 +1,121 @@+import { existsSync, mkdtempSync, realpathSync, rmSync, statSync, symlinkSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import * as acp from "@agentclientprotocol/sdk";+import { afterEach, describe, expect, it } from "vitest";+import type { AgentSessionRuntime } from "../../../src/core/agent-session-runtime.js";+import { PRIME_AGENT_META_NAMESPACE } from "../../../src/modes/acp/acp-meta.js";+import { runAcpModeWithConnection } from "../../../src/modes/acp/index.js";+import { InProcessAgentConnection } from "../../../src/modes/agent-connection/in-process-agent-connection.js";+import { createHarness, type Harness } from "../harness.js";++function runtimeHostFor(session: unknown): AgentSessionRuntime {+ return {+ session,+ setRebindSession() {},+ setBeforeSessionInvalidate() {},+ async dispose() {},+ } as unknown as AgentSessionRuntime;+}++async function newAcpSession(harness: Harness, cwd: string) {+ const connection = new InProcessAgentConnection(runtimeHostFor(harness.session));+ const toAgent = new TransformStream<Uint8Array, Uint8Array>();+ const toClient = new TransformStream<Uint8Array, Uint8Array>();+ void runAcpModeWithConnection(connection, {+ stream: acp.ndJsonStream(toClient.writable, toAgent.readable),+ });+ const handle = acp.client({ name: "cwd-regression" }).connect(acp.ndJsonStream(toAgent.writable, toClient.readable));+ await handle.agent.request("initialize", { protocolVersion: acp.PROTOCOL_VERSION, clientCapabilities: {} });+ return handle.agent.request("session/new", { cwd, mcpServers: [] });+}++function cwdMeta(created: { _meta?: Record<string, unknown> | null }): unknown {+ return (created._meta?.[PRIME_AGENT_META_NAMESPACE] as { cwd?: unknown } | undefined)?.cwd;+}++function findExistingCaseVariant(path: string): string | undefined {+ const originalStat = statSync(path, { bigint: true });+ for (let index = 0; index < path.length; index++) {+ const character = path[index];+ if (!character || !/[A-Z]/i.test(character)) continue;+ const toggled = character === character.toLowerCase() ? character.toUpperCase() : character.toLowerCase();+ const candidate = path.slice(0, index) + toggled + path.slice(index + 1);+ if (!existsSync(candidate)) continue;+ try {+ const candidateStat = statSync(candidate, { bigint: true });+ if (+ realpathSync(candidate) !== realpathSync(path) &&+ originalStat.dev === candidateStat.dev &&+ originalStat.ino === candidateStat.ino+ ) {+ return candidate;+ }+ } catch {+ // Try the next component when this spelling cannot be inspected.+ }+ }+ return undefined;+}++const caseVariantCwd = findExistingCaseVariant(process.cwd());+const harnesses: Harness[] = [];+const directories: string[] = [];++afterEach(() => {+ for (const directory of directories.splice(0)) {+ rmSync(directory, { recursive: true, force: true });+ }+ for (const harness of harnesses.splice(0)) {+ harness.cleanup();+ }+});++describe("#623 ACP canonical cwd comparison", () => {+ it("does not report a mismatch for paths resolving to the same directory", async () => {+ const harness = await createHarness();+ harnesses.push(harness);+ const aliasRoot = mkdtempSync(join(tmpdir(), "prime-agent-acp-cwd-"));+ directories.push(aliasRoot);+ const alias = join(aliasRoot, "alias");+ symlinkSync(process.cwd(), alias, process.platform === "win32" ? "junction" : "dir");++ // macOS exposes /var as /private/var; a portable symlink exercises the+ // same distinction between a displayed path and its canonical path.+ const created = await newAcpSession(harness, alias);++ expect(created.sessionId).toBeTruthy();+ expect(cwdMeta(created)).toBeUndefined();+ }, 30_000);++ it.runIf(caseVariantCwd !== undefined)(+ "uses filesystem identity when realpath preserves different component casing",+ async () => {+ if (!caseVariantCwd) throw new Error("Expected a case-only cwd variant");+ const originalStat = statSync(process.cwd(), { bigint: true });+ const variantStat = statSync(caseVariantCwd, { bigint: true });+ expect(realpathSync(caseVariantCwd)).not.toBe(realpathSync(process.cwd()));+ expect([variantStat.dev, variantStat.ino]).toEqual([originalStat.dev, originalStat.ino]);++ const harness = await createHarness();+ harnesses.push(harness);++ const created = await newAcpSession(harness, caseVariantCwd);++ expect(created.sessionId).toBeTruthy();+ expect(cwdMeta(created)).toBeUndefined();+ },+ 30_000,+ );++ it("preserves mismatch metadata when canonicalization falls back", async () => {+ const harness = await createHarness();+ harnesses.push(harness);+ const requested = join(harness.tempDir, "missing");++ const created = await newAcpSession(harness, requested);++ expect(created.sessionId).toBeTruthy();+ expect(cwdMeta(created)).toEqual({ requested, actual: process.cwd() });+ }, 30_000);+});