The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a retired authority refuses Bot relay writes as well as cron
Tekniumb8b7d502db32
diff --git a/gateway/session_authorities.py b/gateway/session_authorities.pyindex 209b6474fd7..bf448ca2c14 100644--- a/gateway/session_authorities.py+++ b/gateway/session_authorities.py@@ -148,6 +148,13 @@ def all_authorities(runner):return [authority] if authority is not None else []+def require_serving(authority):+ """Refuse a verb reached through a stale connection to a retired authority: its ownership is+ released, and until a successor bumps the epoch its own epoch still passes the store fence."""+ if getattr(authority, 'retired', False):+ raise RuntimeStoreError('runtime_retired')++def admission_owner(authority, session_id):"""The admission identity a physical store row answers to: its compression root, then thelocal creation id a canonical reset forks under (a reset child is not a compression child, sodiff --git a/gateway/session_bot.py b/gateway/session_bot.pyindex 995540a06ea..abedaaa23d1 100644--- a/gateway/session_bot.py+++ b/gateway/session_bot.py@@ -340,7 +340,8 @@ async def refresh_receipt(authority, home, key):def relay_operation(connection, operation, params):authority, actor = connection.authority, connection.actor- from gateway.session_authorities import served_profile_name+ from gateway.session_authorities import require_serving, served_profile_name+ require_serving(authority) # the outbox/roster/replies belong to the home's next ownerhome = Path(authority.db.db_path).parent.resolve()name = served_profile_name(home)_home(authority, actor, name)diff --git a/gateway/session_cron.py b/gateway/session_cron.pyindex d30f23267cb..bc0bbf0c7a2 100644--- a/gateway/session_cron.py+++ b/gateway/session_cron.py@@ -106,8 +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+ from gateway.session_authorities import require_serving+ require_serving(authority) # 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.pyindex c4ecbdb79ba..2765d599f22 100644--- a/tests/gateway/test_retired_authority_cron_fence.py+++ b/tests/gateway/test_retired_authority_cron_fence.py@@ -1,11 +1,14 @@-"""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."""+"""A retired profile authority refuses every verb that writes the home it released, 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, and the Bot relay files carry no epoch at all.+Table-driven over the in-class sites of SWEEP_retirement-fence.md."""import asyncio+import json+from types import SimpleNamespaceimport pytest-from gateway import run_runtime, session_cron+from gateway import run_runtime, session_bot, session_cronfrom gateway.session_contract import Principal, Submissionfrom hermes_state_runtime import RuntimeStoreError, list_session_admissionsfrom tests.gateway.test_profile_retire_stops_turns import REF, _Agent, _authority@@ -13,19 +16,50 @@ from tests.gateway.test_profile_retire_stops_turns import REF, _Agent, _authoritCRON = Principal('cron-owner', 'owned', frozenset({'session:submit', 'session:control'}), 'cron-ticker')+async def _cron_cancel(authority, receipt):+ await session_cron.operation(authority, 'cancel', {'session_id': 's', 'admission_id': receipt.admission_id})+++def _relay(operation, params):+ async def call(authority, receipt):+ session_bot.relay_operation(SimpleNamespace(authority=authority, actor=CRON), operation, params)+ return call+++def _outbox(home):+ box = home / 'bot_relay' / 'outbox'+ box.mkdir(parents=True)+ (box / 'e1.json').write_text(json.dumps({'id': 'e1', 'created_at': 9e12, 'target_handle': 'x'}))+++SITES = {+ 'cron.cancel': (_cron_cancel, None, lambda db, home: list_session_admissions(+ db, session_id='s', pending_only=False)[0]['status'] == 'queued'),+ 'bot_relay.outbox.drain': (_relay('outbox', {}), _outbox,+ lambda db, home: (home / 'bot_relay' / 'outbox' / 'e1.json').exists()),+ 'bot_relay.reply': (_relay('reply', {'id': 'e1', 'reply': 'hi'}), None,+ lambda db, home: not (home / 'bot_relay').exists()),+ 'bot_relay.roster.sync': (_relay('roster', {'agents': []}), None,+ lambda db, home: not (home / 'bot_relay').exists()),+}++@pytest.mark.asyncio-async def test_retired_authority_refuses_cron_cancel_before_successor_epoch(tmp_path, monkeypatch):[email protected]('site', sorted(SITES))+async def test_retired_authority_refuses_writes_before_successor_epoch(tmp_path, monkeypatch, site):+ call, arrange, untouched = SITES[site]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 ownertry:with db:receipt = await authority.submit(CRON, Submission(request_id='cron:j:1', ref=REF, payload={'text': ''}, intent='queue'))+ if arrange is not None:+ arrange(tmp_path)assert await run_runtime._retire_profile_authority(authority, timeout=1.0) is Truewith 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'+ await call(authority, receipt)+ assert untouched(db, tmp_path)finally:await asyncio.sleep(.05)db.close()