Skip to main content

Module context

Module context 

Source
Expand description

The deterministic context compiler (EPIC-P-070): classify, dedup, and pack caller-supplied context fragments under a token budget — no LLM, no cloud, every decision auditable. Gated behind the default context feature. The deterministic context compiler (EPIC-P-070).

Classifies, deduplicates, and packs caller-supplied context fragments under a token budget — no LLM, no network, no clock: the pipeline is a sequence of pure stages (chunk → classify → dedup → score → pack → assemble), so the same CompileRequest always produces the same CompiledContext, byte for byte.

Invariants:

  • Budget: the assembled content never exceeds the request’s token budget — packing accounts per-piece estimates plus joiner costs priced by the injected estimator, which bounds the whole-text estimate for a superadditive estimator (the default rounds every piece up).
  • Provenance: every input fragment gets exactly one ContextDecision with a stable rule id and a content hash; every fragment stays addressable via a content-addressed ctx://source/<content_hash> handle (immune to caller-id collisions).
  • Nothing critical is silently lost: content that cannot fit becomes a RetrievalHandle; losing preserve-classified content raises CompiledContext::risk to FidelityRisk::High; a critical fragment is never sacrificed to near-deduplication, and a duplicate of a twin that did not emit verbatim keeps its own handle and risk.

Memory-backed fragment selection, persisted working contexts, and compilation events layer on top in the persistence-gated bridge (US-002); MCP and Node expose the same types unchanged (US-003).

Re-exports§

pub use chunk::chunk_text;
pub use chunk::ChunkBoundary;
pub use chunk::ChunkPolicy;
pub use chunk::TextChunk;
pub use estimator::DynTokenEstimator;
pub use estimator::HeuristicEstimator;
pub use estimator::TokenEstimator;
pub use insights::CompilationInsights;
pub use insights::ModelPricing;
pub use insights::PricingTable;
pub use model::CompilePolicy;
pub use model::CompileRequest;
pub use model::CompiledContext;
pub use model::CompiledSection;
pub use model::ContextAction;
pub use model::ContextDecision;
pub use model::ContextDecisionRef;
pub use model::ContextFact;
pub use model::ContextFragment;
pub use model::ContextSavings;
pub use model::FidelityRisk;
pub use model::MemoryScope;
pub use model::RetrievalHandle;
pub use model::SectionKind;
pub use model::SourceReference;
pub use model::WorkingContext;

Modules§

chunk
Deterministic text chunking for the packing stage.
estimator
Pluggable token estimation, with a deterministic char-class default.
insights
Savings accounting: tokens (always) and money (only when a pricing table is injected).
model
Data model of the context compiler: the request/response value types.

Structs§

ContextCompiler
The deterministic context compiler. Build one with a policy, optionally inject an estimator and a pricing table, then compile.
DeterministicReranker
The first shipped Reranker: re-orders a fused candidate pool by deterministic lexical overlap with the query, original (fused) order as the tie-break. Never invents or drops ids, never calls a model — safe to wire into recall_fused_reranked where a cross-encoder would be overkill or non-reproducible.

Functions§

fragment_id
The stable, content-addressed id of a fragment whose caller supplied none — the crate’s one id scheme (FNV-1a 64), also used as every decision’s content hash and as the tail of every ctx://source/<hash> handle.