:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(replay-vision): deepen the goal-based scanner draft (#103231)
Kim Svatos Dugan571de38c1471
diff --git a/products/replay_vision/backend/queries/event_volume.py b/products/replay_vision/backend/queries/event_volume.pynew file mode 100644index 000000000000..a672eed3ed12--- /dev/null+++ b/products/replay_vision/backend/queries/event_volume.py@@ -0,0 +1,78 @@+import datetime as dt+from collections.abc import Sequence++from posthog.hogql import ast+from posthog.hogql.constants import HogQLGlobalSettings+from posthog.hogql.parser import parse_select+from posthog.hogql.query import execute_hogql_query++from posthog.clickhouse.client.connection import ClickHouseUser+from posthog.clickhouse.query_tagging import Feature, Product, tag_queries+from posthog.models import Team++# The same window the visited pages and the matched actions use, so one briefing can put a page, an+# action and an event next to each other and have their counts mean the same thing.+_EVENT_WINDOW_DAYS = 7+# Events are counted in one query, so the whole measurement shares this budget.+_EVENT_QUERY_MAX_EXECUTION_SECONDS = 5+++def recent_event_sessions(+ *,+ team: Team,+ event_names: Sequence[str],+ window_days: int = _EVENT_WINDOW_DAYS,+ ch_user: ClickHouseUser = ClickHouseUser.APP,+) -> dict[str, int]:+ """How many sessions each event fired in over a recent window, keyed by event name.++ An event definition only records that a name was seen, so ordering a briefing by `last_seen_at`+ ranks an event that fired twice yesterday above one that fires in every session. A model picking+ a recording filter needs the second one, and only a measured session count tells them apart.++ A name absent from the result fired in no session in the window, which is exactly the event a+ filter must not use: events AND with the rest of the filter, so a dead one takes the whole scan+ to zero.++ Counts sessions that fired the event, the same way action volume does, rather than only the+ sessions a scanner could watch. The two are then comparable to each other; both overstate what+ is scannable by the short sessions the sweep skips.++ Matches names exactly, because `event` is the third column of the events sort key and wrapping+ it would read the whole window. Callers pass the team's own spelling.++ Raises on query failure, so the caller decides whether a missing measurement is fatal.+ """+ names = list(dict.fromkeys(name for name in event_names if name))+ if not names:+ return {}++ window_start = dt.datetime.now(dt.UTC) - dt.timedelta(days=window_days)+ query = parse_select(+ """+ SELECT event, count(DISTINCT `$session_id`) AS sessions+ FROM events+ WHERE timestamp >= {window_start}+ AND event IN {names}+ AND notEmpty(`$session_id`)+ GROUP BY event+ """,+ placeholders={+ "window_start": ast.Constant(value=window_start),+ "names": ast.Constant(value=names),+ },+ )++ tag_queries(team_id=team.id, product=Product.REPLAY_VISION, feature=Feature.QUERY)+ response = execute_hogql_query(+ query=query,+ team=team,+ query_type="ReplayVisionEventVolumeQuery",+ # "throw", not "break": a partial aggregate reads as a busy event that went quiet, which is+ # the distinction this query exists to make.+ settings=HogQLGlobalSettings(+ max_execution_time=_EVENT_QUERY_MAX_EXECUTION_SECONDS, timeout_overflow_mode="throw"+ ),+ ch_user=ch_user,+ )+ return {str(name): int(sessions) for name, sessions in (response.results or [])}diff --git a/products/replay_vision/backend/scanner_draft.py b/products/replay_vision/backend/scanner_draft.pyindex 171d4db87f65..872cc7cbee92 100644--- a/products/replay_vision/backend/scanner_draft.py+++ b/products/replay_vision/backend/scanner_draft.py@@ -11,7 +11,7 @@ re-validates everything on save.import reimport uuid-from collections.abc import Sequence+from collections.abc import Collection, Sequencefrom dataclasses import dataclass, replacefrom typing import Any, Literal, cast@@ -48,6 +48,7 @@ from products.replay_vision.backend.models.replay_scanner import (apply_experiment_targeting,)from products.replay_vision.backend.queries.action_volume import recent_action_sessions+from products.replay_vision.backend.queries.event_volume import recent_event_sessionsfrom products.replay_vision.backend.queries.scanner_candidate_query import MIN_SAMPLING_RATE, SAMPLE_RATE_PRECISIONfrom products.replay_vision.backend.queries.scanner_volume_estimate import (PREVIEW_ESTIMATE_BUDGET,@@ -92,6 +93,10 @@ _SCANNER_GIST_CHARS = 200# more just zeroes out the match set._MAX_FILTER_SCREENS = 1_MAX_FILTER_EVENTS = 2+# A v2 draft can also OR its events (see `filter_events_match`), and a goal about "created or+# edited" names the several events that each mean the same thing, which two of them cannot cover.+# An AND filter still rarely wants more than one, so the extra room is for the OR case only.+_MAX_V2_FILTER_EVENTS = 3# Screens match via icontains, so a short pathname like "/" or "/en" matches nearly every URL:# it renders as a narrowing filter while narrowing nothing. Require this many non-slash# characters before a screen can ground a filter.@@ -326,6 +331,16 @@ class _MatchedAction:recent_sessions: int = 0+@dataclass(frozen=True, kw_only=True)+class _CandidateEvent:+ """One custom event offered to the model, with the volume that says whether it still fires."""++ name: str+ # Sessions the event fired in over the volume query's window, or None when the measurement+ # failed. None is rendered without a count rather than as a zero, which would read as dead.+ sessions: int | None = None++@dataclass(frozen=True, kw_only=True)class _MatchedCohort:"""One of the team's cohorts whose name matches the goal, with the ID a filter needs."""@@ -671,7 +686,9 @@ def _event_names_by_lower(team_id: int, candidates: Sequence[str]) -> dict[str,return {}-def _grounded_events(proposed: list[str], allowed: Sequence[str], cap: int, team_id: int | None) -> list[str]:+def _grounded_events(+ proposed: list[str], allowed: Sequence[str], cap: int, team_id: int | None, excluded: Collection[str] = ()+) -> list[str]:"""The proposed event filters worth keeping: verbatim members of the briefing list, plusproposals that are real events on the team by exact (case-insensitive) name.@@ -679,9 +696,15 @@ def _grounded_events(proposed: list[str], allowed: Sequence[str], cap: int, teamsample missed and the model copies it faithfully. List membership alone would drop it andsilently widen the scan. The definition lookup keeps `_grounded`'s anti-hallucination property,because an invented name matches no definition and still drops. Kept names come back in the- team's canonical casing, which is what the recordings query has to match."""+ team's canonical casing, which is what the recordings query has to match.++ `excluded` names are measured-dead events (0 sessions in the volume window). They drop before+ both paths, so the definition lookup cannot re-admit one whose only liveness signal is a+ non-null `last_seen_at` from long ago. Events AND with the rest of the filter, so one dead event+ takes the whole scan to zero."""allowed_set = set(allowed)- cleaned = list(dict.fromkeys(v for raw in proposed if (v := raw.strip())))+ excluded_lower = {e.lower() for e in excluded}+ cleaned = list(dict.fromkeys(v for raw in proposed if (v := raw.strip()) and v.lower() not in excluded_lower))unknown = [v for v in cleaned if v not in allowed_set]canonical = _event_names_by_lower(team_id, unknown) if unknown and team_id is not None else {}kept = (@@ -865,8 +888,16 @@ class _LlmDraftV2(BaseModel):default_factory=list,description="Custom events a session must contain to be worth scanning, each copied verbatim from the ""briefing's events list. Use when a specific action is the sharpest signal for the goal (e.g. an event "- "fired when a flow starts). Each event ANDs, with the other events and with the pages, so a session must "- "have all of them; keep to the one or two that define the flow, and leave empty when pages already capture it.",+ "fired when a flow starts). `filter_events_match` decides whether a session needs all of them or any of "+ "them; keep to the few that define the flow, and leave empty when pages already capture it.",+ )+ filter_events_match: Literal["all", "any"] = Field(+ default="all",+ description="How several `filter_events` combine. 'all' (the default) scans a session only when it "+ "contains every event, for events that are steps of one flow. 'any' scans a session containing any one "+ "of them, for events that are alternative ways of doing the same thing ('created or edited'). 'any' "+ "needs the events to be the whole filter: with pages, actions, a cohort or a property filter alongside "+ "them, everything ANDs together.",)filter_actions: list[str] = Field(default_factory=list,@@ -931,15 +962,34 @@ there, so a broad draft is the right answer, not a guess at what they meant.Pick the single type that best fits the goal, then draft the scanner:- name: short and specific, under 8 words.- description: one sentence saying what the scanner looks for.-- prompt: a direct, specific instruction grounded in behavior observable in a recording. Reference the- product's real pages and events where relevant so the scanner knows what to look at. Avoid vague- adjectives, multi-part questions, and data the model cannot see in a recording (revenue, account tier).- - monitor prompts state a yes/no question, what counts as a yes, and ask for a one-sentence reason.- - classifier prompts describe the dimension to categorize by; do NOT list the tags in the prompt- (the vocabulary is configured separately via `tags`).- - scorer prompts describe what a low score versus a high score means; the numeric scale is configured- separately via `scale_min`/`scale_max`.- - summarizer prompts say what the summary should focus on.+- prompt: the brief the scanner follows while watching ONE recording. Whoever reads it sees the video+ and nothing else - no account, no ticket, no idea what the goal was - so write the brief you would+ give an analyst on their first day, not a one-line question. Six to fifteen lines is the usual shape.+ Write it in this order:+ 1. Context: one line on what this product is and what the person in the recording is doing in it.+ 2. Where to look: the real pages, UI elements and events involved, named the way the product names+ them, so the scanner knows which moments of the recording decide the answer.+ 3. The decision rule, in the shape the scanner type needs (below).+ 4. Near misses: one to three things that look like a hit but are NOT one, said out loud. A scanner+ without these fires on everything that merely resembles its subject.+ 5. Evidence: answer only from what is visible on screen, cite the moment or the on-screen wording the+ answer rests on, and when the screen is masked, unreadable or the evidence is ambiguous, take the+ safe answer instead of guessing - a no, the neutral tag, the low end of the scale, or a summary+ that says what could not be seen. Recordings are downscaled video with masked inputs, so the+ failure to design against is a confident answer read off text the model could not really see.+ Avoid vague adjectives, multi-part questions, and anything a recording cannot show (revenue, account+ tier, who the person is).+ - monitor: one line stating the yes/no question, then the two to four concrete observable signals+ that each make it a yes, then the near misses that are a no. Close by asking for a one-sentence+ reason citing what was on screen.+ - classifier: name the dimension being categorized and say where the boundary falls between the two+ categories that are easiest to confuse, so the same behavior always lands on the same side. Do NOT+ list the tags in the prompt (the vocabulary is configured separately via `tags`).+ - scorer: describe what the bottom, the middle and the top of the scale each look like in observable+ terms, so two sessions with the same behavior get the same number; the numeric scale itself is+ configured separately via `scale_min`/`scale_max`.+ - summarizer: say what every summary must cover and in what order (what the person was trying to do,+ what the product did, where they got stuck, how it ended), and what to leave out.- Fill only the fields relevant to the chosen type; leave the rest at their defaults.- For monitors: set allow_inconclusive to true when many sessions won't even reach the flow the questionis about (e.g. a checkout question when most sessions never open checkout), so those sessions aren't@@ -957,12 +1007,26 @@ Pick the single type that best fits the goal, then draft the scanner:a goal about creation does not want. Use several pages only when the goal genuinely spans distinctpages (a checkout moving through cart, shipping, and payment), never to add a broader page around aspecific one.- - filter_events: when a specific action is the sharpest signal for the goal, pick the one or two- custom events that mark it, each copied EXACTLY from the briefing's events list. An event is often- more precise than a page for "did the user DO X" (e.g. an event fired when a flow starts).- - Prefer the single strongest signal. Events AND with each other and with the pages, so a session- must satisfy ALL of them: only combine when a session genuinely has both, or the filter matches- nothing. Anything not copied from the briefing's lists is discarded.+ - filter_events: when a specific action is the sharpest signal for the goal, pick up to three custom+ events that mark it, each copied EXACTLY from the briefing's events list. An event is often+ more precise than a page for "did the user DO X" (e.g. an event fired when a flow starts). Each+ event carries the sessions it fired in over the last 7 days: when two events fit the goal, take+ the one that actually fires, and never filter on one showing 0, which would take the scan to+ nothing.+ - filter_events_match decides how several events combine, and the wrong one silently empties the+ scan:+ - "all" (the default): the session must contain EVERY event. For events that are steps of one+ flow a single session really does hit.+ - "any": a session containing ANY of them is scanned. For events that are alternative ways of+ doing the SAME thing - created, edited or drafted the same object; signed up from three+ entrypoints. A goal phrased as "X or Y" is this case, and "all" would match almost nobody.+ "any" covers the events only, so it needs them to be the whole filter: when the draft also has+ pages, actions, a cohort or an event property filter, every part ANDs together as usual. Pick+ the events alone when the goal is "did they DO one of these things".+ - Prefer the single strongest signal. Events AND with the pages, and with each other unless+ filter_events_match is "any", so a session must satisfy ALL of them: only combine when a session+ genuinely has both, or the filter matches nothing. Anything not copied from the briefing's lists+ is discarded.- Leave both empty ONLY when the goal genuinely spans the whole product ("summarize what users dohere").- filter_event_properties: use ONLY when the goal names one specific thing its event cannot identify@@ -1033,7 +1097,7 @@ Output strictly matches the provided JSON schema."""def _build_user_content_v2(goal: str,- events: list[str],+ events: Sequence[_CandidateEvent],pages: Sequence[VisitedPath],*,scanners: list[_ExistingScanner] | None = None,@@ -1067,9 +1131,14 @@ def _build_user_content_v2(+ "\n- ".join(f"{p.pathname} ({p.sessions})" for p in pages))if events:+ # The session counts do for events what they already do for pages and actions: a name the+ # goal matches can belong to an event that has not fired in years, and only the count+ # separates it from the one that fires in every session.taxonomy_lines.append(- "The product's most active custom events (use to ground the prompt, and pick from these "- "for filter_events when an action is the sharpest signal):\n- " + "\n- ".join(events)+ "The product's custom events, the ones matching the goal first (sessions last 7 days; "+ "use to ground the prompt, and pick from these for filter_events when an action is the "+ "sharpest signal):\n- "+ + "\n- ".join(e.name if e.sessions is None else f"{e.name} ({e.sessions})" for e in events))lines.append("\n" + as_untrusted_data("product-data", taxonomy_lines, source="collected from product traffic"))if scanners:@@ -1353,6 +1422,30 @@ def _live_actions(team: Team, actions: list[_MatchedAction]) -> list[_MatchedActreturn live+def _measured_events(team: Team, names: Sequence[str]) -> list[_CandidateEvent]:+ """The candidate events, each carrying the sessions it fired in recently.++ Fails open, because unmeasured names still beat no grounding at all: the briefing then shows+ them with no count, which reads as unknown rather than as dead.+ """+ if not names:+ return []+ try:+ sessions = recent_event_sessions(team=team, event_names=list(names))+ except Exception:+ logger.warning("replay_vision.scanner_draft.event_volume_failed", team_id=team.id, exc_info=True)+ return [_CandidateEvent(name=name) for name in names]+ # The exact name first: event definitions are unique on the name itself, so a team can hold both+ # "Signup" and "signup" as separate live events, and folding their counts together would credit+ # one with the other's volume. The case-insensitive fallback is for a definition that outlived a+ # casing change, and only when one measured name claims that spelling.+ by_lower: dict[str, list[int]] = {}+ for measured_name, count in sessions.items():+ by_lower.setdefault(measured_name.lower(), []).append(count)+ unambiguous = {lowered: counts[0] for lowered, counts in by_lower.items() if len(counts) == 1}+ return [_CandidateEvent(name=name, sessions=sessions.get(name, unambiguous.get(name.lower(), 0))) for name in names]++def _page_filter_regex(pathname: str) -> str | None:"""A ClickHouse regex matching real URLs for a collapsed grounding path, or None when the pathcannot narrow.@@ -1375,9 +1468,22 @@ def _page_filter_regex(pathname: str) -> str | None:return _ID_WILDCARD.join(re.escape(part) for part in pathname.split(":id"))-def _strip_page_count(page: str) -> str:- """Remove the trailing " (123)" session count the briefing appends, leaving the bare pathname."""- return re.sub(r"\s*\(\d+\)\s*$", "", page).strip()+def _strip_briefing_count(value: str) -> str:+ """Remove the trailing " (123)" session count the briefing appends, leaving the bare page or+ event name."""+ return re.sub(r"\s*\(\d+\)\s*$", "", value).strip()+++def _proposed_filter_values(raw: Sequence[str], allowed: Sequence[str]) -> list[str]:+ """The model's proposed filter values, with the briefing's session count removed.++ The briefing renders each page and event as "/billing (10)", and a literal-minded model copies+ the count along with the name. Stripping unconditionally corrupts a name that itself ends in a+ numeric parenthetical, so a value the briefing actually offered is left alone.+ """+ allowed_set = set(allowed)+ kept = (value.strip() if value.strip() in allowed_set else _strip_briefing_count(value) for value in raw)+ return [v for v in kept if v]def _v2_query(@@ -1386,14 +1492,19 @@ def _v2_query(event_properties: Sequence[_LlmEventPropertyFilter] = (),actions: Sequence[_MatchedAction] = (),cohorts: Sequence[_MatchedCohort] = (),+ *,+ events_match: Literal["all", "any"] = "all",) -> dict[str, Any] | None:"""The scanner's recording filter from the grounded pages, events, and event properties.Pages become ONE multi-value `visited_page` property (its values OR). Each value is a regex thatmatches the collapsed page against real URLs, with ":id" runs wildcarded. Events go in the- `events` list, where each event ANDs, with the other events and with the page property. A- property filter rides on its event's entry, so "survey sent where $survey_id is X" stays one- condition rather than matching every survey. The estimate the caller runs, and the review page's+ `events` list, where each event ANDs, with the other events and with the page property, unless+ `events_match` is "any": then the query carries the OR operand, so a session holding any one of+ the events is scanned. That operand covers the whole filter rather than the events alone, so the+ caller only asks for it when the events ARE the filter. A property filter rides on its event's+ entry, so "survey sent where $survey_id is X" stays one condition rather than matching every+ survey. The estimate the caller runs, and the review page'sSave-at-zero gate, catch an over-constrained AND before it ever becomes a scanner."""query: dict[str, Any] = {"kind": "RecordingsQuery"}@@ -1432,6 +1543,11 @@ def _v2_query(]if not any(key in query for key in ("properties", "events", "actions")):return None+ if events_match == "any" and len(query.get("events") or []) > 1:+ # One event is the same set either way, and leaving the operand off keeps that query+ # identical to every other single-event draft. Test-account filtering is AND'd by the query+ # runner whatever the operand, so the union still excludes internal traffic.+ query["operand"] = "OR"return query@@ -1567,6 +1683,7 @@ def _finalize_v2(allowed_pages: Sequence[str],allowed_events: Sequence[str],team_id: int,+ excluded_events: Collection[str] = (),allowed_surveys: Sequence[_MatchedSurvey] = (),allowed_actions: Sequence[_MatchedAction] = (),allowed_cohorts: Sequence[_MatchedCohort] = (),@@ -1578,24 +1695,20 @@ def _finalize_v2(raise DraftError("missing_name_or_prompt")scanner_config = _normalized_config(parsed) # type: ignore[arg-type]- # The briefing shows each page as "/billing (10)" for ranking, but the prompt tells the model to- # copy pages exactly, so a literal-minded model returns the count too. Strip it before matching,- # or a well-behaved model's page fails the verbatim check and the scanner widens to everything.- proposed_pages = [s for p in parsed.filter_pages if (s := _strip_page_count(p))]+ proposed_pages = _proposed_filter_values(parsed.filter_pages, allowed_pages)+ # Dead events count as offered here: they left the briefing, but revival below can still put one+ # back, and a name that itself ends in a numeric parenthetical must survive to be recognised.+ proposed_events = _proposed_filter_values(parsed.filter_events, [*allowed_events, *excluded_events])# Verbatim membership in the lists the model was shown: a page or event the product never emits# would silently match zero sessions, so a hallucinated one must not survive. Events also accept# a definition-lookup match, because the briefing's events list is a sample and the goal can# name a real event the sample missed.pages = _grounded(proposed_pages, allowed_pages, _MAX_FILTER_PAGES)- events = _grounded_events(parsed.filter_events, allowed_events, _MAX_FILTER_EVENTS, team_id)+ # The model's own operand, not the possibly-downgraded one below: the wider cap is what an+ # "any" draft asked for, while three ANDed events describe a flow almost no session has.+ event_cap = _MAX_V2_FILTER_EVENTS if parsed.filter_events_match == "any" else _MAX_FILTER_EVENTS+ events = _grounded_events(proposed_events, allowed_events, event_cap, team_id, excluded=excluded_events)- # Always exclude internal and test users: a scanner defaults to real-user sessions unless the- # creator says otherwise (the recordings step can toggle it back on). No-op for a team that has- # not configured internal-user filters. The narrowing query is None when no page or event- # survives, so the base still carries this default.- event_properties = _grounded_event_properties(- parsed.filter_event_properties, kept_events=events, allowed_surveys=allowed_surveys- )# Grounding by construction: a name maps back to the matched action's own id, so an invented# name has no id to resolve to and drops out.actions_by_name = {a.name: a for a in allowed_actions}@@ -1613,15 +1726,60 @@ def _finalize_v2(targeting = _grounded_targeting(parsed.filter_experiment, parsed.filter_experiment_variant, allowed=allowed_experiments)- narrowing = _v2_query(pages, events, event_properties, kept_actions, kept_cohorts)+ # Dropping a dead event narrows only while another filter still holds the scan down. Alone it+ # inverts, leaving a query that matches every session rather than none, so the event goes back.+ revived_dead_events = (+ bool(proposed_events)+ and not events+ and not (pages or kept_actions or kept_cohorts or targeting)+ and bool(excluded_events)+ )+ if revived_dead_events:+ events = _grounded_events(proposed_events, [*allowed_events, *excluded_events], event_cap, team_id)+ logger.info(+ "replay_vision.scanner_draft.dead_events_revived",+ team_id=team_id,+ kept_events=len(events),+ )+ # Grounded after the events are final: a property filter rides on its event's entry, so it has+ # to name an event the query still carries.+ event_properties = _grounded_event_properties(+ parsed.filter_event_properties, kept_events=events, allowed_surveys=allowed_surveys+ )+ # "any" is an operand on the whole recordings query, so it would OR the pages, actions and+ # cohort with the events as well, scanning a union the goal never asked for. It only survives+ # when the events are the entire filter.+ events_match: Literal["all", "any"] = parsed.filter_events_match+ downgraded_match = events_match == "any" and bool(pages or kept_actions or kept_cohorts or event_properties)+ if downgraded_match:+ # One event, not an AND of all of them: "any" means the events are alternatives, and+ # requiring one session to do every one of them matches almost nobody. A property filter's+ # event takes the slot, or the filter is left riding on an event the query no longer carries.+ kept_event = event_properties[0].event if event_properties else next(iter(events), None)+ events = [kept_event] if kept_event is not None else []+ event_properties = [p for p in event_properties if p.event == kept_event]+ events_match = "all"+ logger.info(+ "replay_vision.scanner_draft.events_match_downgraded",+ team_id=team_id,+ kept_events=len(events),+ kept_pages=len(pages),+ kept_actions=len(kept_actions),+ kept_cohorts=len(kept_cohorts),+ )+ narrowing = _v2_query(pages, events, event_properties, kept_actions, kept_cohorts, events_match=events_match)query: dict[str, Any] = narrowing if narrowing is not None else {"kind": "RecordingsQuery"}+ # Always exclude internal and test users: a scanner defaults to real-user sessions unless the+ # creator says otherwise (the recordings step can toggle it back on). No-op for a team that has+ # not configured internal-user filters. The narrowing query is None when no page or event+ # survives, so the base still carries this default.query["filter_test_accounts"] = Truedropped_pages = set(proposed_pages) - set(pages)# Compare case-insensitively: a kept event may carry the team's canonical casing rather than# the model's, and that is a kept filter, not a dropped one.kept_events_lower = {e.lower() for e in events}- dropped_events = {e for e in (v.strip() for v in parsed.filter_events) if e and e.lower() not in kept_events_lower}+ dropped_events = {e for e in proposed_events if e.lower() not in kept_events_lower}# A page can ground yet drop to None in `_page_filter_value` (a too-short prefix) with nothing# formally dropped, so the query still widens to everything. Fire the warning whenever the model# wanted a filter but none survived, not only when a value was dropped.@@ -1629,7 +1787,7 @@ def _finalize_v2(widened_unexpectedly = (narrowing is Noneand targeting is None- and bool(proposed_pages or parsed.filter_events or parsed.filter_actions or parsed.filter_cohorts)+ and bool(proposed_pages or proposed_events or parsed.filter_actions or parsed.filter_cohorts))if dropped_pages or dropped_events or dropped_experiment or widened_unexpectedly:# Every dropped value silently broadens the scan (worst case to every non-internal session,@@ -1694,14 +1852,45 @@ def draft_scanner_from_goal_v2(# Measured here rather than inside the match, so the access-control helper stays free of a# ClickHouse query its other callers do not need.matches = replace(matches, actions=_live_actions(team, matches.actions))+ survey_events: set[str] = set()if matches.surveys:# A goal can name a survey without using the word "survey" ("who answered XYZ Feedback"), so# the survey events may not have matched on their own. A property filter is useless without# the event it rides on, so offer those events whenever a survey matched.- events = list(dict.fromkeys([*_SURVEY_EVENTS, *events]))+ #+ # Canonicalized first: the constants are lowercase, but a team's own spelling can differ, and+ # the goal search returns that spelling. Both would otherwise reach the briefing as separate+ # candidates for the one event.+ canonical = _event_names_by_lower(team.id, _SURVEY_EVENTS)+ # A list, not the set, so the briefing keeps a stable order: string hashing is randomized+ # per process, and an order that moves between runs makes drafts irreproducible.+ injected = [canonical.get(name, name) for name in _SURVEY_EVENTS]+ survey_events = set(injected)+ events = list(dict.fromkeys([*injected, *events]))+ # Measured here, not in `_events_for_goal`: that lookup is a name search, and only a session+ # count tells the model which of the matching names is worth filtering on. Event volume is an+ # analytics read behind query:read, so a scoped token without it gets the names unmeasured —+ # counts the briefing would show could otherwise reach the model's prompt and rationale.+ measured = (+ _measured_events(team, events)+ if _scopes_allow_read(allowed_scopes, "query")+ else [_CandidateEvent(name=name) for name in events]+ )+ # The survey events are injected to carry a `$survey_id` filter, not because volume picked+ # them. A survey quiet for the window measures zero on all of them, and the filter can only+ # ride on an event the query carries, so dropping them would widen a one-survey scan to every+ # session. Offered without a count instead, the way an unmeasured event reads.+ if survey_events:+ measured = [replace(c, sessions=None) if c.sessions == 0 and c.name in survey_events else c for c in measured]+ # A measured-zero event fired in no session in the window, so a filter on it (events AND with the+ # rest) would take the whole scan to zero. Drop it from the briefing like a dead action, and carry+ # its name so grounding's definition-lookup fallback cannot re-admit it. sessions=None is an+ # unmeasured event, not a dead one, so it stays available.+ dead_events = {c.name for c in measured if c.sessions == 0}+ candidates = [c for c in measured if c.sessions != 0]user_content = _build_user_content_v2(goal,- events,+ candidates,pages,scanners=_existing_scanners(team, user_access_control),surveys=matches.surveys,@@ -1722,7 +1911,8 @@ def draft_scanner_from_goal_v2(draft = _finalize_v2(cast(_LlmDraftV2, parsed),allowed_pages=[p.pathname for p in pages],- allowed_events=events,+ allowed_events=[c.name for c in candidates],+ excluded_events=dead_events,team_id=team.id,allowed_surveys=matches.surveys,allowed_actions=matches.actions,diff --git a/products/replay_vision/backend/tests/test_event_volume.py b/products/replay_vision/backend/tests/test_event_volume.pynew file mode 100644index 000000000000..9c9fa7704cae--- /dev/null+++ b/products/replay_vision/backend/tests/test_event_volume.py@@ -0,0 +1,75 @@+import datetime as dt++import pytest+import time_machine+from posthog.test.base import ClickhouseTestMixin, _create_event++from posthog.models import Team++from products.replay_vision.backend.queries.event_volume import recent_event_sessions++_NOW = dt.datetime(2026, 5, 1, 12, 0, 0, tzinfo=dt.UTC)+_FROZEN_TIME = _NOW.strftime("%Y-%m-%dT%H:%M:%SZ")+++def _event(team, event: str, session_id: str | None, at: dt.datetime) -> None:+ _create_event(+ team=team,+ event=event,+ distinct_id="d1",+ timestamp=at,+ properties={"$session_id": session_id} if session_id else {},+ )+++class TestRecentEventSessions(ClickhouseTestMixin):+ @pytest.fixture(autouse=True)+ def _frozen_clock(self):+ with time_machine.travel(_FROZEN_TIME, tick=False):+ yield++ @pytest.mark.django_db+ def test_a_busy_event_separates_from_a_quiet_one_by_its_session_count(self, team) -> None:+ # Both names exist on the team, so a definition lookup ranks them the same. Only the count+ # tells the model which one is worth filtering a scanner on.+ _event(team, "scanner_created", "s1", _NOW - dt.timedelta(hours=2))+ # Same session twice: the count is sessions, not events, so this must not read as two.+ _event(team, "scanner_created", "s1", _NOW - dt.timedelta(hours=1))+ _event(team, "scanner_created", "s2", _NOW - dt.timedelta(days=3))+ _event(team, "scanner_drafted", "s3", _NOW - dt.timedelta(days=1))+ # Outside the window, so it must not count: an event that stopped firing takes an AND+ # filter to zero, and absence is how the briefing shows that it went quiet.+ _event(team, "old_flow_started", "s4", _NOW - dt.timedelta(days=30))++ counts = recent_event_sessions(+ team=team, event_names=["scanner_created", "scanner_drafted", "old_flow_started"]+ )++ assert counts == {"scanner_created": 2, "scanner_drafted": 1}++ @pytest.mark.django_db+ def test_an_event_with_no_session_is_not_counted(self, team) -> None:+ # A scanner watches recordings, so an event fired outside a session (a backend capture) says+ # nothing about how many sessions a filter on it would match.+ _event(team, "invoice_paid", None, _NOW - dt.timedelta(hours=1))++ assert recent_event_sessions(team=team, event_names=["invoice_paid"]) == {}++ @pytest.mark.django_db+ def test_the_name_is_matched_exactly(self, team) -> None:+ # `event` is the third column of the events sort key, so the filter stays an exact match on+ # the bare column. Callers pass the team's own spelling; a case-folded filter here would+ # read the whole window instead of skipping granules.+ _event(team, "Survey Sent", "s1", _NOW - dt.timedelta(hours=1))++ assert recent_event_sessions(team=team, event_names=["survey sent"]) == {}+ assert recent_event_sessions(team=team, event_names=["Survey Sent"]) == {"Survey Sent": 1}++ @pytest.mark.django_db+ def test_another_projects_events_are_never_counted(self, team) -> None:+ # The names come from a definition search, and a count that leaked across teams would+ # report another project's traffic into this team's briefing.+ other_team = Team.objects.create(organization=team.organization, name="other")+ _event(other_team, "scanner_created", "s1", _NOW - dt.timedelta(hours=1))++ assert recent_event_sessions(team=team, event_names=["scanner_created"]) == {}diff --git a/products/replay_vision/backend/tests/test_scanner_draft.py b/products/replay_vision/backend/tests/test_scanner_draft.pyindex b24159ab428c..9c2923594e8d 100644--- a/products/replay_vision/backend/tests/test_scanner_draft.py+++ b/products/replay_vision/backend/tests/test_scanner_draft.py@@ -27,7 +27,9 @@ from products.replay_vision.backend.scanner_draft import (DraftError,ScannerDraft,_build_user_content,+ _build_user_content_v2,_business_context,+ _CandidateEvent,_events_for_goal,_existing_scanners,_ExistingScanner,@@ -44,6 +46,7 @@ from products.replay_vision.backend.scanner_draft import (_MatchedCohort,_MatchedExperiment,_MatchedSurvey,+ _measured_events,_solve_budget,_v2_query,draft_scanner_from_goal,@@ -146,6 +149,22 @@ class TestBuildUserContent:assert "Checkout drop-off (monitor): Flags abandoned checkouts." in content+class TestBuildUserContentV2:+ def test_events_carry_their_session_counts(self):+ # The counts are what let the model tell a busy event from a name that stopped firing, the+ # same way the pages list already does. An unmeasured event shows no count rather than a+ # zero, which would read as dead.+ content = _build_user_content_v2(+ "watch people who create a scanner",+ [_CandidateEvent(name="scanner_created", sessions=42), _CandidateEvent(name="scanner_edited")],+ [VisitedPath(pathname="/replay-vision", sessions=10)],+ )++ fenced = content.split("<product-data>")[-1].split("</product-data>")[0]+ assert "scanner_created (42)" in fenced+ assert "scanner_edited\n" in fenced++class TestFinalize:def test_monitor_config_is_prompt_only(self):result = _finalize(_draft())@@ -944,6 +963,47 @@ class TestLiveActions(_VisionAPITestCase):assert _live_actions(self.team, actions) == actions+class TestMeasuredEvents(_VisionAPITestCase):+ def test_every_candidate_carries_its_session_count(self):+ # A definition lookup ranks a name that fired twice yesterday with one that fires in every+ # session. Without the counts the model picks the dead name and the scanner never runs.+ with patch(f"{_MODULE}.recent_event_sessions", return_value={"scanner_created": 42}):+ measured = _measured_events(self.team, ["scanner_created", "old_flow_started"])++ assert measured == [+ _CandidateEvent(name="scanner_created", sessions=42),+ _CandidateEvent(name="old_flow_started", sessions=0),+ ]++ def test_a_count_returned_in_another_casing_still_reaches_its_candidate(self):+ # The query keys its result by the stored casing, which the hardcoded survey names do not+ # match. A missed lookup would measure zero, and a zero now drops the event as dead.+ with patch(f"{_MODULE}.recent_event_sessions", return_value={"Survey Sent": 7}):+ measured = _measured_events(self.team, ["survey sent"])++ assert measured == [_CandidateEvent(name="survey sent", sessions=7)]++ def test_two_events_differing_only_in_case_keep_their_own_counts(self):+ # Definitions are unique on the name itself, so both can be real, live events. Folding them+ # together would credit the retired one with the live one's volume -- and which one won+ # would depend on the order ClickHouse happened to emit the rows in.+ with patch(f"{_MODULE}.recent_event_sessions", return_value={"Signup": 0, "signup": 500}):+ measured = _measured_events(self.team, ["Signup", "signup"])++ assert measured == [+ _CandidateEvent(name="Signup", sessions=0),+ _CandidateEvent(name="signup", sessions=500),+ ]++ def test_a_failed_measurement_keeps_every_candidate_uncounted(self):+ # Losing the counts must cost the ranking hint, not the grounding: a briefing with no+ # events sends the model back to drafting filters it invents.+ with patch(f"{_MODULE}.recent_event_sessions", side_effect=Exception("clickhouse down")):+ measured = _measured_events(self.team, ["scanner_created"])++ assert measured == [_CandidateEvent(name="scanner_created", sessions=None)]++class TestV2Query:def test_pages_become_one_multi_value_property(self):# Separate properties would AND and match almost nothing: measured 68 sessions where the@@ -1002,6 +1062,20 @@ class TestV2Query:assert query is not Noneassert {"type": "cohort", "key": "id", "value": 7, "operator": "in"} in query["properties"]+ @pytest.mark.parametrize(+ "events_match,expected_operand",+ [("all", None), ("any", "OR")],+ )+ def test_the_events_match_decides_the_operand(self, events_match, expected_operand):+ # "created or edited" is one goal over several events, and ANDing them keeps only the+ # person who did all of them in one session, which is almost nobody. The default has to+ # stay AND: flipping it would silently widen every filter already drafted.+ query = _v2_query([], ["scanner_created", "scanner_edited"], events_match=events_match)++ assert query is not None+ assert query.get("operand") == expected_operand+ assert [e["id"] for e in query["events"]] == ["scanner_created", "scanner_edited"]+def test_no_pages_and_no_events_is_no_query(self):assert _v2_query([], []) is None@@ -1220,6 +1294,40 @@ class TestFinalizeV2:assert draft.query["filter_test_accounts"] is Trueassert draft.query["properties"][0]["value"] == ["/billing"]+ @pytest.mark.parametrize(+ "filter_pages,expected_operand",+ [([], "OR"), (["/billing"], None)],+ )+ def test_any_match_survives_only_when_the_events_are_the_whole_filter(self, filter_pages, expected_operand):+ # The operand covers the whole recordings query, so keeping "any" next to a page filter+ # would OR the page in too and scan every session that merely visited it.+ draft = _finalize_v2(+ _draft_v2(+ filter_pages=filter_pages,+ filter_events=["scanner_created", "scanner_edited"],+ filter_events_match="any",+ ),+ allowed_pages=["/billing"],+ allowed_events=["scanner_created", "scanner_edited"],+ team_id=1,+ )++ assert draft.query is not None+ assert draft.query.get("operand") == expected_operand++ def test_event_count_suffix_is_stripped_before_grounding(self):+ # The briefing shows "checkout_started (312)" now, and a model that copies it verbatim+ # would fail the exact membership check, dropping the filter and widening the scan.+ draft = _finalize_v2(+ _draft_v2(filter_events=["checkout_started (312)"]),+ allowed_pages=[],+ allowed_events=["checkout_started"],+ team_id=1,+ )++ assert draft.query is not None+ assert [e["id"] for e in draft.query["events"]] == ["checkout_started"]+def test_page_count_suffix_is_stripped_before_grounding(self):# The briefing shows "/billing (10)"; a model that copies it verbatim would fail the exact# membership check and the scanner would widen to everything. Strip the count first.@@ -1230,6 +1338,122 @@ class TestFinalizeV2:assert draft.query is not Noneassert draft.query["properties"][0]["value"] == ["/billing"]+ def test_an_allowed_event_whose_own_name_ends_in_a_count_is_not_stripped(self):+ # "checkout step (2)" is the event's real name, not a briefing count. Stripping it first+ # would either fail the membership check or ground to a different event, and both widen.+ draft = _finalize_v2(+ _draft_v2(filter_events=["checkout step (2)"]),+ allowed_pages=[],+ allowed_events=["checkout step (2)"],+ team_id=1,+ )++ assert draft.query is not None+ assert [e["id"] for e in draft.query["events"]] == ["checkout step (2)"]++ def test_a_dead_event_whose_own_name_ends_in_a_count_survives_to_be_revived(self):+ # A dead event leaves the briefing, so it is not in `allowed_events` — but revival can put+ # it back, and it can only be recognised under its real name. Stripped to "checkout step" it+ # either drops, leaving a query that scans every session, or grounds to a different event.+ draft = _finalize_v2(+ _draft_v2(filter_events=["checkout step (2)"]),+ allowed_pages=[],+ allowed_events=[],+ team_id=1,+ excluded_events={"checkout step (2)"},+ )++ assert draft.query is not None+ assert [e["id"] for e in draft.query["events"]] == ["checkout step (2)"]++ @pytest.mark.parametrize(+ "events_match,expected_events,expected_operand",+ [+ # The third slot exists for the OR case. ANDed, three events need one session to do all+ # of them, which is the over-constrained draft the cap is there to prevent.+ ("all", ["a", "b"], None),+ # "created or edited" names three events that each mean the same thing, and ORed they+ # cost nothing to carry: this is the case the wider cap was raised for.+ ("any", ["a", "b", "c"], "OR"),+ ],+ )+ def test_the_event_cap_follows_the_match_operand(self, events_match, expected_events, expected_operand):+ draft = _finalize_v2(+ _draft_v2(filter_events=["a", "b", "c"], filter_events_match=events_match),+ allowed_pages=[],+ allowed_events=["a", "b", "c"],+ team_id=1,+ )++ assert draft.query is not None+ assert [e["id"] for e in draft.query["events"]] == expected_events+ assert draft.query.get("operand") == expected_operand++ def test_a_downgraded_any_match_keeps_one_event_rather_than_anding_them(self):+ # The page filter rules out the OR operand, but ANDing the alternatives would need one+ # session to have created AND edited AND updated, which matches almost nobody.+ draft = _finalize_v2(+ _draft_v2(filter_pages=["/billing"], filter_events=["a", "b", "c"], filter_events_match="any"),+ allowed_pages=["/billing"],+ allowed_events=["a", "b", "c"],+ team_id=1,+ )++ assert draft.query is not None+ assert [e["id"] for e in draft.query["events"]] == ["a"]++ def test_a_downgraded_any_match_keeps_the_event_its_property_filter_rides_on(self):+ # Keeping the first event blindly would strand the $survey_id filter on an event the query+ # no longer carries, silently turning a one-survey scan into a scan of every survey.+ draft = _finalize_v2(+ _draft_v2(+ filter_pages=["/billing"],+ filter_events=["survey shown", "survey sent"],+ filter_events_match="any",+ filter_event_properties=[+ _LlmEventPropertyFilter(event="survey sent", property="$survey_id", value="abc-123")+ ],+ ),+ allowed_pages=["/billing"],+ allowed_events=["survey shown", "survey sent"],+ team_id=1,+ allowed_surveys=[_MatchedSurvey(name="Pricing feedback", survey_id="abc-123")],+ )++ assert draft.query is not None+ assert [e["id"] for e in draft.query["events"]] == ["survey sent"]+ assert draft.query["events"][0]["properties"] == [+ {"key": "$survey_id", "value": ["abc-123"], "operator": "exact", "type": "event"}+ ]++ def test_a_dead_event_comes_back_rather_than_leaving_the_scan_unfiltered(self):+ # Dropping the dead event narrows nothing here, it inverts: with no page, action or cohort+ # left, the query stops describing the goal's flow and starts matching every session.+ draft = _finalize_v2(+ _draft_v2(filter_events=["billing_limit_set"]),+ allowed_pages=[],+ allowed_events=[],+ team_id=1,+ excluded_events={"billing_limit_set"},+ )++ assert draft.query is not None+ assert [e["id"] for e in draft.query["events"]] == ["billing_limit_set"]++ def test_a_dead_event_still_drops_while_another_filter_holds_the_scan_down(self):+ # The page narrows on its own, so dropping the dead event is the narrowing it looks like.+ draft = _finalize_v2(+ _draft_v2(filter_pages=["/billing"], filter_events=["billing_limit_set"]),+ allowed_pages=["/billing"],+ allowed_events=[],+ team_id=1,+ excluded_events={"billing_limit_set"},+ )++ assert draft.query is not None+ assert "events" not in draft.query+ assert draft.query["properties"][0]["key"] == "visited_page"+def test_a_grounded_page_that_cannot_narrow_warns_it_scans_everything(self):# "/x" is a real page and grounds, but its filter value is too short to narrow, so the query# widens to every session. The warning must report that, even though nothing was dropped.@@ -1388,6 +1612,12 @@ class TestDraftV2(_VisionAPITestCase):def _drafted_with(self, generate, estimate):with (patch(f"{_MODULE}.fetch_visited_paths", return_value=(VisitedPath(pathname="/billing", sessions=10),)),+ # Measure every candidate live, so these tests exercise the estimate-based fallback rather+ # than the earlier measured-dead drop, which would remove the event before it is reached.+ patch(+ f"{_MODULE}._measured_events",+ side_effect=lambda team, names: [_CandidateEvent(name=n, sessions=100) for n in names],+ ),patch(_GENERATE_PATH, return_value=generate),patch(f"{_MODULE}.estimate_scanner_session_volume", side_effect=estimate),):@@ -1451,6 +1681,156 @@ class TestDraftV2(_VisionAPITestCase):assert [e["id"] for e in draft.query["events"]] == ["billing_limit_set"]assert draft.estimated_monthly_observations == 0+ def test_a_measured_dead_event_is_dropped_before_it_can_zero_the_scan(self):+ # billing_limit_set is a real definition, so grounding's definition-lookup fallback would+ # re-admit it even after it leaves the briefing. Measured at zero sessions it is dead, and one+ # dead event ANDs the whole scan to nothing, so it must not reach the query. The page comes+ # from live traffic, so it survives and narrows on its own.+ EventDefinition.objects.create(team=self.team, name="billing_limit_set", last_seen_at=timezone.now())++ with (+ patch(f"{_MODULE}.fetch_visited_paths", return_value=(VisitedPath(pathname="/billing", sessions=10),)),+ patch(f"{_MODULE}._measured_events", return_value=[_CandidateEvent(name="billing_limit_set", sessions=0)]),+ patch(+ _GENERATE_PATH,+ return_value=_draft_v2(filter_pages=["/billing"], filter_events=["billing_limit_set"]),+ ),+ patch(+ f"{_MODULE}.estimate_scanner_session_volume",+ return_value=ScannerVolumeEstimate(matched_sessions=300, effective_window_days=30),+ ),+ ):+ draft = draft_scanner_from_goal_v2(+ team=self.team,+ user=self.user,+ goal="find out where people give up in billing",+ monthly_credit_budget=10_000,+ user_access_control=_access_control(allow=True),+ )++ assert draft.query is not None+ assert "events" not in draft.query+ assert draft.query["properties"][0]["key"] == "visited_page"++ def test_a_quiet_surveys_injected_events_survive_so_its_property_filter_does(self):+ # The survey events are injected to carry the $survey_id filter, not because volume picked+ # them. A survey with no responses in the window measures zero on all of them, and dropping+ # them takes the property filter with them — the one-survey scan becomes a scan of+ # everything, which is the opposite of what the goal asked for.+ survey = Survey.objects.create(team=self.team, name="Pricing feedback", created_by=self.user)++ with (+ patch(f"{_MODULE}.fetch_visited_paths", return_value=()),+ patch(+ f"{_MODULE}._measured_events",+ return_value=[_CandidateEvent(name=name, sessions=0) for name in ("survey sent", "survey shown")],+ ),+ patch(+ _GENERATE_PATH,+ return_value=_draft_v2(+ filter_events=["survey sent"],+ filter_event_properties=[+ _LlmEventPropertyFilter(event="survey sent", property="$survey_id", value=str(survey.id))+ ],+ ),+ ),+ patch(+ f"{_MODULE}.estimate_scanner_session_volume",+ return_value=ScannerVolumeEstimate(matched_sessions=300, effective_window_days=30),+ ),+ ):+ draft = draft_scanner_from_goal_v2(+ team=self.team,+ user=self.user,+ goal='who answered "Pricing feedback"',+ monthly_credit_budget=10_000,+ user_access_control=_access_control(allow=True),+ )++ assert draft.query is not None+ assert [e["id"] for e in draft.query["events"]] == ["survey sent"]+ assert draft.query["events"][0]["properties"] == [+ {"key": "$survey_id", "value": [str(survey.id)], "operator": "exact", "type": "event"}+ ]++ def test_a_teams_own_casing_of_a_survey_event_is_offered_once(self):+ # The injected names are lowercase constants, but the goal search returns the team's own+ # spelling. Offering both would show one event twice, each carrying the same count, and the+ # quiet-survey exemption below only recognises the spelling it injected — so the team's own+ # would measure zero and drop as dead, taking its $survey_id filter with it.+ Survey.objects.create(team=self.team, name="Pricing feedback", created_by=self.user)+ EventDefinition.objects.create(team=self.team, name="Survey Sent", last_seen_at=timezone.now())+ offered: list[list[str]] = []++ def measure(team, names):+ offered.append(list(names))+ return [_CandidateEvent(name=name, sessions=0) for name in names]++ with (+ patch(f"{_MODULE}.fetch_visited_paths", return_value=()),+ patch(f"{_MODULE}._measured_events", side_effect=measure),+ patch(_GENERATE_PATH, return_value=_draft_v2(filter_events=["Survey Sent"])),+ patch(+ f"{_MODULE}.estimate_scanner_session_volume",+ return_value=ScannerVolumeEstimate(matched_sessions=300, effective_window_days=30),+ ),+ ):+ draft_scanner_from_goal_v2(+ team=self.team,+ user=self.user,+ goal='who answered "Pricing feedback"',+ monthly_credit_budget=10_000,+ user_access_control=_access_control(allow=True),+ )++ assert [n for n in offered[0] if n.lower() == "survey sent"] == ["Survey Sent"]++ def test_a_quiet_survey_event_in_the_teams_own_casing_is_still_exempt(self):+ # The exemption matches on the injected names, so it has to recognise the canonical spelling+ # too. Otherwise a quiet survey drops the event its $survey_id filter rides on, and the+ # one-survey scan widens to every session.+ #+ # A page filter rides along so the dead-event revival stays out of it: revival only fires+ # when nothing else survives, and it would otherwise rescue the event whatever the exemption+ # did, leaving this test unable to fail.+ survey = Survey.objects.create(team=self.team, name="Pricing feedback", created_by=self.user)+ EventDefinition.objects.create(team=self.team, name="Survey Sent", last_seen_at=timezone.now())++ with (+ patch(+ f"{_MODULE}.fetch_visited_paths",+ return_value=(VisitedPath(pathname="/pricing", sessions=10),),+ ),+ patch(f"{_MODULE}.recent_event_sessions", return_value={}),+ patch(+ _GENERATE_PATH,+ return_value=_draft_v2(+ filter_pages=["/pricing"],+ filter_events=["Survey Sent"],+ filter_event_properties=[+ _LlmEventPropertyFilter(event="Survey Sent", property="$survey_id", value=str(survey.id))+ ],+ ),+ ),+ patch(+ f"{_MODULE}.estimate_scanner_session_volume",+ return_value=ScannerVolumeEstimate(matched_sessions=300, effective_window_days=30),+ ),+ ):+ draft = draft_scanner_from_goal_v2(+ team=self.team,+ user=self.user,+ goal='who answered "Pricing feedback"',+ monthly_credit_budget=10_000,+ user_access_control=_access_control(allow=True),+ )++ assert draft.query is not None+ assert [e["id"] for e in draft.query["events"]] == ["Survey Sent"]+ assert draft.query["events"][0]["properties"] == [+ {"key": "$survey_id", "value": [str(survey.id)], "operator": "exact", "type": "event"}+ ]+def test_the_experiment_the_goal_named_is_carried_as_targeting_and_counted(self):# The whole point of the targeting: the projection has to count that experiment's# participants, not every session the pages match, while the query the wizard saves stays@@ -1491,6 +1871,45 @@ class TestDraftV2(_VisionAPITestCase):assert draft.model == "gemini-3.8-flash"assert draft.credit_limit == 10_000+ def _briefing_under_scopes(self, allowed_scopes):+ EventDefinition.objects.create(team=self.team, name="billing_limit_set", last_seen_at=timezone.now())+ with (+ patch(f"{_MODULE}.fetch_visited_paths", return_value=()),+ patch(f"{_MODULE}.recent_event_sessions", return_value={"billing_limit_set": 42}) as volume,+ patch(_GENERATE_PATH, return_value=_draft_v2()) as generate,+ patch(+ f"{_MODULE}.estimate_scanner_session_volume",+ return_value=ScannerVolumeEstimate(matched_sessions=300, effective_window_days=30),+ ),+ ):+ draft_scanner_from_goal_v2(+ team=self.team,+ user=self.user,+ goal="find out where people give up in billing",+ monthly_credit_budget=10_000,+ user_access_control=_access_control(allow=True),+ allowed_scopes=allowed_scopes,+ )+ return volume, generate.call_args.kwargs["user_content"]++ def test_a_scoped_token_lacking_query_read_gets_unmeasured_events(self):+ # Event volume is an analytics read: a token with only scanner and recording scopes must not+ # receive counts, which the briefing would surface into the model's prompt and rationale.+ # The names still go in, unmeasured, so grounding survives the gate.+ volume, user_content = self._briefing_under_scopes(["replay_scanner:write", "session_recording:read"])++ assert not volume.called+ assert "billing_limit_set" in user_content+ assert "billing_limit_set (" not in user_content++ def test_query_read_scope_keeps_the_measured_counts(self):+ volume, user_content = self._briefing_under_scopes(+ ["replay_scanner:write", "session_recording:read", "query:read"]+ )++ assert volume.called+ assert "billing_limit_set (42)" in user_content+class TestDraftEndpointGoalFlow(_VisionAPITestCase):@property