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.

fix(replay-vision): keep the session's variant out of the experiment scanner prompt (#116534)

Kim Svatos Dugan0199dff09619
diff --git a/products/replay_vision/backend/temporal/activities/call_scanner_provider.py b/products/replay_vision/backend/temporal/activities/call_scanner_provider.py
index 67419b2a4c7c..79a8a1353207 100644
--- a/products/replay_vision/backend/temporal/activities/call_scanner_provider.py
+++ b/products/replay_vision/backend/temporal/activities/call_scanner_provider.py
@@ -415,31 +415,30 @@ def _is_taglike(slug: str) -> bool:
async def _apply_experiment_scan_context(scanner: BaseScanner, inputs: CallScannerProviderInputs) -> BaseScanner:
- """Give an experiment scanner the variant and experiment description the workflow resolved.
+ """Give an experiment scanner the experiment description the workflow resolved.
Scan-time context, never persisted (see `ExperimentScanner`). A no-op for the other types.
- When the inputs carry neither field, the source observation's persisted attribution stands in.
- Best effort there: a lookup failure must not fail the scan, and a row without attribution simply
- has nothing persisted to inject."""
+ The session's variant is never injected: the prompt stays blind to it, and the workflow stores
+ it beside the model output. When the inputs carry no attribution, the source observation's
+ persisted attribution stands in. Best effort there: a lookup failure must not fail the scan,
+ and a row without attribution simply has nothing persisted to inject."""
if not isinstance(scanner, ExperimentScanner):
return scanner
- variant, context = inputs.experiment_variant, inputs.experiment_context
- if variant is None and context is None:
+ context = inputs.experiment_context
+ if context is None and inputs.experiment_variant is None:
try:
- variant, context = await sync_to_async(_load_persisted_experiment_context)(
+ context = await sync_to_async(_load_persisted_experiment_context)(
inputs.observation_id, inputs.team_id, scanner.experiment_id
)
except Exception:
logger.warning("replay_vision.call_scanner_provider.experiment_context_fallback_failed", exc_info=True)
return scanner
- if variant is None and context is None:
- return scanner
- return scanner.model_copy(update={"experiment_context": context, "session_variant": variant})
+ if context is None:
+ return scanner
+ return scanner.model_copy(update={"experiment_context": context})
-def _load_persisted_experiment_context(
- observation_id: UUID, team_id: int, experiment_id: int
-) -> tuple[str | None, dict[str, Any] | None]:
+def _load_persisted_experiment_context(observation_id: UUID, team_id: int, experiment_id: int) -> dict[str, Any] | None:
# Deferred: the experiments replay facade pulls in the recordings query modules, which circle
# back into this package's importers.
from products.experiments.backend.facade.replay import experiment_prompt_context # noqa: PLC0415
@@ -451,9 +450,9 @@ def _load_persisted_experiment_context(
)
variant = result.get("experiment_variant") if isinstance(result, dict) else None
if not isinstance(variant, str):
- return None, None
+ return None
context = experiment_prompt_context(Team.objects.get(pk=team_id), experiment_id=experiment_id)
- return variant, dataclasses.asdict(context) if context is not None else None
+ return dataclasses.asdict(context) if context is not None else None
def apply_known_freeform_tags(scanner: BaseScanner, tags: list[str]) -> BaseScanner:
diff --git a/products/replay_vision/backend/temporal/scanners/experiment.py b/products/replay_vision/backend/temporal/scanners/experiment.py
index 23a21b481810..c6c257503a7c 100644
--- a/products/replay_vision/backend/temporal/scanners/experiment.py
+++ b/products/replay_vision/backend/temporal/scanners/experiment.py
@@ -1,8 +1,9 @@
"""Experiment scanner: a summarizer that watches one experiment's exposed sessions.
-The type is the summarizer with experiment context: the prompt names the experiment, the change
-under test, and the session's variant, so the summary can speak to the change rather than guess
-at it. Which sessions it watches comes from `experiment_id`/`variants` in the scanner config,
+The type is the summarizer with experiment context: the prompt names the experiment and the change
+under test, so the summary can speak to the change rather than guess at it. The prompt never names
+the session's variant: a model told the variant narrates what that variant should show, not what
+the recording shows, which also hides a broken flag gate. Which sessions it watches comes from `experiment_id`/`variants` in the scanner config,
never from the persisted recordings query (see `ReplayScanner.experiment_scope`).
"""
@@ -45,18 +46,13 @@ class ExperimentScanner(SummarizerScanner, frozen=True):
description="Saved disabled on a draft experiment; turned on when the experiment launches.",
)
# Scan-time context rather than persisted config, injected per scan by the apply workflow
- # (like the classifier's `known_freeform_tags`). `exclude=True` keeps both out of dumps, so
- # they can never leak into a stored scanner_config or snapshot.
+ # (like the classifier's `known_freeform_tags`). `exclude=True` keeps it out of dumps, so it
+ # can never leak into a stored scanner_config or snapshot.
experiment_context: dict[str, Any] | None = Field(default=None, exclude=True)
- session_variant: str | None = Field(default=None, exclude=True)
- session_fields: ClassVar[frozenset[str]] = SummarizerScanner.session_fields | {
- "experiment_context",
- "session_variant",
- }
+ session_fields: ClassVar[frozenset[str]] = SummarizerScanner.session_fields | {"experiment_context"}
def prompt_context(self) -> dict[str, Any]:
return {
**super().prompt_context(),
"experiment": self.experiment_context,
- "session_variant": self.session_variant,
}
diff --git a/products/replay_vision/backend/temporal/scanners/prompts/experiment_step.jinja b/products/replay_vision/backend/temporal/scanners/prompts/experiment_step.jinja
index d68c04da77d4..cd85cc062fe7 100644
--- a/products/replay_vision/backend/temporal/scanners/prompts/experiment_step.jinja
+++ b/products/replay_vision/backend/temporal/scanners/prompts/experiment_step.jinja
@@ -3,12 +3,12 @@
The experiment under test:
- Name: {{ experiment.name }}{% if experiment.description %}
- Hypothesis: {{ experiment.description }}{% endif %}
-- Feature flag: {{ experiment.feature_flag_key }}
-- Variants:{% for variant in experiment.variants %}
- - `{{ variant.key }}` ({{ variant.rollout_percentage }}% rollout){% if variant.description %}: {{ variant.description }}{% endif %}{% endfor %}{% if experiment.primary_metric_names %}
-- Primary metrics: {{ experiment.primary_metric_names | join(', ') }}{% endif %}{% endif %}{% if session_variant %}
+- Feature flag: {{ experiment.feature_flag_key }}{% set forms = experiment.variants | map(attribute='description') | select | list %}{% if forms %}
+- The changed surface can take these forms:{% for form in forms %}
+ - {{ form }}{% endfor %}{% endif %}{% if experiment.primary_metric_names %}
+- Primary metrics: {{ experiment.primary_metric_names | join(', ') }}{% endif %}{% endif %}
-This session's user was exposed to the `{{ session_variant }}` variant. Judge the session with that variant's experience in mind: describe how the user moves through the surface the experiment changes, and whether the variant's change helps, hinders, or goes unnoticed. Never guess at what another variant would have shown.{% endif %}{% if user_prompt %}
+You are not told which variant this user got. Describe the version of the changed surface that the recording shows and how the user moves through it, from what is on screen only. Never name or guess the user's variant or group (such as "control" or "test"), and never describe a feature the recording does not show. If the user never reaches the changed surface, say so.{% if user_prompt %}
Additional context from the team operating the product being recorded:
{{ user_prompt }}{% endif %}
diff --git a/products/replay_vision/backend/tests/test_call_scanner_provider.py b/products/replay_vision/backend/tests/test_call_scanner_provider.py
index 171a4028a469..90204a6803e0 100644
--- a/products/replay_vision/backend/tests/test_call_scanner_provider.py
+++ b/products/replay_vision/backend/tests/test_call_scanner_provider.py
@@ -630,8 +630,8 @@ async def test_video_cache_creation_is_best_effort() -> None:
@pytest.mark.asyncio
async def test_apply_experiment_scan_context_injects_into_experiment_scanners_only() -> None:
- # The injected fields are exclude=True, so this per-scan context is the only way the prompt
- # ever learns the variant; a broken injection silently degrades every experiment scan.
+ # The injected field is exclude=True, so this per-scan context is the only way the prompt
+ # ever learns the experiment; a broken injection silently degrades every experiment scan.
from uuid import uuid4
from products.replay_vision.backend.temporal.activities.call_scanner_provider import _apply_experiment_scan_context
@@ -658,9 +658,10 @@ async def test_apply_experiment_scan_context_injects_into_experiment_scanners_on
experiment_scanner = ExperimentScanner(prompt="p", experiment_id=42)
injected = await _apply_experiment_scan_context(experiment_scanner, inputs)
assert isinstance(injected, ExperimentScanner)
- assert injected.session_variant == "test"
assert injected.experiment_context is not None and injected.experiment_context["name"] == "Checkout CTA copy"
- assert "`test` variant" in injected.core_steps()[0].instruction
+ instruction = injected.core_steps()[0].instruction
+ assert "Checkout CTA copy" in instruction
+ assert "`test`" not in instruction
monitor = MonitorScanner(prompt="p")
assert await _apply_experiment_scan_context(monitor, inputs) is monitor
@@ -681,9 +682,14 @@ async def test_evaluation_calls_fall_back_to_the_persisted_attribution() -> None
)
with patch(
"products.replay_vision.backend.temporal.activities.call_scanner_provider._load_persisted_experiment_context",
- return_value=("control", None),
+ return_value={
+ "name": "Checkout CTA copy",
+ "description": "",
+ "feature_flag_key": "checkout-cta",
+ "variants": [],
+ },
):
injected = await _apply_experiment_scan_context(ExperimentScanner(prompt="p", experiment_id=42), inputs)
assert isinstance(injected, ExperimentScanner)
- assert injected.session_variant == "control"
+ assert injected.experiment_context is not None and injected.experiment_context["name"] == "Checkout CTA copy"
diff --git a/products/replay_vision/backend/tests/test_scanners.py b/products/replay_vision/backend/tests/test_scanners.py
index b04b126f0a0b..f731f0183b6d 100644
--- a/products/replay_vision/backend/tests/test_scanners.py
+++ b/products/replay_vision/backend/tests/test_scanners.py
@@ -1090,7 +1090,7 @@ class TestExperimentScanner:
assert "watch the checkout change" in instruction
assert "(t " in instruction
- def test_core_step_carries_injected_experiment_context_and_variant(self) -> None:
+ def test_core_step_carries_injected_experiment_context_but_never_a_variant_key(self) -> None:
scanner = self._scanner().model_copy(
update={
"experiment_context": {
@@ -1103,14 +1103,15 @@ class TestExperimentScanner:
],
"primary_metric_names": ["Purchases"],
},
- "session_variant": "test",
}
)
instruction = scanner.core_steps()[0].instruction
assert "Checkout CTA copy" in instruction
assert "checkout-cta" in instruction
assert "Purchases" in instruction
- assert "`test` variant" in instruction
+ assert "One-click" in instruction
+ assert "`control`" not in instruction
+ assert "`test`" not in instruction
def test_output_round_trip_keeps_the_summarizer_shape(self) -> None:
out = ExperimentOutput(title="Faster checkout", summary="They breezed through.", confidence=0.9)