Draft: PoC: Trigger AI flows on project webhook events [skip ci]

What does this MR do and why?

This is a proof of concept. It is not for merge. We share it so people can read the code and see the shape of the architecture.

The product idea has two parts:

  1. Any event that fires a webhook can trigger an AI flow.
  2. Any field in the webhook payload can decide if the trigger fires.

Today we build each trigger type by hand. This PoC adds one generic path. A single dispatch point in Project#execute_hooks serves several webhook scopes. To add a scope, you add it to one list and to EVENT_TYPES.

Shape

service (MR approved, issue created, note added, wiki page saved)
  -> Project#execute_hooks(data, scope)          the existing webhook funnel
       -> webhooks, system hooks                 unchanged
       -> EE::Project#execute_hooks, after commit
            -> Ai::FlowTriggers::WebhookDispatchService   in the request
                 1. scope in WEBHOOK_EVENT_TYPES?          no query
                 2. Ai::FlowTrigger.registered_for?        one query
                 3. actor from payload user.id
                 4. caused by a flow? (composite identity) skip, or find the parent workflow
                 5. enqueue the worker with the built payload
            -> Ai::FlowTriggers::WebhookEventWorker       in Sidekiq
                 -> Ai::FlowTriggers::EventTriggerService
                      -> FilterEvaluator on the payload    any field, any operator
                      -> resource from the payload         per scope
                      -> Ai::FlowTriggers::RunService

Generic parts: the funnel, the dispatch checks, the worker, the filter, and the run.

Parts that are still per scope:

  • The resource lookup in EventTriggerService (MR, issue, the noteable of a note, none for a wiki page).
  • The actor field. All four scopes here use the top-level user. Other scopes do not.

Scopes in this PoC

  • merge_request_hooks
  • issue_hooks
  • note_hooks
  • wiki_page_hooks, added in the last commit to show the cost of one more scope

Their callers always call execute_hooks, even when the project has no webhook. Scopes like push, pipeline, job, emoji and milestone only call it when a webhook exists, so they are not in this PoC.

pipeline_hooks keeps its own path in EE::Ci::Pipelines::HookService. It is not in the list, so its triggers do not run twice.

Example filter

This trigger fires when alice approves an MR, and not when bob does:

{
  "merge_request_hooks": {
    "match": "all",
    "rules": [
      { "field": "object_attributes.action", "operator": "in", "value": ["approval", "approved"] },
      { "field": "user.username", "operator": "eq", "value": "alice" }
    ]
  }
}

What is not in this PoC

  • No UI. The new event types are GraphQL experiments.
  • No group-level hooks, and no confidential scopes.
  • No feature flag. A real version would ship behind a wip flag on the project.
  • No change to the pipeline_hooks path or to the CloudEvent triggers.

Known limits

  • Gitlab::FilterEvaluator cannot look inside arrays. Rules like labels.title or reviewers.state never match. A label rule must use contains on the whole labels value.
  • RunService records the run under the webhook event type, for example merge_request_hooks. Features keyed on merge_request or work_item, such as the fix loop and on-finish settings, do not apply to these runs.
  • Each execute_hooks call for the four scopes adds one registered_for? query, also in projects without triggers.
  • The trigger form drops event types it does not know. Editing one of these triggers in the UI removes its event type.

References

Screenshots or screen recordings

Not applicable. This MR has no UI changes.

How to set up and validate locally

You need a license with Duo Agent Platform, and a user who can trigger AI flows in the project.

  1. In a Rails console, create a trigger on a project. Use a config_path flow or a custom catalog flow. The Developer flow has no goal template for these events, so it skips them.

    project = Project.find_by_full_path('group/project')
    service_account = User.service_account.first
    
    Ai::FlowTrigger.create!(
      project: project,
      user: service_account,
      config_path: '.gitlab/duo/flow.yml',
      description: 'Run when alice approves',
      event_types: [Ai::FlowTrigger::EVENT_TYPES[:merge_request_hooks]],
      filter: { 'merge_request_hooks' => { 'match' => 'all', 'rules' => [
        { 'field' => 'object_attributes.action', 'operator' => 'in', 'value' => %w[approval approved] },
        { 'field' => 'user.username', 'operator' => 'eq', 'value' => 'alice' }
      ] } }
    )
  2. Approve an MR in that project as alice. A flow session starts.

  3. Approve another MR as bob. No session starts.

Manual check in GDK

I tested the PoC by hand in GDK with real API requests. The triggers worked as expected.

Setup in gitlab-duo/test:

  • Trigger 1 on merge_request_hooks fires when the action is approval or approved and the user is alice.
  • Trigger 2 on issue_hooks fires when the action is open and labels contains backend.
  • Both triggers use a small config_path flow. It only prints the event name and the payload.

Results:

Action Expected Result
alice approves an MR run A session started, the job printed event=merge_request_hooks, and the progress note was posted on the MR
bob approves an MR no run The worker ran, and the filter rejected the event
alice opens an issue with the backend label run A session started, the job printed event=issue_hooks, and the progress note was posted on the issue
alice opens an issue without a label no run The worker ran, and the filter rejected the event
root opens two MRs no run The worker ran for each event, and the filter rejected them

Each approval sent two webhook events, so it started two worker jobs. See the query cost in "Known limits".

The sessions end as "dropped" because the test flow does not start the Duo agent.

To run Duo Agent Platform flows in GDK, I needed two changes that the setup guide does not mention:

  • CLOUD_CONNECTOR_SELF_SIGN_TOKENS: '1' under env in gdk.yml. Without it, the AI feature catalog is empty on self-managed with an online license, and every flow fails with 403 forbidden to access duo workflow.
  • trusted_forwarded_hosts = ["gdk.test:3000"] in gitlab/workhorse/config.toml. Without it, Workhorse rejects the WebSocket of the flow job, because the HTTP router changes the Host header to the NGINX port. gdk reconfigure removes this line.

MR acceptance checklist

This is a PoC and is not for merge. The checklist does not apply.

Edited by Vitali Tatarintev

Merge request reports

Loading
Loading