agentleFS
liqiongyuliqiongyu/my-agents/skills/hook-development/SKILL.md

hook-development

Skill29 starsChanged 9 months agoMITView on GitHub

REQUIRED for Claude Code or Codex CLI hook queries — has accurate cross-platform reference. Triggers on PreToolUse, PostToolUse, Stop, SessionStart hooks, .claude/settings.json or .codex/hooks.json config, prompt vs command hook types, hooks not firing, exit codes, stdin JSON, matchers, path blocking, or dual-platform hook setups. Skip for React hooks, webhooks, GitHub Actions.

  • Reads credentials
  • Deletes or force-pushes

8 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

14 rules. 0 are common, 12 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 8 edited versions we read keep these.

  • 12

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Before writing hooks, identify which platform(s) the project uses:

    line 18
  2. Distinctive:

    Use for: input validation, path safety checks, file size limits, external tool integration, linting, anything with clear yes/no logic.

    line 43
  3. Distinctive:

    Use an LLM to make context-aware decisions with natural language reasoning:

    line 47
  4. Distinctive:

    Not every event exists on both platforms.

    line 63
  5. Distinctive:

    All hooks receive a JSON object on stdin.

    line 170
  6. Distinctive:

    When a project uses both platforms and you want prompt hook intelligence on Claude Code with a command hook fallback on Codex:

    line 296
  7. Very specific:

    Use /hooks in-session to review loaded hooks.

    line 335
  8. Distinctive:

    Test any hook script by piping sample JSON:

    line 348
  9. Distinctive:

    When maintaining hooks for both platforms in one project:

    line 367
  10. Distinctive:

    Write hook scripts in a shared location

    line 381
  11. Distinctive:

    Add configuration for your platform(s)

    line 382
  12. Distinctive:

    Test scripts directly with piped JSON

    line 383
  13. Very specific:

    Test in runtime (claude --debug or Codex startup)

    line 384
  14. Distinctive:

    Document hooks in project README or AGENTS.md

    line 385

“Common” is measured: kept by at least 6 in 10 of the 8 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Cross-Platform Hook Development

Overview

Hooks are event-driven automation that execute when an AI coding agent takes specific actions — before a tool runs, after a file edit, when the agent tries to stop, or when a session starts. They enforce policies, validate operations, inject context, and integrate external tools.

Both Claude Code and Codex CLI support hooks with the same core model: a hook script receives JSON on stdin, does its work, and communicates back via exit code and stdout/stderr. The differences are in configuration format, supported events, and some advanced capabilities.

This skill teaches you to design hooks that work on both platforms, and to handle the gaps where they diverge.

Platform Detection

Before writing hooks, identify which platform(s) the project uses:

SignalPlatform
.claude/settings.json exists with "hooks" keyClaude Code
.codex/hooks.json existsCodex
.codex/config.toml has [[hooks]] blocksCodex
Both .claude/ and .codex/ directories existDual-platform project

For dual-platform projects, write hook scripts once and reference them from both config files. The configs differ but the scripts can be shared.

Hook Types

Command Hooks (Both Platforms)

Execute a shell command. The workhorse hook type — deterministic, fast, works everywhere:

{
  "type": "command",
  "command": "bash path/to/validate.sh",
  "timeout": 60
}

Use for: input validation, path safety checks, file size limits, external tool integration, linting, anything with clear yes/no logic.

Prompt-Based Hooks (Claude Code Only)

Use an LLM to make context-aware decisions with natural language reasoning:

{
  "type": "prompt",
  "prompt": "Evaluate if this bash command is safe: $TOOL_INPUT. Check for destructive operations, privilege escalation, and network access. Return 'approve' or 'deny'.",
  "timeout": 30
}

Prompt hooks catch edge cases that regex can't — they understand intent, not just patterns. But they cost tokens and add latency.

Codex does not support prompt hooks. It parses the "type": "prompt" field but silently skips execution. For dual-platform projects, always provide a command hook equivalent. See "Prompt Hook Degradation" below.

Hook Events

Not every event exists on both platforms. Design around the intersection, then add platform-specific hooks where valuable.

EventClaude CodeCodexPrimary Use
PreToolUseYesYesValidate/block/modify tool calls before execution
PostToolUseYesYesReact to results, provide feedback, log
StopYesYesVerify task completeness before agent stops
UserPromptSubmitYesYesAdd context, validate, or block user prompts
SessionStartYesYesLoad project context, set environment
SubagentStopYesNoValidate subagent task completion
SessionEndYesNoCleanup, logging, state preservation
PreCompactYesNoPreserve critical info before context compaction
NotificationYesNoReact to user notifications
PermissionRequestNoYesIntercept permission approval requests

Safe cross-platform set: PreToolUse, PostToolUse, Stop, UserPromptSubmit, SessionStart — these five work on both.

Configuration Formats

Claude Code — .claude/settings.json

Hooks live inside a top-level "hooks" key:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "bash tools/validate-bash.sh"
          }
        ]
      }
    ]
  }
}

Codex — .codex/hooks.json

Same structure, but the file is hooks-only (no other settings mixed in). Codex also supports a statusMessage field:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "bash tools/validate-bash.sh",
            "statusMessage": "Checking bash command safety"
          }
        ]
      }
    ]
  }
}

Codex Alternative — .codex/config.toml

Codex also accepts hooks inline in TOML format. Less common but useful for simple setups.

Dual-Platform Template

For projects using both, point both configs at the same scripts:

project/
  .claude/settings.json    # Claude Code hooks config
  .codex/hooks.json        # Codex hooks config
  tools/hooks/             # Shared hook scripts (both configs reference these)
    validate-bash.sh
    validate-write.sh
    load-context.sh

Matchers

Matchers filter which tools trigger a hook. Both platforms use regex against tool names.

"matcher": "Bash"              // Exact tool name
"matcher": "Write|Edit"        // Multiple tools (OR)
"matcher": "mcp__.*__delete.*" // Regex for MCP delete operations
"matcher": "*"                 // Wildcard — all tools

Matchers are case-sensitive. Common tool names: Bash, Read, Write, Edit, Agent.

For MCP tools, the naming pattern is mcp__<server>__<tool>:

"matcher": "mcp__.*"                    // All MCP tools
"matcher": "mcp__plugin_asana_.*"       // Specific MCP server

Codex note: For SessionStart, matcher filters by start source (startup, resume, clear). For UserPromptSubmit and Stop, matchers are ignored.

Hook Input/Output

Input (stdin JSON)

All hooks receive a JSON object on stdin. Common fields:

{
  "session_id": "abc123",
  "cwd": "/path/to/project",
  "hook_event_name": "PreToolUse"
}

Event-specific fields:

EventAdditional Fields
PreToolUsetool_name, tool_input
PostToolUsetool_name, tool_input, tool_result / tool_output
UserPromptSubmituser_prompt
Stopreason
SessionStartstart_source (Codex only)

Platform input differences:

  • Claude Code delivers input via $CLAUDE_TOOL_INPUT env var and stdin
  • Codex delivers input via stdin only (use input=$(cat) in scripts)
  • For cross-platform scripts, always read from stdin — it works on both

Output

Exit codes (both platforms):

  • 0 — Success, stdout shown in transcript
  • 2 — Blocking error, stderr fed back to agent

JSON output (optional, for richer control):

{
  "continue": true,
  "systemMessage": "Message injected into agent context",
  "suppressOutput": false
}

PreToolUse special output (Claude Code):

{
  "hookSpecificOutput": {
    "permissionDecision": "allow|deny|ask",
    "updatedInput": {"field": "modified_value"}
  }
}

Stop/SubagentStop decision output (Claude Code):

{
  "decision": "approve|block",
  "reason": "Explanation"
}

Codex output field support varies by event — see references/codex.md for the full matrix.

Environment Variables

VariableClaude CodeCodexPurpose
$CLAUDE_PROJECT_DIRYesNoProject root
$CLAUDE_PLUGIN_ROOTYesYes (compat alias)Plugin directory
$CLAUDE_ENV_FILEYesNoSessionStart: persist env vars here
$PLUGIN_ROOTNoYesPlugin directory (native)
$PLUGIN_DATANoYesWritable plugin data directory

Cross-platform script pattern:

PROJECT_DIR="${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null || pwd)}"

Common Patterns (Quick Reference)

These patterns work on both platforms using command hooks:

PatternEventWhat It Does
Path SafetyPreToolUse (Write/Edit)Block writes to system paths, .env, credentials
Dangerous CommandPreToolUse (Bash)Block rm -rf, sudo, dd, mkfs
Test EnforcementStopBlock stop if code changed but no tests ran
Context LoadingSessionStartDetect project type, set env vars
Code QualityPostToolUse (Write/Edit)Run linter after file edits
Completion CheckStopVerify build succeeded and questions answered

For complete implementations of each pattern with dual-platform configs, see references/patterns.md.

Security Best Practices

These apply to command hooks on both platforms:

Always validate input:

#!/bin/bash
set -euo pipefail
input=$(cat)
tool_name=$(echo "$input" | jq -r '.tool_name')

if [[ ! "$tool_name" =~ ^[a-zA-Z0-9_]+$ ]]; then
  echo '{"decision": "deny", "reason": "Invalid tool name"}' >&2
  exit 2
fi

Check for path traversal:

file_path=$(echo "$input" | jq -r '.tool_input.file_path')
if [[ "$file_path" == *".."* ]]; then
  echo '{"decision": "deny", "reason": "Path traversal detected"}' >&2
  exit 2
fi

Quote all variables — unquoted variables are injection vectors:

echo "$file_path"       # GOOD
echo $file_path         # BAD — word splitting + globbing

Set timeouts — prevent hooks from blocking the agent indefinitely. Default: 60s for command hooks, 30s for prompt hooks.

Prompt Hook Degradation Strategy

When a project uses both platforms and you want prompt hook intelligence on Claude Code with a command hook fallback on Codex:

Claude Code — use the prompt hook for nuanced reasoning:

{
  "matcher": "Bash",
  "hooks": [
    {
      "type": "prompt",
      "prompt": "Analyze command for destructive ops, privilege escalation, network access without consent. Return 'approve' or 'deny'."
    }
  ]
}

Codex — use a command hook with equivalent deterministic logic:

{
  "matcher": "Bash",
  "hooks": [
    {
      "type": "command",
      "command": "bash tools/hooks/validate-bash.sh",
      "statusMessage": "Checking command safety"
    }
  ]
}

The command hook won't catch as many edge cases as the prompt hook, but it covers the common dangerous patterns. This is an acceptable tradeoff — prompt hooks are a bonus, not a requirement.

Debugging

Claude Code

claude --debug    # Verbose hook execution logs

Use /hooks in-session to review loaded hooks. Changes to hooks require session restart.

Codex

Check .codex/hooks.json syntax:

jq . .codex/hooks.json    # Validates JSON

Codex logs hook execution to its standard output. Invalid JSON causes load failure at startup.

Testing Hook Scripts Directly

Test any hook script by piping sample JSON:

echo '{"tool_name": "Bash", "tool_input": {"command": "rm -rf /"}}' | \
  bash tools/hooks/validate-bash.sh
echo "Exit code: $?"

Common Issues

SymptomCauseFix
Hook never firesWrong matcher (case-sensitive)Check exact tool name
Hook fires but no effectScript exits 0 with no outputUse exit 2 + stderr for blocking
"Invalid JSON" at startupSyntax error in configRun jq . on the config file
Changes not taking effectHooks load at session startRestart the session
Codex prompt hook silently skippedCodex doesn't execute prompt hooksUse command hook instead

Dual-Platform Workflow

When maintaining hooks for both platforms in one project:

  1. Write scripts first — put reusable logic in tools/hooks/ or similar
  2. Scripts read stdin — use input=$(cat) so they work on both platforms
  3. Derive project root portably — git rev-parse --show-toplevel works everywhere
  4. Generate both configs — or maintain them manually with the same script references
  5. Test on both — run script directly with piped JSON, then test in each runtime

Implementation Workflow

To add hooks to a project:

  1. Identify which events you need (start with the cross-platform five)
  2. Choose hook type: command for deterministic checks, prompt for reasoning (Claude Code only)
  3. Write hook scripts in a shared location
  4. Add configuration for your platform(s)
  5. Test scripts directly with piped JSON
  6. Test in runtime (claude --debug or Codex startup)
  7. Document hooks in project README or AGENTS.md

Additional Resources

For detailed platform-specific reference and extended patterns, consult:

  • references/claude-code.md — Complete Claude Code hook reference: all 9 events, prompt hook API, plugin hooks.json format, env vars
  • references/codex.md — Complete Codex hook reference: all 6 events, config.toml format, output field support matrix, limitations
  • references/patterns.md — 10+ proven cross-platform patterns with dual-config examples
  • references/advanced.md — Advanced: multi-stage validation, cross-event state, caching, external integrations, rate limiting, testing

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.