Agent Isolation (run_as_user)
Start every model process as a second Unix user that holds no keys of the daemon.
What the Option Does
With executor.claude_code.run_as_user set, every model process starts as that Unix user through
sudo -n -u <user> -g <group>: the coding agent session, the quality gates, the workflow hooks, the
lint program, the evidence commands and every git command in the workspace of the agent.
The daemon stays the daemon user and keeps every credential: the adapter tokens, the GitHub token, the config file and the Pilot home. The agent user cannot read them:
- Environment allow list. A process of the agent user gets only
PATH,LANG,LC_ALL,TERM,TZ,TMPDIR, the Go names (GOCACHE,GOMODCACHE,GOFLAGS,GOTOOLCHAIN,GOPROXY,GONOSUMDB,GOPRIVATE),NODE_OPTIONS,PILOT_EXECUTOR, the names of the gh guard, and the task names that the code sets.HOME,CLAUDE_CONFIG_DIR,GOPATH,GOCACHEandGOMODCACHEpoint below the home of the agent user. Every other name is dropped, the GitHub names included. - Model names only for the model session. The
CLAUDE_*andANTHROPIC_*names reach the model session only. A gate, a hook, the lint program and an evidence command get none of them, so a test that calls a model API from a gate gets no key. - Own clone, no shared objects. Each task run gets a clone that the agent user creates and owns.
It shares no
.gitdirectory, no alternates file and no object file with the repository of the daemon, and the agent user never reads that repository. - Firewall. The host set-up script rejects the traffic of the agent user to the cloud metadata address and to the gateway port (see Host Set-up).
- gh guard. With the option set and the gh guard on (
gh_guard, default true), a run whose shim cannot be set up does not start; it does not go on without the guard.
With the option empty (the default) model processes run as the daemon user, as before.
What the Daemon Does and What the Agent Does
| Step | Who | What happens |
|---|---|---|
| Fetch | Daemon | Fetches the base branch in its own repository and resolves the base to a commit SHA. |
| Bundle | Daemon → agent | Runs git bundle create for the base commit and pipes it on standard input into a process of the agent user, which stores it as a bundle file. |
| Workspace | Agent | git init, one fetch from the bundle, the task branch and refs/remotes/origin/BASE at the base commit, the origin URL without a user or password part, the bundle file removed. The daemon then checks the workspace (task branch at the SHA, no alternates file, no hard-linked object, no other ref). |
| Session | Agent | The model session, the quality gates and the commits run in the workspace as the agent user. |
| Hand-over | Agent → daemon | The agent user runs git pack-objects --stdout for the commits above the base. The daemon copies the stream into a quarantine object directory of its own repository, checks the pack header, runs git index-pack --strict, checks the tip and every object of the range, and only then moves the pack into its object store and sets refs/pilot/handover/<task>. |
| Secret gate | Daemon | Scans the hand-over SHA with the settings of the project. |
| Push | Daemon | Pushes <handover SHA>:refs/heads/<branch> with its own credentials. No git command of the daemon runs with the workspace as its directory, so the git config, hooks and remotes of the workspace have no effect. |
| Pull request | Daemon | Opens the pull request, as in a run without the option. |
Limits of the hand-over: the pack stream is at most 2 GB, one object at most 512 MB and all objects of the range together at most 4 GB. A branch with no commit above the base has nothing to hand over and takes the path of a run with no new commit. A daemon repository that is a partial clone is refused.
The workspace is removed at the end of the run only when its commits are safe outside it (the push succeeded or a recovery ref holds them) or it has no commit above the base. Otherwise it stays, one Error line names its path, and the sweep at the next daemon start removes it.
Host Set-up
scripts/box/setup-agent-user.sh prepares a Linux host. Run it by hand as root; no daemon code
calls it. Each step is skipped when it is already done.
# Print every command, change nothing
sudo scripts/box/setup-agent-user.sh --daemon-user <daemon-user> --gateway-port <gateway.port> --dry-run
# Apply
sudo scripts/box/setup-agent-user.sh --daemon-user <daemon-user> --gateway-port <gateway.port> --confirm-root-pathFlags
| Flag | Default | Description |
|---|---|---|
--daemon-user U | ec2-user | The user the daemon runs as |
--agent-user U | pilot-agent | The agent user to create |
--group G | pilot | The shared group |
--data-dir D | /var/lib/pilot | Data directory of the daemon |
--agent-root A | DATA_DIR/agent | Root of the agent directories; must not hold the data dir |
--gateway-port P | 9091 | The gateway port of the daemon config (gateway.port); the firewall rejects TCP of the agent user to it. A whole number from 1 to 65535 |
--dry-run | Print every command, change nothing | |
--undo | Remove the set-up (see Turning It Off) | |
--keep-daemon-sudo | Leave the cloud-init sudoers file alone | |
--confirm-root-path | You hold another root session; required when the cloud-init file still has a line to comment out |
Steps
The steps run in this order; an apply stops at the first failed step.
| Step | What it makes or checks |
|---|---|
daemon-private | A check, changes no mode: the data dir gives group and others traverse at most (0711 or stricter), and no entry of it but the agent root is open to the agent user. On a failure it prints one chmod per path to run, and the apply passes once you ran them. |
group | A system group (pilot). |
agent-user | A system user (pilot-agent) with no login shell (/usr/sbin/nologin) and the home AGENT_ROOT/home. |
daemon-membership | The daemon user joins the group. A running daemon picks up the group at the next restart. |
firewall-ipv4, firewall-ipv6 | A chain PILOT_AGENT_EGRESS, reached from OUTPUT by an owner match on the agent user. It rejects the metadata address (169.254.169.254, IPv6 fd00:ec2::254) and TCP to the gateway port (--gateway-port, default 9091) on loopback and on every local address through lo, with a TCP reset. A run with another --gateway-port leaves the rules of the new port only. The IPv6 step is skipped when IPv6 is off on the host. |
firewall-unit | A oneshot systemd unit pilot-agent-firewall.service that applies the chain at boot. |
directories | The agent root (root:root 0755), below it home (agent user, 0750) and workspaces (agent user, 2770), and go, go/cache, go/mod below the home, made by the agent user. |
sudoers-rule | /etc/sudoers.d/90-pilot-agent-runas with <daemon-user> ALL=(pilot-agent) NOPASSWD: SETENV: ALL, validated with visudo before it is moved into place. |
cloud-init-sudoers | Comments out the lines that give the daemon user sudo to root in /etc/sudoers.d/90-cloud-init-users (numbered backups, validated), then proves that sudo -n true as the daemon user fails and sudo -n -u <agent-user> true succeeds. Needs --confirm-root-path; skipped with --keep-daemon-sudo. |
agent-proof | A check as the agent user: create and remove a file in its home and in the workspace root, and fail to list the data dir and each directory in it but the agent root. |
After the cloud-init-sudoers step the daemon user has no sudo to root. Keep a second root session
open while the script runs; that is what --confirm-root-path confirms.
On success the summary ends with the values for the config:
== daemon config (executor section)
run_as_user: pilot-agent
run_as_group: pilot
run_as_workspace_dir: /var/lib/pilot/agent/workspaces
run_as_data_dir: /var/lib/pilot
gateway.port: 9091 # in the gateway section: the port the firewall rules protectThe firewall rules of the script reject the TCP port of --gateway-port; the default of the script
is 9091. The daemon start probes the gateway port of the config (gateway.port, default 9090).
When the two differ the probe reaches the port and the startup check fails. Pass the gateway port
of the config with the flag (--gateway-port 9090 for the default config), and check that the
gateway.port line of the summary matches the config.
Configuration
The keys live in the claude_code block of the executor section:
executor:
use_worktree: true # required with run_as_user
worktree_pool_size: 0 # required with run_as_user
claude_code:
run_as_user: "pilot-agent"
run_as_group: "pilot"
run_as_workspace_dir: "/var/lib/pilot/agent/workspaces"
run_as_data_dir: "/var/lib/pilot"| Key | Default | Description |
|---|---|---|
run_as_user | "" | Unix user for every model process, started through sudo -n -u <user> [-g <group>]. Empty = run as the daemon user. Linux only. |
run_as_group | "" | Group passed as sudo -g; also owns the run guard files. Required when run_as_user is set. |
run_as_workspace_dir | "" = /var/lib/pilot/agent/workspaces | Root of the agent workspaces. Absolute path only. Each task run gets its own clone here (pilot-ws-*), created and removed as the agent user. Must be owned by the agent user and must not be /, the daemon home, the Pilot home, the data dir or a parent of one of them. |
run_as_data_dir | "" = /var/lib/pilot | Data directory of the daemon (the --data-dir of the script). Absolute path only. Its modes are checked at the start and before each agent workspace is created. |
The run_as_* keys have no effect while run_as_user is empty.
The anthropic-api and openai-api backends run the tools of the model inside the daemon process.
With run_as_user set the daemon does not start with either of them.
Startup Checks
The preflight runs at every entry point that can start a model process. The gateway part of the firewall probe runs at the daemon start only. The checks, in order:
- With a daemon start, the gateway port is above 0.
run_as_userandrun_as_groupare not blank, the names are valid, the user is notroot, andrun_as_groupis set.run_as_workspace_dirandrun_as_data_dirare absolute paths.- The backend is not
anthropic-apioropenai-api. GOPROXYof the daemon (and aGOPROXY=in itsGOFLAGS) holds no credentials.- The host is Linux.
- User and group exist with uid and gid not 0, the user is not the daemon user, and the daemon user is a member of the group.
sudo -n -u <user> -g <group> trueexits 0 within 5 seconds, and sudo gives the agent no terminal (use_pty).- The home of the agent user exists.
use_worktreeis true andworktree_pool_sizeis 0.- With the environment of a gate, the agent user finds
shandgit(andmakeandgowhen the daemon finds them) and can make a file in its temporary directory. - The workspace root is not
/, the daemon home, the Pilot home, the data dir or a parent of one of them; it exists, the agent user owns it, and the daemon user can list a directory that the agent user creates in it. - The data dir gives group and others no read or write bit; each top-level entry gives others no
bit, and no group bit when its group is
run_as_group. The entry that holds the workspace root is skipped. - Firewall probe, as the agent user: a connection to a control listener on
127.0.0.1connects; connections to169.254.169.254:80and to the gateway port do not.
When a check fails the daemon logs one Error line that names it:
run_as_user preflight failed: no model process and no tool of the model will run check=<check>From then on every model spawn is refused with run_as_user preflight failed: <check>, and no
process starts. There is no fallback to the daemon user. A failed result is not kept: the next
preflight runs the checks again.
Check with pilot doctor
pilot doctor runs the same checks with the gateway port and host of the config and shows a
run_as_user row under Configuration: ✓ with the agent user when every check passes, ✗ with
run_as_user preflight failed: <check> when one fails. A failed row counts as an error, so the
command exits non-zero, and pilot doctor --verbose points to scripts/box/setup-agent-user.sh.
The firewall probe listens on the gateway port, so run pilot doctor while the daemon is stopped:
a running daemon holds the port and the row then names the gateway check.
Limits
- No GitHub credential for the agent. The agent user gets no GitHub token unless a GitHub App
is configured (
adapters.github.app). Then each run gets a read-only installation token for the repository of the project (contents,issues,pull_requests,metadata: read), used by the gh guard and by a git credential helper forhttps://github.com. There is no fallback to the token of the daemon. Without the App, inside a session theghcommands and agit fetchfail for a private repository. - Private dependencies. A quality gate runs as the agent user with the allow-listed environment
and no git credentials. Private dependencies of a gate must be reachable without a credential of
the agent. A
GOPROXYwith credentials passes to the agent user, so the daemon start refuses it. - Linux and sudo. The option needs Linux and
sudo; the host script needs root,iptables(andip6tableswhen IPv6 is on) and systemd. - Worktree settings.
use_worktree: trueandworktree_pool_size: 0. - CLI backends only. The API backends are refused.
Turning It Off
Remove run_as_user from the config, or set it to "", and restart the daemon. Model processes
then run as the daemon user again.
To remove the host set-up, stop the daemon first and run:
sudo scripts/box/setup-agent-user.sh --daemon-user <daemon-user> --undoThe undo removes the sudoers rule, stops every process of the agent user, stops and disables the boot unit, removes the firewall chain, and last restores the cloud-init sudoers file from its newest backup. The user, the group and the directories stay: the commands to delete them are printed, not run.