Skip to content

grok-hooks-valid

.grok/hooks/*.json must use Grok's hook events, handler types and fields

Severity error (auto)
Autofix -
Since v0.20.0
Repo Types grok-project
Category Grok Build

Why

.grok/hooks/*.json allows you to automate shell commands and HTTP requests during Grok Build agent lifecycle events — such as right before a tool runs, when a session begins, or when the agent completes its work. Because hooks are committed to the repository, they provide a reliable, shared mechanism for running checks and automations across your team.

Grok Build reads all *.json files directly inside .grok/hooks/ and merges their hook definitions. A syntax error skips the whole file; most other mistakes skip one event, matcher group or handler. Grok lists those as hook load errors in grok inspect and the /hooks tab, but a headless session or CI run shows nothing. This rule verifies your .grok/hooks/*.json files against Grok's supported events, handler types, and options so that your automations run reliably.

Command execution patterns are also scanned for security by hooks-dangerous and can be inventoried against an explicit allowlist with hooks-prohibited.

Severity

Severity reflects how much of the hook configuration is affected by an issue:

Errors — issues that prevent Grok Build from loading any hook in the file:

  • Invalid JSON syntax, or non-finite numbers (NaN, Infinity, -Infinity).
  • A root that is not an object, a missing top-level hooks object, or a hooks value that is not an object.

Warnings — the file loads, but an event, matcher group or handler does not:

  • Unrecognized hook event names. Grok skips their values before decoding groups or handlers, so their descendants do not produce shape errors. Declare a newer event in extra-events to enable its strict shape checks.
  • A recognized event whose value is not an array.
  • A matcher group that is not an object, has no hooks array, or has a matcher that is not a string.
  • A regex matcher that does not compile under Rust's regex engine. Rust's regex syntax supports Unicode property classes (\p{...}) and set operations (&&, --, ~~), but does not support lookarounds, backreferences, or conditional groups. Matchers longer than 1,000 characters are skipped to keep checks responsive.
  • A handler that is not an object, has no type, or uses an unsupported type (supported types are command and http).
  • A command handler missing a command string, or an http handler missing a url string.
  • Handler fields with the wrong data type: type, command, url and onFailure must be strings; args an array of strings; timeout a non-negative number; failClosed (or fail_closed) a boolean, not null; and env an object with string values. Setting both failClosed and fail_closed also drops the handler.
  • A Claude Code if condition. Grok does not support it, so it skips the handler rather than run it on every call. An if of null, false, "", [] or {} asks for nothing and is not reported.
  • An empty hooks object or empty event array (valid JSON, but configures no actions).

Info — helpful configuration tips:

  • Environment variables in env that are automatically provided by Grok's hook runner (such as GROK_HOOK_EVENT, GROK_SESSION_ID, or GROK_WORKSPACE_ROOT), as the runner's values take precedence.
  • Defining a matcher on events like Stop or UserPromptSubmit where matchers are not evaluated by Grok.

Event names

Grok accepts several spellings of each event and normalizes them, making it easy to share hooks across tools like Claude Code or Cursor. Accepted variations include:

  • Standard Grok event names: SessionStart, SessionEnd, UserPromptSubmit, PreToolUse, PostToolUse, PostToolUseFailure, PermissionDenied, Stop, StopFailure, StopCancelled, Notification, SubagentStart, SubagentStop, PreCompact, PostCompact.
  • Documented aliases: SubagentEnd (alias for SubagentStop).
  • The snake_case representation passed to GROK_HOOK_EVENT.
  • Cased variants, with the note that userPromptSubmit should be written as UserPromptSubmit, user_prompt_submit, or Cursor's beforeSubmitPrompt.
  • Cursor lifecycle events: beforeShellExecution, beforeMCPExecution, and beforeReadFile map to PreToolUse; afterShellExecution, afterMCPExecution, afterFileEdit, afterAgentResponse, and afterAgentThought map to PostToolUse.

Examples

Bad — a string timeout drops the SessionStart handler, and the if condition makes Grok skip the PreToolUse one:

{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          { "type": "command", "command": "./scripts/version.sh", "timeout": "10" }
        ]
      }
    ],
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": "./scripts/check-push.sh", "if": "Bash(git push *)" }
        ]
      }
    ]
  }
}

Good — well-formed matcher groups and appropriate handler options:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash|run_terminal_command",
        "hooks": [
          { "type": "command", "command": "./scripts/audit-command.sh", "timeout": 5 }
        ]
      }
    ],
    "Stop": [
      {
        "hooks": [
          { "type": "command", "command": "make lint", "timeout": 600 },
          { "type": "http", "url": "https://hooks.example.com/turn-ended", "timeout": 10 }
        ]
      }
    ]
  }
}

How to fix

  • Specify timeout as a non-negative number of seconds (30 or 1.5, not "30"). 0 keeps the default. For longer tasks like test suites in Stop hooks, generous timeouts (such as 600 seconds) work well.
  • Remove an if condition and move the check into the hook command, or narrow the group's matcher.
  • Write args as an array of strings, one per argument, and failClosed as true or false.
  • Provide each handler with "type": "command" and a command string, or "type": "http" and a url string.
  • Keep env values as strings. Variables provided by Grok's runner do not need to be repeated in env.
  • Use standard Grok event names or supported aliases.

If Grok introduces newer event names, you can allow them in .skillsaw.yaml:

rules:
  grok-hooks-valid:
    extra-events:
      - PreSomethingNew

Matcher check limits

Matcher validation is conservative: it translates Rust inline flags and braced hexadecimal escapes only for syntax checking. For example, Bash|(?i)Write, (?-u:\w+), (?U).*, \x{42}ash, \u{42}ash and \U{42}ash are accepted. Unclosed groups/classes and unsupported look-around/backreferences are still reported in the checked subset. Extended-mode (x) patterns are left unresolved because comments change tokenization. No finding is a complete Rust regex validation guarantee.

Configuration

rules:
  grok-hooks-valid:
    enabled: auto  # true | false | auto
    severity: error
Parameter Description Default
extra-events Additional hook event names to accept, for events newer than this skillsaw release []

Run skillsaw explain grok-hooks-valid to see this documentation and the rule's effective configuration in your terminal.