Skip to content

The agent that grows with you

fix(gateway): bot_relay.settle refuses on a retiring or retired authority

unsupportedpastelse78bcbd5e0de
diff --git a/gateway/session_bot.py b/gateway/session_bot.py
index 3c962ff1431..662a86e0b8a 100644
--- a/gateway/session_bot.py
+++ b/gateway/session_bot.py
@@ -697,7 +697,12 @@ async def _deliver(connection, params):
async def settle(connection, params):
+ from gateway.session_authorities import require_serving
from gateway.session_runtime_workers import track_mutation
+ # Retirement joins tracked work but cannot fence a later write: receipts carry no epoch and
+ # mailbox_lock is this authority's own, so a stale connection is refused before it schedules.
+ require_serving(connection.authority)
+ connection.authority._require_admission_open()
return await asyncio.shield(track_mutation(connection.authority, _settle(connection, params)))
diff --git a/tests/gateway/test_bot_dm_held_by_session_writer.py b/tests/gateway/test_bot_dm_held_by_session_writer.py
index 5fd0d6ee3c2..b054d9e1127 100644
--- a/tests/gateway/test_bot_dm_held_by_session_writer.py
+++ b/tests/gateway/test_bot_dm_held_by_session_writer.py
@@ -239,6 +239,48 @@ async def test_a_settled_receipt_states_only_what_settle_serialized(held):
assert _admissions(held) == [] and held.executed == []
+async def test_a_retiring_or_retired_owner_refuses_settle_through_a_retained_connection(held):
+ """Retirement joins a settle it already tracks; one arriving later (a stale connection) must be
+ refused before it writes: receipt files carry no epoch and the mailbox lock is this authority's
+ own, so nothing else keeps it from racing the home's next owner."""
+ from gateway import run_runtime
+ from hermes_cli.gateway_client import GatewayRPCError
+ from tools.bot_live_delivery import find_canonical_live_owner, read_delivery_result, settle_held_delivery
+ owner = find_canonical_live_owner(held.home)
+ held.authority.retiring = True
+ for reason, delivery_id in (('runtime_draining', 'e' * 64), ('runtime_retired', 'f' * 64)):
+ if reason == 'runtime_retired':
+ assert await run_runtime._retire_profile_authority(held.authority, timeout=1.0) is True
+ assert held.authority.retired
+ with pytest.raises(GatewayRPCError) as refused:
+ await asyncio.to_thread(settle_held_delivery, held.home, owner, 'ping', delivery_id=delivery_id)
+ assert str(refused.value) == reason
+ assert read_delivery_result(held.home, delivery_id) is None
+
+
+async def test_a_settle_refused_by_a_retired_owner_keeps_the_unknown_outcome(held, monkeypatch):
+ """The owner refused the DM (writer held), then retired before the tool's settle: that refusal
+ settles nothing, so the tool keeps the intent and gives the ambiguous answer, never 'NOT sent'."""
+ from gateway import run_runtime
+ from tools import bot_live_delivery
+ loop = asyncio.get_running_loop()
+ settle = bot_live_delivery.authority_settle
+
+ def retire_then_settle(home, params):
+ assert asyncio.run_coroutine_threadsafe(
+ run_runtime._retire_profile_authority(held.authority, timeout=1.0), loop).result(30) is True
+ return settle(home, params)
+
+ monkeypatch.setattr(bot_live_delivery, 'authority_settle', retire_then_settle)
+ result = await _send(held, 'ping')
+ assert result.get('status') == 'ambiguous' and 'Do not resend' in result['error'], result
+ assert 'NOT sent' not in result['error'], result
+ assert [path.name for path in held.dm_dir.glob('*.live.json')], 'an unsettled refusal keeps its intent'
+ assert bot_live_delivery.read_delivery_result(held.home, result['delivery_id']) is None
+
+
@pytest.mark.asyncio
async def test_a_refusal_alone_leaves_the_id_open_for_a_same_id_retry(held):
"""Only the Bot DM tool's settle records a not-sent disposition. A producer that retries its
diff --git a/tests/gateway/test_retired_authority_cron_fence.py b/tests/gateway/test_retired_authority_cron_fence.py
index 4cda1bfd73a..e2f88b90627 100644
--- a/tests/gateway/test_retired_authority_cron_fence.py
+++ b/tests/gateway/test_retired_authority_cron_fence.py
@@ -26,6 +26,16 @@ def _relay(operation, params):
return call
+async def _settle(authority, receipt):
+ await session_bot.settle(SimpleNamespace(authority=authority, actor=CRON),
+ {'id': 'e' * 64, 'profile': 'default', 'message': 'ping'})
+
+
+def _no_receipt(db, home):
+ from tools.bot_live_delivery import read_delivery_result
+ return read_delivery_result(home, 'e' * 64) is None
+
+
def _outbox(home):
box = home / 'bot_relay' / 'outbox'
box.mkdir(parents=True)
@@ -61,6 +71,7 @@ SITES = {
lambda db, home: not (home / 'bot_relay').exists()),
'bot_relay.roster.sync': (_relay('roster', {'agents': []}), None,
lambda db, home: not (home / 'bot_relay').exists()),
+ 'bot_relay.settle': (_settle, None, _no_receipt),
}