: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(slack-app): tell a slack user how to reconnect github (#113295)
Vojta Bartoš73b18ec19bc5
diff --git a/products/slack_app/backend/temporal/activities/task_creation.py b/products/slack_app/backend/temporal/activities/task_creation.pyindex 1cc49a92be79..8796290d3d97 100644--- a/products/slack_app/backend/temporal/activities/task_creation.py+++ b/products/slack_app/backend/temporal/activities/task_creation.py@@ -46,6 +46,7 @@ _SLACK_RECOVERY_STRATEGY_RETRY = "retry"_SLACK_RECOVERY_STRATEGY_CONNECT_THEN_REPLAN = "connect_then_replan"_SLACK_RECOVERY_STRATEGY_UNBLOCK_AND_REPLAN = "unblock_and_replan"_SLACK_RECOVERY_STRATEGY_CANCELLED = "cancelled_resume"+_SLACK_RECOVERY_STRATEGY_RECONNECT_GITHUB = "reconnect_github"_THREAD_CONTEXT_TAG = "slack_thread_context"_THREAD_CONTEXT_UPDATE_TAG = "slack_thread_context_update"_INITIATOR_PLACEHOLDER = "<original user message was here>"@@ -1310,6 +1311,7 @@ def _terminal_recovery_strategy(previous_run: Any) -> str | None:_SLACK_RECOVERY_STRATEGY_RETRY,_SLACK_RECOVERY_STRATEGY_CONNECT_THEN_REPLAN,_SLACK_RECOVERY_STRATEGY_UNBLOCK_AND_REPLAN,+ _SLACK_RECOVERY_STRATEGY_RECONNECT_GITHUB,}:return strategyreturn _SLACK_RECOVERY_STRATEGY_RETRY@@ -1346,6 +1348,11 @@ def _build_terminal_recovery_prompt(previous_run: Any, user_text: str) -> str:"The previous sandbox was intentionally stopped. Resume only the work the user asks for now, and ""preserve any useful prior artifact or PR context."),+ _SLACK_RECOVERY_STRATEGY_RECONNECT_GITHUB: (+ "GitHub rejected the acting user's personal credentials on the previous run. Resolve them again "+ "before executing. If the connection now works, re-plan and continue. If it still does not, say so "+ "and ask the acting user to reconnect their personal GitHub in PostHog settings."+ ),}recovery_lines = [diff --git a/products/tasks/backend/temporal/process_task/activities/post_slack_update.py b/products/tasks/backend/temporal/process_task/activities/post_slack_update.pyindex 0f8c91162409..acaeb5a8ca73 100644--- a/products/tasks/backend/temporal/process_task/activities/post_slack_update.py+++ b/products/tasks/backend/temporal/process_task/activities/post_slack_update.py@@ -25,8 +25,23 @@ SLACK_RECOVERY_STRATEGY_CONNECT_THEN_REPLAN = "connect_then_replan"SLACK_RECOVERY_STRATEGY_UNBLOCK_AND_REPLAN = "unblock_and_replan"SLACK_RECOVERY_STRATEGY_CANCELLED = "cancelled_resume"SLACK_RECOVERY_STRATEGY_WAIT_FOR_SPEND_LIMIT = "wait_for_spend_limit"+SLACK_RECOVERY_STRATEGY_RECONNECT_GITHUB = "reconnect_github"SLACK_SPEND_LIMIT_ERROR_FRAGMENT = "this agent run reached its spend limit"+# Checked before the connect-then-replan markers, because a rejected personal credential needs+# the person to authorize again rather than a re-plan against the connections already there.+_RECONNECT_GITHUB_MARKERS = (+ "github user integration for this run requires reauthorization",+ "github user integration requires reauthorization",+)++_RECONNECT_GITHUB_CONNECT_LABEL = "Connect your GitHub in PostHog settings"+_RECONNECT_GITHUB_PROMPT = (+ "Your personal GitHub connection stopped working, so this run could not use it. "+ "{connect} to authorize it again, or to connect it for the first time. "+ "Then reply in this thread and I'll pick up the new credentials."+)+_CONNECT_THEN_REPLAN_MARKERS = ("not connected","connect github",@@ -75,6 +90,7 @@ _RECOVERY_PROMPTS = {SLACK_RECOVERY_STRATEGY_WAIT_FOR_SPEND_LIMIT: ("Wait for this run's spend limit to reset before replying in the thread."),+ SLACK_RECOVERY_STRATEGY_RECONNECT_GITHUB: _RECONNECT_GITHUB_PROMPT.format(connect=_RECONNECT_GITHUB_CONNECT_LABEL),}SLACK_DENIAL_STOP_MESSAGE = "Stopped after the denied action — reply here to continue with a different approach."@@ -242,7 +258,7 @@ def _mark_terminal_notified(task_run: Any, status: str, error: str | None = Noneupdates[SLACK_TERMINAL_NOTIFIED_ERROR_KEY] = error or ""recovery_strategy = _classify_failure_recovery(error or "")updates[SLACK_RECOVERY_STRATEGY_KEY] = recovery_strategy- updates[SLACK_RECOVERY_PROMPT_KEY] = _RECOVERY_PROMPTS[recovery_strategy]+ updates[SLACK_RECOVERY_PROMPT_KEY] = _recovery_prompt(recovery_strategy, task_run.team_id)elif status == TaskRun.Status.CANCELLED:updates[SLACK_RECOVERY_STRATEGY_KEY] = SLACK_RECOVERY_STRATEGY_CANCELLEDupdates[SLACK_RECOVERY_PROMPT_KEY] = _RECOVERY_PROMPTS[SLACK_RECOVERY_STRATEGY_CANCELLED]@@ -254,6 +270,8 @@ def _classify_failure_recovery(error: str) -> str:normalized = error.lower()if SLACK_SPEND_LIMIT_ERROR_FRAGMENT in normalized:return SLACK_RECOVERY_STRATEGY_WAIT_FOR_SPEND_LIMIT+ if any(marker in normalized for marker in _RECONNECT_GITHUB_MARKERS):+ return SLACK_RECOVERY_STRATEGY_RECONNECT_GITHUBif any(marker in normalized for marker in _CONNECT_THEN_REPLAN_MARKERS):return SLACK_RECOVERY_STRATEGY_CONNECT_THEN_REPLANif any(marker in normalized for marker in _UNBLOCK_AND_REPLAN_MARKERS):@@ -261,8 +279,19 @@ def _classify_failure_recovery(error: str) -> str:return SLACK_RECOVERY_STRATEGY_RETRY-def _failure_recovery_prompt(error: str) -> str:- return _RECOVERY_PROMPTS[_classify_failure_recovery(error)]+def _failure_recovery_prompt(error: str, team_id: int | None = None) -> str:+ return _recovery_prompt(_classify_failure_recovery(error), team_id)+++def _recovery_prompt(strategy: str, team_id: int | None) -> str:+ if strategy == SLACK_RECOVERY_STRATEGY_RECONNECT_GITHUB and team_id is not None:+ # The settings page offers Reconnect for a flagged connection and Connect when there is none,+ # so a single link serves a person whose credentials expired and a person who never connected.+ from products.slack_app.backend.services.slack_messages import personal_integrations_url++ link = f"<{personal_integrations_url(team_id)}|{_RECONNECT_GITHUB_CONNECT_LABEL}>"+ return _RECONNECT_GITHUB_PROMPT.format(connect=link)+ return _RECOVERY_PROMPTS[strategy]def _is_suppressed_permission_rejection_error(task_run: Any, error: str) -> bool:@@ -281,7 +310,7 @@ def _post_error_once(task_run: Any, handler: Any, error: str, task_url: str | Nohandler.post_note(SLACK_DENIAL_STOP_MESSAGE)else:handler.update_reaction("x")- handler.post_error(error, task_url, recovery_hint=_failure_recovery_prompt(error))+ handler.post_error(error, task_url, recovery_hint=_failure_recovery_prompt(error, task_run.team_id))_mark_terminal_notified(task_run, TaskRun.Status.FAILED, error)diff --git a/products/tasks/backend/temporal/process_task/activities/tests/test_post_slack_update.py b/products/tasks/backend/temporal/process_task/activities/tests/test_post_slack_update.pyindex 34cced25be80..4f0ab33bc115 100644--- a/products/tasks/backend/temporal/process_task/activities/tests/test_post_slack_update.py+++ b/products/tasks/backend/temporal/process_task/activities/tests/test_post_slack_update.py@@ -1,10 +1,13 @@import pytestfrom unittest.mock import MagicMock, patch+from django.test import override_settings+from products.tasks.backend.temporal.process_task.activities.post_slack_update import (SLACK_DENIAL_STOP_MESSAGE,SLACK_PERMISSION_REJECTION_ERROR_FRAGMENT,SLACK_RECOVERY_STRATEGY_CONNECT_THEN_REPLAN,+ SLACK_RECOVERY_STRATEGY_RECONNECT_GITHUB,SLACK_RECOVERY_STRATEGY_RETRY,SLACK_RECOVERY_STRATEGY_UNBLOCK_AND_REPLAN,_classify_failure_recovery,@@ -42,11 +45,27 @@ from products.tasks.backend.temporal.process_task.activities.post_slack_update iSLACK_RECOVERY_STRATEGY_RETRY,"retry",),+ (+ "GitHub user integration for this run requires reauthorization",+ SLACK_RECOVERY_STRATEGY_RECONNECT_GITHUB,+ "<http://localhost:8000/project/42/settings/user-personal-integrations|Connect your GitHub in PostHog settings>",+ ),+ (+ "GitHub user integration requires reauthorization and no team installation is available",+ SLACK_RECOVERY_STRATEGY_RECONNECT_GITHUB,+ "<http://localhost:8000/project/42/settings/user-personal-integrations|Connect your GitHub in PostHog settings>",+ ),+ (+ "Linear integration requires reauthorization",+ SLACK_RECOVERY_STRATEGY_RETRY,+ "retry",+ ),],)+@override_settings(SITE_URL="http://localhost:8000")def test_classify_failure_recovery(error: str, expected_strategy: str, expected_prompt_fragment: str) -> None:assert _classify_failure_recovery(error) == expected_strategy- assert expected_prompt_fragment in _failure_recovery_prompt(error)+ assert expected_prompt_fragment in _failure_recovery_prompt(error, team_id=42)@patch("products.tasks.backend.models.TaskRun.update_state_atomic")