Skip to content

fix(ai): a rag collection records its embedding model, a mismatch fails instead of wiping the collection, and ranking keeps the cosine score #1066

Closed

Description

@dan-petty

Problem

Three defects let the RAG index silently go wrong when the embedding model changes. Lines are at release/v0.2.25 (e3ff98c).

1. No collection records which model made its vectors. A point's payload holds the file, lines, content, language, category, project, symbols, metadata and content hash, but no model (src/devops_cli/ai/rag/indexer.py:212-228). The collection itself records only its vector size. The planned switch from bge-m3:latest to qwen3-embedding:0.6b (#481) keeps the size at 1024. After it, queries embedded by one model would be compared with vectors from the other, with no error, and the code would never notice. Today the only safeguard is to remember devops ai rag clear --force before re-indexing (commands/rag.py:510-552).

2. A search can delete the index.

  • When a query's dimension does not match the collection, search_points calls _handle_dim_mismatch (ai/rag/qdrant.py:462-468).
  • That handler deletes the collection and creates it again, empty, at the query's dimension (:472-489).
  • It logs a warning and returns no results. Every later search finds nothing until someone re-indexes.
  • So a read path wipes stored data. Any lookup whose model resolves to another dimension does this, from a review, verification, chat or rag query. Examples are a misconfigured ai.tasks.embedding, or an engine that falls back to the dimension table because it could not probe the model (ai/rag/embeddings.py:471-475, where qwen3-embedding is wrongly 768).
  • ensure_collection does the same on the index path (qdrant.py:327-337): it deletes a collection whose size differs and creates it again.

3. Ranking throws away the cosine score.

  • With hybrid search, reciprocal_rank_fusion writes the fused rank value over each result's score (ai/rag/retriever.py:232-235; k = 60).
  • The reranker then reads that value as its vector signal, weighted 0.5 (ai/rag/reranker.py:193-195; DEFAULT_RERANKER_VECTOR_WEIGHT, config/defaults.py:354).
  • The fused value is at most 2/61 ≈ 0.033, so the whole dense-plus-lexical rank term adds at most about 0.016, and the dense rank's own share is at most about 0.008. The raw cosine acts only through Qdrant's score_threshold gate and the size of the candidate pool.
  • devops ai rag query shows the fused or reranked score, not the cosine, so it cannot be used to calibrate the threshold for a new model.

Source: .data/slow-tier-plan-20261003.md, section 4 ("RAG index integrity"), and its verification pass, which re-read each line. The lines above were checked again at e3ff98c.

Acceptance criteria

  • Every point the workspace indexer writes (devops ai rag index and index-kb) carries embedding_model, the model name as configured, in its payload.

  • Before indexing into a collection that already holds points, the indexer reads one point's embedding_model. If it names another model, or the collection's size differs from the model's, indexing fails with exit 1 and a message naming both models and the fix (devops ai rag clear --force, then devops ai rag index --force). --force does not recreate the collection by itself. Only clear deletes it.

  • A search whose dimension does not match never deletes or recreates the collection. _handle_dim_mismatch goes. The search raises an EmbeddingsError marked permanent that names the collection, both dimensions and the same fix. RAG treats it like an unserved model: it stops for the run, with one warning (fix(ai): a failing embedding model pauses rag lookups instead of ending rag for the run, and each embedding model gets its own prompts, cache keys, dimension and bounded chunks #1065's permanent error).

  • A search also checks the model a collection records, once per process. A query model that differs from it raises the same error before any search runs.

  • SearchResult keeps the cosine from Qdrant in its own field (for example vector_score), and fusion writes its value to a separate field. The reranker's vector signal reads the cosine. devops ai rag query prints the cosine beside the final score.

  • Tests with a fake Qdrant client:

    • a collection holding bge-m3:latest points refuses an index run with qwen3-embedding:0.6b, and the collection keeps its points;
    • a 768-dimension query against a 1024-dimension collection raises, and the fake client records no delete or create call;
    • two results with the same fused rank but cosines of 0.9 and 0.4 come out of the reranker in cosine order.

    The first two fail on the current code.

  • The tests run offline, call no Qdrant, gateway, Ollama or model, and each runs in under 1 s.

  • changelog.d/1066.md records the fix under ### Fixed. CHANGELOG.md is not edited.

  • uv run devops ci passes.

  • (person-run) After the next full index: devops ai rag status shows the point counts, and one point fetched from each collection carries embedding_model. Then set ai.tasks.embedding.model to another model and run devops ai rag query "<path> <symbol>". It fails with the message, and the counts are unchanged.

Key questions

Strategic alignment

Depends on: none.
Order: before #481's switch to qwen3-embedding:0.6b, or that switch clears the collections by hand. The RAG client fixes (#1065) edit investigator.py, embeddings.py and the indexer's batch loop. This item edits qdrant.py, the indexer's point payload and retriever.py/reranker.py. Whichever lands second rebases.
Related:

Review redesign placement (2026-10-03)

Activity

  1. added
    type/bug
    scope/ai
    priority/p2-medium
    on Oct 3, 2026
  2. added
    priority/p1-high
    and removed
    priority/p2-medium
    on Oct 3, 2026
  3. added this to the v0.2.34 milestone on Oct 10, 2026
  4. dan-petty-bot commented on Oct 10, 2026

    @dan-petty-bot
    Collaborator

    Moved to v0.2.34: v0.2.34 started with fewer than its 16 items and was topped up to 16 from Ready items, critical fixes and P0 features first. (devops roadmap reprioritize)

  5. dan-petty commented on Oct 11, 2026

    @dan-petty
    OwnerAuthor

    Merged

  6. added a commit that references this issue on Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/aitype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions