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/slack/entrypoint.py b/src/sentry/seer/entrypoints/slack/entrypoint.py index c5a42b635cf6..c94b287615e9 100644 --- a/src/sentry/seer/entrypoints/slack/entrypoint.py +++ b/src/sentry/seer/entrypoints/slack/entrypoint.py @@ -8,6 +8,7 @@ import orjson from sentry import features +from sentry.auth.access import from_user from sentry.constants import ObjectStatus from sentry.integrations.services.integration.service import integration_service from sentry.integrations.utils.metrics import EventLifecycle @@ -24,6 +25,7 @@ SeerAgentResponse, SeerAgentWriteApproval, SeerAutofixError, + SeerAutofixOutOfCredits, SeerAutofixStopped, SeerAutofixUpdate, SeerInvestigationError, @@ -45,6 +47,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, @@ -82,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 @@ -99,6 +103,17 @@ from sentry.models.group import Group +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 + organization = Organization.objects.filter(id=organization_id).first() + if not organization: + return False + user = user_service.get_user(user_id) + return from_user(user, organization).has_scope("org:billing") + + class EntrypointSetupError(Exception): """Raised when entrypoint construction fails during mention processing.""" @@ -120,6 +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 + # The Sentry user who started the run from Slack. Unset for runs nobody started by hand. + user_id: NotRequired[int | None] class SlackAgentCachePayload(TypedDict): @@ -431,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, @@ -439,6 +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), + user_id=user.id if user else None, ) @staticmethod @@ -464,7 +484,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 @@ -474,6 +494,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: + # The copy is written for whoever started the run. + message = SeerAutofixOutOfCredits( + organization_id=cache_payload["organization_id"], + has_billing_access=user_has_billing_access( + organization_id=cache_payload["organization_id"], + user_id=cache_payload.get("user_id"), + ), + ) else: message = SeerAutofixError( organization_id=cache_payload["organization_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..71c9d56a2408 100644 --- a/tests/sentry/seer/entrypoints/slack/test_slack.py +++ b/tests/sentry/seer/entrypoints/slack/test_slack.py @@ -26,6 +26,8 @@ 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, SeerAgentWriteApproval, @@ -108,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, @@ -214,6 +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["user_id"] == self.user.id @patch("sentry.seer.autofix.utils.read_preference_from_sentry_db") def test_create_autofix_cache_payload_captures_handoff_target( @@ -351,6 +355,88 @@ 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_MEMBER_MESSAGE in blocks + assert "couldn't finish working on this issue" not in blocks + + def _post_out_of_credits_message(self) -> str: + 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() + + assert AUTOFIX_OUT_OF_CREDITS_BILLING_MESSAGE in blocks + assert "Configure auto-reload" in blocks + + @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 + @patch("sentry.integrations.slack.integration.SlackIntegration.send_threaded_ephemeral_message") @patch( "sentry.integrations.slack.integration.SlackIntegration.send_threaded_message",