hook.define
# hook.define
What this skill does
# hook.define
## Overview
**hook.define** is a Betty skill that creates and registers validation hooks for Claude Code. Hooks enable automatic validation and policy enforcement by triggering skills on events like file edits, commits, and pushes.
## Purpose
Transform manual validation into automatic safety rails:
- **Before**: Developers must remember to run validation
- **After**: Validation happens automatically on every file edit
## Usage
### Basic Usage
```bash
python skills/hook.define/hook_define.py <event> <command> [options]
```
### Parameters
| Parameter | Required | Description | Example |
|-----------|----------|-------------|---------|
| `event` | Yes | Hook event trigger | `on_file_edit` |
| `command` | Yes | Command to execute | `api.validate {file_path} zalando` |
| `--pattern` | No | File pattern to match | `*.openapi.yaml` |
| `--blocking` | No | Block on failure (default: true) | `true` |
| `--timeout` | No | Timeout in ms (default: 30000) | `10000` |
| `--description` | No | Human-readable description | `Validate OpenAPI specs` |
### Valid Events
| Event | Triggers When | Use Case |
|-------|---------------|----------|
| `on_file_edit` | File is edited in editor | Syntax validation |
| `on_file_save` | File is saved | Code generation |
| `on_commit` | Git commit attempted | Breaking change detection |
| `on_push` | Git push attempted | Full validation suite |
| `on_tool_use` | Any tool is used | Audit logging |
| `on_agent_start` | Agent begins execution | Context injection |
| `on_workflow_end` | Workflow completes | Cleanup/notification |
## Examples
### Example 1: Validate OpenAPI Specs on Edit
```bash
python skills/hook.define/hook_define.py \
on_file_edit \
"python betty/skills/api.validate/api_validate.py {file_path} zalando" \
--pattern="*.openapi.yaml" \
--blocking=true \
--timeout=10000 \
--description="Validate OpenAPI specs against Zalando guidelines"
```
**Result**: Every time a `*.openapi.yaml` file is edited, it's automatically validated against Zalando guidelines. If validation fails, the edit is blocked.
### Example 2: Check Breaking Changes on Commit
```bash
python skills/hook.define/hook_define.py \
on_commit \
"python betty/skills/api.compatibility/check_breaking_changes.py {file_path}" \
--pattern="specs/**/*.yaml" \
--blocking=true \
--description="Prevent commits with breaking API changes"
```
**Result**: Commits are blocked if they contain breaking API changes.
### Example 3: Regenerate Models on Save
```bash
python skills/hook.define/hook_define.py \
on_file_save \
"python betty/skills/api.generate-models/auto_generate.py {file_path}" \
--pattern="specs/*.openapi.yaml" \
--blocking=false \
--description="Auto-regenerate models when specs change"
```
**Result**: When an OpenAPI spec is saved, models are regenerated automatically (non-blocking).
### Example 4: Audit Trail for All Tool Use
```bash
python skills/hook.define/hook_define.py \
on_tool_use \
"python betty/skills/audit.log/log_api_change.py {tool_name}" \
--blocking=false \
--description="Log all tool usage for compliance"
```
**Result**: All tool usage is logged for audit trails (non-blocking).
## Output
### Success Response
```json
{
"status": "success",
"data": {
"hook_config": {
"name": "api-validate-all-openapi-yaml",
"command": "python betty/skills/api.validate/api_validate.py {file_path} zalando",
"blocking": true,
"timeout": 10000,
"when": {
"pattern": "*.openapi.yaml"
},
"description": "Validate OpenAPI specs against Zalando guidelines"
},
"hooks_file_path": "/home/user/betty/.claude/hooks.yaml",
"event": "on_file_edit",
"total_hooks": 1
}
}
```
### Generated Hooks File
The skill creates/updates `.claude/hooks.yaml`:
```yaml
hooks:
on_file_edit:
- name: api-validate-all-openapi-yaml
command: python betty/skills/api.validate/api_validate.py {file_path} zalando
blocking: true
timeout: 10000
when:
pattern: "*.openapi.yaml"
description: Validate OpenAPI specs against Zalando guidelines
```
## How It Works
1. **Load Existing Hooks**: Reads `.claude/hooks.yaml` if it exists
2. **Create Hook Config**: Builds configuration from parameters
3. **Add or Update**: Adds new hook or updates existing one with same name
4. **Save**: Writes updated configuration back to `.claude/hooks.yaml`
## Benefits
### For Developers
- ✅ **Instant feedback**: Errors caught immediately, not at commit time
- ✅ **No discipline required**: Validation happens automatically
- ✅ **Consistent quality**: Standards enforced, not suggested
### For Teams
- ✅ **Enforced standards**: Can't save non-compliant code
- ✅ **Reduced review time**: Automated checks before human review
- ✅ **Onboarding**: New developers can't accidentally break standards
### For Organizations
- ✅ **Compliance**: Policies enforced at tool level
- ✅ **Audit trail**: Every validation is logged
- ✅ **Risk reduction**: Catch issues early, not in production
## Integration with Betty
### Use in Workflows
```yaml
# workflows/setup_api_validation.yaml
steps:
- skill: hook.define
args:
- "on_file_edit"
- "api.validate {file_path} zalando"
- "--pattern=*.openapi.yaml"
- "--blocking=true"
```
### Use in Agents
Agents can dynamically create hooks based on project needs:
```python
# Agent detects OpenAPI specs in project
# Automatically sets up validation hooks
```
## File Pattern Examples
| Pattern | Matches |
|---------|---------|
| `*.openapi.yaml` | All OpenAPI files in current directory |
| `*.asyncapi.yaml` | All AsyncAPI files in current directory |
| `specs/**/*.yaml` | All YAML files in specs/ and subdirectories |
| `src/**/*.ts` | All TypeScript files in src/ and subdirectories |
| `**/*.py` | All Python files anywhere |
## Blocking vs Non-Blocking
### Blocking Hooks (blocking: true)
- Operation is **paused** until hook completes
- If hook **fails**, operation is **aborted**
- Use for: Validation, compliance checks, breaking change detection
### Non-Blocking Hooks (blocking: false)
- Hook runs **asynchronously**
- Operation **continues** regardless of hook result
- Use for: Logging, notifications, background tasks
## Timeout Considerations
| Operation | Suggested Timeout | Reasoning |
|-----------|-------------------|-----------|
| Syntax validation | 5,000 - 10,000 ms | Fast, local checks |
| Zally API call | 10,000 - 30,000 ms | Network latency |
| Model generation | 30,000 - 60,000 ms | Compilation time |
| Full test suite | 300,000 ms (5 min) | Comprehensive testing |
## Error Handling
### Hook Execution Failed
If a blocking hook fails:
```
❌ Hook 'validate-openapi' failed:
- Missing required field: info.x-api-id
- Property 'userId' should use snake_case
Operation blocked. Fix errors and try again.
```
### Hook Timeout
If a hook exceeds timeout:
```
⚠️ Hook 'validate-openapi' timed out after 10000ms
Operation blocked for safety.
```
## Dependencies
- **PyYAML**: Required for YAML file handling
```bash
pip install pyyaml
```
- **context.schema**: For validation rule definitions
## Files Created
- `.claude/hooks.yaml` - Hook configurations for Claude Code
## See Also
- [api.validate](../api.validate/SKILL.md) - API validation skill
- [Betty Architecture](../../docs/betty-architecture.md) - Five-layer model
- [API-Driven Development](../../docs/api-driven-development.md) - Complete guide
- [Claude Code Hooks Documentation](https://docs.claude.com/en/docs/claude-code/hooks)
## Version
**0.1.0** - Initial implementation with basic hook definition support
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.