Semantic Code Search: update status of unindexed project scenarios
What does this MR do and why?
When a project has no ready ActiveContext repository, the Semantic Code Search API returned 422 for every reason. Clients, such as the MCP Semantic Search tool, couldn't tell expected outcomes like "indexing has started" apart from real errors, so those outcomes counted toward the tool's failure rate.
Changes in this MR
This is toggled behind the Feature Flag: semantic_search_update_no_embeddings_status_codes.
The Queries::Code class returns more appropriate status codes and messages depending on the ad-hoc indexing outcome:
- not indexed due to permissions, project not eligible for semantic search, or repository already has a failed status: 422 (unchanged)
- initial indexing started or ongoing: 202
- ad-hoc indexing rate limited: 429
- ad-hoc indexing enqueue raised an error: 500 - this should ideally be raised and let the API handle it, but we need to capture the error in order to make as few changes as possible to
CodebaseSearch::Executorfor Classic Chat. Letting the exception raise can be done in a follow-up.
Semantic Code Search API
- handles 4xx results from
Queries::Code, ensures they are excluded from error SLI (unchanged)- adds
Retry-AfterandRateLimit-Nameheaders to 429 results
- adds
- handles 5xx results from
Queries::Code, ensures they are recorded in the error SLI (updated) - handles 2xx results where the project was unindexed, ie: ad-hoc indexing was performed or ongoing, ensures they are excluded from apdex SLI (new)
Classic Chat codebase search tool
No change. The CodebaseSearch::Executor still returns all "project unindexed" scenarios as failures. It will give the agent a message of A semantic search was attempted... but there was an error: {error detail}.
References
Screenshots or screen recordings
N/A
How to set up and validate locally
This was validated by testing different scenarios on the MR's base branch, on the MR branch with FF disabled, and on the MR branch with FF enabled. The table below shows the results.
| Scenario | Status code and message on base branch | MR branch, FF disabled | MR branch, FF enabled |
|---|---|---|---|
| Indexing failed | 422 (indexing_failed) - Project '1000000' has no embeddings - indexing failed |
Same as base | 422 (uses previous_indexing_failed) - Project '1000000' has no embeddings - a previous indexing for the project failed, please use another tool or context source |
initial_indexing_ongoing |
422 - Project '1000000' has no embeddings - initial indexing is still ongoing, try again in a few minutes |
Same as base | 202, same message |
not_indexed |
422 - Project '1000000' has no embeddings - the project is not indexed, please use another tool or context source |
Same as base | Same as base |
not_eligible |
422 - Project '1000000' has no embeddings - the project is not eligible for indexing, please use another tool or context source |
Same as base | Same as base |
| Ad-hoc indexing enqueue deduplicated | 422 (adhoc_indexing_trigger_failed) - Project '1000000' has no embeddings - initial indexing was attempted but could not be started |
Same as base | 202 (uses initial_indexing_ongoing) - Project '1000000' has no embeddings - initial indexing is still ongoing, try again in a few minutes |
| Ad-hoc indexing enqueue failed | 422 (adhoc_indexing_trigger_failed) - Project '1000000' has no embeddings - initial indexing was attempted but could not be started (tested in specs only) |
Same as base | 500 (adhoc_indexing_trigger_failed) - Project '1000000' has no embeddings - initial indexing was attempted but could not be started (tested in specs only) |
indexing_rate_limited |
422 - Project '1000000' has no embeddings - initial indexing was attempted but could not be started, too many indexing requests for this namespace, please try again later |
Same as base | 429, same message, with Retry-After: 3600 and RateLimit-Name: semantic_code_search_ad_hoc_indexing headers |
initial_indexing_started (ad-hoc indexing succeeds) |
422 - Project '1000000' has no embeddings - initial indexing has been started, try again in a few minutes |
Same as base | 202, same message |
Validation steps
Setup
-
Environment: GDK with
GITLAB_SIMULATE_SAAS=1, and thesemantic_code_search_ad_hoc_indexing_membership_checkfeature flag enabled. -
Project:
gitlab-duo/test(ID1000000, public). Its ActiveContext repository isAi::ActiveContext::Code::Repository.find(20). -
Users:
root: a member of thegitlab-duoroot namespace.other-duo-user: a Duo user who isn't a member ofgitlab-duo, but can read the public project.
-
Code under test:
- Base branch:
6922c95ccc0e(master, this MR's target). - MR branch:
7602f459b6f0. - Restart
rails-webandrails-background-jobsafter switching code or toggling the flag, so the API module and flag state load fresh.
- Base branch:
-
Feature flag:
- FF disabled:
Feature.disable(:semantic_search_update_no_embeddings_status_codes) - FF enabled:
Feature.enable(:semantic_search_update_no_embeddings_status_codes)
- FF disabled:
-
The API call: every scenario calls the endpoint with the user's personal access token:
curl --header "PRIVATE-TOKEN: <token>" \ "http://gdk.test:3000/api/v4/projects/1000000/search/semantic?q=authentication%20logic"
Scenarios
Run each scenario once on the base branch, once on the MR branch with the flag disabled, and once with it enabled. Run the setup in a Rails console, then make the API call. "Repository" below means Ai::ActiveContext::Code::Repository.find(20).
- Indexing failed:
repository.update!(state: :failed), then call asroot. initial_indexing_ongoing:repository.update!(state: :pending), then call asroot.not_indexed:repository.update!(state: :deleted), then call asother-duo-user.- Only the simplest case was tested: the user isn't a member of the root namespace.
- The repository stays
deleted, so no indexing is triggered.
not_eligible:- Set
repository.update!(state: :deleted)andProject.find(1000000).project_setting.update!(duo_features_enabled: false). - Clear the eligibility cache:
Rails.cache.delete("active_context_code_eligible_project_1000000"). - Call as
root, then setduo_features_enabledback totrueand clear the cache again.
- Set
- Ad-hoc indexing enqueue deduplicated:
repository.update!(state: :deleted), thengdk stop rails-background-jobs.- Enqueue
Ai::ActiveContext::Code::AdHocIndexingWorker.perform_async(1000000). While that job is queued, the endpoint's own enqueue is deduplicated (deduplicate :until_executed), soperform_asyncreturns a falsy value. - Call as
root, thengdk start rails-background-jobs. - Wait for the queued job and its indexing to finish (the repository reaches
ready).
indexing_rate_limited:repository.update!(state: :deleted).- Push the namespace counter past its limit of 10 per hour. Call
Gitlab::ApplicationRateLimiter.throttled?(:semantic_code_search_ad_hoc_indexing, scope: { namespace: Project.find(1000000).root_namespace })until it returnstrue. - Call as
root. The repository staysdeleted. - Clear the counter by deleting the
labkit:rl:{applimiter_semantic_code_search_ad_hoc_indexing:limit_semantic_code_search_ad_hoc_indexing_by_namespace:namespace:1000000}key from the rate-limiting Redis.
initial_indexing_started(ad-hoc indexing succeeds; run last):repository.update!(state: :deleted), with Sidekiq running, then call asroot.- Afterwards the repository moves out of
deletedand indexing starts (seen asembedding_indexing_in_progress).
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.
Related to #640551