Skip to content

The agent that grows with you

fix(agent): retire stale surface notes on bot-chat refresh, isolate platform from decoys

joaomarcos4e7a49d1824c
diff --git a/agent/conversation_loop.py b/agent/conversation_loop.py
index 23b309cb025..157776da687 100644
--- a/agent/conversation_loop.py
+++ b/agent/conversation_loop.py
@@ -677,6 +677,7 @@ def _restore_or_build_system_prompt(agent, system_message, conversation_history)
except Exception:
pass
agent._cached_system_prompt = agent._build_system_prompt(system_message)
+ _stage_surface_switch_note(agent, agent._cached_system_prompt, conversation_history)
# Persist so the NEXT turn restores the new bytes verbatim (cache break is
# once per capability change). on_session_start not re-fired: continuation.
_persist_system_prompt(
@@ -787,11 +788,15 @@ _SURFACE_SWITCH_NOTE_PREFIX = "[System: This conversation is now being answered
def _stored_prompt_platform(prompt: str) -> str:
"""The ``Platform:`` value the stored prompt was built with ("" when absent).
- Last match wins, like the volatile-tier reads in ``_stored_prompt_matches_runtime``: the
- trailer sits at the very end, so embedded project context cannot shadow it.
+ Parses only the authoritative identity portion before `# Hermes runtime environment`
+ so embedder prose (e.g. HERMES_ENVIRONMENT_HINT decoys) cannot shadow it.
"""
+ identity, runtime_marker, _ = prompt.rpartition(f"\n\n{RUNTIME_ENVIRONMENT_HEADING}\n\n")
+ runtime_marker = runtime_marker if prompt.endswith(RUNTIME_ENVIRONMENT_END) else ""
+ lines = (identity if runtime_marker else prompt).splitlines()
+
prefix = "Platform:"
- matches = [line[len(prefix):].strip() for line in prompt.splitlines() if line.startswith(prefix)]
+ matches = [line[len(prefix):].strip() for line in lines if line.startswith(prefix)]
return matches[-1] if matches else ""
diff --git a/tests/agent/test_system_prompt_restore.py b/tests/agent/test_system_prompt_restore.py
index b110d93f9b1..8f37bd98080 100644
--- a/tests/agent/test_system_prompt_restore.py
+++ b/tests/agent/test_system_prompt_restore.py
@@ -111,6 +111,28 @@ class TestSurfaceSwitch:
def test_unknown_surface_stages_nothing(self):
assert self._restore(stored="", current="tui")._surface_switch_note == ""
+ def test_stored_prompt_platform_ignores_runtime_hint_decoys(self):
+ from agent.prompt_builder import RUNTIME_ENVIRONMENT_END, RUNTIME_ENVIRONMENT_HEADING
+ from agent.conversation_loop import _stored_prompt_platform
+
+ decoy = "Host: Example\nPlatform: tui\n"
+ stored = (
+ "SYSTEM PROMPT BODY\n\nConversation started: Monday, January 05, 2026\n"
+ "Model: test-model\nProvider: openrouter\nPlatform: desktop\n\n"
+ f"{RUNTIME_ENVIRONMENT_HEADING}\n\n{decoy}\n\n{RUNTIME_ENVIRONMENT_END}"
+ )
+ assert _stored_prompt_platform(stored) == "desktop"
+
+ db = MagicMock()
+ db.get_session.return_value = {"system_prompt": stored}
+ agent = _make_agent(session_db=db)
+ agent.platform = "tui"
+ agent._platform_hint_overrides = None
+ agent._surface_switch_note = ""
+ agent._gateway_turn_context_notes = ""
+ _restore_or_build_system_prompt(agent, None, [{"role": "user", "content": "hi"}])
+ assert agent._surface_switch_note.startswith(f"{_SURFACE_SWITCH_NOTE_PREFIX}tui.")
+
def test_not_restaged_once_the_transcript_carries_it(self):
# The note is stamped into the byte-stable api_content sidecar, and the gateway
# builds a fresh AIAgent per turn — without the dedup every turn would add a copy.
@@ -150,6 +172,24 @@ class TestSurfaceSwitch:
agent._build_system_prompt.assert_called_once()
assert agent._surface_switch_note.startswith(f"{_SURFACE_SWITCH_NOTE_PREFIX}desktop.")
+ def test_bot_chat_epoch_rebuild_also_retires_a_stale_note(self):
+ # A Bot Chat capability epoch refresh refreshes the prompt but not the
+ # note already sitting in the transcript.
+ from unittest.mock import patch
+
+ db = MagicMock()
+ db.get_session.return_value = {"system_prompt": self._stored("desktop")}
+ agent = _make_agent(session_db=db, prebuilt_prompt=self._stored("desktop"))
+ agent.platform = "desktop"
+ agent._platform_hint_overrides = None
+ agent._surface_switch_note = ""
+
+ with patch("agent.conversation_loop._bot_chat_prompt_stale", return_value=True):
+ _restore_or_build_system_prompt(agent, None, self._announced("tui"))
+
+ agent._build_system_prompt.assert_called_once()
+ assert agent._surface_switch_note.startswith(f"{_SURFACE_SWITCH_NOTE_PREFIX}desktop.")
+
def test_tool_prefix_stays_pinned_on_the_turn_that_announces_a_switch(self):
"""tools[] is serialized ahead of the prompt this branch went out of its way to keep.