fix(evidence): enforce active vector generations
This commit is contained in:
@@ -3,6 +3,44 @@
|
||||
from tht.corpus.store import CorpusStore
|
||||
|
||||
|
||||
class ActiveEvidenceSearcher:
|
||||
"""Searcher facade that enforces ACTIVE generation predicates before LIMIT."""
|
||||
|
||||
def __init__(self, corpus: CorpusStore, delegate):
|
||||
self.corpus = corpus
|
||||
self.delegate = delegate
|
||||
|
||||
def search(self, embedding, top_n=10, kinds=None, metadata_filter=None):
|
||||
if kinds != ["evidence"]:
|
||||
return self.delegate.search(embedding, top_n=top_n, kinds=kinds)
|
||||
manifest = self.corpus.active_manifest()
|
||||
if manifest is None:
|
||||
return []
|
||||
by_generation: dict[str, list[str]] = {}
|
||||
mapping = dict(manifest.metadata.get("document_generations", {}))
|
||||
for document in manifest.documents:
|
||||
generation = mapping.get(document.document_id, manifest.vector_generation)
|
||||
if generation:
|
||||
by_generation.setdefault(generation, []).append(document.document_id)
|
||||
hits = []
|
||||
for generation, document_ids in by_generation.items():
|
||||
hits.extend(self.delegate.search(
|
||||
embedding, top_n=top_n, kinds=["evidence"],
|
||||
metadata_filter={
|
||||
"vector_generation": generation,
|
||||
"document_ids": document_ids,
|
||||
},
|
||||
))
|
||||
return sorted(hits, key=lambda hit: (-hit.similarity, hit.id))[:top_n]
|
||||
|
||||
|
||||
def active_searcher(cfg, delegate):
|
||||
corpus_root = cfg.paths.artifacts.parent / "corpus"
|
||||
if not corpus_root.exists():
|
||||
return delegate
|
||||
return ActiveEvidenceSearcher(CorpusStore(corpus_root), delegate)
|
||||
|
||||
|
||||
def active_evidence_hits(store: CorpusStore, vector_store, embedding, *, limit: int):
|
||||
manifest = store.active_manifest()
|
||||
if manifest is None or manifest.vector_generation is None:
|
||||
|
||||
Reference in New Issue
Block a user