Files
hermes-agent/gateway/session_transcript_subagent.py
unsupportedpastels 4d89901ef4 feat(gateway): one session.transcript pages, polls and reads delegated children; handoffs are rows
session.transcript becomes the one stored-transcript read; session.compactions
and subagent.transcript never ship.

- Forward polling: an oldest-order cursor is never null. It also holds the
  segment it read and transcript_cursor_state of the rows it covered; while
  both hold, the next read continues after it (reset: false, only appends).
  An in-place compaction, a rotation, a rewind, or a rewrite or deletion of a
  covered row answers reset: true from the first page. The stored state is
  read between two snapshots around the page read, and a non-append change in
  between reads the page again. A page is empty only when caught up (windows
  that project to nothing are read past). Latest order keeps its relocating
  cursor and ends with a null cursor.
- An oldest-order page continues at the next display slot instead of
  filtering by durable id, which is not monotonic in display order (a
  handoff's new row sits before the carried copies of older messages).
- Every row carries row_kind: "message" (the session.resume display row) or
  "compaction" (a handoff at its position: role system, the readable summary
  as text, compacted_messages, timestamp, row_id). Older handoffs on earlier
  copies of a re-wrapped or unwrapped carrier still show, each once
  (SessionDB.transcript_handoff_rows).
- subagent_id reads a delegated child of session_id with the ownership rule
  from #140477 (running: the parent's live roster; finished: the _subagent_id
  marker and a lineage of delegate-child or compression hops). The reply adds
  subagent_id, available, running and child_session_id; a foreign or unknown
  child is available: false with no rows, a null cursor and reset: true.
- runtime.describe advertises session-transcript-v1 and
  session-list-picker-v1.
- Contracts, OpenRPC/TS and the serve handler ledger regenerated; docs
  updated. Storage-interface port names on every new or changed SessionDB
  method.

Tests: the stored-read suites move to the one method (resets where a
compaction lands between pages, handoffs as rows); new poll tests drive
appends, compaction, rotation, rewind and rewrites between polls, plus a
seeded random sequence checked against a fresh read after every poll. The
subagent ownership, lineage, multiplex-scope and live E2E tests are adapted
from #140477 to session.transcript with subagent_id.
2026-11-06 22:07:09 -05:00

65 lines
3.5 KiB
Python

"""``session.transcript`` with ``subagent_id``: a delegated child's stored transcript, for a viewer of its
parent session (ported from #140477's ``subagent.transcript``, now one read with the stored pager).
A child is a session of its own in the owning profile's store, written message by message while it runs,
but ``session.resume`` refuses it and ``subagent.list`` rows are live-registry projections. This read joins
the two under the parent's authority: a running child is the one the parent's roster names (the
``subagent.list`` rule, nested children included); a finished one is found by the ``subagent_id`` stamped
on its row (``tools/delegate_tool_sessions.py``), and only when its ancestry reaches this conversation
through delegate-child or compression-continuation hops. Anything else is ``available: false``.
"""
import asyncio
from gateway.session_busy_controls import authorize
from hermes_state_runtime import RuntimeStoreError
def unavailable(session_id, subagent_id, running):
return {'session_id': session_id, 'rows': [], 'next_cursor': None, 'reset': True,
'subagent_id': subagent_id, 'available': False, 'running': running, 'child_session_id': None}
async def subagent_transcript(connection, ref, params, binding, held, limit, inline_images):
authority = connection.authority
# Cold adoption belongs to session.resume, never a child poll.
if ref.session_id not in authority.sessions:
raise RuntimeStoreError('not_found')
authorize(connection, ref, params, 'session:read')
return await asyncio.to_thread(_read, authority, authority.sessions[ref.session_id], ref, binding, held,
limit, inline_images)
def _child(authority, live, ref, subagent_id):
"""``(child session id or None, running)``. In a worker thread, like ``subagent.list``: the roster is read
under the session's event lock, which every streamed token takes too."""
from gateway.session_ancillary import (
_compression_lineage, _owner_objects, _require_current_route, locked_projection, physical_target)
from tools.delegate_tool_sessions import stored_child_session
def roster():
_require_current_route(authority, ref)
return _owner_objects(authority, ref, include_fallback_evicted=True)[1], physical_target(authority, ref)
records, target = locked_projection(authority, live, ref, roster)
record = next((r for r in records if r.get('subagent_id') == subagent_id), None)
child = getattr(record.get('agent'), 'session_id', None) if record else None
if isinstance(child, str) and child:
return child, True
return stored_child_session(authority.db, subagent_id, _compression_lineage(authority.db, target)), record is not None
def _read(authority, live, ref, binding, held, limit, inline_images):
from gateway.session_authorities import owner_scope
from gateway.session_stored_reads import read_transcript
subagent_id = binding[1]
with owner_scope(authority): # every store read goes through the owning authority's own store
child, running = _child(authority, live, ref, subagent_id)
if child is None:
return unavailable(ref.session_id, subagent_id, running)
db = authority.db
page = read_transcript(db, child, db.transcript_lineage_ids(child), binding, held, limit,
profile_home=authority.profile_id, inline_images=inline_images)
return {'session_id': ref.session_id, **page, 'subagent_id': subagent_id, 'available': True,
'running': running, 'child_session_id': child}