Files
Teknium 57c9569047 fix(cli): the session title stays on the status bar after the chat compresses
The classic CLI talks to the gateway and keeps the conversation's creation id
for its whole life. Each compression moves the live segment, and the title with
it, to a new row, so after the first compression the status bar, the glyph
layout's top rule and /title all read an untitled row: the name disappeared and
/title said "no title". A /title set afterwards wrote that stale row, so the
name never came back.

- The CLI reads and writes the title on the conversation's compression tip
  (_title_session_id), for the status bar, /title, /branch's default name and
  the exit summary.
- The gateway store's rename sends the named row's own revision when that row
  is a later compression segment. The resume snapshot carries the owner's
  revision, so a tip rename was a revision_conflict every time.
- A manual /compress that rotates moves the conversation's name onto the new
  segment, as the automatic rotation already does (_title_follows_head).
2026-11-16 12:19:49 -08:00

256 lines
10 KiB
Python

"""Tests for the /branch (/fork) command — session branching.
Verifies that:
- Branching creates a new session with copied conversation history
- The original session is preserved (ended with "branched" reason)
- Auto-generated titles use lineage numbering
- Custom branch names are used when provided
- parent_session_id links are set correctly
- Structured reasoning fields survive the copy
- Edge cases: empty conversation, missing session DB
"""
import os
from datetime import datetime
from unittest.mock import MagicMock
import pytest
@pytest.fixture
def session_db(tmp_path):
"""Create a real SessionDB for testing."""
os.environ["HERMES_HOME"] = str(tmp_path / ".hermes")
os.makedirs(tmp_path / ".hermes", exist_ok=True)
from hermes_state import SessionDB
db = SessionDB(db_path=tmp_path / ".hermes" / "test_sessions.db")
yield db
db.close()
@pytest.fixture
def cli_instance(tmp_path, session_db):
"""Create a minimal HermesCLI-like object for testing _handle_branch_command."""
# We'll mock the CLI enough to test the branch logic without full init
from unittest.mock import MagicMock
cli = MagicMock()
cli._agent_running = False # a bare MagicMock attribute is truthy and would trip the mid-turn guard
from hermes_cli.cli_init_mixin import CLIInitMixin
# The id seam: a plain SessionDB (no gateway facade) keeps main's local mint.
cli._new_session_id = lambda now, **kw: CLIInitMixin._new_session_id(cli, now, **kw)
from hermes_cli.cli_session_mixin import CLISessionMixin
cli._title_session_id = lambda: CLISessionMixin._title_session_id(cli)
cli._session_db = session_db
cli.session_id = "20260403_120000_abc123"
cli.model = "anthropic/claude-sonnet-4.6"
cli.max_turns = 90
cli.reasoning_config = {"enabled": True, "effort": "medium"}
cli.session_start = datetime.now()
cli._pending_title = None
cli._resumed = False
cli.agent = None
cli.conversation_history = [
{"role": "user", "content": "Hello, can you help me?"},
{"role": "assistant", "content": "Of course! How can I help?"},
{"role": "user", "content": "Write a Python function to sort a list."},
{"role": "assistant", "content": "def sort_list(lst): return sorted(lst)"},
]
# Create the original session in the DB
session_db.create_session(
session_id=cli.session_id,
source="cli",
model=cli.model,
)
session_db.set_session_title(cli.session_id, "My Coding Session")
return cli
class TestBranchCommandCLI:
"""Test the /branch command logic for the CLI."""
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_gateway_refusal_is_reported_and_leaves_the_session(self, cli_instance, session_db, capsys):
"""A gateway /branch step that fails (e.g. the child's YOLO could not be set, CLI-20) prints
main's branch error and keeps the user on the original session, never a traceback."""
from cli import HermesCLI
from hermes_cli.gateway_client import GatewayClientError
def refused(now, **kw):
raise GatewayClientError("Gateway did not answer config.set")
cli_instance._new_session_id = refused
original = cli_instance.session_id
HermesCLI._handle_branch_command(cli_instance, "/branch")
assert cli_instance.session_id == original
assert "Gateway did not answer config.set" in capsys.readouterr().out
assert session_db.get_session(original)["end_reason"] 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
HermesCLI._handle_branch_command(cli_instance, "/branch")
messages = session_db.get_messages_as_conversation(cli_instance.session_id)
assert len(messages) == 4 # All 4 messages copied
def test_branch_with_custom_name(self, cli_instance, session_db):
"""Custom branch name should be used as the title."""
from cli import HermesCLI
HermesCLI._handle_branch_command(cli_instance, "/branch refactor approach")
title = session_db.get_session_title(cli_instance.session_id)
assert title == "refactor approach"
def test_branch_no_session_db(self, cli_instance):
"""Branching without a session DB should show an error."""
from cli import HermesCLI
cli_instance._session_db = None
HermesCLI._handle_branch_command(cli_instance, "/branch")
# session_id should not have changed
assert cli_instance.session_id == "20260403_120000_abc123"
def test_branch_fires_on_session_switch_hook(self, cli_instance, session_db):
"""The /branch command must notify memory providers of the rotation.
Without this, providers that cache per-session state in
initialize() keep writing under the old session_id. See #6672.
"""
from cli import HermesCLI
# Wire a real-ish agent object with a MagicMock memory_manager
agent = MagicMock()
mm = MagicMock()
agent._memory_manager = mm
cli_instance.agent = agent
original_id = cli_instance.session_id
HermesCLI._handle_branch_command(cli_instance, "/branch")
# Hook must have been called exactly once with the new session_id,
# parent pointing at the branched-from session, reset=False, and
# reason="branch" for diagnostics.
assert mm.on_session_switch.call_count == 1
_, kwargs = mm.on_session_switch.call_args
assert mm.on_session_switch.call_args.args[0] == cli_instance.session_id
assert kwargs["parent_session_id"] == original_id
assert kwargs["reset"] is False
assert kwargs["reason"] == "branch"
class TestBranchFlushesBeforeEndSession:
"""Regression for #47202: /branch must flush un-persisted messages to
the session DB before ending the old session, just like /new and
compress_context() already do."""
def test_branch_flushes_when_agent_present(self, cli_instance, session_db):
from cli import HermesCLI
agent = MagicMock()
cli_instance.agent = agent
HermesCLI._handle_branch_command(cli_instance, "/branch")
agent._flush_messages_to_session_db.assert_called_once_with(
cli_instance.conversation_history,
conversation_history=cli_instance.conversation_history,
)
def test_branch_child_row_carries_the_parent_system_prompt(self, cli_instance, session_db):
"""The branch's first turn must send the bytes the parent already sends: with no stored prompt the
child rebuilds (a fresh workspace probe) and the copied transcript's warm cache is lost at byte 0."""
from cli import HermesCLI
parent_id = cli_instance.session_id
session_db.update_system_prompt(parent_id, "PARENT PROMPT\n\nWorkspace snapshot: session start")
# No live agent: the parent's persisted row is the source.
HermesCLI._handle_branch_command(cli_instance, "/branch")
child = session_db.get_session(cli_instance.session_id)
assert child["parent_session_id"] == parent_id
assert child["system_prompt"] == "PARENT PROMPT\n\nWorkspace snapshot: session start"
# A live agent's cached prompt (what THIS process sends) wins over the row.
cli_instance.agent = MagicMock(_cached_system_prompt="LIVE PROMPT")
HermesCLI._handle_branch_command(cli_instance, "/branch")
assert session_db.get_session(cli_instance.session_id)["system_prompt"] == "LIVE PROMPT"
REASONING_DETAILS = [
{"type": "reasoning.text", "text": "sort in place instead", "format": "unknown"}
]
CODEX_REASONING_ITEMS = [
{"id": "rs_1", "type": "reasoning", "encrypted_content": "opaque-blob"}
]
CODEX_MESSAGE_ITEMS = [
{
"id": "msg_1",
"type": "message",
"role": "assistant",
"content": [{"type": "output_text", "text": "done"}],
}
]
class TestBranchPreservesReasoningFields:
"""The copy loop must carry the structured reasoning columns across.
Only ``reasoning`` was forwarded to append_message, so a branch dropped
the preserved Anthropic thinking blocks and the Codex
encrypted-reasoning/message-item continuation state that the parent had
accumulated — the branch then replayed without them, because every
consumer gates on isinstance(..., list) and a missing field reads as None.
"""
def test_reasoning_fields_survive_branch(self, cli_instance, session_db):
from cli import HermesCLI
cli_instance.conversation_history = [
{"role": "user", "content": "Sort this list."},
{
"role": "assistant",
"content": "def sort_list(lst): return sorted(lst)",
"reasoning": "picked sorted()",
"reasoning_details": REASONING_DETAILS,
"codex_reasoning_items": CODEX_REASONING_ITEMS,
"codex_message_items": CODEX_MESSAGE_ITEMS,
},
]
HermesCLI._handle_branch_command(cli_instance, "/branch")
messages = session_db.get_messages_as_conversation(cli_instance.session_id)
assistant = next(m for m in messages if m["role"] == "assistant")
assert assistant["reasoning_details"] == REASONING_DETAILS
assert assistant["codex_reasoning_items"] == CODEX_REASONING_ITEMS
assert assistant["codex_message_items"] == CODEX_MESSAGE_ITEMS