Files
hermes-agent/gateway/session_transcript_rows.py
unsupportedpastels 57c3630e16 fix(gateway): a transcript read without a pooled reader resets when unproven, and a shared handoff shows once across pages
Review fixes for session.transcript:

- A store without a pooled reader (not WAL, a read-only handle, a reader
  that failed to open, the pool's ceiling) pinned nothing, and its
  consistency check was always true: a row deleted between the page read
  and the cursor-state read stayed in the client forever (reset: false).
  transcript_read_snapshot now checks there that no write landed in the
  database during the block (_write_generation: the writer connection, its
  total_changes for this process's writes and PRAGMA data_version for
  every other connection's commits), holding no lock across the block.
  _poll reads once more when one did; when the retry also overlaps a
  write, the reply is reset: true with the first page and a cursor state
  no transcript has, so the next poll resets again. The pooled path is
  unchanged. The yielded check gains .pinned.
- page_handoffs deduplicated a handoff only within the current window, so
  two carriers holding the same uid-less handoff each showed it once the
  window moved past the first (also limit 1, oldest order). A handoff is
  now shown before the first row in the whole transcript that holds it: a
  rotated lineage checks the rows before the window it already read; one
  segment asks the new SessionDB.transcript_handoffs_shown_before for the
  display rows before the window's first slot.

Tests: tests/gateway/test_session_transcript_handoff_owner.py (seeded
transcripts with identical uid-less handoffs on several carriers, re-wraps,
in-place compactions and rotations: every limit 1-5, both orders, equals
the full read; two identical uid-less carriers, limit 1 and 2, both
orders). tests/gateway/test_session_transcript_snapshot.py: append-then-
clear and delete-during-page, each pooled and without a pooled reader; a
read that never settles resets twice, then converges.
tests/hermes_state/test_transcript_read_snapshot.py: without a pooled
reader a write from this process or another connection makes the block
inconsistent.
2026-11-06 22:07:09 -05:00

90 lines
4.4 KiB
Python

"""``session.transcript`` row kinds: every row carries ``row_kind``.
- ``'message'``: a display row, exactly as ``session.resume``'s ``display_messages`` projects it.
- ``'compaction'``: a compaction handoff at its position in the transcript: ``role: 'system'``, ``text``
the summary a reader sees (``agent.compaction_display.compaction_summary``: without the preamble, end
marker, re-quoted user messages and recovery footer written for the model), ``compacted_messages`` (how
many messages it summarized, when the handoff names it), ``timestamp`` and the stored ``row_id``.
The display projection drops a handoff (``project_compaction_message_for_display``); here each one is put
back as its own row just before the row it was stored on. A later compaction can carry, re-wrap or unwrap
a summary carrier, and the display dedupe then shows one copy of that message; the older handoffs on its
earlier copies (``transcript_handoff_rows``) are still shown, each once.
"""
from __future__ import annotations
import json
ROW_KIND_MESSAGE = 'message'
ROW_KIND_COMPACTION = 'compaction'
# The placeholder a handoff holds in the projection: a system row the projection passes through unchanged.
_MARK_KEY = '__session_transcript_handoff__'
_MARK_TEXT = 'compaction handoff'
def handoff_row(message, summary):
row = {'row_kind': ROW_KIND_COMPACTION, 'role': 'system', 'text': summary['summary'],
'compacted_messages': summary['compacted_messages'], 'row_id': message['id']}
timestamp = message.get('timestamp')
if isinstance(timestamp, (int, float)) and timestamp > 0:
row['timestamp'] = float(timestamp)
return row
def _mark_index(row):
metadata = row.get('display_metadata')
index = metadata.get(_MARK_KEY) if isinstance(metadata, dict) else None
return index if row.get('role') == 'system' and row.get('text') == _MARK_TEXT and type(index) is int else None
def handoff_occurrence(message):
# A handoff's copies (a later generation or a rotated segment carrying it) keep its uid and content.
return message.get('message_uid') or None, json.dumps(message.get('content'), default=str)
def shown_occurrences(messages):
"""The handoff occurrences among stored *messages* (``transcript_handoff_rows`` candidates)."""
from agent.compaction_display import compaction_summary
return {handoff_occurrence(message) for message in messages if compaction_summary(message) is not None}
def page_handoffs(db, rows, segments, earlier=None):
"""Per row of the stored *rows* (``_rows_to_conversation`` dicts of *segments*, display order): the
``compaction`` rows shown just before it, one for each handoff stored on the row or on an earlier copy
of its message. Each handoff is shown once in the whole transcript, before the first row that holds
it, wherever the page starts: ``earlier(candidates)`` (stored handoff messages) is the occurrences
among them a row before *rows* holds."""
from agent.compaction_display import compaction_summary
found = db.transcript_handoff_rows([row['_row_id'] for row in rows if type(row.get('_row_id')) is int], segments)
candidates = [message for messages in found.values() for message in messages]
seen, out = set(earlier(candidates)) if earlier is not None and candidates else set(), []
for row in rows:
mine = []
for message in found.get(row.get('_row_id'), ()):
occurrence = handoff_occurrence(message)
summary = compaction_summary(message) if occurrence not in seen else None
if summary is None:
continue
seen.add(occurrence)
mine.append(handoff_row(message, summary))
out.append(mine)
return out
def project_entries(entries, project):
"""``project(rows)`` of *entries* (display order): ``(ROW_KIND_COMPACTION, handoff row)`` is put back in
place as is, ``(ROW_KIND_MESSAGE, stored row)`` is projected and tagged ``row_kind: 'message'``."""
handoffs, marked = [], []
for kind, value in entries:
if kind == ROW_KIND_COMPACTION:
marked.append({'role': 'system', 'content': _MARK_TEXT, 'display_metadata': {_MARK_KEY: len(handoffs)}})
handoffs.append(value)
else:
marked.append(value)
rows = []
for row in project(marked):
index = _mark_index(row)
rows.append(handoffs[index] if index is not None and index < len(handoffs)
else {**row, 'row_kind': ROW_KIND_MESSAGE})
return rows