# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-retrieval-invariants-must-be-logged-when-an-embedding-model-is-silently-deprecated-upstream.md # Remnant Warrant: https://corpusfetch.com/c/what-retrieval-invariants-must-be-logged-when-an-embedding-model-is-silently-deprecated-upstream.warrant.json # Markdown SHA-256: 375b555aafa27cf1bfd7875d01a90a329c858d3dc639824da8ac9a13ebebfde2 # Code AST SHA-256: 4a5cd07a106b0c9b7b5a8a4f9e4357937f127ff92969a17180cb52ee50f94ca3 # Verified Classes: RetrievalVerdict, EmbeddingEnvironment, QueryContext, RetrievalManifest, EmbeddingDriftGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class RetrievalVerdict(Enum): SAFE_TO_QUERY = auto() DIMENSION_MISMATCH = auto() MODEL_DRIFT_DETECTED = auto() @dataclass(frozen=True) class EmbeddingEnvironment: model_hash: str vector_dimension: int tokenizer_version: str @dataclass(frozen=True) class QueryContext: current_model_hash: str current_dimension: int database_dimension: int @dataclass(frozen=True) class RetrievalManifest: verdict: RetrievalVerdict directive: str manifest_digest: str class EmbeddingDriftGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_query_safety( cls, saved: EmbeddingEnvironment, current: QueryContext ) -> RetrievalManifest: # Gate 1: Dimension check if current.current_dimension != current.database_dimension: return RetrievalManifest( verdict=RetrievalVerdict.DIMENSION_MISMATCH, directive=f"HALT: Query dim {current.current_dimension} != DB dim {current.database_dimension}. Re-index required.", manifest_digest=cls._hash("DIMENSION_MISMATCH") ) # Gate 2: Model weights drift if saved.model_hash != current.current_model_hash: return RetrievalManifest( verdict=RetrievalVerdict.MODEL_DRIFT_DETECTED, directive=f"HALT: Model drift. Stored vectors used {saved.model_hash}, current is {current.current_model_hash}. Re-embed.", manifest_digest=cls._hash(f"DRIFT:{saved.model_hash}:{current.current_model_hash}") ) # Gate 3: Safe digest = cls._hash(json.dumps({"model": saved.model_hash, "dim": str(saved.vector_dimension)}, sort_keys=True)) return RetrievalManifest( verdict=RetrievalVerdict.SAFE_TO_QUERY, directive="PROCEED: Embedding space is identical.", manifest_digest=digest )