Skip to content

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 ​

EventWhen it runs
BeforeToolBefore a tool executes; can validate or modify the call.
AfterToolAfter a tool executes; can process or log results.
BeforeAgentBefore an agent run starts.
AfterAgentAfter an agent run finishes.
NotificationWhen M31A emits a notification.
SessionStartAt session startup.
SessionEndAt session shutdown.
PreCompressBefore context compression.
BeforeModelBefore a model request.
AfterModelAfter a model response.
BeforeToolSelectionBefore the model chooses tools.

Configuration ​

Hooks are configured in settings under hooksConfig (global controls) and hooks (per-event arrays):

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

bash
m31a hooks migrate

The 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.ts distinguishes 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.

Built with VitePress. Powered by NVIDIA NIM.