Skip to content

fix(gateway): re-read terminal.cwd on session reset; use TERMINAL_CWD in environment hints#27488

Open
pinguarmy wants to merge 1 commit into
NousResearch:mainfrom
pinguarmy:fix/27383-terminal-cwd-session-reset
Open

fix(gateway): re-read terminal.cwd on session reset; use TERMINAL_CWD in environment hints#27488
pinguarmy wants to merge 1 commit into
NousResearch:mainfrom
pinguarmy:fix/27383-terminal-cwd-session-reset

Conversation

@pinguarmy

Copy link
Copy Markdown

Summary

Fixes two related bugs that prevent configured terminal.cwd from being used after /new or /reset in messaging gateway sessions.

Changes

1. agent/prompt_builder.py — Use TERMINAL_CWD in environment hints

build_environment_hints() now prefers the TERMINAL_CWD env var (bridged from config.yaml by the gateway) over os.getcwd(), which reflects the daemon's launch directory rather than the user's configured workspace.

2. gateway/run.py — Re-read terminal.cwd on each turn

_reload_runtime_env_preserving_config_authority() (called per-turn) now re-reads terminal.cwd from config.yaml and updates the TERMINAL_CWD env var. This ensures long-lived gateways pick up config changes on /new or /reset without needing a restart.

3. tests/agent/test_prompt_builder.py — Two new tests

  • test_build_environment_hints_prefers_terminal_cwd: verifies TERMINAL_CWD appears in the system prompt when set
  • test_build_environment_hints_falls_back_to_getcwd: verifies os.getcwd() is used when TERMINAL_CWD is unset

Root Cause

The comment on #27383 identified the issue: "The /new command resets the session but the gateway may not re-read terminal.cwd from config for the new session." Additionally, PR #24888 identified that build_environment_hints() uses os.getcwd() directly instead of checking the TERMINAL_CWD env var.

Testing

All 10 TestEnvironmentHints tests pass (including 2 new).

Fixes #27383
References #24882

@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the focused fix. I checked this against current main: the prompt half has landed, but the gateway refresh half needs updating before it can be salvaged cleanly.

Problems

  • The prompt-builder hunk is superseded: current main emits resolve_agent_cwd() at agent/prompt_builder.py:861, with regression coverage for TERMINAL_CWD over launch cwd at tests/agent/test_prompt_builder.py:997.
  • The gateway hunk adds a one-off terminal config bridge. Current main centralizes terminal env mapping at hermes_cli/config.py:5264 and apply_terminal_config_to_env() at hermes_cli/config.py:5309; adding cwd-only reload logic would reintroduce drift from that shared map.
  • The PR skips placeholder cwd values, which would leave a stale TERMINAL_CWD if config changes from an explicit path back to ., auto, or cwd. Startup handles placeholder fallback at gateway/run.py:1224.

Suggested changes

  • Drop the prompt hunk/tests or keep only non-duplicate coverage.
  • Rework the gateway reload to use apply_terminal_config_to_env() plus the same placeholder fallback as startup.
  • Add gateway coverage for explicit→explicit and explicit→placeholder terminal.cwd changes while TERMINAL_CWD is already set.

Automated hermes-sweeper review.

… in environment hints

Fixes two related bugs:

1. **agent/prompt_builder.py** —  now prefers
   the  env var (bridged from config.yaml by the gateway)
   over , which reflects the daemon's launch directory
   rather than the user's configured workspace.

2. **gateway/run.py** —
   (called per-turn) now re-reads  from config.yaml and
   updates the  env var, so long-lived gateways pick up
   config changes on  or  without needing a restart.

3. **tests/agent/test_prompt_builder.py** — Two new tests:
   -
   -

Fixes NousResearch#27383
References NousResearch#24882
@pinguarmy pinguarmy force-pushed the fix/27383-terminal-cwd-session-reset branch from f351dbd to fbe33d1 Compare June 13, 2026 12:43

Copy link
Copy Markdown
Author

Updated this branch against current main and addressed the latest sweeper feedback.

Changes made:

  • Dropped the superseded prompt-builder hunk/tests; current main already has the TERMINAL_CWD prompt behavior.
  • Reworked runtime gateway env reload to use apply_terminal_config_to_env() instead of a cwd-only bridge.
  • Matched startup fallback behavior for terminal.cwd placeholders and for removed/missing terminal.cwd, so stale TERMINAL_CWD is cleared instead of sticking around after config changes.
  • Added regression coverage for explicit -> explicit, explicit -> placeholder, and explicit -> missing terminal.cwd reload cases.

Focused tests run:

  • uv run --extra dev python -m pytest tests/gateway/test_config_cwd_bridge.py tests/agent/test_prompt_builder.py -q -> 159 passed, 1 skipped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/config Config system, migrations, profiles comp/agent Core agent loop, run_agent.py, prompt builder comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: change working directory for Telegram agent

3 participants