One Ticket In, One Reviewed Branch Out: Build a Software Factory with Claude Code
A software factory is loops chained into a script: a spec stage, a red gate, a retry loop, and a read-only judge turn one ticket into a reviewed branch in its own worktree, and the judge catches a case the tests missed, but the merge and the review still belong to you.

Chain a spec stage, a red gate, a retry loop, and a judge that cannot write, then read the branch and merge it yourself.
You can get Claude to write the change, but you still babysit every step: write the tests, check they fail, rerun the build, and read the diff, one prompt at a time.
In this article: Day 26 of a day-by-day guide to Claude Code. Start here: you run one script that turns a ticket into a reviewed branch in its own worktree, then read the branch, merge it, and remove the worktree. Going deeper: why the judge caught a defect that the tests missed, what a read-only judge can and cannot do, and why review capacity is the limit.
Tested on Claude Code 2.1.327.
Yesterday a retry loop added the billing category, and you committed it as Add a billing category.
Start here
At the end of today, you can run one script that turns a ticket into a reviewed branch in its own worktree, read the branch, merge it, and remove the worktree.
Starting fresh or fell behind? Run git switch -C main claude-code/day-25. This replaces your main with the end of Day 25; to keep your own work, run git branch my-backup first.
On Day 3, Claude wrote a spec before any code. On Day 25, a retry loop ran Claude until a check passed. Today you chain several loops, and a ticket plays the part of the spec. A software factory is loops chained into a pipeline that takes a ticket in and puts a reviewed change out. A ticket is a short file that says what to change. The merge stays with you.
Today's factory has four stages, and each one runs in a git worktree from Day 16, so your main checkout does not change until you merge:
- Spec. Claude turns the ticket into tests.
- Red gate. The new tests must fail before the change, or they prove nothing.
- Build. Day 25's
scripts/loop.shruns Claude untilnpm testpasses. - Review. A judge reads the change and answers
APPROVEorREJECT. A judge is aclaude -prun that can read but not write, so it cannot change the work it grades. It is Day 9's read-only helper, run from a script.

The only loop back is from a rejected review to the build. Every other stage either moves forward or stops.
Check the starting state
cd ~/code/ticket-triage
git status
git log --oneline -1
npm test
Git prints nothing to commit, working tree clean, and the log shows Add a billing category. The tests show ℹ fail 0. Both tested runs had 25. Write your number down.
Write the ticket
Create the ticket in an editor, or paste this into the terminal:
mkdir -p tickets
cat > tickets/unsubscribe.md <<'EOF'
# Treat unsubscribe as cancel
Customers write "unsubscribe" when they want to stop their subscription. Today those tickets
go to other. They should go to cancel. A ticket that also matches an older rule, such as
"Unsubscribe me and refund my last order", keeps the older rule's category.
EOF
The first line is the title, and the factory uses it as the commit message. The last sentence repeats the CLAUDE.md rule from Day 5 for this ticket. Check today's behavior:
node -e 'import("./src/classify.js").then(m => console.log(m.classify("Please unsubscribe me"), m.classify("Unsubscribe me and refund my last order")))'
node -e runs one line of JavaScript. This one loads src/classify.js and calls classify() on two tickets. It prints other refund. The first ticket is wrong today.
Write the factory
Create scripts/factory.sh:
cat > scripts/factory.sh <<'EOF'
#!/usr/bin/env bash
# One ticket in, one reviewed branch out. Run it from the main checkout. The merge stays with you.
ticket="$1"; name=$(basename "$ticket" .md)
git worktree add -q ".claude/worktrees/$name" -b "$name" || exit 1
cd ".claude/worktrees/$name" && mkdir -p .loop
log() { echo "$*" | tee -a .loop/factory.txt; }
# 1. Spec: Claude turns the ticket into tests. It writes only test/$name.test.js.
claude -p "Read $ticket. Write test/$name.test.js with node:test tests for what it asks. Follow the test rule in CLAUDE.md. Do not change any other file." --permission-mode acceptEdits > .loop/spec.txt
# 2. Red gate: the new tests exist, src/ is unchanged, and the new tests fail.
[ -f "test/$name.test.js" ] && git diff --quiet -- src/ || { log "spec: no test file, or src/ changed: stop"; exit 1; }
node --test "test/$name.test.js" > /dev/null 2>&1 && { log "red gate: the new tests already pass: stop"; exit 1; }
log "spec: test/$name.test.js fails, as it should before the change"
git add "test/$name.test.js"
# 3. Build: the Day 25 loop, with npm test as the gate.
./scripts/loop.sh "Do what $ticket asks. Change only files in src/." || { log "build: the loop stopped, read .loop/ledger.txt"; exit 1; }
# 4. Review: a judge that can read files and run git diff, and nothing else.
# On REJECT, Claude gets the review word for word, the tests run again, and the judge looks again.
for review in 1 2; do
verdict=$(claude -p "Review a change. Read $ticket and CLAUDE.md, and run git diff HEAD. On the first line, reply APPROVE if the change does what the ticket asks and follows CLAUDE.md. Otherwise reply REJECT, then the reasons." --permission-mode dontAsk --allowedTools "Read" "Bash(git diff *)")
echo "$verdict" > ".loop/review-$review.txt"; log "review $review: $(head -1 ".loop/review-$review.txt")"
[[ "$verdict" == APPROVE* ]] && break
[ "$review" = 2 ] && { log "review: rejected twice, read .loop/review-2.txt: stop"; exit 1; }
claude -p "A reviewer rejected your change for $ticket. Fix what the review says. Change only files in src/. The review:
$verdict" --permission-mode acceptEdits > .loop/fix.txt
./scripts/loop.sh "Do what $ticket asks. Change only files in src/." || { log "build: the loop stopped, read .loop/ledger.txt"; exit 1; }
done
git add -A && git commit -q -m "$(head -1 "$ticket" | sed 's/^# //')"
log "ready: branch $name. Read it, then merge it from the main checkout with: git merge $name"
EOF
chmod +x scripts/factory.sh
Read it stage by stage:
- Setup.
nameis the ticket's file name without.md, hereunsubscribe.git worktree addmakes the folder.claude/worktrees/unsubscribeon a new branch,unsubscribe, and-qkeeps git quiet. Day 16 already put.claude/worktrees/in.gitignore. Everything aftercdruns in the worktree.logprints a line and also saves it in.loop/factory.txt. Day 25 put.loop/in.gitignore. - Spec. One
claude -pcall writes the test file.acceptEditslets it write files with no prompt. Its reply goes to.loop/spec.txt. - Red gate. It stops if the test file is missing, if
src/changed, or if the new tests already pass.node --testruns only the new test file, and> /dev/null 2>&1throws away its output, so only pass or fail counts. Then it stages the tests, so yesterday's loop stops if Claude edits them. - Build. Yesterday's loop, with a task that names the ticket.
- Review.
dontAskdenies every call that would ask, and--allowedToolsaddsReadandgit diffwith any arguments, so the judge can read but cannot edit.git diff HEADshows every change since the last commit. The script reads only the first word of the reply. OnREJECT, a newclaude -pcall gets the review word for word, and the loop runs the gate again. The judge gets two looks. - Ready. On
APPROVE, it commits on the branch with the ticket's title and prints the merge command.

The red gate is three checks in a row. Only a missing feature, shown by failing tests, lets the build start.
Manual mode: no prompts. Every Claude call is claude -p with its mode set in the script, so nothing asks, in either mode.
Commit the ticket and the script
The worktree starts from your last commit, so commit both files first:
git add tickets/unsubscribe.md scripts/factory.sh
git commit -q -m "Add a ticket and the factory script"
git log --oneline -1
The log shows Add a ticket and the factory script.
Run the factory
./scripts/factory.sh tickets/unsubscribe.md
The tested auto run took 84 seconds and printed:
spec: test/unsubscribe.test.js fails, as it should before the change
round 1: gate failed, 2 failing
round 2: gate passed
review 1: REJECT
round 1: gate passed
review 2: APPROVE
ready: branch unsubscribe. Read it, then merge it from the main checkout with: git merge unsubscribe
The round lines come from the loop. The spec wrote 3 tests, and 2 failed until the build added the rule. The judge then rejected the change for a rule-order defect that the tests missed, and the fix round corrected it. The second round 1: gate passed is the loop rerunning the tests after the fix. Going deeper walks through the defect.

The auto run went around the review loop once. The fix call saw the judge's exact words, and the second review approved.
The tested Manual run took 56 seconds and wrote 5 tests, and the judge approved at once:
spec: test/unsubscribe.test.js fails, as it should before the change
round 1: gate failed, 2 failing
round 2: gate passed
review 1: APPROVE
ready: branch unsubscribe. Read it, then merge it from the main checkout with: git merge unsubscribe
Its build had already put the rule last. In the tests, the auto run had the defect and the Manual run did not. Expect either shape.
The last line you want is ready:. A line that ends in stop means the factory stopped, and your main checkout is unchanged. The worktree and the branch stay, so a second run of the same ticket fails with fatal: a branch named '<name>' already exists. Read the .loop/ files in .claude/worktrees/unsubscribe first, such as .loop/review-2.txt after review: rejected twice, because the reset deletes them. Then reset from the main checkout:
git worktree remove --force .claude/worktrees/unsubscribe
git branch -d unsubscribe
--force deletes the worktree with its uncommitted changes. Without it, git refuses with contains modified or untracked files, use --force to delete it. git branch -d works because a stopped run commits nothing on the branch. Afterward, git worktree list shows one line and git status --short prints nothing, so a second run starts clean. If you change the ticket, commit it first, because the worktree starts from your last commit. Going deeper explains each stop line.

Read before you reset. The reset deletes the .loop/ files that explain why the run stopped.
Read the branch
A judge is a second reader, not the last one. Read the branch yourself:
git worktree list
git log --oneline main..unsubscribe
git diff --stat main...unsubscribe
git diff main...unsubscribe -- src/
The list shows two lines: the main checkout on [main] and .claude/worktrees/unsubscribe on [unsubscribe]. main..unsubscribe lists the commits on the branch that main does not have: one, Treat unsubscribe as cancel. main...unsubscribe diffs from where the branch started. The tested auto run showed:
src/classify.js | 1 +
test/unsubscribe.test.js | 15 +++++++++++++++
2 files changed, 16 insertions(+)
The source change is one line, after the billing rule:
+ if (t.includes("unsubscribe")) return "cancel"
The Manual run added the same line and a comment above it, // Newest rule: checked last so every older rule, billing included, wins over it.

The judge approved, and the factory printed ready:. The developer still reads the branch, because the merge is theirs.
Merge and clean up
git merge unsubscribe
git worktree remove .claude/worktrees/unsubscribe
git branch -d unsubscribe
The merge fast-forwards main, as on Day 16. git worktree remove deletes the folder, with the factory's .loop/ files. git branch -d deletes the branch, which is safe because main now holds its commit.
Check the win
git status
git log --oneline -3
git worktree list
npm test
node -e 'import("./src/classify.js").then(m => console.log(m.classify("Please unsubscribe me"), m.classify("Unsubscribe me and refund my last order")))'
Git prints nothing to commit, working tree clean. The log shows Treat unsubscribe as cancel, Add a ticket and the factory script, and Add a billing category. The worktree list shows one line: the main checkout on [main]. The tests show ℹ fail 0 and the spec's new tests on top of this morning's count: 28 on the tested auto run, 30 on the Manual run. The node line prints cancel refund.
You wrote a ticket, ran one script that turned it into a reviewed branch, read the branch, merged it, and removed the worktree.

The four stages prove the gap first and close it second. Reading the branch and merging it stayed with you.
You can stop here. Going deeper explains why this works and where it breaks.
Going deeper
This section is for readers who want to know why the judge caught what the tests missed, what each stop line means, and how a factory grows past one ticket. It assumes you finished Start here in Parts 1 to 26.
Why the judge found what the tests missed
On the tested auto run, Claude first put unsubscribe on the existing cancel line, which runs before the billing rule. A ticket such as Unsubscribe me, I was charged twice would then return cancel, not billing, which breaks the ticket's last sentence. The fix moved the check to its own line after billing.
The spec stage wrote no test for a ticket that matches both unsubscribe and billing. Both tested spec replies, saved in .loop/spec.txt, saw the case and left it to you. The auto reply ended with I didn't add a test for this, so tell me which result you want and I'll add one. A claude -p run cannot ask, and no stage of the script reads .loop/spec.txt, so the question went nowhere. The auto build's reply, in .loop/round-1.txt, even named the case: a ticket with both "unsubscribe" and "charge" or "invoice" now goes to cancel, not billing. The gate passed anyway, because no test checked it.
The judge checked the rule order in the diff against the ticket's last sentence. Its review said that The billing problem comes from reading the order of the rules in the diff, not from running it. Its first review, in .loop/review-1.txt, opened REJECT and gave the defect with the example ticket Unsubscribe me, I was charged twice. A test grades only what someone wrote down. A judge can read the ticket against the code and find a case that no one wrote down. It is still a model, not a test. The Manual judge noted the same missing billing test and called it a minor point, not a reason to reject.

Tests and the judge check different things. Tests check what someone wrote down, and the judge reads the ticket's words against the code.
The billing case is still untested on both tested branches. The fix stage was told to change only src/, so it fixed the rule order and declined the reviewer's second point, a test: Should I add it, even though it's outside src/? The exercise in Do this today adds that test.
Read the .loop/ replies before you merge. They are in the worktree, and git worktree remove deletes them.
Why the review goes back word for word
Day 25's rule holds here too: a retry must show Claude the exact thing that was graded. For the build stage, that is the gate's full output. For the fix stage, it is the review itself. The script passes $verdict, the judge's whole reply, not a summary. On the auto run, the fix reply named the order problem and moved the line exactly where the review said: It now has its own check after billing, so every older rule wins over it. A one-line summary such as "fix the rule order" would have dropped the example ticket that showed why.
Day 3's Going deeper said that a spec with acceptance criteria is something a program can check. The factory keeps that promise in two places: the spec stage turns the ticket's sentences into tests that npm test runs, and the judge checks the ticket's words that no test covers.
What the judge can do
The permission modes docs say that dontAsk denies every tool call that would otherwise prompt. The headless docs add that actions that need no approval in Manual mode still run, such as file reads in your working folders and the read-only command set. They also say that --allowedTools entries run. The judge can read and look, but it cannot edit. The CLI reference shows the same pattern, "Bash(git diff *)". The headless docs say that the space before * matters: without it, Bash(git diff*) would also match git diff-index.

The judge has two paths to a tool call, and neither one writes a file.
On the tested auto run, the first review said: I couldn't run npm test or try the billing example because Bash was denied here (the first git diff call worked, the second call didn't). The denial is the point. The judge did not run the tests. The script's gate ran them. Trust the gate's round line for test results. Both judges said the tests pass anyway, and the logs do not show where they read it.
What each line means
These lines come from the script. The tests covered only the ready: path and one REJECT.

Each line that the factory prints has a meaning and a next step. The Line column holds the exact text that the script prints, the Meaning column says what happened, and the What to do column says where to look next.
spec: no test file, or src/ changed: stopmeans that the spec stage wrote no test file or changed the source, and you read.loop/spec.txtin the worktree.red gate: the new tests already pass: stopmeans that the tests prove nothing or the ticket is already done, and you read the test file and then sharpen the ticket.build: the loop stopped, read .loop/ledger.txtmeans that one of Day 25's stop rules fired, and you read the ledger and.loop/gate.txt.review 1: REJECTmeans that the judge found a problem and the script tries one fix, and you wait forreview 2.review: rejected twice, read .loop/review-2.txt: stopmeans that the fix did not satisfy the judge, and you read the review and then fix the ticket or the code yourself.ready: branch <name>. ...means that the judge approved and the branch has one commit, and you read the branch and then merge it.
After any stop, use the reset in Start here. This guide's cleanup test ran it on a copy of the project with a stopped run's leftovers: a staged test, a change in src/, and .loop/ files, with no commit on the branch. git worktree remove --force exited 0, git branch -d deleted the branch, and git worktree list then showed one line.
The chain test removed the worktree after the merge with --force. Start here leaves it out, because the only files left in the worktree are the ignored .loop/ files. The same cleanup test backs this. In a merged worktree where git status --short --ignored printed only !! .loop/, git worktree remove with no --force exited 0, git branch -d printed Deleted branch tidy (was <hash>)., and git worktree list printed one line, the main checkout on [main].
Four loops a factory would chain
The reliable-agentic-lab workshop has four labs, each one loop with a doer, a gate, and a judge with no write tools:

Each lab in the workshop is one loop, and each one maps to a place in today's factory. The Lab column names the lab, the Loop column says what goes in and what comes out, and the Today's stage column says where that loop fits in today's chain.
- Lab 1, Ticket Enhancer, turns a vague GitHub issue into a checkable ticket, and it runs before the spec stage, where today you wrote the ticket by hand.
- Lab 2, Implementer, writes tests first, proves red, codes to green, and then runs a judge and a gate, which covers today's spec, red gate, build, and review stages.
- Lab 3, Research assistant, turns a question into a cited brief, and it is not part of today's chain.
- Lab 4, Broken PR Fixer, turns a failing pull request into a green one and leaves the merge to a person, and it fits after a pull request fails its checks.
Read them as four loops a factory would chain, not as a shipped factory. The labs do not include code that runs all four end to end.
Two tickets at once
This guide did not test it. The script names the worktree and the branch after the ticket's file name. Two tickets with different names can therefore run at the same time from two terminals in the main checkout, as Day 16 ran two sessions. Each worktree has its own .loop/ folder. Commit both tickets before you start. Two tickets that change the same lines in src/classify.js give a merge conflict when you merge the second branch, as on Day 16. Once main has moved, git merge makes a merge commit instead of a fast-forward.
A pull request instead of a local merge
This guide did not test it. With Ticket Triage on GitHub, as Day 21 set up, push the branch with git push -u origin unsubscribe instead of merging, and open a pull request on GitHub. The review then happens where your team already reviews, and a check such as Day 24's scripts/check.sh can run on it in CI. The judge's text can go in the pull request's description, so the human reviewer starts from it.
Review capacity is the limit
The factory made a reviewed branch in about a minute. Reading it took you longer. A factory that makes ten branches an hour makes ten reviews an hour, and the judge does not remove yours. The articles below argue that review, not code generation, is the limit, and that you should grow review capacity before you add more tickets. One way to spend review time well is ticket risk tiers: an agent labels each ticket by its blast radius, by whether checks can verify it, and by its impact. A high-risk ticket, such as one that touches auth or money, gets a hard look from a person.

A faster factory moves the bottleneck to you. Plan your review time before you add tickets.
Cost
Every stage is a separate claude -p call with a fresh context window. The tested Manual run made three Claude calls: spec, one build round, and one review. The auto run made five, with the fix and the second review. The CLI reference lists --max-turns and --max-budget-usd for -p runs. --max-budget-usd stops a run once its estimated spend reaches the amount you give. This guide did not test either flag in the factory.
Go further: Agentic Software Factory lays out nine loops from requirements to release, the case that verification, not generation, is the limit, and a ninety-day plan that builds review capacity first. It assumes that every change goes through a pull request with a person's approval, whereas today's factory merges on your computer.
Do this today
Everyone
- Write the ticket and
scripts/factory.sh, commit them asAdd a ticket and the factory script, run the factory, read the branch, merge it, and remove the worktree and the branch. Check the win:git log --oneline -1showsTreat unsubscribe as cancel,git worktree listshows one line,git statusprintsnothing to commit, working tree clean, andnpm testshowsℹ fail 0with at least three more tests than this morning. - Habit: read the branch and the
.loop/replies before you merge, even afterAPPROVE. Check: before your nextgit merge, you rangit diff main...<branch>and opened.loop/spec.txtin the worktree.
If you read Going deeper
-
Check the case that the spec missed. Run
node -e 'import("./src/classify.js").then(m => console.log(m.classify("Unsubscribe me, I was charged twice")))'. Check: it printsbilling, because both tested branches put the new rule after billing. This guide did not run this command. It only reads, so there is nothing to reset. Check thatgit status --shortprints nothing. -
Add the test that the judge asked for. Add these lines to the end of
test/unsubscribe.test.js:test("unsubscribe loses to billing", () => { assert.equal(classify("Unsubscribe me, I was charged twice"), "billing") })Run
npm test. Check:ℹ fail 0, with one more test than before. This guide did not run this step. Reset: rungit restore test/unsubscribe.test.js, and check thatgit status --shortprints nothing andnpm testshows the count from the end of Start here.
The merge stays with you
The factory did the steps you used to run by hand. It wrote the tests, proved they failed, looped until they passed, and had a second reader check the result. On the tested auto run, that second reader caught a rule-order defect that every test missed.
The script still stops at a branch. You read it, you merge it, and you decide what the next ticket is worth. Build the factory, then grow your review time before you grow the queue.
This is Part 26 of "Claude Code, Day-to-Day (second edition)," a day-by-day guide to Claude Code in which each day takes 20 to 40 minutes. Part 26 is Day 26. All 28 days are out now.