Documentation

Context Cortex

How lean-ctx's Context Cortex architecture enriches AI context with cross-source intelligence, relevance ranking, and active inference.

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

01

Ingest

Content chunks from files, providers, and APIs are indexed with BM25 full-text search.

02

Link

Cross-source edges connect related content (e.g., a GitHub issue mentioning a file path).

03

Rank

Saliency scoring combines recency, access frequency, and semantic relevance.

04

Predict

Active inference uses tool call patterns to prefetch context before you ask for it.

ctx_read
BM25 Index
Saliency
Hints

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.

[x] [-] [o] ctx_read with cross-source hints
ctx_read(path: "src/auth.rs", mode: "map")
exports: authenticate(), validate_jwt(), refresh_token()
deps: jsonwebtoken, chrono, serde
tokens: 2,400 → 120 (95% saved)
cross-source hints:
github#142 JWT expiry not handled in refresh flow
jira/AUTH-89 Refactor auth middleware for SSO support

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.

GitHub 0.85
Jira 0.62
Internal API 0.34

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'.

[x] [-] [o] Active inference prefetch
# Recent tool calls:
ctx_read src/auth.rs
ctx_read src/middleware.rs
ctx_search "authenticate"
cortex prediction:
prefetch github issues tagged "authentication"
prefetch jira sprint items for "auth"
preload src/routes/login.rs

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.

StageAction
CollectGather chunks from all active providers
DeduplicateRemove identical or near-identical content across sources
RankScore by saliency (recency + frequency + semantic match)
BudgetTrim to free energy budget (configurable token cap)
CacheMerge surviving chunks into session cache for instant retrieval