Documentation Drift Detector
The agent detects documentation drift by mapping code directories to their docs, comparing git modification histories, extracting Python function signatures via AST, validating every markdown link and anchor, and scoring freshness on a weighted 0-100 scale. All four CLI tools use the Python standard library only.
Quick Start
# 1. Run full drift analysis on a repository
python scripts/drift_analyzer.py /path/to/repo
# 2. Score documentation freshness
python scripts/doc_staleness_scorer.py /path/to/repo
# 3. Validate API docs against Python source
python scripts/api_doc_validator.py /path/to/repo/src /path/to/repo/docs/api.md
# 4. Check all markdown links
python scripts/link_checker.py /path/to/repo
# JSON output for any tool
python scripts/drift_analyzer.py /path/to/repo --json
# Set failure threshold for CI
python scripts/doc_staleness_scorer.py /path/to/repo --threshold 60
All tools support --help for full usage details.
Core Workflows
Workflow 1: Full Drift Analysis
Scan all documentation against code changes since each doc was last updated. This is the primary entry point for understanding the overall drift state of a repository.
# Basic analysis
python scripts/drift_analyzer.py /path/to/repo
# Analyze with custom doc patterns
python scripts/drift_analyzer.py /path/to/repo --doc-patterns "*.md,*.rst,*.txt"
# JSON output for tooling
python scripts/drift_analyzer.py /path/to/repo --json
# Only show high-severity drift
python scripts/drift_analyzer.py /path/to/repo --min-severity high
# Analyze specific directory
python scripts/drift_analyzer.py /path/to/repo --scope src/
What it does:
- Discovers all documentation files in the repo
- For each doc, identifies the code directories it describes (via path proximity and content references)
- Compares the doc's last-modified date against the git history of its associated code
- Identifies specific changes (renamed files, moved directories, changed function signatures)
- Classifies each drift instance by category and severity
- Generates an actionable report with specific file:line references
Output example:
Documentation Drift Report
==========================
Repository: /path/to/repo
Scan date: 2026-03-18
Docs found: 12
Drifted: 5
HIGH SEVERITY:
docs/api.md (last updated: 2026-01-15)
- 23 code files changed since doc update
- 4 functions renamed in src/handlers/
- 2 new modules undocumented
Category: Factual + Structural
Recommendation: Manual update required
MEDIUM SEVERITY:
README.md (last updated: 2026-02-28)
- Installation section references removed dependency
- Version string outdated (says 1.8.0, current 2.0.0)
Category: Factual + Temporal
Recommendation: Auto-fixable (version), Manual (installation)
Workflow 2: API Documentation Validation
Check that API documentation accurately reflects the actual function signatures, class definitions, and module structure in your Python source code.
# Validate API docs against source
python scripts/api_doc_validator.py /path/to/src /path/to/docs/api.md
# Scan entire docs directory
python scripts/api_doc_validator.py /path/to/src /path/to/docs/ --recursive
# JSON output
python scripts/api_doc_validator.py /path/to/src /path/to/docs/api.md --json
# Include private methods in validation
python scripts/api_doc_validator.py /path/to/src /path/to/docs/ --include-private
What it detects:
- Functions/classes present in code but missing from docs
- Functions/classes documented but no longer in code (removed or renamed)
- Parameter mismatches (missing params, wrong types, wrong defaults)
- Deprecated items still documented as current
- Return type mismatches
- Module-level docstring drift
How it works:
The tool uses Python's ast module to parse source files and extract function signatures, class definitions, decorators, and docstrings. It then parses the markdown documentation looking for function/class references, parameter lists, and code blocks. Mismatches are reported with exact locations in both source and documentation.
Workflow 3: README Health Check
Validate README sections against the actual project state. This combines drift analysis, link checking, and completeness scoring into a single README-focused report.
# Check README health
python scripts/doc_staleness_scorer.py /path/to/repo --readme-focus
# Check with custom sections
python scripts/doc_staleness_scorer.py /path/to/repo --required-sections "Installation,Usage,API,Contributing,License"
Validates:
- Required sections are present (Installation, Usage, API Reference, Contributing, License)
- Version strings match package version (package.json, setup.py, pyproject.toml)
- File references in README actually exist
- Badge URLs are well-formed
- Code examples reference existing files/functions
- Table of contents matches actual headings
Workflow 4: Link Integrity Audit
Check every link in every markdown file -- local file references, anchors, cross-document links, and optionally external URLs.
# Check all markdown links
python scripts/link_checker.py /path/to/repo
# Include external URL checks (slower, makes HTTP requests)
python scripts/link_checker.py /path/to/repo --check-external
# Check specific file
python scripts/link_checker.py /path/to/repo/README.md
# JSON output
python scripts/link_checker.py /path/to/repo --json
# Only show broken links
python scripts/link_checker.py /path/to/repo --broken-only
What it checks:
- Local file references (
[link](path/to/file.md)) -- does the file exist? - Anchor references (
[link](#section-name)) -- does the heading exist? - Cross-document anchors (
[link](other.md#section)) -- does the file and heading exist? - Relative path correctness (catches
../errors) - Case sensitivity issues (common on Linux but silent on macOS)
- Image references -- do referenced images exist?
- Duplicate anchors that would cause ambiguous links
Workflow 5: Continuous Doc Monitoring
Integrate documentation drift detection into your CI/CD pipeline for ongoing monitoring.
GitHub Actions example:
name: Documentation Drift Check
on:
pull_request:
branches: [main, dev]
push:
branches: [main]
jobs:
doc-drift:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Full history for git log analysis
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Run drift analysis
run: python engineering/doc-drift-detector/scripts/drift_analyzer.py . --json > drift-report.json
- name: Check staleness score
run: python engineering/doc-drift-detector/scripts/doc_staleness_scorer.py . --threshold 50
- name: Validate API docs
run: python engineering/doc-drift-detector/scripts/api_doc_validator.py src/ docs/api.md
- name: Check links
run: python engineering/doc-drift-detector/scripts/link_checker.py .
- name: Upload drift report
if: always()
uses: actions/upload-artifact@v4
with:
name: drift-report
path: drift-report.json
Pre-commit hook:
#!/bin/bash
# .git/hooks/pre-commit
# Fail commit if docs are severely stale
python engineering/doc-drift-detector/scripts/doc_staleness_scorer.py . --threshold 30 --quiet
if [ $? -ne 0 ]; then
echo "Documentation is critically stale. Update docs before committing."
exit 1
fi
Tools
| Tool | Purpose | Lines | Key Feature |
|---|---|---|---|
drift_analyzer.py | Full drift analysis between code and docs | ~550 | Git history comparison with code-to-doc mapping |
doc_staleness_scorer.py | Score documentation freshness 0-100 | ~450 | Weighted multi-dimensional scoring |
api_doc_validator.py | Validate API docs a |