Skip to content

The agent that grows with you

fix(tui): a viewer's reap checks an adopted conversation's owner ledger

Teknium575eddd5afa8
diff --git a/tests/tui_gateway/test_reap_respects_canonical_ledger.py b/tests/tui_gateway/test_reap_respects_canonical_ledger.py
index e6ebdd88de9..a753978e707 100644
--- a/tests/tui_gateway/test_reap_respects_canonical_ledger.py
+++ b/tests/tui_gateway/test_reap_respects_canonical_ledger.py
@@ -71,3 +71,53 @@ def test_reap_still_ends_an_idle_row(tmp_path, monkeypatch):
assert check.get_session('tui-idle')['end_reason'] == 'ws_orphan_reap'
finally:
check.close()
+
+
+
+async def test_reap_of_a_segment_of_an_adopted_conversation_respects_the_owner_ledger(tmp_path, monkeypatch):
+ """An adopted (pre-canonical) conversation keeps its legacy id as owner and as every compression
+ segment's ``chat_id``, not a ``local-`` id; the reap must still check the owner's ledger through
+ the receipt's lineage (review of #140971). Real adoption, compression publish and admission."""
+ from contextlib import contextmanager
+ from types import SimpleNamespace
+ from gateway import run
+ from gateway.config import GatewayConfig
+ from gateway.session import SessionStore
+ from gateway.session_authority import initialize_session_authority
+ from gateway.session_contract import Principal
+ from gateway.session_local_recovery import reopen_local_session
+ from gateway.session_local_title import resolve_titled_session
+ from tui_gateway import server
+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))
+ monkeypatch.setattr(run, '_load_gateway_config', lambda: {'platform_toolsets': {'cli': []}})
+ store = SessionStore(tmp_path / 'sessions', GatewayConfig())
+ db = store._db
+ db.create_session('old-tui', source='tui', model='frozen', cwd=str(tmp_path))
+ db.append_message('old-tui', 'user', 'old question')
+ db.end_session('old-tui', 'tui_close')
+ runner = SimpleNamespace(session_store=store, _session_db=db, adapters={}, _draining=False)
+ authority = await initialize_session_authority(runner, profile_id=str(tmp_path), instance_id='review')
+ actor = Principal('native', str(tmp_path), frozenset({'session:create', 'session:read'}), 'sock')
+ authority._native_legacy_transports = {actor.transport_id: actor}
+ ref = resolve_titled_session(authority, actor, 'old-tui')
+ reopen_local_session(authority, ref)
+ assert db.try_acquire_compression_lock(ref.session_id, 'review')
+ try:
+ db.publish_compression_child(parent_session_id=ref.session_id, child_session_id='child', source='tui',
+ messages=[{'role': 'user', 'content': 'summary'}], compression_lock_holder='review')
+ finally:
+ db.release_compression_lock(ref.session_id, 'review')
+ assert db.get_session('child')['chat_id'] == 'old-tui'
+ admit_session_input(db, epoch=authority.epoch, principal_id=actor.subject, session_id=ref.session_id,
+ request_id='running', payload={'text': 'continue'})
+ claim_session_input(db, epoch=authority.epoch, session_id=ref.session_id)
+
+ @contextmanager
+ def session_db(_session):
+ yield db
+ monkeypatch.setattr(server, '_session_db', session_db)
+ viewer = {'agent': SimpleNamespace(session_id='child', _end_session_on_close=True),
+ 'session_key': 'child', 'history': [], 'profile_home': str(tmp_path), 'source': 'tui'}
+ server._finalize_session(viewer, end_reason='ws_orphan_reap')
+ assert db.get_session('child')['ended_at'] is None
diff --git a/tui_gateway/session_lifecycle.py b/tui_gateway/session_lifecycle.py
index 853bd516612..9c91852b679 100644
--- a/tui_gateway/session_lifecycle.py
+++ b/tui_gateway/session_lifecycle.py
@@ -150,7 +150,8 @@ def _end_unless_ledger_busy(db, session_id: str, session_key, end_reason: str) -
A viewer process (a dashboard, a second Desktop window) runs this reaper for a session the
owning gateway is still executing; stamping it ended mid-turn made the owner treat its own live
route as stale (#54878) and replace the conversation (#140642). The ledger names a local session
- by its logical id (the row's ``chat_id``), so the physical id, the key and that id are checked."""
+ by its logical id (the row's ``chat_id``), so the physical id, the key and that id are checked: a
+ ``local-`` id, or the lineage owner its receipt names (an adopted conversation keeps its legacy id)."""
execute_write = getattr(db, "_execute_write", None)
if execute_write is None:
db.end_session(session_id, end_reason)
@@ -161,6 +162,8 @@ def _end_unless_ledger_busy(db, session_id: str, session_key, end_reason: str) -
ids = {session_id, str(session_key or "")}
if row is not None and str(row[0] or "").startswith("local-"):
ids.add(str(row[0]))
+ from hermes_state_local import local_lineage_owner
+ ids.add(local_lineage_owner(conn, session_id))
for sid in filter(None, ids):
if conn.execute(_CANONICAL_PENDING_SQL, (sid, sid)).fetchone() is not None:
return False