Skip to content

The agent that grows with you

fix(cli): a /worktree new session gets its clean tree back on resume

kshitijk4poor0befbd00d35c
diff --git a/hermes_cli/cli_commands_mixin.py b/hermes_cli/cli_commands_mixin.py
index f4c154c8b63..deecc61874d 100644
--- a/hermes_cli/cli_commands_mixin.py
+++ b/hermes_cli/cli_commands_mixin.py
@@ -1077,7 +1077,7 @@ class CLICommandsMixin(CLICommandsSessionToolsMixin):
from hermes_cli.gateway_client import GatewayClientError
from hermes_cli.gateway_session_store import move_cli_workspace
try:
- move_cli_workspace(self, wt_info["path"]) # the gateway session's tools, then this process
+ move_cli_workspace(self, wt_info) # the gateway session's tools, then this process
except (GatewayClientError, OSError) as e:
return print(f" {_t('worktree.enter_failed', error=e)}")
_cli._active_worktree = wt_info
diff --git a/hermes_cli/gateway_session_store.py b/hermes_cli/gateway_session_store.py
index 00ca298efbd..47f31528c61 100644
--- a/hermes_cli/gateway_session_store.py
+++ b/hermes_cli/gateway_session_store.py
@@ -515,14 +515,19 @@ def _check_resume_launch(launch, snapshot):
raise resume_policy_error(differs)
-def move_cli_workspace(cli, cwd):
+def move_cli_workspace(cli, wt_info):
"""``/worktree new``: the session's tools run in the gateway under the cwd frozen in its policy, so
move that first (``session.mutate workspace``, Desktop's Move to project; a refusal raises before
- anything moved). Then this process's cwd and TERMINAL_CWD follow, so a later /new creates its session
- from the tree; a chdir OSError leaves both where they were (the gateway session has moved by then)."""
+ anything moved). The moved session resumes in the tree, so it is recorded as a ``--tui -w`` tree is:
+ quit removes it when clean and a resume recreates it (N8). Then this process's cwd and TERMINAL_CWD
+ follow, so a later /new creates its session from the tree; a chdir OSError leaves both where they
+ were (the gateway session has moved by then)."""
from hermes_cli.gateway_automation import gateway_connection
+ cwd = wt_info["path"]
if gateway_connection(cli) is not None:
cli._session_db._mutate(cli.session_id, "workspace", {"cwd": cwd})
+ from hermes_cli.tui_worktree_resume import record_tui_worktree
+ record_tui_worktree(wt_info)
os.chdir(cwd)
os.environ["TERMINAL_CWD"] = cwd
diff --git a/hermes_cli/tui_worktree_resume.py b/hermes_cli/tui_worktree_resume.py
index 2303456a5bf..b9dd9d05290 100644
--- a/hermes_cli/tui_worktree_resume.py
+++ b/hermes_cli/tui_worktree_resume.py
@@ -42,7 +42,8 @@ def _read() -> dict:
def record_tui_worktree(wt_info: dict) -> None:
- """Remember a tree the ``--tui -w`` launcher created. Never raises."""
+ """Remember a tree the ``--tui -w`` launcher created, or a classic ``/worktree new`` moved its
+ gateway session into. Never raises."""
try:
from hermes_cli.auth import _file_lock
from utils import atomic_json_write
diff --git a/tests/hermes_cli/test_worktree_command.py b/tests/hermes_cli/test_worktree_command.py
index 7e778c6cd80..27bab9b8354 100644
--- a/tests/hermes_cli/test_worktree_command.py
+++ b/tests/hermes_cli/test_worktree_command.py
@@ -177,3 +177,21 @@ def test_new_refused_by_the_gateway_never_claims_the_tools_moved(repo):
assert "session_busy" in out and t("cli.commands.worktree.tools_in_worktree") not in out
assert os.path.realpath(os.getcwd()) == os.path.realpath(str(repo))
assert "TERMINAL_CWD" not in os.environ and cli_mod._active_worktree is None
+
+
+@requires_git
+def test_a_moved_session_gets_its_clean_tree_back_on_resume(repo):
+ """The move freezes the tree as the session's cwd and quit removes a clean tree, so a resume had
+ every turn refused ``workspace_unavailable``. The tree is recorded as a ``--tui -w`` one is, so the
+ resume recreates it at the same path (N8)."""
+ from hermes_cli.tui_worktree_resume import recreate_tui_worktree
+ stub, _client, conn = _gateway_stub()
+ try:
+ _run(stub, "/worktree new kept")
+ finally:
+ conn.close()
+ wt = repo / ".worktrees" / "kept"
+ os.chdir(repo)
+ cli_mod._cleanup_worktree(cli_mod._active_worktree) # quit: the tree is clean, so it goes
+ assert not wt.exists()
+ assert recreate_tui_worktree(str(wt), "local-s") == str(wt) and wt.is_dir()