Skip to content

The agent that grows with you

fix(agent): read prompt identity lines only from the trailer paragraph

kshitijk4poor03bb82f55c5a
diff --git a/agent/surface_switch.py b/agent/surface_switch.py
index 39e1f8f6bf3..1e5cd6ebe4f 100644
--- a/agent/surface_switch.py
+++ b/agent/surface_switch.py
@@ -51,11 +51,13 @@ def runtime_host_value(prompt: str, label: str) -> str:
def identity_line_value(prompt: str, label: str) -> str:
- """Last ``Label: value`` line in the identity portion (the final runtime block is embedder
- prose, never identity). Last match wins — safe only for the volatile-tier trailer fields."""
+ """``Label: value`` from the trailer paragraph that ends the identity portion (the final
+ runtime block is embedder prose, never identity). Memory, context files and plugin prose
+ come earlier and may hold ``Label:`` lines of their own; they are never read as identity,
+ even when the trailer omits the label because its value is empty."""
prefix = f"{label}:"
- matches = [line[len(prefix):].strip() for line in split_runtime_boundary(prompt)[0].splitlines()
- if line.startswith(prefix)]
+ trailer = split_runtime_boundary(prompt)[0].rsplit("\n\n", 1)[-1]
+ matches = [line[len(prefix):].strip() for line in trailer.splitlines() if line.startswith(prefix)]
return matches[-1] if matches else ""
diff --git a/tests/agent/test_system_prompt_switch_invalidation.py b/tests/agent/test_system_prompt_switch_invalidation.py
index 601af284872..ec8b1346f5a 100644
--- a/tests/agent/test_system_prompt_switch_invalidation.py
+++ b/tests/agent/test_system_prompt_switch_invalidation.py
@@ -96,7 +96,7 @@ def test_compression_tip_adoption_applies_the_identity_check(db):
assert _adopt("model-a", "prov-a")._cached_system_prompt == stale
-def _turn_agent(db, model: str, provider: str) -> MagicMock:
+def _turn_agent(db, model: str, provider: str, prose: str = "") -> MagicMock:
"""A fresh per-turn agent (the gateway shape) whose rebuild renders the real trailer."""
from agent.system_prompt import _timestamp_line
@@ -115,14 +115,14 @@ def _turn_agent(db, model: str, provider: str) -> MagicMock:
agent._surface_switch_note = ""
agent._gateway_turn_context_notes = ""
agent.enabled_toolsets = agent.disabled_toolsets = None
- agent._build_system_prompt = MagicMock(side_effect=lambda _sm: f"You are Hermes Agent.\n\n{_timestamp_line(agent)}")
+ agent._build_system_prompt = MagicMock(side_effect=lambda _sm: f"You are Hermes Agent.\n\n{prose}{_timestamp_line(agent)}")
return agent
-def _run_turn(db, model: str, provider: str) -> MagicMock:
+def _run_turn(db, model: str, provider: str, prose: str = "") -> MagicMock:
from agent.conversation_loop import _restore_or_build_system_prompt
- agent = _turn_agent(db, model, provider)
+ agent = _turn_agent(db, model, provider, prose)
_restore_or_build_system_prompt(agent, None, [{"role": "user", "content": "hi"}])
return agent
@@ -153,6 +153,27 @@ def test_emptied_live_identity_rebuilds_once_then_reuses(db, live_model, live_pr
assert second._cached_system_prompt == rebuilt
+ ("live_model", "live_provider"),
+ [("x-ai/grok-4.5", ""), ("", "nous")],
+ ids=["provider_empty", "model_empty"],
+)
+def test_identity_lines_in_memory_prose_never_count_as_the_trailer(db, live_model, live_provider):
+ """Memory/context prose sits before the trailer and may hold its own ``Provider:``/``Model:``
+ lines. With the live value empty the trailer omits that line; the prose line must not stand in
+ for it, or every turn would read a mismatch and rebuild (a prompt-cache miss per turn)."""
+ prose = "MEMORY\nProvider: openrouter\nModel: some/other-model\n\n"
+ db.create_session(SESSION_ID, source="discord", model="x-ai/grok-4.5")
+ db.update_system_prompt(SESSION_ID, _stored_prompt("x-ai/grok-4.5", "nous"))
+
+ _run_turn(db, live_model, live_provider, prose)._build_system_prompt.assert_called_once()
+ rebuilt = db.get_session(SESSION_ID)["system_prompt"]
+
+ second = _run_turn(db, live_model, live_provider, prose)
+ second._build_system_prompt.assert_not_called()
+ assert second._cached_system_prompt == rebuilt
+
+
def test_matching_live_identity_reuses_the_stored_prompt(db):
prompt = _stored_prompt("x-ai/grok-4.5", "nous")
db.create_session(SESSION_ID, source="discord", model="x-ai/grok-4.5")