|
55 | 55 | nested-results)) |
56 | 56 | [])))) |
57 | 57 |
|
| 58 | +(defn ^:private ancestor-paths |
| 59 | + "Return all ancestor directories of `path` in outermost-first order. |
| 60 | + Excludes `path` itself." |
| 61 | + [path] |
| 62 | + (loop [p (fs/parent path) |
| 63 | + acc '()] |
| 64 | + (if (nil? p) |
| 65 | + (vec acc) |
| 66 | + (recur (fs/parent p) (conj acc p))))) |
| 67 | + |
| 68 | +(defn ^:private safe-canonicalize |
| 69 | + "Canonicalize `path`, falling back to a non-resolved `fs/path` if the |
| 70 | + path does not exist (e.g. a stale workspace folder or test fixture)." |
| 71 | + [path] |
| 72 | + (try (fs/canonicalize path) |
| 73 | + (catch Exception _ (fs/path path)))) |
| 74 | + |
58 | 75 | (defn agents-file-contexts |
59 | 76 | "Search for AGENTS.md file both in workspaceRoot and global config dir. |
| 77 | + When `:includeParentAgentsFiles` is true in `config`, also include |
| 78 | + AGENTS.md files from each workspace's parent directories, ordered |
| 79 | + outermost parent first, then the workspace's own AGENTS.md. |
60 | 80 | Process any found @paths mentions recursively, supporting both relative and absolute paths. |
61 | 81 | Deduplicates files to avoid reading the same file multiple times." |
62 | | - [db] |
| 82 | + [db config] |
63 | 83 | ;; TODO make it customizable by agent |
64 | | - (let [agent-file "AGENTS.md" |
65 | | - local-agent-files (keep (fn [{:keys [uri]}] |
66 | | - (let [agent-file (fs/path (shared/uri->filename uri) agent-file)] |
67 | | - (when (fs/readable? agent-file) |
68 | | - (fs/canonicalize agent-file)))) |
69 | | - (:workspace-folders db)) |
70 | | - global-agent-file (let [agent-file (fs/path (shared/global-config-dir) agent-file)] |
71 | | - (when (fs/readable? agent-file) |
72 | | - (fs/canonicalize agent-file)))] |
73 | | - (->> (concat local-agent-files |
74 | | - (when global-agent-file [global-agent-file])) |
75 | | - (mapcat #(parse-agents-file (str %)))))) |
| 84 | + (let [agent-file-name "AGENTS.md" |
| 85 | + include-parents? (boolean (:includeParentAgentsFiles config)) |
| 86 | + readable-agent-file-in (fn [dir] |
| 87 | + (let [p (fs/path dir agent-file-name)] |
| 88 | + (when (fs/readable? p) |
| 89 | + (str (fs/canonicalize p))))) |
| 90 | + {:keys [paths seen]} |
| 91 | + (reduce |
| 92 | + (fn [{:keys [paths seen]} {:keys [uri]}] |
| 93 | + (let [ws-path (safe-canonicalize (shared/uri->filename uri)) |
| 94 | + candidate-dirs (cond-> [] |
| 95 | + include-parents? (into (ancestor-paths ws-path)) |
| 96 | + true (conj ws-path)) |
| 97 | + new-paths (->> candidate-dirs |
| 98 | + (keep readable-agent-file-in) |
| 99 | + (remove seen))] |
| 100 | + {:paths (into paths new-paths) |
| 101 | + :seen (into seen new-paths)})) |
| 102 | + {:paths [] :seen #{}} |
| 103 | + (:workspace-folders db)) |
| 104 | + global-agent-file (readable-agent-file-in (shared/global-config-dir)) |
| 105 | + all-paths (cond-> paths |
| 106 | + (and global-agent-file (not (contains? seen global-agent-file))) |
| 107 | + (conj global-agent-file))] |
| 108 | + (mapcat parse-agents-file all-paths))) |
76 | 109 |
|
77 | 110 | (defn ^:private file->refined-context [path lines-range] |
78 | 111 | (if (fs/readable? path) |
|
0 commit comments