Automatic enforcement of APEX, SOLID, safety, and notification behavior through Codex plugin hooks.
The hooks system helps agents:
- Consult relevant skills before risky changes.
- Follow APEX where it is useful.
- Validate SOLID/DRY rules.
- Surface blocking feedback through Codex hook output.
~/.codex/config.toml
|
v
plugins/*/hooks/hooks.json
|
v
@fusengine/harness hook codex [scope]
Every configured command handler uses a canonical Harness route. There is no legacy-command exception path: an unregistered plugin/event/matcher tuple is rejected by validation and skipped by generation.
To bump the installed @fusengine/harness version, run bun run update-harness
(--check for a read-only report). It reinstalls, then automatically exercises
all 11 HARNESS_SCOPES against the installed binary — with a negative-control
probe run first — before rewriting the HARNESS_VERSION tripwire in
harness-hook-policy.ts — never by hand, and never on a failed audit. See
plugins/ai-pilot/commands/update-harness.md.
The pin in package.json is always an exact X.Y.Z (never a caret). The audited
update (scripts/harness-auto-update.ts flow) runs best-effort at the end of
bun run sync:agents and the installer (./setup.sh); nothing runs in the background and
nothing extra is installed. It maintains ~/.codex and never touches your
working repo. Set FUSE_HARNESS_AUTO_UPDATE=0 (or false / off) to skip it. If
the registry is unreachable or the audit/validate/tests fail, the command logs one
warning line (naming the resolved codex home) and carries on with its normal work:
the sync or install never fails because of the update.
- If
~/.codexalready holds the latest release: log "up to date", stop (and note when the repo pin trails: "repo pin X behind ~/.codex Y — bump at next commit cycle"; bump it yourself at your next commit cycle). - Otherwise take a PID lock (
~/Library/Caches/fusengine-harness-auto-update.lock, stale if the PID is dead or the lock is older than 24 h) so two runs (sync, install, manual) never overlap. Then clean leftovers of a killed run: in the codex home only.harness-staging-<stamp>andnode_modules.old-<stamp>(if the livenode_modulesis missing, the newest parked one is restored instead), and in the system temp dir only this job's ownharness-auto-update-*worktree dirs (plusgit worktree prune). - Check out
HEADinto a throwawaygit worktreeunder the system temp dir and there runbun install,bun run update-harness <latest>(audit of all 11 scopes,validate,bun test; any failure rolls backpackage.json,bun.lockandHARNESS_VERSIONin that worktree), then the job's ownvalidate,bun testand scope audit, so every codex-home write is audited, validated and tested by this very run, even whenupdate-harnessreports "already" (HEAD already pinned, codex home lagging). The worktree is always removed. - Only if everything passed: stage the new runtime in a side directory
(
<codexHome>/.harness-staging-*) and verify it; then, right before the swap, back up<codexHome>/package.jsonand<codexHome>/bun.lockas<codexHome>/package.json.bak-<YYYY-MM-DDTHHMMSS>(andbun.lock.bak-…); swap intonode_modulesby two directory renames and verify again. Any failure restoresnode_modules,package.jsonandbun.lockand re-runsbun installin the codex home, then logs FAILED. On success only the last 3 backup pairs are kept.
Residual window: between the two renames (microseconds) node_modules/@fusengine/harness
does not exist; an already-running Codex is never waited on or stopped.
Paths: by default the codex home is ~/.codex, the log ~/Library/Logs/… and the
lock ~/Library/Caches/…. With a custom CODEX_HOME (e.g. a test run) the log
(<codexHome>/fusengine-harness-auto-update.log) and the lock
(<codexHome>/.fusengine-harness-auto-update.lock) live inside that directory, so
a test can never write the real log or contend for the real lock. Log messages
print the resolved codex-home path.
- Trigger: end of
bun run sync:agentsand of the installer (./setup.sh). The check compares~/.codexagainst the latest release, so the next run catches up on anything missed. - Log (append-only, ISO timestamps):
~/Library/Logs/fusengine-harness-auto-update.log. - Manual run:
bun run harness:auto-update(--dry-runprints the decision and writes nothing). - Skip:
FUSE_HARNESS_AUTO_UPDATE=0in the environment of the command. - Registry lookup (
bun pm view) is bounded to 15 s (FUSE_HARNESS_REGISTRY_TIMEOUT_SECto change it); on timeout the command logs one warning and carries on. - Lock held by another run: one warning ("auto-update skipped: another run holds the lock").
- The installer keeps a valid
~/.codexharness runtime that is newer than the repo pin (one info line) instead of reinstalling the pin and then climbing back.
Harness-only wiring guarantees that the handler enters Harness; it does not claim complete Codex behavior parity in the installed 0.1.79 runtime. Inspection of that published bundle shows these remaining compatibility limits:
- design-agent lifecycle handling runs only for
id === "claude-code". - core session, rules, cartography, and failure paths still contain Claude roots
such as
CLAUDE_PLUGIN_ROOT,.claude, andCLAUDE.md. - lifecycle dispatch includes Claude-only names such as
TaskCompleted,PostToolUseFailure, andSessionEnd; Codex does not emit those events.
These are Harness runtime gaps, not reasons to wire direct plugin scripts.
Plugin hooks require:
[features]
hooks = true~/.codex/.tmp/marketplaces/fusengine-codex/setup.sh~/.codex/.tmp/marketplaces/fusengine-codex/setup.ps1The installer writes Codex configuration under ${CODEX_HOME:-~/.codex} and
caches local plugins under:
~/.codex/plugins/cache/fusengine-codex/<plugin>/<version>/
Register only stable Codex plugin hook events:
| Hook | Trigger | Purpose |
|---|---|---|
SessionStart |
Session starts | Load context and cleanup state |
UserPromptSubmit |
User sends a message | Detect project type and inject guidance |
PreToolUse |
Before tool execution | Block unsafe or invalid actions |
PostToolUse |
After tool execution | Validate results and track changes |
PermissionRequest |
Permission prompt shown | Notify user |
Stop |
Turn finishes | Cleanup and completion notification |
Do not register PreCompact for plugin hooks until Codex stabilizes it.
- Create the hook directory:
mkdir -p plugins/my-plugin/hooks- Create
hooks/hooks.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "apply_patch",
"hooks": [
{
"type": "command",
"command": "bun \"${CODEX_HOME:-$HOME/.codex}/node_modules/@fusengine/harness/dist/cli/bin.mjs\" hook codex core"
}
]
}
]
}
}Use only the route registered for the exact plugin/event/matcher tuple. Unknown scopes are unsafe because the CLI can fall back silently. Do not wire a new direct script; port its behavior to Harness and add parity tests first.
Optional per-handler keys accepted by the validator: timeout, async,
statusMessage, commandWindows and additionalContextLimit (approximate
token threshold above which Codex spills the handler's additionalContext to
disk; default 2,500; 0 disables spilling; only on SessionStart,
SubagentStart, PreToolUse, PostToolUse and UserPromptSubmit). codex-rules
sets 6000 because its 9 rule files total ≈ 18.6 KB.
Hooks receive JSON input through stdin:
{
"tool_name": "apply_patch",
"tool_input": {
"file_path": "/path/to/file.ts"
}
}Allow:
exit 0Harness emits native structured Codex output, including hookSpecificOutput
for permission decisions. Do not teach legacy stderr/exit 2 protocols.
Plugin scripts/ directories may still contain source or migration artifacts,
but hook configuration must not invoke them. Remove an artifact only after no
source, bundle, generator, test, or documentation references it.
Check feature flags:
codex features list | rg '^hooks'Check installed cache:
find ~/.codex/plugins/cache/fusengine-codex -maxdepth 3 -name hooks.jsonRun validation from the source repository:
bun run validateThis includes the exhaustive Harness route gate.