ir.base¶
Core data model for ir.
Retrieval in ir flows through four small, explicit types:
Artifact— a logical item in a corpus (a file, a skill, a package). Opaquerawpayload plusmetadata.Surface— one embeddable unit derived from an artifact. A single artifact may yield several heterogeneous surfaces (a short description, an AI-authored synopsis, a list of problem classes, body chunks). The artifact→surfaces decomposition is the job of anIndexingStrategy.IndexPlan— a strategy’s output for one artifact: thefilter_fields(hard-filterable metadata, not embedded) and the list of surfaces (embedded).Record— a stored, embedded surface (one row in the index; maps directly to avdDocument).SearchHit— a scored record returned by retrieval, with a helper to collapse multiple surface-hits of the same artifact.
The split between filter_fields (metadata you filter on) and surfaces (text you embed) is deliberate and central: good retrieval is hard metadata filtering and semantic ranking, not only embeddings.
Module Attributes
Non-embedded, hard-filterable metadata for an artifact (name, owner, tags). |
|
Metadata keys checked, in order, for a disclosure pointer — the file/dir whose contents are an artifact's body. |
Functions
|
Collapse hits to the highest-scoring surface per artifact. |
|
The ledger key under which an artifact's entry is filed. |
|
Stable, filesystem-safe id from arbitrary string parts (truncated SHA-256). |
|
Stamp source on every hit that doesn't already carry one. |
Classes
|
A logical corpus item before decomposition into surfaces. |
|
An |
|
A stored, embedded surface — one row of the index. |
|
A scored record returned by retrieval (higher score = closer). |
|
One embeddable unit derived from an artifact. |
- class ir.base.Artifact(id, raw, metadata=<factory>)[source]¶
Bases:
objectA logical corpus item before decomposition into surfaces.
- ir.base.FilterFields¶
Non-embedded, hard-filterable metadata for an artifact (name, owner, tags).
- class ir.base.IndexPlan(filter_fields=<factory>, surfaces=<factory>)[source]¶
Bases:
objectAn
IndexingStrategy’s output for one artifact.
- ir.base.POINTER_KEYS = ('skill_path', 'path')¶
Metadata keys checked, in order, for a disclosure pointer — the file/dir whose contents are an artifact’s body. A
SearchHitis a pointer + snippet (ir_09 §5):textis the snippet, the pointer is the key a resource store dereferences to the full payload. Skills stampskill_path; packages / reports / files stamppath(seeir.sources).ir.selectre-exports this for disclosure.
- class ir.base.Record(id, artifact_id, surface_kind, surface_index, text, vector, metadata=<factory>)[source]¶
Bases:
objectA stored, embedded surface — one row of the index.
- static make_id(artifact_id, surface_kind, surface_index)[source]¶
Deterministic storage id for a surface of an artifact.
surface_indexis the surface’s plan-global position — its enumeration index across all surfaces of the artifact’sIndexPlan, regardless of kind — as assigned byir.index.build(). On multi-kind strategies it therefore differs from per-kind counters likemetadata["chunk_index"](e.g.Package: thedescriptionsurface takes position 0, shiftingreadme_chunkj tosurface_indexj+1— and the offset is plan-dependent, since empty surfaces are dropped).Ids of already-built corpora are a stability contract: never re-derive a sibling’s id from a per-kind index — address siblings through the ledger via
ir.retrieve.records_for_artifact().- Return type:
- class ir.base.SearchHit(artifact_id, surface_kind, score, text, metadata=<factory>, source=None, surface_index=None)[source]¶
Bases:
objectA scored record returned by retrieval (higher score = closer).
Maps onto ir_09’s
Result:textis the snippet,scorethe rank score,metadatathe meta, andpointerthe key into a resource store (ir_09 §5).to_dict()is the serialization-clean form for a cross-process / subagent boundary (no numpy scalars leak).sourceis the corpus/source name the hit came from (Nonewhen unattributed — e.g. an ad-hoc corpus without a name). It is a first-class field, not a metadata key, becausemetadatais the strategy-owned hard-filter namespace and provenance is structural: artifact identity is only unique within a source, so any cross-source operation keys on(source, artifact_id)(seebest_per_artifact()).surface_indexis the storedRecord.surface_indexof the hit’s surface — its plan-global position among the artifact’s surfaces — so a hit can name which surface of its artifact it is (the prerequisite for sibling addressing and context expansion).Nonewhen unknown (e.g. a hand-built hit). It is not the per-kindmetadata["chunk_index"]; seeRecord.make_id()for why the two differ on multi-kind strategies.- property pointer: str | None¶
The disclosure pointer on this hit, if any (see
POINTER_KEYS).
- class ir.base.Surface(artifact_id, kind, text, granularity='document', metadata=<factory>)[source]¶
Bases:
objectOne embeddable unit derived from an artifact.
kindnames the surface type (e.g."description","synopsis","problem_class","chunk") so a query can match the right part of an artifact.granularityis a coarse hint ("document"/"chunk"/"field").metadatais surface-local (e.g. chunk offsets).
- ir.base.best_per_artifact(hits)[source]¶
Collapse hits to the highest-scoring surface per artifact.
Returns the surviving hits sorted by score (descending). Identity is
(source, artifact_id): the same id in two different sources names two different artifacts (the skills-corpus “dol” is not the packages-corpus “dol”), so cross-source input never collapses them. Single-source input (all hits sharing onesource, or allNone) behaves exactly as an id-keyed collapse. Note the raw-score comparison and the final sort assume one score scale — sound within a source; for mixed-source hits useir.retrieve.fuse_hits(), which only compares scores within a source.
- ir.base.ledger_key(artifact_id)[source]¶
The ledger key under which an artifact’s entry is filed.
The single source of truth for ledger keying —
ir.index.build()writes entries under this key and readers (e.g.ir.retrieve.records_for_artifact()) resolve them with it, so the two can never drift apart.- Return type:
- ir.base.storage_key(*parts)[source]¶
Stable, filesystem-safe id from arbitrary string parts (truncated SHA-256).
- Return type:
- ir.base.tag_source(hits, source)[source]¶
Stamp source on every hit that doesn’t already carry one.
Existing tags win: a hit already attributed to a corpus keeps that attribution (so re-tagging under a different registry key cannot double-count one corpus as two sources). A
Nonesource is the untagged pseudo-source — hits pass through unattributed.