Skip to content
Merged
10 changes: 9 additions & 1 deletion src/sentry/seer/autofix/on_completion_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
31 changes: 30 additions & 1 deletion src/sentry/seer/entrypoints/slack/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +25,7 @@
SeerAgentResponse,
SeerAgentWriteApproval,
SeerAutofixError,
SeerAutofixOutOfCredits,
SeerAutofixStopped,
SeerAutofixUpdate,
SeerInvestigationError,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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."""

Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starter user_id overwritten on continue

Medium Severity

user_id is documented as the Sentry user who started the run, and the out-of-credits copy is written for that person. create_autofix_cache_payload always records whoever just clicked, and populate_post_autofix_cache replaces the whole payload on every continue. When someone else continues a run, the thread gets the continuer's billing copy instead of the starter's, so a billing admin who started it can see Contact Admin rather than Configure auto-reload.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 204cca4. Configure here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its intended. whoever clicked continue should be targeted as the user who clicked.

)

@staticmethod
Expand All @@ -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

Expand All @@ -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"],
Expand Down
33 changes: 33 additions & 0 deletions tests/sentry/seer/autofix/test_autofix_on_completion_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
86 changes: 86 additions & 0 deletions tests/sentry/seer/entrypoints/slack/test_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down
Loading