Skip to content

Your lightweight open-source personal agent.

fix(whatsapp_agent): scope the bad-token heuristic away from media methods

Carmelo Campos4d45564362f7
diff --git a/nanobot/channels/whatsapp_agent/protocol.py b/nanobot/channels/whatsapp_agent/protocol.py
index 08cbcc048..2450efb3e 100644
--- a/nanobot/channels/whatsapp_agent/protocol.py
+++ b/nanobot/channels/whatsapp_agent/protocol.py
@@ -160,6 +160,10 @@ class APIError:
message: str
details: str
fbtrace_id: str
+ # Media endpoints share 400/100 between an invalid token, an unknown or
+ # expired media id, and a length cap, so a 400 there is not treated as an
+ # authentication failure.
+ media_endpoint: bool = False
@property
def invalid_token(self) -> bool:
@@ -167,9 +171,14 @@ class APIError:
A malformed or missing ``Authorization`` header yields 401/190. A token
that is present but not valid yields 400/100 — the API deliberately does
- not use 401 for that case.
+ not use 401 for that case. That heuristic is scoped to endpoints without
+ media, where 400/100 has no other common cause.
"""
- return self.status_code == 401 or (
+ if self.status_code == 401:
+ return True
+ if self.media_endpoint:
+ return False
+ return (
self.status_code == 400
and self.code == CODE_INVALID_TOKEN
and not self._is_length_rejection
@@ -232,7 +241,12 @@ class WhatsAppAgentAPIError(WhatsAppAgentError):
self.error = error
-def classify_error(status_code: int, payload: Any) -> APIError:
+def classify_error(
+ status_code: int,
+ payload: Any,
+ *,
+ media_endpoint: bool = False,
+) -> APIError:
"""Extract the structured error from a non-2xx response body."""
body = _as_dict(payload) or {}
error = _as_dict(body.get("error")) or {}
@@ -251,6 +265,7 @@ def classify_error(status_code: int, payload: Any) -> APIError:
message=_text(error.get("message")),
details=details,
fbtrace_id=_text(error.get("fbtrace_id")),
+ media_endpoint=media_endpoint,
)
diff --git a/nanobot/channels/whatsapp_agent/runtime.py b/nanobot/channels/whatsapp_agent/runtime.py
index 526fc2842..c10fd2216 100644
--- a/nanobot/channels/whatsapp_agent/runtime.py
+++ b/nanobot/channels/whatsapp_agent/runtime.py
@@ -280,7 +280,9 @@ class WhatsAppAgentChannel(BaseChannel):
)
if 200 <= status_code < 300:
return payload
- error = classify_error(status_code, payload)
+ error = classify_error(
+ status_code, payload, media_endpoint=method_key == "media"
+ )
if error.invalid_token:
raise WhatsAppAgentAuthError(str(error))
raise WhatsAppAgentAPIError(error)
diff --git a/nanobot/channels/whatsapp_agent/state.py b/nanobot/channels/whatsapp_agent/state.py
index 2ec32a64b..cc77ee6b0 100644
--- a/nanobot/channels/whatsapp_agent/state.py
+++ b/nanobot/channels/whatsapp_agent/state.py
@@ -10,8 +10,10 @@ explicit, crash-safe store on the channel side:
* Marking a message read may delete it server-side, so a message must be
recorded before its receipt is acknowledged.
-The store keeps the cursor, the recently handled wamids, and accumulated
-contact display names in one atomically replaced JSON file.
+The store keeps the cursor, the recently handled wamids, the most recent
+creator identifier, and outbound delivery receipts in one atomically
+replaced JSON file. The API token is represented only by a SHA-256
+fingerprint, so the file carries no credential material.
"""
from __future__ import annotations
diff --git a/nanobot/channels/whatsapp_agent/tests/test_protocol.py b/nanobot/channels/whatsapp_agent/tests/test_protocol.py
index 66455ba48..c3ffcb135 100644
--- a/nanobot/channels/whatsapp_agent/tests/test_protocol.py
+++ b/nanobot/channels/whatsapp_agent/tests/test_protocol.py
@@ -251,6 +251,24 @@ def test_only_confirmed_non_delivery_is_retryable():
assert fatal.ambiguous is False
+def test_media_endpoint_400_is_not_treated_as_an_invalid_token():
+ # Media methods share 400/100 between a bad token, an unknown or expired
+ # media id, and a length cap, so an expired id must not stop the channel.
+ expired = classify_error(
+ 400, {"error": {"code": 100, "message": "unknown media id"}}, media_endpoint=True
+ )
+ assert expired.invalid_token is False
+
+ # The same response on a non-media endpoint is the documented bad-token case.
+ bad_token = classify_error(
+ 400, {"error": {"code": 100, "message": "(#100) invalid"}}, media_endpoint=False
+ )
+ assert bad_token.invalid_token is True
+
+ # A malformed header is 401 everywhere, including media.
+ assert classify_error(401, {"error": {"code": 190}}, media_endpoint=True).invalid_token is True
+
+
def test_duplicate_poller_is_reported_as_poll_replaced():
error = classify_error(409, {"error": {"code": 1752041}})