diff --git a/agent/prompt_builder.py b/agent/prompt_builder.py index 7ba2edfa1..ef0393f2f 100644 --- a/agent/prompt_builder.py +++ b/agent/prompt_builder.py @@ -14,6 +14,7 @@ from pathlib import Path from hermes_constants import get_hermes_home, get_skills_dir, is_wsl from typing import Optional +from agent.runtime_cwd import resolve_agent_cwd from agent.skill_utils import ( extract_skill_conditions, extract_skill_description, @@ -802,7 +803,7 @@ def build_environment_hints() -> str: host_lines.append(f"User home directory: {os.path.expanduser('~')}") try: - host_lines.append(f"Current working directory: {os.getcwd()}") + host_lines.append(f"Current working directory: {resolve_agent_cwd()}") except OSError: pass diff --git a/tests/agent/test_prompt_builder.py b/tests/agent/test_prompt_builder.py index 3f4b0f462..588e30936 100644 --- a/tests/agent/test_prompt_builder.py +++ b/tests/agent/test_prompt_builder.py @@ -3,6 +3,7 @@ import builtins import importlib import logging +import os import sys import pytest @@ -18,6 +19,7 @@ from agent.prompt_builder import ( build_skills_system_prompt, build_nous_subscription_prompt, build_context_files_prompt, + build_environment_hints, CONTEXT_FILE_MAX_CHARS, DEFAULT_AGENT_IDENTITY, TOOL_USE_ENFORCEMENT_GUIDANCE, @@ -1248,3 +1250,25 @@ class TestOpenAIModelExecutionGuidance: + + +class TestBuildEnvironmentHints: + """The cwd line must reflect the configured TERMINAL_CWD, not the daemon launch dir. + + Regression for the gateway working-directory cluster (#24882, #24969, #27383, #29265): + gateway/cron set TERMINAL_CWD, but build_environment_hints emitted os.getcwd() — telling + the model the wrong directory. + """ + + def test_uses_terminal_cwd_when_set(self, monkeypatch, tmp_path): + monkeypatch.delenv("TERMINAL_ENV", raising=False) + monkeypatch.setenv("TERMINAL_CWD", str(tmp_path)) + monkeypatch.chdir(os.path.expanduser("~")) + assert f"Current working directory: {tmp_path}" in build_environment_hints() + + def test_falls_back_to_launch_dir_when_unset(self, monkeypatch, tmp_path): + # The #19242 local-CLI contract: no TERMINAL_CWD → the launch dir. + monkeypatch.delenv("TERMINAL_ENV", raising=False) + monkeypatch.delenv("TERMINAL_CWD", raising=False) + monkeypatch.chdir(tmp_path) + assert f"Current working directory: {tmp_path}" in build_environment_hints()