Skip to content

The agent that grows with you

fix(gateway): keep a local Bot delivery's operator access on its one retry

unsupportedpastelsfff965cfed12
diff --git a/gateway/session_bot.py b/gateway/session_bot.py
index 480cb8f33fe..01b51aa3960 100644
--- a/gateway/session_bot.py
+++ b/gateway/session_bot.py
@@ -294,8 +294,12 @@ async def _maybe_retry(authority, home, path, record):
if not _retry_eligible(authority, record):
return
identity = _retry_identity(record['delivery_id'])
- actor = Principal(record['principal_id'], authority.profile_id, frozenset({'session:submit', 'session:read'}),
- 'bot-delivery-retry')
+ # The retry keeps the access its delivery was admitted on: ``operator`` is recorded only when the
+ # transport granted ``session:operator`` (a local delivery into api-peer's Bot Chat), and the
+ # session check below still pins the retry to the admitted target.
+ operator = {'session:operator'} if record.get('operator') is True else set()
+ actor = Principal(record['principal_id'], authority.profile_id,
+ frozenset({'session:submit', 'session:read'} | operator), 'bot-delivery-retry')
try:
authority._require_admission_open()
ref, live, entry = await _target(authority, actor)
@@ -555,6 +559,8 @@ async def _admit(authority, actor, home, root, key, message, ref, live, entry, a
record = dict(legacy or {}, delivery_id=key, profile_home=str(home), session_id=ref.session_id,
principal_id=actor.subject, message=message, status='ambiguous')
record.update(notification)
+ if 'session:operator' in actor.capabilities:
+ record['operator'] = True # admitted on operator access: its one retry keeps it (``_maybe_retry``)
if author is not None:
record['author'] = dict(author)
# Preserve a foreign cancellation or claim that won after our legacy scan.
diff --git a/tests/gateway/test_peer_dm_bot_chat_headless.py b/tests/gateway/test_peer_dm_bot_chat_headless.py
index 6b8c593feed..37164f4bcba 100644
--- a/tests/gateway/test_peer_dm_bot_chat_headless.py
+++ b/tests/gateway/test_peer_dm_bot_chat_headless.py
@@ -206,6 +206,82 @@ async def test_local_bot_relay_delivery_lands_in_the_peer_minted_bot_chat(headle
assert [row[0] for row in titled] == [session_id], 'a second Bot Chat was minted'
+async def test_local_delivery_into_the_peer_minted_bot_chat_keeps_its_transient_retry(headless, monkeypatch):
+ # The local operator's delivery was admitted on ``session:operator`` (the chat is api-peer's);
+ # its one transient-failure retry must be admitted on that same access, not refused.
+ from pathlib import Path
+ from gateway.session_authorities import served_profile_name
+ from gateway.session_bot import deliver
+ from tests.waiting import async_wait_until
+ from tools.bot_live_delivery import read_delivery_result
+ session_id = await _peer_minted(headless)
+ texts = []
+
+ async def execute(authority, ref, row):
+ texts.append(row['payload']['text'])
+ if len(texts) == 1:
+ authority.pending_results[row['admission_id']] = {'result': {
+ 'final_response': '', 'messages': [], 'failed': True,
+ 'error': 'Error code: 429 - rate limit exceeded'}, 'usage': {}}
+ return ''
+ return 'pong'
+
+ monkeypatch.setattr('gateway.session_finite.execute_finite_admission', execute)
+ home = Path(headless.authority.db.db_path).parent.resolve()
+ key = 'b' * 32
+ await deliver(_local_owner(headless.authority),
+ dict(id=key, profile=served_profile_name(home), message='relayed', author=AUTHOR))
+
+ def receipt():
+ return read_delivery_result(home, key) or {}
+
+ await async_wait_until(lambda: receipt().get('retry', {}).get('refused') or receipt().get('status') == 'settled',
+ timeout=10, interval=.05)
+ assert 'refused' not in receipt()['retry'], receipt()
+ assert (receipt()['status'], receipt()['reply']) == ('settled', 'pong'), receipt()
+ assert texts == ['relayed', 'relayed'] and receipt()['session_id'] == session_id
+ assert receipt()['operator'] is True
+
+
+async def test_a_receipt_without_recorded_operator_access_retries_as_before(headless, monkeypatch):
+ # A receipt written before ``operator`` was recorded (or by a non-operator actor) never gains
+ # that access on retry: the retry into api-peer's chat is still refused, nothing is widened.
+ from pathlib import Path
+ from gateway import session_bot_mailbox
+ from gateway.session_authorities import served_profile_name
+ from gateway.session_bot import deliver
+ from tests.waiting import async_wait_until
+ from tools.bot_live_delivery import read_delivery_result
+ await _peer_minted(headless)
+ texts = []
+
+ async def execute(authority, ref, row):
+ texts.append(row['payload']['text'])
+ authority.pending_results[row['admission_id']] = {'result': {
+ 'final_response': '', 'messages': [], 'failed': True,
+ 'error': 'Error code: 429 - rate limit exceeded'}, 'usage': {}}
+ return ''
+
+ real = session_bot_mailbox._write_receipt
+
+ def old_shape(home, record, expect=session_bot_mailbox._UNCHECKED):
+ return real(home, {k: v for k, v in record.items() if k != 'operator'}, expect)
+
+ monkeypatch.setattr(session_bot_mailbox, '_write_receipt', old_shape)
+ monkeypatch.setattr('gateway.session_finite.execute_finite_admission', execute)
+ home = Path(headless.authority.db.db_path).parent.resolve()
+ key = 'd' * 32
+ await deliver(_local_owner(headless.authority),
+ dict(id=key, profile=served_profile_name(home), message='relayed', author=AUTHOR))
+ await async_wait_until(lambda: (read_delivery_result(home, key) or {}).get('retry', {}).get('refused'),
+ timeout=10, interval=.05)
+ receipt = read_delivery_result(home, key)
+ assert 'operator' not in receipt and receipt['retry']['refused'] == 'permission_denied', receipt
+ assert texts == ['relayed']
+
+
@pytest.mark.asyncio
async def test_peer_minted_bot_chat_is_the_local_rosters_canonical_session(headless):
from pathlib import Path