diff --git a/packages/core/src/config/plugin/instruction.ts b/packages/core/src/config/plugin/instruction.ts index 3685b248f3d..327146f233b 100644 --- a/packages/core/src/config/plugin/instruction.ts +++ b/packages/core/src/config/plugin/instruction.ts @@ -60,14 +60,17 @@ export const Plugin = define({ }) const globalSource = Effect.fn("ConfigInstructionPlugin.globalSource")(function* () { - if (!discovery.global) return [] + if (!discovery.global || !(yield* fs.isFile(globalFile))) return [] const file = yield* read(globalFile) return file ? [file] : [] }) const projectSource = Effect.fn("ConfigInstructionPlugin.projectSource")(function* () { if (!project) return [] - const walked = yield* Effect.forEach(yield* fs.up({ targets: ["AGENTS.md"], start, stop }), fs.resolve) + const walked = yield* Effect.forEach( + yield* fs.up({ targets: ["AGENTS.md"], start, stop, type: "file" }), + fs.resolve, + ) const discovered = new Set(walked.filter((file) => discovery.global || file !== globalFile)) const files = yield* Effect.forEach(discovered, read, { concurrency: "unbounded" }) if (files.some((file) => file === undefined)) return Instructions.unavailable diff --git a/packages/core/src/tool/plugin/read.ts b/packages/core/src/tool/plugin/read.ts index 909de8bbe0c..9150905fe4d 100644 --- a/packages/core/src/tool/plugin/read.ts +++ b/packages/core/src/tool/plugin/read.ts @@ -94,6 +94,7 @@ export const Plugin = { targets: [FILENAME], start: result.content.type === "list-page" ? resolved : dirname(resolved), stop: root, + type: "file", }) const candidates = (yield* Effect.forEach(discovered, fs.resolve)).filter( (file) => !FSUtil.contains(dirname(file), root) && file !== resolved, diff --git a/packages/core/test/instruction-discovery.test.ts b/packages/core/test/instruction-discovery.test.ts index e5a4d701284..1d92d07c4ae 100644 --- a/packages/core/test/instruction-discovery.test.ts +++ b/packages/core/test/instruction-discovery.test.ts @@ -431,7 +431,7 @@ describe("ConfigInstructionPlugin.Plugin", () => { it.effect("canonicalizes boundaries and honors project opt-out", () => Effect.gen(function* () { - const observed: { values: { targets: string[]; start: string; stop?: string }[] } = { values: [] } + const observed: { values: FSUtil.UpOptions[] } = { values: [] } const observingFS = Layer.effect( FSUtil.Service, FSUtil.Service.pipe( @@ -487,7 +487,7 @@ describe("ConfigInstructionPlugin.Plugin", () => { ) const repo = path.resolve("/repo") - expect(observed.values).toEqual([{ targets: ["AGENTS.md"], start: repo, stop: repo }]) + expect(observed.values).toEqual([{ targets: ["AGENTS.md"], start: repo, stop: repo, type: "file" }]) }), ) }) diff --git a/packages/util/src/fs-util.ts b/packages/util/src/fs-util.ts index c478f56fd58..b67f65473e9 100644 --- a/packages/util/src/fs-util.ts +++ b/packages/util/src/fs-util.ts @@ -32,6 +32,8 @@ export namespace FSUtil { readonly start: string readonly stop?: string readonly mode?: "all" | "first" + /** Only match regular files or directories (following symlinks). By default any existing path matches. */ + readonly type?: "file" | "directory" } export interface Interface extends FileSystem.FileSystem { @@ -165,7 +167,13 @@ export namespace FSUtil { while (true) { for (const target of options.targets) { const search = join(current, target) - if (yield* fs.exists(search)) { + const found = + options.type === "file" + ? yield* isFile(search) + : options.type === "directory" + ? yield* isDir(search) + : yield* fs.exists(search) + if (found) { result.push(search) if (options.mode === "first") return result }