transition2.aiStart a project

AI Reliability & Operations

Guard Hooks for Agent Shells

A shell guard that reads commands the way a shell does, proves who owns a process before it is stopped, and asks for a recorded grant before production data is deleted.

Eric MacDougall ·

Answer first

Give an agent's shell three kinds of guard. A command guard that parses a command the way a shell would, then blocks secret dumps and deletion of logs. A process guard that lets the agent stop only processes it can prove it started. A production-delete guard that refuses destructive SQL against a production target unless a recorded, time-limited grant names that exact target. Run them as hooks before the tool executes, and design each to fail in the safe direction. None of them is a security boundary against a determined adversary; they stop the careless mistakes that actually happen.

Why guards exist

The incidents behind these guards were ordinary. An agent announced in chat that it would delete live rows and then did, with no recorded approval. A stop of processes by name pattern, and a stop fed by a process finder, ended other people's runs on a shared host. A test that read an endless stream and a browser sweep that opened dozens of renderers took a machine down. In each case a rule written in prose existed and nothing enforced it. A hook runs before the tool and can refuse, which is why it is the right place (hooks reference).

Guard 1: read the command first

A guard that searches for the word rm is easy to walk around. The command guard instead lexes quotes, escapes, here-documents, command substitutions, wrapper commands, sh -c strings, ssh and container-exec strings, and case or Unicode lookalikes, and only then applies rules. Secret dumps (printing the environment, reading credential files) and log deletion are always enforced. Broad stops warn by default.

Behaviours worth copying:

  • Overwrite rules check that the file exists. Truncating a file that does not exist is allowed; truncating a real log is blocked.
  • Depth has a budget. Nesting beyond eight levels cannot be analysed. If the input looks harmless it is allowed with a warning; if it names a protected path it is blocked.
  • Interpreter one-liners are scanned as text, since a guard cannot execute them to see what they do.
  • A shell guard judges shell only. Edit tools are separate tools and need their own guard.

One more thing, stated plainly: a known gap in this guard at the pinned commit was reported to the maintainers separately. We deliberately do not describe it here, and you should not assume any single guard is complete.

Guard 2: stop only what you started

The process guard blocks pattern stops, finder-fed stops, interpreter code that signals processes, and service stops. A plain stop is allowed only for a literal process id that is provably the caller's own. There are two proofs: the process's working directory or arguments are inside the caller's own git worktree, or the process is in a ledger of processes the agent launched through the guard's own spawn command. The ledger entry holds the pid, its start time, the root and a signature, and matching compares pid and start time together so a reused pid does not inherit ownership.

The guard refuses ancestors, other users' processes, protected programs and interactive shells. It allows harmless forms such as probing whether a pid exists, listing signal names, job specs, and text that merely contains the word. The grammar is closed: anything it cannot parse fails closed. Remote stops through ssh or container exec are blocked, with a narrow exception for the monitoring tool's orphan cleanup.

Known limits from the documentation: text piped into a remote login shell with no command named, and substitutions inside ${...}, are not read, and ancestry is deliberately not treated as proof.

Guard 3: no destructive SQL on production without a grant

The production-delete guard treats these as destructive: DELETE FROM, DROP of tables, databases, indexes or schemas, TRUNCATE, and UPDATE ... SET with no WHERE. Comments and quote characters are removed before matching. A production target is recognized by a remote flag, a host or database name containing prod, a connection string, or environment assignments.

A grant is the only way through: it carries a user-sourced answer id, names the exact target (never a substring), is signed, expires after 900 seconds by default and 3,600 at most, and covers one target. The fail direction is asymmetric on purpose. Before the guard has matched a destructive statement against a production target, an internal error allows the command so a bug does not stop all work. After a match, any error denies, including a broken grant store.

Disclosed gaps: run-time-built SQL, multi-table deletes in some database clients, document-database deleteMany, cache flush commands, generic query tools and plain file removal are out of scope, and the guard says so. Do not rely on it for those.

A fourth guard for heavy jobs

A heavy-job guard denies full test suites, installs, container runs and browser sweeps on a workstation unless an escape hatch with a stated reason is used. The reason is logged to the session log, one use per hour by default. Wrapper nesting beyond twelve levels and absurdly long commands are denied. Bugs in this guard fail open, and hostile-input testing is advisory only.

What we ran

From our lesson labs: Python 3.12.3, standard library only, a throwaway home directory, and nothing executed. The guards were asked only for a verdict. For each guard the lab first built a small toy, then planted bugs that must each break its tests. The command-guard toy had 12 tests and the process-guard toy 9:

Ran 12 tests in 0.021s
OK
mutant QUOTES: FAILED (failures=5)
mutant SHELLC: FAILED (failures=3)
mutant SUBSHELL: FAILED (failures=2)
mutant NORMPATH: FAILED (failures=1)
mutant DEPTH: FAILED (failures=1)
Ran 9 tests in 0.002s
OK
mutant BROAD: FAILED (failures=4)
mutant FEED: FAILED (failures=3)
mutant OWNER: FAILED (failures=8)
mutant ANCESTOR: FAILED (failures=1)
mutant USER: FAILED (failures=1)
mutant PROTECTED: FAILED (failures=1)
mutant LEDGER: FAILED (failures=2)

Then the real command guard at commit 504d930. Twelve disguised forms of a log delete or secret dump were all blocked, plain commands were allowed, and the depth budget behaved as described above:

== 1. plain and direct
allow  -                            ls -la
allow  -                            rm -rf /tmp/scratch-build
block  secret_dump/env-dump         printenv
allow  -                            printenv HOME
== 2. costumes
block  log_delete/log-delete        split quotes in the path
block  log_delete/log-delete        split quotes in the command
block  log_delete/log-delete        env prefix plus command wrapper
block  log_delete/log-delete        shell string
block  secret_dump/env-dump         command substitution
block  log_delete/log-delete        variable assigned first
block  log_delete/log-delete        dot dot in path
block  log_delete/log-delete        over ssh
block  secret_dump/credential-file  inside docker exec
block  log_delete/log-delete        find -delete
block  log_delete/log-delete-text   python one-liner (text scan)
block  secret_dump/env-dump         env piped to grep
== 3. overwrite rules look at files that exist
block  log_delete/log-delete        truncate, file exists
block  log_delete/log-delete        redirect, file exists
allow  -                            truncate, file does not exist
== 4. nesting past the depth budget
warn   parse/unanalysable           twelve levels, harmless
block  log_delete/unanalysable      twelve levels, names a protected path

In the toy lab, a plain text match used as a control missed five of eight disguises while the parsing guard blocked all eight. The real process guard, in a scratch repository, with only three harmless sleep processes that the script itself started:

== 1. a plain kill, judged by who owns the process
allow  -                      kill, started by spawn
allow  -                      kill, cwd inside the worktree
block  not-yours              kill, cwd outside the worktree
block  not-yours              kill pid 1
block  not-yours              kill, pid that does not exist
== 2. broad kills and finders
block  broad-kill             pkill -f sleep
block  broad-kill             killall sleep
block  pidfeed                pgrep piped to xargs kill
block  pidfeed                kill with command substitution
block  unprovable-target      kill in a for loop
block  script-kill            python os.kill one-liner
== 3. things that are not kills, or are harmless
allow  -                      kill -0 literal pid
allow  -                      kill -l
allow  -                      pgrep alone
allow  -                      grep for the word kill
allow  -                      job spec
== 4. across a remote shell
block  remote-kill            ssh box kill
block  remote-kill            docker exec kill

And the real production-delete guard and heavy-job guard, called as functions with no database or build host involved:

== 1. prod_delete_guard: what counts
allow  -                  same delete, local database
deny   no grant           psql to a prod host, DROP TABLE
allow  -                  psql to localhost, DROP TABLE
allow  -                  prod host, SELECT only
deny   no grant           prod host, UPDATE with no WHERE
allow  -                  prod host, UPDATE with WHERE
== 2. disguises and gaps
deny   no grant           split quotes and a comment
deny   file unreadable    SQL in a file that does not exist
allow  -                  prose that only mentions the words
deny   no grant           connection string naming prod
== 3. heavy_job_guard: what counts
deny   full-suite   pytest, whole suite
allow  -            pytest, one file
deny   full-suite   npm test
deny   install      pnpm install
deny   browser      headless browser sweep
deny   container    docker run
allow  -            ls
allow  -            grep for the word pytest
allow  -            commit message that says pytest
== 4. the escape hatch
allow  full-suite   hatch with a reason, full suite
deny   browser      hatch with a reason, browser
deny   full-suite   hatch with no reason

One honest observation from that run: the documentation lists SQL built at run time as out of scope, yet a probe against a production host was denied anyway, because input piped from a source the guard cannot read counts as unreadable. We do not recommend relying on that. Not run: grants (they need a recorded answer from a real session), process-id reuse against the real guard (read in code only), the hook entry points with real payloads, and any real database, build host or remote machine.

Decision framing

Guards add friction: an agent will sometimes be blocked for a legitimate command and need a grant or a reason. They are worth it when agents share hosts, hold credentials, or can reach production data, and when the cost of one wrong command is higher than the cost of a prompt. On a disposable sandbox with nothing to lose, they are more machinery than you need.

Risks and limits

  • Our threat model is a careless or hurried agent, not an adversary. An agent that can edit the installed hooks, or run a command through a tool the hooks never see, defeats them.
  • A known gap in the command guard at the pinned commit was reported and is intentionally not described. Treat the guard as one layer and keep backups, least privilege and review in place.
  • Fail-open choices (errors before a match allow the command) trade safety for availability. Decide that trade deliberately.
  • The heavy-job guard acts only on macOS unless forced, and its fan-out check did not behave as its documentation says in one probe, so we make no claim about it.

FAQ

What is a guard hook for an AI agent?
A guard hook is a small program the agent runtime calls before a tool runs. It receives the command, decides allow, warn or block, and can stop a destructive action before it happens, regardless of what the model intended.
Why not just match dangerous words in the command?
Text matching misses commands written in a different costume: split quotes, command substitution, wrappers, nested shells. A guard has to read the command the way a shell would before any rule looks at it.
How should an agent be allowed to stop a process?
Only a process it can prove it started, for example by checking that its working directory is inside the agent's own worktree or that it appears in a ledger of processes the agent launched. Pattern-based and piped stops should be blocked.
Can a guard hook be bypassed?
Yes. A guard is one layer for careless agents, not a defence against an adversary who can edit the hooks or run commands through a path the hook never sees. Known gaps should be reported and fixed, not hidden.

Sources