Skip to content

The agent that grows with you

fix(gateway): arm startup orphan close for queued sessions; bind profile for the grace

unsupportedpastelscfcc535b3726
diff --git a/gateway/session_local_recovery.py b/gateway/session_local_recovery.py
index 21e3231811b..e588e18676e 100644
--- a/gateway/session_local_recovery.py
+++ b/gateway/session_local_recovery.py
@@ -383,15 +383,16 @@ def local_receipt_ids(authority):
def recover_local_sessions(authority, *, schedule=False):
- idle = []
+ orphans = []
for sid in local_receipt_ids(authority):
try:
ref = restore_local_session(authority, sid)
pending = list_session_admissions(authority.db, session_id=sid)
- if schedule and pending and not any(row['status'] == 'unknown' for row in pending):
+ if any(row['status'] == 'unknown' for row in pending):
+ continue # the drain pauses on it until its user's Discard
+ if schedule and pending:
authority._schedule(ref)
- if not pending:
- idle.append(sid)
+ orphans.append(sid)
except (_ReceiptRefused, _CatchUpPending):
continue # logged: the check that refused it, or the catch-up handed to a worker
except RuntimeStoreError as exc:
@@ -406,4 +407,4 @@ def recover_local_sessions(authority, *, schedule=False):
# A hard-killed owner's turn: the drain pauses on it, so the close resolves it.
track_mutation(authority, settle_viewer_close(authority, sid))
from gateway.session_viewer_close import arm_startup_orphan_closes
- arm_startup_orphan_closes(authority, idle)
+ arm_startup_orphan_closes(authority, orphans)
diff --git a/gateway/session_viewer_close.py b/gateway/session_viewer_close.py
index 7445d9e11ac..8d53c2373e9 100644
--- a/gateway/session_viewer_close.py
+++ b/gateway/session_viewer_close.py
@@ -166,8 +166,12 @@ def orphan_close_grace(authority):
"""Blocking: ``sessions.orphan_end_grace_s`` of the profile that owns *authority*'s store (0 = never)."""
from hermes_cli.config_defaults import DEFAULT_CONFIG
from hermes_cli.config_effective import load_user_config_effective
+ from gateway.session_local import profile_env_scope
default = DEFAULT_CONFIG['sessions']['orphan_end_grace_s']
- config = load_user_config_effective(Path(authority.db.db_path).resolve().parent / 'config.yaml')
+ home = Path(authority.db.db_path).resolve().parent
+ # A dropped socket detaches outside the RPC's owner scope: expand ``${VAR}`` against this profile.
+ with profile_env_scope(home):
+ config = load_user_config_effective(home / 'config.yaml')
with contextlib.suppress(ValueError, TypeError, AttributeError):
return max(0.0, float((config.get('sessions') or {}).get('orphan_end_grace_s', default)))
return default
@@ -212,8 +216,9 @@ async def arm_detached_orphan_close(authority, session_id):
def arm_startup_orphan_closes(authority, session_ids):
- """Boot, after the stored closes are re-armed: the previous owner stopped under these idle
- sessions, so no viewer is left to close them (#143547 LC-37). One that reattaches keeps it open."""
+ """Boot, after the stored closes are re-armed: the previous owner stopped under these sessions
+ (idle, or with input it left queued), so no viewer is left to close them (#143547 LC-37). One
+ that reattaches keeps it open; a fired close still waits for the queued input to settle."""
grace = orphan_close_grace(authority)
for session_id in session_ids:
if session_id not in authority.pending_closes and orphan_candidate(authority, session_id):
diff --git a/tests/gateway/test_local_session_orphan_end.py b/tests/gateway/test_local_session_orphan_end.py
index b33c7dd4b1d..dbe4d2ce558 100644
--- a/tests/gateway/test_local_session_orphan_end.py
+++ b/tests/gateway/test_local_session_orphan_end.py
@@ -199,6 +199,48 @@ async def test_a_session_open_when_the_gateway_stopped_ends_after_boot(tmp_path,
assert authority.db.get_session(sid)['end_reason'] == 'startup_orphan_reap'
+async def test_a_session_left_with_queued_input_ends_after_boot_once_it_drains(tmp_path, monkeypatch):
+ import gateway.session_finite as finite
+ from gateway.session_local_recovery import recover_local_sessions
+ from hermes_state_runtime import admit_session_input, list_session_admissions
+
+ armed = _capture_timers(monkeypatch)
+ first, _ = await _owner(tmp_path, monkeypatch)
+ sid = _create(first).session_id
+ admit_session_input(first.db, epoch=first.epoch, principal_id=first.sessions[sid].source.user_id,
+ session_id=sid, request_id='r1', payload={'text': 'queued'})
+ executed = []
+
+ async def execute(authority, ref, row):
+ executed.append(row['payload']['text'])
+ return 'done'
+ monkeypatch.setattr(finite, 'execute_finite_admission', execute)
+
+ authority, finalized = await _owner(tmp_path, monkeypatch) # the gateway stopped before running it
+ recover_local_sessions(authority, schedule=True)
+ await async_wait_until(lambda: executed == ['queued']
+ and not list_session_admissions(authority.db, session_id=sid))
+ assert [delay for delay, _ in armed] == [300.0]
+ armed[0][1]()
+ await async_wait_until(lambda: finalized == ['startup_orphan_reap'])
+ assert authority.db.get_session(sid)['end_reason'] == 'startup_orphan_reap'
+
+
+async def test_a_dropped_viewer_expands_the_grace_against_its_own_profile(tmp_path, monkeypatch):
+ """The socket's close detaches outside the RPC's owner scope; the launch env must not answer."""
+ armed = _capture_timers(monkeypatch)
+ authority, _ = await _owner(tmp_path, monkeypatch)
+ home = Path(authority.db.db_path).parent
+ (home / 'config.yaml').write_text('sessions:\n orphan_end_grace_s: ${ORPHAN_GRACE}\n')
+ (home / '.env').write_text('ORPHAN_GRACE=7\n')
+ monkeypatch.setenv('ORPHAN_GRACE', '900')
+ viewer, _ = await _attach(authority, _create(authority).session_id)
+ await viewer.close()
+ assert [delay for delay, _ in armed] == [7.0]
+
+
@pytest.mark.asyncio
async def test_a_zero_grace_never_ends_a_detached_session(tmp_path, monkeypatch):
armed = _capture_timers(monkeypatch)