Bug Description
/branch marks the current session as ended with end_reason="branched" before attempting to create the new branch session. If create_session() fails, the user remains on the original session, but that session is already marked closed.
Affected files / lines
cli.py:4269-4289
- related coverage gap:
tests/cli/test_branch_command.py covers happy path and early exits, but not create_session() failure during branching.
Why this is a bug
Branching is not transactional. A failure in the new-session creation path leaves session metadata inconsistent:
- active session stays the same
- original session is marked ended
- end reason says
branched even though no branch exists
This can confuse lineage/session tooling and any logic that treats ended sessions as immutable or resumable only through a different path.
Minimal reproduction
from pathlib import Path
from unittest.mock import MagicMock, patch
from cli import HermesCLI
from hermes_state import SessionDB
# create a real SessionDB and one existing session
# patch SessionDB.create_session to raise during /branch
# call HermesCLI._handle_branch_command(mock_cli, '/branch new-branch')
# inspect db.get_session(original_id)
Observed result from a narrow repro:
- CLI prints:
Failed to create branch session: boom
- original session still exists, but
end_reason == 'branched'
ended_at is set
Expected Behavior
Only mark the original session as ended after the new branch session has been created successfully (or otherwise roll back the original session state on failure).
Suggested investigation direction
Make the branch operation transactional:
- create the new session first
- copy history / finalize setup
- only then end the original session
Or explicitly roll back end_session() if branch creation fails.
Bug Description
/branchmarks the current session as ended withend_reason="branched"before attempting to create the new branch session. Ifcreate_session()fails, the user remains on the original session, but that session is already marked closed.Affected files / lines
cli.py:4269-4289tests/cli/test_branch_command.pycovers happy path and early exits, but notcreate_session()failure during branching.Why this is a bug
Branching is not transactional. A failure in the new-session creation path leaves session metadata inconsistent:
branchedeven though no branch existsThis can confuse lineage/session tooling and any logic that treats ended sessions as immutable or resumable only through a different path.
Minimal reproduction
Observed result from a narrow repro:
Failed to create branch session: boomend_reason == 'branched'ended_atis setExpected Behavior
Only mark the original session as ended after the new branch session has been created successfully (or otherwise roll back the original session state on failure).
Suggested investigation direction
Make the branch operation transactional:
Or explicitly roll back
end_session()if branch creation fails.