Overview
Hooks are executable scripts that AI tools call at specific lifecycle points. hcom provides hooks for Claude Code, Gemini CLI, Codex, and OpenCode.Hook Flow
The hook system bridges tool execution with the hcom event database:Hook Types by Tool
Claude Code Hooks
Claude hooks read from stdin and are installed at~/.claude/hooks/:
- sessionstart
- poll
- pre
- post
- sessionend
- subagent-start
- subagent-stop
When: Session initializationPayload:Actions:
- Bind session to instance name
- Initialize instance in DB
- Inject bootstrap message
- Set status to “listening”
Gemini CLI Hooks
Gemini hooks read from stdin and are installed at~/.gemini/hooks/:
- gemini-sessionstart
- gemini-beforeagent
- gemini-afteragent
- gemini-beforetool
- gemini-aftertool
- gemini-notification
- gemini-sessionend
Same as Claude sessionstart.
Codex Hooks
Codex hook reads from argv (JSON payload as arg) and is installed at~/.codex/hooks/notify.sh:
- codex-notify
When: After agent turn completesPayload (argv[2]):Actions:
- Bind thread-id to instance
- Set status to “listening”
- Update heartbeat
OpenCode Hooks
OpenCode hooks are native Rust functions, not shell scripts:- opencode-start
- opencode-status
- opencode-read
- opencode-stop
When: Session startActions:
- Bind session to instance
- Initialize instance
- Return bootstrap message
Hook Results
Hooks return results via exit code and stdout:Allow (exit 0)
Operation proceeds normally:Block (exit 2)
Operation blocked (used for message delivery):Update Input (exit 0)
Modify tool arguments before execution:Hook Payload Normalization
Each tool has different JSON formats. TheHookPayload struct normalizes them:
Hook Installation
Automatic Installation
Hooks are installed on first launch:Manual Installation
Hook Locations
Hook Context
Hooks access hcom via environment variables:Shared Hook Functions
Core hook logic is shared across tools:Identity Initialization
Message Delivery
Bootstrap Injection
Session Finalization
Hook Performance
Execution Time
- Rust native: < 50ms (open DB, query, close)
- Stdin parsing: < 10ms (JSON deserialization)
- Event logging: < 5ms (SQLite insert)
- Message formatting: < 20ms (DB query + string ops)
Optimization
- Connection reuse: WAL mode allows concurrent reads
- Index usage: Queries use
(type, instance)index - Lazy formatting: Only format messages when needed
- Batch operations: Group DB writes in single transaction
Hook Development
Creating hooks for new tools:
Example hook handler: