: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
fix(slack-app): stop agent runs when the org has not approved AI processing (#127302)
Vojta Bartoš1f4e3dfe3603
diff --git a/products/slack_app/backend/api.py b/products/slack_app/backend/api.pyindex 4af780ddda3d..6b56d95549da 100644--- a/products/slack_app/backend/api.py+++ b/products/slack_app/backend/api.py@@ -333,6 +333,12 @@ QUOTA_EXHAUSTED_MESSAGE = ("Top up at https://us.posthog.com/organization/billing to continue.")+AI_PROCESSING_NOT_APPROVED_MESSAGE = (+ ":lock: *PostHog AI is turned off for your organization*\n"+ "I can't work on this request until AI analysis is on. "+ "Ask an organization admin to turn it on in <{settings_url}|organization settings>."+)+def post_quota_exhausted_denial(*,@@ -367,6 +373,35 @@ def post_quota_exhausted_denial()+def post_ai_processing_not_approved_denial(+ *,+ integration: WorkspaceIntegration,+ slack: SlackIntegration,+ channel: str,+ thread_ts: str,+ slack_user_id: str,+ context: str,+) -> None:+ """Post the AI-consent denial message into a Slack thread."""+ logger.info(+ "slack_app_slack_blocked_by_ai_consent",+ context=context,+ team_id=integration.team_id,+ channel=channel,+ thread_ts=thread_ts,+ )+ _post_slack_user_feedback(+ slack,+ channel,+ slack_user_id,+ thread_ts,+ AI_PROCESSING_NOT_APPROVED_MESSAGE.format(+ settings_url=f"{settings.SITE_URL}/settings/organization-details#organization-ai-consent"+ ),+ prefer_thread_message=True,+ )++def _post_slack_user_feedback(slack: SlackIntegration,channel: str,diff --git a/products/slack_app/backend/temporal/activities/billing.py b/products/slack_app/backend/temporal/activities/billing.pyindex 925cb31e6f0e..f99c5e95c105 100644--- a/products/slack_app/backend/temporal/activities/billing.py+++ b/products/slack_app/backend/temporal/activities/billing.py@@ -4,7 +4,7 @@ from temporalio import activityfrom posthog.temporal.common.utils import close_db_connectionsfrom products.slack_app.backend.services.workspace_integrations import require_workspace_integration-from products.slack_app.backend.temporal.helpers import block_if_team_over_quota+from products.slack_app.backend.temporal.helpers import block_if_ai_processing_not_approved, block_if_team_over_quotafrom products.slack_app.backend.temporal.types import PostHogCodeSlackMentionWorkflowInputslogger = structlog.get_logger(__name__)@@ -18,12 +18,14 @@ def enforce_posthog_code_billing_quota_activity(thread_ts: str,slack_user_id: str,) -> bool:- """Block the workflow when the team has exhausted its AI-credits quota.+ """Block the workflow when the organization has not approved AI data processing,+ or when the team has exhausted its AI-credits quota.- Returns True when the team is over quota and the workflow must stop. Called- as the first activity in the mention workflow so the bot never proceeds to- Slack roundtrips, thread fetches, or billable LLM calls (the classifier,- notably) for an over-quota team.+ Returns True when the workflow must stop. Called as the first activity in the+ mention workflow so the bot never proceeds to Slack roundtrips, thread fetches,+ or LLM calls (the classifier, notably) for a team that cannot use AI. The+ consent check lives in this activity, not a new one, so in-flight workflow+ histories still replay."""from posthog.models.integration import SlackIntegration@@ -36,6 +38,16 @@ def enforce_posthog_code_billing_quota_activity(# PostHog owns, chitchat included. Stop the run without the message and keep the# message for a mention or a confirmed reply, which are the turns that asked for work.unrequested = (inputs.untagged_followup or inputs.untagged_question) and not inputs.untagged_confirmed+ if block_if_ai_processing_not_approved(+ integration=integration,+ slack=slack,+ channel=channel,+ thread_ts=thread_ts,+ slack_user_id=slack_user_id,+ context="task_create",+ post_denial=not unrequested,+ ):+ return Trueblocked = block_if_team_over_quota(integration=integration,slack=slack,diff --git a/products/slack_app/backend/temporal/activities/task_creation.py b/products/slack_app/backend/temporal/activities/task_creation.pyindex fde22c85693b..52c24a2873f6 100644--- a/products/slack_app/backend/temporal/activities/task_creation.py+++ b/products/slack_app/backend/temporal/activities/task_creation.py@@ -34,7 +34,12 @@ from products.slack_app.backend.temporal.attachments import (prepare_slack_file_artifacts,prepare_slack_thread_file_artifacts,)-from products.slack_app.backend.temporal.helpers import block_if_team_over_quota, safe_react, safe_unreact+from products.slack_app.backend.temporal.helpers import (+ block_if_ai_processing_not_approved,+ block_if_team_over_quota,+ safe_react,+ safe_unreact,+)from products.slack_app.backend.temporal.types import (SLACK_APP_PROCESSING_REACTION,PostHogCodeSlackMentionWorkflowInputs,@@ -651,6 +656,16 @@ def create_posthog_code_task_for_repo_activity(# Refuse before the :eyes: reaction or the permalink fetch: a denied# mention should not first ack-react and then refuse a second later.+ if block_if_ai_processing_not_approved(+ integration=integration,+ slack=slack,+ channel=channel,+ thread_ts=thread_ts,+ slack_user_id=slack_user_id,+ context="task_create",+ ):+ return+if block_if_team_over_quota(integration=integration,slack=slack,@@ -1047,6 +1062,16 @@ def forward_posthog_code_followup_activity()return True+ if block_if_ai_processing_not_approved(+ integration=integration,+ slack=slack,+ channel=channel,+ thread_ts=thread_ts,+ slack_user_id=slack_user_id,+ context="followup",+ ):+ return True+if block_if_team_over_quota(integration=integration,slack=slack,diff --git a/products/slack_app/backend/temporal/helpers.py b/products/slack_app/backend/temporal/helpers.pyindex 2919e8125695..139aa136e827 100644--- a/products/slack_app/backend/temporal/helpers.py+++ b/products/slack_app/backend/temporal/helpers.py@@ -52,6 +52,39 @@ def block_if_team_over_quota(return True+def block_if_ai_processing_not_approved(+ *,+ integration: Any,+ slack: Any,+ channel: str,+ thread_ts: str,+ slack_user_id: str,+ context: str,+ post_denial: bool = True,+) -> bool:+ """Refuse a Slack-bot turn when the organization has not approved AI data processing.++ Every turn sends Slack content to external AI services, so it needs the same org+ consent as PostHog AI in the web app. A null consent counts as not approved, as in+ the settings toggle. Returns True when the turn was blocked.+ """+ from products.slack_app.backend.api import post_ai_processing_not_approved_denial++ if integration.team.organization.is_ai_data_processing_approved:+ return False++ if post_denial:+ post_ai_processing_not_approved_denial(+ integration=integration,+ slack=slack,+ channel=channel,+ thread_ts=thread_ts,+ slack_user_id=slack_user_id,+ context=context,+ )+ return True++# Reaction errors that should never abort a follow-up activity — the 👀/🔍 reaction is purely# cosmetic UX feedback, so a deleted/unreachable message or a missing reaction is a no-op._BENIGN_REACTION_ERRORS = frozenset({"already_reacted", "message_not_found", "no_reaction", "cant_react"})diff --git a/products/slack_app/backend/temporal/tests/activities/test_billing_quota_activity.py b/products/slack_app/backend/temporal/tests/activities/test_billing_quota_activity.pyindex fa1191a22426..e70dd515c1cd 100644--- a/products/slack_app/backend/temporal/tests/activities/test_billing_quota_activity.py+++ b/products/slack_app/backend/temporal/tests/activities/test_billing_quota_activity.py@@ -80,3 +80,25 @@ class TestEnforcePostHogCodeBillingQuotaActivity(TestCase):):assert self._call(untagged_followup=True, confirmed=False) is Falsemock_denial.assert_not_called()++ @parameterized.expand(+ [+ ("not_approved_mention", False, False, True, True),+ ("null_consent_mention", None, False, True, True),+ ("not_approved_unconfirmed_reply", False, True, True, False),+ ("approved_mention", True, False, False, False),+ ]+ )+ def test_ai_consent_gates_the_run_before_the_quota(+ self, _name, approved, untagged_followup, expect_blocked, expect_denial+ ):+ self.org.is_ai_data_processing_approved = approved+ self.org.save()+ with (+ patch("ee.billing.quota_limiting.is_team_limited", return_value=False),+ patch("products.slack_app.backend.api.post_ai_processing_not_approved_denial") as mock_denial,+ ):+ blocked = self._call(untagged_followup=untagged_followup, confirmed=False)++ assert blocked is expect_blocked+ assert mock_denial.called is expect_denial