hermes_state_prune_rewrite.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. """In-place row-rewrite persistence for the proactive prune (#124102).
  2. Split out of :mod:`hermes_state_messages` (FILE_LINES ratchet): the prune's own commit primitive and
  3. its lease-less staleness probe. Both are ``SessionMessagesMixin`` methods; this sibling defines a
  4. ``SessionPruneRewriteMixin`` the facade binds into the same MRO slot, so every existing
  5. ``session_db.rewrite_pruned_rows(...)`` / ``rewritten_held_row_ids(...)`` call site and test patch
  6. seam keeps working unchanged.
  7. """
  8. from __future__ import annotations
  9. from typing import Any, Optional
  10. from agent.context_compressor import _DB_PERSISTED_MARKER as _DB_PERSISTED_MARKER_KEY
  11. from agent.message_metadata import DB_ROW_SNAPSHOT
  12. from hermes_state_common import _id_chunks, _placeholders
  13. from hermes_state_messages import _INSERT_MESSAGE_SQL, _parse_tool_calls
  14. # _INSERT_MESSAGE_SQL's columns in bind order, so a row rewrite can diff _message_row_params by name.
  15. _MESSAGE_ROW_COLUMNS = tuple(
  16. column.strip() for column in _INSERT_MESSAGE_SQL.split("(", 1)[1].split(")", 1)[0].split(","))
  17. # A prune only changes what a message says; who said it, when, and in which row never move.
  18. _PRUNE_FIXED_COLUMNS = frozenset({"session_id", "role", "tool_call_id", "timestamp", "active", "display_identity"})
  19. class SessionPruneRewriteMixin:
  20. """``SessionDB`` mixin: the proactive prune's in-place row rewrite and held-row staleness probe."""
  21. def rewritten_held_row_ids(self, session_id: str, held: list[dict[str, Any]]) -> list[int]:
  22. """Ids among *held* message dicts whose ACTIVE row no longer holds the payload the dict holds.
  23. An in-place commit (the proactive prune's ``rewrite_pruned_rows``) changes what a row says and
  24. keeps its id and its ``active`` flag, so an id/activity staleness check cannot see it. A lease-less
  25. writer holding the pre-rewrite bodies must not publish them: that archives rows it never compared
  26. and republishes the stale text over the newer generation (#124102).
  27. The comparison is representation-safe, never raw storage against a loaded view. A dict's content
  28. passed through the loader lens (``_loaded_view_content`` sanitizes and strips user/assistant
  29. strings), so the stored bytes are compared through the same lens; when the dict carries the
  30. stored-row CAS digest (``DB_ROW_SNAPSHOT``, see ``agent.transcript_repair``) that wins instead,
  31. covering every owned column including assistant ``tool_calls`` arguments. Absent or inactive
  32. rows are NOT reported — that is the activity check's job and it has its own fallback rules.
  33. Every model-transcript role is compared (user/assistant/tool): the callers gate the FULL
  34. writer (``archive_and_compact`` via the prune fallback and micro-compaction), which re-inserts
  35. held rows of those roles verbatim — a concurrently rewritten user row (``set_user_message_content``,
  36. the repair batch's in-place rewrite) is as much a lost race as a rewritten tool result (#124102).
  37. Bookkeeping roles (``session_meta``/``system``) never reach the model-facing list and stay excluded.
  38. """
  39. return self._read_retrying_ioerr(
  40. lambda conn: self.held_row_mismatches(conn, session_id, held))
  41. def held_row_mismatches(self, conn, session_id: str, held: list[dict[str, Any]]) -> list[int]:
  42. """``rewritten_held_row_ids`` against *conn*: the same CAS compare, on a connection the
  43. caller already holds (inside a ``BEGIN IMMEDIATE``, the full writer's commit window — a
  44. precheck outside that lock misses a write that lands between check and commit, and the
  45. full writer would then archive that newer row and republish the stale held text, #124102).
  46. Chunked like ``stamp_inserted_row_snapshots``: one ``id IN (...)`` per ``_id_chunks`` batch,
  47. so a long history stays under SQLite's host-parameter limit instead of failing the pass.
  48. Rows without an exact ``_row_id`` but with both the persist marker and a CAS snapshot are
  49. resolved by their logical ``message_uid`` (``idx_messages_session_uid``): the unaddressed
  50. live-replay loaders stamp exactly that pair, and a rewritten-in-place row is as lost a race
  51. there as a row-addressed one (#124102).
  52. """
  53. from agent.message_metadata import message_uid_or_none
  54. from agent.transcript_repair import transcript_row_snapshot
  55. def _exact_id(message: dict[str, Any]) -> Optional[int]:
  56. rid = message.get("_row_id")
  57. if isinstance(rid, int) and not isinstance(rid, bool) and rid > 0:
  58. return rid
  59. return None
  60. exact = [
  61. (message, _exact_id(message))
  62. for message in held
  63. if isinstance(message, dict) and message.get(_DB_PERSISTED_MARKER_KEY)
  64. and message.get("role") in ("user", "assistant", "tool")]
  65. by_id = {row_id: message for message, row_id in exact if row_id is not None}
  66. # The unaddressed replay shape: snapshot + marker, no _row_id. Only rows the caller can
  67. # name with durable identity join the fence; anything else keeps the loader-lens behavior
  68. # of the activity check (absence of evidence is not permission to publish, but a dict the
  69. # store cannot name at all has always belonged to the caller's fallback rules).
  70. by_uid = {}
  71. for message, row_id in exact:
  72. if row_id is None and isinstance(message.get(DB_ROW_SNAPSHOT), str):
  73. uid = message_uid_or_none(message)
  74. if uid is not None:
  75. by_uid[uid] = message
  76. if not session_id or not (by_id or by_uid):
  77. return []
  78. stored: dict[int, Any] = {}
  79. for chunk in _id_chunks(by_id):
  80. for row in conn.execute(
  81. f"SELECT * FROM messages WHERE session_id = ? AND id IN ({_placeholders(chunk)})",
  82. (session_id, *chunk)).fetchall():
  83. stored[int(row["id"])] = row
  84. by_uid_row: dict[str, Any] = {}
  85. for uid, message in by_uid.items():
  86. # Newest active row, same rule as the loader's _active_logical_message_row: a logical
  87. # message_uid can have several active physical rows (a carried or concurrent compaction
  88. # cloning one back beside its archived twin), and the loader stamps the held dict from
  89. # the newest. A bare fetchone() could return an older copy whose payload still matches
  90. # the held digest — the newest row was rewritten by another writer, and the lease-less
  91. # fallback would republish the stale held payload over that newer generation (#124102).
  92. row = conn.execute(
  93. "SELECT * FROM messages WHERE session_id = ? AND active = 1 AND role = ? AND message_uid = ? "
  94. "ORDER BY id DESC LIMIT 1",
  95. (session_id, message.get("role"), uid)).fetchone()
  96. if row is not None:
  97. by_uid_row[uid] = row
  98. stored[int(row["id"])] = row
  99. mismatches: list[int] = []
  100. for message, row_id in exact:
  101. row = stored.get(row_id) if row_id is not None else by_uid_row.get(message_uid_or_none(message) or "")
  102. if row is None or not int(row["active"] or 0):
  103. continue # the activity staleness check owns absent/inactive rows
  104. snapshot = message.get(DB_ROW_SNAPSHOT)
  105. if isinstance(snapshot, str):
  106. if transcript_row_snapshot(row) != snapshot:
  107. mismatches.append(int(row["id"]))
  108. continue
  109. held_identity = self._row_identity(
  110. message.get("role", "unknown"), message.get("content"), message.get("tool_call_id"),
  111. _parse_tool_calls(message.get("tool_calls")))
  112. stored_identity = self._row_identity(
  113. row["role"], self._decode_content(row["content"]), row["tool_call_id"],
  114. _parse_tool_calls(row["tool_calls"]))
  115. if held_identity != stored_identity:
  116. mismatches.append(int(row["id"]))
  117. return mismatches
  118. def rewrite_pruned_rows(self, session_id: str, changes: list[tuple[dict[str, Any], dict[str, Any]]], *,
  119. model_config_patch: Optional[dict[str, Any]] = None, turn_lease_holder: Optional[str] = None,
  120. turn_lease_ttl_seconds: float = 300.0) -> int:
  121. """Proactive-prune commit that rewrites only the changed rows, in place (#124102).
  122. ``archive_and_compact`` is a generation writer: it archives every active row and re-inserts the whole
  123. list, which is right for summary compaction (the row set changes) but makes each prune grow the store
  124. by the full live transcript although a prune only edits a few rows' content. Each
  125. ``(original, replacement)`` in *changes* is a persisted message the prune edited. Its row is named by
  126. ``original["_row_id"]``, else by its tool call id (tool results) or tool-call ids (assistant turns),
  127. else by the single active row with the same role and stored content. That row gets one archived twin
  128. of its pre-prune self (``active=0, compacted=1``: still searchable, like the originals
  129. ``archive_and_compact`` keeps), then takes the replacement's changed columns under its own id.
  130. Unchanged rows, model order, row ids and concurrently appended rows are untouched; the FTS and
  131. display-identity triggers keep both indexes consistent; the counters are unchanged because no row
  132. enters or leaves the live set.
  133. Raises :class:`PruneRowUnresolvedError` before writing anything when a row cannot be named exactly (a
  134. merged dict, zero or several matches, an inactive row or a user row), so the caller can commit
  135. through ``archive_and_compact``. A named row whose stored payload moved on raises the
  136. :class:`PruneRowStaleError` subclass instead: that is another writer's generation, not ambiguity,
  137. and the full writer must not be handed the stale transcript. The staleness check is the repo's
  138. row-version CAS (``DB_ROW_SNAPSHOT`` / ``transcript_row_snapshot``, see ``agent/transcript_repair``)
  139. when the held dict carries the digest — it covers every owned column including assistant
  140. ``tool_calls`` arguments — and the representation-safe loader-lens identity compare otherwise
  141. (raw storage bytes are never compared against a loaded/normalized view; #124102).
  142. ``turn_lease_holder`` (a writer inside a turn) refuses with :class:`SessionTurnLeaseLostError`, before
  143. writing anything, when another process owns the session's turn lease, as ``append_message`` does.
  144. Returns the rows rewritten. Each rewritten dict is stamped with its row's new digest, so a second
  145. prune of the same transcript compares against what this commit wrote, not what it replaced.
  146. """
  147. from agent.conversation_compression_archive import ABSORBED_ROW_IDS
  148. from agent.message_metadata import DB_ROW_SNAPSHOT
  149. from agent.transcript_repair import transcript_row_snapshot
  150. from hermes_state_errors import PruneRowStaleError, PruneRowUnresolvedError
  151. def _call_ids(tool_calls: Any) -> list[Any]:
  152. return [call.get("id") if isinstance(call, dict) else None for call in (_parse_tool_calls(tool_calls) or [])]
  153. def _resolve(conn, original: dict[str, Any]) -> Any:
  154. role = original.get("role")
  155. if role not in ("assistant", "tool") or original.get(ABSORBED_ROW_IDS):
  156. raise PruneRowUnresolvedError(f"a {role} message is not rewritten in place")
  157. row_id = original.get("_row_id")
  158. if isinstance(row_id, int) and not isinstance(row_id, bool) and row_id > 0:
  159. ids = [row_id]
  160. elif role == "tool" and original.get("tool_call_id"):
  161. ids = [int(row["id"]) for row in conn.execute(
  162. "SELECT id FROM messages WHERE session_id = ? AND active = 1 AND role = 'tool' AND tool_call_id = ?",
  163. (session_id, original["tool_call_id"])).fetchall()]
  164. elif role == "assistant" and _call_ids(original.get("tool_calls")):
  165. wanted = _call_ids(original.get("tool_calls"))
  166. ids = [int(row["id"]) for row in conn.execute(
  167. "SELECT id, tool_calls FROM messages WHERE session_id = ? AND active = 1 AND role = 'assistant' "
  168. "AND tool_calls IS NOT NULL", (session_id,)).fetchall() if _call_ids(row["tool_calls"]) == wanted]
  169. else:
  170. ids = self._matching_active_ids(conn, session_id, original)
  171. if len(ids) != 1:
  172. raise PruneRowUnresolvedError(f"{len(ids)} live rows match a pruned {role} message")
  173. row = conn.execute(
  174. "SELECT * FROM messages WHERE id = ? AND session_id = ? AND active = 1", (ids[0], session_id)).fetchone()
  175. if row is None or row["role"] != role:
  176. raise PruneRowStaleError(f"row {ids[0]} no longer holds the pruned {role} message")
  177. snapshot = original.get(DB_ROW_SNAPSHOT)
  178. if isinstance(snapshot, str):
  179. # The stored-row CAS digest: covers every owned column (tool_calls arguments included), and
  180. # is computed from the read-back row, so storage affinity can never fake a mismatch.
  181. if transcript_row_snapshot(row) != snapshot:
  182. raise PruneRowStaleError(f"row {ids[0]} no longer holds the pruned {role} message")
  183. elif (self._row_identity(role, original.get("content"), original.get("tool_call_id"),
  184. _parse_tool_calls(original.get("tool_calls")))
  185. != self._row_identity(row["role"], self._decode_content(row["content"]),
  186. row["tool_call_id"], _parse_tool_calls(row["tool_calls"]))):
  187. # No digest (a resumed dict): compare through the loader lens instead, the same
  188. # representation both sides of _stamp_kept_live_prefix use.
  189. raise PruneRowStaleError(f"row {ids[0]} no longer holds the pruned {role} message")
  190. return row
  191. def _do(conn):
  192. if turn_lease_holder:
  193. # A writer inside a turn commits only while it still owns the turn, like its appends.
  194. self._check_transcript_write_guards(conn, session_id, None, turn_lease_holder=turn_lease_holder,
  195. turn_lease_ttl_seconds=turn_lease_ttl_seconds)
  196. patch = model_config_patch is not None
  197. patched_model_config = self._merge_model_config_json(
  198. conn, session_id, model_config_patch, on_missing="raise") if patch else None
  199. updates: list[tuple[int, list[str], list[Any], Any, Any]] = []
  200. for original, replacement in changes:
  201. row = _resolve(conn, original)
  202. role, timestamp = row["role"], row["timestamp"]
  203. if replacement.get("role") != role:
  204. raise PruneRowUnresolvedError("a prune replacement changed the message role")
  205. before = self._message_row_params(session_id, role, original, _parse_tool_calls(
  206. original.get("tool_calls")), timestamp, keep_reasoning=role == "assistant")
  207. after = self._message_row_params(session_id, role, replacement, _parse_tool_calls(
  208. replacement.get("tool_calls")), timestamp, keep_reasoning=role == "assistant")
  209. changed = [column for column, old, new in zip(_MESSAGE_ROW_COLUMNS, before, after)
  210. if old != new and column not in _PRUNE_FIXED_COLUMNS]
  211. if changed:
  212. updates.append((int(row["id"]), changed, [
  213. value for column, value in zip(_MESSAGE_ROW_COLUMNS, after) if column in changed],
  214. row["display_identity"],
  215. int(row["display_order"]) if row["display_order"] is not None else int(row["id"]),
  216. replacement))
  217. twin_columns = ", ".join(c for c in self._message_column_names(conn)
  218. if c not in ("id", "active", "compacted", "display_order"))
  219. for row_id, columns, values, identity, order, replacement in updates:
  220. twin_id = conn.execute(f"INSERT INTO messages ({twin_columns}, active, compacted) "
  221. f"SELECT {twin_columns}, 0, 1 FROM messages WHERE id = ?", (row_id,)).lastrowid
  222. # The content UPDATE fires the identity trigger, which nulls the slot of the live row and of
  223. # every visible row sharing its old identity: the twin and any earlier generation's copies.
  224. peers = conn.execute(
  225. "SELECT id, display_order FROM messages WHERE session_id = ? AND id <> ? "
  226. "AND display_identity = ? AND (active = 1 OR compacted = 1)",
  227. (session_id, row_id, identity)).fetchall() if identity is not None else [(twin_id, order)]
  228. conn.execute(f"UPDATE messages SET {', '.join(f'{column} = ?' for column in columns)} WHERE id = ?",
  229. [*values, row_id])
  230. # The superseded bodies keep their identity and slot. The live row takes the identity of what it
  231. # now stores, so a later compaction's copy of it joins the same slot, and keeps the slot.
  232. # The page shows one row per display_order, preferring the live one: the stub, in place.
  233. conn.executemany("UPDATE messages SET display_identity = ?, display_order = ? WHERE id = ?",
  234. [(identity, order if peer_order is None else peer_order, peer_id)
  235. for peer_id, peer_order in peers])
  236. live = conn.execute(
  237. "SELECT role, content, timestamp, tool_call_id, tool_calls, tool_name, display_kind, "
  238. "display_metadata FROM messages WHERE id = ?", (row_id,)).fetchone()
  239. conn.execute("UPDATE messages SET display_identity = ?, display_order = ? WHERE id = ?",
  240. (self._display_identity(self._display_dedupe_key(live)), order, row_id))
  241. # Stamp the replacement with the stored row's new CAS digest (read back, not the bind values):
  242. # a second prune of this transcript must compare against what this commit wrote, not what it
  243. # replaced, or it would false-stale against its own output (#124102).
  244. final_row = conn.execute(
  245. "SELECT * FROM messages WHERE id = ?", (row_id,)).fetchone()
  246. replacement[DB_ROW_SNAPSHOT] = transcript_row_snapshot(final_row)
  247. if patch:
  248. conn.execute("UPDATE sessions SET model_config = ? WHERE id = ?", (patched_model_config, session_id))
  249. return len(updates)
  250. self._ensure_display_order(session_id) # every row holds its stored slot before one is carried over
  251. # _execute_transcript_write restores each dict's row state (id/digest/timestamp) on rollback, so a
  252. # failed or retried commit never leaves the replacements stamped with an uncommitted digest.
  253. return self._execute_transcript_write(
  254. _do, [replacement for _, replacement in changes])