Enforce per-user concurrent session limit for Duo Workflows

What does this MR do and why?

Adds a per-user concurrent session limit to CreateWorkflowService to prevent one user from exhausting the shared DWS connection pool and LLM quotas. Addresses https://gitlab.com/groups/gitlab-org/modelops/applied-ml/code-suggestions/-/work_items/101).

How it works

Before creating a new workflow, Rails counts the user's active sessions (created + running) and rejects with HTTP 429 if the count ≥ the configured max.

Which statuses count?

Status Value Holds DWS resources? Counted?
created 0 Yes — waiting for runner ✅
running 1 Yes — gRPC stream open, LLM calls active ✅
paused 2 No — legacy, rarely used ❌
input_required 6 No — turn ended, gRPC closed ❌
plan_approval_required 7 No — waiting for human ❌
tool_call_approval_required 8 No — waiting for human ❌
idle 9 No — task done, resting ❌
finished / failed / stopped 3/4/5 No — terminal ❌

Only created and running hold DWS connections. Awaiting-input sessions have released their gRPC stream; resume creates a new token and connection. This aligns with CleanStuckWorkflowsService which only considers created/running as stuck.

Configuration

Knob Default Notes
DUO_WORKFLOW_MAX_CONCURRENT_SESSIONS ENV var 10 Tunable per-pod; requires restart
DWS has max_instance_request_concurrency: 20, source and p99=5 number of sessions per user per day on gitlab.com
duo_workflow_concurrent_session_limit feature flag disabled Actor-scoped for gradual rollout
Set ENV to 0 — Disables the limit entirely

Design decisions

  • Rails-only enforcement — every session must go through Rails to get a workflow-bound token; no bypass path exists.
  • DB count over Redis — zero new infrastructure; reuses existing in_status_group(:active) scope; no counter drift risk.
  • TOCTOU race — two concurrent creates can both pass before either commits. Acceptable for a soft abuse-prevention limit (default 10).
  • Resume bypasses the limit — when a session moves from awaiting-input back to running via ResumeWorkflowService, it does not go through CreateWorkflowService. A user can temporarily exceed the limit through resumes. This is acceptable: the limit gates new session creation (the abuse vector), not existing sessions returning from a pause.

Query plan (production clone via Database Lab)

SELECT COUNT(*) FROM duo_workflows_workflows
WHERE user_id = :user_id AND status IN (0, 1)
Aggregate  (cost=40.98..40.99 rows=1 width=8) (actual time=2.299..2.300 rows=1 loops=1)
  Buffers: shared hit=3 read=3
  ->  Bitmap Heap Scan on duo_workflows_workflows  (cost=39.46..40.98 rows=1 width=0) (actual time=2.296..2.297 rows=0 loops=1)
        Recheck Cond: (user_id = :user_id AND status = ANY ('{0,1}'::integer[]))
        ->  BitmapAnd  (cost=39.46..39.46 rows=1 width=0) (actual time=2.288..2.290 rows=0 loops=1)
              ->  Bitmap Index Scan on index_duo_workflows_workflows_on_user_id_and_created_at
                    Index Cond: (user_id = :user_id)
              ->  Bitmap Index Scan on idx_workflows_status_updated_at_id
                    Index Cond: (status = ANY ('{0,1}'::integer[]))

Time: 3.843 ms (planning: 1.464 ms, execution: 2.379 ms)
Shared buffers: hits 3, reads 3 (~48 KiB total)

How to verify locally (GDK)

Setup

  1. Checkout MR branch
  2. Run command to apply low limit to hit it easily
cd <your gdk dir>/gitlab && \ 
sed -i '' 's/MAX_CONCURRENT_SESSIONS_PER_USER = ENV.fetch.*/MAX_CONCURRENT_SESSIONS_PER_USER = 2/' \
   ee/app/models/ai/duo_workflows/workflow.rb && \
gdk restart rails-web rails-background-jobs
  1. Enable feature flag in rails console↵
# Enable feature flag (in `gdk rails console`)
Feature.enable(:duo_workflow_concurrent_session_limit)

# make sure it returns 2
Ai::DuoWorkflows::Workflow::MAX_CONCURRENT_SESSIONS_PER_USER

Verification

Trigger 3 Duo flows — the third should fail with "Concurrent session limit reached" type of error. UX with best effort and follow issue is created to ask agent observability team to take it over for improvement.

Duo Code Review Mention Duo Developer Assign Duo Developer Agentic Chat
Screenshot_2026-10-27_at_15.28.34 Screenshot_2026-10-27_at_15.24.55 Screenshot_2026-10-27_at_15.25.17 Screenshot_2026-10-27_at_14.45.37

Verify in console:

user = User.find_by(username: 'root')
Ai::DuoWorkflows::Workflow.for_user(user.id).in_status_group(:active).count

Cleanup: Ai::DuoWorkflows::Workflow.for_user(user.id).in_status_group(:active).find_each(&:stop!)

References

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist.

Edited by Junming Huang

Merge request reports

Loading
Loading