Skip to content

[Bug]: /branch can end the original session even when creating the branch session fails #11030

@NewTurn2017

Description

@NewTurn2017

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:

  1. create the new session first
  2. copy history / finalize setup
  3. only then end the original session

Or explicitly roll back end_session() if branch creation fails.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existscomp/cliCLI entry point, hermes_cli/, setup wizardtype/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions