Skip to content

The agent that grows with you

fix(tui): a /branch child's exit epilogue counts the branched conversation

Teknium18f7e1efa372
diff --git a/hermes_cli/main_tui_launch.py b/hermes_cli/main_tui_launch.py
index 4c02624a021..4ff32edfb56 100644
--- a/hermes_cli/main_tui_launch.py
+++ b/hermes_cli/main_tui_launch.py
@@ -54,6 +54,25 @@ def _short_resume_id(db, session_id: str) -> str:
return session_id
+def _branch_lineage(db, session: dict) -> list:
+ """*session* plus each ``/branch`` ancestor it was cut from. Main's epilogue after ``/branch`` still
+ named the parent and so reported the conversation's tokens; a canonical branch child is named
+ itself, and its copied history was billed on the ancestors."""
+ rows, seen = [session], {session.get("id")}
+ while True:
+ try:
+ config = json.loads(rows[-1].get("model_config") or "{}")
+ except (TypeError, ValueError):
+ break
+ parent_id = config.get("_branched_from") if isinstance(config, dict) else None
+ parent = db.get_session(parent_id) if parent_id and parent_id not in seen else None
+ if not parent:
+ break
+ seen.add(parent_id)
+ rows.append(parent)
+ return rows
+
+
def _print_tui_exit_summary(session_id: Optional[str], active_session_file: Optional[str] = None) -> None:
"""Print a shell-visible epilogue after TUI exits."""
from hermes_cli.main import _resolve_last_session
@@ -77,9 +96,10 @@ def _print_tui_exit_summary(session_id: Optional[str], active_session_file: Opti
message_count = int(session.get("message_count") or 0)
if message_count == 0:
return # No real conversation — don't show resume info
- tokens = {
- k: int(session.get(f"{k}_tokens") or 0)
- for k in ("input", "output", "cache_read", "cache_write", "reasoning")}
+ tokens = {k: 0 for k in ("input", "output", "cache_read", "cache_write", "reasoning")}
+ for row in _branch_lineage(db, session):
+ for k in tokens:
+ tokens[k] += int(row.get(f"{k}_tokens") or 0)
except Exception:
return
finally:
diff --git a/tests/hermes_cli/test_tui_exit_summary_short_id.py b/tests/hermes_cli/test_tui_exit_summary_short_id.py
index 71e44f8407d..1859b856d74 100644
--- a/tests/hermes_cli/test_tui_exit_summary_short_id.py
+++ b/tests/hermes_cli/test_tui_exit_summary_short_id.py
@@ -61,3 +61,23 @@ def test_stored_ids_print_unchanged(home, capsys):
_seed(home, '20261012_185208_a67483')
_print_tui_exit_summary('20261012_185208_a67483')
assert 'hermes --tui --resume 20261012_185208_a67483\n' in capsys.readouterr().out
+
+
+def test_branch_child_epilogue_counts_the_branched_conversation(home, capsys):
+ """Main's epilogue after /branch reported the conversation's tokens (it still named the parent);
+ the canonical child is named, and its total includes the branch it was cut from."""
+ import json
+ from hermes_state import SessionDB
+ from hermes_cli.main_tui_launch import _print_tui_exit_summary
+ parent = 'local-' + '1' * 64
+ child = 'local-' + '2' * 64
+ _seed(home, parent, tokens=(300, 60))
+ db = SessionDB(db_path=home / 'state.db')
+ db.create_session(child, source='tui', model_config={'_branched_from': parent}, parent_session_id=parent)
+ db.append_message(child, 'user', 'q')
+ db.append_message(child, 'assistant', 'a')
+ db.close()
+ _print_tui_exit_summary(child)
+ out = capsys.readouterr().out
+ assert f'--resume {child[:22]}' in out
+ assert 'Tokens: 360 (in 300, out 60' in out