diff --git a/cli.py b/cli.py index 00937e9f9..3ae0701e8 100644 --- a/cli.py +++ b/cli.py @@ -4853,13 +4853,8 @@ class HermesCLI: # Save the current session's state before branching parent_session_id = self.session_id - # End the old session - try: - self._session_db.end_session(self.session_id, "branched") - except Exception: - pass - - # Create the new session with parent link + # Create the new session with parent link BEFORE ending the old one + # (so if creation fails, the original session remains active) try: self._session_db.create_session( session_id=new_session_id, @@ -4875,6 +4870,12 @@ class HermesCLI: _cprint(f" Failed to create branch session: {e}") return + # Only end the old session after the new one is successfully created + try: + self._session_db.end_session(self.session_id, "branched") + except Exception: + pass + # Copy conversation history to the new session for msg in self.conversation_history: try: