Skip to content

The agent that grows with you

fix(gateway): Weixin tokenless re-send no longer consumes the retry budget

teknium12995063bcdc8
diff --git a/gateway/platforms/weixin.py b/gateway/platforms/weixin.py
index e5b1623c430..f673b01015e 100644
--- a/gateway/platforms/weixin.py
+++ b/gateway/platforms/weixin.py
@@ -974,7 +974,8 @@ class WeixinAdapter(OwnAccessPolicyMixin, BasePlatformAdapter):
async with self._send_text_gate:
last_error: Optional[Exception] = None
retried_without_token = False
- for attempt in range(self._send_chunk_retries + 1):
+ attempt = 0 # counts real failures only — the tokenless re-send must not eat the retry budget
+ while True:
if self._rate_limit_cooldown_remaining() > 0:
raise RuntimeError(f"iLink sendmessage rate limited; cooldown active for {self._rate_limit_cooldown_remaining():.1f}s")
try:
@@ -999,6 +1000,7 @@ class WeixinAdapter(OwnAccessPolicyMixin, BasePlatformAdapter):
break
if attempt >= self._send_chunk_retries:
break
+ attempt += 1
wait = self._send_chunk_retry_delay_seconds * 3 # 3x backoff for rate limit
logger.warning("[%s] rate limited for %s; backing off %.1fs before retry", self.name, _safe_id(chat_id), wait)
await asyncio.sleep(wait)
@@ -1010,9 +1012,10 @@ class WeixinAdapter(OwnAccessPolicyMixin, BasePlatformAdapter):
last_error = exc
if attempt >= self._send_chunk_retries:
break
- wait = self._send_chunk_retry_delay_seconds * (attempt + 1)
+ attempt += 1
+ wait = self._send_chunk_retry_delay_seconds * attempt
logger.warning("[%s] send chunk failed to=%s attempt=%d/%d, retrying in %.2fs: %s",
- self.name, _safe_id(chat_id), attempt + 1, self._send_chunk_retries + 1, wait, exc)
+ self.name, _safe_id(chat_id), attempt, self._send_chunk_retries + 1, wait, exc)
if wait > 0:
await asyncio.sleep(wait)
assert last_error is not None
diff --git a/tests/gateway/test_weixin.py b/tests/gateway/test_weixin.py
index 539e16fbd05..1489d8f630b 100644
--- a/tests/gateway/test_weixin.py
+++ b/tests/gateway/test_weixin.py
@@ -337,6 +337,19 @@ class TestWeixinChunkDelivery:
assert [call.kwargs["context_token"] for call in send_message_mock.await_args_list] == ["ctx-token", None]
assert adapter._rate_limit_circuit_until == 0.0
+ @patch("gateway.platforms.weixin._send_message", new_callable=AsyncMock)
+ def test_tokenless_resend_does_not_consume_retry_budget(self, send_message_mock):
+ """With ``send_chunk_retries=0`` the stale-session re-send must still happen: it is a different payload,
+ not a failed attempt, so it must not eat the (only) retry slot and fall out of the loop (#112709)."""
+ adapter = self._connected_adapter()
+ adapter._send_chunk_retries = 0
+ send_message_mock.side_effect = [{"ret": weixin.SESSION_EXPIRED_ERRCODE, "errmsg": "session expired"}, {"ret": 0}]
+
+ result = asyncio.run(adapter.send("wxid_test123", "hello"))
+
+ assert result.success is True
+ assert [call.kwargs["context_token"] for call in send_message_mock.await_args_list] == ["ctx-token", None]
+
class TestWeixinOutboundMedia:
diff --git a/website/docs/user-guide/messaging/weixin.md b/website/docs/user-guide/messaging/weixin.md
index b38ee4a472d..3b7600d73e4 100644
--- a/website/docs/user-guide/messaging/weixin.md
+++ b/website/docs/user-guide/messaging/weixin.md
@@ -322,6 +322,7 @@ Only one Weixin gateway instance can use a given token at a time. The adapter ac
| `Weixin startup failed: WEIXIN_ACCOUNT_ID is required` | Set `WEIXIN_ACCOUNT_ID` in your `.env` or run `hermes gateway setup` |
| `Another local Hermes gateway is already using this Weixin token` | Stop the other gateway instance first — only one poller per token is allowed |
| Session expired (`errcode=-14`) | Your login session has expired. Re-run `hermes gateway setup` to scan a new QR code |
+| Proactive send (cron / notification) fails with `ret=-2 errmsg=prepare failed` or `unknown error` | The peer's `context_token` went stale (no recent inbound message from them). The adapter treats this as a stale session — not a rate limit — and re-sends once without the token, so the message still arrives. Only other `-2` responses trigger the rate-limit backoff/cooldown |
| QR code expired during setup | The QR auto-refreshes up to 3 times. If it keeps expiring, check your network connection |
| Bot doesn't respond to DMs | Check `WEIXIN_DM_POLICY` — if set to `allowlist`, the sender must be in `WEIXIN_ALLOWED_USERS` |
| Bot ignores group messages | Group policy defaults to `disabled`. Set `WEIXIN_GROUP_POLICY=open` or `allowlist` — but note that QR-login iLink bot identities (`[email protected]`) typically cannot receive ordinary WeChat group messages at all. If the gateway logs show no raw inbound events for group messages, the limitation is on the iLink side, not in Hermes. |