Plan mode: from a spec to acceptance criteria to a failing test
By the end of this lesson you will have a new function, sortByMove, in the starter, written only after you approved a plan, with a test file whose tests you watched fail before the code existed and pass after it did — and not one API call spent on the way.
The idea
An agent asked for a feature starts typing, and fills every gap in your request with a guess you meet later as code. Plan mode puts the thinking first.
What plan mode does
In plan mode the agent reads files and explores, then writes a plan, and it does not edit your source until you approve that plan. Both agents get there the same two ways:
- press
Shift+Tabuntil plan mode shows — the status bar says plan mode on in Claude Code, the line under the prompt says Plan mode in Codex; - start a prompt with
/plan.
Claude Code can also start a session in it, with claude --permission-mode plan.
There is one difference worth knowing. Claude Code enforces plan mode: it cannot edit until you approve. In Codex, plan mode is an instruction the model is told to follow, not a lock, so plan in Read Only, where an edit still has to wait for your yes.
When the plan is ready you choose:
| Claude Code | Codex | |
|---|---|---|
| Build it | approve and let it edit, or approve and review each edit yourself | Yes, implement this plan — in Read Only, each edit still waits for you |
| Change it first | keep planning and say what to change, or Ctrl+G to edit the plan in your own editor |
No, stay in Plan mode, and say what to change |
While you are learning, choose the way that has you approve each edit.
Spec, criteria, test
A plan worth approving has three layers, and each turns the one above into something more checkable:
spec "order the watchlist by how much each instrument moved"
↓
acceptance criteria sentences you can be wrong about, one per behaviour
↓
tests one per criterion, written first, failing first
↓
code the least that makes them pass
The spec is one sentence about what you want. Acceptance criteria are what "done" means for it, written so that each one is plainly true or false. For this feature:
- A row that moved −3% comes before a row that moved +2%: size of move, not direction.
- Rows that moved by the same amount keep their watchlist order.
- The list passed in is unchanged afterwards.
- An empty list gives an empty list.
- No new request: the cost of a page load stays what it was.
Look at the third. A plain rows.sort(...) reorders the original array in place — the trap from Reading a diff — the skill everything else rests on. Caught here it costs a sentence; in production it is a bug nobody can explain.
Tests turn each criterion into something the machine checks every time. Writing them first, and watching them fail, proves they can fail. A test that has never been red might be testing nothing.
Why the agent should not write the criteria alone
It can draft them. But the criteria are where you decide what you want, the one job you cannot hand over. Read its list for missing cases and invented scope. The plan is cheap to change; the code is not.
This feature needs no data at all: sortByMove takes rows the page already has. Mock mode stays on, and the whole lesson costs nothing from your daily calls.
Try it now
The prompt
Add the line EODHD_MOCK=1 to .env.local, in your editor. Start the agent in the starter folder in its learning mode, press Shift+Tab until plan mode shows, and paste:
Add a function sortByMove(rows) in a new file src/lib/move.ts. It takes the
Quote rows from src/lib/quotes.ts and returns them ordered by the size of
the day's move in per cent (change_p), biggest first, up or down. First
write acceptance criteria as short sentences I can check. Then, for each
criterion, the test you will add in tests/move.test.ts. Then the code.
Change no other file.
Read the plan against the five criteria above. Add what is missing by asking it to keep planning, or with Ctrl+G in Claude Code. Approve the way that has you review each edit, then paste:
Write the tests first, with sortByMove as a stub that returns its input
unchanged. Run npm test and show me which tests fail and why. Then write
sortByMove until they pass, and run npm test again.
What the agent should do
- Propose criteria, tests and code in that order, naming two files:
src/lib/move.tsandtests/move.test.ts. If it first stops to say thatPLAN.mddoes not cover this, answer that it is in scope (the Purpose line says so) and continue; do not let it add scope you did not ask for. - Write the stub and the tests, run them, and show you red: the ordering tests fail on the stub. Whether the empty-list and unchanged-input tests are already green depends on how they are written; a test that also checks the result is a different array from the input fails on a stub that returns the input itself, and that is fine.
- Replace the stub with a copy-then-sort, run the tests again, and show you green.
Check it yourself
- Before you approved, the plan listed criteria, then tests, then code, and named exactly two files.
- The first
npm testrun showed at least one of your new tests failing on the stub. - The second run is green, and the starter's own tests still pass beside yours.
git statusshows the two new files and no change to any file you did not ask about. (If you see other modified files you never touched, ask the agent whether it changed them; do not assume.)grep -n eodhdFetch src/lib/move.tsprints nothing: the function makes no request.
Break it on purpose
The two files are not committed yet, so git has no copy to restore them from. Give it one first: git add -A. Then, in src/lib/move.ts, replace the copy-then-sort with a plain rows.sort(...) and run npm test. The unchanged-input test must go red. If it stays green, the test was not testing what its name says: fix the test until it catches this. Then restore the code from the staged copy with git restore src/lib/move.ts and run npm test once more: green. If you fixed the test, git add it again before you commit.
Compare with ours
Our function and one of our tests
import type { Quote } from "@/lib/quotes";
// Copy first: Array.prototype.sort reorders in place. The sort is stable,
// so equal moves keep their watchlist order.
export function sortByMove(rows: Quote[]): Quote[] {
return [...rows].sort((a, b) => Math.abs(b.change_p) - Math.abs(a.change_p));
}
const q = (code: string, change_p: number): Quote => ({
code, change_p, change: 0, close: 1, previousClose: 1, volume: 0, timestamp: 0,
});
it("puts the biggest move first, down or up", () => {
expect(sortByMove([q("A", 2), q("B", -3)]).map((r) => r.code)).toEqual(["B", "A"]);
});
The full test file is on the branch ref/work-with-an-agent/4 of the starter repository.
Checkpoint
npm test # green
git status # the two new files, and no .env*
git add -A
git commit -m "sortByMove: order rows by size of move, tested first, input untouched"
Then answer this lesson's questions.