:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(replay-vision): stop scanners inventing findings on irrelevant sessions (#74777)
Arno Hillen77f1222f11fd
diff --git a/products/replay_vision/backend/temporal/scanners/prompts/classifier_step.jinja b/products/replay_vision/backend/temporal/scanners/prompts/classifier_step.jinjaindex 029c10713a1a..737684adac8d 100644--- a/products/replay_vision/backend/temporal/scanners/prompts/classifier_step.jinja+++ b/products/replay_vision/backend/temporal/scanners/prompts/classifier_step.jinja@@ -1,6 +1,6 @@{% from "_macros.jinja" import cite_in %}Categorize this session according to: {{ user_prompt }}-In `reasoning`, work through what the session shows that bears on this; then choose tags from this fixed vocabulary: [{{ vocabulary }}]. {% if multi_label %}Pick every tag that applies.{% else %}Pick exactly one tag.{% endif %}+In `reasoning`, work through what the session shows that bears on this; then choose tags from this fixed vocabulary: [{{ vocabulary }}]. {% if multi_label %}Pick every tag that applies.{% else %}Pick exactly one tag.{% endif %} A tag is required even when the session never shows what the criterion describes, so in that case pick the least wrong one and follow the relevance rules: lead the `reasoning` with the fact that the session doesn't show it, and keep `confidence` low.{% if allow_freeform_tags %}The fixed vocabulary above is authoritative: whenever a fixed tag fits — even loosely — use it rather than coining a freeform one (e.g. with `create new scanner` in the vocabulary, pick it, not `new_scanner`). Reserve `tags_freeform` (up to {{ max_freeform_tags }}) for concepts no fixed tag covers at all, naming each with one canonical lowercase snake_case identifier (e.g. `password_reset`, `slow_checkout`). Leave it empty when the fixed tags already say everything that matters.{% if known_freeform_tags %}diff --git a/products/replay_vision/backend/temporal/scanners/prompts/preamble.jinja b/products/replay_vision/backend/temporal/scanners/prompts/preamble.jinjaindex 3169f9ecfaf5..c183b86fc284 100644--- a/products/replay_vision/backend/temporal/scanners/prompts/preamble.jinja+++ b/products/replay_vision/backend/temporal/scanners/prompts/preamble.jinja@@ -34,7 +34,11 @@ The customer wrote these descriptions for their custom analytics events, listed{% for name, description in event_descriptions.items() %}- `{{ name }}`: {{ description }}{% endfor %}</event_taxonomy>-{% endif %}<gestures>+{% endif %}<relevance>+The criterion you're given often targets a specific flow, screen, or behavior, and the recording you're watching may never reach it. That is a normal outcome of broad recording filters, not something to explain away: open your `reasoning` by saying the session doesn't show it, and keep `confidence` at or below 0.3. Never stretch an unrelated part of the session to fit the criterion, and never reason about what the user would have done had they gone there.+</relevance>++<gestures>Touch and browser gestures often leave no click events: a back-swipe navigates without any click, and scroll flicks can register as taps. On touch devices a single `$dead_click` is often a scroll gesture rather than frustration, so weigh it lower than on desktop unless it repeats on the same element. A burst of rapid, aimless taps and swipes with no coherent goal is more likely accidental input (an unlocked phone in a pocket) than a frustrated user.</gestures>diff --git a/products/replay_vision/backend/temporal/scanners/prompts/scorer_step.jinja b/products/replay_vision/backend/temporal/scanners/prompts/scorer_step.jinjaindex 422d33cdf1af..3a9410b2765e 100644--- a/products/replay_vision/backend/temporal/scanners/prompts/scorer_step.jinja+++ b/products/replay_vision/backend/temporal/scanners/prompts/scorer_step.jinja@@ -2,6 +2,6 @@Before scoring, verify the moments that drive your score with `get_events_around`: what the events record at a moment outranks a visual impression of it, and a score toward either end of the scale needs event-checked moments behind it, not just how the session looks.-In `reasoning`, work through what you observed in the session that bears on this criterion, noting what the events confirmed or contradicted; then set `score` on {% if scale_label %}the '{{ scale_label }}' scale{% else %}the scale{% endif %} from {{ scale_min }} to {{ scale_max }}.+In `reasoning`, work through what you observed in the session that bears on this criterion, noting what the events confirmed or contradicted; then set `score` on {% if scale_label %}the '{{ scale_label }}' scale{% else %}the scale{% endif %} from {{ scale_min }} to {{ scale_max }}. A score is required even when the session never shows what the criterion describes, so in that case follow the relevance rules and stay away from both ends of the scale: an extreme score reads as a real finding, and there's nothing here to find.{{ cite_in('reasoning') }}diff --git a/products/replay_vision/backend/tests/test_scanners.py b/products/replay_vision/backend/tests/test_scanners.pyindex 955630e3c62f..faa17023ab24 100644--- a/products/replay_vision/backend/tests/test_scanners.py+++ b/products/replay_vision/backend/tests/test_scanners.py@@ -149,6 +149,14 @@ class TestPreamble:rendered = scanner_from_db(_build_replay_scanner()).preamble(team_name="Acme")assert "<navigation>" not in rendered+ def test_preamble_tells_the_model_what_to_do_when_the_criterion_does_not_apply(self) -> None:+ # A scanner with broad recording filters feeds sessions the prompt was never about. Without this the model+ # stretches an unrelated session to fit, which is the noise that burns a team's credits.+ rendered = scanner_from_db(_build_replay_scanner()).preamble(team_name="Acme")+ assert "<relevance>" in rendered+ assert "0.3" in rendered+ assert "never reach it" in rendered+def test_preamble_renders_product_context_as_data_not_instructions(self) -> None:rendered = scanner_from_db(_build_replay_scanner()).preamble(team_name="Acme", product_context="Acme sells rockets to coyotes."@@ -308,6 +316,15 @@ class TestClassifierScanner:assert "'a', 'b'" in instructionassert "exactly one tag" in instruction+ def test_core_step_handles_a_session_the_vocabulary_does_not_describe(self) -> None:+ # The response schema forces at least one tag, so the escape hatch has to be the reasoning and confidence.+ scanner = scanner_from_db(+ _build_replay_scanner(scanner_type=ScannerType.CLASSIFIER, scanner_config={"prompt": "x", "tags": ["a"]})+ )+ instruction = _core_instruction(scanner)+ assert "least wrong" in instruction+ assert "keep `confidence` low" in instruction+def test_validate_semantics_rejects_unknown_tag(self) -> None:scanner = scanner_from_db(_build_replay_scanner(@@ -549,6 +566,18 @@ class TestScorerScanner:# Extreme scores must be grounded in event-checked moments, not visual impressions.assert "get_events_around" in instruction+ def test_core_step_keeps_an_inapplicable_session_off_the_ends_of_the_scale(self) -> None:+ # A score is mandatory, so a session the criterion never applies to must not land on an extreme, where it+ # reads as a real finding (a pile of 0s on a frustration scanner looks like a great experience).+ scanner = scanner_from_db(+ _build_replay_scanner(+ scanner_type=ScannerType.SCORER,+ scanner_config={"prompt": "rate", "scale": {"min": 0, "max": 10}},+ )+ )+ instruction = _core_instruction(scanner)+ assert "stay away from both ends of the scale" in instruction+def test_llm_response_schema_carries_range_constraint(self) -> None:scanner = scanner_from_db(_build_replay_scanner(