: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(replay-vision): pick embedding feed rows over the whole window (#123233)
Tue Haulund09dbfe9e960c
diff --git a/products/replay_vision/backend/api/scanners.py b/products/replay_vision/backend/api/scanners.pyindex 9d2bcd6e047a..c67f5d107f1c 100644--- a/products/replay_vision/backend/api/scanners.py+++ b/products/replay_vision/backend/api/scanners.py@@ -1,8 +1,10 @@import json+import hashlibfrom datetime import UTC, datetime, timedeltafrom typing import Any, NoReturn, castfrom uuid import UUID+from django.core.cache import cachefrom django.db import IntegrityError, models, transactionfrom django.db.models import (CharField,@@ -188,9 +190,13 @@ from products.replay_vision.backend.tag_suggestions import SuggestionError, suggfrom products.replay_vision.backend.temporal.constants import VISION_SIGNALS_SOURCE_PRODUCT, VISION_SIGNALS_SOURCE_TYPEfrom products.replay_vision.backend.temporal.metrics import record_estimate_outcome, record_scanner_limit_reachedfrom products.replay_vision.backend.temporal.read_meter_types import current_sweep_throttle_factor-from products.replay_vision.backend.watch_feed import rank_watch_feed_candidates+from products.replay_vision.backend.watch_feed import WATCH_FEED_MIN_ITEMS, rank_watch_feed_candidatesfrom products.replay_vision.backend.watch_feed_impressions import feed_impression_counts, record_feed_impressions-from products.replay_vision.backend.watch_rank import rank_watch_feed_by_embedding+from products.replay_vision.backend.watch_rank import (+ rank_watch_feed_by_embedding,+ select_embedding_filler,+ select_embedding_picks,+)from products.signals.backend.facade.api import get_outcomes_for_signal_source_slice# Date is set by the schedule at trigger time, not by the user — strip on save.@@ -1645,6 +1651,8 @@ WATCH_FEED_PER_SCANNER_CAP = 100# each id__in list bounded while the walk stops as soon as the page is covered.WATCH_FEED_BYPASS_CHUNK = 200WATCH_FEED_IMPRESSIONS_MAX_IDS = 100+# Long enough to cover a reader scrolling one load; a new load gets a new `as_of` and a fresh ranking.+WATCH_FEED_PICKS_CACHE_TTL_S = 15 * 60def select_watch_feed_candidates(@@ -1923,6 +1931,14 @@ class WatchFeedReasonSerializer(serializers.Serializer):"`embedding_pick`. The feed shows one session per group."),)+ affected_people = serializers.IntegerField(+ required=False,+ allow_null=True,+ help_text=(+ "Distinct people across the session's duplicate group in the window, for `embedding_pick`. A finding "+ "that reached more people ranks higher."+ ),+ )class WatchFeedItemSerializer(serializers.Serializer):@@ -2735,10 +2751,42 @@ 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"- candidate_rows = select_watch_feed_candidates(- walkable, allowed_ids, viewed, self.user_access_control, self.team_id- )- if ranker != "embedding":+ if ranker == "embedding":+ # Every page of one load ranks the same window as of the same `as_of`, so later pages reuse the first's picks.+ picks_key = (+ "replay_vision:watch_picks:"+ + hashlib.sha256(+ json.dumps(+ [+ self.team_id,+ viewer_id,+ as_of.isoformat(),+ str(date_from),+ str(window_end),+ sorted(map(str, allowed_ids)),+ params.get("search"),+ params.get("scanner_type"),+ ]+ ).encode()+ ).hexdigest()+ )+ candidate_rows = cache.get(picks_key)+ if candidate_rows is None:+ # The embedding ranker reads only a small stored rank, so it picks over the whole window in SQL.+ candidate_rows = select_embedding_picks(+ walkable, allowed_ids, viewed, self.user_access_control, self.team_id, WATCH_FEED_CANDIDATE_CAP+ )+ # Picks in one duplicate group collapse into one card, so the floor counts groups.+ pick_roots = {row["watch_group_root"] for row in candidate_rows}+ if len(pick_roots) < WATCH_FEED_MIN_ITEMS:+ candidate_rows += select_embedding_filler(+ walkable, allowed_ids, viewed, self.user_access_control, self.team_id, pick_roots+ )+ cache.set(picks_key, candidate_rows, WATCH_FEED_PICKS_CACHE_TTL_S)+ else:+ candidate_rows = select_watch_feed_candidates(+ walkable, allowed_ids, viewed, self.user_access_control, self.team_id+ )attach_scanner_results(self.team_id, candidate_rows)page_end = params["offset"] + params["limit"]if ranker == "embedding":@@ -2794,9 +2842,13 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, virows = {row.id: rowfor row in hydrate_for_serialization(- # team_id is redundant with the already-scoped candidate query these ids came from,- # but keeps the re-fetch fail-closed on its own.- ReplayObservation.objects.filter(team_id=self.team_id, id__in=list(reasons_by_id)),+ # Re-checks experiment access on the page's rows, because a later page can be served from picks+ # cached before the viewer lost access. team_id keeps the re-fetch fail-closed on its own.+ accessible_observations(+ self.user_access_control,+ self.team_id,+ ReplayObservation.objects.filter(team_id=self.team_id, id__in=list(reasons_by_id)),+ ),viewer_id=request.user.id,)}diff --git a/products/replay_vision/backend/migrations/0128_replayobservation_rlo_scanner_ranked_idx.py b/products/replay_vision/backend/migrations/0128_replayobservation_rlo_scanner_ranked_idx.pynew file mode 100644index 000000000000..02495ff6a5f7--- /dev/null+++ b/products/replay_vision/backend/migrations/0128_replayobservation_rlo_scanner_ranked_idx.py@@ -0,0 +1,23 @@+from django.db import migrations, models++from posthog.migration_helpers import SafeAddIndexConcurrently+++class Migration(migrations.Migration):+ atomic = False++ dependencies = [+ ("replay_vision", "0127_replayobservationfeedimpression"),+ ]++ operations = [+ SafeAddIndexConcurrently(+ model_name="replayobservation",+ index=models.Index(+ condition=models.Q(("status", "succeeded"), ("watch_rank__isnull", False)),+ fields=["scanner", "created_at"],+ include=["id", "team", "completed_at", "watch_rank", "distinct_id", "session_id"],+ name="rlo_scanner_ranked_idx",+ ),+ ),+ ]diff --git a/products/replay_vision/backend/migrations/max_migration.txt b/products/replay_vision/backend/migrations/max_migration.txtindex 70c0a42b0982..901ba83cf990 100644--- a/products/replay_vision/backend/migrations/max_migration.txt+++ b/products/replay_vision/backend/migrations/max_migration.txt@@ -1 +1 @@-0127_replayobservationfeedimpression+0128_replayobservation_rlo_scanner_ranked_idxdiff --git a/products/replay_vision/backend/models/replay_observation.py b/products/replay_vision/backend/models/replay_observation.pyindex 5601a4757a11..274e44e9c120 100644--- a/products/replay_vision/backend/models/replay_observation.py+++ b/products/replay_vision/backend/models/replay_observation.py@@ -175,6 +175,13 @@ class ReplayObservation(UUIDModel):name="rlo_team_in_flight_idx",condition=models.Q(status__in=("pending", "running")),),+ # Serves the embedding feed's whole-window pick query, which reads only these columns of ranked rows.+ models.Index(+ fields=["scanner", "created_at"],+ include=["id", "team", "completed_at", "watch_rank", "distinct_id", "session_id"],+ name="rlo_scanner_ranked_idx",+ condition=models.Q(status="succeeded", watch_rank__isnull=False),+ ),# Serves the per-backfill observation filter and progress counts; partial since live rows never match.models.Index(fields=["backfill"],diff --git a/products/replay_vision/backend/tests/test_api.py b/products/replay_vision/backend/tests/test_api.pyindex 02597dda286e..eb97417985dc 100644--- a/products/replay_vision/backend/tests/test_api.py+++ b/products/replay_vision/backend/tests/test_api.py@@ -5174,7 +5174,8 @@ class TestWatchFeedAPI(_VisionAPITestCase):self.assertEqual(resp.json()["ranker"], "embedding")self.assertEqual(items[0]["observation"]["session_id"], "jev-low")self.assertEqual(- items[0]["reason"], {"kind": "embedding_pick", "watch_category": "failure", "similar_count": 0}+ items[0]["reason"],+ {"kind": "embedding_pick", "watch_category": "failure", "similar_count": 0, "affected_people": 1},)def test_pages_do_not_shift_when_the_reader_opens_a_card_mid_scroll(self) -> None:@@ -5198,6 +5199,91 @@ class TestWatchFeedAPI(_VisionAPITestCase):assert sessions == [f"hit-{i}" for i in range(6)]assert second["has_more"] is False+ def test_embedding_picks_reach_past_the_recency_slice_and_count_hidden_duplicates(self) -> None:+ scanner = self._create_scanner(name="m")++ def ranked(session_id: str, minutes_ago: int, category: str, contrast: float, duplicate_of: str | None = None):+ obs = self._succeeded_observation(scanner, session_id, minutes_ago, self._monitor_result("no"))+ ReplayObservation.objects.filter(id=obs.id).update(+ watch_rank={+ "category": category,+ "contrast": contrast,+ "duplicate_of": duplicate_of,+ "anchor_version": ANCHOR_VERSION,+ }+ )+ return obs++ oldest = ranked("oldest-failure", 500, "failure", 0.9)+ # A copy of the pick that scores below the cut still makes the group bigger.+ ranked("copy", 400, "failure", -0.5, duplicate_of=str(oldest.id))+ for index in range(10):+ ranked(f"routine-{index}", index, "routine", -0.1)++ # The candidate slice holds two rows per scanner, so the oldest row only surfaces from the whole window.+ with (+ patch("products.replay_vision.backend.api.scanners.watch_feed_ranker_variant", return_value="embedding"),+ patch("products.replay_vision.backend.api.scanners.WATCH_FEED_PER_SCANNER_CAP", 2),+ ):+ items = self.client.get(self.feed_url).json()["results"]+ picks = [item for item in items if item["reason"]["kind"] == "embedding_pick"]+ self.assertEqual([item["observation"]["session_id"] for item in picks], ["oldest-failure"])+ self.assertEqual(picks[0]["reason"]["similar_count"], 1)+ self.assertEqual(picks[0]["reason"]["affected_people"], 2)++ def test_a_thin_embedding_feed_pads_by_group_and_never_pads_with_a_copy_of_a_pick(self) -> None:+ scanner = self._create_scanner(name="m")++ def ranked(session_id: str, minutes_ago: int, category: str, contrast: float, duplicate_of: str | None = None):+ obs = self._succeeded_observation(scanner, session_id, minutes_ago, self._monitor_result("no"))+ ReplayObservation.objects.filter(id=obs.id).update(+ watch_rank={+ "category": category,+ "contrast": contrast,+ "duplicate_of": duplicate_of,+ "anchor_version": ANCHOR_VERSION,+ }+ )+ return obs++ root = ranked("pick", 500, "failure", 0.9)+ # Two copies of the one finding: one above the cut, one newest of all and below it.+ ranked("copy-above", 400, "failure", 0.8, duplicate_of=str(root.id))+ ranked("copy-newest", 0, "failure", -0.9, duplicate_of=str(root.id))+ for index in range(10):+ ranked(f"routine-{index}", 10 + index, "routine", -0.1)++ with patch("products.replay_vision.backend.api.scanners.watch_feed_ranker_variant", return_value="embedding"):+ items = self.client.get(self.feed_url).json()["results"]+ sessions = [item["observation"]["session_id"] for item in items]+ self.assertEqual(sessions[0], "pick")+ self.assertEqual(len(sessions), 3)+ self.assertNotIn("copy-above", sessions)+ self.assertNotIn("copy-newest", sessions)++ def test_the_pick_cap_keeps_every_scanners_best_before_a_busy_scanners_next(self) -> None:+ busy = self._create_scanner(name="busy")+ quiet = self._create_scanner(name="quiet")+ for scanner, prefix, top in ((busy, "busy", 0.9), (quiet, "quiet", 0.3)):+ for index in range(12):+ obs = self._succeeded_observation(scanner, f"{prefix}-{index}", index, self._monitor_result("yes"))+ ReplayObservation.objects.filter(id=obs.id).update(+ watch_rank={+ "category": "failure",+ "contrast": top - index / 100,+ "duplicate_of": None,+ "anchor_version": ANCHOR_VERSION,+ }+ )++ with (+ patch("products.replay_vision.backend.api.scanners.watch_feed_ranker_variant", return_value="embedding"),+ patch("products.replay_vision.backend.api.scanners.WATCH_FEED_CANDIDATE_CAP", 2),+ ):+ items = self.client.get(self.feed_url).json()["results"]+ picked = {item["observation"]["session_id"] for item in items if item["reason"]["kind"] == "embedding_pick"}+ self.assertEqual(picked, {"busy-0", "quiet-0"})+def test_impressions_lower_an_embedding_pick_only_after_the_load_that_showed_it(self) -> None:scanner = self._create_scanner(name="m")rows = {}@@ -5737,6 +5823,34 @@ class TestWatchFeedAPI(_VisionAPITestCase):sorted(item["observation"]["session_id"] for item in resp.json()["results"]), ["open-sess", "open-sess-2"])+ def test_a_later_page_drops_rows_the_viewer_lost_access_to_after_the_first(self) -> None:+ experiment = create_experiment(self.team, "revoked-flag")+ scanner = self._create_scanner(name="retargeted", experiment_targeting={"experiment_id": experiment.id})+ for index in range(12):+ obs = self._succeeded_observation(scanner, f"sess-{index}", index, self._monitor_result("yes"))+ ReplayObservation.objects.filter(id=obs.id).update(+ watch_rank={+ "category": "failure",+ "contrast": index / 10,+ "duplicate_of": None,+ "anchor_version": ANCHOR_VERSION,+ }+ )+ scanner.experiment_targeting = None+ scanner.save()+ ranker = "products.replay_vision.backend.api.scanners.watch_feed_ranker_variant"++ with patch(ranker, return_value="embedding"):+ first = self.client.get(self.feed_url, {"limit": 1}).json()+ self.assertEqual(len(first["results"]), 1)+ # The second page reads picks cached by the first, so access has to be checked again on the page itself.+ 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,+ ):+ second = self.client.get(self.feed_url, {"limit": 5, "offset": 1, "as_of": first["as_of"]}).json()+ self.assertEqual(second["results"], [])+def test_not_useful_hides_a_row_for_that_user_only_until_undone(self) -> None:scanner = self._create_scanner(name="m")dismissed = self._succeeded_observation(scanner, "dismissed-sess", 1, self._monitor_result("yes"))diff --git a/products/replay_vision/backend/tests/test_watch_rank.py b/products/replay_vision/backend/tests/test_watch_rank.pyindex d1381e8c23ba..f86e76633462 100644--- a/products/replay_vision/backend/tests/test_watch_rank.py+++ b/products/replay_vision/backend/tests/test_watch_rank.py@@ -93,6 +93,17 @@ class TestDuplicateRoot(SimpleTestCase):class TestRankWatchFeedByEmbedding(SimpleTestCase):+ def test_a_finding_that_reached_more_people_outranks_a_stronger_one_off(self) -> None:+ rows = [_row(f"a-{index}", 0.01 * index, minutes_ago=index + 1) for index in range(10)]+ rows += [_row("one-off", 0.9), _row("widespread", 0.5)]+ # Window percentiles and group reach, as the whole-window pick query supplies them.+ for row in rows:+ row["watch_percentile"] = {"one-off": 0.99, "widespread": 0.85}.get(str(row["id"]))+ row["watch_group_people"] = 6 if row["id"] == "widespread" else 1+ ranked = rank_watch_feed_by_embedding(rows)+ assert [entry.observation_id for entry in ranked][:2] == ["widespread", "one-off"]+ assert ranked[0].reason["affected_people"] == 6+def test_picks_the_scanner_top_issues_once_per_duplicate_group(self) -> None:rows = [# scanner-a has enough ranked rows for a percentile; its top two are one duplicate group.@@ -112,7 +123,12 @@ class TestRankWatchFeedByEmbedding(SimpleTestCase):ranked = rank_watch_feed_by_embedding(rows)assert ranked[0].observation_id == "dup-copy"- assert ranked[0].reason == {"kind": "embedding_pick", "watch_category": "failure", "similar_count": 3}+ assert ranked[0].reason == {+ "kind": "embedding_pick",+ "watch_category": "failure",+ "similar_count": 3,+ "affected_people": 1,+ }picks = [entry.observation_id for entry in ranked if entry.reason["kind"] == "embedding_pick"]assert picks == ["dup-copy"]# Collapsed copies, even those below the cutoff, must not come back as filler.diff --git a/products/replay_vision/backend/watch_rank.py b/products/replay_vision/backend/watch_rank.pyindex 1fcb76376779..ac2f5ca0611d 100644--- a/products/replay_vision/backend/watch_rank.py+++ b/products/replay_vision/backend/watch_rank.py@@ -7,12 +7,31 @@ The feed turns scores into a percentile within each scanner when it loads, so aerrors all the time does not fill the feed."""+import mathimport asynciofrom dataclasses import asdictfrom datetime import datetime, timedeltafrom typing import Any, Literal+from uuid import UUIDfrom django.core.cache import cache+from django.db.models import (+ Case,+ CharField,+ Count,+ Exists,+ F,+ FloatField,+ Func,+ IntegerField,+ Q,+ QuerySet,+ Value,+ When,+ Window,+)+from django.db.models.fields.json import KeyTextTransform, KeyTransform+from django.db.models.functions import Cast, Coalesce, NullIf, RowNumberfrom django.utils import timezoneimport numpy as np@@ -25,6 +44,7 @@ from posthog.clickhouse.workload import Workloadfrom posthog.dataclasses import frozenfrom posthog.models.team import Team+from products.access_control.backend.facade.user_access_control import UserAccessControlfrom products.replay_vision.backend.embeddings import (OBSERVATION_EMBEDDING_MODEL,OBSERVATION_EMBEDDINGS_TABLE,@@ -32,7 +52,8 @@ from products.replay_vision.backend.embeddings import (observation_scope_params,)from products.replay_vision.backend.models.replay_observation import ReplayObservation-from products.replay_vision.backend.watch_feed import WatchFeedEntry, finish_feed+from products.replay_vision.backend.scanner_access import accessible_observations+from products.replay_vision.backend.watch_feed import WATCH_FEED_MIN_ITEMS, WatchFeedEntry, finish_feedWatchCategory = Literal["failure", "struggle", "revenue", "routine"]@@ -94,6 +115,12 @@ WATCH_PICK_SEEN_PENALTY = 0.3# Each earlier impression keeps this share of a pick's score, so a pick shown three times without a# click sinks below a fresh one.WATCH_PICK_IMPRESSION_DECAY = 0.8+# A pick's score grows by this factor times the log of the people its group reached, up to the cap, so a problem+# that hit six people outranks an equally strong one-off without burying everything else.+WATCH_PICK_PEOPLE_BOOST = 0.25+WATCH_PICK_PEOPLE_BOOST_CAP = 2.0+# Copies of one finding the pick query keeps, so a viewed or restricted top copy does not hide the group.+WATCH_PICK_GROUP_CANDIDATES = 3_ANCHOR_CACHE_TTL_S = 7 * 24 * 3600@@ -317,6 +344,158 @@ def _group_root(observation_id: str, parents: dict[str, str]) -> str:return observation_id+def people_boost(people: int) -> float:+ """How much a pick's score grows with the number of people its duplicate group reached."""+ return min(WATCH_PICK_PEOPLE_BOOST_CAP, 1 + WATCH_PICK_PEOPLE_BOOST * math.log(max(people, 1)))+++def _is_uuid(value: str) -> bool:+ try:+ UUID(value)+ except ValueError:+ return False+ return True+++def _embedding_group_root() -> Coalesce:+ """A ranked row's duplicate group: the observation it duplicates, or itself."""+ return Coalesce(KeyTextTransform("duplicate_of", "watch_rank"), Cast("id", CharField()), output_field=CharField())+++def select_embedding_picks(+ walkable: QuerySet[ReplayObservation],+ scanner_ids: list[Any],+ viewed: Exists,+ access: UserAccessControl,+ team_id: int,+ cap: int,+) -> list[dict[str, Any]]:+ """The window's pick rows for the embedding ranker, with each row's percentile and the reach of its duplicate group.++ The percentile is taken over every ranked row a scanner has in the window, not over a recency slice, so a+ longer window reaches further back instead of re-ranking the same newest rows.+ """+ contrast = Cast(KeyTextTransform("contrast", "watch_rank"), FloatField())+ category = KeyTextTransform("category", "watch_rank")+ group_root = _embedding_group_root()+ current_ranks = (+ walkable.filter(+ # The explicit null check lets Postgres match the partial `rlo_scanner_ranked_idx`.+ scanner_id__in=scanner_ids,+ watch_rank__isnull=False,+ watch_rank__anchor_version=ANCHOR_VERSION,+ )+ .alias(+ watch_category=category,+ contrast_type=Func(+ KeyTransform("contrast", "watch_rank"), function="jsonb_typeof", output_field=CharField()+ ),+ )+ .filter(contrast_type="number")+ )+ is_issue = Case(+ When(watch_category__in=ISSUE_CATEGORIES, then=Value(1)), default=Value(0), output_field=IntegerField()+ )+ ranked = (+ current_ranks.annotate(+ scanner_rows=Window(Count("id"), partition_by=[F("scanner_id")]),+ scanner_position=Window(+ RowNumber(), partition_by=[F("scanner_id")], order_by=[contrast.asc(), F("id").asc()]+ ),+ # Each scanner's best row ranks 1, so the cap takes every scanner's best before any second-best.+ scanner_rank=Window(+ RowNumber(), partition_by=[F("scanner_id")], order_by=[contrast.desc(), F("id").desc()]+ ),+ # A few rows per duplicate group reach the cap, enough for the ranker to pick a readable, unseen one.+ group_position=Window(+ RowNumber(), partition_by=[group_root], order_by=[is_issue.desc(), contrast.desc(), F("id").desc()]+ ),+ )+ .annotate(+ watch_percentile=(F("scanner_position") - Value(0.5)) / Cast(F("scanner_rows"), FloatField()),+ # Reads a window value, so Django filters on it after the windows run rather than before.+ issue_scanner_rows=Case(+ When(watch_category__in=ISSUE_CATEGORIES, then=F("scanner_rows")),+ default=Value(0),+ output_field=IntegerField(),+ ),+ watch_group_root=group_root,+ )+ .filter(+ issue_scanner_rows__gte=WATCH_PICK_MIN_SCANNER_ROWS,+ watch_percentile__gte=WATCH_PICK_MIN_PERCENTILE,+ group_position__lte=WATCH_PICK_GROUP_CANDIDATES,+ )+ .order_by("scanner_rank", "-watch_percentile", "-created_at")+ .values("id", "watch_percentile", "watch_group_root")[:cap]+ )+ window_values = {row["id"]: row for row in ranked}+ if not window_values:+ return []+ # Counted over readable rows only, so a group never reveals how many restricted sessions it holds. Duplicates+ # below the cutoff still count, and a person who hit the problem twice counts once.+ person = Coalesce(NullIf("distinct_id", Value("")), "session_id", output_field=CharField())+ roots = {row["watch_group_root"] for row in window_values.values()}+ group_reach = {+ row["watch_group_root"]: row+ for row in accessible_observations(+ access,+ team_id,+ # Matches the picks' groups on plain columns first, so only their members reach the snapshot gate.+ current_ranks.filter(+ Q(id__in=[root for root in roots if _is_uuid(root)]) | Q(watch_rank__duplicate_of__in=list(roots)),+ watch_category__in=ISSUE_CATEGORIES,+ ),+ )+ .annotate(watch_group_root=group_root)+ .values("watch_group_root")+ .annotate(sessions=Count("id"), people=Count(person, distinct=True))+ }+ # The row-gate reads each row's snapshot JSON, so it runs on the picks only.+ rows = list(+ accessible_observations(+ access, team_id, ReplayObservation.objects.filter(team_id=team_id, id__in=list(window_values))+ )+ .annotate(feed_viewed=viewed)+ .values("id", "scanner_id", "session_id", "created_at", "watch_rank", "feed_viewed")+ )+ for row in rows:+ window = window_values[row["id"]]+ reach = group_reach.get(window["watch_group_root"], {})+ row["watch_percentile"] = window["watch_percentile"]+ row["watch_group_root"] = window["watch_group_root"]+ row["watch_group_size"] = reach.get("sessions", 1)+ row["watch_group_people"] = reach.get("people", 1)+ return rows+++def select_embedding_filler(+ walkable: QuerySet[ReplayObservation],+ scanner_ids: list[Any],+ viewed: Exists,+ access: UserAccessControl,+ team_id: int,+ pick_roots: set[str],+) -> list[dict[str, Any]]:+ """The newest readable rows outside the picks' duplicate groups, unwatched first, enough to bring a thin feed up to its floor."""+ base = (+ accessible_observations(access, team_id, walkable.filter(scanner_id__in=scanner_ids))+ .annotate(watch_group_root=_embedding_group_root())+ .exclude(watch_group_root__in=pick_roots)+ .annotate(feed_viewed=viewed)+ )+ fields = ("id", "scanner_id", "session_id", "created_at", "watch_rank", "feed_viewed")+ # Two short newest-first walks, because ordering by the viewed flag would sort the whole window.+ rows = list(base.filter(feed_viewed=False).order_by("-created_at", "-id").values(*fields)[:WATCH_FEED_MIN_ITEMS])+ if len(rows) < WATCH_FEED_MIN_ITEMS:+ rows += list(+ base.filter(feed_viewed=True)+ .order_by("-created_at", "-id")+ .values(*fields)[: WATCH_FEED_MIN_ITEMS - len(rows)]+ )+ return rows++def rank_watch_feed_by_embedding(rows: list[dict[str, Any]]) -> list[WatchFeedEntry]:"""Rank candidate rows (`id`, `scanner_id`, `session_id`, `created_at`, `watch_rank`, `feed_viewed`,and optionally `feed_impressions`) on the embedding rank the watch rank consumer wrote.@@ -327,7 +506,12 @@ def rank_watch_feed_by_embedding(rows: list[dict[str, Any]]) -> list[WatchFeedEnof the picks, and one session's picks spaced apart. Every other row is filler, trimmed to the samefloor as the other rankers."""- percentiles = _scanner_percentiles(rows)+ # Rows from `select_embedding_picks` carry their whole-window percentile; a plain candidate slice does not.+ percentiles = (+ {str(row["id"]): row["watch_percentile"] for row in rows if row.get("watch_percentile") is not None}+ if any("watch_percentile" in row for row in rows)+ else _scanner_percentiles(rows)+ )parents = {str(row["id"]): str(rank["duplicate_of"])for row in rows@@ -350,6 +534,7 @@ def rank_watch_feed_by_embedding(rows: list[dict[str, Any]]) -> list[WatchFeedEnscore = (percentile* CATEGORY_WEIGHT[rank["category"]]+ * people_boost(row.get("watch_group_people") or 1)* WATCH_PICK_IMPRESSION_DECAY ** row.get("feed_impressions", 0))if row.get("feed_viewed"):@@ -364,7 +549,8 @@ def rank_watch_feed_by_embedding(rows: list[dict[str, Any]]) -> list[WatchFeedEnreason = {"kind": "embedding_pick","watch_category": rank["category"],- "similar_count": len(group_members[group]) - 1,+ "similar_count": max(len(group_members[group]), row.get("watch_group_size") or 0) - 1,+ "affected_people": row.get("watch_group_people") or 1,}picks.append((diff --git a/products/replay_vision/frontend/generated/api.schemas.ts b/products/replay_vision/frontend/generated/api.schemas.tsindex d8a1870e7b6f..8070ddd4e20b 100644--- a/products/replay_vision/frontend/generated/api.schemas.ts+++ b/products/replay_vision/frontend/generated/api.schemas.ts@@ -3155,6 +3155,11 @@ export interface WatchFeedReasonApi {* @nullable*/similar_count?: number | null+ /**+ * Distinct people across the session's duplicate group in the window, for `embedding_pick`. A finding that reached more people ranks higher.+ * @nullable+ */+ affected_people?: number | null}/**diff --git a/products/replay_vision/frontend/replay_scanners/ReplayScannersScene.stories.tsx b/products/replay_vision/frontend/replay_scanners/ReplayScannersScene.stories.tsxindex a5a0687b2894..696f820856e0 100644--- a/products/replay_vision/frontend/replay_scanners/ReplayScannersScene.stories.tsx+++ b/products/replay_vision/frontend/replay_scanners/ReplayScannersScene.stories.tsx@@ -1266,7 +1266,12 @@ export const HomeWatchFeedEmbeddingArm: StoryObj = {},viewed: false,}),- reason: { kind: 'embedding_pick', watch_category: 'failure', similar_count: 3 },+ reason: {+ kind: 'embedding_pick',+ watch_category: 'failure',+ similar_count: 3,+ affected_people: 3,+ },},{observation: observation({diff --git a/products/replay_vision/frontend/replay_scanners/components/WatchFeedCard.test.ts b/products/replay_vision/frontend/replay_scanners/components/WatchFeedCard.test.tsindex ea0f5ab50ddf..8689fedee27c 100644--- a/products/replay_vision/frontend/replay_scanners/components/WatchFeedCard.test.ts+++ b/products/replay_vision/frontend/replay_scanners/components/WatchFeedCard.test.ts@@ -135,6 +135,16 @@ describe('WatchFeedCard helpers', () => {reason: { kind: 'embedding_pick', watch_category: 'struggle', similar_count: 1 } as WatchFeedReasonApi,expected: 'The user struggled in this session. 1 other session from this scanner looks similar.',},+ {+ name: 'counts the people behind an embedding pick when it reached more than one',+ reason: {+ kind: 'embedding_pick',+ watch_category: 'failure',+ similar_count: 5,+ affected_people: 4,+ } as WatchFeedReasonApi,+ expected: 'Something failed in this session. 4 people had a similar session.',+ },{name: 'renders a sentence for a reason kind this frontend does not know',reason: { kind: 'some_future_kind' } as unknown as WatchFeedReasonApi,diff --git a/products/replay_vision/frontend/replay_scanners/components/WatchFeedCard.tsx b/products/replay_vision/frontend/replay_scanners/components/WatchFeedCard.tsxindex 055c5df5a46a..9c00923af0ec 100644--- a/products/replay_vision/frontend/replay_scanners/components/WatchFeedCard.tsx+++ b/products/replay_vision/frontend/replay_scanners/components/WatchFeedCard.tsx@@ -145,6 +145,10 @@ export function watchReasonCopy(reason: WatchFeedReasonApi): string {const category = reason.watch_category? WATCH_CATEGORY_COPY[reason.watch_category]: 'This session stands out for this scanner.'+ const people = reason.affected_people ?? 1+ if (people > 1) {+ return `${category} ${people} people had a similar session.`+ }const similar = reason.similar_count ?? 0if (similar === 0) {return categorydiff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 6fdab46c5a1a..179bc94d6c32 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -120529,6 +120529,11 @@ export namespace Schemas {* @nullable*/similar_count?: number | null;+ /**+ * Distinct people across the session's duplicate group in the window, for `embedding_pick`. A finding that reached more people ranks higher.+ * @nullable+ */+ affected_people?: number | null;}/**