: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(tasks): resolve repositories outside GitHub installation scopes (#119516)
Alessandro Pogliaghiafce0f251077
diff --git a/docs/internal/task-run-api.md b/docs/internal/task-run-api.mdindex 46c7b6030e83..f2fda75eb67f 100644--- a/docs/internal/task-run-api.md+++ b/docs/internal/task-run-api.md@@ -55,6 +55,8 @@ Run responses omit internal cost accounting, including gateway request IDs.GitHub credentials that require user action fail the run without retrying. Fresh runs and resumed sandboxes preserve the actionable error, and Temporal does not report these expected failures to error tracking.+Sandbox GitHub tokens are scoped to the repositories requested by the run. When GitHub rejects a repository scope, the run refreshes installation access and resolves renamed repositories by their GitHub ID. Public repositories outside the installation can be cloned anonymously; private or missing repositories require the user to check repository access. Public-only access results are cached for five minutes before checking again. GitHub outages and connection failures remain retryable, including failures during token refresh.+## Model selectionTask runs accept `gpt-6.1-sol` with the `codex` runtime adapter and `low`, `medium`, `high`, `xhigh`, or `max` reasoning effort.diff --git a/posthog/models/github_integration_base.py b/posthog/models/github_integration_base.pyindex ee3cda0fe8d2..661f68a61fb3 100644--- a/posthog/models/github_integration_base.py+++ b/posthog/models/github_integration_base.py@@ -2617,7 +2617,9 @@ class GitHubIntegrationBase:integration_id=self.integration.id,status_code=response.status_code,)- raise GitHubIntegrationError("GitHubIntegration: list_repositories non-JSON response")+ raise GitHubIntegrationError(+ "GitHubIntegration: list_repositories non-JSON response", status_code=response.status_code+ )if response.status_code == 200 and isinstance(body, dict):page_repos = extract_repos(body)has_more = len(page_repos) == per_page@@ -2628,7 +2630,7 @@ class GitHubIntegrationBase:status_code=response.status_code,error=body if isinstance(body, dict) else None,)- raise GitHubIntegrationError("GitHubIntegration: failed to list repositories")+ raise GitHubIntegrationError("GitHubIntegration: failed to list repositories", status_code=response.status_code)def list_all_repositories(self, *, priority: Priority | None = None) -> list[dict]:"""Fetch all accessible repositories, paginating through GitHub's API. ``priority`` overrides the client's lane."""diff --git a/posthog/models/test/integration/test_github.py b/posthog/models/test/integration/test_github.pyindex 1202c7327520..c6827c61bba5 100644--- a/posthog/models/test/integration/test_github.py+++ b/posthog/models/test/integration/test_github.py@@ -1966,9 +1966,10 @@ class TestGitHubIntegrationModel(BaseTest):mock_get.side_effect = [transient_1, transient_2]- with pytest.raises(GitHubIntegrationError, match="list_repositories non-JSON response"):+ with pytest.raises(GitHubIntegrationError, match="list_repositories non-JSON response") as raised:GitHubIntegration(integration, source="test", priority=Priority.CRITICAL).list_repositories()+ assert raised.value.status_code == 502assert mock_get.call_count == 2@patch("posthog.egress.transport.transport.requests.request")@@ -1992,9 +1993,10 @@ class TestGitHubIntegrationModel(BaseTest):# Page-1 succeeds. Page-2 fetch is retried once after transient 502.mock_get.side_effect = [first_page, second_page, second_page]- with pytest.raises(GitHubIntegrationError, match="failed to list repositories"):+ with pytest.raises(GitHubIntegrationError, match="failed to list repositories") as raised:GitHubIntegration(integration, source="test", priority=Priority.CRITICAL).list_all_repositories()+ assert raised.value.status_code == 502assert mock_get.call_count == 3@patch("posthog.models.integration.github.GitHubIntegration.list_repositories")diff --git a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.pyindex 63b093960558..20f879fe16ea 100644--- a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py+++ b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py@@ -409,13 +409,16 @@ def _repository_snapshot_integration_id(ctx: TaskProcessingContext, *, has_repo:def _github_token_error(error: Exception, message: str, context: dict[str, Any]) -> ProcessTaskError:if isinstance(error, CredentialUnavailableError):return error- if isinstance(error, GitHubRateLimitError | GitHubEgressBudgetExhausted):- retry_after = getattr(error, "retry_after", None) or DEFAULT_GITHUB_RATE_LIMIT_BACKOFF_SECONDS- return GitHubRateLimitedError(f"{message}; retrying in {retry_after}s", context, retry_after=retry_after)- if isinstance(error, requests.ConnectionError | requests.Timeout) or (- isinstance(error, GitHubIntegrationError) and error.status_code is not None and error.status_code >= 500- ):- return ProcessTaskTransientError(message, context, cause=error)+ cause: BaseException | None = error+ while cause is not None:+ if isinstance(cause, GitHubRateLimitError | GitHubEgressBudgetExhausted):+ retry_after = getattr(cause, "retry_after", None) or DEFAULT_GITHUB_RATE_LIMIT_BACKOFF_SECONDS+ return GitHubRateLimitedError(f"{message}; retrying in {retry_after}s", context, retry_after=retry_after)+ if isinstance(cause, requests.ConnectionError | requests.Timeout) or (+ isinstance(cause, GitHubIntegrationError) and cause.status_code is not None and cause.status_code >= 500+ ):+ return ProcessTaskTransientError(message, context, cause=error)+ cause = cause.__cause__return GitHubAuthenticationError(message, context, cause=error)diff --git a/products/tasks/backend/temporal/process_task/tests/test_utils.py b/products/tasks/backend/temporal/process_task/tests/test_utils.pyindex c44ac95da15f..a19437d0458f 100644--- a/products/tasks/backend/temporal/process_task/tests/test_utils.py+++ b/products/tasks/backend/temporal/process_task/tests/test_utils.py@@ -1,10 +1,14 @@-from unittest.mock import MagicMock, patch+import time_machine+from unittest.mock import MagicMock, call, patchfrom django.test import SimpleTestCase, TestCase, override_settingsfrom parameterized import parameterizedfrom posthog.egress.limiter.policies import Priority+from posthog.models import Integration, Organization, Team+from posthog.models.github_integration_base import GitHubIntegrationError+from posthog.models.integration import GitHubIntegrationfrom posthog.models.user import Userfrom posthog.models.user_integration import UserGitHubIntegration, UserIntegration@@ -14,7 +18,9 @@ from products.tasks.backend.constants import (DEFAULT_SANDBOX_WORKING_DIR,SNAPSHOT_KIND_DIRECTORY,)+from products.tasks.backend.exceptions import CredentialUnavailableErrorfrom products.tasks.backend.models import Task+from products.tasks.backend.redis import get_tasks_cachefrom products.tasks.backend.temporal.process_task.utils import (POSTHOG_MCP_DESCRIPTION,GitHubCredentialSource,@@ -24,6 +30,7 @@ from products.tasks.backend.temporal.process_task.utils import (build_sandbox_environment_variables,get_git_identity_env_vars,get_github_credential_source,+ get_github_token,get_imported_mcp_server_configs,get_models_for_runtime_adapter,get_relayed_mcp_server_names,@@ -905,6 +912,112 @@ class TestGetGithubToken(TestCase):else:mint.assert_called_once_with(None, repositories=expected_mint_repositories)+ @parameterized.expand(+ [+ ("mixed_public_and_installed", ["api", "web"], 200, {"id": 9, "private": False}, ["web"]),+ ("public_only", ["api"], 200, {"id": 9, "private": False}, []),+ ("renamed_private", ["api"], 200, {"id": 2, "private": True}, ["new-api"]),+ ("renamed_public_installed", ["api"], 200, {"id": 2, "private": False}, ["new-api"]),+ ("deduplicates_renamed_repo", ["api", "new-api"], 200, {"id": 2, "private": True}, ["new-api"]),+ ("installed_only", ["web"], 200, {}, ["web"]),+ ("private_outside_installation", ["api"], 200, {"id": 9, "private": True}, None),+ ("private_transferred", ["api"], 200, {"id": 7, "private": True}, None),+ ("internal_not_public", ["api"], 200, {"id": 9, "private": False, "visibility": "internal"}, None),+ ("missing_visibility", ["api"], 200, {"id": 9}, None),+ ("not_found", ["api"], 404, {}, None),+ ("upstream_error", ["api"], 503, {}, None),+ ]+ )+ @override_settings(+ TASKS_REDIS_URL="",+ CACHES={"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}},+ )+ def test_recovers_scoped_mint_using_repository_identity_and_visibility(+ self,+ _name: str,+ names: list[str],+ probe_status: int,+ metadata: dict,+ expected_names: list[str] | None,+ ) -> None:+ get_tasks_cache().clear()+ organization = Organization.objects.create(name="repository-access")+ team = Team.objects.create(organization=organization, name="repository-access")+ integration = Integration.objects.create(+ team=team,+ kind="github",+ integration_id="INSTALL",+ config={"installation_id": "INSTALL", "account": {"name": "acme"}},+ sensitive_config={"access_token": "ghs_shared"},+ )+ installed = [+ {"id": 1, "name": "web", "full_name": "acme/web"},+ {"id": 2, "name": "new-api", "full_name": "acme/new-api"},+ {"id": 7, "name": "api", "full_name": "another-owner/api"},+ ]+ responses = [MagicMock(status_code=200, json=lambda: {"repositories": installed})]+ if "api" in names:+ responses.append(MagicMock(status_code=probe_status, json=lambda: metadata))+ with (+ time_machine.travel("2026-01-01", tick=False) as clock,+ patch.object(+ GitHubIntegration,+ "mint_scoped_installation_token",+ side_effect=[GitHubIntegrationError("repository scope rejected", status_code=422), "ghs_scoped"],+ ) as mint,+ patch.object(GitHubIntegration, "api_request", side_effect=responses) as request,+ ):+ repositories = [f"acme/{name}" for name in names]+ if expected_names is None:+ expected_error = GitHubIntegrationError if probe_status >= 500 else CredentialUnavailableError+ with self.assertRaises(expected_error) as raised:+ get_github_token(integration.id, repositories)+ if isinstance(raised.exception, GitHubIntegrationError):+ assert raised.exception.status_code == probe_status+ else:+ assert "acme/api" in str(raised.exception)+ assert "installation settings" in str(raised.exception)+ else:+ token = get_github_token(integration.id, repositories)+ assert token == ("ghs_scoped" if expected_names else None)+ assert get_github_token(integration.id, repositories) == token+ if not expected_names:+ clock.shift(299)+ assert get_github_token(integration.id, repositories) is None+ clock.shift(2)+ assert get_github_token(integration.id, repositories) == "ghs_scoped"++ expected_calls = [call(None, repositories=sorted(names))]+ if expected_names:+ expected_calls.append(call(None, repositories=expected_names))+ elif expected_names == []:+ expected_calls.append(call(None, repositories=sorted(names)))+ assert mint.call_args_list == expected_calls+ assert request.call_count == (2 if "api" in names else 1)+ integration.refresh_from_db()+ assert integration.repository_cache == installed++ @parameterized.expand([(401,), (403,), (500,)])+ def test_non_scope_mint_failures_do_not_probe_repositories(self, status_code: int) -> None:+ get_tasks_cache().clear()+ organization = Organization.objects.create(name="repository-access")+ team = Team.objects.create(organization=organization, name="repository-access")+ integration = Integration.objects.create(+ team=team,+ kind="github",+ integration_id="INSTALL",+ config={"installation_id": "INSTALL", "account": {"name": "acme"}},+ )+ error = GitHubIntegrationError("mint failed", status_code=status_code)+ with (+ patch.object(GitHubIntegration, "mint_scoped_installation_token", side_effect=error),+ patch.object(GitHubIntegration, "api_request") as request,+ self.assertRaises(GitHubIntegrationError) as raised,+ ):+ get_github_token(integration.id, ["acme/api"])+ assert raised.exception is error+ request.assert_not_called()+class TestSlackTaskRunActorUser(TestCase):def test_credential_user_grandfathers_legacy_runs_without_actor_state(self) -> None:diff --git a/products/tasks/backend/temporal/process_task/utils.py b/products/tasks/backend/temporal/process_task/utils.pyindex 9d3412b7d184..db99d113b672 100644--- a/products/tasks/backend/temporal/process_task/utils.py+++ b/products/tasks/backend/temporal/process_task/utils.py@@ -16,7 +16,7 @@ from pydantic import BaseModelfrom posthog.egress.limiter.policies import Priorityfrom posthog.enums import LabeledStrEnumfrom posthog.llm.gateway_client import GatewayNotConfiguredError, ensure_scout_trial_capture_ready-from posthog.models.github_integration_base import GitHubIntegrationBase+from posthog.models.github_integration_base import GitHubIntegrationBase, GitHubIntegrationErrorfrom posthog.models.integration import GitHubIntegration, Integrationfrom posthog.models.user import Userfrom posthog.models.user_integration import (@@ -905,6 +905,7 @@ def get_sandbox_ph_mcp_configs(# GitHub installation tokens expire after 1 hour and sandboxes refresh theirs every 20 minutes, so a# token cached for 30 minutes still outlives the next refresh.SCOPED_GITHUB_TOKEN_CACHE_TTL_SECONDS = 30 * 60+PUBLIC_GITHUB_REPOSITORY_CACHE_TTL_SECONDS = 5 * 60def _private_repositories(repositories: Sequence[str]) -> list[str]:@@ -938,6 +939,49 @@ def _sandbox_repositories(state: dict[str, Any] | None, repository: str | None)return [repository] if repository else []+def _resolve_installation_repository_names(+ github_integration: GitHubIntegrationBase, names: Sequence[str]+) -> list[str]:+ owner = github_integration.organization().lower()+ installed: dict[int, str] = {}+ for repository in github_integration.sync_repository_cache():+ repository_id = repository.get("id")+ full_name = repository.get("full_name")+ if isinstance(repository_id, int) and isinstance(full_name, str):+ repository_owner, _, name = full_name.lower().rpartition("/")+ if repository_owner == owner and name:+ installed[repository_id] = name++ installed_names = set(installed.values())+ resolved: set[str] = set()+ unavailable: list[str] = []+ for name in names:+ if name in installed_names:+ resolved.add(name)+ continue+ full_name = f"{owner}/{name}"+ response = github_integration.api_request("GET", f"/repos/{full_name}", endpoint="/repos/{owner}/{repo}")+ if response.status_code == 404:+ unavailable.append(full_name)+ continue+ if response.status_code != 200:+ raise GitHubIntegrationError("Failed to check GitHub repository access", status_code=response.status_code)+ repository = response.json()+ repository_id = repository.get("id")+ if isinstance(repository_id, int) and repository_id in installed:+ resolved.add(installed[repository_id])+ elif repository.get("private") is not False or repository.get("visibility") == "internal":+ unavailable.append(full_name)++ if unavailable:+ raise CredentialUnavailableError(+ f"Cannot access GitHub repositories: {', '.join(unavailable)}. "+ "Check that they exist and grant the GitHub App access in your installation settings, then retry.",+ {"repositories": unavailable},+ )+ return sorted(resolved)++def _mint_repository_scoped_token(github_integration: GitHubIntegrationBase, repositories: Sequence[str]) -> str | None:names = _installation_repository_names(github_integration, repositories)if not names:@@ -945,9 +989,18 @@ def _mint_repository_scoped_token(github_integration: GitHubIntegrationBase, repcache_key = f"task-sandbox-github-token:{github_integration.github_installation_id}:{','.join(names)}"cache = get_tasks_cache()cached = cache.get(cache_key)- if isinstance(cached, str) and cached:- return cached- token = github_integration.mint_scoped_installation_token(None, repositories=names)+ if isinstance(cached, str):+ return cached or None+ try:+ token = github_integration.mint_scoped_installation_token(None, repositories=names)+ except GitHubIntegrationError as error:+ if error.status_code != 422:+ raise+ resolved_names = _resolve_installation_repository_names(github_integration, names)+ if not resolved_names:+ cache.set(cache_key, "", timeout=PUBLIC_GITHUB_REPOSITORY_CACHE_TTL_SECONDS)+ return None+ token = github_integration.mint_scoped_installation_token(None, repositories=resolved_names)cache.set(cache_key, token, timeout=SCOPED_GITHUB_TOKEN_CACHE_TTL_SECONDS)return tokendiff --git a/products/tasks/backend/tests/test_sandbox_utils.py b/products/tasks/backend/tests/test_sandbox_utils.pyindex d624e04454e5..05d0ef1f3f38 100644--- a/products/tasks/backend/tests/test_sandbox_utils.py+++ b/products/tasks/backend/tests/test_sandbox_utils.py@@ -1,3 +1,5 @@+from datetime import timedelta+import pytestfrom unittest.mock import MagicMock, patch@@ -224,28 +226,57 @@ def test_repo_less_report_run_follows_the_integration_the_create_path_attached(@pytest.mark.parametrize("resume", [False, True])@pytest.mark.parametrize(- "error, expected_error",+ "error, cause, expected_error",[- (ReauthorizationRequired("relink GitHub"), CredentialUnavailableError),- (CredentialUnavailableError("missing repository", {"repositories": ["acme/repo"]}), CredentialUnavailableError),- (GitHubRateLimitError("limited", retry_after=30), GitHubRateLimitedError),- (requests.ConnectionError("reset"), ProcessTaskTransientError),- (GitHubIntegrationError("bad gateway", status_code=502), ProcessTaskTransientError),- (GitHubIntegrationError("not accessible", status_code=422), GitHubAuthenticationError),+ (ReauthorizationRequired("relink GitHub"), None, CredentialUnavailableError),+ (+ CredentialUnavailableError("missing repository", {"repositories": ["acme/repo"]}),+ None,+ CredentialUnavailableError,+ ),+ (GitHubRateLimitError("limited", retry_after=30), None, GitHubRateLimitedError),+ (requests.ConnectionError("reset"), None, ProcessTaskTransientError),+ (GitHubIntegrationError("network error"), requests.ConnectionError("reset"), ProcessTaskTransientError),+ (GitHubIntegrationError("bad gateway", status_code=502), None, ProcessTaskTransientError),+ (+ GitHubIntegrationError("token refresh failed"),+ GitHubIntegrationError("service unavailable", status_code=503),+ ProcessTaskTransientError,+ ),+ (+ GitHubIntegrationError("token refresh failed"),+ GitHubRateLimitError("limited", retry_after=30),+ GitHubRateLimitedError,+ ),+ (+ GitHubIntegrationError("token refresh failed"),+ GitHubIntegrationError("unauthorized", status_code=401),+ GitHubAuthenticationError,+ ),+ (GitHubIntegrationError("not accessible", status_code=422), None, GitHubAuthenticationError),],ids=["reauthorization","unavailable_credential","rate_limited","connection_error",+ "wrapped_connection_error","server_error",+ "wrapped_server_error",+ "wrapped_rate_limit",+ "wrapped_authentication_error","rejected_mint",],)@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")async def test_github_token_failure_maps_to_run_error(- mock_full: MagicMock, _mock_log: MagicMock, error: Exception, expected_error: type[Exception], resume: bool+ mock_full: MagicMock,+ _mock_log: MagicMock,+ error: Exception,+ cause: Exception | None,+ expected_error: type[Exception],+ resume: bool,) -> 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,@@ -257,6 +288,7 @@ async def test_github_token_failure_maps_to_run_error(inject_fresh_tokens_on_resume,)+ error.__cause__ = causemock_full.side_effect = errorctx = TaskProcessingContext(task_id="t",@@ -285,6 +317,8 @@ async def test_github_token_failure_maps_to_run_error(assert type(raised.value) is expected_errorif isinstance(error, CredentialUnavailableError):assert raised.value is error+ if isinstance(raised.value, GitHubRateLimitedError):+ assert raised.value.next_retry_delay == timedelta(seconds=30)@pytest.mark.parametrize(