Software Factory Part 4: Keeping Agents Away From the Keys
Table of contents
Part 4 of 4 in the software factory series. Part 3 covers the verifier. To run it on your own repo, start at the Software Factory product page.
Two of the rules behind my software factory are about who holds power. Code decides workflow state, never an agent: agents don’t move tickets, open PRs or push branches. And agents don’t hold the keys: the orchestrator holds a GitHub token that can push to your repo and a Jira token that can edit tickets, and the agents must never reach them.
This post covers the orchestrator that holds those keys, the leak I found in my own design before making the repo public, how I fixed it, and how you can see what the factory is doing at any moment.
The Orchestrator: A Poll Loop and Some Labels
The orchestrator is about 1,800 lines of TypeScript, and it’s deliberately boring:
for (const repo of cfg.repos) {
const tracker = trackerFor(ctx, repo);
for (const key of await tracker.find("toClose", repo)) await runStage("close", closeStage, repo, key);
for (const key of await tracker.find("toVerify", repo)) await runStage("verify", verifyStage, repo, key);
for (const key of await tracker.find("toReview", repo)) await runStage("review", reviewStage, repo, key);
const [next] = await tracker.find("toBuild", repo);
if (next) await runStage("build", buildStage, repo, next);
}
Every 60 seconds (poll_interval_seconds in factory.config.yml) it asks each repo’s tracker for four queues; on Jira, each is a JQL query. The tracker and the code host sit behind small interfaces, so nothing here is Jira- or GitHub-specific. It finishes work that’s already in flight before starting anything new, and builds at most one new ticket per pass.
Jira labels are the state machine. A ticket moves through factory-building, factory-pr-open, factory-reviewed and factory-merged, then ends at factory-verified or factory-verify-failed. Each query excludes what’s already done, so every stage is idempotent. Restart the container mid-pass and nothing runs twice. You can also see a ticket’s stage from Jira without opening a log.
Polling, not webhooks. A webhook needs a public URL, and the factory runs on a laptop. A minute’s delay is fine for work that takes twenty. And it runs one ticket at a time, because there’s one app port and one set of test data, so parallel builds would trip over each other.
The Threat
This is the part I got wrong first. If you run a factory like this yourself, it’s the most important part of the series.
The builder runs whatever shell commands the model chooses. The model reads the ticket text, and anyone who can edit a Jira ticket can put instructions in it. The prompt tells the agent to treat ticket text as data, but a prompt is not a security boundary. Treat the factory-enabled label as commit access.
The Mistake
My first version started each agent with the GitHub and Jira tokens removed from its environment, which seemed like enough. When I audited it before making the repo public, the builder’s shell turned out to run as the same Unix user as the orchestrator. One command was enough:
cat /proc/<orchestrator-pid>/environ
That printed the GitHub token, which had admin access to the repo. Removing variables from a child process does nothing about its parent’s environment.
There was a second path I hadn’t considered. During the red-green check, the orchestrator itself ran the repo’s tests, with all its tokens in its environment. The agent wrote those tests. It could also have dropped a git hook into .git/hooks/, and the orchestrator’s own git commit would have run it.
The Fix: Two Unix Users and a Narrow Bridge
| User | Runs | Holds |
|---|---|---|
factory |
The orchestrator: tracker and code-host calls (Jira and GitHub), the network clone, the final push | The GitHub and Jira tokens, and /data (mode 0700) |
agent |
Everything that runs model output or repo code: Claude, install, tests, the app, git inside workspaces | Only the Claude token |
The orchestrator runs agent commands with sudo -n -H -u agent /usr/local/bin/agent-exec. sudo resets the environment and passes through only an explicit allowlist: the Claude token or API key, the git identity, the Playwright browser path and two update-notifier switches. That one command is all factory may run as agent, and agent has no sudo rights at all. A process under a different uid can’t read the orchestrator’s /proc/<pid>/environ, so the first leak is gone.
The clones are separate too. The factory clones from GitHub with its token. The agent clones that copy, with no token, and owns the result.
The token never sits in a git config file either. My first version set it up globally in the container’s entrypoint (gh auth setup-git plus an insteadOf rule to turn SSH URLs into HTTPS), which put credentials where any git process could find them. Now the code host hands the factory an auth header and the SSH-to-HTTPS rewrite as GIT_CONFIG_COUNT / GIT_CONFIG_KEY_n / GIT_CONFIG_VALUE_n environment variables, and they’re passed only to the factory’s own git clone and git push. Git reads them for that one command and nothing writes them to disk. I checked it against a local bare repo: the push works, and the agent clone’s .git/config has no credentials.
To push, the agent exports its branch as a git bundle, which is a plain file. The factory fetches the bundle into a fresh repo it owns and pushes from there. It never opens the agent’s repository, so hooks or core.fsmonitor tricks planted there never run with the token. Only factory/* branches can be pushed.
Testing It Like an Attacker
I checked this in a throwaway container with the real tokens, running a script as the agent user that tried each path. Reading /proc and /data was denied. sudo and gh auth were refused. A pre-push hook and an fsmonitor command planted in the agent’s repo never ran as factory. The push itself worked, and pushing to main was refused. All 15 checks passed.
What’s still open: agents have unrestricted outbound access, so they could send repo contents elsewhere; if that matters to you, add an egress proxy. The agent can read its own Claude token, which it needs in order to run at all. And the factory.yml commands are trusted, because a human approved them in the onboarding PR.
Seeing What It’s Doing
My first version printed KAN-4: build, then nothing for twenty minutes. You can’t trust a factory you can’t watch, so now every layer reports.
The status command shows every ticket at a glance:
docker compose exec factory status
TICKET STATE STAGE / STEP UPDATED COST PR
KAN-4 idle verify: 9 not passing 2m ago $2.23 …/pull/3
Live progress is in the container log, which shows each step, such as KAN-4 [verify] starting the app. Agents run with --output-format stream-json, so every tool call appears as it happens:
KAN-4-verify › browser navigate http://localhost:4321/
KAN-4-verify › browser run_code_unsafe
claude KAN-4-verify done: 1.0 min, 6 turns, 5 tool calls, $0.16
Every agent run’s complete event stream (each message, tool call and result) is saved as a .jsonl transcript you can replay. The log is also written to /data/factory.log, which survives container restarts. And the ticket’s labels and comments in Jira show its stage without anyone opening a terminal.
What Broke in the Plumbing
Two more problems showed up within an hour of the first real ticket, neither of them in the unit tests:
| Problem | Cause | Fix |
|---|---|---|
Lost polls to ENOTFOUND on the Jira host |
The laptop’s network dropped while it slept | Retry Jira requests with backoff on network errors, 429 and 5xx responses |
| 855 MB of workspaces after one ticket | Every stage kept a full clone with node_modules |
Delete a stage’s clones when it succeeds; keep failed ones for debugging |
Key Takeaways
- A prompt is not a security boundary. Anyone who can edit a ticket can instruct the agent, so the label that lets the factory build a ticket is commit access.
- Stripping env vars is not isolation. A child process under the same user can read its parent’s environment. Use a separate Unix user.
- Hand off files, not repositories. A
git bundlecarries the branch without the hooks and config that could run with your token. - Test your sandbox like an attacker. My first design looked safe and leaked an admin token in one command.
- Make the state visible. Labels in Jira, a status command and replayable transcripts are what make an unattended factory trustworthy.
That’s the whole line. The overview has the first ticket end to end, and the code is in the repository.