Hooks run user-defined commands at specific points in the agent lifecycle. They receive JSON on stdin and return JSON on stdout. The implementation lives in packages/core/src/hooks/ (hookSystem.ts, hookRegistry.ts, hookRunner.ts, hookAggregator.ts, hookPlanner.ts, hookEventHandler.ts, trustedHooks.ts).
Events
| Event | When it runs |
|---|---|
BeforeTool | Before a tool executes; can validate or modify the call. |
AfterTool | After a tool executes; can process or log results. |
BeforeAgent | Before an agent run starts. |
AfterAgent | After an agent run finishes. |
Notification | When M31A emits a notification. |
SessionStart | At session startup. |
SessionEnd | At session shutdown. |
PreCompress | Before context compression. |
BeforeModel | Before a model request. |
AfterModel | After a model response. |
BeforeToolSelection | Before the model chooses tools. |
Configuration
Hooks are configured in settings under hooksConfig (global controls) and hooks (per-event arrays):
{
"hooksConfig": {
"enabled": true,
"disabled": [],
"notifications": true
},
"hooks": {
"BeforeTool": [
{
"matcher": "run_shell_command",
"hooks": [
{
"name": "log-shell",
"type": "command",
"command": "tee -a ./m31a-hooks.log >/dev/null",
"timeout": 5000
}
]
}
]
}
}matcher supports exact names, regular expressions such as /^read_/, and wildcards such as *. hooksConfig.enabled defaults to true; hooksConfig.disabled names hooks that should never run.
Management
m31a hooks migrateThe migrate command converts Claude Code hook definitions into M31A format. In the TUI, /hooks panel (aliases list, show) displays configured hooks, and enable, disable, enable-all, and disable-all change their state.
Safety
- Hook commands run with the privileges of the M31A process. Only enable hooks from sources you trust.
trustedHooks.tsdistinguishes trusted definitions from untrusted ones.- Use timeouts to avoid blocking the agent indefinitely.
- Hooks are a good place for audit logging and policy-aware validation, but the policy engine remains the primary authorization mechanism.
Next steps
See Approval modes for authorization and Skills for instruction-based extension.