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): team-scope the PR-tab report association filter (#94228)

José Sequeiraeeeec2236179
diff --git a/products/signals/backend/models.py b/products/signals/backend/models.py
index 516f70d188fd..93ec2550aedd 100644
--- a/products/signals/backend/models.py
+++ b/products/signals/backend/models.py
@@ -749,7 +749,7 @@ class SignalReport(UUIDModel):
return models.Q(id__in=artefact_report_ids) | models.Q(id__in=legacy_report_ids)
@staticmethod
- def reports_for_task_ids_filter(task_ids: Any) -> "models.Q":
+ def reports_for_task_ids_filter(task_ids: Any, *, team_id: int | None = None) -> "models.Q":
"""`reports_for_task_filter` widened to a *set* of tasks: a `Q` on `SignalReport.id` matching
the reports associated with any task in `task_ids` (a collection or, preferably, a `task_id`
subquery), unified across the `task_run` artefact log and the legacy `SignalReportTask` gate
@@ -758,12 +758,20 @@ class SignalReport(UUIDModel):
Lets a per-report correlated `Exists` over `tasks.TaskRun` be *decorrelated*: drive off the
small task set (e.g. tasks that produced a PR) and map it to reports here via the indexed
`task_id` columns, instead of probing the runs once per candidate report.
+
+ Pass `team_id` whenever the caller works within one team. Association rows are always
+ same-team as their report, so the scope drops no valid matches, and without it the planner
+ can invert the join and scan every team's `task_run` artefacts when `task_ids` is a
+ subquery. Only cross-team callers with literal `task_ids` (the PR webhook) leave it unset.
"""
- artefact_report_ids = SignalReportArtefact.objects.filter(
+ artefact_rows = SignalReportArtefact.objects.filter(
type=SignalReportArtefact.ArtefactType.TASK_RUN, task_id__in=task_ids
- ).values("report_id")
- legacy_report_ids = SignalReportTask.objects.filter(task_id__in=task_ids).values("report_id")
- return models.Q(id__in=artefact_report_ids) | models.Q(id__in=legacy_report_ids)
+ )
+ legacy_rows = SignalReportTask.objects.filter(task_id__in=task_ids)
+ if team_id is not None:
+ artefact_rows = artefact_rows.filter(team_id=team_id)
+ legacy_rows = legacy_rows.filter(team_id=team_id)
+ return models.Q(id__in=artefact_rows.values("report_id")) | models.Q(id__in=legacy_rows.values("report_id"))
class SignalEmissionRecord(UUIDModel):
diff --git a/products/signals/backend/test/test_signal_report_api.py b/products/signals/backend/test/test_signal_report_api.py
index 9eea2a490619..378096df5791 100644
--- a/products/signals/backend/test/test_signal_report_api.py
+++ b/products/signals/backend/test/test_signal_report_api.py
@@ -986,6 +986,27 @@ class TestSignalReportListAPI(APIBaseTest):
without_pr = self.client.get(self._list_url(has_implementation_pr="false"))
assert str(report_empty_pr.id) in {r["id"] for r in without_pr.json()["results"]}
+ def test_filter_has_implementation_pr_scopes_association_subqueries_to_team(self):
+ report_with_pr = self._create_report(title="Report with PR")
+ self._create_implementation_task_with_run(report_with_pr, pr_url="https://github.com/org/repo/pull/42")
+
+ with CaptureQueriesContext(connection) as ctx:
+ response = self.client.get(self._list_url(has_implementation_pr="true"))
+
+ assert response.status_code == status.HTTP_200_OK
+ # Both association subqueries must stay team-scoped. Without the scope the planner can
+ # invert the join and scan every team's task_run artefacts per request.
+ filter_sql = [
+ query["sql"]
+ for query in ctx.captured_queries
+ if 'FROM "signals_signalreport"' in query["sql"] and "signals_signalreporttask" in query["sql"]
+ ]
+ assert filter_sql
+ for sql in filter_sql:
+ # Django aliases both association tables as V0; two scoped subqueries means two
+ # V0-qualified team filters.
+ assert sql.count(f'V0."team_id" = {self.team.id}') == 2
+
def test_filter_has_implementation_pr_absent_returns_all(self):
report_with_pr = self._create_report(title="Report with PR")
report_without_pr = self._create_report(title="Report without PR")
diff --git a/products/signals/backend/views.py b/products/signals/backend/views.py
index 1925511e6769..eb281b197839 100644
--- a/products/signals/backend/views.py
+++ b/products/signals/backend/views.py
@@ -1123,7 +1123,8 @@ class SignalReportViewSet(
# `Exists` over `tasks.TaskRun` evaluated once per candidate report (which made the inbox
# PR-tab count scan the whole `ready` set per PR'd run).
return SignalReport.reports_for_task_ids_filter(
- tasks_facade.task_ids_with_pr_url_subquery(self.team.id, pr_bearing_task_run_filter())
+ tasks_facade.task_ids_with_pr_url_subquery(self.team.id, pr_bearing_task_run_filter()),
+ team_id=self.team.id,
)
def _apply_signal_report_implementation_pr_filter(self, queryset):