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

settle tracked its mutation, so retirement waited for a settle already
running, but nothing refused one arriving after retirement had finished.
A stale connection to a retired authority could still write a terminal
not-sent receipt into the home it had released. Those receipt files carry
no runtime epoch and mailbox_lock belongs to the old authority, so nothing
serializes that write with the home's next owner.

settle now runs require_serving (runtime_retired) and
_require_admission_open (runtime_draining) before it registers its
mutation, the same gates deliver and the other relay verbs use. A settle
already tracked when retirement starts is still joined. The Desktop
writer is still not re-checked: it may release legitimately between the
refusal and the settle.

On the tool side, the refusal is an ordinary GatewayRPCError from
settle_held_delivery. _admit_live_dm re-raises it, and the caller answers
"ambiguous ... Do not resend" (the runner gives the unknown outcome) and
keeps the pinned intent, never "NOT sent". The new tests cover both, and
bot_relay.settle joins the table in test_retired_authority_cron_fence.py.

Refs #134889 (MSG-19)
Refs #140208
This commit is contained in:
unsupportedpastels
2026-10-17 14:41:24 +00:00
committed by Mark S.
parent 7f367feaf2
commit e78bcbd5e0
3 changed files with 58 additions and 0 deletions

View File

@@ -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)))

View File

@@ -239,6 +239,48 @@ async def test_a_settled_receipt_states_only_what_settle_serialized(held):
assert _admissions(held) == [] and held.executed == []
@pytest.mark.asyncio
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
@pytest.mark.asyncio
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

View File

@@ -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),
}