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(stamphog): stop reporting retried github 5xx in fetch_review_context (#115461)

posthog[bot]e29bb71def3f
diff --git a/products/stamphog/backend/temporal/activities.py b/products/stamphog/backend/temporal/activities.py
index be046b40de97..22a044f18ddd 100644
--- a/products/stamphog/backend/temporal/activities.py
+++ b/products/stamphog/backend/temporal/activities.py
@@ -54,8 +54,10 @@ from posthog.dataclasses import frozen
from posthog.llm.gateway_client import AIGatewayConfig, resolve_ai_gateway_config
from posthog.models import User
from posthog.ph_client import ph_background_capture, ph_scoped_capture
+from posthog.temporal.common.errors import NonReportableError
from posthog.temporal.common.utils import asyncify
+from products.stamphog.backend.facade.contracts import StamphogGitHubError
from products.stamphog.backend.facade.enums import (
TERMINAL_STATUSES,
ReviewMode,
@@ -95,6 +97,7 @@ from products.stamphog.backend.logic.scrubbing import neutralize_active_markdown
from products.stamphog.backend.models import PullRequest, PullRequestAudience, ReviewRun, StamphogRepoConfig
from products.stamphog.backend.tasks.tasks import request_rereview_after_external_review
from products.stamphog.backend.temporal.constants import (
+ ACTIVITY_RETRY_POLICY,
CLONE_STEP_TIMEOUT_SECONDS,
NETWORK_RESTRICTED_AGENT_ENV,
PREFETCH_DIFF_BLOBS_TIMEOUT_SECONDS,
@@ -486,6 +489,17 @@ class _StepTimer:
self.timings_ms[name] = int((time.monotonic() - started) * 1000)
+class TransientGitHubError(NonReportableError):
+ """A GitHub 5xx or timeout on an attempt that Temporal still retries. The activity still fails,
+ so a review never runs with incomplete context, but only the final attempt reaches error tracking."""
+
+
+def _is_transient_github_error(error: Exception) -> bool:
+ if isinstance(error, requests.Timeout):
+ return True
+ return isinstance(error, StamphogGitHubError) and error.status_code is not None and error.status_code >= 500
+
+
@activity.defn
@asyncify
def fetch_review_context(input: StamphogReviewInput) -> dict:
@@ -494,6 +508,15 @@ def fetch_review_context(input: StamphogReviewInput) -> dict:
The reads are independent GitHub round trips, so they run concurrently. Only the reads that need
the PR's author or its file list wait for those two.
"""
+ try:
+ return _fetch_review_context(input)
+ except (StamphogGitHubError, requests.Timeout) as error:
+ if _is_transient_github_error(error) and activity.info().attempt < ACTIVITY_RETRY_POLICY.maximum_attempts:
+ raise TransientGitHubError(str(error)) from error
+ raise
+
+
+def _fetch_review_context(input: StamphogReviewInput) -> dict:
run = _load_run(input)
pull_request = run.pull_request
repo_config = pull_request.repo_config
diff --git a/products/stamphog/backend/tests/test_integration.py b/products/stamphog/backend/tests/test_integration.py
index c4e06ec890ea..d58609ab874f 100644
--- a/products/stamphog/backend/tests/test_integration.py
+++ b/products/stamphog/backend/tests/test_integration.py
@@ -19,8 +19,10 @@ from posthog.egress.github.transport import GitHubRateLimitError
from posthog.models import OAuthAccessToken, Team
from posthog.models.instance_setting import override_instance_config
from posthog.models.integration import Integration
+from posthog.temporal.common.posthog_client import is_expected_activity_failure
from products.signals.backend.models import SignalReport
+from products.stamphog.backend.facade.contracts import StamphogGitHubError
from products.stamphog.backend.facade.enums import (
AudienceReason,
ChannelResolutionSource,
@@ -1394,6 +1396,43 @@ def test_fetch_review_context_carries_inline_review_threads(team, stamphog_chain
]
+ "error,attempt,expect_reported",
+ [
+ pytest.param(StamphogGitHubError("bad gateway", status_code=502), 1, False, id="5xx_first_attempt"),
+ pytest.param(requests.Timeout("read timed out"), 2, False, id="timeout_second_attempt"),
+ pytest.param(StamphogGitHubError("bad gateway", status_code=502), 3, True, id="5xx_final_attempt"),
+ pytest.param(requests.Timeout("read timed out"), 3, True, id="timeout_final_attempt"),
+ pytest.param(StamphogGitHubError("not found", status_code=404), 1, True, id="4xx_first_attempt"),
+ pytest.param(StamphogGitHubError("truncated list"), 1, True, id="no_status_first_attempt"),
+ ],
+)
[email protected]_db(databases=PRODUCT_DATABASES)
+def test_fetch_review_context_reports_transient_github_errors_only_on_final_attempt(
+ team, stamphog_chain: StamphogChain, error: Exception, attempt: int, expect_reported: bool
+) -> None:
+ repo_config = _repo_config(team.id)
+ head_sha = "sha134a"
+ stamphog_chain.recorder.register_pr(REPO, 134, _pr_object(134, "devex-dev", head_sha), _pr_files())
+ pull_request = PullRequest.objects.for_team(team.id).create(
+ team_id=team.id, repo_config=repo_config, pr_number=134, author_login="devex-dev"
+ )
+ run = ReviewRun.objects.for_team(team.id).create(
+ team_id=team.id, pull_request=pull_request, head_sha=head_sha, status=ReviewRunStatus.QUEUED
+ )
+
+ with (
+ patch.object(activities.activity, "info", return_value=MagicMock(attempt=attempt)),
+ patch.object(StamphogGitHubClient, "get_pr_reactions", side_effect=error),
+ pytest.raises(Exception) as raised,
+ ):
+ _run_activity(fetch_review_context, StamphogReviewInput(review_run_id=str(run.id), team_id=team.id))
+
+ # The activity fails either way, so a review never runs with incomplete context.
+ assert is_expected_activity_failure(raised.value) is not expect_reported
+ assert raised.value is error or raised.value.__cause__ is error
+
+
@pytest.mark.parametrize(
"raw_error,expected_stored",
[