From toybox's lend-the-mower example (dckc/vattr97#50): its boot vat held vatAdmin, bundles, and an ingress, and also rendered the yard page and routed its requests, all reached only by the Cloudflare test, and the kernel test re-implemented its wiring. Dan pointed at agoric-sdk's habit of small vat modules and `*.exo.js` makers, and at security as extreme modularity; splitting it out gave each piece a fast test. Peeling the kernel apart one example at a time, for the same PR, showed the coupling cost: removing one feature took edits across eight files. Dan's caveat shapes the commits rule: reviewers who knew the prior state want small diffs as they go, so fold refactors into history only when the audience changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6.2 KiB
name, description
| name | description |
|---|---|
| refactor-mercilessly | Refactor mercilessly, in the XP sense: once the tests are all green, look for code that only a slow, outer test reaches, logic sitting in the module that holds the most authority, duplicated wiring, and setup a test re-implements, and pull it into small modules with tests of their own. Use when a change has just gone green; when a vat, entry module, boot vat, or handler has grown past its wiring; when code is "only tested at the cf level" or by an end-to-end test; when a test builds what production builds, by hand; when someone says "refactor mercilessly", "all green", "extract", "small vat modules", "*.exo.js", or "provideX". Not for regrouping commits (that's retcon), or for fixing a goof (that's lazy-tdd). |
Refactor mercilessly
// before: the boot vat renders HTML, routes requests, and lends the mower,
// and only the Cloudflare test runs any of it
export const buildRootObject = ({ E, Far, zone }, { vatAdmin, bundles, ingress }) => {
const esc = s => ...; const page = (cut, log) => ...; // 40 lines
const makeYard = zone.exoClass('Yard', ..., { async handle() { ... } });
return Far('boot', { async start() { /* make 3 vats, lend, serve */ } });
};
// after: the boot vat wires; the rest is modules a fast test can reach
import { lendMower, makeNeighbors } from './lend.js'; // the kernel test calls it too
import { provideYard } from './yard.exo.js'; // tested in a heap zone, fake mower
export const buildRootObject = ({ E, Far, zone }, { vatAdmin, bundles, ingress }) => {
const yard = provideYard(zone, { E, lease, fence: FENCE });
return Far('boot', { async start() { /* ensure vats, lend once, serve yard */ } });
};
Context
The tests just went green. The code works, and it was written to get there: logic landed wherever the first caller was, often the entry module, the boot vat, or a handler, the place that holds the most authority. The test that proves it is the slowest one: a whole service under workerd or wrangler.
Problem
When the tests pass, how much of the code they cover is in a shape you'd want to keep?
Forces
- Green is when it's safe to restructure, and the cheapest time, while the code is fresh. Later, every caller and reviewer has learned its shape.
- Security is extreme modularity (see pola). A module that holds vatAdmin, bundles, and an ingress, and also renders HTML, gives every line of that HTML the module's authority. The module holding the most authority should have the least code: wiring that a reader can check by eye.
- Code reached only by an end-to-end test is tested slowly and coarsely. A goof in it shows up as "the yard page is wrong", not as which function is wrong.
- A test that builds by hand what production builds (makes the three vats, then lends the mower) tests its own copy of the wiring. Production's copy can drift untested.
- How hard a feature is to pull out measures its coupling. If peeling one example off a kernel takes edits in eight files (kernel, supervisor, config, types, client, test harness), its seams are in the wrong place.
- Bundles, vats, and other isolation boundaries can force a copy: a boot directory's bundle can't import a helper from outside itself.
- Reviewers who followed along know the prior state, and read a small diff against it easily. A big restructure, folded into history, makes them reread everything.
Therefore
At each green, before moving on, move logic out of the modules that hold authority into small modules that hold none, each with a test as fast as it can be. Then reach for green again.
- Pure functions (rendering, parsing, arithmetic) go in their own module, tested in plain node.
- A kind of durable object goes in
<name>.exo.jsasprovide<Name>(zone, powers)(orprepare<Name>, which answers the maker), taking only the powers it uses. Test its behavior in a heap zone, with fakes made in the same zone: fast, and no vatstore. A heap zone can't show durability (state that persists, an object that outlasts an incarnation), so add one test under a durable zone (liveslots over an in-memory vatstore) that makes a second incarnation over the same vatstore. The heap zone must share the durable zone's defaults, such as what a kind given no guard checks, or the two disagree on the same code; and both keep the library's. When a fast test seems to need a looser default, look for what differs in the test's environment: Node's async_hooks ids on promises, not the kind, made async results fail the passable check. - Wiring that a test also does goes in a function both call, such as
lendMower(E, makeVat, fence), so the test exercises production's wiring. - What's left in the vat, entry module, or handler is wiring: get the powers, call the modules, hand out the results. One slow test covers that it's wired.
- A copy that a boundary forces gets a comment saying where its twin is, until there's a way to share it (a capability, or a library in the bundle).
- Commits: a refactor is a commit of its own, and so is a behavior change, each green, and each pushed when made. While someone is following along, keep them separate, so each one diffs against a state they knew. Fold them into the history (retcon) only when the audience changes to readers who never saw the prior state, or when asked.
Resulting context
The modules that hold authority shrink to wiring, and the logic sits where a fast test reaches it, so a goof names its own function. The next feature adds modules and a line of wiring rather than edits across the kernel. There are more files, and an import graph to learn; a reader starts at the wiring and follows its imports.
A refactor still risks behavior. Keep it to code the tests reach, and when a refactor needs a new test to be safe, write that test first (lazy-tdd).
Sources
- Refactor Mercilessly: the c2 wiki's XP page. Refactor whenever you see the chance, so the design stays simple as it grows.
- From E to EcmaScript and Back Again: Mark S. Miller, QCon SF 2010; slide 3 is "Security as Extreme Modularity".