The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): keep closed local sessions in the stored picker after a restart
unsupportedpastelsd05095663342
diff --git a/gateway/session_listing.py b/gateway/session_listing.pyindex afe650c1603..68768336919 100644--- a/gateway/session_listing.py+++ b/gateway/session_listing.py@@ -93,14 +93,34 @@ def _closed_local_session(authority, actor, row, tip):owner = tip.get('chat_id')live = authority.sessions.get(owner) if owner else Noneif live is None:- return None- try:- handle = authority._handle(SessionRef(authority.profile_id, owner))- authority.authorize(actor, handle.ref, 'session:read')- except RuntimeStoreError:- return None- if not closed_and_unviewed(authority, owner, handle):- return None+ if not owner or not _stored_closed(authority, actor, owner):+ return None+ else:+ try:+ handle = authority._handle(SessionRef(authority.profile_id, owner))+ authority.authorize(actor, handle.ref, 'session:read')+ except RuntimeStoreError:+ return None+ if not closed_and_unviewed(authority, owner, handle):+ return Nonereturn {'id': owner, 'session_id': owner, 'resolved_id': owner, 'title': tip.get('title') or row.get('title') or '','source': tip['source'], 'started_at': row.get('started_at'),'message_count': row.get('message_count', 0), 'model': tip.get('model'), 'running': False}+++def _stored_closed(authority, actor, owner):+ """Boot leaves an ended session unrestored and a listing never restores one (a worker thread): judge+ its stored receipt as ``authorize`` and ``closed_and_unviewed`` would after a restore. No viewer."""+ from hermes_state_local import local_receipt+ from hermes_state_runtime import list_session_admissions+ if actor.profile_id != authority.profile_id or 'session:read' not in actor.capabilities:+ return False+ try:+ receipt = local_receipt(authority.db, owner)+ row = authority.db.get_session(receipt['entry']['session_id'])+ except (RuntimeStoreError, KeyError, TypeError):+ return False+ return (receipt.get('profile_id') == authority.profile_id+ and (receipt.get('principal_id') == actor.subject or 'session:operator' in actor.capabilities)+ and row is not None and row['ended_at'] is not None+ and not list_session_admissions(authority.db, session_id=owner))diff --git a/tests/gateway/test_tui_detach_end_stamp.py b/tests/gateway/test_tui_detach_end_stamp.pyindex 6790affd20e..e088aac11ca 100644--- a/tests/gateway/test_tui_detach_end_stamp.py+++ b/tests/gateway/test_tui_detach_end_stamp.py@@ -80,3 +80,22 @@ async def test_plain_detach_never_stamps(tmp_path, monkeypatch):resumed = await _call(viewer, 'session.resume', session_id='old-tui')await _call(viewer, 'session.detach', session_id='old-tui', subscription_id=resumed['subscription_id'])assert db.get_session('old-tui')['end_reason'] is None+++async def test_a_closed_session_stays_in_the_stored_picker_after_a_restart(tmp_path, monkeypatch):+ from gateway.session_local import create_local_session++ db, authority = await _owner(tmp_path, monkeypatch)+ viewer = _native(authority, tmp_path)+ sid = create_local_session(authority, viewer.actor, {+ 'request_id': 'r1', 'source': 'tui', 'cwd': str(tmp_path), 'model': 'm', 'toolsets': []}).session_id+ db.append_message(sid, 'user', 'q')+ resumed = await _call(viewer, 'session.resume', session_id=sid)+ await _call(viewer, 'session.detach', session_id=sid, subscription_id=resumed['subscription_id'],+ end_reason='tui_close')++ _, restarted = await _owner(tmp_path, monkeypatch) # the gateway restarts+ stored = await _call(_native(restarted, tmp_path), 'session.list', scope='stored')+ assert [row['id'] for row in stored['sessions']] == [sid]+ assert sid not in restarted.sessions # the listing restored nothing