Sub topic 13
Running agents without a human: scripts and CI
This chapter assumes you know the allow, ask, and deny decision, approval modes, and allow and deny rules from earlier chapters. It also assumes you can read a shell script or a CI workflow file.
What changes without a human
In a terminal session, an "ask" decision pauses the agent and shows you a prompt. In a script or a CI job, nobody is there to answer. This is called headless or non-interactive use. The agent has to resolve every decision on its own, so you must decide ahead of time what happens in each case.
Interactive use hides three problems that headless use exposes:
- No reviewer. A mistake or a prompt injection is not caught by a person reading the prompt.
- Unattended credentials. CI jobs often hold deploy keys and tokens that a developer's laptop does not.
- No pause. An unanswered prompt either hangs the job until it times out or gets auto-approved, depending on the tool.
Fail closed on "ask"
A safe headless policy treats "ask" as "deny". The agent runs only what you listed explicitly, and everything else is refused. The agent then reports the refusal instead of waiting.
import re
from fnmatch import fnmatch
DENY = ["rm -rf *", "git push --force*", "curl *"]
ALLOW = ["git status", "git diff*", "pytest*"]
CHAINING = re.compile(r"[;&|`$<>\n]")
def decide(command: str, interactive: bool) -> str:
"""Return 'allow', 'deny', or 'ask' (ask only when a human is present)."""
if any(fnmatch(command, p) for p in DENY):
return "deny"
if CHAINING.search(command):
return "ask" if interactive else "deny"
if any(fnmatch(command, p) for p in ALLOW):
return "allow"
return "ask" if interactive else "deny"
for cmd in ["pytest -q", "npm install", "git diff; echo hi", "curl http://example.com"]:
print(f"{cmd!r}: interactive={decide(cmd, True)}, headless={decide(cmd, False)}")Running it shows pytest -q allowed in both modes. npm install and git diff; echo hi are "ask" interactively and "deny" headless. The curl command is denied in both.
Narrow the environment, not just the rules
Rules decide what the agent may attempt. In CI, also limit what a successful attempt can reach, because pattern rules can be bypassed (see the chapter on rules and patterns).
- Run in a sandbox. Use a throwaway container or VM with no access to your wider network or internal services.
- Use least-privilege credentials. Give the job a read-only token, scoped to one repository, that expires soon. Do not give it your personal credentials.
- Limit the workspace. Check out only the repository the task needs.
- Cap the run. Set a job timeout and a limit on the number of tool calls.
Keep a human at the exit
Headless does not mean unreviewed. Let the agent change files or open a pull request, but have a person approve the merge. Branch protection and required reviews are permission controls too, and they sit outside the agent's reach. Save the agent's logs as a CI artifact so you can audit what it did later.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Treating "ask" as "allow" to stop the job hanging | Every unlisted command runs | Fail closed and expand the allow list deliberately |
| Putting deploy secrets in the agent's environment | An injected instruction can read or send them | Provide only the secrets the task needs |
| Using broad allow rules like `*` | Rules provide no protection | List specific commands |
| Running on untrusted pull requests | Prompt injection reaches a privileged agent | Run untrusted input with no secrets and no write access |
Exercise
A CI job runs a coding agent to fix lint errors. The agent tries npm install some-package && npm test, and your allow list contains only npm test. What should a safe headless policy do, and why?
Show answer
Deny it. The command contains &&, so it is a compound command, and a prefix or pattern rule for npm test should not auto-approve it. It would normally be an "ask", but with no human present "ask" must fail closed. The job should log the refusal and continue or fail, not install an unreviewed package.