Phase 2: Instance-Level Custom Instructions for Duo Code Review
## Overview
Track the work required to implement instance-level custom instructions for Duo Code Review, as described in the parent epic [&21504](https://gitlab.com/groups/gitlab-org/-/work_items/21504).
This is Phase 2 of the group-level custom instructions feature. It extends the same mechanism introduced in Phase 1 (group-level) to the instance as a whole. This phase primarily targets **self-managed GitLab instances**, where instance administrators need a centralized way to enforce organization-wide code review standards across all groups and projects without relying on group-level configuration.
## Motivation
Self-managed instance administrators often manage large, complex GitLab deployments with many groups and projects. Without instance-level instructions, they must either:
- Configure group-level instructions for every top-level group individually, or
- Accept that some groups or projects may not follow organizational standards
Instance-level custom instructions solve this by providing a single, authoritative baseline that applies everywhere on the instance.
## Goal
Instance administrators should be able to designate a project as the instance-level Duo template repository. The instructions defined there will be automatically applied to all merge requests across the entire instance, in addition to any group-level and project-level instructions.
## Scope
Similar to Phase 1 (group-level), but scoped to the instance:
- **DB**: Extend instance-level settings (e.g., `application_settings`) to store a `duo_template_project_id` referencing the designated template project
- **API**: Extend the admin API to allow updating `duo_template_project_id` at the instance level (in the GitLab Duo features section)
- **Rule resolution**: Update the rule hierarchy resolver (introduced in Phase 1) to also traverse instance-level instructions, placing them at the base of the inheritance chain (instance → ancestor groups → project)
- **AI Gateway**: Ensure the rule fetching logic accounts for instance-level instructions in the hierarchy
- **UI**: Allow instance administrators to configure the template project via Admin Area > GitLab Duo settings (behind a feature flag)
## Inheritance Order (with Phase 2)
1. Instance-level rules (baseline, self-managed only)
2. Group-level rules (most-specific ancestor group first)
3. Project-level rules
## Acceptance Criteria
- [ ] Instance admins on self-managed GitLab can designate an existing project as the instance-level Duo template repository via the Admin Area UI and via REST/GraphQL APIs
- [ ] The `.gitlab/duo/mr-review-instructions.yaml` file from the designated instance template project is automatically applied to all projects on the instance during Duo Code Review
- [ ] Instance-level instructions are merged with group-level and project-level instructions following the defined inheritance order
- [ ] The feature is gated behind a feature flag
- [ ] Existing group-level and project-level behavior is not affected
## Related
- Parent epic: [&21504 Group-Level Custom Instructions for Duo Code Review](https://gitlab.com/groups/gitlab-org/-/work_items/21504)
- Phase 1 epic: [&21571 Phase 1: Group-Level Custom Instructions for Duo Code Review – Backend + REST API](https://gitlab.com/groups/gitlab-org/-/work_items/21571)
issue