Feature: Add @ mention support for AmazonQ bot in MR for code review and follow up comments
Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.
Original issue: https://gitlab.com/gitlab-com/ops-sub-department/aws-gitlab-ai-integration/integration-motion-planning/-/issues/651#note_2794729108
One of our goals with GQ (GitLab Duo powered by Amazon Q ProServe engagement) is to reach feature parity between GitLab Duo Enterprise and GitLab Duo powered by Amazon Q.
As it stands, there is a feature gap between the way users can request code reviews from GitLab Duo. Users can mention the GitLab Duo bot identity directly to ask for a review, whereas with Amazon Q, they must use quick actions. With GitLab Duo Enterprise, this interaction style also allows for threaded conversations (allowing the user to follow-up on the AI’s comments), while the quick actions does not.
Current State
- Users must use quick actions
/q review
to request review from AmazonQ - Users cannot reply directly to suggestions from AmazonQ
Desired State
- Users see @amazonq in autocomplete and can mention it directly
-
/assign_reviewer @AmazonQ
works identically to GitLabDuo - Users can respond to AmazonQ’s suggestions by mentioning it in the comment thread (same experience as GitLabDuo)
- If the user is not onboarded, return an error message that directs the user to onboard
There are two parts to the design:
- Adding the initial review support
- Added threaded comments support
1. Adding Initial Review Support
To support initial reviews, modify the existing bot identity provider to use license parsing to determine which bot identity (GitLabDuo or AmazonQ) to return. Then, rely on the existing ReviewMergeRequestService
to provide the AI-powered code review.
- License-based routing - Return the correct bot identity (AmazonQ or GitLabDuo) depending on the user’s license
-
Service provider - Use existing
Llm::ReviewMergeRequestService
for the AI-powered code review
Details for license-based routing:
Customer License | Bot Available | Powered By |
---|---|---|
Amazon Q | @amazonq | Amazon Q APIs |
Duo Enterprise | @GitLabDuo | GitLab Duo |
Key Changes Required
-
Bot Identity Logic (
lib/users/internal.rb
)- Upgrade the
duo_code_review_bot
method to perform license-based routing:- Check
Ai::AmazonQ.enabled?
for instance-level availability (license + configuration) - Return AmazonQ bot if enabled, GitLabDuo bot otherwise
- Single method serves as source of truth for all assignment types (auto and manual)
- Check
- Upgrade the
-
LLM Integration (
ee/app/services/llm/review_merge_request_service.rb
)- Perform an AmazonQ review via. AmazonQ Trigger Service if AmazonQ is enabled instead of queuing a completion worker
- This will use
send_event
-
Permission Integration (
ee/app/policies/ee/project_policy.rb
)- Extend
:access_ai_review_mr
permission to include AmazonQ licensing:(ai_review_mr_enabled | amazon_q_integration_enabled) & user_allowed_to_use_ai_review_mr
- Namespace-level validation: reuse existing
Gitlab::Llm::FeatureAuthorizer
validation chain for GitLab Duo users, withproject.duo_features_enabled
- Extend
-
Service Compatibility (No Changes Required)
- AmazonQ bot works automatically with existing
Llm::ReviewMergeRequestService
- Both bots share
user_type: :duo_code_review_bot
for consistent permission handling - Existing
Llm::BaseService.valid?
validation pattern works for both providers
- AmazonQ bot works automatically with existing
-
Auto-complete (
ee/app/finders/ee/autocomplete/users_finder.rb
)- Return appropriate bot based on license-based routing from
Users::Internal.duo_code_review_bot
- Single bot returned based on current licensing state
- Return appropriate bot based on license-based routing from
-
Auto-assign Reviews (No Changes Required)
- Existing
assign_duo_as_reviewer
callsUsers::Internal.duo_code_review_bot
→ gets correct bot automatically - Same permission validation through
ai_review_merge_request_allowed?
- License-based routing happens transparently in bot selection
- Existing
-
Error Handling Integration (Leverages Existing Patterns)
- Same
Llm::BaseService
validation and error message patterns - Cascading
duo_features_enabled
validation works for both providers - Standard error message: "AI features are not enabled or resource is not permitted to be sent."
- AmazonQ licensing errors handled by
Ai::AmazonQ.enabled?
check
- Same
Control Flow Comparison
- AmazonQ Quick Actions
- /q review → Quick Actions parser → AmazonQTriggerService → AI Gateway → Prompt Resolution → SendEvent → Model
- /assign_reviewer @amazonq
- Add @amazonq as reviewer → ReviewMergeRequestService → AmazonQTriggerService → AI Gateway
- /assign_reviewer @GitLabDuo
- Add @GitLabDuo as reviewer → ReviewMergeRequestService → CompletionWorker → CompletionService → AI Gateway
Error Handling
Error Cases
-
Instance-level: AmazonQ integration not enabled (
::Ai::AmazonQ.enabled?
returns false - license + configuration) -
Cascading Settings:
duo_features_enabled
disabled at instance → group → project levels (reuses existing cascade) - User Permission: User lacks developer access or appropriate license for AmazonQ
-
Validation Chain: Same
Llm::BaseService.valid?
pattern with standard error messages
Solution
-
The instance does not have the AmazonQ license
- The user will not be available, so we do not have to return a specific error
-
The instance has the AmazonQ license but has not onboarded
- If the instance has the correct license for @amazonq but isn’t onboarded, return an error that directs to contact administrator for onboarding.
- “AmazonQ has not been onboarded. Please contact your administrator or see the following setup guide [Link] to onboard.
-
AmazonQ is not enabled on the project or group
- AmazonQ leverages the existing cascading permission system through
duo_features_enabled
settings. The unified permission model means AmazonQ uses the same validation chain as GitLab Duo, with license-based routing determining which provider is used - If AmazonQ is not enabled, then return an error that mentions this fact
- “AmazonQ is not enabled for the project or group. Please see the following documentation [Link] for how to enable the feature.
- AmazonQ leverages the existing cascading permission system through
Technical Risks
-
License Check Performance:
Ai::AmazonQ.feature_available?
should be cached/efficient - Performance Parity: Need to ensure the new model provider implementation for the Merge Request Review has the same performance as the quick actions. Ensure that the request is resolved by the same model providers as the existing implementation.
2. Adding Thread Comment Support
Create a dedicated worker to route @amazonq mention responses through Amazon Q SendEvent, keeping the architecture clean and separate from Duo Chat infrastructure... (or, use the existing DuoCodeReviewChatWorker
and add forking logic within that worker).
SWAG Estimate
3 weeks
- Research into supporting threaded conversation for TARS: 2 days
- GitLab Service modifications: 3 days
- Testing/validation: 2 days
- Rework: 3 days
- Testing/validation: 2 days
Architecture
Current Flow (GitLab Duo)
Note with @ GitLabDuo mention → PostProcessService
→ DuoCodeReviewChatWorker
→ Chat
Proposed Flow
Create Note with @amazonq mention → PostProcessService
→ AmazonQReviewWorker (new)
→ AmazonQTriggerService
→ SendEvent
Implementation Changes
1. ee/app/workers/merge_requests/amazon_q_review_worker.rb (new)
- Handle @amazonq mentions in merge request comments
- Extract mention content and discussion context
- Call
AmazonQTriggerService
withmention_reply
command
2. ee/app/models/ee/note.rb
- Add
amazon_q_mentioned?
method (similar to existingduo_bot_mentioned?
)
3. ee/app/services/ee/notes/post_process_service.rb
- Add
process_amazon_q_review
method - Trigger
AmazonQReviewWorker
for @amazonq mentions - Keep existing
process_duo_code_review_chat
for @GitLabDuo mentions
4. ee/app/services/ai/amazon_q/amazon_q_trigger_service.rb
- Add support for
mention_reply
command - Handle discussion thread context
- Create reply notes in the correct thread
- Reuse existing SendEvent infrastructure
5. ee/app/models/ai/amazon_q/request_payload.rb
- Add
mention_reply_payload
method - Include discussion thread as context
- Include diff context for diff notes
- Format user question from mention
Test Files
5. ee/spec/workers/merge_requests/amazon_q_review_worker_spec.rb (new)
- Test mention detection and processing
- Test error handling
- Test discussion context extraction
6. ee/spec/services/ai/amazon_q/amazon_q_trigger_service_spec.rb
- Add tests for mention_reply command
- Test reply note creation
Data Flow
- User mentions @amazonq in comment
-
Notes::CreateService
callsPostProcessService
after note creation -
PostProcessService
detects @amazonq mention and enqueuesAmazonQReviewWorker
- Worker extracts context and calls
AmazonQTriggerService
- Service sends to TARS via SendEvent with discussion context
- TARS response creates reply note in thread
Payload Structure
{
"command": "mention_reply",
"discussion_id": "...",
"note_id": "...",
"user_question": "extracted mention text",
"discussion_context": [...],
"diff_context": {...}, // if applicable
// standard fields (project, MR, auth)\
}