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): address delayed review feedback (#72180)

Paul D'Ambra2eab44ce0d2e
diff --git a/products/visual_review/backend/logic.py b/products/visual_review/backend/logic.py
index b68ce1056630..fb38358e7e8b 100644
--- a/products/visual_review/backend/logic.py
+++ b/products/visual_review/backend/logic.py
@@ -950,13 +950,17 @@ def complete_run(run_id: UUID) -> Run:
# recorded — leaving every future run requesting the same upload while
# CI posts green.
if run.changed_count == 0 and run.new_count == 0:
+ from .tasks.tasks import emit_run_processing_metrics # noqa: PLC0415 — avoids the logic/tasks circular import
+
try:
verify_uploads_and_create_artifacts(run_id)
except HashIntegrityError as e:
logger.warning("visual_review.hash_integrity_failed", run_id=str(run_id), error=str(e))
finish_processing(run_id, error_message=str(e))
+ emit_run_processing_metrics.delay(run.team_id, str(run_id), "hash_integrity_failed", 0)
return get_run(run_id)
finish_processing(run_id)
+ emit_run_processing_metrics.delay(run.team_id, str(run_id), "completed", 0)
return get_run(run_id)
mark_run_processing(run_id)
@@ -1251,7 +1255,7 @@ def capture_run_processing_metrics(run_id: UUID, *, outcome: str, diffed_count:
"""
try:
try:
- run = Run.objects.select_related("repo").get(id=run_id)
+ run = Run.objects.using(WRITER_DB).select_related("repo").get(id=run_id)
except Run.DoesNotExist:
return
diff --git a/products/visual_review/backend/tasks/tasks.py b/products/visual_review/backend/tasks/tasks.py
index b3de0c9b28d2..26272611e6dd 100644
--- a/products/visual_review/backend/tasks/tasks.py
+++ b/products/visual_review/backend/tasks/tasks.py
@@ -23,6 +23,17 @@ logger = structlog.get_logger(__name__)
TRACER = trace.get_tracer(__name__)
+@shared_task(
+ name="products.visual_review.backend.tasks.emit_run_processing_metrics",
+ ignore_result=True,
+)
+@with_team_scope()
+def emit_run_processing_metrics(team_id: int, run_id: str, outcome: str, diffed_count: int) -> None:
+ from .. import logic # noqa: PLC0415 — avoids the logic/tasks circular import
+
+ logic.capture_run_processing_metrics(UUID(run_id), outcome=outcome, diffed_count=diffed_count)
+
+
@shared_task(
name="products.visual_review.backend.tasks.process_run_diffs",
bind=True,
@@ -47,8 +58,8 @@ def process_run_diffs(self, team_id: int, run_id: str) -> None:
run_uuid = UUID(run_id)
outcome = "completed"
- retrying = False
diffed_count = 0
+ retrying = False
# Phase timings go to OTel spans (where is time spent); counts go to the
# vr_run_processed event (how many runs, how many diffs).
@@ -79,14 +90,13 @@ def process_run_diffs(self, team_id: int, run_id: str) -> None:
retry=self.request.retries,
max_retries=self.max_retries,
)
- try:
+ if self.max_retries is not None and self.request.retries >= self.max_retries:
+ outcome = "rate_limit_exhausted"
+ logic.finish_processing(run_uuid, error_message="GitHub API rate limit exceeded after retries")
+ else:
retrying = True
countdown = e.retry_after or 60
self.retry(countdown=min(countdown, 600), exc=e)
- except self.MaxRetriesExceededError:
- retrying = False
- outcome = "rate_limit_exhausted"
- logic.finish_processing(run_uuid, error_message="GitHub API rate limit exceeded after retries")
except Exception as e:
outcome = "failed"
span.set_status(Status(StatusCode.ERROR, str(e)))
diff --git a/products/visual_review/backend/tests/test_api.py b/products/visual_review/backend/tests/test_api.py
index 9492a25eb5b1..6752ade9f84f 100644
--- a/products/visual_review/backend/tests/test_api.py
+++ b/products/visual_review/backend/tests/test_api.py
@@ -280,8 +280,9 @@ class TestRunAPI:
assert {r.commit_sha for r in runs} == expected_commits
+ @patch("products.visual_review.backend.tasks.tasks.emit_run_processing_metrics.delay")
@patch("products.visual_review.backend.tasks.tasks.process_run_diffs.delay")
- def test_complete_run_no_changes_skips_task(self, mock_delay, repo):
+ def test_complete_run_no_changes_skips_task(self, mock_diff_delay, mock_metrics_delay, repo):
"""Runs with no changes complete immediately without triggering diff task."""
create_result = api.create_run(
CreateRunInput(
@@ -297,7 +298,35 @@ class TestRunAPI:
result = api.complete_run(create_result.run_id)
assert result.status == "completed"
- mock_delay.assert_not_called()
+ mock_diff_delay.assert_not_called()
+ mock_metrics_delay.assert_called_once_with(repo.team_id, str(create_result.run_id), "completed", 0)
+
+ @patch("products.visual_review.backend.tasks.tasks.emit_run_processing_metrics.delay")
+ @patch(
+ "products.visual_review.backend.logic.verify_uploads_and_create_artifacts",
+ side_effect=logic.HashIntegrityError("hash mismatch"),
+ )
+ @patch("products.visual_review.backend.tasks.tasks.process_run_diffs.delay")
+ def test_complete_run_hash_integrity_failure_emits_terminal_metrics(
+ self, mock_diff_delay, _mock_verify, mock_metrics_delay, repo
+ ):
+ create_result = api.create_run(
+ CreateRunInput(
+ repo_id=repo.id,
+ run_type=RunType.STORYBOOK,
+ commit_sha="abc123",
+ branch="main",
+ snapshots=[],
+ ),
+ team_id=repo.team_id,
+ )
+
+ result = api.complete_run(create_result.run_id)
+
+ assert result.status == "failed"
+ assert result.error_message == "hash mismatch"
+ mock_diff_delay.assert_not_called()
+ mock_metrics_delay.assert_called_once_with(repo.team_id, str(create_result.run_id), "hash_integrity_failed", 0)
@patch("products.visual_review.backend.tasks.tasks.process_run_diffs.delay")
def test_complete_run_with_changes_triggers_task(self, mock_delay, repo):