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.

feat(signals): make personal ranking candidate pools configurable (#126638)

posthog[bot]ac2b8441db60
diff --git a/products/signals/backend/management/AGENTS.md b/products/signals/backend/management/AGENTS.md
index 2ed138533a79..70473cab3183 100644
--- a/products/signals/backend/management/AGENTS.md
+++ b/products/signals/backend/management/AGENTS.md
@@ -278,6 +278,24 @@ uv run manage.py run_personal_inbox_ranking --team-id 1 --user-id 1 --dry-run
uv run manage.py run_personal_inbox_ranking --team-id 1 --user-id 1 --focus "checkout errors"
```
+An optional `pools` list in the payload replaces the default candidate pools, in merge order:
+
+```json
+{
+ "pools": [
+ { "source": "priority", "size": 5, "lowest_priority": "P1" },
+ { "source": "for_you_recommended", "size": 10 },
+ { "source": "assigned", "size": 0 },
+ { "source": "scored", "size": 10, "head": "fixed", "actionable_only": true, "has_pr": false, "window_days": 30 }
+ ]
+}
+```
+
+Each entry takes a `source` (`for_you_recommended`, `for_you_updated`, `assigned`, `scored` or `priority`), a `size` (0 turns it off) and an optional `name`.
+`scored` needs a `head` (`open`, `action` or `fixed`) and only joins for a person who can see the ranking model.
+`scored` and `priority` also take `actionable_only`, `has_pr` and `window_days`.
+The build logs and drops an invalid entry, and uses the default pools when no entry is valid.
+
A person with no ready activity summary still gets a list, marked `personalized: False`.
When the model call fails or answers outside the schema, the list is in model order and `fallback_reason` says why.
diff --git a/products/signals/backend/personal_ranking/candidates.py b/products/signals/backend/personal_ranking/candidates.py
index 1f440a1440b9..1c3a8f71b9d7 100644
--- a/products/signals/backend/personal_ranking/candidates.py
+++ b/products/signals/backend/personal_ranking/candidates.py
@@ -1,7 +1,7 @@
"""The reports a personal ranking considers: a few server-side pools, deduped and capped, with the facts the prompt shows.
-The pools come in a fixed order, and a report keeps the position of the first pool that holds it. That
-order is the model order: the fallback when the LLM does not rank a report.
+The pools come in the configured order, and a report keeps the position of the first pool that holds it.
+That order is the model order: the fallback when the LLM does not rank a report.
"""
import datetime
@@ -28,19 +28,18 @@ from products.signals.backend.implementation_pr import (
implementation_pr_report_filter,
)
from products.signals.backend.models import AutonomyPriority, SignalReport, SignalReportArtefact
-from products.signals.backend.personal_ranking.config import PersonalRankingConfig
+from products.signals.backend.personal_ranking.config import CandidatePool, PersonalRankingConfig, candidate_pools
from products.signals.backend.ranking.access import can_view_inbox_ranking
from products.signals.backend.ranking.inbox_sort import InboxRanker, latest_ranking_value, resolve_recommended_ranker
from products.signals.backend.ranking.report_predictions import ReportPredictions, read_report_predictions
from products.signals.backend.ranking.staleness import EDIT_ARTEFACT_TYPES
from products.signals.backend.report_claims import reports_claimed_for_user
from products.signals.backend.report_lifecycle import report_status_filter
-from products.signals.backend.report_priority import latest_report_priorities
+from products.signals.backend.report_priority import latest_priority_rank, latest_report_priorities
from products.signals.backend.signal_metadata import fetch_source_products_for_reports
logger = structlog.get_logger(__name__)
-PoolName = Literal["for_you_recommended", "for_you_updated", "assigned", "actionable", "new"]
NeedsYouKind = Literal["suggested_reviewer", "claimed", "waiting_on_input"]
EXCLUDED_STATUSES: tuple[str, ...] = (
@@ -76,7 +75,8 @@ class Candidate:
predictions: ReportPredictions | None
needs_you: tuple[NeedsYouKind, ...]
tags: tuple[str, ...]
- pools: tuple[PoolName, ...]
+ # The names of the configured pools that hold the report, in pool order.
+ pools: tuple[str, ...]
# Compares field by field in declaration order, so the order of the fields is the sort order.
@@ -179,51 +179,86 @@ def _scored_pool(queryset: QuerySet[SignalReport], head: str, size: int) -> list
)
+def _priority_pool(queryset: QuerySet[SignalReport], lowest_priority: str, size: int) -> list[str]:
+ """Reports with `lowest_priority` or a more urgent one, the most urgent first. A score is not needed."""
+ return _ids(
+ queryset.annotate(_pool_priority=latest_priority_rank())
+ .filter(_pool_priority__lte=AutonomyPriority.values.index(lowest_priority))
+ .order_by("_pool_priority", "-updated_at", "id"),
+ size,
+ )
+
+
+def _filtered(
+ team_reports: QuerySet[SignalReport], team_id: int, pool: CandidatePool, since: datetime.datetime
+) -> QuerySet[SignalReport]:
+ if pool.actionable_only:
+ reports = team_reports.filter(
+ status=SignalReport.Status.READY,
+ latest_actionability=ActionabilityChoice.IMMEDIATELY_ACTIONABLE.value,
+ latest_already_addressed=False,
+ )
+ else:
+ reports = (
+ team_reports.filter(status__in=_OPEN_STATUSES)
+ .exclude(latest_actionability=ActionabilityChoice.NOT_ACTIONABLE.value)
+ .exclude(latest_already_addressed=True)
+ )
+ reports = reports.filter(created_at__gte=since)
+ if pool.has_pr is True:
+ reports = reports.filter(implementation_pr_report_filter(team_id=team_id))
+ elif pool.has_pr is False:
+ reports = reports.exclude(implementation_pr_report_filter(team_id=team_id))
+ return reports
+
+
+def _pool_ids(
+ team: Team,
+ user: User,
+ pool: CandidatePool,
+ config: PersonalRankingConfig,
+ *,
+ team_reports: QuerySet[SignalReport],
+ now: datetime.datetime,
+) -> list[str]:
+ since = now - datetime.timedelta(days=pool.window_days or config.window_days)
+ match pool.source:
+ case "for_you_recommended":
+ return _recommended_for_you(team, user, since, pool.size)
+ case "for_you_updated":
+ return _ids(reports_for_person(team.id, user).order_by("-updated_at", "id"), pool.size)
+ case "assigned":
+ return _ids(
+ team_reports.filter(reports_claimed_for_user(team_id=team.id, user_id=user.id)).order_by(
+ "-updated_at", "id"
+ ),
+ pool.size,
+ )
+ case "scored":
+ assert pool.head is not None
+ return _scored_pool(_filtered(team_reports, team.id, pool, since), pool.head, pool.size)
+ case "priority":
+ return _priority_pool(_filtered(team_reports, team.id, pool, since), pool.lowest_priority, pool.size)
+
+
def _pools(
team: Team, user: User, config: PersonalRankingConfig, *, now: datetime.datetime, can_view_ranking: bool
-) -> list[tuple[PoolName, list[str]]]:
- since = now - datetime.timedelta(days=config.window_days)
+) -> list[tuple[str, list[str]]]:
# The gate-aware filter, because a `monitoring` report reads as resolved while the monitoring gate is off.
team_reports = SignalReport.objects.filter(team_id=team.id).exclude(
report_status_filter(EXCLUDED_STATUSES, team_id=team.id)
)
- pools: list[tuple[PoolName, list[str]]] = [
- ("for_you_recommended", _recommended_for_you(team, user, since, config.for_you_recommended_pool)),
- (
- "for_you_updated",
- _ids(reports_for_person(team.id, user).order_by("-updated_at", "id"), config.for_you_updated_pool),
- ),
- (
- "assigned",
- _ids(
- team_reports.filter(reports_claimed_for_user(team_id=team.id, user_id=user.id)).order_by(
- "-updated_at", "id"
- ),
- config.assigned_pool,
- ),
- ),
+ return [
+ (pool.name, _pool_ids(team, user, pool, config, team_reports=team_reports, now=now))
+ for pool in candidate_pools(config)
+ # The `scored` pools show the ranking model's order, so only a person who can see the ranking gets them.
+ if pool.size and (pool.source != "scored" or can_view_ranking)
]
- if not can_view_ranking:
- return pools
- actionable = team_reports.filter(
- status=SignalReport.Status.READY,
- latest_actionability=ActionabilityChoice.IMMEDIATELY_ACTIONABLE.value,
- latest_already_addressed=False,
- created_at__gte=since,
- ).exclude(implementation_pr_report_filter(team_id=team.id))
- new = (
- team_reports.filter(status__in=_OPEN_STATUSES, created_at__gte=since)
- .exclude(latest_actionability=ActionabilityChoice.NOT_ACTIONABLE.value)
- .exclude(latest_already_addressed=True)
- )
- pools.append(("actionable", _scored_pool(actionable, "action", config.actionable_pool)))
- pools.append(("new", _scored_pool(new, "open", config.new_pool)))
- return pools
-def merge_pools(pools: Sequence[tuple[PoolName, Sequence[str]]], max_candidates: int) -> dict[str, list[PoolName]]:
+def merge_pools(pools: Sequence[tuple[str, Sequence[str]]], max_candidates: int) -> dict[str, list[str]]:
"""Report ids in pool order, each with every pool that holds it, cut to `max_candidates`."""
- merged: dict[str, list[PoolName]] = {}
+ merged: dict[str, list[str]] = {}
for name, report_ids in pools:
for report_id in report_ids:
if report_id in merged:
diff --git a/products/signals/backend/personal_ranking/config.py b/products/signals/backend/personal_ranking/config.py
index 327807855f72..b7a466d36644 100644
--- a/products/signals/backend/personal_ranking/config.py
+++ b/products/signals/backend/personal_ranking/config.py
@@ -3,9 +3,13 @@
A missing or unreadable payload gives the code defaults. An invalid field falls back to its default,
a large one to its maximum, so no value is unbounded. `"enabled": false` in the payload is the kill
switch: it stops every build.
+
+The optional `pools` list replaces the default candidate pools, in order. An invalid entry is dropped
+and logged. When no entry is valid, the build uses the default pools.
"""
-from typing import Any
+import re
+from typing import Any, Literal, cast, get_args
import structlog
@@ -21,6 +25,26 @@ PERSONAL_INBOX_RANKING_FLAG_DISTINCT_ID = "signals_personal_inbox_ranking"
MAX_MODEL_NAME_CHARS = 100
DEFAULT_MODEL = "gpt-6-luna"
+PoolSource = Literal["for_you_recommended", "for_you_updated", "assigned", "scored", "priority"]
+POOL_SOURCES: tuple[PoolSource, ...] = get_args(PoolSource)
+ScoreHead = Literal["open", "action", "fixed"]
+SCORE_HEADS: tuple[ScoreHead, ...] = get_args(ScoreHead)
+PriorityLevel = Literal["P0", "P1", "P2", "P3", "P4"]
+PRIORITY_LEVELS: tuple[PriorityLevel, ...] = get_args(PriorityLevel)
+DEFAULT_LOWEST_PRIORITY: PriorityLevel = "P1"
+MAX_POOLS = 10
+_POOL_NAME = re.compile(r"^[a-z0-9_]{1,40}$")
+# The keys a payload pool entry may have for each source. Any other key drops the entry, so a typo shows in the logs.
+_COMMON_POOL_KEYS = frozenset({"source", "size", "name"})
+_FILTER_POOL_KEYS = frozenset({"actionable_only", "has_pr", "window_days"})
+_POOL_KEYS: dict[PoolSource, frozenset[str]] = {
+ "for_you_recommended": _COMMON_POOL_KEYS,
+ "for_you_updated": _COMMON_POOL_KEYS,
+ "assigned": _COMMON_POOL_KEYS,
+ "scored": _COMMON_POOL_KEYS | _FILTER_POOL_KEYS | {"head"},
+ "priority": _COMMON_POOL_KEYS | _FILTER_POOL_KEYS | {"lowest_priority"},
+}
+
@frozen
class ReasonLimits:
@@ -31,11 +55,33 @@ class ReasonLimits:
later: int = 100
+@frozen
+class CandidatePool:
+ """One source of candidates. The name labels the pool on each candidate and in the build event."""
+
+ name: str
+ source: PoolSource
+ # 0 turns the pool off.
+ size: int
+ # `scored` only: the ranking model head that orders the pool.
+ head: ScoreHead | None = None
+ # `priority` only: the pool takes reports with this priority or a more urgent one.
+ lowest_priority: PriorityLevel = DEFAULT_LOWEST_PRIORITY
+ # `scored` and `priority` only. True takes ready reports judged immediately actionable. False takes
+ # ready and pending input reports that are not judged not actionable.
+ actionable_only: bool = False
+ # `scored` and `priority` only. None takes reports with and without an implementation PR.
+ has_pr: bool | None = None
+ # `scored` and `priority` only: the days back the pool reads by creation time. None uses the config `window_days`.
+ window_days: int | None = None
+
+
@frozen
class PersonalRankingConfig:
enabled: bool = True
model: str = DEFAULT_MODEL
- # The `for_you` pool in `sort=recommended` order and the two ranking model pools read reports created in this window.
+ # The `for_you_recommended` pool, and the `scored` and `priority` pools without their own `window_days`, read
+ # reports created in this window.
window_days: int = 14
for_you_recommended_pool: int = 30
# No window, so older reports with new activity come in too.
@@ -43,6 +89,8 @@ class PersonalRankingConfig:
assigned_pool: int = 20
actionable_pool: int = 20
new_pool: int = 20
+ # The pools in merge order. Empty uses `default_pools`, which take their sizes from the `*_pool` fields.
+ pools: tuple[CandidatePool, ...] = ()
max_candidates: int = 60
# The entries that get a `next_step`, and the most a caller can ask for.
default_limit: int = 25
@@ -74,6 +122,7 @@ _MAXIMUMS: dict[str, int] = {
"actionable_pool": 100,
"new_pool": 100,
"max_candidates": 100,
+ "pool_size": 100,
"default_limit": 100,
"max_limit": 100,
"next_step_max_chars": 400,
@@ -119,19 +168,117 @@ def _reason_limits(value: Any) -> ReasonLimits:
)
+def default_pools(config: PersonalRankingConfig) -> tuple[CandidatePool, ...]:
+ return (
+ CandidatePool(name="for_you_recommended", source="for_you_recommended", size=config.for_you_recommended_pool),
+ CandidatePool(name="for_you_updated", source="for_you_updated", size=config.for_you_updated_pool),
+ CandidatePool(name="assigned", source="assigned", size=config.assigned_pool),
+ CandidatePool(
+ name="actionable",
+ source="scored",
+ size=config.actionable_pool,
+ head="action",
+ actionable_only=True,
+ has_pr=False,
+ ),
+ CandidatePool(name="new", source="scored", size=config.new_pool, head="open"),
+ )
+
+
+def candidate_pools(config: PersonalRankingConfig) -> tuple[CandidatePool, ...]:
+ return config.pools or default_pools(config)
+
+
+class _InvalidPool(ValueError):
+ pass
+
+
+def _pool_count(entry: dict[str, Any], key: str, *, minimum: int, maximum: int) -> int:
+ value = entry.get(key)
+ if isinstance(value, bool) or not isinstance(value, int) or value < minimum:
+ raise _InvalidPool(f"{key} must be an integer of at least {minimum}")
+ return min(value, maximum)
+
+
+def _pool_flag(entry: dict[str, Any], key: str) -> bool | None:
+ value = entry.get(key)
+ if value is not None and not isinstance(value, bool):
+ raise _InvalidPool(f"{key} must be true, false or null")
+ return value
+
+
+def _parse_pool(entry: Any) -> CandidatePool:
+ if not isinstance(entry, dict):
+ raise _InvalidPool("entry must be an object")
+ source = entry.get("source")
+ if source not in POOL_SOURCES:
+ raise _InvalidPool(f"source must be one of {', '.join(POOL_SOURCES)}")
+ source = cast(PoolSource, source)
+ unknown = set(entry) - _POOL_KEYS[source]
+ if unknown:
+ raise _InvalidPool(f"unknown keys for source {source}: {', '.join(sorted(unknown))}")
+ head = entry.get("head")
+ if source == "scored" and head not in SCORE_HEADS:
+ raise _InvalidPool(f"head must be one of {', '.join(SCORE_HEADS)}")
+ lowest_priority = entry.get("lowest_priority", DEFAULT_LOWEST_PRIORITY)
+ if lowest_priority not in PRIORITY_LEVELS:
+ raise _InvalidPool(f"lowest_priority must be one of {', '.join(PRIORITY_LEVELS)}")
+ default_name = f"scored_{head}" if source == "scored" else source
+ name = entry.get("name", default_name)
+ if not isinstance(name, str) or not _POOL_NAME.match(name):
+ raise _InvalidPool("name must be 1 to 40 lowercase letters, digits or underscores")
+ return CandidatePool(
+ name=name,
+ source=source,
+ size=_pool_count(entry, "size", minimum=0, maximum=_MAXIMUMS["pool_size"]),
+ head=cast(ScoreHead, head) if source == "scored" else None,
+ lowest_priority=cast(PriorityLevel, lowest_priority),
+ actionable_only=_pool_flag(entry, "actionable_only") or False,
+ has_pr=_pool_flag(entry, "has_pr"),
+ window_days=None
+ if entry.get("window_days") is None
+ else _pool_count(entry, "window_days", minimum=1, maximum=_MAXIMUMS["window_days"]),
+ )
+
+
+def _pools(value: Any) -> tuple[CandidatePool, ...]:
+ """The valid entries of the payload `pools` list. Empty when the list is missing or holds no valid entry."""
+ if value is None:
+ return ()
+ if not isinstance(value, list):
+ logger.warning("personal_inbox_ranking_pools_invalid", reason="pools must be a list")
+ return ()
+ pools: list[CandidatePool] = []
+ for index, entry in enumerate(value):
+ try:
+ if index >= MAX_POOLS:
+ raise _InvalidPool(f"at most {MAX_POOLS} pools")
+ pool = _parse_pool(entry)
+ if any(existing.name == pool.name for existing in pools):
+ raise _InvalidPool(f"duplicate name {pool.name}")
+ except _InvalidPool as error:
+ logger.warning("personal_inbox_ranking_pool_dropped", index=index, reason=str(error))
+ continue
+ pools.append(pool)
+ if not pools:
+ logger.warning("personal_inbox_ranking_pools_invalid", reason="no valid pool, so the default pools apply")
+ return tuple(pools)
+
+
def parse_config(payload: Any) -> PersonalRankingConfig:
if not isinstance(payload, dict):
return DEFAULTS
ints = {
name: _bounded_int(payload.get(name), getattr(DEFAULTS, name), maximum)
for name, maximum in _MAXIMUMS.items()
- if name not in {"timeout_seconds", "reason_max_chars"}
+ if name not in {"timeout_seconds", "reason_max_chars", "pool_size"}
}
max_limit = ints.pop("max_limit")
return PersonalRankingConfig(
enabled=payload.get("enabled") is not False,
model=_model(payload.get("model")),
reason_limits=_reason_limits(payload.get("reason_max_chars")),
+ pools=_pools(payload.get("pools")),
timeout_seconds=_bounded_number(
payload.get("timeout_seconds"), DEFAULTS.timeout_seconds, _MAXIMUMS["timeout_seconds"]
),
@@ -142,5 +289,10 @@ def parse_config(payload: Any) -> PersonalRankingConfig:
def read_config() -> PersonalRankingConfig:
config = parse_config(read_flag_payload(PERSONAL_INBOX_RANKING_FLAG, PERSONAL_INBOX_RANKING_FLAG_DISTINCT_ID))
- logger.info("personal_inbox_ranking_config_read", enabled=config.enabled, model=config.model)
+ logger.info(
+ "personal_inbox_ranking_config_read",
+ enabled=config.enabled,
+ model=config.model,
+ pools=[pool.name for pool in candidate_pools(config) if pool.size],
+ )
return config
diff --git a/products/signals/backend/report_priority.py b/products/signals/backend/report_priority.py
index 29b1e48bec73..e51a2f2b95a3 100644
--- a/products/signals/backend/report_priority.py
+++ b/products/signals/backend/report_priority.py
@@ -1,11 +1,14 @@
from collections.abc import Sequence
+from django.contrib.postgres.fields import ArrayField
from django.db import transaction
+from django.db.models import CharField, F, Func, IntegerField, JSONField, OuterRef, Subquery, Value
+from django.db.models.functions import Cast, Coalesce
from products.signals.backend.artefact_attribution import ArtefactAttribution
from products.signals.backend.artefact_schemas import PriorityAdjustment, PriorityAssessment, priority_from_judgment
from products.signals.backend.enums import ReportPriority
-from products.signals.backend.models import SignalReport, SignalReportArtefact
+from products.signals.backend.models import AutonomyPriority, SignalReport, SignalReportArtefact
def update_report_priority(*, report: SignalReport, priority: ReportPriority, attribution: ArtefactAttribution) -> None:
@@ -65,3 +68,37 @@ def latest_report_priorities(team_id: int, report_ids: Sequence[str]) -> dict[st
if priority is not None:
latest[report_id] = priority
return latest
+
+
+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"."""
+ # The startswith guard skips non-object content.
+ # The rank is one `array_position` inside the subquery, not a CASE: Django repeats the CASE
+ # operand in each WHEN branch, so a CASE runs the subquery or the JSON parse once per priority.
+ unranked = Value(len(AutonomyPriority.values))
+ priority_value = Func(
+ Cast(F("content"), output_field=JSONField()),
+ Value("priority"),
+ function="jsonb_extract_path_text",
+ output_field=CharField(),
+ )
+ priority_position = Func(
+ Value(list(AutonomyPriority.values), output_field=ArrayField(CharField())),
+ priority_value,
+ function="array_position",
+ output_field=IntegerField(),
+ )
+ latest_rank = Subquery(
+ SignalReportArtefact.objects.filter(
+ report_id=OuterRef("id"),
+ type=SignalReportArtefact.ArtefactType.PRIORITY_JUDGMENT,
+ content__startswith="{",
+ )
+ .order_by("-created_at")
+ # `array_position` counts from 1 and returns NULL for a value outside the list.
+ .annotate(_priority_rank=priority_position - 1)
+ .values("_priority_rank")[:1],
+ output_field=IntegerField(),
+ )
+ return Coalesce(latest_rank, unranked, output_field=IntegerField())
diff --git a/products/signals/backend/test/test_personal_ranking.py b/products/signals/backend/test/test_personal_ranking.py
index f7aafce0be0c..0f6ea345f35b 100644
--- a/products/signals/backend/test/test_personal_ranking.py
+++ b/products/signals/backend/test/test_personal_ranking.py
@@ -34,7 +34,12 @@ from products.signals.backend.models import (
SignalReportAssignment,
)
from products.signals.backend.personal_ranking.candidates import Candidate
-from products.signals.backend.personal_ranking.config import PersonalRankingConfig, ReasonLimits, parse_config
+from products.signals.backend.personal_ranking.config import (
+ CandidatePool,
+ PersonalRankingConfig,
+ ReasonLimits,
+ parse_config,
+)
from products.signals.backend.personal_ranking.logic import BUILT_EVENT, build_personal_ranking
from products.signals.backend.personal_ranking.prompt import render_person, user_message
from products.signals.backend.personal_ranking.validation import MalformedRankingError, validate_ranking
@@ -226,6 +231,57 @@ class TestConfig(SimpleTestCase):
{"default_limit": 40, "max_limit": 30},
PersonalRankingConfig(default_limit=30, max_limit=30),
),
+ (
+ "pools_in_order_with_default_names_and_large_values_capped",
+ {
+ "pools": [
+ {"source": "priority", "size": 5, "lowest_priority": "P0", "window_days": 1000},
+ {"source": "assigned", "size": 0},
+ {"source": "scored", "size": 500, "head": "fixed", "has_pr": True, "actionable_only": True},
+ ]
+ },
+ PersonalRankingConfig(
+ pools=(
+ CandidatePool(name="priority", source="priority", size=5, lowest_priority="P0", window_days=90),
+ CandidatePool(name="assigned", source="assigned", size=0),
+ CandidatePool(
+ name="scored_fixed",
+ source="scored",
+ size=100,
+ head="fixed",
+ has_pr=True,
+ actionable_only=True,
+ ),
+ )
+ ),
+ ),
+ (
+ "invalid_pools_drop",
+ {
+ "pools": [
+ {"source": "assigned", "size": 5, "name": "mine"},
+ {"source": "for_you_updated", "size": 5, "name": "mine"},
+ {"source": "everything", "size": 5},
+ {"source": "scored", "size": 5},
+ {"source": "assigned", "size": 5, "head": "open"},
+ {"source": "assigned", "size": True},
+ {"source": "assigned", "size": -1},
+ {"source": "priority", "size": 5, "lowest_priority": "urgent"},
+ {"source": "priority", "size": 5, "has_pr": "yes"},
+ {"source": "assigned", "size": 5, "name": "Has Spaces"},
+ ]
+ },
+ PersonalRankingConfig(pools=(CandidatePool(name="mine", source="assigned", size=5),)),
+ ),
+ ("no_valid_pool_keeps_the_defaults", {"pools": [{"source": "scored", "size": 5}]}, PersonalRankingConfig()),
+ ("pools_not_a_list", {"pools": {"source": "assigned", "size": 5}}, PersonalRankingConfig()),
+ (
+ "pools_past_the_maximum_drop",
+ {"pools": [{"source": "assigned", "size": 1, "name": f"p{index}"} for index in range(12)]},
+ PersonalRankingConfig(
+ pools=tuple(CandidatePool(name=f"p{index}", source="assigned", size=1) for index in range(10))
+ ),
+ ),
]
)
def test_parse_config(self, _name: str, payload: Any, expected: PersonalRankingConfig) -> None:
@@ -306,6 +362,15 @@ class TestBuildPersonalRanking(BaseTest):
).model_dump_json(),
)
+ def _prioritize(self, report: SignalReport, priority: str) -> SignalReport:
+ SignalReportArtefact.objects.create(
+ team=self.team,
+ report=report,
+ type=SignalReportArtefact.ArtefactType.PRIORITY_JUDGMENT,
+ content=json.dumps({"priority": priority, "explanation": "test"}),
+ )
+ return report
+
def _setup_reports(self) -> dict[str, SignalReport]:
reports = {
"waiting": self._names_me(self._report("Waiting", status=SignalReport.Status.PENDING_INPUT)),
@@ -434,6 +499,34 @@ class TestBuildPersonalRanking(BaseTest):
assert build.status == "empty"
self.create.assert_not_called()
+ @parameterized.expand([("can_see_scores", True), ("cannot_see_ranking", False)])
+ def test_configured_pools_keep_their_order_and_names(self, _name: str, staff: bool) -> None:
+ self.user.is_staff = staff
+ self.user.save(update_fields=["is_staff"])
+ urgent = self._prioritize(self._report("Urgent and unscored"), "P1")
+ self._prioritize(self._report("Not urgent"), "P3")
+ scored = self._report("Scored")
+ self._score(scored, action=0.8)
+ claimed = self._report("Claimed", status=SignalReport.Status.IN_PROGRESS)
+ SignalReportAssignment.all_teams.create(team=self.team, report=claimed, actor_kind="user", actor_user=self.user)
+ self.create.return_value = _response(_answer([_entry("c1", 1)]))
+ config = PersonalRankingConfig(
+ pools=(
+ CandidatePool(name="assigned", source="assigned", size=0),
+ CandidatePool(name="scored_action", source="scored", size=5, head="action"),
+ CandidatePool(name="urgent", source="priority", size=5),
+ )
+ )
+
+ with patch("products.signals.backend.ranking.access.feature_enabled_or_false", return_value=False):
+ build = build_personal_ranking(self.team, self.user, config=config, dry_run=True)
+
+ pools = [(candidate.report_id, candidate.pools) for candidate in build.candidates]
+ expected = [(str(urgent.id), ("urgent",))]
+ if staff:
+ expected = [(str(scored.id), ("scored_action",)), *expected]
+ assert pools == expected
+
@parameterized.expand([("dry_run", True, 0), ("stored", False, 1)])
def test_command(self, _name: str, dry_run: bool, expected_rows: int) -> None:
reports = self._setup_reports()
diff --git a/products/signals/backend/views.py b/products/signals/backend/views.py
index 2bc6bda1ea34..959c837e1d99 100644
--- a/products/signals/backend/views.py
+++ b/products/signals/backend/views.py
@@ -10,19 +10,15 @@ from functools import cached_property, partial
from typing import Any, Literal, cast
from django.conf import settings
-from django.contrib.postgres.fields import ArrayField
from django.core.cache import cache
from django.db import IntegrityError, models, transaction
from django.db.models import (
BooleanField,
Case,
- CharField,
Count,
Exists,
F,
- Func,
IntegerField,
- JSONField,
OuterRef,
Prefetch,
Q,
@@ -33,7 +29,7 @@ from django.db.models import (
Window,
)
from django.db.models.expressions import OrderBy
-from django.db.models.functions import Cast, Coalesce
+from django.db.models.functions import Coalesce
from django.db.models.lookups import GreaterThanOrEqual
from django.utils import timezone
@@ -223,7 +219,7 @@ from products.signals.backend.report_metric_access import ReportMetricAccessPoli
from products.signals.backend.report_metric_refresh import CURRENT_REPORT_STATUSES, refresh_report_metric_snapshots
from products.signals.backend.report_monitoring_attempts import complete_implementation
from products.signals.backend.report_plan import report_plan
-from products.signals.backend.report_priority import update_report_priority
+from products.signals.backend.report_priority import latest_priority_rank, update_report_priority
from products.signals.backend.report_read_state import (
ReportReadStateRequestSerializer,
ReportReadStateResponseSerializer,
@@ -2470,37 +2466,7 @@ 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".
- # The startswith guard skips non-object content.
- # The rank is one `array_position` inside the subquery, not a CASE: Django repeats the CASE
- # operand in each WHEN branch, so a CASE runs the subquery or the JSON parse once per priority.
- unranked = Value(len(AutonomyPriority.values))
- priority_value = Func(
- Cast(F("content"), output_field=JSONField()),
- Value("priority"),
- function="jsonb_extract_path_text",
- output_field=CharField(),
- )
- priority_position = Func(
- Value(list(AutonomyPriority.values), output_field=ArrayField(CharField())),
- priority_value,
- function="array_position",
- output_field=IntegerField(),
- )
- latest_priority_rank = Subquery(
- SignalReportArtefact.objects.filter(
- report_id=OuterRef("id"),
- type=SignalReportArtefact.ArtefactType.PRIORITY_JUDGMENT,
- content__startswith="{",
- )
- .order_by("-created_at")
- # `array_position` counts from 1 and returns NULL for a value outside the list.
- .annotate(_priority_rank=priority_position - 1)
- .values("_priority_rank")[:1],
- output_field=IntegerField(),
- )
- return queryset.annotate(
- priority_sort_rank=Coalesce(latest_priority_rank, unranked, output_field=IntegerField())
- )
+ return queryset.annotate(priority_sort_rank=latest_priority_rank())
def _annotate_signal_report_ranking(self, queryset):
# `ordering=ranking_<head>` sorts by the served model's probability for that head, read from