Fix v4 code suggestion requests sent to the AI Gateway
What does this MR do and why?
This MR fixes three problems in what Rails sends to IDE clients and to the AI Gateway /v4/code/suggestions endpoint. That endpoint handles code completions behind the code_completion_v4_endpoint flag, and code generations always.
- Since completions moved to v4 (!239271 (merged), rolled out on GitLab.com in July 2026), the IDE's
context[]array (open tabs) reached the gateway only as a top-level key that v4 ignores. Context was silently dropped from every completion proxied through Rails. Each context item now becomes acode_contextprompt component, placed after thecode_editor_completioncomponent. The gateway already consumes these components. - v4 requests no longer include the raw client body (
current_file,telemetry,choices_count, and the client-sentmodel_provider/model_name) next toprompt_components. This duplication doubled the file content in every request and could push a large-context request over the 600 kB body limit, causing a 413 error. v2 requests are unchanged. POST /code_suggestions/direct_accessnow also returnsmodel_metadatain the v4 shape (provider,feature_setting,identifier), but only when a model is pinned through model selection. GitLab default returns nomodel_metadata, so a v4 direct connection lets the gateway'sdefault_modelsdecide the model. Self-hosted settings are excluded because their metadata carries the model API key, which must not reach the IDE. The existingmodel_detailsfield is unchanged, so current v2 IDE clients see no difference.
Items 1 and 2 affect every completion that goes through Rails: all self-managed instances (direct connection is refused for online cloud licenses), Amazon Q customers, and GitLab.com fallback traffic. Item 3 prepares the language server's move of direct connections from /v2/completions to /v4/code/suggestions, where a pinned model can only be expressed as model_metadata.
Merge this MR after the AI Gateway MR gitlab-org/modelops/applied-ml/code-suggestions/ai-assist!7024 (merged), which raises the per-item code_context limit to 500 kB. Before that gateway change merges, a context item over 100 kB is rejected by the gateway with a 422 error.
References
- #547122 (closed) (epic)
- #598413 (closed) (v4 completions rollout item)
- gitlab-org/modelops/applied-ml/code-suggestions/ai-assist#2736 (Vertex Codestral traffic split, motivates item 3)
- gitlab-org/modelops/applied-ml/code-suggestions/ai-assist!7024 (merged) (AI Gateway MR, must merge first)
Screenshots or screen recordings
Backend only. No UI change.
How to set up and validate locally
- Run a gateway at or after gitlab-org/modelops/applied-ml/code-suggestions/ai-assist!7024 (merged). Check out that branch and run
gdk restart. - Enable the flag:
Feature.enable(:code_completion_v4_endpoint). - Send
POST /api/v4/code_suggestions/completionswith a PAT withapiscope and the headerX-Gitlab-Language-Server-Version: 9.16.0, with this body:This returns 200 with a suggestion.{"current_file":{"file_name":"main.py","content_above_cursor":"import math\n\ndef","content_below_cursor":"\n"},"intent":"completion","stream":false,"context":[{"type":"file","name":"util.py","content":"def sub(a, b):\n return a - b\n"}]} - Repeat with a 200000-character
contentvalue. This returns 200. On master the context never reaches the gateway as a component, and against a gateway without the 500 kB change the request returns 422. - In the request spec, the Workhorse body for v4 now contains only
prompt_components, andmodel_metadatawhen a model is pinned or self-hosted. - Send
POST /api/v4/code_suggestions/direct_access. With GitLab default the response hasmodel_detailsand nomodel_metadata. Pin a model for code completions in the instance model selection settings and repeat: the response addsmodel_metadatawithprovider: gitlab,feature_setting: code_completionsand the pinnedidentifier, whilemodel_detailsis unchanged.
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.
RuboCop is clean on all 11 changed files. The five touched spec files (ee/spec/lib/code_suggestions/model_details/code_completion_spec.rb, ee/spec/lib/code_suggestions/prompts/code_completion/ai_gateway_code_editor_completion_spec.rb, ee/spec/lib/code_suggestions/tasks/code_completion_spec.rb, ee/spec/lib/code_suggestions/tasks/code_generation_spec.rb, ee/spec/requests/api/code_suggestions_spec.rb) pass: 337 examples, 0 failures. End-to-end tests on GDK against the gateway branch confirm: a completion with a small context item returns a real suggestion, a completion with a 207 kB context item returns a real suggestion, and a non-stream generation still works.