hermes_state_api_content.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """The ``api_content`` sidecar backfill writers for SessionDB (#124102 FILE_LINES split).
  2. Split out of :mod:`hermes_state_messages`: the two row-addressed sidecar UPDATEs the turn prologue
  3. uses when the current turn's user row was materialized before the sidecar could be composed. Bound
  4. into ``SessionDB`` via this mixin, so every existing ``session_db.set_message_api_content(...)``
  5. call site and test patch seam keeps working unchanged.
  6. """
  7. from __future__ import annotations
  8. from typing import Any
  9. class SessionApiContentMixin:
  10. """``SessionDB`` mixin: the ``api_content`` sidecar backfills."""
  11. def set_latest_user_api_content(self, session_id: str, content: Any, api_content: str) -> int:
  12. """Backfill the ``api_content`` sidecar onto the newest ACTIVE user row (0/1 rows). Preflight compaction
  13. inserts that row BEFORE the sidecar exists and the later persist identity-skips compacted dicts;
  14. without this a reload reopens the prompt-cache divergence. ``content`` match guards a racing rewrite.
  15. POSITIONAL, and only safe when the caller already knows the newest
  16. active user row IS the message it stamped. The content match is NOT
  17. sufficient on its own: repeated identical user turns ("ok", "y",
  18. "continue") make an OLDER row compare equal, so calling this before
  19. the current turn's row exists overwrites the previous turn's sidecar
  20. with this turn's bytes — durable wrong-bytes replay, a worse cache
  21. break than the missing sidecar. When the caller holds the durable row
  22. id (``_row_id``, synced onto the live dict by
  23. ``sync_flushed_message_markers`` and stamped by
  24. :meth:`_insert_message_rows`), use :meth:`set_message_api_content`
  25. instead — it addresses the exact row and cannot land on a neighbour.
  26. """
  27. from hermes_state_messages import _scrub_surrogates
  28. return self._write_rowcount(
  29. "UPDATE messages SET api_content = ? WHERE id = (SELECT id FROM messages "
  30. "WHERE session_id = ? AND role = 'user' AND active = 1 ORDER BY id DESC LIMIT 1"
  31. ") AND content IS ?",
  32. (_scrub_surrogates(api_content), session_id, self._encode_content(content)))
  33. def set_message_api_content(
  34. self, session_id: str, row_id: int, content: Any, api_content: str
  35. ) -> int:
  36. """Backfill the ``api_content`` sidecar onto ONE known durable row.
  37. Row-addressed counterpart to :meth:`set_latest_user_api_content`: the
  38. caller passes the ``_row_id`` the write path stamped on the live
  39. message dict, so the update cannot drift onto a neighbouring row that
  40. merely carries the same text.
  41. Used by the turn prologue whenever the current turn's user row was
  42. already materialized before the sidecar could be composed (in-place
  43. preflight compaction, a close/early flush that raced the prologue).
  44. The crash persist then marker-skips that message, so this is the only
  45. way the stamped bytes reach the store.
  46. ``active = 1`` and the ``content`` match stay as defensive guards: a
  47. row the compaction archived, or one a racing rewrite changed, is left
  48. untouched.
  49. """
  50. from hermes_state_messages import _scrub_surrogates
  51. if not session_id or isinstance(row_id, bool) or not isinstance(row_id, int) or row_id <= 0:
  52. return 0
  53. return self._write_rowcount(
  54. "UPDATE messages SET api_content = ? WHERE id = ? AND session_id = ? "
  55. "AND role = 'user' AND active = 1 AND content IS ?",
  56. (_scrub_surrogates(api_content), row_id, session_id, self._encode_content(content)))