# Feature: Unattended Security

<!-- toc -->
- [What turns it on](#what-turns-it-on)
- [The guard](#the-guard)
- [User guard rules](#user-guard-rules)
- [Phase 4: a PR request instead of a push](#phase-4-a-pr-request-instead-of-a-push)
- [The runner's launch](#the-runners-launch)
- [The publish step](#the-publish-step)
- [Phase 5 and channels](#phase-5-and-channels)
- [What leaves the machine is scanned](#what-leaves-the-machine-is-scanned)
- [Operator code in what a run can write](#operator-code-in-what-a-run-can-write)
- [Text a run did not write is data](#text-a-run-did-not-write-is-data)
- [The OS sandbox is the boundary](#the-os-sandbox-is-the-boundary)
- [Machine setup (applied by the operator)](#machine-setup-applied-by-the-operator)
- [Hook timeout and cost](#hook-timeout-and-cost)
- [Residual risk, accepted and stated](#residual-risk-accepted-and-stated)
- [Reference](#reference)
<!-- /toc -->

**Pattern**: a run nobody watches is also a run nobody can stop in time. Its
input is text other people wrote (a ticket, a page, a PR thread), so it has to be
assumed that some of that text will ask for something the task never did. This
feature keeps an unattended run's reach to its own worktree: it can build, test
and commit, and everything that leaves the machine goes through the autopilot
runner, after the runner checked it again outside the run. An attended run is unchanged.

## What turns it on

`MULTI_AGENT_UNATTENDED=1` in the hook's own environment, and nothing else. The
autopilot runner sets it on the `claude` process it spawns, and hooks inherit
it. It is never read from `agent-state.json` or any file the run writes: a rule
the run can switch off is not a rule. `lib/unattended.sh` (`ma_unattended`) and
`lib/unattended.mjs` (`isUnattended`) answer the question; `agent-guard.py` reads
the variable the same way.

With the variable unset every hook behaves as it did without this feature:
`smoke-unattended-redteam.sh` runs the same inputs both ways, and
`test/unattended-guard.serial.mjs` holds the attended verdicts.

## The guard

`scripts/agent-guard.sh` is the PreToolUse hook on `Bash`, on
`Edit|Write|NotebookEdit`, and on `WebFetch|mcp__multi-agent__.*`
(`install/claude.mjs` registers all three; a matcher is a regex over the tool
name, so the last one covers `WebFetch` and every toolkit tool, including
`agent_run_steps`, which dispatches the web tools, and the `ios_open_url` /
`android_open_url` tools, which open a URL in the device browser). The
"already present" check for the Bash guard requires the tool-name matcher
`Bash`, so a guard entry under a `Bash(...)` command filter, which a hook
never matches, does not count and the `Bash` entry is added beside it.
`install/templates/claude-hooks.json` carries the
same three registrations. Its decision core is
`agent-guard.py`; the unattended rules are `scripts/unattended_policy.py` and the
lists they read are `schemas/unattended-policy.json`.

In every mode it blocks AI attribution in commit messages and a force-push to
main, master or develop, including `git push -f origin HEAD`, a remote with any
name, `-o` / `--push-option` values, and a push that names another checkout
with `git -C <dir>` or follows a `cd`. A push that removes a protected branch
without a force flag is blocked the same way: `--delete` / `-d`, a refspec with
an empty source (`git push origin :main`), `--mirror` (always), and `--prune`
with a glob refspec or none. The rule reads through a subshell or group
(`(git push -f origin main)`), a `sh -c` / `bash -c` / `zsh -lc` payload and an
`eval`, and a `cd` it cannot read literally (`cd "$W"`) leaves the directory
unknown for every relative `cd` after it, so a bare force-push there is
blocked.

### The model under the variable: refuse what it cannot parse

A list of bad commands cannot be complete. Under the variable the guard
inverts the default: a Bash command is judged only when it reduces to
**simple commands joined by `;`, `&&`, `||` and plain pipes between
non-interpreter commands**. Anything that hides a command from a line reader is
refused outright, before any family check runs:

- a subshell or group - `(`, `)`, `{`, `}` (outside quotes)
- a backtick, `<(...)` or `>(...)` substitution, and `$((...))` arithmetic
- a `$(...)` the guard cannot analyse: one nested inside another, an
  unterminated one, or one in command position (`$(echo git) push`)
- a heredoc with an unquoted delimiter (its body expands `$(...)`)
- a `${...}` other than a plain `${NAME}`
- a background job - a bare `&` (not `&&`, `&>`, `>&` or `2>&1`)
- a shell keyword in command position - `if`, `then`, `for`, `while`, `case`,
  `function`, `eval`, `exec`, `source`, `.`, `export`, `trap`, `set`, `unset`,
  `alias`, zsh's `sched`, and the rest. zsh's `noglob` and `nocorrect`, like
  `builtin` and `command`, are stripped first, so the command after them is
  what is judged
- an interpreter reading its program from stdin - `echo ... | bash`,
  `python3 -`, `bash <<< '...'` (a pipe into `node script.mjs` only passes
  data and is judged like running the script), or `xargs` with a command
- a `-c` cluster on a shell or Python - `bash -lc "..."`, `sh -c ...`,
  `python3 -c ...`, and zsh's `emulate [-LR] [-o opt] <shell> -c ...`
- an inline program on any other interpreter - `node -e/-p/--eval/--print`,
  `perl -e/-E` (in a cluster too, `-lne`), `ruby -e`, `osascript -e`,
  `php -r/-R/-B/-E`, `lua -e`, `Rscript -e`, `swift -e`, `bun -e/--eval/--print`,
  `deno eval` / `deno repl`
- a program file the run could have written. An interpreter's script
  (`bash x.sh`, `python3 x.py`, `node x.mjs`, `osascript x.scpt`, `tclsh`,
  `swift x.swift`, `bun`/`deno run <file>`), a module `python3 -m` finds in the
  cwd, a command named by path (`./x.sh`, also behind `nohup`, `env`,
  `timeout`), an `awk -f` / `sed -f` program file and the Makefile `make` reads
  must be one of: under `~/.claude/scripts` or `~/.claude/lib` (the installed
  pipeline, itself write-protected), a file the user cannot write, a binary in
  `node_modules/.bin`, or a file tracked by git and unmodified against HEAD
  (staged, unstaged and untracked changes all count as modified)
- `make` with `--eval`, or with a `VAR=value` operand a recipe could expand
- an awk program with `system(`, a pipe (`|`, not `||`), `getline`, a `print >`
  redirect, `@load` or `@include`, and awk's `-i` / `-l` / `-E` loaders
- a sed program with a `w` / `W` / `e` command or the `w` / `e` flag of `s///`
- `script` and `watch`, which run the rest of the line in a way the guard does
  not model, and `env -S` / `env -C`, which re-split the command or move it
- a command run without `MULTI_AGENT_UNATTENDED`, which every pipeline script
  reads to know nobody is watching: `env -i` (also `-`, `--ignore-environment`
  and a cluster such as `-iv`) clears the environment, and `env -u NAME`
  (`-uNAME`, `--unset NAME`, `--unset=NAME`) is refused for every name an
  assignment of it would be refused for, every `MULTI_AGENT_*` included, and
  for a name the shell rewrites first (one holding `$`, a backtick, a glob or a
  brace). `genv` and `gnuenv`, GNU's env under its Homebrew names, are read the
  same way. A long option is read both ways an env binary reads it: GNU takes
  any unique prefix (`--ignore-env`, `--u`, `--spl`, `--ch`), and BSD reads
  `--X` as the option `-`, which clears the environment, followed by X (`--i`,
  `--vi`). So only `--null`, `--debug`, `--help`, `--version`,
  `--list-signal-handling` and the three signal options, spelled exactly, pass;
  every other long option is refused. `-a` (in a cluster too) is refused as
  `--argv0` is, since it runs the program under another name, whatever the
  env binary is called: GNU env also runs as `env` (Homebrew's `gnubin` on
  PATH, or `./env`), and BSD env rejects `-a` anyway.
  `unset`, `export -n` and `exec -c` are shell keywords and refused as such;
  `su` is refused like `sudo`, since `su -l` starts a login shell with a fresh
  environment. A shell started with `--norc` or `--noprofile` keeps the
  environment it was given, so it drops nothing
- a command name built at run time - a first token starting with `$`
- `sudo`, `doas`, `su`, and `find -exec` / `-execdir` / `-ok`
- more than 50 segments (refused unparsed, so a 300-`npm ci;` line ending in a
  push is rejected in milliseconds, not after 300 git reads)

**`$(...)` is analysed, not refused.** Each top-level substitution is
extracted quote-aware and balanced, and its inner command goes through the same
parser and every policy check as if it were typed directly, in the cwd its
segment runs in; the outer command is then judged with the substitution replaced
by a placeholder. `SHA=$(git rev-parse HEAD)` passes; a substitution whose inner
command pushes, POSTs, reads a credential or writes a protected path is refused
for what that inner command does. A substitution-built value can never name a
command, a git or gh subcommand, a publisher verb or a write target. A heredoc
behind a quoted delimiter (`<<'EOF'`) is stripped as literal data before
parsing, so `git commit -m "$(cat <<'EOF' ... EOF)"` works. Segments and
redirections are split quote-aware, so a `|` or `;` inside a quoted sed, jq or
awk program is text. Variables assigned earlier in the same command
(`D=build; ... > "$D/x"`) and `$HOME` are expanded; a write target or `cd` whose
variable cannot be resolved is refused, and so is a relative write after an
unresolvable `cd`.

**The documented steps are written to pass.** The guard judges each Bash call
alone, so every command in the phase docs is one the guard accepts as written
once its placeholders are filled.
Shell control flow lives in installed scripts (`worktree-prepare.sh`,
`build-lock.sh`, `probe-evidence-capability.sh --platform auto`,
`triage-memory.mjs prior-art`, `memory-save.sh --from-json`, and the `--out`,
`--json-out`, `--append`, `--stack-from` and `--analysis-from-state` options of
the gates) and the doc calls each as one command. A write target is spelled
`{worktreePath}/...`, the literal path, rather than `$WORKTREE/...`, because the
guard checks only a target it can read from the command itself; LLM-written JSON
(analysis, plan, reviewer and triage output) is written with the Write tool. The
few steps a run does not take carry a `# unattended: skipped` comment on the
line before them. `test/unattended-doc-lines.test.mjs` runs every command line
of the bash blocks in `phases/*.md` and `features/unattended-gates.md` through
the guard (a `NAME=value` line such as `S="$HOME/.claude/scripts"` sets NAME for
the later lines of the same document), allows only the refusals recorded in
`test/fixtures/unattended-doc-lines-refused.json`, and fails on any other.

Wrappers are stripped WITH their option values before the command is read, so
`env -u FOO`, `nice -n N`, `timeout -s SIG N`, `nohup`, `caffeinate`,
`stdbuf`, `command`, `builtin`, `xcrun [--sdk X]`, `arch -arm64` and
`sandbox-exec -p ...` do not smuggle the wrapped command past the check
(`xcrun git push` is judged as `git push`; `xcrun --find` runs nothing).
Assignments the wrapped command receives - leading ones, those after `env`, and
`arch -e` values - are judged like any other. Command basenames are casefolded
(`GIT push`), and an absolute path is reduced to its basename (`/usr/bin/git`)
once the file itself passed the program-file check above.

Configuration that makes a later program run code is refused wherever it is
set. One key list (`GIT_EXEC_CONFIG`) covers `git -c`, `git --config-env`,
`git clone -c` and `git config`: `alias.*`, `core.hooksPath`,
`core.sshCommand`, `core.fsmonitor`, `core.editor`, `core.pager`,
`core.askPass`, `core.gitProxy`, `sequence.editor`, `diff.external`,
`diff.*.command` / `textconv`, `difftool.*`, `mergetool.*`, `merge.*.driver`,
`filter.*`, `credential.*`, `gpg.*`, `url.*.insteadOf` / `pushInsteadOf`,
`include*`, `http.*`, `remote.*`, `branch.*.remote` / `pushRemote`,
`protocol.*`, `pager.*`, `hook.*`, `submodule.*`, `trailer.*` and the rest.
`git config` writes to `--global` / `--system` and `git config --edit` are
refused too. The environment: every `GIT_*` variable except the commit
identity, date and prompt ones (so `GIT_CONFIG_COUNT` / `KEY_n` / `VALUE_n`,
`GIT_CONFIG_GLOBAL`, `GIT_EXTERNAL_DIFF`, `GIT_SSH(_COMMAND)`, `GIT_ASKPASS`,
`GIT_EXEC_PATH`, `GIT_DIR`), `EDITOR` / `VISUAL` / `PAGER` / `GIT_EDITOR` /
`GIT_PAGER` other than a no-op (`cat`, `true`, `:`, `less`), `BASH_ENV`,
`ENV`, `PROMPT_COMMAND`, `CDPATH`, `PATH`, `HOME`, `XDG_CONFIG_HOME`,
`NODE_OPTIONS`, `PYTHONPATH`, `PERL5OPT`, `RUBYOPT`, `LD_*`, `DYLD_*`,
`npm_config_*` and `MULTI_AGENT*`. Git subcommands that run a command line are
refused: `rebase --exec` / `-x`, `bisect run`, `submodule foreach`,
`difftool -x` / `--extcmd`, `filter-branch`, `grep -O`, `--upload-pack`,
`--receive-pack`, `--exec`, `--template`, and any `ext::` or `fd::`
transport.

Under the variable it also blocks:

| Family | Blocked | Why |
|---|---|---|
| Outward writes | `git push` in any form; `gh pr create/merge/ready/edit/comment/review/close`; `gh issue create/close/edit/comment/...`; `gh release`, `repo`, `workflow`, `secret`, `variable`, `label`, `alias`, `ssh-key`, `gpg-key`, `extension`, `codespace` writes; `gh api` with a mutating method, with `-f`/`-F` (attached forms too) / `--input` and no explicit GET, a GraphQL `mutation`, or a graphql `@file` field; `gh auth` (except `auth status`); `curl`/`wget` with a body or a non-GET method (`-x`, `--proxy`, `--connect-to`, `--resolve`, `-K`, `--unix-socket`, `--doh-url` are refused outright) | the runner is the only writer |
| Publisher scripts | `jira-publish.sh`, `post-pr-review.sh`, `update-issue-progress.sh`, `analysis-jira-write.sh`, `jira-attach.sh`, `plan-attachment.mjs upload`, `md2confluence-v3.py create/update`, `multi-repo-pipeline.sh push/pr`, `keychain-save.sh`, `credential-store.sh get/set/delete`, `keychain.py get/set/delete`, `gate-ledger.mjs append`, `autopilot-control.mjs off`, `routine-share.mjs share/import/update/delete` | the same, a credential never lands in the transcript, gates record verdicts through their own in-process calls (`park` stays allowed: it only stops the run, so there is nothing to forge), a run never switches off the schedule that launched it, and never publishes a routine or installs one that a later run would execute |
| Git redirection | `git remote add/set-url/rename/remove`; the configuration, environment and subcommands above | each changes what a later command runs or where the runner's push goes |
| Keychain | `security find-*`, `dump-keychain`, `export`, `add-*password`, `delete-*password`, also behind leading flags (`security -q find-generic-password`) | a run needs no credential it did not get from a fetcher |
| Network | `curl`/`wget`/`WebFetch`/the toolkit's `web_*` tools to a host outside the allowlist, or to a URL built at run time; `web_eval` outright; every step of `agent_run_steps` judged as if called directly (a nested `agent_*` step or an unreadable step list refused); `ios_open_url` / `android_open_url`, `xcrun simctl openurl` and `open` with a web URL outside the allowlist (an app's own deep-link scheme passes; `open` of anything but an allowed URL is refused); the `research_*` tools outright; `llm_delegate` unless `route-on --free` is on, with its `path` checked like any write target | an injected "fetch this script" is the first step of most attacks, and a browser opened on a crafted URL sends data out in the query |
| Packages | `npm/pnpm/yarn/bun install|add <pkg>`, `npm install-test/it <pkg>`, `update`, global installs (`yarn global` too), `npx` / `npm exec` / `npm x` of a binary not already in `node_modules/.bin` (with no TTY npm installs it without asking), `npx --yes/--package/--call`, `pnpm dlx`, `pnpx`, `yarn dlx`, `bunx`, `bun x`, `uvx`, `uv tool run/install`, `pipx run/install`, `pip install <pkg>`, `uv/poetry add`, `gem install`, `bundle add`, `brew install`, `cargo add/install`, `go get`, `go install pkg@v`, `swift package add-dependency`, `pod update`; `npm ci`, `pod install` and `pip install -r` once the run changed the manifest they restore from | slopsquatting: a model asked to add a dependency can name one that does not exist yet, and someone can register it |
| Host changes | `crontab` (except `-l`), `launchctl` (except the read verbs), `defaults` writes, `altool`, `notarytool` | a job or preference outlives the run and runs as the operator; an upload is outward |
| Manifests | Edit, Write or a shell write to `Package.swift`, `Package.resolved`, `package.json`, lockfiles, `Podfile(.lock)`, `build.gradle(.kts)`, `settings.gradle(.kts)`, `libs.versions.toml`, `requirements*.txt`, `pyproject.toml`, `go.mod`/`go.sum`, `Cargo.toml`/`Cargo.lock`, `Gemfile(.lock)` | the same decision, made through the file instead of the command |
| Protected paths | `.github/workflows/`, `CODEOWNERS`, any `.claude/` in a repo, `.mcp.json`, `.git/` (all casefolded), a directory that is an ancestor of one of these (`mv evil .github`), `~/.gitconfig`, `~/.config/git/`, `~/.config/gh/`, `~/.ssh/`, `~/.gnupg/`, `~/.npmrc`, `~/.netrc`, `~/.git-credentials`, the shell startup files (`~/.zshrc`, `~/.zshenv`, `~/.zprofile`, `~/.zlogin`, `~/.zlogout`, `~/.bashrc`, `~/.bash_profile`, `~/.bash_login`, `~/.profile`), `~/Library/LaunchAgents/`, `~/.claude.json`, the data directory, staging and quarantine under `~/.multi-agent-unattended-homes/` and every directory below `$HOME` above it or above the PR requests directory (`mv ~/.multi-agent-unattended ~/x`), the preferences candidates `~/.config/multi-agent-pipeline/` and the file `$MULTI_AGENT_PREFS` names, the toolkit's `~/.config/multi-agent-mcp/` and the same file's pre-4.0 path, and every directory below `$HOME` above one of those, or the directory holding the `$MULTI_AGENT_PREFS` file wherever it is (`mv ~/.config ~/c2`), the whole host roots `~/.claude`, `~/.copilot` and `~/.codex` (every path under them, `config.toml` and all, not an enumerated list - an unattended run writes under `~/.multi-agent-unattended` instead), everything under the runner's root | each one changes what runs next, with more rights than the run has |

Shell write targets are read from redirections (`>`, `>>`, `&>`), `tee`,
`sed -i`, `perl -pi`, and the targets of `cp`/`mv`/`ln`/`install`/`rsync`/`ditto`
(including `-t DIR`, `--target-directory=DIR`, `-tDIR`), `rm`, `touch`,
`truncate`, `chmod`, `dd of=`, `sort -o`, `find -fprint/-fprint0/-fprintf/-fls`
and the starting points of `find -delete`, `curl -o`/`--output`,
`tar -C`/`--directory`, `unzip -d`, `patch`, `git checkout -- <file>`,
`git restore <file>`, `git diff/log/show --output` and `git format-patch -o`,
with `--opt=value` and attached short options (`-tDIR`, `-oFILE`) parsed. A
write target with an unquoted glob character (`cp x .gi?/hooks/`) is refused:
the shell expands it at run time to paths the guard cannot see. Paths are
casefolded on darwin, resolved through `cd` / `pushd` state, and a directory
that is a prefix of a protected path is protected too. A `cd` into a glob is
refused, and with `CDPATH` in the environment a relative `cd` that does not
start with `./` or `../` leaves the directory unresolved (a `CDPATH=`
assignment is refused outright). Every Bash write target,
and every Write, Edit or NotebookEdit `file_path`, goes through the same check.

**Fail-closed.** An attended guard allows a call it cannot judge, so a guard bug
cannot break legitimate work. Under the variable the same case is refused: an
unparseable payload, a missing `agent-guard.py`, or no `python3`.

**No package allowlist.** A restore from the committed lockfile is allowed
(`npm ci`, `pod install`, `pip install -r` while the manifest is unchanged).
Adding a package is refused without exception. An allowlist the run can reach
would be edited by the same text that asks for the package, and one it cannot
reach is a person's decision made in advance, which is what the refusal already
asks for. A task that needs a new dependency ends with the need recorded in the
PR body under follow-ups.

**The network allowlist is data.** `network.staticHosts` in
`schemas/unattended-policy.json` (GitHub's hosts, localhost), plus the hosts
named by `network.prefsHostKeys` in `prefs.global.hosts` (Jira, Confluence,
Bitbucket, Fortify, Graylog, Jenkins), read at hook time. A subdomain of an
allowed host is allowed; `corpDomain` is not used, because it is an email domain
and would allow every host under it. The host is parsed with
`urllib.parse.urlsplit`, so `http://evil.com#@github.com/` resolves to `evil.com`
and is refused. The same allowlist governs `WebFetch`, the toolkit's
`web_goto` / `web_crawl` / `web_map` / `web_scrape` (any URL argument), each step of `agent_run_steps`,
and a web URL given to `ios_open_url` / `android_open_url`; `web_eval` is
refused outright because inline JavaScript can reach any host. Every entry of a
`paths` list (`web_upload`) is held to the same path rules as a single path argument. Reads only: a request body or
upload - `-d`/`--data*`, `-F`/`--form`, `-T`/`--upload-file`, and their attached
and clustered short forms (`-d@file`, `-sd`, `-Tfile`) - is **refused outright**,
whatever the host, rather than allowed to an allowlisted one; a run has nothing
to POST that is not the runner's to send.

**Toolkit path arguments.** Which argument of a toolkit tool is a write target
and which is a read comes from the toolkit's own declaration, not from the
argument's name: `$HOME/.claude/schemas/toolkit-path-params.json`, generated from the
toolkit's `tool-manifest.json` by `gen-toolkit-path-params.mjs` (`npm test` runs
it with `--check`). A declared write is held to the protected paths above and to
every location a read may not reach. A declared read is refused when it is
`$HOME` or a directory above it, when it lies under a host root (`~/.claude`,
`~/.copilot`, `~/.codex`) or a protected home file or directory, or when it
points into a credential store (`~/.ssh`, `~/.aws`, `~/.gnupg`,
`~/Library/Keychains`, `~/.netrc`, `~/.git-credentials`,
`~/.docker/config.json`, `~/.kube`, `~/.config/gcloud`, `~/.azure`,
`~/.config/op`, `~/.terraform.d`, `~/.pypirc`, `~/Library/Cookies`,
`~/.android/adbkey`, the App Store Connect key folders `~/.appstoreconnect`,
`~/.private_keys` and `~/private_keys`, `~/.config/configstore`,
`~/.config/github-copilot`, `~/.local/share/gh`, `~/.zsh_history`,
`~/.bash_history`, `~/.gradle/gradle.properties`, `~/.gradle` as a whole, and
the Chrome, Firefox, Safari, Microsoft Edge, BraveSoftware, Arc, VS Code
(`Code`) and Vercel CLI (`com.vercel.cli`) folders under
`~/Library/Application Support`), matched per path component and casefolded on
macOS. The same list is a `Read()` deny and a sandbox `denyRead` entry in the
unattended profile, with no exception. `~/.gradle` (its `gradle.properties`,
init scripts, `caches/*/cc-keystore`, the key store that decrypts a
configuration cache, and `daemon`) and `~/.local/share/gh` are read by Gradle
and `gh` on their own at every start with the operator's defaults, so both
tools are pointed elsewhere (`lib/unattended-tool-homes.mjs`).

- `GRADLE_USER_HOME` is a fresh directory per run,
  `~/.multi-agent-unattended/run-<session id>-<random>/gradle-home`. Before each
  launch the runner and the office build the run directory, with a
  `gradle.properties` holding only `org.gradle.daemon=false`, inside
  `~/.multi-agent-unattended-homes/.staging`, where no run writes, and move it
  with one `rename(2)` to an unguessable name under `~/.multi-agent-unattended`.
  They then write the settings file the run is launched with,
  `~/.multi-agent-unattended-homes/launch/<session id>.json`, through the real
  path of the tool-home parent with `O_CREAT|O_EXCL|O_NOFOLLOW`: the installed
  profile with that one run directory added to `allowWrite` and the run's
  Gradle variables added to `env`. They pass it with `claude --settings` in
  place of the profile itself, and set the same variables in the child's
  environment: `GRADLE_USER_HOME` and `GRADLE_OPTS=-Dorg.gradle.daemon=false`.
  No Gradle variable of the launcher's own reaches the run (a `GRADLE_OPTS`
  could name another user home), and no dependency cache is named. The launch
  precondition refuses a launch environment without `MULTI_AGENT_SESSION_ID`,
  and accepts the child's `GRADLE_USER_HOME` only when it is exactly the home
  the launcher made for that session, its `GRADLE_OPTS` only when it is the
  launcher's, no other `GRADLE_` variable, argv's one `--session-id` only
  when it is `MULTI_AGENT_SESSION_ID`, and its one `--settings` file only when
  it is that session's launch file, a plain file holding the shipped posture
  with exactly that run directory added to `allowWrite` and exactly the launch
  environment's Gradle variables added to `env`. A missing value, `~/.gradle`,
  another run's home, file or session id, the shared profile itself, or any
  wider write refuses the launch.

  The per-run write scope. The shared profile does not let a run write
  `~/.multi-agent-unattended`: only its four shared directories are
  `allowWrite` entries (`logs` for `LOGS_ROOT` and the office's pending
  request files, `pr-requests` for the PR request, `repo-profiles` for a
  repository profile `~/.claude` refused, `toolkit-index` for the toolkit's
  index and its usage log). The run root is in no `denyWrite` entry either:
  Claude Code's sandbox gives `denyWrite` precedence over `allowWrite` (its
  settings schema says so, "Paths denied for writing (takes precedence over
  allowWrite)", and Seatbelt applies the later `deny file-write*` subpath rule
  over an earlier allow below it), so a deny on the root would also deny the
  run directory each launch adds. Being in no `allowWrite` entry is what keeps
  it unwritable. A run therefore writes its own run directory, the shared
  directories and nothing else under the root: not another run's directory or
  Gradle home, not the root's mode, not a new entry or a link in place of the
  root, and not a rename of its own run directory to another name, since the
  destination is outside its scope. `test/unattended-tool-homes.test.mjs`
  checks this under `sandbox-exec` with the rules built from a launch file, and
  checks that a deny on the root would override the per-run allow. The guard
  also refuses a write under a run directory named for another session
  (`MULTI_AGENT_SESSION_ID`), for the file tools and the toolkit, which the
  sandbox does not cover. The launcher owns the root: before every launch and
  every removal it sets the root's mode back to 0700 through an `O_NOFOLLOW`
  descriptor, moves a link or a file found in its place into the quarantine
  with one rename and makes the directory again, and makes the shared
  directories plain directories again.

  What does not persist: the Gradle home and its launch file. The runner
  removes both once supervision sees the session end, or at once when a launch
  starts nothing; a launch that timed out keeps them, since the session may be
  running. The office removes a job's once the session list it polls for the
  floor shows the job's session ended: it moves the run directory into the
  quarantine with one rename and drops the launch file, inside the request,
  and the next quarantine drain (the runner, a launch, the installer)
  deletes it. A session that stopped to ask, or a run
  the runner lost track of, keeps them until a later launch's sweep removes
  the run directory.

  When a session counts as ended, for the sweep, the office and the check that
  no run is at work (`lib/claude-sessions.mjs`): `claude agents --json`
  gives a row the status `busy`, `shell`, `idle` or `waiting`, none of them
  terminal, so a listed row is alive unless its `pid` names no process
  (`kill(pid, 0)` answers `ESRCH`). A row with no `pid` and no status, a
  parked session, is alive. A session missing from a readable, non-empty list
  has ended once its run directory (or office record) is a day old; before
  that it is kept, since a list can miss a session that is starting. A list
  that cannot be read, an empty list, or one that holds a row without a string
  `sessionId` shows nothing ended. The sweep never removes the run directory
  of a session listed with a live `pid`, whatever its age; any other run
  directory goes once it is 7 days old, whatever the list says. A run can set
  its own directory's times, so the age bound alone never removes a run whose
  process is listed. The launch file can go while its session is alive:
  Claude Code 2.1.317 reads the `--settings` file once at start and keeps its
  content (`getFlagSettingsFilePinnedContent` in the binary), so it never
  reads that path again; the sweep keeps the file while the run directory is
  kept all the same.

  The run can write everything in its own home, `gradle.properties` and init
  scripts included, and none of it reaches a later run.

  What an unattended run costs: no Gradle cache is shared with it, so each run
  downloads its dependencies, the Gradle distribution its wrapper names and
  any JDK toolchain it needs, and builds cold. A wrapper
  `distributionSha256Sum` in `gradle-wrapper.properties` is recommended, so
  the wrapper checks the distribution it downloads. `~/.gradle` stays wholly
  in `denyRead` and `denyWrite`.

  Disk use: a run directory holds what that run downloaded (its dependencies,
  a Gradle distribution of a few hundred megabytes, a JDK toolchain more). It
  is removed when the run ends, by the runner or the office as above, and
  otherwise by a later launch's sweep under the rule above; the run directory
  of a parked session, or of a session whose list row keeps a live `pid`,
  stays until that changes or, for a parked one, for up to 7 days, so many of
  them can hold several gigabytes. What a removal's budget leaves in the
  quarantine is drained at install, after each run of the autopilot runner and
  before each launch.

- `XDG_DATA_HOME` is `~/.multi-agent-unattended-homes/xdg-data`, set in the
  profile and rebuilt before every launch: a fresh directory is made in the
  parent, filled with one link per entry of the operator's data directory
  except `gh`, renamed into place, and the previous one is removed. What
  persists of it is those links, made again each launch; nothing a run could
  put there reaches the next run. The data directory mirrored is the one
  `install` recorded in `~/.claude/multi-agent-unattended.tool-homes.json` (the
  operator's `XDG_DATA_HOME`, else `~/.local/share`, by its real path), never a
  directory inside the parent. `install` rewrites that record every time it
  runs, through a temporary file and a rename, never through a link.

`~/.multi-agent-unattended-homes` holds the data directory, the launch
settings files, the staging directory and the quarantine. It is in `denyWrite` as a whole and its entries are an `Edit`
deny, so no run writes anything under it. The runner's `prepareLaunch`, the
office's `startJob` and `install` work there and in the run root outside the
sandbox, and never follow a link:

- a path below the parent is checked component by component with `lstat`; a
  link is unlinked, never followed; permissions are set through a descriptor
  opened with `O_NOFOLLOW`, or with `lchmod(2)` (`chmod -h`) for a directory
  its owner cannot open. If the parent itself is a link or a file, it is
  unlinked and made again.
- whatever is discarded (a run directory, a retired data directory, a
  leftover of a refresh that stopped part way) is first moved into
  `<parent>/.quarantine` with one `rename(2)`, which moves a link itself and
  never its target, and deleted there. Nothing is ever deleted where it stands,
  since that place may be one a run can still write. When the rename fails
  (moving a directory to another parent rewrites its `..` entry, so a run
  directory a run set to mode 0500 or 0000 cannot be moved), the directory is
  checked to be the one `lstat` saw (device and inode), given mode 0700, and
  the rename is tried once more; if it still fails it is logged and left for a
  later sweep, which follows the same rule.
- in the quarantine each entry is removed by acting on that entry alone, with
  no check of its parents, since nothing but the launcher writes there. A
  directory is opened through an `O_NOFOLLOW` descriptor that must be the inode
  `lstat` saw, then read one entry at a time with `opendir`, each entry removed
  as it is read: no directory listing is ever held whole, so a directory of
  any width is removed in bounded memory. One nested deeper than 64 levels or
  512 characters of path is moved up to the quarantine's top first and removed
  after, so a tree deeper than `PATH_MAX` is removed in pieces with a bounded
  number of directories open.
- deletion has a budget, checked before every entry: 2 seconds and 200000
  entries before a launch, 60 seconds and 2000000 entries after a run or at
  install. What the budget leaves stays in quarantine for a later pass; an
  entry that cannot be removed is logged. Neither ever refuses a launch or an
  install.
- the sweep at each launch also moves into the quarantine any entry of the run
  root that is neither a run directory, a shared directory nor an older
  layout's tool home, once it is 10 minutes old (only the launcher creates
  entries there, so such an entry is a run directory moved to another name),
  a staging leftover older than an hour, and a launch settings file whose run
  directory is gone.
- the new data directory is checked once it is in place: it is accepted only
  when it holds exactly the expected links, and only when it is the directory
  this refresh made (same device and inode as the descriptor opened right
  after `mkdtemp`) or a complete one a concurrent refresh placed. Anything else
  is set aside and rebuilt.

Seatbelt applies a `denyWrite` entry as a subpath rule on the path as written:
creating, replacing, renaming or removing the directory or any entry in it is
denied, and unlinking a link entry is denied too, since `unlink` acts on the
link itself. Seatbelt judges a write through a link by the link's resolved
target, so a tool writing its data through `xdg-data/mise` is judged as a write
to `~/.local/share/mise`; the deny on the parent neither adds nor removes
anything there, and that target is outside `allowWrite` as before. (Checked
with `sandbox-exec` on macOS 26: a subpath `file-write*` deny refused `rm`,
`mkdir`, `ln -s` and `mv` on the directory and its entries and let a write
through a link entry reach the target.)

What the operator gives up unattended: a build that needs a repository or
signing credential from `~/.gradle/gradle.properties`, or an init script from
`~/.gradle`, fails; the operator's own `GRADLE_OPTS` does not reach a run (the
launcher replaces it with `-Dorg.gradle.daemon=false`, since it could name
another user home), so JVM memory settings and system properties set there
have to be in the repository's `gradle.properties`; every run downloads its
dependencies, Gradle distribution and JDK toolchains; no Gradle daemon is
reused between builds; and `gh` extensions are not available. A client that launches from the
launch contract alone passes the profile itself and sets no
`GRADLE_USER_HOME`, so Gradle would use `~/.gradle`, which the sandbox denies.

Earlier installs kept tool homes elsewhere, and the next `install` removes
them: `~/.multi-agent-unattended/gradle-home`, `xdg-data` and their
leftovers, the per-repository Gradle homes under
`~/.multi-agent-unattended-homes/gradle` with their `.work` directory, the
clone of the operator's Gradle dependency cache at
`~/.multi-agent-unattended-homes/gradle-ro` (it held whatever that cache
held, so it goes even though nothing reads it), and the launch settings files
under `~/.claude/multi-agent-unattended.launch` (found through the real path
of `~/.claude`). A launch that finds one of these under the parent removes
it too, the same way, and never waits for it. `env.GRADLE_USER_HOME` is
removed from the profile, and so is every other `GRADLE_` variable,
`GRADLE_RO_DEP_CACHE` included, `XDG_DATA_HOME` moves under the new parent, the old
`denyWrite` entries and `Edit` deny rules are replaced by the ones for the
whole parent, and `allowWrite` loses `~/.multi-agent-unattended` and gains its
four shared directories. Nothing is removed while an unattended run may be at
work: a session in the autopilot queue's running entries or claim, or an
office job of the last day, that the session list does not show ended by the
rule above; an empty list while such a session is named, or a list with a
row without a string `sessionId`, counts as one at work. The office keeps a job's record for
a day, so a job older than that is not named; this window only decides when
an older layout's tool homes are removed, which no run launched by this
release uses, while the run directories themselves follow the sweep rule above.
The removal is recorded as pending in
`~/.claude/multi-agent-unattended.tool-homes.json` before any of it starts, and
the record is cleared only once all of it is gone, so the next install or
launch finishes what one left. Entries are moved out of
`~/.multi-agent-unattended` relative to a working directory entered once and
checked to be the directory `lstat` saw (same device and inode), and checked
again before each move, so a link swapped in for it is never followed. The
`~/.gradle` and data directory entries the old layouts linked to stay as they
are. Until that install, the launch precondition refuses a profile that still
sets a Gradle home.

`install` replaces the profile's `allowWrite` with the shipped set, so a write
path an operator added there is dropped. There is no place to add one: the
launch precondition refuses a profile that writes anywhere else, and a
checkout's own settings that add a write path.

The OS sandbox is the layer that binds these reads. `check_bash` is a second,
best-effort text layer over it for the two files (`BASH_WATCHED_CREDENTIALS`),
so a run learns early that the read is not allowed. A command is refused when
an argument, a redirection operand (`< ~/.gradle/gradle.properties`) or the
value after `=` or `@` resolves, after `~`, `$HOME`, `$'...'` decoding, the
command's own variables, `$(pwd)` and `$(git rev-parse --show-toplevel)`, any
earlier `cd` and symlinks, into one of the two paths or into a directory above
one that is still below `$HOME` (`tar cf x ~/.gradle`), when a glob reaches
one, or when the path is spelled out from `~`, `$HOME` or the home directory
anywhere in the command text, a quoted `-c` program included. A glob is
over-approximated before it is matched: a bracket expression (`[^x]`,
`[[:alpha:]]`) counts as any one character, a path component holding zsh's
`^` or `#` as any name, and `*` and `**` cross `/`. A shell option that widens
globs (`setopt globdots`, `extendedglob`, `kshglob`, `globstarshort`,
`shopt -s dotglob`, `globstar`, `extglob`, a shell started with `-o` / `-O`
one of them, `emulate -o` one of them, or `emulate ksh` / `emulate sh`, which
switch to that shell's glob rules) is refused; `nullglob` is not. A word the guard cannot expand (an
unknown variable, a command substitution) is refused when its literal text, as
written or with the unresolved parts dropped, holds `.gradle/gradle.properties`
or `share/gh` followed by `/` or the end, when an unresolved part is followed
by `/gh`, when the literal text before the first unresolved part starts a
watched file's full path (`~/.gra$(x)le`, `~/$X`), or when the source text of a
substitution in it (or of a variable this command set from one) reaches
`$HOME` or above, or searches the whole disk (`mdfind`, `locate`), and
mentions a watched name or a glob (`cat $(find ~ -name 'gradle.prop*')`). A
leaf after an unknown directory is not judged: `"$OUT/gradle.properties"` and
`"$PROJECT_DIR/.gradle"` are ordinary project paths. A recursive read rooted at
`$HOME` or a directory above it is refused (`grep -r`, `rg`, `rga`, `ag`,
`ack`, `tar c`, `cp -R`, `rsync -r`, `zip -r`, `pax -w`, `7z a`, `find -exec`,
`fd -x`, also from a relative root after `cd ~`), and so is a path list from
`find` or `fd` rooted there fed to `cpio -o`, `pax -w`, `tar -T -` or `zip -@`.
A search pattern and the values of the tool's own options (`-e`, `-f`, `-g`,
`-t`, `--include`, `-m`, `-A` and the rest, per tool) are not roots. A root
the guard cannot resolve is refused only when it could reach `$HOME` or above:
it starts with `~`, `$HOME`, `/` or `..`, its literal head is `$HOME` or above,
it is a bare substitution other than `$(pwd)` or
`$(git rev-parse --show-toplevel)`, it is a bare variable this command set to a
value the guard cannot resolve, or its literal tail climbs (`..`) or names a
directory at or above `$HOME`. A bare variable the command did not set
(`grep -rn TODO "$REPO_ROOT"`) is unset in the environment the guard sees, and
is not judged: what the operator's own shell setup puts there is bounded by
the sandbox. A local web server rooted at `$HOME` or above
(`python3 -m http.server`, `php -S`, `ruby -run -e httpd`, `http-server`,
`serve`, `live-server`) is refused. Setting `GRADLE_USER_HOME` or
`XDG_DATA_HOME` on a command is refused, like every other variable that moves
a tool's configuration. `gradle -c ~/.gradle/gradle.properties` and
`gh api -F q=@~/.local/share/gh/x` are refused like any other read, and the
Gradle `properties` task is refused unattended, because it prints every
project property. `ls` and `du` may name a directory above an entry
(`ls ~/.local`), the `-m` message of a git commit, tag, merge, stash or notes
command is text, and a heredoc body that `cat` or `tee` writes to a file is
documentation; a body that feeds an interpreter, a pipe, `xargs` or standard
output is matched. The limits of the text layer: it does not follow a `**`
through a link deeper than one level, a path a program assembles at run time
(a script, a build file, a plugin), or the shell's own expansion of a value it
cannot see; the sandbox binds each of those. Some
declared reads take either a path or the data itself (`findings`, `tokens`,
`inventory`): an object or a nested list there is inline data, and only the
path-looking strings inside it are checked. A declared write target that is not
a string is refused. An
undeclared argument whose name reads as a path
(`path`, `output_dir`, `file_path` and the like), on a declared tool or on one
the declaration does not list, is checked as a write, so a toolkit newer than
the declaration fails closed. For a tool the declaration does not list (or
every tool, when the declaration is missing or unreadable), every string in its
arguments, nested list and object values included, that looks like a path (it
starts with `/`, `~` or `$HOME`, or climbs with `..`) is also checked as a read,
so an unknown tool cannot open the credential stores. On a declared tool the
same read check covers every string outside its declared parameters, unless the
key that names the string (the argument itself, or the nearest enclosing key
for a nested value) is text-like: `text`, `value`, `query`, `selector`,
`label`, `title`, `message`, `body`, `content`, `pattern`, `expression`,
`script`, `url`, `name`, `placeholder`, `description`, `comment`, `search`,
`keyword`, `keywords`, `term`, `input`, `key`, `string` or `note`, in any letter
case (`TOOLKIT_TEXT_KEYS`). Typing `~` or `/` into a field stays allowed, and a
path argument the declaration missed (a certificate, a render, an image) is
still held to the credential stores. No declared read or write parameter may
carry a text-like name; a property test fails when one does. `ios_xcodebuild`
appends `extra_args` to its command line. The guard refuses it when it holds a
glob character (`*`, `?`, `[`, `]`), a `~`, a quote or a shell character
anywhere, because a toolkit that runs the line through a shell would rewrite
such a token after the guard looked (`-xcconfi?` becomes `-xcconfig` once a file
of that name exists). What is left is split on whitespace, as the toolkit
splits it (a list is taken as given), and the value after each path flag is
checked: `-project`, `-workspace`, `-xctestrun` and `-exportOptionsPlist` as
reads, `-derivedDataPath`, `-resultBundlePath`, `-archivePath`, `-exportPath`,
`-clonedSourcePackagesDirPath`, `-packageCachePath`, `-resultStreamPath`,
`-testProductsPath` and `-test-enumeration-output-path` as writes, the last one
inside the working directory only. Any other flag ending in `Path` or `Dir`, a
path flag with no value, `-xcconfig` and `-toolchain` are refused, and so is an
`=` anywhere (a build-setting override such as `SYMROOT=...`, an IDE default
such as `-IDECustomDerivedDataLocation=...`, an inline flag value), a list
entry padded with space or tab, and a flag entry holding a space
(`-only-testing:` and `-skip-testing:` carry their value inline and may).
Two list-form values are plain text and pass: the value after `-destination`
when it is `key=value` pairs (`platform`, `name`, `id`, `OS`, `arch`,
`variant`) joined by commas, optionally after `generic/`, with no path or
shell character, and the parentheses of a test identifier in
`-only-testing:` / `-skip-testing:` (`AppTests/FooTests/testBar()`). The
string form keeps refusing both, since the toolkit refuses `(` and `=` in a
string. Every other flag must be in an allowlist
(`XCODEBUILD_UNATTENDED_FLAGS`: output, test selection, enumeration and
repetition, destinations, sanitizers, coverage, package resolution from the
resolved file, scheme, target, configuration, arch, an `-sdk` name,
`-skipUnavailableActions` and the like), and a bare word must be an xcodebuild
action. The allowlist fails closed where a denylist
would not: xcodebuild gains flags every release, and the ones left out sign
or register devices (`-allowProvisioningUpdates`,
`-allowProvisioningDeviceRegistration`, `-authenticationKey*`), skip plugin or
macro validation, pick a package registry or SCM, or point at another
toolchain root or SDK path. The toolkit refuses the same named flags in
unattended mode, so a toolkit run without the guard still holds them back.
Under `--probe`, doctor warns when
the installed toolkit's version differs from the declaration's `toolkitVersion`.

If the allowlist must be pinned at launch instead of re-read from prefs on every
call, the runner may set `MA_UNATTENDED_HOSTS` (a comma-separated snapshot); when
it is absent the guard reads `prefs.global.hosts` as before, and protecting
`~/.claude/multi-agent-preferences.json` from the run is what keeps that read
trustworthy.

**Tools that send text outside.** The guard refuses the `research_*` tools
(`research_search`, `research_ask`, `research_community`) outright, and
`llm_delegate` unless `route-on --free` is on (`modelRouting.enabled` and
`modelRouting.free.enabled`, read at call time). Two more are refused by the
toolkit itself, not by the guard:

- `social_read` refuses itself when `MULTI_AGENT_UNATTENDED=1`: the first read of
  each channel needs the user's confirmation, which an unattended run cannot
  give. It is also off unless `MCP_TOOLKIT_CAPS` lists `social`.
- `media_transcript` is off unless `MCP_TOOLKIT_CAPS` lists `media-remote`. The
  guard checks its `url` like any web URL, so in an unattended run the video
  host must be on the network allowlist.
- `research_community` is off unless `MCP_TOOLKIT_CAPS` lists
  `research-community`.

An autonomous research pass is narrower still: the runner launches it with
`research_community`, `social_read`, `media_transcript`, `llm_delegate` (and the
other `research_*` tools and `WebSearch`) denied, because tracker text is
untrusted and often confidential.

## User guard rules

`prefs.global.guardRules` lets the user add rules to the guard, attended and
unattended (`scripts/_user_guard_rules.py`, schema in `prefs.schema.json`):

```json
{ "id": "no-prod-db", "tool": "Bash", "match": "psql\\s+.*prod",
  "action": "block", "reason": "production database" }
```

- **What is matched.** `match` is a Python regex searched in the command for
  `Bash`, in the target path for `Edit` (which also covers `NotebookEdit`),
  `Write` and `Read` (as given, made absolute, and real-pathed), and in the tool
  input as compact sorted JSON for a toolkit glob such as
  `mcp__multi-agent__web_*`. Each `agent_run_steps` step is judged as a
  call of its own tool. Text is also matched after the confusables fold the
  attribution rule uses. An `Edit` rule also matches `MultiEdit` on a host that
  still has that tool, but the default hooks register the guard on
  `Edit|Write|NotebookEdit` only, so such a host needs a `MultiEdit` entry too.
- **Rules only add.** The built-in verdict is reached first: a built-in block
  or deny is final, a rule `block` outranks a built-in ask, and no rule turns a
  refusal or an ask into an allow. `warn` lets the call through with a note
  (`systemMessage` and `additionalContext`); `ask` raises the same confirmation
  as the built-in asks, one confirmation covering both; `block` refuses. A
  `warn` or `ask` message that meets a built-in ask is added to that prompt's
  reason.
- **Confirming without a prompt.** In `bypassPermissions` and `dontAsk` no
  prompt can be raised, so an ask is refused with the command that records the
  user's yes: `confirm-command.mjs --command '<key>'`, valid ten minutes for one
  use. For `Bash` the key is the exact command. For any other tool it is the
  tool name, the target path when there is one, and `sha256:` of the tool input
  as sorted compact JSON, which the refusal prints in full; the yes covers that
  exact call, and a call with any other input is asked again.
- **Unattended, ask is block.** Nobody is there to answer the prompt.
- **Bounded regex.** A pattern is at most 512 characters. A pattern with a
  back-reference, or one that does not compile, is invalid. Every other
  pattern runs under a 1-second wall-clock budget: `re` has no timeout, but it
  checks for pending signals while it backtracks, so a timer ends a runaway
  match in place, and a `block` or `ask` rule that runs out of time fails
  closed (below). Doctor's `guard-rules` check names a valid rule with a slow
  shape: a variable quantifier (`?`, `*`, `+`, `{m,n}`) inside a group
  repeated more than 16 times whose repetitions can split the same text more
  than one way (`(a+)+`, `(\w+\s?)*`, `(?:a?){28}`), or three or more adjacent
  open-ended quantifiers whose characters overlap (`a*a*a*b`, `\w+\d+\w+`).
  A bounded repeat with a small maximum (`(\d{1,3}\.){3}`) and a group whose
  quantified items cannot match each other's characters
  (`git\s+push(\s+\S+)*\s+--force`, `(\s+-\w+)*\s+prod`) are not slow shapes.
  The shape check cannot catch every slow pattern (`(a|aa)+$` passes it); the
  budget covers those too. Rules run `block`
  first, then `ask`, then `warn`, and each gets an even share of the budget
  still left, so a slow rule cannot use up the time of the rules after it;
  a timeout message names the share the rule had. This costs no process start
  per call, which a child interpreter would. The guard's own 5-second deadline
  shares the timer and is re-armed with what is left, so it still fires on
  time. Text past 65,536 characters is matched on its head and tail (32,768
  characters each); when that finds nothing, the input counts as past the cap.
- **When a rule cannot be applied.** An invalid rule is ignored and doctor's
  `guard-rules` check reports it (health shows doctor's warnings); a repeated
  id keeps the first rule, and the warning names both positions. An invalid
  `block` rule whose tool is readable refuses every call of that tool
  unattended: the rule exists to stop something, and an unwatched run that
  silently drops it is the failure it was written against. A rule gives no
  answer when it runs out of its share of the time, when the budget is spent
  before its turn (reported as not checked), or when the input is past the
  cap.
  Then unattended an `ask` or `block` rule refuses; attended an `ask` or
  `block` rule asks, so input the agent controls cannot switch a refusing rule
  off, and a `warn` rule leaves a note.
- **A file that cannot be read.** A preferences file that does not parse, or a
  `guardRules` that is not a list, applies no rule. Unattended it refuses every
  call a rule could govern (`Bash`, the file tools and toolkit tools) when the
  file names `guardRules`, or cannot be read at all: the rules it holds cannot
  be read, and nobody is watching. A broken file that never names
  `guardRules` holds no rules, so nothing is refused for it. Attended each such
  call proceeds with a note that the guard rules could not be read. Doctor's
  `guard-rules` check warns, and `prefs-valid` blocks on a file that does not
  parse.
- **No echo.** A message names the rule id and its reason, never the matched
  text or the pattern; doctor prints neither.
- **Where it runs.** Only where `agent-guard.sh` is registered: `Bash`, file
  edits, `WebFetch` and toolkit tools under the default hooks. A `Read` rule
  needs a `PreToolUse` `Read` entry for the guard; doctor names the gap. Even
  then a `Read` rule does not stop `cat`, `head` or any other shell read of the
  same file: that needs a matching `Bash` rule.

## Phase 4: a PR request instead of a push

Under the variable Phase 4 commits as usual (the commit hook still needs a
passing gate ledger) and then writes a request instead of pushing:

```bash
node "$HOME/.claude/scripts/pr-request.mjs" write \
  --branch "$BRANCH" --base "$BASE_BRANCH" --repo "$OWNER/$REPO" \
  --title "$PR_TITLE" --body-file "$WORKTREE/.pipeline/pr-body.md"
```

The body is the one Step 3 builds for a PR (the `channels/pr.md` section set,
humanizer pass, no auto-close keywords). The script writes
`<run root>/pr-requests/<MULTI_AGENT_SESSION_ID>.json` (the run root is
`~/.multi-agent-unattended`, whose `pr-requests` directory the sandbox lets the
run write; `lib/pr-request-location.mjs`), mode 0600, validated against
`schemas/pr-request.schema.json`: `{branch, base, title, body, draft: true, repo}`.
It refuses a protected branch, a branch equal to its base and an auto-close
keyword (exit 1), and a body or title carrying a token shape (exit 7, rule and
line reported, never the value). Nothing is written on a refusal. The guard
allows that one file and no other under the runner's root; the run never
hand-writes it.

Skipped under the variable, because each one writes outward: push (standard step
7), the PR prompt and creation (step 8), the Step 2.9 evidence push (`host:
none`, reason `unattended`), the issue body update (step 10), and the Jira
comment. Worktree finalize exits 3 on the unpushed branch and keeps the
worktree, which is what the runner needs to push from.

The runner publishes it after the session ends ("The publish step" below).
Attended runs never call `pr-request.mjs`.

## The runner's launch

Before every launch - the dev run, a research pass, a resume - the runner:

- refuses to launch unless `agent-guard.sh` is registered as a PreToolUse hook
  on `Bash`, `Edit|Write|NotebookEdit` and `WebFetch|mcp__multi-agent__.*`
  in the user settings (`CLAUDE_CONFIG_DIR` or `~/.claude`) or macOS managed
  settings, read the way `install/claude.mjs` writes it: a matcher is a regex
  over the tool name (the legacy `Bash(git push:*)` covers nothing, and the web
  entry has to cover `agent_run_steps` and both `open_url` tools), the hook
  command is exactly the installer's `bash "$HOME/.claude/scripts/agent-guard.sh"`
  (quoted or not, or the same path with `$HOME` expanded) and that script exists. The
  checkout's `.claude/` settings never count toward the registration - the repo
  under work is not where the guard may come from - but `disableAllHooks` in
  any of these files, the checkout's included, turns every hook off. It also
  refuses when `~/.claude/multi-agent-unattended.settings.json`, the permission
  profile, is missing. A refused first launch is recorded as
  `blocked-guard-missing` (nothing ran, no attempt counted) and the item stays
  queued; a refused research or resume launch leaves the run parked.
  `guardRegistration` in the runner.
- launches the child with `--settings
  ~/.multi-agent-unattended-homes/launch/<session id>.json`, the installed
  profile with that run's directory added to `allowWrite` and its Gradle
  variables to `env` (`launchArgv`, `researchArgv`, `resumeArgv`;
  `schemas/launch.json` carries the profile itself as `{settingsFile}` for a
  client that launches itself, and the Gradle variables are added per launch).
- creates `<autopilot root>/pr-requests/` at mode 0700 and removes any request,
  verdict or summary left under the new session's id (`prepareLaunch`).
- passes `MULTI_AGENT_UNATTENDED=1`, `MULTI_AGENT_SESSION_ID`,
  `MCP_TOOLKIT_URL_POLICY=strict` (forced, never loosened by the runner's own
  environment) and `MCP_TOOLKIT_INDEX_DENY` (the profile's list plus any entries
  the runner's environment adds), values read from
  `schemas/unattended-profile.json` (`launchEnv`).

## The publish step

`scripts/autopilot-publish.mjs`, called by the runner when a session ended
(gone, finished, or idle waiting) with `currentPhase >= 4` and a request at
`prRequestPath(sessionId)`; again from the next tick's recovery when supervision
stopped before the session did, and when a parked session ends. Everything the
run wrote is a claim to check:

Files the run could have written are read only as bounded regular files
(`lib/regular-file.mjs`): the path is resolved, opened non-blocking without
following a final symlink, and the open descriptor must be a regular file under
a size cap. A FIFO, a device or an oversized file is refused under its gate
without being read, so nothing the run leaves behind can block the runner's
thread. The runner reads run states and trackers the same way.

| Step | Refused as (`verificationFailed.gate`) |
|---|---|
| the run's state is a regular JSON file (16 MB cap); a refused state cannot be parked, so only the verdict records it | `publish/state` |
| the request is read once (1 MB cap) and `validatePrRequest` from `pr-request.mjs` runs in process on those bytes: schema, protected branch, auto-close keywords, and the outbound gate | `publish/pr-request` |
| `repo` must equal the queue item's `nameWithOwner` from the runner's own config; the request never chooses the repository | `publish/repo` |
| `host` from the config: `github` (default) or `bitbucket`; anything else | `publish/host` |
| the request's base must be the configured `baseBranch`, else the remote's default branch (`ls-remote --symref <url> HEAD`), or a branch listed in `allowedBases`; a remote that names no default with no `baseBranch` set is refused | `publish/base` |
| the worktree shares its git common dir with the configured `localPath`, and has no uncommitted change to a tracked file, so the tree verified below is the tree pushed | `publish/worktree` |
| the branch is not main, master, develop, `release/*`, the configured base or an `allowedBases` entry; it equals the `branch` the run recorded in its state at Phase 0; it exists and is the worktree's HEAD | `publish/branch` |
| the push URL is the config's `pushUrl`, else `gh repo view <repo> --json url`; never `.git/config`. A URL with credentials in it is refused | `publish/remote` |
| the branch is fetched from the worktree into a fresh staging repository, and the base from the push URL; the branch must have commits not on the base | `publish/base`, `publish/branch` |
| a branch already on the remote must have been pushed by this session (its earlier verdict says `pushed: true`) and its tip must be an ancestor of the run's HEAD | `publish/remote-branch` |
| the gate ledger passes for every commit's parent in the range. The ledger is written by the run, so this is a consistency check, not evidence | `publish/gate-ledger` |
| the ledger's latest `test-summary` entry names a test log (`detail.source`), a regular file of at most 64 MB inside the worktree or the run's directory; no log, or failed or zero executed, is a failure | `publish/test-log`, `publish/test-summary` |
| `pre-commit-check.sh --scan-range <merge-base>..<head>`: the commit hook's detectors over every commit in the range on its own, read from the commit objects, so a secret added in one commit and removed in a later one is still found in the history that would be pushed | `publish/secret-scan` |
| the outbound gate on the title and body | `publish/outbound-gate` |
| independent verification, below | `publish/verify-stack`, `publish/verify-scripts`, `publish/verify-build`, `publish/verify-test`, `publish/verify-credentials` |

### Independent verification

The gate ledger is agent-writable: a run can append `not-applicable` for every
required gate and `gate-ledger.mjs check` passes. So the publish step does not
take the run's word that it built and tested. It resolves the worktree's stack
with `_stack-adapter.mjs` and renders the adapter's `build` and `test` commands
from the **base commit's** manifests (`package.json`, the lock files and the
adapter's `when` files, copied out of the merge-base into a private directory).
The run does not choose how it is verified: a range that changes a command
line, a `build`/`test` package script or its `pre`/`post` hook, the declared
`packageManager`, or a repository executable a command starts with (`./gradlew`)
is refused as `publish/verify-scripts`. The commands then run in the worktree:
its own process group killed at `verify.timeoutSec` (default 1800) and again
when the command exits, output to a private file. Every process started for the
command carries a per-run marker variable, and any process still carrying it
after the group is killed is killed too, which covers a child that left the
group with `setsid` or a double fork (a process that also clears its
environment is not reached). The environment is the runner's with `MULTI_AGENT_UNATTENDED`,
`MULTI_AGENT_SESSION_ID`, `SSH_AUTH_SOCK`, the askpass helpers and every
variable whose name ends in `_TOKEN`, `_KEY`, `_SECRET`, `_PASSWORD` or
`_CREDENTIALS` removed (`verifyEnv`). A non-zero exit refuses; then
`evidence-gate.mjs --claim <kind> --status passed --stack <id>` judges the
fresh output, and for the test run `test-summary.mjs` counts it: a failure or
zero tests executed refuses. A stack the adapters cannot build and test (the
unknown adapter, a package with no `build` or `test` script) refuses as
`publish/verify-stack`, unless the config sets `verify.allowUnverifiedStack:
true`, which publishes with the ledger and the log check alone and records
`verified: false` in the verdict.

The operator's credential settings (`credentialConfig`) are read before the
build and tests run, and the push uses that snapshot. Afterwards
`~/.gitconfig`, `$XDG_CONFIG_HOME/git/config` (default `~/.config/git/config`)
and any `GIT_CONFIG_GLOBAL` file must hash to what they did before, and the
credential settings read again must equal the snapshot; otherwise nothing is
pushed (`publish/verify-credentials`).

Running the run's tests executes code the agent wrote, as the runner's user,
outside every hook. Scrubbing the environment keeps token-shaped variables out
of that process; it does not keep credentials away from it. That code can do
anything the runner's user can do without a prompt, including:

- read login-Keychain items whose access list lets the user's own tools read
  them silently, which includes items the pipeline's credential store wrote
  with `security` and anything readable by `security find-generic-password`
  under that user;
- run `gh auth token` and get the `gh` login's token, whether `gh` keeps it in
  the Keychain or in `~/.config/gh/hosts.yml`;
- read any file the user can read (SSH keys, `~/.git-credentials`, cloud CLI
  profiles) and open a network connection.

Nothing in the publish step can take those away from a process running as the
same user. The recommended setup is the separate, non-admin macOS user below,
whose Keychain and home hold only the narrow tokens the runner needs; without
it this step is the widest opening in the design.

### The push, from a staging repository

Nothing that touches the network runs in the worktree. The worktree's git
config is the run's to write, and a denylist of keys that could move or
observe a push (`url.*.insteadOf`, `http.proxy`, `http.curloptResolve`,
`http.sslVerify`, `http.extraHeader`, `credential.*`, `core.sshCommand`,
`include*`, and the ones git adds next) is never complete. So the publish step
makes a fresh bare repository in a private temp directory and runs every
network operation there, with `GIT_CONFIG_NOSYSTEM=1`, `GIT_CONFIG_GLOBAL`
pointing at a file it writes, `-c core.hooksPath=/dev/null` and
`GIT_TERMINAL_PROMPT=0`. That file holds only the operator's `credential.*`
settings, read from the system and global scopes outside any repository
(`credentialConfig`), so the helper the operator already uses still answers and
nothing else from any config reaches the push.

The branch enters the staging repository by a fetch the staging repository
runs, naming the worktree as a local path: its config, not the worktree's,
decides where that fetch reads. The base fetch and the remote-branch check run
from one staging repository, which is deleted before independent verification
starts; the push (`push --no-verify --porcelain <url>
refs/heads/<branch>:refs/heads/<branch>`, never forced, submodules not
recursed) runs from a second one, holding the credential snapshot taken
before verification, made after the run's build and tests have exited, once the worktree is confirmed to be still on the checked commit with
no tracked file changed (`publish/verify-test` otherwise). Code the tests
started could otherwise have written into the staging repository's own config
while it waited. The worktree is
still read locally - `rev-parse`, a status with `core.fsmonitor=false`, the
secret scan's `diff --no-ext-diff --no-textconv` - with the scrubbed
environment. On GitHub, `gh pr create --draft --repo <repo> --base <base>
--head <branch> --title ... --body-file <tmp>` runs from a private temp
directory, not the worktree; the URL goes into `state.pr` through the
write-state lock and the attempt is `pr-opened`. A push or `gh` failure is
`publish/push` or `publish/pr-create`.

Bitbucket has no draft pull request, and opening a ready one would ask for
review of work nobody has looked at. The runner pushes the verified branch,
writes `<session id>.summary.md` (0600) beside the request with the branch,
base, title and body, records `state.publish`, and the attempt is
`pushed-awaiting-pr`: terminal, because the work is delivered and a retry would
push it again, and not parked, because the step left is a person's and needs
neither the worktree nor the repo's queue slot.

An analysis item never reaches any of this. Its queue label says the work is a
document, the publish step returns before it reads a request, and the attempt is
`doc-delivered`: terminal, because the document is the delivery. Nothing is
pushed and no pull request is opened for it, by this path or any other - the
guard blocks both for every unattended run.

Every process the publish step starts runs with `MULTI_AGENT_UNATTENDED` and
`MULTI_AGENT_SESSION_ID` removed and `GIT_TERMINAL_PROMPT=0`. The runner is not
the agent: these are spawns, not tool calls, so no hook sees them, and the
variable would only make the scripts behave as if the agent had called them.
The runner's own credentials (the `gh` login, the git credential helper) are
used; none reaches argv or a log.

The verdict is written to `<session id>.verdict.json` (0600): the outcome, the
URL or the gate and reason, whether the branch was pushed, and what the
independent verification found, never a matched value. A refusal parks the run
through `gate-ledger.mjs park`; once the cause is fixed a person re-runs the one
step with `autopilot-publish.mjs --session <id> --state <agent-state.json>
--repo <owner/name>`.

## Phase 5 and channels

Under the variable Phase 5 skips external delivery (`deferred-to-runner`) and
`channels` posts nothing. It still runs with `--file-only`: the local report
file is written under `LOGS_ROOT`, which is already an `allowWrite` entry. It is
not a post, nothing outside the machine sees it, and no runner step depends on
it (`channels/report-file.md`). After the PR opened, and only then, the runner posts
what `~/.claude/autopilot/config.json` opts into
(`schemas/autopilot-config.schema.json`), once, through the existing scripts:

| Key | What the runner posts |
|---|---|
| `reportChannels: true` with `channels.mode: configured` | `jira`: one comment on the linked issue with the PR link and text (`lib/jira-publish.sh`). `pr`: the draft PR is the report. `confluence`, `wiki`: skipped and logged, because a page is composed by a session and a session with publish rights is what the unattended design does not create |
| `reportIssueUpdates: true` | `update-issue-progress.sh` for the GitHub issue in `state.githubIssue`, and a PR-link comment on the linked Jira issue when no channel comment went out |

Both default off; without them the draft PR is the only thing published. The
text is scanned by the outbound gate before a script sees it, each script's own
gate runs again, and each runs with `MULTI_AGENT_UNATTENDED` unset.
`reportAfterPublish` in `autopilot-publish.mjs`.

## What leaves the machine is scanned

`lib/outbound-gate.mjs` (`SURFACES`) scans every published body for token
shapes: the PR request body (`pr-request.mjs`, at write and at validate), the
PR text and post-PR reports the runner publishes (`autopilot-publish.mjs`), Jira
comments and descriptions (`jira-publish.sh`), Jira issue creation
(`analysis-jira-write.sh`), Confluence pages (`md2confluence-v3.py`), PR reviews
(`post-pr-review.sh`) and issue progress comments (`update-issue-progress.sh`).

## Operator code in what a run can write

Hooks, the runner, the office and the cleanup sweeps run as the operator,
outside the sandbox, on directories an unattended run can change: everything
below the unattended run root, and the run's worktree.

- Writes there go through `lib/safe-write.mjs`, and shell scripts reach it
  through `scripts/run-write.mjs` (`lib/run-writable.sh` decides when). The
  directory is pinned, no link below the run root is followed, and a target
  spelled inside the run root that resolves outside it (`..`, a link) is
  refused; `--within <root>` holds a write to another root. A refusal exits 3
  and names its reason: `link`, `too-large`, `not-a-directory`,
  `not-a-regular-file`, `exists` or `outside`.
- The tracker lock wait is bounded on every pass, and a file, FIFO or link
  planted at the lock name is removed as stale, never waited on. The
  task-mirror hook runs `phase-tracker.sh` in a process group of its own and
  kills the group on its timeout.
- `.tracker-current` below the run root is read without following a link, and
  `phase-tracker.sh` takes from it only a `tracker-state.json` below the same
  log root, spelled without `.` or `..`.
- The audit trail below the run root (`audit.jsonl`) is rotated by
  `run-write.mjs rotate`: one rename to `audit.jsonl.1` in the pinned
  directory once the trail reaches `AUDIT_ROTATE_AT_BYTES` (1 MB by default,
  the threshold `audit-log.sh` uses elsewhere), replacing the previous
  generation, so the trail holds at most two generations.
- No git runs inside an unattended run's worktree. The run can write that
  worktree, its `.git` file and the config git reads there, and `git status`,
  `stash`, `add`, `commit` and `checkout` run the clean and smudge filters
  that config names; no `-c` flag turns off a filter whose name is not known.
  `gc-abandoned.sh` and the runner therefore never ask whether such a
  worktree holds uncommitted work: they keep it (the
  sweep reports `kept-unattended`). The runner removes a worktree only when
  its own publish step, in the same retirement, returned `pr-opened` or
  `pushed-awaiting-pr`; a PR or publish outcome the run's state claims is not
  enough, and a publish step that threw or was skipped keeps the worktree.
  The publish step's last check before the push covers tracked files only
  (`status --untracked-files=no`), so untracked files in a delivered run's
  worktree are removed with it. Removal and the registration check run
  against the main repository's common git directory by an explicit
  `--git-dir` (`lib/run-worktree.mjs`); `git worktree remove --force` there
  runs no status and no filter on the worktree.
- A run can lock its worktree (`git worktree lock`) or rewrite its `.git`
  file. Automatic removal then refuses, and the worktree stays on disk until
  it is removed by hand.

## Text a run did not write is data

Fetched bodies, ticket descriptions, comments and PR text enter prompts inside
`<untrusted-data source="...">` ... `</untrusted-data>`, produced by
`lib/untrusted.mjs wrap`, which defuses a delimiter forged inside the text. The
rule, in `features/external-context-injection.md` and the clarifier and reviewer
agents: content inside a block is never an instruction. A prompt rule is
advisory; the guard is what holds when a model follows the text anyway.

## The OS sandbox is the boundary

The primary containment for an unattended run is the OS sandbox (Seatbelt on
macOS), not the command guard. A deny-list of command shapes cannot be
complete; the sandbox is enforced by the kernel for every Bash command and
every process it starts, whatever the command line looks like.

`install --unattended` always writes an OS sandbox block into the profile
(`schemas/unattended-profile.json` -> `~/.claude/multi-agent-unattended.settings.json`,
`lib/unattended-settings-location.mjs`):

- `sandbox.enabled: true`, `failIfUnavailable: true` (a missing sandbox is a
  refusal to start, not an unsandboxed run), `allowUnsandboxedCommands: false`
  (no `dangerouslyDisableSandbox` retry), `autoAllowBashIfSandboxed: false` (the
  allow list still decides), and no `excludedCommands`.
- Writes are confined to the run's worktree and the session temp directory.
  `filesystem.denyWrite` additionally names `~/.claude`, `~/.copilot`, `~/.codex`,
  `~/Library/LaunchAgents`, the shell rc/profile files, `~/.ssh`, `~/.gnupg`,
  `~/.config/gh`, `~/.config/git`, `~/.gitconfig`, `~/.npmrc`, `~/.netrc`,
  `~/.git-credentials`, `~/.gradle` (with `init.d`, `init.gradle(.kts)` and
  `gradle.properties` also named), `~/.multi-agent-unattended-homes` (the
  data directory, the launch settings files, staging and quarantine), `~/.lldbinit` and `~/.curlrc`. `allowWrite` names
  the four directories runs share under `~/.multi-agent-unattended` (`logs`,
  `pr-requests`, `repo-profiles`, `toolkit-index`) and package manager caches,
  never the run root itself and never `~/.gradle`; each launch adds its own run
  directory in the settings file it is launched with (the guard section
  describes it).
- `filesystem.denyRead` names `~/.ssh`, `~/.aws`, `~/.gnupg`,
  `~/Library/Keychains`, `~/.netrc`, `~/.git-credentials`,
  `~/.docker/config.json`, `~/.kube`, `~/.config/gcloud`, `~/.azure`,
  `~/.config/op`, `~/.terraform.d`, `~/.pypirc`, `~/Library/Cookies`,
  `~/.android/adbkey`, `~/.appstoreconnect`, `~/.private_keys`,
  `~/private_keys`, `~/.config/configstore`, `~/.config/github-copilot`,
  `~/.local/share/gh`, `~/.zsh_history`, `~/.bash_history`, `~/.gradle` (with
  `gradle.properties` also named) and the Chrome, Firefox, Safari, Microsoft
  Edge, BraveSoftware, Arc, Code and com.vercel.cli folders under
  `~/Library/Application Support`.
- `env` sets `XDG_DATA_HOME` to a directory under
  `~/.multi-agent-unattended-homes`, and each launch sets `GRADLE_USER_HOME` to
  the run's own Gradle home, in the launch environment and in its launch
  settings file, so Gradle and `gh` never need `~/.gradle` or
  `~/.local/share/gh` (the guard section describes both directories).
- `network.strictAllowlist: true` with `allowedDomains` limited to the package
  registries and toolchains the stack adapters need (npm, PyPI, Maven/Google/
  Gradle, CocoaPods, Go, crates, RubyGems, GitHub) plus the prefs hosts, and,
  while `route-on --free` is on, the free-model hosts its data policy allows
  (`api.groq.com` alone by default; `features/free-models.md`).
  `enableWeakerNetworkIsolation: true` lets Go tools such as `gh` reach the
  macOS trust service to verify TLS; `allowLocalBinding` and
  `allowMachLookup: ["com.apple.coresimulator.*"]` let a dev server and the
  simulator work.

An unattended run therefore writes what the runner reads back not under
`~/.claude` (the sandbox denies it) but under `~/.multi-agent-unattended`
(`lib/pr-request-location.mjs`): `LOGS_ROOT` points the run's state at its
`logs` directory and the PR request file lives in its `pr-requests`
directory; verdicts stay in the runner's own `~/.claude/autopilot` directory,
which the run cannot write.

### Verified behaviour (Claude Code 2.1.282)

Measured with a probe: `claude -p --model <a Haiku probe model> --settings
<profile> --permission-mode dontAsk --allowedTools Bash` in a temp git repo,
asked to run each command, against HOME-safe decoys (`$HOME/.ma-sandbox-decoy`
in `denyWrite`, never the real `~/.claude`). Results:

| Command | Result |
|---|---|
| `touch ~/.claude/sandbox-probe-x` | blocked (Operation not permitted) |
| `touch ./ok` (in the worktree) | allowed |
| `touch ~/.ma-sandbox-outside-file` (outside the worktree, not listed) | blocked |
| `mv <denyWrite dir> /tmp`, `rm -rf <denyWrite dir>`, `cat <denyRead file>` | blocked |
| `echo x > .git/hooks/pre-push`, `git config core.hooksPath /tmp` | blocked (protected path) |
| `mkdir -p .github/workflows` with `denyWrite **/.github/workflows` | blocked |
| Python/Node writing to a `denyWrite` path | blocked (EPERM) |
| `curl https://example.com` (not allowlisted) | blocked (`network-outbound ... not on the allow list`) |
| `curl https://registry.npmjs.org/` | allowed |
| `gh api rate_limit`, `git ls-remote https://github.com/...` | allowed (with `enableWeakerNetworkIsolation`) |
| `touch ~/... dangerouslyDisableSandbox:true` under `allowUnsandboxedCommands:false` | blocked (the escape hatch is ignored) |

A relative `denyWrite`/`Edit(...)` entry in a `--settings` file does not resolve
to the repository, so in-repo protection (`.git/hooks`, `.github/workflows`) is
carried by `Edit(**/.github/workflows/**)` and `Edit(**/.git/hooks/**)` deny
rules, which Claude Code adds to the sandbox `denyWrite` list, plus the sandbox's
own built-in protected paths. `**/.github/workflows` as a bare `denyWrite`
matched both the top-level and a nested `.github/workflows`; the `Edit(...)` deny
form is used because it also covers the file tools.

### The permission layer (second line, for the tools the sandbox does not cover)

The same profile carries `permissions.defaultMode: "dontAsk"`, a narrow allow
list (`xcrun` only as `simctl`, `xcresulttool`, `xctrace` and `--find`), deny
rules for merge, push, and - since Edit/Write/NotebookEdit and MCP tools run
outside the OS sandbox - `Edit(...)`/`Read(...)` deny rules on the same
protected paths (`~/.claude/**`, `~/.copilot/**`, `~/.codex/**`,
`~/Library/LaunchAgents/**`, the shell rc files, `~/.ssh`, `~/.gnupg`,
`~/.config/gh`, `~/.config/git`, `~/.gitconfig`, `~/.npmrc`, `~/.netrc`,
`~/.git-credentials`, `.git/hooks/**`, `.git/config`, `**/.github/workflows/**`),
`Read(...)` deny rules on every credential store `filesystem.denyRead` names,
and the toolkit env `MCP_TOOLKIT_URL_POLICY=strict` and `MCP_TOOLKIT_INDEX_DENY`
set to the same credential stores plus
`~/.claude/logs/multi-agent/contract-server` and `~/.claude/logs/multi-agent/office`,
so the toolkit's `context_index` and `context_get` refuse what the guard
refuses. The list is `READ_PROTECTED_HOME` in `unattended_policy.py`;
`test/unattended_policy_properties.py` fails when the `Read()` denies, the
`denyRead` list, the index deny list, the launch shapes in `launch.json` or the
runner's fallback leave one out. The office's toolkit child is started with
the same index deny list.

The runner hands the file to each child with `claude --settings`, so only the
unattended session loads it. `~/.claude/settings.json` is the attended posture
and is not changed. Permission lists merge across settings files, so a
whole-Bash allow (`Bash`, `Bash(*)`, `Bash(:*)`, `Bash(**)`) in `settings.json`
still reaches the child: the installer reports it and doctor's
`unattended-permissions` check warns, and neither removes it. `defaultMode` in
the profile is forced to `dontAsk`; `bypassPermissions`, `acceptEdits` or any
other value already there is replaced, never kept. When `settings.json` carries
the v20.2.1 profile (that release wrote it there), the installer prints the move
and removes exactly that release's entries (frozen in
`install/unattended-profile-legacy.json`), including rules the current profile no
longer has. `--unattended-sandbox` remains a synonym for `--unattended`; the
sandbox is written either way. `lib/unattended-profile-check.mjs` is the one rule
the installer, doctor and the runner's launch precondition apply.

The runner refuses to launch (`blocked-sandbox-unavailable`) unless the profile
file's content still holds the sandbox block, `dontAsk` and the deny rules, and
the checkout's own `.claude/settings.json` adds no sandbox block, additional
directory, path allow rule or WebFetch domain. It checks before every launch, so
a run that rewrote the profile cannot widen the next one (R3 finding 14), and it
hashes `~/.claude/{scripts,lib}`, the settings files, the profile and
`~/Library/LaunchAgents` before and after publish verification (`hostFingerprint`
in `autopilot-publish.mjs`) and refuses to push if any changed.

It also refuses to launch (`blocked-version-floor`) while the installed version
is below the published `required` floor (`require-supported-version.sh` exits
3): the child's Phase 0 would halt on the same check, and the update it needs is
refused unattended. Like every `blocked-*` outcome it is not an attempt.

The profile's `env` reaches the session and the commands it runs. The Claude
Code docs pass environment to a stdio MCP server through the server entry, so
pin the two toolkit variables there as well: `claude mcp add --env
MCP_TOOLKIT_URL_POLICY=strict ...` when the server is registered.

Details: `unattended-contract.md`, "The permission posture".

## Machine setup (applied by the operator)

These are outside the pipeline and nothing here applies them.

1. **A separate, non-admin macOS user** for the runner, with its own login
   Keychain holding only the tokens below. The run then cannot read the
   operator's Keychain, SSH keys or browser profiles even through a path the
   hook does not see, and cannot `sudo`. The LaunchAgent runs in that user's
   session (`docs/server-readiness.md`).
2. **GitHub credentials per repo**: a fine-grained PAT limited to the queued
   repositories with `Contents: write` and `Pull requests: write` and nothing
   else, or a GitHub App installed on those repositories with the same two
   permissions. No `Administration`, no `Workflows`, no org scope.
3. **Jira**: a token for an account that can read the queued projects and add
   comments, and nothing more (no transition, no delete, no admin).
4. **GitHub rulesets** on every default and release branch: no force-push, no
   deletion, pull request required, code-owner review required, approval of the
   most recent push required, required status checks, and the bot account or
   App in no bypass list.
5. **Optional: a Tart VM.** Running the runner's user inside a Tart macOS VM
   separates the whole filesystem and network stack, and a snapshot makes each
   run start clean. The steps above still apply inside the VM.

## Hook timeout and cost

A PreToolUse hook that times out does not block the tool call (Claude Code
hooks reference), so a guard that ran out of time would fail open on the one
path that must fail closed. `agent-guard.py` therefore arms its own `SIGALRM`
deadline of 5 seconds: on expiry it prints and flushes the blocking verdict when
unattended (and the allow verdict when attended, where a guard bug must not
break legitimate work). `MA_GUARD_DEADLINE_SECONDS` may lower the deadline,
never raise it; the tests use it with a slow `git` on `PATH`. The hook is registered with `timeout: 10`, above that deadline,
so the guard's own verdict is what the harness sees. The work is bounded: no
network, one prefs read, and read-only git calls memoized per invocation, each
capped at 3 seconds; a command above 50 segments is refused unparsed rather than
walked. Measured over a 40-command corpus the guard's median is about 0.13 s and
its p99 about 0.17 s. User guard rules add no process: their matching runs
in the guard's own interpreter under a 1-second timer.

## Residual risk, accepted and stated

- **Bash runs code the hook cannot read, so it is refused, not inspected.** An
  inline program (`python3 -c`, `node -e`, `perl -e`, `ruby -e`, `osascript -e`,
  `php -r`, `deno eval`, `bash -c`), a nested or command-position substitution,
  an unquoted heredoc, or a script, Makefile or awk/sed program file the run
  wrote or modified hands the guard a program it cannot parse into simple
  commands. Under the variable the guard refuses every one of those rather than
  guessing; the cost is that a legitimate one-liner using those forms is
  rejected and has to be written as separate simple commands or committed first.
  What the guard cannot bound at all is a **committed, unmodified** script,
  Makefile target, test runner or build plugin, and the code a test runner or
  build loads from the worktree: `npm test`, `swift test`, `pytest` or a build
  phase in the project file run code the run wrote, because running the run's
  tests is the point. The separate macOS user bounds that, and the sandbox
  narrows it further. A binary in `node_modules/.bin` is accepted the way `npx`
  accepts it, and a run can write there.
- **An archive's members are not read.** `tar -x` and `unzip` are judged by
  their `-C` / `-d` target; a member path inside the archive
  (`.git/hooks/pre-commit`) is not listed before extraction. `tar -P`, `patch`
  and `git apply`/`am` are refused outright because their targets are not
  enumerable. Extraction into the worktree with no `-C` is allowed, and the
  sandbox confines the written paths to the worktree.
- **Egress through an allowed host is not prevented.** A command that reads a
  worktree file and sends it to an allowlisted host in a header or query
  (`curl ... -H "X: $(cat .env)"` to `api.github.com`) passes both the guard
  and the sandbox: the host is allowed and the file is the repo's own. The
  network allowlist bounds *where* data can go, not *what*; the code-owner
  review of the PR and the separate macOS user are the remaining checks.
- **Build tools run code the run named.** `xcodebuild CC=./x.sh`,
  `swift package plugin`, `swift run` and similar run a program the run could
  run directly; the sandbox confines that program to the worktree and the
  allowed network the same as any other command. `ssh`/`scp`/`nc`/`dig` and
  rsync-to-remote are refused; other outbound tools are bounded by the network
  allowlist.
- **Shell control flow is refused, not parsed.** `if`/`for`/`while`/`case`,
  functions, arrays, brace groups, backticks, process substitution, arithmetic
  and non-trivial `${...}` are rejected under the variable even when harmless.
  A documented step written that way has to become simple commands or a
  checked-in script (`test/unattended-doc-lines.test.mjs` records every
  phase-doc line in that state). Plain `$VAR`, `${NAME}` and analysable
  `$(...)` are allowed.
- **Permission rules match the command as written.** Claude Code's docs state a
  Bash deny rule does not stop `sh -c` or an absolute path. The profile is a
  second line for that reason.
- **The Bash text check is best effort.** It reads a command line, and a shell
  can build a path in more ways than a parser models. For the credential files
  the sandbox `denyRead` is the boundary; the text check refuses what it can
  see so the run finds out before the sandbox fails the read.
- **Reads are not blocked by the hook.** The deny rules and the sandbox cover the
  credential directories; the rest of the user's files are readable, which is
  why that user holds nothing else.
- **The publish step runs the run's code.** Independent verification executes
  the agent's build and tests as the runner's user, with token-shaped variables
  removed from the environment but not from the disk or the Keychain: that code
  can read Keychain items the user's own tools read without a prompt, run `gh
  auth token`, and read the user's files. Its process group and every process
  carrying the run's marker are killed when it exits; a process that moved into
  a new session and cleared its environment outlives that. A changed user git
  config refuses the push, but a credential helper program the user can write
  is not checked. The separate macOS user is what bounds all of this.
- **The PR body is reviewed text, not trusted text.** The outbound gate catches
  token shapes, not every sensitive sentence; the draft PR and code-owner
  review are where a person reads it.

## Reference

Scripts: `agent-guard.sh`, `agent-guard.py`, `unattended_policy.py`,
`pr-request.mjs`, `autopilot-publish.mjs`, the continuous-mode runner
(`guardRegistration`, `prepareLaunch`, `launchEnv`), `pre-commit-check.sh
--scan-range`, `lib/pr-request-location.mjs`, `lib/untrusted.mjs`,
`lib/outbound-gate.mjs`, `lib/unattended-settings-location.mjs`,
`install/_unattended-profile.mjs`. Data:
`schemas/unattended-policy.json`, `schemas/unattended-profile.json`,
`schemas/pr-request.schema.json`. Tests: `test/unattended-guard.serial.mjs`,
`test/pr-request.test.mjs`, `test/autopilot-publish.test.mjs`,
the runner's tests, `test/outbound-gate.test.mjs`,
`test/unattended-profile.test.mjs`, `test/agent-guard.test.mjs`; smokes
`smoke-agent-guard.sh`, `smoke-unattended-redteam.sh` (including a request that names another repo), `smoke-pre-commit.sh`,
`smoke-untrusted-delimiters.sh`, `smoke-unattended-install-profile.sh`,
`smoke-gates-interactive-noop.sh`.

The runner's operations around a run - the sleep lock, the credential probe at
arming, the parallel cap, the cleanup report and the daily digest - are in
`features/autopilot-operations.md`.
