Skip to content
asadullah48Public

About

Diffs Python ASTs and drafts docstrings/README tables from them - deterministic structure, optional free-tier LLM (Ollama/Hugging Face) for the natural-language summary.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

📝 DocuCode

Watches a Python diff, parses both sides with ast, and drafts the docstring and README table that should follow from it — with an optional free-tier LLM for the natural-language parts, and a deterministic fallback for everything else.

CI License: Apache 2.0 Python 3.10+ FastAPI

No hosted demo is live yet — see Quick start below to run it in under a minute (docker compose up --build, or pip install -e ".[dev]" + uvicorn).


What this actually is

Documentation drift is a real, boring problem: someone adds a parameter, the docstring doesn't follow, and six months later an integration breaks on an assumption nobody double-checked. DocuCode is a small, honest attempt at the first half of a fix — not a magic "AI writes your docs" black box, but a pipeline where the parts that must be exact are exact, and the part that's genuinely a judgment call (what does this function mean?) is clearly marked as a best-effort suggestion.

Concretely, given an old and new version of a Python file, DocuCode:

  1. Parses both versions with Python's real ast module (ASTAnalyzer) — not a regex, not a guess — and diffs function signatures: added params, removed params, return-type changes, brand-new functions.
  2. Drafts a docstring and a markdown API table from that diff (DocGenerator). The structure — which sections exist, which parameters are listed, their real type hints — is 100% derived from the AST, so it can never invent a parameter that doesn't exist. Only the plain-English summary and per-argument descriptions are optionally handed to an LLM; if that call fails or isn't configured, generation falls back to a plain, honest description instead of breaking.
  3. Validates the result (ReviewerAgent) by checking that every real parameter from the diff actually appears in the generated docstring before calling it "auto-patched."

Those three steps map to WatcherAgent → DocAgent → ReviewerAgent, orchestrated by DocuCodeEngine behind a small FastAPI service with an interactive, bilingual (English/Arabic) dashboard for trying it out on sample diffs.


Why an LLM is optional, not required

An earlier version of this generator faked its output when there was nothing to document — literally falling back to a hardcoded process_payment example and an invented idempotency_key parameter, even on an unchanged file. That's the failure mode this design deliberately avoids: the LLM is never asked to produce structure, only prose. docucode/core/llm_client.py is a small pluggable client with three backends, selected by DOCUCODE_LLM_PROVIDER:

Provider When to use it Cost
template (default) Local dev, tests, CI — fully offline, deterministic Free, no network
ollama Local development with a real model Free, runs on your machine
huggingface Hosted demo, where no local model server exists Free tier (Inference API)

If the configured provider is unreachable, times out, or returns something unparseable, draft_docstring() returns None and DocGenerator silently uses its deterministic description instead — the service degrades, it never fails a request because a model wasn't running.


Architecture

                    ┌─────────────────────────┐
                    │    IDE Source Code      │
                    └────────────┬────────────┘
                                 │
                    ┌────────────▼────────────┐
                    │      WatcherAgent       │
                    │  Real ast diff, added/  │
                    │  removed param tracking │
                    └────────────┬────────────┘
                                 │
                    ┌────────────▼────────────┐
                    │        DocAgent         │
                    │  Deterministic structure│
                    │  + optional LLM summary │──── llm_client.py
                    │    (ollama / hf / none) │     (pluggable, fails soft)
                    └────────────┬────────────┘
                                 │
                    ┌────────────▼────────────┐
                    │      ReviewerAgent      │
                    │  Real parameter-coverage│
                    │  check against the AST  │
                    └─────────────────────────┘

Quick start

git clone https://github.com/asadullah48/docucode.git
cd docucode

pip install -e ".[dev]"
python -m pytest tests -v

uvicorn docucode.server:app --host 127.0.0.1 --port 8013 --reload

With Docker

docker compose up --build

Enabling a real LLM backend locally (Ollama)

# 1. Install Ollama: https://ollama.com
ollama pull qwen2.5-coder:1.5b
ollama serve

# 2. Point DocuCode at it
cp .env.example .env
# edit .env: DOCUCODE_LLM_PROVIDER=ollama

Restart the server and the dashboard's "LLM Engine" badge will switch from Deterministic template to Ollama (local). Full variable reference, including the Hugging Face Inference API option used for the hosted demo, is in .env.example.


API

Method Path Purpose
GET / Interactive documentation studio (dashboard)
GET /healthz Liveness probe
GET /readyz Readiness probe + currently configured LLM provider
POST /api/v1/docucode/sync-docs Submit {old_source, new_source, ...}, get back the AST diff, generated docs, and review result
GET /docs Auto-generated Swagger UI

Project structure

docucode/
├── docucode/
│   ├── agents/          # WatcherAgent, DocAgent, ReviewerAgent
│   ├── core/             # ASTAnalyzer, DocGenerator, LLM client, Pydantic models
│   ├── orchestration/    # DocuCodeEngine (Watch → Doc → Review pipeline)
│   ├── static/           # Bilingual dashboard (vanilla HTML/CSS/JS)
│   └── server.py         # FastAPI app
├── tests/                 # pytest suite, run in CI on 3.10 / 3.11 / 3.12
├── deploy/                # Hugging Face Space deployment docs & config
├── helm/                  # Kubernetes Helm chart
├── Dockerfile / docker-compose.yml
└── .env.example

Honest limitations

  • Diffing is Python-only right now (real ast parsing); other languages would need their own analyzer behind the same WatcherAgent interface.
  • The demo/API currently documents the first modified function in a diff, not every function in a multi-function change — a deliberate scope cut for a portfolio-sized project, not a hidden gap.
  • Free-tier LLM inference (both Ollama on modest hardware and the Hugging Face Inference API) is slower and less reliable than a paid model — that's the trade this project makes explicitly, and why the deterministic fallback exists rather than being an afterthought.

🤖 Agentic AI Alignment

  • Autonomy — ASTAnalyzer diffs both file versions and drafts the docstring/README update on its own; a human reviews the suggestion rather than writing it from scratch.
  • Resilience — the parts that must be exact (signature diffs, added/ removed params) run on real ast parsing, not an LLM guess, and the natural-language summary has a deterministic fallback if the free-tier LLM (Ollama/Hugging Face) is slow or unavailable.
  • Adaptivity — the same diff-driven pipeline works on any function signature change, so it adapts to a codebase's evolution automatically instead of needing per-project configuration.

Roadmap

  • Wire this into a pre-commit or CI hook so drift is caught automatically on every PR rather than run manually.
  • Expose the AST-diff step as an MCP tool so other documentation agents can reuse the exact-parsing half without re-implementing it.

Author

Built by Asadullah Shafique — portfolio · GitHub

License

Apache License 2.0

About

Diffs Python ASTs and drafts docstrings/README tables from them - deterministic structure, optional free-tier LLM (Ollama/Hugging Face) for the natural-language summary.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages