Files
hermes-agent/tests/gateway/test_session_subagent_transcript.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

152 lines
7.5 KiB
Python

"""A viewer of a session reads its delegated children's transcripts over the shared gateway:
``session.transcript`` with ``subagent_id`` (adapted from #140477's ``subagent.transcript`` tests).
Before it, ``session.resume`` on a child id answered ``not_found``, so a viewer (the hermes-tern plugin's
sidebar) opened ``state.db`` itself and guessed the child by ``parent_session_id`` + first user message.
"""
import json
from pathlib import Path
import subprocess
import sys
import pytest
from tests.gateway.fixtures.local_recovery_probe import child_env
@pytest.fixture(scope='module')
def receipt(tmp_path_factory):
tmp_path = tmp_path_factory.mktemp('subagent-transcript')
root = Path(__file__).resolve().parents[2]
home, user = tmp_path / 'state', tmp_path / 'user'
home.mkdir()
user.mkdir()
env = child_env()
env.update(HOME=str(user), USERPROFILE=str(user), HERMES_HOME=str(home),
PYTHONPATH=str(root), PYTHONUNBUFFERED='1')
result = subprocess.run([sys.executable, str(root / 'tests/gateway/fixtures/subagent_transcript_peer.py')],
cwd=root, env=env, capture_output=True, text=True, timeout=90, check=False)
assert result.returncode == 0, result.stdout + result.stderr
return json.loads((home / 'receipt.json').read_text())
def _texts(reply):
return [(m['role'], m.get('text')) for m in reply['rows'] if m['row_kind'] == 'message']
def _handoffs(reply):
return [m['text'] for m in reply['rows'] if m['row_kind'] == 'compaction']
def test_running_child_transcript_is_served_in_order_with_its_session(receipt):
live, rows = receipt['live'], receipt['rows']['live-child']
assert live['available'] and live['running'] and live['child_session_id'] == 'live-child', live
assert _texts(live) == [('user', 'live goal'), ('assistant', 'live step one')]
assert [m['row_id'] for m in live['rows']] == rows[:2]
assert live['reset'] and isinstance(live['next_cursor'], str)
# subagent.list now names the child's session, so a viewer need not guess it by goal text.
[row] = receipt['list']['subagents']
assert row['subagent_id'] == 'sa-0-live' and row['child_session_id'] == 'live-child', receipt['list']
def test_cursor_returns_only_rows_written_after_it(receipt):
after, caught_up = receipt['live_after'], receipt['live_caught_up']
assert _texts(after) == [('assistant', 'live step two')], after
assert [m['row_id'] for m in after['rows']] == receipt['rows']['live-child'][2:3]
assert caught_up['rows'] == [] and caught_up['next_cursor'], caught_up
def test_finished_children_are_read_from_the_store(receipt):
ended = receipt['live_ended']
assert ended['available'] and not ended['running'] and ended['child_session_id'] == 'live-child', ended
assert _texts(ended)[-1] == ('assistant', 'live step two')
# A compressed child resolves to its live tip, never a foreign child sharing its subagent_id; the segment
# before the rotation is part of its transcript, as session.resume shows a compressed chat.
done = receipt['sa-1-done']
assert done['child_session_id'] == 'done-child-2' and not done['running'], done
assert _texts(done) == [('user', 'done goal'), ('user', 'done summary'), ('assistant', 'done final')]
nested = receipt['sa-0-nested']
assert nested['child_session_id'] == 'nested-child', nested
assert _texts(nested) == [('user', 'nested goal'), ('assistant', 'nested final')]
def test_children_of_other_conversations_are_never_served(receipt):
for name in ('sa-0-foreign', 'sa-0-branch', 'sa-9-unknown'):
reply = receipt[name]
assert reply == {'session_id': reply['session_id'], 'subagent_id': name, 'available': False,
'child_session_id': None, 'running': False, 'rows': [], 'next_cursor': None,
'reset': True}, (name, reply)
assert 'PRIVATE' not in json.dumps({k: v for k, v in receipt.items() if k != 'rows'})
def test_unknown_session_foreign_profile_and_bad_cursor_are_refused(receipt):
assert receipt['unknown_session']['message'] == 'not_found', receipt['unknown_session']
assert receipt['foreign_profile']['message'] == 'profile_mismatch', receipt['foreign_profile']
assert receipt['bad_cursor']['message'] == 'invalid_params', receipt['bad_cursor']
def test_another_authenticated_viewer_reads_the_same_transcript(receipt):
assert receipt['peer_live'] == receipt['live']
def test_runtime_advertises_the_capabilities(receipt):
assert {'session-transcript-v1', 'session-list-picker-v1'} <= set(receipt['capabilities'])
assert 'subagent-transcript-v1' not in receipt['capabilities']
def test_polls_from_a_cursor_read_only_the_new_rows(receipt):
# The first read and the peer's start at the first row; the cursored polls read on from the cursor.
assert receipt['full_reads_while_polling'] == 0, receipt['full_reads_while_polling']
assert not receipt['live_after']['reset'] and not receipt['live_caught_up']['reset']
def test_an_in_place_compaction_resets_the_viewer_instead_of_repeating_rows(receipt):
compacted = receipt['live_compacted']
assert compacted['reset'] and compacted['child_session_id'] == 'live-child', compacted
texts = _texts(compacted)
assert len(texts) == len(set(texts)) and ('user', 'live summary') in texts, compacted
assert texts == [('user', 'live goal'), ('assistant', 'live step one'), ('user', 'live summary'),
('assistant', 'live step two')], compacted
after = receipt['live_compacted_caught_up']
assert not after['reset'] and after['rows'] == [], after
def test_rows_written_and_compacted_between_polls_reach_the_viewer(receipt):
assert receipt['empty']['available'] and receipt['empty']['rows'] == [], receipt['empty']
assert receipt['empty']['next_cursor'], receipt['empty'] # an empty child still hands out a cursor
compacted = receipt['empty_compacted']
assert compacted['reset'], compacted
assert _texts(compacted) == [('user', 'empty goal'), ('assistant', 'empty step'), ('user', 'empty summary')]
def test_a_cursor_the_owner_never_issued_for_this_child_is_refused(receipt):
for reply in receipt['odd_cursors']:
assert reply.get('message') == 'invalid_params', reply
def test_a_metadata_only_rewrite_of_a_held_row_resets_the_viewer(receipt):
reacted = receipt['live_reacted']
assert reacted['reset'], reacted
[row] = [m for m in reacted['rows'] if m.get('text') == 'live summary']
assert [r['emoji'] for r in row['display_metadata']['reactions']] == ['\U0001f525'], row
def test_a_running_worker_of_an_orchestrator_stays_running_after_the_parent_is_rebuilt(receipt):
assert receipt['rebuilt_orch']['running'], receipt['rebuilt_orch']
deep = receipt['rebuilt_deep']
assert deep['running'] and deep['child_session_id'] == 'deep-child', deep
assert receipt['rebuilt_list'] == ['sa-0-deep', 'sa-1-orch'], receipt['rebuilt_list']
def test_a_rewrite_of_a_row_from_before_a_rotation_resets_the_viewer(receipt):
reacted = receipt['done_ancestor_reacted']
assert reacted['reset'] and reacted['child_session_id'] == 'done-child-2', reacted
[row] = [m for m in reacted['rows'] if m.get('text') == 'done goal']
assert [r['emoji'] for r in row['display_metadata']['reactions']] == ['\U0001f44d'], row
def test_a_queued_child_of_a_rotated_delegator_shows_none_of_the_delegator_rows(receipt):
queued = receipt['queued_child']
assert queued['running'] and queued['child_session_id'] == 'queued-child', queued
assert queued['rows'] == [] and queued['reset'], queued