Skip to main content

🧠 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

  1. Pre-Commit Hook: Cognitive Guard runs automatically before each commit
  2. AST Analysis: Parses staged files and builds Abstract Syntax Trees
  3. Brain Score Calculation: Computes cognitive complexity for each function
  4. Documentation Check: Verifies complex functions (Score > threshold) have docs
  5. Interactive TUI: If violations found, launches a beautiful terminal UI to fix them
  6. 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


Remember: Complex code without documentation is like a puzzle missing the picture on the box. 🧩

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cognitive_guard_publish-0.1.0.tar.gz (76.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cognitive_guard_publish-0.1.0-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

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

Hashes for cognitive_guard_publish-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3ab22b176b5a9c12200a33344bf0e2f5e0ffc9a0d19f2fe45757197acf38282e
MD5 c24c58025680d5e3921df7fba0e29170
BLAKE2b-256 8fa8b4093aa689a847439870d27ccee78968cab5da0f73e23c3f71490f3b1ef8

See more details on using hashes here.

File details

Details for the file cognitive_guard_publish-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cognitive_guard_publish-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d03448c33db3771ebc36cc4d02d93b2f22a1551f3b32420312498b7592630d61
MD5 8511ad49132b79e21bd5101d4c3ce6cd
BLAKE2b-256 b46537cd5765ab00b0ed3c5f708ce270c4b61de7c151175a7c5a503dcf6146df

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page