The Context Cortex is lean-ctx's intelligence layer that goes beyond simple file caching. It connects multiple data sources, ranks content by relevance, and proactively prefetches context your AI agent is likely to need next.
Think of it as a search engine for your entire development context — code, issues, documentation, and external APIs — unified into a single query interface.
How It Works
Ingest
Content chunks from files, providers, and APIs are indexed with BM25 full-text search.
Link
Cross-source edges connect related content (e.g., a GitHub issue mentioning a file path).
Rank
Saliency scoring combines recency, access frequency, and semantic relevance.
Predict
Active inference uses tool call patterns to prefetch context before you ask for it.
Cross-Source Intelligence
When you read a file, the Cortex automatically surfaces related context from other sources. A ctx_read of auth.rs might append hints from a GitHub issue about JWT expiry or a Jira ticket about the authentication refactor.
Provider Bandit
lean-ctx uses Thompson sampling to learn which providers deliver the most useful context for your workflow. Providers that consistently return relevant results get queried more often; noisy providers are automatically deprioritized.
Thompson sampling adjusts query probability based on historical relevance scores. Higher-scoring providers receive more queries.
Active Inference
Based on your recent tool calls, the Cortex predicts what context you'll need next and prefetches it in the background. If you just read auth.rs and middleware.rs, it might pre-query GitHub issues tagged with 'authentication'.
Consolidation Engine
The consolidation engine merges provider chunks into the session cache. It deduplicates content across sources, applies token budgets, and ensures the most relevant context is always available — even if the original provider is temporarily unreachable.
| Stage | Action |
|---|---|
| Collect | Gather chunks from all active providers |
| Deduplicate | Remove identical or near-identical content across sources |
| Rank | Score by saliency (recency + frequency + semantic match) |
| Budget | Trim to free energy budget (configurable token cap) |
| Cache | Merge surviving chunks into session cache for instant retrieval |