# divar-ir/ai-doc-gen architecture

> Architecture diagram of the GitHub repository divar-ir/ai-doc-gen, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/divar-ir/ai-doc-gen
- Repository: https://github.com/divar-ir/ai-doc-gen

## Overview

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: [`src/agents/analyzer.py`](https://github.com/divar-ir/ai-doc-gen/blob/main/src/agents/analyzer.py) (Analyzer), [`src/config.py`](https://github.com/divar-ir/ai-doc-gen/blob/main/src/config.py) (Config), [`src/main.py`](https://github.com/divar-ir/ai-doc-gen/blob/main/src/main.py) (Main CLI), [`src/handlers/analyze.py`](https://github.com/divar-ir/ai-doc-gen/blob/main/src/handlers/analyze.py) (Analyze Cmd)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI & Handlers"]
  node_main["Main CLI<br/>entrypoint<br/>[main.py]"]
  node_base_handler["Base Handler<br/>orchestration<br/>[base_handler.py]"]
  node_analyze_handler["Analyze Cmd<br/>command handler<br/>[analyze.py]"]
  node_readme_handler["README Cmd<br/>command handler<br/>[readme.py]"]
  node_ai_rules_handler["AI Rules Cmd<br/>command handler<br/>[ai_rules.py]"]
  node_cronjob_handler["Cronjob Cmd<br/>command handler<br/>[cronjob.py]"]
  node_config["Config<br/>[config.py]"]
end

subgraph group_agents["AI Agents"]
  node_analyzer["Analyzer<br/>analysis agent<br/>[analyzer.py]"]
  node_documenter["Documenter<br/>generation agent<br/>[documenter.py]"]
  node_rules_gen["Rules Generator<br/>generation agent"]
end

subgraph group_access["Repo Access"]
  node_prompt_manager["Prompt Manager<br/>prompt loader<br/>[prompt_manager.py]"]
  node_worker_pool(("Worker Pool<br/>parallelism<br/>[worker_pool.py]"))
  node_list_files["List Files<br/>repo tool<br/>[list_files.py]"]
  node_file_reader["File Reader<br/>repo tool<br/>[file_reader.py]"]
  node_logger(("Logger<br/>shared util<br/>[logger.py]"))
  node_retry_client(("Retry Client<br/>shared util<br/>[retry_client.py]"))
  node_repo_utils(("Repo Utils<br/>shared util<br/>[repo.py]"))
end

subgraph group_ops["Runtime & Ops"]
  node_dockerfile["Dockerfile<br/>runtime image"]
  node_helm_cronjob["Helm Cronjob<br/>deployment<br/>[cronjob.yaml]"]
end

subgraph group_outputs["Generated Artifacts"]
  node_ai_docs["AI Docs<br/>generated docs<br/>[docs]"]
  node_readme_md["README<br/>generated docs<br/>[README.md]"]
  node_ai_files["AI Rules<br/>generated docs<br/>[AGENTS.md]"]
end

node_main -->|"analyze"| node_analyze_handler
node_main -->|"generate readme"| node_readme_handler
node_main -->|"generate rules"| node_ai_rules_handler
node_main -->|"cronjob analyze"| node_cronjob_handler
node_analyze_handler -->|"extends"| node_base_handler
node_readme_handler -->|"extends"| node_base_handler
node_ai_rules_handler -->|"extends"| node_base_handler
node_cronjob_handler -->|"extends"| node_base_handler
node_analyze_handler -->|"runs"| node_analyzer
node_readme_handler -->|"runs"| node_documenter
node_ai_rules_handler -->|"runs"| node_rules_gen
node_cronjob_handler -->|"schedules"| node_analyzer
node_analyzer -->|"parallelizes"| node_worker_pool
node_analyzer -->|"scans"| node_list_files
node_analyzer -->|"reads"| node_file_reader
node_analyzer -->|"uses prompts"| node_prompt_manager
node_documenter -->|"uses prompts"| node_prompt_manager
node_rules_gen -->|"uses prompts"| node_prompt_manager
node_analyze_handler -->|"loads"| node_config
node_readme_handler -->|"loads"| node_config
node_ai_rules_handler -->|"loads"| node_config
node_cronjob_handler -->|"loads"| node_config
node_analyzer -->|"writes"| node_ai_docs
node_documenter -->|"writes"| node_readme_md
node_rules_gen -->|"writes"| node_ai_files
node_config -.->|"supports"| node_dockerfile
node_config -.->|"supports"| node_helm_cronjob
node_retry_client -.->|"supports"| node_analyzer
node_retry_client -.->|"supports"| node_documenter
node_retry_client -.->|"supports"| node_rules_gen
node_logger -.->|"observes"| node_main
node_repo_utils -.->|"supports"| node_analyzer

click node_main "https://github.com/divar-ir/ai-doc-gen/blob/main/src/main.py"
click node_base_handler "https://github.com/divar-ir/ai-doc-gen/blob/main/src/handlers/base_handler.py"
click node_analyze_handler "https://github.com/divar-ir/ai-doc-gen/blob/main/src/handlers/analyze.py"
click node_readme_handler "https://github.com/divar-ir/ai-doc-gen/blob/main/src/handlers/readme.py"
click node_ai_rules_handler "https://github.com/divar-ir/ai-doc-gen/blob/main/src/handlers/ai_rules.py"
click node_cronjob_handler "https://github.com/divar-ir/ai-doc-gen/blob/main/src/handlers/cronjob.py"
click node_config "https://github.com/divar-ir/ai-doc-gen/blob/main/src/config.py"
click node_analyzer "https://github.com/divar-ir/ai-doc-gen/blob/main/src/agents/analyzer.py"
click node_documenter "https://github.com/divar-ir/ai-doc-gen/blob/main/src/agents/documenter.py"
click node_rules_gen "https://github.com/divar-ir/ai-doc-gen/blob/main/src/agents/ai_rules_generator.py"
click node_prompt_manager "https://github.com/divar-ir/ai-doc-gen/blob/main/src/utils/prompt_manager.py"
click node_worker_pool "https://github.com/divar-ir/ai-doc-gen/blob/main/src/utils/worker_pool.py"
click node_list_files "https://github.com/divar-ir/ai-doc-gen/blob/main/src/agents/tools/dir_tool/list_files.py"
click node_file_reader "https://github.com/divar-ir/ai-doc-gen/blob/main/src/agents/tools/file_tool/file_reader.py"
click node_logger "https://github.com/divar-ir/ai-doc-gen/blob/main/src/utils/logger.py"
click node_retry_client "https://github.com/divar-ir/ai-doc-gen/blob/main/src/utils/retry_client.py"
click node_repo_utils "https://github.com/divar-ir/ai-doc-gen/blob/main/src/utils/repo.py"
click node_ai_docs "https://github.com/divar-ir/ai-doc-gen/blob/main/.ai/docs"
click node_readme_md "https://github.com/divar-ir/ai-doc-gen/blob/main/README.md"
click node_ai_files "https://github.com/divar-ir/ai-doc-gen/blob/main/AGENTS.md"
click node_dockerfile "https://github.com/divar-ir/ai-doc-gen/tree/main/Dockerfile"
click node_helm_cronjob "https://github.com/divar-ir/ai-doc-gen/blob/main/k8s/helm/templates/cronjob.yaml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_base_handler,node_analyze_handler,node_readme_handler,node_ai_rules_handler,node_cronjob_handler,node_config toneBlue
class node_analyzer,node_documenter,node_rules_gen toneAmber
class node_prompt_manager,node_worker_pool,node_list_files,node_file_reader,node_logger,node_retry_client,node_repo_utils toneMint
class node_dockerfile,node_helm_cronjob toneRose
class node_ai_docs,node_readme_md,node_ai_files toneIndigo
```

## Components

### CLI & Handlers

Command entrypoints and orchestration boundaries.

- [Main CLI](https://github.com/divar-ir/ai-doc-gen/blob/main/src/main.py): entrypoint · Routes CLI commands into analysis and generation workflows. · `src/main.py`
- [Base Handler](https://github.com/divar-ir/ai-doc-gen/blob/main/src/handlers/base_handler.py): orchestration · Shared handler wiring and execution behavior. · `src/handlers/base_handler.py`
- [Analyze Cmd](https://github.com/divar-ir/ai-doc-gen/blob/main/src/handlers/analyze.py): command handler · Orchestrates repository analysis runs. · `src/handlers/analyze.py`
- [README Cmd](https://github.com/divar-ir/ai-doc-gen/blob/main/src/handlers/readme.py): command handler · Drives README generation from analysis context. · `src/handlers/readme.py`
- [AI Rules Cmd](https://github.com/divar-ir/ai-doc-gen/blob/main/src/handlers/ai_rules.py): command handler · Generates repo-local assistant guidance files. · `src/handlers/ai_rules.py`
- [Cronjob Cmd](https://github.com/divar-ir/ai-doc-gen/blob/main/src/handlers/cronjob.py): command handler · Scheduled or GitLab-oriented execution path. · `src/handlers/cronjob.py`
- [Config](https://github.com/divar-ir/ai-doc-gen/blob/main/src/config.py): runtime config · Loads repo and execution settings for workflows. · `src/config.py`

### AI Agents

Specialized analysis and generation workers.

- [Analyzer](https://github.com/divar-ir/ai-doc-gen/blob/main/src/agents/analyzer.py): analysis agent · Coordinates structural, flow, dependency, request, and API analysis. · `src/agents/analyzer.py`
- [Documenter](https://github.com/divar-ir/ai-doc-gen/blob/main/src/agents/documenter.py): generation agent · Synthesizes human-facing documentation from analysis artifacts. · `src/agents/documenter.py`
- [Rules Generator](https://github.com/divar-ir/ai-doc-gen/blob/main/src/agents/ai_rules_generator.py): generation agent · Produces CLAUDE, AGENTS, and cursor rule guidance. · `src/agents/ai_rules_generator.py`

### Repo Access

Controlled filesystem and prompt access for agents.

- [Prompt Manager](https://github.com/divar-ir/ai-doc-gen/blob/main/src/utils/prompt_manager.py): prompt loader · Loads versioned YAML prompts for agent behavior. · `src/utils/prompt_manager.py`
- [Worker Pool](https://github.com/divar-ir/ai-doc-gen/blob/main/src/utils/worker_pool.py): parallelism · Runs analysis tasks in parallel. · `src/utils/worker_pool.py`
- [List Files](https://github.com/divar-ir/ai-doc-gen/blob/main/src/agents/tools/dir_tool/list_files.py): repo tool · Enumerates target repository contents for agents. · `src/agents/tools/dir_tool/list_files.py`
- [File Reader](https://github.com/divar-ir/ai-doc-gen/blob/main/src/agents/tools/file_tool/file_reader.py): repo tool · Reads source files from the target repository. · `src/agents/tools/file_tool/file_reader.py`
- [Logger](https://github.com/divar-ir/ai-doc-gen/blob/main/src/utils/logger.py): shared util · Common logging and execution visibility. · `src/utils/logger.py`
- [Retry Client](https://github.com/divar-ir/ai-doc-gen/blob/main/src/utils/retry_client.py): shared util · Resilient wrapper for external LLM/API calls. · `src/utils/retry_client.py`
- [Repo Utils](https://github.com/divar-ir/ai-doc-gen/blob/main/src/utils/repo.py): shared util · Repository-related helper functions. · `src/utils/repo.py`

### Runtime & Ops

Container and scheduled execution surfaces.

- [Dockerfile](https://github.com/divar-ir/ai-doc-gen/tree/main/Dockerfile): runtime image · Container image definition for batch execution. · `Dockerfile`
- [Helm Cronjob](https://github.com/divar-ir/ai-doc-gen/blob/main/k8s/helm/templates/cronjob.yaml): deployment · Kubernetes scheduled-job template for automated runs. · `k8s/helm/templates/cronjob.yaml`

### Generated Artifacts

Repository-local documentation and AI guidance outputs.

- [AI Docs](https://github.com/divar-ir/ai-doc-gen/blob/main/.ai/docs): generated docs · Intermediate analysis outputs written under .ai/docs/. · `.ai/docs`
- [README](https://github.com/divar-ir/ai-doc-gen/blob/main/README.md): generated docs · Human-facing repository overview generated from analysis. · `README.md`
- [AI Rules](https://github.com/divar-ir/ai-doc-gen/blob/main/AGENTS.md): generated docs · Repository-local assistant instructions and rules files. · `AGENTS.md`

## Connections

- 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.
