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.

fix(signals): route skill-tagged scout runs to the ai-gateway (#84976)

Brandon37b4db1806ff
diff --git a/products/desktop/packages/agent/src/utils/gateway.test.ts b/products/desktop/packages/agent/src/utils/gateway.test.ts
index 994d84c18dac..35ddf8544035 100644
--- a/products/desktop/packages/agent/src/utils/gateway.test.ts
+++ b/products/desktop/packages/agent/src/utils/gateway.test.ts
@@ -291,6 +291,76 @@ describe("resolveGatewayTarget", () => {
).toBe(true);
});
+ it("routes a skill-qualified scout stage via the plain product entry", () => {
+ expect(
+ resolveGatewayTarget({
+ product: "signals",
+ aiStage: "scout:web-analytics",
+ posthogHost: PY_HOST,
+ env: SIGNALS_ENV,
+ }),
+ ).toEqual({ baseUrl: GO, isAiGateway: true, aiProduct: "signals_scout" });
+ });
+
+ it("routes only the listed skill via a skill-qualified entry", () => {
+ const env = {
+ AI_GATEWAY_URL: GO,
+ AI_GATEWAY_PRODUCTS: "signals_scout:web-analytics",
+ };
+ expect(
+ resolveGatewayTarget({
+ product: "signals",
+ aiStage: "scout:web-analytics",
+ posthogHost: PY_HOST,
+ env,
+ }),
+ ).toEqual({ baseUrl: GO, isAiGateway: true, aiProduct: "signals_scout" });
+ expect(
+ resolveGatewayTarget({
+ product: "signals",
+ aiStage: "scout:logs",
+ posthogHost: PY_HOST,
+ env,
+ }),
+ ).toEqual({
+ baseUrl: "https://gateway.us.posthog.com/signals",
+ isAiGateway: false,
+ aiProduct: "signals_scout",
+ });
+ });
+
+ it("does not let a skill-qualified entry route the bare scout stage", () => {
+ expect(
+ resolveGatewayTarget({
+ product: "signals",
+ aiStage: "scout",
+ posthogHost: PY_HOST,
+ env: {
+ AI_GATEWAY_URL: GO,
+ AI_GATEWAY_PRODUCTS: "signals_scout:web-analytics",
+ },
+ }).isAiGateway,
+ ).toBe(false);
+ });
+
+ it("routes the custom_agent stage when signals_custom_agent is listed", () => {
+ expect(
+ resolveGatewayTarget({
+ product: "signals",
+ aiStage: "custom_agent",
+ posthogHost: PY_HOST,
+ env: {
+ AI_GATEWAY_URL: GO,
+ AI_GATEWAY_PRODUCTS: "signals_custom_agent",
+ },
+ }),
+ ).toEqual({
+ baseUrl: GO,
+ isAiGateway: true,
+ aiProduct: "signals_custom_agent",
+ });
+ });
+
it("honours an LLM_GATEWAY_URL override on the unrouted path", () => {
expect(
resolveGatewayTarget({
@@ -311,10 +381,18 @@ describe("resolveAiProduct", () => {
["research", "signals_research"],
["implementation", "signals_implementation"],
["repo_selection", "signals_repo_selection"],
+ ["custom_agent", "signals_custom_agent"],
])("maps the signals %s stage to %s", (aiStage, expected) => {
expect(resolveAiProduct({ product: "signals", aiStage })).toBe(expected);
});
+ it.each([
+ ["scout:web-analytics", "signals_scout"],
+ ["scout:customer-analytics-billing-and-usage", "signals_scout"],
+ ])("maps the skill-qualified %s stage to %s", (aiStage, expected) => {
+ expect(resolveAiProduct({ product: "signals", aiStage })).toBe(expected);
+ });
+
it("leaves signals unsplit for an unrecognized stage", () => {
expect(resolveAiProduct({ product: "signals", aiStage: "match" })).toBe(
"signals",
diff --git a/products/desktop/packages/agent/src/utils/gateway.ts b/products/desktop/packages/agent/src/utils/gateway.ts
index e67a48d03ef5..898749c766e3 100644
--- a/products/desktop/packages/agent/src/utils/gateway.ts
+++ b/products/desktop/packages/agent/src/utils/gateway.ts
@@ -99,8 +99,19 @@ const SIGNALS_STAGE_PRODUCTS = new Set([
"research",
"implementation",
"repo_selection",
+ "custom_agent",
]);
+/**
+ * Scout runs qualify their stage with the skill (`scout:web-analytics`) for
+ * per-scout cost attribution. The product stays `signals_scout`.
+ */
+const SCOUT_STAGE_PREFIX = "scout:";
+
+function signalsStage(aiStage: string): string {
+ return aiStage.startsWith(SCOUT_STAGE_PREFIX) ? "scout" : aiStage;
+}
+
/**
* The `ai_product` value sent to the Go gateway, which has no product route and
* so cannot infer it from the path.
@@ -117,8 +128,11 @@ export function resolveAiProduct({
product: GatewayProduct;
aiStage?: string | null;
}): string {
- if (product === "signals" && aiStage && SIGNALS_STAGE_PRODUCTS.has(aiStage)) {
- return `signals_${aiStage}`;
+ if (product === "signals" && aiStage) {
+ const stage = signalsStage(aiStage);
+ if (SIGNALS_STAGE_PRODUCTS.has(stage)) {
+ return `signals_${stage}`;
+ }
}
return product;
}
@@ -150,6 +164,10 @@ export interface GatewayTarget {
* Both must be set: an unlisted product, or a listed one with no URL, stays on
* the Python gateway. Migration is therefore opt-in per product, and clearing
* either value rolls everything back.
+ *
+ * Scout entries may qualify by skill (`signals_scout:web-analytics`), matching
+ * only runs of that skill, so the scout fleet can migrate in batches. A plain
+ * `signals_scout` entry matches every skill.
*/
export function resolveGatewayTarget({
product,
@@ -164,9 +182,14 @@ export function resolveGatewayTarget({
}): GatewayTarget {
const aiProduct = resolveAiProduct({ product, aiStage });
const aiGatewayUrl = (env.AI_GATEWAY_URL ?? "").trim();
+ const routedProducts = parseAiGatewayProducts(env.AI_GATEWAY_PRODUCTS);
+ const skillQualified = aiStage?.startsWith(SCOUT_STAGE_PREFIX)
+ ? `${aiProduct}:${aiStage.slice(SCOUT_STAGE_PREFIX.length)}`
+ : null;
const routed =
aiGatewayUrl !== "" &&
- parseAiGatewayProducts(env.AI_GATEWAY_PRODUCTS).has(aiProduct);
+ (routedProducts.has(aiProduct) ||
+ (skillQualified !== null && routedProducts.has(skillQualified)));
if (routed) {
return {
diff --git a/products/signals/backend/custom_agent/base.py b/products/signals/backend/custom_agent/base.py
index 2d13d8e7d840..1254a55b05a0 100644
--- a/products/signals/backend/custom_agent/base.py
+++ b/products/signals/backend/custom_agent/base.py
@@ -680,6 +680,7 @@ Return only a JSON object matching this schema. Do not include markdown fences o
context=context,
step_name=label,
origin_product=tasks_facade.TaskOriginProduct.SIGNAL_REPORT,
+ ai_stage=STEP_CUSTOM_AGENT,
internal=True,
)
self._session = session
diff --git a/products/signals/backend/test/test_custom_agent_gateway_stage.py b/products/signals/backend/test/test_custom_agent_gateway_stage.py
new file mode 100644
index 000000000000..fc3f8025586e
--- /dev/null
+++ b/products/signals/backend/test/test_custom_agent_gateway_stage.py
@@ -0,0 +1,40 @@
+"""Pins the custom-agent run's ai_stage stamp: it must equal STEP_CUSTOM_AGENT so
+`signals-pipeline-models` payload authors target the vocabulary they see in LLM
+analytics. Deleting the kwarg keeps every other suite green."""
+
+from unittest.mock import AsyncMock, MagicMock, patch
+
+from asgiref.sync import async_to_sync
+
+import products.signals.backend.temporal.custom_agent # noqa: F401 (warms the base<->temporal import cycle so standalone collection works)
+from products.signals.backend.agent_runtime import STEP_CUSTOM_AGENT, AgentRuntime
+from products.signals.backend.custom_agent.base import CustomSignalAgent
+
+
+def test_send_raw_stamps_custom_agent_stage():
+ agent = CustomSignalAgent.__new__(CustomSignalAgent)
+ agent._session = None
+ agent.team_id = 123
+ agent.user_id = 456
+ agent._resolved_repository = None
+ agent.model = "claude-sonnet-4-6"
+
+ start_raw = AsyncMock(return_value=(MagicMock(), "ok"))
+ with (
+ patch(
+ "products.signals.backend.custom_agent.base.get_or_create_signals_sandbox_env",
+ return_value="env-id",
+ ),
+ patch(
+ "products.signals.backend.custom_agent.base.resolve_agent_runtime",
+ return_value=AgentRuntime(),
+ ),
+ patch(
+ "products.signals.backend.custom_agent.base.MultiTurnSession.start_raw",
+ start_raw,
+ ),
+ ):
+ result = async_to_sync(agent._send_raw)("prompt", label="step-1")
+
+ assert result == "ok"
+ assert start_raw.call_args.kwargs["ai_stage"] == STEP_CUSTOM_AGENT
diff --git a/products/signals/backend/test/test_custom_agent_log_artefacts.py b/products/signals/backend/test/test_custom_agent_log_artefacts.py
index a23abf8964f2..15614dd0879c 100644
--- a/products/signals/backend/test/test_custom_agent_log_artefacts.py
+++ b/products/signals/backend/test/test_custom_agent_log_artefacts.py
@@ -7,6 +7,7 @@ from unittest.mock import AsyncMock, patch
from asgiref.sync import async_to_sync
from pydantic import BaseModel
+import products.signals.backend.temporal.custom_agent # noqa: F401 (warms the base<->temporal import cycle so standalone collection works)
from products.signals.backend.artefact_schemas import ArtefactContentValidationError, CodeReference, NoteArtefact
from products.signals.backend.custom_agent.base import NO_REPO, CustomSignalAgent
from products.signals.backend.custom_agent.persistence import (