: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): evaluate the desktop gateway flag for the run user (#110212)
Brandonec545b907671
diff --git a/products/tasks/backend/temporal/process_task/ai_gateway_token.py b/products/tasks/backend/temporal/process_task/ai_gateway_token.pyindex 50db9175ab7c..d05beb7905e0 100644--- a/products/tasks/backend/temporal/process_task/ai_gateway_token.py+++ b/products/tasks/backend/temporal/process_task/ai_gateway_token.py@@ -180,11 +180,12 @@ def _posthog_code_team(team_id: int) -> "Team | None":return Team.objects.select_related("organization").filter(id=team_id).first()-def _posthog_code_refusal(team_id: int, model: str | None) -> str | None:+def _posthog_code_refusal(team_id: int, model: str | None, distinct_id: str | None) -> str | None:"""The rollout flag is the switch for the product; a free plan's pin has no model anunpinned or paid-model run could fall back to."""team = _posthog_code_team(team_id)- if team is None or not desktop_rollout_enabled(team.organization, team):+ # The run's user, so a person-targeted flag moves cloud runs with that person's desktop sessions.+ if team is None or not desktop_rollout_enabled(team.organization, team, distinct_id):return "not_rolled_out"if posthog_code_plan(team) == "free" and not model_allowed_by_pin(FREE_TIER_MODELS, model):return "model_outside_pin"@@ -207,6 +208,7 @@ def mint_refusal(runtime: str | None,internal: bool = False,prior_slack_run: bool = False,+ distinct_id: str | None = None,) -> str | None:"""Why a routed run must not mint; a run without a token stays on the Python gateway."""if ai_product == "slack_app" and not has_slack_provenance(@@ -214,7 +216,7 @@ def mint_refusal():return "no_slack_provenance"if ai_product == POSTHOG_CODE_PRODUCT:- refusal = _posthog_code_refusal(team_id, model)+ refusal = _posthog_code_refusal(team_id, model, distinct_id)if refusal:return refusal# The Pi harness reads only LLM_GATEWAY_URL.diff --git a/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py b/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.pyindex d5a265b88b5d..49bee8a3b1cc 100644--- a/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py+++ b/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py@@ -1331,6 +1331,20 @@ class TestPosthogCodeSandboxMint:assert flag.call_args.args[0] == "posthog-desktop-ai-gateway"assert flag.call_args.kwargs["groups"]["organization"] == str(team.organization_id)+ @pytest.mark.django_db+ def test_flag_is_evaluated_for_the_run_user(self, mint_settings):+ team = self._team(paid=True)+ mint_settings.SANDBOX_AI_GATEWAY_PRODUCTS = "posthog_code"+ with (+ patch(+ "products.tasks.backend.logic.services.desktop_gateway_token.feature_enabled_or_false",+ return_value=False,+ ) as flag,+ patch("products.tasks.backend.temporal.process_task.utils.mint_scoped_token"),+ ):+ ai_gateway_env_vars(team_id=team.id, origin_product="user_created", distinct_id="run-user-distinct-id")+ assert flag.call_args.args[1] == "run-user-distinct-id"+@pytest.mark.django_dbdef test_off_pin_model_refuses(self):team = self._team(paid=True)diff --git a/products/tasks/backend/temporal/process_task/utils.py b/products/tasks/backend/temporal/process_task/utils.pyindex 34e2c536fd55..2264ce273370 100644--- a/products/tasks/backend/temporal/process_task/utils.py+++ b/products/tasks/backend/temporal/process_task/utils.py@@ -1486,6 +1486,7 @@ def ai_gateway_env_vars(runtime=runtime,internal=internal,prior_slack_run=prior_slack_run,+ distinct_id=distinct_id,)if refusal:AI_GATEWAY_TOKEN_MINTS.labels(result="skipped").inc()