Skip to content

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 ​

EventFires whenTypical use
BeforeToolImmediately before a tool executes.Validate or annotate a tool call.
AfterToolAfter a tool returns.Audit logging, result enrichment.
BeforeAgentBefore an agent run starts.Inject context or preflight checks.
AfterAgentAfter an agent run completes.Summaries, cleanup.
NotificationM31A emits a user notification.Forward notifications.
SessionStartA session is created or resumed.Initialize per-session state.
SessionEndA session ends.Finalize logs or state.
PreCompressJust before context compression.Archive or annotate the transcript.
BeforeModelBefore a model request.Inspect or record outgoing requests.
AfterModelAfter a model response.Inspect or record responses.
BeforeToolSelectionBefore the tool list is presented to the model.Add, remove, or modify tool definitions.

Configuration shape ​

json
{
  "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 ​

EventAdditional inputNotable output
BeforeTooltool_name, tool_input, optional mcp_contextReplacement tool_input; block or ask.
AfterTooltool_response with llmContent, returnDisplay, erroradditionalContext, tailToolCallRequest.
BeforeAgentpromptadditionalContext.
Notificationnotification_type, message, detailssuppressOutput, systemMessage.
AfterAgentprompt, prompt_response, stop_hook_activeclearContext resets the chat.
SessionStartsource of startup, resume, or clearadditionalContext.
SessionEndreason of exit, clear, logout, prompt_input_exit, otherCommon output only.
PreCompresstrigger of manual or autosuppressOutput, systemMessage.
BeforeModelllm_request with model, messages, config, toolConfigPartial llm_request merge or synthetic llm_response.
AfterModelllm_request, llm_responseReplacement llm_response.
BeforeToolSelectionllm_requesttoolConfig 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 ​

bash
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.

Built with VitePress. Powered by NVIDIA NIM.