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::Executor for 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-After and RateLimit-Name headers to 429 results
  • 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 the semantic_code_search_ad_hoc_indexing_membership_check feature flag enabled.

  • Project: gitlab-duo/test (ID 1000000, public). Its ActiveContext repository is Ai::ActiveContext::Code::Repository.find(20).

  • Users:

    • root: a member of the gitlab-duo root namespace.
    • other-duo-user: a Duo user who isn't a member of gitlab-duo, but can read the public project.
  • Code under test:

    • Base branch: 6922c95ccc0e (master, this MR's target).
    • MR branch: 7602f459b6f0.
    • Restart rails-web and rails-background-jobs after switching code or toggling the flag, so the API module and flag state load fresh.
  • Feature flag:

    • FF disabled: Feature.disable(:semantic_search_update_no_embeddings_status_codes)
    • FF enabled: Feature.enable(:semantic_search_update_no_embeddings_status_codes)
  • 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).

  1. Indexing failed: repository.update!(state: :failed), then call as root.
  2. initial_indexing_ongoing: repository.update!(state: :pending), then call as root.
  3. not_indexed: repository.update!(state: :deleted), then call as other-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.
  4. not_eligible:
    • Set repository.update!(state: :deleted) and Project.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 set duo_features_enabled back to true and clear the cache again.
  5. Ad-hoc indexing enqueue deduplicated:
    • repository.update!(state: :deleted), then gdk 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), so perform_async returns a falsy value.
    • Call as root, then gdk start rails-background-jobs.
    • Wait for the queued job and its indexing to finish (the repository reaches ready).
  6. 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 returns true.
    • Call as root. The repository stays deleted.
    • 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.
  7. initial_indexing_started (ad-hoc indexing succeeds; run last):
    • repository.update!(state: :deleted), with Sidekiq running, then call as root.
    • Afterwards the repository moves out of deleted and indexing starts (seen as embedding_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

Edited by Pam Artiaga

Merge request reports

Loading
Loading