Software Factory Part 2: Red-Green TDD an Agent Can't Fake

Table of contents

Part 2 of 4 in the software factory series. Part 1 covers onboarding a repo. To run it on your own repo, start at the Software Factory product page.

The obvious risk with a coding agent isn’t that it writes bad code. It’s that it writes code, writes tests, and then tells you the tests pass. In a software factory nobody watches each build, so that claim has to be checked by something other than the agent that made it.

This post covers the first three stations of the line: the builder that works red-green, the check the factory runs on its work, the screenshots, and the agent that reviews the PR.


Picking Up a Ticket

A ticket enters the line when it’s in To Do and has the factory-enabled label. The label is an opt-in, so the factory never picks up a ticket you didn’t mean to give it.

The orchestrator moves the ticket to In Progress, comments that the build has started, clones the repo onto a branch named factory/KAN-4, installs dependencies, and starts the builder:

claude -p --agent ticket-builder --output-format stream-json --verbose \
  --max-turns 150 --mcp-config run.mcp.json --strict-mcp-config \
  --allowedTools "Read,Edit,Write,Bash,Grep,Glob,Skill,mcp__atlassian__*,mcp__playwright__*" \
  --model opus

The prompt goes in on stdin, because diffs and tickets can exceed the shell’s argument size limit. The Atlassian tools only appear if you turn on the optional Rovo MCP server. The builder and reviewer run on Opus; the screenshot and verifier agents run on Sonnet, and each role’s model and turn budget are set in factory.config.yml.

The prompt carries the ticket text, the recipe for the ticket’s mode and the repo’s commands. It tells the builder to follow CLAUDE.md and use the red-green skill, which spells out exactly which commits to make:

  1. Write the tests. At least one per acceptance criterion, and they must fail because the feature is missing, not because of a typo.
  2. Commit them as red. Run them, confirm they fail, and commit with the trailer Factory-Phase: red.
  3. Make them pass. Write the smallest implementation that passes the full suite, and commit it with Factory-Phase: green.

Optional clean-up commits after that are marked Factory-Phase: refactor, with the full suite rerun before each one. The red commit may never be amended or rebased.


The Factory Checks the Claim Itself

Then comes the important part. A builder can say “all tests pass” in its summary. The factory ignores the claim. The orchestrator checks the TDD claim itself, in plain code. Simplified from redgreen.ts:

const reds = await d.redCommits();          // commits with "Factory-Phase: red"
if (reds.length !== 1) return fail("exactly one red commit required");
const green = await d.head();              // HEAD, after any refactor commits
if (green === red) return fail("no implementation after the red commit");
await d.checkout(red);  await d.install();
if ((await d.test()).code === 0) return fail("tests passed at the red commit");
await d.checkout(green); await d.install();
if ((await d.test()).code !== 0) return fail("tests fail at HEAD");

Before that, it checks the changed files against the protected paths. The ticket is blocked if the tests pass at the red commit (so they don’t prove anything was missing), if they fail at the end, or if the change touches a protected path such as vercel.json or .env. A blocked ticket gets a factory-blocked label and a Jira comment explaining why.

Every failure blocks, including errors nobody planned for, so a broken ticket can’t burn tokens in a retry loop. To retry, you remove the label.


Catching a Loosened Test

The first version of that check had a gap, and the first real ticket found it. It checked that red fails and green passes, but not what changed in between.

On KAN-4, voice input for this site’s command palette, the green commit had edited one of the red tests, changing the expected text from “download resume” to just “resume”. The test still failed at red and passed at green, so the factory was satisfied. Only the reviewer agent noticed.

A builder that gets stuck can quietly loosen its own test until it passes. That now gets flagged, but it doesn’t block the ticket, because some edits are legitimate. Instead, the PR and the ticket both get a “check this” note with the exact git diff to run.


Screenshots as Evidence

Once the green commit is checked, the orchestrator starts the app and runs a small agent, evidence-capture. It has one tool, the browser, and can’t edit anything. It takes a screenshot of each criterion’s state, plus full-page overviews at 1280px and 390px wide.

The screenshots are committed to the PR branch under .factory/evidence/<KEY>/ in their own commit (Factory-Phase: evidence), embedded in the PR description, and attached to the Jira ticket. GitHub has no API for uploading images to a PR, so committing them to the branch is the practical way to show them in the description.

The first run had no screenshots in the PR at all. Playwright MCP saves named screenshots in the agent’s working directory, not in its --output-dir. The fix was to run the evidence agent from inside the screenshots directory.


An Agent Reviews the PR

The orchestrator pushes the branch, opens the PR, moves the ticket to In Review and adds the factory-pr-open label. The next poll picks it up for review.

The pr-reviewer agent has Read, Grep and Glob. It gets the ticket, the diff, and the PR branch checked out so it can read the surrounding code. It checks, in this order:

  1. Coverage of each criterion. Is there a test that would fail if the criterion weren’t met? A test that only checks for a success message doesn’t count.
  2. The rules in CLAUDE.md. It quotes the rule each change breaks.
  3. Correctness.
  4. Security.
  5. Scope.

It replies with JSON: a summary, a coverage table, and comments tied to a file and line. The orchestrator posts them as a GitHub review of type COMMENT.

The reviewer can’t approve a PR or request changes; a human decides. The review is there to make your read of the PR shorter and better aimed, not to replace it.

On the first run, GitHub rejected the whole review with HTTP 422. The reviewer had cited line 813 of a 128-line file, and one bad line fails the entire review. Now every comment is checked against the lines GitHub will accept, the added and context lines in the diff. Valid ones post inline, and the rest go in the review body. If GitHub still returns a 422, the factory retries with every comment in the body.


What the Review Found on KAN-4

The reviewer left 8 comments in 1.7 minutes, for $0.56. Three of them were real issues:

  • the two-second silence timer started before anyone had spoken;
  • after a blocked-microphone error, the mic stayed disabled for the rest of the page’s life;
  • the green commit had loosened one of the red tests, as described above.

Another cited a line that didn’t exist. The review was worth reading, but not worth obeying blindly, which is exactly the role it’s designed for.


Limits

  • Red-green proves less than it sounds. It shows the tests failed before and pass after. It doesn’t show that they test the right thing. That’s the reviewer’s and the verifier’s job, and the reason a human still merges.
  • The review runs once per PR. Fixes pushed after it aren’t reviewed again.
  • Reviewers invent things. A line number that doesn’t exist is easy to catch. A confident wrong claim about behaviour isn’t, so the review informs your read of the PR rather than replacing it.

Key Takeaways

  • Check the TDD claim, don’t trust it. Rerun the tests at the red commit and at the end, in code the agent doesn’t control.
  • Check what changed between red and green. A loosened test still goes red then green.
  • Block on every failure. A broken ticket shouldn’t burn tokens in a retry loop.
  • Give the reviewer a narrow job. Read-only tools, comments only, no approval.
  • Validate agent output against reality. A single invented line number failed a whole GitHub review until the factory checked it.

Next: Part 3: A Verifier That Never Reads the Code, on the agent that checks the merged result without ever reading the code.

esc