Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(tasks): grant repo-less loop runs read-only github access (#83861)

Tom Owers26e6a1773d9d
diff --git a/products/tasks/backend/logic/services/loop_runs.py b/products/tasks/backend/logic/services/loop_runs.py
index e145db836800..c10430825815 100644
--- a/products/tasks/backend/logic/services/loop_runs.py
+++ b/products/tasks/backend/logic/services/loop_runs.py
@@ -744,6 +744,12 @@ def _create_loop_task_and_run(loop: Loop, trigger: LoopTrigger | None, trigger_c
# regular task's sandbox_environment_id flows through Task._build_task.
if loop.sandbox_environment_id is not None:
extra_state["sandbox_environment_id"] = str(loop.sandbox_environment_id)
+ # A repo-less loop still needs `gh` for evidence gathering (PR digests, commit
+ # history), so provisioning mints the team-scoped read-only GitHub token for it
+ # (see get_readonly_github_token; a team with no usable integration skips the
+ # mint). Repo-pinned loops get the repository integration's token instead.
+ if not loop.repositories:
+ extra_state["github_read_access"] = True
# Reclaim the sandbox promptly once the agent goes idle — a loop run is unattended,
# so nothing sends a follow-up. CI-watching loops keep the default window so the
# sandbox survives the orchestrator's CI follow-up cadence.
diff --git a/products/tasks/backend/tests/test_loop_runs.py b/products/tasks/backend/tests/test_loop_runs.py
index ee70a075ec78..044f2b9c35a1 100644
--- a/products/tasks/backend/tests/test_loop_runs.py
+++ b/products/tasks/backend/tests/test_loop_runs.py
@@ -480,6 +480,30 @@ class TestFireLoopCreatesRun(LoopRunsTestCase):
self.assertEqual(task_run.state["config_snapshot"]["notifications"], loop.notifications)
self.assertEqual(task_run.state["config_snapshot"]["repositories"], loop.repositories)
+ @parameterized.expand(
+ [
+ ("repo_less_loop_gets_read_only_github", False, True),
+ ("repo_pinned_loop_uses_repository_integration", True, False),
+ ]
+ )
+ def test_fire_grants_github_read_access_only_to_repo_less_loops(self, _name, pin_repository, expected_flag):
+ repositories = []
+ if pin_repository:
+ integration = Integration.objects.create(team=self.team, kind="github", integration_id="12345", config={})
+ repositories = [{"github_integration_id": integration.id, "full_name": "acme/repo"}]
+ loop = self.create_loop(repositories=repositories)
+ trigger = self.create_trigger(loop)
+
+ result = fire_loop(loop, trigger, f"fire-{_name}", "rendered context")
+
+ self.assertTrue(result.created)
+ assert result.task_run_id is not None
+ task_run = TaskRun.objects.get(id=result.task_run_id)
+ if expected_flag:
+ self.assertIs(task_run.state["github_read_access"], True)
+ else:
+ self.assertNotIn("github_read_access", task_run.state)
+
@parameterized.expand(
[
("claude_default_resolves_to_sonnet_5", "claude", "", None, "claude-sonnet-5", None),