hermes - ✅(Solved) Fix [Bug]: file_tools.py ignores TERMINAL_CWD — file operations leak out of worktree isolation in CLI -w mode [3 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
NousResearch/hermes-agent#12689Fetched 2026-04-20 12:17:25
View on GitHub
Comments
0
Participants
1
Timeline
7
Reactions
0
Participants
Timeline (top)
cross-referenced ×3referenced ×3labeled ×1

Error Message

Additional Logs / Traceback (optional)

Root Cause

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.

Fix Action

Fix / Workaround

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.

PR fix notes

PR #12695: fix(file_tools): resolve relative paths against TERMINAL_CWD for worktree isolation

Description (problem / solution / changelog)

Resolves #12689

What Changed

When launching Hermes with the worktree flag, file tools were resolving relative paths using os.getcwd() or Path(path).resolve(), which points to the main repository root. This allowed operations to leak out of worktree isolation.

This PR introduces a local _resolve_path helper inside tools/file_tools.py to ensure operations correctly resolve against TERMINAL_CWD when it is set, preventing modification of files outside the worktree.

Changed files

  • tools/file_tools.py (modified, +15/-4)

PR #12721: fix(file-tools): resolve relative paths against TERMINAL_CWD

Description (problem / solution / changelog)

Summary

Fixes #12689 — file tools (, , ) resolve relative paths using os.getcwd() / Path.resolve(), which in worktree (-w) mode points to the main repository instead of the worktree directory set by TERMINAL_CWD.

Changes

  • tools/file_tools.py: Add _resolve_path(path) helper that resolves relative paths against TERMINAL_CWD when set, falling back to os.getcwd(). Replace all Path(path).expanduser().resolve() calls with it.
  • tools/file_operations.py: Update ShellFileOperations._expand_path() to join relative paths with TERMINAL_CWD before returning.
  • tests/tools/test_file_tools_terminal_cwd.py: 7 unit tests covering both layers (absolute paths, relative paths with/without TERMINAL_CWD, tilde expansion, empty/whitespace env var).

Behavior

ScenarioBeforeAfter
Relative path, TERMINAL_CWD setResolves against main repo cwdResolves against TERMINAL_CWD
Relative path, TERMINAL_CWD unsetResolves against cwdResolves against cwd (unchanged)
Absolute pathNo changeNo change

All 7 new tests pass. Existing test suite unaffected.

Changed files

  • tests/tools/test_file_tools_terminal_cwd.py (added, +63/-0)
  • tools/file_operations.py (modified, +8/-0)
  • tools/file_tools.py (modified, +23/-3)

PR #12808: fix(file_tools): resolve relative paths against TERMINAL_CWD for worktree isolation (#12689)

Description (problem / solution / changelog)

Problem

When Hermes is launched with -w (worktree mode), TERMINAL_CWD is correctly set to the worktree path, but file tools (read_file, write_file, patch) resolve relative paths using Path(path).resolve() which defaults to the process CWD — the main repository root. This causes file operations to leak out of the worktree.

Fixes #12689

Changes

  • tools/file_tools.py: Added _resolve_path(path) helper that resolves relative paths against TERMINAL_CWD env var when set, falling back to normal Path.resolve(). Applied to all file path resolution points.
  • tests/tools/test_file_tools.py: 4 tests covering absolute path passthrough, relative path resolution via TERMINAL_CWD, fallback to os.getcwd(), and tilde expansion.

Testing

4 passed in 1.77s

Changed files

  • tests/tools/test_file_tools.py (modified, +32/-0)
  • tools/file_tools.py (modified, +22/-3)

Code Example

Provided on request.

---
RAW_BUFFERClick to expand / collapse

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

extent analysis

TL;DR

Modify the hermes-agent/tools/file_tools.py to use TERMINAL_CWD for resolving relative paths instead of relying on the process's current working directory.

Guidance

  • Identify the file_tools.py module and locate the path resolution logic using Path(path).resolve().
  • Update the path resolution to use TERMINAL_CWD as the base directory for resolving relative paths.
  • Verify that the TERMINAL_CWD variable is correctly set by the _setup_worktree function in cli.py.
  • Test the updated file_tools.py module to ensure that file operations are performed within the intended worktree directory.

Example

import os
from pathlib import Path

# Before
path = Path("relative/path").resolve()

# After
terminal_cwd = os.environ.get("TERMINAL_CWD")
path = (Path(terminal_cwd) / "relative/path").resolve()

Notes

The proposed fix assumes that the TERMINAL_CWD environment variable is correctly set by the cli.py module. If this is not the case, additional debugging may be required to identify the root cause.

Recommendation

Apply workaround: Update the file_tools.py module to use TERMINAL_CWD for resolving relative paths, as this is a targeted fix that addresses the specific issue described.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

hermes - ✅(Solved) Fix [Bug]: file_tools.py ignores TERMINAL_CWD — file operations leak out of worktree isolation in CLI -w mode [3 pull requests, 1 participants]