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): stop personal ranking reads from queuing a duplicate build (#127868)

posthog[bot]4ccc96fe00db
diff --git a/products/signals/backend/personal_ranking/serve.py b/products/signals/backend/personal_ranking/serve.py
index 9da2f0a19cfd..df34cab98852 100644
--- a/products/signals/backend/personal_ranking/serve.py
+++ b/products/signals/backend/personal_ranking/serve.py
@@ -3,7 +3,8 @@
A stored list is reused while it is younger than `freshness_minutes` and its input hash matches the current
inputs. Otherwise, and for every `refresh` or `focus` request, the read queues one background build per person
and serves the latest stored list as stale, or the candidates in inbox order when there is none. The focus is
-part of the hash, so an unfocused request never reuses a focused list. A lock lets one build per person run at a
+part of the hash, so an unfocused request never reuses a focused list. A build that stores a list for the current
+inputs while the read runs answers the read, also a `refresh` read, so the read queues no second build. A lock lets one build per person run at a
time, and each queued build spends two per-person token buckets. A build that may call the model also spends
the shared daily limit of the region (`build_budget.py`). `wait_seconds` waits a bounded time for the queued build
to store its list.
@@ -159,15 +160,30 @@ def _spend_build(team_id: int, user_id: int, config: PersonalRankingConfig, *, c
def _start_build(
- team: Team, user: User, config: PersonalRankingConfig, focus: str | None, limit: int, *, calls_model: bool
-) -> BuildStart:
- """Queue one background build, unless one is already running for the person or the buckets refuse it."""
+ team: Team,
+ user: User,
+ config: PersonalRankingConfig,
+ focus: str | None,
+ limit: int,
+ *,
+ calls_model: bool,
+ seen: PersonalInboxRanking | None,
+ current_hash: str,
+) -> BuildStart | PersonalInboxRanking:
+ """Queue one background build, unless one is already running for the person or the buckets refuse it.
+
+ Returns a row instead when an unfocused request finds a row for the current inputs stored after `seen`.
+ """
lock_key = build_lock_key(team.id, user.id)
locked = False
try:
locked = cache.add(lock_key, "1", timeout=BUILD_LOCK_TTL_SECONDS)
if not locked:
return "running"
+ # A build can store its row and free the lock after the read looked for a row.
+ if focus is None and (stored := _row_stored_since(team, user, seen, current_hash)) is not None:
+ cache.delete(lock_key)
+ return stored
if _spend_build(team.id, user.id, config, calls_model=calls_model) is not None:
cache.delete(lock_key)
return "rate_limited"
@@ -186,6 +202,15 @@ def _latest_row(team: Team, user: User) -> PersonalInboxRanking | None:
return PersonalInboxRanking.objects.for_team(team.id).filter(user_id=user.id).order_by("-created_at", "-id").first()
+def _row_stored_since(
+ team: Team, user: User, seen: PersonalInboxRanking | None, current_hash: str
+) -> PersonalInboxRanking | None:
+ row = _latest_row(team, user)
+ if row is None or (seen is not None and row.id == seen.id) or row.input_hash != current_hash:
+ return None
+ return row
+
+
def _wait_for_new_row(
team: Team, user: User, previous: PersonalInboxRanking | None, wait_seconds: int
) -> PersonalInboxRanking | None:
@@ -328,20 +353,28 @@ def serve_personal_ranking(
wait_seconds = min(max(wait_seconds, 0), MAX_WAIT_SECONDS)
now = timezone.now()
- latest = _latest_row(team, user)
+ started_with = _latest_row(team, user)
candidates = gather_candidates(team, user, config, now=now, with_sources=False)
summary_version = _ready_summary_version(team, user)
current_hash = input_hash(candidates, activity_summary_version=summary_version, focus=focus)
+ # Read again, because a build can store a row while the candidates are gathered.
+ latest = _latest_row(team, user)
current = (
latest is not None
and latest.created_at >= now - datetime.timedelta(minutes=config.freshness_minutes)
and latest.input_hash == current_hash
)
- if latest is not None and current and not refresh and focus is None:
+ # A row stored after this read started also answers a refresh.
+ stored_since_start = latest is not None and (started_with is None or latest.id != started_with.id)
+ if latest is not None and current and focus is None and (not refresh or stored_since_start):
return _serve_row(latest, candidates, limit=limit, cached=True, stale=False, building=False)
calls_model = bool(candidates) and may_call_model(team, summary_ready=summary_version is not None)
- start = _start_build(team, user, config, focus, limit, calls_model=calls_model)
+ start = _start_build(
+ team, user, config, focus, limit, calls_model=calls_model, seen=latest, current_hash=current_hash
+ )
+ if isinstance(start, PersonalInboxRanking):
+ return _serve_row(start, candidates, limit=limit, cached=True, stale=False, building=False)
building = start in ("started", "running")
if building and wait_seconds:
built = _wait_for_new_row(team, user, latest, wait_seconds)
diff --git a/products/signals/backend/test/test_personal_ranking.py b/products/signals/backend/test/test_personal_ranking.py
index 9fc507b1a320..c3ac114d0f08 100644
--- a/products/signals/backend/test/test_personal_ranking.py
+++ b/products/signals/backend/test/test_personal_ranking.py
@@ -34,6 +34,7 @@ from products.signals.backend.models import (
SignalReportArtefact,
SignalReportAssignment,
)
+from products.signals.backend.personal_ranking import serve as personal_ranking_serve
from products.signals.backend.personal_ranking.candidates import Candidate
from products.signals.backend.personal_ranking.config import (
CandidatePool,
@@ -784,6 +785,45 @@ class TestForMeEndpoint(APIBaseTest):
body = self._get(refresh="true", wait_seconds=5)
assert (self._state(body), self._rows()) == ((False, False, False), 5)
+ @parameterized.expand(
+ [
+ ("during_gather", "gather_candidates", {}, False, False),
+ ("before_lock", "may_call_model", {}, False, False),
+ ("refresh", "gather_candidates", {"refresh": "true"}, False, False),
+ ("refresh_before_lock", "may_call_model", {"refresh": "true"}, False, False),
+ ("inputs_changed", "gather_candidates", {}, True, True),
+ ("focus", "gather_candidates", {"focus": "checkout"}, False, True),
+ ]
+ )
+ def test_a_build_that_lands_during_the_read_answers_it(
+ self, _name: str, step: str, params: dict[str, Any], change_inputs: bool, builds: bool
+ ) -> None:
+ self.payload = {"build_burst": 3, "builds_per_hour": 1}
+ report = self._claimed_report("First")
+ self._get()
+ self._run_builds()
+ report.title = "First, edited"
+ report.save()
+ self._get()
+
+ real_step = getattr(personal_ranking_serve, step)
+
+ def build_lands(*args: Any, **kwargs: Any) -> Any:
+ self._run_builds()
+ if change_inputs:
+ report.title = "First, edited again"
+ report.save()
+ return real_step(*args, **kwargs)
+
+ with patch.object(personal_ranking_serve, step, side_effect=build_lands):
+ body = self._get(**params)
+
+ assert (self._rows(), len(self.queued)) == (2, int(builds))
+ assert self._state(body) == ((True, True, True) if builds else (True, False, False))
+ self._run_builds()
+ # A read that queued no build spent no bucket, so the burst still has one build left.
+ assert self._get(refresh="true")["building"] is not builds
+
def test_throttled_build_serves_the_stored_list_or_the_inbox_order(self) -> None:
self.payload = {"build_burst": 1, "builds_per_hour": 1}
first = self._claimed_report("First")