Skip to content

: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.

feat(tasks): read the gateway product from the task profile (#126065)

Charles Vienc08ecdbe5562
diff --git a/products/tasks/backend/temporal/process_task/ai_gateway_token.py b/products/tasks/backend/temporal/process_task/ai_gateway_token.py
index bb54abe734c0..95b72557c8d5 100644
--- a/products/tasks/backend/temporal/process_task/ai_gateway_token.py
+++ b/products/tasks/backend/temporal/process_task/ai_gateway_token.py
@@ -129,12 +129,7 @@ _MINT_ATTEMPTS = 2
_MINT_TIMEOUT_SECONDS = 3
-def resolve_sandbox_ai_product(
- origin_product: str | None, ai_stage: str | None, *, internal: bool = False, billing_product: str | None = None
-) -> str:
- """The `ai_product` the agent server will resolve for this run."""
- if origin_product in ("signal_report", "signals_scout") and billing_product == "posthog_ai":
- return "posthog_ai"
+def _origin_gateway_product(origin_product: str | None, *, internal: bool) -> str:
gateway_product = _ORIGIN_TO_GATEWAY_PRODUCT.get(origin_product or "")
# Stored rows may carry a caller-set review_hog origin predating its
# reservation; only the server-stamped `internal` flag admits the mintable product.
@@ -145,7 +140,22 @@ def resolve_sandbox_ai_product(
logger.warning("account_audit origin without server-stamped internal flag; resolving posthog_code")
return "posthog_code"
if gateway_product is None:
- gateway_product = "background_agents" if internal else "posthog_code"
+ return "background_agents" if internal else "posthog_code"
+ return gateway_product
+
+
+def resolve_sandbox_ai_product(
+ origin_product: str | None,
+ ai_stage: str | None,
+ *,
+ internal: bool = False,
+ billing_product: str | None = None,
+ profile_billing_product: str | None = None,
+) -> str:
+ """The `ai_product` the agent server will resolve for this run."""
+ if origin_product in ("signal_report", "signals_scout") and billing_product == "posthog_ai":
+ return "posthog_ai"
+ gateway_product = profile_billing_product or _origin_gateway_product(origin_product, internal=internal)
if gateway_product == "signals" and ai_stage:
stage = "scout" if ai_stage.startswith(_SCOUT_STAGE_PREFIX) else ai_stage
if stage in _SIGNALS_STAGE_PRODUCTS:
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.py
index 2a73ea11e700..3b172236c2b1 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
@@ -30,6 +30,7 @@ from products.tasks.backend.exceptions import (
)
from products.tasks.backend.facade.billing import get_task_run_cost
from products.tasks.backend.facade.gateway import mint_private_gateway_token, revoke_private_gateway_token
+from products.tasks.backend.facade.profiles import get_task_profile
from products.tasks.backend.logic.services.desktop_gateway_token import valid_caps
from products.tasks.backend.logic.services.gateway_model_pin import (
DESKTOP_AGENT_MODELS,
@@ -118,6 +119,27 @@ class TestResolveSandboxAiProduct:
assert resolve_sandbox_ai_product("loop", "implementation") == "posthog_code"
assert resolve_sandbox_ai_product("review_hog", "implementation", internal=True) == "review_hog"
+ @pytest.mark.parametrize(
+ "origin_product,ai_stage,billing_product,profile_billing_product,expected",
+ [
+ ("user_created", None, None, "background_agents", "background_agents"),
+ ("signals_chat", "research", None, "signals", "signals_research"),
+ ("signal_report", None, "posthog_ai", "signals", "posthog_ai"),
+ ],
+ )
+ def test_profile_billing_product_replaces_the_origin_product(
+ self, origin_product, ai_stage, billing_product, profile_billing_product, expected
+ ):
+ assert (
+ resolve_sandbox_ai_product(
+ origin_product,
+ ai_stage,
+ billing_product=billing_product,
+ profile_billing_product=profile_billing_product,
+ )
+ == expected
+ )
+
class TestSharedRoutingContract:
"""Both matchers consume gateway-routing-cases.json (see the TS suite), so the
@@ -642,9 +664,10 @@ class TestProvisioningBoundaries:
ctx.task_runtime = "acp"
return ctx
- def _task(self):
+ def _task(self, profile=None):
task = MagicMock()
task.internal = True
+ task.stored_profile = profile
return task
@pytest.mark.django_db
@@ -684,14 +707,18 @@ class TestProvisioningBoundaries:
None if incomplete else 0
)
- def test_run_gateway_env_vars_maps_the_full_context(self, mint_settings):
+ @pytest.mark.parametrize(
+ "profile,profile_billing_product",
+ [(None, None), (get_task_profile("business_knowledge"), "background_agents")],
+ )
+ def test_run_gateway_env_vars_maps_the_full_context(self, mint_settings, profile, profile_billing_product):
from products.tasks.backend.temporal.process_task import utils
with (
patch.object(utils, "ai_gateway_env_vars", return_value={"AI_GATEWAY_TOKEN": "phe"}) as env,
patch.object(utils, "record_gateway_routing"),
):
- out = utils.run_gateway_env_vars(self._ctx(), self._task())
+ out = utils.run_gateway_env_vars(self._ctx(), self._task(profile))
assert out == {"AI_GATEWAY_TOKEN": "phe"}
env.assert_called_once_with(
team_id=7,
@@ -703,6 +730,7 @@ class TestProvisioningBoundaries:
model="claude-sonnet-5",
runtime="acp",
prior_slack_run=False,
+ profile_billing_product=profile_billing_product,
)
def test_non_slack_origin_skips_the_prior_run_lookup(self, mint_settings):
@@ -1851,7 +1879,7 @@ class TestPosthogCodeSandboxMint:
ctx = MagicMock(
claude_model_access="gateway", codex_model_access="gateway", state={}, team_id=2, run_id="run-1"
)
- task = MagicMock(is_scout_experiment=False, internal=False)
+ task = MagicMock(is_scout_experiment=False, internal=False, stored_profile=None)
with (
patch.object(utils, "ai_gateway_env_vars", side_effect=RuntimeError("boom")),
patch.object(utils, "record_gateway_routing"),
diff --git a/products/tasks/backend/temporal/process_task/utils.py b/products/tasks/backend/temporal/process_task/utils.py
index 31e8b6ad662e..6470dec813bc 100644
--- a/products/tasks/backend/temporal/process_task/utils.py
+++ b/products/tasks/backend/temporal/process_task/utils.py
@@ -1624,12 +1624,15 @@ def run_gateway_env_vars(ctx: TaskProcessingContext, task: Task) -> dict[str, st
record_gateway_routing(run_id=ctx.run_id, team_id=ctx.team_id, uses_gateway=False)
return {}
ai_stage = (ctx.state or {}).get("ai_stage")
+ profile = task.stored_profile
+ profile_billing_product = profile.billing_product if profile else None
requires_go = (
resolve_sandbox_ai_product(
ctx.origin_product,
ai_stage,
internal=task.internal,
billing_product=(ctx.state or {}).get("billing_product"),
+ profile_billing_product=profile_billing_product,
)
== POSTHOG_CODE_PRODUCT
)
@@ -1639,6 +1642,7 @@ def run_gateway_env_vars(ctx: TaskProcessingContext, task: Task) -> dict[str, st
origin_product=ctx.origin_product,
ai_stage=ai_stage,
internal=task.internal,
+ profile_billing_product=profile_billing_product,
prior_slack_run=_task_has_stamped_slack_run(task, ctx.origin_product, ctx.state),
distinct_id=ctx.distinct_id,
state=ctx.state,
@@ -1747,6 +1751,7 @@ def ai_gateway_env_vars(
model: str | None = None,
runtime: str | None = None,
prior_slack_run: bool = False,
+ profile_billing_product: str | None = None,
) -> dict[str, str]:
"""Env vars routing listed products to the Go ai-gateway, shared by every
injection site so the both-or-nothing guard cannot drift per site. Both
@@ -1766,7 +1771,11 @@ def ai_gateway_env_vars(
"""
ai_product = (
resolve_sandbox_ai_product(
- origin_product, ai_stage, internal=internal, billing_product=(state or {}).get("billing_product")
+ origin_product,
+ ai_stage,
+ internal=internal,
+ billing_product=(state or {}).get("billing_product"),
+ profile_billing_product=profile_billing_product,
)
if team_id is not None
else None