hermes-agent/tests/tools/test_resolve_path.py
ethernet c80b244b52 refactor(terminal,file-tools): delete legacy env-side cwd tracking (step 4)
The per-session record store is now the ONLY cwd mechanism. Deleted:

- env.cwd_owner stamping + prev_owner threading (terminal_tool): the
  shared env no longer carries ownership metadata at all
- _resolve_command_cwd's env/prev_owner params: resolution is
  workdir > session record > config/override default
- file_tools._live_cwd_if_owned + _get_live_tracking_cwd: path
  resolution never consults the shared env's live cwd
- file_tools._last_known_cwd + _remember_last_known_cwd +
  _last_known_cwd_for: the #26211 preserved-anchor registry is
  subsumed by the session record, which never lived on the env and
  therefore cannot be lost to env cleanup. The _get_file_ops
  stale-cache rescue now writes the record instead.
- env recreation (both _get_file_ops and terminal_tool) seeds the
  fresh env from override > session record > config

Why no transition fallback: the legacy state was process-local and
in-memory exactly like the record store — after a restart both start
empty, and within a running process every legacy write site has been
dual-writing the record since step 1. There is no populated-legacy/
empty-record state to fall back for.

Tests updated to drive the record store instead of the deleted
mechanism; the cross-session isolation suite now asserts the same
behavior contracts (no leak, cd isolation, #26211 persistence)
against the new architecture, plus a new "session C inherits nothing"
case that the old ownership guard could not express.
2026-07-16 00:18:38 -07:00

74 lines
2.9 KiB
Python

"""Tests for _resolve_path() — TERMINAL_CWD-aware path resolution in file_tools."""
import os
from pathlib import Path
from types import SimpleNamespace
class TestResolvePath:
"""Verify _resolve_path respects TERMINAL_CWD for worktree isolation."""
def test_relative_path_uses_terminal_cwd(self, monkeypatch, tmp_path):
"""Relative paths resolve against TERMINAL_CWD, not process CWD."""
monkeypatch.setenv("TERMINAL_CWD", str(tmp_path))
from tools.file_tools import _resolve_path
result = _resolve_path("foo/bar.py")
assert result == (tmp_path / "foo" / "bar.py")
def test_absolute_path_ignores_terminal_cwd(self, monkeypatch, tmp_path):
"""Absolute paths are unaffected by TERMINAL_CWD."""
monkeypatch.setenv("TERMINAL_CWD", str(tmp_path))
from tools.file_tools import _resolve_path
absolute = (tmp_path / "already-absolute.txt").resolve()
result = _resolve_path(str(absolute))
assert result == absolute
def test_falls_back_to_cwd_without_terminal_cwd(self, monkeypatch):
"""Without TERMINAL_CWD, falls back to os.getcwd()."""
monkeypatch.delenv("TERMINAL_CWD", raising=False)
from tools.file_tools import _resolve_path
result = _resolve_path("some_file.txt")
assert result == Path(os.getcwd()) / "some_file.txt"
def test_tilde_expansion(self, monkeypatch, tmp_path):
"""~ is expanded before TERMINAL_CWD join (already absolute)."""
monkeypatch.setenv("TERMINAL_CWD", str(tmp_path))
from tools.file_tools import _resolve_path
result = _resolve_path("~/notes.txt")
# After expanduser, ~/notes.txt becomes absolute → TERMINAL_CWD ignored
assert result == Path.home() / "notes.txt"
def test_result_is_resolved(self, monkeypatch, tmp_path):
"""Output path has no '..' components."""
monkeypatch.setenv("TERMINAL_CWD", str(tmp_path))
from tools.file_tools import _resolve_path
result = _resolve_path("a/../b/file.txt")
assert ".." not in str(result)
assert result == (tmp_path / "b" / "file.txt")
def test_relative_path_prefers_recorded_session_cwd(self, monkeypatch, tmp_path):
"""The session's recorded cwd must win after the terminal changes directory."""
start_dir = tmp_path / "start"
live_dir = tmp_path / "worktree"
start_dir.mkdir()
live_dir.mkdir()
monkeypatch.setenv("TERMINAL_CWD", str(start_dir))
from tools import file_tools, terminal_tool
task_id = "live-cwd"
# The session's completed `cd` recorded the new directory.
terminal_tool.record_session_cwd(task_id, str(live_dir))
try:
result = file_tools._resolve_path("nested/file.txt", task_id=task_id)
finally:
terminal_tool.clear_session_cwd(task_id)
assert result == live_dir / "nested" / "file.txt"