Skip to content

The agent that grows with you

fix(cli): read the -w worktree from __main__ too for python cli.py -w

unsupportedpastelsa648f1b4768b
diff --git a/hermes_cli/gateway_session_store.py b/hermes_cli/gateway_session_store.py
index 56abb8e6e30..03b4a0da31c 100644
--- a/hermes_cli/gateway_session_store.py
+++ b/hermes_cli/gateway_session_store.py
@@ -492,9 +492,10 @@ def _launch_cwd():
sends it as is, as on main. A docker/ssh one sends it when it is a host directory or the recorded
``-w`` tree (a vanished tree is then refused as on main, not swapped for the checkout); otherwise
it is a path inside the sandbox, so the launch dir is sent and the backend keeps its own cwd
- (``terminal_uses_host_cwd``). A tree no longer selected (a later /resume moved away) never decides."""
+ (``terminal_uses_host_cwd``). A tree no longer selected (a later /resume moved away) never decides.
+ ``hermes -w`` records the tree in the imported ``cli`` module, ``python cli.py -w`` in ``__main__``."""
terminal_cwd = os.getenv("TERMINAL_CWD")
- trees = (getattr(sys.modules.get(name), "_active_worktree", None) for name in ("cli",))
+ trees = (getattr(sys.modules.get(name), "_active_worktree", None) for name in ("cli", "__main__"))
recorded = {os.path.realpath(tree["path"]) for tree in trees if tree and tree.get("path")}
if terminal_cwd and os.path.realpath(terminal_cwd) in recorded:
return os.path.realpath(terminal_cwd)
diff --git a/tests/hermes_cli/test_gateway_session_store.py b/tests/hermes_cli/test_gateway_session_store.py
index 9b0cc7044e3..e69eef585b2 100644
--- a/tests/hermes_cli/test_gateway_session_store.py
+++ b/tests/hermes_cli/test_gateway_session_store.py
@@ -547,10 +547,12 @@ def test_a_mounting_docker_backend_sends_the_w_worktree_not_the_launch_dir(store
assert [p for m, p in client.calls if m == "session.create"][-1]["cwd"] == str(tree.resolve())
[email protected]("entry", ["cli", "__main__"])
@pytest.mark.parametrize("backend,mount", [("local", None), ("docker", "true")])
-def test_a_removed_w_worktree_is_sent_so_the_gateway_refuses_it(store, monkeypatch, tmp_path, backend, mount):
+def test_a_removed_w_worktree_is_sent_so_the_gateway_refuses_it(store, monkeypatch, tmp_path, backend, mount, entry):
"""-w publishes its tree in TERMINAL_CWD without a chdir; once the tree is gone, creating from the
- original checkout would put the agent's edits there. Main sent the path and the gateway refused it."""
+ original checkout would put the agent's edits there. Main sent the path and the gateway refused it.
+ ``hermes -w`` records the tree in the imported ``cli`` module, ``python cli.py -w`` in ``__main__``."""
import sys
from hermes_cli import gateway_session_store as gss
db, client = store
@@ -558,7 +560,10 @@ def test_a_removed_w_worktree_is_sent_so_the_gateway_refuses_it(store, monkeypat
repo.mkdir()
tree = repo / ".worktrees" / "wt" # never created: removed while the CLI was open
monkeypatch.chdir(repo)
- monkeypatch.setitem(sys.modules, "cli", types.SimpleNamespace(_active_worktree={"path": str(tree)}))
+ if entry == "cli":
+ monkeypatch.setitem(sys.modules, "cli", types.SimpleNamespace(_active_worktree={"path": str(tree)}))
+ else:
+ monkeypatch.setattr(sys.modules["__main__"], "_active_worktree", {"path": str(tree)}, raising=False)
monkeypatch.setenv("TERMINAL_CWD", str(tree))
monkeypatch.setenv("TERMINAL_ENV", backend)
if mount: