diff --git a/src/__tests__/scrub.test.ts b/src/__tests__/scrub.test.ts index fa5d6f8..06ba686 100644 --- a/src/__tests__/scrub.test.ts +++ b/src/__tests__/scrub.test.ts @@ -142,3 +142,25 @@ describe("scrubOpencodeFingerprints — pass-through", () => { expect(scrubOpencodeFingerprints("")).toBe("") }) }) + + +describe("keepClientCwd env forms (issue #21)", () => { + const ENV_BLOCK = (line: string) => + "Here is some useful information about the environment you are running in:\n\n" + line + "\nIs directory a git repo: Yes\n\n" + + test("preserves Primary working directory (Claude Code form)", () => { + const out = scrubOpencodeFingerprints("You are OpenCode, the best coding agent on the planet.\n" + ENV_BLOCK("Primary working directory: C:\\proj\\alpha")) + expect(out).toContain("C:\\proj\\alpha") + expect(out).not.toContain("best coding agent") + }) + + test("preserves legacy Working directory form", () => { + const out = scrubOpencodeFingerprints(ENV_BLOCK("Working directory: /home/user/legacy")) + expect(out).toContain("/home/user/legacy") + }) + + test("preserves uppercase/mixed-case label", () => { + const out = scrubOpencodeFingerprints(ENV_BLOCK("Primary Working Directory: D:\\Mixed")) + expect(out).toContain("D:\\Mixed") + }) +}) diff --git a/src/scrub.ts b/src/scrub.ts index 14942d3..2d4be48 100644 --- a/src/scrub.ts +++ b/src/scrub.ts @@ -96,7 +96,9 @@ const OPENCODE_ENV_BLOCK = /\n?Here is some useful information about the environment you are running in:\n[\s\S]*?<\/env>\n?/ function keepClientCwd(block: string): string { - const cwd = block.match(/(?:^|\n)[ \t]*Working directory:[ \t]*([^\n<]+)/i)?.[1]?.trim() + // Also match Claude Code's "Primary working directory:" form so the kept + // block never loses the client cwd (Meridian and the model both read it). + const cwd = block.match(/(?:^|\n)[ \t]*(?:Primary[ \t]+)?Working[ \t]+directory:[ \t]*([^\n<]+)/i)?.[1]?.trim() return cwd ? `\n\n Working directory: ${cwd}\n\n` : "\n" }