Skip to content

The agent that grows with you

fix(sessions): a /branch child row carries the parent's system prompt (CLI + TUI/Desktop)

teknium12ec129528ddc
diff --git a/hermes_cli/cli_commands_mixin.py b/hermes_cli/cli_commands_mixin.py
index e165a56a45f..469ce0eabf7 100644
--- a/hermes_cli/cli_commands_mixin.py
+++ b/hermes_cli/cli_commands_mixin.py
@@ -1410,10 +1410,17 @@ class CLICommandsMixin:
# user is still on open, not ended with end_reason="branched" and no branch (#11030).
# The stable ``_branched_from`` marker keeps the branch visible in /resume + /sessions
# even after the parent is re-ended with a different end_reason.
+ # The child sends the parent's exact system prompt: a row without one makes the branch's first
+ # turn rebuild (re-probing the workspace), so the warm cache the copied transcript buys is
+ # lost at byte 0 whenever the repo moved since the parent's session start.
+ parent_prompt = getattr(self.agent, "_cached_system_prompt", None)
+ if not isinstance(parent_prompt, str) or not parent_prompt:
+ with suppress(Exception):
+ parent_prompt = (self._session_db.get_session(parent_session_id) or {}).get("system_prompt")
try:
self._session_db.create_session(
session_id=new_session_id, source=os.environ.get("HERMES_SESSION_SOURCE", "cli"),
- model=self.model, parent_session_id=parent_session_id,
+ model=self.model, parent_session_id=parent_session_id, system_prompt=parent_prompt or None,
model_config={"max_iterations": self.max_turns, "reasoning_config": self.reasoning_config,
"_branched_from": parent_session_id})
except Exception as e:
diff --git a/tests/hermes_cli/test_branch_command.py b/tests/hermes_cli/test_branch_command.py
index 2c1ac73a9fd..9b05fe87549 100644
--- a/tests/hermes_cli/test_branch_command.py
+++ b/tests/hermes_cli/test_branch_command.py
@@ -165,6 +165,24 @@ class TestBranchFlushesBeforeEndSession:
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"}
diff --git a/tests/tui_gateway/test_branch_system_prompt.py b/tests/tui_gateway/test_branch_system_prompt.py
new file mode 100644
index 00000000000..117ac35044e
--- /dev/null
+++ b/tests/tui_gateway/test_branch_system_prompt.py
@@ -0,0 +1,35 @@
+"""A TUI/Desktop branch child sends the bytes its parent already sends.
+
+Without a stored prompt the child's first turn rebuilds (a fresh workspace probe), so the warm
+cache the copied transcript buys is lost at byte 0 whenever the repo moved since session start.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+
+def test_persist_branch_copies_the_parent_system_prompt(tmp_path, monkeypatch):
+ from hermes_state import SessionDB
+ from tui_gateway import server
+
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("HERMES_HOME", str(home))
+
+ with SessionDB(home / "state.db") as db:
+ db.create_session("parent", source="desktop", model="test-model")
+ db.update_system_prompt("parent", "PARENT PROMPT\n\nWorkspace snapshot: session start")
+ server._persist_branch(db, "child", "parent", "Branch", [{"role": "user", "content": "hello"}],
+ source="desktop", cwd=str(tmp_path), profile_name="default",
+ model="test-model")
+ # A parent with no stored prompt yields a child with none — never a phantom empty string.
+ db.create_session("bare", source="desktop", model="test-model")
+ server._persist_branch(db, "bare-child", "bare", "Branch 2", [{"role": "user", "content": "hi"}],
+ source="desktop", cwd=str(tmp_path), profile_name="default",
+ model="test-model")
+
+ with SessionDB(home / "state.db") as db:
+ assert db.get_session("child")["system_prompt"] == "PARENT PROMPT\n\nWorkspace snapshot: session start"
+ assert db.get_session("bare-child")["system_prompt"] is None
diff --git a/tui_gateway/methods_session.py b/tui_gateway/methods_session.py
index 7c77411209c..05d03d305c1 100644
--- a/tui_gateway/methods_session.py
+++ b/tui_gateway/methods_session.py
@@ -231,8 +231,16 @@ def _persist_branch(db, new_key: str, parent_key: str, title: str, history: list
deletes a committed row whose transcript/title failed (a durable-but-empty row would defeat the INSERT OR
IGNORE first-prompt seed) — except on disk-full, where the delete cannot land. ``user_id`` is the creating
login: the child is a Desktop session too, and the row only records identity at insert."""
+ # The child sends the parent's exact system prompt: a row without one makes the branch's first
+ # turn rebuild (re-probing the workspace) and forfeits the warm cache the copied transcript buys.
+ parent_prompt = None
+ try:
+ parent_prompt = (db.get_session(parent_key) or {}).get("system_prompt")
+ except Exception:
+ logger.debug("branch: parent system prompt read failed for %s", parent_key, exc_info=True)
db.create_session(new_key, source=source, model=model, model_config={"_branched_from": parent_key},
- parent_session_id=parent_key, cwd=cwd, profile_name=profile_name, user_id=user_id)
+ parent_session_id=parent_key, cwd=cwd, profile_name=profile_name, user_id=user_id,
+ system_prompt=parent_prompt or None)
try:
# Compensation guard (#93959 review): if the transcript copy or title write fails AFTER the row
# committed, the durable-but-empty row would defeat the lazy first-prompt fallback