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): stop loading full task run rows in scout run reads (#117485)

posthog[bot]6a5807bcc4f7
diff --git a/products/signals/backend/scout_harness/tools/runs.py b/products/signals/backend/scout_harness/tools/runs.py
index edc7b8e57e6d..d9e9ed5f07ee 100644
--- a/products/signals/backend/scout_harness/tools/runs.py
+++ b/products/signals/backend/scout_harness/tools/runs.py
@@ -43,6 +43,27 @@ if TYPE_CHECKING:
DEFAULT_RUN_SEARCH_LIMIT = 20
MAX_RUN_SEARCH_LIMIT = 100
+# The columns `_to_summary` reads, so the run reads skip the large TaskRun JSON columns (`output`,
+# `artifacts`, `state`). Add a field here when `_to_summary` reads it, or each row costs one more query.
+RUN_SUMMARY_FIELDS = (
+ "id",
+ "skill_name",
+ "skill_version",
+ "created_at",
+ "summary",
+ "emitted_count",
+ "emitted_finding_ids",
+ "emitted_report_ids",
+ "edited_report_ids",
+ "metadata",
+ "task_run__id",
+ "task_run__task_id",
+ "task_run__status",
+ "task_run__created_at",
+ "task_run__completed_at",
+ "task_run__error_message",
+)
+
# The per-scout run window backing the inbox scout surfaces. 25 runs covers a day for an hourly
# scout and a month for a daily one — the point being that both get enough history to judge, which
# a single fleet-wide time window can't do.
@@ -214,6 +235,7 @@ def search_recent_runs(
SignalScoutRun.objects.filter(team_id=team_id)
.exclude(metadata__has_key="scout_trial")
.select_related("task_run")
+ .only(*RUN_SUMMARY_FIELDS)
.order_by("-created_at")
)
if date_from is not None:
@@ -386,6 +408,7 @@ def recent_runs_per_scout(
rows = (
SignalScoutRun.objects.filter(team_id=team_id, id__in=run_ids)
.select_related("task_run")
+ .only(*RUN_SUMMARY_FIELDS)
.order_by("-created_at")
)
return _without_deleted_reports([_to_summary(row, team_id=team_id) for row in rows], team_id=team_id)
@@ -513,7 +536,12 @@ def get_run(*, team_id: int, run_id: str) -> RunDetail | None:
Team scoping is non-negotiable: a run row from another team must not be
readable, even if the caller knows the UUID.
"""
- row = SignalScoutRun.objects.select_related("task_run").filter(team_id=team_id, id=run_id).first()
+ row = (
+ SignalScoutRun.objects.select_related("task_run")
+ .only(*RUN_SUMMARY_FIELDS)
+ .filter(team_id=team_id, id=run_id)
+ .first()
+ )
if row is None:
return None
return _to_detail(row, team_id=team_id)
diff --git a/products/signals/backend/test/test_scout_harness_tools.py b/products/signals/backend/test/test_scout_harness_tools.py
index 937cfb9870f3..952004753a82 100644
--- a/products/signals/backend/test/test_scout_harness_tools.py
+++ b/products/signals/backend/test/test_scout_harness_tools.py
@@ -11,7 +11,9 @@ from posthog.test.base import BaseTest
from unittest.mock import AsyncMock, MagicMock, patch
from django.apps import apps
+from django.db import connection
from django.test import override_settings
+from django.test.utils import CaptureQueriesContext
from django.utils import timezone
import pytest_asyncio
@@ -86,6 +88,8 @@ from products.signals.backend.scout_harness.tools.runs import (
MAX_FAILURE_REASON_LENGTH,
MAX_RUN_SEARCH_LIMIT,
MAX_SCOUTS_PER_RUNS_QUERY,
+ RunDetail,
+ RunSummary,
recent_runs_per_scout,
)
from products.signals.backend.scout_harness.tools.scratchpad import (
@@ -512,6 +516,32 @@ class TestGetRun(BaseTest):
assert detail is None # team scoping enforced even when caller knows the UUID
+class TestRunReadsSkipTaskRunPayload(BaseTest):
+ @parameterized.expand(["search_recent_runs", "recent_runs_per_scout", "get_run"])
+ def test_reads_task_run_columns_in_one_query_without_json_payload(self, reader: str) -> None:
+ TaskRun = apps.get_model("tasks", "TaskRun")
+ SignalScoutConfig.objects.create(team=self.team, skill_name="signals-scout-errors")
+ runs = [_create_run(self.team), _create_run(self.team, task_run_status=TaskRun.Status.FAILED)]
+ TaskRun.objects.filter(id=runs[1].task_run_id).update(error_message="boom")
+
+ hits: list[RunSummary] | list[RunDetail]
+ with CaptureQueriesContext(connection) as captured:
+ if reader == "search_recent_runs":
+ hits = search_recent_runs(team_id=self.team.id)
+ elif reader == "recent_runs_per_scout":
+ hits = recent_runs_per_scout(team_id=self.team.id)
+ else:
+ detail = get_run(team_id=self.team.id, run_id=str(runs[1].id))
+ hits = [detail] if detail is not None else []
+
+ task_run_queries = [q["sql"] for q in captured.captured_queries if '"posthog_task_run"' in q["sql"]]
+ assert len(task_run_queries) == 1
+ for column in ("output", "artifacts", "state"):
+ assert f'"posthog_task_run"."{column}"' not in task_run_queries[0]
+ failed = next(hit for hit in hits if hit.run_id == str(runs[1].id))
+ assert (failed.status, failed.error, failed.task_run_id) == ("failed", "boom", str(runs[1].task_run_id))
+
+
class TestRemember(BaseTest):
def test_creates_entry_with_default_lineage(self) -> None:
entry = remember(team_id=self.team.id, key="known-noise", content="ignore /favicon 404s")