Skip to content

The agent that grows with you

fix(state): a schema column holding bytes keeps its key in message dicts

teknium186a273156805
diff --git a/hermes_state_messages.py b/hermes_state_messages.py
index 2333a1b8740..ebd4d13b586 100644
--- a/hermes_state_messages.py
+++ b/hermes_state_messages.py
@@ -6,6 +6,7 @@ from __future__ import annotations
import hashlib
import json
import logging
+import re
import time
from typing import Any, Dict, List, Optional, Tuple
@@ -28,6 +29,12 @@ _INSERT_MESSAGE_SQL = """INSERT INTO messages (session_id, role, content, tool_c
codex_message_items, platform_message_id, observed, _compressed_summary, active, api_content, display_kind,
display_metadata, display_identity)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"""
+# Every column this module knows how to read: the ones it writes plus the three SQLite/compaction
+# owns. `_row_to_message_dict` drops raw bytes ONLY outside this set — a schema column keeps its
+# key (and its typed decoder) even when a row holds a BLOB, so no reader ever loses msg["content"].
+_MESSAGE_SCHEMA_KEYS = frozenset(
+ re.findall(r"\w+", _INSERT_MESSAGE_SQL.split("(", 1)[1].split(")", 1)[0])
+) | {"id", "compacted", "display_order"}
_BUMP_GENERATION_SQL = """
INSERT INTO conversation_generations (source, session_key, generation)
VALUES (?, ?, 1)
@@ -914,9 +921,11 @@ class SessionMessagesMixin:
msg["display_metadata"] = self._decode_display_metadata(msg["display_metadata"])
# A `SELECT *` picks up every column, including any BLOB added to the schema later; the
# JSON encoder that serves these dicts over HTTP fails outright on raw bytes. Drop them
- # here, once, rather than needing a new named pop for each future binary column.
+ # here, once, rather than needing a new named pop for each future binary column. Known
+ # columns are exempt: popping `content` because a row holds bytes turns a decode problem
+ # into a KeyError for every msg["content"] reader downstream.
for key, value in list(msg.items()):
- if isinstance(value, (bytes, bytearray)):
+ if key not in _MESSAGE_SCHEMA_KEYS and isinstance(value, (bytes, bytearray)):
msg.pop(key)
return msg
diff --git a/tests/hermes_state/test_hermes_state.py b/tests/hermes_state/test_hermes_state.py
index 10cf6057f2a..9862a73aa02 100644
--- a/tests/hermes_state/test_hermes_state.py
+++ b/tests/hermes_state/test_hermes_state.py
@@ -5900,6 +5900,18 @@ class TestUnknownBlobColumnSurvivesRead:
assert "future_blob" not in window[0]
json.dumps(window)
+ def test_schema_column_holding_bytes_keeps_its_key(self, db):
+ """The bytes pop is for columns this module does not know. A schema column such as
+ ``content`` must never vanish from the dict: every resume/compaction reader indexes
+ ``msg["content"]`` and a KeyError there is worse than the raw value it replaced."""
+ db.create_session("s1", source="cli")
+ message_id = db.append_message("s1", "user", "hello")
+ db._execute_write(lambda conn: conn.execute(
+ "UPDATE messages SET content = X'FFFE' WHERE id = ?", (message_id,)))
+ (message,) = db.get_messages("s1")
+ assert message["content"] == b"\xff\xfe"
+ assert message["role"] == "user"
+
class TestGatewayRoutingPkHeal:
"""Legacy gateway_routing tables (session_key-only PK) get rebuilt on open.