Challenge
- Writing "never delete" and "back up before overwriting" into the instructions is not enough: over a long task the agent can forget
- Once data is deleted or a file is overwritten, it is usually too late by the time anyone notices
Approach
- A pre-Bash hook breaks even compound commands apart, finds dangerous operations, and sends them back with a reason unless the conditions are met
- A pre-edit hook copies the original file, with a version number, into an archive folder inside the project before letting the edit through
Outcome
- The baseline rules no longer depend on memory and apply the same way in every project
- Any file Claude has rewritten can be rolled back to its previous version
The author's global rules include two baselines: delete only when a verified backup exists, and keep the previous version before overwriting. At first they lived in the instruction file, but instructions can be forgotten.
So both were moved into Claude Code hooks, scripts the harness always calls before running a tool. The finer scope, exceptions and history live in a separate skill, and the instruction file keeps only a one-line summary.
The full workflow
| Task | Who | Inputs | Outputs |
|---|---|---|---|
| 1. Set the rule | Authorhuman | — | Global rules |
| 2. Build + test | Main session | Global rules | Hooks + tests |
| 3. Deploy | Deploy scriptsync_toolchain.py | Hooks + tests | Live hooks |
| 4. Propose call | Main session | Refers to Global rules | Tool call |
| 5. Check danger | Delete guarddestructive_guard | Tool call, Refers to Live hooks | Verdict |
| 6. Archive old | Archive hookarchive_before_write | Tool call, Refers to Live hooks | Old version |
| 7. Run or reroute | Main session | Verdict, Old version | Result |
7 steps · 1 role · 3 tools · 1 loop · 34 tests · 57 blocks and 12,215 saved versions, 2026-11-03 – 11-18
Turn the baselines you need kept into hooks rather than instructions: they apply the same way every time, remembered or not.
- Steps 1–3 happen once: the author sets the rule, the main session builds it with tests, and a script deploys it
- Steps 4–7 happen every time: in any project, two hooks step in just before a tool runs
- The delete guard can block; the pre-overwrite archive never does
- The reason for a block goes back to the model, so Claude switches to moving files to the trash or naming a verified backup
Project folders involved (4)
This workflow does not live in one folder: it runs across projects.
- claude-infra/src/hooks
- The core: source of both hooks, their tests and the deploy script
- ~/.claude/hooks
- Deployed: runs in sessions of every project folder
- ~/.claude/skills/global-rules
- The details, exceptions and history of the baselines
- <project>/_archive
- Per-project archive of old versions, kept out of Git
Step 1 · Author
Set the ruleglobal rules
Set two baselines: never delete without a verified backup, and keep the old version before overwriting.
- They used to live in the instruction file, but over long tasks they could be forgotten
- The instruction file keeps a one-line summary; the details and history moved to a skill
- Why it was done is kept as a decision record (ADR)
| Outputs | Global rules |
Step 2 · Main session
Build + testclaude-infra
Build both hooks with tests in the configuration-management project.
- The delete guard splits compound commands and looks inside sudo and bash -c
- Anything that cannot be parsed is blocked
- 34 tests; commands without any risky keyword pass without being parsed
| Inputs | Global rules |
| Outputs | Hooks + tests |
Step 3 · Deploy script
Deploydeploy
Deploy to ~/.claude with the sync script and register them in the settings.
- What actually runs lives in ~/.claude; the repository is its version control
- Any drift between the two is caught by a test
- Old files are moved to a trash folder before being overwritten
| Inputs | Hooks + tests |
| Outputs | Live hooks |
Step 4 · Main session
Propose callany project
In any project, Claude calls tools as usual.
- Claude does not need to remember the hooks
- Just before each call, the harness always runs them
- The same rules apply the same way in every project
| Outputs | Tool call |
| Refers to | Global rules |
Step 5 · Delete guard
Check dangercommand check
Block deletes, forced resets, disk erases and the like unless the conditions are met.
- It passes only if every target is in a trash folder or a real backup is named
- The session's own temporary folders are exempt
- Every block is logged with its reason
Common misconceptionSingle-file deletes and overwriting copies are out of scope; it does not stop every deletion
| Inputs | Tool call |
| Outputs | Verdict |
| Refers to | Live hooks |
Step 6 · Archive hook
Archive oldpre-edit copy
Before an existing file is rewritten, save a numbered copy.
- Copies go into an archive folder inside the project, with a version number and a content hash
- Identical content is not saved twice; files over 50 MB are skipped
- Even when it fails, the edit is never blocked
Claude CodeThe archive folder is kept out of Git, so copies never leave the machine
| Inputs | Tool call |
| Outputs | Old version |
| Refers to | Live hooks |
Step 7 · Main session
Run or rerouterun tool
If allowed, the tool runs; if blocked, Claude reads the reason and switches to moving or backing up.
- The reason for a block is returned to the model
- The usual switch is to move files into a trash folder
- When a delete is truly needed, a verified backup location is named
| Inputs | Verdict, Old version |
| Outputs | Result |
Loops and rework
- A risky command is blocked → step 4: Read the reason, then move files to the trash or name a verified backup
Key points
Parsing that closes the loopholes. Prefixes like sudo, the bodies of bash -c and eval, and commands chained with pipes or separators are all split apart and checked. Recursive deletes, find with delete, forced git resets and pushes and disk erases are covered, and anything that cannot be parsed is blocked.
Exactly two ways through. A command passes only if every target is inside a trash folder, or it names a backup location that exists and is not empty (the session's own temporary folders are exempt). The reason for a block is returned to the model, so Claude can switch to moving files or making a backup.
Archiving never blocks. The pre-overwrite copy never stops an edit, even when it fails. Identical content is not saved twice, and the archive folder is kept out of Git so backups never leave the machine.