74 lines
3.0 KiB
Python
74 lines
3.0 KiB
Python
"""Runtime Evidence lookup bound to the atomically active corpus generation."""
|
|
|
|
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:
|
|
return []
|
|
document_generations = dict(manifest.metadata.get("document_generations", {}))
|
|
active_documents = {document.document_id for document in manifest.documents}
|
|
hits = vector_store.search(["evidence"], embedding, limit=limit, kinds=["evidence"])
|
|
return [
|
|
hit for hit in hits
|
|
if hit.metadata.get("document_id") in active_documents
|
|
and hit.metadata.get("vector_generation")
|
|
== document_generations.get(hit.metadata.get("document_id"), manifest.vector_generation)
|
|
]
|
|
|
|
|
|
def resolve_evidence_file(
|
|
store: CorpusStore, evidence_id: str, *, materialized_root=None,
|
|
) -> str:
|
|
manifest = store.active_manifest()
|
|
if manifest is None:
|
|
return ""
|
|
for document in manifest.documents:
|
|
frontmatter = document.metadata.get("frontmatter", {})
|
|
identifiers = {document.document_id, document.source_id, str(frontmatter.get("id", ""))}
|
|
if evidence_id in identifiers:
|
|
root = materialized_root or (store.root / "runtime")
|
|
filename = document.document_id.removeprefix("doc:") + ".md"
|
|
path = store.materialize_document(document.document_id, root / filename)
|
|
return str(path) if path else ""
|
|
return ""
|