Careful — confirm before the un-undoable
An agent reminding itself to be careful is a probabilistic guard; it fails exactly on the runs nobody is watching. The contract here: a deterministic pre-execution hook that inspects every shell command and every file edit and stops the destructive ones. The model can forget; the hook cannot.
Where it runs
| Host / platform | Status |
|---|---|
| Claude Code on Linux, macOS, WSL (Linux filesystem) | Supported. For v1.4.0 the test table ran in CI on Linux (bash 5.2) and on macOS (the system bash 3.2), and the two live probes in headless Claude Code sessions on Linux; nothing has run on WSL |
| Claude Code on native Windows, hooks under Git Bash | Best-effort. PowerShell payloads and backslash paths are handled and pinned in the table, but no run on a real Windows host has been made |
| Native Windows without Git Bash | No guard. Claude Code then runs hooks in PowerShell, where the registered bash … command cannot run; a hook that fails that way is a non-blocking error, so every call proceeds |
| Other hosts (Codex, Gemini, Copilot, Cursor, …) | Port by hand: decision envelope, tool names, path fields and $CLAUDE_PROJECT_DIR are Claude Code's. See HARNESS §7 |
One installed copy. adopt.py installs the hook into .claude/hooks/ — check-careful.py
(the matcher), check-careful.sh (the shim), careful.json (your additions),
check-careful.test.sh and careful-corpus.txt — and leaves no copy under
.claude/skills/careful/. That is the only copy that runs. The kit copy under
factory/harness/skills/careful/hooks/ is what adopt.py --upgrade installs from; only kit
maintainers edit it. (v1.3 installed the matcher twice: the documented procedure edited and
tested the unregistered copy, the test went green, and the live guard was unchanged — F10.)
adopt.py --check fails while a .claude/skills/careful/hooks/ directory exists, and
--upgrade removes one that matches the live copy, this kit's, or — when factory/ is a git
checkout, so its history is there to compare with — any earlier committed kit version; a copy
that matches none holds local edits — port them to careful.json, then
delete the directory. --check also fails while the installed matcher or shim differs from
the kit's copy: since 1.4.0 careful.json is the only place a project's rules go, so a changed
check-careful.py is a guard that may be off, not a customisation.
Install, adapt, register — in that order
python3 factory/bin/adopt.py --profile <archetype>installs.claude/hooks/.Adapt
.claude/hooks/careful.jsonnow, while the agent can still edit it. It is additive only — every list is added to the defaults, nothing in it removes one:Key Effect Example protected_branchesforce-push or delete → deny; fnmatch patterns ["develop", "release/*"]db_clientscommands that execute SQL; DROP/TRUNCATE/unbounded DELETE through them → ask ["mydb-cli"]safe_dirsrelative dirs (fnmatch) whose recursive delete is routine and regenerated → silent; ~/entries are home-anchored["obj", "Library"]extra_askPython regexes matched against each shell segment as written → ask ["\\bmake\\s+deploy\\b"]extra_denythe same → deny ["\\bterraform\\s+destroy\\b.*\\bprod\\b"]What to add per archetype: ARCHETYPES. Anything malformed — invalid JSON, an unknown key (
extra_denny), a regex that does not compile — is not ignored: every shell command that would have passed getsask, naming the error; deny stays deny. A typo that quietly switched a rule off is the failure this file exists to end.Check it:
python3 .claude/hooks/check-careful.py --check-config, thenbash .claude/hooks/check-careful.test.sh.First commit; create a remote and push (until one exists,
.gitis the only copy of your history — see the deny tier). Thenpython3 factory/bin/adopt.py --register-guard, which merges both matchers from the kit'ssettings.json.templateinto.claude/settings.json(adopt.py installs no copy of the template; the kit's is the one it reads, and the one the guard protects).Run the live probes below.
After registration careful.json is a guarded file: the agent is denied, and changing it is a
human edit (or a session whose host environment carries CAREFUL_ALLOW_HIGH=1). Upgrading the
guard is python3 factory/bin/adopt.py --upgrade — meant to be run and reviewed by a human; the
guard asks when the agent runs it.
🔴 Read first: the guard that was there but was not on
This skill shipped for 2.5 months with a hook that did nothing, and with a green test the
whole time. The hook printed its decision as a top-level {"permissionDecision":"ask", …}.
Claude Code reads a PreToolUse decision only from inside hookSpecificOutput, so the unknown
field was dropped and the command ran. The check — "7/7 dangerous commands asked" — read the
script's stdout and never asked whether the host honoured it. Upstream gstack shipped the
identical bug (CHANGELOG 1.64.0.0: "deny meant allow").
- A guard is not verified until you verify the CONSUMER. Producing the right bytes proves nothing. Trigger a real destructive command in a live session and watch the host stop it.
- How strong
askis depends on the SURFACE. With the host in a skip-permissions mode, a hookaskwas auto-approved on the desktop/terminal surface — measured — and raised a real Allow once / Deny dialog on mobile/remote control.denyis a wall everywhere;askis a wall wherever a human is actually looking. Do not rely onaskfor unattended runs, and do not scatter it either: where someone IS looking it is a real dialog, and a guard that interrupts routine work gets switched off.
Correct shape (Claude Code; find your own host's contract before porting):
{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"[careful] …"}}Never emit permissionDecision: "allow" — on Claude Code that auto-approves the tool,
turning the guard into a blanket bypass for everything it fails to recognise. Passing through
means printing {}.
Three tiers
| Tier | Shapes |
|---|---|
deny — nothing undoes it |
recursive delete (rm -r, find -delete without a filter, Remove-Item -Recurse, rd /s, rsync --delete) of /, $HOME, any directory above $HOME, /home / /Users / C:\Users\<name>, or everything in one of them (~/*, "$DIR/"* with DIR unset); force-push (flag or +refspec, with or without --force-with-lease beside it — plain force defeats the lease) or delete (:main, --delete, gh api -X DELETE …/refs/heads/main) of a protected branch, --mirror, --force --all, and a force-push whose branch cannot be named (detached or unborn HEAD, $VAR, $(…)); raw block-device writes (dd of=/dev/sda, > /dev/disk4, mkfs/wipefs -a/blkdiscard/sgdisk --zap-all on a device, diskutil eraseDisk); eFuse burns (espefuse burn_*, idf.py efuse-burn); recursive delete of .git, .specify or .specify/memory in a repository with no remote, and of the project directory itself; every write, move, link, delete, chmod, git update-index --chmod/--cacheinfo or git checkout/restore/rm of a guarded file (next section) |
ask — destructive but recoverable, or unreadable |
rm -r outside the safe list · SQL DROP/TRUNCATE/DELETE/UPDATE without WHERE through a DB client, including warehouses (bq, snowsql, duckdb, spark-sql, clickhouse-client, …) · ORM resets (prisma migrate reset, rails db:drop, manage.py flush, artisan migrate:fresh, redis-cli FLUSHALL, dropdb, …) · migrate down · cloud and IaC teardown (terraform/tofu/pulumi/cdk destroy, apply -destroy, state rm, helm uninstall, kubectl delete, aws s3 rm --recursive, aws … delete-*, gcloud … delete, az … delete) · flash erase (esptool erase_flash, st-flash erase, nrfjprog --eraseall) · publish and unpublish (npm publish/unpublish, cargo publish/yank, twine upload, gh release delete, …) · git commit/push --no-verify, commit -n, git config core.hooksPath, git -c/--config-env core.hooksPath=…, an include.path set the same ways, git config --remove-section/--rename-section core and git config --edit, update-index --assume-unchanged/--skip-worktree on a gate file, and any edit, move or delete of git's hook wiring (.git/config, .git/hooks/*) · edits, moves and deletes of the CI job adopt.py --ci github installs (.github/workflows/factory-gates.yml), and of a directory holding it (git rm -r .github, mv .github/workflows …, git restore --source=… .github) · adopt.py --upgrade · reset --hard · checkout/restore . · clean -f · branch -D · --force-with-lease alone, and force-push to other branches · sed -i / perl -i · compose down -v · edits to gate configuration (gates/*.conf, gates/orphan-allowlist.txt) and creating a new gates/check-* script · writes to a dot-entry in $HOME or a system path (/etc, /usr, …) · anything piped into a shell that is not a readable literal · a command name built at runtime · a command over 256 K characters |
| pass | everything else — printed as {}, so the host's own permission flow decides |
An abbreviated long option is that option: GNU getopt_long, git and Python's argparse all
accept a unique prefix, so rm --recurs --forc ~, git push --delet origin main,
git commit --no-verif and adopt.py --upg ran as the full option while v1.4.0's first matcher
returned {} for every one (review 2026-09-29). The options the rules key on are now read the
way the tool reads them; an ambiguous prefix, which the tool itself refuses to run, is read the
dangerous way. adopt.py itself no longer accepts abbreviations.
Adapt the lists to your stack through careful.json; the tier split and the classes are the
portable part. Why cloud teardown is ask, not deny: whether terraform destroy erases
production or a throwaway stack depends on credentials the matcher cannot see; an IaC adopter
who knows which one it is promotes it with extra_deny. Why raw devices are deny: one wrong
letter in of=/dev/sdX erases the host disk, the same class as rm -rf /; embedded work
flashes boards with write_flash, which stays silent.
The guarded files: nothing may switch the guard off
| Path (normalised, case-folded) | Why |
|---|---|
.claude/hooks/** — matcher, shim, careful.json, tests |
this is the guard |
.claude/settings.json, .claude/settings.local.json (any .claude/, incl. ~/.claude/) |
where hooks are registered, or disabled wholesale with disableAllHooks |
managed settings: /etc/claude-code/**, /Library/Application Support/ClaudeCode/**, C:\Program Files\ClaudeCode\** |
the same switch, one level up; writable by an agent running as root, as in most containers |
gates/run-chain.sh, gates/check-*.sh, gates/check-*.py, gates/hooks/** — in the project's gates/, in any gates/ holding run-chain.sh (the kit copy under factory/), and wherever the directory cannot be looked at (a relative gates/…, a path built at runtime) |
the gate chain the guard protects. A feature-flag src/gates/hooks/ is not the chain: until review 2 of v1.4.0 any directory named gates was, and editing or moving one was denied with no way to exempt it |
harness/skills/careful/hooks/**, including under factory/ |
the copy an upgrade installs |
harness/settings.json.template, including under factory/ |
the wiring adopt.py --register-guard copies into .claude/settings.json; that command stays allowed because it can only restore the kit's wiring, so its source must not be editable |
harness/hooks/hooks.json; ~/.claude/plugins/{cache,marketplaces}/ai-factory-kit/**; ~/.claude/plugins/{installed_plugins,known_marketplaces}.json |
the plugin install: its wiring and the records the host reads. claude plugin disable|uninstall|remove and marketplace remove naming this plugin, or --all, are denied for the same reason |
.specify/scripts/** |
scripts the Spec Kit flow shells out to |
Deny covers the file-edit tools (Write, Edit, MultiEdit, NotebookEdit) and, on the
shell side, every redirect (>, >>, >|, &>, 2>), rm, mv, cp, ln, install,
truncate, dd of=, tee, sponge, sed -i, perl -i, editors, chmod/chown, rsync,
git rm, git checkout … --, git restore, git mv, git clean <path>, git update-index --chmod=-x/--cacheinfo (they change what is committed there without touching the file;
--chmod=+x passes, since it only restores the bit), find -delete or find -exec whose
traversal reaches the guard, xargs fed a listing of it, cp -r/rsync/tar -C/unzip -d
into a directory holding it, PowerShell Remove-Item/Move-Item/
Set-Content/Out-File/Copy-Item, and deleting or moving any directory that holds these
files (.claude, gates). A path is normalised before it is matched — //, /./, ..,
backslashes, case, NTFS trailing dots and ::$DATA, globs expanded against the disk, symlinks
resolved — because v1.3 matched the literal spelling and .claude//hooks/x, .CLAUDE/Hooks/x,
>| and C:\proj\.claude\hooks\x each passed with {} (F8, F44), as did rm, mv, cp,
ln, truncate, git rm, git checkout HEAD~1 -- … on the guard (F9).
Some edits stay possible, and shown (ask), because the adopter legitimately makes them — but a
quiet one turns a red chain green or stops it running: gate configuration (gates/chain.conf,
gates/*.conf, gates/orphan-allowlist.txt); the CI job .github/workflows/factory-gates.yml,
which the adopter extends with toolchain steps (before the 2026-09-29 review an agent could
git rm it with {} while an edit to chain.conf asked; until the second review, git rm -r .github still could, and the commit landed — a directory holding the job now asks too, and
adopt.py --check fails while it is gone); and git's hook wiring, .git/config
and .git/hooks/* (git config --unset core.hooksPath asked; an Edit deleting the same line
passed). A gates/check-* script that does not exist yet asks, so feature 001 can add a
project gate after registration; once it exists it is guarded. Reading any of these is
always allowed — the first draft denied the read, which blocked the ordinary "look at what this
does" step and bought nothing.
The registered command fails closed: if check-careful.sh is missing, broken, or prints
anything but a JSON decision, it exits 2, which Claude Code treats as a block, and names
.claude/hooks/ on stderr. A plain bash <missing file> exits 127, a non-blocking error —
deleting the shim used to switch the guard off for every later call (F9) — and
bash <empty file> exits 0 printing nothing, which is no decision at all: an emptied shim
passed everything while adopt.py --check said OK (review 2026-09-29). The shim prints a JSON
object on every path it can take, so requiring one costs nothing.
🔴 The second lesson: a fresh adversary, not a fresh test
The v1.3.0 fix was tested 63/63 in both directions and verified live. An adversarial audit then
found six confirmed escapes from the deny tier — sudo rm -rf / and FOO=1 rm -rf /
returned {} (argv[0] read off token 0), rm -rf / ; echo done downgraded to ask, git push origin :main needed no --force, git -C /repo push -f hid the subcommand, rm -rf / /home
softened by adding a target, rm -rf ~/* was missing from the root set.
It happened again in v1.4.0, which is why this is a lesson and not a story — and a third time
in the review of v1.4.0 itself, through abbreviated long options (above, under the tiers). The v1.3.2 matcher
split commands on a regex and tokenised with shlex. Measured 2026-09-29, each of these
returned {} for rm -rf /: if true; then rm -rf /; fi, (rm -rf /), f(){ rm -rf /; },
echo $(rm -rf /), bash <<EOF with rm -rf / in the body, echo "rm -rf /" | sh — and a
129-character echo x | base64 --- --- … took 69 s against the 10 s hook timeout (a
timed-out hook does not block). The matcher is now a quote-, substitution- and heredoc-aware
lexer, one linear pass per nesting level. Then a generator wrapped ten deny-tier cores in random
shell constructs: of 4,000 variants, three escaped — all case … in a) inside $( ), whose
) closed the substitution early. After the fix, 16,000 more escaped none, and a seeded
generator now runs inside the test table.
The transferable rules:
- Your own test table only pins the spellings you thought of. A table proves the cases in it; it never proves coverage. Generate combinations as well as listing escapes.
- Test expectations can encode the vulnerability. When an audit contradicts a test, decide which one is right before touching either.
- Buy an adversary, not another pass of your own eyes — and tell it that
askis auto-approved, so an escape intoaskcounts as a hole. - Pin every escape family, one line each, so the next rewrite cannot quietly reopen them.
- Slowness is an escape. A hook the host kills at its timeout renders no decision. The
matcher answers
askat 7 s on its own, inspects 256 K characters in full (beyond that: the head for deny, thenask), and the table asserts every adversarial size stays under 5 s. (The 7 s self-timeout uses SIGALRM: Linux and macOS have it, Windows Python does not.)
🔴 The third lesson: measure the FALSE POSITIVES, or the guard gets switched off
Replaying 6,638 real shell commands from 48 hours of work through the v1.2 guard: 800
asks — one interruption every eight commands. Three rules produced 85% of it: -i listed as
a "writes in place" flag caught grep -i (594), any redirect onto an absolute or home path
flagged cat > ~/notes.md (89), SQL keywords matched the word "Update" in a grep pattern (9).
After fixing those: 800 → 77.
- A guard's false-positive rate is a safety property, not an ergonomics one. One dialog per eight commands trains people to dismiss without reading, then to switch the guard off.
- Measure it against real traffic, not against your test table. The table said green; the corpus said one in eight.
- Precision beats scope. Every fix above made the matcher narrower.
v1.4.0 numbers, from real runs. careful-corpus.txt holds 395 ordinary commands across stacks
(git, JS, Python, Go, Rust, JVM, .NET, Ruby/PHP/Elixir, mobile, containers, IaC, embedded,
data, PowerShell): 0 interrupted, and the table fails if one ever is. (Review found two
ordinary cleanups missing from it — find . -name '*.pyc' -delete and
find . -type d -name __pycache__ -exec rm -rf {} + — which asked while rm -rf __pycache__
was silent, and no careful.json key could change that. A find cleanup is now silent when every
root is inside the project, every -name is a safe_dirs entry or a bytecode pattern, and
nothing in the expression can widen it: no -o, no !, no -path, no other -exec. A second
review found the guard treating every directory named gates as the gate chain: an Edit of
src/gates/hooks/useGate.ts or git mv src/gates … in feature-flag code was denied, and
careful.json, additive only, could not exempt it. The chain is now the project's gates/ or
one holding run-chain.sh, and eleven such commands joined the corpus.) That corpus is
constructed, so the real-traffic replay was repeated on the 2,002 unique commands from the
agent sessions that built and audited this kit: v1.3.2 interrupted 165 (8.2%), v1.4.0
interrupts 89 (4.4%). Each of the 89 is a rule doing what it says: 40 denies on writes, moves
and deletes of guard files and gate scripts, and on protected pushes (those sessions probed
exactly that); 22 sed -i; 19 inline Python that opens a guarded path and writes (kit
maintenance scripts, mostly — the kit repository does not register the guard); 8 other asks
(core.hooksPath, branch -D, force-push, checkout ., adopt.py --upgrade, a command name
built at runtime). The 8 commands v1.3.2 denied and v1.4.0 allows each passed a destructive
string as quoted data to a probe; none executed it. Replay your own history the same way:
it is the only number about your cost.
Design rules (each one paid for)
Warn by default, deny for the shapes nothing undoes. "Warn, never hard-block" holds for everything recoverable; it stopped holding for
rm -rf ~and force-push-to-main onceaskwas measured auto-approved on unattended runs.Give
denyan escape hatch, and never commit it.CAREFUL_ALLOW_HIGH=1in the host's environment downgrades deny to ask and still prints the reason. Hooks inherit Claude Code's environment, so a command the agent runs cannot set it for later hook runs, and the settings files whoseenvkey could set it are guarded.Fail polarity is asymmetric, on purpose.
Situation Decision payload unreadable, matcher crashed, Python missing, time budget spent, command over 256 K characters askshim missing or broken (registered command) exit 2: block careful.jsoninvalidaskon everything that would pass; deny stays denyno command field, not a write tool, empty stdin pass {}Decisiveness is judged per word, and only real opacity forfeits it. A literal
/next to a$(…)is still a literal/; an unset$DIRin front of/is treated as empty, because that is what bash does (rm -rf "$STEAMROOT/"*). A force-push whose branch cannot be read is denied, not asked: an unresolvable destination is a reason to stop.Substitutions run.
echo $(rm -rf /)executesrm;$(…), backticks,<(…), heredocs fed to a shell,sh -c,eval,ssh host '…',git -c alias.x='!…'and literals piped into a shell are judged as the commands they are.An allowlist that is not anchored is an allowlist for everything. Safe dirs are relative names only;
..climbs out of nothing; an absolute twin never rides the list (binandtmponce did, as*/bin, and swallowedrm -rf /usr/bin).bin,lib,outstay off it because they hold committed files in many repos — this kit'sbin/included.A skip list is a safety claim. A segment whose argv[0] only reads text (
grep,echo,cat, …) skips the SQL and extra rules, sogrep -rn "migrate down" docs/does not cry wolf. Redirects are judged on every segment regardless.Test in both directions, then test the consumer (GATES §6).
What it is blind to
Per GATES §4, a gate says what it cannot see, in its own text:
- Program semantics. An interpreter handed a script —
python3 x.py,node -e, a Makefile target, an npm script,docker run -v /:/host …— writes and deletes whatever it likes. Inline code is flagged only when it opens a guarded path by name and writes, or hands a destructive literal tosystem(); both are string heuristics, and building the path from parts defeats them. - Whole-tree swaps.
git checkout <branch>,git switch,git reset --hard <old>,git stash pop,patch < file, ortar -x/unzipinto the project root can restore a tree without the guard; the matcher judges the paths a command names. - Anything outside the hooked tools: a human's terminal, CI, git hooks, other command-running
tools the matcher is not wired to. (Any tool routed to this hook whose input has a string
commandis inspected as a POSIX command.) - Native Windows without Git Bash (see Where it runs), and every host that is not Claude Code until it is ported.
- Formatters and fixers run over the whole tree.
ruff format .,ruff check --fix .ornpx prettier --write .at the project root rewrite.claude/hooks/check-careful.pyand the gate scripts, and pass silently: a formatter is not a write shape this matcher recognises (measured on this release). Keep the kit's directories out of the formatter's scope (ARCHETYPES, "Stack stanzas");adopt.py --checkreports a kit file that changed anyway. - Git plumbing that writes the index or history directly.
git apply --cached,git read-tree,git commit-tree, orgit revertof the commit that installed the CI job change what is committed without naming a guarded path as a path.git update-index --chmod=-x/--cacheinfoon a guarded path is denied and--index-infoasks; the rest pass. CI runs the committed chain, andadopt.py --checkfails while the CI job is gone. - A session started below the project root. Claude Code reads the shared
.claude/settings.jsonfrom the session's primary working directory (its settings docs, read 2026-09-29; not observed live), so a session opened in a monorepo package runs without the root's hook registration. Start sessions at the repository root.
Verification — two steps, and step 2 is the one that was skipped
bash .claude/hooks/check-careful.test.sh # step 1: passed 747, failed 0(Before --register-guard it reads 711 and prints SKIP for the wiring checks, which test the
registered command in .claude/settings.json: intact, broken, empty, printing no decision, and
missing. It also loads every careful.json stanza in factory/model/ARCHETYPES.md and checks
the CLI stanza's tag rule; with no factory/ there it prints SKIP for that too.)
Step 1 builds its own fixtures — throwaway repos on main, a feature branch, detached and
unborn HEAD, with and without a remote; a fake $HOME and project; a clean copy of the hook —
so it passes on any branch and in any checkout state. (v1.3.2 read 141/142 on every feature
branch, in detached CI checkouts and in a fresh repo, because one row asked the caller's own
checkout which branch it was on — F26.) It pins decision and envelope for every row, the
generated escapes (1,680 variants, 480 of them with abbreviated long options), the size
limits, the fail-closed wiring, the no-Python fallback, your
careful.json, the false-positive corpus, and the ARCHETYPES stanzas.
Step 2, in a live session, after the first commit. Both probes work in a repository with no remote, and neither has any blast radius:
git push --force nonexistent-remote-probe mainMust be blocked, citing [careful] force-push to the protected branch 'main'. If it
instead runs and fails with "'nonexistent-remote-probe' does not appear to be a git
repository", your deny never reached the host — the guard is decoration.
git push --dry-run . HEAD:refs/heads/careful-probeMust run and print * [new branch] HEAD -> careful-probe: a dry run into the repository
itself creates nothing and needs no remote. It sits next to the deny logic (same subcommand)
without --force. A guard that blocks everything is as broken as one that blocks nothing.
Optional third probe, for the file-edit matcher: ask the agent to Write
.claude/hooks/careful-probe.txt. It must be denied. If the file appears, the
Write|Edit|MultiEdit|NotebookEdit matcher is not registered — fix that; a human deletes the
file, because the shell side will (correctly) refuse the agent.
Adding a pattern means: careful.json for your project, or — for kit maintainers — the matcher
in factory/harness/skills/careful/hooks/, with rows in both directions in the table and a
clean corpus run, then both steps again.
Position in the defense stack
The spec gate keeps wrong work from starting (spec-first) → the
gate chain keeps broken work from landing → adversarial review
catches what gates miss → careful catches the one command that would make a mistake
permanent. Wire it through your platform's pre-execution hook mechanism
(HARNESS §4).