Skip to content

The agent that grows with you

refactor(gateway): one spelling of the stored viewer-close key

Tekniumf84c5a6069b7
diff --git a/gateway/session_viewer_close.py b/gateway/session_viewer_close.py
index a41c220e9da..ac52095970b 100644
--- a/gateway/session_viewer_close.py
+++ b/gateway/session_viewer_close.py
@@ -20,11 +20,12 @@ import uuid
from functools import partial
from hermes_state_local import local_receipt
+from hermes_state_mutation_retirement import VIEWER_CLOSE_PREFIX
from hermes_state_runtime import RuntimeStoreError
logger = logging.getLogger(__name__)
-CLOSE_PREFIX = 'gateway.viewer_close.v1:'
+CLOSE_PREFIX = VIEWER_CLOSE_PREFIX # route retirement drops it too
_BUSY_SQL = ("SELECT 1 FROM session_admissions WHERE target_session_id=? AND status IN ('queued','started','unknown') "
"UNION ALL SELECT 1 FROM worker_executions WHERE session_id IN (?,?) "
diff --git a/hermes_state_mutation_retirement.py b/hermes_state_mutation_retirement.py
index 8558b9208b2..e3eb636909b 100644
--- a/hermes_state_mutation_retirement.py
+++ b/hermes_state_mutation_retirement.py
@@ -4,6 +4,8 @@ import time
from hermes_state_runtime import RuntimeStoreError, admission_fingerprint, mutation_receipt_key, _json, _text
RETIRED_PREFIX = 'gateway.retired_session.v1.'
+# A last viewer's close stored until the owner's idle point (gateway/session_viewer_close).
+VIEWER_CLOSE_PREFIX = 'gateway.viewer_close.v1:'
def retired_session(db, session_id):
@@ -200,7 +202,7 @@ def retire_routes(conn, session_ids):
conn.execute('INSERT OR REPLACE INTO state_meta(key,value) VALUES(?,?)',
(RETIRED_PREFIX + sid, json.dumps({'retired_at': time.time()})))
conn.execute('DELETE FROM state_meta WHERE key=?', ('gateway.api.settings.v1.' + sid,))
- conn.execute('DELETE FROM state_meta WHERE key=?', ('gateway.viewer_close.v1:' + sid,))
+ conn.execute('DELETE FROM state_meta WHERE key=?', (VIEWER_CLOSE_PREFIX + sid,))
targets = set(session_ids)
for row in conn.execute('SELECT scope,session_key,entry_json FROM gateway_routing').fetchall():
if entry_session_id(row['entry_json']) in targets:
diff --git a/tests/gateway/test_viewer_close_lifecycle.py b/tests/gateway/test_viewer_close_lifecycle.py
index 39b25508d2a..2321852a7e1 100644
--- a/tests/gateway/test_viewer_close_lifecycle.py
+++ b/tests/gateway/test_viewer_close_lifecycle.py
@@ -543,3 +543,23 @@ async def test_a_viewer_back_before_the_unknown_is_resolved_keeps_the_turn_for_i
await asyncio.wait_for(restarted.sessions['chat'].task, 10) # the drain pauses on the unknown turn
assert get_session_admission(db2, admission_id=row['admission_id'])['status'] == 'unknown'
assert db2.get_session('chat')['ended_at'] is None and calls2 == [] and _stored(db2) is None
+
+
+async def test_attaching_a_session_restored_on_demand_cancels_its_stored_close(tmp_path, monkeypatch):
+ """Why attach reads the store instead of trusting ``pending_closes.pop``: a stored close is only
+ re-armed by the scheduled boot recovery, for sessions it could restore. One mounted any other
+ way (here the authority's own unscheduled mount; in production a session paused at boot and
+ restored by ``authorize`` on first use) has a stored close that never reached memory. Left in
+ place, the next boot would close a session its viewer came back to."""
+ import json
+ from gateway.session_viewer_close import CLOSE_PREFIX
+
+ db, authority, _calls = await _owner(tmp_path, monkeypatch)
+ viewer, subscription = await _open(db, authority, tmp_path)
+ await _call(viewer, 'session.detach', session_id='chat', subscription_id=subscription)
+ db.set_meta(CLOSE_PREFIX + 'chat', json.dumps({'op': 'x', 'reason': 'cli_close'}))
+ db2, restarted, calls2 = await _restarted_owner(tmp_path, monkeypatch, db)
+ assert restarted.pending_closes == {} and db2.get_meta(CLOSE_PREFIX + 'chat') is not None
+ await _call(_viewer(restarted, tmp_path), 'session.resume', session_id='chat')
+ assert db2.get_meta(CLOSE_PREFIX + 'chat') is None and calls2 == []