: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): simplify alert conditions (#72434)
Kim Svatos Dugan289af81cac1b
diff --git a/products/replay_vision/frontend/replay_scanners/ActionEditorScene.tsx b/products/replay_vision/frontend/replay_scanners/ActionEditorScene.tsxindex 1cf26bb179ae..4a3ccf00ff2c 100644--- a/products/replay_vision/frontend/replay_scanners/ActionEditorScene.tsx+++ b/products/replay_vision/frontend/replay_scanners/ActionEditorScene.tsx@@ -296,89 +296,89 @@ const WINDOW_OPTIONS = [{ value: 30, label: 'the last 30 days' },]-// The type-specific match controls (verdict chips / tag picker / score range), shared by both alert-// flavors. Empty controls mean any observation matches.-function MatchControls({ scannerId }: { scannerId: string }): JSX.Element {+// The match predicate as one compact inline line ("Match observations tagged […]"), so it reads as a+// sentence rather than a boxed input under its own heading. Empty controls = every observation counts.+// Returns null for the average-score metric, where a score pre-filter makes no sense.+function AlertMatchLine({ scannerId }: { scannerId: string }): JSX.Element | null {const { actionForm } = useValues(actionEditorSceneLogic)const { setActionFormValue } = useActions(actionEditorSceneLogic)const { scanner } = useValues(replayScannerLogic({ id: scannerId }))const toNumberOrNull = (val: number | undefined): number | null => (val === undefined || isNaN(val) ? null : val)- const isAvg = actionForm.alert_metric === VisionAlertMetricEnumApi.AvgScore+ let lead: string+ let control: JSX.Elementswitch (scanner?.scanner_type) {case 'monitor':- return (- <>- <span className="text-sm">with verdict</span>- <div className="flex gap-1">- {VERDICT_OPTIONS.map(({ value, label }) => (- <LemonButton- key={value}- size="xsmall"- type={actionForm.verdict.includes(value) ? 'primary' : 'secondary'}- onClick={() =>- setActionFormValue(- 'verdict',- actionForm.verdict.includes(value)- ? actionForm.verdict.filter((v) => v !== value)- : [...actionForm.verdict, value]- )- }- data-attr={`vision-action-alert-verdict-${value}`}- >- {label}- </LemonButton>- ))}- </div>- {actionForm.verdict.length === 0 && <span className="text-sm text-muted">(any verdict)</span>}- </>+ lead = 'with verdict'+ control = (+ <div className="flex gap-1">+ {VERDICT_OPTIONS.map(({ value, label }) => (+ <LemonButton+ key={value}+ size="small"+ type={actionForm.verdict.includes(value) ? 'primary' : 'secondary'}+ onClick={() =>+ setActionFormValue(+ 'verdict',+ actionForm.verdict.includes(value)+ ? actionForm.verdict.filter((v) => v !== value)+ : [...actionForm.verdict, value]+ )+ }+ data-attr={`vision-action-alert-verdict-${value}`}+ >+ {label}+ </LemonButton>+ ))}+ </div>)+ breakcase 'classifier': {const configuredTags: string[] = scanner.scanner_config?.tags ?? []const allowFreeform = !!scanner.scanner_config?.allow_freeform_tags- return (- <>- <span className="text-sm">tagged</span>- <div className="min-w-48">- <LemonInputSelect- mode="multiple"- size="small"- allowCustomValues={allowFreeform}- placeholder="any tag"- value={actionForm.tags}- onChange={(tags) => setActionFormValue('tags', tags)}- options={[...new Set([...configuredTags, ...actionForm.tags])].map((tag) => ({- key: tag,- label: tag,- }))}- data-attr="vision-action-alert-tags"- />- </div>- </>+ lead = 'tagged'+ control = (+ <div className="min-w-48">+ <LemonInputSelect+ mode="multiple"+ size="small"+ allowCustomValues={allowFreeform}+ placeholder="any tag"+ value={actionForm.tags}+ onChange={(tags) => setActionFormValue('tags', tags)}+ options={[...new Set([...configuredTags, ...actionForm.tags])].map((tag) => ({+ key: tag,+ label: tag,+ }))}+ data-attr="vision-action-alert-tags"+ />+ </div>)+ break}- case 'scorer':- if (isAvg) {- return <></>+ case 'scorer': {+ if (actionForm.alert_metric === VisionAlertMetricEnumApi.AvgScore) {+ return null}- return (+ const scale = scanner.scanner_config?.scale+ lead = 'scored between'+ control = (<>- <span className="text-sm">scored between</span><LemonInputtype="number"size="small"- placeholder={scanner.scanner_config?.scale ? String(scanner.scanner_config.scale.min) : 'min'}+ placeholder={scale ? String(scale.min) : 'min'}value={actionForm.min_score ?? undefined}onChange={(val) => setActionFormValue('min_score', toNumberOrNull(val))}className="w-20"data-attr="vision-action-alert-min-score"/>- <span className="text-sm">and</span>+ <span className="text-sm text-muted">and</span><LemonInputtype="number"size="small"- placeholder={scanner.scanner_config?.scale ? String(scanner.scanner_config.scale.max) : 'max'}+ placeholder={scale ? String(scale.max) : 'max'}value={actionForm.max_score ?? undefined}onChange={(val) => setActionFormValue('max_score', toNumberOrNull(val))}className="w-20"@@ -386,13 +386,22 @@ function MatchControls({ scannerId }: { scannerId: string }): JSX.Element {/></>)+ break+ }default:- return <></>+ return null}++ return (+ <div className="flex flex-wrap items-center gap-2">+ <span className="text-sm text-muted">Match observations {lead}</span>+ {control}+ </div>+ )}-// The alert condition: pick a frequency, then either "every new match" (just the match controls) or-// a threshold sentence over a rolling window.+// The alert condition, flat under the "Condition" section header: the match predicate as one inline+// line, then a single choice of how often to notify. No nested modes, no extra sub-headings.function ConditionSection({ scannerId }: { scannerId: string }): JSX.Element {const { actionForm, actionFormErrors } = useValues(actionEditorSceneLogic)const { setActionFormValue } = useActions(actionEditorSceneLogic)@@ -400,10 +409,14 @@ function ConditionSection({ scannerId }: { scannerId: string }): JSX.Element {const everyMatch = actionForm.alert_frequency === AlertConfigFrequencyEnumApi.EveryMatchconst isScorer = scanner?.scanner_type === 'scorer'+ // Direction is only offered for the average score ("below a floor" is the natural quality alarm).+ // A count threshold is always "at least" — "at most N matches" reads backwards from intent and is+ // really a went-quiet alarm, so we don't expose it for counts (buildActionBody pins it to above).+ const isAvg = actionForm.alert_metric === VisionAlertMetricEnumApi.AvgScore// Summarizer observations have no verdict/tags/score to threshold on, so the only sensible- // alert is "every new summary" — no frequency or threshold controls to configure. The logic- // normalizes the form to every_match to match (actionEditorSceneLogic.setScannerType).+ // alert is "every new summary" — no controls to show. The logic normalizes the form to+ // every_match to match (actionEditorSceneLogic.setScannerType).if (scanner?.scanner_type === 'summarizer') {return (<div className="flex flex-col gap-2">@@ -416,7 +429,9 @@ function ConditionSection({ scannerId }: { scannerId: string }): JSX.Element {}return (- <div className="flex flex-col gap-2">+ <div className="flex flex-col gap-3">+ <AlertMatchLine scannerId={scannerId} />+<LemonSegmentedButtonsize="small"value={actionForm.alert_frequency}@@ -428,53 +443,53 @@ function ConditionSection({ scannerId }: { scannerId: string }): JSX.Element {}}}options={[- { value: AlertConfigFrequencyEnumApi.EveryMatch, label: 'Every new match' },- { value: AlertConfigFrequencyEnumApi.OnBreach, label: 'When a threshold is crossed' },+ { value: AlertConfigFrequencyEnumApi.EveryMatch, label: 'Notify me on every match' },+ { value: AlertConfigFrequencyEnumApi.OnBreach, label: 'Notify me on a threshold' },]}data-attr="vision-action-alert-frequency"/>- {everyMatch ? (- <div className="flex flex-wrap items-center gap-2">- <span className="text-sm">Notify me about every new observation</span>- <MatchControls scannerId={scannerId} />- </div>- ) : (+ {!everyMatch && (<div className="flex flex-wrap items-center gap-2">- <span className="text-sm">Notify me when</span>+ <span className="text-sm">when the</span>{isScorer ? (<LemonSelectsize="small"value={actionForm.alert_metric}- onChange={(value) => value && setActionFormValue('alert_metric', value)}+ onChange={(value) => {+ if (!value) {+ return+ }+ setActionFormValue('alert_metric', value)+ // Count thresholds are always "at least" (the direction control is+ // hidden for them), so switching back to count clears any "at most".+ if (value === VisionAlertMetricEnumApi.Count) {+ setActionFormValue('alert_direction', VisionAlertDirectionEnumApi.Above)+ }+ }}options={[- { value: VisionAlertMetricEnumApi.Count, label: 'the number of observations' },- { value: VisionAlertMetricEnumApi.AvgScore, label: 'the average score' },+ { value: VisionAlertMetricEnumApi.Count, label: 'number of matches' },+ { value: VisionAlertMetricEnumApi.AvgScore, label: 'average score' },]}data-attr="vision-action-alert-metric"/>) : (- <span className="text-sm">the number of observations</span>+ <span className="text-sm">number of matches</span>+ )}+ {isAvg ? (+ <LemonSelect+ size="small"+ value={actionForm.alert_direction}+ onChange={(value) => value && setActionFormValue('alert_direction', value)}+ options={[+ { value: VisionAlertDirectionEnumApi.Above, label: 'is at least' },+ { value: VisionAlertDirectionEnumApi.Below, label: 'is at most' },+ ]}+ data-attr="vision-action-alert-direction"+ />+ ) : (+ <span className="text-sm">is at least</span>)}- <MatchControls scannerId={scannerId} />- <span className="text-sm">over</span>- <LemonSelect- size="small"- value={actionForm.alert_window_days}- onChange={(value) => value != null && setActionFormValue('alert_window_days', value)}- options={WINDOW_OPTIONS}- data-attr="vision-action-alert-window"- />- <LemonSelect- size="small"- value={actionForm.alert_direction}- onChange={(value) => value && setActionFormValue('alert_direction', value)}- options={[- { value: VisionAlertDirectionEnumApi.Above, label: 'is at least' },- { value: VisionAlertDirectionEnumApi.Below, label: 'is at most' },- ]}- data-attr="vision-action-alert-direction"- /><LemonInputtype="number"size="small"@@ -482,11 +497,20 @@ function ConditionSection({ scannerId }: { scannerId: string }): JSX.Element {onChange={(val) =>setActionFormValue('alert_threshold', val === undefined || isNaN(val) ? null : val)}- className="w-24"+ className="w-20"data-attr="vision-action-alert-threshold"/>+ <span className="text-sm">over</span>+ <LemonSelect+ size="small"+ value={actionForm.alert_window_days}+ onChange={(value) => value != null && setActionFormValue('alert_window_days', value)}+ options={WINDOW_OPTIONS}+ data-attr="vision-action-alert-window"+ /></div>)}+{actionFormErrors?.alert_threshold ? (<span className="text-xs text-danger">{String(actionFormErrors.alert_threshold)}</span>) : null}diff --git a/products/replay_vision/frontend/replay_scanners/actionEditorSceneLogic.ts b/products/replay_vision/frontend/replay_scanners/actionEditorSceneLogic.tsindex 481a3b44ab62..70b253fe244f 100644--- a/products/replay_vision/frontend/replay_scanners/actionEditorSceneLogic.ts+++ b/products/replay_vision/frontend/replay_scanners/actionEditorSceneLogic.ts@@ -387,6 +387,15 @@ export const actionEditorSceneLogic = kea<actionEditorSceneLogicType>([selection?.max_score != null)actions.setTargetingMode(hasFilter ? 'filtered' : 'all')+ // Stored alerts without a frequency predate the field and behaved as on_breach; anything+ // else gets the fresh-form default so flipping a summary to an alert starts at every_match.+ const alertFrequency =+ action.alert_config?.frequency ??+ (action.mode === VisionActionModeEnumApi.Alert+ ? AlertConfigFrequencyEnumApi.OnBreach+ : AlertConfigFrequencyEnumApi.EveryMatch)+ const alertMetric = action.alert_config?.metric ?? VisionAlertMetricEnumApi.Count+ const alertDirection = action.alert_config?.direction ?? VisionAlertDirectionEnumApi.Aboveactions.setActionFormValues({name: action.name,cadence: parseRruleToCadence(action.trigger_config?.rrule),@@ -395,16 +404,10 @@ export const actionEditorSceneLogic = kea<actionEditorSceneLogicType>([integration_id: action.delivery_config?.[0]?.integration_id ?? null,channel: action.delivery_config?.[0]?.channel ?? '',mode: action.mode ?? VisionActionModeEnumApi.GroupSummary,- // Stored alerts without a frequency predate the field and behaved as on_breach; anything- // else gets the fresh-form default so flipping a summary to an alert starts at every_match.- alert_frequency:- action.alert_config?.frequency ??- (action.mode === VisionActionModeEnumApi.Alert- ? AlertConfigFrequencyEnumApi.OnBreach- : AlertConfigFrequencyEnumApi.EveryMatch),- alert_metric: action.alert_config?.metric ?? VisionAlertMetricEnumApi.Count,+ alert_frequency: alertFrequency,+ alert_metric: alertMetric,alert_threshold: action.alert_config?.threshold ?? 1,- alert_direction: action.alert_config?.direction ?? VisionAlertDirectionEnumApi.Above,+ alert_direction: alertDirection,alert_window_days: action.alert_config?.window_days ?? 1,verdict: action.selection?.verdict ?? [],tags: action.selection?.tags ?? [],diff --git a/products/replay_vision/frontend/replay_scanners/visionActionsLogic.test.ts b/products/replay_vision/frontend/replay_scanners/visionActionsLogic.test.tsindex bda96d0b5ff3..5ba07ed5d0ef 100644--- a/products/replay_vision/frontend/replay_scanners/visionActionsLogic.test.ts+++ b/products/replay_vision/frontend/replay_scanners/visionActionsLogic.test.ts@@ -183,6 +183,9 @@ describe('visionActionsLogic', () => {alert_frequency: 'on_breach',alert_metric: 'count',alert_threshold: 1,+ // A stale "below" on a count metric (e.g. carried over from an avg-score edit) must not+ // persist — "at most N matches" is a confusing quiet-window alarm, so counts are pinned to+ // "at least". Only the average score exposes a direction choice.alert_direction: 'below',alert_window_days: 1,}@@ -192,7 +195,7 @@ describe('visionActionsLogic', () => {frequency: 'on_breach',metric: 'count',threshold: 1,- direction: 'below',+ direction: 'above',window_days: 1,})expect(body.selection).toEqual({ tags: ['rage-click'] })@@ -205,5 +208,9 @@ describe('visionActionsLogic', () => {// Every-match alerts carry no threshold machinery — just the frequency and the count metric.const everyMatch = buildActionBody({ ...form, alert_frequency: 'every_match' }, 's1')expect(everyMatch.alert_config).toEqual({ frequency: 'every_match', metric: 'count' })++ // The average score keeps the user's direction choice — "below a floor" is its natural alarm.+ const avgBelow = buildActionBody({ ...form, alert_metric: 'avg_score', alert_direction: 'below' }, 's1')+ expect(avgBelow.alert_config).toEqual(expect.objectContaining({ metric: 'avg_score', direction: 'below' }))})})diff --git a/products/replay_vision/frontend/replay_scanners/visionActionsLogic.ts b/products/replay_vision/frontend/replay_scanners/visionActionsLogic.tsindex ea1bba6debb6..2dbfdec5bb62 100644--- a/products/replay_vision/frontend/replay_scanners/visionActionsLogic.ts+++ b/products/replay_vision/frontend/replay_scanners/visionActionsLogic.ts@@ -107,7 +107,14 @@ export function buildActionBody(form: VisionActionForm, scannerId: string): Parafrequency: form.alert_frequency,metric: form.alert_metric,threshold: form.alert_threshold ?? 1,- direction: form.alert_direction,+ // Direction is only a user choice for the average score (where "below a+ // floor" is the natural case). A count threshold is always "at least" —+ // "at most N matches" is a confusing quiet-window alarm — so pin it,+ // ignoring any stale below a loaded config might carry.+ direction:+ form.alert_metric === VisionAlertMetricEnumApi.AvgScore+ ? form.alert_direction+ : VisionAlertDirectionEnumApi.Above,window_days: form.alert_window_days,},}