hermes-agent/tests/tui_gateway/test_branch_system_prompt.py
teknium1 2ec129528d fix(sessions): a /branch child row carries the parent's system prompt (CLI + TUI/Desktop)
The branch copies the parent transcript byte-for-byte so its first turn can hit the warm
prefix cache, but the child row was created without a system prompt. The child's first
build then found nothing to restore and re-probed the workspace, rewriting the prompt at
byte 0 for any repo that moved since the parent's session start — the same rewrite this PR
removes for /compress and resume, on the one session transition it did not reach. It also
logged the "stored system prompt is null; investigate update_system_prompt" WARNING for
every branch.

Both branch writers now pass the parent's prompt into create_session: the CLI prefers the
running agent's cached bytes (what this process sends) and falls back to the parent row;
_persist_branch (TUI/Desktop, seeded and lazy) reads the parent row. With the row in place
the seeding in agent/system_prompt.py replays the snapshot on the branch's later rebuilds.

Tests: one per surface, red on the PR head (child["system_prompt"] is None), green here.
2026-09-23 17:06:48 -07:00

35 lines
1.7 KiB
Python

"""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