: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
perf(signals): resolve inbox-hidden report ids once for the report list (#122994)
posthog[bot]5fc360e06cf6
diff --git a/products/signals/backend/scout_authorship.py b/products/signals/backend/scout_authorship.pyindex d467fc395795..95eec76ec9cd 100644--- a/products/signals/backend/scout_authorship.py+++ b/products/signals/backend/scout_authorship.py@@ -8,10 +8,11 @@ on a request path resolve it from Postgres here.from __future__ import annotations-from django.db.models import CharField, Exists, OuterRef, Q, QuerySet-from django.db.models.functions import Cast, JSONArray+from django.db import connection+from django.db.models import BooleanField, Q, QuerySet+from django.db.models.expressions import RawSQL-from products.signals.backend.models import SignalScoutConfig, SignalScoutRun+from products.signals.backend.models import SignalReport, SignalScoutConfig, SignalScoutRunfrom products.skills.backend.models.skills import LLMSkill@@ -133,23 +134,37 @@ def report_is_from_background_scout(team_id: int, report_id: str) -> bool:def _inbox_opted_out_runs(team_id: int) -> QuerySet[SignalScoutRun]:# Matches `scout_lists_reports_in_inbox`: only an explicit `inbox: false` opts a scout out.- # A subquery rather than a join, so the inbox list query keeps its join-free plan.opted_out_configs = SignalScoutConfig.objects.for_team(team_id).filter(output_destinations__inbox=False)return SignalScoutRun.objects.for_team(team_id).filter(scout_config_id__in=opted_out_configs.values("id"))-def inbox_hidden_report_exists(team_id: int) -> Exists:- """Matches a report kept out of the inbox because the scout that authored it turned off its inbox destination.+def inbox_hidden_report_filter(team_id: int) -> RawSQL:+ """Keeps a report out of the inbox list when the scout that authored it turned off its inbox destination.Only authorship counts, read from the config's current setting: an opted-out scout that editsanother source's report does not hide it, and turning the scout back on lists its reports again.- Use it in a filter on a `SignalReport` queryset, so the check stays in the database.+ Use it in a filter on a `SignalReport` queryset. The `NOT EXISTS` correlates on plain equality+ with the report id, so Postgres plans it as an anti-join. A JSONB containment check, or a+ `NOT IN` over the expanded ids, can fall back to a probe once for each report in the team."""- return Exists(- _inbox_opted_out_runs(team_id).filter(- emitted_report_ids__contains=JSONArray(Cast(OuterRef("id"), output_field=CharField()))- )- )+ quote = connection.ops.quote_name+ # Trusted identifiers: from Django ORM metadata. Values are parameterized.+ runs = quote(SignalScoutRun._meta.db_table)+ configs = quote(SignalScoutConfig._meta.db_table)+ reports = quote(SignalReport._meta.db_table)+ sql = f"""NOT EXISTS (+ SELECT 1+ FROM {runs} AS hidden_run+ CROSS JOIN LATERAL jsonb_array_elements_text(hidden_run.emitted_report_ids) AS hidden(report_id)+ WHERE hidden_run.team_id = %s+ AND jsonb_typeof(hidden_run.emitted_report_ids) = 'array'+ AND hidden_run.scout_config_id IN (+ SELECT id FROM {configs} WHERE team_id = %s AND output_destinations -> 'inbox' = 'false'::jsonb+ )+ AND hidden.report_id = {reports}.id::text+ )"""+ # nosemgrep: python.django.security.audit.raw-query.avoid-raw-sql (identifiers from ORM metadata, values parameterized)+ return RawSQL(sql, (team_id, team_id), output_field=BooleanField())def report_is_hidden_from_inbox(team_id: int, report_id: str) -> bool:diff --git a/products/signals/backend/test/test_signal_report_api.py b/products/signals/backend/test/test_signal_report_api.pyindex 68c1146a487c..12dcbc3150c4 100644--- a/products/signals/backend/test/test_signal_report_api.py+++ b/products/signals/backend/test/test_signal_report_api.py@@ -423,14 +423,19 @@ class TestSignalReportListAPI(APIBaseTest):scout_config=config,skill_name=config.skill_name,skill_version=1,- emitted_report_ids=[str(authored.id)],+ emitted_report_ids=[str(authored.id), None],edited_report_ids=[str(edited_only.id)],)- default_ids = {row["id"] for row in self.client.get(self._list_url()).json()["results"]}+ with CaptureQueriesContext(connection) as ctx:+ default_ids = {row["id"] for row in self.client.get(self._list_url()).json()["results"]}+ report_list_sql = [q["sql"] for q in ctx.captured_queries if 'FROM "signals_signalreport"' in q["sql"]]opt_in_ids = {row["id"] for row in self.client.get(self._list_url(include_unlisted="true")).json()["results"]}retrieve_response = self.client.get(f"{self._list_url()}{authored.id}/")+ assert report_list_sql+ # A per-report containment probe of the scout run table runs before the page limit and slows every inbox load.+ assert not any("@>" in sql for sql in report_list_sql)assert default_ids == {str(listed.id), str(edited_only.id)}assert opt_in_ids == {str(listed.id), str(edited_only.id), str(authored.id)}assert retrieve_response.status_code == status.HTTP_200_OK@@ -2401,7 +2406,7 @@ class TestSignalReportListAPI(APIBaseTest):self._create_implementation_task_with_run(report_with_pr, pr_url="https://github.com/org/repo/pull/42")with CaptureQueriesContext(connection) as ctx:- # `include_unlisted` drops the inbox-listing subquery, which also aliases a table as V0.+ # `include_unlisted` drops the inbox-listing filter, whose lateral join would match the JOIN check below.response = self.client.get(self._list_url(has_implementation_pr="true", count_only="true", include_unlisted="true"))diff --git a/products/signals/backend/views.py b/products/signals/backend/views.pyindex f7895f1eb94a..10221e69c42e 100644--- a/products/signals/backend/views.py+++ b/products/signals/backend/views.py@@ -238,7 +238,7 @@ from products.signals.backend.report_task_actions import (from products.signals.backend.reviewer_correction_notes import ReviewerCorrection, forward_reviewer_correction_notefrom products.signals.backend.reviewer_pr_assignment import schedule_reviewer_pr_assignmentfrom products.signals.backend.reviewer_rules import ReviewerRules, ReviewerWriteError, StaleReviewerProposal-from products.signals.backend.scout_authorship import inbox_hidden_report_exists+from products.signals.backend.scout_authorship import inbox_hidden_report_filterfrom products.signals.backend.scout_harness.trial_access import trial_state_errors, trial_store_for_requestfrom products.signals.backend.scout_report.trial_inbox import TrialInboxReads, private_report_artefactsfrom products.signals.backend.serializers import (@@ -1606,7 +1606,7 @@ class SignalReportViewSet(# List-only: a report a scout kept out of the inbox stays reachable by id and by its other actions.if self.action != "list" or self._bool_query_param("include_unlisted") is True:return queryset- return queryset.exclude(inbox_hidden_report_exists(self.team_id))+ return queryset.filter(inbox_hidden_report_filter(self.team_id))def _apply_signal_report_search_filter(self, queryset):search = self.request.query_params.get("search")