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

94 lines
4.1 KiB
Python

"""``session.transcript`` with ``subagent_id`` for a served secondary profile reads that profile's store, in its scope.
The read runs off the owner loop with no turn on the stack: it must bind the owning profile
explicitly and never fall back to the launch profile's home or ``state.db``.
"""
import os
from pathlib import Path
import pytest
from gateway.config import GatewayConfig
from gateway.run import GatewayRunner
from gateway.run_runtime import initialize_gateway_runtime
from gateway.runtime_ownership import process_ownership
def _reserve_homes(tmp_path, monkeypatch, names=('beta',)):
root = tmp_path / '.hermes'
root.mkdir(mode=0o700)
monkeypatch.setattr(Path, 'home', lambda: tmp_path)
monkeypatch.setenv('HERMES_HOME', str(root))
import hermes_state
monkeypatch.setattr(hermes_state, 'DEFAULT_DB_PATH', hermes_state._IMPORT_DEFAULT_DB_PATH)
homes = [('default', root)]
for name in names:
home = root / 'profiles' / name
home.mkdir(parents=True, mode=0o700)
(home / 'config.yaml').write_text('{}')
homes.append((name, home))
return root, homes
def _seed_child(db, parent, text):
db.create_session('child', source='subagent', parent_session_id=parent,
model_config={'_delegate_from': parent, '_subagent_id': 'sa-0-scoped'})
db.append_message('child', role='user', content=text)
@pytest.mark.asyncio
async def test_secondary_profile_child_transcript_reads_its_own_store_in_its_scope(tmp_path, monkeypatch):
from gateway import run, session_stored_reads
from gateway.session_ancillary import physical_target
from gateway.session_authorities import owner_scope
from gateway.session_controls import AuthorityConnection
from gateway.session_local import create_local_session
from hermes_constants import get_hermes_home
monkeypatch.setattr(run, '_load_gateway_config', lambda *a: {
'platform_toolsets': {'cli': [], 'tui': []},
'model': {'provider': 'custom', 'base_url': 'http://127.0.0.1:9/v1', 'api_key': 'loopback-only'}})
_root, homes = _reserve_homes(tmp_path, monkeypatch)
config = GatewayConfig()
config.multiplex_profiles = True
config._runtime_profile_homes = tuple(homes)
runner = GatewayRunner(config)
process_ownership.reserve([home for _, home in homes])
seen_homes = []
read_transcript = session_stored_reads.read_transcript
def observed(*args, **kwargs):
seen_homes.append(Path(get_hermes_home()).resolve())
return read_transcript(*args, **kwargs)
monkeypatch.setattr(session_stored_reads, 'read_transcript', observed)
connection = None
try:
await initialize_gateway_runtime(runner)
registry = runner.session_authorities
primary, secondary = registry.for_home(homes[0][1]), registry.for_home(homes[1][1])
connection = AuthorityConnection(secondary, object(), {'user_id': 'human'})
with owner_scope(secondary):
ref = create_local_session(secondary, connection.actor, dict(
request_id='desktop', source='tui', cwd=str(tmp_path), model='default-a', toolsets=[]))
target = physical_target(secondary, ref)
_seed_child(secondary.db, target, 'BETA_CHILD')
# The launch profile's store holds a same-id decoy under the same parent id.
primary.db.create_session(target, source='tui')
_seed_child(primary.db, target, 'PRIMARY_DECOY')
environ = dict(os.environ)
response = await connection.dispatch({'id': 1, 'method': 'session.transcript', 'params': {
'session_id': ref.session_id, 'subagent_id': 'sa-0-scoped'}})
result = response.get('result')
assert result and result['child_session_id'] == 'child', response
assert [m['text'] for m in result['rows']] == ['BETA_CHILD']
assert seen_homes == [homes[1][1].resolve()]
assert dict(os.environ) == environ
finally:
if connection is not None:
await connection.close()
for authority in list(getattr(runner, 'session_authorities', []) or []):
authority.db.close()
for _, home in homes:
process_ownership.release(home)