Draft: Embed custom AI agent context in GitLab HTML responses (instance/group/project level)

What does this MR do and why?

Related issue: #601517

What does this MR do and why?

Adds configurable custom AI instructions that are inlined into the embedded "GitLab AI Context" block, cascading across instance → group(s) → project.

This builds on top of !240378 (merged) (the base MR that embeds the AI context block in work item responses). This MR adds the settings, the cascade resolver, and the rendering of a labelled Custom instructions: section.

When set, the block gains a section like:

Custom instructions: Instance Group: top-group Group: sub-group Project

Levels are concatenated and ordered most-general first, most-specific last, so an agent reads the most specific instruction last. Blank levels are skipped. For a group work item, the project line is omitted.

Scope

  • Instance: Admin → Settings → General (ApplicationSetting)
  • Group: Group → Settings → General (NamespaceSetting), cascades down the full ancestor chain to subgroups and their projects
  • Project: Project → Settings → General (ProjectSetting)

Each level is a free-text field, max 2000 characters.

Notes

  • Gated behind the ai_custom_instructions feature flag.
  • A custom concatenating resolver (Gitlab::Ai::CustomInstructionsResolver) is used instead of the built-in cascading_attr, because cascading_attr is first-non-nil-wins and would not concatenate across levels.

Feature flag

Flag Default Group
ai_custom_instructions disabled ``

References

Screenshots or screen recordings

Bildschirmfoto_2026-06-24_um_16.07.19 Bildschirmfoto_2026-06-24_um_16.07.03

How to set up and validate locally

  1. Enable the flag:
    Feature.enable(:ai_custom_instructions)
  2. Set instructions at one or more levels:
  • Project: Project → Settings → General → AI context
  • Group: Group → Settings → General → AI context
  • Instance: Admin → Settings → General → AI context
  1. Open a work item in an affected project/group and inspect the embedded block: curl -s "///-/work_items/" | rg -A6 'Custom instructions' Expect a Custom instructions: section with one [Level] text line per set level, ordered instance → group(s) → project.
  2. Verify a group work item shows instance + group lines but no [Project] line: curl -s "/groups//-/work_items/" | rg -A6 'Custom instructions'
  3. With the flag disabled, the Custom instructions: section is not rendered. Run the specs:
bundle exec rspec \
  spec/lib/gitlab/ai/custom_instructions_resolver_spec.rb \
  spec/helpers/ai_context_helper_spec.rb \
  spec/models/namespace_setting_spec.rb \
  spec/models/project_setting_spec.rb
Edited by Dennis Meister

Merge request reports

Loading
Loading