Describes hooks/gateguard.mjs, the hook that refuses destructive shell commands and blind edits (an edit of an existing file the session has not read), and tells the agent to stop, show the user the command and wait after a refusal. Use when about to run a command that deletes or overwrites data (rm -rf, git reset --hard, git clean, a force push, DROP TABLE, Remove-Item -Recurse), when a call comes back refused, or before editing a file not yet read. Not for getting a refused command to run (...
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Gateguard?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/naimkatiman-gateguard)
---
name: gateguard
tier: "1"
description: "Describes hooks/gateguard.mjs, the hook that refuses destructive shell commands and blind edits (an edit of an existing file the session has not read), and tells the agent to stop, show the user the command and wait after a refusal. Use when about to run a command that deletes or overwrites data (rm -rf, git reset --hard, git clean, a force push, DROP TABLE, Remove-Item -Recurse), when a call comes back refused, or before editing a file not yet read. Not for getting a refused command to run (only the user can) and not a sandbox. Guidance for Law 1 (Research Before Executing)."
origin: community
user-invocable: false
---
# gateguard: destructive commands and blind edits
`hooks/gateguard.mjs` runs before each tool call when this package's hooks are installed. It reads the call as text and answers allow (no output) or deny (a reason you receive in place of the tool result). This file says what the hook matches today and what you, the agent, do when it answers.
**Hooks not installed?** The Claude Code plugin wires the hook, and so does `npx continuous-improvement install --target <agent> --hooks`. To verify the guard, ask the agent to send the read-only command `git rev-parse @{u}` through its shell tool exactly as written. A refusal beginning `Unquoted git ref @{u}` confirms this guard ran. A normal Git result, a shell error or a reply without a tool call leaves the installation unverified. A skills-only install supplies written guidance.
## When this applies
- You are about to run a shell command that deletes or overwrites files, discards uncommitted work, rewrites or deletes remote history, or drops data.
- A call came back refused with a reason that starts `Destructive command requested:`, `This session's observation log shows no read of`, `Before editing`, `Before creating`, `Unquoted git ref` or `Target is outside the session project root`.
- You are about to edit an existing file you have not read in this session.
## Rule 1: destructive commands
Covers every shell tool call: `Bash`, `PowerShell` and `Monitor` on Claude Code, and the shell tools of the other agents in `lib/agent-profiles.mjs`. It is always on and no environment variable changes it. The rules are in `lib/destructive-bash.mjs`.
The command is split at `&&`, `||`, `|`, `;`, `&`, line breaks, and the braces and parentheses of a block, and each part is checked on its own. A leading `sudo`, `env`, `command` or `VAR=value` is skipped, `git.exe` or a full path reads as `git`, git's global options (`-C <dir>`) are skipped, and the command line inside `bash -c`, `pwsh -Command`, `cmd /c` or `Invoke-Expression` is checked the same way.
| Rule id in the refusal | Refused | Passes |
|---|---|---|
| `rm-recursive-force` | `rm` with a recursive flag and a force flag, in any order or spelling: `rm -rf`, `rm -r -f`, `rm -Rf`, `rm --recursive --force` | `rm -r dir`, `rm -f file` |
| `git-reset-hard` | `git reset --hard` | `git reset` without `--hard` |
| `git-clean-force` | `git clean` with `-f` or `--force` | the same with `-n` or `--dry-run` |
| `git-checkout-discard` | `git checkout -- <path>`, `git checkout .` | `git checkout <branch>` |
| `git-restore-discard` | `git restore <path>`, also with `--worktree` | `git restore --staged <path>` |
| `git-push-force` | `git push` with `--force`, `-f` or `--force-with-lease` | a plain push |
| `git-push-plus-refspec` | `git push <remote> +<ref>` | |
| `git-branch-force-delete` | `git branch -D`, or `-d` together with `-f` | `git branch -d` |
| `git-stash-drop` | `git stash drop`, `git stash clear` | `git stash pop`, `git stash list` |
| `find-delete` | `find … -delete` | `find` without `-delete` |
| `powershell-remove-item` | `Remove-Item`, `ri`, `rd`, `rmdir`, `del` or `erase` with `-Recurse` or a prefix of it (`-r`, `-rec`), and `rm -r` under the PowerShell tool. Also the text `Remove-Item -Recurse …` inside a quoted argument | `-Force` alone, anything with `-WhatIf`, `Env:`, `Function:`, `Alias:` and `Variable:` paths |
| `powershell-remove-item-pipeline` | A pipeline into `Remove-Item`, `ri`, `rd`, `del` or `erase` (and into `rm` or `rmdir` under the PowerShell tool), or `$_` / `$PSItem` as the path | |
| `cmd-rmdir-tree` | `rd /s`, `rmdir /s` | |
| `cmd-del-tree` | `del /s`, `erase /s` | |
| `format-volume` | `format <drive>` as the command word | a `-Format` parameter |
| `substring:<text>` | The fallback for text no rule above read as a command, such as SQL in a quoted argument or a command handed to `ssh` or `xargs`. Any of these, in any letter case: `rm -rf`, `rm -fr`, `git reset --hard`, `git push --force`, `git push -f`, `--force-with-lease`, `drop table`, `drop database`, `drop schema`, `truncate ` (with the space), `mkfs`, `dd if=`, `rmdir /s`, `del /f /q`, `del /q /f`, `io.directory]::delete(`. And `git branch -D` with a capital D | |
Read as data, not as a command: the value of `-m`, `--message`, `-F`, `--file`, `--body`, `--body-file`, `--title`, `--notes` and `--reuse-message`, the message after git's `-am` and gh's `-b`, `-t`, `-n`, comments, the quoted pattern of `grep`, `rg`, `findstr` or `Select-String`, and the quoted text of an `echo`, `printf`, `Write-Host` or `Write-Output` that prints to the terminal. So `git commit -m "drop the rm -rf step"` passes.
### What the refusal looks like
```
Destructive command requested: rm -rf ./build
1. List ALL files/data this command will modify or delete
2. Write a one-line rollback procedure
3. Quote the user's current instruction verbatim
Matched rule: rm-recursive-force
Destructive Bash gates EVERY call — clearance is not cached. This command stays blocked. Review its effects and rollback with the user. If the user chooses to proceed, they can run it in their own terminal.
```
The three items are for the user to read. The hook reads no answer, and this rule has no clearance: the same command is refused on every retry, whatever you wrote first. `bin/gateguard-clear.mjs`, `ci_gateguard_clear`, `_gateguard_facts_presented`, `CI_GATEGUARD_FILE_GATE=off` and `CI_GATEGUARD_EXCLUDE` belong to rule 2 and do nothing here.
### What you do next
1. **Stop.** Do not send the command again. Do not reword it to get past the match (a script file, flags held in a variable, `find -exec rm`, an interpreter one-liner). Those forms pass because the rules match text, and running one is the unreviewed deletion this hook is there to stop. Done when you have sent no further call with the same effect.
2. **Find out what it would change, with read-only commands.** For files, `ls` or a dry run (`git clean -n`, `Remove-Item -WhatIf`). For git, `git status --short`, `git stash list`, `git log --oneline origin/<branch>..<branch>`. With the plugin installed, `bash "${CLAUDE_PLUGIN_ROOT}/scripts/git-state-snapshot.sh"` (source: `scripts/git-state-snapshot.sh`) prints head, upstream, dirty count, root and branch as one JSON line. Done when you can name each path, ref or table the command touches.
3. **Tell the user in one message:** the exact command, the matched rule, what it deletes or overwrites, a one-line rollback or the words "no rollback", and the user's instruction quoted word for word. If no instruction asked for this, say so. Done when the message is sent.
4. **Wait.** To run it anyway, the user runs the command in their own terminal, outside the agent. This rule has no switch: turning it off means disabling or uninstalling the hooks, and that is the user's decision, not yours. Done when the user has answered.
5. **After the user says it ran,** confirm with one read-only command (`ls`, `git status`, `git log -1`) before you build on the result. Done when the output shows the change.
With no user watching, put steps 2 and 3 in your final report, mark the command "not run", and continue only with work that does not depend on it.
A narrower command that does what was asked without the loss is not a dodge: `git branch -d` (git itself refuses it for an unmerged branch), `git stash` before a checkout, `git restore --staged <path>`.
### Not matched
These cases fall outside the destructive-command classifier. Check the actual target and effects. Existing authorization covers the specific action the user requested. Ask before any additional destructive action. A hook refusal still requires the procedure above.
- `rm -r <dir>` without a force flag and `rm -f <file>` under Bash, `find … -exec rm`, `shred`, `unlink`, `rsync --delete`.
- Anything the text does not show: `bash cleanup.sh`, `pwsh -File`, `pwsh -EncodedCommand`, flags held in a variable, a `python -c` or `node -e` that deletes.
- Programs that modify files directly, including `sed -i`, `mv` and `cp`. Literal shell output redirection follows the separate file policy below.
- Git: `git checkout -f <branch>`, `git push origin --delete <branch>` and `git push origin :<branch>`, `git worktree remove --force`, `git filter-branch`, `git rebase`, `git commit --amend`.
- Data and infrastructure: SQL `DELETE FROM`, `dropdb`, `prisma migrate reset`, `redis-cli FLUSHALL`, `terraform destroy`, `kubectl delete`, `docker system prune`, `aws s3 rm --recursive`.
- PowerShell: `Remove-Item -Force` on named files, `Clear-Content`, `Format-Volume`.
A standalone literal `cat` heredoc for a note is treated as data. Its output destination still goes through the file policy. Other shell forms can be refused when the guard cannot distinguish data from commands.
## Rule 2: blind edits
Covers recognized file writes and patches, and supported shell output redirects to literal paths. `CI_GATEGUARD_FILE_GATE` picks the mode, and `off` skips this rule.
**`evidence` (the default).** The edit is allowed when any one of these holds:
- The file does not exist yet. New files always pass.
- The project has no observation log (`<instincts root>/<project-hash>/observations.jsonl`, written by `bin/observe.mjs`). With no log the hook cannot judge, so it allows.
- This session's rows in that log show a completed call that looked at the file: a `Read`, `Edit`, `Write`, `MultiEdit`, `NotebookEdit` or `NotebookRead` of the same path, a patch that names it, or a `Bash`, `PowerShell`, `Grep` or `Glob` call whose command, pattern or output contains the file's base name.
- The session state holds a clearance for the file.
Anything else is refused. A call that names several files is refused until each existing one is read or cleared.
```
This session's observation log shows no read of src/app.ts. Read it, then retry the same call.
If it is already in context (a file attached with @ or by the IDE leaves no row in the log), clear the gate and retry: node "<path>/bin/gateguard-clear.mjs" --state "<path>/gateguard-session.json" "src/app.ts"
```
What you do: read the file with the read tool, then send the same edit again. Done when the edit goes through. Run the printed clear command, exactly as printed, only when the file really is in your context and the log cannot show it: attached with `@`, attached by the IDE, or read before the log rotated (at 10,000 rows). A read in another session, a denied call and a failed call leave no row that counts.
**`read`.** With `CI_GATEGUARD_FILE_GATE=read`, an existing file requires a recorded successful read by this agent in this session, matched by its full path. A missing receipt holds the write. Read the file with the host's file-reading tool and retry. New files can be created.
**`strict`.** The first `Edit`, `Write` or `MultiEdit` of every file is refused, new or existing, read or not. The reason lists four facts (the files that import this one, the public functions or classes affected, the field names and date format of any data file it reads or writes, the user's instruction quoted) and prints two ways to clear: the `bin/gateguard-clear.mjs` command and the `ci_gateguard_clear` MCP tool. Put the four facts in your reply, run one clear route exactly as printed, then retry. Done when the retry goes through. A session can clear 1000 distinct files by default. Past that the clear is skipped and the edit stays refused. Where a harness forwards unknown tool parameters, a retry with `_gateguard_facts_presented: true` also clears. Claude Code rejects that parameter.
Clearances are kept in `gateguard-session.json` under `<instincts root>/<project-hash>/sessions/<session-id>/`, one file per session, and are dropped 12 hours after that file was created.
Two more refusals come from the same hook:
- **An unquoted git `@{…}` ref in a shell command** (`@{u}`, `@{upstream}`, `@{push}`, `@{1}`, a date). The reason prints your command with the ref in single quotes. Run that command. Always on, no switch.
- **Target lock, off by default.** With `CI_GATEGUARD_TARGET_LOCK=block`, a recognized file write or shell output redirect outside the project root is refused. Relative paths are resolved from the host working directory. If the directory is unclear, supply an absolute target. A clearance cannot override the lock. Use the intended checkout.
## Switches
Environment variables of the hook process. Set them where the agent starts. None of them changes rule 1 or the `@{…}` rule.
| Variable | Default | What it changes |
|---|---|---|
| `CI_GATEGUARD_FILE_GATE` | `evidence` | The mode of rule 2: `evidence`, `read`, `strict` or `off`. An unknown value falls back to `evidence` and prints the accepted modes. |
| `CI_GATEGUARD_EXCLUDE` | unset | Comma-separated path fragments, matched without regard to letter case or slash direction. A file call whose targets all match skips rule 2 and the target lock, and the hook prints a stderr line naming the fragment. A call that mixes matching and other paths is still checked for the others. A one-character fragment such as `/` or `.` matches every path, so rule 2 is off, and the stderr line says so. |
| `CI_GATEGUARD_TARGET_LOCK` | unset | `block` turns the target lock on. Any other value leaves it off. |
| `CI_HOOK_FAILURE` | unset | With `block`, a running guard refuses malformed input and reports a guard failure as a refusal on supported pre-tool hooks. |
| `GATEGUARD_MAX_CLEARED_FILES` | `1000` | A positive integer: how many distinct files one session can clear. |
| `GATEGUARD_SESSION_DIR` | unset | A directory for `gateguard-session.json`, in place of the per-session default. The tests use it. |
| `CI_INSTINCTS_HOME` | `~/.claude/instincts` | An absolute path that moves the instincts root, which holds the log and the session state. |
## Limits
- **Rule 1 matches text. It is not a sandbox.** It refuses what the table lists and passes everything under "Not matched".
- **Rule 1 has no approval path through the agent.** A refused command runs only when the user runs it.
- **The default evidence mode accepts indirect evidence.** A shell or search row containing the base name can match another file with that name. Use `read` mode to require a file-reading tool receipt.
- **The clear command is an honor system.** The hook cannot tell whether the file was read or the facts were presented before it was run.
- **Claude Code notebook writes.** The default evidence mode does not read the `notebook_path` field. The `read` mode checks that field.
- **The hook reads no git state.** It does not notice another session writing to the same checkout.
- **It can be absent or fail open.** The hook needs Node on the agent's PATH. By default malformed JSON is allowed. `CI_HOOK_FAILURE=block` covers input and errors inside the running guard. The host controls startup failures and timeouts.
- **The guidance has one eval, of the reply after a refusal, and it ran no command.** Eval, 2026-12-01: with this skill loaded, 16 of 16 replies to a refused destructive command removed nothing by another route, left the check alone and left the decision with the user, against 3 of 16 without it, measured on the stated next step, not on the work, and in neither arm did a reply plan to remove something the user had not asked to remove ([method, every reply and the limits](https://github.com/naimkatiman/continuous-improvement/blob/main/evals/gateguard/README.md)).