Skip to content

The agent that grows with you

fix(bot): a peer DM answers only with a reply its receipt already holds

Tekniumd68e40cbedad
diff --git a/gateway/platforms/api_server_bot_chat.py b/gateway/platforms/api_server_bot_chat.py
index 6826f260ad4..8472b676045 100644
--- a/gateway/platforms/api_server_bot_chat.py
+++ b/gateway/platforms/api_server_bot_chat.py
@@ -89,7 +89,10 @@ async def await_peer_receipt(authority, record, timeout, *, should_stop=None):
continue # The derived retry committed while the receipt was being read.
if interim:
current['status'] = 'claimed' # the retry is being admitted
- if live is None or current['status'] not in _PENDING or (should_stop is not None and should_stop()):
+ if live is None or current['status'] not in _PENDING:
+ await _owner_published(authority, record, deadline)
+ return current
+ if should_stop is not None and should_stop():
return current
remaining = None if deadline is None else deadline - loop.time()
if remaining is not None and remaining <= 0:
@@ -101,6 +104,16 @@ async def await_peer_receipt(authority, record, timeout, *, should_stop=None):
await asyncio.wait([waiter], timeout=tick if remaining is None else min(tick, remaining))
+async def _owner_published(authority, record, deadline):
+ """Answer only once the owner's reply watcher published the outcome this waiter read from the
+ FIFO: its receipt write runs off-loop, and a re-read of the delivery must not say ``queued``
+ after the sender already holds the reply."""
+ watcher = getattr(authority, '_bot_reply_watchers', {}).get(record['delivery_id'])
+ if watcher is not None and not watcher[1].done():
+ remaining = None if deadline is None else max(0.0, deadline - asyncio.get_running_loop().time())
+ await asyncio.wait([watcher[1]], timeout=remaining)
+
+
def _receipt(authority, record):
from gateway.session_bot import _result
return {**record, **_result(authority, record)}
diff --git a/gateway/session_bot.py b/gateway/session_bot.py
index 6b83befd671..8b26a44c621 100644
--- a/gateway/session_bot.py
+++ b/gateway/session_bot.py
@@ -455,10 +455,7 @@ async def _deliver(connection, params):
raise RuntimeStoreError('admission_conflict')
authority.authorize(actor, SessionRef(authority.profile_id, record['session_id']), 'session:submit')
await _maybe_retry(authority, home, path, record)
- record.update(_delivery_result(authority, record))
- await write_receipt(home, record)
- remember_receipt(authority, home, record)
- return _delivery_result(authority, 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'):
@@ -468,10 +465,7 @@ async def _deliver(connection, params):
raise RuntimeStoreError('admission_conflict')
authority.authorize(actor, SessionRef(authority.profile_id, record['session_id']), 'session:submit')
await _maybe_retry(authority, home, path, record)
- record.update(_delivery_result(authority, record))
- await write_receipt(home, record)
- remember_receipt(authority, home, record)
- return _delivery_result(authority, record)
+ return await _publish_result(authority, home, record)
if record is not None:
raise RuntimeStoreError('unknown_execution')
ref, live, entry = _target(authority, actor)
@@ -511,6 +505,16 @@ async def _admit(authority, actor, home, root, key, message, ref, live, entry, a
_watch_reply(authority, home, key, receipt.admission_id)
if record['status'] not in {'queued', 'claimed'}:
await _maybe_retry(authority, home, path, record)
+ return await _publish_result(authority, home, record)
+
+
+async def _publish_result(authority, home, record):
+ """Publish the delivery's projection and answer with exactly that projection. The write is
+ off-loop, so the admission may settle while it runs; re-deriving the answer afterwards would
+ hand the sender a reply the receipt file does not hold yet (the reply watcher publishes it
+ once this delivery releases the mailbox lock)."""
+ result = _delivery_result(authority, record)
+ record.update(result)
await write_receipt(home, record)
remember_receipt(authority, home, record)
- return _delivery_result(authority, record)
+ return result