mixle.substrate.retrieve module¶
Cross-kind retrieval over substrate items.
retrieve() queries the substrate across selected item kinds, applies
per-kind weights, and diversifies results so evidence can span documents,
records, artifacts, traces, and other modalities. The returned
Retrieval preserves merged relevance order, per-kind grouping, scores,
and provenance.
- class RetrievedHit(item, score)[source]
Bases:
objectOne retrieved item immutably paired with its relevance score (MXR-080-0248).
Retrievalused to carryitems/scoresas two independently-settable parallel lists, re-paired on demand viazip(self.items, self.scores)wherever a caller needed both – nothing enforced they stayed the same length, soRetrieval.provenance()’s zip could silently drop whichever tail was longer instead of raising, quietly omitting real evidence from a citation list. ARetrievedHitis the unit of construction instead: an item can never exist in aRetrievalwithout a validated, finite score attached, and – being frozen – the pairing itself cannot drift out of sync after the fact.- Parameters:
item (SubstrateItem)
score (float)
- class Retrieval(query, items=None, scores=None)[source]
Bases:
objectA planned, cross-kind retrieval result: items in merged relevance order, grouped by kind.
Construction contract (MXR-080-0248): built from aligned, validated
(item, score)pairs (seeRetrievedHit) – not independently-settable parallel lists.items/scoresstay available as read-only derived views for every existing reader (r.items,r.scores,zip(r.items, r.scores),r.scores[0]); what changed is that aRetrievalcan no longer be constructed into a state where they disagree on length, or where a score is NaN/inf.- property items: list[SubstrateItem]
Retrieved items in merged relevance order (a read-only derived view of
hits).
- property scores: list[float]
1 with
items(a read-only derived view ofhits).- Type:
Scores aligned 1
- by_kind()[source]
Group retrieved items by substrate kind.
- top(n)[source]
Return the top
nretrieved items.nsharesSubstrate.search()’s exact-non-negative-integer contract (MXR-080-0236) – no bool, no fractional value, no negative-indexing trick.
- provenance()[source]
Return compact provenance records for retrieved items.
- to_context(task=None, **assemble_kw)[source]
Budget THIS retrieval’s own ordered, weighted hits into a
ContextPacket(MXR-080-0249).Previously this copied
self.itemsinto a throwawaySubstrateand calledassemble_context(), which ran ITS OWN fresh, unweighted search over that tiny shard – discarding this retrieval’s cross-kind weights, diversification order, and scores entirely, and substituting whatever a brand-new lexical/cosine fit over just these few items happened to produce instead. A weighted retrieval that had ranked an artifact above a text item (scores 50 and 1) came back from that fresh search reordered text-before-artifact, rescored 1 and 0.5 – the weighting had silently vanished.This instead packs
self.hits– already in this retrieval’s own decided order, at their own decided scores – directly against the budget, with the same greedy, fit-checked packingassemble_context()itself uses (built on the same publicContextPacket/ContextBudget/compress_textsurface, so the fit guarantee – the default rendering is always checked against its TRUE length, never an estimate – holds here too), but never re-scoring, re-ordering, or re-searching.Accepts the same
budget/compress/telemetrykeywordsassemble_contextdid.kind/scopeare no longer accepted: there is no fresh search left for them to filter – restrictkinds=/scope=on the originalretrieve()call instead.
- retrieve(substrate, query, *, k=8, kinds=None, weights=None, diversify=True, scope=None, telemetry=None)[source]
Plan a cross-kind retrieval for
query(see module docstring).- Parameters:
k (int) – total items to return. Must be an exact, non-negative
int(MXR-080-0236, see_require_count()) –booland fractional values are rejected, and a negativekraises rather than silently slicing from the end. Validated once, up front, before any per-kind search runs, and used identically by both the diversified and flat merge paths below so a givenkmeans the same thing regardless ofdiversify.kinds (list[str] | None) – restrict to these substrate kinds (default: every kind present).
weights (dict[str, float] | None) – per-kind score multipliers (e.g.
{"artifact": 1.2}to favor deployable models). Every weight must be finite and non-negative (MXR-080-0248) – a negative weight would silently reverse that kind’s relevance ordering, and a NaN/infinite weight would destabilize the merged order; both are rejected up front, before any score is multiplied.diversify (bool) – when True (default), interleave the top hits of each kind so the result spans modalities; when False, take a flat merged top-k (whichever kind scores highest wins).
scope (str | None) – restrict to a team/access scope.
substrate (Substrate)
query (str)
telemetry (Any)
- Return type:
Retrieval