Skip to content

The agent that grows with you

Merge branch 'simp/r2-state-b' into simp/r2-state

Teknium135973b5b6c8
diff --cc hermes_state_messages.py
index 2a3f57a1f75,8c460a2c733..71e37665e33
--- a/hermes_state_messages.py
+++ b/hermes_state_messages.py
@@@ -126,15 -139,8 +139,14 @@@ class SessionMessagesMixin
def _encode_content(cls, content: Any) -> Any:
"""Serialize list/dict content (multimodal parts) as a sentinel-prefixed JSON
string; sqlite3 can only bind str/bytes/int/float/None. Lone surrogates are
- scrubbed from text so persistence never fails. Paired with :meth:`_decode_content`.
- """
+ scrubbed from text so persistence never fails. Paired with :meth:`_decode_content`."""
if isinstance(content, str):
+ # Lone UTF-16 surrogates arrive in tool results scraped from the web. The
+ # upstream sanitizer only cleans the api_messages copy and the recovery
+ # sanitizer only runs after the API call raises (it no longer does), so the
+ # canonical history keeps them and this write is where they land. Left raw,
+ # sqlite3 raises UnicodeEncodeError, the flush is abandoned, and the session
+ # silently stops persisting for the rest of its life.
return _sanitize_surrogates(content)
if content is None or isinstance(content, (bytes, int, float)):
return content
@@@ -1162,16 -994,10 +1003,13 @@@
content = self._decode_content(row["content"])
if row["role"] in {"user", "assistant"} and isinstance(content, str):
content = sanitize_context(content).strip()
- msg = {"role": row["role"], "content": content}
- # Underscore-prefixed like ``_row_id``: every transport strips it before the
- # wire, and compression's assembly copies deliberately strip it so rotated
- # child handoffs still flush (see _fresh_compaction_message_copy).
- msg[_DB_PERSISTED_MARKER_KEY] = True
++ # The persisted marker is underscore-prefixed like ``_row_id``: every transport
++ # strips it before the wire, and compression's assembly copies deliberately
++ # strip it so rotated child handoffs still flush (see _fresh_compaction_message_copy).
+ msg = {"role": row["role"], "content": content, _DB_PERSISTED_MARKER_KEY: True}
if include_row_ids and row["id"] is not None:
msg["_row_id"] = row["id"]
- for col in ("api_content", "display_kind"):
- if row[col]:
- msg[col] = row[col]
+ msg.update((col, row[col]) for col in _VERBATIM_COLS if row[col])
if row["display_metadata"]:
decoded = self._decode_display_metadata(row["display_metadata"])
if decoded is not None:
@@@ -1529,17 -1291,13 +1303,14 @@@
def message_count(self, session_id: str = None) -> int:
"""Count messages, optionally for a specific session."""
- with self._read_ctx() as conn:
- if session_id:
- cursor = conn.execute("SELECT COUNT(*) FROM messages WHERE session_id = ?", (session_id,))
- else:
- cursor = conn.execute("SELECT COUNT(*) FROM messages")
- return cursor.fetchone()[0]
+ if session_id:
+ return self._read_one("SELECT COUNT(*) FROM messages WHERE session_id = ?", (session_id,))[0]
+ return self._read_one("SELECT COUNT(*) FROM messages")[0]
def has_platform_message_id(self, session_id: str, platform_message_id: str) -> bool:
- """True when a message with *platform_message_id* exists (partial index lookup;
- the gateway's transient-failure dedupe guard)."""
+ """True when a message with *platform_message_id* exists (uses the
+ idx_messages_platform_msg_id partial index; the gateway's transient-failure
+ dedupe guard)."""
return self._read_one(
"SELECT 1 FROM messages WHERE session_id = ? AND platform_message_id = ? LIMIT 1",
(session_id, platform_message_id),