From a9a1a62b14a77bd6543a01bdf8c1372a42aad2bf Mon Sep 17 00:00:00 2001 From: Aliou Diallo Date: Fri, 19 Dec 2025 15:24:49 +0100 Subject: [PATCH] fix(coding-agent): prevent global AGENTS.md from being loaded twice (#239) When cwd is `~/.pi/agent/` or a subdirectory, the global AGENTS.md file was included twice in the system prompt. `loadProjectContextFiles()` loads context files in two steps: 1. Explicitly loads from `getAgentDir()` (`~/.pi/agent/`) 2. Walks from cwd up to root, collecting all AGENTS.md/CLAUDE.md files There was no deduplication. When cwd is at or below `~/.pi/agent/`, the ancestor walk includes that directory again. Fix: track seen paths with a Set and skip duplicates. --- packages/coding-agent/src/core/system-prompt.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/coding-agent/src/core/system-prompt.ts b/packages/coding-agent/src/core/system-prompt.ts index b1305d30..4df25c41 100644 --- a/packages/coding-agent/src/core/system-prompt.ts +++ b/packages/coding-agent/src/core/system-prompt.ts @@ -65,12 +65,14 @@ function loadContextFileFromDir(dir: string): { path: string; content: string } */ export function loadProjectContextFiles(): Array<{ path: string; content: string }> { const contextFiles: Array<{ path: string; content: string }> = []; + const seenPaths = new Set(); // 1. Load global context from ~/{CONFIG_DIR_NAME}/agent/ const globalContextDir = getAgentDir(); const globalContext = loadContextFileFromDir(globalContextDir); if (globalContext) { contextFiles.push(globalContext); + seenPaths.add(globalContext.path); } // 2. Walk up from cwd to root, collecting all context files @@ -82,9 +84,10 @@ export function loadProjectContextFiles(): Array<{ path: string; content: string while (true) { const contextFile = loadContextFileFromDir(currentDir); - if (contextFile) { + if (contextFile && !seenPaths.has(contextFile.path)) { // Add to beginning so we get top-most parent first ancestorContextFiles.unshift(contextFile); + seenPaths.add(contextFile.path); } // Stop if we've reached root