Skip to content

fix: preserve configured cwd in local terminal snapshots#9798

Open
IanGely wants to merge 1 commit into
NousResearch:mainfrom
IanGely:fix/local-terminal-snapshot-cwd
Open

fix: preserve configured cwd in local terminal snapshots#9798
IanGely wants to merge 1 commit into
NousResearch:mainfrom
IanGely:fix/local-terminal-snapshot-cwd

Conversation

@IanGely

@IanGely IanGely commented Apr 14, 2026

Copy link
Copy Markdown

Summary

  • cd into the configured working directory before capturing the local shell snapshot
  • preserve the intended workspace for subsequent local terminal tool calls

Problem

For local terminal environments, init_session() captured the shell snapshot before changing into the configured cwd. When Hermes was launched from its install repo, later terminal calls like pwd could resolve to the Hermes repo instead of the configured workspace.

Reproduction

  • configure TERMINAL_CWD to a project directory
  • start Hermes from a different directory, such as the Hermes install repo
  • run the local terminal tool with pwd
  • observe that it returns the process cwd instead of the configured workspace

Fix

  • prepend cd {self.cwd} to the snapshot bootstrap script in BaseEnvironment.init_session()
  • ensure the initial snapshot and persisted cwd are captured from the configured workspace

Validation

  • python -m py_compile tools/environments/base.py
  • direct local reproduction now returns the configured workspace path instead of the Hermes repo path

@xlionjuan xlionjuan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: Fix is correct, but surfaces a pre-existing bug in init_session() error handling

The core fix (cd {quoted_init_cwd} || exit 126) is correct and matches the diagnosis.

However, I noticed that the init_session() try/except block unconditionally sets self._snapshot_ready = True after a successful _wait_for_process() call, regardless of the subprocess exit code:

try:
    proc = self._run_bash(bootstrap, login=True, timeout=self._snapshot_timeout)
    result = self._wait_for_process(proc, timeout=self._snapshot_timeout)
    self._snapshot_ready = True   # ← set unconditionally; exit code 126 is ignored
    self._update_cwd(result)
    ...
except Exception as exc:
    self._snapshot_ready = False  # ← only Exception triggers this

When cd || exit 126 fails, _wait_for_process() returns normally with returncode=126 (no exception is raised), so _snapshot_ready ends up True even though the cd failed.

This is a pre-existing bug independent of this PR. Before this PR, the snapshot was always created successfully but captured the wrong CWD. After this PR, in the normal case it now captures the correct CWD—but when the configured cwd doesn't exist, the behavior is still broken (snapshot marked ready with the wrong path).

Suggestion: Consider also checking result.get("returncode") != 0 in init_session() and setting _snapshot_ready = False accordingly. The fallback to bash -l per command is the intended safe behavior when the snapshot can't be established at the configured path.

The PR fix itself is correct and ready to merge. This is a separate, pre-existing issue worth addressing either as a follow-up or as part of this PR if the maintainer prefers.

(Generated by AI Agent)

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent loop, run_agent.py, prompt builder tool/terminal Terminal execution and process management backend/local Local shell execution duplicate This issue or pull request already exists labels Apr 22, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #7802 — same fix to honor configured cwd at session init in BaseEnvironment.

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

Labels

backend/local Local shell execution comp/agent Core agent loop, run_agent.py, prompt builder duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists tool/terminal Terminal execution and process management type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants