# -*- coding: utf-8 -*- """rag_documents.embedding_model must name the weights that made the vectors. 2026-08-06: after migrating the corpus to bge-m3, documents still carried `embedding_model = nomic-embed-text`. Two separate causes: 1. The migration re-embedded every vector but never updated the label. 2. Ingestion recorded settings.OLLAMA_EMBED_MODEL — the *local* name — even when the vectors came from the remote provider. In cloud-only mode the local name ("bge-m3") and the remote one ("baai/bge-m3") differ, and during the cutover they were different models entirely. A stale label is worse than an absent one: that column is exactly what you consult to decide whether a document needs re-embedding, so a wrong value makes a migrated corpus look unmigrated, or hides one that genuinely is stale. """ import inspect from app.services.embed import EmbedResult, EmbedService def test_remote_results_are_tagged_with_the_remote_model(): """EmbedResult.model is the ground truth both call sites rely on.""" src = inspect.getsource(EmbedService._embed_remote_batch) assert "model=self._remote_model" in src def test_local_results_are_tagged_with_the_local_model(): """Every local backend tags with the local label, not just Ollama. _embed_local became a dispatcher when TEI was added (2026-08-06), so asserting on it alone would pass while a backend underneath it tagged vectors with the wrong model. """ for backend in (EmbedService._embed_local_ollama, EmbedService._embed_tei_batch): src = inspect.getsource(backend) assert "model=self._model" in src, f"{backend.__name__} loses the local label" def test_tei_label_matches_the_remote_label(): """TEI and OpenRouter serve the same bge-m3 weights (cosine 0.999999 on identical inputs, measured 2026-08-06). Labelling them differently would make identical vectors read as a model change and trigger a pointless re-embed of the whole corpus. """ from app.config import Settings defaults = Settings.model_fields assert ( defaults["TEI_EMBED_MODEL"].default == defaults["EMBED_REMOTE_MODEL"].default ) def test_ingest_labels_from_the_result_not_the_config(): """Ingestion must not assume the local model produced the vectors.""" from app.services import ingest src = inspect.getsource(ingest) assert "actual_embed_model = next(" in src assert '"model": actual_embed_model' in src assert '"model": self._embed_model' not in src, "config name still used as the label" def test_migration_relabels_documents_it_re_embeds(): from pathlib import Path root = Path(inspect.getsourcefile(EmbedService)).parents[2] src = (root / "app_tools" / "migrate_embedding_model.py").read_text(encoding="utf-8") assert "UPDATE rag_documents SET embedding_model" in src assert "model_name = res.model" in src, "label must come from the result" # Only documents that actually have vectors get relabelled — anything # stranded keeps its old label so it still reads as needing attention. assert "WHERE embedding IS NOT NULL" in src