diff --git a/packages/opencode/src/tool/read.ts b/packages/opencode/src/tool/read.ts index c981ac16e43c..c9afa4249311 100644 --- a/packages/opencode/src/tool/read.ts +++ b/packages/opencode/src/tool/read.ts @@ -37,8 +37,12 @@ export const ReadTool = Tool.define("read", { const stat = Filesystem.stat(filepath) + const bypassExternal = + Boolean(ctx.extra?.["bypassCwdCheck"]) || + (!Instance.containsPath(filepath) && (await InstructionPrompt.systemPaths()).has(filepath)) + await assertExternalDirectory(ctx, filepath, { - bypass: Boolean(ctx.extra?.["bypassCwdCheck"]), + bypass: bypassExternal, kind: stat?.isDirectory() ? "directory" : "file", }) diff --git a/packages/opencode/test/tool/read.test.ts b/packages/opencode/test/tool/read.test.ts index 0761a930445f..969165da4e86 100644 --- a/packages/opencode/test/tool/read.test.ts +++ b/packages/opencode/test/tool/read.test.ts @@ -151,6 +151,43 @@ describe("tool.read external_directory permission", () => { }, }) }) + + test("does not ask for external_directory permission when reading global AGENTS.md", async () => { + await using profileTmp = await tmpdir({ + init: async (dir) => { + await Bun.write(path.join(dir, "AGENTS.md"), "# Global Instructions") + }, + }) + await using tmp = await tmpdir() + + const originalConfigDir = process.env["OPENCODE_CONFIG_DIR"] + process.env["OPENCODE_CONFIG_DIR"] = profileTmp.path + + try { + await Instance.provide({ + directory: tmp.path, + fn: async () => { + const read = await ReadTool.init() + const requests: Array> = [] + const testCtx = { + ...ctx, + ask: async (req: Omit) => { + requests.push(req) + }, + } + await read.execute({ filePath: path.join(profileTmp.path, "AGENTS.md") }, testCtx) + const extDirReq = requests.find((r) => r.permission === "external_directory") + expect(extDirReq).toBeUndefined() + }, + }) + } finally { + if (originalConfigDir === undefined) { + delete process.env["OPENCODE_CONFIG_DIR"] + } else { + process.env["OPENCODE_CONFIG_DIR"] = originalConfigDir + } + } + }) }) describe("tool.read env file permissions", () => {