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): cache inbox report ordering (#130240)

Dylan Martinc0c5491a7cd7
diff --git a/docs/internal/inbox-ranking-training.md b/docs/internal/inbox-ranking-training.md
index 7620b3966c4e..da3c6710436a 100644
--- a/docs/internal/inbox-ranking-training.md
+++ b/docs/internal/inbox-ranking-training.md
@@ -26,6 +26,17 @@ The daily snapshots and selectable scoring-moment and report grains remain avail
See the [ranking DAG README](../../products/signals/dags/inbox_ranking/README.md) for the feature-set contract and operating instructions.
+## Cached Inbox ordering
+
+The `inbox-sort-cache` flag controls reads from the cached priority and ranking columns.
+The flag stays off during deployment. API processes and workers update the cache even when the flag is off.
+
+After every API process and worker uses the new cache writers, run `python manage.py backfill_inbox_sort_cache --team-id <team-id>` for each team in the rollout.
+The command uses locked batches and can run again after an interruption.
+Complete the backfill for every team in an organization before you enable the flag for that organization.
+Keep the flag off if any old writer remains. Old writers do not update the cache.
+Disable the flag to return to artefact queries if ordering differs during rollout.
+
## What each probability means
Each head is conditioned on its cohort (`training/heads.py`).
diff --git a/posthog/test/repo_invariants/setup_receivers_baseline.txt b/posthog/test/repo_invariants/setup_receivers_baseline.txt
index 50b8f71a2f09..6fae8cf1290f 100644
--- a/posthog/test/repo_invariants/setup_receivers_baseline.txt
+++ b/posthog/test/repo_invariants/setup_receivers_baseline.txt
@@ -132,7 +132,7 @@ post_delete:products.signals.backend.implementation_pr_cache._refresh_for_artefa
post_delete:products.signals.backend.implementation_pr_cache._refresh_for_report_link
post_delete:products.signals.backend.receivers.mark_scout_suggestions_stale_on_fleet_change
post_delete:products.signals.backend.receivers.reconcile_report_embedding_on_verdict_deleted
-post_delete:products.signals.backend.receivers.sync_report_latest_actionability_on_delete
+post_delete:products.signals.backend.receivers.sync_report_cached_artefact_state_on_delete
post_delete:products.signals.backend.receivers.sync_suggested_reviewer_index_on_delete
post_delete:products.signals.backend.receivers.tombstone_report_embedding_on_hard_delete
post_delete:products.slack_app.backend.signals.invalidate_repo_list_on_user_github_change
@@ -280,7 +280,7 @@ post_save:products.signals.backend.receivers.roll_up_plan_parents_when_report_cl
post_save:products.signals.backend.receivers.schedule_handover_for_report_change
post_save:products.signals.backend.receivers.schedule_handover_for_work_change
post_save:products.signals.backend.receivers.schedule_implementation_handover
-post_save:products.signals.backend.receivers.sync_report_latest_actionability_on_save
+post_save:products.signals.backend.receivers.sync_report_cached_artefact_state_on_save
post_save:products.signals.backend.receivers.sync_suggested_reviewer_index_on_save
post_save:products.signals.backend.receivers.sync_task_run_pr_to_assignments
post_save:products.signals.backend.receivers.tombstone_report_embedding_on_delete
diff --git a/products/signals/backend/facade/taxonomy.py b/products/signals/backend/facade/taxonomy.py
index ca2f42a60f55..5c1df41c3258 100644
--- a/products/signals/backend/facade/taxonomy.py
+++ b/products/signals/backend/facade/taxonomy.py
@@ -25,4 +25,5 @@ def __getattr__(name: str) -> type["SignalRemediation"]:
SignalRemediation, # noqa: PLC0415 - keeps payload schemas off Django startup
)
+ globals()[name] = SignalRemediation
return SignalRemediation
diff --git a/products/signals/backend/management/AGENTS.md b/products/signals/backend/management/AGENTS.md
index 72efab95d4e2..e2b2c0efce75 100644
--- a/products/signals/backend/management/AGENTS.md
+++ b/products/signals/backend/management/AGENTS.md
@@ -212,6 +212,14 @@ uv run manage.py backfill_report_actionability --team-id 1 --batch-size 100 --af
Idempotent. It recomputes each report from its artefacts and writes only the rows that disagree.
+## Filling the cached Inbox ordering values
+
+`uv run manage.py backfill_inbox_sort_cache --team-id <id>` fills the priority and ranking cache in locked batches.
+Run it only after all API processes and workers use the cache writers.
+Keep `inbox-sort-cache` off until the backfill completes for every team in the organization.
+The command can run again after an interruption.
+See [Inbox ranking rollout](../../../../../docs/internal/inbox-ranking-training.md#cached-inbox-ordering) for the rollout and rollback steps.
+
## Filling the cached implementation PR flags
`SignalReport.has_implementation_pr` and `has_active_implementation_pr` cache `implementation_pr_report_filter`, so the inbox filters on a column.
diff --git a/products/signals/backend/management/commands/backfill_inbox_sort_cache.py b/products/signals/backend/management/commands/backfill_inbox_sort_cache.py
new file mode 100644
index 000000000000..305fd89b2212
--- /dev/null
+++ b/products/signals/backend/management/commands/backfill_inbox_sort_cache.py
@@ -0,0 +1,19 @@
+from typing import Any
+
+from django.core.management.base import BaseCommand, CommandError, CommandParser
+
+from products.signals.backend.ranking.cache import backfill_report_sort_cache
+
+
+class Command(BaseCommand):
+ help = "Backfill Inbox sort values after all API and worker processes deploy. Keep inbox-sort-cache off until this completes."
+
+ def add_arguments(self, parser: CommandParser) -> None:
+ parser.add_argument("--team-id", type=int, required=True)
+ parser.add_argument("--batch-size", type=int, default=500)
+
+ def handle(self, *args: str, **options: Any) -> None:
+ if options["batch_size"] < 1:
+ raise CommandError("Batch size must be at least 1.")
+ updated = backfill_report_sort_cache(team_id=options["team_id"], batch_size=options["batch_size"])
+ self.stdout.write(f"Updated {updated} reports.")
diff --git a/products/signals/backend/migrations/0236_signalreport_priority_sort_rank_and_more.py b/products/signals/backend/migrations/0236_signalreport_priority_sort_rank_and_more.py
new file mode 100644
index 000000000000..ac693b0c14d1
--- /dev/null
+++ b/products/signals/backend/migrations/0236_signalreport_priority_sort_rank_and_more.py
@@ -0,0 +1,55 @@
+# Generated by Django 6.1.3 on 2026-11-05 15:46
+
+from django.conf import settings
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("posthog", "1425_workload_trust_policy_request"),
+ ("signals", "0235_signalspend_usage_billing"),
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="signalreport",
+ name="priority_sort_rank",
+ field=models.PositiveSmallIntegerField(db_default=5, default=5),
+ ),
+ migrations.AddField(
+ model_name="signalreport",
+ name="ranking_action_lift",
+ field=models.FloatField(blank=True, null=True),
+ ),
+ migrations.AddField(
+ model_name="signalreport",
+ name="ranking_action_score",
+ field=models.FloatField(blank=True, null=True),
+ ),
+ migrations.AddField(
+ model_name="signalreport",
+ name="ranking_fix_lift",
+ field=models.FloatField(blank=True, null=True),
+ ),
+ migrations.AddField(
+ model_name="signalreport",
+ name="ranking_fix_score",
+ field=models.FloatField(blank=True, null=True),
+ ),
+ migrations.AddField(
+ model_name="signalreport",
+ name="ranking_open_lift",
+ field=models.FloatField(blank=True, null=True),
+ ),
+ migrations.AddField(
+ model_name="signalreport",
+ name="ranking_open_score",
+ field=models.FloatField(blank=True, null=True),
+ ),
+ migrations.AddField(
+ model_name="signalreport",
+ name="ranking_scores",
+ field=models.JSONField(blank=True, null=True),
+ ),
+ ]
diff --git a/products/signals/backend/migrations/0237_signalreport_inbox_sort_indexes.py b/products/signals/backend/migrations/0237_signalreport_inbox_sort_indexes.py
new file mode 100644
index 000000000000..3d66ce3a200f
--- /dev/null
+++ b/products/signals/backend/migrations/0237_signalreport_inbox_sort_indexes.py
@@ -0,0 +1,36 @@
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("signals", "0236_signalreport_priority_sort_rank_and_more"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="signalreport",
+ index=models.Index(fields=["team", "priority_sort_rank"], name="signals_rpt_team_priority"),
+ ),
+ SafeAddIndexConcurrently(
+ model_name="signalreport",
+ index=models.Index(
+ "team", models.F("ranking_open_score").desc(nulls_last=True), name="signals_rpt_team_rank_open"
+ ),
+ ),
+ SafeAddIndexConcurrently(
+ model_name="signalreport",
+ index=models.Index(
+ "team", models.F("ranking_action_score").desc(nulls_last=True), name="signals_rpt_team_rank_action"
+ ),
+ ),
+ SafeAddIndexConcurrently(
+ model_name="signalreport",
+ index=models.Index(
+ "team", models.F("ranking_fix_score").desc(nulls_last=True), name="signals_rpt_team_rank_fix"
+ ),
+ ),
+ ]
diff --git a/products/signals/backend/migrations/max_migration.txt b/products/signals/backend/migrations/max_migration.txt
index 1e795c304404..1ae1a3298a8f 100644
--- a/products/signals/backend/migrations/max_migration.txt
+++ b/products/signals/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0235_signalspend_usage_billing
+0237_signalreport_inbox_sort_indexes
diff --git a/products/signals/backend/models.py b/products/signals/backend/models.py
index 024f4d591343..b71823fa4443 100644
--- a/products/signals/backend/models.py
+++ b/products/signals/backend/models.py
@@ -537,6 +537,19 @@ class SignalReport(UUIDModel):
latest_actionability = models.CharField(max_length=30, null=True, blank=True)
latest_already_addressed = models.BooleanField(null=True, blank=True)
+ # These fields keep inbox ordering on the report table. Artefact receivers and the ranking
+ # sweep update them without changing `updated_at`.
+ priority_sort_rank = models.PositiveSmallIntegerField(
+ default=len(AutonomyPriority.values), db_default=len(AutonomyPriority.values)
+ )
+ ranking_scores = models.JSONField(null=True, blank=True)
+ ranking_open_score = models.FloatField(null=True, blank=True)
+ ranking_action_score = models.FloatField(null=True, blank=True)
+ ranking_fix_score = models.FloatField(null=True, blank=True)
+ ranking_open_lift = models.FloatField(null=True, blank=True)
+ ranking_action_lift = models.FloatField(null=True, blank=True)
+ ranking_fix_lift = models.FloatField(null=True, blank=True)
+
# Cached from `implementation_pr_report_filter`, with and without `active_only`, so the inbox
# filters on a column instead of evaluating five link subqueries for every report.
# implementation_pr_cache.py keeps them current.
@@ -547,6 +560,14 @@ class SignalReport(UUIDModel):
indexes = [
models.Index(fields=["team", "status", "promoted_at"]),
models.Index(fields=["team", "created_at"]),
+ models.Index(fields=["team", "priority_sort_rank"], name="signals_rpt_team_priority"),
+ models.Index(
+ "team", models.F("ranking_open_score").desc(nulls_last=True), name="signals_rpt_team_rank_open"
+ ),
+ models.Index(
+ "team", models.F("ranking_action_score").desc(nulls_last=True), name="signals_rpt_team_rank_action"
+ ),
+ models.Index("team", models.F("ranking_fix_score").desc(nulls_last=True), name="signals_rpt_team_rank_fix"),
# Back the inbox list ordered by `-updated_at`. The view appends `id` as a tie-breaker.
models.Index(fields=["team", "-updated_at", "id"], name="signals_report_team_updated"),
# Partial: the daily-limit gate only ever counts stamped rows for one team and day.
diff --git a/products/signals/backend/ranking/cache.py b/products/signals/backend/ranking/cache.py
new file mode 100644
index 000000000000..6c44507a05ff
--- /dev/null
+++ b/products/signals/backend/ranking/cache.py
@@ -0,0 +1,180 @@
+import json
+from collections.abc import Mapping, Sequence
+from datetime import datetime
+from uuid import UUID
+
+from django.db import transaction
+
+from posthog.dataclasses import frozen
+
+from products.signals.backend.artefact_schemas import RankingScore, priority_from_judgment
+from products.signals.backend.models import AutonomyPriority, SignalReport, SignalReportArtefact
+from products.signals.backend.ranking.staleness import EDIT_ARTEFACT_TYPES, is_stale_score
+
+PUBLIC_SCORE_FIELDS: Mapping[str, str] = {
+ "open": "ranking_open_score",
+ "action": "ranking_action_score",
+ "fixed": "ranking_fix_score",
+}
+PUBLIC_LIFT_FIELDS: Mapping[str, str] = {
+ "open": "ranking_open_lift",
+ "action": "ranking_action_lift",
+ "fixed": "ranking_fix_lift",
+}
+RANKING_CACHE_FIELDS = ["ranking_scores", *PUBLIC_SCORE_FIELDS.values(), *PUBLIC_LIFT_FIELDS.values()]
+
+
+@frozen
+class _RankingValues:
+ scores: Mapping[str, float]
+ lifts: Mapping[str, float]
+
+
+def _parse_datetime(value: object) -> datetime | None:
+ if not isinstance(value, str):
+ return None
+ try:
+ return datetime.fromisoformat(value.replace("Z", "+00:00"))
+ except ValueError:
+ return None
+
+
+def _numbers(value: object) -> dict[str, float]:
+ if not isinstance(value, dict):
+ return {}
+ return {
+ key: float(number)
+ for key, number in value.items()
+ if isinstance(key, str) and isinstance(number, (int, float)) and not isinstance(number, bool)
+ }
+
+
+def ranking_values_from_content(content: str | None, latest_edit_at: datetime | None) -> _RankingValues:
+ try:
+ parsed = json.loads(content or "")
+ except (json.JSONDecodeError, TypeError, ValueError):
+ return _RankingValues(scores={}, lifts={})
+ if not isinstance(parsed, dict):
+ return _RankingValues(scores={}, lifts={})
+
+ read_at_value = parsed.get("embedding_inserted_at")
+ if read_at_value is None:
+ read_at_value = parsed.get("scored_at")
+ read_at = _parse_datetime(read_at_value)
+ if read_at is not None and read_at.tzinfo is not None and latest_edit_at is not None and latest_edit_at > read_at:
+ return _RankingValues(scores={}, lifts={})
+
+ served_key = parsed.get("served_key")
+ results = parsed.get("results")
+ served = results.get(served_key) if isinstance(results, dict) and isinstance(served_key, str) else None
+ if not isinstance(served, dict):
+ return _RankingValues(scores={}, lifts={})
+ return _RankingValues(scores=_numbers(served.get("scores")), lifts=_numbers(served.get("lifts")))
+
+
+def latest_edit_ats(report_ids: Sequence[str]) -> dict[str, datetime]:
+ rows = (
+ SignalReportArtefact.objects.filter(report_id__in=report_ids, type__in=EDIT_ARTEFACT_TYPES)
+ .order_by("report_id", "-created_at", "-id")
+ .distinct("report_id")
+ .values_list("report_id", "created_at")
+ )
+ return {str(report_id): created_at for report_id, created_at in rows}
+
+
+def apply_ranking_cache(report: SignalReport, score: RankingScore | None, latest_edit_at: datetime | None) -> None:
+ scores: Mapping[str, float] = {}
+ lifts: Mapping[str, float] = {}
+ if score is not None and not is_stale_score(score, latest_edit_at):
+ served = score.results.get(score.served_key)
+ if served is not None:
+ scores = served.scores
+ lifts = served.lifts
+
+ _apply_ranking_values(report, scores, lifts)
+
+
+def _apply_ranking_values(report: SignalReport, scores: Mapping[str, float], lifts: Mapping[str, float]) -> None:
+ report.ranking_scores = dict(scores) or None
+ for head, field in PUBLIC_SCORE_FIELDS.items():
+ setattr(report, field, scores.get(head))
+ for head, field in PUBLIC_LIFT_FIELDS.items():
+ setattr(report, field, lifts.get(head))
+
+
+def refresh_report_ranking_cache(*, team_id: int, report_id: UUID | str) -> bool:
+ with transaction.atomic():
+ report = (
+ SignalReport.objects.select_for_update(no_key=True)
+ .filter(team_id=team_id, id=report_id)
+ .only(*RANKING_CACHE_FIELDS)
+ .first()
+ )
+ if report is None:
+ return False
+ artefact = (
+ SignalReportArtefact.objects.filter(
+ report_id=report_id, type=SignalReportArtefact.ArtefactType.RANKING_SCORE
+ )
+ .order_by("-created_at", "-id")
+ .only("content")
+ .first()
+ )
+ latest_edit_at = latest_edit_ats([str(report_id)]).get(str(report_id))
+ previous = tuple(getattr(report, field) for field in RANKING_CACHE_FIELDS)
+ values = ranking_values_from_content(artefact.content if artefact is not None else None, latest_edit_at)
+ _apply_ranking_values(report, values.scores, values.lifts)
+ if previous == tuple(getattr(report, field) for field in RANKING_CACHE_FIELDS):
+ return False
+ SignalReport.objects.filter(team_id=team_id, id=report_id).update(
+ **{field: getattr(report, field) for field in RANKING_CACHE_FIELDS}
+ )
+ return True
+
+
+def backfill_report_sort_cache(*, team_id: int, batch_size: int = 500) -> int:
+ cursor: UUID | None = None
+ updated = 0
+ while True:
+ with transaction.atomic():
+ page = SignalReport.objects.filter(team_id=team_id).select_for_update(no_key=True).order_by("id")
+ if cursor is not None:
+ page = page.filter(id__gt=cursor)
+ reports = list(page[:batch_size])
+ if not reports:
+ return updated
+ cursor = reports[-1].id
+ report_ids = [str(report.id) for report in reports]
+ priorities = dict(
+ SignalReportArtefact.objects.filter(
+ team_id=team_id,
+ report_id__in=report_ids,
+ type=SignalReportArtefact.ArtefactType.PRIORITY_JUDGMENT,
+ content__startswith="{",
+ )
+ .order_by("report_id", "-created_at", "-id")
+ .distinct("report_id")
+ .values_list("report_id", "content")
+ )
+ scores = dict(
+ SignalReportArtefact.objects.filter(
+ team_id=team_id, report_id__in=report_ids, type=SignalReportArtefact.ArtefactType.RANKING_SCORE
+ )
+ .order_by("report_id", "-created_at", "-id")
+ .distinct("report_id")
+ .values_list("report_id", "content")
+ )
+ edit_ats = latest_edit_ats(report_ids)
+ for report in reports:
+ priority = priority_from_judgment(priorities.get(report.id))
+ report.priority_sort_rank = (
+ AutonomyPriority.values.index(priority)
+ if priority in AutonomyPriority.values
+ else len(AutonomyPriority.values)
+ )
+ values = ranking_values_from_content(scores.get(report.id), edit_ats.get(str(report.id)))
+ _apply_ranking_values(report, values.scores, values.lifts)
+ SignalReport.objects.filter(team_id=team_id).bulk_update(
+ reports, ["priority_sort_rank", *RANKING_CACHE_FIELDS], batch_size=batch_size
+ )
+ updated += len(reports)
diff --git a/products/signals/backend/ranking/sinks.py b/products/signals/backend/ranking/sinks.py
index 18e9cfc16593..ccd6127902ab 100644
--- a/products/signals/backend/ranking/sinks.py
+++ b/products/signals/backend/ranking/sinks.py
@@ -9,11 +9,13 @@ from collections.abc import Sequence
from contextlib import nullcontext
from django.conf import settings
+from django.db import transaction
from posthog.ph_client import ScopedCapture, ph_scoped_capture
from products.signals.backend.artefact_schemas import RankingModelResult, RankingScore
from products.signals.backend.models import SignalActorKind, SignalReport, SignalReportArtefact
+from products.signals.backend.ranking.cache import RANKING_CACHE_FIELDS, apply_ranking_cache, latest_edit_ats
from products.signals.backend.ranking.model_contract import classification_thresholds, head_lifts, readable_head_names
from products.signals.backend.ranking.overrides import RankingOverrides
@@ -35,23 +37,30 @@ def persist_scores(
persists many teams passes one `capture` for all of them, so it pays that flush once.
"""
report_ids = [report_id for report_id, _ in scores]
- owned = {
- str(report_id)
- for report_id in SignalReport.objects.filter(team_id=team_id, id__in=report_ids).values_list("id", flat=True)
- }
- kept = [(report_id, score) for report_id, score in scores if report_id in owned]
- SignalReportArtefact.objects.bulk_create(
- [
- SignalReportArtefact(
- team_id=team_id,
- report_id=report_id,
- type=SignalReportArtefact.ArtefactType.RANKING_SCORE,
- content=score.model_dump_json(),
- actor_kind=SignalActorKind.SYSTEM,
- )
- for report_id, score in kept
- ]
- )
+ with transaction.atomic():
+ reports = {
+ str(report.id): report
+ for report in SignalReport.objects.select_for_update(no_key=True)
+ .filter(team_id=team_id, id__in=report_ids)
+ .order_by("id")
+ }
+ kept = [(report_id, score) for report_id, score in scores if report_id in reports]
+ SignalReportArtefact.objects.bulk_create(
+ [
+ SignalReportArtefact(
+ team_id=team_id,
+ report_id=report_id,
+ type=SignalReportArtefact.ArtefactType.RANKING_SCORE,
+ content=score.model_dump_json(),
+ actor_kind=SignalActorKind.SYSTEM,
+ )
+ for report_id, score in kept
+ ]
+ )
+ edit_ats = latest_edit_ats(list(reports))
+ for report_id, score in kept:
+ apply_ranking_cache(reports[report_id], score, edit_ats.get(report_id))
+ SignalReport.objects.bulk_update([reports[report_id] for report_id, _ in kept], RANKING_CACHE_FIELDS)
override_expires_at = (
served_override.expires_at.isoformat() if served_override and served_override.expires_at else None
)
diff --git a/products/signals/backend/receivers.py b/products/signals/backend/receivers.py
index 76fdca933ba9..53049aef4f7e 100644
--- a/products/signals/backend/receivers.py
+++ b/products/signals/backend/receivers.py
@@ -25,11 +25,14 @@ from posthog.models.user import User
from products.signals.backend.artefact_attribution import ArtefactAttribution
from products.signals.backend.models import SignalFirstReportNotification, SignalReport, SignalReportArtefact
+from products.signals.backend.ranking.cache import refresh_report_ranking_cache
+from products.signals.backend.ranking.staleness import EDIT_ARTEFACT_TYPES
from products.signals.backend.report_embeddings import (
emit_report_embeddings,
emit_report_tombstone,
render_report_documents,
)
+from products.signals.backend.report_priority import refresh_report_priority_rank
from products.signals.backend.suggested_reviewer_index import sync_suggested_reviewer_index
from products.tasks.backend.facade.task_run_signals import connect_task_run_post_save
@@ -837,20 +840,27 @@ def _sync_report_latest_actionability(instance: SignalReportArtefact) -> None:
SignalReport.refresh_latest_actionability(team_id=instance.team_id, report_id=instance.report_id)
+def _sync_report_inbox_sort_cache(instance: SignalReportArtefact) -> None:
+ if instance.type == SignalReportArtefact.ArtefactType.PRIORITY_JUDGMENT:
+ refresh_report_priority_rank(team_id=instance.team_id, report_id=instance.report_id)
+ elif instance.type == SignalReportArtefact.ArtefactType.RANKING_SCORE or instance.type in EDIT_ARTEFACT_TYPES:
+ refresh_report_ranking_cache(team_id=instance.team_id, report_id=instance.report_id)
+
+
@receiver(post_save, sender=SignalReportArtefact)
-def sync_report_latest_actionability_on_save(
+def sync_report_cached_artefact_state_on_save(
sender: type[SignalReportArtefact],
instance: SignalReportArtefact,
created: bool,
**kwargs: Any,
) -> None:
- """Keep the report's cached actionability equal to its newest judgment.
+ """Keep report-row filters and ordering equal to their latest artefacts.
- On the artefact write path rather than at each producer, because a judgment reaches a report
- from the research pipeline, a custom agent, a scout edit, the artefact REST API and the MCP
- tools. Not gated on `created`, because `update_content` edits a judgment row in place.
+ This runs on the artefact write path because judgments and edits have several producers. It is
+ not gated on `created`, because `update_content` edits a status row in place.
"""
_sync_report_latest_actionability(instance)
+ _sync_report_inbox_sort_cache(instance)
if instance.type == SignalReportArtefact.ArtefactType.ACTIONABILITY_JUDGMENT:
_schedule_first_report_notification(instance.team_id)
@@ -890,13 +900,13 @@ def reconcile_report_embedding_on_verdict_deleted(
@receiver(post_delete, sender=SignalReportArtefact)
-def sync_report_latest_actionability_on_delete(
+def sync_report_cached_artefact_state_on_delete(
sender: type[SignalReportArtefact],
instance: SignalReportArtefact,
origin: Any = None,
**kwargs: Any,
) -> None:
- """Deleting the newest judgment reverts the report to the one before it.
+ """Deleting the newest cached artefact reverts the report to the one before it.
Skipped for a cascade, where the report itself is going away, so a team teardown does not pay
a read and a write per artefact for a row nobody will read.
@@ -904,6 +914,7 @@ def sync_report_latest_actionability_on_delete(
if not _deleted_directly(origin):
return
_sync_report_latest_actionability(instance)
+ _sync_report_inbox_sort_cache(instance)
def _status_change_properties(
diff --git a/products/signals/backend/report_content_gates.py b/products/signals/backend/report_content_gates.py
index 7a04c53f6569..8cad40863a62 100644
--- a/products/signals/backend/report_content_gates.py
+++ b/products/signals/backend/report_content_gates.py
@@ -23,6 +23,7 @@ REPORT_METRICS_FLAG = "signals-report-metrics"
REPORT_MONITORING_FLAG = "signals-report-monitoring"
FIRST_REPORT_EMAIL_FLAG = "signals-first-report-email"
RESEARCH_REVIEWER_SELECTION_FLAG = "signals-research-reviewer-selection"
+INBOX_SORT_CACHE_FLAG = "inbox-sort-cache"
INBOX_CACHED_FILTERS_FLAG = "signals-inbox-cached-filters"
INBOX_SLACK_SECTION_BLOCKS_FLAG = "signals-inbox-slack-section-blocks"
@@ -109,3 +110,9 @@ def organization_inbox_slack_section_blocks_enabled(organization_id: UUID) -> bo
return _organization_flag_enabled(
INBOX_SLACK_SECTION_BLOCKS_FLAG, organization_id, enable_in_debug=False, only_evaluate_locally=True
)
+
+
+def organization_inbox_sort_cache_enabled(organization_id: UUID) -> bool:
+ return _organization_flag_enabled(
+ INBOX_SORT_CACHE_FLAG, organization_id, enable_in_debug=False, only_evaluate_locally=True
+ )
diff --git a/products/signals/backend/report_priority.py b/products/signals/backend/report_priority.py
index e51a2f2b95a3..dbe03c130550 100644
--- a/products/signals/backend/report_priority.py
+++ b/products/signals/backend/report_priority.py
@@ -1,4 +1,5 @@
from collections.abc import Sequence
+from uuid import UUID
from django.contrib.postgres.fields import ArrayField
from django.db import transaction
@@ -70,6 +71,39 @@ def latest_report_priorities(team_id: int, report_ids: Sequence[str]) -> dict[st
return latest
+def refresh_report_priority_rank(*, team_id: int, report_id: UUID | str) -> bool:
+ with transaction.atomic():
+ report = (
+ SignalReport.objects.select_for_update(no_key=True)
+ .filter(team_id=team_id, id=report_id)
+ .only("priority_sort_rank")
+ .first()
+ )
+ if report is None:
+ return False
+ content = (
+ SignalReportArtefact.objects.filter(
+ team_id=team_id,
+ report_id=report_id,
+ type=SignalReportArtefact.ArtefactType.PRIORITY_JUDGMENT,
+ content__startswith="{",
+ )
+ .order_by("-created_at", "-id")
+ .values_list("content", flat=True)
+ .first()
+ )
+ priority = priority_from_judgment(content)
+ rank = (
+ AutonomyPriority.values.index(priority)
+ if priority in AutonomyPriority.values
+ else len(AutonomyPriority.values)
+ )
+ if report.priority_sort_rank == rank:
+ return False
+ SignalReport.objects.filter(team_id=team_id, id=report_id).update(priority_sort_rank=rank)
+ return True
+
+
def latest_priority_rank() -> Coalesce:
"""A `SignalReport` annotation: the index in `AutonomyPriority.values` of the priority in the report's latest
priority_judgment artefact. Reports without a priority rank after "P4"."""
diff --git a/products/signals/backend/scout_report/trial_inbox.py b/products/signals/backend/scout_report/trial_inbox.py
index 492769e7623f..0c830f4d98de 100644
--- a/products/signals/backend/scout_report/trial_inbox.py
+++ b/products/signals/backend/scout_report/trial_inbox.py
@@ -327,8 +327,7 @@ class TrialInboxReads:
result.append(document)
return result
- @staticmethod
- def _sort_value(document: Mapping[str, object], field: str) -> str | int | float | datetime:
+ def _sort_value(self, document: Mapping[str, object], field: str) -> str | int | float | datetime:
if field == "pipeline_status_rank":
status = document.get("status")
if status == "ready":
@@ -343,7 +342,7 @@ class TrialInboxReads:
"suppressed": 8,
"deleted": 9,
}.get(str(status), 50)
- if field == "priority_sort_rank":
+ if field == self.view._SIGNAL_REPORT_ORDERING_FIELDS["priority"]:
priority = document.get("priority")
return (
{"P0": 0, "P1": 1, "P2": 2, "P3": 3, "P4": 4}[str(priority)]
diff --git a/products/signals/backend/serializers.py b/products/signals/backend/serializers.py
index 141cd0f7820a..d3a0ed51c6c7 100644
--- a/products/signals/backend/serializers.py
+++ b/products/signals/backend/serializers.py
@@ -2680,8 +2680,8 @@ class SignalReportListSerializer(SignalReportSerializer):
ranker = self.context.get("inbox_ranker")
if ranker is None:
return None
- score = None if ranker == "priority" else getattr(obj, f"ranking_{ranker}_score", None)
- key = inbox_merge_key(ranker, score, getattr(obj, "priority_sort_rank", None))
+ score = None if ranker == "priority" else getattr(obj, f"inbox_ranking_{ranker}_score", None)
+ key = inbox_merge_key(ranker, score, getattr(obj, "inbox_priority_sort_rank", None))
personal_order: PersonalListOrder | None = self.context.get("personal_order")
if personal_order is None:
return key
diff --git a/products/signals/backend/test/test_ranking_scoring.py b/products/signals/backend/test/test_ranking_scoring.py
index e826a631c4c9..2e4fc622d07e 100644
--- a/products/signals/backend/test/test_ranking_scoring.py
+++ b/products/signals/backend/test/test_ranking_scoring.py
@@ -635,6 +635,9 @@ class TestScorerPersists(_ScorerTestMixin, BaseTest):
score = RankingScore.model_validate_json(rows[0].content)
assert score.served_key == manifest.served.key
assert score.results[candidate.key].status == "scored"
+ report = SignalReport.objects.get(id=scored)
+ assert report.ranking_scores == score.results[manifest.served.key].scores
+ assert report.ranking_open_score == score.results[manifest.served.key].scores["open"]
assert sorted(
(event["event"], event["properties"]["report_id"], event["properties"]["model_key"])
for event in captured.events
diff --git a/products/signals/backend/test/test_scout_trial_state.py b/products/signals/backend/test/test_scout_trial_state.py
index b752be99132c..72cd8b250f01 100644
--- a/products/signals/backend/test/test_scout_trial_state.py
+++ b/products/signals/backend/test/test_scout_trial_state.py
@@ -996,16 +996,31 @@ class TestScoutTrialReportCapture(APIBaseTest):
may_read_snapshot.assert_any_call(metric)
may_read_query.assert_any_call(metric)
- def test_inbox_search_and_pagination_merge_private_edits_without_duplicates(self) -> None:
+ @parameterized.expand(
+ [("oldest", {"sort": "oldest"}, [0, 1, 2]), ("priority", {"ordering": "priority"}, [2, 1, 0])]
+ )
+ def test_inbox_search_and_pagination_merge_private_edits_without_duplicates(
+ self, _name: str, query: dict[str, str], order: list[int]
+ ) -> None:
first = SignalReport.objects.create(team=self.team, title="Earlier report", summary="Original", status="ready")
second = SignalReport.objects.create(team=self.team, title="Second report", summary="Original", status="ready")
+ SignalReportArtefact.objects.create(
+ team=self.team,
+ report=second,
+ type=SignalReportArtefact.ArtefactType.PRIORITY_JUDGMENT,
+ content=json.dumps({"priority": "P3"}),
+ )
private_id = self._emit()
edit_report_sync(
team=self.team,
author=ScoutRunReportAuthor(run=self.scout_run),
report_id=str(first.id),
title="A synthetic checkout revision",
+ priority="P4",
+ priority_explanation="The fixture affects an optional path.",
)
+ report_ids = [str(first.id), str(second.id), private_id]
+ expected_ids = [report_ids[index] for index in order]
base = f"/api/projects/{self.team.id}/signals/reports/"
with (
patch("products.signals.backend.views.trial_store_for_request", return_value=self.store),
@@ -1014,22 +1029,24 @@ class TestScoutTrialReportCapture(APIBaseTest):
):
results = []
for offset in range(3):
- response = self.client.get(base, {"sort": "oldest", "offset": str(offset), "limit": "1"})
+ response = self.client.get(base, {**query, "offset": str(offset), "limit": "1"})
assert response.status_code == 200
assert response.json()["count"] == 3
results.extend(response.json()["results"])
- assert [row["id"] for row in results] == [str(first.id), str(second.id), private_id]
- search = self.client.get(base, {"search": "checkout", "sort": "oldest"})
- assert [row["id"] for row in search.json()["results"]] == [str(first.id), private_id]
+ assert [row["id"] for row in results] == expected_ids
+ search = self.client.get(base, {**query, "search": "checkout"})
+ assert [row["id"] for row in search.json()["results"]] == [
+ pk for pk in expected_ids if pk != str(second.id)
+ ]
detail = self.client.get(f"{base}{first.id}/")
assert detail.json()["title"] == "A synthetic checkout revision"
count = self.client.get(base, {"search": "checkout", "count_only": "true"})
assert count.json()["count"] == 2
fetch_sources.reset_mock()
- without_metadata = self.client.get(base, {"include_source_metadata": "false", "sort": "oldest"})
+ without_metadata = self.client.get(base, {**query, "include_source_metadata": "false"})
assert without_metadata.status_code == 200
rows = without_metadata.json()["results"]
- assert [row["id"] for row in rows] == [str(first.id), str(second.id), private_id]
+ assert [row["id"] for row in rows] == expected_ids
assert all(row["source_products"] == [] and row["scout_name"] is None for row in rows)
fetch_sources.assert_not_called()
assert self.store.invalid_reason() is None
diff --git a/products/signals/backend/test/test_signal_report_api.py b/products/signals/backend/test/test_signal_report_api.py
index 9ae502c02a47..43d4ab348de8 100644
--- a/products/signals/backend/test/test_signal_report_api.py
+++ b/products/signals/backend/test/test_signal_report_api.py
@@ -74,6 +74,7 @@ from products.signals.backend.models import (
SignalUserAutonomyConfig,
)
from products.signals.backend.personal_ranking.candidates import recommended_order
+from products.signals.backend.ranking.cache import refresh_report_ranking_cache
from products.signals.backend.report_assignments import create_claim
from products.signals.backend.report_claims import get_active_claim
from products.signals.backend.report_merge import MERGE_DISMISSAL_REASON
@@ -255,6 +256,12 @@ class TestSignalReportDeleteAPI(APIBaseTest):
class TestSignalReportListAPI(APIBaseTest):
"""GET list/retrieve: `priority` from actionability artefacts; `ordering` (comma-separated, e.g. `status,-total_weight`)."""
+ def setUp(self) -> None:
+ super().setUp()
+ sort_cache = patch("products.signals.backend.views.organization_inbox_sort_cache_enabled", return_value=True)
+ self.sort_cache_flag = sort_cache.start()
+ self.addCleanup(sort_cache.stop)
+
def _assert_personal_ranking_sorts_like_the_list(self, body: dict[str, Any]) -> None:
# The personal ranking reorders `for_you` reports in Python, and its fallback must match this list.
ids = [r["id"] for r in body["results"]]
@@ -1286,6 +1293,7 @@ class TestSignalReportListAPI(APIBaseTest):
SignalReportArtefact.objects.filter(pk=latest_edit.pk).update(
created_at=datetime(2026, 9, 20, edit_hour, 30, tzinfo=UTC)
)
+ refresh_report_ranking_cache(team_id=self.team.id, report_id=edited.id)
list_response = self.client.get(self._list_url(status="ready", ordering="-ranking_open,status,-updated_at"))
assert list_response.status_code == status.HTTP_200_OK
@@ -1665,8 +1673,9 @@ class TestSignalReportListAPI(APIBaseTest):
ids = [r["id"] for r in response.json()["results"]]
assert ids.index(str(heavy.id)) < ids.index(str(light.id))
- def test_ordering_by_priority_sorts_p0_first(self):
- """priority ordering: P0 > P1 > P2 > P3 > P4 > null."""
+ @parameterized.expand([("artefacts", False), ("cache", True)])
+ def test_ordering_by_priority_sorts_p0_first(self, _name: str, cached: bool) -> None:
+ self.sort_cache_flag.return_value = cached
r_p3 = self._create_report(title="P3 report", summary="s", signal_count=1, total_weight=1.0)
r_p1 = self._create_report(title="P1 report", summary="s", signal_count=1, total_weight=1.0)
r_p0 = self._create_report(title="P0 report", summary="s", signal_count=1, total_weight=1.0)
@@ -1679,6 +1688,15 @@ class TestSignalReportListAPI(APIBaseTest):
self._priority_artefact(r_p2, priority="P2")
self._priority_artefact(r_p4, priority="P4")
+ SignalReportArtefact.objects.create(
+ team=self.team,
+ report=r_p0,
+ type=SignalReportArtefact.ArtefactType.PRIORITY_JUDGMENT,
+ content="legacy plain text",
+ )
+ if not cached:
+ SignalReport.objects.filter(team=self.team).update(priority_sort_rank=5)
+
with CaptureQueriesContext(connection) as ctx:
response = self.client.get(self._list_url(status="ready", ordering="priority"))
assert response.status_code == status.HTTP_200_OK
@@ -1688,9 +1706,8 @@ class TestSignalReportListAPI(APIBaseTest):
if query["sql"].startswith('SELECT "signals_signalreport"."id" AS "pk"')
]
assert len(list_sql) == 1
- # The sort reads every matching report, so a priority subquery repeated per CASE branch
- # multiplies the artefact walks for the whole team.
- assert list_sql[0].count("'priority_judgment'") == 1
+ assert ("priority_judgment" not in list_sql[0]) is cached
+ assert ('"signals_signalreport"."priority_sort_rank"' in list_sql[0]) is cached
ids = [r["id"] for r in response.json()["results"]]
assert ids.index(str(r_p0.id)) < ids.index(str(r_p1.id))
assert ids.index(str(r_p1.id)) < ids.index(str(r_p2.id))
@@ -1698,6 +1715,23 @@ class TestSignalReportListAPI(APIBaseTest):
assert ids.index(str(r_p3.id)) < ids.index(str(r_p4.id))
assert ids.index(str(r_p4.id)) < ids.index(str(r_none.id))
+ @parameterized.expand([("artefacts", False), ("cache", True)])
+ def test_priority_filter_uses_the_latest_object_judgment(self, _name: str, cached: bool) -> None:
+ self.sort_cache_flag.return_value = cached
+ report = self._create_report(title="Priority report")
+ self._priority_artefact(report, priority="P1")
+ SignalReportArtefact.objects.create(
+ team=self.team,
+ report=report,
+ type=SignalReportArtefact.ArtefactType.PRIORITY_JUDGMENT,
+ content="legacy plain text",
+ )
+ if not cached:
+ SignalReport.objects.filter(team=self.team).update(priority_sort_rank=5)
+ response = self.client.get(self._list_url(priority="P1"))
+ assert response.status_code == status.HTTP_200_OK
+ assert [row["id"] for row in response.json()["results"]] == [str(report.id)]
+
def test_ordering_skips_unknown_clause_keeps_valid_ones(self):
"""An unrecognized clause (e.g. a stale persisted field) is skipped, not fatal:
the valid clauses still apply instead of silently reverting to the default order."""
@@ -1756,6 +1790,7 @@ class TestSignalReportListAPI(APIBaseTest):
self._ranking_score_artefact(no_head, scores={other_head: 0.5})
stale = self._ranking_score_artefact(low, scores={head: 0.99})
SignalReportArtefact.objects.filter(pk=stale.pk).update(created_at=timezone.now() - timedelta(days=1))
+ refresh_report_ranking_cache(team_id=self.team.id, report_id=low.id)
impossible_time = self._create_report(title="Impossible time")
impossible_score = self._ranking_score_artefact(impossible_time, scores={head: 0.4})
SignalReportArtefact.objects.filter(pk=impossible_score.pk).update(
@@ -1820,7 +1855,9 @@ class TestSignalReportListAPI(APIBaseTest):
]
self._assert_personal_ranking_sorts_like_the_list(body)
- def test_recommended_sort_action_pills_ranks_by_the_action_score(self):
+ @parameterized.expand([("artefacts", False), ("cache", True)])
+ def test_recommended_sort_action_pills_ranks_by_the_action_score(self, _name: str, cached: bool) -> None:
+ self.sort_cache_flag.return_value = cached
head = "action"
self.user.is_staff = False
self.user.save()
@@ -1855,6 +1892,9 @@ class TestSignalReportListAPI(APIBaseTest):
content=json.dumps({"old_title": "Old", "new_title": "Stale P0"}),
)
+ if not cached:
+ SignalReport.objects.filter(team=self.team).update(priority_sort_rank=5, ranking_action_score=None)
+
with patch("products.signals.backend.ranking.inbox_sort.posthoganalytics") as analytics:
get_flag = analytics.get_feature_flag
get_flag.return_value = "action_pills"
@@ -1868,9 +1908,8 @@ class TestSignalReportListAPI(APIBaseTest):
if query["sql"].startswith('SELECT "signals_signalreport"."id" AS "pk"')
]
assert len(list_sql) == 1
- # The sort reads every matching report, so a score subquery repeated in the ORDER BY
- # multiplies the artefact walks for the whole team.
- assert list_sql[0].count("'ranking_score'") == 1
+ assert ("ranking_score" not in list_sql[0]) is cached
+ assert ('"signals_signalreport"."ranking_action_score"' in list_sql[0]) is cached
get_flag.assert_called_once()
assert get_flag.call_args.args[1] == self.user.distinct_id
body = response.json()
diff --git a/products/signals/backend/test/test_signal_report_artefact_model.py b/products/signals/backend/test/test_signal_report_artefact_model.py
index d9030ed9f77f..5f27fb10f4ed 100644
--- a/products/signals/backend/test/test_signal_report_artefact_model.py
+++ b/products/signals/backend/test/test_signal_report_artefact_model.py
@@ -1,7 +1,11 @@
import json
+from datetime import timedelta
from posthog.test.base import BaseTest
+from django.core.management import call_command
+from django.utils import timezone
+
from parameterized import parameterized
from posthog.models import Team
@@ -14,6 +18,8 @@ from products.signals.backend.artefact_schemas import (
NoteArtefact,
Priority,
PriorityAssessment,
+ RankingModelResult,
+ RankingScore,
RelatedTo,
ReportLink,
SignalFinding,
@@ -378,6 +384,144 @@ class TestSignalReportArtefactHelpers(BaseTest):
assert self._cached_actionability(report) == ("immediately_actionable", False)
+ def test_cached_priority_follows_the_newest_judgment(self):
+ report = self._report()
+ assert report.priority_sort_rank == 5
+
+ first = self._append_priority(report, "P3")
+ report.refresh_from_db()
+ assert report.priority_sort_rank == 3
+
+ second = self._append_priority(report, "P1")
+ report.refresh_from_db()
+ assert report.priority_sort_rank == 1
+
+ SignalReportArtefact.objects.create(
+ team=self.team,
+ report=report,
+ type=SignalReportArtefact.ArtefactType.PRIORITY_JUDGMENT,
+ content="legacy plain text",
+ )
+ report.refresh_from_db()
+ assert report.priority_sort_rank == 1
+
+ second.delete()
+ report.refresh_from_db()
+ assert report.priority_sort_rank == 3
+
+ first.delete()
+ report.refresh_from_db()
+ assert report.priority_sort_rank == 5
+
+ def test_cached_ranking_score_follows_scores_and_edits(self):
+ report = self._report()
+ result = RankingModelResult(
+ model_name="report_embeddings",
+ model_version="2026-11-05",
+ model_kind="xgboost",
+ roles=["served"],
+ feature_schema_version=1,
+ status="scored",
+ scores={"open": 0.8, "fixed": 0.6, "pr_merged": 0.4},
+ lifts={"open": 2.0, "fixed": 3.0},
+ )
+ score = RankingScore(
+ scored_at=timezone.now() - timedelta(minutes=1),
+ manifest_version="manifest",
+ served_key=result.key,
+ results={result.key: result},
+ )
+ artefact = SignalReportArtefact.objects.create(
+ team=self.team,
+ report=report,
+ type=SignalReportArtefact.ArtefactType.RANKING_SCORE,
+ content=score.model_dump_json(),
+ )
+ report = SignalReport.objects.get(id=report.id)
+ assert report.ranking_scores == {"open": 0.8, "fixed": 0.6, "pr_merged": 0.4}
+ assert report.ranking_open_score == 0.8
+ assert report.ranking_fix_score == 0.6
+ assert report.ranking_open_lift == 2.0
+
+ edit = SignalReportArtefact.objects.create(
+ team=self.team,
+ report=report,
+ type=SignalReportArtefact.ArtefactType.TITLE_CHANGE,
+ content=json.dumps({"old_title": "t", "new_title": "new"}),
+ )
+ report = SignalReport.objects.get(id=report.id)
+ assert report.ranking_scores is None
+ assert report.ranking_open_score is None
+
+ edit.delete()
+ report = SignalReport.objects.get(id=report.id)
+ assert report.ranking_open_score == 0.8
+
+ artefact.delete()
+ report = SignalReport.objects.get(id=report.id)
+ assert report.ranking_scores is None
+ assert report.ranking_open_score is None
+
+ def test_backfill_repairs_old_writes_in_batches_and_only_updates_its_team(self) -> None:
+ report = self._report()
+ stale = self._report()
+ other_team = Team.objects.create(organization=self.organization)
+ other = SignalReport.objects.create(team=other_team, title="Other report", summary="s")
+ now = timezone.now()
+ SignalReportArtefact.objects.bulk_create(
+ [
+ SignalReportArtefact(
+ team_id=target.team_id,
+ report=target,
+ type=SignalReportArtefact.ArtefactType.PRIORITY_JUDGMENT,
+ content=json.dumps({"priority": "P1"}),
+ created_at=now - timedelta(minutes=2),
+ )
+ for target in (report, stale, other)
+ ]
+ + [
+ SignalReportArtefact(
+ team=self.team,
+ report=report,
+ type=SignalReportArtefact.ArtefactType.PRIORITY_JUDGMENT,
+ content="legacy plain text",
+ created_at=now,
+ ),
+ *[
+ SignalReportArtefact(
+ team=self.team,
+ report=target,
+ type=SignalReportArtefact.ArtefactType.RANKING_SCORE,
+ content=json.dumps(
+ {
+ "scored_at": (now - timedelta(minutes=1)).isoformat(),
+ "served_key": "test-model",
+ "results": {"test-model": {"scores": {"action": 0.8}, "lifts": {"action": 2.0}}},
+ }
+ ),
+ )
+ for target in (report, stale)
+ ],
+ SignalReportArtefact(
+ team=self.team,
+ report=stale,
+ type=SignalReportArtefact.ArtefactType.SUMMARY_CHANGE,
+ content=json.dumps({"old_summary": "s", "new_summary": "Updated"}),
+ created_at=now,
+ ),
+ ]
+ )
+ for _ in range(2):
+ call_command("backfill_inbox_sort_cache", team_id=self.team.id, batch_size=1, verbosity=0)
+ report.refresh_from_db()
+ stale.refresh_from_db()
+ other.refresh_from_db()
+ assert report.priority_sort_rank == stale.priority_sort_rank == 1
+ assert report.ranking_action_score == 0.8
+ assert report.ranking_action_lift == 2.0
+ assert stale.ranking_action_score is None
+ assert other.priority_sort_rank == 5
+
def test_append_status_rejects_log_content(self):
report = self._report()
with self.assertRaises(ValueError):
diff --git a/products/signals/backend/views.py b/products/signals/backend/views.py
index bd9737238029..58299f77a3d5 100644
--- a/products/signals/backend/views.py
+++ b/products/signals/backend/views.py
@@ -19,6 +19,7 @@ from django.db.models import (
Count,
Exists,
F,
+ FloatField,
Func,
IntegerField,
OuterRef,
@@ -30,8 +31,8 @@ from django.db.models import (
Value,
When,
)
-from django.db.models.expressions import OrderBy
-from django.db.models.functions import Coalesce
+from django.db.models.expressions import BaseExpression, OrderBy
+from django.db.models.functions import Cast, Coalesce
from django.db.models.lookups import GreaterThanOrEqual
from django.utils import timezone
@@ -167,6 +168,7 @@ from products.signals.backend.presentation.spend import ReportSpendSerializer
from products.signals.backend.pull_requests import import_report_pull_requests
from products.signals.backend.quota import self_driving_quota_enforcement_enabled, self_driving_quota_gate
from products.signals.backend.ranking.access import can_view_inbox_ranking, user_flag_enabled
+from products.signals.backend.ranking.cache import PUBLIC_LIFT_FIELDS, PUBLIC_SCORE_FIELDS
from products.signals.backend.ranking.inbox_sort import (
PRIORITY_ORDERING,
RANKER_ORDERINGS,
@@ -199,6 +201,7 @@ from products.signals.backend.report_claims import (
)
from products.signals.backend.report_content_gates import (
organization_inbox_cached_filters_enabled,
+ organization_inbox_sort_cache_enabled,
organization_report_monitoring_enabled,
)
from products.signals.backend.report_generation.research import ActionabilityChoice
@@ -1828,12 +1831,12 @@ class SignalReportViewSet(
"is_suggested_reviewer": "is_suggested_reviewer",
"signal_count": "signal_count",
"total_weight": "total_weight",
- "priority": "priority_sort_rank",
+ "priority": "inbox_priority_sort_rank",
"created_at": "created_at",
"updated_at": "updated_at",
"id": "id",
- **{field: f"{field}_score" for field in RANKING_ORDERING_HEADS},
- "ranking_fixed": "ranking_fix_score",
+ **{field: f"inbox_{field}_score" for field in RANKING_ORDERING_HEADS},
+ "ranking_fixed": "inbox_ranking_fix_score",
}
_RANKING_ORDERING_HEADS: dict[str, str] = RANKING_ORDERING_HEADS
@@ -2051,7 +2054,8 @@ class SignalReportViewSet(
qs = self._apply_signal_report_inbox_view_filter(qs)
qs = self._annotate_signal_report_status_rank(qs)
if self._needs_priority_annotation():
- qs = self._annotate_signal_report_priority(qs)
+ priority_rank = F("priority_sort_rank") if self._inbox_sort_cache else latest_priority_rank()
+ qs = qs.annotate(inbox_priority_sort_rank=priority_rank)
qs = self._apply_signal_report_priority_filter(qs)
if self.action == "list":
qs = self._annotate_signal_report_ranking(qs)
@@ -2467,8 +2471,6 @@ class SignalReportViewSet(
return queryset.filter(SignalReport.reports_for_task_filter(task_uuid))
def _apply_signal_report_priority_filter(self, queryset):
- # Filters on the `priority_sort_rank` annotation, which must be applied first.
- # Reports without a priority artefact rank after every priority, so this filter excludes them.
priority_filter = self.request.query_params.get("priority")
if not priority_filter and self.request.query_params.get("use_priority_preference", "false").lower() == "true":
user = cast(User, self.request.user)
@@ -2483,7 +2485,7 @@ class SignalReportViewSet(
)
threshold = personal_threshold or project_threshold
values = AutonomyPriority.values[: AutonomyPriority.values.index(threshold) + 1]
- return queryset.filter(priority_sort_rank__in=self._priority_ranks(values))
+ return queryset.filter(inbox_priority_sort_rank__in=self._priority_ranks(values))
if not priority_filter:
return queryset
@@ -2500,7 +2502,7 @@ class SignalReportViewSet(
}
)
- return queryset.filter(priority_sort_rank__in=self._priority_ranks(values))
+ return queryset.filter(inbox_priority_sort_rank__in=self._priority_ranks(values))
@staticmethod
def _priority_ranks(priorities: Iterable[str]) -> list[int]:
@@ -2620,29 +2622,35 @@ class SignalReportViewSet(
)
)
- def _annotate_signal_report_priority(self, queryset):
- # `ordering=priority` sorts by the priority value ("P0"–"P4") from the latest priority_judgment
- # artefact, as its index in `AutonomyPriority.values`. Reports without a priority rank after "P4".
- return queryset.annotate(priority_sort_rank=latest_priority_rank())
+ @cached_property
+ def _inbox_sort_cache(self) -> bool:
+ return organization_inbox_sort_cache_enabled(self.organization.id)
+
+ def _needs_priority_annotation(self) -> bool:
+ params = self.request.query_params
+ if params.get("priority") or params.get("use_priority_preference", "false").lower() == "true":
+ return True
+ return any(clause.lstrip("-") == "inbox_priority_sort_rank" for clause in self._parse_signal_report_ordering())
def _annotate_signal_report_ranking(self, queryset):
- # `ordering=ranking_<head>` sorts by the served model's probability for that head, read from
- # the latest ranking_score artefact as `results -> <served_key> -> scores -> <head>`. Only
- # the heads the ordering names are annotated, because each one is a correlated subquery.
ordered_fields = {clause.lstrip("-") for clause in self._parse_signal_report_ordering()}
annotated: set[str] = set()
for field, head in self._RANKING_ORDERING_HEADS.items():
annotation = self._SIGNAL_REPORT_ORDERING_FIELDS[field]
- # An alias shares its annotation with the field it renames, so annotate each one once.
if annotation not in ordered_fields or annotation in annotated:
continue
annotated.add(annotation)
- queryset = queryset.annotate(**{annotation: latest_ranking_value(head, "scores")})
+ value: BaseExpression | F
+ if not self._inbox_sort_cache:
+ value = latest_ranking_value(head, "scores")
+ elif head in PUBLIC_SCORE_FIELDS:
+ value = F(PUBLIC_SCORE_FIELDS[head])
+ else:
+ value = Cast(F(f"ranking_scores__{head}"), FloatField())
+ queryset = queryset.annotate(**{annotation: value})
return queryset
def _apply_signal_report_ranking_filters(self, queryset):
- # Each named head is one correlated subquery, compared with `>=`. NULL never matches, so
- # unscored and stale reports drop out of every ranking filter.
filters: tuple[tuple[str, Literal["scores", "lifts"]], ...] = (
("ranking_min_lift", "lifts"),
("ranking_min_probability", "scores"),
@@ -2653,7 +2661,9 @@ class SignalReportViewSet(
continue
for outcome, minimum in minimums.items():
head = MODEL_HEAD_BY_OUTCOME[outcome]
- queryset = queryset.filter(GreaterThanOrEqual(latest_ranking_value(head, key), minimum))
+ fields = PUBLIC_SCORE_FIELDS if key == "scores" else PUBLIC_LIFT_FIELDS
+ value = F(fields[head]) if self._inbox_sort_cache else latest_ranking_value(head, key)
+ queryset = queryset.filter(GreaterThanOrEqual(value, minimum))
return queryset
def _parse_ranking_minimums(self, param: str) -> dict[str, float]:
@@ -2684,15 +2694,6 @@ class SignalReportViewSet(
minimums[outcome] = max(minimum, minimums.get(outcome, minimum))
return minimums
- def _needs_priority_annotation(self) -> bool:
- # The priority value is a correlated subquery too, and the serializer renders priority from
- # the prefetched artefacts instead, so only a priority filter or a priority sort needs it.
- params = self.request.query_params
- if params.get("priority") or params.get("use_priority_preference", "false").lower() == "true":
- return True
- priority_clause = self._SIGNAL_REPORT_ORDERING_FIELDS["priority"]
- return any(clause.lstrip("-") == priority_clause for clause in self._parse_signal_report_ordering())
-
def _prefetch_signal_report_priority_artefacts(self, queryset):
return queryset.prefetch_related(
Prefetch(