Skip to content

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

Closed
aniruddhaadak80 wants to merge 1 commit into
NousResearch:mainfrom
aniruddhaadak80:fix/file-tools-worktree
Closed

fix(file_tools): resolve relative paths against TERMINAL_CWD for worktree isolation#12695
aniruddhaadak80 wants to merge 1 commit into
NousResearch:mainfrom
aniruddhaadak80:fix/file-tools-worktree

Conversation

@aniruddhaadak80

Copy link
Copy Markdown
Contributor

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.

…te file operations within the worktree as described in NousResearch#12689. Prevents operations from leaking back to the repository root directory.
Copilot AI review requested due to automatic review settings April 19, 2026 21:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to fix worktree isolation in CLI -w mode by ensuring file tool path resolution uses TERMINAL_CWD (worktree base) rather than the host process CWD, preventing file operations from targeting the main repo unintentionally.

Changes:

  • Added a _resolve_path() helper to resolve relative paths against TERMINAL_CWD.
  • Updated sensitive-path checks and read staleness tracking to use _resolve_path().
  • Updated read_file_tool()’s internal resolved-path handling to use _resolve_path().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tools/file_tools.py
Comment on lines +74 to +82
def _resolve_path(filepath: str) -> Path:
"""Resolve a path relative to TERMINAL_CWD (the worktree base directory)
instead of the main repository root.
"""
p = Path(filepath).expanduser()
if not p.is_absolute():
base = os.environ.get("TERMINAL_CWD", os.getcwd())
p = Path(base) / p
return p.resolve()

Copilot AI Apr 19, 2026

Copy link

Choose a reason for hiding this comment

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

_resolve_path() resolves relative paths against TERMINAL_CWD, but the actual read/write/patch operations execute relative to the live terminal environment cwd (which can change after cd). This can desync safety checks/dedup/staleness from the file actually being touched (e.g., a relative write after cd /etc could bypass _check_sensitive_path; dedup can return a stale stub for the wrong file). Consider resolving relative paths against the per-task terminal env cwd (or the same effective cwd used by ShellFileOperations) rather than the static TERMINAL_CWD env var, and plumb task_id through as needed.

Copilot uses AI. Check for mistakes.
Comment thread tools/file_tools.py
Comment on lines +74 to +82
def _resolve_path(filepath: str) -> Path:
"""Resolve a path relative to TERMINAL_CWD (the worktree base directory)
instead of the main repository root.
"""
p = Path(filepath).expanduser()
if not p.is_absolute():
base = os.environ.get("TERMINAL_CWD", os.getcwd())
p = Path(base) / p
return p.resolve()

Copilot AI Apr 19, 2026

Copy link

Choose a reason for hiding this comment

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

New TERMINAL_CWD-based path resolution is core to worktree isolation, but there are no tests asserting that read_file/write_file/patch resolve relative paths against TERMINAL_CWD (and not os.getcwd()). Adding a focused unit test would prevent regressions, especially around relative paths with .. segments.

Copilot uses AI. Check for mistakes.
@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #13161 (#13161). Your commit was cherry-picked onto current main with your authorship preserved in git log. Thanks for the fix!

@aniruddhaadak80

Copy link
Copy Markdown
Contributor Author

Merged via PR #13161 (#13161). Your commit was cherry-picked onto current main with your authorship preserved in git log. Thanks for the fix!

thank you so much @teknium1

@aniruddhaadak80 aniruddhaadak80 deleted the fix/file-tools-worktree branch April 24, 2026 05:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants