Overview
Define, edit, and manage event-driven hooks that automatically activate hatch3r agents when specific project events occur. Hook definitions are tool-agnostic — the adapter pipeline translates them into tool-native configurations duringnpx hatch3r sync.
When to Use
- You want to automate agent activation on specific events
- You need lint checks or tests to run before commits
- You want auto-diagnosis when CI/CD fails
- You need context loading at session start
- You want to trigger agents on file saves, merges, or releases
How It Works
Discover Current State
- Checks
/.agents/hooks/for existing hook definition files - Reads
/.agents/hatch.jsonfor configured tools and features - Lists existing hooks with their event, agent, and conditions
Define Hook
For adding a new hook:
- Selects an event (pre-commit, post-merge, ci-failure, file-save, etc.)
- Selects an agent to activate (lint-fixer, test-writer, reviewer, security-auditor, etc.)
- Optionally defines conditions (glob patterns, branch patterns)
- Writes hook definition file to
/.agents/hooks/{event}-{agent}.md
Edit Existing Hook
- Lists all hooks and asks which to edit
- Shows current definition
- Updates event, agent, conditions, or description
Remove a Hook
- Lists all hooks and asks which to remove
- Deletes the hook definition file
- Tool-specific generated files are cleaned up on next
npx hatch3r sync
Available Events
| Event | Description | Use Cases |
|---|---|---|
pre-commit | Before a commit is created | Lint checks, secret scanning, test running |
post-merge | After a branch merge | Dependency updates, migration checks, notification |
ci-failure | When CI/CD pipeline fails | Auto-diagnosis, fix suggestions |
file-save | When a file is saved | Auto-formatting, auto-testing, live validation |
session-start | When a new coding session starts | Context loading, status checks |
pre-push | Before pushing to remote | Full test suite, build verification |
pre-implementation | Before implementing a sub-issue | Context loading, spec review, learning consultation |
post-implementation | After implementing a sub-issue | Learning capture, code quality check, doc updates |
pre-review | Before code review starts | Checklist generation, context preparation |
post-review | After code review completes | Fix tracking, learning capture, approval notifications |
pre-release | Before a release workflow | Changelog verification, version validation, dependency audit |
post-release | After a release completes | Monitoring setup, notification, deploy verification |
pre-test | Before test execution | Test environment setup, fixture preparation |
post-test | After test execution completes | Coverage reporting, flaky test detection, result analysis |
on-error | When any workflow step fails | Error diagnosis, auto-retry, escalation, incident logging |
on-context-switch | When agent context is refreshed | State persistence, handoff documentation |
on-dependency-change | When dependencies are added/updated/removed | Security audit, compatibility check, license validation |
on-security-finding | When a security issue is discovered | Alert escalation, auto-fix suggestions, incident creation |
Available Agents
lint-fixer— Automatic lint error resolutiontest-writer— Test generation for new or changed codereviewer— Code review and quality checkssecurity-auditor— Security vulnerability scanningci-watcher— CI/CD pipeline monitoring and diagnosisa11y-auditor— Accessibility compliance checksperf-profiler— Performance analysis and optimizationdependency-auditor— Dependency security and update checksdocs-writer— Documentation generation and updates
Hook Definition Format
Hook files are created at/.agents/hooks/{event}-{agent}.md with this structure:
Conditions (Optional)
- Glob patterns: Which files trigger this hook (e.g.,
src/**/*.ts,*.css) - Branch patterns: Which branches (e.g.,
main,release/*)
Custom Events
Define project-specific hook events beyond the built-in types:- Event naming:
custom:{domain}:{action}(e.g.,custom:billing:subscription-change) - Event registration: Add custom events to
hatch.jsonunderhooks.customEvents - Event triggering: Agents trigger custom events via
emit-hook custom:{name} - Event payload: Custom events can pass structured data via JSON payload
Custom Event Definition in hatch.json
Hook Chaining
Hooks can trigger other hooks in sequence:- Chain definition: Define ordered hook chains in
hatch.jsonunderhooks.chains - Execution order: Hooks in a chain execute sequentially; failure in any hook stops the chain
- Error handling: Chain-level error handlers can catch and handle failures
- Conditional chaining: Hooks can conditionally trigger next hook based on output (pass/fail/skip)
Chain Definition in hatch.json
Hook Execution Ordering
When multiple hooks are registered for the same event:- Priority: Hooks have a priority field (1-100, lower runs first, default 50)
- Parallel vs Sequential: Hooks at the same priority level run in parallel; different priority levels run sequentially
- Timeout: Each hook has a configurable timeout (default 30 seconds). Timed-out hooks are reported as failures.
- Isolation: Each hook runs in its own context
Priority Configuration
Addpriority and timeout to hook frontmatter:
Execution Example
Forpre-commit with three hooks:
lint-fixer(priority 10) — runs firstsecurity-auditor(priority 20) — runs secondtest-writer(priority 50) andreviewer(priority 50) — run in parallel, third
Tool-Specific Behavior
Claude Code
session-start→SessionStarthook- Other events (pre-commit, post-merge, etc.) may use no-op or alternative strategy
- Note: Claude Code’s native hooks (PreToolUse, PostToolUse, SubagentStart) are tool-lifecycle events and do NOT map to git/project events
Cursor
- Maps to glob-based rule activation in
.cursor/rules/ - Creates
.mdcrule files in.cursor/rules/hatch3r-hook-*.mdc
Other Tools
- Hook definitions stored; sync when adapter support is added
Sync to Tools
After defining hooks, runnpx hatch3r sync to generate tool-specific configurations:
- Claude Code: Hook documentation appended to managed section of
CLAUDE.md - Cursor: Glob-based
.mdcrule files in.cursor/rules/hatch3r-hook-*.mdc - Others: No-op (hook definitions stored for future adapter support)
Error Handling
/.agents/hooks/doesn’t exist: Creates it automatically- Invalid event type: Warns and shows the valid events table
- Agent not found: Warns but allows (agent may be added later)
- Adapter doesn’t support hooks: Generates hook definition anyway, warns that sync for that tool is a no-op
- Duplicate hook ID: Warns and asks to choose a different name or overwrite
Guardrails
- Hook definitions are tool-agnostic — the adapters handle translation
- Never deletes hook files without explicit user confirmation
- Always validates event names against the known events list
- Hook IDs must be unique across all hook definitions
- The command creates hook DEFINITIONS only — actual hook registration happens via
npx hatch3r sync - Does not modify adapter output files directly — they are managed by the sync pipeline

