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): route review_hog sandbox runs to the ai gateway (#92964)

Brandon8bcc0777fccf
diff --git a/products/desktop/packages/agent/src/utils/gateway-routing-cases.json b/products/desktop/packages/agent/src/utils/gateway-routing-cases.json
index 92b209f9d8a6..5c0dce71c5b5 100644
--- a/products/desktop/packages/agent/src/utils/gateway-routing-cases.json
+++ b/products/desktop/packages/agent/src/utils/gateway-routing-cases.json
@@ -120,6 +120,30 @@
"ai_stage": null,
"internal": false,
"expected": "posthog_code"
+ },
+ {
+ "origin_product": "review_hog",
+ "ai_stage": "validation-c1",
+ "internal": true,
+ "expected": "review_hog"
+ },
+ {
+ "origin_product": "review_hog",
+ "ai_stage": "resolution",
+ "internal": true,
+ "expected": "review_hog"
+ },
+ {
+ "origin_product": "review_hog",
+ "ai_stage": null,
+ "internal": true,
+ "expected": "review_hog"
+ },
+ {
+ "origin_product": "review_hog",
+ "ai_stage": "validation-c1",
+ "internal": false,
+ "expected": "posthog_code"
}
],
"routed": [
@@ -179,6 +203,20 @@
"allowlist": "signals",
"expected": true
},
+ {
+ "origin_product": "review_hog",
+ "ai_stage": "validation-c1",
+ "internal": true,
+ "allowlist": "review_hog,signals_scout",
+ "expected": true
+ },
+ {
+ "origin_product": "review_hog",
+ "ai_stage": "resolution",
+ "internal": true,
+ "allowlist": "signals_scout",
+ "expected": false
+ },
{
"origin_product": "signal_report",
"ai_stage": "inbox",
diff --git a/products/desktop/packages/agent/src/utils/gateway.test.ts b/products/desktop/packages/agent/src/utils/gateway.test.ts
index 537ccfc568d4..5d7ddd03d098 100644
--- a/products/desktop/packages/agent/src/utils/gateway.test.ts
+++ b/products/desktop/packages/agent/src/utils/gateway.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import {
+ getGatewayUsageUrl,
getLlmGatewayUrl,
resolveAiProduct,
resolveGatewayProduct,
@@ -77,6 +78,16 @@ describe("resolveGatewayProduct", () => {
originProduct: "support_reply",
expected: "conversations",
},
+ {
+ isInternal: true,
+ originProduct: "review_hog",
+ expected: "review_hog",
+ },
+ {
+ isInternal: false,
+ originProduct: "review_hog",
+ expected: "posthog_code",
+ },
{
isInternal: true,
originProduct: "loop",
@@ -162,6 +173,15 @@ describe("getLlmGatewayUrl", () => {
"http://localhost:3308/posthog_ai",
);
});
+
+ it("maps review_hog to the background_agents legacy slug everywhere", () => {
+ expect(getLlmGatewayUrl("http://localhost:8000", "review_hog")).toBe(
+ "http://localhost:3308/background_agents",
+ );
+ expect(getGatewayUsageUrl("http://localhost:8000", "review_hog")).toBe(
+ "http://localhost:3308/v1/usage/background_agents",
+ );
+ });
});
describe("resolveLlmGatewayUrl (slugless)", () => {
@@ -490,6 +510,48 @@ describe("resolveGatewayTarget", () => {
});
});
+ it("keeps the review_hog legacy leg on the background_agents slug", () => {
+ expect(
+ resolveGatewayTarget({
+ product: "review_hog",
+ aiStage: "validation-c1",
+ posthogHost: PY_HOST,
+ env: {},
+ }),
+ ).toEqual({
+ baseUrl: "https://gateway.us.posthog.com/background_agents",
+ isAiGateway: false,
+ aiProduct: "review_hog",
+ aiStage: "validation-c1",
+ });
+ });
+
+ it("keeps the legacy override on an LLM_GATEWAY_URL base too", () => {
+ expect(
+ resolveGatewayTarget({
+ product: "review_hog",
+ posthogHost: PY_HOST,
+ env: { LLM_GATEWAY_URL: "https://gateway.dev.posthog.dev" },
+ }).baseUrl,
+ ).toBe("https://gateway.dev.posthog.dev/background_agents");
+ });
+
+ it("routes review_hog to the Go gateway slugless once listed", () => {
+ expect(
+ resolveGatewayTarget({
+ product: "review_hog",
+ aiStage: "validation-c1",
+ posthogHost: PY_HOST,
+ env: { AI_GATEWAY_URL: GO, AI_GATEWAY_PRODUCTS: "review_hog" },
+ }),
+ ).toEqual({
+ baseUrl: GO,
+ isAiGateway: true,
+ aiProduct: "review_hog",
+ aiStage: "validation-c1",
+ });
+ });
+
it("honours an LLM_GATEWAY_URL override on the unrouted path", () => {
expect(
resolveGatewayTarget({
@@ -541,6 +603,9 @@ describe("resolveAiProduct", () => {
expect(
resolveAiProduct({ product: "posthog_code", aiStage: "implementation" }),
).toBe("posthog_code");
+ expect(
+ resolveAiProduct({ product: "review_hog", aiStage: "resolution" }),
+ ).toBe("review_hog");
});
it.each([
@@ -549,6 +614,7 @@ describe("resolveAiProduct", () => {
"slack_app",
"posthog_ai",
"conversations",
+ "review_hog",
] as const)("keeps %s unchanged", (product) => {
expect(resolveAiProduct({ product })).toBe(product);
});
diff --git a/products/desktop/packages/agent/src/utils/gateway.ts b/products/desktop/packages/agent/src/utils/gateway.ts
index 19aa18032e9c..83ba5389fb90 100644
--- a/products/desktop/packages/agent/src/utils/gateway.ts
+++ b/products/desktop/packages/agent/src/utils/gateway.ts
@@ -7,7 +7,8 @@ export type GatewayProduct =
| "slack_app"
| "posthog_ai"
| "conversations"
- | "onboarding";
+ | "onboarding"
+ | "review_hog";
export function resolveGatewayProduct({
isInternal,
@@ -23,6 +24,7 @@ export function resolveGatewayProduct({
loop: "posthog_code",
onboarding: "onboarding",
posthog_ai: "posthog_ai",
+ review_hog: "review_hog",
scout_suggestions: "signals",
signal_report: "signals",
signals_chat: "signals",
@@ -32,7 +34,13 @@ export function resolveGatewayProduct({
};
if (originProduct && originProduct in originProductToGatewayProductMap) {
- return originProductToGatewayProductMap[originProduct];
+ const mapped = originProductToGatewayProductMap[originProduct];
+ // Stored rows may carry a caller-set review_hog origin predating its
+ // reservation; only the server-stamped `internal` flag admits the mintable product.
+ if (mapped === "review_hog" && !isInternal) {
+ return "posthog_code";
+ }
+ return mapped;
}
if (isInternal) {
return "background_agents";
@@ -40,6 +48,19 @@ export function resolveGatewayProduct({
return "posthog_code";
}
+// The legacy gateway's review_hog product is API-key-only, so sandbox OAuth
+// tokens 403 on that slug; the legacy leg, including the mint-failure
+// fallback, uses background_agents.
+const LEGACY_PRODUCT_OVERRIDES: Partial<
+ Record<GatewayProduct, GatewayProduct>
+> = {
+ review_hog: "background_agents",
+};
+
+function legacyProduct(product: GatewayProduct): GatewayProduct {
+ return LEGACY_PRODUCT_OVERRIDES[product] ?? product;
+}
+
function getGatewayBaseUrl(posthogHost: string): string {
return getCloudTaskGatewayUrl(posthogHost).replace(/\/posthog_code$/, "");
}
@@ -48,7 +69,7 @@ export function getLlmGatewayUrl(
posthogHost: string,
product: GatewayProduct = "posthog_code",
): string {
- return `${getGatewayBaseUrl(posthogHost)}/${product}`;
+ return `${getGatewayBaseUrl(posthogHost)}/${legacyProduct(product)}`;
}
/**
@@ -75,7 +96,7 @@ export function resolveLlmGatewayUrl(
return base.replace(/\/v1$/, "");
}
if (envUrl) {
- return `${envUrl.replace(/\/$/, "")}/${product}`;
+ return `${envUrl.replace(/\/$/, "")}/${legacyProduct(product)}`;
}
return getLlmGatewayUrl(posthogHost, product);
}
@@ -146,7 +167,7 @@ export function getGatewayUsageUrl(
posthogHost: string,
product: GatewayProduct = "posthog_code",
): string {
- return `${getGatewayBaseUrl(posthogHost)}/v1/usage/${product}`;
+ return `${getGatewayBaseUrl(posthogHost)}/v1/usage/${legacyProduct(product)}`;
}
export interface GatewayTarget {
diff --git a/products/tasks/backend/presentation/serializers.py b/products/tasks/backend/presentation/serializers.py
index d78bf40c87a7..40828e8ac65a 100644
--- a/products/tasks/backend/presentation/serializers.py
+++ b/products/tasks/backend/presentation/serializers.py
@@ -830,6 +830,9 @@ class TaskWriteSerializer(serializers.Serializer):
# Attributes the task to a workflow, which the workflow_tasks endpoint proves
# via its service JWT. A forged origin would fake that provenance.
tasks_facade.TaskOriginProduct.WORKFLOW,
+ # Maps to the mintable `review_hog` gateway product, so a forged origin would
+ # mint an internally funded scoped token. Only ReviewHog's executor sets it.
+ tasks_facade.TaskOriginProduct.REVIEW_HOG,
tasks_facade.TaskOriginProduct.TASK_ANALYSIS,
}
if value in reserved_origins:
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 514f89f3e60a..cc0991b88ebf 100644
--- a/products/tasks/backend/temporal/process_task/ai_gateway_token.py
+++ b/products/tasks/backend/temporal/process_task/ai_gateway_token.py
@@ -33,6 +33,7 @@ _ORIGIN_TO_GATEWAY_PRODUCT: dict[str, str] = {
"loop": "posthog_code",
"onboarding": "onboarding",
"posthog_ai": "posthog_ai",
+ "review_hog": "review_hog",
"scout_suggestions": "signals",
"signal_report": "signals",
"signals_chat": "signals",
@@ -57,8 +58,12 @@ _MAX_CAP_DECIMAL_PLACES = 6
# stages are set only by server flows: pipeline stages by the flows that start
# them, `inbox` / `chat` by `Task.create_run`. A caller owning a report can reach
# `signals_inbox`, so the per-run cap and the product's daily budget bound those two.
+# review_hog qualifies because validate_origin_product reserves the origin and
+# the resolver requires the server-stamped `internal` flag; rows predating the
+# reservation resolve to posthog_code and cannot mint.
MINTABLE_PRODUCTS = frozenset(
{
+ "review_hog",
"signals_scout",
"signals_research",
"signals_implementation",
@@ -74,6 +79,36 @@ MINTABLE_PRODUCTS = frozenset(
# ceiling. Mirrors the stages `Task.create_run` stamps.
INTERACTIVE_MINTABLE_PRODUCTS = frozenset({"signals_inbox", "signals_chat"})
+# Model pins carried on the minted token: the pipeline's stage pins, the
+# implicit agent-SDK calls (the haiku small/fast utility model and the sonnet
+# generations the explore subagent's bare `sonnet` alias resolves to), and
+# every registry-supported reviewer-arm model. Persisted arms resolve against
+# the live registry with no re-pin step, and a dispatch denial does not fall
+# back to the legacy gateway, so an arm outside the pin would fail its turns
+# outright. Gateway-served models (slash-namespaced) stay out: an entry the
+# gateway cannot resolve fails the whole mint with a 400. A gateway without
+# allowed_models support ignores the field.
+_PRODUCT_ALLOWED_MODELS: dict[str, list[str]] = {
+ "review_hog": [
+ "claude-haiku-4-5",
+ "claude-sonnet-4-5",
+ "claude-sonnet-4-6",
+ "claude-sonnet-5",
+ "claude-opus-4-5",
+ "claude-opus-4-6",
+ "claude-opus-4-7",
+ "claude-opus-4-8",
+ "claude-opus-5",
+ "claude-fable-5",
+ "claude-fable-5-1",
+ "gpt-5",
+ "gpt-5.5",
+ "gpt-5.6-sol",
+ "gpt-5.6-luna",
+ "gpt-5.6-terra",
+ ],
+}
+
# Minting is optional (no token = Python-gateway fallback), so the total budget
# stays a few seconds: 2 attempts x 3s + one short backoff, not a 30s provisioning stall.
_MINT_ATTEMPTS = 2
@@ -83,6 +118,11 @@ _MINT_TIMEOUT_SECONDS = 3
def resolve_sandbox_ai_product(origin_product: str | None, ai_stage: str | None, *, internal: bool = False) -> str:
"""The `ai_product` the agent server will resolve for this run."""
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.
+ if gateway_product == "review_hog" and not internal:
+ logger.warning("review_hog 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"
if gateway_product == "signals" and ai_stage:
@@ -196,6 +236,9 @@ def mint_scoped_token(*, ai_product: str, team_id: int, user: str | None = None)
}
if user:
body["user"] = user
+ allowed_models = _PRODUCT_ALLOWED_MODELS.get(ai_product)
+ if allowed_models:
+ body["allowed_models"] = allowed_models
last_error: str = ""
for attempt in range(_MINT_ATTEMPTS):
try:
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 8675fd5dcc34..4b13a5525cbe 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
@@ -47,6 +47,12 @@ class TestResolveSandboxAiProduct:
def test_mapping(self, origin_product, ai_stage, expected):
assert resolve_sandbox_ai_product(origin_product, ai_stage) == expected
+ def test_review_hog_requires_the_server_stamped_internal_flag(self):
+ assert resolve_sandbox_ai_product("review_hog", "validation-c1", internal=True) == "review_hog"
+ assert resolve_sandbox_ai_product("review_hog", None, internal=True) == "review_hog"
+ assert resolve_sandbox_ai_product("review_hog", "validation-c1") == "posthog_code"
+ assert resolve_sandbox_ai_product("review_hog", None, internal=False) == "posthog_code"
+
def test_unmapped_internal_is_background_agents(self):
assert resolve_sandbox_ai_product("image_builder", None, internal=True) == "background_agents"
@@ -55,6 +61,7 @@ class TestResolveSandboxAiProduct:
def test_stage_does_not_split_non_signals_products(self):
assert resolve_sandbox_ai_product("loop", "implementation") == "posthog_code"
+ assert resolve_sandbox_ai_product("review_hog", "implementation", internal=True) == "review_hog"
class TestSharedRoutingContract:
@@ -135,6 +142,53 @@ class TestMintScopedToken:
assert kwargs["headers"] == {"Authorization": "Bearer phs_test_mint"}
assert kwargs["timeout"] == 3
+ def test_review_hog_mint_carries_the_model_pin(self, mint_settings):
+ with patch("products.tasks.backend.temporal.process_task.ai_gateway_token.requests.post") as post:
+ post.return_value = self._response(201, {"token": "phe_abc"})
+ assert mint_scoped_token(ai_product="review_hog", team_id=2) == "phe_abc"
+ body = post.call_args.kwargs["json"]
+ assert body["product"] == "review_hog"
+ assert body["allowed_models"] == [
+ "claude-haiku-4-5",
+ "claude-sonnet-4-5",
+ "claude-sonnet-4-6",
+ "claude-sonnet-5",
+ "claude-opus-4-5",
+ "claude-opus-4-6",
+ "claude-opus-4-7",
+ "claude-opus-4-8",
+ "claude-opus-5",
+ "claude-fable-5",
+ "claude-fable-5-1",
+ "gpt-5",
+ "gpt-5.5",
+ "gpt-5.6-sol",
+ "gpt-5.6-luna",
+ "gpt-5.6-terra",
+ ]
+
+ def test_review_hog_pin_covers_every_registry_arm_model(self):
+ """A persisted reviewer arm resolves against the live registry with no
+ re-pin, so any registry model missing from the pin fails its turns
+ after cutover. Slash-namespaced served models are exempt: the reviewer
+ never draws them, and an entry the gateway cannot resolve fails the
+ whole mint."""
+ from products.tasks.backend.facade.run_config import RuntimeAdapter, get_models_for_runtime_adapter
+ from products.tasks.backend.temporal.process_task.ai_gateway_token import _PRODUCT_ALLOWED_MODELS
+
+ registry = set(get_models_for_runtime_adapter(RuntimeAdapter.CLAUDE)) | set(
+ get_models_for_runtime_adapter(RuntimeAdapter.CODEX)
+ )
+ arm_models = {model for model in registry if "/" not in model}
+ missing = arm_models - set(_PRODUCT_ALLOWED_MODELS["review_hog"])
+ assert not missing, f"registry arm models absent from the review_hog pin: {sorted(missing)}"
+
+ def test_non_pinned_products_send_no_allowed_models(self, mint_settings):
+ with patch("products.tasks.backend.temporal.process_task.ai_gateway_token.requests.post") as post:
+ post.return_value = self._response(201, {"token": "phe_abc"})
+ mint_scoped_token(ai_product="signals_scout", team_id=123)
+ assert "allowed_models" not in post.call_args.kwargs["json"]
+
def test_retries_mint_rate_limit_then_succeeds(self, mint_settings):
with (
patch("products.tasks.backend.temporal.process_task.ai_gateway_token.requests.post") as post,
@@ -316,6 +370,23 @@ class TestMintableGate:
assert "AI_GATEWAY_TOKEN" not in env
mint.assert_not_called()
+ def test_routed_review_hog_mints(self, mint_settings):
+ mint_settings.SANDBOX_AI_GATEWAY_PRODUCTS = "review_hog"
+ with patch(
+ "products.tasks.backend.temporal.process_task.utils.mint_scoped_token",
+ return_value="phe_abc",
+ ) as mint:
+ env = ai_gateway_env_vars(team_id=2, origin_product="review_hog", ai_stage="validation-c1", internal=True)
+ assert env["AI_GATEWAY_TOKEN"] == "phe_abc"
+ mint.assert_called_once_with(ai_product="review_hog", team_id=2, user=None)
+
+ def test_non_internal_review_hog_does_not_mint(self, mint_settings):
+ mint_settings.SANDBOX_AI_GATEWAY_PRODUCTS = "review_hog"
+ with patch("products.tasks.backend.temporal.process_task.utils.mint_scoped_token") as mint:
+ env = ai_gateway_env_vars(team_id=2, origin_product="review_hog", ai_stage="validation-c1", internal=False)
+ assert "AI_GATEWAY_TOKEN" not in env
+ mint.assert_not_called()
+
def test_stageless_signal_report_cannot_mint_for_bare_signals(self, mint_settings):
mint_settings.SANDBOX_AI_GATEWAY_PRODUCTS = "signals"
with patch("products.tasks.backend.temporal.process_task.utils.mint_scoped_token") as mint:
diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.py
index 4dc7dfc1715d..e1937170cca6 100644
--- a/products/tasks/backend/tests/test_api.py
+++ b/products/tasks/backend/tests/test_api.py
@@ -1697,6 +1697,7 @@ class TestTaskAPI(BaseTaskAPITest):
(Task.OriginProduct.ONBOARDING,),
(Task.OriginProduct.SIGNALS_CHAT,),
(Task.OriginProduct.TASK_ANALYSIS,),
+ (Task.OriginProduct.REVIEW_HOG,),
]
)
def test_create_task_rejects_server_created_origin(self, origin_product: Task.OriginProduct):
diff --git a/services/llm-gateway/src/llm_gateway/products/config.py b/services/llm-gateway/src/llm_gateway/products/config.py
index fe44ce706f82..8c482e46a863 100644
--- a/services/llm-gateway/src/llm_gateway/products/config.py
+++ b/services/llm-gateway/src/llm_gateway/products/config.py
@@ -160,8 +160,9 @@ PRODUCTS: Final[dict[str, ProductConfig]] = {
"gpt-5.2",
"gpt-5-mini",
"gpt-5.6-luna",
- # ReviewHog sandbox runs route here (no review_hog entry in the agent's
- # origin→product map), so its reviewer-experiment arms must be allowed.
+ # ReviewHog sandbox runs route here: the agent's legacy leg maps
+ # review_hog to this slug (LEGACY_PRODUCT_OVERRIDES in the desktop
+ # agent's gateway.ts), so its reviewer-experiment arms must be allowed.
"gpt-5.6-sol",
}
| BEDROCK_MODELS