From 3ec08244513fa2f1b2ace52066f46f53e0a31ea4 Mon Sep 17 00:00:00 2001 From: Alex Sohn Date: Mon, 9 Nov 2026 15:22:34 -0500 Subject: [PATCH 1/8] feat(slack): Reply in the thread when an autofix run runs out of Seer credits Slack posts the autofix out-of-credits notification to every thread watching a run that fails for lack of credits, and the completion hook reports runs whose PRs all failed for credits as out_of_credits. SeerAutofixOperator.can_trigger_next_step decides whether the step after an autofix update can run, so the update hides the next-step button when the org can't run it. Co-Authored-By: Claude Opus 5.5 --- src/sentry/seer/autofix/on_completion_hook.py | 10 +- src/sentry/seer/entrypoints/operator.py | 56 ++++++++- .../seer/entrypoints/slack/entrypoint.py | 11 +- .../test_autofix_on_completion_hook.py | 33 +++++ .../seer/entrypoints/slack/test_slack.py | 115 ++++++++++++++++++ .../sentry/seer/entrypoints/test_operator.py | 88 ++++++++++++++ 6 files changed, 306 insertions(+), 7 deletions(-) diff --git a/src/sentry/seer/autofix/on_completion_hook.py b/src/sentry/seer/autofix/on_completion_hook.py index 2979edb62dce..5043ebe6ee23 100644 --- a/src/sentry/seer/autofix/on_completion_hook.py +++ b/src/sentry/seer/autofix/on_completion_hook.py @@ -63,6 +63,7 @@ clear_preference_automation_handoff, get_automation_handoff, ) +from sentry.seer.credits import OUT_OF_CREDITS from sentry.seer.entrypoints.operator import ( SeerActivityAttribution, SeerAutofixOperator, @@ -574,8 +575,15 @@ def _send_step_webhook( if state.repo_pr_states and not pull_requests: # Seer adds one entry per repository before asking the provider to open # a PR. If every request fails, the entries remain but there is no PR. + out_of_credits = any( + pr.pr_creation_error_code == OUT_OF_CREDITS + for pr in state.repo_pr_states.values() + ) cls._notify_entrypoints_of_failure( - organization, run_id, group, PR_CREATION_FAILED_REASON + organization, + run_id, + group, + OUT_OF_CREDITS if out_of_credits else PR_CREATION_FAILED_REASON, ) return diff --git a/src/sentry/seer/entrypoints/operator.py b/src/sentry/seer/entrypoints/operator.py index 727dc5503643..7e843926f217 100644 --- a/src/sentry/seer/entrypoints/operator.py +++ b/src/sentry/seer/entrypoints/operator.py @@ -1,5 +1,5 @@ import logging -from typing import Any, NotRequired, TypedDict +from typing import TYPE_CHECKING, Any, NotRequired, TypedDict from taskbroker_client.retry import Retry @@ -71,6 +71,9 @@ from sentry.utils.safe import get_path, safe_execute from sentry.viewer_context import ActorId, ActorType +if TYPE_CHECKING: + from sentry.notifications.platform.templates.seer import SeerAutofixUpdate + SEER_EVENT_TO_ACTIVITY_TYPE: dict[SentryAppEventType, ActivityType] = { SentryAppEventType.SEER_ROOT_CAUSE_STARTED: ActivityType.SEER_RCA_STARTED, SentryAppEventType.SEER_ROOT_CAUSE_COMPLETED: ActivityType.SEER_RCA_COMPLETED, @@ -114,6 +117,14 @@ class SeerActivityAttribution(TypedDict): AGENT_REFRESH_TIMEOUT_SECONDS = 60 +# The Seer operation (and so the credit check) for the autofix step a stopping point starts. +AUTOFIX_STOPPING_POINT_TO_SEER_OPERATION: dict[AutofixStoppingPoint, SeerOperation] = { + AutofixStoppingPoint.ROOT_CAUSE: SeerOperation.AUTOFIX_ROOT_CAUSE, + AutofixStoppingPoint.SOLUTION: SeerOperation.AUTOFIX_SOLUTION, + AutofixStoppingPoint.CODE_CHANGES: SeerOperation.AUTOFIX_CODE_CHANGES, + AutofixStoppingPoint.OPEN_PR: SeerOperation.AUTOFIX_OPEN_PR, +} + def has_seer_autofix_entrypoint_access( *, @@ -168,18 +179,23 @@ def has_access( ) @classmethod - def can_trigger_autofix(cls, *, group: Group) -> bool: + def can_trigger_autofix( + cls, + *, + group: Group, + stopping_point: AutofixStoppingPoint = AutofixStoppingPoint.ROOT_CAUSE, + ) -> bool: """ - Checks if a group is permitted to trigger autofix. + Checks if a group is permitted to trigger the autofix step that stopping_point starts. Validates Seer access for the organization, the issue category, and that the - organization can pay for the root cause step. + organization can pay for that step. """ if not has_seer_access(group.organization) or not is_issue_category_eligible(group): return False try: check_autofix_billing( group.organization, - SeerOperation.AUTOFIX_ROOT_CAUSE, + AUTOFIX_STOPPING_POINT_TO_SEER_OPERATION[stopping_point], is_new_run=True, # Free cohort orgs have no subscription, so the quota check always fails for them. skip_quota_for_free_cohort=True, @@ -188,6 +204,36 @@ def can_trigger_autofix(cls, *, group: Group) -> bool: return False return True + @classmethod + def can_trigger_next_step(cls, *, update: "SeerAutofixUpdate") -> bool: + """ + Whether the org can run the Seer step that follows the update. True when there is no + next Seer step to run, so callers only hide the next-step action when it can't run. + """ + # Imported here to avoid a cycle: the templates import issue_summary, which imports this module. + from sentry.notifications.platform.templates.seer import SeerAutofixTrigger + + # At root cause with a handoff target, the next action hands the run to an external + # coding agent, not a Seer step. + if update.handoff_target and update.current_point == AutofixStoppingPoint.ROOT_CAUSE: + return True + if not update.has_next_trigger: + return True + next_point = SeerAutofixTrigger.from_update(update).stopping_point + try: + group = Group.objects.get_from_cache(id=update.group_id) + except Group.DoesNotExist: + logger.warning( + "seer.operator.next_step_check.group_missing", + extra={ + "organization_id": update.organization_id, + "run_id": update.run_id, + "group_id": update.group_id, + }, + ) + return False + return cls.can_trigger_autofix(group=group, stopping_point=next_point) + def trigger_autofix( self, *, diff --git a/src/sentry/seer/entrypoints/slack/entrypoint.py b/src/sentry/seer/entrypoints/slack/entrypoint.py index c5a42b635cf6..c5b9f45608cb 100644 --- a/src/sentry/seer/entrypoints/slack/entrypoint.py +++ b/src/sentry/seer/entrypoints/slack/entrypoint.py @@ -24,6 +24,7 @@ SeerAgentResponse, SeerAgentWriteApproval, SeerAutofixError, + SeerAutofixOutOfCredits, SeerAutofixStopped, SeerAutofixUpdate, SeerInvestigationError, @@ -45,6 +46,7 @@ SeerRunState, ) from sentry.seer.autofix.utils import AutofixStoppingPoint, CodingAgentProviderType +from sentry.seer.credits import OUT_OF_CREDITS from sentry.seer.endpoints.agent_request import ( AgentApprovalRequestData, AgentApprovalRequestSerializer, @@ -57,6 +59,7 @@ SlackEntrypointEventLifecycleMetric, SlackEntrypointInteractionType, ) +from sentry.seer.entrypoints.operator import SeerAutofixOperator from sentry.seer.entrypoints.registry import ( agent_entrypoint_registry, autofix_entrypoint_registry, @@ -464,7 +467,7 @@ def on_autofix_update( match event_type: case SentryAppEventType.SEER_ERRORED | SentryAppEventType.SEER_STOPPED: stopped = event_type == SentryAppEventType.SEER_STOPPED - message: SeerAutofixError | SeerAutofixStopped + message: SeerAutofixError | SeerAutofixOutOfCredits | SeerAutofixStopped if stopped: from sentry.integrations.slack.utils.escape import escape_slack_text @@ -479,6 +482,10 @@ def on_autofix_update( organization_id=cache_payload["organization_id"], error_message=AUTOFIX_RUN_FAILED_MESSAGE, ) + if event_payload.get("failure_reason") == OUT_OF_CREDITS: + message = SeerAutofixOutOfCredits( + organization_id=cache_payload["organization_id"], started=True + ) # Posted to the whole thread: the run may have been started by anyone. schedule_all_thread_updates( threads=cache_payload["threads"], @@ -606,6 +613,8 @@ def on_autofix_update( return data = SeerAutofixUpdate(**data_kwargs) + if not SeerAutofixOperator.can_trigger_next_step(update=data): + data = data.copy(update={"has_credits_for_next_step": False}) schedule_all_thread_updates( threads=cache_payload["threads"], integration_id=cache_payload["integration_id"], diff --git a/tests/sentry/seer/autofix/test_autofix_on_completion_hook.py b/tests/sentry/seer/autofix/test_autofix_on_completion_hook.py index db0e107f0ea2..54041a5f82f8 100644 --- a/tests/sentry/seer/autofix/test_autofix_on_completion_hook.py +++ b/tests/sentry/seer/autofix/test_autofix_on_completion_hook.py @@ -1990,6 +1990,39 @@ def test_failed_pr_creation_notifies_entrypoints_only( } ) + @patch("sentry.seer.autofix.on_completion_hook.process_autofix_updates.apply_async") + @patch("sentry.seer.autofix.on_completion_hook.broadcast_webhooks_for_organization.delay") + def test_pr_creation_out_of_credits_notifies_entrypoints( + self, mock_broadcast: MagicMock, mock_process: MagicMock + ) -> None: + state = run_state(blocks=[code_changes_memory_block()]) + state.repo_pr_states = { + "test-repo": RepoPRState( + repo_name="test-repo", + pr_creation_status="error", + pr_creation_error_code="out_of_credits", + ) + } + + with patch( + "sentry.seer.autofix.on_completion_hook.SeerAutofixOperator.has_access", + return_value=True, + ): + AutofixOnCompletionHook._send_step_webhook(self.organization, 123, state, self.group) + + mock_broadcast.assert_not_called() + mock_process.assert_called_once_with( + kwargs={ + "event_type": SentryAppEventType.SEER_ERRORED, + "event_payload": { + "run_id": 123, + "group_id": self.group.id, + "failure_reason": "out_of_credits", + }, + "organization_id": self.organization.id, + } + ) + @patch("sentry.seer.autofix.on_completion_hook.emit_pr_ready_for_review") @patch("sentry.seer.autofix.on_completion_hook.broadcast_webhooks_for_organization.delay") def test_pr_is_ready_on_open(self, mock_broadcast: MagicMock, mock_emit: MagicMock) -> None: diff --git a/tests/sentry/seer/entrypoints/slack/test_slack.py b/tests/sentry/seer/entrypoints/slack/test_slack.py index 6d49ebe4c874..6e8037b87fb7 100644 --- a/tests/sentry/seer/entrypoints/slack/test_slack.py +++ b/tests/sentry/seer/entrypoints/slack/test_slack.py @@ -26,6 +26,7 @@ from sentry.notifications.platform.service import serialize_notification_data from sentry.notifications.platform.slack.provider import SlackRenderable from sentry.notifications.platform.templates.seer import ( + AUTOFIX_OUT_OF_CREDITS_MESSAGE, SeerAgentError, SeerAgentResponse, SeerAgentWriteApproval, @@ -248,6 +249,89 @@ def test_on_autofix_update(self, mock_schedule_all_thread_updates: MagicMock) -> data=ANY, ) + def _send_autofix_update( + self, event_type: SentryAppEventType, cache_payload: SlackAutofixCachePayload + ) -> SeerAutofixUpdate: + with patch( + "sentry.seer.entrypoints.slack.entrypoint.schedule_all_thread_updates" + ) as mock_schedule_all_thread_updates: + SlackAutofixEntrypoint.on_autofix_update( + event_type=event_type, + event_payload=MOCK_SEER_WEBHOOKS[event_type], + cache_payload=cache_payload, + ) + mock_schedule_all_thread_updates.assert_called_once() + return mock_schedule_all_thread_updates.call_args.kwargs["data"] + + @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=False) + def test_on_autofix_update_without_credits_hides_next_step( + self, mock_can_run: MagicMock + ) -> None: + cache_payload = self._get_entrypoint().create_autofix_cache_payload() + # The check runs once per update, not once per thread. + cache_payload["threads"] = [ + self.thread, + SlackThreadDetails(channel_id="C0OTHER", thread_ts="1234567890.999999"), + ] + expected_next_points = { + SentryAppEventType.SEER_ROOT_CAUSE_COMPLETED: AutofixStoppingPoint.SOLUTION, + SentryAppEventType.SEER_SOLUTION_COMPLETED: AutofixStoppingPoint.CODE_CHANGES, + SentryAppEventType.SEER_CODING_COMPLETED: AutofixStoppingPoint.OPEN_PR, + } + for event_type, next_point in expected_next_points.items(): + mock_can_run.reset_mock() + data = self._send_autofix_update(event_type, cache_payload) + mock_can_run.assert_called_once_with(group=self.group, stopping_point=next_point) + assert data.has_credits_for_next_step is False + + @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=True) + def test_on_autofix_update_with_credits_keeps_next_step(self, mock_can_run: MagicMock) -> None: + cache_payload = self._get_entrypoint().create_autofix_cache_payload() + data = self._send_autofix_update( + SentryAppEventType.SEER_ROOT_CAUSE_COMPLETED, cache_payload + ) + mock_can_run.assert_called_once_with( + group=self.group, stopping_point=AutofixStoppingPoint.SOLUTION + ) + assert data.has_credits_for_next_step is True + + @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=False) + def test_on_autofix_update_skips_credit_check_without_next_step( + self, mock_can_run: MagicMock + ) -> None: + cache_payload = self._get_entrypoint().create_autofix_cache_payload() + data = self._send_autofix_update(SentryAppEventType.SEER_PR_CREATED, cache_payload) + mock_can_run.assert_not_called() + assert data.has_credits_for_next_step is True + + @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=False) + def test_on_autofix_update_skips_credit_check_for_handoff( + self, mock_can_run: MagicMock + ) -> None: + cache_payload = self._get_entrypoint().create_autofix_cache_payload() + cache_payload["handoff_target"] = CodingAgentProviderType.CURSOR_BACKGROUND_AGENT + data = self._send_autofix_update( + SentryAppEventType.SEER_ROOT_CAUSE_COMPLETED, cache_payload + ) + mock_can_run.assert_not_called() + assert data.has_credits_for_next_step is True + + @patch("sentry.seer_credit.backend.check_seer_credits") + @patch("sentry.quotas.backend.check_seer_quota", return_value=True) + @patch("sentry.seer.entrypoints.operator.has_seer_access", return_value=True) + def test_on_autofix_update_skips_billing_when_flag_off( + self, + mock_has_seer_access: MagicMock, + mock_quota: MagicMock, + mock_check_seer_credits: MagicMock, + ) -> None: + cache_payload = self._get_entrypoint().create_autofix_cache_payload() + data = self._send_autofix_update( + SentryAppEventType.SEER_ROOT_CAUSE_COMPLETED, cache_payload + ) + mock_check_seer_credits.assert_not_called() + assert data.has_credits_for_next_step is True + @patch.object(SeerAutofixOperator, "has_access", return_value=True) @patch( "sentry.integrations.slack.integration.SlackIntegration.send_threaded_message", @@ -351,6 +435,37 @@ def test_stopped_run_posts_the_reason_and_suggestion( assert "The API key expired." in str(renderable["blocks"]) assert "Rotate the key." in str(renderable["blocks"]) + @patch.object(SeerAutofixOperator, "has_access", return_value=True) + @patch( + "sentry.integrations.slack.integration.SlackIntegration.send_threaded_message", + return_value={"ok": True, "ts": "1234567890.000100"}, + ) + def test_out_of_credits_run_posts_out_of_credits_message( + self, + mock_send_threaded_message: MagicMock, + _mock_has_access: MagicMock, + ) -> None: + SeerOperatorAutofixCache.populate_post_autofix_cache( + entrypoint_key=SeerEntrypointKey.SLACK, + run_id=MOCK_RUN_ID, + cache_payload=self._get_entrypoint().create_autofix_cache_payload(), + ) + + with self.tasks(): + process_autofix_updates( + event_type=SentryAppEventType.SEER_ERRORED, + event_payload={ + "run_id": MOCK_RUN_ID, + "group_id": self.group.id, + "failure_reason": "out_of_credits", + }, + organization_id=self.organization.id, + ) + + blocks = str(mock_send_threaded_message.call_args.kwargs["renderable"]["blocks"]) + assert AUTOFIX_OUT_OF_CREDITS_MESSAGE in blocks + assert "couldn't finish working on this issue" not in blocks + @patch("sentry.integrations.slack.integration.SlackIntegration.send_threaded_ephemeral_message") @patch( "sentry.integrations.slack.integration.SlackIntegration.send_threaded_message", diff --git a/tests/sentry/seer/entrypoints/test_operator.py b/tests/sentry/seer/entrypoints/test_operator.py index ab228312e865..707472b432ff 100644 --- a/tests/sentry/seer/entrypoints/test_operator.py +++ b/tests/sentry/seer/entrypoints/test_operator.py @@ -22,6 +22,7 @@ from sentry.locks import locks from sentry.models.activity import Activity from sentry.models.organization import Organization +from sentry.notifications.platform.templates.seer import SeerAutofixUpdate from sentry.organizations.services.organization.model import RpcOrganization from sentry.seer.agent.client_models import ( CodingAgentState, @@ -37,8 +38,10 @@ AutofixStoppingPoint, CodingAgentProviderType, ) +from sentry.seer.credits import SeerOperation from sentry.seer.entrypoints.cache import SeerOperatorInvestigationCache from sentry.seer.entrypoints.operator import ( + AUTOFIX_STOPPING_POINT_TO_SEER_OPERATION, SEER_EVENT_TO_ACTIVITY_TYPE, SeerAgentOperator, SeerAutofixOperator, @@ -264,6 +267,15 @@ def open_pr() -> None: "scm_login": "octocat", } + def test_stopping_point_to_seer_operation_covers_every_stopping_point(self) -> None: + assert set(AUTOFIX_STOPPING_POINT_TO_SEER_OPERATION) == set(AutofixStoppingPoint) + assert AUTOFIX_STOPPING_POINT_TO_SEER_OPERATION == { + AutofixStoppingPoint.ROOT_CAUSE: SeerOperation.AUTOFIX_ROOT_CAUSE, + AutofixStoppingPoint.SOLUTION: SeerOperation.AUTOFIX_SOLUTION, + AutofixStoppingPoint.CODE_CHANGES: SeerOperation.AUTOFIX_CODE_CHANGES, + AutofixStoppingPoint.OPEN_PR: SeerOperation.AUTOFIX_OPEN_PR, + } + @patch("sentry.seer.autofix.autofix_agent.trigger_coding_agent_handoff") def test_trigger_handoff_no_config_is_silent_halt( self, mock_trigger_handoff_helper: MagicMock @@ -636,6 +648,82 @@ def test_can_trigger_autofix_returns_false_without_root_cause_credits( assert SeerAutofixOperator.can_trigger_autofix(group=self.group) is False mock_check_seer_credits.assert_called_once() + @with_feature(["organizations:seer-credit-checks", "organizations:token-billed-seer-enabled"]) + @patch("sentry.seer.entrypoints.operator.can_run_seer_operation", return_value=False) + @patch("sentry.quotas.backend.check_seer_quota", return_value=True) + def test_can_trigger_autofix_checks_credits_for_the_given_step( + self, mock_quota: MagicMock, mock_can_run: MagicMock + ) -> None: + for stopping_point, operation in AUTOFIX_STOPPING_POINT_TO_SEER_OPERATION.items(): + mock_can_run.reset_mock() + assert ( + SeerAutofixOperator.can_trigger_autofix( + group=self.group, stopping_point=stopping_point + ) + is False + ) + mock_can_run.assert_called_once_with(self.organization, operation) + + def _autofix_update( + self, + current_point: AutofixStoppingPoint, + handoff_target: CodingAgentProviderType | None = None, + ) -> SeerAutofixUpdate: + return SeerAutofixUpdate( + run_id=MOCK_RUN_ID, + organization_id=self.organization.id, + project_id=self.project.id, + group_id=self.group.id, + current_point=current_point, + group_link=self.group.get_absolute_url(), + handoff_target=handoff_target, + ) + + @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=False) + def test_can_trigger_next_step_checks_the_next_stopping_point( + self, mock_can_trigger: MagicMock + ) -> None: + expected_next_points = { + AutofixStoppingPoint.ROOT_CAUSE: AutofixStoppingPoint.SOLUTION, + AutofixStoppingPoint.SOLUTION: AutofixStoppingPoint.CODE_CHANGES, + AutofixStoppingPoint.CODE_CHANGES: AutofixStoppingPoint.OPEN_PR, + } + for current_point, next_point in expected_next_points.items(): + mock_can_trigger.reset_mock() + update = self._autofix_update(current_point) + assert SeerAutofixOperator.can_trigger_next_step(update=update) is False + mock_can_trigger.assert_called_once_with(group=self.group, stopping_point=next_point) + + @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=False) + def test_can_trigger_next_step_true_without_next_step( + self, mock_can_trigger: MagicMock + ) -> None: + update = self._autofix_update(AutofixStoppingPoint.OPEN_PR) + assert SeerAutofixOperator.can_trigger_next_step(update=update) is True + mock_can_trigger.assert_not_called() + + @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=False) + def test_can_trigger_next_step_true_for_handoff(self, mock_can_trigger: MagicMock) -> None: + update = self._autofix_update( + AutofixStoppingPoint.ROOT_CAUSE, + handoff_target=CodingAgentProviderType.CURSOR_BACKGROUND_AGENT, + ) + assert SeerAutofixOperator.can_trigger_next_step(update=update) is True + mock_can_trigger.assert_not_called() + + @patch("sentry.seer.entrypoints.operator.logger") + @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=True) + def test_can_trigger_next_step_false_when_group_missing( + self, mock_can_trigger: MagicMock, mock_logger: MagicMock + ) -> None: + update = self._autofix_update(AutofixStoppingPoint.ROOT_CAUSE).copy(update={"group_id": 0}) + assert SeerAutofixOperator.can_trigger_next_step(update=update) is False + mock_can_trigger.assert_not_called() + mock_logger.warning.assert_called_once_with( + "seer.operator.next_step_check.group_missing", + extra={"organization_id": self.organization.id, "run_id": MOCK_RUN_ID, "group_id": 0}, + ) + @patch("sentry.seer_credit.backend.check_seer_credits") @patch("sentry.quotas.backend.check_seer_quota", return_value=True) def test_can_trigger_autofix_skips_credit_check_when_flag_off( From 298c4ea2bb8fe005830feb66c8bfba6f40a266a0 Mon Sep 17 00:00:00 2001 From: Alex Sohn Date: Mon, 9 Nov 2026 15:57:57 -0500 Subject: [PATCH 2/8] ref(seer): Drop the import comment in can_trigger_next_step Co-Authored-By: Claude Opus 5.5 --- src/sentry/seer/entrypoints/operator.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sentry/seer/entrypoints/operator.py b/src/sentry/seer/entrypoints/operator.py index 7e843926f217..192f4b4d14a9 100644 --- a/src/sentry/seer/entrypoints/operator.py +++ b/src/sentry/seer/entrypoints/operator.py @@ -210,7 +210,6 @@ def can_trigger_next_step(cls, *, update: "SeerAutofixUpdate") -> bool: Whether the org can run the Seer step that follows the update. True when there is no next Seer step to run, so callers only hide the next-step action when it can't run. """ - # Imported here to avoid a cycle: the templates import issue_summary, which imports this module. from sentry.notifications.platform.templates.seer import SeerAutofixTrigger # At root cause with a handoff target, the next action hands the run to an external From 10d4ab52ebee4994ebb8fed45e6a58782f92053d Mon Sep 17 00:00:00 2001 From: Alex Sohn Date: Tue, 10 Nov 2026 13:03:09 -0500 Subject: [PATCH 3/8] ref(seer): Stop checking credits for the next autofix step The Slack autofix update no longer hides the next-step button when the org has no Seer credits for that step, so drop can_trigger_next_step, the stopping point to Seer operation mapping, and the stopping_point argument on can_trigger_autofix. A step that can't run fails in Seer and the thread gets the out-of-credits reply. Co-Authored-By: Claude Fable 5.2 --- src/sentry/seer/entrypoints/operator.py | 55 ++---------- .../seer/entrypoints/slack/entrypoint.py | 3 - .../seer/entrypoints/slack/test_slack.py | 83 ----------------- .../sentry/seer/entrypoints/test_operator.py | 88 ------------------- 4 files changed, 5 insertions(+), 224 deletions(-) diff --git a/src/sentry/seer/entrypoints/operator.py b/src/sentry/seer/entrypoints/operator.py index 192f4b4d14a9..727dc5503643 100644 --- a/src/sentry/seer/entrypoints/operator.py +++ b/src/sentry/seer/entrypoints/operator.py @@ -1,5 +1,5 @@ import logging -from typing import TYPE_CHECKING, Any, NotRequired, TypedDict +from typing import Any, NotRequired, TypedDict from taskbroker_client.retry import Retry @@ -71,9 +71,6 @@ from sentry.utils.safe import get_path, safe_execute from sentry.viewer_context import ActorId, ActorType -if TYPE_CHECKING: - from sentry.notifications.platform.templates.seer import SeerAutofixUpdate - SEER_EVENT_TO_ACTIVITY_TYPE: dict[SentryAppEventType, ActivityType] = { SentryAppEventType.SEER_ROOT_CAUSE_STARTED: ActivityType.SEER_RCA_STARTED, SentryAppEventType.SEER_ROOT_CAUSE_COMPLETED: ActivityType.SEER_RCA_COMPLETED, @@ -117,14 +114,6 @@ class SeerActivityAttribution(TypedDict): AGENT_REFRESH_TIMEOUT_SECONDS = 60 -# The Seer operation (and so the credit check) for the autofix step a stopping point starts. -AUTOFIX_STOPPING_POINT_TO_SEER_OPERATION: dict[AutofixStoppingPoint, SeerOperation] = { - AutofixStoppingPoint.ROOT_CAUSE: SeerOperation.AUTOFIX_ROOT_CAUSE, - AutofixStoppingPoint.SOLUTION: SeerOperation.AUTOFIX_SOLUTION, - AutofixStoppingPoint.CODE_CHANGES: SeerOperation.AUTOFIX_CODE_CHANGES, - AutofixStoppingPoint.OPEN_PR: SeerOperation.AUTOFIX_OPEN_PR, -} - def has_seer_autofix_entrypoint_access( *, @@ -179,23 +168,18 @@ def has_access( ) @classmethod - def can_trigger_autofix( - cls, - *, - group: Group, - stopping_point: AutofixStoppingPoint = AutofixStoppingPoint.ROOT_CAUSE, - ) -> bool: + def can_trigger_autofix(cls, *, group: Group) -> bool: """ - Checks if a group is permitted to trigger the autofix step that stopping_point starts. + Checks if a group is permitted to trigger autofix. Validates Seer access for the organization, the issue category, and that the - organization can pay for that step. + organization can pay for the root cause step. """ if not has_seer_access(group.organization) or not is_issue_category_eligible(group): return False try: check_autofix_billing( group.organization, - AUTOFIX_STOPPING_POINT_TO_SEER_OPERATION[stopping_point], + SeerOperation.AUTOFIX_ROOT_CAUSE, is_new_run=True, # Free cohort orgs have no subscription, so the quota check always fails for them. skip_quota_for_free_cohort=True, @@ -204,35 +188,6 @@ def can_trigger_autofix( return False return True - @classmethod - def can_trigger_next_step(cls, *, update: "SeerAutofixUpdate") -> bool: - """ - Whether the org can run the Seer step that follows the update. True when there is no - next Seer step to run, so callers only hide the next-step action when it can't run. - """ - from sentry.notifications.platform.templates.seer import SeerAutofixTrigger - - # At root cause with a handoff target, the next action hands the run to an external - # coding agent, not a Seer step. - if update.handoff_target and update.current_point == AutofixStoppingPoint.ROOT_CAUSE: - return True - if not update.has_next_trigger: - return True - next_point = SeerAutofixTrigger.from_update(update).stopping_point - try: - group = Group.objects.get_from_cache(id=update.group_id) - except Group.DoesNotExist: - logger.warning( - "seer.operator.next_step_check.group_missing", - extra={ - "organization_id": update.organization_id, - "run_id": update.run_id, - "group_id": update.group_id, - }, - ) - return False - return cls.can_trigger_autofix(group=group, stopping_point=next_point) - def trigger_autofix( self, *, diff --git a/src/sentry/seer/entrypoints/slack/entrypoint.py b/src/sentry/seer/entrypoints/slack/entrypoint.py index c5b9f45608cb..fc57b07866e1 100644 --- a/src/sentry/seer/entrypoints/slack/entrypoint.py +++ b/src/sentry/seer/entrypoints/slack/entrypoint.py @@ -59,7 +59,6 @@ SlackEntrypointEventLifecycleMetric, SlackEntrypointInteractionType, ) -from sentry.seer.entrypoints.operator import SeerAutofixOperator from sentry.seer.entrypoints.registry import ( agent_entrypoint_registry, autofix_entrypoint_registry, @@ -613,8 +612,6 @@ def on_autofix_update( return data = SeerAutofixUpdate(**data_kwargs) - if not SeerAutofixOperator.can_trigger_next_step(update=data): - data = data.copy(update={"has_credits_for_next_step": False}) schedule_all_thread_updates( threads=cache_payload["threads"], integration_id=cache_payload["integration_id"], diff --git a/tests/sentry/seer/entrypoints/slack/test_slack.py b/tests/sentry/seer/entrypoints/slack/test_slack.py index 6e8037b87fb7..a5d563e96e1d 100644 --- a/tests/sentry/seer/entrypoints/slack/test_slack.py +++ b/tests/sentry/seer/entrypoints/slack/test_slack.py @@ -249,89 +249,6 @@ def test_on_autofix_update(self, mock_schedule_all_thread_updates: MagicMock) -> data=ANY, ) - def _send_autofix_update( - self, event_type: SentryAppEventType, cache_payload: SlackAutofixCachePayload - ) -> SeerAutofixUpdate: - with patch( - "sentry.seer.entrypoints.slack.entrypoint.schedule_all_thread_updates" - ) as mock_schedule_all_thread_updates: - SlackAutofixEntrypoint.on_autofix_update( - event_type=event_type, - event_payload=MOCK_SEER_WEBHOOKS[event_type], - cache_payload=cache_payload, - ) - mock_schedule_all_thread_updates.assert_called_once() - return mock_schedule_all_thread_updates.call_args.kwargs["data"] - - @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=False) - def test_on_autofix_update_without_credits_hides_next_step( - self, mock_can_run: MagicMock - ) -> None: - cache_payload = self._get_entrypoint().create_autofix_cache_payload() - # The check runs once per update, not once per thread. - cache_payload["threads"] = [ - self.thread, - SlackThreadDetails(channel_id="C0OTHER", thread_ts="1234567890.999999"), - ] - expected_next_points = { - SentryAppEventType.SEER_ROOT_CAUSE_COMPLETED: AutofixStoppingPoint.SOLUTION, - SentryAppEventType.SEER_SOLUTION_COMPLETED: AutofixStoppingPoint.CODE_CHANGES, - SentryAppEventType.SEER_CODING_COMPLETED: AutofixStoppingPoint.OPEN_PR, - } - for event_type, next_point in expected_next_points.items(): - mock_can_run.reset_mock() - data = self._send_autofix_update(event_type, cache_payload) - mock_can_run.assert_called_once_with(group=self.group, stopping_point=next_point) - assert data.has_credits_for_next_step is False - - @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=True) - def test_on_autofix_update_with_credits_keeps_next_step(self, mock_can_run: MagicMock) -> None: - cache_payload = self._get_entrypoint().create_autofix_cache_payload() - data = self._send_autofix_update( - SentryAppEventType.SEER_ROOT_CAUSE_COMPLETED, cache_payload - ) - mock_can_run.assert_called_once_with( - group=self.group, stopping_point=AutofixStoppingPoint.SOLUTION - ) - assert data.has_credits_for_next_step is True - - @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=False) - def test_on_autofix_update_skips_credit_check_without_next_step( - self, mock_can_run: MagicMock - ) -> None: - cache_payload = self._get_entrypoint().create_autofix_cache_payload() - data = self._send_autofix_update(SentryAppEventType.SEER_PR_CREATED, cache_payload) - mock_can_run.assert_not_called() - assert data.has_credits_for_next_step is True - - @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=False) - def test_on_autofix_update_skips_credit_check_for_handoff( - self, mock_can_run: MagicMock - ) -> None: - cache_payload = self._get_entrypoint().create_autofix_cache_payload() - cache_payload["handoff_target"] = CodingAgentProviderType.CURSOR_BACKGROUND_AGENT - data = self._send_autofix_update( - SentryAppEventType.SEER_ROOT_CAUSE_COMPLETED, cache_payload - ) - mock_can_run.assert_not_called() - assert data.has_credits_for_next_step is True - - @patch("sentry.seer_credit.backend.check_seer_credits") - @patch("sentry.quotas.backend.check_seer_quota", return_value=True) - @patch("sentry.seer.entrypoints.operator.has_seer_access", return_value=True) - def test_on_autofix_update_skips_billing_when_flag_off( - self, - mock_has_seer_access: MagicMock, - mock_quota: MagicMock, - mock_check_seer_credits: MagicMock, - ) -> None: - cache_payload = self._get_entrypoint().create_autofix_cache_payload() - data = self._send_autofix_update( - SentryAppEventType.SEER_ROOT_CAUSE_COMPLETED, cache_payload - ) - mock_check_seer_credits.assert_not_called() - assert data.has_credits_for_next_step is True - @patch.object(SeerAutofixOperator, "has_access", return_value=True) @patch( "sentry.integrations.slack.integration.SlackIntegration.send_threaded_message", diff --git a/tests/sentry/seer/entrypoints/test_operator.py b/tests/sentry/seer/entrypoints/test_operator.py index 707472b432ff..ab228312e865 100644 --- a/tests/sentry/seer/entrypoints/test_operator.py +++ b/tests/sentry/seer/entrypoints/test_operator.py @@ -22,7 +22,6 @@ from sentry.locks import locks from sentry.models.activity import Activity from sentry.models.organization import Organization -from sentry.notifications.platform.templates.seer import SeerAutofixUpdate from sentry.organizations.services.organization.model import RpcOrganization from sentry.seer.agent.client_models import ( CodingAgentState, @@ -38,10 +37,8 @@ AutofixStoppingPoint, CodingAgentProviderType, ) -from sentry.seer.credits import SeerOperation from sentry.seer.entrypoints.cache import SeerOperatorInvestigationCache from sentry.seer.entrypoints.operator import ( - AUTOFIX_STOPPING_POINT_TO_SEER_OPERATION, SEER_EVENT_TO_ACTIVITY_TYPE, SeerAgentOperator, SeerAutofixOperator, @@ -267,15 +264,6 @@ def open_pr() -> None: "scm_login": "octocat", } - def test_stopping_point_to_seer_operation_covers_every_stopping_point(self) -> None: - assert set(AUTOFIX_STOPPING_POINT_TO_SEER_OPERATION) == set(AutofixStoppingPoint) - assert AUTOFIX_STOPPING_POINT_TO_SEER_OPERATION == { - AutofixStoppingPoint.ROOT_CAUSE: SeerOperation.AUTOFIX_ROOT_CAUSE, - AutofixStoppingPoint.SOLUTION: SeerOperation.AUTOFIX_SOLUTION, - AutofixStoppingPoint.CODE_CHANGES: SeerOperation.AUTOFIX_CODE_CHANGES, - AutofixStoppingPoint.OPEN_PR: SeerOperation.AUTOFIX_OPEN_PR, - } - @patch("sentry.seer.autofix.autofix_agent.trigger_coding_agent_handoff") def test_trigger_handoff_no_config_is_silent_halt( self, mock_trigger_handoff_helper: MagicMock @@ -648,82 +636,6 @@ def test_can_trigger_autofix_returns_false_without_root_cause_credits( assert SeerAutofixOperator.can_trigger_autofix(group=self.group) is False mock_check_seer_credits.assert_called_once() - @with_feature(["organizations:seer-credit-checks", "organizations:token-billed-seer-enabled"]) - @patch("sentry.seer.entrypoints.operator.can_run_seer_operation", return_value=False) - @patch("sentry.quotas.backend.check_seer_quota", return_value=True) - def test_can_trigger_autofix_checks_credits_for_the_given_step( - self, mock_quota: MagicMock, mock_can_run: MagicMock - ) -> None: - for stopping_point, operation in AUTOFIX_STOPPING_POINT_TO_SEER_OPERATION.items(): - mock_can_run.reset_mock() - assert ( - SeerAutofixOperator.can_trigger_autofix( - group=self.group, stopping_point=stopping_point - ) - is False - ) - mock_can_run.assert_called_once_with(self.organization, operation) - - def _autofix_update( - self, - current_point: AutofixStoppingPoint, - handoff_target: CodingAgentProviderType | None = None, - ) -> SeerAutofixUpdate: - return SeerAutofixUpdate( - run_id=MOCK_RUN_ID, - organization_id=self.organization.id, - project_id=self.project.id, - group_id=self.group.id, - current_point=current_point, - group_link=self.group.get_absolute_url(), - handoff_target=handoff_target, - ) - - @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=False) - def test_can_trigger_next_step_checks_the_next_stopping_point( - self, mock_can_trigger: MagicMock - ) -> None: - expected_next_points = { - AutofixStoppingPoint.ROOT_CAUSE: AutofixStoppingPoint.SOLUTION, - AutofixStoppingPoint.SOLUTION: AutofixStoppingPoint.CODE_CHANGES, - AutofixStoppingPoint.CODE_CHANGES: AutofixStoppingPoint.OPEN_PR, - } - for current_point, next_point in expected_next_points.items(): - mock_can_trigger.reset_mock() - update = self._autofix_update(current_point) - assert SeerAutofixOperator.can_trigger_next_step(update=update) is False - mock_can_trigger.assert_called_once_with(group=self.group, stopping_point=next_point) - - @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=False) - def test_can_trigger_next_step_true_without_next_step( - self, mock_can_trigger: MagicMock - ) -> None: - update = self._autofix_update(AutofixStoppingPoint.OPEN_PR) - assert SeerAutofixOperator.can_trigger_next_step(update=update) is True - mock_can_trigger.assert_not_called() - - @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=False) - def test_can_trigger_next_step_true_for_handoff(self, mock_can_trigger: MagicMock) -> None: - update = self._autofix_update( - AutofixStoppingPoint.ROOT_CAUSE, - handoff_target=CodingAgentProviderType.CURSOR_BACKGROUND_AGENT, - ) - assert SeerAutofixOperator.can_trigger_next_step(update=update) is True - mock_can_trigger.assert_not_called() - - @patch("sentry.seer.entrypoints.operator.logger") - @patch.object(SeerAutofixOperator, "can_trigger_autofix", return_value=True) - def test_can_trigger_next_step_false_when_group_missing( - self, mock_can_trigger: MagicMock, mock_logger: MagicMock - ) -> None: - update = self._autofix_update(AutofixStoppingPoint.ROOT_CAUSE).copy(update={"group_id": 0}) - assert SeerAutofixOperator.can_trigger_next_step(update=update) is False - mock_can_trigger.assert_not_called() - mock_logger.warning.assert_called_once_with( - "seer.operator.next_step_check.group_missing", - extra={"organization_id": self.organization.id, "run_id": MOCK_RUN_ID, "group_id": 0}, - ) - @patch("sentry.seer_credit.backend.check_seer_credits") @patch("sentry.quotas.backend.check_seer_quota", return_value=True) def test_can_trigger_autofix_skips_credit_check_when_flag_off( From 16150678eb687f265cbc3645db5f61fa6b340ee8 Mon Sep 17 00:00:00 2001 From: Alex Sohn Date: Tue, 10 Nov 2026 14:26:03 -0500 Subject: [PATCH 4/8] ref(seer): Stop passing started to the out-of-credits notification Co-Authored-By: Claude Fable 5.2 --- src/sentry/seer/entrypoints/slack/entrypoint.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/seer/entrypoints/slack/entrypoint.py b/src/sentry/seer/entrypoints/slack/entrypoint.py index fc57b07866e1..3d0d82de4e1f 100644 --- a/src/sentry/seer/entrypoints/slack/entrypoint.py +++ b/src/sentry/seer/entrypoints/slack/entrypoint.py @@ -483,7 +483,7 @@ def on_autofix_update( ) if event_payload.get("failure_reason") == OUT_OF_CREDITS: message = SeerAutofixOutOfCredits( - organization_id=cache_payload["organization_id"], started=True + organization_id=cache_payload["organization_id"] ) # Posted to the whole thread: the run may have been started by anyone. schedule_all_thread_updates( From 16be33a8811cdcad4e41f605a448b6ba00c9bc33 Mon Sep 17 00:00:00 2001 From: Alex Sohn Date: Tue, 10 Nov 2026 14:29:25 -0500 Subject: [PATCH 5/8] ref(seer): Pick the autofix failure reply with an if/else Co-Authored-By: Claude Fable 5.2 --- src/sentry/seer/entrypoints/slack/entrypoint.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sentry/seer/entrypoints/slack/entrypoint.py b/src/sentry/seer/entrypoints/slack/entrypoint.py index 3d0d82de4e1f..f70f01988312 100644 --- a/src/sentry/seer/entrypoints/slack/entrypoint.py +++ b/src/sentry/seer/entrypoints/slack/entrypoint.py @@ -476,15 +476,15 @@ def on_autofix_update( reason=escape_slack_text(stop["reason"]), suggestion=escape_slack_text(stop["suggestion"]), ) + elif event_payload.get("failure_reason") == OUT_OF_CREDITS: + message = SeerAutofixOutOfCredits( + organization_id=cache_payload["organization_id"] + ) else: message = SeerAutofixError( organization_id=cache_payload["organization_id"], error_message=AUTOFIX_RUN_FAILED_MESSAGE, ) - if event_payload.get("failure_reason") == OUT_OF_CREDITS: - message = SeerAutofixOutOfCredits( - organization_id=cache_payload["organization_id"] - ) # Posted to the whole thread: the run may have been started by anyone. schedule_all_thread_updates( threads=cache_payload["threads"], From df71ccc6ce752b2dffeb0a7f37c1d5b4972da76b Mon Sep 17 00:00:00 2001 From: Alex Sohn Date: Thu, 12 Nov 2026 16:35:37 -0500 Subject: [PATCH 6/8] test(slack): Expect the member copy in the autofix out-of-credits reply The out-of-credits notification now has a copy per reader. The thread reply doesn't know who is reading, so it uses the member copy. Co-Authored-By: Claude Fable 5.2 --- tests/sentry/seer/entrypoints/slack/test_slack.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/sentry/seer/entrypoints/slack/test_slack.py b/tests/sentry/seer/entrypoints/slack/test_slack.py index a5d563e96e1d..2e19cc799148 100644 --- a/tests/sentry/seer/entrypoints/slack/test_slack.py +++ b/tests/sentry/seer/entrypoints/slack/test_slack.py @@ -26,7 +26,7 @@ from sentry.notifications.platform.service import serialize_notification_data from sentry.notifications.platform.slack.provider import SlackRenderable from sentry.notifications.platform.templates.seer import ( - AUTOFIX_OUT_OF_CREDITS_MESSAGE, + AUTOFIX_OUT_OF_CREDITS_MEMBER_MESSAGE, SeerAgentError, SeerAgentResponse, SeerAgentWriteApproval, @@ -380,7 +380,7 @@ def test_out_of_credits_run_posts_out_of_credits_message( ) blocks = str(mock_send_threaded_message.call_args.kwargs["renderable"]["blocks"]) - assert AUTOFIX_OUT_OF_CREDITS_MESSAGE in blocks + assert AUTOFIX_OUT_OF_CREDITS_MEMBER_MESSAGE in blocks assert "couldn't finish working on this issue" not in blocks @patch("sentry.integrations.slack.integration.SlackIntegration.send_threaded_ephemeral_message") From 86481ddac036556c04a2a560615bcdb3968aa7b3 Mon Sep 17 00:00:00 2001 From: Alex Sohn Date: Fri, 13 Nov 2026 11:29:06 -0500 Subject: [PATCH 7/8] feat(seer): Write Autofix out-of-credits reply for whoever started the run Store the Slack user who started the run in the Autofix cache payload. When the run fails for credits, look up whether that user can manage billing and pick the copy and button for them. Runs nobody started by hand keep the member copy. Co-Authored-By: Claude Fable 5.2 --- .../seer/entrypoints/slack/entrypoint.py | 48 +++++++++++++++++- .../seer/entrypoints/slack/test_slack.py | 50 +++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/src/sentry/seer/entrypoints/slack/entrypoint.py b/src/sentry/seer/entrypoints/slack/entrypoint.py index f70f01988312..2e97926860d3 100644 --- a/src/sentry/seer/entrypoints/slack/entrypoint.py +++ b/src/sentry/seer/entrypoints/slack/entrypoint.py @@ -9,12 +9,14 @@ from sentry import features from sentry.constants import ObjectStatus +from sentry.identity.services.identity import identity_service from sentry.integrations.services.integration.service import integration_service from sentry.integrations.utils.metrics import EventLifecycle from sentry.investigations.models import InvestigationOrchestrationRun from sentry.investigations.models.orchestration import InvestigationOrchestrationStatus from sentry.locks import locks from sentry.models.organization import Organization +from sentry.models.organizationmember import InviteStatus, OrganizationMember from sentry.models.project import Project from sentry.notebooks.models import Notebook from sentry.notifications.platform.registry import provider_registry @@ -101,6 +103,41 @@ from sentry.models.group import Group +def has_billing_access(*, organization_id: int, user_id: int) -> bool: + """Whether the user's role in the organization lets them manage billing.""" + member = OrganizationMember.objects.filter( + organization_id=organization_id, + user_id=user_id, + invite_status=InviteStatus.APPROVED.value, + ).first() + return member is not None and "org:billing" in member.get_scopes() + + +def slack_user_has_billing_access( + *, integration_id: int, organization_id: int, slack_user_id: str | None +) -> bool: + """Whether the Slack user is linked to a Sentry user who can manage the organization's billing.""" + if not slack_user_id: + return False + integration = integration_service.get_integration( + integration_id=integration_id, + organization_id=organization_id, + status=ObjectStatus.ACTIVE, + ) + if not integration: + return False + identity = identity_service.get_identity( + filter={ + "provider_type": integration.provider, + "provider_ext_id": integration.external_id, + "identity_ext_id": slack_user_id, + } + ) + if not identity: + return False + return has_billing_access(organization_id=organization_id, user_id=identity.user_id) + + class EntrypointSetupError(Exception): """Raised when entrypoint construction fails during mention processing.""" @@ -122,6 +159,8 @@ class SlackAutofixCachePayload(TypedDict): # Captured at cache creation. When set, the Slack ROOT_CAUSE update swaps the # next-stage trigger button for a "Hand off to ..." button. handoff_target: CodingAgentProviderType | None + # Who started the run from Slack. Unset for runs that nobody started by hand. + slack_user_id: NotRequired[str | None] class SlackAgentCachePayload(TypedDict): @@ -441,6 +480,7 @@ def create_autofix_cache_payload(self) -> SlackAutofixCachePayload: group_id=self.group.id, group_link=self.get_group_link(self.group), handoff_target=_get_handoff_target(self.group.project), + slack_user_id=self.slack_user_id, ) @staticmethod @@ -477,8 +517,14 @@ def on_autofix_update( suggestion=escape_slack_text(stop["suggestion"]), ) elif event_payload.get("failure_reason") == OUT_OF_CREDITS: + # The copy is written for whoever started the run. message = SeerAutofixOutOfCredits( - organization_id=cache_payload["organization_id"] + organization_id=cache_payload["organization_id"], + has_billing_access=slack_user_has_billing_access( + integration_id=cache_payload["integration_id"], + organization_id=cache_payload["organization_id"], + slack_user_id=cache_payload.get("slack_user_id"), + ), ) else: message = SeerAutofixError( diff --git a/tests/sentry/seer/entrypoints/slack/test_slack.py b/tests/sentry/seer/entrypoints/slack/test_slack.py index 2e19cc799148..b92504b75015 100644 --- a/tests/sentry/seer/entrypoints/slack/test_slack.py +++ b/tests/sentry/seer/entrypoints/slack/test_slack.py @@ -26,6 +26,7 @@ from sentry.notifications.platform.service import serialize_notification_data from sentry.notifications.platform.slack.provider import SlackRenderable from sentry.notifications.platform.templates.seer import ( + AUTOFIX_OUT_OF_CREDITS_BILLING_MESSAGE, AUTOFIX_OUT_OF_CREDITS_MEMBER_MESSAGE, SeerAgentError, SeerAgentResponse, @@ -215,6 +216,7 @@ def test_create_autofix_cache_payload(self, mock_read_pref: MagicMock) -> None: assert cache_payload["group_id"] == self.group.id assert cache_payload["threads"] == [self.thread] assert cache_payload["handoff_target"] is None + assert cache_payload["slack_user_id"] == self.slack_user_id @patch("sentry.seer.autofix.utils.read_preference_from_sentry_db") def test_create_autofix_cache_payload_captures_handoff_target( @@ -383,6 +385,54 @@ def test_out_of_credits_run_posts_out_of_credits_message( assert AUTOFIX_OUT_OF_CREDITS_MEMBER_MESSAGE in blocks assert "couldn't finish working on this issue" not in blocks + def _post_out_of_credits_message_for_linked_user(self) -> str: + self.create_identity( + user=self.user, + identity_provider=self.create_identity_provider(integration=self.integration), + external_id=self.slack_user_id, + ) + SeerOperatorAutofixCache.populate_post_autofix_cache( + entrypoint_key=SeerEntrypointKey.SLACK, + run_id=MOCK_RUN_ID, + cache_payload=self._get_entrypoint().create_autofix_cache_payload(), + ) + with ( + patch.object(SeerAutofixOperator, "has_access", return_value=True), + patch( + "sentry.integrations.slack.integration.SlackIntegration.send_threaded_message", + return_value={"ok": True, "ts": "1234567890.000100"}, + ) as mock_send_threaded_message, + self.tasks(), + ): + process_autofix_updates( + event_type=SentryAppEventType.SEER_ERRORED, + event_payload={ + "run_id": MOCK_RUN_ID, + "group_id": self.group.id, + "failure_reason": "out_of_credits", + }, + organization_id=self.organization.id, + ) + return str(mock_send_threaded_message.call_args.kwargs["renderable"]["blocks"]) + + @patch( + "sentry.models.organizationmember.OrganizationMember.get_scopes", + return_value=frozenset({"org:billing"}), + ) + def test_out_of_credits_run_started_by_billing_member_posts_billing_message( + self, _mock_get_scopes: MagicMock + ) -> None: + blocks = self._post_out_of_credits_message_for_linked_user() + + assert AUTOFIX_OUT_OF_CREDITS_BILLING_MESSAGE in blocks + assert "Configure auto-reload" in blocks + + def test_out_of_credits_run_started_by_member_posts_member_message(self) -> None: + blocks = self._post_out_of_credits_message_for_linked_user() + + assert AUTOFIX_OUT_OF_CREDITS_MEMBER_MESSAGE in blocks + assert "Contact Admin" in blocks + @patch("sentry.integrations.slack.integration.SlackIntegration.send_threaded_ephemeral_message") @patch( "sentry.integrations.slack.integration.SlackIntegration.send_threaded_message", From 204cca496bf94cffaa1998a7a16f9f81b028b444 Mon Sep 17 00:00:00 2001 From: Alex Sohn Date: Fri, 13 Nov 2026 12:19:40 -0500 Subject: [PATCH 8/8] ref(seer): Reuse the user resolved at click time for the billing check The action webhook already resolves who clicked, so store that Sentry user with the run instead of the Slack user ID. Check billing access with from_user(...).has_scope instead of a hand-rolled membership query. Co-Authored-By: Claude Fable 5.2 --- .../seer/entrypoints/slack/entrypoint.py | 55 ++++++------------- .../seer/entrypoints/slack/test_slack.py | 24 ++++---- 2 files changed, 30 insertions(+), 49 deletions(-) diff --git a/src/sentry/seer/entrypoints/slack/entrypoint.py b/src/sentry/seer/entrypoints/slack/entrypoint.py index 2e97926860d3..c94b287615e9 100644 --- a/src/sentry/seer/entrypoints/slack/entrypoint.py +++ b/src/sentry/seer/entrypoints/slack/entrypoint.py @@ -8,15 +8,14 @@ import orjson from sentry import features +from sentry.auth.access import from_user from sentry.constants import ObjectStatus -from sentry.identity.services.identity import identity_service from sentry.integrations.services.integration.service import integration_service from sentry.integrations.utils.metrics import EventLifecycle from sentry.investigations.models import InvestigationOrchestrationRun from sentry.investigations.models.orchestration import InvestigationOrchestrationStatus from sentry.locks import locks from sentry.models.organization import Organization -from sentry.models.organizationmember import InviteStatus, OrganizationMember from sentry.models.project import Project from sentry.notebooks.models import Notebook from sentry.notifications.platform.registry import provider_registry @@ -86,6 +85,7 @@ ) from sentry.sentry_apps.event_types import SentryAppEventType from sentry.shared_integrations.exceptions import IntegrationConfigurationError, IntegrationError +from sentry.users.services.user.service import user_service from sentry.utils import metrics from sentry.utils.cache import cache from sentry.utils.locking import UnableToAcquireLock @@ -103,39 +103,15 @@ from sentry.models.group import Group -def has_billing_access(*, organization_id: int, user_id: int) -> bool: - """Whether the user's role in the organization lets them manage billing.""" - member = OrganizationMember.objects.filter( - organization_id=organization_id, - user_id=user_id, - invite_status=InviteStatus.APPROVED.value, - ).first() - return member is not None and "org:billing" in member.get_scopes() - - -def slack_user_has_billing_access( - *, integration_id: int, organization_id: int, slack_user_id: str | None -) -> bool: - """Whether the Slack user is linked to a Sentry user who can manage the organization's billing.""" - if not slack_user_id: +def user_has_billing_access(*, organization_id: int, user_id: int | None) -> bool: + """Whether the user can manage the organization's billing.""" + if user_id is None: return False - integration = integration_service.get_integration( - integration_id=integration_id, - organization_id=organization_id, - status=ObjectStatus.ACTIVE, - ) - if not integration: - return False - identity = identity_service.get_identity( - filter={ - "provider_type": integration.provider, - "provider_ext_id": integration.external_id, - "identity_ext_id": slack_user_id, - } - ) - if not identity: + organization = Organization.objects.filter(id=organization_id).first() + if not organization: return False - return has_billing_access(organization_id=organization_id, user_id=identity.user_id) + user = user_service.get_user(user_id) + return from_user(user, organization).has_scope("org:billing") class EntrypointSetupError(Exception): @@ -159,8 +135,8 @@ class SlackAutofixCachePayload(TypedDict): # Captured at cache creation. When set, the Slack ROOT_CAUSE update swaps the # next-stage trigger button for a "Hand off to ..." button. handoff_target: CodingAgentProviderType | None - # Who started the run from Slack. Unset for runs that nobody started by hand. - slack_user_id: NotRequired[str | None] + # The Sentry user who started the run from Slack. Unset for runs nobody started by hand. + user_id: NotRequired[int | None] class SlackAgentCachePayload(TypedDict): @@ -472,6 +448,8 @@ def on_trigger_handoff_already_exists( ) def create_autofix_cache_payload(self) -> SlackAutofixCachePayload: + # The action webhook already resolved who clicked, so this lookup is cached. + user = self.slack_request.get_identity_user() return SlackAutofixCachePayload( threads=[self.thread], organization_id=self.organization_id, @@ -480,7 +458,7 @@ def create_autofix_cache_payload(self) -> SlackAutofixCachePayload: group_id=self.group.id, group_link=self.get_group_link(self.group), handoff_target=_get_handoff_target(self.group.project), - slack_user_id=self.slack_user_id, + user_id=user.id if user else None, ) @staticmethod @@ -520,10 +498,9 @@ def on_autofix_update( # The copy is written for whoever started the run. message = SeerAutofixOutOfCredits( organization_id=cache_payload["organization_id"], - has_billing_access=slack_user_has_billing_access( - integration_id=cache_payload["integration_id"], + has_billing_access=user_has_billing_access( organization_id=cache_payload["organization_id"], - slack_user_id=cache_payload.get("slack_user_id"), + user_id=cache_payload.get("user_id"), ), ) else: diff --git a/tests/sentry/seer/entrypoints/slack/test_slack.py b/tests/sentry/seer/entrypoints/slack/test_slack.py index b92504b75015..71c9d56a2408 100644 --- a/tests/sentry/seer/entrypoints/slack/test_slack.py +++ b/tests/sentry/seer/entrypoints/slack/test_slack.py @@ -110,6 +110,7 @@ def setUp(self) -> None: self.slack_request.integration = self.integration self.slack_request.channel_id = self.channel_id self.slack_request.user_id = self.slack_user_id + self.slack_request.get_identity_user.return_value = self.user self.slack_request.data = {"message": {"ts": self.thread_ts}} self.action = BlockKitMessageAction( name=SlackAction.SEER_AUTOFIX_START.value, @@ -216,7 +217,7 @@ def test_create_autofix_cache_payload(self, mock_read_pref: MagicMock) -> None: assert cache_payload["group_id"] == self.group.id assert cache_payload["threads"] == [self.thread] assert cache_payload["handoff_target"] is None - assert cache_payload["slack_user_id"] == self.slack_user_id + assert cache_payload["user_id"] == self.user.id @patch("sentry.seer.autofix.utils.read_preference_from_sentry_db") def test_create_autofix_cache_payload_captures_handoff_target( @@ -385,12 +386,7 @@ def test_out_of_credits_run_posts_out_of_credits_message( assert AUTOFIX_OUT_OF_CREDITS_MEMBER_MESSAGE in blocks assert "couldn't finish working on this issue" not in blocks - def _post_out_of_credits_message_for_linked_user(self) -> str: - self.create_identity( - user=self.user, - identity_provider=self.create_identity_provider(integration=self.integration), - external_id=self.slack_user_id, - ) + def _post_out_of_credits_message(self) -> str: SeerOperatorAutofixCache.populate_post_autofix_cache( entrypoint_key=SeerEntrypointKey.SLACK, run_id=MOCK_RUN_ID, @@ -422,13 +418,21 @@ def _post_out_of_credits_message_for_linked_user(self) -> str: def test_out_of_credits_run_started_by_billing_member_posts_billing_message( self, _mock_get_scopes: MagicMock ) -> None: - blocks = self._post_out_of_credits_message_for_linked_user() + blocks = self._post_out_of_credits_message() assert AUTOFIX_OUT_OF_CREDITS_BILLING_MESSAGE in blocks assert "Configure auto-reload" in blocks - def test_out_of_credits_run_started_by_member_posts_member_message(self) -> None: - blocks = self._post_out_of_credits_message_for_linked_user() + @patch( + "sentry.models.organizationmember.OrganizationMember.get_scopes", + return_value=frozenset({"org:billing"}), + ) + def test_out_of_credits_run_without_a_starter_posts_member_message( + self, _mock_get_scopes: MagicMock + ) -> None: + self.slack_request.get_identity_user.return_value = None + + blocks = self._post_out_of_credits_message() assert AUTOFIX_OUT_OF_CREDITS_MEMBER_MESSAGE in blocks assert "Contact Admin" in blocks