Skip to content

[Bug]: file_tools.py ignores TERMINAL_CWD — file operations leak out of worktree isolation in CLI -w mode #12689

@leoschwarz

Description

@leoschwarz

Bug Description

I have been observing this issue repeatedly and first thought it was due to the models used, because it does not happen in every session.

When launching Hermes with -w (worktree flag), file tools (read_file, write_file, patch) resolve relative paths using os.getcwd() or Path(path).resolve(), which points to the main repository root — not the worktree. This causes the agent to accidentally modify files in the main repository instead of the isolated worktree, even though TERMINAL_CWD is correctly set to the worktree path.

Steps to Reproduce

  1. Launch Hermes with -w pointing to a new worktree branch: hermes -w feature-x
  2. Verify worktree exists: git worktree list shows feature-x at .worktrees/feature-x/
  3. Ask the agent to read, write, or edit any file using a relative path
  4. File modifications can land in the main repository instead of the worktree

Expected Behavior

File operations should resolve relative paths against TERMINAL_CWD (the worktree directory), not os.getcwd() (the main repository).

Actual Behavior

os.getcwd() returns the main repo path. File tools use Path(path).resolve() which defaults to process CWD, ignoring TERMINAL_CWD.

Affected Component

Tools (terminal, file ops, web, code execution, etc.)

Messaging Platform (if gateway-related)

N/A (CLI only)

Debug Report

Provided on request.

Operating System

Debian 13

Python Version

Python 3.13.5

Hermes Version

Hermes Agent v0.10.0 (2026.4.16)

Additional Logs / Traceback (optional)

Root Cause Analysis (optional)

In hermes-agent/tools/file_tools.py, path resolution uses Path(path).resolve() which defaults to the process CWD. Meanwhile TERMINAL_CWD is set correctly by cli.py's _setup_worktree but only respected by the terminal tool backend, not file tools.

Proposed Fix (optional)

In hermes-agent/tools/file_tools.py, path resolution uses Path(path).resolve() which defaults to the process CWD. Meanwhile TERMINAL_CWD is set correctly by cli.py's _setup_worktree but only respected by the terminal tool backend, not file tools.

Are you willing to submit a PR for this?

  • I'd like to fix this myself and submit a PR

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions