: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(visual-review): fail the merge queue on snapshot drift (#88782)
Julian Bezc898ce37ad23
diff --git a/.github/workflows/ci-storybook.yml b/.github/workflows/ci-storybook.ymlindex 9cbda31f8647..9a6675203da3 100644--- a/.github/workflows/ci-storybook.yml+++ b/.github/workflows/ci-storybook.yml@@ -792,22 +792,27 @@ jobs:# Pass the same --purpose vr-setup created the run with. The backend reports zero# unresolved on an observe run however many snapshots drifted, so without the flag# the CLI cannot tell a clean tracking run from a drifting one and reports neither.- # With it, drift becomes a warning annotation naming the identifiers and the command- # still exits 0, so the run stays non-gating without the finding being dropped.- # That signal matters because an identifier drifting on master without being approved- # or quarantined reds the next PR that renders it.#- # No exit-code branching: an observe run cannot exit 1, so every non-zero exit here- # is a real failure (exit 1 unresolved on a review run, exit 2 CLI error) and reds CI.+ # An observe run fails on drift. The merge-queue leg renders the merged tree that is+ # about to land, and it is the only leg that renders every story, so drift there is+ # the merge itself moving pictures that no reviewer approved. Quarantine an+ # identifier that flips between runs; that excludes it from these counts.+ #+ # --tolerate-drift exempts master: no merge is left to stop there, and a red job+ # would block the repair as well. Exit 2 is a CLI failure and reds CI everywhere.env:VR_TOKEN: ${{ secrets.VR_API_TOKEN }}RUN_ID: ${{ needs.vr-setup.outputs.run_id }}VR_OBSERVE: ${{ github.event_name == 'push' || needs.changes.outputs.merge_queue == 'true' }}+ VR_TOLERATE_DRIFT: ${{ github.event_name == 'push' }}run: |args=(--run-id "$RUN_ID" --baseline frontend/snapshots.yml --token "$VR_TOKEN")if [ "$VR_OBSERVE" = 'true' ]; thenargs+=(--purpose observe)fi+ if [ "$VR_TOLERATE_DRIFT" = 'true' ]; then+ args+=(--tolerate-drift)+ finode vr-cli/products/visual_review/cli/dist/cli/src/index.js run complete "${args[@]}"calculate-running-time:diff --git a/products/visual_review/README.md b/products/visual_review/README.mdindex faadb1a0a6bc..c1fe8f1a5600 100644--- a/products/visual_review/README.md+++ b/products/visual_review/README.md@@ -103,7 +103,7 @@ The CLI uploads directly to S3 via presigned POST URLs — the backend never pro### Commands-**`vr submit`** — single-command flow. Scans a directory, hashes PNGs, creates a run with full manifest, uploads, and completes. Default `--purpose review` (gating, exits 1 on unapproved changes). Pass `--purpose observe` on master/non-PR runs for tracking-only (won't block, won't prompt for approval). Pass `--auto-approve` to approve everything and write the signed baseline (forces `--purpose review`).+**`vr submit`** — single-command flow. Scans a directory, hashes PNGs, creates a run with full manifest, uploads, and completes. Default `--purpose review` (gating, exits 1 on unapproved changes). Pass `--purpose observe` on master/non-PR runs for tracking-only (no approval prompt). An observe run still exits 1 on snapshot drift, so pass `--tolerate-drift` too where a red job is not wanted. Pass `--auto-approve` to approve everything and write the signed baseline (forces `--purpose review`).**`vr verify`** — local baseline check without API. Hashes PNGs in a directory and compares against `snapshots.yml`. No backend involvement.@@ -114,13 +114,15 @@ The CLI uploads directly to S3 via presigned POST URLs — the backend never pro**`vr run complete`** — triggers completion (classification, removal detection, diffs).Exits 1 if unapproved changes are detected, 0 if clean or `--auto-approve` is set, and 2 if the command itself failed (auth, network, timeout, backend processing).Pass the same `--purpose` the run was created with.-On `--purpose observe` the command names the drifted identifiers, emits a `::warning::` annotation, and exits 0, because a tracking-only run has nothing to approve and must not gate.-Without the flag it reports nothing on such a run: the backend reports zero unresolved for an observe run whatever drifted, so a clean run and a drifting one look identical.+On `--purpose observe` the command names the drifted identifiers, emits a `::warning::` annotation, and exits 1.+The CLI has to be the one to say so: the backend reports zero unresolved for an observe run whatever drifted, so a clean run and a drifting one look identical to it.+Add `--tolerate-drift` to report the drift and still exit 0. Use it on the default branch, where there is no merge left to stop and a red job would block the repair too.### Run purposes- **`review`** (default) — approvable. Backend posts PR comment prompts; UI surfaces it under "needs review"; CLI gates on unapproved changes.-- **`observe`** — tracking only. Backend rejects approval attempts; no PR comment; excluded from "needs review". The commit status is posted green (`success`, "Tracking only…") to a separate, non-gating `… (tracking)` context — never the gating `PostHog Visual Review / {run_type}` one. `purpose` is client-supplied, so greening the gating context would let an observe run bypass branch protection on a PR head SHA; the separate context keeps observe runs informational-only (like `(partial)` runs). The UI hides all approval affordances. Use on master pushes where there's no PR to approve.+- **`observe`** — tracking only. Backend rejects approval attempts; no PR comment; excluded from "needs review". The commit status is posted green (`success`, "Tracking only…") to a separate, non-gating `… (tracking)` context — never the gating `PostHog Visual Review / {run_type}` one. `purpose` is client-supplied, so greening the gating context would let an observe run bypass branch protection on a PR head SHA; the separate context keeps observe runs informational-only (like `(partial)` runs). The UI hides all approval affordances. Use on master pushes and merge-queue branches, where there's no PR to approve.+ The commit status never gates, but the exit code of `vr run complete` still does, and that is where a caller chooses. A merge-queue branch renders the tree about to land, so it lets drift fail the job. Master passes `--tolerate-drift` instead.## Current statediff --git a/products/visual_review/cli/src/index.ts b/products/visual_review/cli/src/index.tsindex 30b674f523d8..db3dd253d5f4 100644--- a/products/visual_review/cli/src/index.ts+++ b/products/visual_review/cli/src/index.ts@@ -37,6 +37,7 @@ program.option('--token <value>', 'Personal API token (Authorization: Bearer)').option('--cookie <value>', 'Session cookie for authentication').option('--purpose <purpose>', 'Run purpose: review (gating, approvable) or observe (tracking only)', 'review')+ .option('--tolerate-drift', 'Report snapshot drift on an observe run without failing. Default branch only.').option('--auto-approve', 'Auto-approve all changes and write signed baseline').action(async (options: SubmitOptions) => {if (!baselineExists(options.baseline)) {@@ -133,6 +134,7 @@ run.command('complete').option('--token <value>', 'Personal API token').option('--cookie <value>', 'Session cookie').option('--purpose <purpose>', 'Run purpose: review or observe. Must match `run create`.', 'review')+ .option('--tolerate-drift', 'Report snapshot drift on an observe run without failing. Default branch only.').option('--auto-approve', 'Auto-approve all changes and write signed baseline').action(async (options: RunCompleteOptions) => {if (!baselineExists(options.baseline)) {@@ -171,6 +173,7 @@ interface SubmitOptions {token?: stringcookie?: stringpurpose?: string+ tolerateDrift?: booleanautoApprove?: boolean}@@ -207,6 +210,7 @@ interface RunCompleteOptions {token?: stringcookie?: stringpurpose?: string+ tolerateDrift?: booleanautoApprove?: boolean}@@ -433,7 +437,8 @@ async function runComplete(options: RunCompleteOptions): Promise<number> {client,run,`${api}/project/${team}/visual_review/runs/${runId}`,- options.purpose ?? 'review'+ options.purpose ?? 'review',+ options.tolerateDrift ?? false)}@@ -656,5 +661,11 @@ async function runSubmit(options: SubmitOptions): Promise<number> {return 0}- return reportRunOutcome(client, run, `${api}/project/${team}/visual_review/runs/${runId}`, purpose)+ return reportRunOutcome(+ client,+ run,+ `${api}/project/${team}/visual_review/runs/${runId}`,+ purpose,+ options.tolerateDrift ?? false+ )}diff --git a/products/visual_review/cli/src/outcome.test.ts b/products/visual_review/cli/src/outcome.test.tsindex c650a464edbb..61b0573c1cbb 100644--- a/products/visual_review/cli/src/outcome.test.ts+++ b/products/visual_review/cli/src/outcome.test.ts@@ -47,12 +47,13 @@ describe('reportRunOutcome', () => {})// The mirror image: netting tolerated matches off real drift hid it entirely.- it('warns about drift on an observe run even when tolerated matches exist', async () => {+ it('warns about drift on a tolerating observe run even when tolerated matches exist', async () => {const exitCode = await reportRunOutcome(client,run({ unchanged: 4113, total: 4115, changed: 2, tolerated_matched: 14 }),'https://vr.example.com/run-1',- 'observe'+ 'observe',+ true)expect(exitCode).toBe(0)@@ -60,6 +61,29 @@ describe('reportRunOutcome', () => {expect(output).toContain('changed: story--light (12.07% diff)')})+ // The merge-queue case. The tree in the run is the tree about to land, so drift there has+ // to stop the merge rather than warn about it after the fact.+ it('fails an observe run whose drift is not tolerated', async () => {+ const exitCode = await reportRunOutcome(+ client,+ run({ unchanged: 4113, total: 4115, changed: 2 }),+ 'https://vr.example.com/run-1',+ 'observe'+ )++ expect(exitCode).toBe(1)+ expect(output).toContain('Unapproved snapshot drift on master')+ expect(output).toContain('changed: story--light (12.07% diff)')+ })++ it('passes a clean observe run whose drift would not be tolerated', async () => {+ const exitCode = await reportRunOutcome(client, run(), 'https://vr.example.com/run-1', 'observe')++ expect(exitCode).toBe(0)+ expect(output).toContain('No visual changes')+ expect(output).not.toContain('::warning::')+ })+it('gates a review run with unresolved changes', async () => {const exitCode = await reportRunOutcome(client,diff --git a/products/visual_review/cli/src/outcome.ts b/products/visual_review/cli/src/outcome.tsindex 0190ef4fe17a..1cc1014b1990 100644--- a/products/visual_review/cli/src/outcome.ts+++ b/products/visual_review/cli/src/outcome.ts@@ -60,9 +60,14 @@ function warn(message: string): void {* Report what the run found and return the exit code.** Observe (tracking-only) runs have nothing to approve, so the backend reports zero unresolved- * however many snapshots drifted, and they must not fail the job. They still have to leave a- * trace: an identifier that drifts on the default branch without being approved or quarantined- * fails the next PR that renders it.+ * however many snapshots drifted. The drift still has to stop something. A merge-queue run+ * renders the tree that is about to land, so drift there is the merge moving pictures nobody+ * approved, and the job must fail.+ *+ * `tolerateDrift` exempts the default branch: no merge is left to stop there, and a red job+ * would block the repair too. Such a run still names the drift, because an identifier that+ * drifts on the default branch without being approved or quarantined reds the next PR that+ * renders it.** The change counts are already the drift count on an observe run. A snapshot matching a* tolerated hash is reclassified `unchanged` by the server, so it never reaches `changed`, and@@ -75,7 +80,8 @@ export async function reportRunOutcome(client: VisualReviewClient,run: Run,reviewUrl: string,- purpose: string+ purpose: string,+ tolerateDrift = false): Promise<number> {const runId = run.idconst s = run.summary@@ -90,7 +96,7 @@ export async function reportRunOutcome(warn(`Unapproved snapshot drift on ${run.branch}: approve the new baseline, or quarantine the identifier if it flips between runs, before it fails an unrelated PR. Review at: ${reviewUrl}`)- return 0+ return tolerateDrift ? 0 : 1}const unresolved = s.unresolved ?? changes