AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): log why MailerLite refused a backfill write (#15045)
Nicholas Tindle169582743007
diff --git a/autogpt_platform/backend/backend/notifications/mailerlite_backfill.py b/autogpt_platform/backend/backend/notifications/mailerlite_backfill.pyindex e408b3ae58..7a30c462b3 100644--- a/autogpt_platform/backend/backend/notifications/mailerlite_backfill.py+++ b/autogpt_platform/backend/backend/notifications/mailerlite_backfill.py@@ -16,7 +16,9 @@ finds nothing to do and a failed call is simply picked up by the next run.import asyncioimport logging+import refrom enum import Enum+from typing import Anyfrom urllib.parse import urlencodefrom pydantic import BaseModel@@ -121,6 +123,14 @@ class ApplyResult(BaseModel):failed: dict[Decision, int]+class BatchAnswer(BaseModel):+ """MailerLite's answer to one call in a /batch request."""++ code: int+ # Why it was refused, when it was: see `_failure_reason`.+ body: Any = None++def classify(subscriptions: list[Subscription]) -> Standing:"""A customer who pays on any subscription is a subscriber, whatever thestate of their others."""@@ -205,11 +215,11 @@ async def apply(changes: list[PlannedChange], audience: Audience) -> ApplyResultfor index, (decision, chunk) in enumerate(batches):if index:await asyncio.sleep(_interval_before(decision))- codes = await _send_batch(+ answers = await _send_batch([_call_for(decision, c.customer.email, audience) for c in chunk])- for change, code in zip(chunk, codes):- _record(result, decision, change, code)+ for change, answer in zip(chunk, answers):+ _record(result, decision, change, answer)return result@@ -252,23 +262,62 @@ def _upsert(email: str, group_id: str) -> dict:def _record(- result: ApplyResult, decision: Decision, change: PlannedChange, code: int+ result: ApplyResult, decision: Decision, change: PlannedChange, answer: BatchAnswer) -> None:# 404 on a removal means they already left, which is the desired state.- ok = code in (200, 201, 202, 204) or (- code == 404 and decision in (Decision.REMOVE_CHANGELOG, Decision.REMOVE_TRIAL)+ ok = answer.code in (200, 201, 202, 204) or (+ answer.code == 404+ and decision in (Decision.REMOVE_CHANGELOG, Decision.REMOVE_TRIAL))if ok:result.succeeded[decision] += 1returnresult.failed[decision] += 1logger.warning(- f"{decision.value} failed for {_pseudonym(change.customer.email)} "- f"with {code}; the next run retries it"+ f"{decision.value} failed for {_refusal(change.customer.email, answer)}; "+ "the next run retries it")-async def _send_batch(requests: list[dict]) -> list[int]:+def _refusal(email: str, answer: BatchAnswer) -> str:+ """A refused call, for the log: the pseudonym, the top-level domain and+ MailerLite's reason. Enough to spot a pattern without naming anyone."""+ return (+ f"{_pseudonym(email)} at {_top_level_domain(email)} with {answer.code} "+ f"({_failure_reason(answer.body, email)})"+ )+++# Labels that sit under a country code as part of its suffix: .co.uk, .com.au.+_SECOND_LEVEL = {"ac", "co", "com", "edu", "gov", "net", "org"}+++def _top_level_domain(email: str) -> str:+ labels = email.strip().lower().rpartition("@")[2].split(".")+ if len(labels) < 2 or not labels[-1]:+ return "no TLD"+ if len(labels) >= 3 and len(labels[-1]) == 2 and labels[-2] in _SECOND_LEVEL:+ return f".{labels[-2]}.{labels[-1]}"+ return f".{labels[-1]}"+++def _failure_reason(body: Any, email: str) -> str:+ """What MailerLite said was wrong with a refused call: its message and+ per-field errors. The address is swapped for its pseudonym in case+ MailerLite echoed it back."""+ if not isinstance(body, dict):+ return "no reason given"+ parts = [str(body["message"])] if body.get("message") else []+ errors = body.get("errors")+ if isinstance(errors, dict):+ for field, problems in errors.items():+ listed = problems if isinstance(problems, list) else [problems]+ parts.append(f"{field}: {'; '.join(str(p) for p in listed)}")+ reason = " | ".join(parts) or "no reason given"+ return re.sub(re.escape(email.strip()), _pseudonym(email), reason, flags=re.I)+++async def _send_batch(requests: list[dict]) -> list[BatchAnswer]:"""One /batch call. Requests retries a 429 with backoff before this seesit, so a status here is final."""response = await _client().post(@@ -281,7 +330,9 @@ async def _send_batch(requests: list[dict]) -> list[int]:raise MailerLiteError(f"MailerLite answered {len(responses)} of {len(requests)} batched calls")- return [int(r.get("code") or 0) for r in responses]+ return [+ BatchAnswer(code=int(r.get("code") or 0), body=r.get("body")) for r in responses+ ]async def _read_group(group_id: str) -> dict[str, str]:diff --git a/autogpt_platform/backend/backend/notifications/mailerlite_backfill_test.py b/autogpt_platform/backend/backend/notifications/mailerlite_backfill_test.pyindex ef80622fae..c26244ac73 100644--- a/autogpt_platform/backend/backend/notifications/mailerlite_backfill_test.py+++ b/autogpt_platform/backend/backend/notifications/mailerlite_backfill_test.py@@ -253,6 +253,13 @@ async def test_apply_paces_batches_for_the_import_limit(] * 2+# MailerLite's validation error, echoing the address back in another case.+_REFUSAL = {+ "message": "The given data was invalid.",+ "errors": {"email": ["[email protected] is not a deliverable address."]},+}++@pytest.mark.asyncioasync def test_apply_counts_failures_and_treats_gone_as_removed(configured, no_sleep, monkeypatch, caplog@@ -260,7 +267,7 @@ async def test_apply_counts_failures_and_treats_gone_as_removed(client = MagicMock()client.post = AsyncMock(side_effect=[- _response(200, {"responses": [{"code": 422}]}),+ _response(200, {"responses": [{"code": 422, "body": _REFUSAL}]}),_response(200, {"responses": [{"code": 404}]}),])@@ -274,8 +281,32 @@ async def test_apply_counts_failures_and_treats_gone_as_removed(assert result.failed[Decision.ADD_CHANGELOG] == 1assert result.succeeded[Decision.REMOVE_CHANGELOG] == 1- assert "[email protected]" not in caplog.text- assert mailerlite._pseudonym("[email protected]") in caplog.text+ assert "[email protected]" not in caplog.text.lower()+ assert f"{mailerlite._pseudonym('[email protected]')} at .io with 422" in caplog.text+ assert "The given data was invalid." in caplog.text+ assert "is not a deliverable address" in caplog.text+++def test_failure_reason_without_a_body():+ assert mailerlite_backfill._failure_reason(None, "[email protected]") == "no reason given"+ assert mailerlite_backfill._failure_reason({}, "[email protected]") == "no reason given"+++ "email, expected",+ [+ ("[email protected]", ".com"),+ ("[email protected]", ".co"),+ ("[email protected]", ".fart"),+ ("[email protected]", ".co.uk"),+ ("[email protected] ", ".com.au"),+ ("[email protected]", ".uk"),+ ("a@localhost", "no TLD"),+ ("a@example.", "no TLD"),+ ],+)+def test_top_level_domain(email, expected):+ assert mailerlite_backfill._top_level_domain(email) == expected@pytest.mark.asynciodiff --git a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.py b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.pyindex d83060a0d7..e03bd53331 100644--- a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.py+++ b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.py@@ -25,7 +25,6 @@ from backend.notifications.mailerlite import (MailerLiteError,_client,_headers,- _pseudonym,_require_token,)from backend.notifications.mailerlite_backfill import (@@ -34,6 +33,7 @@ from backend.notifications.mailerlite_backfill import (PAGE_SIZE,UPSERT_BATCH_INTERVAL_SECONDS,Subscription,+ _refusal,_send_batch,next_cursor,)@@ -183,15 +183,15 @@ async def apply(if start:await asyncio.sleep(UPSERT_BATCH_INTERVAL_SECONDS)chunk = changes[start : start + BATCH_SIZE]- codes = await _send_batch([_upsert(c) for c in chunk])- for change, code in zip(chunk, codes):- if code in (200, 201, 202, 204):+ answers = await _send_batch([_upsert(c) for c in chunk])+ for change, answer in zip(chunk, answers):+ if answer.code in (200, 201, 202, 204):succeeded += 1continuefailed += 1logger.warning(- f"Field update failed for {_pseudonym(change.person.email)} "- f"with {code}; the next run retries it"+ f"Field update failed for {_refusal(change.person.email, answer)}; "+ "the next run retries it")if on_progress:on_progress(start + len(chunk), len(changes))diff --git a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.py b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.pyindex 4680c1816d..b8d1fca513 100644--- a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.py+++ b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.py@@ -206,12 +206,21 @@ async def test_apply_counts_a_refused_upsert_without_logging_the_address(configured, monkeypatch, caplog):client = MagicMock()- client.post = AsyncMock(return_value=_response(200, {"responses": [{"code": 422}]}))+ refusal = {+ "message": "The given data was invalid.",+ "errors": {"email": ["[email protected] is not a deliverable address."]},+ }+ client.post = AsyncMock(+ return_value=_response(200, {"responses": [{"code": 422, "body": refusal}]})+ )monkeypatch.setattr(mailerlite_backfill, "_client", lambda: client)changes = backfill.plan([_person("[email protected]")], {}).changesassert await backfill.apply(changes) == (0, 1)assert "[email protected]" not in caplog.text+ assert " at .io with 422 " in caplog.text+ assert "The given data was invalid." in caplog.text+ assert "is not a deliverable address" in caplog.text@pytest.mark.asyncio