Skip to content

Context Engine

The Context Engine is the brain. It knows your codebase, your team’s history, and what matters right now.

Stuffing everything into the context window degrades AI output at ~60% utilization. A 2,000-line file read wastes 15,000 tokens when you need 10 lines. A single-file memory approach fails within a week of growth.

The solution is not “give the AI more context.” It is “give the AI the right context.”

The Context Engine assembles context from four layers, each loaded only when the command needs it.

LayerWhat it containsBudgetLoaded when
WorkingCurrent branch, PLAN.md, touched files, last verification result~15%Always
EpisodicCompressed PR summaries, review feedback, session notes. Uses Ebbinghaus decay — memories fade if not reinforced by access.~15%Most commands
SemanticModule entities (tree-sitter AST), dependency graph (PageRank-scored), ADRs, conventions, constitution, custom context~20%When AI needs codebase awareness
RetrievalZoekt code search, on-demand only~10%Only when explicitly needed

40% headroom is always reserved for AI reasoning and output. This space is never filled with context.

The token budget expands and contracts based on the task. Each command declares its context needs via a selector.

ModeBudgetUsed by
Focused40% context, 60% headroommaina commit — fast, minimal context
Default60% context, 40% headroommaina verify, maina review — balanced
Explore80% context, 20% headroommaina context — full codebase exploration

This prevents waste. A commit message does not need the full dependency graph. A code review does not need the retrieval layer. Each command gets exactly what it needs, nothing more.

Not all files are equally important. The Context Engine uses PageRank to score relevance.

  1. tree-sitter parses your codebase and extracts cross-file references (imports, function calls, type usage)
  2. A directed dependency graph is built from these references
  3. PageRank runs on the graph with a personalization vector biased toward the current task
ConditionWeight
Identifier appears in current ticket/planx10
File already in context (manually added)x50
Private/internal namesx0.1

The result: files that are central to the codebase and relevant to the current task score highest. Peripheral files with no connection to the task score lowest. The budget is spent on what matters.

Each command declares exactly which layers it needs:

CommandLayersWhy
maina commitWorking onlyFast path — just needs the diff and last verification
maina verifyWorking + SemanticNeeds codebase structure to understand the diff
maina verify --fixWorking + Semantic + EpisodicFix generation benefits from past review patterns
maina contextAll 4 layersFull exploration mode
maina prAll 4 layersComprehensive review needs everything
maina explainSemanticDependency graph visualization
maina ticketSemanticModule tagging
CommandDescription
maina contextGenerate focused codebase context (exploration mode, 80% budget)
maina context add <file>Add a file to the semantic custom context layer
maina context showShow all context layers with token counts and budget utilization

Shows each layer, how many tokens it contains, and what percentage of the budget it uses. Useful for understanding what the AI sees when you run a command.

The episodic layer uses Ebbinghaus-inspired decay. PR summaries and review feedback fade over time unless reinforced by access. Recent and frequently-accessed memories stay; old, unused memories are compressed or dropped.

This prevents the episodic layer from growing unbounded while keeping genuinely useful history available.