diff --git a/packages/opencode/src/session/system.ts b/packages/opencode/src/session/system.ts index 4dc227656..7d7603b22 100644 --- a/packages/opencode/src/session/system.ts +++ b/packages/opencode/src/session/system.ts @@ -55,8 +55,13 @@ export namespace SystemPrompt { const config = await Config.get() const found = [] for (const item of CUSTOM_FILES) { - const matches = await Filesystem.findUp(item, cwd, root) - found.push(...matches.map((x) => Bun.file(x).text())) + // Search upward from current directory + const upMatches = await Filesystem.findUp(item, cwd, root) + found.push(...upMatches.map((x) => Bun.file(x).text())) + + // Search downward from current directory (limited depth to avoid performance issues) + const downMatches = await Filesystem.findDown(item, cwd, 3) + found.push(...downMatches.map((x) => Bun.file(x).text())) } found.push( Bun.file(path.join(Global.Path.config, "AGENTS.md")) diff --git a/packages/opencode/src/util/filesystem.ts b/packages/opencode/src/util/filesystem.ts index d5149cf39..4fe069b59 100644 --- a/packages/opencode/src/util/filesystem.ts +++ b/packages/opencode/src/util/filesystem.ts @@ -64,4 +64,25 @@ export namespace Filesystem { } return result } + + export async function findDown(target: string, start: string, maxDepth: number = 3) { + const result = [] + const glob = new Bun.Glob(`**/${target}`) + try { + for await (const match of glob.scan({ + cwd: start, + onlyFiles: true, + dot: true, + })) { + const fullPath = join(start, match) + const depth = match.split('/').length - 1 + if (depth <= maxDepth) { + result.push(fullPath) + } + } + } catch { + // Skip if glob fails + } + return result + } }