The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(sessions): /branch and API fork end the parent only after the child exists
vominh1919d0093a11f0b1
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.pyindex 3e4aaecc893..5c6c8c5d515 100644--- a/gateway/platforms/api_server.py+++ b/gateway/platforms/api_server.py@@ -2980,11 +2980,12 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):if await asyncio.to_thread(db.get_session, fork_id):return _error_response(f"Session already exists: {fork_id}", 409, code="session_exists")- # CLI /branch semantics: end the original as branched, create a child with the transcript.- await asyncio.to_thread(db.end_session, source_id, "branched")+ # CLI /branch semantics: create the child, then end the original as branched (child first, so+ # a failed create never leaves the source ended with no fork, #11030).await asyncio.to_thread(db.create_session, fork_id, "api_server", model=source.get("model"),system_prompt=source.get("system_prompt"), parent_session_id=source_id)+ await asyncio.to_thread(db.end_session, source_id, "branched")messages = await asyncio.to_thread(db.get_messages, source_id)await asyncio.to_thread(db.replace_messages, fork_id, messages)title = body.get("title")diff --git a/hermes_cli/cli_commands_mixin.py b/hermes_cli/cli_commands_mixin.pyindex f68fa78d333..f0d3743e775 100644--- a/hermes_cli/cli_commands_mixin.py+++ b/hermes_cli/cli_commands_mixin.py@@ -1377,7 +1377,8 @@ class CLICommandsMixin:branch_title = branch_name or self._session_db.get_next_title_in_lineage(self._session_db.get_session_title(self.session_id) or "branch")parent_session_id = self.session_id- _end_current_session(self, "branched")+ # Create the child BEFORE ending the parent: a failed create_session must leave the session the+ # user is still on open, not ended with end_reason="branched" and no branch (#11030).# The stable ``_branched_from`` marker keeps the branch visible in /resume + /sessions# even after the parent is re-ended with a different end_reason.try:@@ -1388,6 +1389,7 @@ class CLICommandsMixin:"_branched_from": parent_session_id})except Exception as e:return _cp(f" Failed to create branch session: {e}")+ _end_current_session(self, "branched")# Best-effort chunked copy (a failed copy still yields a usable branch); the api_content# sidecar lets the branch's first turn replay the parent's exact wire bytes (warm cache).with suppress(Exception):diff --git a/tests/cli/test_branch_command.py b/tests/cli/test_branch_command.pyindex ad1025d9076..c8cab5e8fcd 100644--- a/tests/cli/test_branch_command.py+++ b/tests/cli/test_branch_command.py@@ -77,6 +77,20 @@ class TestBranchCommandCLI:new_session = session_db.get_session(cli_instance.session_id)assert new_session is not None+ def test_failed_branch_creation_leaves_original_session_open(self, cli_instance, session_db):+ """Branching is child-first: when create_session fails the user stays on the original+ session, so it must not be marked ended as "branched" (#11030)."""+ from unittest.mock import patch+ from cli import HermesCLI++ original = cli_instance.session_id+ with patch.object(session_db, "create_session", side_effect=RuntimeError("boom")):+ HermesCLI._handle_branch_command(cli_instance, "/branch")++ assert cli_instance.session_id == original+ row = session_db.get_session(original)+ assert row["end_reason"] is None and row["ended_at"] is None+def test_branch_copies_history(self, cli_instance, session_db):"""Branching should copy all messages to the new session."""from cli import HermesCLI