Skip to content

The agent that grows with you

fix(sms): split long replies under Twilio's 1600-char cap and deliver full cron output

teknium125077f6be827
diff --git a/plugins/platforms/sms/adapter.py b/plugins/platforms/sms/adapter.py
index cfebe932132..0c6cfb91e1f 100644
--- a/plugins/platforms/sms/adapter.py
+++ b/plugins/platforms/sms/adapter.py
@@ -88,6 +88,8 @@ class SmsAdapter(BasePlatformAdapter):
serves_profile_prefix: bool = True
MAX_MESSAGE_LENGTH = MAX_SMS_LENGTH
+ # send() splits at MAX_MESSAGE_LENGTH, so cron delivery hands over the full payload.
+ splits_long_messages = True
def __init__(self, config: PlatformConfig):
super().__init__(config, Platform.SMS)
@@ -163,7 +165,7 @@ class SmsAdapter(BasePlatformAdapter):
url, headers = _messages_endpoint(self._account_sid, self._auth_token)
session = self._http_session or _new_session(trust_env=gateway_trust_env())
try:
- for chunk in self.truncate_message(self.format_message(content)):
+ for chunk in self.truncate_message(self.format_message(content), self.MAX_MESSAGE_LENGTH):
form_data = _twilio_form(self._from_number, chat_id, chunk)
try:
async with session.post(url, data=form_data, headers=headers) as resp:
diff --git a/tests/gateway/test_sms.py b/tests/gateway/test_sms.py
index d0eeae109ea..bf24747df04 100644
--- a/tests/gateway/test_sms.py
+++ b/tests/gateway/test_sms.py
@@ -311,3 +311,40 @@ class TestMultiplexProfileScope:
finally:
reset_secret_scope(token)
assert "TWILIO_PHONE_NUMBER required" in result["error"]
+
+
+async def test_oversized_cron_output_reaches_twilio_in_1600_char_chunks():
+ """The router hands SMS the full cron payload and send() splits it under Twilio's 1600-char
+ cap (a larger Body is rejected with 21617, which used to fail the whole delivery)."""
+ from gateway.config import GatewayConfig
+ from gateway.delivery import DeliveryRouter
+ from plugins.platforms.sms.adapter import SmsAdapter
+
+ with patch.dict(os.environ, {"TWILIO_ACCOUNT_SID": "ACtest", "TWILIO_AUTH_TOKEN": "tok",
+ "TWILIO_PHONE_NUMBER": "+15550001111"}):
+ adapter = SmsAdapter(PlatformConfig(enabled=True, api_key="tok"))
+ bodies = []
+
+ class _Resp:
+ status = 201
+ async def json(self):
+ return {"sid": "SM1"}
+ async def __aenter__(self):
+ return self
+ async def __aexit__(self, *exc):
+ return False
+
+ class _Session:
+ def post(self, url, data, headers):
+ bodies.append(next(value for opts, _, value in data._fields if opts["name"] == "Body"))
+ return _Resp()
+
+ adapter._http_session = _Session()
+ content = "\n\n".join(f"line {i} " + "x" * 200 for i in range(40))
+ payload = DeliveryRouter(GatewayConfig())._cap_oversized_output(adapter, content, "job")
+ result = await adapter.send("+15550002222", payload)
+
+ assert result.success
+ assert len(bodies) > 1 and max(map(len, bodies)) <= 1600
+ assert "line 39 " in bodies[-1]