Hooks are command definitions invoked at lifecycle events. The event enum is defined in packages/core/src/hooks/types.ts; runtime orchestration lives in hookSystem.ts, hookRegistry.ts, hookRunner.ts, and hookAggregator.ts.
Common fields
Every hook input includes session_id, transcript_path, cwd, hook_event_name, and an ISO timestamp. Common output fields are continue, stopReason, suppressOutput, systemMessage, decision (ask, block, deny, approve, allow), reason, and hookSpecificOutput.
Aggregation gives block and deny priority over ask; otherwise the decision is allow. continue: false stops execution.
Event reference
| Event | Fires when | Typical use |
|---|---|---|
BeforeTool | Immediately before a tool executes. | Validate or annotate a tool call. |
AfterTool | After a tool returns. | Audit logging, result enrichment. |
BeforeAgent | Before an agent run starts. | Inject context or preflight checks. |
AfterAgent | After an agent run completes. | Summaries, cleanup. |
Notification | M31A emits a user notification. | Forward notifications. |
SessionStart | A session is created or resumed. | Initialize per-session state. |
SessionEnd | A session ends. | Finalize logs or state. |
PreCompress | Just before context compression. | Archive or annotate the transcript. |
BeforeModel | Before a model request. | Inspect or record outgoing requests. |
AfterModel | After a model response. | Inspect or record responses. |
BeforeToolSelection | Before the tool list is presented to the model. | Add, remove, or modify tool definitions. |
Configuration shape
{
"hooksConfig": {
"enabled": true,
"disabled": [],
"notifications": true
},
"hooks": {
"BeforeTool": [
{
"matcher": "run_shell_command",
"hooks": [
{
"name": "shell-audit",
"type": "command",
"command": "tee -a .m31a/shell-audit.log >/dev/null",
"timeout": 5000
}
]
}
]
}
}matcher accepts an exact tool name, a regular expression such as /^read_/, or *. Each hook has a unique name, type: "command", a shell command, an optional description, and an optional timeout in milliseconds. Hook commands receive JSON on stdin and are expected to emit JSON on stdout.
hooksConfig.enabled defaults to true; hooksConfig.disabled lists hook names that should not run. Non-event fields in hooksConfig are enabled, disabled, and notifications.
### Event-specific payloads
| Event | Additional input | Notable output |
|---|---|---|
BeforeTool | tool_name, tool_input, optional mcp_context | Replacement tool_input; block or ask. |
AfterTool | tool_response with llmContent, returnDisplay, error | additionalContext, tailToolCallRequest. |
BeforeAgent | prompt | additionalContext. |
Notification | notification_type, message, details | suppressOutput, systemMessage. |
AfterAgent | prompt, prompt_response, stop_hook_active | clearContext resets the chat. |
SessionStart | source of startup, resume, or clear | additionalContext. |
SessionEnd | reason of exit, clear, logout, prompt_input_exit, other | Common output only. |
PreCompress | trigger of manual or auto | suppressOutput, systemMessage. |
BeforeModel | llm_request with model, messages, config, toolConfig | Partial llm_request merge or synthetic llm_response. |
AfterModel | llm_request, llm_response | Replacement llm_response. |
BeforeToolSelection | llm_request | toolConfig with mode and allowedFunctionNames. |
Modifying events
These are the only events whose output can change what happens next: BeforeToolHookOutput and BeforeToolSelectionHookOutput carry tool-specific modifications. Other events are observational.
Management
m31a hooks migrate/hooks panel (aliases list, show) displays hooks in the TUI; enable, disable, enable-all, and disable-all change their state.
Safety
Hook commands run with the privileges of the M31A process. Review them like any other executable code, set timeouts, and keep secrets out of their output.
Next steps
See Hooks for a worked example and Hooks tutorial to build one.