Put your software factory in a file, then decide which rules are law
A factory's flow should be a versioned, reviewable file. The design decision that matters most is which decisions the platform enforces and which ones an agent is allowed to make.
The flow belongs in a file
A software factory is a lot of moving parts: triggers, agents, checks, approvals, hand-offs. If that flow only lives in prompts, skill files and somebody's memory, nobody can see it all at once, nobody can diff it, and changing it means editing several places and hoping they still agree.
The fix is the same one we found for infrastructure and for CI: write it down as code. Put the flow in a file, version it, review changes in a pull request, and let teams start from a shared template and adapt it. Several tools shipped something along these lines this month, so I suspect this is becoming the default, and I'm glad. It makes a factory something you can reason about.
But putting the flow in a file is the easy decision. The harder one is what you write in it, because a file can make two very different kinds of rule look exactly the same. The examples below use the open .fct format, which I know best.
Two kinds of decision between steps
Every factory decides what happens next. Some of those decisions are routing. A simple, well-understood bug can skip the design phase and go straight to a fix. A big change probably needs a spec first. Those are judgment calls, and agents are good at judgment calls. If one gets it wrong, you lose some time.
Others are not routing at all. They are limits: a schema change needs a reviewed spec, nothing touching auth merges without a human, a run can only see the secrets it needs, and only certain things can deploy. If one of these gets it wrong, you have an incident.
The trap is that both kinds read the same in a config file. A reviewer sees "schema changes need a spec reviewed by the infra team" and assumes it is a rule. If an agent interprets that line, it is a strong suggestion. It will be followed most of the time, and the one run where it wasn't is the run you end up reading in a postmortem.
My rule of thumb is simple. If being wrong costs time, let the agent decide. If being wrong costs an incident, take the decision away from the agent. Routing can be smart. Enforcement has to be boring: deterministic, outside the agent, and the same on every run. In practice the list is short:
- What is allowed to merge.
- Anything that changes a database schema.
- Anything that touches auth.
- Which secrets a run can see.
- What can deploy, and where.
Let the agent choose, and let the file decide what the choice allows
In .fct, a factory is a graph of triggers and steps connected by typed bindings. A step runs when its inputs are present and its when: guard holds. That gives you a clean way to split the two kinds of decision. The agent's judgment is a typed value. The guard that reads the value is plain, deterministic code.
Here is a small factory that starts when a CI workflow run fails. An agent triages the failure, but it can only answer with one of three outcomes declared in the file. Only one of those outcomes is allowed to reach the step that writes a fix.
type Repository = { name: text, url: text }
type WorkflowRun = {
id: int, name: text, url: text,
conclusion: text, head_branch: text, head_sha: text
}
type AgentCall = { token: secret, prompt: text }
# The only answers triage may give. The tag set is closed.
type Route = choice {
small_fix { summary: text }
needs_spec { reason: text }
schema_change { reason: text }
}
external port credential(key: text) -> secret
runner agent:
kind: opencode
in: AgentCall
out: open
trigger ci_failed:
kind: github
config: {
path: "/api/github/ci",
target: { one: { repo: "acme/widgets" } },
event: workflow_run,
branches: ["main"]
}
out: { run: WorkflowRun, repository: Repository }
step triage:
runner: agent
in:
token: credential(key: "agent")
prompt: file "prompts/triage.md"
run: ci_failed.run
out: { route: Route }
step fix:
runner: agent
when: triage.route is small_fix
in:
token: credential(key: "agent")
prompt: file "prompts/fix.md"
run: ci_failed.run
plan: triage.route
out: { branch: text, summary: text }
The file "prompts/….md" paths in the examples are illustrative.
Read it the way a reviewer would. The trigger is a CI event, so nobody has to remember to start anything. The triage step declares its output port as a Route, a choice with exactly three tags, and every typed boundary is strict, so an answer outside that set is an error, not a suggestion. The fix step carries when: triage.route is small_fix. If triage says schema_change, no agent writes a fix, whatever the model thinks.
That is what I mean by typed outputs at each stage. The next step knows exactly what it is getting, and triage is a typed step with fixed outcomes. If triage is wrong, it is a wrong label you can see and correct, not a long agent session that wandered somewhere on its own.
Enforce outside the agent
Checks inside the agent, like a hook that stops a command before it runs, are great for fast feedback. But they live in the same place as the thing they guard, so they should never be the only line. The checks that matter, such as what can merge, what can deploy and which secrets a run can touch, belong outside the agent: in the factory's guards and effect steps, in branch protection, in permissions.
The industry is moving this way. In the last few days, several tools shipped features that let a failed check block an action, enforce rules outside the agent before each action runs, or refuse to publish a change until a person says yes. Different teams landed on the same answer: let the agent think, and keep the authority somewhere it can't talk its way around.
Here is the other half of the example: a factory that starts when a pull request is opened or updated. The agent reviews it, a person approves, and a merge step does the rest. The agent never gets a merge button.
type Repository = { name: text, url: text }
type PullRequest = { number: int, url: text }
type AgentCall = { token: secret, prompt: text }
type GithubAccess = enum { read, write, admin }
type GithubScope = map(text, GithubAccess)
external port credential(key: text) -> secret
external port github_token(repo: text, scope: GithubScope) -> secret
runner agent:
kind: opencode
in: AgentCall
out: open
trigger agent_pr:
kind: github
config: {
path: "/api/github/pr",
target: { one: { repo: "acme/widgets" } }
}
out: { pr: PullRequest, commit: text, repository: Repository }
step review:
runner: agent
in:
token: credential(key: "agent")
prompt: file "prompts/review.md"
pr: agent_pr.pr
commit: agent_pr.commit
out: { summary: text }
step approve:
kind: gate
config: { timeout: "1d", default: { approved: false } }
in: { pr: agent_pr.pr, commit: agent_pr.commit, summary: review.summary }
out: { approved: bool, note: text? }
step merge:
kind: github_merge
when: approve.approved
config: { method: "squash" }
in:
token: github_token(
repo: "acme/widgets"
scope: {
"contents": "write"
"pull_requests": "write"
"checks": "read"
"statuses": "read"
}
)
repo: "acme/widgets"
number: agent_pr.pr.number
sha: agent_pr.commit
body: review.summary
Three things are doing the enforcing here, and none of them is the agent. The merge step only fires when the human gate resolved with approved true. The merge is pinned to sha, the commit that was reviewed, and per the effect's reference it re-reads the pull request first and refuses unless the head is still that commit and every CI check on it is green. And the token is minted for one repository with only the permissions listed, so the model never holds a broader credential.
Gates need roles, escalation and a frozen snapshot
Human approval gates sound simple and fail in boring ways. The classic one: the gate is bound to a specific person, that person goes on leave, and the work stalls silently for a week. People who have run approval workflows for decades learned this long ago. Bind the gate to a role, not a person, and add an escalation timer so a stuck approval gets noticed and passed along.
Timeouts need care too. A timeout should never invent an approval. In the example, the gate's default is approved: false, so if nobody answers within a day, the answer is no. That is the shape I want: a missing human is a rejection, not a yes.
The other failure is subtler. A gate that waits for three days can end up approving something that changed underneath it. In the example, a Run's trigger seed is held constant for the whole Run and the merge is pinned to the reviewed commit, so a new push can't ride in on an old approval. A new push admits a new Run, with its own gate. Whatever platform you use, the gate should show the approver the diff, the typed output and the evidence, and what they approve should be exactly what ships. A stuck gate is annoying. A gate that approves a moving target is the dangerous one.
I'll be straight about the limits. The gate in the spec today has a timeout and a default resolution. Roles and escalation timers are things I want from an approval step, not things the format gives you yet.
Break your guardrails on purpose
A guardrail you have never seen fire is a guess. Test each one by trying to get past it. Feed the factory a failing check, a branch that moved after review, or a triage answer that should be refused, and confirm the action was blocked. A factory with a manual trigger is convenient for this, because you can supply the input yourself. Include the awkward cases too: what happens when a check crashes or times out? It should fail closed.
Guardrails are code, and code drifts. Someone renames a field, and a check silently starts passing. If a "should block" test goes green when it shouldn't, you want to find out on the next pull request, not in production. For anything critical, enforce the same rule a second time outside the agent, so both layers have to fail before something bad happens.
Where dotFactory fits
I build dotFactory, a hosted platform that runs factories like these. It isn't open source, but the .fct specification is open, so you can implement it yourself. The examples above follow the spec and the adapter reference, and the docs are at factory.redbeardlab.com/doc.
Flow as code is the right call. The skill now is deciding, on purpose, which parts of the code are law and which are advice.
Questions and answers
How do I stop an AI agent from merging its own PR?
Don't rely on the agent to refuse. Have the agent open the pull request, and make the merge a separate step the platform controls: it only fires after an approval, it is pinned to the commit that was reviewed, and it refuses if CI isn't green or the branch moved. Back that with branch protection and credentials that can't push to the protected branch or approve the agent's own work. The agent proposes, and the platform decides whether the change can merge.
What should be deterministic in a software factory?
Anything where a wrong decision causes an incident rather than lost time: what is allowed to merge, schema changes, anything touching authentication or authorization, which secrets a run can see, and what can deploy. These rules should be enforced by the platform outside the agent and behave the same on every run. Routing decisions, like which agent takes a task or whether a ticket needs a spec first, can stay with the agent, as long as the agent's answer is a typed value and a plain guard decides what it allows.
What is a typed output for an AI coding agent?
A typed output is a result with a declared shape, such as a record with a summary and a branch name, or a triage answer chosen from a fixed list, instead of free-form text. The next step knows exactly what it receives, the platform can validate it, and guards can compare against its fields. In .fct you declare the shape with type and put it in a step's out: port. The agent still does the open-ended work, but what it hands over is checked.
What is a human approval gate for AI agents, and how should it work?
A gate pauses a run until a person decides. A good one shows the approver the context they need, waits on a form with fixed fields, and resolves to a safe default if nobody answers, so a timeout never turns into an approval. Ideally it is bound to a role with an escalation timer so it doesn't stall when someone is away, and what gets approved is pinned to the exact commit that was reviewed.
How do I test that an agent guardrail actually works?
Break it on purpose. Trigger each guardrail with an input that should be blocked, such as a failing check, a moved branch or an out-of-range agent answer, and confirm the action did not happen. Run those tests in CI so drift gets caught on the next pull request. Also test the failure modes, like a check that crashes or times out, and confirm they fail closed. For anything critical, enforce the same rule a second time outside the agent.
Written by
Simone has spent more than 10 years building software, from research at CERN to big tech at AWS and fast-growing startups like Rippling. These days he's building dotFactory, because he thinks writing software should feel a lot more like running a well-oiled factory.