Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(visual-review): refuse a quarantine lift that cannot be scoped to a commit (#111770)

Julian Bez4e4016106d27
diff --git a/products/visual_review/backend/facade/api.py b/products/visual_review/backend/facade/api.py
index 945db4e33676..0d5e817e8599 100644
--- a/products/visual_review/backend/facade/api.py
+++ b/products/visual_review/backend/facade/api.py
@@ -93,6 +93,7 @@ QuarantineLiftRequestNotFoundError = errors.QuarantineLiftRequestNotFoundError
ArtifactNotFoundError = errors.ArtifactNotFoundError
GitHubIntegrationNotFoundError = errors.GitHubIntegrationNotFoundError
GitHubCommitError = errors.GitHubCommitError
+LiftCommitUnknownError = errors.LiftCommitUnknownError
GitHubRateLimitError = GitHubRateLimitError
PRSHAMismatchError = errors.PRSHAMismatchError
StaleRunError = errors.StaleRunError
diff --git a/products/visual_review/backend/logic/errors.py b/products/visual_review/backend/logic/errors.py
index 55d0f1294c6e..cef6d0fc3510 100644
--- a/products/visual_review/backend/logic/errors.py
+++ b/products/visual_review/backend/logic/errors.py
@@ -25,6 +25,12 @@ class GitHubIntegrationNotFoundError(Exception):
pass
+class LiftCommitUnknownError(Exception):
+ """GitHub cannot name the default branch head, so a quarantine lift cannot be scoped to a commit."""
+
+ pass
+
+
class GitHubCommitError(Exception):
"""Failed to commit to GitHub."""
diff --git a/products/visual_review/backend/logic/quarantine.py b/products/visual_review/backend/logic/quarantine.py
index 1395c0caf590..ce67e5d43938 100644
--- a/products/visual_review/backend/logic/quarantine.py
+++ b/products/visual_review/backend/logic/quarantine.py
@@ -13,7 +13,7 @@ from django.utils import timezone
from ..db import READER_DB, WRITER_DB
from ..facade.contracts import AGENT_QUARANTINE_MAX_DAYS, FLAKINESS_EXPIRY_SOON_DAYS
from ..facade.enums import ActorType
-from ..models import QuarantinedIdentifier, Run
+from ..models import QuarantinedIdentifier, Repo, Run
from . import errors, github_api, repos
from .run_queries import SnapshotKey
@@ -177,12 +177,24 @@ def quarantine_identifier(
return entry
+def _lift_commit(repo: Repo) -> str:
+ # A lift without a commit would apply to every branch, including branches that forked before
+ # the fix and still render the old picture. Refuse it, so the quarantine stays.
+ lifted_at_sha = github_api.default_branch_head_sha(repo)
+ if lifted_at_sha is None:
+ raise errors.LiftCommitUnknownError(
+ "GitHub cannot name the default branch head, so the lift cannot be scoped to a commit. "
+ "The quarantine stays. Try again in a minute."
+ )
+ return lifted_at_sha
+
+
def unquarantine_identifier(repo_id: UUID, identifier: str, run_type: str, team_id: int) -> None:
repo = repos.get_repo(repo_id, team_id) # raises RepoNotFoundError if repo not owned by team
# Take the cutoff before the GitHub request, and lift only rows that existed at the cutoff. A
# quarantine that somebody creates while the request is in flight must survive this lift.
now = timezone.now()
- lifted_at_sha = github_api.default_branch_head_sha(repo)
+ lifted_at_sha = _lift_commit(repo)
QuarantinedIdentifier.objects.using(WRITER_DB).filter(
repo_id=repo_id,
identifier=identifier,
@@ -202,7 +214,7 @@ def expire_quarantine_entry(entry_id: UUID, team_id: int) -> None:
# The cutoff is `now`, taken before the GitHub request, for the same reason as in
# `unquarantine_identifier`.
- lifted_at_sha = github_api.default_branch_head_sha(entry.repo)
+ lifted_at_sha = _lift_commit(entry.repo)
# Expire all active entries for the same identifier/run_type, not just this one
QuarantinedIdentifier.objects.using(WRITER_DB).filter(
repo_id=entry.repo_id,
diff --git a/products/visual_review/backend/presentation/views.py b/products/visual_review/backend/presentation/views.py
index d914544580b9..494f7b0e64bc 100644
--- a/products/visual_review/backend/presentation/views.py
+++ b/products/visual_review/backend/presentation/views.py
@@ -348,6 +348,10 @@ class RepoViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
)
except api.RepoNotFoundError:
return Response({"detail": "Repo not found"}, status=status.HTTP_404_NOT_FOUND)
+ except api.LiftCommitUnknownError as e:
+ return Response(
+ {"detail": str(e), "code": "lift_commit_unknown"}, status=status.HTTP_503_SERVICE_UNAVAILABLE
+ )
return Response(status=status.HTTP_204_NO_CONTENT)
@extend_schema(
diff --git a/products/visual_review/backend/tests/logic/test_gating.py b/products/visual_review/backend/tests/logic/test_gating.py
index 87e978a9f228..f5d58033a6cf 100644
--- a/products/visual_review/backend/tests/logic/test_gating.py
+++ b/products/visual_review/backend/tests/logic/test_gating.py
@@ -83,7 +83,7 @@ class TestQuarantineStamping:
("lift", "older", True, False),
],
ids=[
- "lift-without-sha",
+ "legacy-lift-without-sha",
"run-contains-lift",
"branch-forked-before-lift",
"ancestry-unknown",
@@ -95,7 +95,7 @@ class TestQuarantineStamping:
):
from products.visual_review.backend.models import QuarantinedIdentifier
- mocker.patch.object(github_api, "default_branch_head_sha", return_value=lifted_at_sha)
+ mocker.patch.object(github_api, "default_branch_head_sha", return_value=lifted_at_sha or "lift")
mocker.patch.object(github_api, "get_github_integration_for_repo")
mocker.patch.object(github_api, "_get_merge_base_sha", return_value=merge_base)
@@ -124,6 +124,8 @@ class TestQuarantineStamping:
quarantine.unquarantine_identifier(
repo_id=repo.id, identifier="Button-primary", run_type=RunType.STORYBOOK, team_id=team.id
)
+ if lifted_at_sha is None:
+ QuarantinedIdentifier.objects.filter(identifier="Button-primary").update(lifted_at_sha=None)
if superseded:
QuarantinedIdentifier.objects.create(
repo=repo,
diff --git a/products/visual_review/backend/tests/test_presentation.py b/products/visual_review/backend/tests/test_presentation.py
index f40b663a41de..8f835c381f91 100644
--- a/products/visual_review/backend/tests/test_presentation.py
+++ b/products/visual_review/backend/tests/test_presentation.py
@@ -28,7 +28,7 @@ from products.visual_review.backend.facade.enums import (
RunType,
SnapshotResult,
)
-from products.visual_review.backend.logic import artifact_store, quarantine, runs
+from products.visual_review.backend.logic import artifact_store, github_api, quarantine, runs
from products.visual_review.backend.models import Run, RunSnapshot
from products.visual_review.backend.tests.conftest import PRODUCT_DATABASES, VisualReviewTeamScopedTestMixin
@@ -89,7 +89,13 @@ class TestRepoViewSet(VisualReviewTeamScopedTestMixin, APIBaseTest):
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
- def test_expire_quarantine_takes_only_an_identifier(self):
+ @parameterized.expand(
+ [
+ ("head_known", "abc123", status.HTTP_204_NO_CONTENT, []),
+ ("head_unknown", None, status.HTTP_503_SERVICE_UNAVAILABLE, ["Button"]),
+ ]
+ )
+ def test_expire_quarantine_takes_only_an_identifier(self, _name, head_sha, expected_status, expected_active):
repo = api.create_repo(team_id=self.team.id, repo_external_id=444, repo_full_name="org/expire")
quarantine.quarantine_identifier(
repo_id=repo.id,
@@ -100,14 +106,16 @@ class TestRepoViewSet(VisualReviewTeamScopedTestMixin, APIBaseTest):
team_id=self.team.id,
)
- response = self.client.post(
- f"/api/projects/{self.team.id}/visual_review/repos/{repo.id}/quarantine/{RunType.STORYBOOK}/expire",
- {"identifier": "Button"},
- format="json",
- )
+ with patch.object(github_api, "default_branch_head_sha", return_value=head_sha):
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/visual_review/repos/{repo.id}/quarantine/{RunType.STORYBOOK}/expire",
+ {"identifier": "Button"},
+ format="json",
+ )
- assert response.status_code == status.HTTP_204_NO_CONTENT
- assert quarantine.list_quarantined_identifiers(repo.id, team_id=self.team.id) == []
+ assert response.status_code == expected_status
+ active = quarantine.list_quarantined_identifiers(repo.id, team_id=self.team.id)
+ assert [entry.identifier for entry in active] == expected_active
@parameterized.expand(
[
diff --git a/products/visual_review/mcp/tools.yaml b/products/visual_review/mcp/tools.yaml
index 6ba9be624180..8dcd3da1da99 100644
--- a/products/visual_review/mcp/tools.yaml
+++ b/products/visual_review/mcp/tools.yaml
@@ -150,9 +150,12 @@ tools:
idempotent: true
title: Lift a visual review quarantine
description: >
- Lift every active quarantine on one identifier and run type, so the snapshot gates runs again. Lift one
- after the fix that stabilizes the story merges, or when its flakiness entry has `needs_decision`. If the
- story still flakes, the next pull request that renders it fails.
+ Lift every active quarantine on one identifier and run type. The lift is scoped to the current
+ default-branch commit: a run whose commit contains it gates the snapshot again, and a branch that forked
+ earlier keeps the quarantine for up to 30 days or until it merges the default branch. Lift one after the fix
+ that stabilizes the story merges, or when its flakiness entry has `needs_decision`. If the story still
+ flakes, the next pull request that contains the lift and renders it fails. On `503 lift_commit_unknown`
+ GitHub could not name the default-branch head, so the quarantine stays: retry later.
param_overrides:
id:
description: The repo's UUID, from `visual-review-repos-list`.
diff --git a/services/mcp/schema/generated-tool-definitions.json b/services/mcp/schema/generated-tool-definitions.json
index 4396f7b1d79f..4b3c6a70f1d2 100644
--- a/services/mcp/schema/generated-tool-definitions.json
+++ b/services/mcp/schema/generated-tool-definitions.json
@@ -13708,7 +13708,7 @@
"feature_flag": "visual-review"
},
"visual-review-repos-quarantine-expire-create": {
- "description": "Lift every active quarantine on one identifier and run type, so the snapshot gates runs again. Lift one after the fix that stabilizes the story merges, or when its flakiness entry has `needs_decision`. If the story still flakes, the next pull request that renders it fails.",
+ "description": "Lift every active quarantine on one identifier and run type. The lift is scoped to the current default-branch commit: a run whose commit contains it gates the snapshot again, and a branch that forked earlier keeps the quarantine for up to 30 days or until it merges the default branch. Lift one after the fix that stabilizes the story merges, or when its flakiness entry has `needs_decision`. If the story still flakes, the next pull request that contains the lift and renders it fails. On `503 lift_commit_unknown` GitHub could not name the default-branch head, so the quarantine stays: retry later.",
"category": "Visual review",
"feature": "visual_review",
"summary": "Lift a visual review quarantine",
diff --git a/services/mcp/schema/tool-definitions-all.json b/services/mcp/schema/tool-definitions-all.json
index 870812e298f4..54507e944d73 100644
--- a/services/mcp/schema/tool-definitions-all.json
+++ b/services/mcp/schema/tool-definitions-all.json
@@ -14386,7 +14386,7 @@
"feature_flag": "visual-review"
},
"visual-review-repos-quarantine-expire-create": {
- "description": "Lift every active quarantine on one identifier and run type, so the snapshot gates runs again. Lift one after the fix that stabilizes the story merges, or when its flakiness entry has `needs_decision`. If the story still flakes, the next pull request that renders it fails.",
+ "description": "Lift every active quarantine on one identifier and run type. The lift is scoped to the current default-branch commit: a run whose commit contains it gates the snapshot again, and a branch that forked earlier keeps the quarantine for up to 30 days or until it merges the default branch. Lift one after the fix that stabilizes the story merges, or when its flakiness entry has `needs_decision`. If the story still flakes, the next pull request that contains the lift and renders it fails. On `503 lift_commit_unknown` GitHub could not name the default-branch head, so the quarantine stays: retry later.",
"category": "Visual review",
"feature": "visual_review",
"summary": "Lift a visual review quarantine",