From 8ee4f3281990dd78667cd1ae6d68fbd310742884 Mon Sep 17 00:00:00 2001 From: Teknium Date: Tue, 24 Mar 2026 17:30:33 -0700 Subject: [PATCH] fix(gateway): use TERMINAL_CWD for context file discovery, not process cwd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The gateway process runs from the hermes-agent install directory, so os.getcwd() picks up the repo's AGENTS.md (16k chars) and other dev context files — inflating input tokens by ~10k on every gateway message. Fix: use TERMINAL_CWD (which the gateway sets to MESSAGING_CWD or $HOME) as the cwd for build_context_files_prompt(). In CLI mode, TERMINAL_CWD is the user's actual project directory, so behavior is unchanged. Before: gateway 15-20k input tokens, CLI 6-8k After: gateway ~6-8k input tokens (same as CLI) Reported by keri on Discord. --- run_agent.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/run_agent.py b/run_agent.py index 13b5e2d3c..220921160 100644 --- a/run_agent.py +++ b/run_agent.py @@ -2361,7 +2361,13 @@ class AIAgent: prompt_parts.append(skills_prompt) if not self.skip_context_files: - context_files_prompt = build_context_files_prompt(skip_soul=_soul_loaded) + # Use TERMINAL_CWD for context file discovery when set (gateway + # mode). The gateway process runs from the hermes-agent install + # dir, so os.getcwd() would pick up the repo's AGENTS.md and + # other dev files — inflating token usage by ~10k for no benefit. + _context_cwd = os.getenv("TERMINAL_CWD") or None + context_files_prompt = build_context_files_prompt( + cwd=_context_cwd, skip_soul=_soul_loaded) if context_files_prompt: prompt_parts.append(context_files_prompt)