Gamified code documentation enforcer with cognitive complexity analysis
Project description
๐ง Cognitive Guard
Gamify your code documentation. Block commits that hurt brains.
Cognitive Guard is a revolutionary CLI tool that analyzes the cognitive complexity of your code in real-time and enforces documentation where it matters most. Instead of nagging you about every missing docstring, it focuses on the code that's genuinely hard to understand.
๐ฏ The Problem
- Traditional linters check for missing docs everywhere (boring!)
- Complex code without documentation causes cognitive overload
- Developers skip documentation because it feels like busywork
- No feedback loop between code complexity and documentation requirements
โจ The Solution
Cognitive Guard:
- ๐ฎ Gamifies documentation with Brain Scores and achievements
- ๐งฎ Calculates cognitive complexity using AST traversal
- ๐ฏ Targets only complex code (Score > configurable threshold)
- ๐ซ Blocks commits for undocumented complex functions
- ๐ฅ๏ธ Launches an interactive TUI to fix issues on the spot
- ๐ Tracks your documentation coverage over time
๐ Quick Start
Installation
pip install cognitive-guard
Initialize in Your Project
cd your-project
cognitive-guard init
This installs the git pre-commit hook and creates a .cognitive-guard.yml config file.
Configuration
Edit .cognitive-guard.yml to customize:
# Complexity threshold (1-20, default: 10)
complexity_threshold: 10
# Target documentation coverage (0.0-1.0, default: 0.9)
target_coverage: 0.9
# Languages to analyze
languages:
- python
- javascript
- typescript
- java
# Patterns to ignore
ignore:
- "**/test_*.py"
- "**/*.test.js"
- "**/migrations/**"
# Gamification settings
gamification:
enabled: true
show_achievements: true
track_stats: true
๐ก How It Works
- Pre-Commit Hook: Cognitive Guard runs automatically before each commit
- AST Analysis: Parses staged files and builds Abstract Syntax Trees
- Brain Score Calculation: Computes cognitive complexity for each function
- Documentation Check: Verifies complex functions (Score > threshold) have docs
- Interactive TUI: If violations found, launches a beautiful terminal UI to fix them
- Commit Decision: Allows commit only when documentation requirements are met
๐ฎ Brain Score Metrics
Cognitive complexity is calculated based on:
- Control Flow: if/else, switch, loops (+1 each)
- Nesting: Each level of nesting (+1)
- Boolean Operators: &&, ||, ?? in conditions (+1)
- Recursion: Recursive calls (+2)
- Exception Handling: try/catch blocks (+1)
Score Ranges
- 0-5: ๐ข Simple - No docs required
- 6-10: ๐ก Moderate - Docs recommended
- 11-15: ๐ Complex - Docs required
- 16+: ๐ด Very Complex - Detailed docs required + consider refactoring
๐ฅ๏ธ CLI Commands
Check Staged Changes
cognitive-guard check
Analyzes staged changes and reports complexity scores.
Scan Entire Codebase
cognitive-guard scan
Full codebase analysis with coverage report.
Interactive TUI
cognitive-guard tui
Launch the interactive documentation assistant.
Stats & Achievements
cognitive-guard stats
View your documentation journey and achievements.
Update Hook
cognitive-guard update-hook
Update the git hook to the latest version.
๐ Gamification Features
Achievements
- ๐ First Steps: Document your first complex function
- ๐ฏ Marksman: Achieve 90% documentation coverage
- ๐ง Mind Reader: Document 10 functions with Score > 15
- ๐ Speed Demon: Document 5 functions in one session
- ๐ฏ Perfectionist: Achieve 100% documentation coverage
Stats Tracking
- Total functions analyzed
- Documentation coverage %
- Average Brain Score
- Most complex function
- Documentation streak (consecutive days)
๐ ๏ธ Advanced Usage
Bypass Hook (Emergency)
git commit --no-verify
Custom Thresholds Per File
# cognitive-guard: threshold=15
def ultra_complex_function():
"""This function needs threshold 15 instead of default 10"""
pass
CI/CD Integration
# .github/workflows/cognitive-guard.yml
name: Cognitive Guard
on: [pull_request]
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install Cognitive Guard
run: pip install cognitive-guard
- name: Check Documentation
run: cognitive-guard scan --fail-under 0.9
๐งช Development
Setup Development Environment
git clone https://github.com/yourusername/cognitive-guard.git
cd cognitive-guard
pip install -e ".[dev]"
pre-commit install
Run Tests
pytest
Code Quality
black cognitive_guard tests
ruff cognitive_guard tests
mypy cognitive_guard
๐ Architecture
cognitive_guard/
โโโ core/ # Core complexity calculation engine
โโโ parsers/ # Language-specific AST parsers
โโโ analyzers/ # Complexity analyzers per language
โโโ tui/ # Interactive terminal UI (Textual)
โโโ hooks/ # Git hook integration
โโโ utils/ # Shared utilities
๐ค Contributing
Contributions are welcome! Please read CONTRIBUTING.md for guidelines.
๐ License
MIT License - see LICENSE for details.
๐ Acknowledgments
- Inspired by cognitive complexity research
- Built with Textual for the TUI
- Uses Radon and Lizard for complexity analysis
๐ Support
- ๐ Documentation
- ๐ Issue Tracker
- ๐ฌ Discussions
Remember: Complex code without documentation is like a puzzle missing the picture on the box. ๐งฉ
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cognitive_guard_publish-0.1.0.tar.gz.
File metadata
- Download URL: cognitive_guard_publish-0.1.0.tar.gz
- Upload date:
- Size: 76.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ab22b176b5a9c12200a33344bf0e2f5e0ffc9a0d19f2fe45757197acf38282e
|
|
| MD5 |
c24c58025680d5e3921df7fba0e29170
|
|
| BLAKE2b-256 |
8fa8b4093aa689a847439870d27ccee78968cab5da0f73e23c3f71490f3b1ef8
|
File details
Details for the file cognitive_guard_publish-0.1.0-py3-none-any.whl.
File metadata
- Download URL: cognitive_guard_publish-0.1.0-py3-none-any.whl
- Upload date:
- Size: 23.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d03448c33db3771ebc36cc4d02d93b2f22a1551f3b32420312498b7592630d61
|
|
| MD5 |
8511ad49132b79e21bd5101d4c3ce6cd
|
|
| BLAKE2b-256 |
b46537cd5765ab00b0ed3c5f708ce270c4b61de7c151175a7c5a503dcf6146df
|