Add a cascading setting to turn off Code Suggestions

What does this MR do and why?

Customers with GitLab Duo Agent Platform on have no way to turn off Code Suggestions on its own. The only group or instance control that stops it is GitLab Duo availability, which also turns off agents, flows and Chat. Code Suggestions draws on GitLab Credits for every suggestion, so customers who see low acceptance want to stop that spend and keep the rest of Duo.

This MR adds a Code Suggestions on/off setting that works like the other cascading Duo settings:

  • Top-level group Owners set it, and turning it off locks it for subgroups and projects. Projects cannot override it.
  • On GitLab Self-Managed and Dedicated, administrators set it for the instance. Instance-level values are stripped on GitLab.com, like the sibling settings. An instance setting that is off is always locked, so a group cannot turn Code Suggestions back on.
  • When it is off, the Code Suggestions API refuses completions, direct-connection tokens and the project enabled check with a 403 and a distinct code_suggestions_disabled error, so IDE clients can tell the user why instead of showing a generic error. IDE clients show that reason only with the Language Server change in gitlab-org/editor-extensions/gitlab-lsp!4768 (merged). Without that change, the clients show the generic API error.
  • A project transfer takes the value from the new group, or the instance value in a personal namespace, because a project has no control of its own.
  • Group changes produce the duo_code_suggestions_enabled_updated audit event. The value is exposed in the group and application settings REST APIs and as Project.duoCodeSuggestionsEnabled in GraphQL, which the Language Server will read to gate suggestions per project.

Enforcement lives in the three policies that already hold access_code_suggestions, next to the Duo availability rules. The API only chooses the error text after the policy denies. The instance check runs in the endpoint before block, because the global policy denial would otherwise answer with a plain 401 first.

On GitLab.com, a request can name a project outside the group that pays for the user, for example a personal project. ProjectPolicy also refuses that request when the group that the usage is billed to has the setting off. A group that turns the setting off then stops the usage of its users in all projects, not only in its own. This rule costs one more namespace_settings query for each completion in the user's own group. For a project outside that group, the lookup of the billed group runs a second time, which is three more queries.

The project column is write-cascaded like duo_features_enabled, so the completions request reads one loaded attribute plus one indexed lock lookup. Measured on a GDK: one extra namespace_settings query per completion for a project in a group, out of 13 for the whole policy check. Project creation copies the value from the group, which adds two indexed namespace_settings lookups.

On Self-Managed, completions and direct_access are checked against the instance value only, because the policy subject is :global there, same as Duo availability for Code Suggestions today. A group that turns the setting off still cascades to its projects, which the enabled endpoint and the GraphQL field report. This is documented in the developer availability doc and is a follow-up decision.

Direct-connection tokens live for one hour and cannot be revoked. A token minted before the change keeps working until it expires. The UI toggle (group and admin pages) and the Language Server change follow in separate MRs.

Database

EE::Projects::TransferService#sync_duo_code_suggestions_setting runs once for each project transfer, after the transfer transaction. It reads the setting of the new group and writes one project row by primary key. A transfer to a personal namespace has no read, because the instance value comes from the cached application settings.

1. Read the setting of the new group (existing cascading reader)

SELECT "namespace_settings".* FROM "namespace_settings" WHERE "namespace_settings"."namespace_id" = <group_id> LIMIT 1

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/93951/commands/199869 (Index Scan on namespace_settings_pkey, 1 row, 7.6 ms cold, buffers: 0 hit / 5 read)

2. Write the project value

UPDATE "project_settings" SET "duo_code_suggestions_enabled" = <value> WHERE "project_settings"."project_id" = <project_id>

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/93951/commands/199871 (Index Scan on project_settings_pkey, 1 row, 35.1 ms cold, buffers: 28 hit / 32 read / 9 dirtied)

The column does not exist in production yet. The clone got it first with the ALTER TABLE statement of this merge request's migration (26 ms).

3. Locked ancestor lookup of the cascading reader

The reader runs this for a project in a group. It is on the completions path through ProjectPolicy, and on project creation and transfer. The same query runs for a subgroup.

SELECT "namespace_settings"."namespace_id", "namespace_settings"."lock_duo_code_suggestions_enabled", "namespace_settings"."duo_code_suggestions_enabled"
FROM "namespace_settings"
WHERE "namespace_settings"."namespace_id" IN (<ancestor_namespace_ids>)
  AND "namespace_settings"."lock_duo_code_suggestions_enabled" = TRUE
LIMIT 1

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/95099/commands/200921 (Index Scan on namespace_settings_pkey, three ancestors, 1 row, 0.8 ms warm, buffers: 5 hit / 0 read. One ancestor was set to off and locked on the clone.)

4. Ancestor value lookup for a subgroup

The reader runs this when a subgroup has no value of its own.

SELECT "namespace_settings"."duo_code_suggestions_enabled"
FROM "namespace_settings"
JOIN unnest(ARRAY[<ancestor_namespace_ids>]) WITH ORDINALITY t(namespace_id, ord) USING (namespace_id)
WHERE (duo_code_suggestions_enabled IS NOT NULL)
ORDER BY t.ord
LIMIT 1

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/95099/commands/200922 (Nested Loop, Function Scan on unnest, Index Scan on namespace_settings_pkey, 1 row, 7.8 ms, buffers: 7 hit / 3 read)

References

Screenshots or screen recordings

No UI change in this MR.

How to set up and validate locally

  1. Turn the setting off for a group: PUT /api/v4/groups/:id with duo_code_suggestions_availability=false. The response shows duo_code_suggestions_enabled: false and lock_duo_code_suggestions_enabled: true.
  2. After the cascade worker runs, POST /api/v4/code_suggestions/enabled with a project of that group returns 403, and project.duoCodeSuggestionsEnabled is false in GraphQL.
  3. On a GitLab.com-mode instance, POST /api/v4/code_suggestions/completions for that project returns 403 with "error": "code_suggestions_disabled".
  4. Turn the instance setting off: PUT /api/v4/application/settings with duo_code_suggestions_enabled=false. The response shows lock_duo_code_suggestions_enabled: true. Completions, direct_access, enabled and connection_details return the same 403 body.
  5. Set both back to on. Requests return 200 again.

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.

Edited by Mike Wronski

Merge request reports

Loading
Loading