Files
hermes-agent/gateway/session_picker_fields.py
unsupportedpastels 857464b871 fix(gateway): a transcript cursor vouches for its own page, covers every segment, and limit counts handoffs
Review fixes for session.transcript (all behind SessionDB methods now):

- One read generation per poll. The cursor check, the page and the state the
  next cursor stores are read in one read transaction
  (SessionDB.transcript_read_snapshot: _read_ctx hands the thread's pinned
  pooled reader to every nested read). Before, a row appended after the poll
  started could land on the page and be deleted (clear_messages moves neither
  the inactive count nor transcript_revision) before the state was sampled;
  the cursor then certified the post-deletion state while the reader held the
  deleted row, and later polls answered reset: false forever. A store without
  a pooled reader (not WAL, read-only, pool ceiling) is not pinned, so a write
  from inside can never wait on its own lock.
- transcript_cursor_state's live count spans the whole displayed lineage: the
  segment's own rows up to the cursor, plus every live row of its earlier
  segments. Deleting an earlier segment's rows now resets a poller, as its
  reactions already did.
- limit bounds the rows sent, compaction rows included. A row's handoffs and
  the row can fall on two pages; the cursor then names the row and how many
  of its group were sent (a third key part), so no handoff is dropped or
  sent twice, both orders, several handoffs on one carrier included.
- Storage interface: every new store read is a narrowly named SessionDB method
  in one module, hermes_state_stored_reads.py (was
  hermes_state_transcript_cursor.py; hermes_state_display_set.py folds in as
  _display_set / _with_removed_identities, the worker calls the latter as on
  main): transcript_read_snapshot, transcript_segment_page (with the cursor
  slot and durable-id lookups), transcript_tool_calls_before,
  sessions_in_workspace, picker_session_facts, session_current_segments,
  subagent_session_ids. get_messages and _fetch_conversation_rows gain their
  port lines. The gateway and tool modules keep their function names.

Tests: tests/gateway/test_session_transcript_snapshot.py (append+delete during
a page read; an earlier segment's deletion and, separately, its reaction;
limit 1 and 2 across a carrier with two handoffs, oldest and latest) and
tests/hermes_state/test_transcript_read_snapshot.py (one generation inside a
snapshot; a DELETE-journal store is not pinned). One consistency test's first
page now ends at the handoff, which counts toward limit.
2026-11-06 22:07:09 -05:00

76 lines
4.4 KiB
Python

"""What a session picker shows for each ``session.list`` row (``picker: true``), and the folder filter (#150091).
Location (``cwd``, ``workspace``) comes from the conversation's first row: the row ``workspace`` filters
by (``list_sessions_rich`` filters root rows, as ``hermes -c`` does), and the row a workspace move
rewrites with every other one. The rest comes from its current segment (a local receipt's entry, else
the compression tip), as ``list_sessions_rich`` projects a compressed chain onto its tip: the first
prompt there (``preview``), its branch, pin, newest activity and newest reply. The stored feed passes the
columns ``list_sessions_rich`` already read; the live feed reads them here. Either way a page is one
store checkout in a worker thread: batched lookups and one indexed probe per segment for its last reply.
"""
from __future__ import annotations
def workspace_members(db, session_ids, workspace):
"""The ids among *session_ids* whose row is in *workspace* (``SessionDB.sessions_in_workspace``: the
clause ``list_sessions_rich`` filters the stored feed by, ``hermes -c``'s rule)."""
return db.sessions_in_workspace(session_ids, workspace)
def _last_reply(candidates):
"""The newest of a segment's replies (``picker_session_facts``) that shows text, projected as a display
read projects it (``project_compaction_message_for_display``): a pure handoff the compressor wrote as an
assistant row shows nothing, so the reply before it stands; a carrier shows the reply it wraps."""
from agent.compaction_display import project_compaction_message_for_display
from agent.message_content import flatten_message_text
from hermes_state_common import _shape_preview
for content, summary in candidates:
shown = project_compaction_message_for_display(
{'role': 'assistant', 'content': content, '_compressed_summary': summary})
text = _shape_preview(flatten_message_text(shown['content'])) if shown is not None else ''
if text:
return text
return None
def fill_picker_fields(db, items):
"""Set the picker fields on each ``(row, root, segment, listed)``: *listed* is the segment's
``list_sessions_rich`` row (its ``preview`` / ``last_active`` / branch / pin), None to read them."""
from hermes_state_common import _shape_preview
from hermes_state_sessions import workspace_key
roots, segments, replies = db.picker_session_facts(
[root for _, root, _, _ in items], [segment for _, _, segment, listed in items if listed is None],
[segment for _, _, segment, _ in items])
replies = {segment: _last_reply(candidates) for segment, candidates in replies.items()}
for row, root, segment, listed in items:
if listed is None:
listed = segments.get(segment)
if listed is None:
continue # deleted since it was listed: the row keeps what the feed read
listed = {**listed, 'preview': _shape_preview(listed['_preview_raw'])}
first = roots.get(root) or {}
row.update(preview=listed.get('preview') or None, cwd=first.get('cwd') or None,
workspace=workspace_key(first), git_branch=listed.get('git_branch') or None,
last_active_at=listed.get('_effective_last_active') or listed.get('last_active'),
pinned=bool(listed.get('pinned')), last_reply=replies[segment])
def current_segments(authority, session_ids):
"""``{logical id: current physical segment}`` (``SessionDB.session_current_segments``: a local receipt's
entry, else the compression tip) in one store checkout."""
return authority.db.session_current_segments(session_ids)
def live_picker_page(authority, sessions, limit, workspace=None, picker=False):
"""The live feed's page: *sessions* (newest first) in *workspace*, cut to *limit*, with the picker
fields when asked."""
if workspace is not None:
members = workspace_members(authority.db, [row['session_id'] for row in sessions], workspace)
sessions = [row for row in sessions if row['session_id'] in members]
page = sessions[:limit]
if picker and page:
segments = current_segments(authority, [row['session_id'] for row in page])
fill_picker_fields(authority.db, [(row, row['session_id'], segments.get(row['session_id'], row['session_id']),
None) for row in page])
return page