The internal/config package manages the lifecycle, hierarchical resolution, and persistence of configuration settings. It enables the merging of settings from multiple sources into a unified Config struct to direct the LLM-based pipeline.
Data Flow Pipeline:
- Ingestion: Aggregation of data from the local filesystem (
.code-reducer.yaml), environment variables, and CLI arguments. - Resolution: Hierarchical merging based on precedence: CLI flags > Environment variables > Configuration file > System defaults.
- Validation: Numerical parameters (e.g.,
OllamaNumCtx) are validated to ensure they are non-zero and valid integers. - Output: Returns a validated
*Configpointer for LLM prompt construction and execution logic. - Persistence: Configuration changes are serialized to YAML and written via an atomic rename pattern.
The primary operational schema for the system.
| Field | Type | Description |
|---|---|---|
ModelID |
string |
Identifier for the LLM model. |
OllamaBaseURL |
string |
Base endpoint for the local Ollama API. |
OllamaNumCtx |
int |
Context window size for the LLM. |
DocsDir |
string |
Target directory for generated documentation. |
SystemPrompt |
string |
Primary system message for the LLM. |
ModuleSynthesisPrompt |
string |
Prompt template for module-level synthesis. |
ArchitecturePrompt |
string |
Prompt template for architectural analysis. |
FileFactConsolidationPrompt |
string |
Prompt template for consolidating file facts. |
ExtractionSteps |
[]ExtractionStep |
Sequence of extraction phases for the pipeline. |
Ignore |
[]string |
Paths or patterns excluded from analysis. |
Defines a specific phase in the multi-step LLM extraction process.
| Field | Type | Description |
|---|---|---|
Name |
string |
Identifier for the extraction phase. |
Prompt |
string |
Specific prompt utilized for this phase. |
The ResolveConfig function determines the final state of the Config instance using the following precedence:
- Command-line Flags:
modelIDFlag,numCtxFlag. - Environment Variables:
CODE_REDUCER_MODEL_ID,OLLAMA_BASE_URL,OLLAMA_NUM_CTX. - Configuration File: Values from
.code-reducer.yaml. - System Defaults: Hardcoded values.
Constraints:
- Numerical Validation:
OllamaNumCtxmust be a valid integer and greater than zero. Non-zero/non-integer environment variables result in an error. - Deduplication: The
Ignoreslice is deduplicated during resolution. - Default Logic: If
ExtractionStepsis empty in the config file, the system defaults to theDefaultExtractionStepssequence.
ConfigExists: Employsos.Statto check for the presence of the configuration file. All errors are swallowed; returnsfalseon error.LoadConfig: Utilizesos.ReadFileandyaml.Unmarshal. Errors are wrapped and returned.SaveConfig: Implements an atomic write pattern:yaml.Marshalthe struct to bytes.os.CreateTempa temporary file in the target directory.- Write data and invoke
os.Syncfor durability. os.Chmodto set permissions.os.Renameto atomically replace the target file.
- Note: The deferred
os.Removedoes not contain arecover().
The internal/engine module is an orchestration engine using a hierarchical Map-Reduce pattern to transform source code into structured documentation via LLMs. It supports ModeInit (initial generation) and ModeUpdate (incremental updates).
Data Flow Lifecycle:
-
Initialization: The
Runneracquires an exclusive repository lock viasecurity.AcquireLock. -
Discovery: The
Treelogic converts file paths into a hierarchicalDirNodestructure. -
Change Detection: The
Orchestratorcompares SHA256 hashes againstMetadataCache. Changes propagate upward through theDirNodetree to mark nodes as "affected." -
Hierarchical Synthesis:
-
Leaf Processing: Files undergo
Chunking(recursive reduction if context limits are exceeded) followed by LLM extraction. -
Aggregation: Results aggregate from file
$\rightarrow$ module$\rightarrow$ directory levels.
-
Leaf Processing: Files undergo
-
Persistence: Writes synthesized Markdown and the
MetadataCache(JSON) to thedocsDir.
- Runner (
runner.go): Entry point; manages execution lifecycle and repository locking. - Orchestrator (
orchestrator.go): Manages cache invalidation, directory pruning, and global (root-level) documentation regeneration. - Synthesizer (
synthesize.go): Traverses theDirNodetree; decides between cached summaries or new LLM extractions based on "affected" status.
-
Chunking and Reduction (
chunking.go): Implements recursive reduction for oversized items using expansion (overlapping segments), batching, and consolidation. Recursion terminates if output is$\ge 95%$ of input. -
Markdown Processing (
markdown.go): Strips triple-backtick or JSON fences from LLM output to extract raw content. -
Metadata Cache (
cache.go): Persists SHA256 fingerprints. Only version1is supported; version mismatches trigger a clean state. -
Tree and Change Analysis (
tree.go): Tracks file status (StatusAdded,StatusModified,StatusDeleted) and propagates impact up the directory hierarchy.
- LLM Client (
client.go): Synchronous HTTP client for theapi/chatprotocol. Performs no retry logic or circuit breaking. - Constants (
constants.go):defaultHTTPTimeout: 10 minutes.contextWindowAllocRatio: 0.75 (75% for content, 25% for overhead).defaultChunkOverlap: 800 tokens.maxErrorBodyBytes: 1 KB.
- **Utilities (`utils.go