:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(tasks): require a paid invoice for the standard posthog code limit tier (#122409)
Brandon978c1e3c8b4d
diff --git a/products/tasks/backend/logic/services/desktop_gateway_token.py b/products/tasks/backend/logic/services/desktop_gateway_token.pyindex 8613b72e115b..59909082b109 100644--- a/products/tasks/backend/logic/services/desktop_gateway_token.py+++ b/products/tasks/backend/logic/services/desktop_gateway_token.py@@ -8,13 +8,11 @@ import jsonimport loggingimport functoolsfrom collections.abc import Mapping-from datetime import timedeltafrom decimal import Decimal, InvalidOperationfrom types import MappingProxyTypefrom typing import TYPE_CHECKING, Any, Literalfrom django.conf import settings-from django.utils import timezoneimport requestsimport posthoganalytics@@ -195,21 +193,15 @@ def posthog_code_billing_synced(organization: "Organization") -> bool:return bool(usage.get("period")) and isinstance(entry, dict) and ("usage" in entry or "limit" in entry)-# (org age below, minimum highest product trust score); older orgs need none. Matches the-# legacy gateway's PostHog Code account trust policy.-_TRUST_MINIMUMS: tuple[tuple[timedelta, float], ...] = ((timedelta(days=7), 7), (timedelta(days=30), 3))+# Billing's SUBSCRIBED_PAYING_TRUSTED (a paid invoice). Billing sets score 3 when a card subscribes, before any payment.+_PAID_TRUST_MINIMUM = 7.0def posthog_code_trusted(organization: "Organization") -> bool:- """Whether the org's age and best product trust score clear the minimum; a missing or- malformed score counts as zero."""+ """Whether the org's best trust score shows a paid invoice; a missing or malformed score counts as zero."""scores = organization.customer_trust_scores or {}best = max((float(v) for v in scores.values() if isinstance(v, int | float)), default=0.0)- age = timezone.now() - organization.created_at- for below_age, minimum in _TRUST_MINIMUMS:- if age < below_age:- return best >= minimum- return True+ return best >= _PAID_TRUST_MINIMUMdef _override_tier(raw: object) -> LimitTier | None:diff --git a/products/tasks/backend/tests/test_desktop_gateway_token.py b/products/tasks/backend/tests/test_desktop_gateway_token.pyindex 548e3b47dc29..d206b2ae050e 100644--- a/products/tasks/backend/tests/test_desktop_gateway_token.py+++ b/products/tasks/backend/tests/test_desktop_gateway_token.py@@ -747,17 +747,14 @@ class TestPosthogCodeTrusted(APIBaseTest):@parameterized.expand([("new org below 7", 2, {"posthog_code_credits": 6}, False),- ("6-day-old org needs 7", 6, {"posthog_code_credits": 6}, False),- ("8-day-old org needs 3", 8, {"posthog_code_credits": 3}, True),- ("29-day-old org needs 3", 29, {"posthog_code_credits": 2}, False),("new org at 7", 2, {"posthog_code_credits": 7}, True),("new org best score of any product", 2, {"ai_credits": 8, "posthog_code_credits": 0}, True),("new org no score", 2, {}, False),("new org malformed score", 2, {"posthog_code_credits": "9"}, False),- ("week-old org below 3", 10, {"posthog_code_credits": 2}, False),- ("week-old org at 3", 10, {"posthog_code_credits": 3}, True),- ("25-day-old org below 3", 25, {"posthog_code_credits": 2}, False),- ("month-old org no score", 31, {}, True),+ ("week-old org subscribed never paid", 10, {"posthog_code_credits": 3}, False),+ ("month-old org subscribed never paid", 32, {"posthog_code_credits": 3, "events": 3}, False),+ ("month-old org no score", 31, {}, False),+ ("month-old org paying", 31, {"posthog_code_credits": 7}, True),])def test_age_and_trust(self, _name, age_days, scores, expected) -> None: