Skip to content

The agent that grows with you

fix(gateway): replay a refused Bot delivery only for the same sender and category

unsupportedpastels5b7f7974798e
diff --git a/gateway/session_bot.py b/gateway/session_bot.py
index 1688b89480e..1c5baf17b1d 100644
--- a/gateway/session_bot.py
+++ b/gateway/session_bot.py
@@ -479,6 +479,15 @@ async def deliver(connection, params):
return await asyncio.shield(track_mutation(connection.authority, _deliver(connection, params)))
+def _require_same_delivery(record, actor, message, author, category):
+ """A delivery id names one delivery: the same message, principal, author and category (the
+ mailbox writer's identity), whether its receipt is admission-backed or refused."""
+ if (record['message'] != message or record['principal_id'] != actor.subject
+ or record.get('author') != author
+ or record.get('notification_category', 'result') != category):
+ raise RuntimeStoreError('admission_conflict')
+
+
async def _deliver(connection, params):
authority, actor = connection.authority, connection.actor
home = _home(authority, actor, params.get('profile'))
@@ -506,26 +515,19 @@ async def _deliver(connection, params):
root, record = await read_receipt(home, key)
path = root / f'{key}.json'
if record is not None and record.get('admission_id'):
- if (record['message'] != message or record['principal_id'] != actor.subject
- or record.get('author') != author
- or record.get('notification_category', 'result') != category):
- raise RuntimeStoreError('admission_conflict')
+ _require_same_delivery(record, actor, message, author, category)
authority.authorize(actor, SessionRef(authority.profile_id, record['session_id']), 'session:submit')
await _maybe_retry(authority, home, path, record)
return await _publish_result(authority, home, record)
await _migrate(authority, actor, home, root)
_, record = await read_receipt(home, key)
if record is not None and record.get('admission_id'):
- if (record['message'] != message or record['principal_id'] != actor.subject
- or record.get('author') != author
- or record.get('notification_category', 'result') != category):
- raise RuntimeStoreError('admission_conflict')
+ _require_same_delivery(record, actor, message, author, category)
authority.authorize(actor, SessionRef(authority.profile_id, record['session_id']), 'session:submit')
await _maybe_retry(authority, home, path, record)
return await _publish_result(authority, home, record)
if record is not None and record.get('refused') is True:
- if record['message'] != message or record['principal_id'] != actor.subject:
- raise RuntimeStoreError('admission_conflict')
+ _require_same_delivery(record, actor, message, author, category)
return {k: record[k] for k in ('status', 'delivery_id', 'profile_home', 'session_id', 'message',
'reply', 'error', 'reason')}
if record is not None:
diff --git a/tests/gateway/test_bot_receipt_refusal.py b/tests/gateway/test_bot_receipt_refusal.py
index 866d486deac..6d8558eea69 100644
--- a/tests/gateway/test_bot_receipt_refusal.py
+++ b/tests/gateway/test_bot_receipt_refusal.py
@@ -52,6 +52,31 @@ async def test_refused_delivery_leaves_a_terminal_receipt_the_scan_accepts(bot,
assert list_session_admissions(bot.authority.db, session_id=bot.chat, pending_only=False) == []
+AUTHOR = {'id': 'bot:cto', 'name': 'cto', 'is_bot': True}
+
+
[email protected]('changed', [
+ {'author': {'id': 'bot:cfo', 'name': 'cfo', 'is_bot': True}},
+ {'notification_category': 'diagnostic'},
+ {'message': 'pong'},
+])
+async def test_a_refused_receipt_keeps_the_whole_delivery_identity(bot, monkeypatch, changed):
+ """The refused receipt answers only the delivery it refused: the same id with another sender,
+ category or message conflicts, exactly as an admission-backed receipt does."""
+ from gateway.session_bot import deliver
+ params = dict(id=KEY, profile='default', message='ping', author=AUTHOR)
+ refusing = _refuse(monkeypatch, 'permission_denied')
+ with pytest.raises(RuntimeStoreError, match='permission_denied'):
+ await deliver(bot.connection, params)
+ refusing.clear()
+ with pytest.raises(RuntimeStoreError, match='admission_conflict'):
+ await deliver(bot.connection, {**params, **changed})
+ again = await deliver(bot.connection, params)
+ assert (again['status'], again['reason']) == ('failed', 'agent_blocked'), again
+ assert list_session_admissions(bot.authority.db, session_id=bot.chat, pending_only=False) == []
+
+
@pytest.mark.asyncio
async def test_a_pin_without_refusal_proof_still_warns(bot, monkeypatch, caplog):
"""A refusal raised after the admission row committed is not a refusal: the pin stays the