Skip to content

The agent that grows with you

fix(gateway): a retired profile authority refuses cron mutations

Teknium86d4c2ef2f34
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.py
index 93911466500..7a5a16a1f4f 100644
--- a/gateway/run_runtime.py
+++ b/gateway/run_runtime.py
@@ -279,6 +279,9 @@ async def _retire_profile_authority(authority, timeout=None):
if rest:
await asyncio.gather(*rest, return_exceptions=True)
unbind_owner(authority)
+ # Ownership is released next; until a successor bumps the epoch, A's epoch still fences its
+ # writes as current, so a stale connection to A must find it retired instead.
+ authority.retired = True
# No drain is left to answer an observer (a session_busy pause keeps API/webhook/Bot ones
# waiting): release them all. Their rows stay queued for the next owner, so nothing is resent.
from hermes_state_runtime import RuntimeStoreError
diff --git a/gateway/session_cron.py b/gateway/session_cron.py
index a4d8d35543f..84b4d7774e7 100644
--- a/gateway/session_cron.py
+++ b/gateway/session_cron.py
@@ -106,6 +106,8 @@ RUN_JOB_FLAGS = ('_model_unreachable', '_quota_hold_seconds')
async def operation(authority, name, params, actor=None):
actor = _actor(authority, actor)
+ if getattr(authority, 'retired', False):
+ raise RuntimeStoreError('runtime_retired') # released ownership: nothing here is ours to touch
# Only new work is fenced by a drain. ``recover``/``status``/``cancel`` observe or stop work
# already admitted, which the drain is waiting for: refusing them makes the firer give up
# (CronExecutionUnknown pauses the job) on every restart that overlaps a cron run.
diff --git a/tests/gateway/test_retired_authority_cron_fence.py b/tests/gateway/test_retired_authority_cron_fence.py
new file mode 100644
index 00000000000..c4ecbdb79ba
--- /dev/null
+++ b/tests/gateway/test_retired_authority_cron_fence.py
@@ -0,0 +1,31 @@
+"""A retired profile authority refuses cron mutations from a stale connection: its ownership is
+released, but until a successor advances the runtime epoch its epoch would still pass the store's
+stale-epoch fence, so cron.cancel could cancel an admission the next owner is about to run."""
+import asyncio
+
+import pytest
+
+from gateway import run_runtime, session_cron
+from gateway.session_contract import Principal, Submission
+from hermes_state_runtime import RuntimeStoreError, list_session_admissions
+from tests.gateway.test_profile_retire_stops_turns import REF, _Agent, _authority
+
+CRON = Principal('cron-owner', 'owned', frozenset({'session:submit', 'session:control'}), 'cron-ticker')
+
+
+async def test_retired_authority_refuses_cron_cancel_before_successor_epoch(tmp_path, monkeypatch):
+ db, authority, _execute, _returned = _authority(tmp_path, _Agent())
+ monkeypatch.setattr('gateway.session_cron.unbind_owner', lambda authority: None)
+ monkeypatch.setattr(authority, '_schedule', lambda ref: None) # the row stays queued for the next owner
+ try:
+ with db:
+ receipt = await authority.submit(CRON, Submission(request_id='cron:j:1', ref=REF, payload={'text': ''}, intent='queue'))
+ assert await run_runtime._retire_profile_authority(authority, timeout=1.0) is True
+ with pytest.raises(RuntimeStoreError, match='runtime_retired'):
+ await session_cron.operation(authority, 'cancel', {'session_id': 's', 'admission_id': receipt.admission_id})
+ [row] = list_session_admissions(db, session_id='s', pending_only=False)
+ assert row['status'] == 'queued'
+ finally:
+ await asyncio.sleep(.05)
+ db.close()