@@ -1,8 +1,15 @@
11 import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2-import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2+import {
3+ mkdirSync,
4+ mkdtempSync,
5+ rmSync,
6+ symlinkSync,
7+ writeFileSync,
8+} from "node:fs";
39 import { tmpdir } from "node:os";
410 import { join, resolve } from "node:path";
511 import type { GitState } from "./git.ts";
12+import { canonicalizePath } from "./paths.ts";
613 import {
714 buildSystemPrompt,
815 discoverAgentsMd,
@@ -40,9 +47,11 @@ describe("discoverAgentsMd", () => {
4047
4148 // Should be ordered root-to-leaf
4249 expect(files).toHaveLength(2);
43- expect(files[0]!.path).toBe(join(tmp, "AGENTS.md"));
50+ expect(files[0]!.path).toBe(canonicalizePath(join(tmp, "AGENTS.md")));
4451 expect(files[0]!.content).toBe("Root instructions");
45- expect(files[1]!.path).toBe(join(tmp, "src", "AGENTS.md"));
52+ expect(files[1]!.path).toBe(
53+ canonicalizePath(join(tmp, "src", "AGENTS.md")),
54+ );
4655 expect(files[1]!.content).toBe("Src instructions");
4756 });
4857
@@ -95,6 +104,44 @@ describe("discoverAgentsMd", () => {
95104 expect(files[1]!.content).toBe("level-1");
96105 expect(files[2]!.content).toBe("level-2");
97106 });
107+
108+ test("does not walk above the scan root when cwd uses a symlinked path", () => {
109+ const project = join(tmp, "project");
110+ const linkedProject = join(tmp, "linked-project");
111+ const child = join(linkedProject, "src");
112+
113+ mkdirSync(join(project, "src"), { recursive: true });
114+ symlinkSync(project, linkedProject);
115+
116+ writeFileSync(join(tmp, "AGENTS.md"), "Above root");
117+ writeFileSync(join(project, "AGENTS.md"), "At root");
118+
119+ const files = discoverAgentsMd(child, canonicalizePath(project));
120+ expect(files).toHaveLength(1);
121+ expect(files[0]!.content).toBe("At root");
122+ });
123+
124+ test("includes intermediate AGENTS.md files from the canonical parent chain", () => {
125+ const home = join(tmp, "home");
126+ const work = join(home, "work");
127+ const project = join(work, "project");
128+ const linkedProject = join(tmp, "project-link");
129+ const child = join(linkedProject, "src");
130+
131+ mkdirSync(join(project, "src"), { recursive: true });
132+ symlinkSync(project, linkedProject);
133+
134+ writeFileSync(join(home, "AGENTS.md"), "Home instructions");
135+ writeFileSync(join(work, "AGENTS.md"), "Work instructions");
136+ writeFileSync(join(project, "AGENTS.md"), "Project instructions");
137+
138+ const files = discoverAgentsMd(child, home);
139+ expect(files.map((file) => file.content)).toEqual([
140+ "Home instructions",
141+ "Work instructions",
142+ "Project instructions",
143+ ]);
144+ });
98145 });
99146
100147 // ---------------------------------------------------------------------------