Skip to content
The AI Agent Factory

33.2 Write the spec

Approved by
Panaversity ·
6 min read

In everyday life. Before builders start a kitchen, the owner signs a drawing: where the sink goes, which wall carries weight, what the inspector will check. Changes after the walls are up cost ten times more.

What you will learn. What goes in the spec for a durable worker, and how each line becomes a test.

Why it matters. The spec is where you decide what "correct" means. Claude Code builds to the spec and the tests. If a rule is not written there, nobody checked it.

The spec, in full

You write this file yourself. It is spec.md in the lab folder.

# Brightline AP worker, release 33.1

## Job
Read a supplier invoice, match it to a purchase order, ask the AP manager to
approve payments over $500, and post approved invoices to the ledger.
Works for one client company per conversation.

## Model
Claude Haiku, through the model setting only. No model name in tool code.

## Tools
| Tool             | Changes the outside? | Replay           | Notes                          |
| ---------------- | -------------------- | ---------------- | ------------------------------ |
| lookup_invoice   | No                   | safe             | Reads only                     |
| match_po         | No                   | safe             | Reads only                     |
| request_approval | No (waits)           | safe             | Parks until answer or deadline |
| post_invoice     | Yes: ledger          | safe, with a key | Key saved before the call      |

## Approval
- Needed for any invoice over $500.
- Only the holder of the named reviewer's link may answer (today: Ana Morales, AP manager,
  user name ana.morales).
- The answer comes by a signed link that works once and expires with the wait.
- Wait up to 3 days. After that: no payment; tell the conversation and
  flag the invoice for a person.
- A rejection is a normal result for the model, with the reviewer's reason.

## Budget
- At most 12 model turns per invoice.
- At most $0.05 of model spend per invoice (estimate: about $0.02).

## Storage
- One Durable Object per conversation. The run's state lives in its storage.
- The worker's identity, role contract version and release live in Postgres.

## Failure tests (written first)
1. Killed right after post_invoice calls the ledger: posted exactly once.
2. Runtime restarted while waiting for approval: still waiting afterwards,
   and the approval still works.
3. Rejected: the model gets "rejected by ana.morales: <reason>" as a result.
4. Approved twice, or with a forged or altered link: posted at most once,
   and the forged link is refused.
5. No answer in 3 days: no post, the invoice is flagged.
6. Two messages sent to one conversation at once: handled one after the other.
7. Every test runs from recorded model answers, without an API key.

Why each line is there

"Changes the outside?" This column decides everything else. Three tools change nothing, so running them twice is harmless. One tool moves money. That tool is where the crash test points.

"Replay: safe, with a key." You do not leave this to Claude Code. A tool marked safe that is not safe is the most expensive mistake in this part. Writing "with a key" and "key saved before the call" in the spec makes it a rule you can check in review.

"Only the holder of the named reviewer's link may answer." A harness can deliver an answer to a waiting tool. It does not know who sent the answer. Checking that is your code's job, and so it is in your spec.

"A rejection is a normal result." Do not throw a rejection as an error. If you do, the harness may retry the step, or the run may end with a stack trace instead of a sentence. The model should read "rejected, because the PO is closed" and reply sensibly.

"No model name in tool code." In Chapter 32 you changed only the model setting to move to an economy model. That works only if the tools do not care which model calls them. The lab's port, Task 7, which moves the worker to an economy model, does the same again.

The budget. Twelve turns is about twice what a normal invoice needs. A worker that hits the limit is stuck in a loop, and it is cheaper to stop it and ask a person.

From spec to tests

Each failure test becomes one test in tests/. Send this to Claude Code:

Read spec.md and CLAUDE.md. Write the seven failure tests in tests/, one file per test, using the recorded model answers in fixtures/. Use the lab's crash helper for test 1 and the runtime restart helper for test 2. Write no worker code. Run npm test and show me that all seven fail for the right reason.

What good looks like: seven tests, all failing because the worker does not exist yet, not because of a typing error or a missing package. Test 7 checks a property, no API key, rather than a failure, and it fails now only because nothing builds yet.

Check before you approve. Read test 1. Does it count the ledger's posts, or only the worker's final message? A worker can say "posted once" and have posted twice. The test must ask the ledger.

The invariant. If the spec does not say it, no test checks it, and no review catches it.

Check yourself. Why does the spec mark request_approval as changing nothing outside, when it sends a person a link? (Because in this design the link is only recorded, and a separate notification step sends it. If the tool itself sent an email, it would have a side effect and need a saved key too.)

Owner
Panaversity