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): bound the scout findings summary join to the newest runs (#118327)

posthog[bot]2ce179fcebda
diff --git a/products/signals/backend/scout_harness/tools/runs.py b/products/signals/backend/scout_harness/tools/runs.py
index d397b92badb2..11d16f9be6ff 100644
--- a/products/signals/backend/scout_harness/tools/runs.py
+++ b/products/signals/backend/scout_harness/tools/runs.py
@@ -117,6 +117,9 @@ DEFAULT_FINDINGS_WINDOW_HOURS = 72
MAX_FINDINGS_WINDOW_HOURS = 168
FLEET_FINDINGS_SUMMARY_RUN_CAP = 120
FLEET_FINDINGS_SUMMARY_REPORT_CAP = 50
+# The completion-time sort has no index, so it only ranks this many of the newest output runs by
+# `created_at`. The slack over the run cap keeps a run that completes after newer runs in the set.
+FLEET_FINDINGS_SUMMARY_CANDIDATE_RUNS = 2 * FLEET_FINDINGS_SUMMARY_RUN_CAP
# `failure_reason` is the concise, list-safe derived signal; `error` carries the full
# `TaskRun.error_message`. Bound the derived reason so it stays cheap to scan in bulk.
@@ -527,12 +530,17 @@ def fleet_findings_summary(*, team_id: int, window_hours: int = DEFAULT_FINDINGS
touched_a_report = (~Q(emitted_report_ids=[]) & ~Q(emitted_report_ids__isnull=True)) | (
~Q(edited_report_ids=[]) & ~Q(edited_report_ids__isnull=True)
)
- # Order by completion (fall back to creation) so the cap keeps the *most recently emitted* runs,
- # matching the frontend's `completed_at ?? created_at` sort; `-id` tie-breaks on the time-ordered PK.
- rows = (
+ candidate_ids = (
SignalScoutRun.objects.filter(team_id=team_id, created_at__gte=window_start)
.exclude(metadata__has_key="scout_trial")
.filter(Q(emitted_count__gt=0) | touched_a_report)
+ .order_by("-created_at", "-id")
+ .values("id")[:FLEET_FINDINGS_SUMMARY_CANDIDATE_RUNS]
+ )
+ # Order by completion (fall back to creation) so the cap keeps the *most recently emitted* runs,
+ # matching the frontend's `completed_at ?? created_at` sort; `-id` tie-breaks on the time-ordered PK.
+ rows = (
+ SignalScoutRun.objects.filter(team_id=team_id, id__in=candidate_ids)
.annotate(_emitted_at=Coalesce("task_run__completed_at", "created_at"))
.order_by("-_emitted_at", "-id")
.values_list("emitted_count", "skill_name", "_emitted_at", "emitted_report_ids", "edited_report_ids")[
diff --git a/products/signals/backend/test/test_scout_harness_api.py b/products/signals/backend/test/test_scout_harness_api.py
index b0e2bab7ca92..c510da00337a 100644
--- a/products/signals/backend/test/test_scout_harness_api.py
+++ b/products/signals/backend/test/test_scout_harness_api.py
@@ -865,6 +865,24 @@ class TestScoutHarnessFindingsSummaryAPI(APIBaseTest):
# or the callout advertises a scout the findings page's filter won't show.
assert body["scout_count"] == 1
+ def test_summary_caps_runs_by_completion_among_the_newest_candidates(self) -> None:
+ # The page keeps the most recently *completed* runs, so a run that completes after a newer
+ # one must win the cap. Runs older than the candidate set are never ranked.
+ now = timezone.now()
+ timings = {"outside": (3, 0), "late": (2, 1), "newest": (1, 50)}
+ for emitted_count, (name, (created_hours_ago, completed_minutes_ago)) in zip((4, 2, 1), timings.items()):
+ run = _make_run(self.team, skill_name=f"signals-scout-{name}", emitted_count=emitted_count)
+ SignalScoutRun.objects.filter(id=run.id).update(created_at=now - timedelta(hours=created_hours_ago))
+ run.task_run.completed_at = now - timedelta(minutes=completed_minutes_ago)
+ run.task_run.save(update_fields=["completed_at"])
+ with (
+ patch("products.signals.backend.scout_harness.tools.runs.FLEET_FINDINGS_SUMMARY_RUN_CAP", 1),
+ patch("products.signals.backend.scout_harness.tools.runs.FLEET_FINDINGS_SUMMARY_CANDIDATE_RUNS", 2),
+ ):
+ response = self.client.get(self._url())
+ assert response.status_code == status.HTTP_200_OK
+ assert response.json()["count"] == 2
+
def test_summary_excludes_runs_outside_the_window(self) -> None:
# Guards the `created_at` window filter: a finding emitted before the lookback must not count,
# else the callout would advertise stale findings the findings page won't show.