: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
fix(replay-vision): walk each scanner's newest rows for the watch feed (#123215)
Tue Haulundb3a5af0276ce
diff --git a/products/replay_vision/backend/api/scanners.py b/products/replay_vision/backend/api/scanners.pyindex 3fb92bceb640..9d2bcd6e047a 100644--- a/products/replay_vision/backend/api/scanners.py+++ b/products/replay_vision/backend/api/scanners.py@@ -17,9 +17,8 @@ from django.db.models import (Subquery,Sum,Value,- Window,)-from django.db.models.functions import Coalesce, NullIf, RowNumber+from django.db.models.functions import Coalesce, NullIffrom django.utils import timezoneimport structlog@@ -64,6 +63,7 @@ from posthog.rate_limit import (ReplayVisionWatchFeedSustainedRateThrottle,)+from products.access_control.backend.facade.user_access_control import UserAccessControlfrom products.access_control.backend.presentation.access_control import (AccessControlViewSetMixin,UserAccessControlSerializerMixin,@@ -1632,8 +1632,7 @@ class ScannerStatsResponseSerializer(serializers.Serializer):WATCH_FEED_DEFAULT_LIMIT = 20WATCH_FEED_MAX_LIMIT = 50WATCH_FEED_DEFAULT_DATE_FROM = "-7d"-# Widest window one request may rank. An arbitrarily early `date_from` would make Postgres partition-# and sort the team's entire observation history before the row caps apply.+# Widest window one request may rank. A scanner with few rows in the window walks its whole window before the cap stops it.WATCH_FEED_MAX_WINDOW_DAYS = 90# Rank at most this many of the newest succeeded observations in the window. A very high-volume# team ranks only the newest slice, which recency-biases the feed — acceptable for a preview surface.@@ -1648,6 +1647,51 @@ WATCH_FEED_BYPASS_CHUNK = 200WATCH_FEED_IMPRESSIONS_MAX_IDS = 100+def select_watch_feed_candidates(+ walkable: QuerySet[ReplayObservation],+ scanner_ids: list[UUID],+ viewed: Exists,+ access: UserAccessControl,+ team_id: int,+) -> list[dict[str, Any]]:+ """The newest readable rows of the window, at most WATCH_FEED_PER_SCANNER_CAP per scanner, without scan results."""+ # One slice per scanner stops each index walk at the cap, where a window function sorts the whole window first.+ slices: list[Any] = [+ walkable.filter(scanner_id=scanner_id)+ .order_by("-created_at", "-id")+ .values_list("id", "created_at")[:WATCH_FEED_PER_SCANNER_CAP]+ for scanner_id in scanner_ids+ ]+ if not slices:+ return []+ walked = slices[0].union(*slices[1:], all=True)+ newest_first = [row_id for row_id, _ in sorted(walked, key=lambda row: (row[1], row[0]), reverse=True)]+ # The row-gate reads each row's snapshot JSON, so it runs on the newest walked rows only.+ rows: list[dict[str, Any]] = []+ for start in range(0, len(newest_first), WATCH_FEED_CANDIDATE_CAP):+ chunk = newest_first[start : start + WATCH_FEED_CANDIDATE_CAP]+ rows += list(+ accessible_observations(access, team_id, ReplayObservation.objects.filter(team_id=team_id, id__in=chunk))+ .annotate(feed_viewed=viewed)+ .order_by("-created_at", "-id")+ .values("id", "scanner_id", "session_id", "created_at", "watch_rank", "feed_viewed")+ )+ if len(rows) >= WATCH_FEED_CANDIDATE_CAP:+ break+ return rows[:WATCH_FEED_CANDIDATE_CAP]+++def attach_scanner_results(team_id: int, rows: list[dict[str, Any]]) -> None:+ """Load the scan result for each candidate row, by primary key, after the window cap has applied."""+ results = dict(+ ReplayObservation.objects.filter(team_id=team_id, id__in=[row["id"] for row in rows]).values_list(+ "id", "scanner_result"+ )+ )+ for row in rows:+ row["scanner_result"] = results.get(row["id"])++class WatchFeedRanker(models.TextChoices):WEIGHTED_SCORE = "weighted-score"JEV = "jev"@@ -2642,9 +2686,9 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, vi).values_list("id", flat=True))allowed_ids = [scanner_id for scanner_id in allowed_ids if scanner_id in created_ids]+ # The scanner list stays off this base: every per-scanner walk would otherwise carry the whole list.candidates = ReplayObservation.objects.filter(team_id=self.team_id,- scanner_id__in=allowed_ids,status=ObservationStatus.SUCCEEDED,created_at__gte=date_from,created_at__lte=window_end,@@ -2664,18 +2708,20 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, vi| Q(scanner_result__model_output__notability_reason__icontains=term)| Q(scanner_snapshot__name__icontains=term))- # Row-gate on each row's snapshot experiment before ranking, so a restricted row can't take a slot.- candidates = accessible_observations(self.user_access_control, self.team_id, candidates)viewer_id = cast(User, request.user).id# Before ranking, so a row the viewer marked not useful neither takes a slot nor shifts the window baselines.# Views and dismissals after `as_of` wait for the next load, so later pages of this one do not shift.- candidates = candidates.exclude(+ walkable = candidates.exclude(Exists(ReplayObservationFeedDismissal.objects.filter(observation_id=OuterRef("id"), user_id=viewer_id, created_at__lte=as_of)))+ # Row-gate on each row's snapshot experiment before ranking, so a restricted row can't take a feed slot.+ candidates = accessible_observations(+ self.user_access_control, self.team_id, walkable.filter(scanner_id__in=allowed_ids)+ )viewed = Exists(ReplayObservationView.objects.filter(observation_id=OuterRef("id"), user_id=viewer_id, created_at__lte=as_of@@ -2689,23 +2735,11 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, vivariant = watch_feed_ranker_variant(self.team_id, self.team.uuid)mode = ranker_mode(variant)ranker = mode if mode in ("jev", "embedding") else "weighted-score"- # The embedding ranker reads only the stored rank, so it skips loading every row's scan result.- row_fields = ["id", "scanner_id", "session_id", "created_at", "watch_rank", "feed_viewed"]- if ranker != "embedding":- row_fields.append("scanner_result")- candidate_rows = list(- candidates.annotate(- feed_viewed=viewed,- scanner_recency_rank=Window(- RowNumber(),- partition_by=F("scanner_id"),- order_by=[F("created_at").desc(), F("id").desc()],- ),- )- .filter(scanner_recency_rank__lte=WATCH_FEED_PER_SCANNER_CAP)- .values(*row_fields)- .order_by("-created_at", "-id")[:WATCH_FEED_CANDIDATE_CAP]+ candidate_rows = select_watch_feed_candidates(+ walkable, allowed_ids, viewed, self.user_access_control, self.team_id)+ if ranker != "embedding":+ attach_scanner_results(self.team_id, candidate_rows)page_end = params["offset"] + params["limit"]if ranker == "embedding":impressions = feed_impression_counts(diff --git a/products/replay_vision/backend/tests/test_api.py b/products/replay_vision/backend/tests/test_api.pyindex a6f717902816..02597dda286e 100644--- a/products/replay_vision/backend/tests/test_api.py+++ b/products/replay_vision/backend/tests/test_api.py@@ -5718,16 +5718,24 @@ class TestWatchFeedAPI(_VisionAPITestCase):experiment = create_experiment(self.team, "restricted-flag")scanner = self._create_scanner(name="retargeted", experiment_targeting={"experiment_id": experiment.id})self._succeeded_observation(scanner, "restricted-sess", 1, self._monitor_result("yes"))+ self._succeeded_observation(scanner, "restricted-sess-2", 2, self._monitor_result("yes"))scanner.experiment_targeting = Nonescanner.save()- self._succeeded_observation(scanner, "open-sess", 2, self._monitor_result("no"))+ self._succeeded_observation(scanner, "open-sess", 3, self._monitor_result("no"))+ self._succeeded_observation(scanner, "open-sess-2", 4, self._monitor_result("no"))- with patch(- "products.access_control.backend.facade.user_access_control.UserAccessControl.filter_queryset_by_access_level",- side_effect=lambda qs, **_: qs.exclude(pk=experiment.pk) if qs.model is Experiment else qs,+ # The newest rows fill the first candidate page and are all restricted, so the feed must read on.+ with (+ patch(+ "products.access_control.backend.facade.user_access_control.UserAccessControl.filter_queryset_by_access_level",+ side_effect=lambda qs, **_: qs.exclude(pk=experiment.pk) if qs.model is Experiment else qs,+ ),+ patch("products.replay_vision.backend.api.scanners.WATCH_FEED_CANDIDATE_CAP", 2),):resp = self.client.get(self.feed_url)- self.assertEqual([item["observation"]["session_id"] for item in resp.json()["results"]], ["open-sess"])+ self.assertEqual(+ sorted(item["observation"]["session_id"] for item in resp.json()["results"]), ["open-sess", "open-sess-2"]+ )def test_not_useful_hides_a_row_for_that_user_only_until_undone(self) -> None:scanner = self._create_scanner(name="m")