Skip to content

The agent that grows with you

fix(cli): /worktree new's failure lines say what actually happened

kshitijk4poor11d48c7af485
diff --git a/hermes_cli/cli_commands_mixin.py b/hermes_cli/cli_commands_mixin.py
index deecc61874d..21ed6c44c95 100644
--- a/hermes_cli/cli_commands_mixin.py
+++ b/hermes_cli/cli_commands_mixin.py
@@ -1074,12 +1074,9 @@ class CLICommandsMixin(CLICommandsSessionToolsMixin):
# Same keep-if-unpushed cleanup as `hermes -w`. Only one tree is "active" per process;
# an earlier one keeps its own atexit registration (explicit info arg).
atexit.register(_cli._cleanup_worktree, wt_info)
- from hermes_cli.gateway_client import GatewayClientError
- from hermes_cli.gateway_session_store import move_cli_workspace
- try:
- 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)}")
+ from hermes_cli.cli_commands_worktree import enter_new_worktree
+ if not enter_new_worktree(self, wt_info):
+ return
_cli._active_worktree = wt_info
_pr(f" {_t('worktree.ready', path=wt_info['path'])}",
f" {_t('worktree.branch', branch=wt_info['branch'])}", f" {_t('worktree.tools_in_worktree')}")
diff --git a/hermes_cli/cli_commands_worktree.py b/hermes_cli/cli_commands_worktree.py
new file mode 100644
index 00000000000..a395b6a1df9
--- /dev/null
+++ b/hermes_cli/cli_commands_worktree.py
@@ -0,0 +1,33 @@
+"""``/worktree new``'s step into the tree it just created, split out of ``cli_commands_mixin``."""
+
+from __future__ import annotations
+
+from agent.i18n import t
+from hermes_cli.cli_commands_session_tools import _t
+
+
+def enter_new_worktree(cli, wt_info) -> bool:
+ """Move *cli*'s session into the fresh tree -> True when the session's tools now run there.
+
+ A gateway refusal (e.g. ``session_busy``) moved nothing: the fresh tree is removed now instead of
+ sitting unlisted until quit, and an earlier active tree stays active. A chdir failure after the
+ gateway accepted the move leaves only this process behind, so it is reported as that."""
+ import cli as _cli
+ from hermes_cli.cli_chat_error_copy import refusal_text
+ from hermes_cli.gateway_automation import gateway_connection
+ from hermes_cli.gateway_client import GatewayClientError
+ from hermes_cli.gateway_session_store import move_cli_workspace
+ try:
+ move_cli_workspace(cli, wt_info) # the gateway session's tools, then this process
+ except GatewayClientError as e:
+ print(f" {_t('worktree.enter_failed', error=refusal_text(e))}")
+ active = _cli._active_worktree
+ _cli._cleanup_worktree(wt_info)
+ _cli._active_worktree = active
+ return False
+ except OSError as e:
+ if gateway_connection(cli) is None: # a local session: nothing moved
+ print(f" {_t('worktree.enter_failed', error=e)}")
+ return False
+ print(f" {t('cli.session.cwd_enter_failed', path=wt_info['path'], error=e)}")
+ return True
diff --git a/tests/hermes_cli/test_worktree_command.py b/tests/hermes_cli/test_worktree_command.py
index 27bab9b8354..03466f293f8 100644
--- a/tests/hermes_cli/test_worktree_command.py
+++ b/tests/hermes_cli/test_worktree_command.py
@@ -177,6 +177,29 @@ 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
+ assert not (repo / ".worktrees" / "busy").exists() # not left on disk, unlisted, until quit
+
+
+@requires_git
+def test_new_whose_shell_cannot_follow_says_the_session_moved(repo, monkeypatch):
+ """The gateway accepted the move, so the session's tools run in the tree; only this process stays
+ behind. Say that, instead of "could not enter it" for a session that did move."""
+ real_chdir = os.chdir
+
+ def chdir(path):
+ if ".worktrees" in str(path):
+ raise PermissionError("denied")
+ real_chdir(path)
+ stub, _client, conn = _gateway_stub()
+ monkeypatch.setattr(os, "chdir", chdir)
+ try:
+ out = _run(stub, "/worktree new stuck")
+ finally:
+ conn.close()
+ wt = repo / ".worktrees" / "stuck"
+ assert t("cli.session.cwd_enter_failed", path=str(wt), error="denied") in out
+ assert t("cli.commands.worktree.tools_in_worktree") in out and cli_mod._active_worktree["path"] == str(wt)
+ assert "TERMINAL_CWD" not in os.environ
@requires_git