How divar-ir/ai-doc-gen works
1. What this repository is
- This is not an application backend; it is a CLI-driven documentation generator for other repositories.
- Its architecture is centered on taking a target repo path, analyzing that codebase with multiple specialized AI agents, and writing generated artifacts back into the target repo or into .ai/docs/.
2. Top-level execution paths
- The main entry point is src/main.py, which routes into three primary workflows:
- analyze
- generate readme
- generate ai-rules
- There is also a cronjob analyze path for scheduled/GitLab-oriented execution.
- The repo also includes a Dockerfile and Helm chart under k8s/helm, indicating it is meant to run as a containerized batch job, with optional Kubernetes scheduling and monitoring.
3. Command/handler layer
- src/handlers/ is the orchestration boundary between the CLI and the AI logic.
- Handlers are split by use case:
- analyze.py for repository analysis
- readme.py for README generation
- ai_rules.py for assistant configuration generation
- cronjob.py for scheduled or GitLab-integrated execution
- base_handler.py likely provides shared command wiring, config loading, and common execution behavior.
4. Agent layer: the core intelligence
- src/agents/ contains the specialized AI workers that do the actual reasoning.
- analyzer.py coordinates the analysis phase, with distinct sub-analyses for:
- code structure
- data flow
- dependency analysis
- request flow
- API analysis
- documenter.py turns analysis output into a README-style document.
- ai_rules_generator.py produces repository-specific AI assistant instructions such as CLAUDE.md, AGENTS.md, and .cursor/rules/.
5. Prompt assets as a separate contract
- Prompt templates live in src/agents/prompts/ as YAML files.
- This keeps the prompt text versioned and separate from execution code, which is important because the system depends heavily on prompt consistency.
- The prompt files are the boundary between static orchestration logic and LLM behavior.
6. Repository access and file-system tools
- src/agents/tools/ contains the agent-facing tools for interacting with the target repository.
- dir_tool/list_files.py supports directory enumeration.
- file_tool/file_reader.py supports reading source files.
- These tools are deliberately narrow: the agents reason over repository content, but the tools expose only controlled filesystem access rather than broad arbitrary execution.
7. Configuration and context propagation
- src/config.py plus .ai/config.yaml / config_example.yaml define the runtime behavior.
- Configuration controls what analyses run, what sections appear in generated docs, and cronjob behavior.
- .env.sample suggests some settings come from environment variables, likely for API keys, tracing, or GitLab access.
- The repository is designed so the target project’s local .ai/ directory acts as both input config and output location.
8. Primary data flow for analysis
- The target repo is scanned through file/directory tools.
- Analysis agents run in parallel, enabled by src/utils/worker_pool.py.
- Their outputs are aggregated into structured analysis artifacts.
- Those artifacts are written under .ai/docs/, forming the intermediate knowledge base used by later generation steps.
9. README generation flow
- The README workflow consumes analysis results, and optionally an existing README for context.
- The documenter agent synthesizes a repo overview, architecture, usage, and other sections according to config and CLI exclusions.
- This path is the main “documentation output” pipeline: repository content in, markdown documentation out.
10. AI rules generation flow
- The AI-rules workflow is a separate generation path aimed at coding assistants rather than end users.
- It produces repo-local guidance files: CLAUDE.md, AGENTS.md, and .cursor/rules/*.
- This path is more about shaping future AI behavior in the repository than describing the codebase for humans.
11. Cronjob and GitLab-oriented execution
- The cronjob handler and Helm cronjob.yaml show the system can be run on a schedule, likely for periodic repo analysis/documentation refresh.
- The README explicitly mentions GitLab integration and merge request creation, so this path likely supports automated updates in a GitLab workflow.
- config_example.yaml probably includes recency filters and working-path settings for that automation mode.
12. Cross-cutting concerns
- src/utils/ contains shared infrastructure:
- logger.py for logging
- prompt_manager.py for loading prompt assets
- retry_client.py for resilient external calls
- repo.py for repository-related helpers
- dict.py for common data shaping
- OpenTelemetry and Langfuse are material here because the system makes many LLM calls and benefits from tracing/observability.
- The architecture also depends on OpenAI-compatible model APIs, which is why provider choice is configuration-driven rather than hardcoded.
13. Operational boundaries
- The system’s external dependencies are mostly:
- an OpenAI-compatible LLM endpoint
- Git/GitLab for repository and automation workflows
- optional observability backends
- Internally, the main boundary is between deterministic orchestration code and nondeterministic AI agent execution.
- That separation is what makes the repo diagrammatically important: CLI/handlers manage flow, agents generate content, and tools expose controlled repo access.
Main entry points: analyzer.py Analyzer · config.py Config · main.py Main CLI · analyze.py Analyze Cmd
Components 22
CLI & Handlers
Command entrypoints and orchestration boundaries.
- Main CLI · Routes CLI commands into analysis and generation workflows.
src/main.py - Base Handler · Shared handler wiring and execution behavior.
src/handlers/base_handler.py - Analyze Cmd · Orchestrates repository analysis runs.
src/handlers/analyze.py - README Cmd · Drives README generation from analysis context.
src/handlers/readme.py - AI Rules Cmd · Generates repo-local assistant guidance files.
src/handlers/ai_rules.py - Cronjob Cmd · Scheduled or GitLab-oriented execution path.
src/handlers/cronjob.py - Config · Loads repo and execution settings for workflows.
src/config.py
AI Agents
Specialized analysis and generation workers.
- Analyzer · Coordinates structural, flow, dependency, request, and API analysis.
src/agents/analyzer.py - Documenter · Synthesizes human-facing documentation from analysis artifacts.
src/agents/documenter.py - Rules Generator · Produces CLAUDE, AGENTS, and cursor rule guidance.
src/agents/ai_rules_generator.py
Repo Access
Controlled filesystem and prompt access for agents.
- Prompt Manager · Loads versioned YAML prompts for agent behavior.
src/utils/prompt_manager.py - Worker Pool · Runs analysis tasks in parallel.
src/utils/worker_pool.py - List Files · Enumerates target repository contents for agents.
src/agents/tools/dir_tool/list_files.py - File Reader · Reads source files from the target repository.
src/agents/tools/file_tool/file_reader.py - Logger · Common logging and execution visibility.
src/utils/logger.py - Retry Client · Resilient wrapper for external LLM/API calls.
src/utils/retry_client.py - Repo Utils · Repository-related helper functions.
src/utils/repo.py
Runtime & Ops
Container and scheduled execution surfaces.
- Dockerfile · Container image definition for batch execution.
Dockerfile - Helm Cronjob · Kubernetes scheduled-job template for automated runs.
k8s/helm/templates/cronjob.yaml
Generated Artifacts
Repository-local documentation and AI guidance outputs.
Connections 32
- Main CLI → Analyze Cmd: analyze · CLI dispatches analysis workflow.
- Main CLI → README Cmd: generate readme · CLI dispatches README generation workflow.
- Main CLI → AI Rules Cmd: generate rules · CLI dispatches assistant-rules generation workflow.
- Main CLI → Cronjob Cmd: cronjob analyze · CLI dispatches scheduled execution path.
- Analyze Cmd → Base Handler: extends · Shares common handler behavior.
- README Cmd → Base Handler: extends · Shares common handler behavior.
- AI Rules Cmd → Base Handler: extends · Shares common handler behavior.
- Cronjob Cmd → Base Handler: extends · Shares common handler behavior.
- Analyze Cmd → Analyzer: runs · Triggers repository analysis agents.
- README Cmd → Documenter: runs · Triggers documentation synthesis.
- AI Rules Cmd → Rules Generator: runs · Triggers assistant guidance generation.
- Cronjob Cmd → Analyzer: schedules · Executes analysis on a recurring or remote-triggered basis.
- Analyzer → Worker Pool: parallelizes · Fan-out across specialized analyses.
- Analyzer → List Files: scans · Discovers repository structure.
- Analyzer → File Reader: reads · Loads source content for analysis.
- Analyzer → Prompt Manager: uses prompts · Loads analyzer prompt templates.
- Documenter → Prompt Manager: uses prompts · Loads document-generation templates.
- Rules Generator → Prompt Manager: uses prompts · Loads rule-generation templates.
- Analyze Cmd → Config: loads · Reads execution and analysis settings.
- README Cmd → Config: loads · Reads README generation settings.
- AI Rules Cmd → Config: loads · Reads rule-generation settings.
- Cronjob Cmd → Config: loads · Reads scheduled-job settings.
- Analyzer → AI Docs: writes · Persists structured analysis artifacts.
- Documenter → README: writes · Produces human-facing README output.
- Rules Generator → AI Rules: writes · Produces assistant guidance files.
- Config → Dockerfile: supports · Runtime is intended for containerized execution.
- Config → Helm Cronjob: supports · Settings align with scheduled Kubernetes execution.
- Retry Client → Analyzer: supports · Resilient external calls during analysis.
- Retry Client → Documenter: supports · Resilient external calls during generation.
- Retry Client → Rules Generator: supports · Resilient external calls during generation.
- Logger → Main CLI: observes · Central logging across CLI execution.
- Repo Utils → Analyzer: supports · Repository helper functions for analysis flows.