Dynamic Context Registry
Relevant source files
The following files were used as context for generating this wiki page:
The Dynamic Context Registry is a core architectural component of the M31 Autonomous (M31A) engine responsible for tracking, reconciling, and rendering changes across the codebase to provide the LLM with a high-fidelity mental model. It serves as the bridge between the physical filesystem and the semantic context required by the AI agent during different phases of the seven-phase workflow.
By indexing symbols, mapping dependency graphs, and scoring file relevance, the registry ensures that the LLM receives only the most pertinent information, thereby optimizing token usage while maintaining technical accuracy.
Architecture and Code Intelligence
The registry is primarily powered by the codeintel package, which implements a multi-language parser and indexer. It builds a structured representation of the project that includes import graphs and symbol indices.
Core Components
| Component | Description |
|---|---|
| Indexer | The primary API for project analysis, managing parsers, graphs, and indices. |
| ImportGraph | Tracks upstream and downstream dependencies between files. |
| SymbolIndex | Maps identifiers (functions, types, constants) to their locations and definitions. |
| RelevanceScorer | Uses the graph and index to rank files based on their proximity to a specific task. |
Sources: internal/integrations/codeintel/codeintel.go:17-30, README.md:270-285
Data Flow for Context Generation
The following diagram illustrates how the registry processes project data to generate context for the LLM.
The registry takes raw files and transforms them into a scored list of relevant data points for the LLM.
Sources: internal/integrations/codeintel/codeintel.go:340-360
Code Intelligence Implementation
The Indexer struct in internal/integrations/codeintel/codeintel.go provides the unified API for codebase intelligence. It supports incremental builds, using a disk cache to avoid reparsing unchanged files.
Key Indexing Functions
| Function | Purpose |
|---|---|
Build(ctx) | Triggers the parsing of source files and construction of the index/graph. |
RelevantFiles(targets, desc, topN) | Returns the top-N files most relevant to a task description. |
FormatContext(targets, desc, topN, maxBytes) | Generates an LLM-ready text summary of codebase intelligence. |
ProjectSummary(maxBytes) | Provides a high-level structural overview for the Plan phase. |
Sources: internal/integrations/codeintel/codeintel.go:42-55, internal/integrations/codeintel/codeintel.go:332-345
// internal/integrations/codeintel/codeintel.go:340-345
func (idx *Indexer) FormatContext(targetFiles []string, description string, topN int, maxBytes int) string {
idx.mu.RLock()
defer idx.mu.RUnlock()
if idx.graph == nil || idx.scorer == nil {
return ""
}
// ... logic to build context string
}
Phase-Specific Context Building
The engine tailors the context provided to the LLM based on the current workflow phase. The Dynamic Context Registry provides the data required by these phases via the ContextBuilder.
Plan Phase Context
In the Plan phase, the registry injects research findings, project summaries, and existing file schemas. This ensures the implementation plan respects existing architecture.
Sources: internal/engine/workflow/plan.go:400-430
Discuss Phase Context
The Discuss phase context includes intent classification and a project summary to help the LLM ask meaningful clarifying questions without repeating information already available in the codebase.
Sources: internal/engine/workflow/discuss.go:190-210
Sequence of Context Injection
This sequence ensures that the LLM prompt remains within context window limits while providing the most relevant technical data.
Sources: internal/engine/workflow/plan.go:391-405, internal/integrations/codeintel/codeintel.go:340-350
Relevance Scoring and Filtering
The RelevanceScorer ranks files by analyzing relationships in the ImportGraph and matching identifiers in the SymbolIndex against task descriptions.
Scoring Criteria
- Dependency Proximity: Files directly imported by or importing the target files are prioritized.
- Symbol Matching: Files containing types or functions mentioned in the user's task description receive higher scores.
- Structural Relevance: Core architectural files (e.g.,
go.mod,package.json) are included to provide configuration context.
Sources: internal/integrations/codeintel/codeintel.go:347-360, internal/integrations/codeintel/codeintel.go:415-430
Conclusion
The Dynamic Context Registry is the technical foundation that allows M31A to operate autonomously within complex codebases. By providing the LLM with filtered, scored, and semantically relevant snippets from the project, it enables accurate code generation and architecture-aware planning. Its use of incremental indexing and multi-language parsing ensures that the context remains fresh as the project evolves through the execution phases.