Draft: Decide who a Duo run acts as before either runtime starts
What does this MR do and why?
A Duo flow that runs on Workhorse fails silently when its service account isn't a member of the project. The same flow on CI works. In GDK, workplan on Workhorse (!267832 (merged)) failed within a second: the Duo Workflow Service logged Workflow not found because the service account couldn't read the session, the session ended with drop_reason: no_status_reported, and the user got no message. Recommend Reviewers has the same problem: it now declares coding_environment: none behind recommend_reviewers_on_workhorse (!267985 (merged)), which is on in staging since 2026-11-10. This MR has to land before that flag goes to production (#636378).
Why: a run acts either as the user or as the flow's service account, but nothing decided that in one place. CI always used the service account, and only Runtimes::Ci added its membership. Workhorse decided late, in the :execute endpoint, and fell back to the user's token when the service account couldn't be used. Slack and MCP agent sessions act as the user only because their sessions have no service account.
The fix: each run's identity is decided once, before either runtime starts, and the service account's membership comes with that decision instead of with the CI runtime.
Ai::DuoWorkflows::RunIdentitysays who the run acts as: the user, or the flow's service account. Each runtime declares which identities it supports: CI the service account only, Workhorse both. A run that can't act as its service account is refused, never run as the user instead.ExecuteRunServicemakes sure the service account can act in the project before it dispatches to either runtime. It adds the membership when the caller asks for it (provision_membership:, unchanged from !260061 (merged)), and otherwise refuses with a clear error.- The
:executeendpoint reads the same decision instead of making its own.
What users see:
- Workplan on Workhorse works when its service account was removed from the project: the run adds it back, as on CI.
- Recommend Reviewers on Workhorse, started by a trigger, doesn't re-add the account (unless the flow is inherited from the group). It now fails with a logged reason instead of silently.
- A run that doesn't re-add the account (triggers, the REST resume, the workplan resume) now fails with "The flow's service account is blocked or isn't a member of this project". Before, it failed silently on Workhorse, and on CI only when the pipeline was created (
Insufficient permissions to create a new pipeline). - Slack, MCP agent sessions and anything else that acts as the user are unchanged.
This is part of step 4 ("Membership decision") of #629005.
Decisions worth a look
-
Three questions that used to be one. A run has an identity (the user or the flow's service account), a container (project, group or namespace, whose settings apply), and an executor (GitLab or the client,
FlowExecutionAuthorizer). Historically a service account always meant CI, and CI always meant a project, so project settings got attached to service-account runs. Slack was the first GitLab-executed run at group level, and it acts as the user because service accounts don't work there. This MR keeps the three apart:RunIdentityanswers only the identity, and "Allow flow execution" is its own step. -
"Allow flow execution" keeps its scope. The project setting (
duo_remote_flows_enabled) governs runs that act as the flow's service account, the scope it has had since such runs were CI only, and that:executeused since !265182 (merged). Runs that act as the user, such as Slack and MCP agent sessions, don't require it, as today. It now lives in one place,ExecuteRunService.run_refusal, which checks the identity and then this setting, and which:executealso calls. We decided to keep this scope rather than apply it to every run GitLab executes in a project (#629005 (comment 4191780129)): a run as the user can't reach anything the user can't, and web chat and the Duo CLI don't check it either. -
The identity is a fact about each run, not about the session. A session may later move between runtimes, for example from the Duo CLI (as the user) to a CI runner (as a service account).
RunIdentitytakes the runtime as input, so recording it per run (follow-up) only changes where:executereads it from. -
Refuse instead of falling back to the user. A session that should act as its service account but can't (a namespace-level session, or "Allow flow execution" is off) is refused on both runtimes. Before,
:executesilently minted the user's token instead. -
The access check is membership in any role, as today. The service account must be active and a member of the project, inherited membership included. That is the shape of
Ai::FlowTriggers::AutonomousServiceAccountEligibilityValidator, and like both existing membership services it keeps any existing role, so a role a maintainer lowered stays their choice. A blocked account is refused before anything is provisioned. Two alternatives don't work: a role floor goes against the permission conventions (roles such as Planner aren't hierarchical), and an ability check (:duo_workflow) is always false at dispatch, because a composite account has no Duo entitlement of its own until it's linked to the user. -
Access is checked before provisioning.
ServiceAccountMemberAddServiceonly sees direct project members, so adding Developer to an account that is a Maintainer through the group fails the inherited-access validation. The run checks first and adds the membership only when there is none, so it never changes a role a maintainer chose. -
Who gets re-added: same callers, now on both runtimes. Only the callers that passed
provision_membership: truebefore re-add the account:Adapters::Base#start_ci_flow,CreateAndStartWorkflowServiceandResolveToolApprovalService. Their Workhorse runs now re-add it too (workplan, tool approvals), which is the fix: the same behaviour on every runtime. Triggers on inherited consumers keep re-adding throughFlowTriggers::RunService#add_inherited_service_account_to_project. Deciding this from the session instead of per caller is still open in step 4. -
The membership is added with
Ai::ServiceAccountMemberAddService, as before. The other service,Ai::Catalog::AddServiceAccountToProjectService, bulk-inserts and skips member callbacks, validations and audit events. When a run re-adds an account a maintainer removed, the audit event matters, so picking one service stays with step 4.
Details for reviewers: what changes, by class
| Class | Change |
|---|---|
Ai::DuoWorkflows::RunIdentity (new) |
Read-only: service_account, refusal. The SUPPORTED matrix mirrors ExecuteRunService::SUPPORTED_CODING_ENVIRONMENTS. A service-account run needs a project-level session. FlowExecutionAuthorizer picks the session's service account at creation; RunIdentity checks per run that the runtime supports acting as it. |
ExecuteRunService |
run_refusal (public) checks the identity, then "Allow flow execution". prepare_run runs before dispatch on both runtimes, so before reset_messaging_turn!: run_refusal, then ensure_service_account_member (blocked check, membership check, provisioning with a member-access cache purge, membership check again). |
Runtimes::Ci |
No longer adds the membership or guards the project and service account (RunIdentity owns both). Still links composite identity. |
:execute endpoint (workflows.rb#run_service_account) |
For a session with a service account: answers 403 unless the session resolves to Workhorse, then calls run_refusal and answers 403 on a refusal. A session without one runs as the user, as before. Never adds a membership: any session owner can call it. |
Ai::Catalog::ExecuteWorkflowService |
Its Workhorse copy of the "Allow flow execution" check goes: RunIdentity checks it for every service-account run. |
One concern per commit: identity and dispatch; :execute; the access check on CI (the error text change); dropping the copied check; checking access before provisioning; membership in any role instead of a Developer floor; separating "Allow flow execution" from the identity.
Details for reviewers: behaviour changes
- CI error text. A CI run whose service account isn't a member fails before the pipeline with
:service_account_errorand the message above, instead ofInsufficient permissions to create a new pipeline. Code Review and Slack already map:service_account_errorto their own messages. - Triggers and the REST resume fail early when the account isn't a member. For Recommend Reviewers there is no user to tell, so the session is dropped and the reason is logged.
:executereturns403instead of the user's token for a session whose service account can't act: namespace-level, "Allow flow execution" off, or the session no longer resolves to Workhorse (for example after rolling backrecommend_reviewers_on_workhorsewhile a turn waits).- CI checks "Allow flow execution" before adding the membership. Before,
Runtimes::Ciadded it andStartWorkflowServicerefused afterwards. - A blocked service account is refused on both runtimes, before anything is provisioned.
- Restarts of a session whose account isn't a member fail early too.
- An account with a role inherited from the group gets no direct membership. Before, the CI path tried to add a redundant direct Developer membership, which failed for a group Maintainer.
Details for reviewers: security
Who may run what doesn't widen anywhere. "Allow flow execution" keeps exactly the scope it had: runs that act as the flow's service account.
| Change | Effect |
|---|---|
| Who gets re-added | Wider on Workhorse: the same callers that passed provision_membership: true before (CreateAndStartWorkflowService, ResolveToolApprovalService) now re-add the account on Workhorse runs too, not only on CI. A composite identity still limits the run to what the user can do |
| What role is added | Unchanged: Developer, and only when there is no membership at all |
| Existing roles | Never changed: a Reporter stays a Reporter |
| Account with a role inherited from the group | Stricter: no redundant direct membership, so removing the group membership removes access |
| "Allow flow execution" off | Stricter: refused before provisioning; before, CI added the membership and then refused |
:execute when the account can't act, or the session doesn't resolve to Workhorse |
Stricter: 403 instead of the user's token |
| Token type and the composite intersection | Unchanged: still decided by WorkflowContextGenerationService |
| Slack, MCP and other runs that act as the user | Unchanged: they never reach the new checks |
| "Allow flow execution" for runs that act as the user | Unchanged: not required, as for Slack. ExecuteWorkflowService checked it for every Workhorse run, but every caller passes a service account, so after this MR they still get it |
Details for reviewers: known limits and follow-ups
- A downgraded account still fails late. If a maintainer lowers the service account to Reporter or Guest, the check passes (any role, as today) and the role stays. The run is still blocked, because reading the session needs
duo_workflow, which only Developer has. On Workhorse it then fails in the Duo Workflow Service without a message, as every non-member case does today; on CI it fails at pipeline creation. A permission-level check needs the composite link first, which belongs with linking composite identity in one place (#629005). - Drift between dispatch and
:execute.:executeevaluates the same rule again. If its inputs change in between, for example "Allow flow execution" is turned off while a turn waits for a Sidekiq slot or a retry (!266219 (merged)), the run is refused there, and a session without a messaging surface drops without a message. It only fails closed: a session with a service account is refused, never run as the user. Recording the runtime and identity per run (follow-up in #629005) closes it. - Wrong message on
@-mentions of other flows.Adapters::GitlabDuoNote#error_textmaps:service_account_errorto the Code Review messageDCR4001for every flow, and this MR makes one more case reach it (for example a Duo Developer mention whose account was removed). Making that message flow-neutral is a separate small MR. - A refused
@GitLabDuoor Slack start leaves the sessioncreateduntil the stuck-session cleanup drops it: the known limit of !260061 (merged), whose fix belongs inAdapters::Base#with_lifecycle_hooks. - CI checks "Allow flow execution" twice, in
RunIdentityand inStartWorkflowService, until the REST start route moves ontoExecuteRunService(step 5 in #629005). - Merge order with !267832 (merged): 267832 merges first. This MR then removes the "Allow flow execution" checks it adds (
CreateAndStartWorkflowService#execution_allowed?and the extra clauses inResumeWorkplanService#can_resume?), becauseExecuteRunService.run_refusalcovers them (!268477 (comment 4199747713)). If this MR merges first, 267832 drops them instead. - Scheduled and system runs act as their service account alone.
Ai::FlowTrigger#autonomous_account_not_compositekeeps those accounts non-composite, so the "Allow flow execution" check passes for them. A spec pins that a composite one would be refused rather than run as someone else.
References
- Consolidation issue, step 4: #629005
- Builds on (merged): !260061 (merged)
- Workplan on Workhorse, where this was found: !267832 (merged) (review comment !267832 (comment 4168255967))
- Recommend Reviewers on Workhorse: !267985 (merged), #635674, #636378
- Workhorse runs acting as the flow's service account: !265182 (merged)
- In-flight work on the same dispatch: !267496, !266219 (merged)
Screenshots or screen recordings
Not applicable: backend only.
How to set up and validate locally
The steps run workplan on Workhorse with its service account removed, and a Duo Developer mention on CI with its account removed. I ran them in GDK with !267832 (merged) on top, before the latest merge of master; I haven't retested since. Once 267832 is merged, skip the temporary diff below. Until then, it is the part of 267832 the Plan button needs.
Before you start
- GDK with Duo Agent Platform working (Duo Workflow Service and AI Gateway running) and a GDK runner for Duo Agent Platform jobs.
- A project where Generate Workplan and Developer are turned on, for example
gitlab-duo/test, with an issue. Their flow consumers must not be inherited from the group, otherwise triggers re-add the account.
-
Check out the branch:
git fetch origin duo/fix/629005-run-identity-membership git checkout duo/fix/629005-run-identity-membership -
Temporarily run workplan on Workhorse:
# ee/app/models/ai/catalog/foundational_flow/workplan/definition.rb environment: "web", + coding_environment: "none", # ee/app/services/ai/duo_workflows/create_and_start_workflow_service.rb, in #start_workflow - runtime: :ci,Then restart Rails:
gdk restart rails-web rails-background-jobs. -
In
bin/rails console, define these helpers.membersets the service account's role in the project (nilremoves it; the printed level must be0, otherwise a group membership is still there), andrunshows where a session ran and who wrote the notes since it started:PROJECT = Project.find_by_full_path('gitlab-duo/test') def member(username, level) sa = User.find_by_username(username) PROJECT.members.find_by(user_id: sa.id)&.destroy! PROJECT.add_member(sa, level, immediately_sync_authorizations: true) if level sa.refresh_authorized_projects puts "#{username}: #{Project.find(PROJECT.id).team.max_member_access(sa.id)}" end def run(id) w = Ai::DuoWorkflows::Workflow.find(id) puts "#{w.workflow_definition}: #{w.status_name}, runtime #{w.last_runtime}, CI jobs #{w.workloads.count}" Note.where(noteable: w.noteable).where('created_at > ?', w.created_at).order(:id) .each { |n| puts " #{n.author.username}: #{n.note.truncate(100)}" } endThe service accounts are named
duo-generate-workplan-<group>andduo-developer-<group>. Find a session's ID under Automate > Sessions.
| # | Do | Expect |
|---|---|---|
| 1 | member('duo-generate-workplan-gitlab-duo', nil), then on the issue select Plan |
The session runs. run(<id>) shows runtime workhorse, CI jobs 0 and notes by the service account, and the account is a Developer again (member or Manage > Members). Without this MR the session fails within a second and nothing is posted. |
| 2 | member('duo-developer-gitlab-duo', nil), then comment @duo-developer-gitlab-duo Say hi in one sentence. on the issue |
No CI job starts (CI jobs 0) and the account isn't added back. The reply is the Code Review message DCR4001 (see known limits). |
When you're done, restore both accounts with member(<username>, Gitlab::Access::DEVELOPER) and revert the temporary diff.
Also run in GDK, not in this table: a Reporter account (start accepted, role kept, then fails late: the known limit), a blocked non-member (refused, nothing added), a group Maintainer without direct membership (runs, nothing added), "Allow flow execution" off (refused before any job), and the CI happy path.
Covered by specs only: Slack and MCP agent sessions (they act as the user and never reach the new checks), tool approvals on Workhorse, and the :execute refusals. CI runs the full suite. Locally I ran the specs of ExecuteRunService, RunIdentity, Runtimes::Ci, the :execute endpoint and their callers.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.