Skip to content

The agent that grows with you

fix(bot): the shared Bot door caps relayed bot DMs, not trusted producer reports

Tekniumf10284030c14
diff --git a/gateway/session_bot.py b/gateway/session_bot.py
index abedaaa23d1..b09f630629d 100644
--- a/gateway/session_bot.py
+++ b/gateway/session_bot.py
@@ -21,6 +21,8 @@ from gateway.session_bot_mailbox import (mailbox_lock, read_receipt, write_recei
_CANONICAL_RECEIPT_STATUSES = frozenset({
'canonical', 'queued', 'claimed', 'ambiguous', 'settled', 'failed', 'cancelled',
})
+_BOT_DM_MAX_CHARS = 16200
+_PRODUCER_MAX_CHARS = 1024 * 1024
def _canonical_record_error(path, record):
@@ -459,7 +461,11 @@ async def _deliver(connection, params):
except ValueError as exc:
raise RuntimeStoreError('invalid_params') from exc
message = params.get('message')
- if not isinstance(message, str) or not message.strip() or len(message) > 16200:
+ # Main capped only a relayed bot DM (MESSAGE_MAX_CHARS + attribution headroom), at the relay
+ # edge; a trusted producer's report (cron bot-chat, its deferred drain) carries no author and
+ # was admitted whole, so it gets a resource bound only.
+ limit = _BOT_DM_MAX_CHARS if params.get('author') is not None else _PRODUCER_MAX_CHARS
+ if not isinstance(message, str) or not message.strip() or len(message) > limit:
raise RuntimeStoreError('invalid_params')
author = parse_turn_author(params.get('author'))
if params.get('author') is not None and (not isinstance(params['author'], dict) or author is None):
diff --git a/tests/gateway/test_bot_door_message_cap.py b/tests/gateway/test_bot_door_message_cap.py
new file mode 100644
index 00000000000..6415849615c
--- /dev/null
+++ b/tests/gateway/test_bot_door_message_cap.py
@@ -0,0 +1,38 @@
+"""The shared Bot door caps a relayed bot DM like main's relay edge (16,200), never a trusted
+producer's report: a cron bot-chat digest over that size is admitted whole."""
+import asyncio
+
+import pytest
+
+from tests.waiting import async_wait_until
+
+from gateway.session_contract import Principal
+from hermes_state_runtime import RuntimeStoreError, list_session_admissions
+
+pytest_plugins = ['tests.gateway.test_session_bot_retry']
+
+KEY = 'e' * 32
+
+
+async def test_long_cron_report_is_admitted_and_relayed_dm_stays_capped(bot):
+ from cron.scheduler_delivery import bot_chat_message
+ from gateway.session_bot import deliver
+ from tools.bot_live_delivery import read_delivery_result
+ report = '\n'.join(f'- item {i}: status ok, details recorded' for i in range(480))
+ message = bot_chat_message({'id': 'daily', 'name': 'Daily report'}, report)
+ assert len(message) > 16200
+ await deliver(bot.connection, dict(id=KEY, profile='default', message=message))
+ await async_wait_until(lambda: (read_delivery_result(bot.home, KEY) or {}).get('status') == 'settled',
+ timeout=10, what='the cron report to settle')
+ assert (await asyncio.to_thread(read_delivery_result, bot.home, KEY))['message'] == message
+
+ relayed = Principal('uid:1000', 'default', frozenset({'session:submit', 'session:read'}), 'relay')
+ author = {'id': 'bot:conn/peer', 'name': 'peer', 'is_bot': True}
+ connection = type(bot.connection)(authority=bot.authority, actor=relayed)
+ with pytest.raises(RuntimeStoreError, match='invalid_params'):
+ await deliver(connection, dict(id='f' * 32, profile='default', message='x' * 16201, author=author))
+ ok = await deliver(connection, dict(id='a' * 32, profile='default', message='y' * 16000, author=author))
+ assert ok['admission_id']
+ rows = list_session_admissions(bot.authority.db, session_id=bot.chat, pending_only=False)
+ assert sorted(r['request_id'] for r in rows) == ['bot:' + 'a' * 32, 'bot:' + KEY]