Skip to content

The agent that grows with you

refactor(gateway): one helper answers a Bot Chat id's existing receipt; share the held reason

unsupportedpastelsa096c025cd37
diff --git a/gateway/session_bot.py b/gateway/session_bot.py
index 2940375171f..3c962ff1431 100644
--- a/gateway/session_bot.py
+++ b/gateway/session_bot.py
@@ -15,6 +15,7 @@ from gateway.session_contract import Principal, SessionRef
from gateway.config import Platform
from gateway.platforms.event import MessageEvent
from hermes_state_runtime import RuntimeStoreError, get_session_admission
+from tools.bot_failure_reasons import HELD_NOT_SENT
from tools.bot_live_delivery import _TERMINAL, _delivery_id, _read
from gateway.session_bot_mailbox import (mailbox_lock, read_receipt, write_receipt,
scan_receipts, remember_receipt, track_receipt_task, withdraw_pin, refuse_pin, requeue_pin)
@@ -652,33 +653,38 @@ def _refused_answer(record):
'message', 'reply', 'error', 'reason')}
+async def _receipt_answer(authority, actor, home, record, identity, retry_path=None):
+ """The answer an id's existing receipt gives, or None when it has none yet: an admitted id's
+ published result (after its one retry when ``retry_path`` is given), or a terminal refusal
+ (``refuse_pin``, ``settle``), which is the id's answer for good. Both check the identity."""
+ if record is None or not (record.get('admission_id') or record.get('refused') is True):
+ return None
+ _require_same_delivery(record, actor, *identity)
+ if not record.get('admission_id'):
+ return _refused_answer(record)
+ authority.authorize(actor, SessionRef(authority.profile_id, record['session_id']), 'session:submit')
+ if retry_path is not None:
+ await _maybe_retry(authority, home, retry_path, record)
+ return await _publish_result(authority, home, record)
+
+
async def _deliver(connection, params):
authority, actor = connection.authority, connection.actor
home, key, message, author, category = _delivery_params(authority, actor, params)
+ identity = (message, author, category)
authority._require_admission_open()
async with mailbox_lock(authority):
root, record = await read_receipt(home, key)
path = root / f'{key}.json'
- if record is not None and record.get('admission_id'):
- _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:
- # A terminal disposition (``refuse_pin``, ``settle``) is the id's answer for good.
- _require_same_delivery(record, actor, message, author, category)
- return _refused_answer(record)
+ answer = await _receipt_answer(authority, actor, home, record, identity, retry_path=path)
+ if answer is not None:
+ return answer
try:
await _migrate(authority, actor, home, root)
_, record = await read_receipt(home, key)
- if record is not None and record.get('admission_id'):
- _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:
- _require_same_delivery(record, actor, message, author, category)
- return _refused_answer(record)
+ answer = await _receipt_answer(authority, actor, home, record, identity, retry_path=path)
+ if answer is not None:
+ return answer
if record is not None:
raise RuntimeStoreError('unknown_execution')
ref, live, entry = await _target(authority, actor)
@@ -719,12 +725,10 @@ async def _settle(connection, params):
await write_receipt(home, record)
logging.getLogger(__name__).info('Bot Chat delivery %s settled as not sent (%s)', key, record['reason'])
_require_same_delivery(record, actor, message, author, category)
- if record.get('refused') is True:
- return _refused_answer(record)
- if not record.get('admission_id'):
+ answer = await _receipt_answer(authority, actor, home, record, (message, author, category))
+ if answer is None:
raise RuntimeStoreError('unknown_execution')
- authority.authorize(actor, SessionRef(authority.profile_id, record['session_id']), 'session:submit')
- return await _publish_result(authority, home, record)
+ return answer
def _not_sent_record(authority, actor, home, key, message, author, category):
@@ -738,7 +742,7 @@ def _not_sent_record(authority, actor, home, key, message, author, category):
from gateway.session_automation import automation_notification_metadata
record = dict(delivery_id=key, profile_home=str(home), session_id=authority.db.get_compression_tip(row['id']),
principal_id=actor.subject, message=message, status='failed', refused=True, reply='',
- error='Settled as not sent after a session-writer refusal', reason='runtime_coordination_required')
+ error='Settled as not sent after a session-writer refusal', reason=HELD_NOT_SENT)
record.update(automation_notification_metadata({'notification_category': category}))
if author is not None:
record['author'] = dict(author)
@@ -753,7 +757,7 @@ async def _held_by_another_writer(home, key, exc):
pins one before it admits and withdraws it only when nothing committed. That holds only while
this request has the lock, so ``admitted=False`` is a hint, not the verdict: the sender asks
``settle`` before it reports a no-send. Any other refusal keeps its unknown outcome."""
- if exc.reason != 'runtime_coordination_required' or (await read_receipt(home, key))[1] is not None:
+ if exc.reason != HELD_NOT_SENT or (await read_receipt(home, key))[1] is not None:
return None
held = RuntimeStoreError(exc.reason, field='session_writer',
detail='another session writer holds the Bot Chat; nothing was admitted yet')
diff --git a/tools/bot_failure_reasons.py b/tools/bot_failure_reasons.py
index bf1d0f1d930..a8775fd5036 100644
--- a/tools/bot_failure_reasons.py
+++ b/tools/bot_failure_reasons.py
@@ -39,6 +39,11 @@ ALL_REASONS = frozenset({
TARGET_SCOPE_UNRESOLVED, UNKNOWN,
})
+# Not a bot-turn reason (kept out of ALL_REASONS): the runtime refusal code a Bot Chat delivery id's
+# settled not-sent receipt carries (``gateway/session_bot.py:_not_sent_record``). The Bot DM tool
+# recognises that definite no-send by it, so owner and tool share this one literal.
+HELD_NOT_SENT = "runtime_coordination_required"
+
#: Reasons a supervisor may retry automatically without human intervention.
AUTO_RETRYABLE = frozenset({RUNTIME_OFFLINE, DELIVERY_TIMEOUT, PROVIDER_RATE_LIMIT, PROVIDER_SERVER_ERROR})
diff --git a/tools/bot_mode_dm.py b/tools/bot_mode_dm.py
index 24b8433dd18..1b18e55c905 100644
--- a/tools/bot_mode_dm.py
+++ b/tools/bot_mode_dm.py
@@ -447,9 +447,13 @@ class LiveTargetHeldError(LiveTargetUnavailableError):
disposition, so this is a definite no-send too. A refusal without that settlement stays the
ambiguous outcome."""
- reason = "runtime_coordination_required"
resend = "after that session closes"
+ @property
+ def reason(self) -> str:
+ from tools.bot_failure_reasons import HELD_NOT_SENT
+ return HELD_NOT_SENT
+
def _unavailable_payload(dm_file: str, exc: LiveTargetUnavailableError) -> str:
return json.dumps({"reason": exc.reason, "delivery_id": _dm_delivery_id(dm_file),
@@ -473,7 +477,8 @@ def _settled_no_send(dm_file: str, exc: LiveTargetUnavailableError) -> str:
def _held_disposition(record: dict | None) -> bool:
"""The owner's terminal not-sent receipt for a delivery refused while another writer held the
chat (``gateway/session_bot.py:_not_sent_record``)."""
- return bool(record) and record.get("refused") is True and record.get("reason") == LiveTargetHeldError.reason
+ from tools.bot_failure_reasons import HELD_NOT_SENT
+ return bool(record) and record.get("refused") is True and record.get("reason") == HELD_NOT_SENT
def _admit_live_dm(profile_home: Path | None, dm_file: str, author: Optional[dict] = None) -> dict | None:
@@ -511,13 +516,14 @@ def _admit_live_dm(profile_home: Path | None, dm_file: str, author: Optional[dic
record = read_delivery_result(home, intent["delivery_id"])
if record is None or intent["owner"].get("canonical"):
from hermes_cli.gateway_client import GatewayRPCError
+ from tools.bot_failure_reasons import HELD_NOT_SENT
from tools.bot_live_delivery import settle_held_delivery
identity = dict(delivery_id=intent["delivery_id"], author=intent.get("author"))
try:
record = deliver_to_live_owner(home, intent["owner"], intent["message"], **identity)
except GatewayRPCError as exc:
- if str(exc) != LiveTargetHeldError.reason or exc.data.get("admitted") is not False:
+ if str(exc) != HELD_NOT_SENT or exc.data.get("admitted") is not False:
raise
# Nothing was admitted while that refusal held the owner's lock, but a same-id attempt
# may have admitted since: only the owner's settle decides, under that same lock.