Code-Reducer employs a hierarchical Map-Reduce architecture to transform source code into structured documentation. The system transitions from granular file analysis to high-level architectural synthesis through a recursive reduction process.
- Input/Interface: The system accepts local source code within a git repository, user input via
os.Stdin, configuration via.code-reducer.yaml, environment variables, and CLI flags. - External Dependencies:
- LLM Provider: Communicates with local LLM instances via the
api/chatprotocol (Ollama). - Frontend Assets:
mkdocs.ymlfacilitates documentation deployment by fetching assets fromcdn.jsdelivr.netandcdnjs.cloudflare.com.
- LLM Provider: Communicates with local LLM instances via the
- Output: Produces structured Markdown files in a designated
docsDirand aMetadataCachein JSON format.
The cmd module serves as the system entry point, managing the execution lifecycle:
- Validation: Verifies the working directory is a git repository.
- State Enforcement: Uses
engine.IsInitializedto switch betweenModeInit(full scan) andModeUpdate(incremental analysis). - Dispatch: Resolves the configuration and hands off execution to the
engine.
The config module handles the hierarchical resolution of settings:
-
Precedence: CLI Flags
$\rightarrow$ Environment Variables$\rightarrow$ Configuration File$\rightarrow$ Defaults. -
Schema: Manages parameters for LLM interaction (e.g.,
OllamaNumCtx) and prompt templates used during theExtractionStepphases. - Persistence: Ensures atomic writes to disk using a temporary file and rename pattern.
The core engine executes the hierarchical synthesis via a DirNode tree structure:
Change Detection & Propagation:
- Leaf Level: The
Orchestratorcompares current file SHA256 hashes against theMetadataCache. - Propagation: If a change is detected, the "affected" status propagates up the
DirNodetree from the file level to the directory and module levels.
Hierarchical Synthesis (The Map-Reduce Cycle):
-
Map (Extraction):
- Files are processed through the
Synthesizer. -
Chunking: If content exceeds the context window (governed by a
0.75 contextWindowAllocRatio), the system triggers recursive reduction. -
Recursion Termination: The
Chunkingprocess terminates when the output size is$\ge$ 95% of the input size.
- Files are processed through the
-
Reduce (Aggregation):
- Extracted information is aggregated from individual files
$\rightarrow$ Modules$\rightarrow$ Directories$\rightarrow$ High-level Architecture.
- Extracted information is aggregated from individual files
| Component | Responsibility | Primary Data/Format |
|---|---|---|
cmd |
Lifecycle & Dispatch | CLI Args, config.Config |
config |
Parameter Resolution | YAML, Environment Variables |
engine |
Hierarchical Synthesis | DirNode Tree, MetadataCache (JSON) |
engine/LLM |
Content Reduction | HTTP api/chat |
Output |
Documentation | Markdown |