: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
feat(signals): research reports without github access (#123614)
Georgiy Tarasov08412fd6f2b0
diff --git a/docs/internal/signals-pr-lifecycle.md b/docs/internal/signals-pr-lifecycle.mdindex e95841ce07a0..e492ada219ae 100644--- a/docs/internal/signals-pr-lifecycle.md+++ b/docs/internal/signals-pr-lifecycle.md@@ -209,6 +209,20 @@ For information from a private repository or another explicitly private source,Each candidate carries a private, public, or unknown label from the cached GitHub repository list, so the agent does not query GitHub for visibility.These broader context checks remain prompt guidance; the source-repository restriction does not validate selections made outside the shared selector.+### GitHub-optional research++The boolean `self-driving-github-optional` flag targets the canonical project group (project UUID, with its numeric `id` as a group property). It defaults to off, including in development.++Flagged research continues without code when no repository is selected or GitHub is unavailable. Its `repo_selection` artefact records `research_context.mode` (`code` or `data_only`) and `repository_status`. Recognized repository authentication and clone failures can retry once without a checkout or GitHub credentials. Other failures keep their normal handling. Findings remain code-focused; missing code access alone is not a product decision requiring human input.++Research without a repository preserves structured agent startup failures and clears their health issues after a successful startup, using the same provider and runtime scope as research with code.++A saved repository is reused before checking GitHub availability. A data-only fallback leaves that selection and its recorded context unchanged, so a transient failure cannot replace a reviewer's pinned target.++Disable the flag to restore legacy behavior on subsequent research passes. Running passes finish with their recorded decision. Previous blocked reports are not automatically rerun.++Without a repository, legacy ownership lookup is skipped. Existing human assignments remain intact.+## Reviewer notificationsWith `signals-research-reviewer-selection` enabled, the research agent chooses report recipients in relevance order.diff --git a/posthog/egress/github/README.md b/posthog/egress/github/README.mdindex 584fe1944ea6..f4bcb23362fb 100644--- a/posthog/egress/github/README.md+++ b/posthog/egress/github/README.md@@ -32,6 +32,7 @@ A shed sweep stops for the cycle and resumes on the next scheduled run.A caller that walks pages, such as the warehouse source, paces with `github_installation_pace_seconds` instead of getting denied.The warehouse source's page fetches run on `BATCH`, while its repository validation and webhook management run on `NORMAL`, because a person waits on them. Customer Analytics feature-request link and resume lookups also run on `NORMAL` because an editor waits for the current issue state. Signals refreshes the stored pull request review decision from GitHub webhooks on `BATCH`; the inbox can keep the last known value when the lane is shed and update it after a retry.Self-Driving onboarding probes one repository per installation on `NORMAL` during activation and on `BATCH` during background completion checks. Failed probes back off without exhausting the completion polling window.+The GitHub-optional Self-driving pilot checks implementation repository access (source `signals_github_optional`) on `NORMAL` for manual PR creation and on `BATCH` for automatic implementation. Unavailable access blocks implementation while research from project data remains available.The ownership file reader (`posthog/ownership/`, source `ownership_github_api`) runs on `NORMAL` where a person waits for a board or a snapshot list, and on `BATCH` for the scheduled digests and report generation. Its GraphQL reads charge `core`, as every GraphQL call does, and so does the one compare call per new head commit that lets it reuse cached files.The repository cache behind repository selection (`posthog/models/integration_repository_cache.py`) runs on `BATCH` for Signals reports and on `NORMAL` when a person waits in Slack. It reads each repository's `pushed_at` from the installation's repository listing, refreshed when older than ten minutes, and calls GitHub only for repositories pushed since their last sync. A shed call waits for the limiter's pace and tries again.The branch picker serves its cached branch list at once. When the list is older than ten minutes, a background task lists the branches again on `BATCH`, waiting for the limiter's pace before each page. Only a repository with no cached list is listed inside the request.diff --git a/posthog/temporal/tests/ai/test_module_integrity.py b/posthog/temporal/tests/ai/test_module_integrity.pyindex 4886c6524c7d..4382b8e56c7e 100644--- a/posthog/temporal/tests/ai/test_module_integrity.py+++ b/posthog/temporal/tests/ai/test_module_integrity.py@@ -179,6 +179,7 @@ class TestSignalsProductModuleIntegrity:"report_safety_judge_activity","safety_filter_activity","select_repository_activity",+ "github_optional_pilot_activity","soft_delete_report_signals_activity","verify_match_specificity_activity","wait_for_signal_in_clickhouse_activity",diff --git a/products/signals/backend/auto_start.py b/products/signals/backend/auto_start.pyindex dc2c974b970c..2bbe208b4426 100644--- a/products/signals/backend/auto_start.py+++ b/products/signals/backend/auto_start.py@@ -15,6 +15,7 @@ import posthoganalyticsfrom posthog.clickhouse.client.connection import ClickHouseUserfrom posthog.dataclasses import frozen+from posthog.egress.limiter.policies import Priority as EgressPriorityfrom posthog.event_usage import groupsfrom posthog.models import Team, Userfrom posthog.models.organization import OrganizationMembership@@ -39,6 +40,7 @@ from products.signals.backend.billing import ()from products.signals.backend.enums import ReportLinkKindfrom products.signals.backend.free_trial import capture_signal_report_free_trial_paused, self_driving_free_trial_enabled+from products.signals.backend.github_optional import ResearchAccessPolicyfrom products.signals.backend.models import (MAX_SCOUT_CONTENT_REVISIONS,SignalReport,@@ -1518,6 +1520,10 @@ async def maybe_autostart_implementation_task()return AutostartOutcome(status="blocked", reason="Autostart is disabled")team_default_priority = Priority(team_config.default_autostart_priority) if team_config else Priority.P4+ if await database_sync_to_async(ResearchAccessPolicy.enabled)(team_id) and not await database_sync_to_async(+ ResearchAccessPolicy.repository_available, thread_sensitive=False+ )(team_id, repository, priority=EgressPriority.BATCH):+ return AutostartOutcome(status="blocked", reason="Connect or repair GitHub access to the selected repository")# Between the team's master switch and the quota gate. A team that opted out of auto-start# entirely gets no artefact and no gate reason, and a duplicate or a plan parent never countsdiff --git a/products/signals/backend/facade/github_optional.py b/products/signals/backend/facade/github_optional.pynew file mode 100644index 000000000000..abe4176599fc--- /dev/null+++ b/products/signals/backend/facade/github_optional.py@@ -0,0 +1,9 @@+from products.signals.backend.github_optional import ResearchAccessPolicy+++def github_optional_pilot_enabled(team_id: int) -> bool:+ return ResearchAccessPolicy.enabled(team_id)+++def pilot_repository_integration_id(team_id: int, repository: str) -> int | None:+ return ResearchAccessPolicy.repository_integration_id(team_id, repository)diff --git a/products/signals/backend/github_optional.py b/products/signals/backend/github_optional.pynew file mode 100644index 000000000000..ba62bcd1b1d5--- /dev/null+++ b/products/signals/backend/github_optional.py@@ -0,0 +1,107 @@+from __future__ import annotations++from django.db.models import Q++import structlog+from requests import RequestException+from rest_framework.exceptions import APIException++from posthog.egress.github.transport import GitHubRateLimitError+from posthog.egress.limiter.policies import Priority+from posthog.egress.transport.transport import EgressBudgetExhausted+from posthog.models.github_integration_base import _is_safe_github_repo_path+from posthog.models.integration import GitHubIntegration, GitHubIntegrationError, Integration+from posthog.models.team.team import Team+from posthog.ph_client import feature_enabled_or_false++from products.signals.backend.models import SignalTeamConfig++logger = structlog.get_logger(__name__)++GITHUB_OPTIONAL_FLAG = "self-driving-github-optional"+GITHUB_OPTIONAL_ACTIVATION = "github_optional"+++class RepositoryAccessTemporarilyUnavailable(APIException):+ status_code = 503+ default_code = "github_access_temporarily_unavailable"+ default_detail = "GitHub access could not be checked. Try again shortly."++ def __init__(self, *, wait: int | None = None) -> None:+ super().__init__()+ self.wait = wait+++class ResearchAccessPolicy:+ @staticmethod+ def repository_integration_id(team_id: int, repository: str, *, priority: Priority = Priority.NORMAL) -> int | None:+ if not _is_safe_github_repo_path(repository):+ return None+ transient_error: Exception | None = None+ for integration in Integration.objects.filter(team_id=team_id, kind="github"):+ try:+ github = GitHubIntegration(integration, source="signals_github_optional", priority=priority)+ if github.installation_can_access_repository(repository, raise_on_transient=True):+ return integration.id+ except GitHubIntegrationError as error:+ if error.status_code is None or error.status_code >= 500:+ transient_error = error+ except (GitHubRateLimitError, EgressBudgetExhausted, RequestException) as error:+ transient_error = error+ if transient_error is not None:+ raise RepositoryAccessTemporarilyUnavailable(+ wait=transient_error.retry_after if isinstance(transient_error, GitHubRateLimitError) else None+ ) from transient_error+ return None++ @staticmethod+ def repository_available(team_id: int, repository: str, *, priority: Priority = Priority.NORMAL) -> bool:+ return ResearchAccessPolicy.repository_integration_id(team_id, repository, priority=priority) is not None++ @staticmethod+ def enabled_for_project(project: Team) -> bool:+ try:+ return (+ feature_enabled_or_false(+ GITHUB_OPTIONAL_FLAG,+ str(project.uuid),+ groups={"project": str(project.uuid)},+ group_properties={"project": {"id": project.id, "uuid": str(project.uuid)}},+ send_feature_flag_events=False,+ )+ is True+ )+ except Exception:+ logger.warning("github_optional_pilot_flag_failed", team_id=project.id, exc_info=True)+ return False++ @staticmethod+ def enabled(team_id: int) -> bool:+ team = Team.objects.select_related("parent_team").get(id=team_id)+ return ResearchAccessPolicy.enabled_for_project(team.parent_team or team)++ @staticmethod+ def enrolled(team_id: int) -> bool:+ return SignalTeamConfig.objects.filter(+ Q(team_id=team_id) | Q(team__parent_team_id=team_id),+ onboarding__draft__activation_mode=GITHUB_OPTIONAL_ACTIVATION,+ onboarding__completed_at__isnull=False,+ ).exclude(onboarding__completed_at=None).exists() and ResearchAccessPolicy.enabled(team_id)++ @staticmethod+ def enrolled_team_ids(*, exclude_team_ids: set[int] | None = None) -> set[int]:+ projects = {+ parent_id or team_id+ for team_id, parent_id in SignalTeamConfig.objects.filter(+ onboarding__draft__activation_mode=GITHUB_OPTIONAL_ACTIVATION,+ onboarding__completed_at__isnull=False,+ )+ .exclude(onboarding__completed_at=None)+ .values_list("team_id", "team__parent_team_id")+ }+ projects -= exclude_team_ids or set()+ return {+ project.id+ for project in Team.objects.filter(id__in=projects).only("id", "uuid")+ if ResearchAccessPolicy.enabled_for_project(project)+ }diff --git a/products/signals/backend/report_generation/AGENTS.md b/products/signals/backend/report_generation/AGENTS.mdindex 705144350e20..16909ac740b0 100644--- a/products/signals/backend/report_generation/AGENTS.md+++ b/products/signals/backend/report_generation/AGENTS.md@@ -7,7 +7,7 @@ It is exercised locally via management commands, and it is also used by the prod- `select_repo.py`Selects the most relevant GitHub repository for a set of signals.- - If no eligible repo passes the source-visibility check: returns `None` (report goes to `pending_input`).+ - If no eligible repo passes the source-visibility check: returns `None`. The `self-driving-github-optional` pilot continues research with project data; other projects go to `pending_input`.- If 1 eligible repo passes the source-visibility check: returns it directly (no sandbox needed).- If multiple eligible repos pass the source-visibility check: spawns a sandbox agent that uses `gh` CLI to explore candidates and pick the best match. Uses `PostHog/.github` as a small dummy repo for the sandbox clone, since the agent only needs `gh` CLI access (not the repo itself).- Output: `RepoSelectionResult(repository: str | None, reason: str)`.@@ -32,6 +32,7 @@ It is exercised locally via management commands, and it is also used by the prod- an optional task-attributed fix verification note for actionable reportsThe repository used for research is tracked separately via the `repo_selection` artefact.+ Pilot research records `research_context` there, omits code-specific instructions without a checkout, and skips ownership lookup and PR-stack planning. Read [GitHub-optional pilot](../../../../docs/internal/signals-pr-lifecycle.md#github-optional-pilot) when changing fallback or enrollment behavior.Startup failures cross the Tasks facade as `AgentStartupFailed`. The caller activity preservestheir typed details in `ResearchAgentStartupFailed`; transient sandbox failures retain the extendeddiff --git a/products/signals/backend/temporal/__init__.py b/products/signals/backend/temporal/__init__.pyindex 5591f606e5c8..cef5192e596a 100644--- a/products/signals/backend/temporal/__init__.py+++ b/products/signals/backend/temporal/__init__.py@@ -54,7 +54,10 @@ from products.signals.backend.temporal.agentic.scout_trial_evaluation import (load_scout_trial_evaluation_activity,poll_scout_trial_judge_activity,)-from products.signals.backend.temporal.agentic.select_repository import select_repository_activity+from products.signals.backend.temporal.agentic.select_repository import (+ github_optional_pilot_activity,+ select_repository_activity,+)from products.signals.backend.temporal.backfill_error_tracking import (BackfillErrorTrackingWorkflow,emit_backfill_signal_activity,@@ -234,6 +237,7 @@ ACTIVITIES = [report_safety_judge_activity,safety_filter_activity,select_repository_activity,+ github_optional_pilot_activity,soft_delete_report_signals_activity,verify_match_specificity_activity,wait_for_signal_in_clickhouse_activity,diff --git a/products/signals/backend/temporal/agentic/report.py b/products/signals/backend/temporal/agentic/report.pyindex 351411452d30..0c49756efb98 100644--- a/products/signals/backend/temporal/agentic/report.py+++ b/products/signals/backend/temporal/agentic/report.py@@ -1,5 +1,5 @@import json-from dataclasses import asdict, dataclass+from dataclasses import asdict, dataclass, replacefrom datetime import datetimefrom typing import Any, TypeVarfrom uuid import UUID@@ -66,7 +66,7 @@ from products.signals.backend.report_generation.reviewer_telemetry import (capture_suggested_reviewers_resolved,capture_suggested_reviewers_unresolved,)-from products.signals.backend.report_generation.select_repo import RepoSelectionResult+from products.signals.backend.report_generation.select_repo import RepoSelectionResult, persisted_repo_selectionfrom products.signals.backend.report_lifecycle import report_status_filterfrom products.signals.backend.report_metrics import REPORT_METRIC_GOAL_FIELDS, ReportMetric, metric_batch_errorfrom products.signals.backend.report_steering import ReportSteering, load_research_steering@@ -83,6 +83,7 @@ from products.signals.backend.supersession import research_implementation_contexfrom products.signals.backend.temporal.agentic import (SIGNALS_REPORT_RESEARCH_ENV_NAME,get_or_create_signals_sandbox_env,+ resolve_acting_user_id_for_team,resolve_user_id_for_team,)from products.signals.backend.temporal.types import SignalData@@ -95,9 +96,11 @@ from products.tasks.backend.facade import api as tasks_facadefrom products.tasks.backend.facade.agents import (AgentStartupFailed,CustomPromptSandboxContext,+ RepositoryContextUnavailable,SandboxRunFailed,turn_failure_error_type,)+from products.tasks.backend.facade.repo_selection_types import ReportResearchContextlogger = structlog.get_logger(__name__)@@ -114,6 +117,7 @@ class RunAgenticReportInput:# older workflow history that predates this field replays cleanly (guard off).repo_selection_as_of: datetime | None = Nonesignal_id: str | None = None+ github_optional: bool = False@frozen@@ -124,7 +128,7 @@ class RunAgenticReportOutput:priority: Priority | Noneexplanation: stralready_addressed: bool- repository: str+ repository: str | None# Resolved chart payload for `SignalReport.charts`: the JSON set to store, `[]` to clear, or# `None` to leave the column untouched. Applied by the transition activity that writes the# matching title/summary, so charts and their prose land in one transaction. Defaults to `None`@@ -836,7 +840,11 @@ async def _persist_agentic_report_artefacts(research_outcome = "research_candidates_rejected"elif reviewer_selection_enabled:research_outcome = "research_decision_missing"- if not reviewer_selection_enabled and (decision is None or research_outcome == "research_candidates_rejected"):+ if (+ repo_selection.repository is not None+ and not reviewer_selection_enabled+ and (decision is None or research_outcome == "research_candidates_rejected")+ ):try:reviewer_resolution = await database_sync_to_async(_build_reviewers_content, thread_sensitive=False)(team_id=team_id,@@ -909,10 +917,16 @@ async def _persist_agentic_report_artefacts(repository=repo_selection.repository,)+ # Losing code access must not erase the target a reviewer already selected.+ previous = (+ await database_sync_to_async(persisted_repo_selection, thread_sensitive=False)(report_id)+ if repo_selection.research_context and repo_selection.research_context.mode == "data_only"+ else None+ )artefacts = [*([]- if superseded_mid_run+ if superseded_mid_run or (previous is not None and previous.repository is not None)else [ArtefactDraft(content=repo_selection, attribution=repo_selection_attribution)]),*(ArtefactDraft(content=content, attribution=research_attribution) for content in result.new_artefacts),@@ -1126,14 +1140,12 @@ def _capture_research_steering_attached(*, team_id: int, report_id: str, steerinlogger.exception("Failed to capture signals_research_steering_attached", report_id=report_id)-@scoped_temporal()-@close_db_connections-async def run_agentic_report_activity(input: RunAgenticReportInput) -> RunAgenticReportOutput:+async def _run_agentic_report(input: RunAgenticReportInput) -> RunAgenticReportOutput:"""Run the sandbox-backed report research and persist its artefacts after full success."""try:- # The workflow only calls this activity when repo_selection.repository is not None.- assert input.repo_selection.repository is not None, "run_agentic_report_activity called without a repository"+ assert input.github_optional or input.repo_selection.repository is not None, (+ "run_agentic_report_activity called without a repository"+ )repository = input.repo_selection.repositoryassignment = await database_sync_to_async(ReviewerRules, thread_sensitive=False)(@@ -1150,7 +1162,12 @@ async def run_agentic_report_activity(input: RunAgenticReportInput) -> RunAgentiasync with Heartbeater():# 1. Get context for the sandbox- user_id = await database_sync_to_async(resolve_user_id_for_team, thread_sensitive=False)(input.team_id)+ user_id = await database_sync_to_async(+ resolve_acting_user_id_for_team if input.github_optional else resolve_user_id_for_team,+ thread_sensitive=False,+ )(input.team_id)+ if user_id is None:+ raise ValueError("No active project member can run report research")sandbox_env_id = await database_sync_to_async(get_or_create_signals_sandbox_env, thread_sensitive=False)(input.team_id, SIGNALS_REPORT_RESEARCH_ENV_NAME, tasks_facade.SandboxNetworkAccessLevel.TRUSTED)@@ -1161,6 +1178,7 @@ async def run_agentic_report_activity(input: RunAgenticReportInput) -> RunAgentiteam_id=input.team_id,user_id=user_id,repository=repository,+ github_read_access=input.github_optional and repository is not None,sandbox_environment_id=sandbox_env_id,# Reads, plus the scratchpad: the research agent queries data/insights and can# list the report's artefacts, but never writes artefacts itself — the pipeline@@ -1315,12 +1333,14 @@ async def run_agentic_report_activity(input: RunAgenticReportInput) -> RunAgenti"failure": asdict(error.failure),"task_id": error.task_id,"run_id": error.run_id,- "repository": input.repo_selection.repository,+ "repository": input.repo_selection.repository or "",},type=RESEARCH_SANDBOX_UNAVAILABLE_ERROR_TYPE if error.transient else RESEARCH_STARTUP_FAILURE,non_retryable=error.failure.retryability == "requires_action",) from errorexcept SandboxRunFailed as error:+ if input.github_optional and isinstance(error, RepositoryContextUnavailable):+ raiselogger.exception("signals agentic report failed",report_id=input.report_id,@@ -1345,10 +1365,27 @@ async def run_agentic_report_activity(input: RunAgenticReportInput) -> RunAgentiraise ApplicationError(str(error), type=error_type) from errorraisefinally:- if input.repo_selection.repository:- try:- await database_sync_to_async(reconcile_research_startups, thread_sensitive=False)(- input.team_id, input.repo_selection.repository- )- except Exception:- logger.warning("signals_startup_recovery_failed", team_id=input.team_id, exc_info=True)+ try:+ await database_sync_to_async(reconcile_research_startups, thread_sensitive=False)(+ input.team_id, input.repo_selection.repository or ""+ )+ except Exception:+ logger.warning("signals_startup_recovery_failed", team_id=input.team_id, exc_info=True)+++@scoped_temporal()+@close_db_connections+async def run_agentic_report_activity(input: RunAgenticReportInput) -> RunAgenticReportOutput:+ try:+ return await _run_agentic_report(input)+ except RepositoryContextUnavailable:+ if not input.github_optional or input.repo_selection.repository is None:+ raise+ fallback = RepoSelectionResult(+ repository=None,+ reason="GitHub code access failed. Research used project data instead.",+ autostart_eligible=False,+ research_context=ReportResearchContext(mode="data_only", repository_status="github_unavailable"),+ )+ return await _run_agentic_report(replace(input, repo_selection=fallback))diff --git a/products/signals/backend/temporal/agentic/select_repository.py b/products/signals/backend/temporal/agentic/select_repository.pyindex 2e5967bd31c8..764ca9c20a85 100644--- a/products/signals/backend/temporal/agentic/select_repository.py+++ b/products/signals/backend/temporal/agentic/select_repository.py@@ -1,18 +1,21 @@-from dataclasses import dataclass-import structlogimport temporalioimport posthoganalytics+from requests import RequestException+from posthog.dataclasses import frozen+from posthog.egress.github.transport import GitHubRateLimitError+from posthog.egress.transport.transport import EgressBudgetExhaustedfrom posthog.event_usage import groupsfrom posthog.models import Organization-from posthog.models.integration import GitHubIntegrationError+from posthog.models.integration import GitHubIntegrationError, Integrationfrom posthog.models.team.team import Teamfrom posthog.sync import database_sync_to_asyncfrom posthog.temporal.common.heartbeat import Heartbeaterfrom posthog.temporal.common.scoped import scoped_temporalfrom posthog.temporal.common.utils import aretry_on_db_connection_drop, close_db_connections+from products.signals.backend.github_optional import ResearchAccessPolicyfrom products.signals.backend.recurrence import recurrence_rejects_repositoryfrom products.signals.backend.report_generation.select_repo import (RepoSelectionResult,@@ -27,6 +30,8 @@ from products.signals.backend.temporal.agentic import ()from products.signals.backend.temporal.types import SignalDatafrom products.tasks.backend.facade import api as tasks_facade+from products.tasks.backend.facade.agents import RepositoryContextUnavailable+from products.tasks.backend.facade.repo_selection_types import ReportResearchContext# Repo discovery only runs `gh` CLI commands — limit egress to GitHub hosts.GITHUB_ONLY_DOMAINS = [@@ -41,12 +46,31 @@ GITHUB_ONLY_DOMAINS = [logger = structlog.get_logger(__name__)-@dataclass(frozen=False)+@frozenclass SelectRepositoryInput:team_id: intreport_id: strsignals: list[SignalData]signal_id: str | None = None+ github_optional: bool = False+++@scoped_temporal()+@close_db_connections+async def github_optional_pilot_activity(team_id: int) -> bool:+ return await database_sync_to_async(ResearchAccessPolicy.enabled, thread_sensitive=False)(team_id)+++def _pilot_code_availability(team_id: int) -> ReportResearchContext:+ integrations = list(Integration.objects.filter(team_id=team_id, kind="github"))+ if not integrations:+ return ReportResearchContext(mode="data_only", repository_status="no_integration")+ if all(item.repository_cache_updated_at and item.repository_cache == [] for item in integrations):+ return ReportResearchContext(mode="data_only", repository_status="no_repositories")+ if resolve_team_github_integration(team_id, team_only=True) is None:+ return ReportResearchContext(mode="data_only", repository_status="github_unavailable")+ return ReportResearchContext(mode="code", repository_status="available")def _resolve_sandbox_user_id(team_id: int) -> int | None:@@ -128,8 +152,30 @@ async def select_repository_activity(input: SelectRepositoryInput) -> RepoSelectinput.report_id,result="reused",)+ if input.github_optional:+ previous.research_context = ReportResearchContext(mode="code", repository_status="available")return previous+ if input.github_optional:+ availability = await database_sync_to_async(_pilot_code_availability, thread_sensitive=False)(+ input.team_id+ )+ if availability.mode == "data_only":+ _capture_repo_research_event(+ "signals_repo_research_completed",+ team,+ team.organization,+ input.report_id,+ result="data_only",+ failure_reason=availability.repository_status,+ )+ return RepoSelectionResult(+ repository=None,+ reason="Research will use project data without code access.",+ research_context=availability,+ autostart_eligible=False,+ )+user_id = await database_sync_to_async(_resolve_sandbox_user_id, thread_sensitive=False)(input.team_id)if user_id is None:logger.info(@@ -188,8 +234,52 @@ async def select_repository_activity(input: SelectRepositoryInput) -> RepoSelectinput.report_id,result="selected" if result.repository is not None else "no_repo",)+ if input.github_optional:+ availability = (+ await database_sync_to_async(_pilot_code_availability, thread_sensitive=False)(input.team_id)+ if result.repository is None+ else ReportResearchContext(mode="code", repository_status="available")+ )+ result.research_context = ReportResearchContext(+ mode="code" if result.repository else "data_only",+ repository_status="available"+ if result.repository+ else availability.repository_status+ if availability.mode == "data_only"+ else "selection_unresolved",+ )return resultexcept Exception as e:+ if input.github_optional and isinstance(+ e,+ (+ GitHubIntegrationError,+ GitHubRateLimitError,+ EgressBudgetExhausted,+ RequestException,+ RepositoryContextUnavailable,+ ),+ ):+ logger.warning(+ "signals_repository_unavailable_using_project_data",+ team_id=input.team_id,+ report_id=input.report_id,+ error_type=type(e).__name__,+ )+ _capture_repo_research_event(+ "signals_repo_research_completed",+ team,+ team.organization,+ input.report_id,+ result="data_only",+ failure_reason="github_unavailable",+ )+ return RepoSelectionResult(+ repository=None,+ reason="GitHub is unavailable. Research will use project data.",+ research_context=ReportResearchContext(mode="data_only", repository_status="github_unavailable"),+ autostart_eligible=False,+ )_capture_repo_research_event("signals_repo_research_completed",team,diff --git a/products/signals/backend/temporal/summary.py b/products/signals/backend/temporal/summary.pyindex db5022a5b26a..8da1248a74b0 100644--- a/products/signals/backend/temporal/summary.py+++ b/products/signals/backend/temporal/summary.py@@ -44,6 +44,7 @@ from products.signals.backend.auto_start import (start_requested_implementation,)from products.signals.backend.daily_limit import capture_signal_report_daily_limit_paused, daily_report_limit_gate+from products.signals.backend.github_optional import RepositoryAccessTemporarilyUnavailablefrom products.signals.backend.models import (SIGNALS_AT_RUN_INCREMENT,SignalReport,@@ -89,6 +90,7 @@ from products.signals.backend.temporal.agentic.report import ()from products.signals.backend.temporal.agentic.select_repository import (SelectRepositoryInput,+ github_optional_pilot_activity,select_repository_activity,)from products.signals.backend.temporal.inbox_notification import (@@ -119,7 +121,7 @@ EMPTY_FETCH_RETRY_ATTEMPTS = 6EMPTY_FETCH_RETRY_INTERVAL = timedelta(seconds=10)-def _capture_report_event(+async def _capture_report_event(event: str,team: Team,organization: Organization,@@ -163,6 +165,20 @@ def _capture_report_event(metrics.increment_report_completed(result, failure_reason=failure_reason, error_type=error_type)try:+ if event == "signal_report_completed":+ content = (+ await SignalReportArtefact.objects.filter(+ team_id=team.pk, report_id=report_id, type=SignalReportArtefact.ArtefactType.REPO_SELECTION+ )+ .order_by("-created_at")+ .values_list("content", flat=True)+ .afirst()+ )+ context = json.loads(content).get("research_context") if content else None+ if context:+ properties.update(+ github_optional=True, research_mode=context["mode"], repository_status=context["repository_status"]+ )posthoganalytics.capture(event=event,distinct_id=str(team.uuid),@@ -593,6 +609,14 @@ class SignalReportSummaryWorkflow:# Captured before the selection is resolved, so the research activity can tell whether# a reviewer rewrote the report's repo selection while this run was in flight.repo_selection_as_of = workflow.now()+ github_optional = False+ if workflow.patched("self-driving-github-optional"):+ github_optional = await workflow.execute_activity(+ github_optional_pilot_activity,+ inputs.team_id,+ start_to_close_timeout=timedelta(minutes=1),+ retry_policy=RetryPolicy(maximum_attempts=3),+ )repo_result: RepoSelectionResult = await workflow.execute_activity(select_repository_activity,SelectRepositoryInput(@@ -600,6 +624,7 @@ class SignalReportSummaryWorkflow:report_id=inputs.report_id,signal_id=inputs.signal_id,signals=fetch_result.signals,+ github_optional=github_optional,),# Budget = heavy-cache warmup (cold, ≤1000 repos, rate-limit backoffs) +# follower lock wait (≤20m) + agent poll window (≤30m). 45m fits all three.@@ -609,7 +634,7 @@ class SignalReportSummaryWorkflow:retry_policy=RetryPolicy(maximum_attempts=2),)await self._wait_for_task_settlement(inputs)- if repo_result.repository is None:+ if repo_result.repository is None and not github_optional:log.warning("Report has no repository selected",reason=repo_result.reason,@@ -642,6 +667,7 @@ class SignalReportSummaryWorkflow:signals=fetch_result.signals,repo_selection=repo_result,repo_selection_as_of=repo_selection_as_of,+ github_optional=github_optional,),)await self._wait_for_task_settlement(inputs)@@ -1031,7 +1057,7 @@ async def mark_report_in_progress_activity(input: MarkReportInProgressInput) ->assert input.signal_count is not Noneteam = await Team.objects.select_related("organization").aget(pk=input.team_id)- _capture_report_event(+ await _capture_report_event(event="signal_report_started",team=team,organization=team.organization,@@ -1260,7 +1286,7 @@ async def mark_report_ready_activity(input: MarkReportReadyInput) -> bool:return transition.has_new_signalsteam = await Team.objects.select_related("organization").aget(pk=input.team_id)- _capture_report_event(+ await _capture_report_event(event="signal_report_completed",team=team,organization=team.organization,@@ -1364,13 +1390,19 @@ async def maybe_autostart_implementation_activity(input: MaybeAutostartImplementch_user=ClickHouseUser.SIGNALS,)else:- await maybe_autostart_from_report_artefacts(- team_id=input.team_id, report_id=input.report_id, ch_user=ClickHouseUser.SIGNALS- )- # A plan never starts its own run, so its first layers start here, at the same settle point.- await database_sync_to_async(start_unblocked_layers_of_plan, thread_sensitive=False)(- team_id=input.team_id, parent_report_id=input.report_id, ch_user=ClickHouseUser.SIGNALS- )+ try:+ await maybe_autostart_from_report_artefacts(+ team_id=input.team_id, report_id=input.report_id, ch_user=ClickHouseUser.SIGNALS+ )+ # A plan never starts its own run, so its first layers start here, at the same settle point.+ await database_sync_to_async(start_unblocked_layers_of_plan, thread_sensitive=False)(+ team_id=input.team_id, parent_report_id=input.report_id, ch_user=ClickHouseUser.SIGNALS+ )+ except RepositoryAccessTemporarilyUnavailable as error:+ raise ApplicationError(+ str(error),+ next_retry_delay=timedelta(seconds=error.wait) if error.wait else None,+ ) from error@dataclass(frozen=True)@@ -1447,7 +1479,7 @@ async def mark_report_failed_activity(input: MarkReportFailedInput) -> None:returnteam = await Team.objects.select_related("organization").aget(pk=input.team_id)- _capture_report_event(+ await _capture_report_event(event="signal_report_completed",team=team,organization=team.organization,@@ -1567,7 +1599,7 @@ async def mark_report_pending_input_activity(input: MarkReportPendingInput) -> Nreturnteam = await Team.objects.select_related("organization").aget(pk=input.team_id)- _capture_report_event(+ await _capture_report_event(event="signal_report_completed",team=team,organization=team.organization,@@ -1628,7 +1660,7 @@ async def reset_report_to_potential_activity(input: ResetReportToPotentialInput)returnteam = await Team.objects.select_related("organization").aget(pk=input.team_id)- _capture_report_event(+ await _capture_report_event(event="signal_report_completed",team=team,organization=team.organization,diff --git a/products/signals/backend/test/test_agentic_report_activity.py b/products/signals/backend/test/test_agentic_report_activity.pyindex 06eea191e72a..52ca05050c6d 100644--- a/products/signals/backend/test/test_agentic_report_activity.py+++ b/products/signals/backend/test/test_agentic_report_activity.py@@ -17,7 +17,8 @@ from parameterized import parameterizedfrom pydantic import ValidationErrorfrom temporalio.exceptions import ApplicationError-from posthog.models import Organization, Team, User+from posthog.models import Integration, Organization, Team, User+from posthog.models.integration import GitHubIntegrationErrorfrom posthog.models.organization import OrganizationMembershipfrom posthog.models.scoping import team_scopefrom posthog.models.user_integration import UserIntegration@@ -87,8 +88,14 @@ from products.signals.backend.temporal.summary import (mark_report_ready_activity,)from products.signals.backend.temporal.types import SignalData-from products.tasks.backend.facade.agents import AgentStartupFailed, CustomPromptSandboxContext, SandboxRunFailed+from products.tasks.backend.facade.agents import (+ AgentStartupFailed,+ CustomPromptSandboxContext,+ RepositoryContextUnavailable,+ SandboxRunFailed,+)from products.tasks.backend.facade.contracts import AgentStartupFailure+from products.tasks.backend.facade.repo_selection_types import ReportResearchContextfrom products.tasks.backend.models import Task # tach-ignore@@ -586,6 +593,49 @@ async def test_load_previous_research_ignores_artefacts_from_another_team(ateam,assert await _load_previous_research(ateam.id, str(report.id)) is None+ "connection,expected",+ [+ ("missing", "no_integration"),+ ("empty", "no_repositories"),+ ("broken", "github_unavailable"),+ ("transport_failure", "github_unavailable"),+ ],+)+async def test_pilot_repository_failure_keeps_research_available(monkeypatch, ateam, connection, expected):+ if connection != "missing":+ await Integration.objects.acreate(+ team=ateam,+ kind="github",+ integration_id="123",+ config={"installation_id": 123},+ errors="TOKEN_REFRESH_FAILED" if connection == "broken" else "",+ repository_cache=[] if connection == "empty" else [{"full_name": "example/app"}],+ repository_cache_updated_at=timezone.now(),+ )+ monkeypatch.setattr(+ "products.signals.backend.temporal.agentic.select_repository._resolve_sandbox_user_id", lambda team_id: 1+ )+ with (+ patch("products.signals.backend.temporal.agentic.select_repository.Heartbeater"),+ patch(+ "products.signals.backend.temporal.agentic.select_repository.select_repository_for_report",+ side_effect=GitHubIntegrationError("Unavailable", status_code=503),+ ),+ ):+ result = await select_repository_activity(+ SelectRepositoryInput(+ team_id=ateam.id, report_id=str(uuid4()), signals=_build_signals(), github_optional=True+ )+ )+ assert result.repository is None+ assert result.research_context is not None+ assert result.research_context.mode == "data_only"+ assert result.research_context.repository_status == expected++@pytest.mark.asyncio@pytest.mark.django_dbasync def test_select_repository_activity_returns_repo(monkeypatch, ateam):@@ -617,7 +667,8 @@ async def test_select_repository_activity_returns_repo(monkeypatch, ateam):@pytest.mark.asyncio@pytest.mark.django_db-async def test_select_repository_activity_reuses_previous_selection(monkeypatch, ateam):[email protected]("github_optional", [False, True])+async def test_select_repository_activity_reuses_previous_selection(monkeypatch, ateam, github_optional):previous = RepoSelectionResult(repository="posthog/posthog", reason="Previously selected")monkeypatch.setattr(@@ -639,7 +690,9 @@ async def test_select_repository_activity_reuses_previous_selection(monkeypatch,with patch("products.signals.backend.temporal.agentic.select_repository.Heartbeater"):result = await select_repository_activity(- SelectRepositoryInput(team_id=ateam.id, report_id=str(uuid4()), signals=_build_signals())+ SelectRepositoryInput(+ team_id=ateam.id, report_id=str(uuid4()), signals=_build_signals(), github_optional=github_optional+ ))assert result is previous@@ -749,6 +802,7 @@ async def test_select_repository_activity_does_not_raise_with_only_user_integrat@pytest.mark.asyncio@pytest.mark.django_db[email protected]("repository", ["posthog/posthog", None])@pytest.mark.parametrize("sandbox_error_type,expected_transient",[@@ -761,7 +815,7 @@ async def test_select_repository_activity_does_not_raise_with_only_user_integrat],)async def test_run_agentic_report_activity_flags_transient_sandbox_failures(- monkeypatch, ateam, sandbox_error_type, expected_transient+ monkeypatch, ateam, repository, sandbox_error_type, expected_transient):report = await database_sync_to_async(SignalReport.objects.create)(team=ateam,@@ -770,6 +824,9 @@ async def test_run_agentic_report_activity_flags_transient_sandbox_failures(total_weight=1.3,)monkeypatch.setattr("products.signals.backend.temporal.agentic.report.resolve_user_id_for_team", lambda team_id: 1)+ monkeypatch.setattr(+ "products.signals.backend.temporal.agentic.report.resolve_acting_user_id_for_team", lambda team_id: 1+ )async def fake_run_multi_turn_research(*args, **kwargs):if sandbox_error_type.startswith("startup_"):@@ -804,7 +861,8 @@ async def test_run_agentic_report_activity_flags_transient_sandbox_failures(team_id=ateam.id,report_id=str(report.id),signals=_build_signals(),- repo_selection=RepoSelectionResult(repository="posthog/posthog", reason="single repository"),+ repo_selection=RepoSelectionResult(repository=repository, reason="test research context"),+ github_optional=repository is None,))@@ -822,6 +880,73 @@ async def test_run_agentic_report_activity_flags_transient_sandbox_failures(assert isinstance(error.value, SandboxRunFailed)+ "repository,failure",+ [+ (None, None),+ ("example/app", RepositoryContextUnavailable("RepositoryAccessError")),+ ("example/app", RepositoryContextUnavailable("RepositoryCloneError")),+ ],+)+async def test_pilot_research_persists_a_report_without_code(monkeypatch, ateam, repository, failure):+ report = await database_sync_to_async(SignalReport.objects.create)(+ team=ateam, status=SignalReport.Status.IN_PROGRESS+ )+ if repository:+ await SignalReportArtefact.objects.acreate(+ team=ateam,+ report=report,+ type="repo_selection",+ content=RepoSelectionResult(repository=repository, reason="Reviewer pinned").model_dump_json(),+ )+ monkeypatch.setattr(+ "products.signals.backend.temporal.agentic.report.resolve_acting_user_id_for_team", lambda team_id: 1+ )+ contexts = []++ async def research(signals, context, **kwargs):+ contexts.append(context)+ if failure is not None and len(contexts) == 1:+ raise failure+ return _build_research_output()++ with (+ patch("products.signals.backend.temporal.agentic.report.Heartbeater"),+ patch("products.signals.backend.temporal.agentic.report.run_multi_turn_research", side_effect=research),+ ):+ result = await run_agentic_report_activity(+ RunAgenticReportInput(+ team_id=ateam.id,+ report_id=str(report.id),+ signals=_build_signals(),+ github_optional=True,+ repo_selection=RepoSelectionResult(+ repository=repository,+ reason="Pilot",+ research_context=ReportResearchContext(+ mode="code" if repository else "data_only",+ repository_status="available" if repository else "no_integration",+ ),+ ),+ )+ )+ assert result.repository is None+ assert result.title == "Onboarding funnel completion tracking may be regressing"+ assert contexts[-1].repository is None+ assert contexts[-1].github_read_access is False+ assert contexts[0].github_read_access is (repository is not None)+ assert len(contexts) == (2 if failure else 1)+ selections = SignalReportArtefact.objects.filter(report=report, type="repo_selection")+ assert await selections.acount() == 1+ selection = await selections.afirst()+ assert selection is not None+ assert json.loads(selection.content)["repository"] == repository+ if not repository:+ assert json.loads(selection.content)["research_context"]["mode"] == "data_only"++@pytest.mark.asyncio@pytest.mark.django_dbasync def test_run_agentic_report_activity_persists_artefacts(monkeypatch, ateam):@@ -923,6 +1048,7 @@ async def test_run_agentic_report_activity_persists_artefacts(monkeypatch, ateamassert repo_selection_content == {"repository": "posthog/posthog","reason": "Single repository connected: posthog/posthog",+ "research_context": None,"task_id": None,"autostart_eligible": True,}diff --git a/products/signals/backend/test/test_auto_start.py b/products/signals/backend/test/test_auto_start.pyindex 40faffce8902..bb8353c88679 100644--- a/products/signals/backend/test/test_auto_start.py+++ b/products/signals/backend/test/test_auto_start.py@@ -863,9 +863,19 @@ def test_create_implementation_task_threads_resolved_runtime(organization, team)@pytest.mark.asyncio@pytest.mark.django_db(transaction=True)@pytest.mark.parametrize(- ("autostart_enabled", "concurrent_edit"), [(True, False), (False, False), (None, False), (True, True)]+ ("autostart_enabled", "concurrent_edit", "pilot", "repository_available"),+ [+ (True, False, False, True),+ (False, False, False, True),+ (None, False, False, True),+ (True, True, False, True),+ (True, False, True, False),+ (True, False, True, True),+ ],)-async def test_team_autostart_switch_gates_reviewerless_fallback(autostart_enabled, concurrent_edit):+async def test_team_autostart_switch_gates_reviewerless_fallback(+ autostart_enabled, concurrent_edit, pilot, repository_available+):# The master switch must gate the reviewer-less fallback — the path email-login teams (no linked# GitHub) hit. An actionable, prioritized report with no resolvable reviewer auto-starts under the# team's signals enabler unless the switch is an explicit False (null leaves autostart on); committed@@ -923,6 +933,11 @@ async def test_team_autostart_switch_gates_reviewerless_fallback(autostart_enablwith (patch.object(tasks_facade, "create_and_run_task", side_effect=_fake_create_and_run_task) as mock_create,patch("products.signals.backend.auto_start.resolve_agent_runtime", side_effect=resolve_runtime),+ patch("products.signals.backend.github_optional.ResearchAccessPolicy.enabled", return_value=pilot),+ patch(+ "products.signals.backend.github_optional.ResearchAccessPolicy.repository_available",+ return_value=repository_available,+ ),):await maybe_autostart_implementation_task(team_id=team.id,@@ -939,7 +954,7 @@ async def test_team_autostart_switch_gates_reviewerless_fallback(autostart_enablpriority=PriorityAssessment(explanation="Affects many sessions.", priority=Priority.P2),)- assert (mock_create.call_count == 1) is (autostart_enabled is not False)+ assert (mock_create.call_count == 1) is (autostart_enabled is not False and (not pilot or repository_available))if concurrent_edit:assert "A newer fix" in mock_create.call_args.kwargs["description"]diff --git a/products/signals/backend/test/test_github_optional.py b/products/signals/backend/test/test_github_optional.pynew file mode 100644index 000000000000..02af1f8811d6--- /dev/null+++ b/products/signals/backend/test/test_github_optional.py@@ -0,0 +1,29 @@+from posthog.test.base import BaseTest+from unittest.mock import patch++from parameterized import parameterized++from posthog.models import Team+from posthog.models.integration import GitHubIntegration, Integration++from products.signals.backend.github_optional import GITHUB_OPTIONAL_FLAG, ResearchAccessPolicy+++class TestResearchAccessPolicy(BaseTest):+ def test_flag_uses_canonical_project_and_fails_closed(self) -> None:+ child = Team.objects.create(organization=self.organization, parent_team=self.team)+ with patch("products.signals.backend.github_optional.feature_enabled_or_false", return_value=True) as flag:+ assert ResearchAccessPolicy.enabled(child.id)+ assert flag.call_args.args == (GITHUB_OPTIONAL_FLAG, str(self.team.uuid))+ assert flag.call_args.kwargs["groups"] == {"project": str(self.team.uuid)}+ with patch("products.signals.backend.github_optional.feature_enabled_or_false", side_effect=RuntimeError):+ assert not ResearchAccessPolicy.enabled(self.team.id)++ @parameterized.expand(+ ["example/app/contents/file", "example/app?ref=main", "example/app#section", "example/app%2Fpath"]+ )+ def test_repository_access_rejects_non_repository_paths(self, repository: str) -> None:+ Integration.objects.create(team=self.team, kind="github", integration_id="first")+ with patch.object(GitHubIntegration, "installation_can_access_repository") as probe:+ assert ResearchAccessPolicy.repository_integration_id(self.team.id, repository) is None+ probe.assert_not_called()diff --git a/products/signals/backend/test/test_ownership_reviewers.py b/products/signals/backend/test/test_ownership_reviewers.pyindex f5fe3023af24..3b7830e68c2e 100644--- a/products/signals/backend/test/test_ownership_reviewers.py+++ b/products/signals/backend/test/test_ownership_reviewers.py@@ -293,7 +293,8 @@ def test_pipeline_skips_unchanged_reviewer_selections(team, change):@pytest.mark.django_db@pytest.mark.parametrize("selection", ["member", "empty", "inactive", "outsider", "private_project", "missing"])@pytest.mark.parametrize("selection_enabled", [False, True])-def test_research_decision_controls_persisted_reviewers(team, selection, selection_enabled):[email protected]("repository", [None, "example/app"])+def test_research_decision_controls_persisted_reviewers(team, selection, selection_enabled, repository):member = User.objects.create(email="[email protected]", is_active=selection != "inactive")if selection != "outsider":OrganizationMembership.objects.create(organization=team.organization, user=member)@@ -349,14 +350,14 @@ def test_research_decision_controls_persisted_reviewers(team, selection, selectiteam.id,str(report.id),result,- RepoSelectionResult(repository=None, reason="No repository"),+ RepoSelectionResult(repository=repository, reason="Research access"),reviewer_selection_enabled=selection_enabled,)latest = SignalReportArtefact.objects.filter(report=report, type=SignalReportArtefact.ArtefactType.SUGGESTED_REVIEWERS).latest("created_at")reviewers = SuggestedReviewers.model_validate_json(latest.content).root- if selection_enabled and selection not in {"member", "empty"}:+ if (selection_enabled or repository is None) and selection not in {"member", "empty"}:assert [entry.github_login for entry in reviewers] == ["previous"]capture.assert_not_called()returndiff --git a/products/signals/backend/test/test_summary_settle_autostart.py b/products/signals/backend/test/test_summary_settle_autostart.pyindex 00860def990c..9cfd21379d06 100644--- a/products/signals/backend/test/test_summary_settle_autostart.py+++ b/products/signals/backend/test/test_summary_settle_autostart.py@@ -4,7 +4,7 @@ import asynciofrom datetime import UTC, datetime, timedeltaimport pytest-from unittest.mock import patch+from unittest.mock import AsyncMock, patchfrom django.conf import settingsfrom django.utils import timezone@@ -13,11 +13,13 @@ import pytest_asynciofrom asgiref.sync import sync_to_asyncfrom temporalio import activity, workflowfrom temporalio.contrib.pydantic import pydantic_data_converter-from temporalio.testing import WorkflowEnvironment+from temporalio.exceptions import ApplicationError+from temporalio.testing import ActivityEnvironment, WorkflowEnvironmentfrom temporalio.worker import UnsandboxedWorkflowRunner, Workerfrom posthog.models import Organization, Team+from products.signals.backend.github_optional import RepositoryAccessTemporarilyUnavailablefrom products.signals.backend.models import SignalTeamConfigfrom products.signals.backend.report_generation.research import ActionabilityChoice, Priorityfrom products.signals.backend.report_generation.select_repo import RepoSelectionResult@@ -38,6 +40,7 @@ from products.signals.backend.temporal.summary import (ReportIsCandidateInput,SignalReportSummaryWorkflow,implementation_buffer_seconds_activity,+ maybe_autostart_implementation_activity,)from products.signals.backend.temporal.types import SignalData, SignalReportSummaryWorkflowInputs@@ -48,6 +51,22 @@ SUMMARY_MODULE_PATH = "products.signals.backend.temporal.summary"TASK_QUEUE = settings.VIDEO_EXPORT_TASK_QUEUE[email protected]("retry_after", [None, 300])+async def test_autostart_preserves_repository_retry_delay(retry_after: int | None) -> None:+ with (+ patch(+ f"{SUMMARY_MODULE_PATH}.maybe_autostart_from_report_artefacts",+ new=AsyncMock(side_effect=RepositoryAccessTemporarilyUnavailable(wait=retry_after)),+ ),+ pytest.raises(ApplicationError) as raised,+ ):+ await ActivityEnvironment().run(+ maybe_autostart_implementation_activity, MaybeAutostartImplementationInput(team_id=1, report_id="report")+ )+ assert raised.value.next_retry_delay == (timedelta(seconds=retry_after) if retry_after else None)++@pytest_asyncio.fixtureasync def aorganization():organization = await sync_to_async(Organization.objects.create)(@@ -189,6 +208,10 @@ async def _run_summary_workflow(recorder.work_owners.append(input.signal_id)return RepoSelectionResult(repository="owner/repo", reason="selected")+ @activity.defn(name="github_optional_pilot_activity")+ async def fake_research_access(team_id: int) -> bool:+ return False+@activity.defn(name="run_agentic_report_activity")async def fake_research(input: RunAgenticReportInput) -> RunAgenticReportOutput:recorder.work_owners.append(input.signal_id)@@ -248,6 +271,7 @@ async def _run_summary_workflow(fake_spend_owner,fake_safety,fake_select_repo,+ fake_research_access,fake_research,fake_mark_ready,fake_publish,diff --git a/products/signals/backend/test/test_summary_workflow.py b/products/signals/backend/test/test_summary_workflow.pyindex 7cc20e926a02..42c9e56606bc 100644--- a/products/signals/backend/test/test_summary_workflow.py+++ b/products/signals/backend/test/test_summary_workflow.py@@ -196,6 +196,7 @@ class _Recorder:spend_failures: int = 0,spend_owner_available: bool = True,research_errors: list[Exception] | None = None,+ github_optional: bool = False,) -> None:self.gate_answers = gate_answers or {}self.fetch_results = fetch_results or [[_signal_data()]]@@ -210,6 +211,7 @@ class _Recorder:self.failure_error_types: list[str | None] = []self.failure_startups: list[ResearchStartupFailure | None] = []self.startup_notice_attempts = 0+ self.github_optional = github_optionalself.gate_checks: list[str] = []self.fetches = 0self.claimed_before_fetch: list[bool] = []@@ -262,6 +264,10 @@ def test_legacy_agentic_activity_payload_defaults_metrics_to_preserve() -> None:async def _run_summary_workflow(recorder: _Recorder) -> None:+ @activity.defn(name="github_optional_pilot_activity")+ async def fake_pilot(team_id: int) -> bool:+ return recorder.github_optional+@activity.defn(name="check_report_quota_gate_activity")async def fake_quota(input: CheckReportQuotaGateInput) -> bool:recorder.gate_checks.append(input.stage)@@ -311,7 +317,7 @@ async def _run_summary_workflow(recorder: _Recorder) -> None:priority=None,explanation="e",already_addressed=False,- repository="owner/repo",+ repository=recorder.selected_repository,metrics=recorder.research_metrics,checks_snapshot=recorder.checks_snapshot,checks_monitoring_enabled=recorder.checks_monitoring_enabled,@@ -374,6 +380,7 @@ async def _run_summary_workflow(recorder: _Recorder) -> None:workflows=[SignalReportSummaryWorkflow],activities=[fake_quota,+ fake_pilot,fake_fetch,fake_has_assigned_signals,fake_mark_in_progress,@@ -537,11 +544,22 @@ async def test_metric_payload_reaches_the_report_transition(choice, target):@pytest.mark.asyncio-async def test_no_repository_keeps_report_content_and_logs_the_blocker():- recorder = _Recorder(selected_repository=None)[email protected]("github_optional", [False, True])+async def test_no_repository_researches_only_for_the_pilot(github_optional):+ recorder = _Recorder(+ selected_repository=None,+ github_optional=github_optional,+ research_choice=ActionabilityChoice.IMMEDIATELY_ACTIONABLE,+ )await _run_summary_workflow(recorder)+ if github_optional:+ assert recorder.researches == 1+ assert recorder.pending_inputs == []+ assert recorder.ready_inputs[0].title == "t"+ assert recorder.failures == 0+ returnassert recorder.researches == 0assert len(recorder.pending_inputs) == 1pending = recorder.pending_inputs[0]diff --git a/products/tasks/backend/facade/agents.py b/products/tasks/backend/facade/agents.pyindex de74c94c78af..4d7c85181d97 100644--- a/products/tasks/backend/facade/agents.py+++ b/products/tasks/backend/facade/agents.py@@ -14,6 +14,7 @@ from products.tasks.backend.logic.services.custom_prompt_internals import (CustomPromptSandboxContext,EmptyAgentTurnError,OutputFn,+ RepositoryContextUnavailable,SandboxRunFailed,TruncatedAgentOutputError,TurnPollResult,@@ -55,6 +56,7 @@ __all__ = ["ENV_LOCAL_SKILLS_HOST_PATH","AgentTurnFailed","AgentStartupFailed",+ "RepositoryContextUnavailable","CustomPromptSandboxContext","EmptyAgentTurnError","LocalSkillsCache",diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.pyindex 783e8e4c73de..19165546770d 100644--- a/products/tasks/backend/facade/api.py+++ b/products/tasks/backend/facade/api.py@@ -8170,7 +8170,13 @@ def create_task(and validated_data.get("origin_product") == Task.OriginProduct.SIGNAL_REPORT)if (validated_data.get("repository") or entitled_report_task) and not validated_data.get("github_integration"):- default_integration = Integration.objects.filter(team=team, kind="github").first()+ if signal_report is not None:+ integration_id = _report_task_github_integration_id(team, code_access_allowed=True)+ default_integration = (+ Integration.objects.filter(id=integration_id).first() if integration_id is not None else None+ )+ else:+ default_integration = Integration.objects.filter(team=team, kind="github").first()if default_integration:validated_data["github_integration"] = default_integration@@ -8220,6 +8226,26 @@ def create_task(_capture_report_task_refused_without_repository(team=team, report_id=str(signal_report.id))raise ValidationError({"repository": REPORT_IMPLEMENTATION_REPOSITORY_UNRESOLVED})+ if signal_report is not None and signal_report_task_relationship in (None, "implementation"):+ from products.signals.backend.facade.github_optional import ( # noqa: PLC0415 - keeps the signals model graph out of tasks facade initialization+ github_optional_pilot_enabled,+ pilot_repository_integration_id,+ )++ if github_optional_pilot_enabled(team_id):+ integration_id = (+ pilot_repository_integration_id(team_id, validated_data["repository"])+ if validated_data.get("repository")+ else None+ )+ if integration_id is None:+ raise ValidationError(+ {+ "repository": "Connect or repair GitHub access to the selected repository before creating a pull request."+ }+ )+ validated_data["github_integration"] = Integration.objects.get(team_id=team_id, id=integration_id)+logger.info("Creating task with data: %s", validated_data)with transaction.atomic():if signal_report_id:@@ -9037,6 +9063,12 @@ def _activate_warm_run(def _report_task_github_integration_id(team: Team, *, code_access_allowed: bool) -> int | None:if not code_access_allowed:return None+ from products.signals.backend.facade.github_optional import (+ github_optional_pilot_enabled, # noqa: PLC0415 - keeps the signals model graph out of tasks facade initialization+ )++ if github_optional_pilot_enabled(team.id):+ return readonly_github_integration_id(team.id)integration = Integration.objects.filter(team=team, kind="github").first()return integration.id if integration is not None else Nonediff --git a/products/tasks/backend/facade/repo_selection_types.py b/products/tasks/backend/facade/repo_selection_types.pyindex f85638722ff8..9795ae9eb653 100644--- a/products/tasks/backend/facade/repo_selection_types.py+++ b/products/tasks/backend/facade/repo_selection_types.py@@ -6,6 +6,6 @@ Kept separate from ``facade.repo_selection`` so callers that only need the frame(e.g. signals' ``artefact_schemas``), which must stay dependency-light."""-from products.tasks.backend.logic.repo_selection.types import RepoSelectionResult+from products.tasks.backend.logic.repo_selection.types import ReportResearchContext, RepoSelectionResult-__all__ = ["RepoSelectionResult"]+__all__ = ["RepoSelectionResult", "ReportResearchContext"]diff --git a/products/tasks/backend/logic/repo_selection/agent.py b/products/tasks/backend/logic/repo_selection/agent.pyindex fac52e05ffb3..7d9b128f1e6c 100644--- a/products/tasks/backend/logic/repo_selection/agent.py+++ b/products/tasks/backend/logic/repo_selection/agent.py@@ -351,6 +351,8 @@ def _build_repo_selection_prompt(# So is `autostart_eligible`: it records how the repo was chosen, which is the caller's fact,# not the model's. Offering it would let untrusted context talk the model into vetoing autostart.schema.get("properties", {}).pop("autostart_eligible", None)+ schema.get("properties", {}).pop("research_context", None)+ schema.get("$defs", {}).pop("ReportResearchContext", None)schema_json = json.dumps(schema, indent=2)visibility = visibility or {}repo_list = "\n".join(@@ -787,6 +789,7 @@ async def select_repository(# A selection this agent made is a candidate-list pick for a caller that asked for one, so it# carries full autostart authority no matter what the model emitted.result.autostart_eligible = True+ result.research_context = Nonetry:if result.repository is not None:result.repository = result.repository.strip().lower()diff --git a/products/tasks/backend/logic/repo_selection/types.py b/products/tasks/backend/logic/repo_selection/types.pyindex 7a749e62a3d0..996d85a14a86 100644--- a/products/tasks/backend/logic/repo_selection/types.py+++ b/products/tasks/backend/logic/repo_selection/types.py@@ -1,8 +1,17 @@from __future__ import annotations+from typing import Literal+from pydantic import BaseModel, Field+class ReportResearchContext(BaseModel):+ mode: Literal["code", "data_only"]+ repository_status: Literal[+ "available", "no_integration", "no_repositories", "selection_unresolved", "github_unavailable"+ ]++class RepoSelectionResult(BaseModel):"""Outcome of repository selection: the chosen repo (or none) and why.@@ -14,6 +23,9 @@ class RepoSelectionResult(BaseModel):repository: str | None = Field(description="Selected repository in 'owner/repo' format, or null if none of the candidates are relevant.")+ research_context: ReportResearchContext | None = Field(+ default=None, description="Server-recorded code availability during report research; null for older reports."+ )reason: str = Field(description=("Why this repository was selected (or why none matched). When cache queries were made, "diff --git a/products/tasks/backend/logic/services/custom_prompt_internals.py b/products/tasks/backend/logic/services/custom_prompt_internals.pyindex bc9c551a3ed0..86fab04d46c3 100644--- a/products/tasks/backend/logic/services/custom_prompt_internals.py+++ b/products/tasks/backend/logic/services/custom_prompt_internals.py@@ -278,6 +278,11 @@ class AgentTurnFailed(RuntimeError):return self.category in UPSTREAM_RETRYABLE_ERROR_CATEGORIES+class RepositoryContextUnavailable(SandboxRunFailed):+ def __init__(self, error_type: str, message: str | None = None) -> None:+ super().__init__(message or error_type, error_type=error_type)++SESSION_FILE_ARTIFACT_TYPE = "context"@@ -942,6 +947,14 @@ async def _drain_final_log(cause = f" (cause: {error_message})" if error_message else ""message = f"custom_prompt - drain_final_log: TaskRun reached terminal status={refreshed_status}{cause} — {reason}"if refreshed_status == TaskRun.Status.FAILED:+ if failure_error_type in {+ "RepositoryAccessError",+ "RepositoryCloneError",+ "GitHubAuthenticationError",+ "GitHubIntegrationError",+ "GitHubRateLimitedError",+ }:+ raise RepositoryContextUnavailable(failure_error_type, f"{message} ({failure_error_type})")raise SandboxRunFailed(message, error_type=failure_error_type)raise RuntimeError(message)diff --git a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.pyindex cbfc92c98eb0..5550d0e0ccdc 100644--- a/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py+++ b/products/tasks/backend/temporal/process_task/activities/provision_sandbox.py@@ -449,6 +449,7 @@ def _resolve_sandbox_github_token("""if ctx.github_read_access:github_token = get_readonly_github_token(ctx.team_id, repositories=ctx.repositories) or ""+ TaskRun.update_state_atomic(ctx.run_id, updates={"github_read_access_available": bool(github_token)})if github_token:log_with_activity_context("Read-only GitHub token minted for evidence gathering", run_id=ctx.run_id)else:diff --git a/products/tasks/backend/temporal/process_task/activities/tests/test_update_task_run_status.py b/products/tasks/backend/temporal/process_task/activities/tests/test_update_task_run_status.pyindex 035c5569cc43..321f0b4d7ac8 100644--- a/products/tasks/backend/temporal/process_task/activities/tests/test_update_task_run_status.py+++ b/products/tasks/backend/temporal/process_task/activities/tests/test_update_task_run_status.py@@ -119,11 +119,13 @@ class TestUpdateTaskRunStatusActivity:run_id=str(test_task_run.id),status=TaskRun.Status.FAILED,error_message=error_msg,+ error_type="RepositoryAccessError",)async_to_sync(activity_environment.run)(update_task_run_status, input_data)test_task_run.refresh_from_db()assert test_task_run.error_message == error_msg+ assert test_task_run.state["failure_error_type"] == "RepositoryAccessError"@pytest.mark.django_db(transaction=True)def test_timed_out_inactivity_sets_state_marker_without_error_message(self, activity_environment, test_task_run):diff --git a/products/tasks/backend/temporal/process_task/activities/update_task_run_status.py b/products/tasks/backend/temporal/process_task/activities/update_task_run_status.pyindex de5866a96db6..5358648dc4d0 100644--- a/products/tasks/backend/temporal/process_task/activities/update_task_run_status.py+++ b/products/tasks/backend/temporal/process_task/activities/update_task_run_status.py@@ -103,6 +103,14 @@ def update_task_run_status(input: UpdateTaskRunStatusInput) -> None:returntask_run.status = input.status+ if (+ input.status == TaskRun.Status.FAILED+ and input.error_type+ and not (task_run.state or {}).get("failure_error_type")+ ):+ task_run.state = TaskRun.update_state_atomic(+ task_run.id, updates={"failure_error_type": input.error_type}+ )if input.error_message:task_run.error_message = input.error_messagemarker = TIMED_OUT_INACTIVITY_STATE_KEY if input.timed_out_inactivity else input.timeout_markerdiff --git a/products/tasks/backend/tests/agent_log_fixtures.py b/products/tasks/backend/tests/agent_log_fixtures.pyindex 1af617854726..4183d9db7417 100644--- a/products/tasks/backend/tests/agent_log_fixtures.py+++ b/products/tasks/backend/tests/agent_log_fixtures.py@@ -161,7 +161,7 @@ def _cost_less_usage_update_line(used: int = 1000) -> str:)-@dataclass+@dataclass(frozen=False)class FakeTaskRun:id: str = "run-1"task_id: str = "task-1"diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.pyindex 1638708cd016..523917da624a 100644--- a/products/tasks/backend/tests/test_api.py+++ b/products/tasks/backend/tests/test_api.py@@ -3034,22 +3034,42 @@ class TestTaskAPI(BaseTaskAPITest):# a scout's no-repo selection is not authoritative for a human "Create PR" click, so it# falls through to the cascade rather than making a task that can never push("persisted_no_repo", True, None, "acme/web"),+ ("pilot_without_access", True, "example/app", None, True, False),+ ("pilot_with_access", True, "example/app", "example/app", True, True),+ ("pilot_temporary_failure", True, "example/app", None, True, True, True),])def test_create_signal_report_task_resolves_repository_when_none_provided(- self, _name, has_artefact, persisted_repository, expected_repository+ self,+ _name,+ has_artefact,+ persisted_repository,+ expected_repository,+ pilot=False,+ repository_available=True,+ temporary_failure=False,):+ from products.signals.backend.github_optional import RepositoryAccessTemporarilyUnavailablefrom products.signals.backend.models import SignalReport, SignalReportArtefact- Integration.objects.create(+ integration = Integration.objects.create(team=self.team,kind="github",integration_id="gh-cascade",- config={"installation_id": "gh-cascade"},+ config={"installation_id": "gh-cascade", "account": {"type": "User"}},sensitive_config={},repository_cache=[{"full_name": "acme/web", "name": "web", "id": 1}],repository_cache_updated_at=django_timezone.now(),)+ if pilot:+ Integration.objects.create(+ team=self.team,+ kind="github",+ integration_id="gh-other",+ config={"installation_id": "gh-other", "account": {"type": "Organization"}},+ repository_cache=[{"full_name": "example/other", "name": "other", "id": 2}],+ repository_cache_updated_at=django_timezone.now(),+ )report = SignalReport.objects.create(team=self.team)if has_artefact:SignalReportArtefact.objects.create(@@ -3059,28 +3079,50 @@ class TestTaskAPI(BaseTaskAPITest):content=RepoSelectionResult(repository=persisted_repository, reason="test").model_dump_json(),)- response = self.client.post(- "/api/projects/@current/tasks/",- {- "title": "Signal Task",- "description": "From a signal report",- "origin_product": "signal_report",- "signal_report": str(report.id),- },- format="json",- )+ with (+ patch("products.signals.backend.facade.github_optional.github_optional_pilot_enabled", return_value=pilot),+ patch(+ "products.signals.backend.facade.github_optional.pilot_repository_integration_id",+ return_value=integration.id if repository_available else None,+ side_effect=RepositoryAccessTemporarilyUnavailable(wait=30) if temporary_failure else None,+ ),+ ):+ response = self.client.post(+ "/api/projects/@current/tasks/",+ {+ "title": "Signal Task",+ "description": "From a signal report",+ "origin_product": "signal_report",+ "signal_report": str(report.id),+ },+ format="json",+ )+ if temporary_failure:+ self.assertEqual(response.status_code, status.HTTP_503_SERVICE_UNAVAILABLE)+ self.assertEqual(response.headers["Retry-After"], "30")+ self.assertFalse(Task.objects.filter(signal_report=report).exists())+ return+ if pilot and not repository_available:+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertFalse(Task.objects.filter(signal_report=report).exists())+ returnself.assertEqual(response.status_code, status.HTTP_201_CREATED)self.assertEqual(response.json()["repository"], expected_repository)+ if pilot:+ self.assertEqual(Task.objects.get(id=response.json()["id"]).github_integration_id, integration.id)@parameterized.expand([("allowed", tasks_access.DesktopAccessDecision.ALLOWED, True),("refused", tasks_access.DesktopAccessDecision.STARTUP_PLAN, False),("unresolvable", DesktopAccessResolutionError("cannot verify"), False),+ ("pilot_broken_github", tasks_access.DesktopAccessDecision.ALLOWED, False, True),])- def test_discussion_starts_repo_less_and_credential_follows_the_desktop_gate(self, _name, decision, entitled):+ def test_discussion_starts_repo_less_and_credential_follows_the_desktop_gate(+ self, _name, decision, entitled, pilot=False+ ):# A "Discuss" kickoff never clones: the sandbox boots repo-less so the first answer is not# held behind a checkout. It is also credential-less for a caller the gate refuses, so the# generally-available Inbox never 403s on the click this path exists to unblock. An@@ -3095,6 +3137,7 @@ class TestTaskAPI(BaseTaskAPITest):integration_id="gh-cascade",config={"installation_id": "gh-cascade"},sensitive_config={},+ errors="TOKEN_REFRESH_FAILED" if pilot else "",repository_cache=[{"full_name": "acme/web", "name": "web", "id": 1}],repository_cache_updated_at=django_timezone.now(),)@@ -3107,7 +3150,10 @@ class TestTaskAPI(BaseTaskAPITest):)gate = {"side_effect": decision} if isinstance(decision, Exception) else {"return_value": decision}- with patch("products.tasks.backend.logic.services.code_usage_gate.get_desktop_access_decision", **gate):+ with (+ patch("products.tasks.backend.logic.services.code_usage_gate.get_desktop_access_decision", **gate),+ patch("products.signals.backend.facade.github_optional.github_optional_pilot_enabled", return_value=pilot),+ ):response = self.client.post("/api/projects/@current/tasks/",{diff --git a/products/tasks/backend/tests/test_multi_turn_session.py b/products/tasks/backend/tests/test_multi_turn_session.pyindex 6186d0651239..c8642e6a4be4 100644--- a/products/tasks/backend/tests/test_multi_turn_session.py+++ b/products/tasks/backend/tests/test_multi_turn_session.py@@ -27,6 +27,7 @@ from products.tasks.backend.logic.services.custom_prompt_internals import (AgentTurnFailed,CustomPromptSandboxContext,EmptyAgentTurnError,+ RepositoryContextUnavailable,SandboxRunFailed,TurnPollResult,TurnPollTimeout,@@ -1171,6 +1172,19 @@ class TestPollForTurnSurfacesAgentError:assert error.value.error_type == error_typeassert error.value.transient is transient+ @pytest.mark.asyncio+ @pytest.mark.parametrize("failure", ["RepositoryAccessError", "RepositoryCloneError", "GitHubAuthenticationError"])+ async def test_repository_failure_survives_the_task_boundary(self, failure):+ run = FakeTaskRun(status="failed", error_message="Activity task failed", state={"failure_error_type": failure})+ with (+ patch("posthog.storage.object_storage.read", return_value=""),+ patch("asyncio.sleep", new=AsyncMock()),+ patch("products.tasks.backend.logic.services.custom_prompt_internals.POLL_INTERVAL_SECONDS", 0),+ patch("products.tasks.backend.models.TaskRun.objects.get", return_value=run),+ ):+ with pytest.raises(RepositoryContextUnavailable, match=failure):+ await poll_for_turn(run)+@parameterized.expand([("upstream_provider_failure", "API Error: 429 rate_limit_error", True),diff --git a/products/tasks/backend/tests/test_sandbox_utils.py b/products/tasks/backend/tests/test_sandbox_utils.pyindex 4eddc5bc37a8..940966f49920 100644--- a/products/tasks/backend/tests/test_sandbox_utils.py+++ b/products/tasks/backend/tests/test_sandbox_utils.py@@ -148,19 +148,23 @@ def test_repository_snapshot_reuse_is_refused_for_read_only_runs(@parameterized.expand([- ("repo_less", None, False),- ("repo_backed", "acme/repo", True),+ ("repo_less", None, False, True),+ ("repo_backed", "acme/repo", True, True),+ ("mint_unavailable", None, False, False),])@patch("products.tasks.backend.temporal.process_task.activities.provision_sandbox.emit_agent_log")+@patch("products.tasks.backend.temporal.process_task.activities.provision_sandbox.TaskRun.update_state_atomic")@patch("products.tasks.backend.temporal.process_task.activities.provision_sandbox.get_sandbox_github_token")@patch("products.tasks.backend.temporal.process_task.activities.provision_sandbox.get_readonly_github_token")def test_readonly_request_takes_priority_over_full_credential_path(_name: str,repository: str | None,has_repo: bool,+ token_available: bool,mock_readonly: MagicMock,mock_full: MagicMock,+ mock_state: MagicMock,_mock_log: MagicMock,) -> None:# Task creation attaches the team's GitHub integration to every task, so a run on a@@ -175,7 +179,7 @@ def test_readonly_request_takes_priority_over_full_credential_path(_resolve_sandbox_github_token,)- mock_readonly.return_value = "READONLY_TOKEN"+ mock_readonly.return_value = "READONLY_TOKEN" if token_available else Nonectx = TaskProcessingContext(task_id="t",run_id="r",@@ -193,7 +197,8 @@ def test_readonly_request_takes_priority_over_full_credential_path()mock_readonly.assert_called_once_with(ctx.team_id, repositories=ctx.repositories)- assert token == "READONLY_TOKEN"+ assert token == ("READONLY_TOKEN" if token_available else "")+ mock_state.assert_called_once_with(ctx.run_id, updates={"github_read_access_available": token_available})mock_full.assert_not_called()