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): say why a scan batch produced nothing (#96027)

Arno Hillen3697dc49e9b7
diff --git a/products/replay_vision/backend/api/scanners.py b/products/replay_vision/backend/api/scanners.py
index e1db0febbd66..81be31c6941c 100644
--- a/products/replay_vision/backend/api/scanners.py
+++ b/products/replay_vision/backend/api/scanners.py
@@ -113,7 +113,12 @@ from products.replay_vision.backend.scanner_config import (
scanner_config_error,
)
from products.replay_vision.backend.scanner_draft import DraftError, draft_scanner_from_goal, draft_scanner_from_goal_v2
-from products.replay_vision.backend.scanning import MAX_SESSIONS_PER_SCAN, run_inline_scan, scan_existing_scanner
+from products.replay_vision.backend.scanning import (
+ MAX_SESSIONS_PER_SCAN,
+ run_inline_scan,
+ scan_existing_scanner,
+ scan_outcome_counts,
+)
from products.replay_vision.backend.session_limits import MAX_SESSION_ID_LENGTH
from products.replay_vision.backend.tag_suggestions import SuggestionError, suggest_classifier_tags
from products.replay_vision.backend.temporal.constants import VISION_SIGNALS_SOURCE_PRODUCT, VISION_SIGNALS_SOURCE_TYPE
@@ -1878,6 +1883,7 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, vi
"scanner_type": scanner.scanner_type,
"requested": len(session_ids),
"started": started,
+ **scan_outcome_counts(results),
},
team=self.team,
request=request,
@@ -1940,6 +1946,22 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, vi
)
if scan.scanner is None:
# Nothing started and nothing already existed, so there is no id to read results through.
+ # The request still happened, and reporting it only when something started would drop the
+ # fully-refused batches from the request count and bias every rate built on it.
+ report_user_action(
+ user,
+ "replay_vision_inline_scan_requested",
+ {
+ "scan_id": None,
+ "scanner_type": scanner_type,
+ "model": model,
+ "requested": len(session_ids),
+ "started": 0,
+ **scan_outcome_counts(scan.results),
+ },
+ team=self.team,
+ request=request,
+ )
# Key off the outcomes: the in-flight cap can bind here too, and that is not exhaustion.
if any(result["scan_outcome"] == "skipped_quota" for result in scan.results):
self._report_quota_exhausted(None, "inline")
@@ -1958,6 +1980,7 @@ class ReplayScannerViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, vi
"model": scanner.model,
"requested": len(session_ids),
"started": started,
+ **scan_outcome_counts(results),
},
team=self.team,
request=request,
diff --git a/products/replay_vision/backend/scanning.py b/products/replay_vision/backend/scanning.py
index fff155260de4..b95525ca0011 100644
--- a/products/replay_vision/backend/scanning.py
+++ b/products/replay_vision/backend/scanning.py
@@ -38,6 +38,34 @@ from products.replay_vision.backend.temporal.constants import (
# One page of recordings. Above this the in-flight caps bind long before the batch does.
MAX_SESSIONS_PER_SCAN = 200
+# Every outcome `start_observations` can report, mirroring the API's `scan_outcome` choices. Listed so a
+# batch reports all of them and a zero is a measured zero rather than a key nobody wrote.
+SCAN_OUTCOMES: tuple[str, ...] = (
+ "started",
+ "already_running",
+ "already_scanned",
+ "skipped_limit",
+ "skipped_quota",
+ "skipped_scanner_limit",
+ "failed",
+)
+
+
+def scan_outcome_counts(results: list[dict[str, str]]) -> dict[str, int]:
+ """Per-outcome counts for one batch, shaped as analytics properties.
+
+ A batch reports how many sessions it asked for and how many started, which says how many produced
+ nothing but never why. A reused answer costs nothing and is a cache hit; a quota skip is a customer
+ hitting a wall. Those need opposite responses and are indistinguishable today. The counts sum to the
+ number of sessions requested.
+ """
+ counts: dict[str, int] = dict.fromkeys(SCAN_OUTCOMES, 0)
+ for result in results:
+ outcome = str(result["scan_outcome"])
+ # An outcome outside the known set still counts, so the sum keeps holding if one is added.
+ counts[outcome] = counts.get(outcome, 0) + 1
+ return {f"outcome_{outcome}": count for outcome, count in counts.items()}
+
@dataclass(frozen=True, kw_only=True)
class ScanHeadroom:
diff --git a/products/replay_vision/backend/tests/test_api.py b/products/replay_vision/backend/tests/test_api.py
index b4687134f845..f3b1679e31bf 100644
--- a/products/replay_vision/backend/tests/test_api.py
+++ b/products/replay_vision/backend/tests/test_api.py
@@ -2505,6 +2505,16 @@ class TestBulkObserveAction(_VisionAPITestCase):
events = [call.args[1] for call in report.call_args_list]
self.assertEqual(events, ["replay_vision_bulk_scan_started", "replay_vision_quota_exhausted"])
self.assertEqual(report.call_args.args[2]["trigger"], "bulk")
+ # `requested` minus `started` says two sessions produced nothing but never that quota was why.
+ bulk_properties = report.call_args_list[0].args[2]
+ self.assertEqual(bulk_properties["outcome_skipped_quota"], 1)
+ self.assertEqual(bulk_properties["outcome_started"], 1)
+ # Every outcome is reported, so an untaken path is a measured zero rather than a missing key.
+ self.assertEqual(bulk_properties["outcome_already_scanned"], 0)
+ self.assertEqual(
+ sum(value for key, value in bulk_properties.items() if key.startswith("outcome_")),
+ bulk_properties["requested"],
+ )
def test_bulk_observe_reports_the_scanner_limit_as_the_skip_reason(
self, mock_sync_connect: MagicMock, mock_async_to_sync: MagicMock
@@ -3760,6 +3770,28 @@ class TestInlineScanAction(_VisionAPITestCase):
completed_at=timezone.now(),
)
+ def test_a_fully_refused_scan_still_reports_the_request(
+ self, mock_sync_connect: MagicMock, mock_async_to_sync: MagicMock
+ ) -> None:
+ # A batch where nothing starts mints no scanner, but the request still happened. The endpoint must
+ # report it, or the refused batches drop out of the request count and bias every rate built on it.
+ mock_sync_connect.return_value = MagicMock()
+ mock_async_to_sync.return_value = MagicMock()
+
+ with patch("products.replay_vision.backend.quota.MONTHLY_CREDIT_QUOTA", 0):
+ with patch("products.replay_vision.backend.api.scanners.report_user_action") as report:
+ resp = self._scan()
+
+ self.assertEqual(resp.status_code, 202, resp.json())
+ self.assertIsNone(resp.json()["scan_id"])
+ events = [call.args[1] for call in report.call_args_list]
+ self.assertIn("replay_vision_inline_scan_requested", events)
+ properties = report.call_args_list[events.index("replay_vision_inline_scan_requested")].args[2]
+ self.assertIsNone(properties["scan_id"])
+ self.assertEqual(properties["started"], 0)
+ self.assertEqual(properties["requested"], 1)
+ self.assertEqual(properties["outcome_skipped_quota"], 1)
+
def test_same_prompt_reuses_one_scan_and_a_different_prompt_gets_its_own(
self, mock_sync_connect: MagicMock, mock_async_to_sync: MagicMock
) -> None: