Skip to content

Commit 35a36e5

Browse files
fix: prevent duplicate AGENTS.md injection when reading instruction files (anomalyco#11581)
Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
1 parent 605a445 commit 35a36e5

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

packages/opencode/src/session/instruction.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ export namespace InstructionPrompt {
7575
for (const file of FILES) {
7676
const matches = await Filesystem.findUp(file, Instance.directory, Instance.worktree)
7777
if (matches.length > 0) {
78-
matches.forEach((p) => paths.add(path.resolve(p)))
78+
matches.forEach((p) => {
79+
paths.add(path.resolve(p))
80+
})
7981
break
8082
}
8183
}
@@ -103,7 +105,9 @@ export namespace InstructionPrompt {
103105
}),
104106
).catch(() => [])
105107
: await resolveRelative(instruction)
106-
matches.forEach((p) => paths.add(path.resolve(p)))
108+
matches.forEach((p) => {
109+
paths.add(path.resolve(p))
110+
})
107111
}
108112
}
109113

@@ -168,12 +172,14 @@ export namespace InstructionPrompt {
168172
const already = loaded(messages)
169173
const results: { filepath: string; content: string }[] = []
170174

171-
let current = path.dirname(path.resolve(filepath))
175+
const target = path.resolve(filepath)
176+
let current = path.dirname(target)
172177
const root = path.resolve(Instance.directory)
173178

174-
while (current.startsWith(root)) {
179+
while (current.startsWith(root) && current !== root) {
175180
const found = await find(current)
176-
if (found && !system.has(found) && !already.has(found) && !isClaimed(messageID, found)) {
181+
182+
if (found && found !== target && !system.has(found) && !already.has(found) && !isClaimed(messageID, found)) {
177183
claim(messageID, found)
178184
const content = await Bun.file(found)
179185
.text()
@@ -182,7 +188,6 @@ export namespace InstructionPrompt {
182188
results.push({ filepath: found, content: "Instructions from: " + found + "\n" + content })
183189
}
184190
}
185-
if (current === root) break
186191
current = path.dirname(current)
187192
}
188193

packages/opencode/test/session/instruction.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,24 @@ describe("InstructionPrompt.resolve", () => {
4747
},
4848
})
4949
})
50+
51+
test("doesn't reload AGENTS.md when reading it directly", async () => {
52+
await using tmp = await tmpdir({
53+
init: async (dir) => {
54+
await Bun.write(path.join(dir, "subdir", "AGENTS.md"), "# Subdir Instructions")
55+
await Bun.write(path.join(dir, "subdir", "nested", "file.ts"), "const x = 1")
56+
},
57+
})
58+
await Instance.provide({
59+
directory: tmp.path,
60+
fn: async () => {
61+
const filepath = path.join(tmp.path, "subdir", "AGENTS.md")
62+
const system = await InstructionPrompt.systemPaths()
63+
expect(system.has(filepath)).toBe(false)
64+
65+
const results = await InstructionPrompt.resolve([], filepath, "test-message-2")
66+
expect(results).toEqual([])
67+
},
68+
})
69+
})
5070
})

0 commit comments

Comments
 (0)