Skip to content

The agent that grows with you

fix(cli): refuse /branch mid-turn — an in-flight run flushes onto the branch

wuwei23e128f175c5
diff --git a/hermes_cli/cli_commands_mixin.py b/hermes_cli/cli_commands_mixin.py
index 64ff4dd823d..4de01f0d360 100644
--- a/hermes_cli/cli_commands_mixin.py
+++ b/hermes_cli/cli_commands_mixin.py
@@ -1367,6 +1367,11 @@ class CLICommandsMixin:
def _handle_branch_command(self, cmd_original: str) -> None:
"""Handle /branch [name] — fork the current session into a new independent copy of the
full history so a different approach can be explored without losing the original."""
+ # An in-flight agent run would flush through the rotating session identity: the branch
+ # ends the parent row and repoints agent.session_id (_sync_agent_to_session), so the
+ # turn's remaining messages land on the branch. Refuse mid-turn like /handoff does.
+ if getattr(self, "_agent_running", False):
+ return _cp(" Agent is busy. Wait for the current turn to finish, then retry /branch.")
from cli import _sync_process_session_id
if not self.conversation_history:
return _cp(" No conversation to branch — send a message first.")
diff --git a/tests/hermes_cli/test_branch_mid_turn_guard.py b/tests/hermes_cli/test_branch_mid_turn_guard.py
new file mode 100644
index 00000000000..e59f7652ce3
--- /dev/null
+++ b/tests/hermes_cli/test_branch_mid_turn_guard.py
@@ -0,0 +1,42 @@
+"""Regression: /branch refuses mid-turn (cross-project proposal pi-8937, upstream pi#8937).
+
+An in-flight agent run flushes through the rotating session identity: /branch ends
+the parent session row and repoints ``agent.session_id`` via ``_sync_agent_to_session``,
+so the turn's remaining messages land on the freshly created branch instead of the
+session they belong to. ``/handoff`` already refuses this race mid-turn via
+``_handoff_prepare_session``; ``/branch`` must match that behavior.
+"""
+import sys
+from types import SimpleNamespace
+
+from hermes_cli import cli_commands_mixin
+
+
+def _make_cli(agent_running: bool):
+ cli = object.__new__(cli_commands_mixin.CLICommandsMixin)
+ cli._agent_running = agent_running
+ cli.conversation_history = [{"role": "user", "content": "hello"}]
+ cli._session_db = None
+ return cli
+
+
+def test_branch_refuses_mid_turn(monkeypatch):
+ printed = []
+ monkeypatch.setattr(cli_commands_mixin, "_cp", lambda *lines: printed.extend(lines))
+ monkeypatch.setitem(sys.modules, "cli", SimpleNamespace(_sync_process_session_id=lambda *a, **k: None))
+ cli = _make_cli(agent_running=True)
+
+ cli._handle_branch_command("/branch explore")
+
+ assert any("busy" in line for line in printed), printed
+
+
+def test_branch_still_proceeds_when_idle(monkeypatch):
+ printed = []
+ monkeypatch.setattr(cli_commands_mixin, "_cp", lambda *lines: printed.extend(lines))
+ monkeypatch.setitem(sys.modules, "cli", SimpleNamespace(_sync_process_session_id=lambda *a, **k: None))
+ cli = _make_cli(agent_running=False)
+
+ cli._handle_branch_command("/branch explore")
+
+ assert printed and "busy" not in " ".join(printed), printed