Expand description
§AINL Context Compiler — LLM context-window assembly
Phase 6 of SELF_LEARNING_INTEGRATION_MAP.md.
Multi-segment, role-aware, question-aware prompt orchestration with progressive-enhancement capability tiers (heuristic → LLM-driven anchored summarization → embedding-based relevance).
§Boundary vs ainl-context-freshness
These two crates share a name prefix but solve different problems:
| Crate | Lifecycle phase | “Context” means |
|---|---|---|
ainl-context-freshness | Pre-tool execution policy gate | the agent’s knowledge of the world (repo/index state vs HEAD) |
ainl-context-compiler (this crate) | Prompt assembly / window management | the LLM’s input context window (prompt bytes about to be sent) |
ainl-context-compiler consumes ainl-context-freshness as a per-segment rank-down signal
(stale segments are ranked lower) — see relevance::HeuristicScorer.
See docs/ainl-crates-overview.md for the broader
ainl-* family map.
§Design tiers (auto-detected at runtime)
- Tier 0 — Heuristic (always available): question-token overlap × recency × freshness;
per-segment compression via
ainl_compression. - Tier 1 — Anchored summarization (M2): when a
summarizer::Summarizeris injected, older history collapses into a structuredAnchoredSummary(Factory.ai pattern). - Tier 2 — Embedding rerank (M3): when an
embedder::Embedderis injected, segments are reranked by cosine similarity to the latest user message.
Each tier auto-degrades on per-call failure; the system never blocks on optional capabilities.
§Telemetry sink (canonical pattern from §15.4)
Mirrors ainl_compression::CompressionTelemetrySink exactly. Hosts implement
ContextEmissionSink once and pass it via orchestrator::ContextCompiler::with_sink;
everything downstream just emits structured events.
Telemetry field names come from
ainl_contracts::telemetry constants prefixed CONTEXT_COMPILER_*
so dashboards, Prometheus exporters, and CI gates reference them consistently across hosts.
Re-exports§
pub use budget::BudgetPolicy;pub use capability::CapabilityProbe;pub use capability::Tier;pub use embedder::cosine;pub use embedder::Embedder;pub use embedder::EmbedderError;pub use embedder::PlaceholderEmbedder;pub use failure_recall::memory_block_for_user_query;pub use mcp_ainl_prompt::mcp_ainl_run_adapters_cheatsheet_segment;pub use mcp_ainl_prompt::MCP_AINL_RUN_ADAPTERS_CHEATSHEET;pub use metrics::ContextCompilerMetrics;pub use metrics::SegmentMetrics;pub use orchestrator::ComposedPrompt;pub use orchestrator::ContextCompiler;pub use relevance::HeuristicScorer;pub use relevance::RelevanceScore;pub use relevance::RelevanceScorer;pub use segment::Role;pub use segment::Segment;pub use segment::SegmentKind;pub use summarizer::AnchoredSummary;pub use summarizer::AnchoredSummarySection;pub use summarizer::Summarizer;pub use summarizer::SummarizerError;pub use trajectory_recap::format_trajectory_recap_lines;
Modules§
- budget
- Token budget policy.
- capability
- Capability tier auto-detection.
- embedder
- Tier 2 embedding surface (M3).
- failure_
recall - Optional failure recall → memory-block segment (
sources-failure-warningsfeature). - mcp_
ainl_ prompt - Optional compressed host hint for
mcp_ainl_ainl_runadapter registration. - metrics
- Per-call telemetry struct emitted alongside
crate::ComposedPrompt. - orchestrator
ContextCompiler::compose— orchestrates segment selection, budget allocation, compaction, and per-segment compression, returning aComposedPromptplus telemetry.- relevance
- Relevance scoring (Tier 0 heuristic; Tier 2 embedding rerank in
embedder.rs). - segment
- Prompt-window segments.
- summarizer
- Summarizer trait + structured anchored-summary types.
- trajectory_
recap - Optional trajectory recap lines for prompt injection (
sources-trajectory-recapfeature).
Enums§
- Context
Compiler Event - Structured event emitted by
ContextCompilerduring prompt composition.
Traits§
- Context
Emission Sink - Optional structured telemetry sink for context-compiler events.
Type Aliases§
- SinkRef
- Convenience type alias used throughout the crate.