ef.diagnostics¶
Staleness diagnostics — has an indexed corpus drifted from its source?
Phase 5 wired corpus → segment → embed → vd and recorded provenance on
every vd document: source_id, source_hash and config_hash ride
in the document metadata (see ef.source_manager._RESERVED_METADATA_KEYS).
This module reads that provenance back and answers a single question — *is the
index still a faithful picture of the corpus?* — by computing the **four
staleness conditions** (ef_use_cases.md §C):
The detection is a set comparison, exactly as the design intends — the
vd collection is the single source of truth for what is indexed, the
corpus for what should be indexed, and the current
ConfigId for how:
orphan/missingpartition the symmetric difference of the two id sets;stalecompares each indexed source’s recordedsource_hashagainst a freshly recomputedcontent_hash();misconfiguredcompares each document’s recordedconfig_hashagainst the config being diagnosed.
This module is read-only — it never mutates the corpus, the collection or
the graph. Acting on a StalenessReport is ef.refresh’s job.
Example — diagnose a config whose corpus has since drifted:
>>> from ef import SourceManager
>>> sm = SourceManager({'a': 'alpha', 'b': 'beta'}, embedder='hashing')
>>> _ = sm.materialize()
>>> sm.corpus['b'] = 'beta changed' # an edit, behind the index's back
>>> sm.corpus['c'] = 'gamma' # a brand-new source
>>> del sm.corpus['a'] # and a deletion
>>> report = sm.diagnose()
>>> report.orphan, report.missing, report.stale
(('a',), ('c',), ('b',))
>>> bool(report) # True — the index has drifted
True
Functions
|
Group a |
|
The current content hash of every source in |
|
Compute the four staleness conditions of |
Classes
|
The provenance a |
|
The four staleness conditions of one config — what |
- class ef.diagnostics.IndexedSource(source_id, doc_ids, source_hashes, config_hashes)[source]¶
Bases:
objectThe provenance a
vdcollection records for one source document.A source is segmented into several documents; they all carry the same
source_idand should carry onesource_hash/config_hasheach.indexed_state()groups a collection’s documents into one of these per source — collecting the hashes as sets so a partially re-indexed source (documents disagreeing on a hash) is detectable rather than papered over.- source_id¶
the corpus key of the source these documents were cut from.
- doc_ids¶
the ids of every
vddocument belonging to the source.
- source_hashes¶
the distinct
source_hashvalues its documents record — a single-element set for a cleanly indexed source.
- config_hashes¶
likewise the distinct
config_hashvalues.
- class ef.diagnostics.StalenessReport(config, orphan=(), missing=(), stale=(), misconfigured=(), fresh=())[source]¶
Bases:
objectThe four staleness conditions of one config — what
diagnose()returns.Each field is a sorted tuple of
source_ids, so a report is deterministic and JSON-friendly.orphanandmissingare disjoint from the rest (a source absent from one side cannot also be compared); a source can be bothstaleandmisconfigured(its content changed and the config did) — the conditions are reported independently.freshis the complement: indexed, current and built by this config.Truthiness reports whether anything is wrong —
bool(report)isFalseonly for a perfectly in-sync index.>>> r = StalenessReport(config='c0', stale=('a',), fresh=('b',)) >>> bool(r), r.needs_reindexing (True, ('a',)) >>> bool(StalenessReport(config='c0', fresh=('a', 'b'))) False
- ef.diagnostics.corpus_state(corpus)[source]¶
The current content hash of every source in
corpus.The what should be indexed half of
diagnose(): a freshcontent_hash()per source — recomputed, not read from any registry, so it reflects out-of-band edits to the backing store. It matches thesource_hashSourceManagerwrites intovd(both arecontent_hashof the source with nocontent_keys).>>> sorted(corpus_state({'a': 'one', 'b': 'two'})) ['a', 'b'] >>> corpus_state({'a': 'x'})['a'] == corpus_state({'a': 'x'})['a'] True
- ef.diagnostics.diagnose(corpus, collection, config)[source]¶
Compute the four staleness conditions of
collectionagainstcorpus.The pure core of
ef’s diagnostics — no side effects. It reads what thevdcollectionrecords (indexed_state()), what thecorpuscurrently holds (corpus_state()), and partitions every source id into exactly the buckets it belongs in:orphan — in the collection, not in the corpus;
missing — in the corpus, not in the collection;
stale — in both, but the recorded
source_hash≠ the current one;misconfigured — in both, but a recorded
config_hash≠config;fresh — in both, content current and config matching.
- Parameters:
- Return type:
- Returns:
- ef.diagnostics.indexed_state(collection)[source]¶
Group a
vdcollection’s documents by source — its recorded state.Iterates the collection (a
MutableMappingofvddocuments) and folds every documentefwrote into anIndexedSourcekeyed by itssource_id. Documents with nosource_idmetadata (not written byef) are skipped. This is the what is indexed half ofdiagnose().- Parameters:
collection (
Any) – thevdcollection backing one config.- Return type:
- Returns:
a
{source_id: IndexedSource}mapping.