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(tasks): handle ReauthorizationRequired for GitHub repo access (#73086)

posthog[bot]f760cd455cad
diff --git a/products/tasks/backend/temporal/process_task/activities/create_sandbox_from_snapshot.py b/products/tasks/backend/temporal/process_task/activities/create_sandbox_from_snapshot.py
index 30ee740299db..7a37cc291c89 100644
--- a/products/tasks/backend/temporal/process_task/activities/create_sandbox_from_snapshot.py
+++ b/products/tasks/backend/temporal/process_task/activities/create_sandbox_from_snapshot.py
@@ -2,9 +2,11 @@ from dataclasses import dataclass
from temporalio import activity
+from posthog.models.user_integration import ReauthorizationRequired
from posthog.temporal.common.utils import asyncify
from products.tasks.backend.exceptions import (
+ CredentialUnavailableError,
GitHubAuthenticationError,
OAuthTokenError,
SnapshotNotFoundError,
@@ -87,6 +89,16 @@ def create_sandbox_from_snapshot(input: CreateSandboxFromSnapshotInput) -> Creat
)
or ""
)
+ except ReauthorizationRequired as e:
+ raise CredentialUnavailableError(
+ "GitHub user integration for this run requires reauthorization",
+ {
+ "github_integration_id": ctx.github_integration_id,
+ "task_id": ctx.task_id,
+ "team_id": ctx.team_id,
+ },
+ cause=e,
+ )
except Exception as e:
raise GitHubAuthenticationError(
f"Failed to get GitHub token for integration {ctx.github_integration_id}",
diff --git a/products/tasks/backend/temporal/process_task/activities/get_sandbox_for_repository.py b/products/tasks/backend/temporal/process_task/activities/get_sandbox_for_repository.py
index 7f6a2f443e6c..3ca704025039 100644
--- a/products/tasks/backend/temporal/process_task/activities/get_sandbox_for_repository.py
+++ b/products/tasks/backend/temporal/process_task/activities/get_sandbox_for_repository.py
@@ -7,10 +7,16 @@ from django.utils import timezone
from temporalio import activity
+from posthog.models.user_integration import ReauthorizationRequired
from posthog.temporal.common.utils import asyncify
from products.tasks.backend.constants import SNAPSHOT_KIND_FILESYSTEM, filter_user_sandbox_env_vars
-from products.tasks.backend.exceptions import GitHubAuthenticationError, OAuthTokenError, TaskNotFoundError
+from products.tasks.backend.exceptions import (
+ CredentialUnavailableError,
+ GitHubAuthenticationError,
+ OAuthTokenError,
+ TaskNotFoundError,
+)
from products.tasks.backend.logic.services.connection_token import (
SANDBOX_JWT_STATE_KID_KEY,
get_primary_sandbox_jwt_kid,
@@ -179,6 +185,12 @@ def get_sandbox_for_repository(input: GetSandboxForRepositoryInput) -> GetSandbo
)
or ""
)
+ except ReauthorizationRequired as e:
+ raise CredentialUnavailableError(
+ "GitHub user integration for this run requires reauthorization",
+ {"github_integration_id": github_integration_id, "task_id": ctx.task_id},
+ cause=e,
+ )
except Exception as e:
raise GitHubAuthenticationError(
f"Failed to get GitHub token for integration {github_integration_id}",
diff --git a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py
index cddab3fc97fb..a121568ae5dc 100644
--- a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py
+++ b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py
@@ -8,10 +8,16 @@ from django.utils import timezone
from temporalio import activity
+from posthog.models.user_integration import ReauthorizationRequired
from posthog.temporal.common.utils import asyncify
from products.tasks.backend.constants import SNAPSHOT_KIND_FILESYSTEM, filter_user_sandbox_env_vars
-from products.tasks.backend.exceptions import GitHubAuthenticationError, OAuthTokenError, TaskNotFoundError
+from products.tasks.backend.exceptions import (
+ CredentialUnavailableError,
+ GitHubAuthenticationError,
+ OAuthTokenError,
+ TaskNotFoundError,
+)
from products.tasks.backend.logic.services.agentsh import INFRASTRUCTURE_DOMAINS, _get_debug_only_domains
from products.tasks.backend.logic.services.connection_token import (
SANDBOX_JWT_STATE_KID_KEY,
@@ -222,6 +228,15 @@ def _resolve_sandbox_github_token(
)
or ""
)
+ except ReauthorizationRequired as e:
+ # Expected user-actionable state — the acting user must re-link GitHub. Non-retryable and
+ # kept out of the raw error stream (CredentialUnavailableError does not capture) so it does
+ # not surface as error-tracking noise. Mirrors the refresh path in sandbox_credentials.py.
+ raise CredentialUnavailableError(
+ "GitHub user integration for this run requires reauthorization",
+ {"github_integration_id": ctx.github_integration_id, "task_id": ctx.task_id},
+ cause=e,
+ )
except Exception as e:
raise GitHubAuthenticationError(
f"Failed to get GitHub token for integration {ctx.github_integration_id}",
@@ -771,6 +786,12 @@ def inject_fresh_tokens_on_resume(input: InjectFreshTokensOnResumeInput) -> None
)
or ""
)
+ except ReauthorizationRequired as e:
+ raise CredentialUnavailableError(
+ "GitHub user integration for this run requires reauthorization",
+ {"github_integration_id": ctx.github_integration_id, "task_id": ctx.task_id},
+ cause=e,
+ )
except Exception as e:
raise GitHubAuthenticationError(
f"Failed to refresh GitHub token for integration {ctx.github_integration_id}",
diff --git a/products/tasks/backend/tests/test_sandbox_utils.py b/products/tasks/backend/tests/test_sandbox_utils.py
index af5aaf1141aa..48934f0544f1 100644
--- a/products/tasks/backend/tests/test_sandbox_utils.py
+++ b/products/tasks/backend/tests/test_sandbox_utils.py
@@ -5,8 +5,9 @@ from django.test import override_settings
from posthog.models.github_integration_base import GitHubIntegrationError
from posthog.models.integration import GitHubIntegration
-from posthog.models.user_integration import UserGitHubIntegration
+from posthog.models.user_integration import ReauthorizationRequired, UserGitHubIntegration
+from products.tasks.backend.exceptions import CredentialUnavailableError
from products.tasks.backend.temporal.process_task.activities.get_task_processing_context import TaskProcessingContext
from products.tasks.backend.temporal.process_task.utils import (
can_mint_readonly_github_token,
@@ -119,3 +120,33 @@ def test_readonly_request_takes_priority_over_full_credential_path(
assert token == "READONLY_TOKEN"
mock_full.assert_not_called()
+
+
+@patch("products.tasks.backend.temporal.process_task.activities.provision_sandbox.emit_agent_log")
+@patch("products.tasks.backend.temporal.process_task.activities.provision_sandbox.get_sandbox_github_token")
+def test_reauthorization_required_surfaces_as_credential_unavailable(
+ mock_full: MagicMock, _mock_log: MagicMock
+) -> None:
+ # ReauthorizationRequired is a user-actionable "re-link your GitHub account" signal, not a
+ # system fault. Provisioning must surface it as CredentialUnavailableError (fatal, non-retryable,
+ # not captured to error tracking) rather than wrapping it in GitHubAuthenticationError, which
+ # captures the raw exception and floods error tracking with noise.
+ from products.tasks.backend.temporal.process_task.activities.provision_sandbox import ( # noqa: PLC0415 — activities import the workflow stack; keep it off this module's import path
+ _resolve_sandbox_github_token,
+ )
+
+ mock_full.side_effect = ReauthorizationRequired("relink GitHub")
+ ctx = TaskProcessingContext(
+ task_id="t",
+ run_id="r",
+ team_id=1,
+ team_uuid="u",
+ organization_id="o",
+ github_integration_id=5,
+ repository="acme/repo",
+ distinct_id="d",
+ state={},
+ )
+
+ with pytest.raises(CredentialUnavailableError):
+ _resolve_sandbox_github_token(ctx, task=MagicMock(), actor_user=None, repository="acme/repo", has_repo=True)