Repository navigation
feat(agents): port the PreToolUse guard to Codex, where ask means allow - #830
Conversation
…llow `.claude/settings.json` wires `aws-write-guard.sh` as a PreToolUse hook and carries a `permissions.deny` list. Codex reads neither, and no project file can give it either: a project-level `.codex/hooks.json` or `.agents/hooks.json` never fires (only `$CODEX_HOME`'s does — those path strings live in Codex's Claude-Code *migration* reader, which is what makes them look repo-scoped), and there is no per-command deny list at all. So on the Codex path, the only things between an agent and this AWS account were the read-only `er-readonly` profile and the IAM denies. `scripts/codex-hooks-sync.sh` installs the guard into `$CODEX_HOME/hooks.json`, and `scripts/hooks/codex-pretooluse-guard.sh` applies both halves: the deny patterns are read OUT OF `.claude/settings.json` rather than restated, so nobody can hand the two agents different lists, and the graded aws/terraform/kubectl/helm table is delegated to the existing guard. THE RULE THAT INVERTS. Measured against Codex 0.167.0 by driving a hook that emits each decision in turn: silent exit 0 -> Completed, runs (the correct pass) permissionDecision: deny -> Blocked, refused exit 2 + reason on stderr -> Blocked, refused permissionDecision: ask -> Failed, RUNS permissionDecision: allow -> Failed, runs non-JSON stdout, exit 0 -> Completed, runs So `ask` is not a weaker deny there, it is an allow — and `ask` is the guard's widest case, every mutating call that is not unambiguously destructive. Ported unchanged it would have been the biggest hole in the guard on the one agent with no prompt to fall back on. `ER_GUARD_ASK_AS_DENY=1` collapses it to `deny` for that caller only; Claude Code keeps the prompt, and the test pins both mappings, because a change that unified them either way is a regression in one of the two agents. Two more facts worth the measurement: - A `deny` blocks even under `--dangerously-bypass-approvals-and-sandbox`, the flag set these sessions actually use. Sandbox bypass is not hook bypass — which is what makes this worth building rather than documenting. - A hook Codex has not trusted is skipped SILENTLY, and `--dangerously-bypass-hook-trust` does not persist trust (four bypassed runs wrote no `hooks.state`). So the sync prints the one-time interactive approval step after installing: an installed-but-untrusted hook is worse than none, because it reads as cover. Proved end to end, not just in unit tests: with the guard installed, a Codex session asked to run `kubectl delete pod …` reported `Command blocked by PreToolUse hook: Destructive infrastructure command …`, and the formerly-`ask` case (`kubectl patch …`) — which ran before this change — is now refused with the reason explaining it is a refusal rather than an escalation. A garbage-emitting guard fails open silently, so the assertion that the deny list actually parsed is in the gate, not at runtime: `codex-hooks-sync-test.sh` fails if the derivation yields zero patterns, and the installer refuses to install against an empty list. 21 cases, mutation-tested — breaking the deny-list match, removing the ask mapping, and applying the ask mapping unconditionally each turn the suite red. Closes #826 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
CI red here is the account, not this diff — do not "fix" itEvery job on this pull request failed in ~2 seconds having executed zero Evidence, in the order it rules things out:
Both Codex pull requests (#830 and #831) failed identically, across five The onset is sharp: healthy at What to do: check Actions billing / spending limits on the account, then |
Closes #826
What
Installs this repo's
PreToolUseguard into Codex, with the one rule that hasto invert there:
scripts/codex-hooks-sync.shwrites a hook into$CODEX_HOME/hooks.json,scripts/hooks/codex-pretooluse-guard.shapplies thepermissions.denypatterns read out of.claude/settings.jsonanddelegates the graded aws/terraform/kubectl/helm table to the existing
aws-write-guard.shwithER_GUARD_ASK_AS_DENY=1.Why
Codex reads neither
.claude/settings.jsonnor any project-level hooks file, andhas no per-command deny list at all — so on the Codex path nothing intercepted an
aws,terraform,kubectlorhelmwrite. #826 asked for option 1 (write thedecision down) or option 2 (port the guard, if its blocking behaviour could
be observed first). It could, so this is option 2.
Proof
make checkgreen. The decision matrix was measured, not assumed — a hookemitting each decision in turn, against Codex 0.167.0:
exit 0CompletedpermissionDecision: denyBlockedexit 2+ stderrBlockedpermissionDecision: askFailedpermissionDecision: allowFailedexit 0CompletedEnd to end, with the guard installed and under
--dangerously-bypass-approvals-and-sandbox— the flag set these sessionsactually use:
and the formerly-
askcase, which ran before this change:codex-hooks-sync-test.sh: 21 cases, mutation-tested — breaking the deny-listmatch, removing the
askmapping, and applying theaskmappingunconditionally each turn it red.
Risk & rollback
The behaviour change is confined to Codex: a mutating
aws/kubectl/helmcommand that Claude Code would prompt about is now refused there. That is
deliberate (the alternative is allowing it) and the refusal message says so.
Claude Code is unchanged — the test pins that
askstaysaskwithout the envvar.
Nothing is installed by merging;
scripts/codex-hooks-sync.shis run permachine. Rollback:
git revert <merge-sha>, andscripts/codex-hooks-sync.sh --removeon any machine already set up.Post-merge ledger
Closed by: agent · session
c19a9a1f-4446-4d2f-bd7d-5ac2d0f08ecaonMac· 2026-11-08T02:44:00Zmainis green with this change on it — merge SHA43da3b62, all fourpush runs succeeded with real work done:
rust23 jobs / 251 steps,frontend9/8,infra8/5,devcontainer2/5, zero failures. The stepcounts are quoted rather than the conclusions because a starved run also
reports a conclusion — this repo spent ~90 minutes red on exactly that
today (see the blocked note below). The four steps this change adds all
ran green on
main:agent config bridge symlinks,agent-bridge regression tests,codex-mcp-sync derivation tests,codex PreToolUse guard tests.images—n/a — correctly never ran.images-detect.shkeys on the^rust/and^frontend/trees plusimages.yml; this commit's own diff(
43da3b62^..43da3b62) is 10 files under.claude/,.github/,scripts/,CLAUDE.mdandMakefile— none of them. Worth recordingbecause the obvious check misleads: diffing from this branch's base
instead showed
rust/changed, which is feat(match): cross-field comparison groups — compare mobile on A against home_phone on B #796 from another session landingin between. Attributing that to this commit would have meant waiting on a
deploy that was never going to exist.
deploy—n/a — nothing to deploy.It fires onworkflow_runafterimages, which did not run. No container input changed.driftscheduled 2026-11-07T06:01 success,
kindscheduled 2026-11-07T06:44success. The one red thing there is the
deploypush trigger on70f31c54from 2026-11-07T21:37, which predates this merge by five hoursand is the known defect deploy.yml's push trigger was a misdiagnosis — remove it and gate what it was protecting #661/deploy.yml: a mixed infra/k8s + image-input commit always produces one spurious red deploy #817.
Donethrough the automation: No PreToolUse guard under Codex: aws-write-guard and permissions.deny do not port #826 readsCLOSED / COMPLETED, its card readsDone, and theClosedEvent'scloser.__typenameisPullRequest— the merge closed it, not a handedit to
Status(which would have attributed the close toProjectV2).resolved by choosing option 2 and proving it, rather than deferring any
part. The sibling finding went out as Codex truncates skill descriptions at ~670 chars — every routing tail is invisible there #825, which merged as
5e62a87d.trust mechanics and the
ask-fails-open consequence are in theagent-skills standard's Codex bridge section;
CLAUDE.mdcarries theshort version Codex itself reads;
aws-account-operations'smcp-and-guardrails.mdgains "Layer 3 under Codex", since that is wheresomeone reasoning about account protection will look. Memory:
codex-agent-config-bridgenow states thataskis an allow there.Specific to this change
main, not from thebranch worktree:
scripts/codex-hooks-sync.shinstalled 10 derived denypatterns with the hook command pointing at the primary checkout
(
ER_REPO_ROOT=/Users/athvin/github.com/athvin/entity-resolution-engine),and a Codex session under
--dangerously-bypass-approvals-and-sandboxthen reported:Command blocked by PreToolUse hook: Destructive infrastructure command. … Command: kubectl delete pod probe-nonexistent --context does-not-exist.This mattered: an install done from the worktree would have embedded a
path that is about to be deleted.
deferred — #835.Every proof above passed--dangerously-bypass-hook-trust, and that flag persists no trust(verified: four bypassed runs wrote no
hooks.state), so on this machinethe guard is live only when that flag is passed. It cannot be
scripted: an agent cannot drive the interactive approval, and deriving the
trusted_hashso the installer could self-trust failed across twelvecandidate recipes against the one known-good entry. Grant Codex hook trust per machine — an untrusted PreToolUse guard is skipped silently #835 owns the
per-machine approval and the confirmation step — asking a session to run a
denied command without the bypass flag, which is the only check that
proves trust took.
Context — plan, discoveries, measurements, files
Plan
Probe whether a Codex hook can fire, and whether it can blockDecide between the ticket's three options on that evidence→ option 2Mapasktodenyfor Codex only, and pin both mappings in a testDerive thepermissions.denyhalf from.claude/settings.jsonProve it end to end in a real session, then wire the gate and the docsDiscoveries
.codex/hooks.jsonand.agents/hooks.jsonwere both placed with a marker-touchingSessionStarthook; neither fired. Only
$CODEX_HOME/hooks.jsondid. Both path strings arein the binary — inside its Claude-Code migration reader, which is what made
them look repo-scoped when grepped.
demonstrably works did not fire in a fresh
CODEX_HOME. The discriminator wasa
SessionStarthook of the identical shape to one that fires in the realhome — which has a
trusted_hashentry.--dangerously-bypass-hook-trustmakes hooks run but persists nothing: four bypassed runs left
config.tomluntouched. Attempting to derive the hash so an installer couldself-trust failed across twelve candidate recipes, so the honest instruction
is the interactive approval, and the script prints it.
tool_name: "Bash",tool_input.commanda plain string — which is the whole reason one guard canserve both agents rather than two copies drifting apart.
askfails open. This was the finding that changed the design. Theguard's middle case is most of its surface, and ported unchanged it would have
been the widest hole in it on the one agent with no prompt to fall back on.
Completedand the command proceeds. So "did the deny list actually parse" isasserted in the gate, where failing is free, and the installer refuses to
install against an empty list.
rather than documenting.
literal destructive command on this session's own command line and
aws-write-guard.shdenied it.Measurements
.codex/hooks.jsonfires?.agents/hooks.jsonfires?$CODEX_HOME/hooks.jsonfires?tool_name,tool_input.command(string)denyblocks?PreToolUse Blocked--dangerously-bypass-approvals-and-sandboxaskblocks?exit 2blocks?grep hooks.state config.tomlafter 4 runsjqover.claude/settings.jsonFiles
scripts/hooks/codex-pretooluse-guard.sh— the Codex-side guardscripts/hooks/aws-write-guard.sh—ER_GUARD_ASK_AS_DENYhonouredscripts/codex-hooks-sync.sh— install /--dry-run/--removescripts/ci/codex-hooks-sync-test.sh— 21 casesMakefile,.github/workflows/rust.yml— wired intoagent-bridge-checkCLAUDE.md, the agent-skills standard,aws-account-operations'smcp-and-guardrails.md— the record, including the matrix above