Add conversation context to AI mention flow goals

What does this MR do and why?

When a foundational-flow service account is @mentioned in a note, the AI agent currently only receives the single triggering note as its input. To understand a multi-turn conversation, the agent therefore has to fetch the rest of the discussion itself — which means additional LLM calls and token usage, i.e. extra latency and cost for our customers. This MR gives the agent the full discussion thread up front so that round-trip is no longer needed.

This is part 1 of 3 in splitting up the larger mention → messaging-adapter migration (alternative to !235039 (closed) / !238360 (closed)) into independently reviewable pieces. This part is self-contained and improves the existing RunService mention path — no feature flag, no new adapter.

Changes:

  1. New Ai::Notes::ConversationContextBuilder — extracts the human (non-system) notes of a note's discussion, ordered chronologically, capped at 15 (older messages truncated with a notice telling the agent how to fetch more), formatted as <message> blocks.
  2. Mention goal template restructured — the single <mention> block becomes explicit <gitlab_context> (resource/title/note URLs) and <conversation> (thread history) sections. Delivery instructions are now selectable via a delivery_mode param: :default preserves today's reply-in-thread + glab api guidance; :adapter is added (dormant) for the later messaging-adapter path.
  3. PostProcessService wiring — feeds the built conversation context (instead of just note.note) plus a structured source_context and discussion_id into the mention flow.

Database

Raw SQL:

SELECT "notes".*
   FROM "notes"
   WHERE "notes"."noteable_type" = 'Issue' -- O
     AND "notes"."noteable_id" = [notable_id]
     AND "notes"."discussion_id" = [discussion_id]
     AND "notes"."system" = FALSE
   ORDER BY "notes"."created_at" DESC, "notes"."id" DESC
   LIMIT 16;

Plan: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/52186/commands/153708

How to set up and validate locally

  1. Set up a foundational-flow service account with a :mention trigger on a project.
  2. In an issue/MR/work-item discussion, post a couple of comments, then @mention the service account in a reply.
  3. Confirm the agent's goal now contains a <conversation> block with the prior messages (not just the triggering note) and a <gitlab_context> block with the resource/note URLs. This can be inspected in the agent session view or in the LangSmith traces for the run.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist.

References

Edited by Thomas Schmidt

Merge request reports

Loading