series · from legacy to agent-ready · post 2
Working Backward from Three Yeses: The Map of the Series
Start from the end
The first post left you with a table: every component you own, three questions, mostly noes.
Can you merge a change an agent made to it without reading every line? Can you get that change implemented from one prompt, or does it take ten? Can you release that change without anyone watching production?
The natural next move is to ask "what should we buy, or install, or adopt?" Resist it. Every roadmap I have seen starts with the tools, ends with the tools in place, and leaves the three answers unchanged. The roadmaps that worked started from the other end: from the day the team could stop reading the diffs, and asked what had to be true the day before that. Then the day before that.
This post does that, all the way down. It maps the series. Most of the posts that follow work one leaf of this tree, and the posts come in the order they do because of the tree's dependencies, not my preferences.
The method: a goal tree, built backward
Working backward needs one question and one stopping rule.
The question is: what would have to be true for this to be a yes? You ask it of a goal and get sub-goals. You ask it of a sub-goal and get smaller ones.
The stopping rule is: stop when the answer is one piece of work with a check you can run. "Tests that survive a rewrite" is not a leaf, because you cannot tell on Monday whether you have it. "The mutation score for this component is above the gate, and every survivor is classified" is a leaf. (Mutation testing plants small faults in the code and counts how many the tests catch; more on it below.) You can run it. It is also, not by coincidence, the kind of thing an agent can run for you.
Two warnings before the tree. First, it is for one component, not a system. Pick the component that hurts most, or the one where an agent could help most, and draw its tree. Keeping many trees honest at once is a problem for later. Second, the tree is not a project plan. Branches are drawn as independent because they are logically independent. The order you work them in comes later, from the dependencies between leaves.
None of this is new, and I did not invent it. Working backward from a goal comes from the Theory of Constraints school again: H. William Dettmer's Goal Tree, built on Goldratt's Thinking Processes, asks what must be true for a goal, then what must be true for that, until the answer is something you can act on. Gojko Adzic's Impact Mapping applies the same backward walk to product goals, through the people whose behavior has to change. The Mikado Method applies it to code: start from the change you want, find what blocks it, and work back to the prerequisites that nothing blocks. I am only applying it to the three yeses.
The tree
GOAL A Merge an agent's change without reading every line
├── A1 There is a definition of right
│ └── rules with an ID and a reason, each traced to a check
├── A2 There is an oracle the change cannot fake
│ ├── behavior tests at a stable boundary, not in the guts
│ ├── the code can be put in a harness (seams; the clock, random numbers and the network faked)
│ ├── the existing tests do not lie (test smells found and fixed)
│ └── the suite is verified by mutation testing, survivors classified
├── A3 There are boundaries the change cannot cross
│ ├── a machine-readable contract, with one owner per piece of data
│ ├── the hidden clients are counted
│ └── fitness functions fail the build on a crossed boundary
├── A4 The agent cannot edit the judge
│ └── tests, contracts and decisions are read-only to an implementation change, enforced in CI
├── A5 Every check fails the build
│ └── lint, types, static analysis, security scan, coverage threshold: none can be ignored
└── A6 The reviewer reads evidence, not diff
├── an evidence pack per change
├── deterministic checks first, judgment calls second, synthesis last
└── risk tiers decide which changes merge on the pack alone
GOAL B Implement a change from one prompt
├── B1 The rules are written where the agent reads them (shares A1)
├── B2 The boundaries are written where the agent reads them (shares A3)
│ └── one vocabulary, one meaning per term
├── B3 The reasons are written down
│ ├── the history is reconstructed (what changed, when, why, with confidence)
│ └── every scar has a decision record with its rejected alternatives
├── B4 The repository says how to work
│ └── one short file: the commands, the conventions, always / ask first / never
├── B5 The build gives green or red in one command
│ └── a deterministic, headless build, runnable in a sandbox
└── B6 There is a way to stop
└── one escalation format, and a person to escalate to
GOAL C Release the change without anyone watching production
├── C1 The deploy is automated and reversible
│ └── one step forward, one step back, a smoke test in between
├── C2 Telemetry is linked to the rules
│ ├── technical signals: errors, latency, anomalies
│ ├── business signals: the numbers the change was meant to move
│ └── an alert names the rule it protects, not only the metric that moved
└── C3 Rollback does not wait for a meeting
└── a breached threshold, technical or business, restores the previous version; the cause goes to the specification
Three goals, fifteen sub-goals, and a little over twenty leaves. Here is what the branches mean and, more usefully, what they depend on.
Goal A: merge without reading
A1, a definition of right. A check can only say "wrong" against something. If the only reference is the old code, every check is a regression test against yesterday, and yesterday is not the truth. The leaf is a specification: rules with identifiers and reasons, each traced to a test. On legacy code, you do not write this first. You recover it from the tests you write for the oracle, and you give every recovered rule a status: observed, accepted, or contested. That ordering matters and I will come back to it.
A2, an oracle the change cannot fake. This is the branch with the most leaves because it is where legacy code fights back. Behavior tests at a stable boundary depend on being able to put the code in a harness at all, which depends on breaking the dependencies that stop you: the smells that prevent testing (the catalogue I use, built on Feathers's work, has eleven), and the three dependencies that defeat everyone: the clock, random numbers, and the network. If tests already exist, they need auditing first, because a suite that asserts mocks were called protects nothing. And when the tests exist, mutation testing tells you whether they would catch a real fault. The dependencies run strictly downward: smells before seams, seams before tests, tests before mutation.
A3, boundaries the change cannot cross. A contract, an owner for every piece of data, and a census of the hidden clients: the mobile app that still calls the old route, the team that reads your file. Then fitness functions that fail the build when a dependency points the wrong way. This is where work on the code starts, not where it ends. Tests written anywhere but at a boundary validate the implementation that is there, and they die on the first real change. So you find the boundaries first: the API its clients call, the external systems it integrates with, the data it owns. In much legacy code they are not explicit at all, because input and output sit in the middle of the logic. Then the first changes are the unsafe ones: pushing I/O out to the edges until there is a boundary to test against, protected only by slow end-to-end tests that compare input and output from the outside. The written contract, the client census, and the fitness functions can be completed later, once the tests show what really crosses each boundary.
A4, the agent cannot edit the judge. Everything above is worthless if an implementation change can modify the tests to go green. The leaf is mechanical: protected paths, a CI check that fails any implementation change touching an oracle file, sandbox permissions. Prompt text does not count. It depends on nothing except the oracle existing, and it must be in place before the first controlled prompt under goal B.
The core of it fits in one line of CI. Git already knows which files a change touches, so the check is deterministic: the same change gives the same answer every time.
# Fail a change that edits the oracle and the code at the same time.
oracle="spec/ tests/acceptance/ contracts/ decisions/"
touches_oracle=$(git diff --name-only origin/main...HEAD -- $oracle)
touches_code=$(git diff --name-only origin/main...HEAD -- . ':!spec/' ':!tests/acceptance/' ':!contracts/' ':!decisions/')
if [ -n "$touches_oracle" ] && [ -n "$touches_code" ]; then
echo "This change edits the oracle and the code together. Change rules and tests in their own reviewed change."
exit 1
fi
Git lists the files a change touches, so the build fails before anyone reads a line whenever one change edits both the oracle and the code. Changing a rule or a test on its own still passes: it has to arrive as its own change, reviewed by a person, never inside the change that has to pass it. Pair it with a CODEOWNERS entry on the same paths, so that the separate change needs the right reviewer, and with sandbox permissions, so that the agent cannot write there in the first place.
A5, every check fails the build. The cheapest branch and usually the first one I do, because it needs no understanding of the code. Linting, types, static analysis, security scanning, a coverage threshold, each ratcheted from "runs somewhere" to "cannot merge". It depends only on the one-command build.
A6, evidence instead of diff. The branch that actually changes the reviewer's day. An evidence pack per change (every gate, the mutation score, the agent's list of what no test reaches, any escalation), three tiers of review (deterministic rules, calibrated judgment calls, synthesis), and risk tiers that say which changes may merge on the pack alone. It depends on the oracle, the boundaries, the read-only judge, and the failing gates all existing, because a pack of missing evidence is just paperwork. And the auto-merge part depends on goal C, which is the point of goal C.
Goal B: one prompt
Goal B shares its first two branches with A. I come back to what that means below, because it is the most important thing the tree shows.
B3, the reasons are written down. The corrections you type most often are "no, that timeout is there because…". The leaves are archaeology and decision records. Archaeology, reconstructing what changed and when and why from git and comments, is read-only and safe on the first day, which is why it is near the start of the series despite sitting under goal B. The decision records come from it: one per scar, with the alternatives rejected, numbered with a timestamp and not a sequence because agents and branches collide.
B4, the repository says how to work. One short file with the exact commands, the conventions that differ from the language defaults, and three lists: always, ask first, never. Short because every line costs attention on every session. It depends on the one-command build (there have to be commands to write down) and on the conventions being known, so it comes late in the series even though it feels like a day-one task.
B5, one command for green or red. The root of everything. You can't check any leaf in the tree without it. Deterministic, headless, runnable in a sandbox from a clean checkout. In my experience, it's also the step most often skipped in writing about agents, because the examples are built on code that already has it.
B6, a way to stop. When the agent finds the specification contradictory or incomplete, it needs a format to say so and a person to say it to. Otherwise, it improvises, and corrective prompts come from improvisation. One escalation format, agreed once.
Goal C: release without watching
Goal C has the fewest leaves and the largest effect on goal A, because it is what makes merging on evidence defensible. The evidence will miss things. The question is who finds out: a customer, or the system.
C1, a reversible deploy with a smoke test that fails the release. C2, telemetry linked to rules, and the telemetry has two kinds of signal. The technical kind: errors, latency, anomalies in the shape of traffic. And the business kind: the numbers the change was meant to move, or must not move, such as orders completed, sign-ups, or payments that go through. A change can be technically green and still be wrong, and only the second kind of signal tells you. Either way, the alert says which rule was violated, not only which metric moved, so a person or an agent can act without a diagnosis first. C3, rollback without a meeting: a breached threshold, technical or business, restores the previous version, with the root cause routed to the specification and never straight to a code patch.
Goal C depends on nothing in A or B. You can build it on a legacy component before a single test exists, and some teams should. But merging on evidence depends on it, which is why the series puts it just before the review tiers.
Written once, used twice
Look back at the tree and notice that two branches appear under both A and B. The definition of right appears under the first goal and again under the second. So do the boundaries. That is not a drawing shortcut. It is the most important thing the tree shows.
The same files do two jobs. Read one way, the specification is the judge: a check names the rule it enforces, and a change that breaks the rule fails. Read the other way, it is the input: the agent reads the rule before it writes a line, and does not have to be told it in a prompt. The contract is the same. It is the fence the change cannot cross, and it is the description of the fence that the agent reads so that it does not try.
This is why teams that write specifications and contracts "as documentation" get neither benefit. A document that nothing checks does not judge, and a document the agent is not pointed at does not inform. The leaves under the definition of right and the boundaries are written so that both uses hold at once: rules with identifiers that tests cite, and contracts in a form a tool can validate, and an agent can read.
It also explains why the same work pays twice. Every rule you recover from a characterization test, every decision you record, closes one gap in the judge and removes one corrective prompt from the agent's future. That is the leverage the whole series runs on.
What the dependencies give you: the order
Lay the leaves out by what they depend on, and an order falls out. It is not the only valid order, but every alternative I have tried violates a dependency.
- Diagnose. Classify the component (business domain, integration, or plain CRUD; the depth of everything below depends on it), pick the one that goes first, and run the read-only archaeology. Nothing is changed yet.
- Build the safety net. The one-command build first, because everything needs it. Then the gates that fail the build, because they are cheap. Then find the boundaries, and make them explicit if they are not, pushing I/O to the edges under slow end-to-end tests. Then the oracle, at those boundaries, in its own order: find the smells, break them with seams, fake the clock, random numbers, and the network, audit the tests that exist, characterize at the boundary, verify with mutation.
- Write down what the code knows. Complete the boundaries in writing: the contract, the hidden clients, the data owners, the fitness functions. Recover the rules from the tests, with each rule's status. Turn the archaeology into decision records. Write the repository's one-page guide for agents.
- Hand over. The first controlled implementation prompt on a low-risk change, with the judge made read-only before it runs and the way to stop agreed. Build the reversible deploy, the rule-linked telemetry, and the automatic rollback. Refactor in slices small enough to verify, from the edges inward. Then the evidence pack, the review tiers, and the risk tiers that let the first changes merge unread.
- Prove and scale. Regenerate the component from its files alone and see what the regeneration gets wrong; that list is what you still have not written down. Then keep the ledger that says, per component, which leaves are done and which yeses are earned.
Under this order runs a second one, for the agent. It is first allowed to read (step 1), then to write tests and the boundary refactors that the end-to-end tests protect (step 2), then to write documents (step 3), then to write code under a judge it cannot edit (step 4), and only then to merge without a line review (step 5). Each step raises the agent's autonomy exactly as far as the verification built in the previous step allows, because autonomy is bounded by verification capacity, not by generation capacity. That sentence is the whole series. Rung by rung, it is the move from in the loop to on the loop.
What I would do in your codebase this week
Take one component from last week's table, the one with three noes that matters most. Draw its tree. Use mine as the template and cross out any leaf that doesn't apply; a CRUD service doesn't need all of them.
Then mark every leaf as done or not done. Be strict: "we have a linter" is not done unless the linter can fail the build; "we have tests" is not done unless you know their mutation score.
Now look at the undone leaves nearest the roots: the one-command build, the gates that fail the build, and the archaeology that recovers the reasons. Those are your first three milestones, and none of them requires anyone to understand the code yet. That is where I would start on Monday.
One warning before you do: the shortcut every team reaches for first is to skip the tree and ask the agent to rewrite the component. Resist it.
Let's make it engineering.
Sources
- Pedro Santos, Pedro's Law: Specifications First, Code Second. The specs loop, the implementation loop, read-only oracles, and escalation.
- Michael Feathers, Working Effectively with Legacy Code (2004). Seams, the smells that prevent testing, characterization tests.
- Chad Fowler, The Phoenix Primitives, The Deletion Test, Evaluations Are the Real Codebase, and Compile to Architecture, in the Phoenix Architecture series. The four primitives, the regeneration test for checks, and data ownership as the reliable seam.
- H. William Dettmer, The Logical Thinking Process: A Systems Approach to Complex Problem Solving (2007). The Goal Tree, a version of Goldratt's Thinking Processes.
- Gojko Adzic, Impact Mapping: Making a Big Impact with Software Products and Projects (2012).
- Ola Ellnestam and Daniel Brolund, The Mikado Method (Manning, 2014).
- Addy Osmani, Software Factories, Light and Dark. Verification capacity bounds autonomy.
- Pedro Santos, Marco Consolaro, Alessandro Di Gioia, Agile Technical Practices Distilled. The legacy-code chapters.
How this was written: I used AI heavily to draft, edit, and polish this article. The ideas are mine, and so is the responsibility for every word. Why I am unapologetic about it.
Want help taking your codebase there?
Let's talk