Skip to content

The agent that grows with you

fix(web): hide untyped gateway scaffold notices from REST session transcripts

Abdulrahman Jahfalib287651e2d20
diff --git a/hermes_cli/web_routers/sessions.py b/hermes_cli/web_routers/sessions.py
index 64cd049f456..d35489735c5 100644
--- a/hermes_cli/web_routers/sessions.py
+++ b/hermes_cli/web_routers/sessions.py
@@ -588,7 +588,32 @@ def _session_files_dir(profile) -> Path:
return _history_profile_home(profile) / "sessions"
+def _is_untyped_scaffold_notice(message) -> bool:
+ """A ``[System: …]`` role=user row persisted without a ``display_kind``.
+
+ ``[System:`` is a reserved gateway-notice namespace — it must never render as a user
+ bubble (the gateway's own history projection drops these rows outright) — but recovery
+ scaffolding written before typing existed carries no kind. Rows WITH a kind
+ (``model_switch``, …) are timeline entries and keep flowing.
+ """
+ if not isinstance(message, dict) or message.get("role") != "user" or message.get("display_kind"):
+ return False
+ content = message.get("content")
+ return isinstance(content, str) and content.lstrip().startswith("[System:")
+
+
def _project_for_display(messages: list, *, home=None, inline_images: bool = True) -> list:
+ """Replace compaction summaries with their display-only projection and hide untyped
+ gateway-scaffold notices.
+
+ Recovery scaffolding (e.g. the stream-timeout nudge appended when a tool call's stream
+ is cut) persists as a ``[System: …]`` ``role=user`` row with no ``display_kind``. This
+ projection feeds the Desktop's transcript prefetch, which addresses VISIBLE user rows by
+ durable row id — and the gateway truncation resolver refuses scaffold rows fail-closed,
+ so a shipped scaffold row can never resolve as a rewind/regenerate target and dead-ends
+ every retry (``refusing truncation without fallback``). Hide them the same way the
+ Desktop collapses other display-only rows; typed notices stay for the timeline.
+ """
from agent.compaction_display import project_compaction_message_for_display
from agent.context_compressor import is_compaction_summary_message
from agent.conversation_compression import _extract_steer_text_from_message
@@ -618,6 +643,11 @@ def _project_for_display(messages: list, *, home=None, inline_images: bool = Tru
message.get("role"), message.get("content"))
if failed_turn:
message = {**message, "display_kind": failed_turn}
+ if _is_untyped_scaffold_notice(message):
+ projected = message.copy()
+ projected["display_kind"] = "hidden"
+ projected_messages.append(projected)
+ continue
# Mid-turn steer: the user's own words, not the model-facing marker (same as session.resume).
if message.get("role") == "user" and message.get("display_kind") == STEER_DISPLAY_KIND and (
steer_text := _extract_steer_text_from_message(message)):
diff --git a/tests/hermes_cli/test_sessions_rest_scaffold_notices.py b/tests/hermes_cli/test_sessions_rest_scaffold_notices.py
new file mode 100644
index 00000000000..122b7d6ffc6
--- /dev/null
+++ b/tests/hermes_cli/test_sessions_rest_scaffold_notices.py
@@ -0,0 +1,70 @@
+"""REST session transcripts must not ship untyped gateway scaffold notices as user rows.
+
+A ``[System: …]`` ``role=user`` row persisted without a ``display_kind`` is model-facing
+recovery scaffolding (e.g. the stream-timeout nudge appended by the turn-truncation path).
+The gateway's own history projection (``tui_gateway.session_history._history_to_messages``)
+drops these rows, but this REST projection feeds the Desktop's transcript prefetch, which
+addresses VISIBLE user rows by durable row id — and a scaffold row can never resolve as a
+rewind/regenerate target (the gateway truncation resolver refuses it fail-closed), so
+shipping it as a user row dead-ends every retry. Typed notices (``model_switch``, …) must
+keep flowing: the Desktop renders them as timeline rows.
+"""
+
+from hermes_cli.web_routers.sessions import _project_for_display
+
+
+def test_rest_projection_hides_untyped_gateway_notices_and_keeps_typed_ones():
+ messages = [
+ {"role": "user", "id": 1, "content": "hello", "timestamp": 1.0},
+ {
+ "role": "user",
+ "id": 2,
+ "content": (
+ "[System: Your previous tool call (terminal) was too large and the stream "
+ "timed out before it could be delivered. Do NOT retry the same tool call "
+ "with the same large content.]"
+ ),
+ "timestamp": 2.0,
+ },
+ {
+ "role": "user",
+ "id": 3,
+ "content": "[System: The active model for this chat has changed to example.]",
+ "display_kind": "model_switch",
+ "timestamp": 3.0,
+ },
+ {"role": "assistant", "id": 4, "content": "ok", "timestamp": 4.0},
+ ]
+
+ projected = _project_for_display(messages)
+ by_id = {message["id"]: message for message in projected}
+
+ # No row is dropped: offsets/counts stay stable for pagination and "Show earlier".
+ assert len(projected) == len(messages)
+ # The untyped scaffold notice is hidden (the Desktop collapses `hidden` rows).
+ assert by_id[2]["display_kind"] == "hidden"
+ # A human user row and a typed timeline notice remain untouched.
+ assert by_id[1].get("display_kind") is None
+ assert by_id[3].get("display_kind") == "model_switch"
+ # The payload itself is preserved — only the display kind is added.
+ assert by_id[2]["content"] == messages[1]["content"]
+ # Input rows are not mutated in place.
+ assert "display_kind" not in messages[1]
+
+
+def test_rest_projection_still_projects_compaction_summaries():
+ from agent.context_compressor import COMPRESSED_SUMMARY_METADATA_KEY
+
+ summary = {
+ "role": "user",
+ "id": 10,
+ "content": "[CONTEXT COMPACTION — REFERENCE ONLY] earlier turns were compacted",
+ "timestamp": 5.0,
+ COMPRESSED_SUMMARY_METADATA_KEY: True,
+ }
+
+ projected = _project_for_display([summary])
+
+ assert len(projected) == 1
+ # A pure handoff compacts to `hidden` — the pre-existing behavior this edit sits in.
+ assert projected[0]["display_kind"] == "hidden"