: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
feat(signals): tag scout generations with the scout that ran (#75691)
posthog[bot]4a79742bf843
diff --git a/products/signals/backend/agent_runtime.py b/products/signals/backend/agent_runtime.pyindex 9ebf81dee4af..58405b5763d2 100644--- a/products/signals/backend/agent_runtime.py+++ b/products/signals/backend/agent_runtime.py@@ -57,7 +57,8 @@ PIPELINE_MODELS_DISCOVERY_DISTINCT_ID = "internal_signals_pipeline_models_discovWILDCARD = "*"# Step names match the `ai_stage` tags on $ai_generation so payload authors target the-# vocabulary they see in LLM analytics.+# vocabulary they see in LLM analytics. Scout generations tag `scout:<skill>` for per-scout+# cost attribution; the step key here stays the bare stage.STEP_SCOUT = "scout"STEP_RESEARCH = "research"STEP_REPO_SELECTION = "repo_selection"diff --git a/products/signals/backend/scout_harness/runner.py b/products/signals/backend/scout_harness/runner.pyindex 445e04c83f74..dbd8a3abdc86 100644--- a/products/signals/backend/scout_harness/runner.py+++ b/products/signals/backend/scout_harness/runner.py@@ -19,7 +19,7 @@ from posthog.sync import database_sync_to_asyncfrom products.signals.backend.agent_runtime import STEP_SCOUT, resolve_agent_runtimefrom products.signals.backend.models import SignalScoutConfig, SignalScoutRunfrom products.signals.backend.scout_harness.derived_metadata import stamp_derived_metadata-from products.signals.backend.scout_harness.lazy_seed import sync_canonical_skills+from products.signals.backend.scout_harness.lazy_seed import canonical_skill_names, sync_canonical_skillsfrom products.signals.backend.scout_harness.limits import DEFAULT_MAX_RUNTIME_S, STALE_RUN_CUTOFF_Sfrom products.signals.backend.scout_harness.model_selection import resolve_scout_modelfrom products.signals.backend.scout_harness.prompt import (@@ -28,6 +28,7 @@ from products.signals.backend.scout_harness.prompt import (build_run_prompt,)from products.signals.backend.scout_harness.skill_loader import (+ SIGNALS_SCOUT_SKILL_PREFIX,LoadedSkill,load_skill_for_run,resolve_report_channel_variant,@@ -51,6 +52,10 @@ logger = logging.getLogger(__name__)# network, MCP read scopes injected. Split out later if the agent needs different policy.SIGNALS_SCOUT_SANDBOX_ENV_NAME = SIGNALS_REPORT_RESEARCH_ENV_NAME+# Every scout `ai_stage` starts with this, so `ai_stage LIKE 'scout:%'` rolls the whole fleet+# up as one stage even though the tag names the individual scout.+SCOUT_AI_STAGE_PREFIX = "scout:"+# The report channel (emit_report/edit_report) is opt-in per skill. A scout's sandbox token# carries the report-write scope ONLY when its skill listed one of these in `allowed_tools` (see# the posture selection where the sandbox context is built). A baseline scout never carries that@@ -532,11 +537,12 @@ async def _spawn_and_run(step_name=_step_name(skill),verbose=verbose,origin_product=tasks_facade.TaskOriginProduct.SIGNALS_SCOUT,- # Tag every scout $ai_generation with a coarse pipeline stage so scout spend is- # splittable out of the ai_product='signals' bucket (scouts carry no signal_report_id).- # Constant 'scout' keeps ai_stage a low-cardinality stage enum (peer of research /- # repo_selection / implementation); per-scout granularity comes from scout_name (task_title).- ai_stage="scout",+ # Tag every scout $ai_generation with its stage AND its scout, so scout spend is both+ # splittable out of the ai_product='signals' bucket (scouts carry no signal_report_id)+ # and attributable to one scout. `ai_stage` is the only run-shaped value the harness+ # controls that reaches $ai_generation — the rest of the properties there are stamped+ # by the agent server off the task row. Team attribution rides along as `team_id`.+ ai_stage=_ai_stage(skill),on_task_run_created=_create_bridge_row,# Keep the per-turn poll budget at the run's runtime cap so the dropped-finalization# salvage fires before the activity's `start_to_close_timeout` (DEFAULT_MAX_RUNTIME_S +@@ -943,3 +949,18 @@ def _step_name(skill: LoadedSkill) -> str:# Surfaces in the Task title and S3 log prefix. Keep terse — the sandbox truncates.safe = skill.name.replace(" ", "_")[:40]return f"signals_scout:{safe}"+++def _ai_stage(skill: LoadedSkill) -> str:+ """The `ai_stage` tag every $ai_generation of this run carries.++ `scout:<skill>` so LLM-analytics cost is breakdown-able per scout while the whole fleet+ stays selectable by the `scout:` prefix. Only canonical skill *names* go in the tag — a+ custom scout's name is team-authored, so admitting it would grow the cardinality of a+ stage tag with the fleet's teams. The gate is the name, not `skill.origin`: a canonical+ scout a team has edited in place is still one named scout across the fleet.+ """+ if skill.name not in canonical_skill_names():+ return f"{SCOUT_AI_STAGE_PREFIX}custom"+ short = skill.name.removeprefix(SIGNALS_SCOUT_SKILL_PREFIX)+ return f"{SCOUT_AI_STAGE_PREFIX}{short}"diff --git a/products/signals/backend/test/test_scout_harness.py b/products/signals/backend/test/test_scout_harness.pyindex a336c3b39027..fdeb1466616b 100644--- a/products/signals/backend/test/test_scout_harness.py+++ b/products/signals/backend/test/test_scout_harness.py@@ -34,7 +34,7 @@ from products.signals.backend.scout_harness.lazy_seed import HARNESS_SEEDED_BY,from products.signals.backend.scout_harness.limits import STALE_RUN_CUTOFF_Sfrom products.signals.backend.scout_harness.model_selection import ScoutModelfrom products.signals.backend.scout_harness.prompt import _REPORT_CHARTS, HARNESS_PROMPT_VERSION, build_run_prompt-from products.signals.backend.scout_harness.runner import RunResult, _create_run_row, arun_signals_scout+from products.signals.backend.scout_harness.runner import RunResult, _ai_stage, _create_run_row, arun_signals_scoutfrom products.signals.backend.scout_harness.skill_loader import (LoadedSkill,SkillNotFoundError,@@ -818,8 +818,9 @@ async def test_successful_run_creates_bridge_row_pointing_at_task_run(ateam, aer@pytest.mark.asyncio@pytest.mark.django_dbasync def test_run_tags_session_with_scout_ai_stage(ateam, aerrors_skill):- # Scouts pass ai_stage='scout' to the sandbox session so every $ai_generation carries it,- # letting scout spend be split out of the ai_product='signals' bucket (scouts have no report id).+ # Scouts pass a `scout:<skill>` ai_stage to the sandbox session so every $ai_generation+ # carries it, letting scout spend be split out of the ai_product='signals' bucket (scouts+ # have no report id) and attributed to one scout.session, result = await database_sync_to_async(_make_fake_session, thread_sensitive=False)(ateam)captured: dict = {}@@ -842,7 +843,32 @@ async def test_run_tags_session_with_scout_ai_stage(ateam, aerrors_skill):):await arun_signals_scout(team_id=ateam.id, skill_name="signals-scout-errors")- assert captured["ai_stage"] == "scout"+ # `signals-scout-errors` is not a canonical scout, so its team-authored name is withheld.+ assert captured["ai_stage"] == "scout:custom"+++ [+ ("canonical", "signals-scout-general", "scout:general"),+ ("team_authored", "signals-scout-our-own-thing", "scout:custom"),+ ]+)+def test_ai_stage_tag_only_carries_canonical_scout_names(_name, skill_name, expected):+ # Team-authored names must collapse to `custom`: ai_stage is a low-cardinality tag and the+ # fleet can enroll teams by wildcard, so admitting them grows it without bound.+ skill = LoadedSkill(+ name=skill_name,+ version=1,+ body="scout",+ description="",+ allowed_tools=[],+ files=[],+ skill_id="skill-1",+ # A canonical scout a team edited reads as `custom` here, and must still be named.+ origin="custom",+ authors=[],+ )+ assert _ai_stage(skill) == expected@pytest.mark.asyncio