Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/opencode/src/session/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Instance } from "../project/instance"
import { Flag } from "@/flag/flag"
import { Log } from "../util/log"
import { Glob } from "../util/glob"
import { git } from "../util/git"
import type { MessageV2 } from "./message-v2"

const log = Log.create({ service: "instruction" })
Expand Down Expand Up @@ -42,6 +43,28 @@ async function resolveRelative(instruction: string): Promise<string[]> {
return Filesystem.globUp(instruction, Flag.OPENCODE_CONFIG_DIR, Flag.OPENCODE_CONFIG_DIR).catch(() => [])
}

function normalize(cwd: string, value: string) {
const text = value.replace(/[\r\n]+$/, "")
if (!text) return
const resolved = Filesystem.windowsPath(text)
if (path.isAbsolute(resolved)) return path.normalize(resolved)
return path.resolve(cwd, resolved)
}

async function repo(filepath: string) {
const cwd = path.dirname(filepath)
const result = await git(["rev-parse", "--show-toplevel"], { cwd })
if (result.exitCode !== 0) return
return normalize(cwd, result.text())
}

async function main(filepath: string) {
const dir = await repo(filepath)
if (!dir) return
const file = path.resolve(path.join(dir, "AGENTS.md"))
if (await Filesystem.exists(file)) return file
}

export namespace InstructionPrompt {
const state = Instance.state(() => {
return {
Expand Down Expand Up @@ -187,6 +210,22 @@ export namespace InstructionPrompt {
current = path.dirname(current)
}

if (Instance.containsPath(target)) {
return results
}

const found = await main(target)
if (!found || found === target || system.has(found) || already.has(found) || isClaimed(messageID, found)) {
return results
}

claim(messageID, found)
const content = await Filesystem.readText(found).catch(() => undefined)
if (!content) {
return results
}

results.push({ filepath: found, content: "Instructions from: " + found + "\n" + content })
return results
}
}
29 changes: 29 additions & 0 deletions packages/opencode/test/session/instruction.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { afterEach, beforeEach, describe, expect, test } from "bun:test"
import { $ } from "bun"
import path from "path"
import { InstructionPrompt } from "../../src/session/instruction"
import { Instance } from "../../src/project/instance"
Expand Down Expand Up @@ -68,6 +69,34 @@ describe("InstructionPrompt.resolve", () => {
},
})
})

test("returns repo root AGENTS.md for files outside current project", async () => {
await using external = await tmpdir({
init: async (dir) => {
await Bun.write(path.join(dir, "AGENTS.md"), "# External Root")
await Bun.write(path.join(dir, "subdir", "AGENTS.md"), "# External Subdir")
await Bun.write(path.join(dir, "subdir", "nested", "file.ts"), "const x = 1")
},
})
await using tmp = await tmpdir()

await $`git init`.cwd(external.path).quiet()
await $`git init`.cwd(tmp.path).quiet()

await Instance.provide({
directory: tmp.path,
fn: async () => {
const results = await InstructionPrompt.resolve(
[],
path.join(external.path, "subdir", "nested", "file.ts"),
"test-message-3",
)

expect(results.length).toBe(1)
expect(results[0].filepath).toBe(path.join(external.path, "AGENTS.md"))
},
})
})
})

describe("InstructionPrompt.systemPaths OPENCODE_CONFIG_DIR", () => {
Expand Down
Loading