Cap concurrent business logic security scans per group

What does this MR do and why?

Problem

  • There is no per-group limit on concurrent business logic security scans. The per-user session check only logs.
  • When CustomersDot sends no max-age, Ai::UsageQuotaService caches the quota answer for 1 hour, so a change in quota can take up to an hour to be noticed.

Change

  1. Per-group cap on active business logic security scans. At workflow create, when the flow is bl_security/experimental, count the active (created / running) scans in the root namespace's hierarchy. At the limit (default 20), refuse the new scan with reason concurrent_scan_limit and a message saying why.
    • The service marks the refusal :too_many_requests, but the REST and pipeline paths turn every create refusal into a 400 with the message, so clients see 400. GraphQL returns it in errors.
    • Each refusal logs Business logic security scan refused: concurrent scan limit reached with the root namespace ID and the limit, so we can see how often groups hit it.
    • The business logic scanning docs list the limit under Known issues.
    • Ops can change the limit without a code change with DUO_WORKFLOW_MAX_CONCURRENT_BL_SECURITY_SCANS_PER_GROUP, the same pattern as the per-user session limit. 0 disables it; a value that isn't a number falls back to 20.
  2. 5-minute fallback for the usage quota cache. When CustomersDot sends no max-age, the answer is cached for 5 minutes instead of 1 hour. A max-age from CustomersDot is still honored (with the 30-second floor), so CustomersDot keeps control of the pacing.
Case Today on master With this MR
21st concurrent scan in one root namespace Starts Refused with a clear message
21st concurrent session, other flows (Chat, Code Review, …) Starts Starts (unchanged)
Limit variable set to 0 – No limit
Finished / failed / stopped scans – Not counted
Scans in another root namespace – Not counted
CustomersDot max-age=1200 (sent today) Cached 20 minutes Cached 20 minutes (unchanged)
No max-age Cached 1 hour Cached 5 minutes
max-age=0 Cached 30 s Cached 30 s (unchanged)

Limits

  • Why 20: across 161 production business logic security sessions since 2026-10-16, the busiest group was an internal benchmark group at 7 at once. No other group went above 3.
    • A session holds its slot for 14 minutes at p50 and 51 minutes at p95.
    • 20 is about 3× the observed peak. It also covers a group running ~20 MR pipelines an hour at p95 (~17 at once).
    • Ops can raise or disable it with the variable above, with no code change.
  • A refused pipeline scan is visible and can be retried. It goes through the refusal hook from Notify the merge request when a pipeline flow l... (!268209 - merged): the merge request gets one note, and the per-pipeline lock is released so a retried job runs.

Deliberate choices

  • Fail-open stays. UsageQuotaService still allows the request when CustomersDot can't be reached. Duo Chat and other callers depend on that. Changing it is out of scope here.
  • Soft cap. Creates that race between the count and the insert can all pass, so a group can go over the limit by the number of racing creates. Those scans run to completion. A lock would make it exact, for more code.
  • Stuck scans free their slot within an hour. The count skips sessions not updated in the last hour, even before CleanStuckWorkflowsService drops them (30 minutes idle, worker every 30 minutes). A stuck session therefore never holds a slot past that window.
  • Only active statuses count. Paused or input-waiting sessions don't run anything, and the stuck-session cleanup doesn't touch them, so counting them could lock a group out.

Impact

  • A group already running 20 scans gets the 21st refused until one finishes.

  • A refused pipeline-triggered scan posts this note on the merge request, as the flow's service account:

    ⚠️ Business Logic Security Scan did not run because this group already has the maximum number of scans running. Retry the job when one of them finishes.

  • Only answers without a max-age refresh more often (every 5 minutes instead of hourly). Answers with a max-age are unchanged.

  • CustomersDot plans to step its max-age down to 5 minutes after re-measuring load (discussed with Fulfillment on this MR).

  • A cached CustomersDot error without a max-age (the fail-open path) is also kept 5 minutes instead of 1 hour.

Backward compatibility

  • Groups already running 20 business logic security scans at once get the 21st refused. Nothing changes below 20, and other flows aren't affected.
  • The limit is set with DUO_WORKFLOW_MAX_CONCURRENT_BL_SECURITY_SCANS_PER_GROUP (default 20; 0 turns it off). Changing it needs the variable set and Rails restarted, not a deploy.
Change Who consumes it Risk Mitigation
New refusal in CreateWorkflowService Only bl_security/experimental creates (GraphQL, REST, pipeline-triggered flows) A busy group hits the cap Returns a clear message and a distinct reason; other flows untouched
Cache TTL fallback CreateWorkflowService#check_usage_quota (Duo Chat and all Agent Platform sessions), the REST direct-access endpoint, Ai::Agents::CheckAgentStatusService More CustomersDot calls for answers without a max-age Only the no-max-age case changes; CustomersDot keeps pacing everything else; fail-open path unchanged

Database

One new query per business logic security scan create. It reuses Workflow.in_namespace_hierarchy, only counts sessions updated in the last hour, and stops once it reaches the limit (20 rows by default).

SELECT COUNT(*) FROM (
  SELECT 1 AS one FROM (
    (SELECT duo_workflows_workflows.* FROM duo_workflows_workflows
      WHERE duo_workflows_workflows.project_id IN (
        SELECT projects.id FROM projects WHERE projects.namespace_id IN (
          SELECT namespaces.id FROM namespaces WHERE (traversal_ids @> ('{9970}')))))
    UNION ALL
    (SELECT duo_workflows_workflows.* FROM duo_workflows_workflows
      WHERE duo_workflows_workflows.namespace_id IN (
        SELECT namespaces.id FROM namespaces WHERE (traversal_ids @> ('{9970}'))))
  ) duo_workflows_workflows
  WHERE duo_workflows_workflows.workflow_definition = 'bl_security/experimental'
    AND duo_workflows_workflows.status IN (0, 1)
    AND duo_workflows_workflows.updated_at >= '2026-11-05 12:00:00'
  LIMIT 20) subquery_for_count;

Plans from Database Lab (gitlab-production-main), for gitlab-org (namespace 9970):

Shape Cold Warm Buffers (warm)
Without the activity window 5,860 ms 8.0 ms 3,949 shared hit
With updated_at in the last hour (this MR) 1.5 ms 0.07 ms 19 shared hit
Stress: updated_at in the last 24 hours 7.4 ms 2.7 ms 1,349 shared hit
  • Both arms drive from idx_workflows_status_updated_at_id (status, updated_at, id). The time bound joins the index condition, so only recently updated active rows are read.
  • The planner never walks the group's projects: it checks each candidate row's project against the hierarchy.
  • A small group gives the same plan (8.9 ms warm without the window).

Why one hour is safe: every checkpoint touches the workflow (Checkpoint#touch_workflow). A running scan keeps updated_at fresh, and one idle for 30 minutes is dropped by CleanStuckWorkflowsService. So the window never hides a live scan; it only skips sessions that are already stuck.

How to verify

Specs: ee/spec/services/ai/duo_workflows/create_workflow_service_spec.rb, ee/spec/services/ai/usage_quota_service_spec.rb.

  • 22 targeted examples pass.
  • With the two service files reverted to master, the refusal example and the cache TTL examples fail on their assertions (checked on the first commit).
  • Mutations: parsing the variable with .to_i fails the "not a number" example; dropping the 0 guard fails the "disabled" example; dropping the activity window fails the "idle session" example.

GDK, real REST create path (POST /api/v4/ai/duo_workflows/workflows, SaaS mode, Ultimate group with beta features on, project in a subgroup).

  • The other "running" scans were seeded rows, so this checks the limit, not scan execution.
Step Active scans in the group Request Result
1 19 business logic security scan Created
2 20 business logic security scan 400: "This group already has 20 business logic security scans running. Try again when one of them finishes."
3 20, variable set to 0 for Rails business logic security scan Created (21 active)

Earlier run at a limit of 5, same path: another flow at the limit was created, and a freed slot (one scan finished) was accepted.

GDK, real merge request pipeline (flow enabled with enabled_foundational_flows, Business Logic scan profile attached, limit set to 2 with the variable):

Active scans Event Result
2 MR pipeline succeeds Flow refused (concurrent_scan_limit); one note on the MR by the flow's service account; per-pipeline lock released
2, limit back to 20 New MR pipeline succeeds Session created; no note

Context

🤖 Generated with Claude Code

Edited by Meir Benayoun

Merge request reports

Loading
Loading