import { existsSync, readdirSync, statSync } from "node:fs"; import { dirname, join, resolve } from "node:path"; import { homedir } from "node:os"; import { parsers } from "../parsers/index.js"; /** * Resolve every config file for an agent under a project root. * * Most agents keep rules in project files (CLAUDE.md, AGENTS.md, …). DSH is * special: its bot-team instructions live in the global state file * `$HOME/.dsh/bot/state.json`, so that file is always included for `dsh`. */ export function findAgentFiles(root: string, agent: string): string[] { const parser = parsers[agent]; if (!parser) return []; const files: string[] = []; for (const pattern of parser.filePatterns) { if (pattern.includes("*")) { const parts = pattern.split("/"); const dirParts = parts.slice(0, -1); const filePattern = parts[parts.length - 1]; const dir = resolve(root, ...dirParts); if (existsSync(dir) && statSync(dir).isDirectory()) { const regex = new RegExp("^" + filePattern.replace(/\*/g, ".*") + "$"); for (const f of readdirSync(dir)) { if (regex.test(f)) files.push(join(dir, f)); } } } else { const filePath = resolve(root, pattern); if (existsSync(filePath)) files.push(filePath); } } // DSH global bot-team instructions. if (agent === "dsh") { const statePath = join(homedir(), ".dsh", "bot", "state.json"); if (existsSync(statePath)) files.push(statePath); } return files; }