Skip to content

The agent that grows with you

fix(cli): let the -w worktree decide session.create's cwd only while selected

unsupportedpastelsfc18e69bfc1f
diff --git a/hermes_cli/gateway_session_store.py b/hermes_cli/gateway_session_store.py
index a0ba27916af..56abb8e6e30 100644
--- a/hermes_cli/gateway_session_store.py
+++ b/hermes_cli/gateway_session_store.py
@@ -487,15 +487,17 @@ def _creation_policy(launch):
def _launch_cwd():
- """``session.create``'s ``cwd``, a host folder the owner checks on its own host. A folder chosen on
- this host (the ``-w`` / ``/worktree new`` tree, or a local backend's TERMINAL_CWD) is sent as is, so
- a vanished one is refused as on main rather than swapped for the checkout. Only a docker/ssh
- ``terminal.cwd`` that is not a host directory (a path inside the sandbox) sends the launch dir, and
- the backend keeps its own cwd (``terminal_uses_host_cwd``)."""
- worktree = getattr(sys.modules.get("cli"), "_active_worktree", None)
- if worktree and worktree.get("path"):
- return os.path.realpath(worktree["path"])
+ """``session.create``'s ``cwd``, a host folder the owner checks on its own host. TERMINAL_CWD is the
+ folder this CLI has selected (``-w``, ``/worktree new`` and ``/resume`` all move it). A local backend
+ 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_cwd = os.getenv("TERMINAL_CWD")
+ trees = (getattr(sys.modules.get(name), "_active_worktree", None) for name in ("cli",))
+ 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)
local = (os.getenv("TERMINAL_ENV") or "local").strip().lower() in ("", "local")
return os.path.realpath(terminal_cwd if terminal_cwd and (local or os.path.isdir(terminal_cwd)) else os.getcwd())
diff --git a/tests/hermes_cli/test_gateway_session_store.py b/tests/hermes_cli/test_gateway_session_store.py
index 471e19a22d5..9b0cc7044e3 100644
--- a/tests/hermes_cli/test_gateway_session_store.py
+++ b/tests/hermes_cli/test_gateway_session_store.py
@@ -571,6 +571,28 @@ def test_a_removed_w_worktree_is_sent_so_the_gateway_refuses_it(store, monkeypat
assert [p for m, p in client.calls if m == "session.create"][-1]["cwd"] == str(tree.resolve())
[email protected]("backend,mount", [("local", None), ("docker", "true")])
+def test_a_project_resumed_after_w_is_sent_not_the_w_worktree(store, monkeypatch, tmp_path, backend, mount):
+ """``hermes -w`` in A, ``/resume`` of a session from B, then ``/new``: the resume moves cwd and
+ TERMINAL_CWD to B but A stays recorded (its quit cleanup is still due), so A must not win."""
+ import sys
+ from hermes_cli.cli_session_mixin import CLISessionMixin
+ db, client = store
+ tree, project_b = tmp_path / "a" / ".worktrees" / "wt", tmp_path / "b"
+ tree.mkdir(parents=True)
+ project_b.mkdir()
+ monkeypatch.chdir(tmp_path / "a")
+ monkeypatch.setitem(sys.modules, "cli", types.SimpleNamespace(_active_worktree={"path": str(tree)}))
+ monkeypatch.setenv("TERMINAL_CWD", str(tree))
+ monkeypatch.setenv("TERMINAL_ENV", backend)
+ if mount:
+ monkeypatch.setenv("TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", mount)
+ monkeypatch.delenv("HERMES_SESSION_SOURCE", raising=False)
+ CLISessionMixin._restore_session_cwd(types.SimpleNamespace(), {"cwd": str(project_b)}, quiet=True)
+ db.create({"toolsets": ["file"]}, record_cwd=False)
+ assert [p for m, p in client.calls if m == "session.create"][-1]["cwd"] == str(project_b.resolve())
+
+
def test_explicit_reasoning_max_turns_and_checkpoints_reach_session_create(store, monkeypatch):
"""Main's HermesCLI built AIAgent with the run's `--reasoning`, `--max-turns` and `--checkpoints`;
the gateway now builds the agent, so `session.create` must carry them or the run silently uses