Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(backend): count only paying subscribers as subscribed, in PostHog and MailerLite (SECRT-2795) (#15530)

Krzysztof Czerwinskib67bd0192125
diff --git a/autogpt_platform/backend/backend/cli/mailerlite_backfill.py b/autogpt_platform/backend/backend/cli/mailerlite_backfill.py
index 61e56964d2..cae079e0c5 100644
--- a/autogpt_platform/backend/backend/cli/mailerlite_backfill.py
+++ b/autogpt_platform/backend/backend/cli/mailerlite_backfill.py
@@ -5,6 +5,8 @@ from typing import TYPE_CHECKING
import click
if TYPE_CHECKING:
+ from backend.data.stripe_subscription_facts import StripeSubscriptionFacts
+ from backend.data.subscription_trial import TrialState
from backend.notifications.checkout_backfill import OpenerPlan
from backend.notifications.mailerlite_backfill import (
Customer,
@@ -13,6 +15,8 @@ if TYPE_CHECKING:
)
from backend.notifications.mailerlite_field_backfill import FieldPlan, Person
+logger = logging.getLogger(__name__)
+
_ACCOUNT_PAGE = 5000
_SKIPPED_SINCE_PLAN = "opted out or seen in Iran or Russia since the plan (skipped)"
@@ -41,12 +45,13 @@ def mailerlite_backfill_command(
the customers on a trial that is not set to cancel.
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. Accounts that opted out
- of marketing, or that a signal places in Iran or Russia, are never written,
- removals included.
+ gets its subscription_status and dates, by the mapping PostHog's status
+ comes from: only a paying subscriber is subscribed. 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. Accounts that opted out of marketing, or that a signal places
+ in Iran or Russia, are never written, removals included.
Dry run by default: prints counts and one pseudonymised line per customer,
and writes nothing. Idempotent, so a partial or repeated --apply is safe,
@@ -196,21 +201,16 @@ async def _run(
await disconnect()
-async def _stripe_subscriptions() -> "dict[str, list[Subscription]]":
- """Every Stripe subscription, by customer ID."""
+async def _stripe_subscriptions() -> "dict[str, list[StripeSubscriptionFacts]]":
+ """Every Stripe subscription, by customer ID, listed as the lifecycle
+ sweep lists them, so the status follows the same rules."""
import stripe
- from backend.data.stripe_client import stripe_call, stripe_list_items
- from backend.notifications.mailerlite_backfill import Subscription
+ from backend.data.posthog_lifecycle_sync import all_subscriptions_by_customer
from backend.util.settings import Settings
stripe.api_key = Settings().secrets.stripe_api_key
- subscriptions: dict[str, list[Subscription]] = {}
- page = await stripe_call(stripe.Subscription.list_async, status="all", limit=100)
- async for sub in stripe_list_items(page):
- # Not expanded, so this is the customer ID.
- subscriptions.setdefault(str(sub.customer), []).append(_subscription(sub))
- return subscriptions
+ return await all_subscriptions_by_customer()
async def stripe_billing_countries(
@@ -277,69 +277,66 @@ def _keep_country(
countries[customer_id] = str(country)
-def _subscription(sub) -> "Subscription":
+def _subscription(
+ sub: "StripeSubscriptionFacts", trial: "TrialState | None"
+) -> "Subscription":
+ """The fields the group backfill reads (`mailerlite_backfill.classify`)."""
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"),
+ id=sub.id,
+ status=sub.status,
+ cancel_at_period_end=sub.cancel_at_period_end,
+ from_trial=sub.trial_enrollment_id is not None,
+ converted=trial is not None
+ and trial.converted_at is not None
+ and trial.subscription_id == sub.id,
)
-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
+async def _refresh(person: "Person") -> "Person":
+ """The person with their subscriptions as Stripe has them now and their
+ trial row as it is 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. An unreadable trial row raises, and the
+ person is left for the next run."""
+ import prisma.models
- 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)
- ]
+ from backend.data.posthog_lifecycle_sync import list_customer_subscriptions
+ from backend.data.subscription_trial import TrialState
- return refresh
+ subscriptions = (
+ await list_customer_subscriptions(person.stripe_customer_id)
+ if person.stripe_customer_id
+ else []
+ )
+ row = await prisma.models.SubscriptionTrial.prisma().find_unique(
+ where={"userId": person.user_id}
+ )
+ return person.model_copy(
+ update={
+ "subscriptions": subscriptions,
+ "trial": TrialState.from_db(row) if row else None,
+ }
+ )
async def _people(
- subscriptions: "dict[str, list[Subscription]]",
+ subscriptions: "dict[str, list[StripeSubscriptionFacts]]",
billing_countries: dict[str, str],
) -> "list[Person]":
"""Every account with a Stripe customer, paged, with its Stripe
- subscriptions and billing country. 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
+ subscriptions, trial and billing country. 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."""
+ handlers do, never Stripe's. An account whose trial row can't be read is
+ left out, rather than written as if it had no trial."""
import prisma.models
from backend.notifications.mailerlite_field_backfill import Person
- converted = {
- t.stripeSubscriptionId
- for t in await prisma.models.SubscriptionTrial.prisma().find_many(
- where={"convertedAt": {"not": None}}
- )
- if t.stripeSubscriptionId
- }
+ trials = await _trials()
people: list[Person] = []
cursor: str | None = None
while True:
@@ -350,16 +347,15 @@ async def _people(
**({"cursor": {"id": cursor}, "skip": 1} if cursor else {}),
)
for user in page:
- subs = subscriptions.get(user.stripeCustomerId or "", [])
+ if user.id in trials and trials[user.id] is None:
+ continue
people.append(
Person(
user_id=user.id,
email=user.email,
created_at=user.createdAt,
- subscriptions=[
- s.model_copy(update={"converted": s.id in converted})
- for s in subs
- ],
+ subscriptions=subscriptions.get(user.stripeCustomerId or "", []),
+ trial=trials.get(user.id),
stripe_customer_id=user.stripeCustomerId,
timezone=user.timezone,
marketing_opt_out_at=user.marketingOptOutAt,
@@ -372,13 +368,32 @@ async def _people(
cursor = page[-1].id
+async def _trials() -> "dict[str, TrialState | None]":
+ """Every trial row by user ID. None marks one that can't be read."""
+ import prisma.models
+
+ from backend.data.subscription_trial import TrialState
+
+ trials: dict[str, TrialState | None] = {}
+ for row in await prisma.models.SubscriptionTrial.prisma().find_many():
+ try:
+ trials[row.userId] = TrialState.from_db(row)
+ except Exception:
+ trials[row.userId] = None
+ logger.warning(
+ f"Unreadable trial row for user {row.userId}; account skipped",
+ exc_info=True,
+ )
+ return trials
+
+
def _customer(person: "Person") -> "Customer":
from backend.notifications.mailerlite_backfill import Customer
return Customer(
user_id=person.user_id,
email=person.email,
- subscriptions=person.subscriptions,
+ subscriptions=[_subscription(s, person.trial) for s in person.subscriptions],
marketing_opt_out_at=person.marketing_opt_out_at,
timezone=person.timezone,
billing_country=person.billing_country,
@@ -415,6 +430,7 @@ def _report_fields(plan: "FieldPlan", accounts: int) -> None:
click.echo(f" invalid email (skipped): {plan.invalid}")
click.echo(f" opted out of marketing (skipped): {plan.opted_out}")
click.echo(f" placed in Iran or Russia (skipped): {plan.excluded_country}")
+ click.echo(f" not in MailerLite (skipped): {plan.not_held}")
click.echo(
f"{len(plan.changes)} existing MailerLite subscribers to update "
"(nobody is created here; see mailerlite-checkout-backfill)"
@@ -495,7 +511,6 @@ async def _run_checkout(*, apply: bool, yes: bool) -> None:
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}
# Connected again for the writes: each opener is checked against the
# account as it is now, since the plan is older (`consent.KeptOut`).
await connect()
@@ -504,7 +519,7 @@ async def _run_checkout(*, apply: bool, yes: bool) -> None:
plan.changes,
group_id,
_checkout_progress,
- refresh=_refresher(converted),
+ refresh=_refresh,
kept_out=is_marketing_opted_out,
)
finally:
diff --git a/autogpt_platform/backend/backend/cli/mailerlite_backfill_test.py b/autogpt_platform/backend/backend/cli/mailerlite_backfill_test.py
index e433ed5451..8386c8be3c 100644
--- a/autogpt_platform/backend/backend/cli/mailerlite_backfill_test.py
+++ b/autogpt_platform/backend/backend/cli/mailerlite_backfill_test.py
@@ -13,13 +13,15 @@ import stripe
from click.testing import CliRunner
from backend.cli import mailerlite_backfill as cli
+from backend.data.stripe_subscription_facts import StripeSubscriptionFacts
from backend.notifications import checkout_backfill, mailerlite_backfill
from backend.notifications import mailerlite_field_backfill as field_backfill
-from backend.notifications.mailerlite_backfill import Audience, Subscription
+from backend.notifications.mailerlite_backfill import Audience
from backend.notifications.mailerlite_field_backfill import Person
CREATED = datetime(2026, 7, 14, tzinfo=UTC)
OPTED_OUT = datetime(2026, 10, 2, tzinfo=UTC)
+ACTIVE = StripeSubscriptionFacts(id="sub_1", status="active")
# The root conftest spins a full test server for every test via an autouse
@@ -89,7 +91,7 @@ async def test_each_account_carries_its_consent_and_countries_into_the_backfills
patch("prisma.models.SubscriptionTrial.prisma", return_value=trials),
):
out, kept = await cli._people(
- {"cus_out": [Subscription(status="active")], "cus_in": []},
+ {"cus_out": [ACTIVE], "cus_in": []},
{"cus_out": "RU"},
)
assert out.marketing_opt_out_at == OPTED_OUT
@@ -102,12 +104,43 @@ async def test_each_account_carries_its_consent_and_countries_into_the_backfills
assert cli._customer(kept).marketing_opt_out_at is None
+async def test_the_checkout_refresh_reads_the_trial_row_with_stripe():
+ """A trial that converts mid-run must not be read with the planned,
+ unconverted row: both come back fresh."""
+ person = Person(
+ user_id="u1",
+ created_at=CREATED,
+ stripe_customer_id="cus_1",
+ )
+ row = MagicMock()
+ trials = MagicMock(find_unique=AsyncMock(return_value=row))
+ fresh = object()
+ with (
+ patch(
+ "backend.data.posthog_lifecycle_sync.list_customer_subscriptions",
+ AsyncMock(return_value=[ACTIVE]),
+ ) as listed,
+ patch("prisma.models.SubscriptionTrial.prisma", return_value=trials),
+ patch(
+ "backend.data.subscription_trial.TrialState.from_db",
+ return_value=fresh,
+ ),
+ patch.object(Person, "model_copy", lambda self, update: update),
+ ):
+ refreshed = await cli._refresh(person)
+ listed.assert_awaited_once_with("cus_1")
+ trials.find_unique.assert_awaited_once_with(where={"userId": "u1"})
+ assert refreshed == {"subscriptions": [ACTIVE], "trial": fresh}
+
+
def test_every_report_counts_the_opted_out(capsys):
person = Person(
user_id="out",
created_at=CREATED,
- subscriptions=[Subscription(status="active")],
+ subscriptions=[ACTIVE],
stripe_customer_id="cus_out",
marketing_opt_out_at=OPTED_OUT,
)
@@ -140,7 +173,7 @@ def test_every_report_counts_anyone_placed_in_iran_or_russia(capsys):
user_id="ru",
created_at=CREATED,
- subscriptions=[Subscription(status="active")],
+ subscriptions=[ACTIVE],
stripe_customer_id="cus_ru",
timezone="Europe/Moscow",
)
diff --git a/autogpt_platform/backend/backend/copilot/rate_limit.py b/autogpt_platform/backend/backend/copilot/rate_limit.py
index 91597f2d0d..3cce9cb1dd 100644
--- a/autogpt_platform/backend/backend/copilot/rate_limit.py
+++ b/autogpt_platform/backend/backend/copilot/rate_limit.py
@@ -67,7 +67,6 @@ from redis.exceptions import RedisClusterException, RedisError
from backend.copilot.trial_cost_context import record_attributed_trial_cost
from backend.copilot.trial_reads import shared_trial_read
from backend.data.db_accessors import credit_db, user_db
-from backend.data.posthog_lifecycle_sync import schedule_posthog_lifecycle_sync
from backend.data.redis_client import AsyncRedisClient, get_redis_async
from backend.data.subscription_trial import TrialState
from backend.data.user import get_user_by_id
@@ -1139,7 +1138,6 @@ async def set_user_tier(user_id: str, tier: SubscriptionTier) -> None:
)
get_user_tier.cache_delete(user_id) # type: ignore[attr-defined]
get_user_by_id.cache_delete(user_id) # type: ignore[attr-defined]
- schedule_posthog_lifecycle_sync(user_id)
# Fire-and-forget drift check so admin bulk ops don't wait on Stripe.
asyncio.ensure_future(_drift_check_background(user_id, tier))
diff --git a/autogpt_platform/backend/backend/data/credit.py b/autogpt_platform/backend/backend/data/credit.py
index 42e3bf3f57..791172ac47 100644
--- a/autogpt_platform/backend/backend/data/credit.py
+++ b/autogpt_platform/backend/backend/data/credit.py
@@ -1574,20 +1574,15 @@ async def set_auto_top_up(user_id: str, config: AutoTopUpConfig):
get_user_by_id.cache_delete(user_id)
-async def set_subscription_tier(
- user_id: str,
- tier: SubscriptionTier,
- *,
- track_lifecycle: bool = True,
-) -> None:
- """Set the user's subscription tier."""
+async def set_subscription_tier(user_id: str, tier: SubscriptionTier) -> None:
+ """Set the user's subscription tier. No lifecycle sync: the subscription
+ status doesn't read the tier, and the Stripe change that comes with a
+ tier change syncs through its own webhook."""
data: UserUpdateInput = {
"subscriptionTier": tier,
}
await User.prisma().update(where={"id": user_id}, data=data)
invalidate_subscription_caches(user_id)
- if track_lifecycle:
- schedule_posthog_lifecycle_sync(user_id)
async def invalidate_billing_lookups(customer_id: str) -> None:
@@ -3012,9 +3007,7 @@ async def _sync_subscription_tier_from_stripe(stripe_subscription: dict) -> None
# A future improvement would be to write the new tier first, then
# cancel the old sub.
await _cleanup_stale_subscriptions(customer_id, new_sub_id)
- # The wrapper schedules the lifecycle sync (or, for the tier sweep, doesn't),
- # so the tier write mustn't schedule a second one.
- await set_subscription_tier(user.id, tier, track_lifecycle=False)
+ await set_subscription_tier(user.id, tier)
if is_tier_upgrade(current_tier, tier):
billing_cycle = (
metadata.get("billing_cycle") if isinstance(metadata, dict) else None
diff --git a/autogpt_platform/backend/backend/data/credit_subscription_test.py b/autogpt_platform/backend/backend/data/credit_subscription_test.py
index 4924792b77..733bbab980 100644
--- a/autogpt_platform/backend/backend/data/credit_subscription_test.py
+++ b/autogpt_platform/backend/backend/data/credit_subscription_test.py
@@ -142,9 +142,7 @@ async def test_sync_subscription_from_stripe_active():
) as mock_set,
):
await sync_subscription_from_stripe(stripe_sub)
- mock_set.assert_awaited_once_with(
- "user-1", SubscriptionTier.PRO, track_lifecycle=False
- )
+ mock_set.assert_awaited_once_with("user-1", SubscriptionTier.PRO)
@pytest.mark.asyncio
@@ -246,9 +244,7 @@ async def test_sync_subscription_from_stripe_yearly_pro_maps_to_pro():
) as mock_set,
):
await sync_subscription_from_stripe(stripe_sub)
- mock_set.assert_awaited_once_with(
- "user-1", SubscriptionTier.PRO, track_lifecycle=False
- )
+ mock_set.assert_awaited_once_with("user-1", SubscriptionTier.PRO)
@pytest.mark.asyncio
@@ -349,9 +345,7 @@ async def test_sync_subscription_from_stripe_cancelled():
) as mock_set,
):
await sync_subscription_from_stripe(stripe_sub)
- mock_set.assert_awaited_once_with(
- "user-1", SubscriptionTier.NO_TIER, track_lifecycle=False
- )
+ mock_set.assert_awaited_once_with("user-1", SubscriptionTier.NO_TIER)
@pytest.mark.asyncio
@@ -385,9 +379,7 @@ async def test_sync_subscription_from_stripe_past_due_downgrades_to_no_tier():
) as mock_set,
):
await sync_subscription_from_stripe(stripe_sub)
- mock_set.assert_awaited_once_with(
- "user-1", SubscriptionTier.NO_TIER, track_lifecycle=False
- )
+ mock_set.assert_awaited_once_with("user-1", SubscriptionTier.NO_TIER)
@pytest.mark.asyncio
@@ -409,8 +401,6 @@ async def test_sync_subscription_from_stripe_cancelled_applies_no_tier_storage_l
async def _set_tier(
_user_id: str,
tier: SubscriptionTier,
- *,
- track_lifecycle: bool = True,
) -> None:
mock_user.subscriptionTier = tier
@@ -546,9 +536,7 @@ async def test_sync_subscription_from_stripe_trialing():
) as mock_set,
):
await sync_subscription_from_stripe(stripe_sub)
- mock_set.assert_awaited_once_with(
- "user-1", SubscriptionTier.PRO, track_lifecycle=False
- )
+ mock_set.assert_awaited_once_with("user-1", SubscriptionTier.PRO)
@pytest.mark.asyncio
@@ -1213,9 +1201,7 @@ async def test_sync_subscription_from_stripe_business_tier():
) as mock_set,
):
await sync_subscription_from_stripe(stripe_sub)
- mock_set.assert_awaited_once_with(
- "user-1", SubscriptionTier.BUSINESS, track_lifecycle=False
- )
+ mock_set.assert_awaited_once_with("user-1", SubscriptionTier.BUSINESS)
@pytest.mark.asyncio
@@ -1267,9 +1253,7 @@ async def test_sync_subscription_from_stripe_basic_tier_via_ld_price():
) as mock_set,
):
await sync_subscription_from_stripe(stripe_sub)
- mock_set.assert_awaited_once_with(
- "user-1", SubscriptionTier.BASIC, track_lifecycle=False
- )
+ mock_set.assert_awaited_once_with("user-1", SubscriptionTier.BASIC)
@pytest.mark.asyncio
@@ -1324,9 +1308,7 @@ async def test_sync_subscription_from_stripe_cancels_stale_subs():
) as mock_set,
):
await sync_subscription_from_stripe(stripe_sub)
- mock_set.assert_awaited_once_with(
- "user-1", SubscriptionTier.BUSINESS, track_lifecycle=False
- )
+ mock_set.assert_awaited_once_with("user-1", SubscriptionTier.BUSINESS)
# Only the stale sub should be cancelled — never the new one.
mock_cancel.assert_called_once_with("sub_old")
@@ -1380,9 +1362,7 @@ async def test_sync_subscription_from_stripe_stale_cancel_errors_swallowed():
):
# Must not raise — tier update proceeds even if cleanup cancel fails.
await sync_subscription_from_stripe(stripe_sub)
- mock_set.assert_awaited_once_with(
- "user-1", SubscriptionTier.PRO, track_lifecycle=False
- )
+ mock_set.assert_awaited_once_with("user-1", SubscriptionTier.PRO)
@pytest.mark.asyncio
@@ -1721,9 +1701,7 @@ async def test_sync_subscription_from_stripe_metadata_user_id_matches():
) as mock_set,
):
await sync_subscription_from_stripe(stripe_sub)
- mock_set.assert_awaited_once_with(
- "user-1", SubscriptionTier.PRO, track_lifecycle=False
- )
+ mock_set.assert_awaited_once_with("user-1", SubscriptionTier.PRO)
@pytest.mark.asyncio
@@ -1796,9 +1774,7 @@ async def test_sync_subscription_from_stripe_no_metadata_user_id_skips_check():
):
await sync_subscription_from_stripe(stripe_sub)
# No metadata → cross-check skipped → tier updated normally
- mock_set.assert_awaited_once_with(
- "user-1", SubscriptionTier.PRO, track_lifecycle=False
- )
+ mock_set.assert_awaited_once_with("user-1", SubscriptionTier.PRO)
@pytest.mark.asyncio
@@ -3992,9 +3968,7 @@ async def test_sync_subscription_from_stripe_phase_transition_updates_tier():
) as mock_set,
):
await sync_subscription_from_stripe(stripe_sub)
- mock_set.assert_awaited_once_with(
- "user-1", SubscriptionTier.PRO, track_lifecycle=False
- )
+ mock_set.assert_awaited_once_with("user-1", SubscriptionTier.PRO)
@pytest.mark.asyncio
diff --git a/autogpt_platform/backend/backend/data/credit_trial_subscription_test.py b/autogpt_platform/backend/backend/data/credit_trial_subscription_test.py
index 40688c3c80..781905b3b3 100644
--- a/autogpt_platform/backend/backend/data/credit_trial_subscription_test.py
+++ b/autogpt_platform/backend/backend/data/credit_trial_subscription_test.py
@@ -89,9 +89,7 @@ async def test_new_plan_ends_cancel_pending_trial_without_invoice():
mock_cancel.assert_awaited_once_with("sub_trial", invoice_now=False, prorate=False)
reconcile.assert_awaited_once_with("user-1", "sub_trial")
- mock_set.assert_awaited_once_with(
- "user-1", SubscriptionTier.MAX, track_lifecycle=False
- )
+ mock_set.assert_awaited_once_with("user-1", SubscriptionTier.MAX)
assert events == ["cancel:sub_trial", "tier:MAX"]
diff --git a/autogpt_platform/backend/backend/data/db_accessors.py b/autogpt_platform/backend/backend/data/db_accessors.py
index 1cce74dd47..802636c51d 100644
--- a/autogpt_platform/backend/backend/data/db_accessors.py
+++ b/autogpt_platform/backend/backend/data/db_accessors.py
@@ -372,3 +372,17 @@ def bot_installs_db():
bot_installs_db = get_database_manager_async_client()
return bot_installs_db
+
+
+def posthog_lifecycle_db():
+ if db.is_connected():
+ from backend.data import posthog_lifecycle_sync as _posthog_lifecycle_db
+
+ posthog_lifecycle_db = _posthog_lifecycle_db
+ else:
+ from backend.util.clients import get_database_manager_async_client
+
+ # Best-effort and asked for after the work is queued: fail fast.
+ posthog_lifecycle_db = get_database_manager_async_client(should_retry=False)
+
+ return posthog_lifecycle_db
diff --git a/autogpt_platform/backend/backend/data/db_manager.py b/autogpt_platform/backend/backend/data/db_manager.py
index 1df52488b6..6f2a3a7e65 100644
--- a/autogpt_platform/backend/backend/data/db_manager.py
+++ b/autogpt_platform/backend/backend/data/db_manager.py
@@ -160,7 +160,10 @@ from backend.data.org_credit import get_org_credits as _get_org_credits_raw
from backend.data.org_credit import get_personal_org_owner
from backend.data.org_credit import spend_org_credits as _spend_org_credits_raw
from backend.data.platform_cost import log_platform_cost
-from backend.data.posthog_lifecycle_sync import start_posthog_lifecycle_sweep
+from backend.data.posthog_lifecycle_sync import (
+ request_posthog_lifecycle_sync,
+ start_posthog_lifecycle_sweep,
+)
from backend.data.push_subscription import (
cleanup_failed_subscriptions,
delete_push_subscription,
@@ -548,6 +551,7 @@ class DatabaseManager(AppService):
# row via db_accessors.credit_db() instead of crashing on direct Prisma.
reconcile_stripe_tier_for_user = _(reconcile_stripe_tier_for_user)
start_posthog_lifecycle_sweep = _(start_posthog_lifecycle_sweep)
+ request_posthog_lifecycle_sync = _(request_posthog_lifecycle_sync)
# ============ Platform Linking ============ #
# ============ Orgs ============ #
@@ -1054,6 +1058,7 @@ class DatabaseManagerAsyncClient(AppServiceClient):
reconcile_all_stripe_tiers = d.reconcile_all_stripe_tiers
reconcile_stripe_tier_for_user = d.reconcile_stripe_tier_for_user
start_posthog_lifecycle_sweep = d.start_posthog_lifecycle_sweep
+ request_posthog_lifecycle_sync = d.request_posthog_lifecycle_sync
# ============ Platform Linking ============ #
find_server_link_owner = d.find_server_link_owner
diff --git a/autogpt_platform/backend/backend/data/notifications.py b/autogpt_platform/backend/backend/data/notifications.py
index e268462e40..c5d3e982a2 100644
--- a/autogpt_platform/backend/backend/data/notifications.py
+++ b/autogpt_platform/backend/backend/data/notifications.py
@@ -438,6 +438,11 @@ class AudienceAction(Enum):
# given after checkout opened: they fill in someone MailerLite already has
# and never create anyone (see `mailerlite.record_onboarding_profile`).
ONBOARDING_PROFILE = "onboarding_profile"
+ # The subscription status and its dates, from the lifecycle snapshot
+ # PostHog gets too. Only the fields that differ are written, into someone
+ # MailerLite already has; nobody is created (see
+ # `mailerlite.record_lifecycle`).
+ LIFECYCLE_FIELDS = "lifecycle_fields"
class SubscriberField(str, Enum):
@@ -467,13 +472,21 @@ class SubscriberField(str, Enum):
class SubscriptionStatus(str, Enum):
- """Exactly one of these is a subscriber's `subscription_status`."""
+ """Exactly one of these is a subscriber's `subscription_status`: the same
+ values, by the same rules, as the PostHog person property
+ (`data/posthog_lifecycle.py`). Only a paying subscriber is `subscribed`.
+ GTM segments on these, so a value changes only together with MailerLite."""
SIGNED = "signed"
IN_TRIAL = "in_trial"
+ # The user canceled their trial.
TRIAL_CANCELED = "trial_canceled"
+ # The trial ended without a payment and without the user canceling it.
+ TRIAL_EXPIRED = "trial_expired"
SUBSCRIBED = "subscribed"
SUBSCRIPTION_CANCELED = "subscription_canceled"
+ # A payment failed and Stripe is retrying it; access is lost meanwhile.
+ PAYMENT_FAILED = "payment_failed"
SUBSCRIPTION_ENDED = "subscription_ended"
diff --git a/autogpt_platform/backend/backend/data/posthog_lifecycle.py b/autogpt_platform/backend/backend/data/posthog_lifecycle.py
index f9b2d2bbc9..f90e2d1778 100644
--- a/autogpt_platform/backend/backend/data/posthog_lifecycle.py
+++ b/autogpt_platform/backend/backend/data/posthog_lifecycle.py
@@ -1,47 +1,69 @@
-"""Subscription status and lifecycle dates for the PostHog person (SECRT-2778).
+"""Subscription status and lifecycle dates (SECRT-2778, SECRT-2795).
-This module is the mapping only: pure, no I/O. ``posthog_lifecycle_sync``
-reads the inputs and sends the result.
+The one mapping both sinks use: the PostHog person properties, and the
+MailerLite subscriber fields of the same names (``subscription_status`` and
+the ``*_date`` fields, see ``notifications/lifecycle_audience.py``). This
+module is the mapping only: pure, no I/O. ``posthog_lifecycle_sync`` reads the
+inputs and sends the result to both.
-The status is worked out from the current state every time (the user row,
-the trial row and all of the customer's Stripe subscriptions), never from the
-event that triggered a sync, so an out-of-order webhook can't leave it wrong.
+The status is worked out from the current state every time (the trial row and
+all of the customer's Stripe subscriptions), never from the event that
+triggered a sync, so an out-of-order webhook can't leave it wrong. The
+subscription tier is not read: only a payment makes a subscriber, so an
+admin-granted plan (ENTERPRISE or any other) with no paid Stripe subscription
+is ``signed``.
Person properties, set on distinct id = platform user id (dates are ISO-8601
UTC strings of the real lifecycle moment, never the sync time; a date that
doesn't apply is ``$unset``, never sent as null):
- ``subscription_status``: one of ``signed``, ``in_trial``,
- ``trial_canceled``, ``subscribed``, ``subscription_canceled`` (canceled,
- still active until the period ends), ``payment_failed`` (renewal payment
- failed; Stripe ``past_due`` or ``unpaid``, access lost),
- ``subscription_ended``. Rules in
+ ``trial_canceled`` (the user canceled their trial), ``trial_expired`` (the
+ trial ended without a payment and without the user canceling it),
+ ``subscribed``, ``subscription_canceled`` (canceled, still active until the
+ period ends), ``payment_failed`` (a payment failed; Stripe ``past_due`` or
+ ``unpaid``, access lost), ``subscription_ended``. Rules in
``compute_lifecycle_snapshot``.
- ``signup_at``: ``User.createdAt``.
- ``trial_started_at``: ``SubscriptionTrial.startedAt``, for a trial whose
checkout completed and wasn't rejected; for a Stripe-native trial (one
created outside our trial flow, e.g. in the dashboard), its ``trial_start``.
-- ``subscription_started_at``: start of the current or most recent paid
- subscription (Stripe ``start_date``); for a converted trial, the conversion
- (``SubscriptionTrial.convertedAt``).
-- ``subscription_canceled_at``: Stripe ``canceled_at``, while
- ``subscription_canceled`` or ``subscription_ended``.
-- ``subscription_ended_at``: Stripe ``ended_at``, while ``subscription_ended``.
+- ``subscription_started_at``: when the person first became a paying
+ subscriber: the earliest start of any paid subscription (Stripe
+ ``start_date``), kept across a resubscribe after a gap. For a converted
+ trial, the conversion (``SubscriptionTrial.convertedAt``), not the trial
+ start. Kept too for a former payer now on a free subscription, who is
+ ``signed``.
+- ``subscription_canceled_at``: Stripe ``canceled_at`` of the current or most
+ recent paid subscription, while ``subscription_canceled`` or
+ ``subscription_ended``.
+- ``subscription_ended_at``: its Stripe ``ended_at``, while
+ ``subscription_ended``.
+
+A paid subscription is one that charges something: a subscription whose
+discounts take the whole price (a 100% coupon, or an amount off at least the
+price, on the subscription or the customer) is treated like a trial that never
+converted, and never makes anyone ``subscribed``, ``subscription_canceled`` or
+``subscription_ended``. So is one whose only invoice so far charged nothing
+(a 100% coupon for the first period, which Stripe takes off the subscription
+once that invoice is made). A partial discount is still paid, and so is a
+subscription whose full discount has ended.
"""
from collections.abc import Iterable, Sequence
-from datetime import UTC, datetime
+from datetime import UTC, datetime, timedelta
from typing import Any, Literal
-from prisma.enums import SubscriptionTier
-from pydantic import BaseModel, Field
+from pydantic import BaseModel
+from backend.data.stripe_subscription_facts import StripeSubscriptionFacts, utc
from backend.data.subscription_trial import TrialState
LifecycleStatus = Literal[
"signed",
"in_trial",
"trial_canceled",
+ "trial_expired",
"subscribed",
"subscription_canceled",
"payment_failed",
@@ -50,48 +72,21 @@ LifecycleStatus = Literal[
# Stripe statuses in which a paid subscriber still has a subscription.
_LIVE_STATUSES = frozenset({"active"})
-# A renewal payment failed. The backend drops the user to NO_TIER, so they have
-# lost access, but they didn't choose to leave (SECRT-2778, question 3).
+# A payment failed. The backend drops the user to NO_TIER, so they have lost
+# access, but they didn't choose to leave (SECRT-2778, question 3).
_PAYMENT_FAILED_STATUSES = frozenset({"past_due", "unpaid"})
# A subscription in one of these was never paid for.
_NEVER_PAID_STATUSES = frozenset({"trialing", "incomplete", "incomplete_expired"})
-_EPOCH = datetime.min.replace(tzinfo=UTC)
_NEVER = datetime.max.replace(tzinfo=UTC)
-
-
-class StripeSubscriptionFacts(BaseModel):
- """The fields of a Stripe subscription the mapping reads.
-
- Validates straight from a Stripe object or dict; Stripe's unix
- timestamps become UTC datetimes.
- """
-
- id: str
- customer: str | None = None
- status: str
- cancel_at_period_end: bool = False
- start_date: datetime | None = None
- created: datetime | None = None
- canceled_at: datetime | None = None
- ended_at: datetime | None = None
- trial_start: datetime | None = None
- trial_end: datetime | None = None
- metadata: dict[str, str] = Field(default_factory=dict)
-
- @property
- def started_at(self) -> datetime | None:
- return self.start_date or self.created
-
- @property
- def trial_enrollment_id(self) -> str | None:
- return self.metadata.get("trial_enrollment_id") or None
+# A coupon applied at checkout can start a moment after the subscription;
+# one that started later than this left at least the first period charged.
+_FIRST_CHARGE_GRACE = timedelta(days=1)
class LifecycleUser(BaseModel):
id: str
created_at: datetime | None = None
stripe_customer_id: str | None = None
- subscription_tier: SubscriptionTier | None = None
class LifecycleSnapshot(BaseModel):
@@ -128,11 +123,6 @@ def compute_lifecycle_snapshot(
First match wins:
- 0. ENTERPRISE (admin-managed, never touched by Stripe) -> ``subscribed``.
- A live Stripe subscription alongside it still supplies the start date.
- A self-serve paid tier with no Stripe subscription is not treated as a
- grant: the column defaulted to PRO from 2026-03-26 to 2026-05-01, so
- most such rows never paid. They follow the rules below.
1. Live paid subscription set to cancel at period end
-> ``subscription_canceled``
2. Live paid subscription (``active``) -> ``subscribed``
@@ -141,18 +131,24 @@ def compute_lifecycle_snapshot(
between the trial ending and its first invoice being charged, when
Stripe already says ``active`` but the trial hasn't converted), else a
Stripe-native ``trialing`` subscription with no trial enrollment.
- 5. The most recent paid subscription is ``past_due`` or ``unpaid``
+ 5. A trial's first charge failed and Stripe is still retrying it
-> ``payment_failed``
- 6. A paid subscription that has ended -> ``subscription_ended``
- 7. A trial that ended without converting -> ``trial_canceled``
- 8. Otherwise -> ``signed``
-
- A trial's own Stripe subscription counts as paid only once the trial has
- converted, and its paid start is then the conversion, not the trial start.
- The subscription dates belong to the subscription statuses; they are
- unset in every other status.
+ 6. Has paid before and now has a live free subscription (a customer
+ given a 100% coupon) -> ``signed``, keeping the first paid start
+ 7. The most recent paid subscription is ``past_due`` or ``unpaid``
+ -> ``payment_failed``
+ 8. A paid subscription that has ended -> ``subscription_ended``
+ 9. A trial that ended without converting -> ``trial_canceled`` if the
+ user canceled it (set it to cancel, or asked for the cancel), else
+ ``trial_expired`` (it ran out, or its first charge never cleared)
+ 10. Otherwise -> ``signed``
+
+ Paid means charging something (see the module docstring). A trial's own
+ Stripe subscription counts as paid only once the trial has converted, and
+ its paid start is then the conversion, not the trial start. The
+ subscription dates belong to the subscription statuses; they are unset in
+ every other status, except the first paid start of rule 6.
"""
- tier = SubscriptionTier(user.subscription_tier or SubscriptionTier.NO_TIER)
started = _trial_started(trial)
all_subs = list(subscriptions)
common: dict[str, Any] = {
@@ -161,20 +157,16 @@ def compute_lifecycle_snapshot(
trial.started_at if trial and started else _native_trial_start(all_subs)
),
}
- paid = [sub for sub in all_subs if _is_paid(sub, trial)]
+ paid = [sub for sub in all_subs if _is_paid(sub, trial, now)]
+ charged = [sub for sub in all_subs if _charged_before_free(sub, trial)]
+ first_paid = _first_paid_start([*paid, *charged], trial, all_subs)
live = _current_live(paid)
- if tier == SubscriptionTier.ENTERPRISE:
- return LifecycleSnapshot(
- subscription_status="subscribed",
- subscription_started_at=_paid_start(live, trial) if live else None,
- **common,
- )
if live is not None:
canceling = live.cancel_at_period_end
return LifecycleSnapshot(
subscription_status="subscription_canceled" if canceling else "subscribed",
- subscription_started_at=_paid_start(live, trial),
+ subscription_started_at=first_paid,
subscription_canceled_at=live.canceled_at if canceling else None,
**common,
)
@@ -191,54 +183,91 @@ def compute_lifecycle_snapshot(
if native is not None:
status = "trial_canceled" if native.cancel_at_period_end else "in_trial"
return LifecycleSnapshot(subscription_status=status, **common)
- ended_native = any(_is_ended_native_trial(sub, trial) for sub in all_subs)
- return _after_access_ended(paid, trial, started or ended_native, common)
+ if first_paid is not None and _has_live_free(all_subs, now):
+ # Comped: not paying now, but not churned either.
+ return LifecycleSnapshot(
+ subscription_status="signed", subscription_started_at=first_paid, **common
+ )
+ ended = _after_access_ended(paid, first_paid, common)
+ if ended is not None:
+ return ended
+ trial_end = _ended_trial_status(trial, started, all_subs, now)
+ return LifecycleSnapshot(subscription_status=trial_end or "signed", **common)
-def _is_ended_native_trial(
- sub: StripeSubscriptionFacts, trial: TrialState | None
+def _has_live_free(
+ subscriptions: Sequence[StripeSubscriptionFacts], now: datetime
) -> bool:
- return (
- not sub.trial_enrollment_id
- and sub.trial_start is not None
- and sub.status != "trialing"
- and not _is_paid(sub, trial)
+ """A live subscription that charges nothing (see the module docstring)."""
+ return any(
+ sub.status in _LIVE_STATUSES and sub.free_at(now) for sub in subscriptions
)
def _after_access_ended(
paid: Sequence[StripeSubscriptionFacts],
- trial: TrialState | None,
- trial_started: bool,
+ first_paid: datetime | None,
common: dict[str, Any],
-) -> LifecycleSnapshot:
- """Rules 5-8: nothing live, no trial running."""
+) -> LifecycleSnapshot | None:
+ """Rules 5-6: nothing live, no trial running, and they have paid."""
ended = max(paid, key=_ended_order, default=None)
if ended is not None and ended.status in _PAYMENT_FAILED_STATUSES:
return LifecycleSnapshot(
subscription_status="payment_failed",
- subscription_started_at=_paid_start(ended, trial),
+ subscription_started_at=first_paid,
**common,
)
if ended is not None:
return LifecycleSnapshot(
subscription_status="subscription_ended",
- subscription_started_at=_paid_start(ended, trial),
+ subscription_started_at=first_paid,
subscription_canceled_at=ended.canceled_at,
subscription_ended_at=ended.ended_at,
**common,
)
- if trial and trial.converted_at is not None:
+ if first_paid is not None:
# The converted subscription is missing from Stripe's list, but the
# conversion alone proves a paid subscription existed and has ended.
return LifecycleSnapshot(
subscription_status="subscription_ended",
- subscription_started_at=trial.converted_at,
+ subscription_started_at=first_paid,
**common,
)
- if trial_started:
- return LifecycleSnapshot(subscription_status="trial_canceled", **common)
- return LifecycleSnapshot(subscription_status="signed", **common)
+ return None
+
+
+def _ended_trial_status(
+ trial: TrialState | None,
+ started: bool,
+ subscriptions: Sequence[StripeSubscriptionFacts],
+ now: datetime,
+) -> LifecycleStatus | None:
+ """Rule 7: a trial that ended unpaid, canceled by the user or not."""
+ if trial and started:
+ canceled = trial.cancel_at_period_end or any(
+ sub.id == trial.subscription_id and sub.cancel_requested
+ for sub in subscriptions
+ )
+ return "trial_canceled" if canceled else "trial_expired"
+ native = max(
+ (sub for sub in subscriptions if _is_ended_native_trial(sub, trial, now)),
+ key=lambda sub: utc(sub.trial_start),
+ default=None,
+ )
+ if native is None:
+ return None
+ return "trial_canceled" if native.cancel_requested else "trial_expired"
+
+
+def _is_ended_native_trial(
+ sub: StripeSubscriptionFacts, trial: TrialState | None, now: datetime
+) -> bool:
+ return (
+ not sub.trial_enrollment_id
+ and sub.trial_start is not None
+ and sub.status != "trialing"
+ and not _is_paid(sub, trial, now)
+ )
def _trial_started(trial: TrialState | None) -> bool:
@@ -258,7 +287,7 @@ def _in_trial(trial: TrialState, now: datetime) -> bool:
trial.status == "trialing"
and trial.converted_at is None
and trial.ends_at is not None
- and _utc(trial.ends_at) > now
+ and utc(trial.ends_at) > now
)
@@ -270,9 +299,7 @@ def _awaiting_conversion(
so the trial hasn't converted yet. A failed charge moves the subscription
to ``past_due`` and ends this."""
return trial.converted_at is None and any(
- sub.id == trial.subscription_id
- and sub.trial_enrollment_id == trial.id
- and sub.status in _LIVE_STATUSES
+ _is_trial_subscription(sub, trial) and sub.status in _LIVE_STATUSES
for sub in subscriptions
)
@@ -283,19 +310,43 @@ def _first_charge_failed(
"""The trial's first charge failed and Stripe is still retrying it: the
same state the backend reports as ``payment_failed``."""
return trial.converted_at is None and any(
- sub.id == trial.subscription_id
- and sub.trial_enrollment_id == trial.id
- and sub.status in _PAYMENT_FAILED_STATUSES
+ _is_trial_subscription(sub, trial) and sub.status in _PAYMENT_FAILED_STATUSES
for sub in subscriptions
)
-def _is_paid(sub: StripeSubscriptionFacts, trial: TrialState | None) -> bool:
+def _is_trial_subscription(sub: StripeSubscriptionFacts, trial: TrialState) -> bool:
+ return sub.id == trial.subscription_id and sub.trial_enrollment_id == trial.id
+
+
+def _is_paid(
+ sub: StripeSubscriptionFacts, trial: TrialState | None, now: datetime
+) -> bool:
+ if sub.free_at(utc(sub.ended_at) if sub.ended_at else now):
+ return False
+ return _charges(sub, trial)
+
+
+def _charged_before_free(
+ sub: StripeSubscriptionFacts, trial: TrialState | None
+) -> bool:
+ """Free now (or when it ended), but it charged first: the full discount
+ started after its first paid period began. Its paid start still counts
+ as the first paid start."""
+ start = _paid_start(sub, trial)
+ return (
+ start is not None
+ and _charges(sub, trial)
+ and not sub.free_at(utc(start) + _FIRST_CHARGE_GRACE)
+ )
+
+
+def _charges(sub: StripeSubscriptionFacts, trial: TrialState | None) -> bool:
+ """Everything that makes a subscription paid except its discounts."""
if sub.trial_enrollment_id:
return (
trial is not None
- and trial.id == sub.trial_enrollment_id
- and trial.subscription_id == sub.id
+ and _is_trial_subscription(sub, trial)
and trial.converted_at is not None
)
if sub.status in _NEVER_PAID_STATUSES:
@@ -310,10 +361,28 @@ def _ended_during_native_trial(sub: StripeSubscriptionFacts) -> bool:
sub.trial_end is not None
and sub.status not in _LIVE_STATUSES
and ended is not None
- and _utc(ended) <= _utc(sub.trial_end)
+ and utc(ended) <= utc(sub.trial_end)
)
+def _first_paid_start(
+ paid: Sequence[StripeSubscriptionFacts],
+ trial: TrialState | None,
+ subscriptions: Sequence[StripeSubscriptionFacts],
+) -> datetime | None:
+ """When they first became a paying subscriber. A converted trial whose
+ subscription is missing from Stripe's list still proves a payment at its
+ conversion; one that is listed but free does not."""
+ starts = [_paid_start(sub, trial) for sub in paid]
+ if (
+ trial is not None
+ and trial.converted_at is not None
+ and not any(sub.id == trial.subscription_id for sub in subscriptions)
+ ):
+ starts.append(trial.converted_at)
+ return min((s for s in starts if s), key=utc, default=None)
+
+
def _current_live(
paid: Sequence[StripeSubscriptionFacts],
) -> StripeSubscriptionFacts | None:
@@ -321,7 +390,7 @@ def _current_live(
# the subscription the user is keeping.
return max(
(sub for sub in paid if sub.status in _LIVE_STATUSES),
- key=lambda sub: (not sub.cancel_at_period_end, _utc(sub.started_at)),
+ key=lambda sub: (not sub.cancel_at_period_end, utc(sub.started_at)),
default=None,
)
@@ -336,9 +405,9 @@ def _native_trial(
for sub in subscriptions
if sub.status == "trialing"
and not sub.trial_enrollment_id
- and (sub.trial_end is None or _utc(sub.trial_end) > now)
+ and (sub.trial_end is None or utc(sub.trial_end) > now)
),
- key=lambda sub: _utc(sub.started_at),
+ key=lambda sub: utc(sub.started_at),
default=None,
)
@@ -352,8 +421,8 @@ def _ended_order(sub: StripeSubscriptionFacts) -> tuple[datetime, datetime]:
# kept using.
failed = sub.status in _PAYMENT_FAILED_STATUSES
return (
- _utc(sub.started_at),
- _NEVER if failed else _utc(sub.ended_at or sub.canceled_at),
+ utc(sub.started_at),
+ _NEVER if failed else utc(sub.ended_at or sub.canceled_at),
)
@@ -364,7 +433,7 @@ def _native_trial_start(
kept after it converts or ends, as our own trial's start is."""
native = max(
(s for s in subscriptions if not s.trial_enrollment_id and s.trial_start),
- key=lambda s: _utc(s.trial_start),
+ key=lambda s: utc(s.trial_start),
default=None,
)
return native.trial_start if native else None
@@ -378,11 +447,5 @@ def _paid_start(
return sub.started_at
-def _utc(value: datetime | None) -> datetime:
- if value is None:
- return _EPOCH
- return value.replace(tzinfo=UTC) if value.tzinfo is None else value
-
-
def _iso(value: datetime) -> str:
- return _utc(value).astimezone(UTC).isoformat(timespec="seconds")
+ return utc(value).astimezone(UTC).isoformat(timespec="seconds")
diff --git a/autogpt_platform/backend/backend/data/posthog_lifecycle_hooks_test.py b/autogpt_platform/backend/backend/data/posthog_lifecycle_hooks_test.py
index 25dc790f3c..e889417310 100644
--- a/autogpt_platform/backend/backend/data/posthog_lifecycle_hooks_test.py
+++ b/autogpt_platform/backend/backend/data/posthog_lifecycle_hooks_test.py
@@ -1,5 +1,6 @@
"""Every write that can change a user's lifecycle snapshot schedules a sync,
-and a PostHog or Stripe failure in that sync never reaches the caller."""
+a tier write alone doesn't, and a PostHog or Stripe failure in that sync
+never reaches the caller."""
import asyncio
from unittest.mock import AsyncMock, MagicMock, patch
@@ -36,27 +37,6 @@ async def test_subscription_sync_schedules_by_customer():
schedule.assert_called_once_with(stripe_customer_id="cus_1")
-async def test_a_tier_change_honours_track_lifecycle_false():
- """The tier sweep's opt-out must reach set_subscription_tier too, or a
- tier change inside the sweep schedules a sync anyway."""
- update = AsyncMock()
- with (
- patch(
- "backend.data.credit.User.prisma",
- return_value=MagicMock(update=update),
- ),
- patch("backend.data.credit.invalidate_subscription_caches"),
- patch("backend.data.credit.schedule_posthog_lifecycle_sync") as schedule,
- ):
- await set_subscription_tier(
- "user-1", SubscriptionTier.PRO, track_lifecycle=False
- )
- schedule.assert_not_called()
- await set_subscription_tier("user-1", SubscriptionTier.PRO)
- schedule.assert_called_once_with("user-1")
- assert update.await_count == 2
-
-
async def test_tier_sweep_does_not_schedule_lifecycle_syncs():
"""The 6-hourly tier sweep re-syncs every trial; scheduling from there
would fan out one Stripe call per trial and could rate-limit the sweep's
@@ -173,7 +153,9 @@ async def test_webhook_survives_posthog_and_stripe_failures():
client.capture.assert_not_called()
-async def test_tier_change_schedules_the_user():
+async def test_a_tier_change_alone_schedules_no_sync():
+ """SECRT-2795: the status doesn't read the tier, so a tier write can't
+ change it; the Stripe change that comes with one syncs via its webhook."""
with (
patch(
"backend.data.credit.User.prisma",
@@ -183,10 +165,10 @@ async def test_tier_change_schedules_the_user():
patch("backend.data.credit.schedule_posthog_lifecycle_sync") as schedule,
):
await set_subscription_tier("user-1", SubscriptionTier.PRO)
- schedule.assert_called_once_with("user-1")
+ schedule.assert_not_called()
-async def test_admin_tier_override_schedules_the_user():
+async def test_an_admin_tier_override_schedules_no_sync():
with (
patch(
"backend.copilot.rate_limit.PrismaUser.prisma",
@@ -196,10 +178,10 @@ async def test_admin_tier_override_schedules_the_user():
patch("backend.copilot.rate_limit.get_user_tier", new=MagicMock()),
patch("backend.copilot.rate_limit.get_user_by_id", new=MagicMock()),
patch("backend.data.credit.get_pending_subscription_change", new=MagicMock()),
- patch("backend.copilot.rate_limit.schedule_posthog_lifecycle_sync") as schedule,
+ patch.object(lifecycle, "schedule_posthog_lifecycle_sync") as schedule,
):
await set_user_tier("user-1", SubscriptionTier.ENTERPRISE)
- schedule.assert_called_once_with("user-1")
+ schedule.assert_not_called()
@pytest.mark.parametrize("existing", [False, True])
diff --git a/autogpt_platform/backend/backend/data/posthog_lifecycle_sync.py b/autogpt_platform/backend/backend/data/posthog_lifecycle_sync.py
index 874763b7e3..203ceab38b 100644
--- a/autogpt_platform/backend/backend/data/posthog_lifecycle_sync.py
+++ b/autogpt_platform/backend/backend/data/posthog_lifecycle_sync.py
@@ -1,17 +1,23 @@
-"""Keep subscription status and lifecycle dates on the PostHog person.
+"""Keep subscription status and lifecycle dates on the PostHog person, and
+the same status and dates on the MailerLite subscriber.
These are person properties, not events: each sync sends one ``$set`` on the
platform user id, the same id the browser passes to ``identify``, so they land
-on the canonical person. The mapping itself is ``posthog_lifecycle``.
+on the canonical person. The mapping itself is ``posthog_lifecycle``. The same
+snapshot is queued for MailerLite (``notifications/lifecycle_audience.py``),
+so both agree on who is a paying subscriber.
Three ways in, all best-effort (a failure is logged, never raised):
- ``schedule_posthog_lifecycle_sync`` after a write that can change the
- snapshot (signup, tier changes, ``sync_subscription_from_stripe``). It runs
- in the background, so a webhook or a signup never waits on Stripe or
- PostHog.
+ snapshot (signup, ``sync_subscription_from_stripe``), and after a group
+ join that can create the MailerLite subscriber
+ (``lifecycle_audience.resync_after_group_change``). It runs
+ in the background, so a webhook or a signup never waits on Stripe, PostHog
+ or MailerLite. It sends to PostHog and queues MailerLite's fields.
- ``sync_all_posthog_lifecycles``, daily from the scheduler: the safety net
- for a missed hook and for a trial that runs out on the clock.
+ for a missed hook and for a trial that runs out on the clock. PostHog only:
+ MailerLite's safety net is its field backfill (``cli mailerlite-backfill``).
- ``scripts/backfill_posthog_lifecycle.py``, the same sweep run by hand.
"""
@@ -31,17 +37,30 @@ from backend.data.db import query_raw_with_schema
from backend.data.posthog_lifecycle import (
LifecycleSnapshot,
LifecycleUser,
- StripeSubscriptionFacts,
compute_lifecycle_snapshot,
)
-from backend.data.stripe_client import stripe_call, stripe_list_items
+from backend.data.stripe_client import (
+ stripe_call,
+ stripe_call_timeout,
+ stripe_list_items,
+)
+from backend.data.stripe_subscription_facts import LIST_EXPAND, StripeSubscriptionFacts
from backend.data.subscription_trial import TrialState
+from backend.notifications.lifecycle_audience import (
+ LifecycleContact,
+ mailerlite_lifecycle_enabled,
+ queue_lifecycle_fields,
+)
from backend.util.posthog_client import get_posthog_client
from backend.util.posthog_events import PostHogEvent
logger = logging.getLogger(__name__)
_SYNC_TIMEOUT_SECONDS = 60
+# Per page of the full listing: 100 subscriptions, each with its customer and
+# latest invoice expanded, can take longer than a plain call's 30s, and one
+# failed page aborts the whole sweep.
+_LIST_ALL_TIMEOUT_SECONDS = 90
_MAX_CONCURRENT_SYNCS = 4
@@ -61,10 +80,12 @@ def schedule_posthog_lifecycle_sync(
- two syncs for one user never run at the same time in this process, so
an older read can't be sent after a newer one here. Another pod, or the
daily sweep (which reads Stripe once at its start), can still send an
- older read; the next sync or sweep corrects it.
+ older read. The next sync or sweep corrects PostHog. MailerLite has no
+ sweep: its fields stay stale until the user's next sync, or until
+ ``cli mailerlite-backfill --fields-only --apply --yes`` catches it up.
"""
try:
- if get_posthog_client() is None:
+ if not _sync_enabled():
return
if user_id:
_schedule_user(user_id)
@@ -74,6 +95,13 @@ def schedule_posthog_lifecycle_sync(
logger.warning("Failed to schedule a lifecycle sync", exc_info=True)
+async def request_posthog_lifecycle_sync(user_id: str) -> None:
+ """``schedule_posthog_lifecycle_sync`` for a process without Prisma, such
+ as the notification service, through the database manager
+ (``db_accessors.posthog_lifecycle_db``)."""
+ schedule_posthog_lifecycle_sync(user_id)
+
+
# Per user id: "queued", "running", or "rerun" (running, then run once more).
_syncs: dict[str, str] = {}
_background_tasks: set[asyncio.Task] = set()
@@ -162,27 +190,51 @@ def _sync_slot() -> asyncio.Semaphore:
return slot
+def _sync_enabled() -> bool:
+ """A sync has somewhere to go: PostHog, or MailerLite's checkout
+ openers, whose settings this process may not hold."""
+ return get_posthog_client() is not None or mailerlite_lifecycle_enabled()
+
+
async def sync_posthog_lifecycle(
user_id: str,
*,
stripe_subscriptions: Sequence[StripeSubscriptionFacts] | None = None,
) -> LifecycleSnapshot | None:
- """Send the user's current lifecycle snapshot. Never raises.
+ """Send the user's current lifecycle snapshot to PostHog and queue it for
+ MailerLite. Never raises.
``stripe_subscriptions`` is the customer's complete list, for a caller
that already has it. Otherwise it is read from Stripe, and only for a
user with a Stripe customer. A webhook payload is deliberately not used
in its place: it is one subscription as of one event, which is exactly
the "last event received" the status must not depend on.
+
+ MailerLite's change is queued whatever this process's MailerLite settings:
+ only the notification service holds them, and drops it when MailerLite
+ is off.
"""
try:
- if not user_id or get_posthog_client() is None:
+ if not user_id or not _sync_enabled():
+ return None
+ row = await User.prisma().find_unique(
+ where={"id": user_id}, include={"subscriptionTrial": True}
+ )
+ if row is None:
return None
- snapshot = await load_lifecycle_snapshot(
- user_id, stripe_subscriptions=stripe_subscriptions
+ snapshot = await _snapshot_for(row, stripe_subscriptions)
+ send_lifecycle_snapshot(user_id, snapshot)
+ await queue_lifecycle_fields(
+ LifecycleContact(
+ id=row.id,
+ email=row.email,
+ timezone=row.timezone,
+ marketing_opt_out_at=row.marketingOptOutAt,
+ excluded_country=row.marketingExcludedCountry,
+ stripe_customer_id=row.stripeCustomerId,
+ ),
+ snapshot,
)
- if snapshot is not None:
- send_lifecycle_snapshot(user_id, snapshot)
return snapshot
except Exception:
logger.warning(
@@ -191,18 +243,10 @@ async def sync_posthog_lifecycle(
return None
-async def load_lifecycle_snapshot(
- user_id: str,
- *,
- stripe_subscriptions: Sequence[StripeSubscriptionFacts] | None = None,
- now: datetime | None = None,
-) -> LifecycleSnapshot | None:
- """Read the user's current state and map it. None if the user is gone."""
- row = await User.prisma().find_unique(
- where={"id": user_id}, include={"subscriptionTrial": True}
- )
- if row is None:
- return None
+async def _snapshot_for(
+ row: User, stripe_subscriptions: Sequence[StripeSubscriptionFacts] | None
+) -> LifecycleSnapshot:
+ """Map the user's current state."""
if stripe_subscriptions is None:
stripe_subscriptions = (
await list_customer_subscriptions(row.stripeCustomerId)
@@ -215,11 +259,10 @@ async def load_lifecycle_snapshot(
id=row.id,
created_at=row.createdAt,
stripe_customer_id=row.stripeCustomerId,
- subscription_tier=row.subscriptionTier,
),
trial=TrialState.from_db(trial_row) if trial_row else None,
subscriptions=stripe_subscriptions,
- now=now or datetime.now(UTC),
+ now=datetime.now(UTC),
)
@@ -227,7 +270,11 @@ async def list_customer_subscriptions(
customer_id: str,
) -> list[StripeSubscriptionFacts]:
page = await stripe_call(
- stripe.Subscription.list_async, customer=customer_id, status="all", limit=100
+ stripe.Subscription.list_async,
+ customer=customer_id,
+ status="all",
+ limit=100,
+ expand=LIST_EXPAND,
)
return [
StripeSubscriptionFacts.model_validate(sub)
@@ -321,7 +368,7 @@ async def sync_all_posthog_lifecycles(
summary.aborted = "PostHog is not configured"
return summary
try:
- by_customer = await _all_subscriptions_by_customer()
+ by_customer = await all_subscriptions_by_customer()
except Exception:
logger.exception("Lifecycle sync: listing Stripe subscriptions failed")
summary.aborted = "Stripe listing failed"
@@ -372,10 +419,19 @@ async def _sync_batch(
summary.errors += 1
-async def _all_subscriptions_by_customer() -> dict[str, list[StripeSubscriptionFacts]]:
- page = await stripe_call(stripe.Subscription.list_async, status="all", limit=100)
+async def all_subscriptions_by_customer() -> dict[str, list[StripeSubscriptionFacts]]:
+ """Every Stripe subscription, by customer ID, with the discounts the
+ mapping reads. A failed page raises: a partial list would mark people
+ whose subscriptions sit on the missing pages as ``signed``."""
+ page = await stripe_call_timeout(
+ _LIST_ALL_TIMEOUT_SECONDS,
+ stripe.Subscription.list_async,
+ status="all",
+ limit=100,
+ expand=LIST_EXPAND,
+ )
by_customer: dict[str, list[StripeSubscriptionFacts]] = {}
- async for sub in stripe_list_items(page):
+ async for sub in stripe_list_items(page, _LIST_ALL_TIMEOUT_SECONDS):
facts = StripeSubscriptionFacts.model_validate(sub)
if facts.customer:
by_customer.setdefault(facts.customer, []).append(facts)
@@ -384,8 +440,7 @@ async def _all_subscriptions_by_customer() -> dict[str, list[StripeSubscriptionF
_USER_BATCH = """
SELECT u."id", u."createdAt" AS created_at,
- u."stripeCustomerId" AS stripe_customer_id,
- u."subscriptionTier"::text AS subscription_tier
+ u."stripeCustomerId" AS stripe_customer_id
FROM {schema_prefix}"User" u
WHERE u."id" > $1 AND ($3::boolean OR u."stripeCustomerId" IS NOT NULL
OR EXISTS (SELECT 1 FROM {schema_prefix}"SubscriptionTrial" t
diff --git a/autogpt_platform/backend/backend/data/posthog_lifecycle_sync_integration_test.py b/autogpt_platform/backend/backend/data/posthog_lifecycle_sync_integration_test.py
index df48f02ddf..9fb4f8f573 100644
--- a/autogpt_platform/backend/backend/data/posthog_lifecycle_sync_integration_test.py
+++ b/autogpt_platform/backend/backend/data/posthog_lifecycle_sync_integration_test.py
@@ -4,7 +4,6 @@ paging over more than one batch, and the billing-history filter."""
import uuid
import pytest
-from prisma.enums import SubscriptionTier
from prisma.models import SubscriptionTrial, User
from backend.data.posthog_lifecycle import LifecycleUser
@@ -28,11 +27,6 @@ async def lifecycle_users():
"id": user_id,
"email": f"{user_id}@example.com",
"stripeCustomerId": customers.get(user_id),
- "subscriptionTier": (
- SubscriptionTier.PRO
- if user_id == ids[0]
- else SubscriptionTier.NO_TIER
- ),
}
)
await SubscriptionTrial.prisma().create(
@@ -73,7 +67,6 @@ async def test_batches_page_over_users_with_billing_history(lifecycle_users):
]
first = batches[0][0]
assert first.stripe_customer_id == f"cus_{prefix}1"
- assert first.subscription_tier == SubscriptionTier.PRO
assert first.created_at is not None
assert batches[0][1].stripe_customer_id is None
@@ -89,4 +82,3 @@ async def test_all_users_includes_users_without_billing_history(lifecycle_users)
ids[2:4],
ids[4:5],
]
- assert batches[0][1].subscription_tier == SubscriptionTier.NO_TIER
diff --git a/autogpt_platform/backend/backend/data/posthog_lifecycle_sync_test.py b/autogpt_platform/backend/backend/data/posthog_lifecycle_sync_test.py
index 7d97234392..7a806d98c2 100644
--- a/autogpt_platform/backend/backend/data/posthog_lifecycle_sync_test.py
+++ b/autogpt_platform/backend/backend/data/posthog_lifecycle_sync_test.py
@@ -8,6 +8,8 @@ from prisma.enums import SubscriptionTier
from backend.data import posthog_lifecycle_sync as lifecycle
from backend.data.posthog_lifecycle import LifecycleSnapshot, LifecycleUser
+from backend.data.stripe_subscription_facts import LIST_EXPAND
+from backend.notifications.lifecycle_audience import LifecycleContact
MODULE = "backend.data.posthog_lifecycle_sync"
SIGNUP = datetime(2026, 1, 1, tzinfo=UTC)
@@ -36,6 +38,10 @@ def stripe_sub(sub_id: str, customer: str, status: str = "active", **extra) -> d
def db_user(customer: str | None = "cus_1", tier=SubscriptionTier.PRO) -> MagicMock:
return MagicMock(
id="user-1",
+ timezone="Europe/London",
+ marketingOptOutAt=None,
+ marketingExcludedCountry=None,
createdAt=SIGNUP,
stripeCustomerId=customer,
subscriptionTier=tier,
@@ -50,6 +56,17 @@ def posthog():
yield client
[email protected](autouse=True)
+def mailerlite_sink():
+ """MailerLite's half of each sync: off unless a test turns it on."""
+ queued = AsyncMock()
+ with (
+ patch(f"{MODULE}.queue_lifecycle_fields", queued),
+ patch(f"{MODULE}.mailerlite_lifecycle_enabled", return_value=False),
+ ):
+ yield queued
+
+
@pytest.fixture(autouse=True)
def clean_background_state():
lifecycle._syncs.clear()
@@ -111,7 +128,9 @@ async def test_sync_reads_the_customers_current_subscriptions(posthog):
assert snapshot is not None
assert snapshot.subscription_status == "subscribed"
- list_async.assert_awaited_once_with(customer="cus_1", status="all", limit=100)
+ list_async.assert_awaited_once_with(
+ customer="cus_1", status="all", limit=100, expand=LIST_EXPAND
+ )
sent = posthog.capture.call_args.kwargs["properties"]["$set"]
assert sent["subscription_status"] == "subscribed"
assert sent["signup_at"] == "2026-01-01T00:00:00+00:00"
@@ -147,7 +166,7 @@ async def test_sync_uses_a_supplied_subscription_list(posthog):
list_async.assert_not_awaited()
-async def test_sync_is_a_no_op_when_analytics_is_off():
+async def test_sync_is_a_no_op_when_posthog_and_mailerlite_are_off():
prisma = MagicMock()
with (
patch(f"{MODULE}.get_posthog_client", return_value=None),
@@ -157,6 +176,79 @@ async def test_sync_is_a_no_op_when_analytics_is_off():
prisma.assert_not_called()
+async def test_sync_queues_the_same_snapshot_for_mailerlite(posthog, mailerlite_sink):
+ find_unique = AsyncMock(return_value=db_user())
+ list_async = AsyncMock(return_value=stripe_page(stripe_sub("sub_1", "cus_1")))
+ with (
+ patch(f"{MODULE}.User.prisma", return_value=MagicMock(find_unique=find_unique)),
+ patch.object(stripe.Subscription, "list_async", list_async),
+ ):
+ snapshot = await lifecycle.sync_posthog_lifecycle("user-1")
+
+ mailerlite_sink.assert_awaited_once_with(
+ LifecycleContact(
+ id="user-1",
+ timezone="Europe/London",
+ stripe_customer_id="cus_1",
+ ),
+ snapshot,
+ )
+ sent = posthog.capture.call_args.kwargs["properties"]["$set"]
+ assert sent["subscription_status"] == snapshot.subscription_status
+
+
+async def test_sync_runs_for_mailerlite_alone(mailerlite_sink):
+ find_unique = AsyncMock(return_value=db_user(customer=None))
+ with (
+ patch(f"{MODULE}.get_posthog_client", return_value=None),
+ patch(f"{MODULE}.mailerlite_lifecycle_enabled", return_value=True),
+ patch(f"{MODULE}.User.prisma", return_value=MagicMock(find_unique=find_unique)),
+ ):
+ snapshot = await lifecycle.sync_posthog_lifecycle("user-1")
+ assert snapshot is not None
+ mailerlite_sink.assert_awaited_once()
+
+
[email protected]("paying", [False, True])
+async def test_an_enterprise_user_is_subscribed_only_while_paying(
+ posthog, mailerlite_sink, paying
+):
+ """SECRT-2795: no special case for the tier. Both sinks get the status."""
+ find_unique = AsyncMock(return_value=db_user(tier=SubscriptionTier.ENTERPRISE))
+ subs = [stripe_sub("sub_1", "cus_1")] if paying else []
+ with (
+ patch(f"{MODULE}.User.prisma", return_value=MagicMock(find_unique=find_unique)),
+ patch.object(
+ stripe.Subscription,
+ "list_async",
+ AsyncMock(return_value=stripe_page(*subs)),
+ ),
+ ):
+ snapshot = await lifecycle.sync_posthog_lifecycle("user-1")
+
+ expected = "subscribed" if paying else "signed"
+ sent = posthog.capture.call_args.kwargs["properties"]["$set"]
+ assert sent["subscription_status"] == expected
+ assert mailerlite_sink.await_args.args[1].subscription_status == expected
+ assert snapshot is not None and snapshot.subscription_status == expected
+
+
+async def test_a_sync_requested_over_rpc_is_scheduled_like_a_local_one():
+ with patch(f"{MODULE}.schedule_posthog_lifecycle_sync") as schedule:
+ await lifecycle.request_posthog_lifecycle_sync("user-1")
+ schedule.assert_called_once_with("user-1")
+
+
+async def test_the_daily_sweep_leaves_mailerlite_to_its_backfill(
+ sweep_sources, posthog, mailerlite_sink
+):
+ with patch(f"{MODULE}.mailerlite_lifecycle_enabled", return_value=True):
+ summary = await lifecycle.sync_all_posthog_lifecycles(now=NOW)
+ assert summary.sent == 3
+ mailerlite_sink.assert_not_awaited()
+
+
async def test_sync_swallows_stripe_errors_and_sends_nothing(posthog):
find_unique = AsyncMock(return_value=db_user())
with (
@@ -240,7 +332,7 @@ async def test_scheduled_sync_failure_stays_in_the_background(posthog):
await _drain()
-async def test_schedule_is_a_no_op_when_analytics_is_off():
+async def test_schedule_is_a_no_op_when_posthog_and_mailerlite_are_off():
with (
patch(f"{MODULE}.get_posthog_client", return_value=None),
patch(f"{MODULE}.sync_posthog_lifecycle", new_callable=AsyncMock) as sync,
@@ -263,13 +355,8 @@ def test_schedule_swallows_client_errors():
# ---- sync_all_posthog_lifecycles --------------------------------------------
-def batch_user(user_id: str, customer: str | None, tier: str = "NO_TIER"):
- return LifecycleUser(
- id=user_id,
- created_at=SIGNUP,
- stripe_customer_id=customer,
- subscription_tier=SubscriptionTier(tier),
- )
+def batch_user(user_id: str, customer: str | None):
+ return LifecycleUser(id=user_id, created_at=SIGNUP, stripe_customer_id=customer)
@pytest.fixture
@@ -277,8 +364,8 @@ def sweep_sources():
"""Two DB batches over three users, and every subscription in Stripe."""
batches = [
[
- batch_user("u-sub", "cus_sub", "PRO"),
- batch_user("u-canceling", "cus_canceling", "PRO"),
+ batch_user("u-sub", "cus_sub"),
+ batch_user("u-canceling", "cus_canceling"),
],
[batch_user("u-ended", "cus_ended")],
[],
@@ -311,7 +398,7 @@ async def test_sweep_dry_run_counts_and_sends_nothing(sweep_sources):
summary = await lifecycle.sync_all_posthog_lifecycles(dry_run=True, now=NOW)
get_client.assert_not_called()
- list_async.assert_awaited_once_with(status="all", limit=100)
+ list_async.assert_awaited_once_with(status="all", limit=100, expand=LIST_EXPAND)
assert summary.aborted is None
assert summary.stripe_subscriptions == 4
assert summary.users == 3
diff --git a/autogpt_platform/backend/backend/data/posthog_lifecycle_test.py b/autogpt_platform/backend/backend/data/posthog_lifecycle_test.py
index 8d524232e1..2f159977d6 100644
--- a/autogpt_platform/backend/backend/data/posthog_lifecycle_test.py
+++ b/autogpt_platform/backend/backend/data/posthog_lifecycle_test.py
@@ -2,14 +2,14 @@ from datetime import UTC, datetime, timedelta
from itertools import permutations
import pytest
-from prisma.enums import SubscriptionTier
+from backend.data.credit import PAYMENT_FAILURE_CANCELLATION_COMMENT
from backend.data.posthog_lifecycle import (
LifecycleSnapshot,
LifecycleUser,
- StripeSubscriptionFacts,
compute_lifecycle_snapshot,
)
+from backend.data.stripe_subscription_facts import StripeSubscriptionFacts
from backend.data.subscription_trial import TrialState
from backend.data.subscription_trial_config import AcceptedTrialOffer
from backend.data.subscription_trial_rejection import TrialRejectionReason
@@ -19,15 +19,8 @@ SIGNUP = NOW - timedelta(days=90)
TRIAL_START = NOW - timedelta(days=30)
-def user(
- tier: SubscriptionTier = SubscriptionTier.NO_TIER, customer: str | None = "cus_1"
-) -> LifecycleUser:
- return LifecycleUser(
- id="user-1",
- created_at=SIGNUP,
- stripe_customer_id=customer,
- subscription_tier=tier,
- )
+def user(customer: str | None = "cus_1") -> LifecycleUser:
+ return LifecycleUser(id="user-1", created_at=SIGNUP, stripe_customer_id=customer)
def trial(**overrides) -> TrialState:
@@ -78,9 +71,13 @@ def sub(
ended_at: datetime | None = None,
enrollment: str | None = None,
trial_end: datetime | None = None,
+ **stripe_fields,
) -> StripeSubscriptionFacts:
+ """``stripe_fields`` are more of Stripe's own, as a list call returns
+ them with ``data.discounts`` and ``data.customer`` expanded."""
return StripeSubscriptionFacts.model_validate(
{
+ "items": {"data": [{"price": {"unit_amount": 5000}, "quantity": 1}]},
"id": sub_id,
"customer": "cus_1",
"status": status,
@@ -92,10 +89,28 @@ def sub(
"trial_start": int(start.timestamp()) if trial_end else None,
"trial_end": int(trial_end.timestamp()) if trial_end else None,
"metadata": {"trial_enrollment_id": enrollment} if enrollment else {},
+ **stripe_fields,
}
)
+def coupon(
+ *, percent_off: float | None = None, amount_off: int | None = None, **discount
+) -> dict:
+ """A Stripe discount object, as ``data.discounts`` expands it."""
+ return {
+ "object": "discount",
+ "coupon": {"percent_off": percent_off, "amount_off": amount_off},
+ "end": None,
+ **discount,
+ }
+
+
+def by_request(**details) -> dict:
+ """Canceled through the API with no comment, as when the subscriber asks."""
+ return {"cancellation_details": {"reason": "cancellation_requested", **details}}
+
+
def snapshot(
*,
u: LifecycleUser | None = None,
@@ -158,15 +173,29 @@ def test_trial_set_to_cancel():
def test_trial_canceled_immediately():
result = snapshot(
t=trial(status="canceled", ends_at=NOW - timedelta(days=1)),
+ subs=[sub("sub_trial", "canceled", enrollment="trial-1", **by_request())],
+ )
+ assert result.subscription_status == "trial_canceled"
+
+
+def test_a_trial_set_to_cancel_that_ran_out_stays_trial_canceled():
+ result = snapshot(
+ t=trial(
+ status="canceled",
+ ends_at=NOW - timedelta(days=1),
+ cancel_at_period_end=True,
+ ),
subs=[sub("sub_trial", "canceled", enrollment="trial-1")],
)
assert result.subscription_status == "trial_canceled"
-def test_trial_that_ran_out_on_the_clock():
+def test_trial_that_ran_out_on_the_clock_has_expired():
# The webhook for the trial end hasn't landed; the clock alone ends it.
+ # Nobody canceled it, so it ran out rather than being canceled.
result = snapshot(t=trial(ends_at=NOW - timedelta(minutes=1)))
- assert result.subscription_status == "trial_canceled"
+ assert result.subscription_status == "trial_expired"
+ assert result.trial_started_at == TRIAL_START
@pytest.mark.parametrize("status", ["past_due", "unpaid"])
@@ -181,12 +210,62 @@ def test_a_failed_first_charge_after_the_trial_is_payment_failed(status: str):
assert result.subscription_started_at is None
-def test_a_trial_canceled_after_its_first_charge_failed_is_trial_canceled():
+ "details",
+ [
+ # Stripe gave up retrying the first charge.
+ {"reason": "payment_failed"},
+ # Our own cancel after the charge failed carries a comment.
+ {"reason": "cancellation_requested", "comment": "autogpt:payment_failed"},
+ ],
+)
+def test_a_trial_whose_first_charge_never_cleared_has_expired(details: dict):
result = snapshot(
t=trial(status="canceled", ends_at=NOW - timedelta(days=1)),
- subs=[sub("sub_trial", "canceled", enrollment="trial-1")],
+ subs=[
+ sub(
+ "sub_trial",
+ "canceled",
+ enrollment="trial-1",
+ cancellation_details=details,
+ )
+ ],
+ )
+ assert result.subscription_status == "trial_expired"
+ assert result.subscription_started_at is None
+
+
+ "comment, expected",
+ [
+ # Written by the customer in the billing portal's cancel flow.
+ ("Too expensive for me right now", "trial_canceled"),
+ ("autogpt_trial:intro_offer_already_used", "trial_expired"),
+ ("autogpt:replaced_by_another_plan", "trial_expired"),
+ ],
+)
+def test_only_our_own_cancel_comments_mean_we_canceled(comment, expected):
+ result = snapshot(
+ t=trial(status="canceled", ends_at=NOW - timedelta(days=1)),
+ subs=[
+ sub(
+ "sub_trial",
+ "canceled",
+ enrollment="trial-1",
+ **by_request(comment=comment),
+ )
+ ],
)
- assert result.subscription_status == "trial_canceled"
+ assert result.subscription_status == expected
+
+
+def test_every_comment_we_cancel_with_is_recognised_as_ours():
+ ours = [PAYMENT_FAILURE_CANCELLATION_COMMENT] + [
+ reason.stripe_comment for reason in TrialRejectionReason
+ ]
+ for comment in ours:
+ facts = sub(status="canceled", **by_request(comment=comment))
+ assert not facts.cancel_requested, comment
def test_trial_awaiting_its_first_charge_is_still_in_trial():
@@ -194,7 +273,7 @@ def test_trial_awaiting_its_first_charge_is_still_in_trial():
draft invoice about an hour later; until then it hasn't converted."""
t = trial(status="active", ends_at=NOW - timedelta(minutes=10))
live = sub("sub_trial", "active", start=TRIAL_START, enrollment="trial-1")
- result = snapshot(u=user(SubscriptionTier.NO_TIER), t=t, subs=[live])
+ result = snapshot(t=t, subs=[live])
assert result == LifecycleSnapshot(
subscription_status="in_trial", signup_at=SIGNUP, trial_started_at=TRIAL_START
)
@@ -203,7 +282,7 @@ def test_trial_awaiting_its_first_charge_is_still_in_trial():
paid = trial(
status="active", ends_at=NOW - timedelta(minutes=10), converted_at=converted
)
- result = snapshot(u=user(SubscriptionTier.PRO), t=paid, subs=[live])
+ result = snapshot(t=paid, subs=[live])
assert result.subscription_status == "subscribed"
assert result.subscription_started_at == converted
@@ -211,7 +290,6 @@ def test_trial_awaiting_its_first_charge_is_still_in_trial():
def test_converted_trial_starts_its_subscription_at_conversion():
converted = NOW - timedelta(days=20)
result = snapshot(
- u=user(SubscriptionTier.PRO),
t=trial(status="active", converted_at=converted),
subs=[sub("sub_trial", "active", start=TRIAL_START, enrollment="trial-1")],
)
@@ -266,12 +344,12 @@ def test_converted_trial_missing_from_stripe_still_counts_as_ended():
def test_subscription_from_another_trial_attempt_is_not_paid():
stale = sub("sub_old_attempt", "active", enrollment="trial-1")
result = snapshot(t=trial(ends_at=NOW - timedelta(days=1)), subs=[stale])
- assert result.subscription_status == "trial_canceled"
+ assert result.subscription_status == "trial_expired"
def test_subscribed():
start = NOW - timedelta(days=10)
- result = snapshot(u=user(SubscriptionTier.PRO), subs=[sub(start=start)])
+ result = snapshot(subs=[sub(start=start)])
assert result == LifecycleSnapshot(
subscription_status="subscribed",
signup_at=SIGNUP,
@@ -329,40 +407,55 @@ def test_never_paid_subscriptions_are_ignored():
assert snapshot(subs=subs).subscription_status == "signed"
-def test_resubscribe_after_ended_clears_the_old_dates():
+def test_resubscribe_after_a_gap_keeps_the_first_paid_start():
+ first_start = NOW - timedelta(days=200)
old = sub(
"sub_old",
"canceled",
- start=NOW - timedelta(days=200),
+ start=first_start,
canceled_at=NOW - timedelta(days=100),
ended_at=NOW - timedelta(days=90),
)
- new_start = NOW - timedelta(days=3)
- new = sub("sub_new", "active", start=new_start)
- result = snapshot(u=user(SubscriptionTier.PRO), subs=[old, new])
+ new = sub("sub_new", "active", start=NOW - timedelta(days=3))
+ result = snapshot(subs=[old, new])
assert result.subscription_status == "subscribed"
- assert result.subscription_started_at == new_start
+ assert result.subscription_started_at == first_start
_, unset = result.person_update()
assert "subscription_canceled_at" in unset
assert "subscription_ended_at" in unset
-def test_latest_ended_subscription_supplies_the_dates():
+def test_latest_ended_subscription_supplies_the_end_and_the_first_the_start():
+ first_start = NOW - timedelta(days=300)
first = sub(
- "sub_1",
- "canceled",
- start=NOW - timedelta(days=300),
- ended_at=NOW - timedelta(days=200),
+ "sub_1", "canceled", start=first_start, ended_at=NOW - timedelta(days=200)
)
- latest_start = NOW - timedelta(days=100)
latest_end = NOW - timedelta(days=10)
- latest = sub("sub_2", "canceled", start=latest_start, ended_at=latest_end)
+ latest = sub(
+ "sub_2", "canceled", start=NOW - timedelta(days=100), ended_at=latest_end
+ )
for order in permutations([first, latest]):
result = snapshot(subs=list(order))
- assert result.subscription_started_at == latest_start
+ assert result.subscription_started_at == first_start
assert result.subscription_ended_at == latest_end
+def test_a_converted_trial_then_a_resubscribe_keeps_the_conversion():
+ converted = NOW - timedelta(days=60)
+ t = trial(status="canceled", converted_at=converted)
+ ended = sub(
+ "sub_trial",
+ "canceled",
+ start=TRIAL_START - timedelta(days=40),
+ enrollment="trial-1",
+ ended_at=NOW - timedelta(days=20),
+ )
+ again = sub("sub_new", "active", start=NOW - timedelta(days=5))
+ result = snapshot(t=t, subs=[ended, again])
+ assert result.subscription_status == "subscribed"
+ assert result.subscription_started_at == converted
+
+
@pytest.mark.parametrize("order", list(permutations(range(2))))
def test_out_of_order_delete_of_old_subscription(order):
"""A late ``deleted`` for the old sub after ``created`` for the new one.
@@ -379,9 +472,10 @@ def test_out_of_order_delete_of_old_subscription(order):
)
new = sub("sub_new", "active", start=NOW - timedelta(days=2))
subs = [[old, new][i] for i in order]
- result = snapshot(u=user(SubscriptionTier.PRO), subs=subs)
+ result = snapshot(subs=subs)
assert result.subscription_status == "subscribed"
- assert result.subscription_started_at == new.started_at
+ # The first paid start stays, whichever subscription is the current one.
+ assert result.subscription_started_at == old.started_at
assert result.subscription_canceled_at is None
@@ -399,26 +493,6 @@ def test_paid_subscription_wins_over_a_running_trial():
assert result.trial_started_at == TRIAL_START
[email protected]("customer", [None, "cus_1"])
-def test_enterprise_is_subscribed(customer):
- ended = sub(status="canceled", ended_at=NOW - timedelta(days=1))
- result = snapshot(u=user(SubscriptionTier.ENTERPRISE, customer), subs=[ended])
- assert result == LifecycleSnapshot(
- subscription_status="subscribed", signup_at=SIGNUP
- )
-
-
-def test_enterprise_with_a_live_subscription_keeps_its_start():
- start = NOW - timedelta(days=40)
- live = sub(start=start, cancel_at_period_end=True)
- result = snapshot(u=user(SubscriptionTier.ENTERPRISE), subs=[live])
- assert result == LifecycleSnapshot(
- subscription_status="subscribed",
- signup_at=SIGNUP,
- subscription_started_at=start,
- )
-
-
def test_failed_renewal_after_an_upgrade_beats_the_older_canceled_sub():
"""The old sub is canceled after the new one starts (stale cleanup), so
end dates would pick the old one; the newer start wins."""
@@ -434,7 +508,7 @@ def test_failed_renewal_after_an_upgrade_beats_the_older_canceled_sub():
for order in permutations([old, new]):
result = snapshot(subs=list(order))
assert result.subscription_status == "payment_failed"
- assert result.subscription_started_at == new_start
+ assert result.subscription_started_at == old.started_at
assert result.subscription_ended_at is None
@@ -518,6 +592,7 @@ def test_stripe_native_trial_canceled_before_its_first_charge():
trial_end=NOW - timedelta(days=3),
canceled_at=NOW - timedelta(days=5),
ended_at=NOW - timedelta(days=5),
+ **by_request(),
)
result = snapshot(subs=[native])
assert result == LifecycleSnapshot(
@@ -527,6 +602,22 @@ def test_stripe_native_trial_canceled_before_its_first_charge():
)
+def test_stripe_native_trial_whose_first_charge_never_cleared_has_expired():
+ start = NOW - timedelta(days=10)
+ native = sub(
+ "sub_native",
+ "canceled",
+ start=start,
+ trial_end=NOW - timedelta(days=3),
+ canceled_at=NOW - timedelta(days=3),
+ ended_at=NOW - timedelta(days=3),
+ cancellation_details={"reason": "payment_failed"},
+ )
+ result = snapshot(subs=[native])
+ assert result.subscription_status == "trial_expired"
+ assert result.trial_started_at == start
+
+
def test_stripe_native_trial_that_was_paid_then_canceled_has_ended():
ended = NOW - timedelta(days=1)
native = sub(
@@ -547,25 +638,269 @@ def test_our_trial_subscription_is_not_a_native_trial():
assert snapshot(subs=[ours]).subscription_status == "signed"
+# ---- only paying subscribers are subscribed (SECRT-2795) ---------------------
+
+
[email protected]("customer", [None, "cus_1"])
+def test_a_tier_without_a_paid_subscription_is_not_subscribed(customer):
+ """The tier is not read at all: an admin-granted ENTERPRISE, BUSINESS or
+ any other plan with no paid Stripe subscription is ``signed``. The tier
+ column also defaulted to PRO for signups from 2026-03-26 to 2026-05-01,
+ so a paid tier alone never meant a payment."""
+ assert "subscription_tier" not in LifecycleUser.model_fields
+ assert snapshot(u=user(customer)).subscription_status == "signed"
+
+
+def test_an_enterprise_user_who_pays_through_stripe_is_subscribed():
+ start = NOW - timedelta(days=40)
+ result = snapshot(subs=[sub(start=start)])
+ assert result.subscription_status == "subscribed"
+ assert result.subscription_started_at == start
+
+
@pytest.mark.parametrize(
- "tier",
+ "discounts",
[
- SubscriptionTier.BASIC,
- SubscriptionTier.PRO,
- SubscriptionTier.MAX,
- SubscriptionTier.BUSINESS,
+ [coupon(percent_off=100)],
+ [coupon(amount_off=5000)],
+ [coupon(amount_off=9900)],
+ # Two discounts that together take the whole price.
+ [coupon(percent_off=50), coupon(amount_off=2500)],
],
)
-def test_paid_tier_without_stripe_is_not_a_grant(tier):
- """The tier column defaulted to PRO for signups from 2026-03-26 to
- 2026-05-01, so a paid tier with no Stripe customer usually never paid."""
- assert snapshot(u=user(tier, customer=None)).subscription_status == "signed"
+def test_a_fully_discounted_subscription_is_not_subscribed(discounts):
+ result = snapshot(subs=[sub(discounts=discounts)])
+ assert result == LifecycleSnapshot(subscription_status="signed", signup_at=SIGNUP)
-def test_stale_paid_tier_with_stripe_customer_follows_stripe():
- # Stripe owns this tier (the reconcile sweep will downgrade it), so a
- # missing subscription is not a manual grant.
- assert snapshot(u=user(SubscriptionTier.PRO)).subscription_status == "signed"
+def test_a_free_price_is_not_paid():
+ free = sub(items={"data": [{"price": {"unit_amount": 0}, "quantity": 1}]})
+ assert snapshot(subs=[free]).subscription_status == "signed"
+
+
+def test_a_customer_wide_full_discount_is_not_paid():
+ # A discount on the customer applies to a subscription without its own.
+ free = sub(customer={"id": "cus_1", "discount": coupon(percent_off=100)})
+ assert free.customer == "cus_1"
+ assert snapshot(subs=[free]).subscription_status == "signed"
+
+
+def test_a_subscription_discount_overrides_the_customers():
+ partial = sub(
+ customer={"id": "cus_1", "discount": coupon(percent_off=100)},
+ discounts=[coupon(percent_off=20)],
+ )
+ assert snapshot(subs=[partial]).subscription_status == "subscribed"
+
+
[email protected]("discount", [coupon(percent_off=50), coupon(amount_off=4999)])
+def test_a_partial_discount_is_still_paid(discount):
+ result = snapshot(subs=[sub(discounts=[discount])])
+ assert result.subscription_status == "subscribed"
+
+
+def test_a_full_discount_that_has_ended_is_paid_again():
+ # A repeating coupon (N months): once it ends, the subscription is paid.
+ ended = coupon(percent_off=100, end=int((NOW - timedelta(days=1)).timestamp()))
+ running = coupon(percent_off=100, end=int((NOW + timedelta(days=1)).timestamp()))
+ assert snapshot(subs=[sub(discounts=[ended])]).subscription_status == ("subscribed")
+ assert snapshot(subs=[sub(discounts=[running])]).subscription_status == "signed"
+
+
+def test_stacked_discounts_apply_in_their_listed_order():
+ # $25 off then 50% off a $50 price leaves $12.50, as Stripe charges it;
+ # 50% off then $25 off leaves nothing.
+ amount_first = [coupon(amount_off=2500), coupon(percent_off=50)]
+ percent_first = [coupon(percent_off=50), coupon(amount_off=2500)]
+ assert snapshot(subs=[sub(discounts=amount_first)]).subscription_status == (
+ "subscribed"
+ )
+ assert snapshot(subs=[sub(discounts=percent_first)]).subscription_status == (
+ "signed"
+ )
+
+
+def test_a_customer_coupon_given_later_keeps_an_old_paid_subscription():
+ # Paid, ended, and only then a 100% coupon on the customer: the old
+ # subscription was paid, so the person has ended, not merely signed up.
+ start = NOW - timedelta(days=100)
+ ended_at = NOW - timedelta(days=50)
+ later = coupon(percent_off=100, start=int((NOW - timedelta(days=5)).timestamp()))
+ old = sub(
+ status="canceled",
+ start=start,
+ canceled_at=ended_at,
+ ended_at=ended_at,
+ customer={"id": "cus_1", "discount": later},
+ )
+ result = snapshot(subs=[old])
+ assert result.subscription_status == "subscription_ended"
+ assert result.subscription_started_at == start
+
+
+def first_invoice(total: int, discounted: int = 0, reason="subscription_create"):
+ """A subscription's ``latest_invoice``, as ``data.latest_invoice``
+ expands it."""
+ return {
+ "billing_reason": reason,
+ "total": total,
+ "total_discount_amounts": (
+ [{"amount": discounted, "discount": "di_1"}] if discounted else []
+ ),
+ }
+
+
+ "invoice, expected",
+ [
+ # A 100% coupon for the first period: Stripe removed it from the
+ # subscription after the first invoice, which it brought to zero.
+ (first_invoice(0, discounted=5000), "signed"),
+ (first_invoice(2500, discounted=2500), "subscribed"),
+ # $0 without a discount (a metered price with no usage yet, a later
+ # billing anchor): still paid.
+ (first_invoice(0), "subscribed"),
+ # A renewal that a coupon made free is read from the coupon instead.
+ (first_invoice(0, discounted=5000, reason="subscription_cycle"), "subscribed"),
+ # Unexpanded: only the invoice ID.
+ ("in_1", "subscribed"),
+ ],
+)
+def test_a_first_invoice_a_discount_took_all_of_is_not_paid(invoice, expected):
+ result = snapshot(subs=[sub(latest_invoice=invoice)])
+ assert result.subscription_status == expected
+
+
+def test_a_first_free_period_canceled_before_any_charge_never_paid():
+ gone = sub(
+ status="canceled",
+ canceled_at=NOW - timedelta(days=3),
+ ended_at=NOW - timedelta(days=1),
+ latest_invoice=first_invoice(0, discounted=5000),
+ )
+ assert snapshot(subs=[gone]) == LifecycleSnapshot(
+ subscription_status="signed", signup_at=SIGNUP
+ )
+
+
+def test_an_unexpanded_discount_is_not_read_as_a_coupon():
+ # A list call without the expansion returns discount IDs only.
+ result = snapshot(subs=[sub(discounts=["di_1"])])
+ assert result.subscription_status == "subscribed"
+
+
+def test_a_fully_discounted_subscription_neither_cancels_nor_ends():
+ free = [coupon(percent_off=100)]
+ canceling = sub(
+ cancel_at_period_end=True, canceled_at=NOW - timedelta(days=1), discounts=free
+ )
+ gone = sub(
+ status="canceled",
+ canceled_at=NOW - timedelta(days=3),
+ ended_at=NOW - timedelta(days=1),
+ discounts=free,
+ )
+ assert snapshot(subs=[canceling]).subscription_status == "signed"
+ assert snapshot(subs=[gone]).subscription_status == "signed"
+
+
[email protected]("old_status", ["canceled", "past_due"])
+def test_a_former_payer_on_a_free_subscription_is_signed_not_churned(old_status):
+ """Comped after paying: not paying now, so not subscribed, but not
+ churned or failing either. The first paid start stays."""
+ start = NOW - timedelta(days=100)
+ old = sub(
+ "sub_old",
+ old_status,
+ start=start,
+ canceled_at=NOW - timedelta(days=30) if old_status == "canceled" else None,
+ ended_at=NOW - timedelta(days=30) if old_status == "canceled" else None,
+ )
+ comped = sub(
+ "sub_comped",
+ start=NOW - timedelta(days=20),
+ discounts=[coupon(percent_off=100)],
+ )
+ assert snapshot(subs=[old, comped]) == LifecycleSnapshot(
+ subscription_status="signed",
+ signup_at=SIGNUP,
+ subscription_started_at=start,
+ )
+
+
+def test_a_subscription_that_paid_before_its_coupon_keeps_its_paid_start():
+ """One subscription, paid for a month, then given a 100% coupon: they're
+ comped now, not a never-paid signup, and they first paid at its start."""
+ start = NOW - timedelta(days=60)
+ comp = coupon(percent_off=100, start=int((NOW - timedelta(days=30)).timestamp()))
+ result = snapshot(subs=[sub(start=start, discounts=[comp])])
+ assert result == LifecycleSnapshot(
+ subscription_status="signed", signup_at=SIGNUP, subscription_started_at=start
+ )
+
+
+def test_a_converted_trial_given_a_coupon_later_keeps_its_conversion():
+ converted = NOW - timedelta(days=40)
+ t = trial(status="active", ends_at=converted, converted_at=converted)
+ comp = coupon(percent_off=100, start=int((NOW - timedelta(days=10)).timestamp()))
+ later = sub("sub_trial", start=TRIAL_START, enrollment="trial-1", discounts=[comp])
+ result = snapshot(t=t, subs=[later])
+ assert result.subscription_status == "signed"
+ assert result.subscription_started_at == converted
+
+
+def test_a_subscription_ending_with_its_repeating_coupon_was_never_paid():
+ # Three free months, set to cancel: Stripe ends it the moment the coupon
+ # ends.
+ start = NOW - timedelta(days=90)
+ ended = NOW - timedelta(days=1)
+ free = coupon(
+ percent_off=100,
+ start=int(start.timestamp()),
+ end=int(ended.timestamp()),
+ )
+ gone = sub(
+ status="canceled",
+ start=start,
+ canceled_at=NOW - timedelta(days=20),
+ ended_at=ended,
+ discounts=[free],
+ )
+ assert snapshot(subs=[gone]) == LifecycleSnapshot(
+ subscription_status="signed", signup_at=SIGNUP
+ )
+
+
+def test_a_free_subscription_does_not_supply_the_first_paid_start():
+ free = sub(
+ "sub_free",
+ "canceled",
+ start=NOW - timedelta(days=100),
+ ended_at=NOW - timedelta(days=50),
+ discounts=[coupon(percent_off=100)],
+ )
+ paid_start = NOW - timedelta(days=10)
+ result = snapshot(subs=[free, sub("sub_paid", start=paid_start)])
+ assert result.subscription_status == "subscribed"
+ assert result.subscription_started_at == paid_start
+
+
+def test_a_trial_converted_onto_a_free_subscription_is_not_subscribed():
+ t = trial(
+ status="active",
+ ends_at=NOW - timedelta(days=20),
+ converted_at=NOW - timedelta(days=20),
+ )
+ free = sub(
+ "sub_trial",
+ "active",
+ start=TRIAL_START,
+ enrollment="trial-1",
+ discounts=[coupon(percent_off=100)],
+ )
+ result = snapshot(t=t, subs=[free])
+ assert result.subscription_status == "trial_expired"
+ assert result.subscription_started_at is None
def test_person_update_sets_utc_iso_dates_and_unsets_the_rest():
diff --git a/autogpt_platform/backend/backend/data/posthog_lifecycle_trial_cancel_test.py b/autogpt_platform/backend/backend/data/posthog_lifecycle_trial_cancel_test.py
index 022f26b390..35866e2b02 100644
--- a/autogpt_platform/backend/backend/data/posthog_lifecycle_trial_cancel_test.py
+++ b/autogpt_platform/backend/backend/data/posthog_lifecycle_trial_cancel_test.py
@@ -5,9 +5,8 @@ another plan is bought or the trial converts early."""
from datetime import timedelta
import pytest
-from prisma.enums import SubscriptionTier
-from backend.data.posthog_lifecycle import LifecycleSnapshot, StripeSubscriptionFacts
+from backend.data.posthog_lifecycle import LifecycleSnapshot
from backend.data.posthog_lifecycle_test import (
NOW,
SIGNUP,
@@ -15,8 +14,8 @@ from backend.data.posthog_lifecycle_test import (
snapshot,
sub,
trial,
- user,
)
+from backend.data.stripe_subscription_facts import StripeSubscriptionFacts
TRIAL_END = NOW + timedelta(days=5)
@@ -82,7 +81,7 @@ def test_buying_another_plan_while_cancel_pending_is_subscribed():
_trial_sub("canceled", canceled_at=NOW, ended_at=NOW),
)
for t, trial_sub in (before, after):
- result = snapshot(u=user(SubscriptionTier.MAX), t=t, subs=[trial_sub, max_sub])
+ result = snapshot(t=t, subs=[trial_sub, max_sub])
assert result == LifecycleSnapshot(
subscription_status="subscribed",
signup_at=SIGNUP,
@@ -99,6 +98,6 @@ def test_converting_early_while_cancel_pending_is_subscribed_from_conversion():
charging = trial(status="active", ends_at=early_end)
assert snapshot(t=charging, subs=[live]).subscription_status == "in_trial"
converted = trial(status="active", ends_at=early_end, converted_at=NOW)
- result = snapshot(u=user(SubscriptionTier.PRO), t=converted, subs=[live])
+ result = snapshot(t=converted, subs=[live])
assert result.subscription_status == "subscribed"
assert result.subscription_started_at == NOW
diff --git a/autogpt_platform/backend/backend/data/stripe_client.py b/autogpt_platform/backend/backend/data/stripe_client.py
index 8b6c446e02..29b53e72ff 100644
--- a/autogpt_platform/backend/backend/data/stripe_client.py
+++ b/autogpt_platform/backend/backend/data/stripe_client.py
@@ -64,8 +64,11 @@ async def stripe_call_timeout(
async def stripe_list_items(
page: stripe.ListObject[StripeResource],
+ timeout_seconds: float | None = None,
) -> AsyncIterator[StripeResource]:
- """Keep subsequent SDK list requests inside the same timeout/metrics boundary."""
+ """Keep subsequent SDK list requests inside the same timeout/metrics
+ boundary. Pass the first page's ``timeout_seconds`` when it wasn't the
+ default."""
while True:
for item in page.data:
yield item
@@ -73,7 +76,10 @@ async def stripe_list_items(
return
if not page.data:
raise ValueError("Stripe returned an empty page with more results")
- page = await stripe_call(page.next_page_async)
+ page = await _call(
+ DEFAULT_TIMEOUT_SECONDS if timeout_seconds is None else timeout_seconds,
+ page.next_page_async,
+ )
async def _call(
diff --git a/autogpt_platform/backend/backend/data/stripe_reconciliation_integration_test.py b/autogpt_platform/backend/backend/data/stripe_reconciliation_integration_test.py
index b767b8990c..d38eab61c1 100644
--- a/autogpt_platform/backend/backend/data/stripe_reconciliation_integration_test.py
+++ b/autogpt_platform/backend/backend/data/stripe_reconciliation_integration_test.py
@@ -87,7 +87,6 @@ def _scope_sweep_to(mocker: pytest_mock.MockFixture, user_id: str) -> AsyncMock:
async def test_admin_grant_survives_opening_billing_and_the_sweep(
server, mocker: pytest_mock.MockFixture, comped_user_id: str
) -> None:
- mocker.patch("backend.copilot.rate_limit.schedule_posthog_lifecycle_sync")
mocker.patch(
"backend.copilot.rate_limit._drift_check_background", new_callable=AsyncMock
)
diff --git a/autogpt_platform/backend/backend/data/stripe_subscription_facts.py b/autogpt_platform/backend/backend/data/stripe_subscription_facts.py
new file mode 100644
index 0000000000..0054066115
--- /dev/null
+++ b/autogpt_platform/backend/backend/data/stripe_subscription_facts.py
@@ -0,0 +1,187 @@
+"""The fields of a Stripe subscription the lifecycle mapping reads.
+
+Read from a subscription list call with ``data.discounts``,
+``data.customer`` and ``data.latest_invoice`` expanded (``LIST_EXPAND``), so a
+coupon on the subscription or on the customer, and a first invoice that
+charged nothing, are visible without a call per subscription. Without the
+expansion a discount is only an ID, and is not read as a coupon.
+"""
+
+from collections.abc import Mapping, Sequence
+from datetime import UTC, datetime
+from typing import Any
+
+from pydantic import BaseModel, Field, model_validator
+
+# Pass as ``expand=`` to ``stripe.Subscription.list``.
+LIST_EXPAND = ["data.discounts", "data.customer", "data.latest_invoice"]
+
+_EPOCH = datetime.min.replace(tzinfo=UTC)
+
+# The comments on our own cancels: after a failed payment
+# (``autogpt:payment_failed``), of a rejected trial (``autogpt_trial:...``),
+# of a replaced plan. A comment the customer wrote, as the billing portal
+# asks for one, doesn't start with these.
+OUR_CANCEL_COMMENT_PREFIXES = ("autogpt:", "autogpt_trial:")
+
+
+class DiscountFacts(BaseModel):
+ """A Stripe discount: its coupon's reduction, and when it starts and
+ stops."""
+
+ percent_off: float | None = None
+ amount_off: int | None = None
+ start: datetime | None = None
+ end: datetime | None = None
+
+ @model_validator(mode="before")
+ @classmethod
+ def _from_stripe(cls, data: Any) -> Any:
+ if not isinstance(data, Mapping) or "coupon" not in data:
+ return data
+ coupon = data.get("coupon") or {}
+ return {
+ "percent_off": coupon.get("percent_off"),
+ "amount_off": coupon.get("amount_off"),
+ "start": data.get("start"),
+ "end": data.get("end"),
+ }
+
+ def in_force_at(self, moment: datetime) -> bool:
+ # A customer's discount is read as it is now, so one that started
+ # after ``moment`` (say, after an old subscription ended) didn't apply.
+ # Its end counts as in force: a subscription that ends when its
+ # repeating coupon does was free to the last moment.
+ started = self.start is None or utc(self.start) <= moment
+ return started and (self.end is None or utc(self.end) >= moment)
+
+
+class StripeSubscriptionFacts(BaseModel):
+ """Validates straight from a Stripe object or dict; Stripe's unix
+ timestamps become UTC datetimes."""
+
+ id: str
+ customer: str | None = None
+ status: str
+ cancel_at_period_end: bool = False
+ start_date: datetime | None = None
+ created: datetime | None = None
+ canceled_at: datetime | None = None
+ ended_at: datetime | None = None
+ trial_start: datetime | None = None
+ trial_end: datetime | None = None
+ metadata: dict[str, str] = Field(default_factory=dict)
+ # Stripe's ``cancellation_details``: ``cancellation_requested`` for any
+ # cancel made through the API or the billing portal, ``payment_failed``
+ # when Stripe gave up on a payment. Our own cancels carry one of our
+ # comments (``OUR_CANCEL_COMMENT_PREFIXES``).
+ cancellation_reason: str | None = None
+ cancellation_comment: str | None = None
+ # The subscription's own discounts, and the customer's, which applies
+ # only to a subscription without any of its own.
+ discounts: list[DiscountFacts] = Field(default_factory=list)
+ customer_discount: DiscountFacts | None = None
+ # The price of one billing period before any discount, when every item
+ # has a flat price.
+ gross_amount: int | None = None
+ # Its only invoice so far is the first one, and a discount took all of
+ # it: a 100% coupon for one period, which Stripe takes off the
+ # subscription once that invoice is made, so ``discounts`` no longer
+ # shows it. A first invoice that is $0 without a discount (a trial, a
+ # metered price with no usage yet) doesn't count.
+ first_invoice_free: bool = False
+
+ @model_validator(mode="before")
+ @classmethod
+ def _from_stripe(cls, data: Any) -> Any:
+ if not isinstance(data, Mapping):
+ return data
+ values = dict(data)
+ customer = values.get("customer")
+ if isinstance(customer, Mapping):
+ values["customer"] = customer.get("id")
+ values.setdefault("customer_discount", customer.get("discount"))
+ details = values.get("cancellation_details") or {}
+ values.setdefault("cancellation_reason", details.get("reason"))
+ values.setdefault("cancellation_comment", details.get("comment"))
+ # Unexpanded, a discount is only its ID.
+ values["discounts"] = [
+ d for d in values.get("discounts") or [] if isinstance(d, Mapping)
+ ]
+ items = (values.get("items") or {}).get("data")
+ if items is not None:
+ values.setdefault("gross_amount", _gross_amount(items))
+ invoice = values.get("latest_invoice")
+ if isinstance(invoice, Mapping):
+ values.setdefault("first_invoice_free", _discounted_to_zero(invoice))
+ return values
+
+ @property
+ def started_at(self) -> datetime | None:
+ return self.start_date or self.created
+
+ @property
+ def trial_enrollment_id(self) -> str | None:
+ return self.metadata.get("trial_enrollment_id") or None
+
+ @property
+ def cancel_requested(self) -> bool:
+ """The subscriber asked for it to end: set to cancel at the period
+ end, or canceled on request without a comment of ours (one they
+ wrote themselves still counts). Our own cancels (after a failed
+ payment, of a rejected trial, of a replaced plan) carry our comment,
+ and Stripe ending it after a failed payment says ``payment_failed``."""
+ comment = self.cancellation_comment or ""
+ return self.cancel_at_period_end or (
+ self.cancellation_reason == "cancellation_requested"
+ and not comment.startswith(OUR_CANCEL_COMMENT_PREFIXES)
+ )
+
+ def free_at(self, moment: datetime) -> bool:
+ """Its discounts in force at ``moment`` take the whole price, the
+ price itself is zero, or its only invoice charged nothing: nothing is
+ charged. A partial discount, or an amount off a price that isn't
+ flat, still leaves a charge. Stacked discounts apply one after the
+ other, in their listed order, as Stripe applies them."""
+ if self.gross_amount == 0 or self.first_invoice_free:
+ return True
+ discounts = self.discounts or (
+ [self.customer_discount] if self.customer_discount else []
+ )
+ live = [d for d in discounts if d.in_force_at(moment)]
+ if any((d.percent_off or 0) >= 100 for d in live):
+ return True
+ if self.gross_amount is None or not any(d.amount_off for d in live):
+ return False
+ remaining = float(self.gross_amount)
+ for discount in live:
+ remaining *= 1 - (discount.percent_off or 0) / 100
+ remaining = max(remaining - (discount.amount_off or 0), 0.0)
+ return remaining <= 0
+
+
+def _discounted_to_zero(invoice: Mapping[str, Any]) -> bool:
+ """The subscription's first invoice, brought to zero by a discount: one
+ that took something off, not merely one attached to a $0 invoice."""
+ return (
+ invoice.get("billing_reason") == "subscription_create"
+ and invoice.get("total") == 0
+ and any(
+ (amount or {}).get("amount")
+ for amount in invoice.get("total_discount_amounts") or []
+ )
+ )
+
+
+def _gross_amount(items: Sequence[Mapping[str, Any]]) -> int | None:
+ units = [(item.get("price") or {}).get("unit_amount") for item in items]
+ flat = [unit for unit in units if unit is not None]
+ if not units or len(flat) < len(units):
+ return None
+ return sum(unit * (item.get("quantity") or 1) for unit, item in zip(flat, items))
+
+
+def utc(value: datetime | None) -> datetime:
+ if value is None:
+ return _EPOCH
+ return value.replace(tzinfo=UTC) if value.tzinfo is None else value
diff --git a/autogpt_platform/backend/backend/notifications/audience_consent_recheck_test.py b/autogpt_platform/backend/backend/notifications/audience_consent_recheck_test.py
index 55bcbbaa81..b267c001a5 100644
--- a/autogpt_platform/backend/backend/notifications/audience_consent_recheck_test.py
+++ b/autogpt_platform/backend/backend/notifications/audience_consent_recheck_test.py
@@ -23,6 +23,7 @@ HANDLERS = {
AudienceAction.CHECKOUT_OPENED: "record_checkout_opened",
AudienceAction.UNSUBSCRIBE: "unsubscribe",
AudienceAction.ONBOARDING_PROFILE: "record_onboarding_profile",
+ AudienceAction.LIFECYCLE_FIELDS: "record_lifecycle",
}
WRITES = [action for action in AudienceAction if action != AudienceAction.UNSUBSCRIBE]
diff --git a/autogpt_platform/backend/backend/notifications/checkout_audience_test.py b/autogpt_platform/backend/backend/notifications/checkout_audience_test.py
index 7914fc2aa8..4663465530 100644
--- a/autogpt_platform/backend/backend/notifications/checkout_audience_test.py
+++ b/autogpt_platform/backend/backend/notifications/checkout_audience_test.py
@@ -247,6 +247,7 @@ async def test_every_audience_action_has_a_handler(monkeypatch):
"record_checkout_opened",
"unsubscribe",
"record_onboarding_profile",
+ "record_lifecycle",
):
handlers[name] = AsyncMock()
monkeypatch.setattr(mailerlite, name, handlers[name])
diff --git a/autogpt_platform/backend/backend/notifications/checkout_backfill.py b/autogpt_platform/backend/backend/notifications/checkout_backfill.py
index 90ad6585a9..06f08a2087 100644
--- a/autogpt_platform/backend/backend/notifications/checkout_backfill.py
+++ b/autogpt_platform/backend/backend/notifications/checkout_backfill.py
@@ -36,16 +36,12 @@ from backend.notifications.mailerlite import (
_find_subscriber,
_headers,
_payload,
+ held_value,
)
-from backend.notifications.mailerlite_backfill import (
- BatchAnswer,
- Subscription,
- _refusal,
-)
+from backend.notifications.mailerlite_backfill import BatchAnswer, _refusal
from backend.notifications.mailerlite_field_backfill import (
Current,
Person,
- _normalise,
_valid,
desired,
)
@@ -176,7 +172,7 @@ def _changed(fields: Fields, held: Mapping[str, object] | None) -> Fields:
return {
field: value
for field, value in fields.items()
- if held is None or _normalise(field, held.get(field.value)) != value
+ if held is None or held_value(field, held.get(field.value)) != value
}
@@ -185,13 +181,15 @@ async def apply(
group_id: str,
on_progress: Callable[[int, int], None] | None = None,
*,
- refresh: Callable[[str], Awaitable[list[Subscription]]] | None = None,
+ refresh: Callable[[Person], Awaitable[Person]] | None = None,
kept_out: KeptOut | None = None,
) -> tuple[int, int, int]:
"""Visit each planned opener: with `kept_out`, check they may still enter
MailerLite, since they may have opted out or been seen in Iran or Russia
since the plan; read them again from MailerLite and, with `refresh`,
- their subscriptions again from Stripe; and write what they still need as
+ their subscriptions from Stripe and their trial row again, since the
+ status reads both (a trial that converted since the plan needs both
+ fresh, or it reads as still in trial); 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.
@@ -211,12 +209,9 @@ async def apply(
skipped += 1
continue
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": await refresh(opener.person)}
)
- opener = opener.model_copy(update={"person": person})
subscriber = await _find_subscriber(email)
except Exception:
failed += 1
diff --git a/autogpt_platform/backend/backend/notifications/checkout_backfill_test.py b/autogpt_platform/backend/backend/notifications/checkout_backfill_test.py
index d3beb41033..922ddc1114 100644
--- a/autogpt_platform/backend/backend/notifications/checkout_backfill_test.py
+++ b/autogpt_platform/backend/backend/notifications/checkout_backfill_test.py
@@ -2,14 +2,15 @@
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 datetime import UTC, datetime, timedelta
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from backend.data.notifications import SubscriberField
+from backend.data.posthog_lifecycle_test import NOW, trial
+from backend.data.stripe_subscription_facts import StripeSubscriptionFacts
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())
@@ -71,7 +72,11 @@ def test_an_existing_subscriber_only_joins_the_group():
def test_the_status_comes_from_stripe_not_the_signed_default():
- opener = _opener(subscriptions=[Subscription(status="active", start_date=OPENED)])
+ opener = _opener(
+ subscriptions=[
+ StripeSubscriptionFacts(id="sub_1", status="active", start_date=OPENED)
+ ]
+ )
held = {**_held(_opener()), "subscription_status": "signed"}
plan = checkout_backfill.plan(
[opener], current={"[email protected]": held}, members={"[email protected]": "1"}
@@ -246,7 +251,16 @@ async def test_a_failed_write_is_counted_and_the_run_goes_on(monkeypatch, caplog
assert "[email protected]" not in caplog.text
-ACTIVE = [Subscription(id="sub_1", status="active", start_date=OPENED)]
+ACTIVE = [StripeSubscriptionFacts(id="sub_1", status="active", start_date=OPENED)]
+
+
+def _now_with(**update):
+ """A `refresh` that finds the person as `update` says they are now."""
+
+ async def refresh(person: Person) -> Person:
+ return person.model_copy(update=update)
+
+ return AsyncMock(side_effect=refresh)
async def _apply_with(monkeypatch, plan, *, held, refresh, kept_out=None):
@@ -270,10 +284,10 @@ async def test_the_status_written_is_stripes_now_not_the_plans(monkeypatch):
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)
+ refresh = _now_with(subscriptions=ACTIVE)
result, client = await _apply_with(monkeypatch, plan, held=None, refresh=refresh)
assert result == (1, 0, 0)
- refresh.assert_awaited_once_with("cus_1")
+ refresh.assert_awaited_once_with(plan.changes[0].opener.person)
fields = client.post.await_args.kwargs["json"]["fields"]
assert fields["subscription_status"] == "subscribed"
assert fields["subscription_started_date"] == "2026-07-15"
@@ -295,12 +309,44 @@ async def test_a_newer_status_the_live_event_wrote_is_not_overwritten(monkeypatc
monkeypatch,
plan,
held={"fields": live},
- refresh=AsyncMock(return_value=ACTIVE),
+ refresh=_now_with(subscriptions=ACTIVE),
)
assert result == (0, 0, 1)
client.post.assert_not_awaited()
+async def test_a_trial_that_converted_since_the_plan_is_written_subscribed(
+ monkeypatch,
+):
+ """The trial row is read again with Stripe: Stripe alone says active,
+ and the planned row, not yet converted, would make that the hour before
+ the first charge (`in_trial`) rather than a paying subscriber."""
+ trial_sub = StripeSubscriptionFacts(
+ id="sub_trial",
+ status="active",
+ start_date=OPENED,
+ metadata={"trial_enrollment_id": "trial-1"},
+ )
+ planned = trial(
+ status="active", ends_at=NOW - timedelta(hours=2), converted_at=None
+ )
+ opener = _opener(subscriptions=[trial_sub])
+ opener.person.trial = planned
+ plan = checkout_backfill.plan([opener], current={}, members={})
+ assert plan.changes[0].fields[SubscriberField.STATUS] == "in_trial"
+ converted = planned.model_copy(update={"converted_at": NOW - timedelta(hours=1)})
+ result, client = await _apply_with(
+ monkeypatch,
+ plan,
+ held=None,
+ refresh=_now_with(subscriptions=[trial_sub], trial=converted),
+ )
+ assert result == (1, 0, 0)
+ fields = client.post.await_args.kwargs["json"]["fields"]
+ assert fields["subscription_status"] == "subscribed"
+
+
@pytest.mark.asyncio
async def test_a_failed_stripe_refresh_is_counted_and_retried_later(monkeypatch):
plan = checkout_backfill.plan([_opener()], current={}, members={})
diff --git a/autogpt_platform/backend/backend/notifications/conftest.py b/autogpt_platform/backend/backend/notifications/conftest.py
index db0b39bece..f79773bac1 100644
--- a/autogpt_platform/backend/backend/notifications/conftest.py
+++ b/autogpt_platform/backend/backend/notifications/conftest.py
@@ -107,3 +107,19 @@ def audience_consent(monkeypatch: pytest.MonkeyPatch) -> SimpleNamespace:
delivery, "get_database_manager_async_client", lambda **_: client
)
return client
+
+
[email protected](autouse=True)
+def lifecycle_resync(monkeypatch: pytest.MonkeyPatch) -> AsyncMock:
+ """A group join asks the database manager for one more lifecycle sync
+ (`lifecycle_audience.resync_after_group_change`). Stubbed so no test
+ reaches for the RPC; assert on the returned mock to check the request."""
+ from backend.notifications import lifecycle_audience
+
+ request = AsyncMock()
+ monkeypatch.setattr(
+ lifecycle_audience,
+ "posthog_lifecycle_db",
+ lambda: SimpleNamespace(request_posthog_lifecycle_sync=request),
+ )
+ return request
diff --git a/autogpt_platform/backend/backend/notifications/lifecycle.py b/autogpt_platform/backend/backend/notifications/lifecycle.py
index ddbc50aee4..32b6912f15 100644
--- a/autogpt_platform/backend/backend/notifications/lifecycle.py
+++ b/autogpt_platform/backend/backend/notifications/lifecycle.py
@@ -11,6 +11,12 @@ A customer who opted out of marketing still gets every one of these emails;
only the MailerLite audience changes that ride along are skipped for them
(`consent.py`). So does one a signal places in Iran or Russia, the checkout's
billing address included.
+
+The group changes carry no status or dates: those come from the lifecycle
+sync that every subscription webhook triggers (`lifecycle_audience.py`), by
+the same rules as PostHog's, so a free (100% coupon) subscription is not
+written as `subscribed` here. Joining a group can create the subscriber after
+that sync found nobody to write, so a join asks for one more sync.
"""
import logging
@@ -32,10 +38,10 @@ from backend.data.notifications import (
)
from backend.data.stripe_client import stripe_call
from backend.data.user import BillingEmailRecipient
-from backend.notifications import subscriber_fields
from backend.notifications.audience_enrichment import billing_country
from backend.notifications.consent import audience_change_allowed
from backend.notifications.dedupe import claim_once, release_claim
+from backend.notifications.lifecycle_audience import resync_after_group_change
from backend.notifications.lifecycle_plan import (
card_from_invoice,
format_amount,
@@ -117,17 +123,14 @@ async def on_checkout_completed(session: dict, subscription: dict) -> None:
user = await _user_for(session.get("customer"))
if user is None:
return
- fields = subscriber_fields.subscribed(subscription.get("start_date"))
countries = (billing_country(session),)
if user.welcome_email_sent_at is not None:
if not audience_change_allowed(user, AudienceAction.ADD_CHANGELOG, countries):
return
- event = audience_event(
- AudienceAction.ADD_CHANGELOG, user.email, user.id, fields
- )
- if event is not None:
- await queue_audience_change(event)
+ event = audience_event(AudienceAction.ADD_CHANGELOG, user.email, user.id)
+ if event is not None and (await queue_audience_change(event)).success:
+ await resync_after_group_change(user.id)
return
# Claim before queueing: Stripe retries webhooks, and two welcomes is worse
@@ -166,7 +169,7 @@ async def on_checkout_completed(session: dict, subscription: dict) -> None:
# Must not propagate: the welcome is already out and the claim is durable,
# so a Stripe retry would take the returning-customer branch and enrol them
# in the changelog instead of the tour. Report it rather than fail.
- event = audience_event(AudienceAction.ENROLL_TOUR, user.email, user.id, fields)
+ event = audience_event(AudienceAction.ENROLL_TOUR, user.email, user.id)
if event is None:
return
enrolled = await queue_audience_change(event)
@@ -175,6 +178,8 @@ async def on_checkout_completed(session: dict, subscription: dict) -> None:
f"Welcomed user {user.id} but could not queue the onboarding tour: "
f"{enrolled.message}"
)
+ return
+ await resync_after_group_change(user.id)
async def on_payment_failed(invoice: dict) -> None:
@@ -278,10 +283,6 @@ async def on_subscription_updated(subscription: dict, previous: dict) -> None:
),
claim_key,
)
- await _queue_fields(
- user,
- subscriber_fields.subscription_canceled(subscription.get("canceled_at")),
- )
return
claim_key = f"resumed:{sub_id}:{episode}"
@@ -299,7 +300,6 @@ async def on_subscription_updated(subscription: dict, previous: dict) -> None:
),
claim_key,
)
- await _queue_fields(user, subscriber_fields.subscription_resumed())
async def on_subscription_deleted(subscription: dict) -> None:
@@ -340,23 +340,11 @@ async def on_subscription_deleted(subscription: dict) -> None:
# Churned users get win-back only, never the monthly update.
if not audience_change_allowed(user, AudienceAction.REMOVE_CHANGELOG):
return
- event = audience_event(
- AudienceAction.REMOVE_CHANGELOG,
- user.email,
- user.id,
- subscriber_fields.subscription_ended(subscription.get("ended_at")),
- )
+ event = audience_event(AudienceAction.REMOVE_CHANGELOG, user.email, user.id)
if event is not None:
await queue_audience_change(event)
-async def _queue_fields(
- user: BillingEmailRecipient, fields: subscriber_fields.Fields
-) -> None:
- if audience_change_allowed(user, AudienceAction.UPDATE_FIELDS):
- await subscriber_fields.queue_fields(user.id, user.email, fields)
-
-
def _churn_reason(subscription: dict) -> str | None:
"""Stripe's ``cancellation_details.reason``, except that our own cancel
after a failed renewal is involuntary churn, not a requested one."""
diff --git a/autogpt_platform/backend/backend/notifications/lifecycle_audience.py b/autogpt_platform/backend/backend/notifications/lifecycle_audience.py
new file mode 100644
index 0000000000..3ae52d57c8
--- /dev/null
+++ b/autogpt_platform/backend/backend/notifications/lifecycle_audience.py
@@ -0,0 +1,109 @@
+"""The subscription status and dates in MailerLite, from the lifecycle
+snapshot PostHog gets too (SECRT-2795).
+
+One mapping (`data/posthog_lifecycle.py`) decides the status for both, so
+both agree on who is a paying subscriber. Two ways in:
+
+- live: each lifecycle sync (`data/posthog_lifecycle_sync.py`), which runs
+ after every write that can change the status, queues the fields here;
+- the field backfill (`mailerlite_field_backfill.py`), which also catches up
+ anyone a live write missed. The daily lifecycle sweep does not write
+ MailerLite: one read and one write per account would use up MailerLite's
+ rate limit every day for changes the live path already made.
+
+MailerLite holds checkout openers, so only an account with a Stripe customer
+is queued, and the consumer writes only someone MailerLite already has
+(`mailerlite.record_lifecycle`). Nothing is queued for an account that opted
+out of marketing or that a signal places in Iran or Russia (`consent.py`).
+"""
+
+import logging
+from datetime import datetime
+
+from pydantic import BaseModel
+
+from backend.data.db_accessors import posthog_lifecycle_db
+from backend.data.notifications import AudienceAction, SubscriberField
+from backend.data.posthog_lifecycle import LifecycleSnapshot
+from backend.notifications.consent import audience_change_allowed
+from backend.notifications.subscriber_fields import (
+ Fields,
+ mailerlite_date,
+ queue_fields,
+)
+from backend.util.settings import Settings
+
+logger = logging.getLogger(__name__)
+settings = Settings()
+
+
+class LifecycleContact(BaseModel):
+ """The account a lifecycle sync is for, as consent reads it."""
+
+ id: str
+ email: str
+ timezone: str | None = None
+ marketing_opt_out_at: datetime | None = None
+ # The Iranian or Russian country a checkout recorded (`consent.py`).
+ excluded_country: str | None = None
+ stripe_customer_id: str | None = None
+
+
+def lifecycle_fields(snapshot: LifecycleSnapshot) -> Fields:
+ """The snapshot as MailerLite fields: the status, and each date as a day.
+ A date that doesn't apply is cleared, so none from an earlier state
+ lingers."""
+ return {
+ SubscriberField.STATUS: snapshot.subscription_status,
+ SubscriberField.SIGNUP: _day(snapshot.signup_at),
+ SubscriberField.TRIAL_STARTED: _day(snapshot.trial_started_at),
+ SubscriberField.SUBSCRIPTION_STARTED: _day(snapshot.subscription_started_at),
+ SubscriberField.SUBSCRIPTION_CANCELED: _day(snapshot.subscription_canceled_at),
+ SubscriberField.SUBSCRIPTION_ENDED: _day(snapshot.subscription_ended_at),
+ }
+
+
+def mailerlite_lifecycle_enabled() -> bool:
+ """MailerLite's half of a sync can have someone to write: it holds
+ checkout openers, and only a stack with Stripe has any. Whether MailerLite
+ itself is on is not asked here: only the notification service holds its
+ settings, and it drops the change when MailerLite is off."""
+ return bool(settings.secrets.stripe_api_key)
+
+
+async def queue_lifecycle_fields(
+ contact: LifecycleContact, snapshot: LifecycleSnapshot
+) -> None:
+ """Queue the snapshot's fields for MailerLite. Reports rather than
+ raises: it rides on a sync that must never fail its caller."""
+ if not contact.stripe_customer_id:
+ return
+ if not audience_change_allowed(
+ contact, AudienceAction.LIFECYCLE_FIELDS, (contact.excluded_country,)
+ ):
+ return
+ await queue_fields(
+ contact.id,
+ contact.email,
+ lifecycle_fields(snapshot),
+ AudienceAction.LIFECYCLE_FIELDS,
+ )
+
+
+async def resync_after_group_change(user_id: str) -> None:
+ """Ask for one more lifecycle sync after a group change that can create
+ the subscriber (joining the tour, the changelog or the trial group). The
+ sync for the same transition can reach MailerLite first and find nobody
+ to write, as when the checkout openers group is off; this one queues its
+ fields behind the group change, on the same one-at-a-time queue. Never
+ raises: the group change is already queued."""
+ try:
+ await posthog_lifecycle_db().request_posthog_lifecycle_sync(user_id)
+ except Exception:
+ logger.warning(
+ f"Could not request a lifecycle sync for user {user_id}", exc_info=True
+ )
+
+
+def _day(moment: datetime | None) -> str | None:
+ return mailerlite_date(moment) if moment else None
diff --git a/autogpt_platform/backend/backend/notifications/lifecycle_audience_test.py b/autogpt_platform/backend/backend/notifications/lifecycle_audience_test.py
new file mode 100644
index 0000000000..55919dc6bf
--- /dev/null
+++ b/autogpt_platform/backend/backend/notifications/lifecycle_audience_test.py
@@ -0,0 +1,282 @@
+"""MailerLite gets the same subscription status and dates as PostHog, from the
+same snapshot (SECRT-2795): written only into someone MailerLite already
+holds, and only the fields that differ."""
+
+from datetime import datetime, timedelta
+from types import SimpleNamespace
+from typing import get_args
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from backend.data.notifications import AudienceAction
+from backend.data.notifications import SubscriberField as F
+from backend.data.notifications import SubscriptionStatus
+from backend.data.posthog_lifecycle import LifecycleSnapshot, LifecycleStatus
+from backend.data.posthog_lifecycle_test import (
+ NOW,
+ SIGNUP,
+ TRIAL_START,
+ coupon,
+ snapshot,
+ sub,
+ trial,
+ user,
+)
+from backend.notifications import lifecycle_audience, mailerlite, subscriber_fields
+from backend.notifications.lifecycle_audience import (
+ LifecycleContact,
+ lifecycle_fields,
+ mailerlite_lifecycle_enabled,
+ queue_lifecycle_fields,
+ resync_after_group_change,
+)
+from backend.notifications.notifications import NotificationManager
+
+
+
+def _day(moment: datetime) -> str:
+ return moment.strftime("%Y-%m-%d")
+
+
+def _contact(**overrides) -> LifecycleContact:
+ values = {"id": "user-1", "email": EMAIL, "stripe_customer_id": "cus_1"}
+ return LifecycleContact(**{**values, **overrides})
+
+
+def test_every_lifecycle_status_is_a_mailerlite_status():
+ """GTM segments on these values: one list, both sinks."""
+ assert set(get_args(LifecycleStatus)) == {s.value for s in SubscriptionStatus}
+
+
+def test_the_snapshot_becomes_the_status_and_its_days():
+ started = NOW - timedelta(days=40)
+ canceled = NOW - timedelta(days=3)
+ result = lifecycle_fields(
+ LifecycleSnapshot(
+ subscription_status="subscription_canceled",
+ signup_at=SIGNUP,
+ trial_started_at=TRIAL_START,
+ subscription_started_at=started,
+ subscription_canceled_at=canceled,
+ )
+ )
+ assert result == {
+ F.STATUS: "subscription_canceled",
+ F.SIGNUP: _day(SIGNUP),
+ F.TRIAL_STARTED: _day(TRIAL_START),
+ F.SUBSCRIPTION_STARTED: _day(started),
+ F.SUBSCRIPTION_CANCELED: _day(canceled),
+ # A date that doesn't apply is cleared, so none lingers.
+ F.SUBSCRIPTION_ENDED: None,
+ }
+
+
+# ---- the four SECRT-2795 rules, as MailerLite receives them ------------------
+
+
+def test_an_enterprise_user_without_a_paid_subscription_is_signed():
+ # The mapping never reads the tier, so an admin grant is just a signup.
+ fields = lifecycle_fields(snapshot(u=user(customer=None)))
+ assert fields[F.STATUS] == "signed"
+ assert fields[F.SUBSCRIPTION_STARTED] is None
+
+
+def test_a_fully_discounted_subscription_is_not_subscribed():
+ fields = lifecycle_fields(snapshot(subs=[sub(discounts=[coupon(percent_off=100)])]))
+ assert fields[F.STATUS] == "signed"
+ assert fields[F.SUBSCRIPTION_STARTED] is None
+
+
+def test_a_trial_that_ran_out_has_expired():
+ fields = lifecycle_fields(snapshot(t=trial(ends_at=NOW - timedelta(minutes=1))))
+ assert fields[F.STATUS] == "trial_expired"
+ assert fields[F.TRIAL_STARTED] == _day(TRIAL_START)
+
+
+def test_the_subscription_start_is_the_first_paid_one():
+ first = NOW - timedelta(days=200)
+ old = sub("sub_old", "canceled", start=first, ended_at=NOW - timedelta(days=90))
+ fields = lifecycle_fields(snapshot(subs=[old, sub("sub_new")]))
+ assert fields[F.STATUS] == "subscribed"
+ assert fields[F.SUBSCRIPTION_STARTED] == _day(first)
+
+
+# ---- queueing ---------------------------------------------------------------
+
+
+async def test_the_fields_are_queued_as_a_lifecycle_change():
+ queued = AsyncMock(return_value=SimpleNamespace(success=True, message=""))
+ result = snapshot(subs=[sub()])
+ with patch("backend.notifications.subscriber_fields.queue_audience_change", queued):
+ await queue_lifecycle_fields(_contact(), result)
+ [event] = [call.args[0] for call in queued.await_args_list]
+ assert event.action is AudienceAction.LIFECYCLE_FIELDS
+ assert event.user_id == "user-1"
+ assert event.fields == lifecycle_fields(result)
+
+
+ "contact",
+ [
+ # Never reached checkout, so not a MailerLite subscriber.
+ {"stripe_customer_id": None},
+ {"marketing_opt_out_at": NOW},
+ {"excluded_country": "IR"},
+ {"timezone": "Europe/Moscow"},
+ ],
+)
+async def test_nothing_is_queued_for_someone_mailerlite_must_not_hold(contact):
+ queued = AsyncMock()
+ with patch("backend.notifications.subscriber_fields.queue_audience_change", queued):
+ await queue_lifecycle_fields(_contact(**contact), snapshot())
+ queued.assert_not_awaited()
+
+
+async def test_a_queue_failure_is_reported_not_raised():
+ queued = AsyncMock(side_effect=RuntimeError("rabbit down"))
+ with patch("backend.notifications.subscriber_fields.queue_audience_change", queued):
+ await queue_lifecycle_fields(_contact(), snapshot())
+
+
+# ---- the consumer -----------------------------------------------------------
+
+
+ "stripe_key, group_id, expected",
+ [
+ # The API process: Stripe, but no MailerLite settings at all.
+ ("sk_test", "", True),
+ ("", "grp_checkout", False),
+ ("", "", False),
+ ],
+)
+def test_mailerlite_s_half_runs_wherever_stripe_does(
+ monkeypatch, stripe_key, group_id, expected
+):
+ """Only the notification service holds the MailerLite settings, so the
+ process that queues the fields can't ask for them; a stack without
+ Stripe has no checkout openers to write."""
+ settings = SimpleNamespace(
+ secrets=SimpleNamespace(stripe_api_key=stripe_key),
+ config=SimpleNamespace(mailerlite_checkout_group_id=group_id),
+ )
+ monkeypatch.setattr(lifecycle_audience, "settings", settings)
+ assert mailerlite_lifecycle_enabled() is expected
+
+
+async def test_a_group_join_asks_for_one_more_sync(lifecycle_resync):
+ await resync_after_group_change("user-1")
+ lifecycle_resync.assert_awaited_once_with("user-1")
+
+
+async def test_a_failed_resync_request_is_reported_not_raised(lifecycle_resync, caplog):
+ lifecycle_resync.side_effect = RuntimeError("database manager down")
+ await resync_after_group_change("user-1")
+ assert "Could not request a lifecycle sync for user user-1" in caplog.text
+
+
+def mailerlite_configured(monkeypatch):
+ fake = SimpleNamespace(
+ config=SimpleNamespace(
+ mailerlite_checkout_group_id="grp_checkout",
+ mailerlite_trial_group_id="",
+ ),
+ secrets=SimpleNamespace(mailerlite_api_token="token"),
+ )
+ monkeypatch.setattr(mailerlite, "settings", fake)
+ monkeypatch.setattr(mailerlite, "_fields_ready", True)
+
+
+def _response(status: int, body: dict | None = None) -> MagicMock:
+ response = MagicMock(status=status)
+ response.json.return_value = body or {}
+ return response
+
+
+class _FakeMailerLite:
+ def __init__(self, held: dict[str, dict] | None = None):
+ self.subscribers = held or {}
+ self.posts: list[dict] = []
+
+ async def get(self, url: str, **_) -> MagicMock:
+ 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)
+ self.subscribers.setdefault(json["email"], {}).update(json.get("fields") or {})
+ return _response(200)
+
+
+async def _consume(fields: subscriber_fields.Fields) -> None:
+ event = subscriber_fields.audience_event(
+ AudienceAction.LIFECYCLE_FIELDS, EMAIL, "user-1", fields
+ )
+ assert event is not None
+ with patch(
+ "backend.notifications.notifications._kept_out_since_queued",
+ AsyncMock(return_value=False),
+ ):
+ assert await NotificationManager._process_audience_change(
+ MagicMock(), event.model_dump_json()
+ )
+
+
+async def test_only_the_fields_that_differ_are_written(mailerlite_configured):
+ held = {
+ "subscription_status": "subscribed",
+ # MailerLite may hand a date back with a time part.
+ "signup_date": "2026-07-01 00:00:00",
+ "subscription_started_date": "2026-07-05",
+ "checkout_opened_date": "2026-07-01",
+ }
+ ml = _FakeMailerLite({EMAIL: held})
+ fields = {
+ F.STATUS: "signed",
+ F.SIGNUP: "2026-07-01",
+ F.SUBSCRIPTION_STARTED: None,
+ F.SUBSCRIPTION_ENDED: None,
+ }
+ with patch.object(mailerlite, "_client", return_value=ml):
+ await _consume(fields)
+ assert ml.posts == [
+ {
+ "email": EMAIL,
+ "fields": {
+ "subscription_status": "signed",
+ "subscription_started_date": None,
+ },
+ }
+ ]
+ assert ml.subscribers[EMAIL]["checkout_opened_date"] == "2026-07-01"
+
+
+async def test_nothing_is_written_when_mailerlite_already_agrees(
+ mailerlite_configured,
+):
+ ml = _FakeMailerLite({EMAIL: {"subscription_status": "signed"}})
+ with patch.object(mailerlite, "_client", return_value=ml):
+ await _consume({F.STATUS: "signed", F.SUBSCRIPTION_ENDED: None})
+ assert ml.posts == []
+
+
+async def test_the_status_never_creates_a_subscriber(mailerlite_configured):
+ """MailerLite holds checkout openers; a Stripe customer alone is not one."""
+ ml = _FakeMailerLite()
+ with patch.object(mailerlite, "_client", return_value=ml):
+ await _consume({F.STATUS: "subscribed"})
+ assert ml.posts == []
+ assert EMAIL not in ml.subscribers
diff --git a/autogpt_platform/backend/backend/notifications/lifecycle_test.py b/autogpt_platform/backend/backend/notifications/lifecycle_test.py
index 0be7d6852b..e6597b6a1b 100644
--- a/autogpt_platform/backend/backend/notifications/lifecycle_test.py
+++ b/autogpt_platform/backend/backend/notifications/lifecycle_test.py
@@ -17,9 +17,7 @@ from backend.data.credit import PAYMENT_FAILURE_CANCELLATION_COMMENT
from backend.data.notifications import (
AudienceAction,
NotificationResult,
- SubscriberField,
SubscriptionPlan,
- SubscriptionStatus,
)
from backend.notifications import lifecycle, lifecycle_plan, subscriber_fields
from backend.notifications.lifecycle_plan import card_from_invoice
@@ -546,6 +544,10 @@ async def test_a_failed_welcome_publish_gives_the_claim_back():
# ── subscriber fields ──────────────────────────────────────────────────────
+#
+# The status and dates are not written from these events: the lifecycle sync
+# each subscription webhook triggers writes them, by PostHog's rules
+# (`lifecycle_audience.py`), so a 100% coupon checkout is not `subscribed`.
@pytest.fixture
@@ -555,14 +557,10 @@ def fields_on(monkeypatch):
return queued
-def _day(timestamp: int) -> str:
- return datetime.fromtimestamp(timestamp, timezone.utc).strftime("%Y-%m-%d")
-
-
@pytest.mark.asyncio
@pytest.mark.parametrize("welcomed", [False, True])
-async def test_a_checkout_marks_them_subscribed_from_stripes_start_date(
- fields_on, welcomed
+async def test_a_checkout_joins_its_group_without_a_status(
+ fields_on, lifecycle_resync, welcomed
):
user = _User(welcome_sent_at=datetime(2026, 1, 1) if welcomed else None)
with patch(
@@ -575,88 +573,51 @@ async def test_a_checkout_marks_them_subscribed_from_stripes_start_date(
),
user,
)
- assert calls["audience"].await_args.args[0].fields == {
- SubscriberField.STATUS: SubscriptionStatus.SUBSCRIBED.value,
- SubscriberField.SUBSCRIPTION_STARTED: _day(1788000000),
- SubscriberField.SUBSCRIPTION_CANCELED: None,
- SubscriberField.SUBSCRIPTION_ENDED: None,
- }
+ event = calls["audience"].await_args.args[0]
+ assert event.action in (AudienceAction.ENROLL_TOUR, AudienceAction.ADD_CHANGELOG)
+ assert event.fields == {}
+ # The join may create the subscriber, so the status follows it.
+ lifecycle_resync.assert_awaited_once_with(user.id)
@pytest.mark.asyncio
-async def test_a_paid_cancellation_sets_the_status_and_stripes_cancel_date(
- fields_on,
-):
- await _run(
- lambda: lifecycle.on_subscription_updated(
- _subscription(cancel_at_period_end=True, canceled_at=1789100000),
+ "subscription, previous",
+ [
+ (
+ {"cancel_at_period_end": True, "canceled_at": 1789100000},
{"cancel_at_period_end": False},
),
- _User(),
- )
- event = fields_on.await_args.args[0]
- assert event.action is AudienceAction.UPDATE_FIELDS
- assert event.fields == {
- SubscriberField.STATUS: SubscriptionStatus.SUBSCRIPTION_CANCELED.value,
- SubscriberField.SUBSCRIPTION_CANCELED: _day(1789100000),
- }
-
-
-async def test_resuming_a_paid_subscription_clears_the_cancel_date(fields_on):
- await _run(
- lambda: lifecycle.on_subscription_updated(
- _subscription(cancel_at_period_end=False),
+ (
+ {"cancel_at_period_end": False},
{"cancel_at_period_end": True, "canceled_at": 1789100000},
),
- _User(),
- )
- assert fields_on.await_args.args[0].fields == {
- SubscriberField.STATUS: SubscriptionStatus.SUBSCRIBED.value,
- SubscriberField.SUBSCRIPTION_CANCELED: None,
- }
-
-
-async def test_a_replayed_cancellation_writes_no_fields(fields_on):
- await _run(
+ ],
+)
+async def test_a_cancel_or_resume_writes_no_fields(fields_on, subscription, previous):
+ calls = await _run(
lambda: lifecycle.on_subscription_updated(
- _subscription(cancel_at_period_end=True, canceled_at=1789100000),
- {"cancel_at_period_end": False},
+ _subscription(**subscription), previous
),
_User(),
- claim=False,
)
- fields_on.assert_not_awaited()
-
-
-async def test_a_failed_field_update_never_fails_the_cancellation(fields_on, caplog):
- fields_on.return_value = NotificationResult(success=False, message="down")
- with caplog.at_level(logging.ERROR):
- calls = await _run(
- lambda: lifecycle.on_subscription_updated(
- _subscription(cancel_at_period_end=True, canceled_at=1789100000),
- {"cancel_at_period_end": False},
- ),
- _User(),
- )
calls["notify"].assert_awaited_once()
- assert "MailerLite fields" in caplog.text
+ fields_on.assert_not_awaited()
@pytest.mark.asyncio
-async def test_an_ended_subscription_sets_stripes_end_date_on_the_churn(fields_on):
+async def test_an_ended_subscription_leaves_the_changelog_without_a_status(
+ fields_on, lifecycle_resync
+):
calls = await _run(
lambda: lifecycle.on_subscription_deleted(_subscription(ended_at=1789200000)),
_User(),
)
event = calls["audience"].await_args.args[0]
assert event.action is AudienceAction.REMOVE_CHANGELOG
- assert event.fields == {
- SubscriberField.STATUS: SubscriptionStatus.SUBSCRIPTION_ENDED.value,
- SubscriberField.SUBSCRIPTION_ENDED: _day(1789200000),
- }
+ assert event.fields == {}
+ # A removal never creates a subscriber: no extra sync.
+ lifecycle_resync.assert_not_awaited()
@pytest.mark.parametrize(
diff --git a/autogpt_platform/backend/backend/notifications/mailerlite.py b/autogpt_platform/backend/backend/notifications/mailerlite.py
index 5267e40364..55fe7965b2 100644
--- a/autogpt_platform/backend/backend/notifications/mailerlite.py
+++ b/autogpt_platform/backend/backend/notifications/mailerlite.py
@@ -96,6 +96,15 @@ def field_type(field: SubscriberField) -> str:
return FIELD_TYPES.get(field) or BUILT_IN_FIELD_TYPES[field]
+def held_value(field: SubscriberField, value: object) -> str | None:
+ """A field as MailerLite handed it back, in the form we write it: empty
+ is None, and a date loses the time part it may come back with."""
+ if value is None or value == "":
+ return None
+ text = str(value)
+ return text[:10] if field_type(field) == "date" else text
+
+
Fields = Mapping[SubscriberField, str | None]
# Set once this process has seen every field exist, so it is checked once,
@@ -215,6 +224,33 @@ async def record_onboarding_profile(email: str, fields: Fields | None = None) ->
logger.info(f"Wrote the onboarding profile of {pseudonym(email)} to MailerLite")
+async def record_lifecycle(email: str, fields: Fields | None = None) -> None:
+ """The subscription status and its dates, from the lifecycle snapshot
+ (`lifecycle_audience`). They fill in someone MailerLite already has and
+ never create a subscriber: MailerLite holds checkout openers, and having
+ a subscription does not make someone one. Only the fields that differ
+ from MailerLite's are written, so a sync that changed nothing costs one
+ read. 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()
+ subscriber = await _find_subscriber(email)
+ if subscriber is None:
+ logger.info(
+ f"No MailerLite subscriber for {pseudonym(email)}; "
+ "subscription status not written"
+ )
+ return
+ held = subscriber.get("fields") or {}
+ changed = {
+ field: value
+ for field, value in fields.items()
+ if held_value(field, held.get(field.value)) != value
+ }
+ await update_fields(email, changed)
+
+
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
diff --git a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.py b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.py
index 25bb803af4..4e55bf3c33 100644
--- a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.py
+++ b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill.py
@@ -1,8 +1,9 @@
"""Give accounts the MailerLite status and dates the live code would have.
-Signups were never synced and the lifecycle handlers only react to new
-events, so this works out each account's fields from scratch: the signup date
-from our database, the rest from Stripe, by the rules in `subscriber_fields.py`.
+Signups were never synced and the live writes only react to new events, so
+this works out each account's fields from scratch, from our database (the
+signup and the trial row) and Stripe, by the same mapping PostHog's status
+comes from (`data/posthog_lifecycle.py`, via `lifecycle_audience`).
Only accounts with a Stripe customer are given (see `cli/mailerlite_backfill`):
MailerLite holds checkout openers, not every signup. An account that opted out
of marketing, or that a signal places in Iran or Russia, is never written,
@@ -16,21 +17,25 @@ where the last one stopped.
import asyncio
import logging
from collections.abc import Callable
-from datetime import datetime
+from datetime import UTC, datetime
from urllib.parse import urlencode
from pydantic import BaseModel, EmailStr, TypeAdapter, ValidationError
from backend.data.notifications import SubscriberField, SubscriptionStatus
+from backend.data.posthog_lifecycle import LifecycleUser, compute_lifecycle_snapshot
+from backend.data.stripe_subscription_facts import StripeSubscriptionFacts
+from backend.data.subscription_trial import TrialState
from backend.notifications.audience_enrichment import points_at_excluded_country
from backend.notifications.consent import KeptOut, marketing_allowed
+from backend.notifications.lifecycle_audience import lifecycle_fields
from backend.notifications.mailerlite import (
API_BASE,
MailerLiteError,
_client,
_headers,
_require_token,
- field_type,
+ held_value,
)
from backend.notifications.mailerlite_backfill import (
BATCH_SIZE,
@@ -38,29 +43,29 @@ from backend.notifications.mailerlite_backfill import (
PAGE_SIZE,
UPSERT_BATCH_INTERVAL_SECONDS,
BatchAnswer,
- Subscription,
_refusal,
_send_batch,
next_cursor,
)
-from backend.notifications.subscriber_fields import Fields, mailerlite_date
+from backend.notifications.subscriber_fields import Fields
logger = logging.getLogger(__name__)
_EMAIL = TypeAdapter(EmailStr)
-_NEVER_STARTED = ("incomplete", "incomplete_expired", "trialing")
# Email → our fields as MailerLite holds them.
Current = dict[str, dict[str, str | None]]
class Person(BaseModel):
- """A platform account and its Stripe subscriptions, if any."""
+ """A platform account, its Stripe subscriptions and its trial, if any."""
user_id: str
email: str
created_at: datetime
- subscriptions: list[Subscription] = []
+ # Listed with the discounts expanded (`stripe_subscription_facts`).
+ subscriptions: list[StripeSubscriptionFacts] = []
+ trial: TrialState | None = None
stripe_customer_id: str | None = None
# The browser's IANA timezone, for the checkout opener's country.
timezone: str | None = None
@@ -99,54 +104,26 @@ class FieldPlan(BaseModel):
opted_out: int
# People a signal places in Iran or Russia, left out the same way.
excluded_country: int
-
-
-def standing(subscriptions: list[Subscription]) -> tuple[SubscriptionStatus, Fields]:
- """The status and every date but signup. A paid subscription outranks a
- trial, and a live one outranks one that ended."""
- trials = [s for s in subscriptions if s.from_trial]
- trial_start = max((s.trial_start for s in trials if s.trial_start), default=None)
- dates: Fields = {
- SubscriberField.TRIAL_STARTED: _day(trial_start),
- SubscriberField.SUBSCRIPTION_STARTED: None,
- SubscriberField.SUBSCRIPTION_CANCELED: None,
- SubscriberField.SUBSCRIPTION_ENDED: None,
- }
- paid = [s for s in subscriptions if _ever_paid(s)]
- live = [s for s in paid if s.status in ("active", "past_due")]
- if live:
- current = _latest(live)
- dates[SubscriberField.SUBSCRIPTION_STARTED] = _day(_started(current))
- if current.cancel_at_period_end:
- dates[SubscriberField.SUBSCRIPTION_CANCELED] = _day(current.canceled_at)
- return SubscriptionStatus.SUBSCRIPTION_CANCELED, dates
- return SubscriptionStatus.SUBSCRIBED, dates
- trialing = [s for s in subscriptions if s.status == "trialing"]
- if any(not s.cancel_at_period_end for s in trialing):
- return SubscriptionStatus.IN_TRIAL, dates
- if trialing:
- return SubscriptionStatus.TRIAL_CANCELED, dates
- if paid:
- last = _latest(paid)
- dates[SubscriberField.SUBSCRIPTION_STARTED] = _day(_started(last))
- if last.cancel_at_period_end:
- dates[SubscriberField.SUBSCRIPTION_CANCELED] = _day(last.canceled_at)
- dates[SubscriberField.SUBSCRIPTION_ENDED] = _day(last.ended_at)
- return SubscriptionStatus.SUBSCRIPTION_ENDED, dates
- if trials:
- # Ended, or failed its first payment, without ever converting.
- return SubscriptionStatus.TRIAL_CANCELED, dates
- return SubscriptionStatus.SIGNED, dates
-
-
-def desired(person: Person) -> tuple[SubscriptionStatus, Fields]:
- """All six fields, so a run leaves nothing stale behind."""
- status, dates = standing(person.subscriptions)
- return status, {
- SubscriberField.STATUS: status.value,
- SubscriberField.SIGNUP: mailerlite_date(person.created_at),
- **dates,
- }
+ # Without create, people MailerLite doesn't hold: left out the same way.
+ not_held: int = 0
+
+
+def desired(
+ person: Person, now: datetime | None = None
+) -> tuple[SubscriptionStatus, Fields]:
+ """All six fields, so a run leaves nothing stale behind: the status and
+ dates of the lifecycle snapshot, as the live sync writes them."""
+ snapshot = compute_lifecycle_snapshot(
+ user=LifecycleUser(
+ id=person.user_id,
+ created_at=person.created_at,
+ stripe_customer_id=person.stripe_customer_id,
+ ),
+ trial=person.trial,
+ subscriptions=person.subscriptions,
+ now=now or datetime.now(UTC),
+ )
+ return SubscriptionStatus(snapshot.subscription_status), lifecycle_fields(snapshot)
def plan(people: list[Person], current: Current, *, create: bool = True) -> FieldPlan:
@@ -171,15 +148,16 @@ def plan(people: list[Person], current: Current, *, create: bool = True) -> Fiel
if not _valid(person.email):
result.invalid += 1
continue
- status, fields = desired(person)
- result.statuses[status] += 1
held = current.get(person.email.strip().lower())
if held is None and not create:
+ result.not_held += 1
continue
+ status, fields = desired(person)
+ result.statuses[status] += 1
differ = {
key: value
for key, value in fields.items()
- if held is None or _normalise(key, held.get(key.value)) != value
+ if held is None or held_value(key, held.get(key.value)) != value
}
if differ:
result.changes.append(
@@ -286,37 +264,6 @@ def _upsert(change: FieldChange) -> dict:
}
-def _ever_paid(subscription: Subscription) -> bool:
- if subscription.from_trial:
- return subscription.converted or subscription.status == "active"
- return subscription.status not in _NEVER_STARTED
-
-
-def _started(subscription: Subscription) -> int | None:
- """A converted trial's paid subscription began when its trial ended."""
- if subscription.from_trial:
- return subscription.trial_end
- return subscription.start_date
-
-
-def _latest(subscriptions: list[Subscription]) -> Subscription:
- return max(subscriptions, key=lambda s: _started(s) or 0)
-
-
-def _day(timestamp: int | None) -> str | None:
- """Unlike a live event, a missing timestamp here is no reason to write
- today's date."""
- return mailerlite_date(timestamp) if timestamp is not None else None
-
-
-def _normalise(field: SubscriberField, value: object) -> str | None:
- if value is None or value == "":
- return None
- text = str(value)
- # A date may come back with a time part.
- return text[:10] if field_type(field) == "date" else text
-
-
def _valid(email: str) -> bool:
try:
_EMAIL.validate_python(email)
diff --git a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.py b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.py
index d346d78847..b74a616ac7 100644
--- a/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.py
+++ b/autogpt_platform/backend/backend/notifications/mailerlite_field_backfill_test.py
@@ -2,16 +2,19 @@
would have, writes only what MailerLite does not already hold, and so resumes
an interrupted run by simply running again."""
-from datetime import UTC, datetime
+from datetime import UTC, datetime, timedelta
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
from backend.data.notifications import SubscriberField, SubscriptionStatus
+from backend.data.posthog_lifecycle_test import NOW as LIFECYCLE_NOW
+from backend.data.posthog_lifecycle_test import TRIAL_START, by_request, coupon, trial
+from backend.data.stripe_subscription_facts import StripeSubscriptionFacts
+from backend.data.subscription_trial import TrialState
from backend.notifications import mailerlite, mailerlite_backfill
from backend.notifications import mailerlite_field_backfill as backfill
-from backend.notifications.mailerlite_backfill import Subscription
from backend.notifications.mailerlite_field_backfill import Person
S = SubscriptionStatus
@@ -25,22 +28,42 @@ def _day(timestamp: int) -> str:
return datetime.fromtimestamp(timestamp, UTC).strftime("%Y-%m-%d")
-def _paid(status: str, **over) -> Subscription:
- return Subscription(status=status, start_date=AUG, **over)
+def _sub(status: str, sub_id: str = "sub_1", **stripe) -> StripeSubscriptionFacts:
+ """A Stripe subscription as the backfill lists it."""
+ return StripeSubscriptionFacts.model_validate(
+ {
+ "id": sub_id,
+ "customer": "cus_1",
+ "status": status,
+ "start_date": AUG,
+ "items": {"data": [{"price": {"unit_amount": 5000}, "quantity": 1}]},
+ **stripe,
+ }
+ )
+
+def _paid(status: str, **stripe) -> StripeSubscriptionFacts:
+ return _sub(status, **stripe)
-def _trial(status: str, **over) -> Subscription:
- return Subscription(
- status=status, from_trial=True, trial_start=AUG, trial_end=SEP, **over
+
+def _native_trial(status: str, **stripe) -> StripeSubscriptionFacts:
+ return _sub(
+ status, "sub_native", **{"trial_start": AUG, "trial_end": SEP, **stripe}
)
-def _person(email: str = "[email protected]", *subscriptions: Subscription) -> Person:
+def _person(
+ email: str = "[email protected]",
+ *subscriptions: StripeSubscriptionFacts,
+ trial: TrialState | None = None,
+) -> Person:
return Person(
user_id=f"u-{email}",
email=email,
created_at=CREATED,
+ stripe_customer_id="cus_1",
subscriptions=list(subscriptions),
+ trial=trial,
)
@@ -51,24 +74,83 @@ def _person(email: str = "[email protected]", *subscriptions: Subscription) -> Person:
# Checkout never finished: nothing started.
([_paid("incomplete_expired")], S.SIGNED),
([_paid("active")], S.SUBSCRIBED),
- ([_paid("past_due")], S.SUBSCRIBED),
+ # A failed payment is not a paying subscriber (PostHog says the same).
+ ([_paid("past_due")], S.PAYMENT_FAILED),
+ ([_paid("unpaid")], S.PAYMENT_FAILED),
([_paid("active", cancel_at_period_end=True)], S.SUBSCRIPTION_CANCELED),
([_paid("canceled")], S.SUBSCRIPTION_ENDED),
- ([_paid("unpaid")], S.SUBSCRIPTION_ENDED),
- ([_trial("trialing")], S.IN_TRIAL),
- ([_trial("trialing", cancel_at_period_end=True)], S.TRIAL_CANCELED),
- # Its first payment never cleared.
- ([_trial("past_due")], S.TRIAL_CANCELED),
- ([_trial("canceled")], S.TRIAL_CANCELED),
- ([_trial("active")], S.SUBSCRIBED),
- ([_trial("canceled", converted=True)], S.SUBSCRIPTION_ENDED),
+ ([_native_trial("trialing", trial_end=SEP20 * 2)], S.IN_TRIAL),
+ (
+ [_native_trial("trialing", trial_end=SEP20 * 2, cancel_at_period_end=True)],
+ S.TRIAL_CANCELED,
+ ),
+ # Native trials that ended before their first charge.
+ ([_native_trial("canceled", ended_at=SEP, **by_request())], S.TRIAL_CANCELED),
+ (
+ [
+ _native_trial(
+ "canceled",
+ ended_at=SEP,
+ cancellation_details={"reason": "payment_failed"},
+ )
+ ],
+ S.TRIAL_EXPIRED,
+ ),
# A paid subscription outranks a trial, and a live one an ended one.
- ([_paid("canceled"), _trial("trialing")], S.IN_TRIAL),
- ([_trial("canceled"), _paid("active")], S.SUBSCRIBED),
+ ([_native_trial("canceled"), _paid("active")], S.SUBSCRIBED),
],
)
-def test_standing(subscriptions, status):
- assert backfill.standing(subscriptions)[0] is status
+def test_the_status_is_the_shared_lifecycle_status(subscriptions, status):
+ assert backfill.desired(_person("[email protected]", *subscriptions))[0] is status
+
+
+# ---- the SECRT-2795 rules, as the backfill writes them ----------------------
+
+
+def test_a_tier_without_a_paid_subscription_is_signed():
+ # An admin-granted ENTERPRISE plan has no Stripe subscription; the tier is
+ # never read.
+ assert backfill.desired(_person("[email protected]"))[0] is S.SIGNED
+
+
+ "discounts, status",
+ [
+ ([coupon(percent_off=100)], S.SIGNED),
+ ([coupon(amount_off=5000)], S.SIGNED),
+ ([coupon(percent_off=50)], S.SUBSCRIBED),
+ ],
+)
+def test_only_a_subscription_that_charges_something_is_subscribed(discounts, status):
+ person = _person("[email protected]", _paid("active", discounts=discounts))
+ status_, fields = backfill.desired(person)
+ assert status_ is status
+ if status is S.SIGNED:
+ assert fields[F.SUBSCRIPTION_STARTED] is None
+
+
+def test_a_trial_that_ran_out_has_expired():
+ ran_out = trial(ends_at=LIFECYCLE_NOW - timedelta(days=1))
+ status, fields = backfill.desired(
+ _person("[email protected]", trial=ran_out), now=LIFECYCLE_NOW
+ )
+ assert status is S.TRIAL_EXPIRED
+ assert fields[F.TRIAL_STARTED] == TRIAL_START.strftime("%Y-%m-%d")
+
+
+def test_a_trial_the_user_canceled_is_trial_canceled():
+ canceled = trial(
+ ends_at=LIFECYCLE_NOW - timedelta(days=1), cancel_at_period_end=True
+ )
+ status, _ = backfill.desired(_person("[email protected]", trial=canceled), now=LIFECYCLE_NOW)
+ assert status is S.TRIAL_CANCELED
+
+
+def test_the_subscription_start_is_the_first_paid_one():
+ first = _sub("canceled", "sub_old", start_date=AUG, ended_at=SEP)
+ again = _sub("active", "sub_new", start_date=SEP20)
+ _, fields = backfill.desired(_person("[email protected]", first, again))
+ assert fields[F.SUBSCRIPTION_STARTED] == _day(AUG)
def test_every_field_is_set_so_nothing_stale_survives():
@@ -84,28 +166,27 @@ def test_every_field_is_set_so_nothing_stale_survives():
}
-def test_the_dates_come_from_stripe():
- _, fields = backfill.desired(
- _person(
- _trial(
- "canceled",
- converted=True,
- cancel_at_period_end=True,
- canceled_at=SEP10,
- ended_at=SEP20,
- ),
- )
+def test_the_dates_come_from_stripe_and_the_trial_row():
+ converted = datetime.fromtimestamp(SEP, UTC)
+ ended_trial = trial(status="canceled", converted_at=converted)
+ gone = _sub(
+ "canceled",
+ "sub_trial",
+ metadata={"trial_enrollment_id": "trial-1"},
+ cancel_at_period_end=True,
+ canceled_at=SEP10,
+ ended_at=SEP20,
)
- assert fields[F.TRIAL_STARTED] == _day(AUG)
- # A converted trial's paid subscription began when the trial ended.
+ _, fields = backfill.desired(_person("[email protected]", gone, trial=ended_trial))
+ assert fields[F.TRIAL_STARTED] == TRIAL_START.strftime("%Y-%m-%d")
+ # A converted trial's paid subscription began at the conversion.
assert fields[F.SUBSCRIPTION_STARTED] == _day(SEP)
assert fields[F.SUBSCRIPTION_CANCELED] == _day(SEP10)
assert fields[F.SUBSCRIPTION_ENDED] == _day(SEP20)
def test_a_missing_timestamp_is_left_empty_not_today():
- _, fields = backfill.desired(_person("[email protected]", Subscription(status="canceled")))
+ _, fields = backfill.desired(_person("[email protected]", _paid("canceled")))
assert fields[F.SUBSCRIPTION_ENDED] is None
@@ -146,7 +227,10 @@ def test_an_address_mailerlite_would_refuse_is_counted_not_sent():
def test_a_second_run_finds_nothing_to_do():
- people = [_person("[email protected]"), _person("[email protected]", _trial("trialing"))]
+ people = [
+ _person("[email protected]"),
+ _person("[email protected]", _native_trial("trialing", trial_end=SEP20 * 2)),
+ ]
first = backfill.plan(people, {})
held = {
c.person.email: {k.value: v for k, v in c.fields.items()} for c in first.changes
@@ -315,6 +399,9 @@ def test_without_create_only_existing_subscribers_are_planned():
)
assert [c.person.email for c in plan.changes] == ["[email protected]"]
assert not any(c.new for c in plan.changes)
+ # The dry run's counts are of the people it would write.
+ assert sum(plan.statuses.values()) == 1
+ assert plan.not_held == 1
@pytest.mark.parametrize("create", [True, False])
diff --git a/autogpt_platform/backend/backend/notifications/notifications.py b/autogpt_platform/backend/backend/notifications/notifications.py
index 01bc5a526a..02780965c7 100644
--- a/autogpt_platform/backend/backend/notifications/notifications.py
+++ b/autogpt_platform/backend/backend/notifications/notifications.py
@@ -422,6 +422,7 @@ class NotificationManager(AppService):
AudienceAction.CHECKOUT_OPENED: mailerlite.record_checkout_opened,
AudienceAction.UNSUBSCRIBE: mailerlite.unsubscribe,
AudienceAction.ONBOARDING_PROFILE: mailerlite.record_onboarding_profile,
+ AudienceAction.LIFECYCLE_FIELDS: mailerlite.record_lifecycle,
}[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.py
index 49d98c6410..d6a2bdbb26 100644
--- a/autogpt_platform/backend/backend/notifications/subscriber_fields.py
+++ b/autogpt_platform/backend/backend/notifications/subscriber_fields.py
@@ -1,17 +1,10 @@
-"""Where each person stands with us, as MailerLite subscriber fields.
+"""Where each person stands with us, as MailerLite subscriber fields, and
+the queue that carries them.
GTM segments on one status and five dates (see `SubscriberField`), plus the
-checkout opener's segmentation, which `audience_enrichment` works out. Each
-transition below sets the status and the date it is named for, from Stripe's
-own timestamp where the event carries one. The dates describe the current or
-most recent subscription: a new subscription clears the previous one's
-cancellation and end, and a resumed one clears its cancellation. The signup
-and trial start dates are never cleared.
-
-A trial that ends without its first payment clearing, whether it was cancelled
-or the payment failed, is `trial_canceled`: the person never had a paid
-subscription, so `subscription_ended` would count them as churned customers.
-`in_trial` is therefore exactly the trial group's membership.
+checkout opener's segmentation, which `audience_enrichment` works out. The
+status and dates come from the lifecycle snapshot PostHog gets too
+(`lifecycle_audience`), so only a paying subscriber is `subscribed` in either.
"""
import logging
@@ -23,7 +16,6 @@ from backend.data.notifications import (
AudienceAction,
AudienceEventModel,
SubscriberField,
- SubscriptionStatus,
)
from backend.notifications.queue import queue_audience_change
@@ -32,12 +24,12 @@ logger = logging.getLogger(__name__)
Fields = dict[SubscriberField, str | None]
-def mailerlite_date(value: datetime | int | float | None) -> str:
+def mailerlite_date(value: datetime | float | None) -> str:
"""A Stripe timestamp or a datetime as a MailerLite date, in UTC."""
return utc_moment(value).strftime("%Y-%m-%d")
-def utc_moment(value: datetime | int | float | None) -> datetime:
+def utc_moment(value: datetime | float | None) -> datetime:
"""A Stripe timestamp or a datetime, in UTC. Only an event without a
timestamp of its own falls back to now."""
if value is None:
@@ -49,58 +41,6 @@ def utc_moment(value: datetime | int | float | None) -> datetime:
return moment.astimezone(UTC)
-def signed(created_at: datetime) -> Fields:
- return {
- SubscriberField.STATUS: SubscriptionStatus.SIGNED.value,
- SubscriberField.SIGNUP: mailerlite_date(created_at),
- }
-
-
-def trial_started(trial_start: int | None) -> Fields:
- return {
- SubscriberField.STATUS: SubscriptionStatus.IN_TRIAL.value,
- SubscriberField.TRIAL_STARTED: mailerlite_date(trial_start),
- }
-
-
-def trial_resumed() -> Fields:
- return {SubscriberField.STATUS: SubscriptionStatus.IN_TRIAL.value}
-
-
-def trial_canceled() -> Fields:
- return {SubscriberField.STATUS: SubscriptionStatus.TRIAL_CANCELED.value}
-
-
-def subscribed(started: int | None) -> Fields:
- return {
- SubscriberField.STATUS: SubscriptionStatus.SUBSCRIBED.value,
- SubscriberField.SUBSCRIPTION_STARTED: mailerlite_date(started),
- SubscriberField.SUBSCRIPTION_CANCELED: None,
- SubscriberField.SUBSCRIPTION_ENDED: None,
- }
-
-
-def subscription_resumed() -> Fields:
- return {
- SubscriberField.STATUS: SubscriptionStatus.SUBSCRIBED.value,
- SubscriberField.SUBSCRIPTION_CANCELED: None,
- }
-
-
-def subscription_canceled(canceled_at: int | None) -> Fields:
- return {
- SubscriberField.STATUS: SubscriptionStatus.SUBSCRIPTION_CANCELED.value,
- SubscriberField.SUBSCRIPTION_CANCELED: mailerlite_date(canceled_at),
- }
-
-
-def subscription_ended(ended_at: int | None) -> Fields:
- return {
- SubscriberField.STATUS: SubscriptionStatus.SUBSCRIPTION_ENDED.value,
- SubscriberField.SUBSCRIPTION_ENDED: mailerlite_date(ended_at),
- }
-
-
def audience_event(
action: AudienceAction, email: str, user_id: str, fields: Fields | None = None
) -> AudienceEventModel | None:
diff --git a/autogpt_platform/backend/backend/notifications/subscriber_fields_test.py b/autogpt_platform/backend/backend/notifications/subscriber_fields_test.py
index 525203fa04..ea037bcc75 100644
--- a/autogpt_platform/backend/backend/notifications/subscriber_fields_test.py
+++ b/autogpt_platform/backend/backend/notifications/subscriber_fields_test.py
@@ -46,16 +46,16 @@ def test_a_naive_datetime_is_read_as_utc():
)
-def test_a_new_subscription_clears_the_last_ones_cancellation_and_end():
- assert subscriber_fields.subscribed(1788305400) == {
- SubscriberField.STATUS: "subscribed",
- SubscriberField.SUBSCRIPTION_STARTED: "2026-09-01",
- SubscriberField.SUBSCRIPTION_CANCELED: None,
- SubscriberField.SUBSCRIPTION_ENDED: None,
- }
-
-
-SIGNED = subscriber_fields.signed(datetime(2026, 9, 29, 10, 0, tzinfo=UTC))
+SIGNED = {
+ SubscriberField.STATUS: SubscriptionStatus.SIGNED.value,
+ SubscriberField.SIGNUP: "2026-09-29",
+}
+SUBSCRIBED = {
+ SubscriberField.STATUS: SubscriptionStatus.SUBSCRIBED.value,
+ SubscriberField.SUBSCRIPTION_STARTED: "2026-09-01",
+ SubscriberField.SUBSCRIPTION_CANCELED: None,
+ SubscriberField.SUBSCRIPTION_ENDED: None,
+}
@pytest.mark.asyncio
@@ -433,7 +433,7 @@ def _checkout_event():
AudienceAction.ENROLL_TOUR,
EMAIL,
"user-1",
- subscriber_fields.subscribed(1788305400),
+ SUBSCRIBED,
)
@@ -495,9 +495,7 @@ async def test_a_missing_required_group_is_dead_lettered_at_once(
are written once and the problem is said once."""
mailerlite_configured.config.mailerlite_onboarding_group_id = ""
mailerlite_configured.config.mailerlite_changelog_group_id = ""
- event = subscriber_fields.audience_event(
- action, EMAIL, "user-1", subscriber_fields.subscribed(1788305400)
- )
+ event = subscriber_fields.audience_event(action, EMAIL, "user-1", SUBSCRIBED)
message = MagicMock(
body=event.model_dump_json().encode(), ack=AsyncMock(), reject=AsyncMock()
)
diff --git a/autogpt_platform/backend/backend/notifications/trial.py b/autogpt_platform/backend/backend/notifications/trial.py
index 1be190d6b0..9871b0bca1 100644
--- a/autogpt_platform/backend/backend/notifications/trial.py
+++ b/autogpt_platform/backend/backend/notifications/trial.py
@@ -97,7 +97,7 @@ async def notify_trial(subscription: dict, kind: TrialNoticeKind) -> bool:
if not await claim_once(claim):
return True
try:
- await queue_trial_audience_change(kind, user, current)
+ await queue_trial_audience_change(kind, user)
result = await queue_notification_async(
NotificationEventModel[TrialUpdateData](
user_id=user_id, type=NotificationType.TRIAL_UPDATE, data=data
diff --git a/autogpt_platform/backend/backend/notifications/trial_audience.py b/autogpt_platform/backend/backend/notifications/trial_audience.py
index ed1b8b14aa..96226342b6 100644
--- a/autogpt_platform/backend/backend/notifications/trial_audience.py
+++ b/autogpt_platform/backend/backend/notifications/trial_audience.py
@@ -10,9 +10,10 @@ the group on its own. The backend owns both edges of this group (see
A conversion is also the customer's first paid subscription, so it joins the
paying audience the way a first checkout does.
-The subscriber's status and dates (`subscriber_fields.py`) ride on the same
-event. The notification service writes only the fields while the trial group
-is not configured.
+The subscriber's status and dates are not written here: they come from the
+lifecycle sync every trial transition triggers (`lifecycle_audience.py`), by
+the same rules as PostHog's. Joining a group can create the subscriber after
+that sync found nobody to write, so a join asks for one more sync.
Nothing is queued for a customer who opted out of marketing, or whom a signal
places in Iran or Russia (`consent.py`); their trial notices and claims are
@@ -20,14 +21,14 @@ unaffected.
"""
import logging
-from collections.abc import Callable, Mapping
+from collections.abc import Mapping
from backend.data.db_accessors import user_db
from backend.data.notifications import AudienceAction
-from backend.notifications import subscriber_fields
from backend.notifications.consent import MarketingContact, audience_change_allowed
+from backend.notifications.lifecycle_audience import resync_after_group_change
from backend.notifications.queue import queue_audience_change
-from backend.notifications.subscriber_fields import Fields, audience_event
+from backend.notifications.subscriber_fields import audience_event
logger = logging.getLogger(__name__)
@@ -40,55 +41,41 @@ _TRIAL_GROUP_CHANGES: Mapping[str, AudienceAction] = {
"ended": AudienceAction.REMOVE_TRIAL,
}
-# Each from the live Stripe subscription at the moment the notice applies.
-_TRIAL_FIELDS: Mapping[str, Callable[[dict], Fields]] = {
- "started": lambda sub: subscriber_fields.trial_started(sub.get("trial_start")),
- "resumed": lambda _: subscriber_fields.trial_resumed(),
- "canceled": lambda _: subscriber_fields.trial_canceled(),
- "payment_failed": lambda _: subscriber_fields.trial_canceled(),
- "ended": lambda _: subscriber_fields.trial_canceled(),
- # The trial's end is when the first paid period began.
- "converted": lambda sub: subscriber_fields.subscribed(sub.get("trial_end")),
-}
-
-async def queue_trial_audience_change(
- kind: str, user: MarketingContact, subscription: dict
-) -> None:
- """Queue the trial group change and field update for this notice, if it
- has either.
+async def queue_trial_audience_change(kind: str, user: MarketingContact) -> None:
+ """Queue the trial group change for this notice, if it has one.
Raises when it cannot be queued, so the caller releases the notice claim
- and Stripe's retry makes the change. Both are idempotent, so a retry that
- repeats one is harmless. It is queued whatever this process's MailerLite
+ and Stripe's retry makes the change. It is idempotent, so a retry that
+ repeats it is harmless. It is queued whatever this process's MailerLite
settings: only the notification service holds them.
"""
action = _TRIAL_GROUP_CHANGES.get(kind)
if action is None:
return
- await _queue_group_change(action, user, _TRIAL_FIELDS[kind](subscription))
+ await _queue_group_change(action, user)
async def leave_trial_group(user: MarketingContact) -> None:
- """The trial ended because another plan was bought: leave the trial group
- without writing any fields, so that plan's status and dates stay.
+ """The trial ended because another plan was bought: leave the trial group.
No notice or claim rides on it: a repeated removal changes nothing. Raises
when it cannot be queued, so Stripe retries the event."""
- await _queue_group_change(AudienceAction.REMOVE_TRIAL, user, None)
+ await _queue_group_change(AudienceAction.REMOVE_TRIAL, user)
-async def _queue_group_change(
- action: AudienceAction, user: MarketingContact, fields: Fields | None
-) -> None:
+async def _queue_group_change(action: AudienceAction, user: MarketingContact) -> None:
if not audience_change_allowed(user, action):
return
- event = audience_event(action, user.email, user.id, fields)
+ event = audience_event(action, user.email, user.id)
if event is None:
return
result = await queue_audience_change(event)
if not result.success:
raise RuntimeError(f"Could not queue {action.value}: {result.message}")
+ if action == AudienceAction.ADD_TRIAL:
+ # A join can create the subscriber; a removal never does.
+ await resync_after_group_change(user.id)
async def join_paying_audience(user: MarketingContact) -> None:
@@ -120,3 +107,5 @@ async def join_paying_audience(user: MarketingContact) -> None:
f"Trial for user {user.id} converted but {action.value} could not be "
f"queued: {result.message}"
)
+ return
+ await resync_after_group_change(user.id)
diff --git a/autogpt_platform/backend/backend/notifications/trial_audience_cancel_test.py b/autogpt_platform/backend/backend/notifications/trial_audience_cancel_test.py
index 39c20527d0..800017e472 100644
--- a/autogpt_platform/backend/backend/notifications/trial_audience_cancel_test.py
+++ b/autogpt_platform/backend/backend/notifications/trial_audience_cancel_test.py
@@ -1,20 +1,15 @@
"""The MailerLite trial group across a scheduled cancellation: each cancel or
resume flip moves the group once, a pending cancellation that runs out leaves
-it as trial canceled, a trial ended by buying another plan still leaves the
-group without touching that plan's status, and no ending reminder goes out
-while it is pending."""
+it, a trial ended by buying another plan still leaves the group, and no ending
+reminder goes out while it is pending. The status itself is the lifecycle
+sync's (`lifecycle_audience.py`)."""
from datetime import UTC, datetime, timedelta
from unittest.mock import AsyncMock
import pytest
-from backend.data.notifications import (
- AudienceAction,
- NotificationResult,
- SubscriberField,
- SubscriptionStatus,
-)
+from backend.data.notifications import AudienceAction, NotificationResult
from backend.notifications import trial_audience_test
from backend.notifications.trial_audience_test import _notify, _state
@@ -71,16 +66,14 @@ async def test_each_cancel_or_resume_flip_moves_the_group_once(trial):
sent = [await _flip(trial, kind, rev, keys, audience) for kind, rev in flips]
assert sent == [1, 0, 1, 0]
events = [c.args[0] for c in audience.await_args_list]
- assert [(e.action, e.fields[SubscriberField.STATUS]) for e in events] == [
- (AudienceAction.REMOVE_TRIAL, SubscriptionStatus.TRIAL_CANCELED.value),
- (AudienceAction.ADD_TRIAL, SubscriptionStatus.IN_TRIAL.value),
+ assert [e.action for e in events] == [
+ AudienceAction.REMOVE_TRIAL,
+ AudienceAction.ADD_TRIAL,
]
@pytest.mark.asyncio
-async def test_a_cancel_pending_trial_that_runs_out_leaves_the_group_as_trial_canceled(
- trial,
-):
+async def test_a_cancel_pending_trial_that_runs_out_leaves_the_group(trial):
pending = trial.model_copy(
update={"cancel_at_period_end": True, "status": "canceled"}
)
@@ -89,8 +82,6 @@ async def test_a_cancel_pending_trial_that_runs_out_leaves_the_group_as_trial_ca
audience = AsyncMock(return_value=NotificationResult(success=True))
got, notice, _ = await _notify(pending, raw, "ended", audience=audience)
assert got == [AudienceAction.REMOVE_TRIAL]
- status = audience.await_args.args[0].fields[SubscriberField.STATUS]
- assert status == SubscriptionStatus.TRIAL_CANCELED.value
notice.assert_awaited_once()
diff --git a/autogpt_platform/backend/backend/notifications/trial_audience_test.py b/autogpt_platform/backend/backend/notifications/trial_audience_test.py
index f385f1fc72..421e1fcf79 100644
--- a/autogpt_platform/backend/backend/notifications/trial_audience_test.py
+++ b/autogpt_platform/backend/backend/notifications/trial_audience_test.py
@@ -15,7 +15,6 @@ from backend.data.notifications import (
AudienceEventModel,
NotificationResult,
SubscriberField,
- SubscriptionStatus,
)
from backend.data.subscription_trial import TrialState
from backend.data.subscription_trial_config import AcceptedTrialOffer
@@ -182,13 +181,18 @@ async def _notify(
("ending", []),
],
)
-async def test_each_trial_transition_moves_the_trial_group(trial, kind, actions):
+async def test_each_trial_transition_moves_the_trial_group(
+ trial, lifecycle_resync, kind, actions
+):
trial, raw = _state(trial, kind)
if kind == "ending":
raw["trial_end"] = int((datetime.now(UTC) + timedelta(days=1)).timestamp())
got, notice, _ = await _notify(trial, raw, kind)
assert got == actions
notice.assert_awaited_once()
+ # Joining may create the subscriber, so the status follows a join.
+ joined = AudienceAction.ADD_TRIAL in actions
+ assert lifecycle_resync.await_count == (1 if joined else 0)
@pytest.mark.asyncio
@@ -197,12 +201,13 @@ async def test_each_trial_transition_moves_the_trial_group(trial, kind, actions)
[(False, AudienceAction.ENROLL_TOUR), (True, AudienceAction.ADD_CHANGELOG)],
)
async def test_conversion_leaves_the_trial_and_joins_the_paying_audience(
- trial, welcomed, paying
+ trial, lifecycle_resync, welcomed, paying
):
trial, raw = _state(trial, "converted")
got, notice, _ = await _notify(trial, raw, "converted", welcomed=welcomed)
assert got == [AudienceAction.REMOVE_TRIAL, paying]
notice.assert_awaited_once()
+ lifecycle_resync.assert_awaited_once()
@pytest.mark.asyncio
@@ -218,12 +223,11 @@ async def test_the_api_server_queues_without_any_mailerlite_settings(
):
"""The API server, which handles the webhook, never holds the MailerLite
token or group IDs: only the notification service does. Its changes must
- be queued anyway, with their fields, for the consumer to decide."""
+ be queued anyway, for the consumer to decide."""
trial, raw = _state(trial, kind)
audience = AsyncMock(return_value=NotificationResult(success=True))
got, notice, _ = await _notify(trial, raw, kind, audience=audience)
assert got == actions
- assert audience.await_args_list[0].args[0].fields
notice.assert_awaited_once()
@@ -259,13 +263,16 @@ async def test_an_email_mailerlite_cannot_take_never_blocks_the_notice(trial, ki
@pytest.mark.asyncio
-async def test_joining_the_paying_audience_never_fails_the_sent_conversion(trial):
+async def test_joining_the_paying_audience_never_fails_the_sent_conversion(
+ trial, lifecycle_resync
+):
trial, raw = _state(trial, "converted")
broken = AsyncMock(side_effect=RuntimeError("database unavailable"))
got, notice, release = await _notify(trial, raw, "converted", claim_welcome=broken)
assert got == [AudienceAction.REMOVE_TRIAL]
notice.assert_awaited_once()
release.assert_not_awaited()
+ lifecycle_resync.assert_not_awaited()
@pytest.mark.asyncio
@@ -434,54 +441,23 @@ async def test_remove_from_trial_fails_loudly_so_the_job_retries(mailerlite_conf
# ── subscriber fields ──────────────────────────────────────────────────────
-
-
-def _day(timestamp: int) -> str:
- return datetime.fromtimestamp(timestamp, UTC).strftime("%Y-%m-%d")
+#
+# The status and dates come from the lifecycle sync every trial transition
+# triggers (`lifecycle_audience.py`), by PostHog's rules: a trial that runs
+# out is `trial_expired` there, not here.
@pytest.mark.asyncio
@pytest.mark.parametrize(
- "kind, status",
- [
- ("started", SubscriptionStatus.IN_TRIAL),
- ("resumed", SubscriptionStatus.IN_TRIAL),
- ("canceled", SubscriptionStatus.TRIAL_CANCELED),
- # Neither ever became a paid subscription, so neither is churn.
- ("ended", SubscriptionStatus.TRIAL_CANCELED),
- ("payment_failed", SubscriptionStatus.TRIAL_CANCELED),
- ("converted", SubscriptionStatus.SUBSCRIBED),
- ],
+ "kind", ["started", "resumed", "canceled", "ended", "payment_failed", "converted"]
)
-async def test_each_trial_transition_sets_the_status_on_its_group_change(
- trial, kind, status
-):
+async def test_a_trial_group_change_carries_no_status(trial, kind):
trial, raw = _state(trial, kind)
audience = AsyncMock(return_value=NotificationResult(success=True))
await _notify(trial, raw, kind, audience=audience)
event = audience.await_args_list[0].args[0]
assert event.action in (AudienceAction.ADD_TRIAL, AudienceAction.REMOVE_TRIAL)
- assert event.fields[SubscriberField.STATUS] == status.value
-
-
-async def test_a_trial_start_carries_stripes_trial_start_date(trial):
- trial, raw = _state(trial, "started")
- raw["trial_start"] = 1788000000
- audience = AsyncMock(return_value=NotificationResult(success=True))
- await _notify(trial, raw, "started", audience=audience)
- fields = audience.await_args.args[0].fields
- assert fields[SubscriberField.TRIAL_STARTED] == _day(1788000000)
-
-
-async def test_a_conversion_starts_the_subscription_when_the_trial_ended(trial):
- trial, raw = _state(trial, "converted")
- audience = AsyncMock(return_value=NotificationResult(success=True))
- await _notify(trial, raw, "converted", audience=audience)
- fields = audience.await_args_list[0].args[0].fields
- assert fields[SubscriberField.SUBSCRIPTION_STARTED] == _day(raw["trial_end"])
- assert fields[SubscriberField.SUBSCRIPTION_ENDED] is None
+ assert event.fields == {}
@pytest.mark.asyncio
diff --git a/autogpt_platform/backend/scripts/backfill_posthog_lifecycle.py b/autogpt_platform/backend/scripts/backfill_posthog_lifecycle.py
index 4fa08b7b70..873addb623 100644
--- a/autogpt_platform/backend/scripts/backfill_posthog_lifecycle.py
+++ b/autogpt_platform/backend/scripts/backfill_posthog_lifecycle.py
@@ -2,8 +2,10 @@
Runs the same sweep as the daily ``sync_posthog_lifecycles`` scheduler job
(``backend.data.posthog_lifecycle_sync.sync_all_posthog_lifecycles``): one
-Stripe ``Subscription.list(status="all")`` pass mapped to users by customer
-id, trials from the database, and one ``$set`` per user sent in batches.
+Stripe ``Subscription.list(status="all")`` pass, with discounts expanded,
+mapped to users by customer id, trials from the database, and one ``$set``
+per user sent in batches. The status follows ``compute_lifecycle_snapshot``,
+which MailerLite's ``cli mailerlite-backfill`` uses too.
A dry run (the default) reads Stripe and the database and prints how many
users land in each status; it sends nothing. ``--send`` sends the ``$set``s.
diff --git a/autogpt_platform/backend/scripts/test_backfill_posthog_lifecycle.py b/autogpt_platform/backend/scripts/test_backfill_posthog_lifecycle.py
index fe480fdc1c..d0b3e59f2f 100644
--- a/autogpt_platform/backend/scripts/test_backfill_posthog_lifecycle.py
+++ b/autogpt_platform/backend/scripts/test_backfill_posthog_lifecycle.py
@@ -6,10 +6,10 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest
import stripe
-from prisma.enums import SubscriptionTier
import scripts.backfill_posthog_lifecycle as backfill
from backend.data.posthog_lifecycle import LifecycleUser
+from backend.data.stripe_subscription_facts import LIST_EXPAND
SYNC = "backend.data.posthog_lifecycle_sync"
NOW = datetime.now(UTC)
@@ -27,12 +27,9 @@ def _sub(sub_id: str, customer: str, status: str, **extra) -> dict:
}
-def _user(user_id: str, customer: str | None, tier: str) -> LifecycleUser:
+def _user(user_id: str, customer: str | None) -> LifecycleUser:
return LifecycleUser(
- id=user_id,
- created_at=NOW - timedelta(days=100),
- stripe_customer_id=customer,
- subscription_tier=SubscriptionTier(tier),
+ id=user_id, created_at=NOW - timedelta(days=100), stripe_customer_id=customer
)
@@ -56,15 +53,23 @@ def sources(monkeypatch: pytest.MonkeyPatch, secrets: MagicMock):
"data": [
_sub("sub_1", "cus_1", "active"),
_sub("sub_2", "cus_2", "canceled", ended_at=int(NOW.timestamp())),
+ # A 100% coupon: not a paying subscriber (SECRT-2795).
+ _sub(
+ "sub_4",
+ "cus_4",
+ "active",
+ discounts=[{"coupon": {"percent_off": 100}, "end": None}],
+ ),
],
"has_more": False,
},
"k",
)
users = [
- _user("u-1", "cus_1", "PRO"),
- _user("u-2", "cus_2", "NO_TIER"),
- _user("u-3", "cus_3", "NO_TIER"),
+ _user("u-1", "cus_1"),
+ _user("u-2", "cus_2"),
+ _user("u-3", "cus_3"),
+ _user("u-4", "cus_4"),
]
with (
patch(
@@ -96,12 +101,13 @@ async def test_dry_run_prints_counts_and_sends_nothing(sources, capsys):
assert await backfill.main(send=False, all_users=False, batch_size=500) == 0
posthog.capture.assert_not_called()
- list_async.assert_awaited_once_with(status="all", limit=100)
+ # Discounts expanded, so a full coupon is seen without a call per user.
+ list_async.assert_awaited_once_with(status="all", limit=100, expand=LIST_EXPAND)
out = capsys.readouterr().out
assert "DRY RUN, nothing sent" in out
assert "Stripe: test mode" in out
- assert "Users mapped: 3" in out
- assert " signed: 1" in out
+ assert "Users mapped: 4" in out
+ assert " signed: 2" in out
assert " subscribed: 1" in out
assert " subscription_ended: 1" in out
assert "$set sent" not in out
@@ -122,9 +128,10 @@ async def test_send_mode_sets_every_user_and_flushes(sources, capsys):
"u-1": "subscribed",
"u-2": "subscription_ended",
"u-3": "signed",
+ "u-4": "signed",
}
assert posthog.flush.called
- assert "$set sent: 3" in capsys.readouterr().out
+ assert "$set sent: 4" in capsys.readouterr().out
async def test_send_requires_a_posthog_key(sources, secrets):
diff --git a/docs/engineering/tracking-plan.md b/docs/engineering/tracking-plan.md
index f8c04cdc8b..88ae429d36 100644
--- a/docs/engineering/tracking-plan.md
+++ b/docs/engineering/tracking-plan.md
@@ -359,17 +359,66 @@ replaying events.
| Property | Value |
| --- | --- |
-| `subscription_status` | `signed`, `in_trial`, `trial_canceled` (a trial set to cancel, which keeps access until its end and goes back to `in_trial` on resume, or a trial that ended unconverted), `subscribed`, `subscription_canceled` (set to cancel, active until the period ends), `payment_failed` (a renewal or the first charge after a trial failed; Stripe is retrying), `subscription_ended`. Worked out from the current user, trial and Stripe state, never from the last event received. |
+| `subscription_status` | One of the values below. Worked out from the current trial and Stripe state, never from the last event received. |
| `signup_at` | When the user row was created. |
| `trial_started_at` | When the trial started. |
-| `subscription_started_at` | Start of the current or last paid subscription; for a converted trial, the conversion. |
-| `subscription_canceled_at` | When the subscription was canceled, while `subscription_canceled` or `subscription_ended`. |
-| `subscription_ended_at` | When the subscription ended, while `subscription_ended`. |
+| `subscription_started_at` | When the person first became a paying subscriber: the earliest start of any paid subscription, kept across a resubscribe after a gap. For a converted trial, the conversion (its first successful charge), not the trial start. |
+| `subscription_canceled_at` | When the current or last paid subscription was canceled, while `subscription_canceled` or `subscription_ended`. |
+| `subscription_ended_at` | When it ended, while `subscription_ended`. |
Dates are ISO-8601 UTC strings of the lifecycle moment, not of the sync. A
date that doesn't apply to the current status is `$unset`, never sent as
null.
+`subscription_status` counts only **paying** subscribers as `subscribed`
+(SECRT-2795). First match wins:
+
+| Value | Meaning |
+| --- | --- |
+| `subscription_canceled` | A live paid subscription set to cancel; active until the period ends. |
+| `subscribed` | A live (`active`) paid subscription. |
+| `trial_canceled` | A running trial set to cancel (keeps access until its end, back to `in_trial` on resume), or a trial that ended unpaid because the user canceled it. |
+| `in_trial` | A running trial, ours or a Stripe-native one; includes the hour between the trial's end and its first charge. |
+| `payment_failed` | The latest paid subscription is `past_due` or `unpaid`, or a trial's first charge failed and Stripe is still retrying it. Access is lost meanwhile. |
+| `subscription_ended` | Has paid before, and no paid subscription is live. |
+| `trial_expired` | A trial that ended without a payment and without the user canceling it: it ran out, or its first charge never cleared and Stripe gave up. |
+| `signed` | Anything else, including a signup that never started a trial, and a former payer now on a free (fully discounted) subscription, who keeps `subscription_started_at`. |
+
+- **Paid** means the subscription charges something. A subscription whose
+ discounts take the whole price (a 100% coupon, or an amount off at least the
+ price, on the subscription or on the customer) is not paid: it never makes
+ anyone `subscribed`, `subscription_canceled` or `subscription_ended`, and
+ its start is not a paid start. Neither is one whose only invoice so far
+ charged nothing (a 100% coupon for the first period, which Stripe takes off
+ the subscription once that invoice is made). A partial discount is paid. A
+ full discount that ends (a repeating coupon) makes the subscription paid
+ from then on. Stacked discounts apply one after another, in Stripe's
+ order. A customer's discount counts only from its start, so a coupon given
+ later doesn't make an older subscription free. Someone who paid before and
+ now has a live free subscription (another one, or the same one given a
+ coupon after it first charged) is `signed` (not `payment_failed` or
+ `subscription_ended`), keeping their first paid start.
+- **The plan tier is not read.** An admin-granted plan (ENTERPRISE, BUSINESS
+ or any other) with no paid Stripe subscription is `signed`; one who pays
+ through Stripe is `subscribed` by the usual rule.
+- **The user canceled** a trial when it was set to cancel, or canceled on
+ request without a comment of ours (a comment the customer typed in the
+ billing portal still counts as theirs). Our own cancels (after a failed
+ payment, of a rejected trial, of a replaced plan) carry a comment starting
+ `autogpt:` or `autogpt_trial:`, and Stripe giving up on the first charge
+ says `payment_failed`: both are `trial_expired`.
+
+MailerLite's `subscription_status` and its `*_date` fields hold the same
+values by the same rules: both come from one mapping
+(`backend/data/posthog_lifecycle.py`). Each lifecycle sync queues them for
+MailerLite as well (`backend/notifications/lifecycle_audience.py`), written
+only into a subscriber MailerLite already has and only when they differ. The
+daily sweep is PostHog only, so nothing corrects MailerLite on a schedule: a
+sync that failed before it was queued, or two syncs from different pods queued
+out of order, leave it stale until the user's next sync. `poetry run cli
+mailerlite-backfill --fields-only --apply --yes` (after its dry run) catches
+MailerLite up. MailerLite holds days, not moments.
+
The role picked in the onboarding wizard is set with a `$set` of its own when
the profile is submitted at the Preparing step (`POST /api/onboarding/profile`,
`product_analytics.set_onboarding_role`), so funnels and retention can be split