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): let research sandboxes upload their own run log (#106506)

Andrew Maguire0feea08d8448
diff --git a/products/tasks/backend/temporal/oauth.py b/products/tasks/backend/temporal/oauth.py
index daf31cbc415d..d9b4aea2c0c9 100644
--- a/products/tasks/backend/temporal/oauth.py
+++ b/products/tasks/backend/temporal/oauth.py
@@ -10,6 +10,7 @@ from posthog.temporal.oauth import (
ARRAY_APP_CLIENT_ID_DEV,
ARRAY_APP_CLIENT_ID_EU,
ARRAY_APP_CLIENT_ID_US,
+ RESEARCH_WITHHELD_SCOPES,
McpScopePreset,
PosthogMcpScopes,
SandboxOAuthApplication,
@@ -258,6 +259,12 @@ def create_oauth_access_token_for_run(
"""
if scopes is None:
scopes = dispatched_run_scopes(task, state)
+ # Only the provisioning activities omit `scopes`, and their token is the one the agent
+ # server uses to upload the run's own log through `append_log`, which requires
+ # `task:write`. The research MCP session keeps the withheld posture because
+ # `start_agent_server` mints its token with explicit scopes.
+ if scopes == "signals_research":
+ scopes = [*resolve_scopes(scopes), *RESEARCH_WITHHELD_SCOPES]
with transaction.atomic():
locked_task = (
Task.objects.select_for_update(of=("self",))
diff --git a/products/tasks/backend/temporal/tests/test_oauth.py b/products/tasks/backend/temporal/tests/test_oauth.py
index 54f9e31f67b1..ba9aa8d6ab68 100644
--- a/products/tasks/backend/temporal/tests/test_oauth.py
+++ b/products/tasks/backend/temporal/tests/test_oauth.py
@@ -4,7 +4,7 @@ from unittest.mock import MagicMock, patch
from posthog.models import Organization, Team
from posthog.models.organization import OrganizationMembership
from posthog.models.user import User
-from posthog.temporal.oauth import PosthogMcpScopes
+from posthog.temporal.oauth import PosthogMcpScopes, resolve_scopes
from products.tasks.backend.exceptions import TaskInvalidStateError
from products.tasks.backend.models import (
@@ -556,6 +556,18 @@ def test_workflow_run_scopes_never_exceed_request_or_snapshot(
(Task.OriginProduct.USER_CREATED, {"run_source": "agent"}, None, "read_only"),
(Task.OriginProduct.LOOP, {}, None, "read_only"),
(Task.OriginProduct.SIGNALS_SCOUT, {}, None, "signals_scout_reports"),
+ (
+ Task.OriginProduct.SIGNAL_REPORT,
+ {"pending_dispatch": {"posthog_mcp_scopes": "signals_research"}},
+ None,
+ [*resolve_scopes("signals_research"), "task:write"],
+ ),
+ (
+ Task.OriginProduct.SIGNAL_REPORT,
+ {"pending_dispatch": {"posthog_mcp_scopes": "signals_research"}},
+ "signals_research",
+ "signals_research",
+ ),
],
)
@patch("products.tasks.backend.temporal.oauth._create_oauth_access_token_for_user", return_value="token")