From b45c48d0b5ec4a99429ddbf3edf262eb4cba007c Mon Sep 17 00:00:00 2001 From: ayoubdiourin7 Date: Tue, 24 Nov 2026 21:06:07 +0100 Subject: [PATCH 1/2] Record the Slack account and source on runs started from a button --- .../listeners/actions/start_agent_run.py | 19 +++++++++--- .../hackbot-api/tests/test_slack_listeners.py | 31 ++++++++++++++----- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/services/hackbot-api/app/slack/listeners/actions/start_agent_run.py b/services/hackbot-api/app/slack/listeners/actions/start_agent_run.py index 91c9349b855..0d6db76b08c 100644 --- a/services/hackbot-api/app/slack/listeners/actions/start_agent_run.py +++ b/services/hackbot-api/app/slack/listeners/actions/start_agent_run.py @@ -4,7 +4,7 @@ from typing import Any from uuid import UUID -from hackbot_client import HackbotClient +from hackbot_client import HackbotClient, Identity from pydantic import AliasChoices, BaseModel, ConfigDict, Field, Json, validate_call from slack_bolt.context.ack.async_ack import AsyncAck from slack_bolt.context.async_context import AsyncBoltContext @@ -37,7 +37,8 @@ class SlackUser(BaseModel): https://docs.slack.dev/reference/objects/user-object/ """ - id: str | None = None + id: str + username: str class BlockActionsPayload(BaseModel): @@ -46,7 +47,7 @@ class BlockActionsPayload(BaseModel): https://docs.slack.dev/reference/interaction-payloads/block_actions-payload/ """ - user: SlackUser | None = None + user: SlackUser message: dict @@ -127,7 +128,7 @@ async def start_agent_run_callback( should be fixed with https://github.com/mozilla/ai-dev/issues/6468. """ client: HackbotClient = context["hackbot_client"] - user = body.user.id if body.user else None + user = body.user.id value = action.value if value.apply_run_id: @@ -151,7 +152,15 @@ async def start_agent_run_callback( return run = await client.trigger_run( - value.agent_name, value.inputs, dedupe_key=value.dedupe_key + value.agent_name, + value.inputs, + dedupe_key=value.dedupe_key, + source="slack", + identity=Identity( + identity_provider="slack", + user_id=body.user.id, + username=body.user.username, + ), ) triggered_by = user if run.is_new else None diff --git a/services/hackbot-api/tests/test_slack_listeners.py b/services/hackbot-api/tests/test_slack_listeners.py index 5e224a475b7..40d66004f58 100644 --- a/services/hackbot-api/tests/test_slack_listeners.py +++ b/services/hackbot-api/tests/test_slack_listeners.py @@ -17,7 +17,13 @@ StartAgentRunValue, start_agent_run_callback, ) -from hackbot_client import ApplyActionsResponse, RunAction, RunStatus, TriggeredRun +from hackbot_client import ( + ApplyActionsResponse, + Identity, + RunAction, + RunStatus, + TriggeredRun, +) from pydantic import ValidationError from slack_bolt.context.ack.async_ack import AsyncAck from slack_bolt.context.async_context import AsyncBoltContext @@ -99,7 +105,7 @@ def setup_method(self): ) self.context = AsyncBoltContext({"hackbot_client": self.fake_client}) self.body = { - "user": {"id": "U0CLICKER"}, + "user": {"id": "U0CLICKER", "username": "clicker"}, "channel": {"id": "C0TRIAGE"}, "message": _message(_action()), } @@ -133,16 +139,16 @@ async def _call(self, action: dict | None = None): async def test_starts_the_run_the_button_asks_for(self): await self._call() - self.fake_client.trigger_run.assert_awaited_once_with( - "bug-fix", {"bug_id": 1234}, dedupe_key=DEDUPE_KEY - ) + self.fake_client.trigger_run.assert_awaited_once() + args = self.fake_client.trigger_run.await_args + assert args.args == ("bug-fix", {"bug_id": 1234}) + assert args.kwargs["dedupe_key"] == DEDUPE_KEY async def test_the_agent_and_inputs_come_from_the_buttons_value(self): await self._call(_action(agent_name="test-repair", inputs={"task_id": "abc"})) - self.fake_client.trigger_run.assert_awaited_once_with( - "test-repair", {"task_id": "abc"}, dedupe_key=DEDUPE_KEY - ) + args = self.fake_client.trigger_run.await_args + assert args.args == ("test-repair", {"task_id": "abc"}) async def test_the_key_is_forwarded_so_the_api_can_collapse_repeats(self): # The whole of #6732: the receiver keeps no state, so this key reaching @@ -153,6 +159,15 @@ async def test_the_key_is_forwarded_so_the_api_can_collapse_repeats(self): "frontend-triage-run:abc" ) + async def test_the_clicker_is_recorded_as_the_runs_account(self): + await self._call() + + kwargs = self.fake_client.trigger_run.await_args.kwargs + assert kwargs["source"] == "slack" + assert kwargs["identity"] == Identity( + identity_provider="slack", user_id="U0CLICKER", username="clicker" + ) + async def test_the_click_is_acknowledged(self): await self._call() From 782c166ea6bffd16efe83aeca954c195af3ef81b Mon Sep 17 00:00:00 2001 From: ayoubdiourin7 Date: Thu, 26 Nov 2026 07:07:40 +0100 Subject: [PATCH 2/2] Use the source and identity provider enums for Slack buttons --- .../app/slack/listeners/actions/start_agent_run.py | 6 +++--- services/hackbot-api/tests/test_slack_listeners.py | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/services/hackbot-api/app/slack/listeners/actions/start_agent_run.py b/services/hackbot-api/app/slack/listeners/actions/start_agent_run.py index 0d6db76b08c..7875ee68520 100644 --- a/services/hackbot-api/app/slack/listeners/actions/start_agent_run.py +++ b/services/hackbot-api/app/slack/listeners/actions/start_agent_run.py @@ -4,7 +4,7 @@ from typing import Any from uuid import UUID -from hackbot_client import HackbotClient, Identity +from hackbot_client import HackbotClient, Identity, IdentityProvider, Source from pydantic import AliasChoices, BaseModel, ConfigDict, Field, Json, validate_call from slack_bolt.context.ack.async_ack import AsyncAck from slack_bolt.context.async_context import AsyncBoltContext @@ -155,9 +155,9 @@ async def start_agent_run_callback( value.agent_name, value.inputs, dedupe_key=value.dedupe_key, - source="slack", + source=Source.slack, identity=Identity( - identity_provider="slack", + identity_provider=IdentityProvider.slack, user_id=body.user.id, username=body.user.username, ), diff --git a/services/hackbot-api/tests/test_slack_listeners.py b/services/hackbot-api/tests/test_slack_listeners.py index 40d66004f58..bb4913a632b 100644 --- a/services/hackbot-api/tests/test_slack_listeners.py +++ b/services/hackbot-api/tests/test_slack_listeners.py @@ -20,8 +20,10 @@ from hackbot_client import ( ApplyActionsResponse, Identity, + IdentityProvider, RunAction, RunStatus, + Source, TriggeredRun, ) from pydantic import ValidationError @@ -163,9 +165,11 @@ async def test_the_clicker_is_recorded_as_the_runs_account(self): await self._call() kwargs = self.fake_client.trigger_run.await_args.kwargs - assert kwargs["source"] == "slack" + assert kwargs["source"] == Source.slack assert kwargs["identity"] == Identity( - identity_provider="slack", user_id="U0CLICKER", username="clicker" + identity_provider=IdentityProvider.slack, + user_id="U0CLICKER", + username="clicker", ) async def test_the_click_is_acknowledged(self):