Agent skill

hook-development

Use when the user wants to create Codex workflow hooks (pre/post run gates, tool-use validators, stop checks) or needs guidance on hook scripts and hooks.json configuration.

Stars 5
Forks 0

Install this agent skill to your Project

npx add-skill https://github.com/troykelly/codex-skills/tree/main/external/skills/hook-development

SKILL.md

Hook Development (Codex)

Overview

Codex CLI does not provide Claude-style hook events. To replicate hook behavior, implement a hook runner (wrapper script or CI job) that:

  1. Runs hook scripts before/after codex exec.
  2. Optionally parses codex exec --json output to detect tool usage.
  3. Applies a hooks.json configuration to decide which hooks to run.

This skill defines a Codex hook contract and provides utilities for validating, testing, and linting hooks.

This repository ships a reference runner at scripts/codex-hook-runner (installed by install.sh) that implements the contract described below. Hooks are enabled by default in codex-autonomous; set CODEX_DISABLE_HOOKS=1 to disable.

Hook Types

Prompt Hooks (LLM-driven)

Use an LLM (Codex or another model) to make contextual decisions.

json
{
  "type": "prompt",
  "prompt": "Validate this operation. Return approve|deny with a reason."
}

Command Hooks (Deterministic)

Use shell scripts for fast, repeatable checks.

json
{
  "type": "command",
  "command": "bash ${CODEX_HOOK_ROOT}/scripts/validate-write.sh",
  "timeout": 30
}

Hook Configuration (hooks.json)

Define hooks in a single hooks.json. This format is consumed by your hook runner.

json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write|Edit",
        "hooks": [
          {
            "type": "command",
            "command": "bash ${CODEX_HOOK_ROOT}/scripts/validate-write.sh"
          }
        ]
      }
    ],
    "Stop": [
      {
        "matcher": "*",
        "hooks": [
          {
            "type": "prompt",
            "prompt": "Review transcript at $TRANSCRIPT_PATH. Block if tests were not run after code changes."
          }
        ]
      }
    ]
  }
}

If your runner prefers a direct format, you can omit the hooks wrapper and use events at top level. scripts/validate-hook-schema.sh supports both.

Hook Events (Codex Mapping)

Your hook runner chooses which events to emit. Recommended events:

  • SessionStart: before running Codex
  • UserPromptSubmit: after prompt is assembled but before execution
  • PreToolUse: before a tool call (requires --json parsing)
  • PostToolUse: after a tool call (requires --json parsing)
  • Stop: before accepting the final response
  • SessionEnd: after the run completes
  • SubagentStop: for worker processes spawned by worker-dispatch

Hook Script Contract

Hook scripts receive JSON on stdin and return decisions via exit code:

  • Exit 0: allow / continue
  • Exit 2: block (stderr message is surfaced)

Recommended output shape for decision hooks:

json
{
  "decision": "allow|deny|ask",
  "reason": "short explanation",
  "systemMessage": "optional context for the main agent"
}

Input Fields (Suggested)

{
  "event": "PreToolUse",
  "hook_event_name": "PreToolUse",
  "tool_name": "Write",
  "tool_input": {"file_path": "..."},
  "tool_result": {"stdout": "..."},
  "session_id": "...",
  "transcript_path": "...",
  "cwd": "...",
  "approval_policy": "on-request",
  "sandbox_mode": "workspace-write"
}

Your runner may add additional fields as needed.

Environment Variables

The hook runner should set:

  • CODEX_PROJECT_ROOT: repository root
  • CODEX_HOOK_ROOT: directory containing hooks.json and scripts
  • CODEX_ENV_FILE: path to a file for exporting environment variables (optional)

Utilities

Use these helper scripts while developing hooks:

  • scripts/validate-hook-schema.sh: validate hooks.json structure
  • scripts/hook-linter.sh: lint hook scripts
  • scripts/test-hook.sh: run hooks with sample inputs

References

  • references/patterns.md for common hook patterns
  • references/advanced.md for advanced techniques
  • references/migration.md for migrating from script-only gates to prompt-based hooks
  • examples/ for sample hooks

Expand your agent's capabilities with these related and highly-rated skills.

troykelly/codex-skills

sentry-setup-ai-monitoring

Setup Sentry AI Agent Monitoring in any project. Use this when asked to add AI monitoring, track LLM calls, monitor AI agents, or instrument OpenAI/Anthropic/Vercel AI/LangChain/Google GenAI. Automatically detects installed AI SDKs and configures the appropriate Sentry integration.

5 0
Explore
troykelly/codex-skills

agent-development

Use when the user wants to design Codex agent equivalents (specialized workers/profiles/prompt files), define triggering conditions, or build reusable agent prompts and validation tools.

5 0
Explore
troykelly/codex-skills

skill-development

Use when the user wants to create or refine Codex skills, improve skill descriptions, organize skill resources, or follow Codex skill best practices.

5 0
Explore
troykelly/codex-skills

sentry-setup-logging

Setup Sentry Logging in any project. Use this when asked to add Sentry logs, enable structured logging, setup console log capture, or integrate logging with Sentry. Supports JavaScript, TypeScript, Python, Ruby, React, Next.js, and other frameworks.

5 0
Explore
troykelly/codex-skills

frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.

5 0
Explore
troykelly/codex-skills

sentry-code-review

Analyze and resolve Sentry comments on GitHub Pull Requests. Use this when asked to review or fix issues identified by Sentry in PR comments. Can review specific PRs by number or automatically find recent PRs with Sentry feedback.

5 0
Explore

Didn't find tool you were looking for?

Be as detailed as possible for better results