Sub topic 13
Running agents without a human: scripts and CI
This chapter assumes you have read the earlier chapters on approval modes, allow and deny rules, and sandboxing. You should be comfortable with shell scripting and have seen a CI pipeline such as GitHub Actions or GitLab CI.
Why headless use changes the model
In interactive use, the "ask" decision is a safety net. When no rule matches, the agent pauses and a person decides. In a script or CI job, nobody is there to answer. A run that reaches an "ask" decision will do one of three things, depending on the tool:
- Hang until the job times out.
- Fail the step with a non-zero exit code.
- Auto-approve, if the tool or your flags say so.
The third outcome is the dangerous one. Headless mode removes the human reviewer, so every control that depended on that reviewer has to be replaced by something that works without one. Your options are explicit rules, a sandbox, narrow credentials, and review of the output after the run.
Making "ask" impossible
The safest headless setup resolves every decision before the run starts. An "ask" should not be possible at runtime. You can model this by treating an unmatched command as a denial instead of a prompt.
from fnmatch import fnmatch
import re
DENY = ["rm -rf *", "git push --force*", "curl *"]
ALLOW = ["git status", "git diff", "pytest*"]
# Characters that let one command string run, chain, or substitute other commands.
CHAINING = re.compile(r"[;&|`$<>\n]")
def decide_headless(command: str) -> str:
"""Return 'deny' or 'allow'. There is no 'ask' without a human."""
if any(fnmatch(command, p) for p in DENY):
return "deny"
if CHAINING.search(command):
return "deny" # compound commands are refused, not queued for review
if any(fnmatch(command, p) for p in ALLOW):
return "allow"
return "deny" # default deny: unmatched means blocked
print(decide_headless("git status")) # allow
print(decide_headless("git diff; echo hi")) # deny
print(decide_headless("npm install")) # denyThis is the same pattern as the interactive check, with two changes. Unmatched commands are denied, and chained commands are denied instead of sent to a user. A denial in a headless run should be logged and should fail the job loudly, so a person finds out the agent was blocked.
Note that git diff is allowed here without a trailing *. A prefix pattern such as git diff* also matches git diff; curl evil.example, and that is the failure the chaining check exists to catch.
Modes in headless runs
Most agent CLIs offer a non-interactive flag. Names differ between tools, so check your tool's documentation. The behaviors fall into a few families.
| Headless strategy | What happens on an unmatched action | Risk |
|---|---|---|
| Pre-approved rules only | Action is blocked and the run fails or skips it | Low. Runs may fail on unforeseen needs. |
| Read-only mode | Any write or execute is blocked | Low. Useful for review and analysis jobs. |
| Auto-approve everything | Action runs without a check | High. Needs a sandbox and narrow credentials. |
Pre-approved rules are the default recommendation. Auto-approve is a legitimate choice only when the environment itself is disposable and contained, for example a container with no network access and no secrets mounted.
The loop without a person
Here is how a headless run flows. The agent still runs the same tool loop, but the decision step has no interactive branch.
The last two steps matter most. The human moves from approving each action to reviewing the outcome. That works only if the outcome is reviewable, which leads to the next section.
Containing the blast radius
Because no one watches the run, the sandbox and credentials carry more weight than the rules do.
- Filesystem. Run in a fresh checkout or container. Restrict writes to the workspace, using path resolution so
..and symlinks cannot escape it. - Network. Block outbound traffic by default, and allow only the hosts the task needs, such as your package mirror.
- Credentials. Give the job a token scoped to one repository and the minimum permissions. Prefer short-lived tokens issued per run over long-lived secrets.
- Time and cost. Set a job timeout and a cap on model calls or tokens. An agent stuck in a loop will otherwise run until the platform stops it.
The path check from earlier chapters applies directly to the workspace boundary:
from pathlib import Path
def inside_workspace(path: str, workspace: str) -> bool:
root = Path(workspace).resolve()
target = (root / path).resolve()
return target.is_relative_to(root)
print(inside_workspace("src/app.py", "/home/me/project")) # True
print(inside_workspace("../.ssh/id_rsa", "/home/me/project")) # FalseThis needs Python 3.9 or later. Resolving first is what defeats .. segments and symlinks.
Prompt injection is worse in CI
CI jobs often process content that outsiders control: pull request titles and bodies, issue text, commit messages, and code from forks. An attacker who can open an issue can put instructions in it. If an agent reads that issue with write access and a deploy token, the instructions become a real attack, and no human sees the action happen.
Practical rules:
- Do not run agents with secrets on pull requests from forks. Most CI systems withhold secrets from fork builds by default. Do not override that.
- Separate read jobs from write jobs. A job that reads untrusted text should have read-only tools and no secrets. A second job, with no untrusted input, can act on its structured output.
- Treat the agent's output as untrusted too. Do not pipe it into
bash, and do not use it to build a command line without validation. - Keep network egress closed so an injected instruction cannot send data out.
Reviewing after the fact
Without per-action approval, the audit trail is your main control. Make sure every run leaves evidence.
- Save the full transcript and tool-call log as a CI artifact.
- Have the agent commit to a branch and open a pull request. Never let it push directly to a protected branch.
- Require a human approval on the pull request, and keep branch protection on.
- Log denials. A spike in blocked actions means the task drifted or the agent was steered.
A short example of a job that applies these ideas, with an invented agent-cli command standing in for your tool:
name: agent-review
on:
pull_request:
types: [opened, synchronize]
permissions:
contents: read # read-only token for the job
jobs:
review:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- name: Run agent in read-only mode
run: agent-cli run --mode read-only --task "Summarize risks in this diff" > review.md
- uses: actions/upload-artifact@v4
with:
name: agent-review
path: review.mdHere agent-cli and its flags are placeholders. Replace them with your tool's real non-interactive command and read-only option. The structure is what matters: a read-only token, a timeout, a read-only mode, and an artifact for later review.
Common errors
- Assuming a non-interactive flag means "safe". Some flags skip prompts by approving everything. Read what the flag does.
- Reusing your interactive allow list. An allow list built up over months of clicking "always" is often broader than a CI job needs. Write a fresh, minimal one.
- Leaving defaults in place. If no deny rule or default-deny exists, an unmatched action may run.
- Mounting secrets "just in case". Anything the agent can read, an injected instruction can try to leak.
- No timeout. A looping agent burns money and runner time.
Decision checklist
Before enabling a headless run, confirm:
- Every action the task needs is covered by an explicit allow rule, and everything else is denied.
- The job runs in a disposable sandbox with limited network access.
- The credentials are scoped to one repository and expire quickly.
- No untrusted input reaches a job that holds write access or secrets.
- Output goes through a pull request and a human review.
- Logs and transcripts are saved.
Exercise
A CI job runs an agent on every new issue to draft a fix. The job has a repository write token and uses an "approve everything" flag. Name two changes that reduce the risk, and explain why each helps.
Show answer
First, split the work. The step that reads issue text should run read-only with no token, and a separate step should create the change from validated output. This blocks an injected instruction in the issue from reaching write access. Second, replace the approve-everything flag with a minimal allow list and default deny, plus a sandbox with closed network egress and a short-lived, single-repository token. Rules stop unexpected commands, and the sandbox and token limit damage if a rule fails. Having the agent open a pull request that a human must approve is also a valid answer, because it puts review on the outcome.
Exercise
Why does decide_headless treat a command containing ; as a denial, even if it begins with an allowed command such as git diff?
Show answer
A prefix pattern such as git diff* matches the whole string, including anything after the semicolon. The shell would run both commands, so the second one could be arbitrary. With no human to inspect the command, the safe choice is to deny any string with chaining or redirection characters.