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(replay-vision): page what to watch and decay picks per impression (#122766)

Tue Haulund6c1775e2e332
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yaml
index 6afda3bb2514..7f5d4db587b2 100644
--- a/.semgrep/rules/security/idor-team-scoped-models.yaml
+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml
@@ -349,6 +349,7 @@ rules:
|QuickFilterContext
|ReplayObservation
|ReplayObservationFeedDismissal
+ |ReplayObservationFeedImpression
|ReplayObservationLabel
|ReplayObservationMedia
|ReplayObservationRequest
@@ -822,6 +823,7 @@ rules:
|QuickFilterContext
|ReplayObservation
|ReplayObservationFeedDismissal
+ |ReplayObservationFeedImpression
|ReplayObservationLabel
|ReplayObservationMedia
|ReplayObservationRequest
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 37eac1dcdb81..954ac674d12b 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -14593,21 +14593,21 @@ snapshots:
scenes-app-replay-vision--experiment-variants-waiting-for-observations--light:
hash: v1.k794b7964.a1555dab4c0006028e8c240451c56c6aed5e31c8a8e68c893e3d6a07b562bba6.VYeYPsjZ_p0Rt4cbZzF7lbxjxurMjLXff9kJRsy49gU
scenes-app-replay-vision--home-watch-feed--dark:
- hash: v1.k794b7964.224bbfbc43123ce0e2cd595760ce671f478cacf8463516916394a6c0ddd0ab43.qGDd_Kmn36tQeubJpNtts5MBuT-zMB7uBsPKcz5zGQM
+ hash: v1.k794b7964.031ff3d767c9b2d0900e559fbf4a9c78f5066fbcecde5b309bd9b22f2f2db342.vRyLsopnMHAdtwaz8dJ1hkQKMh3wMRmXY3nRRxWmByY
scenes-app-replay-vision--home-watch-feed--light:
- hash: v1.k794b7964.3568ea053d48ab0611a61368084debe8b4bb3b76a4942de108a7aeef8a2eb75b.KNuEmyD9A8JuHR4fGE_OuqEmTaDNzKpe6EGftWsZGV0
+ hash: v1.k794b7964.060f25ab51f3ce69e2e68af90055c134b0f2d7905d9678bda4c300d2e51726c3.gLCeY7ckAsB-jYPVfZrkZS2BadtD4-UOleMGllrOVQU
scenes-app-replay-vision--home-watch-feed-empty--dark:
hash: v1.k794b7964.508e7537ec9368ae5f9b9df50086f1e552ac7cd292496e8a32d7261b9416a808.vmCvpR4ES-v0zOQJpuaz0l0LJWvzgyDJHZomDXMvfpo
scenes-app-replay-vision--home-watch-feed-empty--light:
hash: v1.k794b7964.5b95650e20cbf2fc7515cc99bac499c83bad574858817d0232c095ccf9944b69.t_3o-A2yWVHUKwMsfsgENI4qFMrF4IWp4EQwKOAAnnM
scenes-app-replay-vision--home-watch-feed-jev-arm--dark:
- hash: v1.k794b7964.3d83103988320304961959dff03adba8b047428f6f763b0e151c8362269cbbb2.6ncWLzPYn6taVYoELanUHUbsgKMtEyMNOyLhDRI2gro
+ hash: v1.k794b7964.013fab882504a541802c347ed72ad1f4eaa8cbd966216c89de79fc3ebec352f3.0dfk-dxpMi-aygVKWhCBLGIEXE65qVHpCaggbaNC_Ds
scenes-app-replay-vision--home-watch-feed-jev-arm--light:
- hash: v1.k794b7964.2667e6c8758895020a00cad45b6638375eb598911a29749ad8e333c4538b8b7c.UV5baB_GBCPp8ymfqMCSmZk3-VZ7-lccTkWKa6Wdr1g
+ hash: v1.k794b7964.3af0fd2963dc4bbb700bc8305ada330486f9abcaee7945fb25d5e3bcbd4be92c.r9HN72bBYYbgsx_O6VyhI21zejjNCQ-kmfWcp67xbBY
scenes-app-replay-vision--home-watch-feed-only-newest--dark:
- hash: v1.k794b7964.d10deddecabd5571ca3c96c9036395fa1a2989065080fe504680973fd0edb7be.SN1mq28CvW7tl0HWqXDcaWzvJU_kTi7XHVe14rXqO_w
+ hash: v1.k794b7964.39af7e6060e2ae5664f6534a6d76af2de997e0f10d3a85e6ad18cd49666c727c.L_KdE8x_W_YZ4tlxxcNwf3qycSGhoP7lGmXW_yNTsHc
scenes-app-replay-vision--home-watch-feed-only-newest--light:
- hash: v1.k794b7964.7e6e9186317aa457e0fe4551e85aa42782192a88b0d20c0b4d3033854c60a520.C8yEc9dCsgi8C3NI4Wgl2hh4EiGQ1MbfD2rp0RFRTyA
+ hash: v1.k794b7964.7f0bce5ea238bcff38e8e8d2339a2dce62e6b135b9bf9fee311eeb92c57f795c.8rgy6-qnerQKdi7LWHqgY4FeRVzeBD6Jr-50w6ZygCI
scenes-app-replay-vision--monitor-observations--dark:
hash: v1.k794b7964.9ec7ef6c17074e9547647fe71aa2613100208fa0c7ba87a3d97897b4f183a5c8.-UKB-w06mWDRH2yNgzIBhn3Qhn2MYlFTNXjhMce1hh4
scenes-app-replay-vision--monitor-observations--light:
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index ebc96d5f90c3..3ffd1fae5b5c 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -4880,6 +4880,9 @@ importers:
react:
specifier: 18.3.1
version: 18.3.1
+ react-intersection-observer:
+ specifier: ^9.5.3
+ version: 9.5.3([email protected])
typescript:
specifier: 6.0.3
version: 6.0.3
diff --git a/posthog/rate_limit.py b/posthog/rate_limit.py
index cc97bdf1902f..1ff5b5647e2d 100644
--- a/posthog/rate_limit.py
+++ b/posthog/rate_limit.py
@@ -791,6 +791,13 @@ class ReplayVisionWatchFeedSustainedRateThrottle(_TeamBucketRateThrottle):
rate = "600/hour"
+# Impression reports arrive while a reader scrolls, so they get their own per-user bucket and never use up the
+# team's feed bucket.
+class ReplayVisionWatchFeedImpressionsRateThrottle(_UserBucketRateThrottle):
+ scope = "replay_vision_watch_feed_impressions"
+ rate = "120/minute"
+
+
# Each observation search makes a synchronous embedding request and a brute-force cosine scan over
# the team's embedding rows, and its primary caller is the session-authenticated Search tab, which
# the default Burst/Sustained throttles bypass. The burst bucket is per credential so one user
diff --git a/products/replay_vision/backend/api/scanners.py b/products/replay_vision/backend/api/scanners.py
index 713fc56577dd..3fb92bceb640 100644
--- a/products/replay_vision/backend/api/scanners.py
+++ b/products/replay_vision/backend/api/scanners.py
@@ -60,6 +60,7 @@ from posthog.rate_limit import (
ReplayVisionEstimateBurstRateThrottle,
ReplayVisionEstimateSustainedRateThrottle,
ReplayVisionWatchFeedBurstRateThrottle,
+ ReplayVisionWatchFeedImpressionsRateThrottle,
ReplayVisionWatchFeedSustainedRateThrottle,
)
@@ -188,6 +189,7 @@ from products.replay_vision.backend.temporal.constants import VISION_SIGNALS_SOU
from products.replay_vision.backend.temporal.metrics import record_estimate_outcome, record_scanner_limit_reached
from 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_impressions import feed_impression_counts, record_feed_impressions
from products.replay_vision.backend.watch_rank import rank_watch_feed_by_embedding
from products.signals.backend.facade.api import get_outcomes_for_signal_source_slice
@@ -1643,6 +1645,7 @@ WATCH_FEED_PER_SCANNER_CAP = 100
# Ids per query when the jev feed fetches cached watchable rows the recency slice cut off. Keeps
# each id__in list bounded while the walk stops as soon as the page is covered.
WATCH_FEED_BYPASS_CHUNK = 200
+WATCH_FEED_IMPRESSIONS_MAX_IDS = 100
class WatchFeedRanker(models.TextChoices):
@@ -1725,9 +1728,22 @@ class WatchFeedQuerySerializer(serializers.Serializer):
min_value=1,
max_value=WATCH_FEED_MAX_LIMIT,
help_text=(
- f"Ceiling on feed items to return, at most {WATCH_FEED_MAX_LIMIT}. The feed is bounded, not "
- "paginated, and routinely returns far fewer: a window is not padded to this number with "
- "clips that carry no finding."
+ f"Ceiling on feed items to return per page, at most {WATCH_FEED_MAX_LIMIT}. A page routinely "
+ "returns fewer: a window is not padded to this number with clips that carry no finding."
+ ),
+ )
+ offset = serializers.IntegerField(
+ required=False,
+ default=0,
+ min_value=0,
+ max_value=WATCH_FEED_CANDIDATE_CAP,
+ help_text="How many ranked items to skip. Send `as_of` with every page after the first.",
+ )
+ as_of = serializers.DateTimeField(
+ required=False,
+ help_text=(
+ "The time the first page loaded. Later pages rank as of that time: observations, views, "
+ "dismissals and impressions after it are ignored, so pages do not shift under a reader who scrolls."
),
)
@@ -1884,6 +1900,12 @@ class WatchFeedResponseSerializer(serializers.Serializer):
"so a quiet window answers with a handful of rows rather than a full page of newest clips."
),
)
+ has_more = serializers.BooleanField(
+ help_text="Whether a later page holds more ranked items. Request it with `offset` set past this page.",
+ )
+ as_of = serializers.DateTimeField(
+ help_text="The time this feed ranks as of. Send it back with later pages.",
+ )
ranker = serializers.ChoiceField(
choices=WatchFeedRanker.choices,
help_text=(
@@ -1904,6 +1926,23 @@ class WatchFeedResponseSerializer(serializers.Serializer):
)
+class WatchFeedImpressionsSerializer(serializers.Serializer):
+ """Body of POST /vision/scanners/watch_feed_impressions/."""
+
+ feed_load_id = serializers.UUIDField(
+ help_text="The client's id for the feed load that showed these cards. One load counts each card once.",
+ )
+ observation_ids = serializers.ListField(
+ child=serializers.UUIDField(),
+ min_length=1,
+ max_length=WATCH_FEED_IMPRESSIONS_MAX_IDS,
+ help_text=(
+ f"Observations the calling user saw in What to watch, at most {WATCH_FEED_IMPRESSIONS_MAX_IDS}. "
+ "Ids the user cannot read are ignored."
+ ),
+ )
+
+
class ScannerCreatorsResponseSerializer(serializers.Serializer):
"""Distinct creators across all scanners on the team — feeds the `Created by` filter dropdown."""
@@ -2563,16 +2602,18 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, vi
# Observations expose recording-derived output, so reading them requires session_recording read.
if not self.user_access_control.check_access_level_for_resource("session_recording", required_level="viewer"):
raise PermissionDenied("Reading replay observations requires session_recording read access.")
+ as_of = params.get("as_of") or timezone.now()
try:
- date_from = parse_date_bound(params["date_from"], self.team.timezone_info, end_of_range=False)
+ # Relative bounds resolve against `as_of`, so every page of one load reads the same window.
+ date_from = parse_date_bound(params["date_from"], self.team.timezone_info, end_of_range=False, now=as_of)
date_to = (
- parse_date_bound(params["date_to"], self.team.timezone_info, end_of_range=True)
+ parse_date_bound(params["date_to"], self.team.timezone_info, end_of_range=True, now=as_of)
if params.get("date_to")
else None
)
except ValueError as e:
raise ValidationError(str(e))
- window_end = date_to if date_to is not None else timezone.now()
+ window_end = min(date_to, as_of) if date_to is not None else as_of
if window_end - date_from > timedelta(days=WATCH_FEED_MAX_WINDOW_DAYS):
raise ValidationError(
f"The window between date_from and date_to may span at most {WATCH_FEED_MAX_WINDOW_DAYS} days."
@@ -2606,9 +2647,9 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, vi
scanner_id__in=allowed_ids,
status=ObservationStatus.SUCCEEDED,
created_at__gte=date_from,
+ created_at__lte=window_end,
+ completed_at__lte=as_of,
)
- if date_to is not None:
- candidates = candidates.filter(created_at__lte=date_to)
if params.get("scanner_type"):
candidates = candidates.filter(scanner_snapshot__scanner_type=params["scanner_type"])
if params.get("search"):
@@ -2627,10 +2668,31 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, vi
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(
- Exists(ReplayObservationFeedDismissal.objects.filter(observation_id=OuterRef("id"), user_id=viewer_id))
+ Exists(
+ ReplayObservationFeedDismissal.objects.filter(
+ observation_id=OuterRef("id"), user_id=viewer_id, created_at__lte=as_of
+ )
+ )
)
- viewed = Exists(ReplayObservationView.objects.filter(observation_id=OuterRef("id"), user_id=viewer_id))
+ viewed = Exists(
+ ReplayObservationView.objects.filter(
+ observation_id=OuterRef("id"), user_id=viewer_id, created_at__lte=as_of
+ )
+ )
+ # The flag selects one of two independent rankers; nothing is blended between them. Shadow
+ # teams rank on the weighted score too, because only the `jev` arm reads the probabilities
+ # the hourly sweep cached. Neither arm makes a model call here. The response names the
+ # ranker that ordered it, so the shadow arm reads as weighted-score to the client, and the
+ # flag variant separately, which the client reports as the experiment's exposure.
+ variant = 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,
@@ -2641,19 +2703,20 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, vi
),
)
.filter(scanner_recency_rank__lte=WATCH_FEED_PER_SCANNER_CAP)
- .values("id", "scanner_id", "session_id", "created_at", "scanner_result", "watch_rank", "feed_viewed")
+ .values(*row_fields)
.order_by("-created_at", "-id")[:WATCH_FEED_CANDIDATE_CAP]
)
- # The flag selects one of two independent rankers; nothing is blended between them. Shadow
- # teams rank on the weighted score too, because only the `jev` arm reads the probabilities
- # the hourly sweep cached. Neither arm makes a model call here. The response names the
- # ranker that ordered it, so the shadow arm reads as weighted-score to the client, and the
- # flag variant separately, which the client reports as the experiment's exposure.
- variant = watch_feed_ranker_variant(self.team_id, self.team.uuid)
- mode = ranker_mode(variant)
- ranker = mode if mode in ("jev", "embedding") else "weighted-score"
+ page_end = params["offset"] + params["limit"]
if ranker == "embedding":
- ranked = rank_watch_feed_by_embedding(candidate_rows)[: params["limit"]]
+ impressions = feed_impression_counts(
+ team_id=self.team_id,
+ user_id=viewer_id,
+ observation_ids=[row["id"] for row in candidate_rows],
+ as_of=as_of,
+ )
+ for row in candidate_rows:
+ row["feed_impressions"] = impressions.get(row["id"], 0)
+ ranked = rank_watch_feed_by_embedding(candidate_rows)
elif ranker == "jev":
ranks = load_watch_ranks(self.team_id, allowed_ids)
probabilities = ranks.probabilities
@@ -2677,7 +2740,7 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, vi
)
loaded_ids = {row["id"] for row in jev_rows}
missing = [observation_id for observation_id in watchable_missing if observation_id not in loaded_ids]
- needed = params["limit"]
+ needed = page_end
for start in range(0, len(missing), WATCH_FEED_BYPASS_CHUNK):
if needed <= 0:
break
@@ -2688,9 +2751,11 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, vi
)
jev_rows += fetched
needed -= len(fetched)
- ranked = rank_watch_feed_by_jev(jev_rows, probabilities, ranks.reasons)[: params["limit"]]
+ ranked = rank_watch_feed_by_jev(jev_rows, probabilities, ranks.reasons)
else:
- ranked = rank_watch_feed_candidates(candidate_rows)[: params["limit"]]
+ ranked = rank_watch_feed_candidates(candidate_rows)
+ has_more = len(ranked) > page_end
+ ranked = ranked[params["offset"] : page_end]
reasons_by_id = {entry.observation_id: entry.reason for entry in ranked}
rows = {
row.id: row
@@ -2711,7 +2776,47 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, vi
for entry in ranked
if entry.observation_id in rows
]
- return Response({"results": results, "ranker": ranker, "ranker_variant": variant})
+ return Response(
+ {
+ "results": results,
+ "has_more": has_more,
+ "as_of": as_of,
+ "ranker": ranker,
+ "ranker_variant": variant,
+ }
+ )
+
+ @extend_schema(request=WatchFeedImpressionsSerializer, responses={204: None})
+ @action(
+ detail=False,
+ methods=["post"],
+ pagination_class=None,
+ # Per-user state, like an observation's `viewed`, so read scope is enough.
+ required_scopes=["replay_scanner:read", "session_recording:read"],
+ throttle_classes=[ReplayVisionWatchFeedImpressionsRateThrottle],
+ )
+ def watch_feed_impressions(self, request: Request, **kwargs: Any) -> Response:
+ """Record that the calling user saw these cards in What to watch, so the feed ranks them lower next time."""
+ body = WatchFeedImpressionsSerializer(data=request.data)
+ body.is_valid(raise_exception=True)
+ if not self.user_access_control.check_access_level_for_resource("session_recording", required_level="viewer"):
+ raise PermissionDenied("Reading replay observations requires session_recording read access.")
+ readable = accessible_observations(
+ self.user_access_control,
+ self.team_id,
+ ReplayObservation.objects.filter(
+ team_id=self.team_id,
+ scanner_id__in=readable_observation_scanner_ids(self.user_access_control, self.team_id),
+ id__in=body.validated_data["observation_ids"],
+ ),
+ )
+ record_feed_impressions(
+ team_id=self.team_id,
+ user_id=cast(User, request.user).id,
+ observation_ids=list(readable.values_list("id", flat=True)),
+ feed_load_id=body.validated_data["feed_load_id"],
+ )
+ return Response(status=status.HTTP_204_NO_CONTENT)
@extend_schema(
request=ObserveRequestSerializer,
diff --git a/products/replay_vision/backend/migrations/0127_replayobservationfeedimpression.py b/products/replay_vision/backend/migrations/0127_replayobservationfeedimpression.py
new file mode 100644
index 000000000000..47c5ece90aca
--- /dev/null
+++ b/products/replay_vision/backend/migrations/0127_replayobservationfeedimpression.py
@@ -0,0 +1,70 @@
+# Generated by Django 5.2.18 on 2026-10-23 15:57
+
+import django.db.models.deletion
+from django.conf import settings
+from django.db import migrations, models
+
+import posthog.uuidt
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("posthog", "1411_personalapikey_expires_at"),
+ ("replay_vision", "0126_replayobservation_watch_rank"),
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="ReplayObservationFeedImpression",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.uuidt.uuid7,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ ("shown_count", models.PositiveIntegerField(default=1)),
+ ("last_shown_at", models.DateTimeField()),
+ ("last_feed_load_id", models.UUIDField(blank=True, null=True)),
+ (
+ "observation",
+ models.ForeignKey(
+ db_index=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="feed_impressions",
+ to="replay_vision.replayobservation",
+ ),
+ ),
+ (
+ "team",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="+",
+ to="posthog.team",
+ ),
+ ),
+ (
+ "user",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="+",
+ to=settings.AUTH_USER_MODEL,
+ ),
+ ),
+ ],
+ options={
+ "constraints": [
+ models.UniqueConstraint(
+ fields=("observation", "user"),
+ name="unique_replay_observation_feed_impression_per_user",
+ )
+ ],
+ },
+ ),
+ ]
diff --git a/products/replay_vision/backend/migrations/max_migration.txt b/products/replay_vision/backend/migrations/max_migration.txt
index 52d3ffafe31f..70c0a42b0982 100644
--- a/products/replay_vision/backend/migrations/max_migration.txt
+++ b/products/replay_vision/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0126_replayobservation_watch_rank
+0127_replayobservationfeedimpression
diff --git a/products/replay_vision/backend/models/__init__.py b/products/replay_vision/backend/models/__init__.py
index ed4e1fb2e961..942516461587 100644
--- a/products/replay_vision/backend/models/__init__.py
+++ b/products/replay_vision/backend/models/__init__.py
@@ -1,5 +1,6 @@
from products.replay_vision.backend.models.replay_observation import ReplayObservation
from products.replay_vision.backend.models.replay_observation_feed_dismissal import ReplayObservationFeedDismissal
+from products.replay_vision.backend.models.replay_observation_feed_impression import ReplayObservationFeedImpression
from products.replay_vision.backend.models.replay_observation_label import ReplayObservationLabel
from products.replay_vision.backend.models.replay_observation_media import ReplayObservationMedia
from products.replay_vision.backend.models.replay_observation_request import ReplayObservationRequest
@@ -19,6 +20,7 @@ from products.replay_vision.backend.models.vision_alert import (
__all__ = [
"ReplayObservation",
"ReplayObservationFeedDismissal",
+ "ReplayObservationFeedImpression",
"ReplayObservationLabel",
"ReplayObservationMedia",
"ReplayObservationRequest",
diff --git a/products/replay_vision/backend/models/replay_observation_feed_impression.py b/products/replay_vision/backend/models/replay_observation_feed_impression.py
new file mode 100644
index 000000000000..a8467f918596
--- /dev/null
+++ b/products/replay_vision/backend/models/replay_observation_feed_impression.py
@@ -0,0 +1,26 @@
+from django.db import models
+
+from posthog.models.scoping.root_mixin import TeamScopedRootMixin
+from posthog.models.utils import UUIDModel
+
+
+class ReplayObservationFeedImpression(TeamScopedRootMixin, UUIDModel):
+ """How often What to watch showed this observation to one user, so the feed can rank it lower each time."""
+
+ # The unique constraint leads with observation_id, so a separate index on it would only slow writes.
+ observation = models.ForeignKey(
+ "replay_vision.ReplayObservation", on_delete=models.CASCADE, related_name="feed_impressions", db_index=False
+ )
+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, related_name="+", db_constraint=False)
+ user = models.ForeignKey("posthog.User", on_delete=models.CASCADE, related_name="+", db_constraint=False)
+ shown_count = models.PositiveIntegerField(default=1)
+ last_shown_at = models.DateTimeField()
+ # The feed load that last counted, so one load counts each card once however often it reports it.
+ last_feed_load_id = models.UUIDField(null=True, blank=True)
+
+ class Meta:
+ constraints = [
+ models.UniqueConstraint(
+ fields=["observation", "user"], name="unique_replay_observation_feed_impression_per_user"
+ ),
+ ]
diff --git a/products/replay_vision/backend/search.py b/products/replay_vision/backend/search.py
index 99f2c51ddc1a..2191c2e435cc 100644
--- a/products/replay_vision/backend/search.py
+++ b/products/replay_vision/backend/search.py
@@ -324,15 +324,17 @@ def fetch_ranked_observations(
return [obs for observation_id in ordered_ids if (obs := observations.get(observation_id)) is not None]
-def parse_date_bound(value: str, timezone_info: ZoneInfo | None, *, end_of_range: bool) -> datetime:
+def parse_date_bound(
+ value: str, timezone_info: ZoneInfo | None, *, end_of_range: bool, now: datetime | None = None
+) -> datetime:
"""Parse a caller-supplied date bound: ISO 8601, `now`, or relative (`-7d`), naive values in the
project's timezone. A date-only upper bound covers its whole day. Shared by the observation list and
search. Raises `ValueError` on text that is none of those, rather than treating it as now."""
timezone_info = timezone_info or ZoneInfo("UTC")
if value.strip().lower() == "now":
# `now` already carries a time of day, so it skips the end-of-range widening below.
- return datetime.now(timezone_info)
- parsed, delta_mapping, _ = relative_date_parse_with_delta_mapping(value, timezone_info)
+ return now.astimezone(timezone_info) if now else datetime.now(timezone_info)
+ parsed, delta_mapping, _ = relative_date_parse_with_delta_mapping(value, timezone_info, now=now)
# The relative branch hands back an empty mapping when nothing in the text matched a period.
if delta_mapping == {}:
raise ValueError(f"Unrecognized date: {value!r}. Use ISO 8601, a relative date like -7d, or 'now'.")
diff --git a/products/replay_vision/backend/tests/test_api.py b/products/replay_vision/backend/tests/test_api.py
index 5ffa3ef63a49..a6f717902816 100644
--- a/products/replay_vision/backend/tests/test_api.py
+++ b/products/replay_vision/backend/tests/test_api.py
@@ -48,6 +48,7 @@ from products.replay_vision.backend.models.replay_observation import (
ObservationTrigger,
ReplayObservation,
)
+from products.replay_vision.backend.models.replay_observation_feed_impression import ReplayObservationFeedImpression
from products.replay_vision.backend.models.replay_observation_label import ReplayObservationLabel
from products.replay_vision.backend.models.replay_observation_usage import ReplayObservationUsage
from products.replay_vision.backend.models.replay_scanner import (
@@ -5176,6 +5177,88 @@ class TestWatchFeedAPI(_VisionAPITestCase):
items[0]["reason"], {"kind": "embedding_pick", "watch_category": "failure", "similar_count": 0}
)
+ def test_pages_do_not_shift_when_the_reader_opens_a_card_mid_scroll(self) -> None:
+ scanner = self._create_scanner(
+ name="s", scanner_type=ScannerType.SUMMARIZER, scanner_config={"prompt": "p", "length": "short"}
+ )
+ for i in range(6):
+ self._succeeded_observation(scanner, f"hit-{i}", 10 + i, self._friction_result())
+
+ first = self.client.get(f"{self.feed_url}?limit=3").json()
+ assert first["has_more"] is True
+ # Opening the top card would dock it below page two, and a card arriving would push everything down.
+ top_id = first["results"][0]["observation"]["id"]
+ assert self.client.post(f"{self.observations_url(str(scanner.id))}{top_id}/viewed/").status_code == 204
+ self._succeeded_observation(scanner, "late-hit", 0, self._friction_result())
+ # A scan already running at page one, which completes before page two, would also shift it.
+ self._succeeded_observation(scanner, "finished-late", 12, self._friction_result())
+ second = self.client.get(self.feed_url, {"limit": 3, "offset": 3, "as_of": first["as_of"]}).json()
+
+ sessions = [item["observation"]["session_id"] for item in first["results"] + second["results"]]
+ assert sessions == [f"hit-{i}" for i in range(6)]
+ assert second["has_more"] is False
+
+ def test_impressions_lower_an_embedding_pick_only_after_the_load_that_showed_it(self) -> None:
+ scanner = self._create_scanner(name="m")
+ rows = {}
+ for index in range(10):
+ obs = self._succeeded_observation(scanner, f"s-{index}", 10 + index, self._monitor_result("no"))
+ # The top two contrasts clear the top-fifth cut; the rest are the scanner's baseline.
+ category = "failure" if index < 2 else "routine"
+ ReplayObservation.objects.filter(id=obs.id).update(
+ watch_rank={
+ "category": category,
+ "contrast": 0.5 - index * 0.01,
+ "duplicate_of": None,
+ "anchor_version": ANCHOR_VERSION,
+ }
+ )
+ rows[f"s-{index}"] = obs
+ other_team = Team.objects.create(organization=self.organization, name="other")
+ foreign = ReplayObservation.objects.create(
+ team=other_team,
+ scanner=ReplayScanner.objects.create(
+ team=other_team,
+ name="theirs",
+ scanner_type=ScannerType.MONITOR,
+ scanner_config={"prompt": "p"},
+ model=ScannerModel.GEMINI_3_8_FLASH,
+ ),
+ session_id="foreign",
+ status=ObservationStatus.SUCCEEDED,
+ completed_at=timezone.now(),
+ )
+ load = uuid.uuid4()
+
+ def load_picks(**params: str) -> tuple[list[str], str]:
+ with patch(
+ "products.replay_vision.backend.api.scanners.watch_feed_ranker_variant", return_value="embedding"
+ ):
+ body = self.client.get(self.feed_url, params).json()
+ picks = [
+ item["observation"]["session_id"]
+ for item in body["results"]
+ if item["reason"]["kind"] == "embedding_pick"
+ ]
+ return picks, body["as_of"]
+
+ picks, as_of = load_picks()
+ assert picks == ["s-0", "s-1"]
+ for _ in range(2):
+ # The same load reporting twice, as a tab hidden and shown again does, counts once.
+ resp = self.client.post(
+ f"{self.scanners_url}watch_feed_impressions/",
+ {"feed_load_id": str(load), "observation_ids": [str(rows["s-0"].id), str(foreign.id)]},
+ format="json",
+ )
+ assert resp.status_code == 204, resp.content
+ assert ReplayObservationFeedImpression.objects.for_team(self.team.id).get().shown_count == 1
+ assert not ReplayObservationFeedImpression.objects.for_team(other_team.id).exists()
+
+ # A later page of the same load ranks as of its first page, before the impression.
+ assert load_picks(as_of=as_of)[0] == ["s-0", "s-1"]
+ assert load_picks()[0] == ["s-1", "s-0"]
+
def test_the_jev_arm_surfaces_a_watchable_row_the_recency_slice_cut_off(self) -> None:
# The candidate query keeps only each scanner's newest rows, so on a high-volume scanner a
# watchable row from days ago never reaches the weighted feed. The jev arm must fetch it
diff --git a/products/replay_vision/backend/watch_feed_impressions.py b/products/replay_vision/backend/watch_feed_impressions.py
new file mode 100644
index 000000000000..43cc9eb1b2dd
--- /dev/null
+++ b/products/replay_vision/backend/watch_feed_impressions.py
@@ -0,0 +1,62 @@
+"""Per-user counts of how often What to watch showed each observation."""
+
+from collections.abc import Sequence
+from datetime import datetime
+from uuid import UUID
+
+from django.db import transaction
+from django.db.models import F
+from django.utils import timezone
+
+from products.replay_vision.backend.models.replay_observation_feed_impression import ReplayObservationFeedImpression
+
+
+def record_feed_impressions(*, team_id: int, user_id: int, observation_ids: Sequence[UUID], feed_load_id: UUID) -> None:
+ """Count one impression per observation per feed load; a repeat from the same load changes nothing."""
+ if not observation_ids:
+ return
+ now = timezone.now()
+ with transaction.atomic():
+ ReplayObservationFeedImpression.objects.for_team(team_id).filter(
+ user_id=user_id, observation_id__in=observation_ids
+ ).exclude(last_feed_load_id=feed_load_id).update(
+ shown_count=F("shown_count") + 1, last_shown_at=now, last_feed_load_id=feed_load_id
+ )
+ # Rows that exist were counted above, so the conflict only skips them. A concurrent first
+ # impression from another tab wins the race the same way, which is harmless.
+ ReplayObservationFeedImpression.objects.bulk_create(
+ [
+ ReplayObservationFeedImpression(
+ team_id=team_id,
+ user_id=user_id,
+ observation_id=observation_id,
+ last_shown_at=now,
+ last_feed_load_id=feed_load_id,
+ )
+ for observation_id in observation_ids
+ ],
+ ignore_conflicts=True,
+ )
+
+
+def feed_impression_counts(
+ *, team_id: int, user_id: int, observation_ids: Sequence[UUID], as_of: datetime
+) -> dict[UUID, int]:
+ """Times each observation was shown to the user as of `as_of`.
+
+ A row shown again since `as_of` counts one less, which covers the load being paged, so its own
+ impressions do not shift its later pages.
+ """
+ if not observation_ids:
+ return {}
+ rows = (
+ ReplayObservationFeedImpression.objects.for_team(team_id)
+ .filter(user_id=user_id, observation_id__in=observation_ids)
+ .values_list("observation_id", "shown_count", "last_shown_at")
+ )
+ counts: dict[UUID, int] = {}
+ for observation_id, shown_count, last_shown_at in rows:
+ count = shown_count - 1 if last_shown_at > as_of else shown_count
+ if count > 0:
+ counts[observation_id] = count
+ return counts
diff --git a/products/replay_vision/backend/watch_rank.py b/products/replay_vision/backend/watch_rank.py
index 2dc4d7d0f3f8..1fcb76376779 100644
--- a/products/replay_vision/backend/watch_rank.py
+++ b/products/replay_vision/backend/watch_rank.py
@@ -91,6 +91,9 @@ _DUPLICATE_QUERY_TIMEOUT_S = 10
WATCH_PICK_MIN_PERCENTILE = 0.8
WATCH_PICK_MIN_SCANNER_ROWS = 10
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
_ANCHOR_CACHE_TTL_S = 7 * 24 * 3600
@@ -315,13 +318,14 @@ def _group_root(observation_id: str, parents: dict[str, str]) -> str:
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`)
- on the embedding rank the watch rank consumer wrote.
+ """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.
A pick is a row in its scanner's top fifth by score whose category is an issue. Near-duplicates
collapse to their best-scoring row, which carries the size of the group. Picks sort by weighted
- percentile, viewed rows docked, one scanner held to a share of the picks, and one session's picks
- spaced apart. Every other row is filler, trimmed to the same floor as the other rankers.
+ percentile, decayed for each earlier impression, viewed rows docked, one scanner held to a share
+ of the picks, and one session's picks spaced apart. Every other row is filler, trimmed to the same
+ floor as the other rankers.
"""
percentiles = _scanner_percentiles(rows)
parents = {
@@ -343,7 +347,11 @@ def rank_watch_feed_by_embedding(rows: list[dict[str, Any]]) -> list[WatchFeedEn
# Only a member above the cutoff can stand for the group, so the seen penalty never disqualifies it.
if percentile < WATCH_PICK_MIN_PERCENTILE:
continue
- score = percentile * CATEGORY_WEIGHT[rank["category"]]
+ score = (
+ percentile
+ * CATEGORY_WEIGHT[rank["category"]]
+ * WATCH_PICK_IMPRESSION_DECAY ** row.get("feed_impressions", 0)
+ )
if row.get("feed_viewed"):
score -= WATCH_PICK_SEEN_PENALTY
if group not in best_in_group or score > best_in_group[group][0]:
diff --git a/products/replay_vision/frontend/generated/api.schemas.ts b/products/replay_vision/frontend/generated/api.schemas.ts
index ae8506b988b9..d8a1870e7b6f 100644
--- a/products/replay_vision/frontend/generated/api.schemas.ts
+++ b/products/replay_vision/frontend/generated/api.schemas.ts
@@ -3186,6 +3186,10 @@ export const WatchFeedRankerEnumApi = {
export interface WatchFeedResponseApi {
/** Succeeded observations in the window worth watching, most interesting first, each carrying the reason it ranked. Every observation that carries a finding is returned; observations that carry none (`unviewed_recent`, `recent`) are returned only to pad a near-empty feed to three items, so a quiet window answers with a handful of rows rather than a full page of newest clips. */
results: WatchFeedItemApi[]
+ /** Whether a later page holds more ranked items. Request it with `offset` set past this page. */
+ has_more: boolean
+ /** The time this feed ranks as of. Send it back with later pages. */
+ as_of: string
/** Which ranker ordered this feed: `jev` ranks on the decision model's cached judgments, `embedding` on the rank the watch rank consumer wrote from the observation's embedding, `weighted-score` on the deterministic blend. The arm is decided server-side per team, so clients read it from here rather than evaluating the flag themselves.
*
* * `weighted-score` - Weighted Score
@@ -3199,6 +3203,20 @@ export interface WatchFeedResponseApi {
ranker_variant: string | null
}
+/**
+ * Body of POST /vision/scanners/watch_feed_impressions/.
+ */
+export interface WatchFeedImpressionsApi {
+ /** The client's id for the feed load that showed these cards. One load counts each card once. */
+ feed_load_id: string
+ /**
+ * Observations the calling user saw in What to watch, at most 100. Ids the user cannot read are ignored.
+ * @minItems 1
+ * @maxItems 100
+ */
+ observation_ids: string[]
+}
+
export type VisionAlertsListParams = {
/**
* Number of results to return per page.
@@ -3732,6 +3750,10 @@ export type VisionScannersObservationsStatsRetrieveParams = {
}
export type VisionScannersWatchFeedRetrieveParams = {
+ /**
+ * The time the first page loaded. Later pages rank as of that time: observations, views, dismissals and impressions after it are ignored, so pages do not shift under a reader who scrolls.
+ */
+ as_of?: string
/**
* Comma-separated user IDs. Restrict the feed to observations from scanners those users created.
* @minLength 1
@@ -3748,11 +3770,17 @@ export type VisionScannersWatchFeedRetrieveParams = {
*/
date_to?: string
/**
- * Ceiling on feed items to return, at most 50. The feed is bounded, not paginated, and routinely returns far fewer: a window is not padded to this number with clips that carry no finding.
+ * Ceiling on feed items to return per page, at most 50. A page routinely returns fewer: a window is not padded to this number with clips that carry no finding.
* @minimum 1
* @maximum 50
*/
limit?: number
+ /**
+ * How many ranked items to skip. Send `as_of` with every page after the first.
+ * @minimum 0
+ * @maximum 1000
+ */
+ offset?: number
/**
* Comma-separated scanner UUIDs to restrict the feed to. Defaults to every scanner you can read.
* @minLength 1
diff --git a/products/replay_vision/frontend/generated/api.ts b/products/replay_vision/frontend/generated/api.ts
index 8650c33293c7..1aa4a51034b4 100644
--- a/products/replay_vision/frontend/generated/api.ts
+++ b/products/replay_vision/frontend/generated/api.ts
@@ -80,6 +80,7 @@ import type {
VisionScannersObservationsThumbnailRetrieveParams,
VisionScannersWatchFeedRetrieveParams,
VisionSpendSeriesApi,
+ WatchFeedImpressionsApi,
WatchFeedResponseApi,
} from './api.schemas'
@@ -1693,3 +1694,23 @@ export const visionScannersWatchFeedRetrieve = async (
method: 'GET',
})
}
+
+export const getVisionScannersWatchFeedImpressionsCreateUrl = (projectId: string) => {
+ return `/api/projects/${projectId}/vision/scanners/watch_feed_impressions/`
+}
+
+/**
+ * Record that the calling user saw these cards in What to watch, so the feed ranks them lower next time.
+ */
+export const visionScannersWatchFeedImpressionsCreate = async (
+ projectId: string,
+ watchFeedImpressionsApi: WatchFeedImpressionsApi,
+ options?: RequestInit
+): Promise<void> => {
+ return apiMutator<void>(getVisionScannersWatchFeedImpressionsCreateUrl(projectId), {
+ ...options,
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json', ...options?.headers },
+ body: JSON.stringify(watchFeedImpressionsApi),
+ })
+}
diff --git a/products/replay_vision/frontend/generated/api.zod.ts b/products/replay_vision/frontend/generated/api.zod.ts
index faa8b4f09089..944bd277168c 100644
--- a/products/replay_vision/frontend/generated/api.zod.ts
+++ b/products/replay_vision/frontend/generated/api.zod.ts
@@ -1580,3 +1580,23 @@ export const VisionScannersSuggestTagsCreateBody = /* @__PURE__ */ zod
),
})
.describe('Body of POST \/vision\/scanners\/suggest_tags\/ — the classifier config currently being edited.')
+
+/**
+ * Record that the calling user saw these cards in What to watch, so the feed ranks them lower next time.
+ */
+export const visionScannersWatchFeedImpressionsCreateBodyObservationIdsMax = 100
+
+export const VisionScannersWatchFeedImpressionsCreateBody = /* @__PURE__ */ zod
+ .object({
+ feed_load_id: zod
+ .uuid()
+ .describe("The client's id for the feed load that showed these cards. One load counts each card once."),
+ observation_ids: zod
+ .array(zod.uuid())
+ .min(1)
+ .max(visionScannersWatchFeedImpressionsCreateBodyObservationIdsMax)
+ .describe(
+ 'Observations the calling user saw in What to watch, at most 100. Ids the user cannot read are ignored.'
+ ),
+ })
+ .describe('Body of POST \/vision\/scanners\/watch_feed_impressions\/.')
diff --git a/products/replay_vision/frontend/replay_scanners/components/WatchFeedTab.tsx b/products/replay_vision/frontend/replay_scanners/components/WatchFeedTab.tsx
index 42c18f307e25..61c4febaf26b 100644
--- a/products/replay_vision/frontend/replay_scanners/components/WatchFeedTab.tsx
+++ b/products/replay_vision/frontend/replay_scanners/components/WatchFeedTab.tsx
@@ -1,4 +1,6 @@
import { useActions, useValues } from 'kea'
+import { useEffect } from 'react'
+import { useInView } from 'react-intersection-observer'
import { IconSearch } from '@posthog/icons'
import { LemonButton, LemonInput, LemonSkeleton } from '@posthog/lemon-ui'
@@ -42,6 +44,43 @@ function FeedSkeleton(): JSX.Element {
)
}
+/** Loads the next page as the reader nears the end, and closes the feed once the ranking runs out. */
+function FeedEnd({ itemCount }: { itemCount: number }): JSX.Element | null {
+ const { feedHasMore, feedItemsLoading, feedMoreFailed } = useValues(watchFeedLogic)
+ const { loadMoreFeed } = useActions(watchFeedLogic)
+ // Starts the request a little before the end is on screen, so a steady scroll rarely waits.
+ const { ref, inView } = useInView({ rootMargin: '600px 0px' })
+
+ useEffect(() => {
+ if (inView && feedHasMore && !feedItemsLoading && !feedMoreFailed) {
+ loadMoreFeed({})
+ }
+ }, [inView, feedHasMore, feedItemsLoading, feedMoreFailed, loadMoreFeed])
+
+ if (feedMoreFailed) {
+ return (
+ <div className="flex items-center gap-2 text-sm text-secondary border rounded p-4">
+ <span>Couldn't load more sessions.</span>
+ <LemonButton size="small" type="secondary" onClick={() => loadMoreFeed({})}>
+ Try again
+ </LemonButton>
+ </div>
+ )
+ }
+ if (feedHasMore) {
+ return (
+ <div ref={ref} aria-busy>
+ <LemonSkeleton className="h-32 rounded" />
+ </div>
+ )
+ }
+ return itemCount > 3 ? (
+ <p className="text-sm text-secondary text-center m-0 py-4" data-attr="vision-watch-feed-end">
+ You're caught up. New sessions show up here as your scanners find them.
+ </p>
+ ) : null
+}
+
export function WatchFeedTab(): JSX.Element {
const {
feedItems,
@@ -88,9 +127,7 @@ export function WatchFeedTab(): JSX.Element {
<div className="@container flex flex-col gap-4">
<div className="flex flex-wrap items-end justify-between gap-2">
<div className="flex flex-col gap-1 flex-1 min-w-[16rem]">
- <h2 className="text-xl font-semibold m-0">
- {items.length === 0 ? 'What to watch' : `Top ${pluralize(items.length, 'session')}`}
- </h2>
+ <h2 className="text-xl font-semibold m-0">What to watch</h2>
<p className="text-muted text-sm m-0">
{narrowedToScanners > 0
? `Following ${pluralize(narrowedToScanners, 'scanner')} of ${allScanners.length}. `
@@ -183,13 +220,16 @@ export function WatchFeedTab(): JSX.Element {
</p>
)}
{items.length > 0 ? (
- items.map((item, index) => (
- <WatchFeedRow
- key={`${feedLoad?.feedLoadId}-${item.observation.id}`}
- item={item}
- position={index}
- />
- ))
+ <>
+ {items.map((item, index) => (
+ <WatchFeedRow
+ key={`${feedLoad?.feedLoadId}-${item.observation.id}`}
+ item={item}
+ position={index}
+ />
+ ))}
+ <FeedEnd itemCount={items.length} />
+ </>
) : feedFailed ? null : emptyReason ? (
<WatchFeedEmptyState reason={emptyReason} />
) : (
diff --git a/products/replay_vision/frontend/replay_scanners/watchFeedImpressionCounts.ts b/products/replay_vision/frontend/replay_scanners/watchFeedImpressionCounts.ts
new file mode 100644
index 000000000000..32799aa6e7e4
--- /dev/null
+++ b/products/replay_vision/frontend/replay_scanners/watchFeedImpressionCounts.ts
@@ -0,0 +1,22 @@
+import { chunk } from 'lib/utils/arrays'
+import { getCurrentTeamIdOrNone } from 'lib/utils/getAppContext'
+
+import { visionScannersWatchFeedImpressionsCreate } from '../generated/api'
+
+// The impressions endpoint takes at most this many ids per request.
+const IMPRESSIONS_BATCH_SIZE = 100
+
+/** Tells the server which cards one feed load showed, so the feed ranks them lower next time. Best effort:
+ * a lost batch only means a card ranks a little higher on the next load. */
+export function recordFeedImpressions(feedLoadId: string, observationIds: string[]): void {
+ const teamId = getCurrentTeamIdOrNone()
+ if (!teamId) {
+ return
+ }
+ for (const ids of chunk(observationIds, IMPRESSIONS_BATCH_SIZE)) {
+ void visionScannersWatchFeedImpressionsCreate(String(teamId), {
+ feed_load_id: feedLoadId,
+ observation_ids: ids,
+ }).catch(() => {})
+ }
+}
diff --git a/products/replay_vision/frontend/replay_scanners/watchFeedLogic.test.ts b/products/replay_vision/frontend/replay_scanners/watchFeedLogic.test.ts
index d323f1f54846..36070a64e372 100644
--- a/products/replay_vision/frontend/replay_scanners/watchFeedLogic.test.ts
+++ b/products/replay_vision/frontend/replay_scanners/watchFeedLogic.test.ts
@@ -1,3 +1,4 @@
+import { waitFor } from '@testing-library/react'
import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
import posthog from 'posthog-js'
@@ -209,6 +210,104 @@ describe('watchFeedLogic', () => {
expect(logic.values.feedItems?.map((i) => i.observation.id)).toEqual(['o3'])
})
+ it('pages through one load, counting the offset from what was served', async () => {
+ useMocks({ post: { '/api/projects/:team/vision/observations/:id/feed_dismissal/': () => [204] } })
+ feedSpy.mockImplementation(() => [
+ 200,
+ { results: [item('o1', 'embedding_pick'), item('o2', 'embedding_pick')], has_more: true, as_of: 'T0' },
+ ])
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadFeedSuccess']).toFinishAllListeners()
+ // Hiding a card shortens the list, but the next page still starts after both served cards.
+ await expectLogic(logic, () =>
+ logic.actions.dismissItem(logic.values.feedItems![0], 0, null)
+ ).toFinishAllListeners()
+
+ feedSpy.mockImplementation(() => [
+ 200,
+ { results: [item('o2', 'embedding_pick'), item('o3', 'recent')], has_more: true, as_of: 'T0' },
+ ])
+ await expectLogic(logic, () => logic.actions.loadMoreFeed({}))
+ .toDispatchActions(['loadMoreFeedSuccess'])
+ .toFinishAllListeners()
+
+ const pageUrl = new URL(feedSpy.mock.calls.at(-1)[0].request.url)
+ expect(pageUrl.searchParams.get('offset')).toBe('2')
+ expect(pageUrl.searchParams.get('as_of')).toBe('T0')
+ expect(logic.values.feedItems?.map((i) => i.observation.id)).toEqual(['o2', 'o3'])
+ expect(logic.values.feedLoad?.positions).toEqual({ o1: 0, o2: 1, o3: 2 })
+
+ // The deduplicated o2 still counts toward the offset, and an empty page ends paging instead of repeating.
+ feedSpy.mockImplementation(() => [200, { results: [], has_more: true, as_of: 'T0' }])
+ await expectLogic(logic, () => logic.actions.loadMoreFeed({}))
+ .toDispatchActions(['loadMoreFeedSuccess'])
+ .toFinishAllListeners()
+
+ expect(new URL(feedSpy.mock.calls.at(-1)[0].request.url).searchParams.get('offset')).toBe('4')
+ expect(logic.values.feedHasMore).toBe(false)
+ })
+
+ it('drops a next page that fails after the filters changed', async () => {
+ feedSpy.mockImplementation(() => [
+ 200,
+ { results: [item('o1', 'embedding_pick')], has_more: true, as_of: 'T0' },
+ ])
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadFeedSuccess']).toFinishAllListeners()
+
+ let failPage: () => void = () => {}
+ // The next page (it carries an offset) stays pending until the test fails it; a fresh load answers at once.
+ feedSpy.mockImplementation(({ request }) =>
+ new URL(request.url).searchParams.has('offset')
+ ? new Promise((resolve) => {
+ failPage = () => resolve([500, { detail: 'nope' }])
+ })
+ : [200, { results: [item('o2', 'recent')], has_more: true, as_of: 'T1' }]
+ )
+ logic.actions.loadMoreFeed({})
+ // Waiting for every listener would wait on the held page too, so wait for the new load alone.
+ await expectLogic(logic, () => logic.actions.setScannerTypeFilter('monitor')).toDispatchActions([
+ 'loadFeedSuccess',
+ ])
+
+ failPage()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(logic.values.feedMoreFailed).toBe(false)
+ expect(logic.values.feedItems?.map((i) => i.observation.id)).toEqual(['o2'])
+ expect(logic.values.feedHasMore).toBe(true)
+ })
+
+ it('appends a next page once when it is requested again while in flight', async () => {
+ feedSpy.mockImplementation(() => [
+ 200,
+ { results: [item('o1', 'embedding_pick')], has_more: true, as_of: 'T0' },
+ ])
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadFeedSuccess']).toFinishAllListeners()
+
+ const pending: (() => void)[] = []
+ feedSpy.mockImplementation(
+ () =>
+ new Promise((resolve) => {
+ pending.push(() =>
+ resolve([200, { results: [item('o2', 'recent')], has_more: false, as_of: 'T0' }])
+ )
+ })
+ )
+ logic.actions.loadMoreFeed({})
+ logic.actions.loadMoreFeed({})
+ await expectLogic(logic).toMatchValues({ feedItemsLoading: true })
+ await waitFor(() => expect(pending).toHaveLength(2))
+ // The newer request lands first, then the older one would append the same page again.
+ pending[1]()
+ await expectLogic(logic).toDispatchActions(['loadMoreFeedSuccess'])
+ pending[0]()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(logic.values.feedItems?.map((i) => i.observation.id)).toEqual(['o1', 'o2'])
+ })
+
it('flags a failed load and clears the flag on retry', async () => {
feedSpy.mockImplementation(() => [500, { detail: 'nope' }])
logic.mount()
diff --git a/products/replay_vision/frontend/replay_scanners/watchFeedLogic.ts b/products/replay_vision/frontend/replay_scanners/watchFeedLogic.ts
index 9b23e14849f0..057f941b42b6 100644
--- a/products/replay_vision/frontend/replay_scanners/watchFeedLogic.ts
+++ b/products/replay_vision/frontend/replay_scanners/watchFeedLogic.ts
@@ -23,12 +23,14 @@ import { csvParam, parseCsvParam } from '../utils/urlParams'
import { ScannerType } from './types'
import { dismissFeedItem, withFeedItemRestored, withoutFeedItem } from './watchFeedDismissal'
import { type WatchFeedEmptyReason, resolveWatchFeedEmptyReason } from './watchFeedEmptyState'
+import { recordFeedImpressions } from './watchFeedImpressionCounts'
import {
type WatchFeedLoad,
WatchFeedImpressions,
cardProperties,
newWatchFeedLoad,
servedListProperties,
+ withNextPage,
} from './watchFeedTelemetry'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -42,12 +44,16 @@ export interface watchFeedLogicValues {
dateFrom: string
dateTo: string | null
emptyReason: WatchFeedEmptyReason | null
+ feedAsOf: string | null
feedFailed: boolean
+ feedHasMore: boolean
feedItems: WatchFeedItemApi[] | null
feedItemsLoading: boolean
feedLoad: WatchFeedLoad | null
+ feedMoreFailed: boolean
feedRanker: WatchFeedRanker
feedRankerVariant: string | null
+ feedServed: number
hasFeedFilters: boolean
scannerIdsFilter: string[]
scannerTypeFilter: ScannerType | null
@@ -83,6 +89,9 @@ export interface watchFeedLogicActions {
position: number
reason: FeedDismissalReasonEnumApi | null
}
+ extendFeedLoad: (load: WatchFeedLoad) => {
+ load: WatchFeedLoad
+ }
loadFeed: () => {
value: true
}
@@ -104,6 +113,21 @@ export interface watchFeedLogicActions {
value: true
}
}
+ loadMoreFeed: (_: any) => any
+ loadMoreFeedFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadMoreFeedSuccess: (
+ feedItems: WatchFeedItemApi[] | null,
+ payload?: any
+ ) => {
+ feedItems: WatchFeedItemApi[] | null
+ payload?: any
+ }
restoreFeedFilters: (
search: string,
scannerIds: string[],
@@ -135,6 +159,15 @@ export interface watchFeedLogicActions {
setFeedLoad: (load: WatchFeedLoad) => {
load: WatchFeedLoad
}
+ setFeedPaging: (
+ hasMore: boolean,
+ asOf: string | null,
+ served: number
+ ) => {
+ asOf: string | null
+ hasMore: boolean
+ served: number
+ }
setFeedRanker: (
ranker: WatchFeedRanker,
variant: string | null
@@ -191,6 +224,7 @@ export type watchFeedLogicType = MakeLogicType<
>
export const DEFAULT_FEED_DATE_FROM: string = '-7d'
+export const FEED_PAGE_SIZE = 20
/** Which ranker ordered the loaded feed. Server-decided per team (the flag evaluates against the
* team, not the viewer), so the client reads it from the response instead of checking the flag. */
@@ -200,6 +234,30 @@ export type WatchFeedRanker = WatchFeedResponseApi['ranker']
* evaluate it and reports the variant the response carries instead. */
const WATCH_FEED_RANKER_FLAG = 'vision-watch-feed-ranker'
+function feedParams(values: watchFeedLogicValues): VisionScannersWatchFeedRetrieveParams {
+ const params: VisionScannersWatchFeedRetrieveParams = { date_from: values.dateFrom, limit: FEED_PAGE_SIZE }
+ if (values.dateTo) {
+ params.date_to = values.dateTo
+ }
+ if (values.scannerTypeFilter) {
+ params.scanner_type = values.scannerTypeFilter
+ }
+ if (values.scannerIdsFilter.length > 0) {
+ params.scanner_ids = values.scannerIdsFilter.join(',')
+ }
+ if (values.tagsFilter.length > 0) {
+ params.tags = values.tagsFilter.join(',')
+ }
+ if (values.createdByFilter.length > 0) {
+ params.created_by = values.createdByFilter.join(',')
+ }
+ const trimmedSearch = values.search.trim()
+ if (trimmedSearch) {
+ params.search = trimmedSearch
+ }
+ return params
+}
+
function reportFiltered(values: watchFeedLogicValues): void {
posthog.capture('replay_vision_watch_feed_filtered', {
date_from: values.dateFrom,
@@ -249,6 +307,8 @@ export const watchFeedLogic = kea<watchFeedLogicType>([
loadFeed: true,
setFeedRanker: (ranker: WatchFeedRanker, variant: string | null) => ({ ranker, variant }),
setFeedLoad: (load: WatchFeedLoad) => ({ load }),
+ extendFeedLoad: (load: WatchFeedLoad) => ({ load }),
+ setFeedPaging: (hasMore: boolean, asOf: string | null, served: number) => ({ hasMore, asOf, served }),
cardSeen: (item: WatchFeedItemApi, position: number) => ({ item, position }),
dismissItem: (item: WatchFeedItemApi, position: number, reason: FeedDismissalReasonEnumApi | null) => ({
item,
@@ -258,44 +318,72 @@ export const watchFeedLogic = kea<watchFeedLogicType>([
restoreItem: (item: WatchFeedItemApi, position: number) => ({ item, position }),
}),
- loaders(({ values, actions }) => ({
+ loaders(({ values, actions, cache }) => ({
feedItems: [
null as WatchFeedItemApi[] | null,
{
loadFeed: async (_, breakpoint) => {
+ // Counts loads from the moment one starts, so a page requested for an older load is dropped.
+ cache.feedGeneration = (cache.feedGeneration ?? 0) + 1
const teamId = teamLogic.values.currentTeamId
if (!teamId) {
// Reporting this as a successful empty result would read as "nothing to watch";
// fail instead so the tab shows the error state and a retry.
throw new Error('No current team')
}
- const params: VisionScannersWatchFeedRetrieveParams = { date_from: values.dateFrom }
- if (values.dateTo) {
- params.date_to = values.dateTo
- }
- if (values.scannerTypeFilter) {
- params.scanner_type = values.scannerTypeFilter
- }
- if (values.scannerIdsFilter.length > 0) {
- params.scanner_ids = values.scannerIdsFilter.join(',')
- }
- if (values.tagsFilter.length > 0) {
- params.tags = values.tagsFilter.join(',')
- }
- if (values.createdByFilter.length > 0) {
- params.created_by = values.createdByFilter.join(',')
- }
- const trimmedSearch = values.search.trim()
- if (trimmedSearch) {
- params.search = trimmedSearch
- }
- const response = await visionScannersWatchFeedRetrieve(String(teamId), params)
+ const response = await visionScannersWatchFeedRetrieve(String(teamId), feedParams(values))
// Drop out-of-order responses — the most recent filter change owns the feed.
breakpoint()
actions.setFeedRanker(response.ranker ?? 'weighted-score', response.ranker_variant ?? null)
actions.setFeedLoad(newWatchFeedLoad(response.ranker, response.results))
+ actions.setFeedPaging(response.has_more ?? false, response.as_of ?? null, response.results.length)
return response.results
},
+ loadMoreFeed: async (_, breakpoint) => {
+ const teamId = teamLogic.values.currentTeamId
+ const load = values.feedLoad
+ if (!teamId || !load || !values.feedHasMore || !values.feedAsOf) {
+ return values.feedItems
+ }
+ // Counted against what the server returned, so neither a hidden card nor a deduplicated one shifts the next page.
+ const offset = values.feedServed
+ const generation = cache.feedGeneration
+ let response
+ try {
+ response = await visionScannersWatchFeedRetrieve(String(teamId), {
+ ...feedParams(values),
+ offset,
+ as_of: values.feedAsOf,
+ })
+ } catch (error) {
+ // A failure for a load the reader has since replaced must not stop paging the new one.
+ if (cache.feedGeneration !== generation) {
+ return values.feedItems
+ }
+ throw error
+ }
+ // A newer request for the next page supersedes this one, so the same page never appends twice.
+ breakpoint()
+ // A filter change while this page was in flight started a new load, which owns the feed now.
+ if (cache.feedGeneration !== generation || values.feedLoad?.feedLoadId !== load.feedLoadId) {
+ return values.feedItems
+ }
+ const page = response.results.filter((item) => !(item.observation.id in load.positions))
+ actions.extendFeedLoad(withNextPage(load, page))
+ // An empty page cannot advance the offset, so it ends paging rather than repeating the request.
+ actions.setFeedPaging(
+ (response.has_more ?? false) && response.results.length > 0,
+ response.as_of ?? null,
+ offset + response.results.length
+ )
+ posthog.capture('replay_vision_watch_feed_page_loaded', {
+ ...servedListProperties(values.feedLoad, page),
+ offset,
+ clip_count: page.length,
+ has_more: response.has_more,
+ })
+ return [...(values.feedItems ?? []), ...page]
+ },
},
],
})),
@@ -309,6 +397,36 @@ export const watchFeedLogic = kea<watchFeedLogicType>([
null as WatchFeedLoad | null,
{
setFeedLoad: (_, { load }) => load,
+ extendFeedLoad: (_, { load }) => load,
+ },
+ ],
+ feedHasMore: [
+ false,
+ {
+ loadFeed: () => false,
+ setFeedPaging: (_, { hasMore }) => hasMore,
+ },
+ ],
+ feedAsOf: [
+ null as string | null,
+ {
+ loadFeed: () => null,
+ setFeedPaging: (_, { asOf }) => asOf,
+ },
+ ],
+ feedServed: [
+ 0,
+ {
+ loadFeed: () => 0,
+ setFeedPaging: (_, { served }) => served,
+ },
+ ],
+ feedMoreFailed: [
+ false,
+ {
+ loadFeed: () => false,
+ loadMoreFeed: () => false,
+ loadMoreFeedFailure: () => true,
},
],
dateFrom: [
@@ -395,6 +513,9 @@ export const watchFeedLogic = kea<watchFeedLogicType>([
setFeedLoad: ({ load }) => {
cache.impressions.start(load)
},
+ extendFeedLoad: ({ load }) => {
+ cache.impressions.extend(load)
+ },
cardSeen: ({ item, position }) => {
cache.impressions.add(item, position)
},
@@ -566,7 +687,9 @@ export const watchFeedLogic = kea<watchFeedLogicType>([
// The tab content mounts only while the tab is active, so mount is the fetch signal.
afterMount(({ actions, cache }) => {
- cache.impressions = new WatchFeedImpressions('feed_tab')
+ cache.impressions = new WatchFeedImpressions('feed_tab', (load, ids) =>
+ recordFeedImpressions(load.feedLoadId, ids)
+ )
// Not paused on hide: hiding is when the batch must go out.
cache.disposables.add(cache.impressions.flushOnHideAndUnmount, 'impressionsFlush', { pauseOnPageHidden: false })
actions.loadFeed()
diff --git a/products/replay_vision/frontend/replay_scanners/watchFeedTelemetry.ts b/products/replay_vision/frontend/replay_scanners/watchFeedTelemetry.ts
index d92f0b502aba..6a625cf69d76 100644
--- a/products/replay_vision/frontend/replay_scanners/watchFeedTelemetry.ts
+++ b/products/replay_vision/frontend/replay_scanners/watchFeedTelemetry.ts
@@ -22,6 +22,16 @@ export function newWatchFeedLoad(ranker: string | null | undefined, items: Watch
}
}
+/** The same load with a later page of unseen cards appended, each placed after every card served so far. */
+export function withNextPage(load: WatchFeedLoad, items: WatchFeedItemApi[]): WatchFeedLoad {
+ const served = Object.keys(load.positions).length
+ const positions = { ...load.positions }
+ items.forEach((item, index) => {
+ positions[item.observation.id] = served + index
+ })
+ return { ...load, positions }
+}
+
function servedPosition(load: WatchFeedLoad | null, item: WatchFeedItemApi, position: number): number {
return load?.positions[item.observation.id] ?? position
}
@@ -64,7 +74,11 @@ export class WatchFeedImpressions {
private reported = new Set<string>()
private load: WatchFeedLoad | null = null
- constructor(private readonly surface: string) {}
+ /** `onFlush` receives each batch of newly seen cards, for anything besides analytics that counts them. */
+ constructor(
+ private readonly surface: string,
+ private readonly onFlush?: (load: WatchFeedLoad, observationIds: string[]) => void
+ ) {}
start(load: WatchFeedLoad | null): void {
this.flush()
@@ -72,6 +86,11 @@ export class WatchFeedImpressions {
this.load = load
}
+ /** Takes a later page of the same load without reporting its cards again. */
+ extend(load: WatchFeedLoad): void {
+ this.load = load
+ }
+
add(item: WatchFeedItemApi, position: number): void {
if (!this.seen.has(item.observation.id) && !this.reported.has(item.observation.id)) {
this.seen.set(item.observation.id, {
@@ -92,6 +111,9 @@ export class WatchFeedImpressions {
count: this.seen.size,
impressions: Array.from(this.seen, ([observation_id, card]) => ({ observation_id, ...card })),
})
+ if (this.load) {
+ this.onFlush?.(this.load, Array.from(this.seen.keys()))
+ }
for (const observationId of this.seen.keys()) {
this.reported.add(observationId)
}
diff --git a/products/replay_vision/frontend/replay_scanners/watchPicksLogic.ts b/products/replay_vision/frontend/replay_scanners/watchPicksLogic.ts
index 34d8aaa2b479..b19178925cf9 100644
--- a/products/replay_vision/frontend/replay_scanners/watchPicksLogic.ts
+++ b/products/replay_vision/frontend/replay_scanners/watchPicksLogic.ts
@@ -31,6 +31,7 @@ import { SessionRecordingSidebarTab } from '~/types'
import { visionObservationsViewedCreate, visionScannersWatchFeedRetrieve } from '../generated/api'
import type { WatchFeedItemApi } from '../generated/api.schemas'
import { observationKeyMomentMs, watchStartSeconds } from './components/WatchFeedCard'
+import { recordFeedImpressions } from './watchFeedImpressionCounts'
import {
type WatchFeedLoad,
WatchFeedImpressions,
@@ -355,7 +356,7 @@ export const watchPicksLogic: LogicWrapper<watchPicksLogicType> = kea<watchPicks
}),
afterMount(({ actions, cache }) => {
- cache.impressions = new WatchFeedImpressions('list')
+ cache.impressions = new WatchFeedImpressions('list', (load, ids) => recordFeedImpressions(load.feedLoadId, ids))
// Not paused on hide: hiding is when the batch must go out.
cache.disposables.add(cache.impressions.flushOnHideAndUnmount, 'impressionsFlush', { pauseOnPageHidden: false })
actions.loadPicks()
diff --git a/products/replay_vision/package.json b/products/replay_vision/package.json
index d14db704b4dc..a1276dfce2ab 100644
--- a/products/replay_vision/package.json
+++ b/products/replay_vision/package.json
@@ -12,7 +12,8 @@
"kea-router": "catalog:",
"kea-subscriptions": "catalog:",
"lodash.uniqby": "^4.7.0",
- "posthog-js": "catalog:"
+ "posthog-js": "catalog:",
+ "react-intersection-observer": "^9.5.3"
},
"devDependencies": {
"@testing-library/react": "^14.3.1",
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 395b50059e0b..3d350dd26cc1 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -120371,6 +120371,20 @@ export namespace Schemas {
Revenue: 'revenue',
} as const;
+ /**
+ * Body of POST /vision/scanners/watch_feed_impressions/.
+ */
+ export interface WatchFeedImpressions {
+ /** The client's id for the feed load that showed these cards. One load counts each card once. */
+ feed_load_id: string;
+ /**
+ * Observations the calling user saw in What to watch, at most 100. Ids the user cannot read are ignored.
+ * @minItems 1
+ * @maxItems 100
+ */
+ observation_ids: string[];
+ }
+
/**
* * `signal_emitted` - Signal Emitted
* * `unusual_verdict` - Unusual Verdict
@@ -120543,6 +120557,10 @@ export namespace Schemas {
export interface WatchFeedResponse {
/** Succeeded observations in the window worth watching, most interesting first, each carrying the reason it ranked. Every observation that carries a finding is returned; observations that carry none (`unviewed_recent`, `recent`) are returned only to pad a near-empty feed to three items, so a quiet window answers with a handful of rows rather than a full page of newest clips. */
results: WatchFeedItem[];
+ /** Whether a later page holds more ranked items. Request it with `offset` set past this page. */
+ has_more: boolean;
+ /** The time this feed ranks as of. Send it back with later pages. */
+ as_of: string;
/** Which ranker ordered this feed: `jev` ranks on the decision model's cached judgments, `embedding` on the rank the watch rank consumer wrote from the observation's embedding, `weighted-score` on the deterministic blend. The arm is decided server-side per team, so clients read it from here rather than evaluating the flag themselves.
*
* * `weighted-score` - Weighted Score
@@ -139649,6 +139667,10 @@ export namespace Schemas {
};
export type VisionScannersWatchFeedRetrieveParams = {
+ /**
+ * The time the first page loaded. Later pages rank as of that time: observations, views, dismissals and impressions after it are ignored, so pages do not shift under a reader who scrolls.
+ */
+ as_of?: string;
/**
* Comma-separated user IDs. Restrict the feed to observations from scanners those users created.
* @minLength 1
@@ -139665,11 +139687,17 @@ export namespace Schemas {
*/
date_to?: string;
/**
- * Ceiling on feed items to return, at most 50. The feed is bounded, not paginated, and routinely returns far fewer: a window is not padded to this number with clips that carry no finding.
+ * Ceiling on feed items to return per page, at most 50. A page routinely returns fewer: a window is not padded to this number with clips that carry no finding.
* @minimum 1
* @maximum 50
*/
limit?: number;
+ /**
+ * How many ranked items to skip. Send `as_of` with every page after the first.
+ * @minimum 0
+ * @maximum 1000
+ */
+ offset?: number;
/**
* Comma-separated scanner UUIDs to restrict the feed to. Defaults to every scanner you can read.
* @minLength 1
diff --git a/services/mcp/src/generated/replay_vision/api.ts b/services/mcp/src/generated/replay_vision/api.ts
index b885f0bc8f8c..5a64cbd4821a 100644
--- a/services/mcp/src/generated/replay_vision/api.ts
+++ b/services/mcp/src/generated/replay_vision/api.ts
@@ -2362,7 +2362,17 @@ export const visionScannersWatchFeedRetrieveQueryDateFromDefault = `-7d`
export const visionScannersWatchFeedRetrieveQueryLimitDefault = 20
export const visionScannersWatchFeedRetrieveQueryLimitMax = 50
+export const visionScannersWatchFeedRetrieveQueryOffsetDefault = 0
+export const visionScannersWatchFeedRetrieveQueryOffsetMin = 0
+export const visionScannersWatchFeedRetrieveQueryOffsetMax = 1000
+
export const VisionScannersWatchFeedRetrieveQueryParams = () => zod.object({
+ as_of: zod.iso
+ .datetime({ offset: true })
+ .optional()
+ .describe(
+ 'The time the first page loaded. Later pages rank as of that time: observations, views, dismissals and impressions after it are ignored, so pages do not shift under a reader who scrolls.'
+ ),
created_by: zod
.string()
.min(1)
@@ -2388,8 +2398,14 @@ export const VisionScannersWatchFeedRetrieveQueryParams = () => zod.object({
.max(visionScannersWatchFeedRetrieveQueryLimitMax)
.default(visionScannersWatchFeedRetrieveQueryLimitDefault)
.describe(
- 'Ceiling on feed items to return, at most 50. The feed is bounded, not paginated, and routinely returns far fewer: a window is not padded to this number with clips that carry no finding.'
+ 'Ceiling on feed items to return per page, at most 50. A page routinely returns fewer: a window is not padded to this number with clips that carry no finding.'
),
+ offset: zod
+ .number()
+ .min(visionScannersWatchFeedRetrieveQueryOffsetMin)
+ .max(visionScannersWatchFeedRetrieveQueryOffsetMax)
+ .default(visionScannersWatchFeedRetrieveQueryOffsetDefault)
+ .describe('How many ranked items to skip. Send `as_of` with every page after the first.'),
scanner_ids: zod
.string()
.min(1)
diff --git a/services/mcp/src/tools/generated/replay_vision.ts b/services/mcp/src/tools/generated/replay_vision.ts
index 05edd22861d9..a8fbbdb08abd 100644
--- a/services/mcp/src/tools/generated/replay_vision.ts
+++ b/services/mcp/src/tools/generated/replay_vision.ts
@@ -1690,10 +1690,12 @@ const visionScannersWatchFeed = (): ToolBase<
method: 'GET',
path: `/api/projects/${encodeURIComponent(String(projectId))}/vision/scanners/watch_feed/`,
query: {
+ as_of: params.as_of,
created_by: params.created_by,
date_from: params.date_from,
date_to: params.date_to,
limit: params.limit,
+ offset: params.offset,
scanner_ids: params.scanner_ids,
scanner_type: params.scanner_type,
search: params.search,
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/vision-scanners-watch-feed.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/vision-scanners-watch-feed.json
index aeb437aa28a8..95256ddcece3 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/vision-scanners-watch-feed.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/vision-scanners-watch-feed.json
@@ -1,6 +1,12 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
+ "as_of": {
+ "description": "The time the first page loaded. Later pages rank as of that time: observations, views, dismissals and impressions after it are ignored, so pages do not shift under a reader who scrolls.",
+ "format": "date-time",
+ "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d+)?)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$",
+ "type": "string"
+ },
"created_by": {
"description": "Comma-separated user IDs. Restrict the feed to observations from scanners those users created.",
"minLength": 1,
@@ -19,11 +25,18 @@
},
"limit": {
"default": 20,
- "description": "Ceiling on feed items to return, at most 50. The feed is bounded, not paginated, and routinely returns far fewer: a window is not padded to this number with clips that carry no finding.",
+ "description": "Ceiling on feed items to return per page, at most 50. A page routinely returns fewer: a window is not padded to this number with clips that carry no finding.",
"maximum": 50,
"minimum": 1,
"type": "number"
},
+ "offset": {
+ "default": 0,
+ "description": "How many ranked items to skip. Send `as_of` with every page after the first.",
+ "maximum": 1000,
+ "minimum": 0,
+ "type": "number"
+ },
"scanner_ids": {
"description": "Comma-separated scanner UUIDs to restrict the feed to. Defaults to every scanner you can read.",
"minLength": 1,