mixle.substrate.context module¶
Budgeted, provenanced context packets assembled from the substrate.
A ContextPacket is a task-specific view of selected substrate items:
the task, items in relevance order, rendered text, budget, and provenance for
the included evidence. A ContextBudget describes how much context a
target can accept and in what shape.
Assembly combines substrate retrieval with greedy budgeted selection. The most relevant items are packed until the budget is reached, and an optional telemetry event records the budget, usage, and number of selected items.
- class ContextBudget(max_chars=2000, max_items=20, shape='passages')[source]
Bases:
objectWhat a target can take – the DeviceSpec of context.
shapehints the rendering style.
- class ContextPacket(task, items=<factory>, scores=<factory>, budget=<factory>, used_chars=0, n_candidates=0, texts=<factory>, compressed=False)[source]
Bases:
objectA budgeted, provenanced view of the substrate assembled for one target + task.
textsholds the text actually used per item – the full item surface, or (when the packet was compressed) an extractive summary that keeps only the query-relevant sentences.preservationreceipts how much of each item’s query-relevant content survived, so compression is measured, not trusted.- Parameters:
- render(*, header=True)[source]
The assembled context string the target consumes (respecting the budget shape).
- preservation()[source]
Per item, the fraction of the task’s query terms retained in the used text (1.0 = all kept).
The receipt for compression: a value near 1.0 means the summary kept what the query cares about; a low value flags an item whose relevant content was squeezed out.
- property compression_ratio: float
Used chars / full chars over the selected items (1.0 = uncompressed).
- provenance()[source]
Where every included piece came from – ids, kinds, sources, relevance scores.
- to_knowledge_dict(*, id, project_id, target_kind, target_id=None, expected_output_schema=None, factuality=None)[source]
Return a plain dict shaped like
mixle_knowledge.contracts.ContextPacket.The exported fields cover
id,project_id,task,target_kind,target_id, token and byte budgets, evidence item identifiers, constraints, citations,expected_output_schema, andpayload. Constructing a validated pydantic object is the receiving package’s responsibility; core mixle intentionally keeps this as a dependency-free dictionary so platform contract packages can depend on core rather than the reverse.When
factualityis aFactualityReceipt, it is included inpayload["factuality"]so receivers can inspect grounding metadata before trusting the packet.
- assemble_context(substrate, task, *, budget=None, kind=None, scope=None, compress=False, telemetry=None)[source]
Assemble the best-affordable
ContextPacketfortaskfromsubstrate.Retrieves relevant items (
Substrate.search()), then packs them in descending relevance until the character budget or item cap is reached – always keeping at least the single most relevant item so a small budget still yields something. Withcompress=True, an item too large to fit whole is extractively summarized to its query-relevant sentences instead of dropped;packet.preservation()reports what was kept. Emits acontextevent when telemetry is supplied.
- compress_text(text, task, max_chars)[source]
Extractive, torch-free summary of
textkeeping the sentences most relevant totask, withinmax_chars(the standalone compressor used byassemble_context()withcompress=True).
- class ReceiverProfile(name, max_chars=2000, max_items=20, shape='passages', compress=False)[source]
Bases:
objectA named receiver’s capacity – what
assemble_for_receivers()budgets and shapes for it.A frontier LM and a local student are not the same target: the LM affords a large, prose-shaped context; the student needs a small, feature-shaped one.
ReceiverProfilenames that difference so it is set once per receiver, not re-derived ad hoc at every call site.- to_budget()[source]
Convert this receiver profile to a context budget.
- Return type:
ContextBudget
- assemble_for_receivers(substrate, task, receivers, *, kind=None, scope=None, telemetry=None)[source]
Assemble ONE task-conditioned
ContextPacketper named receiver – the concrete receiver-conditioned compression path.Two receivers reading the same substrate for the same task get genuinely different renderings: budget, shape, and, via
compress, which sentences survive. The result is not the same blob truncated to fit each consumer.- packets = assemble_for_receivers(substrate, task, [
ReceiverProfile(“frontier_llm”, max_chars=2000, shape=”passages”), ReceiverProfile(“local_student”, max_chars=200, shape=”features”, compress=True),
]) packets[“frontier_llm”].render(), packets[“local_student”].render()