ws-codebase-documenter
Codebase Documenter
Generate structured documentation designed for consumption by AI assistants and code agents working on dependent projects.
Workflow
1. Determine Mode
Check if documentation/config.json exists:
- Does not exist: Bootstrap Mode (first run)
- Exists: Check the user's argument:
regenerate [doc-type]: Regenerate Mode (targeted re-generation)- Otherwise: Incremental Mode (subsequent runs)
2. Bootstrap Mode
Execute when documentation/config.json is absent.
2.1 Detect Stack
Check for these files in order:
| File | Stack |
|---|---|
package.json |
nodejs |
requirements.txt, pyproject.toml, setup.py |
python |
go.mod |
go |
Cargo.toml |
rust |
*.csproj, *.sln |
dotnet |
pom.xml, build.gradle |
java |
composer.json |
php |
2.2 Create Config
Write a single documentation/config.json file containing all fields:
{
"stack": "[detected-stack]",
"exclude": [
"**/*.test.*",
"**/*.spec.*",
"**/test/**",
"**/tests/**",
"**/__tests__/**",
"**/node_modules/**",
"**/vendor/**",
"**/dist/**",
"**/build/**",
"**/target/**"
],
"include_inline_examples": true,
"include_architecture_diagrams": true,
"docusaurus": null,
"frontend": {
"enabled": true,
"css_paths": [],
"js_paths": [],
"token_files": [],
"build_tool": null,
"methodology": null
},
"playbook": {
"enabled": true,
"custom_procedures": []
},
"capability_map": {
"enabled": true,
"custom_categories": []
},
"style_guide": {
"enabled": true,
"forbidden_patterns": ["!important", "inline styles"],
"custom_rules": []
},
"integration_map": {
"enabled": true
},
"consistency_check": {
"enabled": true,
"strict": false
},
"claude_md": {
"inject_rules": true,
"max_rules": 25,
"custom_rules": []
}
}
If Docusaurus sync is requested, set the docusaurus field:
{
"docusaurus": {
"repo": "git@github.com:org/docs-site.git",
"branch": "main",
"docs_path": "docs/api",
"sidebar_label": "API Reference"
}
}
All fields from frontend onward are optional — existing config.json files without these fields use the defaults shown above.
2.2b Backfill Config
After the scan (step 2.4) completes, update documentation/config.json a second time with auto-detected values:
- Set
frontend.css_pathsto detected CSS/SCSS directory paths - Set
frontend.js_pathsto detected JS/TS frontend directory paths - Set
frontend.token_filesto detected design token file paths - Set
frontend.build_toolto detected build tool name - Set
frontend.methodologyto detected CSS methodology
This second write backfills detected values so the user can review and override them in subsequent runs.
2.3 Read Stack Reference
Load the appropriate reference file:
references/stacks/[stack].md
This provides patterns for identifying public/private API, documentation comments, error handling, and project structure conventions.
2.4 Scan Codebase
- List all source files (respecting
excludepatterns) - Identify public API surface using stack-specific patterns
- Categorize items: functions, types, errors, features
- Extract documentation comments
- Map relationships between items
- If
config.frontend.enabledistrue: scan frontend assets following the procedures inreferences/frontend-detection.md. This detects CSS/SCSS organization, methodology, design tokens, breakpoints, JS initialization patterns, build tools, and anti-patterns. - Track cross-module function calls using patterns from the "Cross-Module Patterns" section in
references/stacks/[stack].md. Record: calling module, called function/method, file path, and data passed. - For each design pattern detected in step 5, extract concrete details for playbook generation: file path globs, base classes/interfaces, directory conventions, required method signatures.
- While scanning, build the capability map incrementally — classify each public function/type into a functional domain category as it is discovered. Do NOT defer this to a second pass.
2.5 Generate Documentation
Read references/doc-templates.md for exact formats.
Create directory structure:
documentation/
├── config.json
├── .docstate
├── overview.md
├── architecture.md
├── playbook.md
├── capability-map.md
├── style-guide.md
├── integration-map.md
├── public/
│ ├── _index.md
│ ├── features/
│ ├── api/
│ ├── functions/
│ ├── types/
│ └── errors/
└── private/
├── _index.md
├── functions/
├── types/
└── utils/
Generate files:
overview.md- Project purpose, entry points, quick startarchitecture.md- Component diagrams, data flow, design patterns- Function docs - One file per function in appropriate directory
- Type docs - One file per type
- Error docs - Error hierarchy and patterns
- Index files -
_index.mdin each directory playbook.md— Read template 11 fromreferences/doc-templates.md. For each design pattern extracted in step 2.4 (item 8), generate a numbered step-by-step procedure. Include checklists and common mistakes. Only generated whenconfig.playbook.enabledistrue.capability-map.md— Read template 12 fromreferences/doc-templates.md. Use the domain classifications built incrementally during step 2.4 (item 9). Organize into task-oriented categories. Only generated whenconfig.capability_map.enabledistrue.style-guide.md— Read template 13 fromreferences/doc-templates.md. Use frontend scan results from step 2.4 (item 6). Only generated whenconfig.style_guide.enabledistrueAND frontend assets were detected.integration-map.md— Read template 14 fromreferences/doc-templates.md. Use cross-module data from step 2.4 (item 7). Only generated whenconfig.integration_map.enabledistrue.
2.6 Update Claude Code Instructions
Create or update CLAUDE.md in the project root to reference the documentation. If the file exists, check for existing sections before modifying:
- Read the file and search for
## Project Rulesand## Codebase Documentationheadings - If a section already exists, replace it in place (preserve surrounding content)
- If a section does not exist, append it to the end of the file
- If the file does not exist, create it
Step A: If config.claude_md.inject_rules is true (default), add the Project Rules section:
## Project Rules
These rules MUST be followed when modifying this codebase:
### Architecture
- [Generated rules from architecture.md and playbook.md]
### Data Access
- [Generated rules about ORM/Model usage patterns]
### Frontend
- [Generated rules from style-guide.md]
### Before Creating New Code
- Check `documentation/capability-map.md` before creating any new utility, helper, or shared function
- Check `documentation/public/_index.md` before creating any new public API
- Follow the step-by-step procedures in `documentation/playbook.md` for common tasks
### Documentation
- After adding new public functions, types, or components, update the relevant documentation files
- After adding new cross-module integrations, update `documentation/integration-map.md`
Extract project-specific rules to populate the subsections above (max config.claude_md.max_rules, default 25). Priority order when candidates exceed the limit:
config.claude_md.custom_rules— user-defined rules, always included first- Architecture rules from
architecture.mdandplaybook.md - Data access rules from detected Model/Repository patterns
- Frontend rules from
style-guide.md(when applicable) - Documentation maintenance rules
If config.claude_md.inject_rules is false, skip Step A entirely.
Step B: Always add the Codebase Documentation section:
## Codebase Documentation
This project has AI-optimized documentation in the `documentation/` folder.
Before making changes to this codebase:
1. Read `documentation/overview.md` for project purpose and entry points
2. Read `documentation/architecture.md` for system design and data flow
3. Read `documentation/playbook.md` for how to add new features
4. Check `documentation/capability-map.md` for existing utilities and helpers
5. Check `documentation/style-guide.md` for frontend conventions
6. Check `documentation/public/_index.md` for the public API surface
When modifying existing code:
- Check the relevant function/type doc in `documentation/public/` or `documentation/private/`
- Note any error handling patterns in `documentation/public/errors/`
When adding new public APIs:
- Follow patterns documented in `documentation/playbook.md`
- Ensure consistency with existing APIs in `documentation/public/`
- Update `documentation/capability-map.md` with the new capability
2.7 Write State
Create documentation/.docstate:
{
"last_commit": "[current HEAD SHA]",
"last_run": "[ISO timestamp]",
"docusaurus_last_sync": null,
"docusaurus_synced_files": [],
"consistency_last_check": null,
"consistency_violations": 0,
"generated_docs": [
"overview.md",
"architecture.md",
"playbook.md",
"capability-map.md",
"style-guide.md",
"integration-map.md"
],
"detected_patterns": {
"rest_endpoint": {
"description": "Router → Controller → Model",
"glob": "src/controllers/*.php",
"base_class": "BaseController",
"required_methods": ["handle", "validate"]
},
"data_access": {
"description": "Model static methods with caching",
"glob": "src/models/*.php",
"base_class": "BaseModel",
"required_methods": ["save", "to_array", "get_by"]
},
"frontend_css": {
"description": "Component SCSS files",
"glob": "src/css/blocks/*.scss"
},
"frontend_js": {
"description": "Block entry points with .init()",
"glob": "src/js/blocks/*.js"
}
},
"frontend_stats": {
"important_count": 0,
"inline_style_count": 0,
"token_count": 0,
"breakpoint_count": 0
}
}
Only include detected_patterns entries for patterns that were actually detected. generated_docs lists only the doc files that were actually generated (based on config flags). frontend_stats is only included when config.frontend.enabled is true.
Get current HEAD:
git rev-parse HEAD
3. Incremental Mode
Execute when documentation/config.json exists.
3.1 Load State
Read documentation/config.json and documentation/.docstate.
3.2 Get Changes
git diff --name-status [last_commit]..HEAD
Filter to source files only (exclude test files, config, etc).
3.3 Analyze Changes
For each changed file:
-
Get the diff:
git diff [last_commit]..HEAD -- [file] -
Identify semantic changes:
- New functions/types/classes added
- Signatures modified (parameters, return types)
- Items removed or deprecated
- Error handling changes
- Documentation comment updates
3.4 Update Documentation
For each semantic change:
| Change Type | Action |
|---|---|
| New item | Create new doc file |
| Modified signature | Update existing doc |
| Removed item | Mark deprecated or delete |
| New error | Add to error docs |
Also update:
- Relevant
_index.mdfiles architecture.mdif structure changed- Cross-references in related docs
Also update prescriptive documents when relevant changes are detected:
| Change Type | Document to Update |
|---|---|
| New design pattern or pattern change | playbook.md — add or update procedure |
| New public function/type/helper | capability-map.md — add to relevant category |
| New frontend files or convention change | style-guide.md — update rules and tokens |
| New cross-module call or shared resource | integration-map.md — add integration pattern |
3.4.5 Consistency Check
Skip if config.consistency_check.enabled is false.
- Load
references/consistency-rules.mdfor rule definitions and the execution procedure - Load
detected_patternsfromdocumentation/.docstate - For each changed file from step 3.2, run applicable consistency rules following the execution procedure in
references/consistency-rules.md - Generate
documentation/.consistency-report.mdusing template 15 fromreferences/doc-templates.md. This file is fully replaced on each run (never appended). If no violations are found, write a passing report. - Include consistency results in the PR description (step 4.3)
Severity is determined by config.consistency_check.strict:
strict: true— High-confidence violations are Errors, best-effort findings are Warningsstrict: false— All findings are Warnings
3.5 Verify Claude Code Instructions
Check that CLAUDE.md contains both:
- The "Codebase Documentation" reference section (from step 2.6)
- The "Project Rules" section (from step 2.6), if
config.claude_md.inject_rulesistrue
If the documentation reference section is missing, add it using the template from step 2.6.
If rules have changed (because underlying documentation was updated in step 3.4), regenerate the rules section using the priority order from step 2.6 and update CLAUDE.md.
3.6 Update State
Update documentation/.docstate with new HEAD and timestamp.
3b. Regenerate Mode
Execute when user runs with regenerate [doc-type].
Valid doc-types: playbook, capability-map, style-guide, integration-map, overview, architecture, consistency-report, all
3b.1 Determine Scan Scope
Each doc type requires a different scan scope:
| Doc Type | Required Scan Steps |
|---|---|
playbook |
Pattern detection (step 2.4, items 5, 8) |
capability-map |
Full API scan (step 2.4, items 1-5, 9) |
style-guide |
Frontend scan only (step 2.4, item 6) |
integration-map |
Cross-module scan (step 2.4, item 7) |
overview |
Basic scan (step 2.4, items 1-3) |
architecture |
Full scan (step 2.4, items 1-5) |
consistency-report |
No scan — run consistency check (step 3.4.5) against current HEAD using existing .docstate patterns |
all |
Full scan (all of step 2.4) |
3b.2 Load Config and State
Read documentation/config.json and documentation/.docstate. Load the stack reference file.
3b.3 Execute Targeted Scan
Run only the scan steps required by the doc type (per the table above).
3b.4 Regenerate Document
Regenerate the specified document file using the corresponding template from references/doc-templates.md.
3b.5 Update State
Update documentation/.docstate:
- Set
last_runto current timestamp - Update
detected_patternsif the scan detected changes - Update
frontend_statsif frontend scan was run - Do NOT update
last_commit— regeneration does not advance the incremental baseline
4. Create PR
4.1 Branch Naming
Format: docs/auto-update-YYYY-MM-DD-HH:MM
If branch exists, append -2, -3, etc.
BRANCH="docs/auto-update-$(date +%Y-%m-%d-%H:%M)"
git checkout -b "$BRANCH"
4.2 Commit Changes
git add documentation/
git commit -m "docs: update documentation for [commit range]"
4.3 Push and Create PR
git push -u origin "$BRANCH"
Create PR with description summarizing changes (see PR Description template in references/doc-templates.md).
5. Docusaurus Sync (Optional)
Execute only if config.docusaurus is configured. See references/docusaurus.md for detailed patterns.
5.1 Clone/Update Docusaurus Repo
DOCS_REPO="[config.docusaurus.repo]"
DOCS_BRANCH="[config.docusaurus.branch]"
DOCS_PATH="[config.docusaurus.docs_path]"
# Clone to temp directory if not exists
if [ ! -d ".docusaurus-sync" ]; then
git clone --depth 1 -b "$DOCS_BRANCH" "$DOCS_REPO" .docusaurus-sync
else
cd .docusaurus-sync && git pull && cd ..
fi
5.2 Transform Documentation
For each markdown file in documentation/:
-
Add Docusaurus frontmatter:
--- id: [filename-without-extension] title: [extracted-from-h1] sidebar_label: [short-title] sidebar_position: [order] --- -
Convert internal links from
../types/Foo.mdto Docusaurus paths -
Copy to
.docusaurus-sync/[docs_path]/
5.3 Generate Sidebar
Create or update .docusaurus-sync/[docs_path]/_category_.json for each directory:
{
"label": "[directory-name-titlecased]",
"position": [order],
"collapsed": false
}
Generate sidebars.js entry if needed (see references/docusaurus.md).
5.4 Commit and Push to Docusaurus Repo
cd .docusaurus-sync
git add .
git commit -m "docs: sync from [source-repo] @ [commit-sha]"
git push origin "$DOCS_BRANCH"
Or create a PR in the Docusaurus repo if preferred.
5.5 Cleanup
rm -rf .docusaurus-sync
Documentation Standards
AI Optimization
Documentation must be optimized for AI consumption:
- Structured data over prose - Use tables for parameters, errors, options
- Explicit types - Always include full type signatures
- Complete examples - Show imports, setup, and usage
- Error recovery - Document how to handle each error
- Cross-references - Link to related functions and types
- No ambiguity - Specify defaults, constraints, edge cases
Public vs Private
Public (documentation/public/):
- Exported functions, classes, types
- Items intended for external use
- API surface for dependent projects
Private (documentation/private/):
- Internal implementation details
- Helper functions
- Useful for understanding internals but not for direct use
When to Document Private Items
Document private items when:
- They're complex and non-obvious
- They're frequently modified
- Understanding them helps debug issues
- They contain important business logic
Skip documenting:
- Trivial helpers (less than 5 lines)
- Generated code
- Boilerplate
Error Handling
If documentation generation fails:
- Do not commit partial changes
- Log the error
- Exit with non-zero status
Common issues:
- Cannot detect stack: Ask user to set
stackin config.json - Cannot parse source files: Skip unparseable files, continue with others
- Git operations fail: Ensure clean working directory
- Reference file missing: See the fallback column in the Reference Files table below
Reference Files
Load these as needed. If a file cannot be read (missing, corrupted install, partial update), follow the fallback behavior:
| File | When to Load | If Missing |
|---|---|---|
references/stacks/[stack].md |
After detecting or reading stack | Fail — cannot generate accurate docs without stack-specific rules. Log: "Missing reference file for stack '{stack}'. Reinstall the plugin." Stop. |
references/doc-templates.md |
When generating any documentation | Fail — templates are required for all output. Log: "Missing doc-templates.md. Reinstall the plugin." Stop. |
references/docusaurus.md |
When config.docusaurus is configured |
Skip feature — skip Docusaurus sync. Log: "Missing docusaurus.md. Skipping Docusaurus sync." |
references/frontend-detection.md |
During frontend scan (step 2.4, item 6) | Skip feature — set all frontend results to empty/null, set config.frontend.enabled to false. Log: "Missing frontend-detection.md. Skipping frontend scan." |
references/consistency-rules.md |
During consistency check (step 3.4.5) | Skip feature — skip consistency checking entirely, omit the Consistency Check section from the PR description. Log: "Missing consistency-rules.md. Skipping consistency checks." |