Hooks provide an extensible event-driven system for automating actions in response to agent commands and events. Hooks are automatically discovered from directories and can be managed via CLI commands, similar to how skills work in CoderClaw.
Hooks are small scripts that run when something happens. There are two kinds:
/new, /reset, /stop, or lifecycle events.coderclaw webhooks for Gmail helper commands.Hooks can also be bundled inside plugins; see Plugins.
Common uses:
If you can write a small TypeScript function, you can write a hook. Hooks are discovered automatically, and you enable or disable them via the CLI.
The hooks system allows you to:
/new is issuedCoderClaw ships with four bundled hooks that are automatically discovered:
~/.coderclaw/workspace/memory/) when you issue /newagent:bootstrap~/.coderclaw/logs/commands.logBOOT.md when the gateway starts (requires internal hooks enabled)List available hooks:
coderclaw hooks list
Enable a hook:
coderclaw hooks enable session-memory
Check hook status:
coderclaw hooks check
Get detailed information:
coderclaw hooks info session-memory
During onboarding (coderclaw onboard), youβll be prompted to enable recommended hooks. The wizard automatically discovers eligible hooks and presents them for selection.
Hooks are automatically discovered from three directories (in order of precedence):
<workspace>/hooks/ (per-agent, highest precedence)~/.coderclaw/hooks/ (user-installed, shared across workspaces)<coderclaw>/dist/hooks/bundled/ (shipped with CoderClaw)Managed hook directories can be either a single hook or a hook pack (package directory).
Each hook is a directory containing:
my-hook/
βββ HOOK.md # Metadata + documentation
βββ handler.ts # Handler implementation
Hook packs are standard npm packages that export one or more hooks via coderclaw.hooks in
package.json. Install them with:
coderclaw hooks install <path-or-spec>
Npm specs are registry-only (package name + optional version/tag). Git/URL/file specs are rejected.
Example package.json:
{
"name": "@acme/my-hooks",
"version": "0.1.0",
"coderclaw": {
"hooks": ["./hooks/my-hook", "./hooks/other-hook"]
}
}
Each entry points to a hook directory containing HOOK.md and handler.ts (or index.ts).
Hook packs can ship dependencies; they will be installed under ~/.coderclaw/hooks/<id>.
Security note: coderclaw hooks install installs dependencies with npm install --ignore-scripts
(no lifecycle scripts). Keep hook pack dependency trees βpure JS/TSβ and avoid packages that rely
on postinstall builds.
The HOOK.md file contains metadata in YAML frontmatter plus Markdown documentation:
---
name: my-hook
description: "Short description of what this hook does"
homepage: https://docs.coderclaw.ai/automation/hooks#my-hook
metadata:
{ "coderclaw": { "emoji": "π", "events": ["command:new"], "requires": { "bins": ["node"] } } }
---
# My Hook
Detailed documentation goes here...
## What It Does
- Listens for `/new` commands
- Performs some action
- Logs the result
## Requirements
- Node.js must be installed
## Configuration
No configuration needed.
The metadata.coderclaw object supports:
emoji: Display emoji for CLI (e.g., "πΎ")events: Array of events to listen for (e.g., ["command:new", "command:reset"])export: Named export to use (defaults to "default")homepage: Documentation URLrequires: Optional requirements
bins: Required binaries on PATH (e.g., ["git", "node"])anyBins: At least one of these binaries must be presentenv: Required environment variablesconfig: Required config paths (e.g., ["workspace.dir"])os: Required platforms (e.g., ["darwin", "linux"])always: Bypass eligibility checks (boolean)install: Installation methods (for bundled hooks: [{"id":"bundled","kind":"bundled"}])The handler.ts file exports a HookHandler function:
import type { HookHandler } from "../../src/hooks/hooks.js";
const myHandler: HookHandler = async (event) => {
// Only trigger on 'new' command
if (event.type !== "command" || event.action !== "new") {
return;
}
console.log(`[my-hook] New command triggered`);
console.log(` Session: ${event.sessionKey}`);
console.log(` Timestamp: ${event.timestamp.toISOString()}`);
// Your custom logic here
// Optionally send message to user
event.messages.push("β¨ My hook executed!");
};
export default myHandler;
Each event includes:
{
type: 'command' | 'session' | 'agent' | 'gateway' | 'message',
action: string, // e.g., 'new', 'reset', 'stop', 'received', 'sent'
sessionKey: string, // Session identifier
timestamp: Date, // When the event occurred
messages: string[], // Push messages here to send to user
context: {
// Command events:
sessionEntry?: SessionEntry,
sessionId?: string,
sessionFile?: string,
commandSource?: string, // e.g., 'whatsapp', 'telegram'
senderId?: string,
workspaceDir?: string,
bootstrapFiles?: WorkspaceBootstrapFile[],
cfg?: CoderClawConfig,
// Message events (see Message Events section for full details):
from?: string, // message:received
to?: string, // message:sent
content?: string,
channelId?: string,
success?: boolean, // message:sent
}
}
Triggered when agent commands are issued:
command: All command events (general listener)command:new: When /new command is issuedcommand:reset: When /reset command is issuedcommand:stop: When /stop command is issuedagent:bootstrap: Before workspace bootstrap files are injected (hooks may mutate context.bootstrapFiles)Triggered when the gateway starts:
gateway:startup: After channels start and hooks are loadedTriggered when messages are received or sent:
message: All message events (general listener)message:received: When an inbound message is received from any channelmessage:sent: When an outbound message is successfully sentMessage events include rich context about the message:
// message:received context
{
from: string, // Sender identifier (phone number, user ID, etc.)
content: string, // Message content
timestamp?: number, // Unix timestamp when received
channelId: string, // Channel (e.g., "whatsapp", "telegram", "discord")
accountId?: string, // Provider account ID for multi-account setups
conversationId?: string, // Chat/conversation ID
messageId?: string, // Message ID from the provider
metadata?: { // Additional provider-specific data
to?: string,
provider?: string,
surface?: string,
threadId?: string,
senderId?: string,
senderName?: string,
senderUsername?: string,
senderE164?: string,
}
}
// message:sent context
{
to: string, // Recipient identifier
content: string, // Message content that was sent
success: boolean, // Whether the send succeeded
error?: string, // Error message if sending failed
channelId: string, // Channel (e.g., "whatsapp", "telegram", "discord")
accountId?: string, // Provider account ID
conversationId?: string, // Chat/conversation ID
messageId?: string, // Message ID returned by the provider
}
import type { HookHandler } from "../../src/hooks/hooks.js";
import { isMessageReceivedEvent, isMessageSentEvent } from "../../src/hooks/internal-hooks.js";
const handler: HookHandler = async (event) => {
if (isMessageReceivedEvent(event)) {
console.log(`[message-logger] Received from ${event.context.from}: ${event.context.content}`);
} else if (isMessageSentEvent(event)) {
console.log(`[message-logger] Sent to ${event.context.to}: ${event.context.content}`);
}
};
export default handler;
These hooks are not event-stream listeners; they let plugins synchronously adjust tool results before CoderClaw persists them.
tool_result_persist: transform tool results before they are written to the session transcript. Must be synchronous; return the updated tool result payload or undefined to keep it as-is. See Agent Loop.Planned event types:
session:start: When a new session beginssession:end: When a session endsagent:error: When an agent encounters an error<workspace>/hooks/): Per-agent, highest precedence~/.coderclaw/hooks/): Shared across workspacesmkdir -p ~/.coderclaw/hooks/my-hook
cd ~/.coderclaw/hooks/my-hook
---
name: my-hook
description: "Does something useful"
metadata: { "coderclaw": { "emoji": "π―", "events": ["command:new"] } }
---
# My Custom Hook
This hook does something useful when you issue `/new`.
import type { HookHandler } from "../../src/hooks/hooks.js";
const handler: HookHandler = async (event) => {
if (event.type !== "command" || event.action !== "new") {
return;
}
console.log("[my-hook] Running!");
// Your logic here
};
export default handler;
# Verify hook is discovered
coderclaw hooks list
# Enable it
coderclaw hooks enable my-hook
# Restart your gateway process (menu bar app restart on macOS, or restart your dev process)
# Trigger the event
# Send /new via your messaging channel
{
"hooks": {
"internal": {
"enabled": true,
"entries": {
"session-memory": { "enabled": true },
"command-logger": { "enabled": false }
}
}
}
}
Hooks can have custom configuration:
{
"hooks": {
"internal": {
"enabled": true,
"entries": {
"my-hook": {
"enabled": true,
"env": {
"MY_CUSTOM_VAR": "value"
}
}
}
}
}
}
Load hooks from additional directories:
{
"hooks": {
"internal": {
"enabled": true,
"load": {
"extraDirs": ["/path/to/more/hooks"]
}
}
}
}
The old config format still works for backwards compatibility:
{
"hooks": {
"internal": {
"enabled": true,
"handlers": [
{
"event": "command:new",
"module": "./hooks/handlers/my-handler.ts",
"export": "default"
}
]
}
}
}
Note: module must be a workspace-relative path. Absolute paths and traversal outside the workspace are rejected.
Migration: Use the new discovery-based system for new hooks. Legacy handlers are loaded after directory-based hooks.
# List all hooks
coderclaw hooks list
# Show only eligible hooks
coderclaw hooks list --eligible
# Verbose output (show missing requirements)
coderclaw hooks list --verbose
# JSON output
coderclaw hooks list --json
# Show detailed info about a hook
coderclaw hooks info session-memory
# JSON output
coderclaw hooks info session-memory --json
# Show eligibility summary
coderclaw hooks check
# JSON output
coderclaw hooks check --json
# Enable a hook
coderclaw hooks enable session-memory
# Disable a hook
coderclaw hooks disable command-logger
Saves session context to memory when you issue /new.
Events: command:new
Requirements: workspace.dir must be configured
Output: <workspace>/memory/YYYY-MM-DD-slug.md (defaults to ~/.coderclaw/workspace)
What it does:
Example output:
# Session: 2026-01-16 14:30:00 UTC
- **Session Key**: agent:main:main
- **Session ID**: abc123def456
- **Source**: telegram
Filename examples:
2026-01-16-vendor-pitch.md2026-01-16-api-design.md2026-01-16-1430.md (fallback timestamp if slug generation fails)Enable:
coderclaw hooks enable session-memory
Injects additional bootstrap files (for example monorepo-local AGENTS.md / TOOLS.md) during agent:bootstrap.
Events: agent:bootstrap
Requirements: workspace.dir must be configured
Output: No files written; bootstrap context is modified in-memory only.
Config:
{
"hooks": {
"internal": {
"enabled": true,
"entries": {
"bootstrap-extra-files": {
"enabled": true,
"paths": ["packages/*/AGENTS.md", "packages/*/TOOLS.md"]
}
}
}
}
}
Notes:
AGENTS.md and TOOLS.md only).Enable:
coderclaw hooks enable bootstrap-extra-files
Logs all command events to a centralized audit file.
Events: command
Requirements: None
Output: ~/.coderclaw/logs/commands.log
What it does:
Example log entries:
{"timestamp":"2026-01-16T14:30:00.000Z","action":"new","sessionKey":"agent:main:main","senderId":"+1234567890","source":"telegram"}
{"timestamp":"2026-01-16T15:45:22.000Z","action":"stop","sessionKey":"agent:main:main","senderId":"[email protected]","source":"whatsapp"}
View logs:
# View recent commands
tail -n 20 ~/.coderclaw/logs/commands.log
# Pretty-print with jq
cat ~/.coderclaw/logs/commands.log | jq .
# Filter by action
grep '"action":"new"' ~/.coderclaw/logs/commands.log | jq .
Enable:
coderclaw hooks enable command-logger
Runs BOOT.md when the gateway starts (after channels start).
Internal hooks must be enabled for this to run.
Events: gateway:startup
Requirements: workspace.dir must be configured
What it does:
BOOT.md from your workspaceEnable:
coderclaw hooks enable boot-md
Hooks run during command processing. Keep them lightweight:
// β Good - async work, returns immediately
const handler: HookHandler = async (event) => {
void processInBackground(event); // Fire and forget
};
// β Bad - blocks command processing
const handler: HookHandler = async (event) => {
await slowDatabaseQuery(event);
await evenSlowerAPICall(event);
};
Always wrap risky operations:
const handler: HookHandler = async (event) => {
try {
await riskyOperation(event);
} catch (err) {
console.error("[my-handler] Failed:", err instanceof Error ? err.message : String(err));
// Don't throw - let other handlers run
}
};
Return early if the event isnβt relevant:
const handler: HookHandler = async (event) => {
// Only handle 'new' commands
if (event.type !== "command" || event.action !== "new") {
return;
}
// Your logic here
};
Specify exact events in metadata when possible:
metadata: { "coderclaw": { "events": ["command:new"] } } # Specific
Rather than:
metadata: { "coderclaw": { "events": ["command"] } } # General - more overhead
The gateway logs hook loading at startup:
Registered hook: session-memory -> command:new
Registered hook: bootstrap-extra-files -> agent:bootstrap
Registered hook: command-logger -> command
Registered hook: boot-md -> gateway:startup
List all discovered hooks:
coderclaw hooks list --verbose
In your handler, log when itβs called:
const handler: HookHandler = async (event) => {
console.log("[my-handler] Triggered:", event.type, event.action);
// Your logic
};
Check why a hook isnβt eligible:
coderclaw hooks info my-hook
Look for missing requirements in the output.
Monitor gateway logs to see hook execution:
# macOS
./scripts/clawlog.sh -f
# Other platforms
tail -f ~/.coderclaw/gateway.log
Test your handlers in isolation:
import { test } from "vitest";
import { createHookEvent } from "./src/hooks/hooks.js";
import myHandler from "./hooks/my-hook/handler.js";
test("my handler works", async () => {
const event = createHookEvent("command", "new", "test-session", {
foo: "bar",
});
await myHandler(event);
// Assert side effects
});
src/hooks/types.ts: Type definitionssrc/hooks/workspace.ts: Directory scanning and loadingsrc/hooks/frontmatter.ts: HOOK.md metadata parsingsrc/hooks/config.ts: Eligibility checkingsrc/hooks/hooks-status.ts: Status reportingsrc/hooks/loader.ts: Dynamic module loadersrc/cli/hooks-cli.ts: CLI commandssrc/gateway/server-startup.ts: Loads hooks at gateway startsrc/auto-reply/reply/commands-core.ts: Triggers command eventsGateway startup
β
Scan directories (workspace β managed β bundled)
β
Parse HOOK.md files
β
Check eligibility (bins, env, config, os)
β
Load handlers from eligible hooks
β
Register handlers for events
User sends /new
β
Command validation
β
Create hook event
β
Trigger hook (all registered handlers)
β
Command processing continues
β
Session reset
Check directory structure:
ls -la ~/.coderclaw/hooks/my-hook/
# Should show: HOOK.md, handler.ts
Verify HOOK.md format:
cat ~/.coderclaw/hooks/my-hook/HOOK.md
# Should have YAML frontmatter with name and metadata
List all discovered hooks:
coderclaw hooks list
Check requirements:
coderclaw hooks info my-hook
Look for missing:
Verify hook is enabled:
coderclaw hooks list
# Should show β next to enabled hooks
Restart your gateway process so hooks reload.
Check gateway logs for errors:
./scripts/clawlog.sh | grep hook
Check for TypeScript/import errors:
# Test import directly
node -e "import('./path/to/handler.ts').then(console.log)"
Before:
{
"hooks": {
"internal": {
"enabled": true,
"handlers": [
{
"event": "command:new",
"module": "./hooks/handlers/my-handler.ts"
}
]
}
}
}
After:
Create hook directory:
mkdir -p ~/.coderclaw/hooks/my-hook
mv ./hooks/handlers/my-handler.ts ~/.coderclaw/hooks/my-hook/handler.ts
Create HOOK.md:
---
name: my-hook
description: "My custom hook"
metadata: { "coderclaw": { "emoji": "π―", "events": ["command:new"] } }
---
# My Hook
Does something useful.
Update config:
{
"hooks": {
"internal": {
"enabled": true,
"entries": {
"my-hook": { "enabled": true }
}
}
}
}
Verify and restart your gateway process:
coderclaw hooks list
# Should show: π― my-hook β
Benefits of migration: