Building a software factory that learns from its own mistakes
How I combined two habits, agentic coding and a template for the parts every app needs, into a factory: what the first version got wrong, what I changed, and how every mistake now goes back into the template.
For a long time I have been doing two things side by side. I wrote code with agents, and I kept a template repository, in more than one framework, because almost every app I built needed the same parts. At some point it only made sense to combine them. This is the story of that factory: the first version, what broke, what I changed, and the loop that keeps it improving.
Why build a factory
Most business apps share the same foundation. Invite-only login, people with roles, an audit log, searchable lists, two languages, e-mail and push notifications, and tests on every layer. Each app then adds its own entities and rules on top: customers, planning, hours, invoices.
I was rebuilding that foundation every time, and I kept relearning the same lessons. The template took care of the first part. Agents could take care of much of the rest, if I gave them something solid to stand on.
The first version
The first version was one big agent, a template and a prompt. There were no clear guidelines and no issue tracking. It got surprisingly far, and then it got lost: work that was done and work that was half done looked the same, the same thing was solved twice in different ways, and I could only tell what it was doing by reading along.
Everything since then has been about replacing hope with structure, without taking away what agents are good at. Four changes did most of the work:
- Rules next to the code. The template says how to extend it, in files the agents read, and the guidelines are fixed rather than rewritten per app.
- A deterministic generator. Everything predictable is written by a script from a typed spec, once.
- Issues and a lead session. Work is cut into issues on a board, and one coordinating session hands them to build agents and merges the results.
- Independent reviewers. An agent does not get to call its own work done.

One spec, generated once
Everything starts from one typed file, app.spec.ts. I write it together with an agent and approve it myself; it is the one place where what the app should do gets decided. It names the entities, their fields, which role may read or write what, and the screens.
export default defineApp({
name: 'Example app',
roles: { is_planner: 'Planner', is_finance: 'Finance' },
entities: {
customer: {
fields: {
name: text({ required: true, max: 200 }),
payment_terms_days: int({ min: 0, max: 120, default: 30 }),
billing_email: email(),
},
read: ['is_planner', 'is_finance'], write: ['is_planner'],
screens: { list: { columns: ['name'], search: ['name'] }, detail: true, form: true },
},
},
});
A field kind is a rule from the template, written as a declaration. money is always integer cents, never a float. choice is a shared vocabulary table with a picker, never free text. Each kind knows its database column, its CHECK constraint, its form field, its list cell, its formatter and its test values.
From that spec the generator writes, per entity:
- the migration, with row-level security from the first line
- one save and one delete function, the only way to write, each writing its audit entry in the same transaction
- database tests, rows in the access matrix, and a performance measurement
- the list page, the record page, the form, a command-palette group, error messages and every text in every language
- unit, integration and end-to-end tests, and demo data
It generates once. After that the generated code is ordinary code, which agents extend like anything else. I first built machinery to regenerate code while keeping hand edits, then deleted it. Keeping two sources of truth in sync cost more than it saved.
The lead session and its agents
The build is run by one coordinating session per app. It picks ready issues from the project board, starts a build agent for each, and merges one reviewed PR at a time. It tells me about something only when a person is needed. GitHub holds all the state: the board, the issues, the PRs and a report issue per run. There is no orchestration framework, no planner script and no state file. Restarting the lead loses nothing.
Each agent works in its own slot. A slot is a git worktree, an isolated local Supabase stack and its own port. Three agents can build, migrate and run their test suites side by side without touching each other's data. A laptop runs at most three at once.
Model by risk. The lead plans on the strongest model. Normal issues are built on a faster, cheaper model. Anything that touches security, row-level security, auth, money, contracts or migrations of existing tables is built on the strongest model from the start.
Escalation is a table, not a judgement call:
| When | Normal issue | Critical issue |
|---|---|---|
| The same step failed twice | move to the strong model | n/a |
| A blocking finding after 3 review rounds | move to the strong model | n/a |
| The diff touches a high-risk area | move to the strong model | n/a |
| The same step failed 3 times | stop, ask a person | stop, ask a person |
| A question only a person can answer | stop, ask a person | stop, ask a person |
Every PR passes a review loop. The agent runs code review rounds until a round has no critical or major finding, at most three rounds. A major still open after that becomes its own issue, named in the PR. Security, access-control, data-loss and migration findings are never capped: those get fixed however many rounds it takes. A separate security review runs on the same diff before any merge.
Open questions never block a build. Each question in the spec comes with a default. The agent builds the default and logs the question, and a person confirms or changes it later.
Guardrails: why I can trust what agents build
Agents make confident mistakes, so the factory is built to make mistakes loud and to make the dangerous ones impossible.
- The database holds the rules. Every table has row-level security from the migration that creates it. Every write with a business rule goes through one function per entity, and the role is always looked up server-side, never taken from the client. A UI bug can't skip a rule because the UI never enforces one.
- A completeness test. Every table, view and function must appear in an access matrix and be covered by database tests. Add a table and forget its policy, and the build fails.
- Hooks that block, and hooks that inform. Two early failures were tests that quietly got skipped, and bugs where an agent changed a function without knowing everything else that touched it. Hooks now put that context in front of the agent before it edits, and refuse the actions the rules forbid before they run. That includes reading secret env files, editing a migration that is already on the main branch, pushing straight to the hosted database, broad process kills, and git operations in the main checkout while agents are working.
- Conventions are lint errors. No hard-coded colours, no raw date formatting, no untranslated text, no dead code. An end-to-end test also checks that no page overflows at desktop or phone width. A rule a machine can check is not left to review.
- Every test layer, on every change. Unit tests, database tests, an integration suite against a local stack, and browser tests. "Done" means the full check passes, not that the agent says it is finished.
The factory learns from every project
Every project ends with a list of what went wrong, and that list changes the template before the next project starts. This loop matters more than any single feature of the factory.

Record. While a run goes, a local working board logs every issue's status changes, every review round with its critical, major and minor findings, and every pause with its reason. In one run of one app that came to 93 issues and 148 review rounds: 3 critical, 182 major and 737 minor findings. The run paused 18 times: 11 on usage limits and 7 on a hung Docker VM.
Group. At the end of the run, the coordinator turns that log into a list of patterns rather than incidents. Each entry has four parts: what happened, which issues it hit, what it cost in rounds or hours, and the proposed fix. Only generic lessons go in. Anything specific to one app stays in that app.
Decide and change. I read the list and decide what goes into the template. The template keeps its own list of improvements, fed by four projects so far, and works through it in waves. Each lesson lands as the strongest thing that fits: a database rule, a lint error, a check script, a hook, or one line in an agent's brief.
What went wrong, grouped
These are the patterns from four projects, ordered by what they cost. Each one ends with what it changed in the factory.
1. Review loops that don't converge
One invoicing issue ran 25 review rounds with 162 findings and took about 12 hours of agent time, most of it after round 10. The issue bundled several hard problems into one migration: gapless invoice numbers, credits per VAT group, and bundles. Each fix opened new edge cases. Shared rules it should have followed turned up halfway through the review.
What changed: the loop is capped at three rounds, and leftover non-blocking majors become their own issues. Security, access, data-loss and migration findings stay uncapped. An issue that mixes several hard problems is split before anyone builds it, and the hard rules go in the agent's brief before the first line is written.
2. An agent's own review is not enough
On the money-related PRs, the agents' own review rounds kept calling the work clean. An independent review then still found majors: lost updates, hidden characters, missing locks. One PR had four majors after nine rounds whose last two had been clean.
What changed: an independent review on high-risk PRs, with a checklist built from the most frequent kinds of finding: concurrency, data integrity and authorization.
3. Concurrency
A new money function deadlocked with existing planning and hours functions, because each one locked rows in its own order. A separate race let a setting be switched off between a check and the write.
What changed: one global lock order, locks taken at the top of the function, and a setter that a check depends on takes a lock that conflicts with that check.
4. The same rule, implemented twice
Agents wrote a second VAT calculation, a second retry loop and a second text normalizer. A helper already existed in each case, but nothing listed them, or a parallel agent's helper wasn't merged yet.
What changed: a page of shared helpers ("for this, use that"), kept honest by a test. Agents check it before building. An issue that needs another agent's unmerged helper waits for it instead of writing a stand-in.
5. Parallel work collides
Nearly every PR had to renumber its migration once or twice as the base branch moved, about 55 minutes over five issues.
What changed: a script renumbers a branch's migrations and lists the functions they re-create that the base has changed since. It runs before every PR.
6. Tests that depend on other tests
Database tests and browser tests share a stack, so a test that counted "all rows" failed after the browser tests had added some. Picking the "latest" row by creation time failed when several rows were created in one transaction.
What changed: every database test scopes to its own fixture ids and never assumes an empty table.
7. Bugs every new app hit
Tests pinned styling instead of behaviour. Tooling broke when someone rewrapped a line. The dev server rewrote a config file. The template's own colour palette failed nine contrast pairs.
What changed: tests pin behaviour, and a contrast check runs on every colour pair in light and dark mode. Token, icon and theme generation became scripts.
8. The laptop is a bottleneck
Three agents, each with its own database stack and test runs, overloaded the machine. In one run the factory paused 11 times on usage limits and 7 times on a hung Docker VM.
What changed: per-slot caps on test workers and build CPUs, slimmer database stacks, and agents stopping their stack once their PR is open.
9. Process lessons
- A separate reviewer agent added a hand-off without adding quality, so the builder reviews its own work and the independent review is kept for high-risk changes.
- Copying my manual coordination workflow as it was beat designing a new harness.
- I couldn't see what the lead was doing without reading the terminal. That led to a local working board that shows what needs me, the questions waiting, the agents' lanes, and how long each issue took.
Still open
The loop runs with a lag. The round cap, the migration renumbering, the test-isolation rule, the laptop limits and the working board are in the template. The lock order, the shared-helpers page and the independent review have proven themselves in the project where they came from, but haven't moved into the template yet. Still to come: a check that fails on unscoped row counts in database tests, and a copy-paste detector.
The factory runs on a laptop for now. Nothing in it assumes that, and cloud sessions are the next step.
What I'd tell someone building one
- Put the rules where agents can't skip them. In the database, in lint rules, in hooks. A rule that lives only in a prompt is a suggestion.
- Let people own the decisions. Agents are good between decisions, not at making them. Give every open question a default so nothing waits.
- Measure every review round. Without counts I would have called the 25-round issue "a hard one". With them, I could see that most rounds after round 10 were minors, and that the issue should have been three issues.
- Keep it deterministic, but don't fight the agents. Script what is predictable. Forcing scripts onto the work agents are best at breaks the part that makes them worth having.
- Treat every project as input. The most valuable output of a project is the list of what went wrong, and it's worth something only once it changes the template.
By the numbers
| Template history | ~480 commits, 151 merged PRs, 184 closed issues since February 2026 |
| Test suite | ~5,600 unit tests, 57 database test files, 36 browser specs |
| Database | 92 migrations, every table in the access matrix |
| One run of one app | 93 issues, 148 review rounds; 3 critical, 182 major, 737 minor findings |
| Projects feeding the template | 4 |