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): carry PR authorship mode into Slack follow-up runs (#90673)

posthog[bot]046cb2f84858
diff --git a/posthog/temporal/ai/slack_app/activities/task_creation.py b/posthog/temporal/ai/slack_app/activities/task_creation.py
index 53861033ab0e..1b32d109f1a1 100644
--- a/posthog/temporal/ai/slack_app/activities/task_creation.py
+++ b/posthog/temporal/ai/slack_app/activities/task_creation.py
@@ -1306,6 +1306,15 @@ def _resume_task_with_new_run(
if previous_state.get("slack_thread_url"):
extra_state["slack_thread_url"] = previous_state["slack_thread_url"]
+ # Carry the PR authorship mode forward. Without it, get_pr_authorship_mode re-derives USER for a
+ # Slack-origin task, so a creator with no personal GitHub install dead-ends every follow-up at the
+ # token guard. The first run already resolved the mode (BOT when there is no install), and the
+ # successor must resume under the same identity, like the cloud resume path. A carried BOT is
+ # re-checked after create_run below, so a creator who connected GitHub in the meantime gets
+ # their identity back.
+ if previous_state.get("pr_authorship_mode"):
+ extra_state["pr_authorship_mode"] = previous_state["pr_authorship_mode"]
+
# A successor launches its own agent server, so the whole triple is open again —
# including the runtime a live run could never be moved onto. Resolved rather than
# carried over, like the keys above: a preference changed since the previous run is
@@ -1356,6 +1365,21 @@ def _resume_task_with_new_run(
)
return True
+ # The carried mode can pin BOT after the creator connected GitHub, because the live-sandbox
+ # promotion in _refresh_sandbox_github never sees a successor's first turn. Re-check here so
+ # the run that follows the connection is the one that carries their identity. Best-effort:
+ # a failure leaves the run bot-authored, which still works.
+ try:
+ tasks_facade.promote_run_to_user_authorship(new_run.id, run_actor.id)
+ except Exception:
+ logger.exception(
+ "posthog_code_resume_authorship_promotion_failed",
+ channel=channel,
+ thread_ts=thread_ts,
+ task_id=str(mapping.task_id),
+ run_id=str(new_run.id),
+ )
+
uploaded_attachments, attachment_skips = _upload_prepared_slack_attachments(
tasks_facade,
task_run_id=new_run.id,
diff --git a/products/slack_app/backend/tests/test_followup_forwarding.py b/products/slack_app/backend/tests/test_followup_forwarding.py
index 34a224f34cbd..2bc04d36458c 100644
--- a/products/slack_app/backend/tests/test_followup_forwarding.py
+++ b/products/slack_app/backend/tests/test_followup_forwarding.py
@@ -1044,6 +1044,57 @@ class TestForwardPostHogCodeFollowupActivity(TestCase):
assert new_run.state["model"] == SLACK_DEFAULT_MODEL
assert new_run.state["runtime_adapter"]
+ @patch("products.tasks.backend.facade.temporal.dispatch_task_processing_workflow")
+ @patch("posthog.models.integration.SlackIntegration")
+ def test_a_resumed_run_carries_the_pr_authorship_mode(self, mock_slack_cls, mock_execute_workflow):
+ # `create_run` builds a fresh state, so without the carry-forward the follow-up re-derives
+ # USER authorship for a Slack task. A creator with no personal GitHub install then dead-ends
+ # at the token guard on every follow-up, while the first run ran as BOT.
+ self.task_run.status = self.TaskRun.Status.COMPLETED
+ self.task_run.state = {"pr_authorship_mode": "bot"}
+ self.task_run.save()
+ self._create_mapping()
+ mock_slack_cls.return_value = MagicMock()
+
+ inputs = _make_inputs(self.integration.id, self.user.id)
+ result = forward_posthog_code_followup_activity(
+ inputs, "C123", "1234.5678", "U_ALICE", "<@BOT> fix the tests", "1234.5679"
+ )
+
+ assert result is True
+ new_run = self.TaskRun.objects.get(id=mock_execute_workflow.call_args.kwargs["run_id"])
+ assert new_run.state["pr_authorship_mode"] == "bot"
+
+ @patch("products.tasks.backend.facade.temporal.dispatch_task_processing_workflow")
+ @patch("posthog.models.integration.SlackIntegration")
+ def test_a_resumed_run_promotes_bot_authorship_once_the_creator_connects_github(
+ self, mock_slack_cls, mock_execute_workflow
+ ):
+ # The carried BOT must not outlive its reason. A creator who connected GitHub after the
+ # previous run went terminal gets their identity on the successor, matching the promotion
+ # a live sandbox performs in _refresh_sandbox_github.
+ self.task_run.status = self.TaskRun.Status.COMPLETED
+ self.task_run.state = {"pr_authorship_mode": "bot"}
+ self.task_run.save()
+ self._create_mapping()
+ mock_slack_cls.return_value = MagicMock()
+ UserIntegration.objects.create(
+ user=self.user,
+ kind=UserIntegration.IntegrationKind.GITHUB,
+ integration_id="gh-1",
+ config={},
+ sensitive_config={"user_access_token": "at", "user_refresh_token": "rt"},
+ )
+
+ inputs = _make_inputs(self.integration.id, self.user.id)
+ result = forward_posthog_code_followup_activity(
+ inputs, "C123", "1234.5678", "U_ALICE", "<@BOT> fix the tests", "1234.5679"
+ )
+
+ assert result is True
+ new_run = self.TaskRun.objects.get(id=mock_execute_workflow.call_args.kwargs["run_id"])
+ assert new_run.state["pr_authorship_mode"] == "user"
+
@patch("products.tasks.backend.facade.api.signal_task_run_user_message", return_value=True)
@patch("posthog.models.integration.SlackIntegration")
def test_forwards_while_sandbox_is_still_provisioning(self, mock_slack_cls, mock_signal):
diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.py
index 6dfb56f50990..86ca2c179a14 100644
--- a/products/tasks/backend/facade/api.py
+++ b/products/tasks/backend/facade/api.py
@@ -1549,6 +1549,24 @@ def update_task_run_state(
return TaskRun.update_state_atomic(run_id, updates=updates, remove_keys=remove_keys)
+def promote_run_to_user_authorship(run_id: str | UUID, actor_user_id: int) -> bool:
+ """Promote a bot-authored run to user authorship when the actor now has a usable GitHub install.
+
+ Delegates to ``upgrade_run_to_user_authorship``, which persists the change and leaves
+ runs that are bot-authored by design (signal reports, caller-token runs) untouched.
+ Returns True when the run was promoted.
+ """
+ from products.tasks.backend.temporal.process_task.utils import ( # noqa: PLC0415 — keep tasks internals off the api import path
+ upgrade_run_to_user_authorship,
+ )
+
+ run = TaskRun.objects.select_related("task").filter(id=run_id).first()
+ actor_user = User.objects.filter(id=actor_user_id).first()
+ if run is None or actor_user is None:
+ return False
+ return upgrade_run_to_user_authorship(run, actor_user, run.state) is not None
+
+
def signal_task_run_client_activity(run_id: str | UUID, task_id: str | UUID, team_id: int) -> None:
"""Best-effort: tell the run's workflow a client command landed, so the idle timer resets."""
try: