All Agent Engineering

Check delivery completion with a Codex Stop hook

Define a Stop hook's trigger logic, JSON output and loop guard, then verify them by hand.

AINavWritten / Updated
On this page

At a glance

If you want Codex to recheck local delivery standards before it finishes, such as whether a notes file was updated or the check results appear in the final reply, a Stop hook can do it. It runs when a turn is about to stop, and returning block makes Codex continue with your reason as the next prompt. That also means a poorly written hook can keep a turn going repeatedly. This guide gives an acceptance policy: turn the standards into items checkable locally, then fix the trigger logic, the output format and the loop guard, and verify with a manual walkthrough. The loop guard is the simplest one: block at most once per turn. Observations here are hypothetical expectations and were not executed.

Runtime and applicability

Article type
Engineering practice
Verified
Applies to
  • Codex Check delivery completion with a Codex Stop hook

Execution locations and responsibilities

  • Codex host and calling client Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Engineering workflow order
Engineering workflow order1. Write the standard as checkable items → 2. Place the hook and complete trust review → 3. Write the trigger logic in the script → 4. Fix the output contract → 5. Add the loop guard → 6. Walk through three cases by hand and record them. Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.1. Write the standard as checkable items2. Place the hook and complete trust review3. Write the trigger logic in the script4. Fix the output contract5. Add the loop guard6. Walk through three cases by hand and record them

Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.

When to use

  • Stop does not use a matcher and any configured matcher is ignored, so it fires every time a turn stops. A non-managed hook must be reviewed and trusted first, and a definition change alters its hash so it is skipped until trusted again.

When not to use

  • This guide's Stop hook checks only what the local machine can judge; that is a scope choice. If your script queries state beyond the local machine, how to handle timeouts or failures (allow or block) is not designed here and you must define and walk through it yourself.
  • A Stop hook can only give a continuation prompt as a turn stops; it cannot undo anything already done.
  • Cloud orchestration supports only Global MCP hooks, not command handlers or hooks from local configuration, so this command-type Stop hook applies to local Codex only.
  • Several matching command hooks for one event start concurrently, and any continue set to false outranks continuation decisions from other hooks, so do not assume your hook owns Stop; prompt and agent handlers are parsed but skipped, so they cannot serve as acceptance checks.

Prerequisites and input

  • A local delivery standard written as items, each decidable from local files or command output. Limiting scope to local items is this guide's choice, not a capability limit of the Stop hook.
  • A decision on whether the hook lives in user or project config: the project .codex/ layer loads only when the project is trusted, and a non-managed hook must be reviewed and trusted in /hooks before it runs.
  • Acceptance that hook commands run with the session cwd, and that Codex may start from a subdirectory, so the script location should be resolved from the git root.

Engineering checks

Trigger and trust — Stop does not use a matcher and any configured matcher is ignored, so it fires every time a turn stops. A non-managed hook must be reviewed and trusted first, and a definition change alters its hash so it is skipped until trusted again.

Output contract — On exit code 0, Stop expects JSON on stdout and plain text is invalid. A decision of block with a reason, or exit code 2 with the reason on stderr, makes Codex continue and the reason becomes the new continuation prompt.

Loop guard — The input field stop_hook_active only says whether this turn was already continued by Stop; it does not record which gaps were reported. The docs give no limit on the number of continuations, so this guide sets a rule: always allow when the value is true.

1. Write the standard as checkable items

Where: your acceptance list. Input: the local delivery standards to meet before finishing. Action: write each standard as one judgment with its evidence source, such as a file existing, a command output matching expectations, or the final reply mentioning the check result. Done when: each item yields yes or no on the local machine. Stop when: an item can only be judged outside the local machine; move it out and record it separately instead of letting the Stop hook guess.

2. Place the hook and complete trust review

Where: <repo>/.codex/hooks.json or the same layer's config.toml, using one representation per layer. Input: one command handler under the Stop event. Action: resolve the command path from the git root with git rev-parse --show-toplevel, set timeout in seconds to fit the script (the default is 600 for most hooks), and in the CLI open /hooks to review and trust it. Done when: /hooks lists the hook as trusted. Stop when: it still shows as needing review; do not rely on it as an acceptance control, and do not reach for --dangerously-bypass-hook-trust unless you have vetted the source outside Codex.

3. Write the trigger logic in the script

Where: the top of the hook script. Input: one JSON object on stdin with fields such as session_id, cwd, turn_id, stop_hook_active and last_assistant_message (which can be null). Action: since the matcher has no effect, decide yourself whether this run needs checking; for example allow immediately when cwd is not the target repository, and check only file-based items when last_assistant_message is null. Do not depend on the transcript_path format, which the docs say is not a stable interface. Done when: different inputs take the intended branches. Stop when: required fields are unreadable; allow the stop and mention it in systemMessage rather than blocking.

4. Fix the output contract

Where: the output section of the script. Input: the item check results and stop_hook_active. Action: on exit code 0 print one JSON object on every path, of three kinds: when everything is met, continue set to true; when stop_hook_active is false and items are unmet, decision set to block with a short reason listing only the unmet items; when stop_hook_active is true and items are still unmet, continue set to true with the gaps listed in systemMessage (see the next step). The docs also say exit code 0 with no output counts as success, which sits slightly apart from "Stop expects JSON"; settle in the manual walkthrough which your version accepts. Keep the reason short, because continuation prompts keep the default length limit and oversized output is spilled to disk, so never include secrets. Done when: no path prints plain text and only the second kind contains block. Stop when: you need to end outright; return continue set to false, which takes precedence over block decisions from other matching Stop hooks.

5. Add the loop guard

Where: the script's branching logic. Input: stop_hook_active. Action: when it is true the turn was already continued; allow regardless of whether the gaps match the previous ones, and use systemMessage to ask for human attention. This avoids storing reported items in the script, at the cost that a gap appearing after the continuation only produces a warning and no second block. Do not make the check asynchronous: a background hook cannot block or control the turn, so continuation needs a synchronous hook. Done when: in any one turn the script blocks at most once. Stop when: you see a turn blocked repeatedly; switch to allow plus a warning immediately and disable the hook as in the next step to investigate.

6. Walk through three cases by hand and record them

Where: a real session in a practice repository. Input: a task that deliberately misses one standard, a task that can be fixed, and a task that can never be fixed. Action: observe whether the hook fires, whether it blocks, whether it allows after the continuation, and note the value of stop_hook_active. Done when: each case has one row covering trigger, output, whether it continued and the end result. Stop when: any case shows repeated continuation; disable the hook (set hooks to false under [features], or disable it in /hooks), fix the script, then re-enable.

Task example

This is a fictional walkthrough; the expectations are not observations. The standard has two items: CHANGELOG.md in the repository contains an entry for this change, and the final reply states which check command was run. The hook sits in the practice repository's .codex/hooks.json and is trusted. Case one: the task changes code but not CHANGELOG.md and the reply mentions no check. Expect the hook to return block with a reason such as "CHANGELOG.md has no entry; final reply does not say which check ran", and Codex continues with that reason. After the fix, stop_hook_active is true, both items are met, and the hook outputs continue set to true. Case two: the task edits documentation only, both items already hold, and the hook allows at once. Case three: CHANGELOG.md is read-only and cannot be fixed. After the continuation stop_hook_active is true and the item is still unmet, so expect the hook to allow the stop and warn in systemMessage. Each case gets one record row. None of this was actually run.

Completion checks

  • Check the hook definition trust state in /hooks. Manually reviewing a changed script again is this guide’s recommendation. The documented new review on plugin-script updates does not establish an automatic rule for every local script.
  • Every output path is JSON: it contains continue set to true when all items are met or stop_hook_active is true, and contains decision set to block with a short reason only when stop_hook_active is false and items are unmet.
  • In a turn where stop_hook_active is true the script no longer blocks, and unmet gaps are surfaced through systemMessage.
  • All three walkthrough cases are recorded, with no run of repeated continuations.

Verification scope

Written from the official hooks documentation only. Codex was not installed, no account was signed in, no hook was executed and no real output was seen; the example and observations are hypothetical expectations. The docs do not resolve the gap between exit code 0 with no output and "Stop expects JSON", nor say whether continuations have a built-in cap; confirm both on your version. MCP tool hooks and other events such as SessionEnd are outside this guide.

Sources

All links are official pages; verification dates are listed per source, and links point to the version checked.

  1. Codex · Codex documentation: codex-hooks

Found an error? Tell us →