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.
feat(backend): backfill existing subscribers into MailerLite and add a trial group (#15026)
Bentlybf6e0c33d0f6
diff --git a/autogpt_platform/backend/.env.default b/autogpt_platform/backend/.env.defaultindex c384361a6f..811bc63153 100644--- a/autogpt_platform/backend/.env.default+++ b/autogpt_platform/backend/.env.default@@ -259,6 +259,8 @@ ADMIN_PANEL_BASE_URL=https://admin.agpt.coMAILERLITE_API_TOKEN=MAILERLITE_ONBOARDING_GROUP_ID=MAILERLITE_CHANGELOG_GROUP_ID=+# Customers in a card-required trial. Blank keeps trials out of MailerLite.+MAILERLITE_TRIAL_GROUP_ID=# Error TrackingSENTRY_DSN=diff --git a/autogpt_platform/backend/backend/cli/mailerlite_backfill.py b/autogpt_platform/backend/backend/cli/mailerlite_backfill.pynew file mode 100644index 0000000000..0044957a81--- /dev/null+++ b/autogpt_platform/backend/backend/cli/mailerlite_backfill.py@@ -0,0 +1,256 @@+import asyncio+import logging+from typing import TYPE_CHECKING++import click++if TYPE_CHECKING:+ from backend.notifications.mailerlite_backfill import (+ Customer,+ PlannedChange,+ Subscription,+ )+ from backend.notifications.mailerlite_field_backfill import FieldPlan, Person++_ACCOUNT_PAGE = 5000++[email protected](name="mailerlite-backfill")[email protected]("--apply", is_flag=True, help="Write the changes. Without it, dry run.")[email protected]("--yes", is_flag=True, help="With --apply, skip the confirmation.")+ "--fields-only",+ is_flag=True,+ help="Only the status and date fields; leave group membership alone.",+)+def mailerlite_backfill_command(apply: bool, yes: bool, fields_only: bool):+ """Put existing accounts where the live MailerLite code would have.++ Groups: paying customers join the changelog unless they are in the+ onboarding tour; churned customers leave it. When+ MAILERLITE_TRIAL_GROUP_ID is set, the trial group ends up holding exactly+ the customers on a trial that is not set to cancel.++ Fields: every account, with or without a Stripe customer, gets its+ subscription_status and dates. An account MailerLite does not have yet is+ created as a subscriber.++ Dry run by default: prints counts and one pseudonymised line per customer,+ and writes nothing. Idempotent, so a partial or repeated --apply is safe,+ and running it again resumes an interrupted one.+ """+ # Keep Prisma and client chatter out of the report.+ logging.disable(logging.INFO)+ asyncio.run(_run(apply=apply, yes=yes, fields_only=fields_only))++[email protected](name="mailerlite-fields")[email protected]("--apply", is_flag=True, help="Create the missing fields.")+def mailerlite_fields_command(apply: bool):+ """Create the MailerLite subscriber fields the backend writes, if missing.++ The notification service also creates them before its first field write,+ so this only sets them up ahead of time. Idempotent.+ """+ logging.disable(logging.INFO)+ asyncio.run(_fields(apply=apply))+++async def _fields(*, apply: bool) -> None:+ from backend.notifications import mailerlite+ from backend.notifications.mailerlite import FIELD_TYPES, MailerLiteNotConfigured++ try:+ mailerlite._require_token()+ existing = await mailerlite.read_fields()+ except MailerLiteNotConfigured as e:+ raise click.ClickException(str(e))+ for field, kind in FIELD_TYPES.items():+ held = existing.get(field.value)+ if held is None:+ state = "missing"+ elif held != kind:+ state = f"exists as {held}, expected {kind}"+ else:+ state = "ok"+ click.echo(f"{field.value:<28} {kind:<5} {state}")+ if not apply:+ click.echo("\nDry run: nothing was created. Re-run with --apply to create.")+ return+ created = await mailerlite.ensure_fields()+ click.echo(f"\nCreated {len(created)} fields.")+++async def _run(*, apply: bool, yes: bool, fields_only: bool) -> None:+ from backend.data.db import connect, disconnect+ from backend.notifications import mailerlite, mailerlite_backfill+ from backend.notifications import mailerlite_field_backfill as field_backfill+ from backend.notifications.mailerlite import MailerLiteNotConfigured+ from backend.notifications.mailerlite_backfill import CHANGES+ from backend.util.settings import Settings++ if not Settings().secrets.stripe_api_key:+ raise click.ClickException("STRIPE_API_KEY is not set.")+ try:+ audience = None if fields_only else await mailerlite_backfill.read_audience()+ current = await field_backfill.read_current()+ except MailerLiteNotConfigured as e:+ raise click.ClickException(str(e))+ subscriptions = await _stripe_subscriptions()+ await connect()+ try:+ people = await _people(subscriptions)+ finally:+ await disconnect()++ changes: "list[PlannedChange]" = []+ if audience is not None:+ customers = [_customer(p) for p in people if p.subscriptions]+ changes = mailerlite_backfill.plan(customers, audience)+ _report(changes, len(subscriptions) - len(customers))+ fields = field_backfill.plan(people, current)+ _report_fields(fields, len(people))++ if not apply:+ click.echo("\nDry run: nothing was written. Re-run with --apply to write.")+ return+ due = sum(d in CHANGES for c in changes for d in c.decisions)+ if not due and not fields.changes:+ click.echo("\nNothing to write.")+ return+ if not yes:+ click.confirm(+ f"\nWrite {due} group changes and {len(fields.changes)} field updates?",+ abort=True,+ )+ if due and audience is not None:+ result = await mailerlite_backfill.apply(changes, audience)+ for decision in CHANGES:+ click.echo(+ f"{decision.value}: {result.succeeded[decision]} ok, "+ f"{result.failed[decision]} failed"+ )+ if fields.changes:+ await mailerlite.ensure_fields()+ ok, failed = await field_backfill.apply(fields.changes, _progress)+ click.echo(f"fields: {ok} ok, {failed} failed")+++async def _stripe_subscriptions() -> "dict[str, list[Subscription]]":+ """Every Stripe subscription, by customer ID."""+ import stripe++ from backend.data.stripe_client import stripe_call, stripe_list_items+ from backend.notifications.mailerlite_backfill import Subscription+ from backend.util.settings import Settings++ stripe.api_key = Settings().secrets.stripe_api_key+ subscriptions: dict[str, list[Subscription]] = {}+ page = await stripe_call(stripe.Subscription.list_async, status="all", limit=100)+ async for sub in stripe_list_items(page):+ # Not expanded, so this is the customer ID.+ subscriptions.setdefault(str(sub.customer), []).append(+ Subscription(+ id=str(sub.id),+ status=str(sub.status),+ cancel_at_period_end=bool(sub.get("cancel_at_period_end")),+ from_trial=bool((sub.get("metadata") or {}).get("trial_enrollment_id")),+ start_date=sub.get("start_date"),+ trial_start=sub.get("trial_start"),+ trial_end=sub.get("trial_end"),+ canceled_at=sub.get("canceled_at"),+ ended_at=sub.get("ended_at"),+ )+ )+ return subscriptions+++async def _people(subscriptions: "dict[str, list[Subscription]]") -> "list[Person]":+ """Every account, paged, with its Stripe subscriptions. The account's+ email is used, as the live handlers do, never Stripe's."""+ import prisma.models++ from backend.notifications.mailerlite_field_backfill import Person++ converted = {+ t.stripeSubscriptionId+ for t in await prisma.models.SubscriptionTrial.prisma().find_many(+ where={"convertedAt": {"not": None}}+ )+ if t.stripeSubscriptionId+ }+ people: list[Person] = []+ cursor: str | None = None+ while True:+ page = await prisma.models.User.prisma().find_many(+ take=_ACCOUNT_PAGE,+ order={"id": "asc"},+ **({"cursor": {"id": cursor}, "skip": 1} if cursor else {}),+ )+ for user in page:+ subs = subscriptions.get(user.stripeCustomerId or "", [])+ people.append(+ Person(+ user_id=user.id,+ email=user.email,+ created_at=user.createdAt,+ subscriptions=[+ s.model_copy(update={"converted": s.id in converted})+ for s in subs+ ],+ )+ )+ if len(page) < _ACCOUNT_PAGE:+ return people+ cursor = page[-1].id+++def _customer(person: "Person") -> "Customer":+ from backend.notifications.mailerlite_backfill import Customer++ return Customer(+ user_id=person.user_id,+ email=person.email,+ subscriptions=person.subscriptions,+ )+++def _report(changes: "list[PlannedChange]", unmatched: int) -> None:+ from collections import Counter++ from backend.notifications.mailerlite import _pseudonym++ for change in changes:+ click.echo(+ f"{_pseudonym(change.customer.email)} {change.standing.value:<16} "+ + ", ".join(d.value for d in change.decisions)+ )+ counts = Counter(d.value for c in changes for d in c.decisions)+ click.echo(f"\n{len(changes)} customers with an account")+ click.echo(f"{unmatched} Stripe customers with no account (skipped)")+ for name, count in sorted(counts.items()):+ click.echo(f" {name}: {count}")+++def _report_fields(plan: "FieldPlan", accounts: int) -> None:+ from backend.notifications.mailerlite_backfill import (+ BATCH_SIZE,+ UPSERT_BATCH_INTERVAL_SECONDS,+ )++ click.echo(f"\nFields for {accounts} accounts")+ for status, count in plan.statuses.items():+ click.echo(f" {status.value}: {count}")+ click.echo(f" invalid email (skipped): {plan.invalid}")+ new = sum(c.new for c in plan.changes)+ click.echo(+ f"{len(plan.changes)} to write: {new} new MailerLite subscribers, "+ f"{len(plan.changes) - new} existing ones updated"+ )+ batches = -(-len(plan.changes) // BATCH_SIZE)+ minutes = batches * UPSERT_BATCH_INTERVAL_SECONDS / 60+ click.echo(f"About {batches} batches, {minutes:.0f} minutes at the import limit")+++def _progress(done: int, total: int) -> None:+ click.echo(f"fields: {done}/{total} written")diff --git a/autogpt_platform/backend/backend/cli/main.py b/autogpt_platform/backend/backend/cli/main.pyindex f190466948..d7c8a753dd 100644--- a/autogpt_platform/backend/backend/cli/main.py+++ b/autogpt_platform/backend/backend/cli/main.py@@ -9,6 +9,7 @@ import psutilfrom backend.util.process import AppProcessfrom .chat import chat+from .mailerlite_backfill import mailerlite_backfill_command, mailerlite_fields_commandfrom .rotate_key import rotate_encryption_keyfrom .store import storefrom .test import test@@ -21,6 +22,8 @@ def main():main.add_command(chat)+main.add_command(mailerlite_backfill_command)+main.add_command(mailerlite_fields_command)main.add_command(rotate_encryption_key)main.add_command(store)main.add_command(test)diff --git a/autogpt_platform/backend/backend/data/notifications.py b/autogpt_platform/backend/backend/data/notifications.pyindex 40382d894c..19430e6ff5 100644--- a/autogpt_platform/backend/backend/data/notifications.py+++ b/autogpt_platform/backend/backend/data/notifications.py@@ -411,11 +411,42 @@ class NotificationResult(BaseModel):class AudienceAction(Enum):"""Membership changes the backend owns. The tour → changelog handoff is- deliberately absent: MailerLite's automation owns that edge."""+ deliberately absent: MailerLite's automation owns that edge. Both edges of+ the trial group are the backend's."""ENROLL_TOUR = "enroll_tour"ADD_CHANGELOG = "add_changelog"REMOVE_CHANGELOG = "remove_changelog"+ ADD_TRIAL = "add_trial"+ REMOVE_TRIAL = "remove_trial"+ # No group change: only the subscriber's fields.+ UPDATE_FIELDS = "update_fields"+ # A new account's fields. Its `signed` never replaces a status already+ # held (see `mailerlite.record_signup`).+ SIGNUP = "signup"+++class SubscriberField(str, Enum):+ """MailerLite custom field keys the backend writes. GTM segments on these,+ so a key is renamed only together with MailerLite."""++ STATUS = "subscription_status"+ SIGNUP = "signup_date"+ TRIAL_STARTED = "trial_started_date"+ SUBSCRIPTION_STARTED = "subscription_started_date"+ SUBSCRIPTION_CANCELED = "subscription_canceled_date"+ SUBSCRIPTION_ENDED = "subscription_ended_date"+++class SubscriptionStatus(str, Enum):+ """Exactly one of these is a subscriber's `subscription_status`."""++ SIGNED = "signed"+ IN_TRIAL = "in_trial"+ TRIAL_CANCELED = "trial_canceled"+ SUBSCRIBED = "subscribed"+ SUBSCRIPTION_CANCELED = "subscription_canceled"+ SUBSCRIPTION_ENDED = "subscription_ended"class AudienceEventModel(BaseModel):@@ -425,6 +456,10 @@ class AudienceEventModel(BaseModel):action: AudienceActionemail: EmailStruser_id: str+ # Written with the group change, or alone for UPDATE_FIELDS and SIGNUP. A+ # date is YYYY-MM-DD; None clears the field, and a field left out is+ # untouched.+ fields: dict[SubscriberField, str | None] = Field(default_factory=dict)class NotificationPreference(BaseModel):diff --git a/autogpt_platform/backend/backend/data/user.py b/autogpt_platform/backend/backend/data/user.pyindex 76c37de307..ce5f4e9521 100644--- a/autogpt_platform/backend/backend/data/user.py+++ b/autogpt_platform/backend/backend/data/user.py@@ -36,6 +36,7 @@ from backend.data.notifications import NotificationPreference, NotificationPrefefrom backend.data.org_migration import ensure_personal_orgfrom backend.data.posthog_lifecycle_sync import schedule_posthog_lifecycle_syncfrom backend.data.subscription_trial import get_subscription_trial+from backend.notifications.subscriber_fields import queue_signupfrom backend.util.cache import cachedfrom backend.util.encryption import JSONCryptorfrom backend.util.exceptions import DatabaseError, NotFoundError@@ -88,6 +89,7 @@ async def _get_or_create_user(user_data: dict) -> UserCreationResult:))was_created = True+ _sync_signup(user)else:was_created = False@@ -125,6 +127,21 @@ async def _get_or_create_user(user_data: dict) -> UserCreationResult:) from e+_signup_sync_tasks: set[asyncio.Task] = set()+++def _sync_signup(user: PrismaUser) -> None:+ """Queue the new account for MailerLite in the background, so signup never+ waits on the broker, and never fails because of it: the backfill catches+ anyone this misses."""+ try:+ task = asyncio.create_task(queue_signup(user.id, user.email, user.createdAt))+ _signup_sync_tasks.add(task)+ task.add_done_callback(_signup_sync_tasks.discard)+ except Exception:+ logger.warning(f"Could not queue the MailerLite signup for {user.id}")++# Word lists mirror the legacy generate_username() SQL function so that app-# and DB-generated handles look consistent._PROFILE_USERNAME_ADJECTIVES = (diff --git a/autogpt_platform/backend/backend/notifications/consumer_test.py b/autogpt_platform/backend/backend/notifications/consumer_test.pyindex 82500c291b..141ea7daa2 100644--- a/autogpt_platform/backend/backend/notifications/consumer_test.py+++ b/autogpt_platform/backend/backend/notifications/consumer_test.py@@ -571,12 +571,12 @@ async def test_audience_changes_for_one_email_keep_their_published_order():manager = _manager()applied: list[str] = []- async def remove(_: str) -> None:+ async def remove(_: str, __: object = None) -> None:# A removal is a lookup and then a delete; an add is one call.await asyncio.sleep(0.02)applied.append("remove")- async def add(_: str) -> None:+ async def add(_: str, __: object = None) -> None:applied.append("add")messages = [@@ -585,6 +585,7 @@ async def test_audience_changes_for_one_email_keep_their_published_order():]with (+ patch.object(delivery.mailerlite, "configured", return_value=True),patch.object(delivery.mailerlite, "remove_from_changelog", remove),patch.object(delivery.mailerlite, "add_to_changelog", add),):diff --git a/autogpt_platform/backend/backend/notifications/lifecycle.py b/autogpt_platform/backend/backend/notifications/lifecycle.pyindex cb63eb0a07..77c4b4c4a3 100644--- a/autogpt_platform/backend/backend/notifications/lifecycle.py+++ b/autogpt_platform/backend/backend/notifications/lifecycle.py@@ -16,7 +16,6 @@ from prisma.enums import NotificationTypefrom backend.data.notifications import (AudienceAction,- AudienceEventModel,NotificationEventModel,PaymentFailedData,PaymentFinalNoticeData,@@ -27,6 +26,7 @@ from backend.data.notifications import ()from backend.data.stripe_client import stripe_callfrom backend.data.user import BillingEmailRecipient+from backend.notifications import subscriber_fieldsfrom backend.notifications.dedupe import claim_once, release_claimfrom backend.notifications.lifecycle_plan import (card_from_invoice,@@ -36,6 +36,7 @@ from backend.notifications.lifecycle_plan import (plan_from_subscription,)from backend.notifications.queue import queue_audience_change, queue_notification_async+from backend.notifications.subscriber_fields import audience_eventfrom backend.notifications.trial import notify_trial, on_trial_subscription_updatedfrom backend.util.clients import get_database_manager_async_clientfrom backend.util.logging import TruncatedLogger@@ -105,13 +106,14 @@ async def on_checkout_completed(session: dict, subscription: dict) -> None:user = await _user_for(session.get("customer"))if user is None:return+ fields = subscriber_fields.subscribed(subscription.get("start_date"))if user.welcome_email_sent_at is not None:- await queue_audience_change(- AudienceEventModel(- action=AudienceAction.ADD_CHANGELOG, email=user.email, user_id=user.id- )+ event = audience_event(+ AudienceAction.ADD_CHANGELOG, user.email, user.id, fields)+ if event is not None:+ await queue_audience_change(event)return# Claim before queueing: Stripe retries webhooks, and two welcomes is worse@@ -142,11 +144,10 @@ async def on_checkout_completed(session: dict, subscription: dict) -> None:# Must not propagate: the welcome is already out and the claim is durable,# so a Stripe retry would take the returning-customer branch and enrol them# in the changelog instead of the tour. Report it rather than fail.- enrolled = await queue_audience_change(- AudienceEventModel(- action=AudienceAction.ENROLL_TOUR, email=user.email, user_id=user.id- )- )+ event = audience_event(AudienceAction.ENROLL_TOUR, user.email, user.id, fields)+ if event is None:+ return+ enrolled = await queue_audience_change(event)if not enrolled.success:logger.error(f"Welcomed user {user.id} but could not queue the onboarding tour: "@@ -255,6 +256,11 @@ async def on_subscription_updated(subscription: dict, previous: dict) -> None:),claim_key,)+ await subscriber_fields.queue_fields(+ user.id,+ user.email,+ subscriber_fields.subscription_canceled(subscription.get("canceled_at")),+ )returnclaim_key = f"resumed:{sub_id}:{episode}"@@ -272,6 +278,9 @@ async def on_subscription_updated(subscription: dict, previous: dict) -> None:),claim_key,)+ await subscriber_fields.queue_fields(+ user.id, user.email, subscriber_fields.subscription_resumed()+ )async def on_subscription_deleted(subscription: dict) -> None:@@ -304,11 +313,14 @@ async def on_subscription_deleted(subscription: dict) -> None:claim_key,)# Churned users get win-back only, never the monthly update.- await queue_audience_change(- AudienceEventModel(- action=AudienceAction.REMOVE_CHANGELOG, email=user.email, user_id=user.id- )+ event = audience_event(+ AudienceAction.REMOVE_CHANGELOG,+ user.email,+ user.id,+ subscriber_fields.subscription_ended(subscription.get("ended_at")),)+ if event is not None:+ await queue_audience_change(event)async def _user_for(customer_id: object) -> BillingEmailRecipient | None:diff --git a/autogpt_platform/backend/backend/notifications/lifecycle_test.py b/autogpt_platform/backend/backend/notifications/lifecycle_test.pyindex d97917a4bd..89554668c3 100644--- a/autogpt_platform/backend/backend/notifications/lifecycle_test.py+++ b/autogpt_platform/backend/backend/notifications/lifecycle_test.py@@ -13,8 +13,14 @@ from unittest.mock import AsyncMock, patchimport pytestfrom prisma.enums import NotificationType-from backend.data.notifications import NotificationResult, SubscriptionPlan-from backend.notifications import lifecycle+from backend.data.notifications import (+ AudienceAction,+ NotificationResult,+ SubscriberField,+ SubscriptionPlan,+ SubscriptionStatus,+)+from backend.notifications import lifecycle, subscriber_fieldsfrom backend.notifications.lifecycle_plan import card_from_invoiceCUSTOMER = "cus_1"@@ -432,3 +438,117 @@ async def test_a_failed_welcome_publish_gives_the_claim_back():released.assert_awaited_once_with(user)# The tour enrolment must not run for a welcome that never went out.audience.assert_not_awaited()+++# ── subscriber fields ──────────────────────────────────────────────────────+++def fields_on(monkeypatch):+ queued = AsyncMock(return_value=NotificationResult(success=True))+ monkeypatch.setattr(subscriber_fields, "queue_audience_change", queued)+ return queued+++def _day(timestamp: int) -> str:+ return datetime.fromtimestamp(timestamp, timezone.utc).strftime("%Y-%m-%d")++[email protected]("welcomed", [False, True])+async def test_a_checkout_marks_them_subscribed_from_stripes_start_date(+ fields_on, welcomed+):+ user = _User(welcome_sent_at=datetime(2026, 1, 1) if welcomed else None)+ with patch(+ "backend.notifications.lifecycle._claim_welcome",+ AsyncMock(return_value=True),+ ):+ calls = await _run(+ lambda: lifecycle.on_checkout_completed(+ {"customer": CUSTOMER}, _subscription(start_date=1788000000)+ ),+ user,+ )+ assert calls["audience"].await_args.args[0].fields == {+ SubscriberField.STATUS: SubscriptionStatus.SUBSCRIBED.value,+ SubscriberField.SUBSCRIPTION_STARTED: _day(1788000000),+ SubscriberField.SUBSCRIPTION_CANCELED: None,+ SubscriberField.SUBSCRIPTION_ENDED: None,+ }+++async def test_a_paid_cancellation_sets_the_status_and_stripes_cancel_date(+ fields_on,+):+ await _run(+ lambda: lifecycle.on_subscription_updated(+ _subscription(cancel_at_period_end=True, canceled_at=1789100000),+ {"cancel_at_period_end": False},+ ),+ _User(),+ )+ event = fields_on.await_args.args[0]+ assert event.action is AudienceAction.UPDATE_FIELDS+ assert event.fields == {+ SubscriberField.STATUS: SubscriptionStatus.SUBSCRIPTION_CANCELED.value,+ SubscriberField.SUBSCRIPTION_CANCELED: _day(1789100000),+ }+++async def test_resuming_a_paid_subscription_clears_the_cancel_date(fields_on):+ await _run(+ lambda: lifecycle.on_subscription_updated(+ _subscription(cancel_at_period_end=False),+ {"cancel_at_period_end": True, "canceled_at": 1789100000},+ ),+ _User(),+ )+ assert fields_on.await_args.args[0].fields == {+ SubscriberField.STATUS: SubscriptionStatus.SUBSCRIBED.value,+ SubscriberField.SUBSCRIPTION_CANCELED: None,+ }+++async def test_a_replayed_cancellation_writes_no_fields(fields_on):+ await _run(+ lambda: lifecycle.on_subscription_updated(+ _subscription(cancel_at_period_end=True, canceled_at=1789100000),+ {"cancel_at_period_end": False},+ ),+ _User(),+ claim=False,+ )+ fields_on.assert_not_awaited()+++async def test_a_failed_field_update_never_fails_the_cancellation(fields_on, caplog):+ fields_on.return_value = NotificationResult(success=False, message="down")+ with caplog.at_level(logging.ERROR):+ calls = await _run(+ lambda: lifecycle.on_subscription_updated(+ _subscription(cancel_at_period_end=True, canceled_at=1789100000),+ {"cancel_at_period_end": False},+ ),+ _User(),+ )+ calls["notify"].assert_awaited_once()+ assert "MailerLite fields" in caplog.text+++async def test_an_ended_subscription_sets_stripes_end_date_on_the_churn(fields_on):+ calls = await _run(+ lambda: lifecycle.on_subscription_deleted(_subscription(ended_at=1789200000)),+ _User(),+ )+ event = calls["audience"].await_args.args[0]+ assert event.action is AudienceAction.REMOVE_CHANGELOG+ assert event.fields == {+ SubscriberField.STATUS: SubscriptionStatus.SUBSCRIPTION_ENDED.value,+ SubscriberField.SUBSCRIPTION_ENDED: _day(1789200000),+ }diff --git a/autogpt_platform/backend/backend/notifications/mailerlite.py b/autogpt_platform/backend/backend/notifications/mailerlite.pyindex faf5f3b2ab..cbb916b89b 100644--- a/autogpt_platform/backend/backend/notifications/mailerlite.py+++ b/autogpt_platform/backend/backend/notifications/mailerlite.py@@ -12,6 +12,16 @@ transition:2. ENTER · resubscribers and pre-tour users → changelog: owned here.3. LEAVE · churn: owned here. Churned users get win-back only, never themonthly update.+4. ENTER and LEAVE · the trial group: both owned here. It holds exactly the+ customers currently on a trial, so any MailerLite automation on it may send+ mail but must never move people in or out.++Subscriber fields (`SubscriberField`) are the backend's alone: every write+comes from here, and MailerLite automations only read them.++Only the notification service holds the MailerLite settings. Whatever queues an+audience change does not ask whether MailerLite is configured; the consumer+does (`configured()`).If both sides managed the same edge we would double-add or fight overremovals, so nothing in this module touches the tour → changelog handoff.@@ -19,7 +29,9 @@ removals, so nothing in this module touches the tour → changelog handoff.import hashlibimport logging+from collections.abc import Mapping+from backend.data.notifications import SubscriberFieldfrom backend.util.request import Requestsfrom backend.util.settings import Settings@@ -39,13 +51,36 @@ def _pseudonym(email: str) -> str:settings = Settings()-API_BASE = "https://connect.mailerlite.com/api"+API_BASE = settings.config.mailerlite_api_url.rstrip("/")_OK_STATUSES = (200, 201, 202, 204)+# The type MailerLite stores each field as. A date is written YYYY-MM-DD.+FIELD_TYPES: dict[SubscriberField, str] = {+ SubscriberField.STATUS: "text",+ SubscriberField.SIGNUP: "date",+ SubscriberField.TRIAL_STARTED: "date",+ SubscriberField.SUBSCRIPTION_STARTED: "date",+ SubscriberField.SUBSCRIPTION_CANCELED: "date",+ SubscriberField.SUBSCRIPTION_ENDED: "date",+}++Fields = Mapping[SubscriberField, str | None]++# Set once this process has seen every field exist, so it is checked once,+# not on every write.+_fields_ready = False+class MailerLiteNotConfigured(RuntimeError):- """Raised so a queued job retries once credentials are in place, rather- than silently reporting success."""+ """A setting the change needs is missing. The consumer dead-letters it at+ once rather than silently reporting success, so it can be replayed once the+ setting is in place."""+++def configured() -> bool:+ """A stack without a token, such as a self-hosted one, has no MailerLite:+ its audience changes are acknowledged and dropped, never retried."""+ return bool(settings.secrets.mailerlite_api_token)class MailerLiteError(RuntimeError):@@ -54,25 +89,142 @@ class MailerLiteError(RuntimeError):silently drop an enrolment either."""-async def enroll_in_onboarding(email: str) -> None:+async def enroll_in_onboarding(email: str, fields: Fields | None = None) -> None:"""Add a first-time subscriber to the tour group. Joining the group is theautomation's trigger; MailerLite sends the six emails from there."""await _add_to_group(- email, settings.config.mailerlite_onboarding_group_id, "onboarding tour"+ email, settings.config.mailerlite_onboarding_group_id, "onboarding tour", fields)-async def add_to_changelog(email: str) -> None:+async def add_to_changelog(email: str, fields: Fields | None = None) -> None:"""Returning customers and anyone who predates the tour."""await _add_to_group(- email, settings.config.mailerlite_changelog_group_id, "changelog"+ email, settings.config.mailerlite_changelog_group_id, "changelog", fields)-async def remove_from_changelog(email: str) -> None:+async def remove_from_changelog(email: str, fields: Fields | None = None) -> None:"""The day a plan ends."""- group_id = settings.config.mailerlite_changelog_group_id- _require_config(group_id, "changelog")+ await _remove_from_group(+ email, settings.config.mailerlite_changelog_group_id, "changelog", fields+ )+++async def add_to_trial(email: str, fields: Fields | None = None) -> None:+ """A trial started, or a cancelled one was resumed. Without a trial group+ only the fields are written: the group is optional, so waiting for it+ would only retry into the dead-letter queue."""+ group_id = settings.config.mailerlite_trial_group_id+ if not group_id:+ await update_fields(email, fields)+ return+ await _add_to_group(email, group_id, "trial", fields)+++async def remove_from_trial(email: str, fields: Fields | None = None) -> None:+ """The trial was cancelled, converted, or ended unpaid. Without a trial+ group only the fields are written, as for `add_to_trial`."""+ group_id = settings.config.mailerlite_trial_group_id+ if not group_id:+ await update_fields(email, fields)+ return+ await _remove_from_group(email, group_id, "trial", fields)+++async def update_fields(email: str, fields: Fields | None = None) -> None:+ """Write a subscriber's fields, creating the subscriber if MailerLite has+ none. It never changes their subscription status, so an unsubscribed+ person stays unsubscribed."""+ if not fields:+ return+ _require_token()+ await ensure_fields()+ await _upsert(email, {"fields": _payload(fields)}, "field update")+++async def record_signup(email: str, fields: Fields | None = None) -> None:+ """A new account's fields. `signed` is where every subscriber starts, so+ it never replaces a status MailerLite already holds: the signup is queued+ in the background, and a checkout queued before it must not be undone.+ The audience queue is worked one message at a time, so nothing of ours+ writes between the read and the write."""+ if not fields:+ return+ _require_token()+ held = (await _find_subscriber(email) or {}).get("fields") or {}+ if held.get(SubscriberField.STATUS.value):+ fields = {k: v for k, v in fields.items() if k != SubscriberField.STATUS}+ await update_fields(email, fields)+++async def ensure_fields() -> list[SubscriberField]:+ """Create any of our fields MailerLite does not have yet, and return the+ ones created. Idempotent, and checked once per process."""+ global _fields_ready+ if _fields_ready:+ return []+ _require_token()+ existing = await read_fields()+ created = []+ for field, kind in FIELD_TYPES.items():+ if field.value in existing:+ continue+ response = await _client().post(+ f"{API_BASE}/fields",+ headers=_headers(),+ json={"name": field.value, "type": kind},+ )+ if response.status not in _OK_STATUSES:+ raise MailerLiteError(+ f"Creating the MailerLite field {field.value} failed with "+ f"{response.status}"+ )+ key = ((response.json() or {}).get("data") or {}).get("key")+ if key != field.value:+ # Writes go by key, so a field under another key would silently+ # never be filled.+ raise MailerLiteError(+ f"MailerLite created the field {field.value} as {key!r}"+ )+ created.append(field)+ logger.info(f"Created the MailerLite field {field.value}")+ _fields_ready = True+ return created+++# An account holds far fewer fields than this many pages of 100; more means+# MailerLite's last_page keeps moving.+_MAX_FIELD_PAGES = 50+++async def read_fields() -> dict[str, str]:+ """Every custom field MailerLite has, as key → type. An empty page ends the+ read whatever last_page says."""+ fields: dict[str, str] = {}+ for page in range(1, _MAX_FIELD_PAGES + 1):+ response = await _client().get(+ f"{API_BASE}/fields?limit=100&page={page}", headers=_headers()+ )+ if response.status != 200:+ raise MailerLiteError(+ f"Reading MailerLite fields failed with {response.status}"+ )+ body = response.json() or {}+ rows = body.get("data") or []+ for row in rows:+ fields[str(row["key"])] = str(row.get("type") or "")+ if not rows or page >= int((body.get("meta") or {}).get("last_page") or 1):+ return fields+ raise MailerLiteError(f"MailerLite still had fields after {_MAX_FIELD_PAGES} pages")+++async def _remove_from_group(+ email: str, group_id: str, description: str, fields: Fields | None+) -> None:+ # Fields first: they are written even when the group change cannot be.+ await update_fields(email, fields)+ _require_config(group_id, description)subscriber_id = await _find_subscriber_id(email)if subscriber_id is None:@@ -88,28 +240,47 @@ async def remove_from_changelog(email: str) -> None:# 404 means they are already out of the group, which is the desired state.if response.status not in _OK_STATUSES and response.status != 404:raise MailerLiteError(- f"Removing subscriber {_pseudonym(email)} from the changelog group "+ f"Removing subscriber {_pseudonym(email)} from the {description} group "f"failed with {response.status}")- logger.info("Removed %s from the MailerLite changelog group", _pseudonym(email))+ logger.info(f"Removed {_pseudonym(email)} from the MailerLite {description} group")-async def _add_to_group(email: str, group_id: str, description: str) -> None:+async def _add_to_group(+ email: str, group_id: str, description: str, fields: Fields | None+) -> None:+ if not group_id:+ # The fields still land; the group change is then dead-lettered.+ await update_fields(email, fields)_require_config(group_id, description)+ body: dict = {"groups": [group_id]}+ if fields:+ await ensure_fields()+ body["fields"] = _payload(fields)+ await _upsert(email, body, f"{description} group")+ logger.info("Added %s to the MailerLite %s group", _pseudonym(email), description)+++async def _upsert(email: str, body: dict, description: str) -> None:response = await _client().post(- f"{API_BASE}/subscribers",- headers=_headers(),- json={"email": email, "groups": [group_id]},+ f"{API_BASE}/subscribers", headers=_headers(), json={"email": email, **body})if response.status not in _OK_STATUSES:raise MailerLiteError(- f"Adding subscriber {_pseudonym(email)} to the {description} group "+ f"MailerLite {description} for subscriber {_pseudonym(email)} "f"failed with {response.status}")- logger.info("Added %s to the MailerLite %s group", _pseudonym(email), description)+++def _payload(fields: Fields) -> dict[str, str | None]:+ return {SubscriberField(key).value: value for key, value in fields.items()}async def _find_subscriber_id(email: str) -> str | None:+ return (await _find_subscriber(email) or {}).get("id")+++async def _find_subscriber(email: str) -> dict | None:response = await _client().get(f"{API_BASE}/subscribers/{email}", headers=_headers())@@ -120,7 +291,7 @@ async def _find_subscriber_id(email: str) -> str | None:f"Looking up MailerLite subscriber {_pseudonym(email)} failed with "f"{response.status}")- return ((response.json() or {}).get("data") or {}).get("id")+ return (response.json() or {}).get("data") or {}def _client() -> Requests:@@ -136,9 +307,13 @@ def _headers() -> dict[str, str]:}-def _require_config(group_id: str, description: str) -> None:+def _require_token() -> None:if not settings.secrets.mailerlite_api_token:raise MailerLiteNotConfigured("MAILERLITE_API_TOKEN is not set")+++def _require_config(group_id: str, description: str) -> None:+ _require_token()if not group_id:raise MailerLiteNotConfigured(f"The MailerLite {description} group ID is not configured"diff --git a/autogpt_platform/backend/backend/notifications/mailerlite_backfill.py b/autogpt_platform/backend/backend/notifications/mailerlite_backfill.pynew file mode 100644index 0000000000..e408b3ae58--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/mailerlite_backfill.py@@ -0,0 +1,327 @@+"""Put existing Stripe customers where the live audience code would have.++The lifecycle handlers only react to new Stripe events, so everyone who+subscribed before they shipped sits in no MailerLite group. This works out,+per customer, the membership those handlers would have produced, and applies+only the difference.++The one-owner-per-transition rule in `mailerlite.py` holds here too. Anyone in+the tour group is left alone: they are either mid-tour, and must not get the+changelog yet, or they finished it and MailerLite's automation has already+moved them across.++Idempotent by construction: current membership is read first, so a second run+finds nothing to do and a failed call is simply picked up by the next run.+"""++import asyncio+import logging+from enum import Enum+from urllib.parse import urlencode++from pydantic import BaseModel++from backend.notifications.mailerlite import (+ API_BASE,+ MailerLiteError,+ _client,+ _headers,+ _pseudonym,+ _require_config,+)+from backend.util.settings import Settings++logger = logging.getLogger(__name__)+settings = Settings()++# MailerLite accepts at most 50 calls per batch. A batch made only of+# subscriber upserts is processed as an import, which has its own limit of 5+# per minute on top of the global 120 per minute.+BATCH_SIZE = 50+UPSERT_BATCH_INTERVAL_SECONDS = 13.0+OTHER_BATCH_INTERVAL_SECONDS = 1.0+PAGE_SIZE = 1000+MEMBER_STATUSES = ("active", "unsubscribed", "unconfirmed", "bounced", "junk")++_ENDED = {"canceled", "incomplete_expired"}+++class Standing(str, Enum):+ PAYING = "paying"+ TRIALING = "trialing"+ # Still trialing, but set to cancel: no longer "currently on a trial".+ TRIAL_CANCELLING = "trial_cancelling"+ CHURNED = "churned"+ # incomplete, unpaid, paused: no lifecycle event has settled these yet.+ UNSETTLED = "unsettled"+++class Decision(str, Enum):+ ADD_CHANGELOG = "add_changelog"+ ADD_TRIAL = "add_trial"+ REMOVE_CHANGELOG = "remove_changelog"+ REMOVE_TRIAL = "remove_trial"+ SKIP_IN_TOUR = "skip_in_tour"+ SKIP_NO_TRIAL_GROUP = "skip_no_trial_group"+ SKIP_UNSETTLED = "skip_unsettled"+ ALREADY_CORRECT = "already_correct"+++CHANGES = (+ Decision.ADD_CHANGELOG,+ Decision.ADD_TRIAL,+ Decision.REMOVE_CHANGELOG,+ Decision.REMOVE_TRIAL,+)+++class Subscription(BaseModel):+ status: str+ id: str = ""+ cancel_at_period_end: bool = False+ # Born from a card-required trial. Past due on one of these may be a+ # failed conversion rather than a paying customer's missed renewal.+ from_trial: bool = False+ # A trial whose first payment cleared, from our own trial record. Stripe+ # alone cannot tell an ended converted trial from an unpaid one.+ converted: bool = False+ # Stripe's timestamps, for the subscriber's dates.+ start_date: int | None = None+ trial_start: int | None = None+ trial_end: int | None = None+ canceled_at: int | None = None+ ended_at: int | None = None+++class Customer(BaseModel):+ """A platform account and all of its Stripe subscriptions."""++ user_id: str+ email: str+ subscriptions: list[Subscription]+++class Audience(BaseModel):+ """Current membership of each group, keyed by lowercased email, valued by+ MailerLite subscriber ID."""++ tour: dict[str, str]+ changelog: dict[str, str]+ trial: dict[str, str]+++class PlannedChange(BaseModel):+ customer: Customer+ standing: Standing+ decisions: list[Decision]+++class ApplyResult(BaseModel):+ succeeded: dict[Decision, int]+ failed: dict[Decision, int]+++def classify(subscriptions: list[Subscription]) -> Standing:+ """A customer who pays on any subscription is a subscriber, whatever the+ state of their others."""+ if any(_paying(s) for s in subscriptions):+ return Standing.PAYING+ trialing = [s for s in subscriptions if s.status == "trialing"]+ if any(not s.cancel_at_period_end for s in trialing):+ return Standing.TRIALING+ if trialing:+ return Standing.TRIAL_CANCELLING+ statuses = {s.status for s in subscriptions}+ if statuses and statuses <= _ENDED:+ return Standing.CHURNED+ return Standing.UNSETTLED+++def _paying(subscription: Subscription) -> bool:+ if subscription.status == "active":+ return True+ return subscription.status == "past_due" and not subscription.from_trial+++def decide(+ customer: Customer, audience: Audience, trial_enabled: bool+) -> PlannedChange:+ standing = classify(customer.subscriptions)+ email = customer.email.strip().lower()+ in_tour = email in audience.tour+ in_changelog = email in audience.changelog+ in_trial = email in audience.trial++ decisions: list[Decision] = []+ if standing is not Standing.TRIALING and in_trial:+ decisions.append(Decision.REMOVE_TRIAL)+ if standing is Standing.PAYING and not in_changelog:+ decisions.append(Decision.SKIP_IN_TOUR if in_tour else Decision.ADD_CHANGELOG)+ elif standing is Standing.TRIALING and not in_trial:+ decisions.append(+ Decision.ADD_TRIAL if trial_enabled else Decision.SKIP_NO_TRIAL_GROUP+ )+ elif standing is Standing.CHURNED and in_changelog:+ decisions.append(Decision.REMOVE_CHANGELOG)+ elif standing is Standing.UNSETTLED:+ decisions.append(Decision.SKIP_UNSETTLED)++ return PlannedChange(+ customer=customer,+ standing=standing,+ decisions=decisions or [Decision.ALREADY_CORRECT],+ )+++def plan(customers: list[Customer], audience: Audience) -> list[PlannedChange]:+ trial_enabled = bool(settings.config.mailerlite_trial_group_id)+ return [decide(c, audience, trial_enabled) for c in customers]+++async def read_audience() -> Audience:+ """Read every group this touches, in every subscriber status: an+ unsubscribed member is still a member, and must not be re-added."""+ config = settings.config+ _require_config(config.mailerlite_onboarding_group_id, "onboarding tour")+ _require_config(config.mailerlite_changelog_group_id, "changelog")+ trial_group = config.mailerlite_trial_group_id+ return Audience(+ tour=await _read_group(config.mailerlite_onboarding_group_id),+ changelog=await _read_group(config.mailerlite_changelog_group_id),+ trial=await _read_group(trial_group) if trial_group else {},+ )+++async def apply(changes: list[PlannedChange], audience: Audience) -> ApplyResult:+ result = ApplyResult(+ succeeded={d: 0 for d in CHANGES}, failed={d: 0 for d in CHANGES}+ )+ batches = [+ (decision, due[start : start + BATCH_SIZE])+ for decision in CHANGES+ for due in [[c for c in changes if decision in c.decisions]]+ for start in range(0, len(due), BATCH_SIZE)+ ]+ for index, (decision, chunk) in enumerate(batches):+ if index:+ await asyncio.sleep(_interval_before(decision))+ codes = 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)+ return result+++def _interval_before(decision: Decision) -> float:+ if decision in (Decision.ADD_CHANGELOG, Decision.ADD_TRIAL):+ return UPSERT_BATCH_INTERVAL_SECONDS+ return OTHER_BATCH_INTERVAL_SECONDS+++def _call_for(decision: Decision, email: str, audience: Audience) -> dict:+ """The same call the live handler makes for this change."""+ config = settings.config+ if decision is Decision.ADD_CHANGELOG:+ group = config.mailerlite_changelog_group_id+ return {+ "method": "POST",+ "path": "api/subscribers",+ "body": _upsert(email, group),+ }+ if decision is Decision.ADD_TRIAL:+ group = config.mailerlite_trial_group_id+ return {+ "method": "POST",+ "path": "api/subscribers",+ "body": _upsert(email, group),+ }+ key = email.strip().lower()+ if decision is Decision.REMOVE_CHANGELOG:+ subscriber, group = (+ audience.changelog[key],+ config.mailerlite_changelog_group_id,+ )+ else:+ subscriber, group = audience.trial[key], config.mailerlite_trial_group_id+ return {"method": "DELETE", "path": f"api/subscribers/{subscriber}/groups/{group}"}+++def _upsert(email: str, group_id: str) -> dict:+ return {"email": email, "groups": [group_id]}+++def _record(+ result: ApplyResult, decision: Decision, change: PlannedChange, code: int+) -> 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)+ )+ if ok:+ result.succeeded[decision] += 1+ return+ result.failed[decision] += 1+ logger.warning(+ f"{decision.value} failed for {_pseudonym(change.customer.email)} "+ f"with {code}; the next run retries it"+ )+++async def _send_batch(requests: list[dict]) -> list[int]:+ """One /batch call. Requests retries a 429 with backoff before this sees+ it, so a status here is final."""+ response = await _client().post(+ f"{API_BASE}/batch", headers=_headers(), json={"requests": requests}+ )+ if response.status != 200:+ raise MailerLiteError(f"MailerLite batch failed with {response.status}")+ responses = (response.json() or {}).get("responses") or []+ if len(responses) != len(requests):+ raise MailerLiteError(+ f"MailerLite answered {len(responses)} of {len(requests)} batched calls"+ )+ return [int(r.get("code") or 0) for r in responses]+++async def _read_group(group_id: str) -> dict[str, str]:+ members: dict[str, str] = {}+ for status in MEMBER_STATUSES:+ cursor: str | None = None+ seen: set[str] = set()+ while True:+ params = {"limit": str(PAGE_SIZE), "filter[status]": status}+ if cursor:+ params["cursor"] = cursor+ page = await _get_page(group_id, params)+ for row in page.get("data") or []:+ members[str(row["email"]).strip().lower()] = str(row["id"])+ cursor = next_cursor(page, seen)+ if not cursor:+ break+ return members+++def next_cursor(page: dict, seen: set[str]) -> str | None:+ """The cursor for the page after this one, or None after the last. One+ MailerLite already handed out would page forever, so it is an error rather+ than the end: stopping there would plan from a partial read."""+ cursor = (page.get("meta") or {}).get("next_cursor")+ if not cursor:+ return None+ if cursor in seen:+ raise MailerLiteError(f"MailerLite repeated the page cursor {cursor!r}")+ seen.add(cursor)+ return cursor+++async def _get_page(group_id: str, params: dict[str, str]) -> dict:+ response = await _client().get(+ f"{API_BASE}/groups/{group_id}/subscribers?{urlencode(params)}",+ headers=_headers(),+ )+ if response.status != 200:+ raise MailerLiteError(+ f"Reading MailerLite group {group_id} failed with {response.status}"+ )+ return response.json() or {}diff --git a/autogpt_platform/backend/backend/notifications/mailerlite_backfill_test.py b/autogpt_platform/backend/backend/notifications/mailerlite_backfill_test.pynew file mode 100644index 0000000000..ef80622fae--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/mailerlite_backfill_test.py@@ -0,0 +1,371 @@+"""The backfill must put each existing customer where the live handlers would+have, and never fight MailerLite's own tour → changelog handoff."""++from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock++import pytest++from backend.notifications import mailerlite, mailerlite_backfill+from backend.notifications.mailerlite_backfill import (+ Audience,+ Customer,+ Decision,+ Standing,+ Subscription,+)++TOUR, CHANGELOG, TRIAL = "grp_tour", "grp_changelog", "grp_trial"+++def configured(monkeypatch):+ fake = SimpleNamespace(+ config=SimpleNamespace(+ mailerlite_onboarding_group_id=TOUR,+ mailerlite_changelog_group_id=CHANGELOG,+ mailerlite_trial_group_id=TRIAL,+ ),+ secrets=SimpleNamespace(mailerlite_api_token="token"),+ )+ monkeypatch.setattr(mailerlite, "settings", fake)+ monkeypatch.setattr(mailerlite_backfill, "settings", fake)+ return fake+++def no_sleep(monkeypatch):+ sleep = AsyncMock()+ monkeypatch.setattr(mailerlite_backfill.asyncio, "sleep", sleep)+ return sleep+++def _sub(status: str) -> Subscription:+ """`trialing+cancel` is a trial set to cancel; `past_due+trial` is past due+ on a subscription that started as a trial."""+ name, _, flag = status.partition("+")+ return Subscription(+ status=name, cancel_at_period_end=flag == "cancel", from_trial=flag == "trial"+ )+++def _customer(email: str, *statuses: str) -> Customer:+ return Customer(+ user_id=f"u-{email}", email=email, subscriptions=[_sub(s) for s in statuses]+ )+++def _audience(tour=(), changelog=(), trial=()) -> Audience:+ return Audience(+ tour={e: f"sub-{e}" for e in tour},+ changelog={e: f"sub-{e}" for e in changelog},+ trial={e: f"sub-{e}" for e in trial},+ )+++def _response(status: int, body: dict) -> MagicMock:+ response = MagicMock(status=status)+ response.json.return_value = body+ return response+++def _batch_ok(requests: list[dict], code: int = 201) -> MagicMock:+ return _response(200, {"responses": [{"code": code, "body": {}} for _ in requests]})+++ "statuses, standing",+ [+ (["active"], Standing.PAYING),+ (["past_due"], Standing.PAYING),+ (["canceled", "active"], Standing.PAYING),+ (["trialing"], Standing.TRIALING),+ (["canceled", "trialing"], Standing.TRIALING),+ (["trialing+cancel"], Standing.TRIAL_CANCELLING),+ (["trialing+cancel", "trialing"], Standing.TRIALING),+ (["past_due+trial"], Standing.UNSETTLED),+ (["past_due+trial", "active"], Standing.PAYING),+ (["canceled"], Standing.CHURNED),+ (["canceled", "incomplete_expired"], Standing.CHURNED),+ (["unpaid"], Standing.UNSETTLED),+ (["incomplete"], Standing.UNSETTLED),+ (["canceled", "paused"], Standing.UNSETTLED),+ ([], Standing.UNSETTLED),+ ],+)+def test_classify(statuses, standing):+ assert mailerlite_backfill.classify([_sub(s) for s in statuses]) is standing+++ "statuses, audience, decisions",+ [+ (["active"], _audience(), [Decision.ADD_CHANGELOG]),+ # Mid-tour, or finished and already moved across by MailerLite.+ (["active"], _audience(tour=["[email protected]"]), [Decision.SKIP_IN_TOUR]),+ (["active"], _audience(changelog=["[email protected]"]), [Decision.ALREADY_CORRECT]),+ (+ ["active"],+ _audience(trial=["[email protected]"]),+ [Decision.REMOVE_TRIAL, Decision.ADD_CHANGELOG],+ ),+ (["trialing"], _audience(), [Decision.ADD_TRIAL]),+ (["trialing"], _audience(trial=["[email protected]"]), [Decision.ALREADY_CORRECT]),+ # The trial group is "currently on a trial": cancelling means leaving.+ (["trialing+cancel"], _audience(trial=["[email protected]"]), [Decision.REMOVE_TRIAL]),+ (["trialing+cancel"], _audience(), [Decision.ALREADY_CORRECT]),+ # A trial cancel only leaves the trial group live; the changelog is+ # for churn, and a trialist has not churned.+ (+ ["trialing+cancel"],+ _audience(changelog=["[email protected]"]),+ [Decision.ALREADY_CORRECT],+ ),+ (+ ["trialing+cancel"],+ _audience(changelog=["[email protected]"], trial=["[email protected]"]),+ [Decision.REMOVE_TRIAL],+ ),+ # A failed conversion is neither on a trial nor paying.+ (+ ["past_due+trial"],+ _audience(trial=["[email protected]"]),+ [Decision.REMOVE_TRIAL, Decision.SKIP_UNSETTLED],+ ),+ (["canceled"], _audience(changelog=["[email protected]"]), [Decision.REMOVE_CHANGELOG]),+ (+ ["canceled"],+ _audience(changelog=["[email protected]"], trial=["[email protected]"]),+ [Decision.REMOVE_TRIAL, Decision.REMOVE_CHANGELOG],+ ),+ (["canceled"], _audience(), [Decision.ALREADY_CORRECT]),+ (["unpaid"], _audience(changelog=["[email protected]"]), [Decision.SKIP_UNSETTLED]),+ ],+)+def test_decide(statuses, audience, decisions):+ change = mailerlite_backfill.decide(+ _customer("[email protected]", *statuses), audience, trial_enabled=True+ )+ assert change.decisions == decisions+++def test_trialing_is_skipped_without_a_trial_group():+ change = mailerlite_backfill.decide(+ _customer("[email protected]", "trialing"), _audience(), trial_enabled=False+ )+ assert change.decisions == [Decision.SKIP_NO_TRIAL_GROUP]+++def test_membership_matches_case_insensitively():+ change = mailerlite_backfill.decide(+ _customer(" [email protected] ", "active"),+ _audience(changelog=["[email protected]"]),+ trial_enabled=True,+ )+ assert change.decisions == [Decision.ALREADY_CORRECT]+++def test_plan_reads_the_trial_setting(configured):+ configured.config.mailerlite_trial_group_id = ""+ [change] = mailerlite_backfill.plan([_customer("[email protected]", "trialing")], _audience())+ assert change.decisions == [Decision.SKIP_NO_TRIAL_GROUP]+++async def test_apply_sends_the_live_handlers_calls(configured, no_sleep, monkeypatch):+ client = MagicMock()+ client.post = AsyncMock(+ side_effect=lambda url, **kw: _batch_ok(kw["json"]["requests"])+ )+ monkeypatch.setattr(mailerlite_backfill, "_client", lambda: client)+ audience = _audience(changelog=["[email protected]"], trial=["[email protected]"])+ changes = mailerlite_backfill.plan(+ [+ _customer("[email protected]", "active"),+ _customer("[email protected]", "trialing"),+ _customer("[email protected]", "canceled"),+ _customer("[email protected]", "active"),+ ],+ audience,+ )++ result = await mailerlite_backfill.apply(changes, audience)++ sent = [call.kwargs["json"]["requests"] for call in client.post.await_args_list]+ assert all(+ c.args[0] == f"{mailerlite.API_BASE}/batch" for c in client.post.await_args_list+ )+ assert sent == [+ [+ {+ "method": "POST",+ "path": "api/subscribers",+ "body": {"email": "[email protected]", "groups": [CHANGELOG]},+ },+ {+ "method": "POST",+ "path": "api/subscribers",+ "body": {"email": "[email protected]", "groups": [CHANGELOG]},+ },+ ],+ [+ {+ "method": "POST",+ "path": "api/subscribers",+ "body": {"email": "[email protected]", "groups": [TRIAL]},+ }+ ],+ [+ {+ "method": "DELETE",+ "path": f"api/subscribers/[email protected]/groups/{CHANGELOG}",+ }+ ],+ [{"method": "DELETE", "path": f"api/subscribers/[email protected]/groups/{TRIAL}"}],+ ]+ assert result.succeeded == {+ Decision.ADD_CHANGELOG: 2,+ Decision.ADD_TRIAL: 1,+ Decision.REMOVE_CHANGELOG: 1,+ Decision.REMOVE_TRIAL: 1,+ }+ assert sum(result.failed.values()) == 0+++async def test_apply_paces_batches_for_the_import_limit(+ configured, no_sleep, monkeypatch+):+ client = MagicMock()+ client.post = AsyncMock(+ side_effect=lambda url, **kw: _batch_ok(kw["json"]["requests"])+ )+ monkeypatch.setattr(mailerlite_backfill, "_client", lambda: client)+ customers = [_customer(f"c{i}@x.io", "active") for i in range(120)]+ changes = mailerlite_backfill.plan(customers, _audience())++ await mailerlite_backfill.apply(changes, _audience())++ sizes = [len(c.kwargs["json"]["requests"]) for c in client.post.await_args_list]+ assert sizes == [50, 50, 20]+ assert [c.args[0] for c in no_sleep.await_args_list] == [+ mailerlite_backfill.UPSERT_BATCH_INTERVAL_SECONDS+ ] * 2+++async def test_apply_counts_failures_and_treats_gone_as_removed(+ configured, no_sleep, monkeypatch, caplog+):+ client = MagicMock()+ client.post = AsyncMock(+ side_effect=[+ _response(200, {"responses": [{"code": 422}]}),+ _response(200, {"responses": [{"code": 404}]}),+ ]+ )+ monkeypatch.setattr(mailerlite_backfill, "_client", lambda: client)+ audience = _audience(changelog=["[email protected]"])+ changes = mailerlite_backfill.plan(+ [_customer("[email protected]", "active"), _customer("[email protected]", "canceled")], audience+ )++ result = await mailerlite_backfill.apply(changes, audience)++ assert result.failed[Decision.ADD_CHANGELOG] == 1+ assert result.succeeded[Decision.REMOVE_CHANGELOG] == 1+ assert "[email protected]" not in caplog.text+ assert mailerlite._pseudonym("[email protected]") in caplog.text+++async def test_a_short_batch_answer_is_an_error(configured, no_sleep, monkeypatch):+ client = MagicMock()+ client.post = AsyncMock(return_value=_response(200, {"responses": []}))+ monkeypatch.setattr(mailerlite_backfill, "_client", lambda: client)+ changes = mailerlite_backfill.plan([_customer("[email protected]", "active")], _audience())++ with pytest.raises(mailerlite.MailerLiteError):+ await mailerlite_backfill.apply(changes, _audience())+++async def test_read_audience_pages_every_status(configured, monkeypatch):+ pages = {+ (TOUR, "active", None): {+ "data": [{"id": "1", "email": "[email protected]"}],+ "meta": {"next_cursor": "c2"},+ },+ (TOUR, "active", "c2"): {"data": [{"id": "2", "email": "[email protected]"}], "meta": {}},+ (CHANGELOG, "unsubscribed", None): {+ "data": [{"id": "3", "email": "[email protected]"}],+ "meta": {},+ },+ }++ async def get(url, **kw):+ path, _, query = url.partition("?")+ params = dict(p.split("=", 1) for p in query.split("&"))+ group = path.split("/")[-2]+ key = (group, params["filter%5Bstatus%5D"], params.get("cursor"))+ return _response(200, pages.get(key, {"data": [], "meta": {}}))++ client = MagicMock()+ client.get = AsyncMock(side_effect=get)+ monkeypatch.setattr(mailerlite_backfill, "_client", lambda: client)++ audience = await mailerlite_backfill.read_audience()++ assert audience.tour == {"[email protected]": "1", "[email protected]": "2"}+ # Unsubscribed is still a member, so it is not re-added.+ assert audience.changelog == {"[email protected]": "3"}+ assert audience.trial == {}+++async def test_read_audience_needs_the_groups(configured):+ configured.config.mailerlite_changelog_group_id = ""+ with pytest.raises(mailerlite.MailerLiteNotConfigured):+ await mailerlite_backfill.read_audience()+++def test_a_second_run_finds_nothing_to_do():+ customers = [+ _customer("[email protected]", "active"),+ _customer("[email protected]", "trialing"),+ _customer("[email protected]", "canceled"),+ ]+ after = _audience(changelog=["[email protected]"], trial=["[email protected]"])+ changes = [+ mailerlite_backfill.decide(c, after, trial_enabled=True) for c in customers+ ]+ assert all(c.decisions == [Decision.ALREADY_CORRECT] for c in changes)+++async def test_read_audience_stops_on_a_repeated_cursor(configured, monkeypatch):+ """A cursor MailerLite already handed out would page forever. It is an+ error, not the end, or the plan would be made from a partial read."""+ page = {"data": [{"id": "1", "email": "[email protected]"}], "meta": {"next_cursor": "c2"}}+ client = MagicMock()+ client.get = AsyncMock(side_effect=_paging_forever(page))+ monkeypatch.setattr(mailerlite_backfill, "_client", lambda: client)++ with pytest.raises(mailerlite.MailerLiteError, match="repeated"):+ await mailerlite_backfill.read_audience()+ assert client.get.await_count == 2+++def _paging_forever(page: dict):+ """The same page every time. A mock never yields to the event loop, so a+ reader that does not stop would hang the test rather than time out."""+ calls = 0++ async def get(url, **kw):+ nonlocal calls+ calls += 1+ if calls > 10:+ raise AssertionError("the reader requested the same page forever")+ return _response(200, page)++ return getdiff --git a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.py b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.pynew file mode 100644index 0000000000..d83060a0d7--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.py@@ -0,0 +1,259 @@+"""Give every account the MailerLite status and dates the live code would have.++Signups were never synced and the lifecycle handlers only react to new+events, so this works out each account's fields from scratch: the signup date+from our database, the rest from Stripe, by the rules in `subscriber_fields.py`.+Accounts with no Stripe customer are `signed`.++Resumable by construction: the fields MailerLite already holds are read first+and only the difference is written, so an interrupted or repeated run picks up+where the last one stopped.+"""++import asyncio+import logging+from collections.abc import Callable+from datetime import datetime+from urllib.parse import urlencode++from pydantic import BaseModel, EmailStr, TypeAdapter, ValidationError++from backend.data.notifications import SubscriberField, SubscriptionStatus+from backend.notifications.mailerlite import (+ API_BASE,+ FIELD_TYPES,+ MailerLiteError,+ _client,+ _headers,+ _pseudonym,+ _require_token,+)+from backend.notifications.mailerlite_backfill import (+ BATCH_SIZE,+ MEMBER_STATUSES,+ PAGE_SIZE,+ UPSERT_BATCH_INTERVAL_SECONDS,+ Subscription,+ _send_batch,+ next_cursor,+)+from backend.notifications.subscriber_fields import Fields, mailerlite_date++logger = logging.getLogger(__name__)++_EMAIL = TypeAdapter(EmailStr)+_NEVER_STARTED = ("incomplete", "incomplete_expired", "trialing")++# Email → our fields as MailerLite holds them.+Current = dict[str, dict[str, str | None]]+++class Person(BaseModel):+ """A platform account and its Stripe subscriptions, if any."""++ user_id: str+ email: str+ created_at: datetime+ subscriptions: list[Subscription] = []+++class FieldChange(BaseModel):+ person: Person+ status: SubscriptionStatus+ # Only the fields that differ from MailerLite's.+ fields: Fields+ # Not a MailerLite subscriber yet: writing creates one.+ new: bool+++class FieldPlan(BaseModel):+ statuses: dict[SubscriptionStatus, int]+ changes: list[FieldChange]+ # Addresses MailerLite would refuse, such as a reserved domain.+ invalid: int+++def standing(subscriptions: list[Subscription]) -> tuple[SubscriptionStatus, Fields]:+ """The status and every date but signup. A paid subscription outranks a+ trial, and a live one outranks one that ended."""+ trials = [s for s in subscriptions if s.from_trial]+ trial_start = max((s.trial_start for s in trials if s.trial_start), default=None)+ dates: Fields = {+ SubscriberField.TRIAL_STARTED: _day(trial_start),+ SubscriberField.SUBSCRIPTION_STARTED: None,+ SubscriberField.SUBSCRIPTION_CANCELED: None,+ SubscriberField.SUBSCRIPTION_ENDED: None,+ }+ paid = [s for s in subscriptions if _ever_paid(s)]+ live = [s for s in paid if s.status in ("active", "past_due")]+ if live:+ current = _latest(live)+ dates[SubscriberField.SUBSCRIPTION_STARTED] = _day(_started(current))+ if current.cancel_at_period_end:+ dates[SubscriberField.SUBSCRIPTION_CANCELED] = _day(current.canceled_at)+ return SubscriptionStatus.SUBSCRIPTION_CANCELED, dates+ return SubscriptionStatus.SUBSCRIBED, dates+ trialing = [s for s in subscriptions if s.status == "trialing"]+ if any(not s.cancel_at_period_end for s in trialing):+ return SubscriptionStatus.IN_TRIAL, dates+ if trialing:+ return SubscriptionStatus.TRIAL_CANCELED, dates+ if paid:+ last = _latest(paid)+ dates[SubscriberField.SUBSCRIPTION_STARTED] = _day(_started(last))+ if last.cancel_at_period_end:+ dates[SubscriberField.SUBSCRIPTION_CANCELED] = _day(last.canceled_at)+ dates[SubscriberField.SUBSCRIPTION_ENDED] = _day(last.ended_at)+ return SubscriptionStatus.SUBSCRIPTION_ENDED, dates+ if trials:+ # Ended, or failed its first payment, without ever converting.+ return SubscriptionStatus.TRIAL_CANCELED, dates+ return SubscriptionStatus.SIGNED, dates+++def desired(person: Person) -> tuple[SubscriptionStatus, Fields]:+ """All six fields, so a run leaves nothing stale behind."""+ status, dates = standing(person.subscriptions)+ return status, {+ SubscriberField.STATUS: status.value,+ SubscriberField.SIGNUP: mailerlite_date(person.created_at),+ **dates,+ }+++def plan(people: list[Person], current: Current) -> FieldPlan:+ result = FieldPlan(+ statuses={s: 0 for s in SubscriptionStatus}, changes=[], invalid=0+ )+ for person in people:+ if not _valid(person.email):+ result.invalid += 1+ continue+ status, fields = desired(person)+ result.statuses[status] += 1+ held = current.get(person.email.strip().lower())+ differ = {+ key: value+ for key, value in fields.items()+ if held is None or _normalise(key, held.get(key.value)) != value+ }+ if differ:+ result.changes.append(+ FieldChange(+ person=person, status=status, fields=differ, new=held is None+ )+ )+ return result+++async def read_current() -> Current:+ """Our fields for every subscriber, in every status: an unsubscribed+ person still gets their fields, and must not be counted as new."""+ _require_token()+ keys = [f.value for f in SubscriberField]+ current: Current = {}+ for status in MEMBER_STATUSES:+ cursor: str | None = None+ seen: set[str] = set()+ while True:+ params = {"limit": str(PAGE_SIZE), "filter[status]": status}+ if cursor:+ params["cursor"] = cursor+ page = await _get_subscribers(params)+ for row in page.get("data") or []:+ fields = row.get("fields") or {}+ current[str(row["email"]).strip().lower()] = {+ k: fields.get(k) for k in keys+ }+ cursor = next_cursor(page, seen)+ if not cursor:+ break+ return current+++async def apply(+ changes: list[FieldChange],+ on_progress: Callable[[int, int], None] | None = None,+) -> tuple[int, int]:+ """Write each change as a subscriber upsert, fifty to a batch, paced for+ MailerLite's import limit. Returns (succeeded, failed); a failure is left+ for the next run."""+ succeeded = failed = 0+ for start in range(0, len(changes), BATCH_SIZE):+ 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):+ succeeded += 1+ continue+ failed += 1+ logger.warning(+ f"Field update failed for {_pseudonym(change.person.email)} "+ f"with {code}; the next run retries it"+ )+ if on_progress:+ on_progress(start + len(chunk), len(changes))+ return succeeded, failed+++def _upsert(change: FieldChange) -> dict:+ return {+ "method": "POST",+ "path": "api/subscribers",+ "body": {+ "email": change.person.email,+ "fields": {key.value: value for key, value in change.fields.items()},+ },+ }+++def _ever_paid(subscription: Subscription) -> bool:+ if subscription.from_trial:+ return subscription.converted or subscription.status == "active"+ return subscription.status not in _NEVER_STARTED+++def _started(subscription: Subscription) -> int | None:+ """A converted trial's paid subscription began when its trial ended."""+ if subscription.from_trial:+ return subscription.trial_end+ return subscription.start_date+++def _latest(subscriptions: list[Subscription]) -> Subscription:+ return max(subscriptions, key=lambda s: _started(s) or 0)+++def _day(timestamp: int | None) -> str | None:+ """Unlike a live event, a missing timestamp here is no reason to write+ today's date."""+ return mailerlite_date(timestamp) if timestamp is not None else None+++def _normalise(field: SubscriberField, value: object) -> str | None:+ if value is None or value == "":+ return None+ text = str(value)+ # A date may come back with a time part.+ return text[:10] if FIELD_TYPES[field] == "date" else text+++def _valid(email: str) -> bool:+ try:+ _EMAIL.validate_python(email)+ except ValidationError:+ return False+ return True+++async def _get_subscribers(params: dict[str, str]) -> dict:+ response = await _client().get(+ f"{API_BASE}/subscribers?{urlencode(params)}", headers=_headers()+ )+ if response.status != 200:+ raise MailerLiteError(+ f"Reading MailerLite subscribers failed with {response.status}"+ )+ return response.json() or {}diff --git a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.py b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.pynew file mode 100644index 0000000000..4680c1816d--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.py@@ -0,0 +1,270 @@+"""The field backfill gives every account the status and dates the live code+would have, writes only what MailerLite does not already hold, and so resumes+an interrupted run by simply running again."""++from datetime import UTC, datetime+from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock++import pytest++from backend.data.notifications import SubscriberField, SubscriptionStatus+from backend.notifications import mailerlite, mailerlite_backfill+from backend.notifications import mailerlite_field_backfill as backfill+from backend.notifications.mailerlite_backfill import Subscription+from backend.notifications.mailerlite_field_backfill import Person++S = SubscriptionStatus+F = SubscriberField+# 1 Aug, 1 Sep, 10 Sep and 20 Sep 2026, noon UTC.+AUG, SEP, SEP10, SEP20 = 1785585600, 1788264000, 1789041600, 1789905600+CREATED = datetime(2026, 6, 1, 9, 0, tzinfo=UTC)+++def _day(timestamp: int) -> str:+ return datetime.fromtimestamp(timestamp, UTC).strftime("%Y-%m-%d")+++def _paid(status: str, **over) -> Subscription:+ return Subscription(status=status, start_date=AUG, **over)+++def _trial(status: str, **over) -> Subscription:+ return Subscription(+ status=status, from_trial=True, trial_start=AUG, trial_end=SEP, **over+ )+++def _person(email: str = "[email protected]", *subscriptions: Subscription) -> Person:+ return Person(+ user_id=f"u-{email}",+ email=email,+ created_at=CREATED,+ subscriptions=list(subscriptions),+ )+++ "subscriptions, status",+ [+ ([], S.SIGNED),+ # Checkout never finished: nothing started.+ ([_paid("incomplete_expired")], S.SIGNED),+ ([_paid("active")], S.SUBSCRIBED),+ ([_paid("past_due")], S.SUBSCRIBED),+ ([_paid("active", cancel_at_period_end=True)], S.SUBSCRIPTION_CANCELED),+ ([_paid("canceled")], S.SUBSCRIPTION_ENDED),+ ([_paid("unpaid")], S.SUBSCRIPTION_ENDED),+ ([_trial("trialing")], S.IN_TRIAL),+ ([_trial("trialing", cancel_at_period_end=True)], S.TRIAL_CANCELED),+ # Its first payment never cleared.+ ([_trial("past_due")], S.TRIAL_CANCELED),+ ([_trial("canceled")], S.TRIAL_CANCELED),+ ([_trial("active")], S.SUBSCRIBED),+ ([_trial("canceled", converted=True)], S.SUBSCRIPTION_ENDED),+ # A paid subscription outranks a trial, and a live one an ended one.+ ([_paid("canceled"), _trial("trialing")], S.IN_TRIAL),+ ([_trial("canceled"), _paid("active")], S.SUBSCRIBED),+ ],+)+def test_standing(subscriptions, status):+ assert backfill.standing(subscriptions)[0] is status+++def test_every_field_is_set_so_nothing_stale_survives():+ status, fields = backfill.desired(_person("[email protected]"))+ assert status is S.SIGNED+ assert fields == {+ F.STATUS: "signed",+ F.SIGNUP: "2026-06-01",+ F.TRIAL_STARTED: None,+ F.SUBSCRIPTION_STARTED: None,+ F.SUBSCRIPTION_CANCELED: None,+ F.SUBSCRIPTION_ENDED: None,+ }+++def test_the_dates_come_from_stripe():+ _, fields = backfill.desired(+ _person(+ "[email protected]",+ _trial(+ "canceled",+ converted=True,+ cancel_at_period_end=True,+ canceled_at=SEP10,+ ended_at=SEP20,+ ),+ )+ )+ assert fields[F.TRIAL_STARTED] == _day(AUG)+ # A converted trial's paid subscription began when the trial ended.+ assert fields[F.SUBSCRIPTION_STARTED] == _day(SEP)+ assert fields[F.SUBSCRIPTION_CANCELED] == _day(SEP10)+ assert fields[F.SUBSCRIPTION_ENDED] == _day(SEP20)+++def test_a_missing_timestamp_is_left_empty_not_today():+ _, fields = backfill.desired(_person("[email protected]", Subscription(status="canceled")))+ assert fields[F.SUBSCRIPTION_ENDED] is None+++def test_only_the_difference_is_written():+ held = {+ "[email protected]": {+ "subscription_status": "signed",+ # MailerLite may hand a date back with a time part.+ "signup_date": "2026-06-01 00:00:00",+ "trial_started_date": None,+ "subscription_started_date": "",+ "subscription_canceled_date": None,+ "subscription_ended_date": None,+ }+ }+ people = [_person("[email protected]"), _person("[email protected]", _paid("active"))]+ result = backfill.plan(people, held)+ assert [c.person.email for c in result.changes] == ["[email protected]"]+ [change] = result.changes+ assert change.new is True+ assert change.fields[F.STATUS] == "subscribed"+ assert result.statuses[S.SIGNED] == 1+ assert result.statuses[S.SUBSCRIBED] == 1+++def test_a_changed_status_writes_just_that_field():+ held = {"[email protected]": {k.value: v for k, v in backfill.desired(_person())[1].items()}}+ held["[email protected]"]["subscription_status"] = "in_trial"+ [change] = backfill.plan([_person()], held).changes+ assert change.fields == {F.STATUS: "signed"}+ assert change.new is False+++def test_an_address_mailerlite_would_refuse_is_counted_not_sent():+ result = backfill.plan([_person("[email protected]")], {})+ assert result.invalid == 1+ assert result.changes == []+++def test_a_second_run_finds_nothing_to_do():+ people = [_person("[email protected]"), _person("[email protected]", _trial("trialing"))]+ first = backfill.plan(people, {})+ held = {+ c.person.email: {k.value: v for k, v in c.fields.items()} for c in first.changes+ }+ assert backfill.plan(people, held).changes == []+++def configured(monkeypatch):+ fake = SimpleNamespace(secrets=SimpleNamespace(mailerlite_api_token="token"))+ monkeypatch.setattr(mailerlite, "settings", fake)+ sleep = AsyncMock()+ monkeypatch.setattr(backfill.asyncio, "sleep", sleep)+ return sleep+++def _response(status: int, body: dict) -> MagicMock:+ response = MagicMock(status=status)+ response.json.return_value = body+ return response+++async def test_apply_batches_upserts_at_the_import_pace(configured, monkeypatch):+ client = MagicMock()+ client.post = AsyncMock(+ side_effect=lambda url, **kw: _response(+ 200,+ {"responses": [{"code": 200} for _ in kw["json"]["requests"]]},+ )+ )+ monkeypatch.setattr(mailerlite_backfill, "_client", lambda: client)+ changes = backfill.plan([_person(f"p{i}@x.io") for i in range(120)], {}).changes+ progress = []++ ok, failed = await backfill.apply(changes, lambda d, t: progress.append(d))++ batches = [c.kwargs["json"]["requests"] for c in client.post.await_args_list]+ assert [len(b) for b in batches] == [50, 50, 20]+ assert batches[0][0] == {+ "method": "POST",+ "path": "api/subscribers",+ "body": {+ "email": "[email protected]",+ "fields": {k.value: v for k, v in changes[0].fields.items()},+ },+ }+ assert [c.args[0] for c in configured.await_args_list] == [+ mailerlite_backfill.UPSERT_BATCH_INTERVAL_SECONDS+ ] * 2+ assert (ok, failed) == (120, 0)+ assert progress == [50, 100, 120]+++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}]}))+ monkeypatch.setattr(mailerlite_backfill, "_client", lambda: client)+ changes = backfill.plan([_person("[email protected]")], {}).changes++ assert await backfill.apply(changes) == (0, 1)+ assert "[email protected]" not in caplog.text+++async def test_read_current_pages_every_status(configured, monkeypatch):+ pages = {+ ("active", None): {+ "data": [+ {+ "email": "[email protected]",+ "fields": {"subscription_status": "signed", "city": "Leeds"},+ }+ ],+ "meta": {"next_cursor": "c2"},+ },+ ("active", "c2"): {"data": [{"email": "[email protected]", "fields": {}}], "meta": {}},+ ("unsubscribed", None): {+ "data": [{"email": "[email protected]", "fields": None}],+ "meta": {},+ },+ }++ async def get(url, **kw):+ _, _, query = url.partition("?")+ params = dict(p.split("=", 1) for p in query.split("&"))+ key = (params["filter%5Bstatus%5D"], params.get("cursor"))+ return _response(200, pages.get(key, {"data": [], "meta": {}}))++ client = MagicMock(get=AsyncMock(side_effect=get))+ monkeypatch.setattr(backfill, "_client", lambda: client)++ current = await backfill.read_current()++ assert current["[email protected]"]["subscription_status"] == "signed"+ assert "city" not in current["[email protected]"]+++async def test_read_current_stops_on_a_repeated_cursor(configured, monkeypatch):+ """As for the audience read: a repeated cursor is an error, not the end."""+ page = {"data": [{"email": "[email protected]", "fields": {}}], "meta": {"next_cursor": "c2"}}+ calls = 0++ async def get(url, **kw):+ nonlocal calls+ calls += 1+ if calls > 10:+ raise AssertionError("the reader requested the same page forever")+ return _response(200, page)++ client = MagicMock(get=AsyncMock(side_effect=get))+ monkeypatch.setattr(backfill, "_client", lambda: client)++ with pytest.raises(mailerlite.MailerLiteError, match="repeated"):+ await backfill.read_current()+ assert client.get.await_count == 2diff --git a/autogpt_platform/backend/backend/notifications/notifications.py b/autogpt_platform/backend/backend/notifications/notifications.pyindex 9b2e5ad5b2..ab572521c5 100644--- a/autogpt_platform/backend/backend/notifications/notifications.py+++ b/autogpt_platform/backend/backend/notifications/notifications.py@@ -63,6 +63,8 @@ from backend.util.settings import Settingslogger = TruncatedLogger(logging.getLogger(__name__), "[NotificationManager]")settings = Settings()+# The missing MailerLite token is logged once per process, not per message.+_mailerlite_off_logged = Falsedef _utc_today() -> date:@@ -364,13 +366,31 @@ class NotificationManager(AppService):except ValueError as e:logger.warning(f"Unparseable audience change (sending to DLQ): {e}")return False+ if not mailerlite.configured():+ global _mailerlite_off_logged+ if not _mailerlite_off_logged:+ logger.info(+ "MAILERLITE_API_TOKEN is not set; dropping audience changes"+ )+ _mailerlite_off_logged = True+ return Truehandler = {AudienceAction.ENROLL_TOUR: mailerlite.enroll_in_onboarding,AudienceAction.ADD_CHANGELOG: mailerlite.add_to_changelog,AudienceAction.REMOVE_CHANGELOG: mailerlite.remove_from_changelog,+ AudienceAction.ADD_TRIAL: mailerlite.add_to_trial,+ AudienceAction.REMOVE_TRIAL: mailerlite.remove_from_trial,+ AudienceAction.UPDATE_FIELDS: mailerlite.update_fields,+ AudienceAction.SIGNUP: mailerlite.record_signup,}[event.action]- await handler(event.email)+ try:+ await handler(event.email, event.fields or None)+ except mailerlite.MailerLiteNotConfigured as e:+ # A group this change needs has no ID. No retry makes a setting+ # appear, so it is dead-lettered now, to be replayed once it is set.+ logger.error(f"{e}; sending {event.action.value} to the DLQ")+ return Falsereturn Truedef _parse_message(self, message: str) -> NotificationEventModel | None:diff --git a/autogpt_platform/backend/backend/notifications/subscriber_fields.py b/autogpt_platform/backend/backend/notifications/subscriber_fields.pynew file mode 100644index 0000000000..1633d2626e--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/subscriber_fields.py@@ -0,0 +1,149 @@+"""Where each person stands with us, as MailerLite subscriber fields.++GTM segments on one status and five dates (see `SubscriberField`). Each+transition below sets the status and the date it is named for, from Stripe's+own timestamp where the event carries one. The dates describe the current or+most recent subscription: a new subscription clears the previous one's+cancellation and end, and a resumed one clears its cancellation. The signup+and trial start dates are never cleared.++A trial that ends without its first payment clearing, whether it was cancelled+or the payment failed, is `trial_canceled`: the person never had a paid+subscription, so `subscription_ended` would count them as churned customers.+`in_trial` is therefore exactly the trial group's membership.+"""++import logging+from datetime import UTC, datetime++from pydantic import ValidationError++from backend.data.notifications import (+ AudienceAction,+ AudienceEventModel,+ SubscriberField,+ SubscriptionStatus,+)+from backend.notifications.queue import queue_audience_change++logger = logging.getLogger(__name__)++Fields = dict[SubscriberField, str | None]+++def mailerlite_date(value: datetime | int | float | None) -> str:+ """A Stripe timestamp or a datetime as a MailerLite date, in UTC. Only an+ event without a timestamp of its own falls back to now."""+ if value is None:+ moment = datetime.now(UTC)+ elif isinstance(value, datetime):+ moment = value if value.tzinfo else value.replace(tzinfo=UTC)+ else:+ moment = datetime.fromtimestamp(value, UTC)+ return moment.astimezone(UTC).strftime("%Y-%m-%d")+++def signed(created_at: datetime) -> Fields:+ return {+ SubscriberField.STATUS: SubscriptionStatus.SIGNED.value,+ SubscriberField.SIGNUP: mailerlite_date(created_at),+ }+++def trial_started(trial_start: int | None) -> Fields:+ return {+ SubscriberField.STATUS: SubscriptionStatus.IN_TRIAL.value,+ SubscriberField.TRIAL_STARTED: mailerlite_date(trial_start),+ }+++def trial_resumed() -> Fields:+ return {SubscriberField.STATUS: SubscriptionStatus.IN_TRIAL.value}+++def trial_canceled() -> Fields:+ return {SubscriberField.STATUS: SubscriptionStatus.TRIAL_CANCELED.value}+++def subscribed(started: int | None) -> Fields:+ return {+ SubscriberField.STATUS: SubscriptionStatus.SUBSCRIBED.value,+ SubscriberField.SUBSCRIPTION_STARTED: mailerlite_date(started),+ SubscriberField.SUBSCRIPTION_CANCELED: None,+ SubscriberField.SUBSCRIPTION_ENDED: None,+ }+++def subscription_resumed() -> Fields:+ return {+ SubscriberField.STATUS: SubscriptionStatus.SUBSCRIBED.value,+ SubscriberField.SUBSCRIPTION_CANCELED: None,+ }+++def subscription_canceled(canceled_at: int | None) -> Fields:+ return {+ SubscriberField.STATUS: SubscriptionStatus.SUBSCRIPTION_CANCELED.value,+ SubscriberField.SUBSCRIPTION_CANCELED: mailerlite_date(canceled_at),+ }+++def subscription_ended(ended_at: int | None) -> Fields:+ return {+ SubscriberField.STATUS: SubscriptionStatus.SUBSCRIPTION_ENDED.value,+ SubscriberField.SUBSCRIPTION_ENDED: mailerlite_date(ended_at),+ }+++def audience_event(+ action: AudienceAction, email: str, user_id: str, fields: Fields | None = None+) -> AudienceEventModel | None:+ """None for an address MailerLite would refuse, such as a reserved domain:+ no retry can ever deliver that change, so it must not hold anything up.++ Whether MailerLite is configured is not asked here: the API server that+ queues these never holds its settings. The notification service decides+ (see `NotificationManager._process_audience_change`)."""+ try:+ return AudienceEventModel(+ action=action,+ email=email,+ user_id=user_id,+ fields=fields or {},+ )+ except ValidationError:+ logger.warning(+ f"User {user_id}'s email cannot be a MailerLite subscriber; "+ f"skipping {action.value}"+ )+ return None+++async def queue_fields(+ user_id: str,+ email: str,+ fields: Fields,+ action: AudienceAction = AudienceAction.UPDATE_FIELDS,+) -> None:+ """Queue a field update with no group change. Reports rather than raises:+ it follows a billing email that is already out, and a status must never+ cost one."""+ event = audience_event(action, email, user_id, fields)+ if event is None:+ return+ try:+ result = await queue_audience_change(event)+ except Exception:+ logger.exception(f"Could not queue MailerLite fields for user {user_id}")+ return+ if not result.success:+ logger.error(+ f"Could not queue MailerLite fields for user {user_id}: {result.message}"+ )+++async def queue_signup(user_id: str, email: str, created_at: datetime) -> None:+ """A new account: `signed`, with the day it was created. It is queued+ from a background task, so nothing orders it before the account's first+ checkout; the notification service keeps it from undoing one."""+ await queue_fields(user_id, email, signed(created_at), AudienceAction.SIGNUP)diff --git a/autogpt_platform/backend/backend/notifications/subscriber_fields_test.py b/autogpt_platform/backend/backend/notifications/subscriber_fields_test.pynew file mode 100644index 0000000000..9cd5f47174--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/subscriber_fields_test.py@@ -0,0 +1,525 @@+"""Every account carries its status and dates into MailerLite, from signup on,+and neither a MailerLite outage nor an address it refuses ever costs the+signup or the billing email the update rides along with."""++from datetime import UTC, datetime+from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.data import user as user_data+from backend.data.notifications import (+ AudienceAction,+ NotificationResult,+ SubscriberField,+ SubscriptionStatus,+)+from backend.notifications import mailerlite+from backend.notifications import notifications as delivery+from backend.notifications import subscriber_fields+from backend.notifications.notifications import NotificationManager++EMAIL = "[email protected]"+++def fields_on(monkeypatch):+ queued = AsyncMock(return_value=NotificationResult(success=True))+ monkeypatch.setattr(subscriber_fields, "queue_audience_change", queued)+ return queued+++def test_a_stripe_timestamp_is_its_utc_day():+ # 23:30 UTC on 1 Sep 2026 is already 2 Sep further east; MailerLite gets UTC.+ assert subscriber_fields.mailerlite_date(1788305400) == "2026-09-01"+ assert (+ subscriber_fields.mailerlite_date(datetime(2026, 6, 24, 23, 59, tzinfo=UTC))+ == "2026-06-24"+ )+++def test_a_naive_datetime_is_read_as_utc():+ assert subscriber_fields.mailerlite_date(datetime(2026, 6, 24, 1, 0)) == (+ "2026-06-24"+ )+++def test_a_new_subscription_clears_the_last_ones_cancellation_and_end():+ assert subscriber_fields.subscribed(1788305400) == {+ SubscriberField.STATUS: "subscribed",+ SubscriberField.SUBSCRIPTION_STARTED: "2026-09-01",+ SubscriberField.SUBSCRIPTION_CANCELED: None,+ SubscriberField.SUBSCRIPTION_ENDED: None,+ }+++async def test_a_signup_queues_signed_with_the_account_creation_day(fields_on):+ await subscriber_fields.queue_signup(+ "user-1", EMAIL, datetime(2026, 9, 29, 10, 0, tzinfo=UTC)+ )+ event = fields_on.await_args.args[0]+ assert event.action is AudienceAction.SIGNUP+ assert event.fields == {+ SubscriberField.STATUS: SubscriptionStatus.SIGNED.value,+ SubscriberField.SIGNUP: "2026-09-29",+ }+++async def test_a_signup_is_queued_without_any_mailerlite_settings(fields_on):+ """Signup runs in the API server, which never holds the MailerLite token:+ the notification service decides whether it goes anywhere."""+ await subscriber_fields.queue_signup("user-1", EMAIL, datetime.now(UTC))+ assert fields_on.await_args.args[0].fields[SubscriberField.STATUS] == "signed"+++async def test_a_reserved_domain_is_skipped_not_retried(fields_on):+ await subscriber_fields.queue_signup("user-1", "[email protected]", datetime.now(UTC))+ fields_on.assert_not_awaited()+++ "failure",+ [+ AsyncMock(return_value=NotificationResult(success=False, message="down")),+ AsyncMock(side_effect=RuntimeError("broker gone")),+ ],+)+async def test_a_queue_failure_is_reported_never_raised(+ monkeypatch, fields_on, failure+):+ monkeypatch.setattr(subscriber_fields, "queue_audience_change", failure)+ await subscriber_fields.queue_signup("user-1", EMAIL, datetime.now(UTC))+ failure.assert_awaited_once()+++# ── signup hook ────────────────────────────────────────────────────────────+++def _prisma(existing):+ created = SimpleNamespace(+ id="user-1",+ email=EMAIL,+ createdAt=datetime(2026, 9, 29, tzinfo=UTC),+ name=None,+ )+ return (+ MagicMock(+ user=MagicMock(+ find_unique=AsyncMock(return_value=existing),+ create=AsyncMock(return_value=created),+ )+ ),+ created,+ )+++async def _get_or_create(existing, queue_signup):+ prisma, created = _prisma(existing)+ with (+ patch.object(user_data, "prisma", prisma),+ patch.object(user_data, "_ensure_user_profile", AsyncMock()),+ patch.object(user_data, "ensure_personal_org", AsyncMock()),+ patch.object(user_data.User, "from_db", MagicMock()),+ patch.object(user_data, "UserCreationResult", MagicMock()),+ patch.object(user_data, "queue_signup", queue_signup),+ ):+ await user_data.get_or_create_user_with_status(+ {"sub": "user-1", "email": EMAIL}+ )+ # Let the background task run.+ for task in list(user_data._signup_sync_tasks):+ await task+ return created+++async def test_a_new_account_is_queued_for_mailerlite():+ queue_signup = AsyncMock()+ created = await _get_or_create(None, queue_signup)+ queue_signup.assert_awaited_once_with("user-1", EMAIL, created.createdAt)+++async def test_an_existing_account_is_not_queued_again():+ queue_signup = AsyncMock()+ await _get_or_create(SimpleNamespace(id="user-1", email=EMAIL), queue_signup)+ queue_signup.assert_not_awaited()+++async def test_signup_never_fails_because_the_sync_does():+ def broken(*_):+ raise RuntimeError("no event loop for you")++ await _get_or_create(None, MagicMock(side_effect=broken))+++# ── the MailerLite client ──────────────────────────────────────────────────+++def mailerlite_configured(monkeypatch):+ fake = SimpleNamespace(+ config=SimpleNamespace(+ mailerlite_changelog_group_id="grp_changelog",+ mailerlite_trial_group_id="",+ ),+ secrets=SimpleNamespace(mailerlite_api_token="token"),+ )+ monkeypatch.setattr(mailerlite, "settings", fake)+ monkeypatch.setattr(mailerlite, "_fields_ready", False)+ return fake+++def _response(status: int, body: dict | None = None) -> MagicMock:+ response = MagicMock(status=status)+ response.json.return_value = body or {}+ return response+++def _all_fields() -> dict:+ return {+ "data": [+ {"key": f.value, "type": t} for f, t in mailerlite.FIELD_TYPES.items()+ ],+ "meta": {"last_page": 1},+ }+++STATUS = {+ SubscriberField.STATUS: "subscribed",+ SubscriberField.SUBSCRIPTION_ENDED: None,+}+++async def test_a_group_add_carries_the_fields_in_the_same_upsert(+ mailerlite_configured,+):+ client = MagicMock(+ get=AsyncMock(return_value=_response(200, _all_fields())),+ post=AsyncMock(return_value=_response(200)),+ )+ with patch.object(mailerlite, "_client", return_value=client):+ await mailerlite.add_to_changelog(EMAIL, STATUS)+ assert client.post.await_args.kwargs["json"] == {+ "email": EMAIL,+ "groups": ["grp_changelog"],+ "fields": {+ "subscription_status": "subscribed",+ "subscription_ended_date": None,+ },+ }+++async def test_missing_fields_are_created_once_before_the_first_write(+ mailerlite_configured,+):+ client = MagicMock(+ get=AsyncMock(return_value=_response(200, {"data": [], "meta": {}})),+ post=AsyncMock(+ side_effect=lambda url, **kw: _response(+ 201,+ (+ {"data": {"key": kw["json"].get("name")}}+ if url.endswith("/fields")+ else {}+ ),+ )+ ),+ )+ with patch.object(mailerlite, "_client", return_value=client):+ await mailerlite.update_fields(EMAIL, STATUS)+ await mailerlite.update_fields(EMAIL, STATUS)+ created = [+ c.kwargs["json"]+ for c in client.post.await_args_list+ if c.args[0].endswith("/fields")+ ]+ assert created == [+ {"name": f.value, "type": t} for f, t in mailerlite.FIELD_TYPES.items()+ ]+ client.get.assert_awaited_once()+ upserts = [+ c for c in client.post.await_args_list if c.args[0].endswith("/subscribers")+ ]+ assert len(upserts) == 2+ assert "groups" not in upserts[0].kwargs["json"]+++async def test_a_field_under_another_key_fails_loudly(mailerlite_configured):+ client = MagicMock(+ get=AsyncMock(return_value=_response(200, {"data": [], "meta": {}})),+ post=AsyncMock(return_value=_response(201, {"data": {"key": "status_2"}})),+ )+ with (+ patch.object(mailerlite, "_client", return_value=client),+ pytest.raises(mailerlite.MailerLiteError),+ ):+ await mailerlite.update_fields(EMAIL, STATUS)+++async def test_reading_fields_stops_on_an_empty_page(mailerlite_configured):+ """An empty page is the end, whatever last_page claims."""+ client = MagicMock(+ get=AsyncMock(+ side_effect=[+ _response(200, _all_fields() | {"meta": {"last_page": 1000}}),+ _response(200, {"data": [], "meta": {"last_page": 1000}}),+ ]+ + [AssertionError("requested a page after an empty one")] * 1000+ ),+ )+ with patch.object(mailerlite, "_client", return_value=client):+ fields = await mailerlite.read_fields()+ assert set(fields) == {f.value for f in mailerlite.FIELD_TYPES}+ assert client.get.await_count == 2+++async def test_reading_fields_stops_when_last_page_keeps_growing(+ mailerlite_configured, monkeypatch+):+ monkeypatch.setattr(mailerlite, "_MAX_FIELD_PAGES", 3)+ calls = 0++ def page(url, **kw):+ nonlocal calls+ calls += 1+ if calls > 10:+ raise AssertionError("still paging")+ return _response(+ 200,+ {+ "data": [{"key": f"f{calls}", "type": "text"}],+ "meta": {"last_page": calls + 1},+ },+ )++ client = MagicMock(get=AsyncMock(side_effect=page))+ with (+ patch.object(mailerlite, "_client", return_value=client),+ pytest.raises(mailerlite.MailerLiteError, match="pages"),+ ):+ await mailerlite.read_fields()+ assert calls == 3+++async def test_fields_land_even_while_the_group_is_unconfigured(+ mailerlite_configured,+):+ """The status is not the group's to hold hostage: it is written, and the+ group change is then dead-lettered (see below)."""+ mailerlite_configured.config.mailerlite_changelog_group_id = ""+ client = MagicMock(+ get=AsyncMock(return_value=_response(200, _all_fields())),+ post=AsyncMock(return_value=_response(200)),+ )+ with (+ patch.object(mailerlite, "_client", return_value=client),+ pytest.raises(mailerlite.MailerLiteNotConfigured),+ ):+ await mailerlite.add_to_changelog(EMAIL, STATUS)+ assert client.post.await_args.kwargs["json"]["fields"] == {+ "subscription_status": "subscribed",+ "subscription_ended_date": None,+ }+++ "change", [mailerlite.add_to_trial, mailerlite.remove_from_trial]+)+async def test_without_a_trial_group_only_the_fields_are_written(+ mailerlite_configured, change+):+ """The trial group is optional, so a trial change without one writes the+ status and is done, instead of retrying into the dead-letter queue."""+ client = MagicMock(+ get=AsyncMock(return_value=_response(200, _all_fields())),+ post=AsyncMock(return_value=_response(200)),+ delete=AsyncMock(),+ )+ with patch.object(mailerlite, "_client", return_value=client):+ await change(EMAIL, STATUS)+ assert client.post.await_args.kwargs["json"] == {+ "email": EMAIL,+ "fields": {+ "subscription_status": "subscribed",+ "subscription_ended_date": None,+ },+ }+ client.delete.assert_not_awaited()+++async def test_a_removal_writes_the_fields_too(mailerlite_configured):+ client = MagicMock(+ get=AsyncMock(+ side_effect=[+ _response(200, _all_fields()),+ _response(200, {"data": {"id": "ml_1"}}),+ ]+ ),+ post=AsyncMock(return_value=_response(200)),+ delete=AsyncMock(return_value=_response(204)),+ )+ with patch.object(mailerlite, "_client", return_value=client):+ await mailerlite.remove_from_changelog(EMAIL, STATUS)+ assert client.post.await_args.kwargs["json"]["fields"]["subscription_status"] == (+ "subscribed"+ )+ client.delete.assert_awaited_once()+++async def test_no_fields_means_no_field_calls(mailerlite_configured):+ client = MagicMock(post=AsyncMock(return_value=_response(201)))+ with patch.object(mailerlite, "_client", return_value=client):+ await mailerlite.add_to_changelog(EMAIL)+ await mailerlite.update_fields(EMAIL, {})+ assert client.post.await_args.kwargs["json"] == {+ "email": EMAIL,+ "groups": ["grp_changelog"],+ }+ client.post.assert_awaited_once()+++# ── ordering against the checkout ──────────────────────────────────────────+++class _FakeMailerLite:+ """Subscribers as MailerLite holds them: an upsert merges its fields into+ whatever is already there, so the last write wins."""++ def __init__(self, held: dict[str, dict] | None = None):+ self.subscribers = held or {}++ async def get(self, url: str, **_) -> MagicMock:+ if "/fields" in url:+ return _response(200, _all_fields())+ held = self.subscribers.get(url.rsplit("/", 1)[-1])+ if held is None:+ return _response(404)+ return _response(200, {"data": {"id": "ml_1", "fields": dict(held)}})++ async def post(self, url: str, json: dict, **_) -> MagicMock:+ held = self.subscribers.setdefault(json["email"], {})+ held.update(json.get("fields") or {})+ return _response(200)+++async def _consume(*events) -> None:+ for event in events:+ assert await NotificationManager._process_audience_change(+ MagicMock(), event.model_dump_json()+ )+++async def _signup_event(fields_on):+ await subscriber_fields.queue_signup(+ "user-1", EMAIL, datetime(2026, 9, 29, 10, 0, tzinfo=UTC)+ )+ return fields_on.await_args.args[0]+++def _checkout_event():+ return subscriber_fields.audience_event(+ AudienceAction.ENROLL_TOUR,+ EMAIL,+ "user-1",+ subscriber_fields.subscribed(1788305400),+ )+++async def test_a_signup_synced_after_the_checkout_does_not_undo_it(+ mailerlite_configured, fields_on+):+ """The signup is queued from a background task, so nothing orders it+ before the account's first checkout. Landing second, its `signed` must not+ replace the `subscribed` the checkout wrote; the signup date still lands."""+ mailerlite_configured.config.mailerlite_onboarding_group_id = "grp_tour"+ signup = await _signup_event(fields_on)+ ml = _FakeMailerLite()+ with patch.object(mailerlite, "_client", return_value=ml):+ await _consume(_checkout_event(), signup)+ assert ml.subscribers[EMAIL]["subscription_status"] == "subscribed"+ assert ml.subscribers[EMAIL]["signup_date"] == "2026-09-29"+++async def test_a_signup_in_order_is_overtaken_by_the_checkout(+ mailerlite_configured, fields_on+):+ mailerlite_configured.config.mailerlite_onboarding_group_id = "grp_tour"+ signup = await _signup_event(fields_on)+ ml = _FakeMailerLite()+ with patch.object(mailerlite, "_client", return_value=ml):+ await _consume(signup)+ assert ml.subscribers[EMAIL]["subscription_status"] == "signed"+ await _consume(_checkout_event())+ assert ml.subscribers[EMAIL]["subscription_status"] == "subscribed"+++async def test_a_signup_gives_a_subscriber_with_no_status_signed(+ mailerlite_configured, fields_on+):+ """Someone already on the newsletter has no status of ours yet."""+ signup = await _signup_event(fields_on)+ ml = _FakeMailerLite({EMAIL: {"subscription_status": None, "city": "Leeds"}})+ with patch.object(mailerlite, "_client", return_value=ml):+ await _consume(signup)+ assert ml.subscribers[EMAIL]["subscription_status"] == "signed"+++# ── a required group without an ID ─────────────────────────────────────────+++ "action", [AudienceAction.ENROLL_TOUR, AudienceAction.REMOVE_CHANGELOG]+)+async def test_a_missing_required_group_is_dead_lettered_at_once(+ mailerlite_configured, monkeypatch, caplog, action+):+ """No retry makes a setting appear, so the change goes to the dead-letter+ queue on the first attempt, to be replayed once the ID is set. The fields+ are written once and the problem is said once."""+ mailerlite_configured.config.mailerlite_onboarding_group_id = ""+ mailerlite_configured.config.mailerlite_changelog_group_id = ""+ event = subscriber_fields.audience_event(+ action, EMAIL, "user-1", subscriber_fields.subscribed(1788305400)+ )+ message = MagicMock(+ body=event.model_dump_json().encode(), ack=AsyncMock(), reject=AsyncMock()+ )+ ml = _FakeMailerLite()+ ml.post = AsyncMock(side_effect=ml.post)+ sleep = AsyncMock()+ monkeypatch.setattr(delivery.asyncio, "sleep", sleep)+ with (+ patch.object(mailerlite, "_client", return_value=ml),+ caplog.at_level("WARNING"),+ ):+ manager = NotificationManager.__new__(NotificationManager)+ await manager._process_message_with_retry(+ message, manager._process_audience_change, delivery.AUDIENCE_QUEUE+ )+ message.reject.assert_awaited_once_with(requeue=False)+ message.ack.assert_not_awaited()+ sleep.assert_not_awaited()+ assert ml.post.await_count == 1+ assert ml.subscribers[EMAIL]["subscription_status"] == "subscribed"+ assert caplog.text.count("group ID is not configured") == 1+ assert EMAIL not in caplog.textdiff --git a/autogpt_platform/backend/backend/notifications/trial.py b/autogpt_platform/backend/backend/notifications/trial.pyindex 9b10e96e87..bf89d25334 100644--- a/autogpt_platform/backend/backend/notifications/trial.py+++ b/autogpt_platform/backend/backend/notifications/trial.py@@ -24,6 +24,10 @@ from backend.data.subscription_trial import TrialStatefrom backend.notifications.dedupe import claim_once, release_claimfrom backend.notifications.lifecycle_plan import format_amountfrom backend.notifications.queue import queue_notification_async+from backend.notifications.trial_audience import (+ join_paying_audience,+ queue_trial_audience_change,+)from backend.util.posthog_events import PostHogEventlogger = logging.getLogger(__name__)@@ -84,6 +88,7 @@ async def notify_trial(subscription: dict, kind: TrialNoticeKind) -> bool:if not await claim_once(claim):return Truetry:+ await queue_trial_audience_change(kind, user_id, user.email, current)result = await queue_notification_async(NotificationEventModel[TrialUpdateData](user_id=user_id, type=NotificationType.TRIAL_UPDATE, data=data@@ -94,6 +99,8 @@ async def notify_trial(subscription: dict, kind: TrialNoticeKind) -> bool:except Exception:await release_claim(claim)raise+ if kind == "converted":+ await join_paying_audience(user_id, user.email)_track_billing_event(TRIAL_NOTICE_EVENTS[kind],user_id,diff --git a/autogpt_platform/backend/backend/notifications/trial_audience.py b/autogpt_platform/backend/backend/notifications/trial_audience.pynew file mode 100644index 0000000000..510522e28d--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/trial_audience.py@@ -0,0 +1,94 @@+"""The MailerLite trial group holds exactly the customers currently on a trial.++Every transition in and out of a trial already produces exactly one trial+notice, deduped by its own claim, so the group change rides on that notice:+join on start and on un-cancel, leave on cancel, conversion, failed conversion+and end. The backend owns both edges of this group (see `mailerlite.py`).++A conversion is also the customer's first paid subscription, so it joins the+paying audience the way a first checkout does.++The subscriber's status and dates (`subscriber_fields.py`) ride on the same+event. The notification service writes only the fields while the trial group+is not configured.+"""++import logging+from collections.abc import Callable, Mapping++from backend.data.db_accessors import user_db+from backend.data.notifications import AudienceAction+from backend.notifications import subscriber_fields+from backend.notifications.queue import queue_audience_change+from backend.notifications.subscriber_fields import Fields, audience_event++logger = logging.getLogger(__name__)++_TRIAL_GROUP_CHANGES: Mapping[str, AudienceAction] = {+ "started": AudienceAction.ADD_TRIAL,+ "resumed": AudienceAction.ADD_TRIAL,+ "canceled": AudienceAction.REMOVE_TRIAL,+ "converted": AudienceAction.REMOVE_TRIAL,+ "payment_failed": AudienceAction.REMOVE_TRIAL,+ "ended": AudienceAction.REMOVE_TRIAL,+}++# Each from the live Stripe subscription at the moment the notice applies.+_TRIAL_FIELDS: Mapping[str, Callable[[dict], Fields]] = {+ "started": lambda sub: subscriber_fields.trial_started(sub.get("trial_start")),+ "resumed": lambda _: subscriber_fields.trial_resumed(),+ "canceled": lambda _: subscriber_fields.trial_canceled(),+ "payment_failed": lambda _: subscriber_fields.trial_canceled(),+ "ended": lambda _: subscriber_fields.trial_canceled(),+ # The trial's end is when the first paid period began.+ "converted": lambda sub: subscriber_fields.subscribed(sub.get("trial_end")),+}+++async def queue_trial_audience_change(+ kind: str, user_id: str, email: str, subscription: dict+) -> None:+ """Queue the trial group change and field update for this notice, if it+ has either.++ Raises when it cannot be queued, so the caller releases the notice claim+ and Stripe's retry makes the change. Both are idempotent, so a retry that+ repeats one is harmless. It is queued whatever this process's MailerLite+ settings: only the notification service holds them.+ """+ action = _TRIAL_GROUP_CHANGES.get(kind)+ if action is None:+ return+ event = audience_event(action, email, user_id, _TRIAL_FIELDS[kind](subscription))+ if event is None:+ return+ result = await queue_audience_change(event)+ if not result.success:+ raise RuntimeError(f"Could not queue {action.value}: {result.message}")+++async def join_paying_audience(user_id: str, email: str) -> None:+ """The onboarding tour for a first subscription, else the changelog.++ The conversion notice stands in for the subscription welcome, so this takes+ the same welcome claim a first checkout does: a later resubscription is+ then treated as the returning customer it is. Called once the notice is+ out, so, like the tour enrolment after a welcome, a failure is reported+ rather than raised: a Stripe retry would find the notice claimed and do+ nothing, so raising could only fail the webhook.+ """+ try:+ first = await user_db().claim_welcome_email(user_id)+ action = AudienceAction.ENROLL_TOUR if first else AudienceAction.ADD_CHANGELOG+ event = audience_event(action, email, user_id)+ if event is None:+ return+ result = await queue_audience_change(event)+ except Exception:+ logger.exception(f"Trial for user {user_id} converted but was not enrolled")+ return+ if not result.success:+ logger.error(+ f"Trial for user {user_id} converted but {action.value} could not be "+ f"queued: {result.message}"+ )diff --git a/autogpt_platform/backend/backend/notifications/trial_audience_test.py b/autogpt_platform/backend/backend/notifications/trial_audience_test.pynew file mode 100644index 0000000000..8df26ee8ed--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/trial_audience_test.py@@ -0,0 +1,424 @@+"""The MailerLite trial group holds exactly the customers currently on a trial:+join on start and on un-cancel, leave on cancel, conversion, failed conversion+and end. A conversion also joins the paying audience, as a first checkout+would."""++from datetime import UTC, datetime, timedelta+from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.data.notifications import (+ AudienceAction,+ AudienceEventModel,+ NotificationResult,+ SubscriberField,+ SubscriptionStatus,+)+from backend.data.subscription_trial import TrialState+from backend.data.subscription_trial_config import AcceptedTrialOffer+from backend.notifications import mailerlite+from backend.notifications import notifications as delivery+from backend.notifications import trial as notices+from backend.notifications import trial_audience+from backend.notifications.notifications import NotificationManager++EMAIL = "[email protected]"+++def trial() -> TrialState:+ now = datetime.now(UTC)+ return TrialState(+ id="trial-1",+ user_id="user-1",+ customer_id="cus_1",+ offer=AcceptedTrialOffer(+ version="offer-v1",+ new_users_from=now - timedelta(days=1),+ duration_days=7,+ tier="PRO",+ billing_cycle="monthly",+ daily_cost_limit=250_000,+ weekly_cost_limit=1_000_000,+ total_cost_limit=1_000_000,+ onboarding_credit_amount=300,+ price_id="price_pro",+ unit_amount=2000,+ currency="usd",+ ),+ checkout_session_id="cs_1",+ subscription_id="sub_1",+ checkout_attempt=0,+ success_url="https://example.com",+ cancel_url="https://example.com",+ checkout_metadata={},+ status="trialing",+ card_verified_at=now,+ started_at=now,+ ends_at=now + timedelta(days=7),+ consumed_at=now,+ converted_at=None,+ cancel_at_period_end=False,+ cost_microdollars=0,+ )+++def _state(trial: TrialState, kind: str) -> tuple[TrialState, dict]:+ """A trial and the live Stripe subscription at the moment `kind` applies."""+ trial_end = int(trial.ends_at.timestamp())+ raw = {+ "id": trial.subscription_id,+ "customer": trial.customer_id,+ "status": "trialing",+ "trial_end": trial_end,+ "cancel_at_period_end": False,+ "metadata": {+ "trial_enrollment_id": trial.id,+ "user_id": trial.user_id,+ "trial_checkout_attempt": str(trial.checkout_attempt),+ },+ }+ if kind == "canceled":+ raw["cancel_at_period_end"] = True+ elif kind == "converted":+ raw["status"] = "active"+ trial = trial.model_copy(+ update={+ "converted_at": datetime.now(UTC),+ "conversion_invoice_id": "in_1",+ }+ )+ elif kind == "ended":+ raw["status"] = "canceled"+ elif kind == "payment_failed":+ raw["status"] = "past_due"+ return trial, raw+++async def _notify(+ trial,+ raw,+ kind,+ *,+ welcomed=False,+ claimed=True,+ audience=None,+ raises=False,+ email=EMAIL,+ claim_welcome=None,+):+ audience = audience or AsyncMock(return_value=NotificationResult(success=True))+ notice = AsyncMock(return_value=NotificationResult(success=True))+ user = SimpleNamespace(name="Sam", email=email)+ users = MagicMock(+ get_user_by_id=AsyncMock(return_value=user),+ claim_welcome_email=claim_welcome or AsyncMock(return_value=not welcomed),+ )+ with (+ patch.object(notices, "stripe_call", AsyncMock(return_value=raw)),+ patch.object(+ notices,+ "credit_db",+ return_value=MagicMock(+ get_subscription_trial=AsyncMock(return_value=trial)+ ),+ ),+ patch.object(notices, "user_db", return_value=users),+ patch.object(trial_audience, "user_db", return_value=users),+ patch.object(notices, "claim_once", AsyncMock(return_value=claimed)),+ patch.object(notices, "release_claim", AsyncMock()) as release,+ patch.object(notices, "queue_notification_async", notice),+ patch.object(trial_audience, "queue_audience_change", audience),+ patch.object(notices, "_track_billing_event"),+ ):+ if raises:+ with pytest.raises(RuntimeError):+ await notices.notify_trial(raw, kind)+ else:+ await notices.notify_trial(raw, kind)+ actions = [c.args[0].action for c in audience.await_args_list]+ return actions, notice, release+++ "kind, actions",+ [+ ("started", [AudienceAction.ADD_TRIAL]),+ ("canceled", [AudienceAction.REMOVE_TRIAL]),+ ("resumed", [AudienceAction.ADD_TRIAL]),+ ("ended", [AudienceAction.REMOVE_TRIAL]),+ ("payment_failed", [AudienceAction.REMOVE_TRIAL]),+ ("ending", []),+ ],+)+async def test_each_trial_transition_moves_the_trial_group(trial, kind, actions):+ trial, raw = _state(trial, kind)+ if kind == "ending":+ raw["trial_end"] = int((datetime.now(UTC) + timedelta(days=1)).timestamp())+ got, notice, _ = await _notify(trial, raw, kind)+ assert got == actions+ notice.assert_awaited_once()+++ "welcomed, paying",+ [(False, AudienceAction.ENROLL_TOUR), (True, AudienceAction.ADD_CHANGELOG)],+)+async def test_conversion_leaves_the_trial_and_joins_the_paying_audience(+ trial, welcomed, paying+):+ trial, raw = _state(trial, "converted")+ got, notice, _ = await _notify(trial, raw, "converted", welcomed=welcomed)+ assert got == [AudienceAction.REMOVE_TRIAL, paying]+ notice.assert_awaited_once()+++ "kind, actions",+ [+ ("started", [AudienceAction.ADD_TRIAL]),+ ("converted", [AudienceAction.REMOVE_TRIAL, AudienceAction.ENROLL_TOUR]),+ ],+)+async def test_the_api_server_queues_without_any_mailerlite_settings(+ trial, kind, actions+):+ """The API server, which handles the webhook, never holds the MailerLite+ token or group IDs: only the notification service does. Its changes must+ be queued anyway, with their fields, for the consumer to decide."""+ trial, raw = _state(trial, kind)+ audience = AsyncMock(return_value=NotificationResult(success=True))+ got, notice, _ = await _notify(trial, raw, kind, audience=audience)+ assert got == actions+ assert audience.await_args_list[0].args[0].fields+ notice.assert_awaited_once()+++async def test_a_replayed_event_changes_nothing(trial):+ trial, raw = _state(trial, "canceled")+ got, notice, _ = await _notify(trial, raw, "canceled", claimed=False)+ assert got == []+ notice.assert_not_awaited()+++async def test_a_failed_group_change_releases_the_claim_so_stripe_retries(trial):+ trial, raw = _state(trial, "canceled")+ failing = AsyncMock(return_value=NotificationResult(success=False, message="down"))+ _, notice, release = await _notify(+ trial, raw, "canceled", audience=failing, raises=True+ )+ release.assert_awaited_once_with(notices.trial_notice_key(trial, "canceled"))+ notice.assert_not_awaited()++[email protected]("kind", ["started", "converted"])+async def test_an_email_mailerlite_cannot_take_never_blocks_the_notice(trial, kind):+ """A reserved-domain address fails the audience event's email validation.+ That must cost the MailerLite change, not the customer's trial notice."""+ trial, raw = _state(trial, kind)+ got, notice, release = await _notify(trial, raw, kind, email="[email protected]")+ assert got == []+ notice.assert_awaited_once()+ release.assert_not_awaited()+++async def test_joining_the_paying_audience_never_fails_the_sent_conversion(trial):+ trial, raw = _state(trial, "converted")+ broken = AsyncMock(side_effect=RuntimeError("database unavailable"))+ got, notice, release = await _notify(trial, raw, "converted", claim_welcome=broken)+ assert got == [AudienceAction.REMOVE_TRIAL]+ notice.assert_awaited_once()+ release.assert_not_awaited()+++def consumer_configured(monkeypatch):+ monkeypatch.setattr(mailerlite, "configured", lambda: True)+++ "action, handler",+ [+ (AudienceAction.ADD_TRIAL, "add_to_trial"),+ (AudienceAction.REMOVE_TRIAL, "remove_from_trial"),+ ],+)+async def test_the_consumer_routes_trial_changes_to_mailerlite(+ consumer_configured, action, handler+):+ event = AudienceEventModel(action=action, email=EMAIL, user_id="user-1")+ with patch.object(mailerlite, handler, AsyncMock()) as called:+ assert await NotificationManager._process_audience_change(+ MagicMock(), event.model_dump_json()+ )+ called.assert_awaited_once_with(EMAIL, None)+++async def test_without_a_token_the_consumer_drops_changes_quietly(monkeypatch, caplog):+ """A self-hosted stack queues audience changes too. With no token they are+ acknowledged, not retried into the dead-letter queue, and said once."""+ monkeypatch.setattr(+ mailerlite,+ "settings",+ SimpleNamespace(secrets=SimpleNamespace(mailerlite_api_token="")),+ )+ monkeypatch.setattr(delivery, "_mailerlite_off_logged", False)+ event = AudienceEventModel(+ action=AudienceAction.UPDATE_FIELDS,+ email=EMAIL,+ user_id="user-1",+ fields={SubscriberField.STATUS: "signed"},+ )+ with (+ patch.object(mailerlite, "update_fields", AsyncMock()) as called,+ caplog.at_level("INFO"),+ ):+ for _ in range(2):+ assert await NotificationManager._process_audience_change(+ MagicMock(), event.model_dump_json()+ )+ called.assert_not_awaited()+ assert caplog.text.count("MAILERLITE_API_TOKEN is not set") == 1+++def mailerlite_configured(monkeypatch):+ fake = SimpleNamespace(+ config=SimpleNamespace(mailerlite_trial_group_id="grp_trial"),+ secrets=SimpleNamespace(mailerlite_api_token="token"),+ )+ monkeypatch.setattr(mailerlite, "settings", fake)+ return fake+++def _response(status: int, body: dict | None = None) -> MagicMock:+ response = MagicMock(status=status)+ response.json.return_value = body or {}+ return response+++async def test_add_to_trial_upserts_into_the_trial_group(mailerlite_configured):+ client = MagicMock(post=AsyncMock(return_value=_response(201)))+ with patch.object(mailerlite, "_client", return_value=client):+ await mailerlite.add_to_trial(EMAIL)+ assert client.post.await_args.kwargs["json"] == {+ "email": EMAIL,+ "groups": ["grp_trial"],+ }++[email protected]("status", [204, 404])+async def test_remove_from_trial_unassigns_and_treats_gone_as_done(+ mailerlite_configured, status+):+ client = MagicMock(+ get=AsyncMock(return_value=_response(200, {"data": {"id": "ml_1"}})),+ delete=AsyncMock(return_value=_response(status)),+ )+ with patch.object(mailerlite, "_client", return_value=client):+ await mailerlite.remove_from_trial(EMAIL)+ assert client.delete.await_args.args[0].endswith(+ "/subscribers/ml_1/groups/grp_trial"+ )+++async def test_remove_from_trial_fails_loudly_so_the_job_retries(mailerlite_configured):+ client = MagicMock(+ get=AsyncMock(return_value=_response(200, {"data": {"id": "ml_1"}})),+ delete=AsyncMock(return_value=_response(500)),+ )+ with (+ patch.object(mailerlite, "_client", return_value=client),+ pytest.raises(mailerlite.MailerLiteError) as raised,+ ):+ await mailerlite.remove_from_trial(EMAIL)+ assert EMAIL not in str(raised.value)+++# ── subscriber fields ──────────────────────────────────────────────────────+++def _day(timestamp: int) -> str:+ return datetime.fromtimestamp(timestamp, UTC).strftime("%Y-%m-%d")+++ "kind, status",+ [+ ("started", SubscriptionStatus.IN_TRIAL),+ ("resumed", SubscriptionStatus.IN_TRIAL),+ ("canceled", SubscriptionStatus.TRIAL_CANCELED),+ # Neither ever became a paid subscription, so neither is churn.+ ("ended", SubscriptionStatus.TRIAL_CANCELED),+ ("payment_failed", SubscriptionStatus.TRIAL_CANCELED),+ ("converted", SubscriptionStatus.SUBSCRIBED),+ ],+)+async def test_each_trial_transition_sets_the_status_on_its_group_change(+ trial, kind, status+):+ trial, raw = _state(trial, kind)+ audience = AsyncMock(return_value=NotificationResult(success=True))+ await _notify(trial, raw, kind, audience=audience)+ event = audience.await_args_list[0].args[0]+ assert event.action in (AudienceAction.ADD_TRIAL, AudienceAction.REMOVE_TRIAL)+ assert event.fields[SubscriberField.STATUS] == status.value+++async def test_a_trial_start_carries_stripes_trial_start_date(trial):+ trial, raw = _state(trial, "started")+ raw["trial_start"] = 1788000000+ audience = AsyncMock(return_value=NotificationResult(success=True))+ await _notify(trial, raw, "started", audience=audience)+ fields = audience.await_args.args[0].fields+ assert fields[SubscriberField.TRIAL_STARTED] == _day(1788000000)+++async def test_a_conversion_starts_the_subscription_when_the_trial_ended(trial):+ trial, raw = _state(trial, "converted")+ audience = AsyncMock(return_value=NotificationResult(success=True))+ await _notify(trial, raw, "converted", audience=audience)+ fields = audience.await_args_list[0].args[0].fields+ assert fields[SubscriberField.SUBSCRIPTION_STARTED] == _day(raw["trial_end"])+ assert fields[SubscriberField.SUBSCRIPTION_ENDED] is None+++async def test_a_reminder_changes_no_fields(trial):+ trial, raw = _state(trial, "started")+ raw["trial_end"] = int((datetime.now(UTC) + timedelta(days=1)).timestamp())+ got, notice, _ = await _notify(trial, raw, "ending")+ assert got == []+ notice.assert_awaited_once()+++async def test_the_consumer_writes_fields_with_no_group_change(consumer_configured):+ event = AudienceEventModel(+ action=AudienceAction.UPDATE_FIELDS,+ email=EMAIL,+ user_id="user-1",+ fields={SubscriberField.STATUS: "signed"},+ )+ with patch.object(mailerlite, "update_fields", AsyncMock()) as called:+ assert await NotificationManager._process_audience_change(+ MagicMock(), event.model_dump_json()+ )+ called.assert_awaited_once_with(EMAIL, {SubscriberField.STATUS: "signed"})diff --git a/autogpt_platform/backend/backend/notifications/trial_test.py b/autogpt_platform/backend/backend/notifications/trial_test.pyindex 7fb754b74f..656f57b04b 100644--- a/autogpt_platform/backend/backend/notifications/trial_test.py+++ b/autogpt_platform/backend/backend/notifications/trial_test.py@@ -140,7 +140,9 @@ async def test_notice_uses_shared_notification_queue(trial, outcome):notices,"user_db",return_value=MagicMock(- get_user_by_id=AsyncMock(return_value=SimpleNamespace(name="Sam"))+ get_user_by_id=AsyncMock(+ return_value=SimpleNamespace(name="Sam", email="[email protected]")+ )),),patch.object(diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.pyindex c0f7e2a9ef..8608875baf 100644--- a/autogpt_platform/backend/backend/util/settings.py+++ b/autogpt_platform/backend/backend/util/settings.py@@ -599,6 +599,20 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):default="",description="MailerLite group that receives the monthly changelog campaign",)+ mailerlite_trial_group_id: str = Field(+ default="",+ description=(+ "MailerLite group holding customers in a card-required trial. "+ "Blank leaves trial customers out of MailerLite."+ ),+ )+ mailerlite_api_url: str = Field(+ default="https://connect.mailerlite.com/api",+ description=(+ "MailerLite API base URL. Only a test stack changes it, to point "+ "at a stub."+ ),+ )expert_avatar_model: str = Field(default="gpt-image-2-2026-04-21",