: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
feat(signals): keep only recent personal inbox rankings per person (#127029)
posthog[bot]e7f0e2bdc834
diff --git a/products/signals/backend/models.py b/products/signals/backend/models.pyindex c72d0d82197c..e3d9167fb5f2 100644--- a/products/signals/backend/models.py+++ b/products/signals/backend/models.py@@ -4296,8 +4296,9 @@ class UserActivitySummaryQueueCursor(models.Model):class PersonalInboxRanking(TeamScopedRootMixin, UUIDModel):"""One build of one person's ranked inbox list, from `personal_ranking/logic.py`.- Append-only: every build writes one row, so later readers can reuse the latest row while its- `input_hash` still matches the current inputs.+ Every build writes one row, so later readers can reuse the latest row while its `input_hash`+ still matches the current inputs. A build then deletes the person's rows past the newest+ `max_rows_per_person`."""class Status(models.TextChoices):diff --git a/products/signals/backend/personal_ranking/config.py b/products/signals/backend/personal_ranking/config.pyindex 7f41ec596895..b0b2790402b7 100644--- a/products/signals/backend/personal_ranking/config.py+++ b/products/signals/backend/personal_ranking/config.py@@ -118,6 +118,8 @@ class PersonalRankingConfig:build_burst: int = 2builds_per_hour: int = 12builds_per_day: int = 40+ # Each build keeps this many of the person's newest rows and deletes the older ones.+ max_rows_per_person: int = 5DEFAULTS = PersonalRankingConfig()@@ -147,6 +149,7 @@ _MAXIMUMS: dict[str, int] = {"build_burst": 20,"builds_per_hour": 120,"builds_per_day": 500,+ "max_rows_per_person": 100,"reason_max_chars": 500,}diff --git a/products/signals/backend/personal_ranking/logic.py b/products/signals/backend/personal_ranking/logic.pyindex 1a700ba2f5d8..a1b73fe2c764 100644--- a/products/signals/backend/personal_ranking/logic.py+++ b/products/signals/backend/personal_ranking/logic.py@@ -55,6 +55,8 @@ AI_PRODUCT = "personal_inbox_ranking"PIPELINE_DISTINCT_ID = "signals_personal_inbox_ranking"BUILT_EVENT = "personal inbox ranking built"TRUNCATED_FINISH_REASON = "length"+# The most old rows one build deletes. Each build deletes again, so a larger backlog shrinks over later builds.+PRUNE_BATCH_SIZE = 100FALLBACK_LLM_ERROR = "llm_error"FALLBACK_TIMEOUT = "timeout"@@ -310,6 +312,14 @@ def _store(team: Team, user: User, build: PersonalRankingBuild) -> PersonalRankireturn dataclasses.replace(build, ranking_id=str(row.id))+def _prune(team: Team, user: User, keep: int) -> None:+ """Delete one batch of the person's rows older than the newest `keep`, so the table stays bounded."""+ rows = PersonalInboxRanking.objects.for_team(team.id).filter(user_id=user.id)+ stale_ids = list(rows.order_by("-created_at", "-id").values_list("id", flat=True)[keep : keep + PRUNE_BATCH_SIZE])+ if stale_ids:+ rows.filter(id__in=stale_ids).delete()++def build_personal_ranking(team: Team,user: User,@@ -411,5 +421,9 @@ def build_personal_ranking(if dry_run:return buildbuild = _store(team, user, build)+ try:+ _prune(team, user, config.max_rows_per_person)+ except Exception:+ logger.exception("signals.personal_ranking.prune_failed", team_id=team.id, user_id=user.id)_capture(team, user, build, config, has_focus=focus is not None)return builddiff --git a/products/signals/backend/test/test_personal_ranking.py b/products/signals/backend/test/test_personal_ranking.pyindex a814c791f1e5..003e198296be 100644--- a/products/signals/backend/test/test_personal_ranking.py+++ b/products/signals/backend/test/test_personal_ranking.py@@ -18,7 +18,7 @@ import httpxfrom openai import APITimeoutError, BadRequestError, omitfrom parameterized import parameterized-from posthog.models import User+from posthog.models import Team, Userfrom posthog.redis import TEST_clear_clientsfrom posthog.token_bucket import TEST_reset_scripts@@ -656,6 +656,24 @@ class TestBuildPersonalRanking(BaseTest):assert "next: Review the PR" in outputassert all(f"] {report.title} (" in output for report in reports.values())+ def test_a_build_keeps_only_the_newest_rows_of_that_person(self) -> None:+ config = PersonalRankingConfig(max_rows_per_person=2)+ other_user = User.objects.create_and_join(self.organization, "[email protected]", None)+ other_team = Team.objects.create(organization=self.organization, name="Other")+ start = timezone.now()+ with time_machine.travel(start, tick=False):+ build_personal_ranking(self.team, other_user, config=config)+ build_personal_ranking(other_team, self.user, config=config)+ ranking_ids = []+ for minute in range(1, 5):+ with time_machine.travel(start + datetime.timedelta(minutes=minute), tick=False):+ ranking_ids.append(build_personal_ranking(self.team, self.user, config=config).ranking_id)++ mine = PersonalInboxRanking.objects.for_team(self.team.id).filter(user_id=self.user.id)+ assert {str(row_id) for row_id in mine.values_list("id", flat=True)} == set(ranking_ids[-2:])+ assert PersonalInboxRanking.objects.for_team(self.team.id).filter(user_id=other_user.id).count() == 1+ assert PersonalInboxRanking.objects.for_team(other_team.id).filter(user_id=self.user.id).count() == 1+class TestForMeEndpoint(APIBaseTest):def setUp(self) -> None:@@ -757,7 +775,7 @@ class TestForMeEndpoint(APIBaseTest):self.delay.side_effect = build_personal_inbox_rankingbody = self._get(refresh="true", wait_seconds=5)- assert (self._state(body), self._rows()) == ((False, False, False), 7)+ assert (self._state(body), self._rows()) == ((False, False, False), 5)def test_throttled_build_serves_the_stored_list_or_the_inbox_order(self) -> None:self.payload = {"build_burst": 1, "builds_per_hour": 1}