AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): only checkout openers go to MailerLite, segmented for GTM (#15141)
Nicholas Tindle957128fbc169
diff --git a/autogpt_platform/backend/.env.default b/autogpt_platform/backend/.env.defaultindex 811bc63153..e3186321f8 100644--- a/autogpt_platform/backend/.env.default+++ b/autogpt_platform/backend/.env.default@@ -261,6 +261,8 @@ MAILERLITE_ONBOARDING_GROUP_ID=MAILERLITE_CHANGELOG_GROUP_ID=# Customers in a card-required trial. Blank keeps trials out of MailerLite.MAILERLITE_TRIAL_GROUP_ID=+# Everyone who opened Stripe checkout. Blank keeps them out of MailerLite.+MAILERLITE_CHECKOUT_GROUP_ID=# Error TrackingSENTRY_DSN=diff --git a/autogpt_platform/backend/backend/api/features/billing/client_country.py b/autogpt_platform/backend/backend/api/features/billing/client_country.pynew file mode 100644index 0000000000..c5244a425c--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/billing/client_country.py@@ -0,0 +1,32 @@+from typing import Annotated++from autogpt_libs.auth.service import frontend_service_claims+from fastapi import Depends, Header++CLIENT_COUNTRY_SCOPE = "client-country"+++async def attested_country(+ token: Annotated[+ str | None, Header(alias="X-Client-Country-Token", include_in_schema=False)+ ] = None,+) -> str | None:+ """The visitor's country, as the frontend proxy vouches for it, or None.++ The backend is reachable directly -- the browser already calls it with+ its own bearer token -- so a plain country header would be whatever the+ caller typed. The proxy instead sends what Vercel's edge geolocated inside+ a short-lived frontend service token, signed with the JWKS key only the+ frontend holds. Anything else -- no token, a forged or expired one, a+ user token -- is no country at all, which the trial offer's country rule+ treats as unknown and withholds. Hidden from the schema: it is+ proxy-to-backend plumbing, not API surface.+ """+ if not token:+ return None+ claims = await frontend_service_claims(token, CLIENT_COUNTRY_SCOPE)+ country = claims.get("country") if claims else None+ return country if isinstance(country, str) else None+++ClientCountry = Annotated[str | None, Depends(attested_country)]diff --git a/autogpt_platform/backend/backend/api/features/billing/credits/routes.py b/autogpt_platform/backend/backend/api/features/billing/credits/routes.pyindex 92581b9e6a..2857a166f3 100644--- a/autogpt_platform/backend/backend/api/features/billing/credits/routes.py+++ b/autogpt_platform/backend/backend/api/features/billing/credits/routes.py@@ -11,7 +11,9 @@ from autogpt_libs.auth.models import RequestContextfrom autogpt_libs.auth.permissions import OrgActionfrom fastapi import APIRouter, Header, HTTPException, Query, Response, Security+from backend.api.features.billing.client_country import ClientCountryfrom backend.api.model import RequestTopUp+from backend.data.checkout_audience import schedule_checkout_openedfrom backend.data.credit import (AutoTopUpConfig,InvoiceListItem,@@ -67,6 +69,7 @@ async def request_top_up(x_datafast_session_id: Annotated[str | None, Header(include_in_schema=False)] = None,+ country: ClientCountry = None,):credit_model = await get_credit_model(user_id, ctx.org_id)checkout_url = await credit_model.top_up_intent(@@ -75,6 +78,8 @@ async def request_top_up(datafast_visitor_id=x_datafast_visitor_id,datafast_session_id=x_datafast_session_id,)+ if checkout_url:+ schedule_checkout_opened(user_id, ip_country=country)return {"checkout_url": checkout_url}diff --git a/autogpt_platform/backend/backend/api/features/billing/subscriptions/routes.py b/autogpt_platform/backend/backend/api/features/billing/subscriptions/routes.pyindex ee698d78bb..8eee5729f0 100644--- a/autogpt_platform/backend/backend/api/features/billing/subscriptions/routes.py+++ b/autogpt_platform/backend/backend/api/features/billing/subscriptions/routes.py@@ -20,10 +20,12 @@ from prisma.enums import SubscriptionTierfrom pydantic import BaseModel, Fieldfrom typing_extensions import Optional+from backend.api.features.billing.client_country import ClientCountryfrom backend.api.features.billing.credits_rate_limit import (enforce_subscription_status_rate_limit,)from backend.copilot.rate_limit import get_tier_multipliers+from backend.data import checkout_audiencefrom backend.data.credit import (PendingChangeUnknown,UserCredit,@@ -345,6 +347,7 @@ async def update_subscription_tier(x_datafast_session_id: Annotated[str | None, Header(include_in_schema=False)] = None,+ country: ClientCountry = None,) -> SubscriptionStatusResponse:# Pydantic validates tier is one of BASIC/PRO/MAX/BUSINESS via Literal type.tier = SubscriptionTier(request.tier)@@ -640,6 +643,8 @@ async def update_subscription_tier(),)+ if url:+ checkout_audience.schedule_checkout_opened(user_id, ip_country=country)status = await get_subscription_status(user_id)status.url = urlreturn status@@ -825,6 +830,9 @@ async def stripe_webhook(request: Request):# both would double-send.if event_type == "checkout.session.completed":await _notify_checkout_completed(data_object)+ # The billing address is the strongest country signal, and+ # it only exists once checkout completes. Never raises.+ await checkout_audience.record_checkout_completed(data_object)if event_type in ("customer.subscription.created",diff --git a/autogpt_platform/backend/backend/api/features/subscription_trial_routes.py b/autogpt_platform/backend/backend/api/features/subscription_trial_routes.pyindex d3993395d2..bcfd7e4b12 100644--- a/autogpt_platform/backend/backend/api/features/subscription_trial_routes.py+++ b/autogpt_platform/backend/backend/api/features/subscription_trial_routes.py@@ -3,13 +3,18 @@ from typing import Annotated, Literalimport stripefrom autogpt_libs.auth import get_user_id-from autogpt_libs.auth.service import frontend_service_claimsfrom fastapi import APIRouter, Depends, Header, HTTPException, Securityfrom pydantic import BaseModel, Field+from backend.api.features.billing.client_country import ( # noqa: F401 -- re-exported+ CLIENT_COUNTRY_SCOPE,+ ClientCountry,+ attested_country,+)from backend.api.features.billing.credits_rate_limit import (enforce_subscription_status_rate_limit,)+from backend.data.checkout_audience import schedule_checkout_openedfrom backend.data.credit import _datafast_metadata, sync_subscription_from_stripefrom backend.data.stripe_client import stripe_callfrom backend.data.subscription_trial import (@@ -73,35 +78,6 @@ class TrialCheckoutResponse(BaseModel):url: str-CLIENT_COUNTRY_SCOPE = "client-country"---async def attested_country(- token: Annotated[- str | None, Header(alias="X-Client-Country-Token", include_in_schema=False)- ] = None,-) -> str | None:- """The visitor's country, as the frontend proxy vouches for it, or None.-- The backend is reachable directly -- the browser already calls it with- its own bearer token -- so a plain country header would be whatever the- caller typed. The proxy instead sends what Vercel's edge geolocated inside- a short-lived frontend service token, signed with the JWKS key only the- frontend holds. Anything else -- no token, a forged or expired one, a- user token -- is no country at all, which the offer's country rule treats- as unknown and withholds. Hidden from the schema: it is proxy-to-backend- plumbing, not API surface.- """- if not token:- return None- claims = await frontend_service_claims(token, CLIENT_COUNTRY_SCOPE)- country = claims.get("country") if claims else None- return country if isinstance(country, str) else None---ClientCountry = Annotated[str | None, Depends(attested_country)]--@router.get("")async def get_trial_status(user_id: CurrentUser, country: ClientCountry = None@@ -198,6 +174,7 @@ async def start_trial_checkout(raise HTTPException(409, str(exc)) from excexcept stripe.StripeError as exc:raise HTTPException(502, "Unable to start checkout. Please try again.") from exc+ schedule_checkout_opened(user_id, ip_country=country)return TrialCheckoutResponse(url=url)diff --git a/autogpt_platform/backend/backend/cli/mailerlite_backfill.py b/autogpt_platform/backend/backend/cli/mailerlite_backfill.pyindex 0044957a81..14cc7054a1 100644--- a/autogpt_platform/backend/backend/cli/mailerlite_backfill.py+++ b/autogpt_platform/backend/backend/cli/mailerlite_backfill.py@@ -5,6 +5,7 @@ from typing import TYPE_CHECKINGimport clickif TYPE_CHECKING:+ from backend.notifications.checkout_backfill import OpenerPlanfrom backend.notifications.mailerlite_backfill import (Customer,PlannedChange,@@ -23,25 +24,60 @@ _ACCOUNT_PAGE = 5000is_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-only",+ is_flag=True,+ help="Only group membership; leave the status and date fields alone.",+)+def mailerlite_backfill_command(+ apply: bool, yes: bool, fields_only: bool, groups_only: bool+):+ """Put existing customers where the live MailerLite code would have.Groups: paying customers join the changelog unless they are in theonboarding tour; churned customers leave it. WhenMAILERLITE_TRIAL_GROUP_ID is set, the trial group ends up holding exactlythe 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.+ Fields: every account with a Stripe customer that MailerLite already holds+ gets its subscription_status and dates. Nobody is created here: a Stripe+ customer alone does not mean they opened checkout (the billing portal+ makes one too), so new people come only from mailerlite-checkout-backfill.+ Accounts without a Stripe customer are never read.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."""+ if fields_only and groups_only:+ raise click.UsageError("--fields-only and --groups-only exclude each other")# Keep Prisma and client chatter out of the report.logging.disable(logging.INFO)- asyncio.run(_run(apply=apply, yes=yes, fields_only=fields_only))+ asyncio.run(+ _run(apply=apply, yes=yes, fields_only=fields_only, groups_only=groups_only)+ )++[email protected](name="mailerlite-checkout-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.")+def mailerlite_checkout_backfill_command(apply: bool, yes: bool):+ """Put everyone who opened Stripe checkout into the checkout openers group.++ An opener is an account with at least one Stripe Checkout Session; no+ other account is read or written. Each gets the fields GTM segments on:+ checkout_opened_date (their first session), email_type, signin_method,+ country and country_code (the Stripe billing address, else the browser's+ timezone), country_source and exclude_de_at, plus their status and dates.++ Visits one person every two seconds, re-reading each just before the+ write so nothing the live checkout event wrote meanwhile is overwritten.+ Dry run by default,+ with counts only. Idempotent, so a repeated --apply resumes an interrupted+ one; run the dry run again afterwards to confirm nothing is left.+ """+ logging.disable(logging.INFO)+ asyncio.run(_run_checkout(apply=apply, yes=yes))@click.command(name="mailerlite-fields")@@ -81,7 +117,9 @@ async def _fields(*, apply: bool) -> None:click.echo(f"\nCreated {len(created)} fields.")-async def _run(*, apply: bool, yes: bool, fields_only: bool) -> None:+async def _run(+ *, apply: bool, yes: bool, fields_only: bool, groups_only: bool = False+) -> None:from backend.data.db import connect, disconnectfrom backend.notifications import mailerlite, mailerlite_backfillfrom backend.notifications import mailerlite_field_backfill as field_backfill@@ -108,8 +146,9 @@ async def _run(*, apply: bool, yes: bool, fields_only: bool) -> 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))+ fields = field_backfill.plan([] if groups_only else people, current, create=False)+ if not groups_only:+ _report_fields(fields, len(people))if not apply:click.echo("\nDry run: nothing was written. Re-run with --apply to write.")@@ -149,25 +188,58 @@ async def _stripe_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"),- )- )+ subscriptions.setdefault(str(sub.customer), []).append(_subscription(sub))return subscriptions+def _subscription(sub) -> "Subscription":+ from backend.notifications.mailerlite_backfill import Subscription++ return 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"),+ )+++def _refresher(converted: set[str]):+ """One customer's subscriptions as Stripe has them now, so the checkout+ backfill writes the current status rather than its snapshot's: a trial+ that converts during the run is not written back as a trial."""++ async def refresh(customer_id: str) -> "list[Subscription]":+ import stripe++ from backend.data.stripe_client import stripe_call, stripe_list_items++ page = await stripe_call(+ stripe.Subscription.list_async,+ customer=customer_id,+ status="all",+ limit=100,+ )+ return [+ _subscription(sub).model_copy(+ update={"converted": str(sub.id) in converted}+ )+ async for sub in stripe_list_items(page)+ ]++ return refresh++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."""+ """Every account with a Stripe customer, paged, with its Stripe+ subscriptions. Accounts without one never reached checkout, so they are+ not read at all. Having one is not proof of a checkout either: callers+ that create MailerLite subscribers must also check for a Checkout Session.+ The account's email is used, as the live handlers do, never Stripe's."""import prisma.modelsfrom backend.notifications.mailerlite_field_backfill import Person@@ -183,6 +255,7 @@ async def _people(subscriptions: "dict[str, list[Subscription]]") -> "list[Persocursor: str | None = Nonewhile True:page = await prisma.models.User.prisma().find_many(+ where={"stripeCustomerId": {"not": None}},take=_ACCOUNT_PAGE,order={"id": "asc"},**({"cursor": {"id": cursor}, "skip": 1} if cursor else {}),@@ -198,6 +271,8 @@ async def _people(subscriptions: "dict[str, list[Subscription]]") -> "list[Persos.model_copy(update={"converted": s.id in converted})for s in subs],+ stripe_customer_id=user.stripeCustomerId,+ timezone=user.timezone,))if len(page) < _ACCOUNT_PAGE:@@ -242,10 +317,9 @@ def _report_fields(plan: "FieldPlan", accounts: int) -> None: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"+ f"{len(plan.changes)} existing MailerLite subscribers to update "+ "(nobody is created here; see mailerlite-checkout-backfill)")batches = -(-len(plan.changes) // BATCH_SIZE)minutes = batches * UPSERT_BATCH_INTERVAL_SECONDS / 60@@ -254,3 +328,155 @@ def _report_fields(plan: "FieldPlan", accounts: int) -> None:def _progress(done: int, total: int) -> None:click.echo(f"fields: {done}/{total} written")+++async def _run_checkout(*, apply: bool, yes: bool) -> None:+ import stripe++ from backend.data.db import connect, disconnect+ from backend.data.stripe_client import stripe_call, stripe_list_items+ from backend.notifications import checkout_backfill, mailerlite+ from backend.notifications import mailerlite_field_backfill as field_backfill+ from backend.notifications.mailerlite import MailerLiteNotConfigured+ from backend.notifications.mailerlite_backfill import _read_group+ from backend.util.settings import Settings++ settings = Settings()+ if not settings.secrets.stripe_api_key:+ raise click.ClickException("STRIPE_API_KEY is not set.")+ group_id = settings.config.mailerlite_checkout_group_id+ if not group_id:+ raise click.ClickException("MAILERLITE_CHECKOUT_GROUP_ID is not set.")+ try:+ mailerlite._require_token()+ current = await field_backfill.read_current()+ members = await _read_group(group_id)+ except MailerLiteNotConfigured as e:+ raise click.ClickException(str(e))++ stripe.api_key = settings.secrets.stripe_api_key+ first_open: dict[str, int] = {}+ page = await stripe_call(stripe.checkout.Session.list_async, limit=100)+ async for session in stripe_list_items(page):+ customer = session.get("customer")+ if isinstance(customer, str):+ first_open[customer] = min(+ first_open.get(customer, session.created), session.created+ )+ billing_countries: dict[str, str] = {}+ page = await stripe_call(stripe.Customer.list_async, limit=100)+ async for customer in stripe_list_items(page):+ country = (customer.get("address") or {}).get("country")+ if country:+ billing_countries[customer.id] = str(country)+ subscriptions = await _stripe_subscriptions()++ await connect()+ try:+ people = await _people(subscriptions)+ providers = await _signin_providers()+ finally:+ await disconnect()++ openers = [+ checkout_backfill.Opener(+ person=person,+ opened_at=first_open[person.stripe_customer_id],+ signin_providers=providers.get(person.user_id, []),+ stripe_country=billing_countries.get(person.stripe_customer_id),+ )+ for person in people+ if person.stripe_customer_id in first_open+ ]+ linked = {p.stripe_customer_id for p in people}+ plan = checkout_backfill.plan(openers, current, members)+ _report_checkout(+ plan,+ sessions_unlinked=len(set(first_open) - linked),+ customers_without_session=len(people) - len(openers),+ )++ if not apply:+ click.echo("\nDry run: nothing was written. Re-run with --apply to write.")+ return+ if not plan.changes:+ click.echo("\nNothing to write.")+ return+ if not yes:+ click.confirm(+ f"\nWrite {len(plan.changes)} checkout openers to MailerLite?", abort=True+ )+ await mailerlite.ensure_fields()+ converted = {s.id for p in people for s in p.subscriptions if s.converted}+ ok, failed, skipped = await checkout_backfill.apply(+ plan.changes, group_id, _checkout_progress, refresh=_refresher(converted)+ )+ _finish_checkout(ok, failed, skipped)+++def _finish_checkout(ok: int, failed: int, skipped: int) -> None:+ """Report the run, and fail it when anyone was not written: the run goes+ on past each failure, but a Job or script must not read a partial run as+ done. A rerun retries only what is left."""+ click.echo(+ f"checkout openers: {ok} ok, {failed} failed, {skipped} already up to date"+ )+ if failed:+ raise click.ClickException(+ f"{failed} checkout openers were not written (the log says why); "+ "run again to retry them"+ )+ click.echo("Run the dry run again to confirm nothing is left.")+++async def _signin_providers() -> dict[str, list[str]]:+ """Every account's Better Auth providers, by account id."""+ from backend.data.db import query_raw_with_schema++ rows = await query_raw_with_schema(+ 'SELECT "userId" AS user_id, "providerId" AS provider '+ 'FROM {schema_prefix}"UserAuthAccount"'+ )+ providers: dict[str, list[str]] = {}+ for row in rows:+ providers.setdefault(str(row["user_id"]), []).append(str(row["provider"]))+ return providers+++def _report_checkout(+ plan: "OpenerPlan", *, sessions_unlinked: int, customers_without_session: int+) -> None:+ from backend.notifications.checkout_backfill import WRITE_INTERVAL_SECONDS++ def counts(values: dict[str, int], limit: int = 15) -> str:+ top = sorted(values.items(), key=lambda kv: -kv[1])[:limit]+ return ", ".join(f"{k}={v}" for k, v in top)++ click.echo(+ f"Checkout openers (accounts with a Stripe Checkout Session): {plan.openers}"+ )+ click.echo(+ f" Stripe customers with a session but no account (skipped): {sessions_unlinked}"+ )+ click.echo(+ f" Accounts with a Stripe customer but no session (skipped): {customers_without_session}"+ )+ click.echo(" Accounts without a Stripe customer: never read or written")+ click.echo(f" invalid email (skipped): {plan.invalid}")+ click.echo(f"\nCountry source: {counts(plan.country_sources)}")+ click.echo(f"Countries: {counts(plan.countries)}")+ click.echo(f"exclude_de_at=yes: {plan.exclude_de_at}")+ click.echo(f"Email types: {counts(plan.email_types)}")+ click.echo(f"Sign-in methods: {counts(plan.signin_methods)}")+ new = sum(c.new for c in plan.changes)+ joins = sum(c.joins for c in plan.changes)+ click.echo(+ f"\n{len(plan.changes)} to write: {new} new MailerLite subscribers, "+ f"{len(plan.changes) - new} existing ones updated, {joins} joining the group"+ )+ minutes = len(plan.changes) * WRITE_INTERVAL_SECONDS / 60+ click.echo(f"About {minutes:.0f} minutes at one person every two seconds")+++def _checkout_progress(done: int, total: int) -> None:+ click.echo(f"checkout openers: {done}/{total} written")diff --git a/autogpt_platform/backend/backend/cli/mailerlite_backfill_test.py b/autogpt_platform/backend/backend/cli/mailerlite_backfill_test.pynew file mode 100644index 0000000000..a7d5647613--- /dev/null+++ b/autogpt_platform/backend/backend/cli/mailerlite_backfill_test.py@@ -0,0 +1,47 @@+"""A checkout backfill that could not write everyone fails as a whole, after+reporting, so a Job or script never reads a partial run as done."""++import click+import pytest+from click.testing import CliRunner++from backend.cli import mailerlite_backfill as cli+++# The root conftest spins a full test server for every test via an autouse+# session fixture; these never touch it.[email protected](scope="session")+def server():+ yield None++[email protected](scope="session", autouse=True)+def graph_cleanup():+ yield+++def test_a_complete_run_reports_and_succeeds(capsys):+ cli._finish_checkout(5, 0, 2)+ out = capsys.readouterr().out+ assert "5 ok, 0 failed, 2 already up to date" in out+ assert "Run the dry run again" in out+++def test_a_run_with_failures_reports_then_fails():+ with pytest.raises(+ click.ClickException, match="3 checkout openers were not written"+ ):+ cli._finish_checkout(5, 3, 0)+++def test_the_command_exits_non_zero_when_anyone_failed(monkeypatch):+ async def run(*, apply: bool, yes: bool) -> None:+ cli._finish_checkout(4, 1, 0)++ monkeypatch.setattr(cli, "_run_checkout", run)+ result = CliRunner().invoke(+ cli.mailerlite_checkout_backfill_command, ["--apply", "--yes"]+ )+ assert result.exit_code == 1+ assert "4 ok, 1 failed" in result.output+ assert "1 checkout openers were not written" in result.outputdiff --git a/autogpt_platform/backend/backend/cli/main.py b/autogpt_platform/backend/backend/cli/main.pyindex d7c8a753dd..f902d6676d 100644--- a/autogpt_platform/backend/backend/cli/main.py+++ b/autogpt_platform/backend/backend/cli/main.py@@ -9,7 +9,11 @@ import psutilfrom backend.util.process import AppProcessfrom .chat import chat-from .mailerlite_backfill import mailerlite_backfill_command, mailerlite_fields_command+from .mailerlite_backfill import (+ mailerlite_backfill_command,+ mailerlite_checkout_backfill_command,+ mailerlite_fields_command,+)from .rotate_key import rotate_encryption_keyfrom .store import storefrom .test import test@@ -23,6 +27,7 @@ def main():main.add_command(chat)main.add_command(mailerlite_backfill_command)+main.add_command(mailerlite_checkout_backfill_command)main.add_command(mailerlite_fields_command)main.add_command(rotate_encryption_key)main.add_command(store)diff --git a/autogpt_platform/backend/backend/data/checkout_audience.py b/autogpt_platform/backend/backend/data/checkout_audience.pynew file mode 100644index 0000000000..36f99e7335--- /dev/null+++ b/autogpt_platform/backend/backend/data/checkout_audience.py@@ -0,0 +1,111 @@+"""Queue the checkout-opened audience change for an account.++Called by the checkout routes once a Stripe Checkout Session exists, with the+visitor's IP country, and by the checkout.session.completed webhook with the+Stripe billing country. Firing on every checkout, not once, is safe: the+notification service keeps the first open date, an existing status, and the+strongest country it has seen (`audience_enrichment.merge_with_held`).++It runs in the background and never raises. A checkout must not wait on, or+fail because of, MailerLite bookkeeping; the backfill catches anyone missed.+"""++import asyncio+import logging+from datetime import datetime++import prisma.models++from backend.data.db import query_raw_with_schema+from backend.data.notifications import AudienceAction+from backend.data.user import get_user_by_id+from backend.notifications.audience_enrichment import checkout_fields+from backend.notifications.subscriber_fields import queue_fields++logger = logging.getLogger(__name__)++# Strong references, so a scheduled task is not garbage-collected mid-flight.+_tasks: set[asyncio.Task] = set()+++def schedule_checkout_opened(+ user_id: str,+ *,+ ip_country: str | None = None,+ stripe_country: str | None = None,+ opened_at: datetime | int | None = None,+) -> None:+ """Queue the change in the background, so the checkout response never+ waits on the database or the broker."""+ try:+ task = asyncio.create_task(+ queue_checkout_opened(+ user_id,+ ip_country=ip_country,+ stripe_country=stripe_country,+ opened_at=opened_at,+ )+ )+ _tasks.add(task)+ task.add_done_callback(_tasks.discard)+ except Exception:+ logger.warning(f"Could not schedule the MailerLite checkout for {user_id}")+++async def queue_checkout_opened(+ user_id: str,+ *,+ ip_country: str | None = None,+ stripe_country: str | None = None,+ opened_at: datetime | int | None = None,+) -> None:+ try:+ user = await get_user_by_id(user_id)+ fields = checkout_fields(+ email=user.email,+ created_at=user.created_at,+ opened_at=opened_at,+ signin_providers=await signin_providers(user_id),+ timezone=user.timezone,+ stripe_country=stripe_country,+ ip_country=ip_country,+ )+ await queue_fields(user_id, user.email, fields, AudienceAction.CHECKOUT_OPENED)+ except Exception:+ logger.exception(f"Could not queue the MailerLite checkout for {user_id}")+++async def record_checkout_completed(session: dict) -> None:+ """A finished checkout carries the billing address, the strongest country+ signal, which also catches a German or Austrian buyer whose IP and+ timezone did not say so. Never raises: the webhook must not fail on it."""+ try:+ customer = session.get("customer")+ customer_id = customer if isinstance(customer, str) else None+ if not customer_id:+ return+ user = await prisma.models.User.prisma().find_first(+ where={"stripeCustomerId": customer_id}+ )+ if user is None:+ # An organization's customer, or one whose account is gone.+ return+ address = (session.get("customer_details") or {}).get("address") or {}+ schedule_checkout_opened(+ user.id,+ stripe_country=address.get("country"),+ opened_at=session.get("created"),+ )+ except Exception:+ logger.exception("Could not record the MailerLite checkout completion")+++async def signin_providers(user_id: str) -> list[str]:+ """The account's Better Auth providers: 'google', 'credential' and so on.+ The account id is the auth user id."""+ rows = await query_raw_with_schema(+ 'SELECT "providerId" AS provider FROM {schema_prefix}"UserAuthAccount" '+ 'WHERE "userId" = $1',+ user_id,+ )+ return [str(row["provider"]) for row in rows if row.get("provider")]diff --git a/autogpt_platform/backend/backend/data/checkout_audience_test.py b/autogpt_platform/backend/backend/data/checkout_audience_test.pynew file mode 100644index 0000000000..999abab51d--- /dev/null+++ b/autogpt_platform/backend/backend/data/checkout_audience_test.py@@ -0,0 +1,226 @@+"""Opening Stripe checkout queues the checkout opener for MailerLite, from+each of the three checkout routes and the completed-checkout webhook, and none+of it can cost the checkout or fail the webhook."""++import inspect+import re+from datetime import UTC, datetime+from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from fastapi import HTTPException++from backend.api.features import subscription_trial_routes as trial_routes+from backend.api.features.billing.credits import routes as credit_routes+from backend.api.features.billing.subscriptions import routes as subscription_routes+from backend.api.model import RequestTopUp+from backend.data import checkout_audience+from backend.data.notifications import (+ AudienceAction,+ NotificationResult,+ SubscriberField,+)+from backend.data.subscription_trial_checkout import TrialUnavailable+from backend.notifications import subscriber_fields++EMAIL = "[email protected]"+CREATED = datetime(2026, 7, 14, 9, 0, tzinfo=UTC)+++# The root conftest spins a full test server for every test via an autouse+# session fixture. These are unit tests over mocks, so shadow it for this+# module, as backend/data/db_test.py does.[email protected](scope="session")+def server():+ yield None++[email protected](scope="session", autouse=True)+def graph_cleanup():+ yield+++# ── queueing it ──────────────────────────────────────────────────────────────+++def queued(monkeypatch):+ queue = AsyncMock(return_value=NotificationResult(success=True))+ monkeypatch.setattr(subscriber_fields, "queue_audience_change", queue)+ monkeypatch.setattr(+ checkout_audience,+ "get_user_by_id",+ AsyncMock(+ return_value=SimpleNamespace(+ email=EMAIL, created_at=CREATED, timezone="Europe/Vienna"+ )+ ),+ )+ monkeypatch.setattr(+ checkout_audience, "signin_providers", AsyncMock(return_value=["credential"])+ )+ return queue+++async def test_opening_checkout_queues_the_enriched_change(queued):+ checkout_audience.schedule_checkout_opened("user-1", ip_country="US")+ for task in list(checkout_audience._tasks):+ await task+ event = queued.await_args.args[0]+ assert event.action is AudienceAction.CHECKOUT_OPENED+ assert event.fields[SubscriberField.COUNTRY_CODE] == "US"+ assert event.fields[SubscriberField.COUNTRY_SOURCE] == "ip"+ assert event.fields[SubscriberField.SIGNIN_METHOD] == "email"+ # The IP says US, but the browser sits in Vienna.+ assert event.fields[SubscriberField.EXCLUDE_DE_AT] == "yes"+++async def test_a_failure_never_reaches_the_checkout(queued, monkeypatch):+ monkeypatch.setattr(+ checkout_audience, "get_user_by_id", AsyncMock(side_effect=RuntimeError("db"))+ )+ await checkout_audience.queue_checkout_opened("user-1", ip_country="US")+ queued.assert_not_awaited()+++def _user_prisma(user):+ return MagicMock(find_first=AsyncMock(return_value=user))+++async def test_a_completed_checkout_sends_its_billing_country(monkeypatch):+ schedule = MagicMock()+ monkeypatch.setattr(checkout_audience, "schedule_checkout_opened", schedule)+ with patch(+ "prisma.models.User.prisma",+ return_value=_user_prisma(SimpleNamespace(id="user-1")),+ ):+ await checkout_audience.record_checkout_completed(+ {+ "customer": "cus_1",+ "created": 1788305400,+ "customer_details": {"address": {"country": "DE"}},+ }+ )+ schedule.assert_called_once_with(+ "user-1", stripe_country="DE", opened_at=1788305400+ )+++ "session, user",+ [+ ({"customer": "cus_org"}, None),+ ({"customer": None}, SimpleNamespace(id="user-1")),+ ],+ ids=["no-account", "no-customer"],+)+async def test_a_completed_checkout_without_an_account_is_skipped(+ monkeypatch, session, user+):+ schedule = MagicMock()+ monkeypatch.setattr(checkout_audience, "schedule_checkout_opened", schedule)+ with patch("prisma.models.User.prisma", return_value=_user_prisma(user)):+ await checkout_audience.record_checkout_completed(session)+ schedule.assert_not_called()+++async def test_a_completed_checkout_never_fails_the_webhook(monkeypatch):+ with patch("prisma.models.User.prisma", side_effect=RuntimeError("db down")):+ await checkout_audience.record_checkout_completed({"customer": "cus_1"})+++# ── the checkout routes and the webhook ──────────────────────────────────────+++def schedule():+ return MagicMock()+++ "url, queued", [("https://checkout.stripe.com/x", True), ("", False)]+)+async def test_a_top_up_checkout_queues_its_opener(monkeypatch, schedule, url, queued):+ model = SimpleNamespace(top_up_intent=AsyncMock(return_value=url))+ monkeypatch.setattr(+ credit_routes, "get_credit_model", AsyncMock(return_value=model)+ )+ monkeypatch.setattr(credit_routes, "schedule_checkout_opened", schedule)+ await credit_routes.request_top_up(+ request=RequestTopUp(credit_amount=500),+ user_id="user-1",+ ctx=SimpleNamespace(org_id=None),+ country="US",+ )+ if queued:+ schedule.assert_called_once_with("user-1", ip_country="US")+ else:+ schedule.assert_not_called()+++def _trial_settings():+ return SimpleNamespace(+ config=SimpleNamespace(+ frontend_base_url="https://platform.example.com", platform_base_url=""+ )+ )+++async def test_a_trial_checkout_queues_its_opener(monkeypatch, schedule):+ monkeypatch.setattr(trial_routes, "Settings", _trial_settings)+ monkeypatch.setattr(+ trial_routes, "create_trial_checkout", AsyncMock(return_value="https://x")+ )+ monkeypatch.setattr(trial_routes, "schedule_checkout_opened", schedule)+ await trial_routes.start_trial_checkout(+ body=trial_routes.TrialCheckoutRequest(offer_token="a" * 64),+ user_id="user-1",+ country="DE",+ )+ schedule.assert_called_once_with("user-1", ip_country="DE")+++async def test_a_trial_checkout_that_never_opened_queues_nothing(monkeypatch, schedule):+ monkeypatch.setattr(trial_routes, "Settings", _trial_settings)+ monkeypatch.setattr(+ trial_routes,+ "create_trial_checkout",+ AsyncMock(side_effect=TrialUnavailable("taken")),+ )+ monkeypatch.setattr(trial_routes, "schedule_checkout_opened", schedule)+ with pytest.raises(HTTPException):+ await trial_routes.start_trial_checkout(+ body=trial_routes.TrialCheckoutRequest(offer_token="a" * 64),+ user_id="user-1",+ country="DE",+ )+ schedule.assert_not_called()+++def _source(function) -> str:+ return re.sub(r"\s+", "", inspect.getsource(function))+++def test_the_subscription_checkout_and_the_webhook_are_wired():+ """Both need the full stack to drive end to end, so pin the calls: the+ subscription route queues an opener once its session exists, and the+ completed-checkout webhook sends the billing country."""+ route = _source(subscription_routes.update_subscription_tier)+ assert (+ "ifurl:checkout_audience.schedule_checkout_opened(user_id,ip_country=country)"+ in route+ )+ assert route.index("create_subscription_checkout(") < route.index(+ "schedule_checkout_opened"+ )+ webhook = _source(subscription_routes.stripe_webhook)+ assert "awaitcheckout_audience.record_checkout_completed(data_object)" in webhookdiff --git a/autogpt_platform/backend/backend/data/notifications.py b/autogpt_platform/backend/backend/data/notifications.pyindex 19430e6ff5..b9c0faa1c5 100644--- a/autogpt_platform/backend/backend/data/notifications.py+++ b/autogpt_platform/backend/backend/data/notifications.py@@ -421,14 +421,20 @@ class AudienceAction(Enum):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`).+ # Nothing queues this: a signup stays out of MailerLite until it opens+ # checkout. It exists so an older queued or dead-lettered message still+ # parses, and only updates someone MailerLite already has (see+ # `mailerlite.record_signup`).SIGNUP = "signup"+ # Someone opened Stripe checkout: into the checkout openers group, with+ # the fields GTM segments them on (see `mailerlite.record_checkout_opened`).+ CHECKOUT_OPENED = "checkout_opened"class SubscriberField(str, Enum):- """MailerLite custom field keys the backend writes. GTM segments on these,- so a key is renamed only together with MailerLite."""+ """MailerLite field keys the backend writes: our custom fields, and+ MailerLite's built-in `country`. GTM segments on these, so a key is renamed+ only together with MailerLite."""STATUS = "subscription_status"SIGNUP = "signup_date"@@ -436,6 +442,15 @@ class SubscriberField(str, Enum):SUBSCRIPTION_STARTED = "subscription_started_date"SUBSCRIPTION_CANCELED = "subscription_canceled_date"SUBSCRIPTION_ENDED = "subscription_ended_date"+ # The checkout opener's segmentation (see `audience_enrichment`).+ CHECKOUT_OPENED = "checkout_opened_date"+ EMAIL_TYPE = "email_type"+ SIGNIN_METHOD = "signin_method"+ # Built into MailerLite, so never created: the full English name.+ COUNTRY = "country"+ COUNTRY_CODE = "country_code"+ COUNTRY_SOURCE = "country_source"+ EXCLUDE_DE_AT = "exclude_de_at"class SubscriptionStatus(str, Enum):@@ -458,7 +473,7 @@ class AudienceEventModel(BaseModel):user_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.+ # untouched. CHECKOUT_OPENED's are merged with what MailerLite holds first.fields: dict[SubscriberField, str | None] = Field(default_factory=dict)diff --git a/autogpt_platform/backend/backend/data/user.py b/autogpt_platform/backend/backend/data/user.pyindex ce5f4e9521..76c37de307 100644--- a/autogpt_platform/backend/backend/data/user.py+++ b/autogpt_platform/backend/backend/data/user.py@@ -36,7 +36,6 @@ 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@@ -89,7 +88,6 @@ async def _get_or_create_user(user_data: dict) -> UserCreationResult:))was_created = True- _sync_signup(user)else:was_created = False@@ -127,21 +125,6 @@ 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/audience_enrichment.py b/autogpt_platform/backend/backend/notifications/audience_enrichment.pynew file mode 100644index 0000000000..a6cce00556--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/audience_enrichment.py@@ -0,0 +1,295 @@+"""What GTM segments checkout openers on, worked out from what we hold.++Pure functions, shared by the live checkout event and the backfill so both+write the same values:++- `checkout_opened_date`: the first time they opened Stripe checkout.+- `email_type`: business, personal (a webmail provider) or education.+- `signin_method`: google or email.+- `country` (MailerLite's built-in field, the full English name) and+ `country_code` (ISO 3166-1 alpha-2), from the strongest source we have: the+ Stripe billing address, then the visitor's IP country as the edge saw it,+ then the browser's timezone. `country_source` says which.+- `exclude_de_at`: "yes" when any signal points at Germany or Austria, whose+ stricter marketing rules GTM skips. Once yes it is never set back to no.++The timezone and country tables come from tzdata, which zoneinfo already+depends on. A zone is looked up by the name the browser reported before any+alias is followed: tzdata merges zones with identical history across borders+(Europe/Stockholm is a link to Europe/Berlin), and following that link first+would tag a Swede as German.+"""++import functools+import importlib.resources+from collections.abc import Iterable, Mapping+from datetime import datetime++from backend.data.notifications import SubscriberField, SubscriptionStatus+from backend.notifications.subscriber_fields import Fields, mailerlite_date++YES = "yes"+NO = "no"++DE_AT = frozenset({"DE", "AT"})++# Strongest first. A held country from a stronger source is never replaced by+# a weaker one; a hand-typed one with no source ranks lowest.+COUNTRY_SOURCES = ("stripe", "ip", "timezone")+_SOURCE_RANK = {+ source: len(COUNTRY_SOURCES) - i for i, source in enumerate(COUNTRY_SOURCES)+}++# Zones that name no country.+_COUNTRYLESS = frozenset(+ {+ "", "not-set", "UTC", "UCT", "GMT", "GMT0", "GMT+0", "GMT-0", "Greenwich",+ "Universal", "Zulu", "Factory", "CET", "MET", "EET", "WET", "EST", "MST",+ "HST", "EST5EDT", "CST6CDT", "MST7MDT", "PST8PDT",+ }+) # fmt: skip++# Old names whose tzdata link points across a border.+_CROSS_BORDER_ALIASES = {+ "Africa/Asmera": "ER",+ "Africa/Timbuktu": "ML",+ "America/Coral_Harbour": "CA",+ "America/Virgin": "VI",+ "Antarctica/South_Pole": "AQ",+ "Atlantic/Jan_Mayen": "SJ",+ "Iceland": "IS",+ "Pacific/Johnston": "UM",+ "Pacific/Ponape": "FM",+ "Pacific/Truk": "FM",+ "Pacific/Yap": "FM",+}++# tzdata's names, where they read oddly in a segment filter.+_NAME_OVERRIDES = {+ "GB": "United Kingdom",+ "KR": "South Korea",+ "KP": "North Korea",+ "MM": "Myanmar",+ "SZ": "Eswatini",+}++# Webmail providers: an address on one is a person's, not a company's.+_PERSONAL_DOMAINS = frozenset(+ {+ "gmail.com", "googlemail.com", "outlook.com", "hotmail.com", "live.com",+ "msn.com", "yahoo.com", "ymail.com", "rocketmail.com", "icloud.com",+ "me.com", "mac.com", "aol.com", "proton.me", "protonmail.com", "pm.me",+ "gmx.com", "gmx.net", "gmx.de", "web.de", "t-online.de", "freenet.de",+ "yandex.com", "yandex.ru", "ya.ru", "mail.ru", "inbox.ru", "list.ru",+ "bk.ru", "rambler.ru", "qq.com", "foxmail.com", "163.com", "126.com",+ "yeah.net", "sina.com", "sohu.com", "aliyun.com", "naver.com",+ "daum.net", "hanmail.net", "kakao.com", "rediffmail.com", "zoho.com",+ "zohomail.com", "tutanota.com", "tuta.io", "fastmail.com", "hey.com",+ "mail.com", "email.com", "seznam.cz", "wp.pl", "o2.pl", "interia.pl",+ "libero.it", "virgilio.it", "orange.fr", "free.fr", "laposte.net",+ "sfr.fr", "btinternet.com", "comcast.net", "verizon.net", "att.net",+ "sbcglobal.net", "bellsouth.net", "cox.net", "shaw.ca", "rogers.com",+ "bigpond.com", "optusnet.com.au", "uol.com.br", "bol.com.br",+ "terra.com.br",+ }+) # fmt: skip++# Providers that run the same webmail under many country domains.+_PERSONAL_PREFIXES = ("yahoo.", "hotmail.", "outlook.", "live.", "gmx.", "yandex.")+++def _tz_file(name: str) -> str:+ return (+ importlib.resources.files("tzdata")+ .joinpath("zoneinfo")+ .joinpath(name)+ .read_text(encoding="utf-8")+ )+++def _zone_countries() -> dict[str, str]:+ """Each zone in zone.tab, by the name a browser reports, to its country.+ zone.tab keeps one row per named zone even where tzdata has since merged+ zones, which is why it is used instead of zone1970.tab."""+ zones: dict[str, str] = {}+ for line in _tz_file("zone.tab").splitlines():+ if line and not line.startswith("#"):+ code, _coordinates, zone, *_ = line.split("\t")+ zones[zone] = code+ return zones+++def _zone_links() -> dict[str, str]:+ """Old and alternative zone names to the zone they now point at."""+ links: dict[str, str] = {}+ for line in _tz_file("tzdata.zi").splitlines():+ if line.startswith("L "):+ _, target, alias = line.split()+ links[alias] = target+ return links+++def _country_names() -> dict[str, str]:+ names: dict[str, str] = {}+ for line in _tz_file("iso3166.tab").splitlines():+ if line and not line.startswith("#"):+ code, name = line.split("\t")[:2]+ names[code] = name.strip()+ names.update(_NAME_OVERRIDES)+ return names+++def timezone_country(timezone: str | None) -> str | None:+ """The country an IANA timezone name, as the browser reported it, belongs+ to, or None for a zone that names no country."""+ name = (timezone or "").strip()+ if name in _COUNTRYLESS or name.startswith("Etc/"):+ return None+ if name in _CROSS_BORDER_ALIASES:+ return _CROSS_BORDER_ALIASES[name]+ zones = _zone_countries()+ if name in zones:+ return zones[name]+ return zones.get(_zone_links().get(name, ""))+++def country_code(value: str | None) -> str | None:+ """An ISO 3166-1 alpha-2 code we know a name for, or None."""+ code = (value or "").strip().upper()+ return code if code in _country_names() else None+++def country_name(code: str) -> str:+ return _country_names()[code]+++def email_domain(email: str) -> str:+ return email.strip().lower().rpartition("@")[2]+++def email_type(email: str) -> str:+ """education for a school or university address, personal for a webmail+ provider's, and business for everything else."""+ domain = email_domain(email)+ labels = domain.split(".")+ if labels[-1] == "edu" or (+ len(labels) >= 3 and len(labels[-1]) == 2 and labels[-2] in ("edu", "ac")+ ):+ return "education"+ if domain in _PERSONAL_DOMAINS or domain.startswith(_PERSONAL_PREFIXES):+ return "personal"+ return "business"+++def email_points_at_de_at(email: str) -> bool:+ """A .de or .at address, whatever sits under the country code (.co.at,+ .ac.at)."""+ return email_domain(email).rsplit(".", 1)[-1] in ("de", "at")+++def signin_method(providers: Iterable[str]) -> str | None:+ """How the account signs in, from its auth provider ids: google wins over+ a password, since it proves the address."""+ found = {p.strip().lower() for p in providers if p}+ if "google" in found:+ return "google"+ if found & {"credential", "email"}:+ return "email"+ return min(found) if found else None+++def checkout_fields(+ *,+ email: str,+ created_at: datetime,+ opened_at: datetime | int | float | None,+ signin_providers: Iterable[str],+ timezone: str | None,+ stripe_country: str | None = None,+ ip_country: str | None = None,+ status: str = SubscriptionStatus.SIGNED.value,+) -> Fields:+ """Every field a checkout opener gets. Merge it with what MailerLite holds+ (`merge_with_held`) before writing."""+ tz_country = timezone_country(timezone)+ candidates = {+ "stripe": country_code(stripe_country),+ "ip": country_code(ip_country),+ "timezone": tz_country,+ }+ signals = {code for code in candidates.values() if code}+ fields: Fields = {+ SubscriberField.STATUS: status,+ SubscriberField.SIGNUP: mailerlite_date(created_at),+ SubscriberField.CHECKOUT_OPENED: mailerlite_date(opened_at),+ SubscriberField.EMAIL_TYPE: email_type(email),+ SubscriberField.EXCLUDE_DE_AT: (+ YES if signals & DE_AT or email_points_at_de_at(email) else NO+ ),+ }+ method = signin_method(signin_providers)+ if method:+ fields[SubscriberField.SIGNIN_METHOD] = method+ for source in COUNTRY_SOURCES:+ code = candidates[source]+ if code:+ fields[SubscriberField.COUNTRY] = country_name(code)+ fields[SubscriberField.COUNTRY_CODE] = code+ fields[SubscriberField.COUNTRY_SOURCE] = source+ break+ return fields+++_COUNTRY_FIELDS = (+ SubscriberField.COUNTRY,+ SubscriberField.COUNTRY_CODE,+ SubscriberField.COUNTRY_SOURCE,+)++# A hand-typed built-in country that names Germany or Austria.+_DE_AT_NAMES = frozenset({"germany", "deutschland", "austria", "österreich"})+++def _held_points_at_de_at(held: Mapping[str, object]) -> bool:+ """A country MailerLite already holds is a signal too, whether ours or+ typed by hand, and the exclusion holds for any signal."""+ code = str(held.get(SubscriberField.COUNTRY_CODE.value) or "").strip().upper()+ name = str(held.get(SubscriberField.COUNTRY.value) or "").strip().lower()+ return code in DE_AT or name in _DE_AT_NAMES+++def merge_with_held(+ fields: Mapping[SubscriberField, str | None],+ held: Mapping[str, object],+ *,+ keep_held_status: bool = True,+) -> Fields:+ """Drop whatever would make MailerLite's copy worse:++ - a status it already holds (unless the caller knows the true one), since+ `signed` is only where everyone starts;+ - a later checkout_opened_date than the one it holds;+ - exclude_de_at "no" over "yes", or over a German or Austrian country it+ holds;+ - a country from a weaker source than the one it holds.+ """+ merged = dict(fields)+ if keep_held_status and held.get(SubscriberField.STATUS.value):+ merged.pop(SubscriberField.STATUS, None)+ held_opened = str(held.get(SubscriberField.CHECKOUT_OPENED.value) or "")[:10]+ ours_opened = merged.get(SubscriberField.CHECKOUT_OPENED)+ if held_opened and ours_opened and held_opened <= ours_opened:+ merged.pop(SubscriberField.CHECKOUT_OPENED)+ if held.get(SubscriberField.EXCLUDE_DE_AT.value) == YES:+ merged.pop(SubscriberField.EXCLUDE_DE_AT, None)+ elif _held_points_at_de_at(held):+ merged[SubscriberField.EXCLUDE_DE_AT] = YES+ held_rank = _SOURCE_RANK.get(str(held.get(SubscriberField.COUNTRY_SOURCE.value)), 0)+ ours_rank = _SOURCE_RANK.get(str(merged.get(SubscriberField.COUNTRY_SOURCE)), 0)+ if held_rank > ours_rank:+ for field in _COUNTRY_FIELDS:+ merged.pop(field, None)+ return mergeddiff --git a/autogpt_platform/backend/backend/notifications/audience_enrichment_test.py b/autogpt_platform/backend/backend/notifications/audience_enrichment_test.pynew file mode 100644index 0000000000..c29d9253a0--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/audience_enrichment_test.py@@ -0,0 +1,248 @@+"""What GTM segments checkout openers on: country from the strongest source,+Germany and Austria caught by any signal and never released, and nothing+MailerLite holds made worse by a later or weaker write."""++from datetime import UTC, datetime++import pytest++from backend.data.notifications import SubscriberField+from backend.notifications import audience_enrichment as enrichment++CREATED = datetime(2026, 7, 14, 9, 0, tzinfo=UTC)+OPENED = datetime(2026, 7, 15, 9, 0, tzinfo=UTC)+++ "timezone, country",+ [+ ("Asia/Kolkata", "IN"),+ # Old names browsers still report, followed through tzdata's links.+ ("Asia/Calcutta", "IN"),+ ("Asia/Katmandu", "NP"),+ ("Europe/Kiev", "UA"),+ # tzdata links Stockholm to Berlin; the reported name must win.+ ("Europe/Stockholm", "SE"),+ ("Europe/Berlin", "DE"),+ ("Europe/Busingen", "DE"),+ ("Europe/Vienna", "AT"),+ ("America/Chicago", "US"),+ # A link that points across a border keeps its own country.+ ("Atlantic/Jan_Mayen", "SJ"),+ (" Asia/Kolkata ", "IN"),+ ("UTC", None),+ ("Etc/GMT+3", None),+ ("not-set", None),+ ("", None),+ (None, None),+ ("Mars/Olympus_Mons", None),+ ],+)+def test_a_timezone_names_its_country(timezone, country):+ assert enrichment.timezone_country(timezone) == country+++def test_country_names_read_well_in_a_segment_filter():+ assert enrichment.country_name("GB") == "United Kingdom"+ assert enrichment.country_name("KR") == "South Korea"+ assert enrichment.country_name("US") == "United States"+ assert enrichment.country_name("IN") == "India"+++ "value, code",+ [("us", "US"), (" DE ", "DE"), ("XX", None), ("", None), (None, None)],+)+def test_only_known_country_codes_are_kept(value, code):+ assert enrichment.country_code(value) == code+++ "email, kind",+ [+ ("[email protected]", "personal"),+ ("[email protected]", "personal"),+ ("[email protected]", "personal"),+ ("[email protected]", "personal"),+ ("[email protected]", "business"),+ ("[email protected]", "business"),+ ("[email protected]", "education"),+ ("[email protected]", "education"),+ ("[email protected]", "education"),+ ],+)+def test_an_address_is_business_personal_or_education(email, kind):+ assert enrichment.email_type(email) == kind+++ "email, de_at",+ [+ ("[email protected]", True),+ ("[email protected]", True),+ ("[email protected]", True),+ ("[email protected]", False),+ ("[email protected]", False),+ ],+)+def test_a_de_or_at_address_counts_whatever_sits_under_it(email, de_at):+ assert enrichment.email_points_at_de_at(email) is de_at+++ "providers, method",+ [+ (["google"], "google"),+ (["credential", "google"], "google"),+ (["credential"], "email"),+ (["github"], "github"),+ ([], None),+ ],+)+def test_google_wins_the_signin_method(providers, method):+ assert enrichment.signin_method(providers) == method+++def _fields(**kwargs) -> dict:+ defaults = dict(+ email="[email protected]",+ created_at=CREATED,+ opened_at=OPENED,+ signin_providers=["google"],+ timezone="Asia/Kolkata",+ )+ return enrichment.checkout_fields(**{**defaults, **kwargs})+++def test_a_checkout_opener_gets_every_field():+ assert _fields() == {+ SubscriberField.STATUS: "signed",+ SubscriberField.SIGNUP: "2026-07-14",+ SubscriberField.CHECKOUT_OPENED: "2026-07-15",+ SubscriberField.EMAIL_TYPE: "business",+ SubscriberField.SIGNIN_METHOD: "google",+ SubscriberField.COUNTRY: "India",+ SubscriberField.COUNTRY_CODE: "IN",+ SubscriberField.COUNTRY_SOURCE: "timezone",+ SubscriberField.EXCLUDE_DE_AT: "no",+ }+++ "sources, country, source",+ [+ (dict(stripe_country="US", ip_country="GB"), "US", "stripe"),+ (dict(ip_country="GB"), "GB", "ip"),+ (dict(stripe_country="??", ip_country="GB"), "GB", "ip"),+ ({}, "IN", "timezone"),+ ],+)+def test_country_comes_from_the_strongest_source(sources, country, source):+ fields = _fields(**sources)+ assert fields[SubscriberField.COUNTRY_CODE] == country+ assert fields[SubscriberField.COUNTRY_SOURCE] == source+++def test_without_any_country_signal_the_country_is_left_alone():+ fields = _fields(timezone="UTC", signin_providers=[])+ for field in (+ SubscriberField.COUNTRY,+ SubscriberField.COUNTRY_CODE,+ SubscriberField.COUNTRY_SOURCE,+ SubscriberField.SIGNIN_METHOD,+ ):+ assert field not in fields+++ "signals",+ [+ dict(timezone="Europe/Berlin"),+ dict(timezone="Europe/Vienna"),+ dict(ip_country="AT"),+ # The billing address is German even though the IP and timezone say UK.+ dict(stripe_country="DE", ip_country="GB", timezone="Europe/London"),+ # A weaker signal still excludes when a stronger one names elsewhere.+ dict(stripe_country="US", timezone="Europe/Berlin"),+ dict(email="[email protected]", timezone="America/Chicago"),+ ],+)+def test_any_de_or_at_signal_excludes(signals):+ assert _fields(**signals)[SubscriberField.EXCLUDE_DE_AT] == "yes"+++def _held(**fields) -> dict:+ return {SubscriberField[k.upper()].value: v for k, v in fields.items()}+++def test_a_status_mailerlite_holds_is_kept_from_signed():+ merged = enrichment.merge_with_held(_fields(), _held(status="subscribed"))+ assert SubscriberField.STATUS not in merged+++def test_a_known_status_replaces_the_held_one_when_asked():+ merged = enrichment.merge_with_held(+ _fields(status="in_trial"),+ _held(status="signed"),+ keep_held_status=False,+ )+ assert merged[SubscriberField.STATUS] == "in_trial"+++ "held, kept",+ [("2026-07-01", True), ("2026-07-15", True), ("2026-07-20T10:00:00", False)],+)+def test_the_first_checkout_date_is_kept(held, kept):+ merged = enrichment.merge_with_held(_fields(), _held(checkout_opened=held))+ assert (SubscriberField.CHECKOUT_OPENED not in merged) is kept+++def test_exclude_de_at_is_never_released():+ merged = enrichment.merge_with_held(_fields(), _held(exclude_de_at="yes"))+ assert SubscriberField.EXCLUDE_DE_AT not in merged+++ "held",+ [+ # Ours, from Stripe, written before the exclusion ever was.+ {"country": "Germany", "country_code": "DE", "country_source": "stripe"},+ {"country_code": "at"},+ # Typed by hand into MailerLite's built-in field.+ {"country": "Deutschland"},+ {"country": " Austria "},+ ],+)+def test_a_held_german_or_austrian_country_excludes(held):+ """The live guess (India by timezone) says no, but MailerLite already+ holds a German or Austrian country: that is a signal, so it excludes."""+ merged = enrichment.merge_with_held(_fields(), held)+ assert merged[SubscriberField.EXCLUDE_DE_AT] == "yes"+++def test_a_held_country_elsewhere_does_not_exclude():+ merged = enrichment.merge_with_held(_fields(), {"country": "United States"})+ assert merged[SubscriberField.EXCLUDE_DE_AT] == "no"+++def test_a_weaker_country_never_replaces_a_stronger_one():+ held = _held(country="Germany", country_code="DE", country_source="stripe")+ merged = enrichment.merge_with_held(_fields(ip_country="GB"), held)+ for field in (+ SubscriberField.COUNTRY,+ SubscriberField.COUNTRY_CODE,+ SubscriberField.COUNTRY_SOURCE,+ ):+ assert field not in merged+++def test_a_stronger_country_replaces_a_weaker_one():+ held = _held(country="India", country_code="IN", country_source="timezone")+ merged = enrichment.merge_with_held(_fields(stripe_country="US"), held)+ assert merged[SubscriberField.COUNTRY_CODE] == "US"+++def test_a_hand_typed_country_is_replaced():+ merged = enrichment.merge_with_held(_fields(), {"country": "USA"})+ assert merged[SubscriberField.COUNTRY] == "India"diff --git a/autogpt_platform/backend/backend/notifications/checkout_audience_test.py b/autogpt_platform/backend/backend/notifications/checkout_audience_test.pynew file mode 100644index 0000000000..c2bcaa4827--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/checkout_audience_test.py@@ -0,0 +1,198 @@+"""The notification service puts a checkout opener in the checkout openers+group with the fields GTM segments on, and a later or weaker event never+makes MailerLite's copy worse."""++from datetime import UTC, datetime+from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.data.notifications import AudienceAction+from backend.notifications import mailerlite, subscriber_fields+from backend.notifications.audience_enrichment import checkout_fields+from backend.notifications.notifications import NotificationManager++EMAIL = "[email protected]"+CREATED = datetime(2026, 7, 14, 9, 0, tzinfo=UTC)+++def mailerlite_configured(monkeypatch):+ fake = SimpleNamespace(+ config=SimpleNamespace(+ mailerlite_checkout_group_id="grp_checkout",+ 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)+ monkeypatch.setattr(mailerlite, "_checkout_off_logged", False)+ return fake+++def _response(status: int, body: dict | None = None) -> MagicMock:+ response = MagicMock(status=status)+ response.json.return_value = body or {}+ return response+++class _FakeMailerLite:+ """Subscribers as MailerLite holds them: an upsert merges its fields and+ groups into whatever is already there."""++ def __init__(self, held: dict[str, dict] | None = None):+ self.subscribers = held or {}+ self.groups: dict[str, set[str]] = {}+ self.posts: list[dict] = []++ async def get(self, url: str, **_) -> MagicMock:+ if "/fields" in url:+ return _response(+ 200,+ {+ "data": [+ {"key": f.value, "type": t}+ for f, t in mailerlite.FIELD_TYPES.items()+ ],+ "meta": {"last_page": 1},+ },+ )+ 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:+ self.posts.append(json)+ held = self.subscribers.setdefault(json["email"], {})+ held.update(json.get("fields") or {})+ self.groups.setdefault(json["email"], set()).update(json.get("groups") or [])+ return _response(200)+++def _event(**kwargs):+ defaults = dict(+ email=EMAIL,+ created_at=CREATED,+ opened_at=datetime(2026, 7, 15, tzinfo=UTC),+ signin_providers=["google"],+ timezone="Asia/Kolkata",+ )+ fields = checkout_fields(**{**defaults, **kwargs})+ return subscriber_fields.audience_event(+ AudienceAction.CHECKOUT_OPENED, EMAIL, "user-1", fields+ )+++async def _consume(*events) -> None:+ for event in events:+ assert await NotificationManager._process_audience_change(+ MagicMock(), event.model_dump_json()+ )+++# ── the handler ────────────────────────────────────────────────────────────+++async def test_a_checkout_opener_joins_the_group_with_every_field(+ mailerlite_configured,+):+ ml = _FakeMailerLite()+ with patch.object(mailerlite, "_client", return_value=ml):+ await _consume(_event(ip_country="IN"))+ assert ml.groups[EMAIL] == {"grp_checkout"}+ assert ml.subscribers[EMAIL] == {+ "subscription_status": "signed",+ "signup_date": "2026-07-14",+ "checkout_opened_date": "2026-07-15",+ "email_type": "business",+ "signin_method": "google",+ "country": "India",+ "country_code": "IN",+ "country_source": "ip",+ "exclude_de_at": "no",+ }+++async def test_without_a_checkout_group_openers_are_dropped_not_dead_lettered(+ mailerlite_configured, caplog+):+ """The group is the switch: off, nothing is written and the message is+ acknowledged, said once; the backfill brings in anyone missed."""+ mailerlite_configured.config.mailerlite_checkout_group_id = ""+ ml = _FakeMailerLite()+ ml.post = AsyncMock(side_effect=ml.post)+ with (+ patch.object(mailerlite, "_client", return_value=ml),+ caplog.at_level("INFO"),+ ):+ await _consume(_event(), _event())+ ml.post.assert_not_awaited()+ assert caplog.text.count("MAILERLITE_CHECKOUT_GROUP_ID is not set") == 1+++async def test_a_later_checkout_never_makes_mailerlite_worse(mailerlite_configured):+ """A paying customer opening checkout again, from a UK IP in a UK+ timezone, keeps their status, their first open date, their billing+ country and their exclusion."""+ held = {+ "subscription_status": "subscribed",+ "checkout_opened_date": "2026-07-01",+ "country": "Germany",+ "country_code": "DE",+ "country_source": "stripe",+ "exclude_de_at": "yes",+ }+ ml = _FakeMailerLite({EMAIL: dict(held)})+ with patch.object(mailerlite, "_client", return_value=ml):+ await _consume(_event(ip_country="GB", timezone="Europe/London"))+ for key, value in held.items():+ assert ml.subscribers[EMAIL][key] == value+ assert ml.groups[EMAIL] == {"grp_checkout"}+++async def test_the_billing_country_upgrades_a_timezone_guess(mailerlite_configured):+ ml = _FakeMailerLite(+ {+ EMAIL: {+ "country_code": "IN",+ "country_source": "timezone",+ "exclude_de_at": "no",+ }+ }+ )+ with patch.object(mailerlite, "_client", return_value=ml):+ await _consume(_event(stripe_country="AT"))+ assert ml.subscribers[EMAIL]["country"] == "Austria"+ assert ml.subscribers[EMAIL]["country_source"] == "stripe"+ assert ml.subscribers[EMAIL]["exclude_de_at"] == "yes"+++async def test_every_audience_action_has_a_handler(monkeypatch):+ """An action the consumer cannot route would raise on every delivery."""+ monkeypatch.setattr(mailerlite, "configured", lambda: True)+ handlers = {}+ for name in (+ "enroll_in_onboarding",+ "add_to_changelog",+ "remove_from_changelog",+ "add_to_trial",+ "remove_from_trial",+ "update_fields",+ "record_signup",+ "record_checkout_opened",+ ):+ handlers[name] = AsyncMock()+ monkeypatch.setattr(mailerlite, name, handlers[name])+ for action in AudienceAction:+ event = subscriber_fields.audience_event(action, EMAIL, "user-1")+ await _consume(event)+ assert all(h.await_count == 1 for h in handlers.values())diff --git a/autogpt_platform/backend/backend/notifications/checkout_backfill.py b/autogpt_platform/backend/backend/notifications/checkout_backfill.pynew file mode 100644index 0000000000..e8c34d81ef--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/checkout_backfill.py@@ -0,0 +1,242 @@+"""Put everyone who has opened Stripe checkout where the live checkout event+would have: in the checkout openers group, with their status, dates and the+fields GTM segments on (`audience_enrichment`).++Only accounts with at least one Stripe Checkout Session are openers. Nobody+else is written, so the accounts that never reached checkout stay out of+MailerLite.++Each person is written with one subscriber upsert, one at a time and paced,+never a /batch: MailerLite processes an upsert-only batch as an import, and+imports were seen to answer OK for existing subscribers without storing their+fields. The pace leaves MailerLite's per-account limit for the live consumer.++The live consumer keeps writing while a run lasts its hours, so the plan is+only a list of who to visit: each person is read again just before their+write and merged by the same rules as the live event, so a German IP the+live event recorded mid-run is never overwritten by the plan's older guess.+That also makes a run resumable: an interrupted or repeated one picks up+where the last one stopped.+"""++import asyncio+import logging+from collections import Counter+from collections.abc import Awaitable, Callable, Mapping++from pydantic import BaseModel++from backend.data.notifications import SubscriberField+from backend.notifications.audience_enrichment import checkout_fields, merge_with_held+from backend.notifications.mailerlite import (+ API_BASE,+ _client,+ _find_subscriber,+ _headers,+ _payload,+)+from backend.notifications.mailerlite_backfill import (+ BatchAnswer,+ Subscription,+ _refusal,+)+from backend.notifications.mailerlite_field_backfill import (+ Current,+ Person,+ _normalise,+ _valid,+ desired,+)+from backend.notifications.subscriber_fields import Fields++logger = logging.getLogger(__name__)++# One person every two seconds, a read and a write each: half MailerLite's+# 120 calls a minute, so the live consumer keeps the rest.+WRITE_INTERVAL_SECONDS = 2.0+++class Opener(BaseModel):+ """An account that opened Stripe checkout, with what we know of it."""++ person: Person+ # The first Checkout Session's creation, as a Unix timestamp.+ opened_at: int+ signin_providers: list[str] = []+ # The Stripe customer's billing address country, when it has one.+ stripe_country: str | None = None+++class OpenerChange(BaseModel):+ opener: Opener+ # Only the fields that differ from MailerLite's.+ fields: Fields+ # Not a MailerLite subscriber yet: writing creates one.+ new: bool+ # Not in the checkout openers group yet.+ joins: bool+++class OpenerPlan(BaseModel):+ changes: list[OpenerChange]+ openers: int+ # Addresses MailerLite would refuse, such as a reserved domain.+ invalid: int+ # What the openers end up with, for the report.+ country_sources: dict[str, int]+ countries: dict[str, int]+ email_types: dict[str, int]+ signin_methods: dict[str, int]+ exclude_de_at: int+++def wanted(opener: Opener) -> Fields:+ """Everything the opener should hold: the checkout segmentation, with the+ status and dates worked out from Stripe in place of the live default."""+ status, standing_fields = desired(opener.person)+ fields = checkout_fields(+ email=opener.person.email,+ created_at=opener.person.created_at,+ opened_at=opener.opened_at,+ signin_providers=opener.signin_providers,+ timezone=opener.person.timezone,+ stripe_country=opener.stripe_country,+ status=status.value,+ )+ return {**fields, **standing_fields}+++def plan(+ openers: list[Opener], current: Current, members: Mapping[str, str]+) -> OpenerPlan:+ changes: list[OpenerChange] = []+ invalid = 0+ sources: Counter[str] = Counter()+ countries: Counter[str] = Counter()+ email_types: Counter[str] = Counter()+ methods: Counter[str] = Counter()+ exclude = 0+ for opener in openers:+ email = opener.person.email+ if not _valid(email):+ invalid += 1+ continue+ key = email.strip().lower()+ held = current.get(key)+ fields = _merged(opener, held)+ final = {**(held or {}), **{k.value: v for k, v in fields.items()}}+ sources[str(final.get(SubscriberField.COUNTRY_SOURCE.value) or "unknown")] += 1+ countries[str(final.get(SubscriberField.COUNTRY_CODE.value) or "unknown")] += 1+ email_types[str(final.get(SubscriberField.EMAIL_TYPE.value))] += 1+ methods[str(final.get(SubscriberField.SIGNIN_METHOD.value) or "unknown")] += 1+ exclude += final.get(SubscriberField.EXCLUDE_DE_AT.value) == "yes"+ differ = _changed(fields, held)+ joins = key not in members+ if differ or joins:+ changes.append(+ OpenerChange(+ opener=opener, fields=differ, new=held is None, joins=joins+ )+ )+ return OpenerPlan(+ changes=changes,+ openers=len(openers),+ invalid=invalid,+ country_sources=dict(sources),+ countries=dict(countries),+ email_types=dict(email_types),+ signin_methods=dict(methods),+ exclude_de_at=exclude,+ )+++def _merged(opener: Opener, held: Mapping[str, object] | None) -> Fields:+ """Everything the opener should hold, by the live event's rules against+ what MailerLite holds; the status is Stripe's, so it always wins."""+ return merge_with_held(wanted(opener), held or {}, keep_held_status=False)+++def _changed(fields: Fields, held: Mapping[str, object] | None) -> Fields:+ """The fields MailerLite does not already hold: all of them for someone+ it does not hold at all."""+ return {+ field: value+ for field, value in fields.items()+ if held is None or _normalise(field, held.get(field.value)) != value+ }+++async def apply(+ changes: list[OpenerChange],+ group_id: str,+ on_progress: Callable[[int, int], None] | None = None,+ *,+ refresh: Callable[[str], Awaitable[list[Subscription]]] | None = None,+) -> tuple[int, int, int]:+ """Visit each planned opener: read them again from MailerLite and, with+ `refresh`, their subscriptions again from Stripe, and write what they+ still need as one subscriber upsert into the group. The status the plan+ took from Stripe hours earlier is never written over a newer one.++ Returns (succeeded, failed, skipped); a failure is logged with+ MailerLite's reason and left for the next run, and someone who needs+ nothing any more is skipped."""+ succeeded = failed = skipped = 0+ for index, change in enumerate(changes):+ if index:+ await asyncio.sleep(WRITE_INTERVAL_SECONDS)+ if on_progress and index and index % 100 == 0:+ on_progress(index, len(changes))+ email = change.opener.person.email+ opener = change.opener+ try:+ if refresh and opener.person.stripe_customer_id:+ person = opener.person.model_copy(+ update={+ "subscriptions": await refresh(opener.person.stripe_customer_id)+ }+ )+ opener = opener.model_copy(update={"person": person})+ subscriber = await _find_subscriber(email)+ except Exception:+ failed += 1+ logger.warning(+ "Re-reading checkout opener %s failed; the next run retries it",+ _refusal(email, BatchAnswer(code=0)),+ )+ continue+ held = None if subscriber is None else (subscriber.get("fields") or {})+ fields = _changed(_merged(opener, held), held)+ if not fields and not change.joins:+ skipped += 1+ continue+ body: dict = {"email": email, "groups": [group_id]}+ if fields:+ body["fields"] = _payload(fields)+ try:+ response = await _client().post(+ f"{API_BASE}/subscribers", headers=_headers(), json=body+ )+ except Exception:+ # One person's network failure must not end a run of thousands.+ failed += 1+ logger.warning(+ "Checkout opener write failed for %s; the next run retries it",+ _refusal(email, BatchAnswer(code=0)),+ )+ continue+ if response.status in (200, 201, 202, 204):+ succeeded += 1+ else:+ failed += 1+ try:+ answer_body = response.json()+ except Exception:+ answer_body = None+ logger.warning(+ "Checkout opener write failed for %s; the next run retries it",+ _refusal(email, BatchAnswer(code=response.status, body=answer_body)),+ )+ if on_progress:+ on_progress(len(changes), len(changes))+ return succeeded, failed, skippeddiff --git a/autogpt_platform/backend/backend/notifications/checkout_backfill_test.py b/autogpt_platform/backend/backend/notifications/checkout_backfill_test.pynew file mode 100644index 0000000000..a9083e5ed1--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/checkout_backfill_test.py@@ -0,0 +1,311 @@+"""The backfill puts existing checkout openers where the live event would+have, writes only the difference, never makes MailerLite's copy worse, and+writes one subscriber at a time so MailerLite stores what it accepts."""++from datetime import UTC, datetime+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.data.notifications import SubscriberField+from backend.notifications import checkout_backfill, mailerlite+from backend.notifications.mailerlite_backfill import Subscription+from backend.notifications.mailerlite_field_backfill import Person++OPENED = int(datetime(2026, 7, 15, 9, 0, tzinfo=UTC).timestamp())+++def _opener(email="[email protected]", subscriptions=None, **kwargs):+ return checkout_backfill.Opener(+ person=Person(+ user_id="user-1",+ email=email,+ created_at=datetime(2026, 7, 14, tzinfo=UTC),+ subscriptions=subscriptions or [],+ stripe_customer_id="cus_1",+ timezone=kwargs.pop("timezone", "Asia/Kolkata"),+ ),+ opened_at=OPENED,+ signin_providers=["google"],+ **kwargs,+ )+++def _held(opener) -> dict:+ """Exactly what a full write of this opener leaves in MailerLite."""+ return {+ field.value: value for field, value in checkout_backfill.wanted(opener).items()+ }+++def test_a_new_opener_is_created_in_the_group_with_every_field():+ opener = _opener()+ plan = checkout_backfill.plan([opener], current={}, members={})+ (change,) = plan.changes+ assert change.new and change.joins+ assert change.fields[SubscriberField.COUNTRY_CODE] == "IN"+ assert change.fields[SubscriberField.COUNTRY_SOURCE] == "timezone"+ assert change.fields[SubscriberField.CHECKOUT_OPENED] == "2026-07-15"+ assert change.fields[SubscriberField.STATUS] == "signed"+++def test_an_opener_already_in_place_is_left_alone():+ opener = _opener()+ plan = checkout_backfill.plan(+ [opener], current={"[email protected]": _held(opener)}, members={"[email protected]": "1"}+ )+ assert plan.changes == []+ assert plan.openers == 1+++def test_an_existing_subscriber_only_joins_the_group():+ opener = _opener()+ plan = checkout_backfill.plan(+ [opener], current={"[email protected]": _held(opener)}, members={}+ )+ (change,) = plan.changes+ assert change.joins and not change.new and change.fields == {}+++def test_the_status_comes_from_stripe_not_the_signed_default():+ opener = _opener(subscriptions=[Subscription(status="active", start_date=OPENED)])+ held = {**_held(_opener()), "subscription_status": "signed"}+ plan = checkout_backfill.plan(+ [opener], current={"[email protected]": held}, members={"[email protected]": "1"}+ )+ (change,) = plan.changes+ assert change.fields[SubscriberField.STATUS] == "subscribed"+++def test_a_billing_country_is_used_and_never_downgraded_to_the_timezone():+ with_billing = _opener(stripe_country="DE")+ plan = checkout_backfill.plan([with_billing], current={}, members={})+ assert plan.changes[0].fields[SubscriberField.COUNTRY_CODE] == "DE"+ assert plan.changes[0].fields[SubscriberField.EXCLUDE_DE_AT] == "yes"+ assert plan.exclude_de_at == 1++ held = {+ **_held(_opener()),+ "country": "Germany",+ "country_code": "DE",+ "country_source": "stripe",+ "exclude_de_at": "yes",+ }+ plan = checkout_backfill.plan(+ [_opener()], current={"[email protected]": held}, members={"[email protected]": "1"}+ )+ assert plan.changes == []+ assert plan.countries == {"DE": 1}+++def test_an_address_mailerlite_would_refuse_is_skipped():+ plan = checkout_backfill.plan([_opener(email="[email protected]")], {}, {})+ assert plan.changes == [] and plan.invalid == 1+++async def test_each_opener_is_one_paced_upsert_into_the_group(monkeypatch, caplog):+ sleep = AsyncMock()+ monkeypatch.setattr(checkout_backfill.asyncio, "sleep", sleep)+ plan = checkout_backfill.plan(+ [_opener(email="[email protected]"), _opener(email="[email protected]")], {}, {}+ )+ refusal = {"message": "The given data was invalid.", "errors": {"email": ["no"]}}+ client = MagicMock(+ post=AsyncMock(side_effect=[MagicMock(status=200), _refused(refusal)])+ )+ with (+ patch.object(checkout_backfill, "_client", return_value=client),+ patch.object(+ checkout_backfill, "_find_subscriber", AsyncMock(return_value=None)+ ),+ caplog.at_level("WARNING"),+ ):+ result = await checkout_backfill.apply(plan.changes, "grp_checkout")+ assert result == (1, 1, 0)+ first = client.post.await_args_list[0]+ assert first.args[0].endswith("/subscribers")+ assert first.kwargs["json"]["groups"] == ["grp_checkout"]+ assert first.kwargs["json"]["fields"]["country_code"] == "IN"+ sleep.assert_awaited_once_with(checkout_backfill.WRITE_INTERVAL_SECONDS)+ assert "The given data was invalid." in caplog.text+ assert "[email protected]" not in caplog.text+++async def test_a_live_write_made_during_the_run_is_not_overwritten(monkeypatch):+ """Planned from an early snapshot as Indian by timezone; while the run+ went on, the live event recorded a German billing address. The write is+ merged against a fresh read, so Germany and the exclusion stay."""+ monkeypatch.setattr(checkout_backfill.asyncio, "sleep", AsyncMock())+ opener = _opener()+ plan = checkout_backfill.plan([opener], current={}, members={})+ fresh = {+ **_held(opener),+ "country": "Germany",+ "country_code": "DE",+ "country_source": "stripe",+ "exclude_de_at": "yes",+ }+ client = MagicMock(post=AsyncMock(return_value=MagicMock(status=200)))+ with (+ patch.object(checkout_backfill, "_client", return_value=client),+ patch.object(+ checkout_backfill,+ "_find_subscriber",+ AsyncMock(return_value={"fields": fresh}),+ ),+ ):+ result = await checkout_backfill.apply(plan.changes, "grp_checkout")+ assert result == (1, 0, 0)+ body = client.post.await_args.kwargs["json"]+ assert body["groups"] == ["grp_checkout"]+ assert "fields" not in body+++async def test_someone_already_up_to_date_is_skipped(monkeypatch):+ monkeypatch.setattr(checkout_backfill.asyncio, "sleep", AsyncMock())+ opener = _opener()+ plan = checkout_backfill.plan(+ [opener], current={"[email protected]": {}}, members={"[email protected]": "1"}+ )+ client = MagicMock(post=AsyncMock())+ with (+ patch.object(checkout_backfill, "_client", return_value=client),+ patch.object(+ checkout_backfill,+ "_find_subscriber",+ AsyncMock(return_value={"fields": _held(opener)}),+ ),+ ):+ result = await checkout_backfill.apply(plan.changes, "grp_checkout")+ assert result == (0, 0, 1)+ client.post.assert_not_awaited()+++async def test_a_failed_reread_is_counted_and_retried_later(monkeypatch, caplog):+ monkeypatch.setattr(checkout_backfill.asyncio, "sleep", AsyncMock())+ plan = checkout_backfill.plan([_opener()], current={}, members={})+ client = MagicMock(post=AsyncMock())+ with (+ patch.object(checkout_backfill, "_client", return_value=client),+ patch.object(+ checkout_backfill,+ "_find_subscriber",+ AsyncMock(side_effect=RuntimeError("MailerLite down")),+ ),+ caplog.at_level("WARNING"),+ ):+ result = await checkout_backfill.apply(plan.changes, "grp_checkout")+ assert result == (0, 1, 0)+ client.post.assert_not_awaited()+ assert "[email protected]" not in caplog.text+++def _refused(body: dict) -> MagicMock:+ response = MagicMock(status=422)+ response.json.return_value = body+ return response+++def test_the_built_in_country_is_written_but_never_created():+ assert SubscriberField.COUNTRY not in mailerlite.FIELD_TYPES+ assert mailerlite.field_type(SubscriberField.COUNTRY) == "text"+ assert mailerlite.FIELD_TYPES[SubscriberField.CHECKOUT_OPENED] == "date"+++async def test_a_failed_write_is_counted_and_the_run_goes_on(monkeypatch, caplog):+ """A network error on one person's write must not end a run of+ thousands: it is counted, logged without the address, and the next+ person is still written."""+ monkeypatch.setattr(checkout_backfill.asyncio, "sleep", AsyncMock())+ plan = checkout_backfill.plan(+ [_opener(email="[email protected]"), _opener(email="[email protected]")], {}, {}+ )+ client = MagicMock(+ post=AsyncMock(+ side_effect=[RuntimeError("connection reset"), MagicMock(status=200)]+ )+ )+ with (+ patch.object(checkout_backfill, "_client", return_value=client),+ patch.object(+ checkout_backfill, "_find_subscriber", AsyncMock(return_value=None)+ ),+ caplog.at_level("WARNING"),+ ):+ result = await checkout_backfill.apply(plan.changes, "grp_checkout")+ assert result == (1, 1, 0)+ assert client.post.await_count == 2+ assert "[email protected]" not in caplog.text+++ACTIVE = [Subscription(id="sub_1", status="active", start_date=OPENED)]+++async def _apply_with(monkeypatch, plan, *, held, refresh):+ monkeypatch.setattr(checkout_backfill.asyncio, "sleep", AsyncMock())+ client = MagicMock(post=AsyncMock(return_value=MagicMock(status=200)))+ with (+ patch.object(checkout_backfill, "_client", return_value=client),+ patch.object(+ checkout_backfill, "_find_subscriber", AsyncMock(return_value=held)+ ),+ ):+ result = await checkout_backfill.apply(+ plan.changes, "grp_checkout", refresh=refresh+ )+ return result, client+++async def test_the_status_written_is_stripes_now_not_the_plans(monkeypatch):+ """Planned hours earlier as a signup; the trial converted since. The+ write re-reads Stripe, so it says subscribed, not the snapshot's signed."""+ plan = checkout_backfill.plan([_opener()], current={}, members={})+ assert plan.changes[0].fields[SubscriberField.STATUS] == "signed"+ refresh = AsyncMock(return_value=ACTIVE)+ result, client = await _apply_with(monkeypatch, plan, held=None, refresh=refresh)+ assert result == (1, 0, 0)+ refresh.assert_awaited_once_with("cus_1")+ fields = client.post.await_args.kwargs["json"]["fields"]+ assert fields["subscription_status"] == "subscribed"+ assert fields["subscription_started_date"] == "2026-07-15"+++async def test_a_newer_status_the_live_event_wrote_is_not_overwritten(monkeypatch):+ """The live lifecycle event already wrote subscribed during the run; the+ snapshot still said signed. Stripe now agrees with the live write, so+ nothing is left to write."""+ # Not in MailerLite when planned (already in the group by the time it+ # runs, joined by the live event), so the plan wants the snapshot's signed.+ plan = checkout_backfill.plan(+ [_opener()], current={}, members={"[email protected]": "1"}+ )+ assert plan.changes[0].fields[SubscriberField.STATUS] == "signed"+ live = _held(_opener(subscriptions=ACTIVE))+ result, client = await _apply_with(+ monkeypatch,+ plan,+ held={"fields": live},+ refresh=AsyncMock(return_value=ACTIVE),+ )+ assert result == (0, 0, 1)+ client.post.assert_not_awaited()+++async def test_a_failed_stripe_refresh_is_counted_and_retried_later(monkeypatch):+ plan = checkout_backfill.plan([_opener()], current={}, members={})+ result, client = await _apply_with(+ monkeypatch,+ plan,+ held=None,+ refresh=AsyncMock(side_effect=RuntimeError("Stripe down")),+ )+ assert result == (0, 1, 0)+ client.post.assert_not_awaited()diff --git a/autogpt_platform/backend/backend/notifications/mailerlite.py b/autogpt_platform/backend/backend/notifications/mailerlite.pyindex cbb916b89b..e59ab96e41 100644--- a/autogpt_platform/backend/backend/notifications/mailerlite.py+++ b/autogpt_platform/backend/backend/notifications/mailerlite.py@@ -15,6 +15,9 @@ transition:4. ENTER and LEAVE · the trial group: both owned here. It holds exactly thecustomers currently on a trial, so any MailerLite automation on it may sendmail but must never move people in or out.+5. ENTER · the checkout openers group: owned here. Everyone who opened Stripe+ checkout joins it, and nobody else enters MailerLite through us: a signup+ alone does not create a subscriber. GTM segments it for outreach.Subscriber fields (`SubscriberField`) are the backend's alone: every writecomes from here, and MailerLite automations only read them.@@ -32,6 +35,7 @@ import loggingfrom collections.abc import Mappingfrom backend.data.notifications import SubscriberField+from backend.notifications.audience_enrichment import merge_with_heldfrom backend.util.request import Requestsfrom backend.util.settings import Settings@@ -54,7 +58,8 @@ settings = Settings()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.+# The type MailerLite stores each of our custom fields as, which is also what+# `ensure_fields` creates. A date is written YYYY-MM-DD.FIELD_TYPES: dict[SubscriberField, str] = {SubscriberField.STATUS: "text",SubscriberField.SIGNUP: "date",@@ -62,13 +67,29 @@ FIELD_TYPES: dict[SubscriberField, str] = {SubscriberField.SUBSCRIPTION_STARTED: "date",SubscriberField.SUBSCRIPTION_CANCELED: "date",SubscriberField.SUBSCRIPTION_ENDED: "date",+ SubscriberField.CHECKOUT_OPENED: "date",+ SubscriberField.EMAIL_TYPE: "text",+ SubscriberField.SIGNIN_METHOD: "text",+ SubscriberField.COUNTRY_CODE: "text",+ SubscriberField.COUNTRY_SOURCE: "text",+ SubscriberField.EXCLUDE_DE_AT: "text",}+# MailerLite's own fields: written like ours, but never created.+BUILT_IN_FIELD_TYPES: dict[SubscriberField, str] = {SubscriberField.COUNTRY: "text"}+++def field_type(field: SubscriberField) -> str:+ return FIELD_TYPES.get(field) or BUILT_IN_FIELD_TYPES[field]++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+# Logged once: without a checkout group, checkout openers are dropped.+_checkout_off_logged = Falseclass MailerLiteNotConfigured(RuntimeError):@@ -144,20 +165,49 @@ async def update_fields(email: str, fields: Fields | None = None) -> None: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."""+ """A signup queued before signups stopped being sent here. It only fills+ in someone MailerLite already has: a signup alone must not create a+ subscriber, since only checkout openers belong in MailerLite. `signed` is+ where every subscriber starts, so it never replaces a status MailerLite+ already holds. 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):+ subscriber = await _find_subscriber(email)+ if subscriber is None:+ return+ if (subscriber.get("fields") or {}).get(SubscriberField.STATUS.value):fields = {k: v for k, v in fields.items() if k != SubscriberField.STATUS}await update_fields(email, fields)+async def record_checkout_opened(email: str, fields: Fields | None = None) -> None:+ """Someone opened Stripe checkout: into the checkout openers group, with+ the fields GTM segments on. Whatever MailerLite already holds that is+ better than ours stays (`audience_enrichment.merge_with_held`): the audience+ queue is worked one message at a time, so nothing of ours writes between+ the read and the write.++ Without a checkout group the change is dropped rather than dead-lettered:+ the group is how this is switched on, and the backfill brings in anyone+ opened while it was off."""+ group_id = settings.config.mailerlite_checkout_group_id+ if not group_id:+ global _checkout_off_logged+ if not _checkout_off_logged:+ logger.warning(+ "MAILERLITE_CHECKOUT_GROUP_ID is not set; dropping checkout openers"+ )+ _checkout_off_logged = True+ return+ _require_token()+ held = (await _find_subscriber(email) or {}).get("fields") or {}+ await _add_to_group(+ email, group_id, "checkout openers", merge_with_held(fields or {}, held)+ )++async def ensure_fields() -> list[SubscriberField]:"""Create any of our fields MailerLite does not have yet, and return theones created. Idempotent, and checked once per process."""diff --git a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.py b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.pyindex e03bd53331..23781b43e8 100644--- a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.py+++ b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.py@@ -1,9 +1,10 @@-"""Give every account the MailerLite status and dates the live code would have.+"""Give accounts the MailerLite status and dates the live code would have.Signups were never synced and the lifecycle handlers only react to newevents, so this works out each account's fields from scratch: the signup datefrom our database, the rest from Stripe, by the rules in `subscriber_fields.py`.-Accounts with no Stripe customer are `signed`.+Only accounts with a Stripe customer are given (see `cli/mailerlite_backfill`):+MailerLite holds checkout openers, not every signup.Resumable by construction: the fields MailerLite already holds are read firstand only the difference is written, so an interrupted or repeated run picks up@@ -21,11 +22,11 @@ from pydantic import BaseModel, EmailStr, TypeAdapter, ValidationErrorfrom backend.data.notifications import SubscriberField, SubscriptionStatusfrom backend.notifications.mailerlite import (API_BASE,- FIELD_TYPES,MailerLiteError,_client,_headers,_require_token,+ field_type,)from backend.notifications.mailerlite_backfill import (BATCH_SIZE,@@ -55,6 +56,9 @@ class Person(BaseModel):email: strcreated_at: datetimesubscriptions: list[Subscription] = []+ stripe_customer_id: str | None = None+ # The browser's IANA timezone, for the checkout opener's country.+ timezone: str | None = Noneclass FieldChange(BaseModel):@@ -121,7 +125,11 @@ def desired(person: Person) -> tuple[SubscriptionStatus, Fields]:}-def plan(people: list[Person], current: Current) -> FieldPlan:+def plan(people: list[Person], current: Current, *, create: bool = True) -> FieldPlan:+ """Each person's fields that differ from MailerLite's. With create=False,+ someone MailerLite does not hold is left out: only the checkout openers+ backfill brings new people in, since a Stripe customer alone does not+ mean they opened checkout (the billing portal creates one too)."""result = FieldPlan(statuses={s: 0 for s in SubscriptionStatus}, changes=[], invalid=0)@@ -132,6 +140,8 @@ def plan(people: list[Person], current: Current) -> FieldPlan:status, fields = desired(person)result.statuses[status] += 1held = current.get(person.email.strip().lower())+ if held is None and not create:+ continuediffer = {key: valuefor key, value in fields.items()@@ -237,7 +247,7 @@ def _normalise(field: SubscriberField, value: object) -> str | None:return Nonetext = str(value)# A date may come back with a time part.- return text[:10] if FIELD_TYPES[field] == "date" else text+ return text[:10] if field_type(field) == "date" else textdef _valid(email: str) -> bool:diff --git a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.py b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.pyindex b8d1fca513..e9604b8c7c 100644--- a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.py+++ b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.py@@ -277,3 +277,18 @@ async def test_read_current_stops_on_a_repeated_cursor(configured, monkeypatch):with pytest.raises(mailerlite.MailerLiteError, match="repeated"):await backfill.read_current()assert client.get.await_count == 2+++def test_without_create_only_existing_subscribers_are_planned():+ """A Stripe customer is not proof of a checkout: the billing portal makes+ one too. So `mailerlite-backfill` never creates anyone, and someone+ MailerLite does not hold gets no change at all."""+ portal_only = _person("[email protected]")+ existing = _person("[email protected]", _paid("active"))+ plan = backfill.plan(+ [portal_only, existing],+ {"[email protected]": {"subscription_status": "signed"}},+ create=False,+ )+ assert [c.person.email for c in plan.changes] == ["[email protected]"]+ assert not any(c.new for c in plan.changes)diff --git a/autogpt_platform/backend/backend/notifications/notifications.py b/autogpt_platform/backend/backend/notifications/notifications.pyindex 1710c8af90..b6467151b5 100644--- a/autogpt_platform/backend/backend/notifications/notifications.py+++ b/autogpt_platform/backend/backend/notifications/notifications.py@@ -392,6 +392,7 @@ class NotificationManager(AppService):AudienceAction.REMOVE_TRIAL: mailerlite.remove_from_trial,AudienceAction.UPDATE_FIELDS: mailerlite.update_fields,AudienceAction.SIGNUP: mailerlite.record_signup,+ AudienceAction.CHECKOUT_OPENED: mailerlite.record_checkout_opened,}[event.action]try:await handler(event.email, event.fields or None)diff --git a/autogpt_platform/backend/backend/notifications/subscriber_fields.py b/autogpt_platform/backend/backend/notifications/subscriber_fields.pyindex 1633d2626e..840683bbf5 100644--- a/autogpt_platform/backend/backend/notifications/subscriber_fields.py+++ b/autogpt_platform/backend/backend/notifications/subscriber_fields.py@@ -1,6 +1,7 @@"""Where each person stands with us, as MailerLite subscriber fields.-GTM segments on one status and five dates (see `SubscriberField`). Each+GTM segments on one status and five dates (see `SubscriberField`), plus the+checkout opener's segmentation, which `audience_enrichment` works out. Eachtransition below sets the status and the date it is named for, from Stripe'sown timestamp where the event carries one. The dates describe the current ormost recent subscription: a new subscription clears the previous one's@@ -140,10 +141,3 @@ async def queue_fields(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.pyindex 9cd5f47174..5d350ddb12 100644--- a/autogpt_platform/backend/backend/notifications/subscriber_fields_test.py+++ b/autogpt_platform/backend/backend/notifications/subscriber_fields_test.py@@ -1,7 +1,8 @@-"""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."""+"""Customers carry their status and dates into MailerLite, and neither a+MailerLite outage nor an address it refuses ever costs the checkout or the+billing email the update rides along with. A signup alone queues nothing."""+import asynciofrom datetime import UTC, datetimefrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMock, patch@@ -54,30 +55,27 @@ def test_a_new_subscription_clears_the_last_ones_cancellation_and_end():}+SIGNED = subscriber_fields.signed(datetime(2026, 9, 29, 10, 0, tzinfo=UTC))++@pytest.mark.asyncio-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)+async def test_a_field_change_is_queued_without_any_mailerlite_settings(fields_on):+ """It is queued from the API server, which never holds the MailerLite+ token: the notification service decides whether it goes anywhere."""+ await subscriber_fields.queue_fields(+ "user-1", EMAIL, SIGNED, AudienceAction.CHECKOUT_OPENED)event = fields_on.await_args.args[0]- assert event.action is AudienceAction.SIGNUP+ assert event.action is AudienceAction.CHECKOUT_OPENEDassert 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"--@pytest.mark.asyncioasync def test_a_reserved_domain_is_skipped_not_retried(fields_on):- await subscriber_fields.queue_signup("user-1", "[email protected]", datetime.now(UTC))+ await subscriber_fields.queue_fields("user-1", "[email protected]", SIGNED)fields_on.assert_not_awaited()@@ -93,7 +91,7 @@ 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))+ await subscriber_fields.queue_fields("user-1", EMAIL, SIGNED)failure.assert_awaited_once()@@ -118,45 +116,42 @@ def _prisma(existing):)-async def _get_or_create(existing, queue_signup):+async def _get_or_create(existing):prisma, created = _prisma(existing)+ scheduled: list[asyncio.Task] = []+ create_task = asyncio.create_task++ def recording(coro, **kwargs):+ task = create_task(coro, **kwargs)+ scheduled.append(task)+ return task+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, "schedule_posthog_lifecycle_sync", MagicMock()),patch.object(user_data.User, "from_db", MagicMock()),patch.object(user_data, "UserCreationResult", MagicMock()),- patch.object(user_data, "queue_signup", queue_signup),+ patch("asyncio.create_task", recording),):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+ # Whatever signup scheduled in the background runs before the patches+ # go, so a MailerLite queue it reached would be seen.+ await asyncio.gather(*scheduled, return_exceptions=True)return created@pytest.mark.asyncio-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))+async def test_a_new_account_is_not_sent_to_mailerlite(fields_on):+ """Only checkout openers belong in MailerLite: a signup alone queues+ nothing, so it can never create a subscriber."""+ await _get_or_create(None)+ fields_on.assert_not_awaited()+ for name in ("_sync_signup", "queue_signup", "queue_fields", "_signup_sync_tasks"):+ assert not hasattr(user_data, name)# ── the MailerLite client ──────────────────────────────────────────────────@@ -426,10 +421,11 @@ async def _consume(*events) -> None:async def _signup_event(fields_on):- await subscriber_fields.queue_signup(- "user-1", EMAIL, datetime(2026, 9, 29, 10, 0, tzinfo=UTC)+ """A signup queued before signups stopped being sent, still in the queue+ or replayed from the dead-letter queue."""+ return subscriber_fields.audience_event(+ AudienceAction.SIGNUP, EMAIL, "user-1", SIGNED)- return fields_on.await_args.args[0]def _checkout_event():@@ -458,17 +454,18 @@ async def test_a_signup_synced_after_the_checkout_does_not_undo_it(@pytest.mark.asyncio-async def test_a_signup_in_order_is_overtaken_by_the_checkout(+async def test_a_stale_signup_never_creates_a_subscriber(mailerlite_configured, fields_on):- mailerlite_configured.config.mailerlite_onboarding_group_id = "grp_tour"+ """A signup still queued from before signups stopped being sent must not+ bring someone who never opened checkout into MailerLite."""signup = await _signup_event(fields_on)ml = _FakeMailerLite()+ ml.post = AsyncMock(side_effect=ml.post)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"+ ml.post.assert_not_awaited()+ assert EMAIL not in ml.subscribers@pytest.mark.asynciodiff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.pyindex 4c7a184e9d..5247276b66 100644--- a/autogpt_platform/backend/backend/util/settings.py+++ b/autogpt_platform/backend/backend/util/settings.py@@ -617,6 +617,14 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):"Blank leaves trial customers out of MailerLite."),)+ mailerlite_checkout_group_id: str = Field(+ default="",+ description=(+ "MailerLite group holding everyone who opened Stripe checkout, "+ "which GTM segments for outreach. Blank leaves checkout openers "+ "out of MailerLite."+ ),+ )mailerlite_api_url: str = Field(default="https://connect.mailerlite.com/api",description=(