: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): commit an approved quarantined new snapshot on finalize (#89504)
Julian Bez8daf66424995
diff --git a/products/visual_review/README.md b/products/visual_review/README.mdindex c1fe8f1a5600..c86b5d25699d 100644--- a/products/visual_review/README.md+++ b/products/visual_review/README.md@@ -134,6 +134,9 @@ Developers can also manually tolerate a snapshot from the UI.**Quarantine** — known-flaky identifiers can be quarantined per repo and run type.Quarantined snapshots are still captured and diffed but excluded from gating.+A quarantined snapshot is not committed to the baseline, with one exception: a quarantined `new` snapshot that a person approved by identifier.+This is the way to give a story a baseline entry when it has none and the quarantine must stay, because every run without the entry classifies the story `new`, and lifting the quarantine first fails every run until the entry lands.+The procedure is: open a PR that renders the story, approve the `new` snapshot on that run by identifier (the API or the `visual-review-runs-approve-create` MCP tool; "Approve all" skips quarantined snapshots), finalize the run so the entry is committed to the PR branch, merge the PR, then lift the quarantine.**Flakiness tab** — surfaces the tolerated-hash data, which is otherwise written and never read.A snapshot is scored on how many alternate hashes the classifier can still match for its current baseline, so the score resets when the baseline moves.diff --git a/products/visual_review/backend/logic/approvals.py b/products/visual_review/backend/logic/approvals.pyindex 3fa311f1d5dc..ab0ee1d94e8b 100644--- a/products/visual_review/backend/logic/approvals.py+++ b/products/visual_review/backend/logic/approvals.py@@ -30,7 +30,8 @@ def finalize_run(derived from DB state — exactly the snapshots with ``review_state == APPROVED``, bytheir approved hash — so a tolerated snapshot keeps its existing baseline and isnever silently overwritten, and the commit always contains the full approved set- regardless of how many calls it took to review them.+ regardless of how many calls it took to review them. A quarantined NEW snapshot that+ was approved by identifier is committed too (see ``_approved_baseline_updates``).With ``approve_all=True`` every still-pending changed/new snapshot is approved first(tolerated ones are left untouched) — the "approve everything and ship" path. Without@@ -92,8 +93,9 @@ def finalize_run()# Commit set is derived from DB state, not a caller-supplied list, so it always reflects- # the full approved set however many calls reviewed it.- approved_updates = baselines._approved_baseline_updates(actionable)+ # the full approved set however many calls reviewed it. It reads every snapshot, not only+ # the actionable ones, so an approved quarantined NEW snapshot reaches the commit.+ approved_updates = baselines._approved_baseline_updates(run.snapshots.using(WRITER_DB).all())has_removed = run.snapshots.using(WRITER_DB).filter(result=SnapshotResult.REMOVED).exists()# Commit first — before DB writes — so a GitHub failure aborts cleanly. Removed snapshotsdiff --git a/products/visual_review/backend/logic/baselines.py b/products/visual_review/backend/logic/baselines.pyindex d9166906412c..51550b7f7b11 100644--- a/products/visual_review/backend/logic/baselines.py+++ b/products/visual_review/backend/logic/baselines.py@@ -284,13 +284,18 @@ def _approved_baseline_updates(snapshots: Iterable[RunSnapshot]) -> list[dict]:Derived from DB state so the commit always reflects every approval regardless of howmany calls it took. Tolerated snapshots are excluded — toleration never updates the baseline.++ Quarantine keeps a flapping hash out of the baseline, so a quarantined CHANGED snapshot is+ excluded even when approved. A quarantined NEW snapshot has no baseline entry to protect,+ and only a per-identifier approval can mark it (bulk approve skips quarantined rows), so an+ approved one is committed. Without this a story that lost its entry while quarantined could+ never get one back: lifting the quarantine fails every run until the entry lands."""return [{"identifier": s.identifier, "new_hash": s.approved_hash}for s in snapshots- if s.result in (SnapshotResult.CHANGED, SnapshotResult.NEW)- and not s.is_quarantined- and s.review_state == ReviewState.APPROVED+ if s.review_state == ReviewState.APPROVED+ and (s.result == SnapshotResult.NEW or (s.result == SnapshotResult.CHANGED and not s.is_quarantined))]diff --git a/products/visual_review/backend/presentation/views.py b/products/visual_review/backend/presentation/views.pyindex 8c67f41aca17..41c7b85cb603 100644--- a/products/visual_review/backend/presentation/views.py+++ b/products/visual_review/backend/presentation/views.py@@ -662,7 +662,9 @@ class RunViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):"""Mark snapshots reviewed (DB only).Records the per-snapshot "Accept change" decision. Does not commit the baseline- or change the GitHub gate — call finalize to ship the run.+ or change the GitHub gate — call finalize to ship the run. Works on a quarantined+ snapshot too: a quarantined NEW snapshot approved here is committed by finalize,+ which gives a quarantined story a baseline entry without lifting the quarantine."""body = request.validated_datarun_id = _parse_uuid(pk)@@ -691,8 +693,10 @@ class RunViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):Commits exactly the snapshots approved in the DB (tolerated ones keep their baseline)and only succeeds once every changed/new snapshot is resolved. With approve_all=true,- any still-pending changed/new snapshot is approved first. With commit_to_github=false- the server returns the signed baseline YAML instead of committing it.+ any still-pending changed/new snapshot is approved first; quarantined snapshots are+ skipped, but a quarantined NEW snapshot approved by identifier is still committed.+ With commit_to_github=false the server returns the signed baseline YAML instead of+ committing it."""body = request.validated_datarun_id = _parse_uuid(pk)diff --git a/products/visual_review/backend/tests/logic/test_approvals.py b/products/visual_review/backend/tests/logic/test_approvals.pyindex f9e2521f95fd..013f78945d14 100644--- a/products/visual_review/backend/tests/logic/test_approvals.py+++ b/products/visual_review/backend/tests/logic/test_approvals.py@@ -9,6 +9,7 @@ from products.visual_review.backend.facade.enums import ReviewState, RunType, Snfrom products.visual_review.backend.logic import (approvals,artifact_store,+ baselines,ci_status,errors,repos,@@ -16,6 +17,7 @@ from products.visual_review.backend.logic import (runs,toleration,)+from products.visual_review.backend.models import QuarantinedIdentifierfrom products.visual_review.backend.tests.conftest import PRODUCT_DATABASES@@ -154,6 +156,62 @@ class TestApproveRun:assert delay.called is Trueassert delay.call_args.args[2] is add_images+ @pytest.mark.parametrize(+ ("result", "approve_all", "expect_committed"),+ [+ ("new", False, True),+ ("changed", False, False),+ ("new", True, False),+ ],+ )+ def test_finalize_commits_quarantined_new_only_when_approved_by_identifier(+ self, repo, user, mocker, result, approve_all, expect_committed+ ):+ # A quarantined NEW snapshot has no entry to protect, so an explicit approval lands in the+ # commit. A quarantined CHANGED one keeps its entry, and approve_all never touches quarantine.+ run = self._completed_quarantined_run(repo, mocker, result)+ if not approve_all:+ approvals.approve_snapshots(+ run_id=run.id, user_id=user.id, approved_snapshots=[{"identifier": "Q", "new_hash": "hq"}]+ )+ commit = mocker.patch.object(baselines, "_commit_baseline_to_github")+ mocker.patch.object(ci_status, "_post_commit_status")++ updated = approvals.finalize_run(run_id=run.id, user_id=user.id, approve_all=approve_all)++ assert updated.approved is True+ if expect_committed:+ assert commit.call_args.args[2] == [{"identifier": "Q", "new_hash": "hq"}]+ else:+ assert commit.called is False++ def _completed_quarantined_run(self, repo, mocker, result):+ artifact_store.get_or_create_artifact(repo_id=repo.id, content_hash="hq", storage_path="p/q")+ baseline = {} if result == "new" else {"Q": "oldq"}+ run, _ = runs.create_run(+ CreateRunInput(+ repo_id=repo.id,+ run_type=RunType.STORYBOOK,+ commit_sha="abc",+ branch="main",+ pr_number=7,+ snapshots=[SnapshotManifestItem(identifier="Q", content_hash="hq")],+ baseline_hashes=baseline,+ ),+ team_id=repo.team_id,+ )+ QuarantinedIdentifier.objects.create(+ repo=repo, team_id=repo.team_id, identifier="Q", run_type=RunType.STORYBOOK, reason="test"+ )+ mocker.patch(+ "products.visual_review.backend.logic.baselines._resolve_baselines_with_merge_base",+ return_value=(baseline, 0),+ )+ mocker.patch("products.visual_review.backend.tasks.tasks.process_run_diffs.delay")+ runs.complete_run(run.id)+ runs.finish_processing(run.id)+ return run+@pytest.mark.django_db(databases=PRODUCT_DATABASES)class TestApproveSnapshots:diff --git a/products/visual_review/frontend/generated/api.ts b/products/visual_review/frontend/generated/api.tsindex 36866b8679e0..bb4f65d757cc 100644--- a/products/visual_review/frontend/generated/api.ts+++ b/products/visual_review/frontend/generated/api.ts@@ -481,7 +481,9 @@ export const getVisualReviewRunsApproveCreateUrl = (projectId: string, id: strin* Mark snapshots reviewed (DB only).** Records the per-snapshot "Accept change" decision. Does not commit the baseline- * or change the GitHub gate — call finalize to ship the run.+ * or change the GitHub gate — call finalize to ship the run. Works on a quarantined+ * snapshot too: a quarantined NEW snapshot approved here is committed by finalize,+ * which gives a quarantined story a baseline entry without lifting the quarantine.*/export const visualReviewRunsApproveCreate = async (projectId: string,@@ -524,8 +526,10 @@ export const getVisualReviewRunsFinalizeCreateUrl = (projectId: string, id: stri** Commits exactly the snapshots approved in the DB (tolerated ones keep their baseline)* and only succeeds once every changed/new snapshot is resolved. With approve_all=true,- * any still-pending changed/new snapshot is approved first. With commit_to_github=false- * the server returns the signed baseline YAML instead of committing it.+ * any still-pending changed/new snapshot is approved first; quarantined snapshots are+ * skipped, but a quarantined NEW snapshot approved by identifier is still committed.+ * With commit_to_github=false the server returns the signed baseline YAML instead of+ * committing it.*/export const visualReviewRunsFinalizeCreate = async (projectId: string,diff --git a/products/visual_review/frontend/generated/api.zod.ts b/products/visual_review/frontend/generated/api.zod.tsindex e8c944cccbbd..e56ec20f0ace 100644--- a/products/visual_review/frontend/generated/api.zod.ts+++ b/products/visual_review/frontend/generated/api.zod.ts@@ -121,7 +121,9 @@ export const VisualReviewRunsAddSnapshotsCreateBody = /* @__PURE__ */ zod.object* Mark snapshots reviewed (DB only).** Records the per-snapshot "Accept change" decision. Does not commit the baseline- * or change the GitHub gate — call finalize to ship the run.+ * or change the GitHub gate — call finalize to ship the run. Works on a quarantined+ * snapshot too: a quarantined NEW snapshot approved here is committed by finalize,+ * which gives a quarantined story a baseline entry without lifting the quarantine.*/export const VisualReviewRunsApproveCreateBody = /* @__PURE__ */ zod.object({snapshots: zod@@ -145,8 +147,10 @@ export const VisualReviewRunsApproveCreateBody = /* @__PURE__ */ zod.object({** Commits exactly the snapshots approved in the DB (tolerated ones keep their baseline)* and only succeeds once every changed/new snapshot is resolved. With approve_all=true,- * any still-pending changed/new snapshot is approved first. With commit_to_github=false- * the server returns the signed baseline YAML instead of committing it.+ * any still-pending changed/new snapshot is approved first; quarantined snapshots are+ * skipped, but a quarantined NEW snapshot approved by identifier is still committed.+ * With commit_to_github=false the server returns the signed baseline YAML instead of+ * committing it.*/export const visualReviewRunsFinalizeCreateBodyApproveAllDefault = falseexport const visualReviewRunsFinalizeCreateBodyCommitToGithubDefault = truediff --git a/products/visual_review/mcp/tools.yaml b/products/visual_review/mcp/tools.yamlindex 3cbbfdf043cb..e66fed83c98b 100644--- a/products/visual_review/mcp/tools.yaml+++ b/products/visual_review/mcp/tools.yaml@@ -94,7 +94,9 @@ tools:`{identifier, new_hash}` — `new_hash` is the `content_hash` of the snapshot's `current_artifact`. This onlyrecords the review in the database (the per-snapshot "Accept change" action) — it does NOT update thebaseline, push a commit, or change the GitHub gate, and it can be re-run safely. To actually ship the visual- change (commit the baseline + green the gate), use `visual-review-runs-finalize-create`.+ change (commit the baseline + green the gate), use `visual-review-runs-finalize-create`. Approving a+ quarantined `new` snapshot here makes finalize commit its baseline entry while the quarantine stays in place+ — the way to give a quarantined story an entry without failing other runs.feature_flag: visual-reviewvisual-review-runs-complete-create:operation: visual_review_runs_complete_create@@ -131,12 +133,15 @@ tools:visual-review check. Commits exactly the snapshots approved in the DB (tolerated snapshots keep theirbaseline and are never overwritten) and is all-or-nothing — it only succeeds once every changed/new snapshotis resolved (approved, tolerated, or quarantined), otherwise it returns the unresolved list. With- `approve_all: true` every still-pending changed/new snapshot is approved first. When a baseline commit is- pushed its SHA comes back on the run's `metadata.baseline_commit_sha`; it's absent when nothing needed- committing (all changes resolved by toleration/quarantine — the gate still greens) or when the commit was- skipped (no PR, or a `409 sha_mismatch` from newer PR commits, which leaves the gate red — re-run CI and- finalize again). STOP: never finalize on your own initiative. Require explicit, per-run human confirmation- first — finalizing ships the visual change and rewrites the committed baseline.+ `approve_all: true` every still-pending changed/new snapshot is approved first; quarantined snapshots are+ skipped by that, but a quarantined `new` snapshot approved by identifier through+ `visual-review-runs-approve-create` is committed, so a quarantined story can get its baseline entry while+ the quarantine stays in place. When a baseline commit is pushed its SHA comes back on the run's+ `metadata.baseline_commit_sha`; it's absent when nothing needed committing (all changes resolved by+ toleration/quarantine — the gate still greens) or when the commit was skipped (no PR, or a `409+ sha_mismatch` from newer PR commits, which leaves the gate red — re-run CI and finalize again). STOP: never+ finalize on your own initiative. Require explicit, per-run human confirmation first — finalizing ships the+ visual change and rewrites the committed baseline.feature_flag: visual-reviewvisual-review-runs-list:operation: visual_review_runs_listdiff --git a/services/mcp/schema/generated-tool-definitions.json b/services/mcp/schema/generated-tool-definitions.jsonindex 5954cc97a800..90ebe8690a37 100644--- a/services/mcp/schema/generated-tool-definitions.json+++ b/services/mcp/schema/generated-tool-definitions.json@@ -11504,7 +11504,7 @@"feature_flag": "visual-review"},"visual-review-runs-approve-create": {- "description": "Mark specific snapshots as reviewed (approved) in a run. Pass a `snapshots` list, each entry being `{identifier, new_hash}` — `new_hash` is the `content_hash` of the snapshot's `current_artifact`. This only records the review in the database (the per-snapshot \"Accept change\" action) — it does NOT update the baseline, push a commit, or change the GitHub gate, and it can be re-run safely. To actually ship the visual change (commit the baseline + green the gate), use `visual-review-runs-finalize-create`.",+ "description": "Mark specific snapshots as reviewed (approved) in a run. Pass a `snapshots` list, each entry being `{identifier, new_hash}` — `new_hash` is the `content_hash` of the snapshot's `current_artifact`. This only records the review in the database (the per-snapshot \"Accept change\" action) — it does NOT update the baseline, push a commit, or change the GitHub gate, and it can be re-run safely. To actually ship the visual change (commit the baseline + green the gate), use `visual-review-runs-finalize-create`. Approving a quarantined `new` snapshot here makes finalize commit its baseline entry while the quarantine stays in place — the way to give a quarantined story an entry without failing other runs.","category": "Visual review","feature": "visual_review","summary": "Mark visual review snapshots reviewed",@@ -11534,7 +11534,7 @@"feature_flag": "visual-review"},"visual-review-runs-finalize-create": {- "description": "Finalize a fully-reviewed run: commit the approved baseline to the PR branch and green the GitHub visual-review check. Commits exactly the snapshots approved in the DB (tolerated snapshots keep their baseline and are never overwritten) and is all-or-nothing — it only succeeds once every changed/new snapshot is resolved (approved, tolerated, or quarantined), otherwise it returns the unresolved list. With `approve_all: true` every still-pending changed/new snapshot is approved first. When a baseline commit is pushed its SHA comes back on the run's `metadata.baseline_commit_sha`; it's absent when nothing needed committing (all changes resolved by toleration/quarantine — the gate still greens) or when the commit was skipped (no PR, or a `409 sha_mismatch` from newer PR commits, which leaves the gate red — re-run CI and finalize again). STOP: never finalize on your own initiative. Require explicit, per-run human confirmation first — finalizing ships the visual change and rewrites the committed baseline.",+ "description": "Finalize a fully-reviewed run: commit the approved baseline to the PR branch and green the GitHub visual-review check. Commits exactly the snapshots approved in the DB (tolerated snapshots keep their baseline and are never overwritten) and is all-or-nothing — it only succeeds once every changed/new snapshot is resolved (approved, tolerated, or quarantined), otherwise it returns the unresolved list. With `approve_all: true` every still-pending changed/new snapshot is approved first; quarantined snapshots are skipped by that, but a quarantined `new` snapshot approved by identifier through `visual-review-runs-approve-create` is committed, so a quarantined story can get its baseline entry while the quarantine stays in place. When a baseline commit is pushed its SHA comes back on the run's `metadata.baseline_commit_sha`; it's absent when nothing needed committing (all changes resolved by toleration/quarantine — the gate still greens) or when the commit was skipped (no PR, or a `409 sha_mismatch` from newer PR commits, which leaves the gate red — re-run CI and finalize again). STOP: never finalize on your own initiative. Require explicit, per-run human confirmation first — finalizing ships the visual change and rewrites the committed baseline.","category": "Visual review","feature": "visual_review","summary": "Finalize a visual review run",diff --git a/services/mcp/schema/tool-definitions-all.json b/services/mcp/schema/tool-definitions-all.jsonindex 57a6ada7a0de..9ddbeb150708 100644--- a/services/mcp/schema/tool-definitions-all.json+++ b/services/mcp/schema/tool-definitions-all.json@@ -12118,7 +12118,7 @@"feature_flag": "visual-review"},"visual-review-runs-approve-create": {- "description": "Mark specific snapshots as reviewed (approved) in a run. Pass a `snapshots` list, each entry being `{identifier, new_hash}` — `new_hash` is the `content_hash` of the snapshot's `current_artifact`. This only records the review in the database (the per-snapshot \"Accept change\" action) — it does NOT update the baseline, push a commit, or change the GitHub gate, and it can be re-run safely. To actually ship the visual change (commit the baseline + green the gate), use `visual-review-runs-finalize-create`.",+ "description": "Mark specific snapshots as reviewed (approved) in a run. Pass a `snapshots` list, each entry being `{identifier, new_hash}` — `new_hash` is the `content_hash` of the snapshot's `current_artifact`. This only records the review in the database (the per-snapshot \"Accept change\" action) — it does NOT update the baseline, push a commit, or change the GitHub gate, and it can be re-run safely. To actually ship the visual change (commit the baseline + green the gate), use `visual-review-runs-finalize-create`. Approving a quarantined `new` snapshot here makes finalize commit its baseline entry while the quarantine stays in place — the way to give a quarantined story an entry without failing other runs.","category": "Visual review","feature": "visual_review","summary": "Mark visual review snapshots reviewed",@@ -12148,7 +12148,7 @@"feature_flag": "visual-review"},"visual-review-runs-finalize-create": {- "description": "Finalize a fully-reviewed run: commit the approved baseline to the PR branch and green the GitHub visual-review check. Commits exactly the snapshots approved in the DB (tolerated snapshots keep their baseline and are never overwritten) and is all-or-nothing — it only succeeds once every changed/new snapshot is resolved (approved, tolerated, or quarantined), otherwise it returns the unresolved list. With `approve_all: true` every still-pending changed/new snapshot is approved first. When a baseline commit is pushed its SHA comes back on the run's `metadata.baseline_commit_sha`; it's absent when nothing needed committing (all changes resolved by toleration/quarantine — the gate still greens) or when the commit was skipped (no PR, or a `409 sha_mismatch` from newer PR commits, which leaves the gate red — re-run CI and finalize again). STOP: never finalize on your own initiative. Require explicit, per-run human confirmation first — finalizing ships the visual change and rewrites the committed baseline.",+ "description": "Finalize a fully-reviewed run: commit the approved baseline to the PR branch and green the GitHub visual-review check. Commits exactly the snapshots approved in the DB (tolerated snapshots keep their baseline and are never overwritten) and is all-or-nothing — it only succeeds once every changed/new snapshot is resolved (approved, tolerated, or quarantined), otherwise it returns the unresolved list. With `approve_all: true` every still-pending changed/new snapshot is approved first; quarantined snapshots are skipped by that, but a quarantined `new` snapshot approved by identifier through `visual-review-runs-approve-create` is committed, so a quarantined story can get its baseline entry while the quarantine stays in place. When a baseline commit is pushed its SHA comes back on the run's `metadata.baseline_commit_sha`; it's absent when nothing needed committing (all changes resolved by toleration/quarantine — the gate still greens) or when the commit was skipped (no PR, or a `409 sha_mismatch` from newer PR commits, which leaves the gate red — re-run CI and finalize again). STOP: never finalize on your own initiative. Require explicit, per-run human confirmation first — finalizing ships the visual change and rewrites the committed baseline.","category": "Visual review","feature": "visual_review","summary": "Finalize a visual review run",diff --git a/services/mcp/src/generated/visual_review/api.ts b/services/mcp/src/generated/visual_review/api.tsindex 8777c43e89e5..158d69d09118 100644--- a/services/mcp/src/generated/visual_review/api.ts+++ b/services/mcp/src/generated/visual_review/api.ts@@ -73,7 +73,9 @@ export const VisualReviewRunsRetrieveParams = /* @__PURE__ */ zod.object({* Mark snapshots reviewed (DB only).** Records the per-snapshot "Accept change" decision. Does not commit the baseline- * or change the GitHub gate — call finalize to ship the run.+ * or change the GitHub gate — call finalize to ship the run. Works on a quarantined+ * snapshot too: a quarantined NEW snapshot approved here is committed by finalize,+ * which gives a quarantined story a baseline entry without lifting the quarantine.*/export const VisualReviewRunsApproveCreateParams = /* @__PURE__ */ zod.object({id: zod.string(),@@ -106,8 +108,10 @@ export const VisualReviewRunsApproveCreateBody = /* @__PURE__ */ zod.object({** Commits exactly the snapshots approved in the DB (tolerated ones keep their baseline)* and only succeeds once every changed/new snapshot is resolved. With approve_all=true,- * any still-pending changed/new snapshot is approved first. With commit_to_github=false- * the server returns the signed baseline YAML instead of committing it.+ * any still-pending changed/new snapshot is approved first; quarantined snapshots are+ * skipped, but a quarantined NEW snapshot approved by identifier is still committed.+ * With commit_to_github=false the server returns the signed baseline YAML instead of+ * committing it.*/export const VisualReviewRunsFinalizeCreateParams = /* @__PURE__ */ zod.object({id: zod.string(),