Code-Reducer is a command-line tool designed to generate structured developer documentation (wikis) from codebases by utilizing local Large Language Models (LLMs) via Ollama. The system employs a Map-Reduce engine to perform hierarchical synthesis, transitioning from individual file analysis to high-level architectural documentation. It supports incremental updates by tracking file state via SHA256 fingerprints.
The cmd module acts as the orchestration layer, managing the CLI lifecycle, environment validation, and dispatching execution to the underlying engine. It handles user interaction for initial configuration and enforces state transitions between project initialization and incremental updates.
- Input Acquisition: Captures CLI arguments and interactive user input via
os.Stdin. - Environment Validation: Verifies the existence of a git repository and identifies the working directory.
- State Assessment: Evaluates the current project state (e.g., via
engine.IsInitialized) to enforce valid modes (ModeInitvs.ModeUpdate). - Configuration Resolution: Loads and merges settings from disk and environmental sources.
- Execution Dispatch: Passes the validated
config.Configand executionmodeto theenginepackage. - Output Stream: Directs status updates and errors to
stdoutandstderr.
RootCmd: The primarycobra.Commandentry point.executeCommand: The central controller that orchestrates directory resolution, git verification, and configuration loading.RunSetupFlow: An interactive process that captures LLM Model IDs, Ollama endpoints, context sizes, and documentation directory paths.- Subcommands:
init: Triggers a full repository scan and initial Markdown generation.update: Triggers incremental analysis by comparing current file states against the last documented git commit.
This module manages the hierarchical resolution, validation, and persistence of system settings required for the LLM pipeline.
-
Ingestion: Aggregates data from
.code-reducer.yaml, environment variables, and CLI flags. -
Resolution: Resolves values based on the following precedence: CLI flags
$\rightarrow$ Environment variables$\rightarrow$ Configuration file$\rightarrow$ System defaults. -
Validation: Ensures numerical parameters (e.g.,
OllamaNumCtx) are integers greater than zero. -
Persistence: Serializes the
Configstruct to YAML and performs an atomic write using a temporary file and rename pattern.
Config: The primary schema containingModelID,OllamaBaseURL,OllamaNumCtx,DocsDir, and prompt templates for different synthesis stages (Module, Architecture, File Fact Consolidation).ExtractionStep: Defines specific phases of the LLM extraction pipeline, mapping aNameto a specificPrompt.
The engine performs the core logic of transforming source code into structured documentation through a hierarchical reduction process. It manages file discovery, change detection, and recursive LLM-based content reduction.
- Acquisition: The
Runnersecures a repository lock. - Discovery: Files are mapped into a hierarchical
DirNodetree structure. - Change Detection: The
Orchestratorcompares current file SHA256 hashes against theMetadataCache. "Affected" status propagates up theDirNodetree. - Hierarchical Synthesis:
- Leaf Processing: Files are processed via
Chunking. If content exceeds the context window (using a 0.75contextWindowAllocRatio), recursive reduction is applied. - Aggregation: Extracted information is aggregated from the file level up to the module and directory levels.
- Leaf Processing: Files are processed via
- Persistence: Synthesized Markdown and
MetadataCache(JSON) are written to thedocsDir.
-
Synthesizer: Traverses theDirNodetree to decide between using cached summaries or triggering new LLM extractions. -
Chunking: Implements recursive reduction for oversized segments. Recursion terminates if the output size is$\ge$ 95% of the input. -
MetadataCache: Persists SHA256 fingerprints to facilitate incremental updates. -
LLM Client: A synchronous HTTP client communicating via theapi/chatprotocol.
Defines the deployment parameters for the generated documentation wiki.
- Domain Logic: Orchestrates the Map-Reduce engine and local LLM interaction.
- External I/O: Fetches frontend assets (JS/CSS) from
cdn.jsdelivr.netandcdnjs.cloudflare.com.
Configures static analysis and code coverage reporting.
- Filtering Logic: Excludes test files, dependencies, and auxiliary assets from source analysis.
- I/O: Reads from the current directory and the
coverage.outreport.