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.

perf(signals): look up the scout behind a task by run id (#120860)

posthog[bot]9f4ddfea44c4
diff --git a/products/signals/backend/facade/activity_client.py b/products/signals/backend/facade/activity_client.py
index 3438b80f4fe8..447370cecde3 100644
--- a/products/signals/backend/facade/activity_client.py
+++ b/products/signals/backend/facade/activity_client.py
@@ -24,10 +24,19 @@ def resolve_scout_client_tag(*, sandbox_task_id: UUID, team_id: int) -> str | No
A task with several runs (a retry) is still one scout, so any of its rows answers the
question. The ordering only keeps repeated reads of the same task consistent.
+
+ The run ids are read first so both lookups use a key index. A join filtered on the task
+ lets the planner walk the team's whole run history when no scout run matches.
"""
+ # `posthog.auth` imports this module, and the tasks facade imports DRF, which imports `posthog.auth`.
+ from products.tasks.backend.facade import api as tasks_facade # noqa: PLC0415 - breaks the auth import cycle
+
+ task_run_ids = tasks_facade.list_task_run_ids(sandbox_task_id, team_id)
+ if not task_run_ids:
+ return None
skill_name = (
SignalScoutRun.objects.for_team(team_id)
- .filter(task_run__task_id=sandbox_task_id)
+ .filter(task_run_id__in=task_run_ids)
.order_by("-created_at")
.values_list("skill_name", flat=True)
.first()
diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.py
index 5c0bfecf14e5..2a4009a14c55 100644
--- a/products/tasks/backend/facade/api.py
+++ b/products/tasks/backend/facade/api.py
@@ -348,6 +348,7 @@ __all__ = [
"list_task_run_living_artifacts",
"list_task_run_peers",
"list_task_repositories",
+ "list_task_run_ids",
"list_task_runs",
"list_tasks",
"list_workflow_last_runs",
@@ -3200,6 +3201,11 @@ def task_accessible_for_run_view(
return task is not None and bool(task["accessible"])
+def list_task_run_ids(task_id: str | UUID, team_id: int) -> list[UUID]:
+ """Ids of every run of a task, team-scoped. Caller enforces task visibility."""
+ return list(TaskRun.objects.filter(team_id=team_id, task_id=task_id).values_list("id", flat=True))
+
+
def list_task_runs(
task_id: str | UUID,
team_id: int,