This file provides guidance for AI agents working with code in this repository.
This is a Python library (cchooks) for developing Claude Code hooks - user-defined shell commands that execute at various points in Claude Code's lifecycle. The library provides type-safe interfaces and utilities for all 8 hook types.
The codebase is organized into:
- Core Types:
src/cchooks/types.py- Type definitions and literals for hook events, tools, and decisions - Base Classes:
src/cchooks/contexts/base.py- Abstract base classes for contexts and outputs - Hook Contexts: Individual files in
src/cchooks/contexts/for each hook type - Utilities:
src/cchooks/utils.py- JSON parsing and validation helpers - Exceptions:
src/cchooks/exceptions.py- Custom exception hierarchy
- PreToolUse: Runs before tool execution, can approve/block tools
- PostToolUse: Runs after tool execution, can only provide feedback
- Notification: Processes notifications, no decision control
- Stop: Controls Claude's stopping behavior
- SubagentStop: Controls subagent stopping behavior
- PreCompact: Runs before transcript compaction
- UserPromptSubmit: After user prompt submission
- SessionStart: When Claude Code starts or resumes sessions
- SessionEnd: When Claude Code session ends
make test- Run all tests with coveragemake test-quick- Run tests without coverage (faster)uv run pytest tests/contexts/test_pre_tool_use.py -v- Run specific test fileuv run pytest tests/contexts/test_pre_tool_use.py::test_valid_context_creation -v- Run single test
make check- Run all checks (lint, type-check, format, test)make lint- Lint code (ruff check)make lint-fix- Auto-fix linting issuesmake format- Format code (ruff format)make type-check- Type checking (mypy)
make setup- Install dependenciesuv sync- Alternative dependency installmake build- Build packagemake clean- Clean build artifacts
- Type hints: Required for all function signatures and public attributes
- Imports: Group imports (stdlib, third-party, local) with blank lines
- Naming:
- Classes: PascalCase (PreToolUseContext)
- Functions/Methods: snake_case (validate_fields)
- Constants: UPPER_SNAKE_CASE (HOOK_TYPE_MAP)
- Private: _single_underscore
- Use custom exceptions from
cchooks.exceptions - Validate input data in context constructors
- Provide clear error messages with field names
- Use
HookValidationErrorfor missing required fields
- Test files:
test_*.pyintests/directory - Test classes:
Test* - Test methods:
test_* - Use pytest fixtures for sample data
- Mock stdin/stdout for I/O testing
- Main module:
src/cchooks/ - Contexts:
src/cchooks/contexts/(one file per hook type) - Tests: Mirror source structure in
tests/ - Type definitions:
src/cchooks/types.py
- Each hook has dedicated Context and Output classes
- Inherit from BaseHookContext/BaseHookOutput
- Validate required fields in constructor
- Use factory function
create_context()for instantiation - Follow JSON input/output patterns for Claude Code integration
- Simple Mode: Exit codes (0=success, 1=non-blocking, 2=blocking)
- Advanced Mode: JSON with
continue,decision,reasonfields
from cchooks import create_context
# Read from stdin automatically
c = create_context()
# Type-specific handling
if isinstance(c, PreToolUseContext):
if c.tool_name == "Write" and "password" in c.tool_input.get("file_path", ""):
c.output.simple_block("Refusing to write to password file")
else:
c.output.simple_approve()src/cchooks/__init__.py: Main entry point and factory functionsrc/cchooks/contexts/pre_tool_use.py: Most complex hook with approval decisionssrc/cchooks/types.py: Complete type system for Claude Code integrationdocs/what-is-cc-hook.md: Comprehensive documentation of Claude Code hooks
- When generating git commit messages, follow patterns like "feat:", "fix:", "docs:", "refactor:" and other best practices
- Use type hints throughout the codebase
- Write tests for all new functionality
- Run
make checkbefore committing changes - Follow existing naming conventions and code style
- Document public APIs with clear docstrings
src/cchooks/
├── __init__.py # Main factory function create_context()
├── types.py # Type definitions and literals
├── exceptions.py # Custom exception classes
├── utils.py # JSON parsing utilities
└── contexts/
├── __init__.py # Context exports
├── base.py # Abstract base classes
├── pre_tool_use.py # Pre-tool execution decisions
├── post_tool_use.py # Post-tool execution feedback
├── notification.py # Notification processing
├── stop.py # Stop behavior control
├── subagent_stop.py # Subagent stop control
├── pre_compact.py # Pre-compaction processing
├── user_prompt_submit.py # User prompt submission
└── session_start.py # Session start/resume
tests/
├── contexts/ # Context-specific tests
├── fixtures/ # Test data and helpers
├── integration/ # End-to-end tests
├── test_context_creation.py # Factory function tests
├── test_exceptions.py # Exception handling tests
├── test_types.py # Type validation tests
└── test_utils.py # Utility function tests
=== Content from CLAUDE.md (repaired 2025-08-04T10:54:24+08:00) ===
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a Python library (cchooks) for developing Claude Code hooks - user-defined shell commands that execute at various points in Claude Code's lifecycle. The library provides type-safe interfaces and utilities for all 8 hook types.
The codebase is organized into:
- Core Types:
src/cchooks/types.py- Type definitions and literals for hook events, tools, and decisions - Base Classes:
src/cchooks/contexts/base.py- Abstract base classes for contexts and outputs - Hook Contexts: Individual files in
src/cchooks/contexts/for each hook type - Utilities:
src/cchooks/utils.py- JSON parsing and validation helpers - Exceptions:
src/cchooks/exceptions.py- Custom exception hierarchy
- PreToolUse: Runs before tool execution, can approve/block tools
- PostToolUse: Runs after tool execution, can only provide feedback
- Notification: Processes notifications, no decision control
- Stop: Controls Claude's stopping behavior
- SubagentStop: Controls subagent stopping behavior
- PreCompact: Runs before transcript compaction
- UserPromptSubmit: After user prompt submission
- SessionStart: When Claude Code starts or resumes sessions
- SessionEnd: When Claude Code session ends
# Install dependencies
make setup
# Or with uv directly
uv sync# Run all tests with coverage
make test
# Run tests without coverage (faster)
make test-quick
# Run specific test file
uv run pytest tests/contexts/test_pre_tool_use.py -v
# Run single test
uv run pytest tests/contexts/test_pre_tool_use.py::test_pre_tool_use_approve -v# Check all code quality
make check
# Run individual checks
make lint # ruff check
make type-check # mypy
make format-check # ruff format --check
make test # pytest with coverage
# Auto-fix issues
make lint-fix # ruff check --fix
make format # ruff format# Build package
make build
# Clean build artifacts
make clean
# Full release preparation
make release-check# Install in development mode
make install-dev
# Show dependency tree
make deps-tree
# Update lockfile
make lockfrom cchooks import create_context
# Read from stdin automatically
c = create_context()
# Type-specific handling
if isinstance(c, PreToolUseContext):
if c.tool_name == "Write" and "password" in c.tool_input.get("file_path", ""):
c.output.simple_block("Refusing to write to password file")
else:
c.output.simple_approve()exit 0: Success/approveexit 1: Non-blocking errorexit 2: Blocking error
- Use context-specific output methods
- Each context provides specialized decision methods
- JSON output includes
continue,decision,reasonfields
src/cchooks/
├── __init__.py # Main factory function create_context()
├── types.py # Type definitions and literals
├── exceptions.py # Custom exception classes
├── utils.py # JSON parsing utilities
└── contexts/
├── __init__.py # Context exports
├── base.py # Abstract base classes
├── pre_tool_use.py # Pre-tool execution decisions
├── post_tool_use.py # Post-tool execution feedback
├── notification.py # Notification processing
├── stop.py # Stop behavior control
├── subagent_stop.py # Subagent stop control
└── pre_compact.py # Pre-compaction processing
tests/
├── contexts/ # Context-specific tests
├── fixtures/ # Test data and helpers
├── integration/ # End-to-end tests
├── test_context_creation.py # Factory function tests
├── test_exceptions.py # Exception handling tests
├── test_types.py # Type validation tests
└── test_utils.py # Utility function tests
src/cchooks/__init__.py: Main entry point and factory functionsrc/cchooks/contexts/pre_tool_use.py: Most complex hook with approval decisionssrc/cchooks/types.py: Complete type system for Claude Code integrationdocs/what-is-cc-hook.md: Comprehensive documentation of Claude Code hooks
- When generating git commit messages, follow patterns like "feat:", "fix:", "docs:", "refactor:" and other best practices
- Use type hints throughout the codebase
- Write tests for all new functionality
- Run
make checkbefore committing changes - Follow existing naming conventions and code style
- Document public APIs with clear docstrings === End CLAUDE.md content ===
=== Content from CRUSH.md (repaired 2025-08-04T10:54:24+08:00) ===
Python library for developing Claude Code hooks - user-defined shell commands that execute at various points in Claude Code's lifecycle. Provides type-safe interfaces for 9 hook types.
make test- Run all tests with coveragemake test-quick- Run tests without coverage (faster)uv run pytest tests/contexts/test_pre_tool_use.py -v- Run specific test fileuv run pytest tests/contexts/test_pre_tool_use.py::test_valid_context_creation -v- Run single test
make check- Run all checks (lint, type-check, format, test)make lint- Lint code (ruff check)make lint-fix- Auto-fix linting issuesmake format- Format code (ruff format)make type-check- Type checking (mypy)
make setup- Install dependenciesuv sync- Alternative dependency installmake build- Build packagemake clean- Clean build artifacts
- PreToolUse: Before tool execution, can approve/block
- PostToolUse: After tool execution, feedback only
- Notification: Process notifications, no decisions
- Stop: Control Claude stopping behavior
- SubagentStop: Control subagent stopping
- PreCompact: Before transcript compaction
- UserPromptSubmit: After user prompt submission
- SessionStart: When Claude Code starts or resumes sessions
- SessionEnd: When Claude Code session ends
from cchooks import create_context
c = create_context()
if isinstance(c, PreToolUseContext):
if c.tool_name == "Write" and "password" in c.tool_input.get("file_path", ""):
c.output.simple_block("Refusing to write to password file")
else:
c.output.simple_approve()- Type hints: Required for all function signatures and public attributes
- Imports: Group imports (stdlib, third-party, local) with blank lines
- Naming:
- Classes: PascalCase (PreToolUseContext)
- Functions/Methods: snake_case (validate_fields)
- Constants: UPPER_SNAKE_CASE (HOOK_TYPE_MAP)
- Private: _single_underscore
- Use custom exceptions from
cchooks.exceptions - Validate input data in context constructors
- Provide clear error messages with field names
- Use
HookValidationErrorfor missing required fields
- Test files:
test_*.pyintests/directory - Test classes:
Test* - Test methods:
test_* - Use pytest fixtures for sample data
- Mock stdin/stdout for I/O testing
- Main module:
src/cchooks/ - Contexts:
src/cchooks/contexts/(one file per hook type) - Tests: Mirror source structure in
tests/ - Type definitions:
src/cchooks/types.py
- Each hook has dedicated Context and Output classes
- Inherit from BaseHookContext/BaseHookOutput
- Validate required fields in constructor
- Use factory function
create_context()for instantiation - Follow JSON input/output patterns for Claude Code integration
- Simple Mode: Exit codes (0=success, 1=non-blocking, 2=blocking)
- Advanced Mode: JSON with
continue,decision,reasonfields
src/cchooks/__init__.py: Main entry point and factory functionsrc/cchooks/contexts/pre_tool_use.py: Most complex hook with approval decisionssrc/cchooks/types.py: Complete type system for Claude Code integration === End CRUSH.md content ===