# Lab 33: The AP worker that waits and does not pay twice (/affordable-self-improving-ai-workers/building-a-durable-ai-worker/lab)

---
type: Document
title: "Lab 33: The AP worker that waits and does not pay twice"
description: "Lab 33: from your own spec and failure tests, build Brightline's AP worker with Claude Code on Pi Durable in a Durable Object, crash it after a payment, make it wait for an approval, price 50 invoices, and port it to an economy model."
status: stable
order: 433.9
ksor:
  owner: team:panaversity
  audience: [ public ]
  approval:
    by: process:panaversity
    at: 2026-11-30T08:18:55Z
chapter: "33"
part: IV
expert_status: manager
concepts: []
last_verified: 2026-11-29
generated:
  at: 2026-11-30T08:18:55Z
  by: human:zia
trust_tier: unverified
build_id: sha256:d6ce3f28a2cf8e492a60bae1b426b7cc6466e35e83d70c3b9763db2c55009f4b
dirty: true
ksor_version: 0.0.60
---

**What you do.** You write the spec and its failure cases for Brightline's AP worker. Claude Code turns the cases into tests, and builds the worker on Pi Durable in a Durable Object, one step at a time, and you review each step. You crash it right after a payment, make it wait for an approval, reject one invoice, let one deadline pass, and price 50 invoices. Then you port it to an economy model with only the model setting.

**What you need.** The setup from [32.8](../choosing-the-foundation/set-up-once-with-claude-code.md): Claude Code, Node.js 24 or later, Cloudflare with Wrangler and a spending notification, and a Neon database. No API key: every test runs on recorded model answers.

**Download.** [brightline-lab-ch33.zip](https://github.com/panaversity/agentfactory-v2-resources/releases/latest/download/brightline-lab-ch33.zip). It ships `CLAUDE.md`, a ledger Durable Object and recorded model answers for 50 invoices. It also ships a crash helper, a restart helper, a test clock (`npm run advance`), `model.ts`, and an empty `spec.md` and `REVIEW.md`. Scripts send an invoice (`npm run send`), crash the worker (`npm run crash`) and read the ledger (`npm run ledger`). Until you set your Neon connection string, a small local stand-in answers the same SQL, so every test runs offline. Each task's answer key is its own download, such as [Task 1's answer key](https://github.com/panaversity/agentfactory-v2-resources/releases/latest/download/brightline-lab-ch33-key-task-1.md). For another task, change the number at the end of that address. The complete solution is one more download, [brightline-lab-ch33-answer-key.zip](https://github.com/panaversity/agentfactory-v2-resources/releases/latest/download/brightline-lab-ch33-answer-key.zip). Unzip it inside your lab folder, and open it only after you try.

**How a task works.** Each task gives you a request to send to Claude Code, then **What good looks like**, then **Check before you approve**. Predict first, then check.

The lab has two sittings. Sitting 1 builds the worker. Sitting 2 breaks it, prices it and ports it.

**Core path.** Tasks 0 to 6. **Extension.** Task 7, the port to an economy model. The core tasks are enough for the next chapter. Each lab's download includes what it needs from the labs before it, so a skipped extension never blocks you.

### Sitting 1: specify and build

#### Task 0. Start clean

> Task 0. Read CLAUDE.md and the lab README. Install the pinned packages. Run npm test and tell me what fails and why. Change nothing else.

**What good looks like:** `agents` 0.34.x and Pi Durable 1.6.x installed. Every test fails, because no worker exists yet.

**Check before you approve.** Is your billing notification still set? Open the Cloudflare dashboard and look.

#### Task 1. Write the spec and the tests

Write `spec.md` yourself, following [33.2](write-the-spec.md). Change at least one thing to make it yours: a different approval limit, a second reviewer, a shorter wait. Then:

> Task 1. Read spec.md. Write one test per failure test in tests/, using the recorded answers in fixtures/ and the crash and restart helpers. Write no worker code. Run npm test and show me that each test fails for the right reason.

**What good looks like:** seven tests, all failing because the worker is missing. Test 7 checks a property, no API key, and fails now only because nothing builds yet.

**Check before you approve.** Does test 1 count posts in the ledger? Does test 4 try a link forged for a reviewer who is not named in the spec?

#### Task 2. Build in steps

Send Steps 1 to 5 from [33.3](build-it-step-by-step.md) and [33.4](one-object-per-conversation.md), one at a time. After each one, use your `REVIEW.md` checklist from [33.8](when-claude-code-gets-it-wrong.md) before you send the next.

**What good looks like:** after Step 5, tests 1, 6 and 7 pass. `post_invoice` saves its key with `api.memo()` before the ledger call. The registry is a field of the object.

**Check before you approve.** How many Durable Objects exist after you start three conversations? Where is the Neon connection string?

#### Task 3. Build the approval

Send Step 6 from [33.5](waiting-for-a-person.md).

**What good looks like:** tests 2, 3, 4 and 5 pass. The endpoint takes the reviewer's name from the signed token, not from the request.

**Check before you approve.** Search the code for `setTimeout`, `setInterval` and `while (`. Is any of them part of the wait?

### Sitting 2: break it, price it, port it

#### Task 4. Crash it yourself

Start the worker on your machine with `npm run dev`, and leave it running in its own terminal for Tasks 4 and 5. Start an invoice under $500, so it needs no approval. Run `npm run crash`, then send the invoice. Read the ledger.

**What good looks like:** one post and two post calls. The conversation ends with "already posted".

**Check before you approve.** Predict first: if you move the crash to *before* the ledger call, how many calls will the ledger show? Ask Claude Code to add that test and see.

#### Task 5. Wait, reject, expire

Start three invoices over $500 with `npm run send`, such as INV-2111, INV-2115 and INV-2131. Each prints an approval link. Answer a link by sending it a POST with a JSON body: `{"approved": true}` for the first, and `{"approved": false, "reason": "PO is on hold"}` for the second, for example:

```
curl -X POST "http://localhost:8787/approve?token=..." -d '{"approved":false,"reason":"PO is on hold"}'
```

For the third, run the lab's `npm run advance -- 3d`, which moves the test clock forward three days.

**What good looks like:** the first posts once. The second ends with your reason in the model's reply. The third is not posted, and is flagged for a person.

**Check before you approve.** Send the first link again. What does the reviewer see?

#### Task 6. Price 50 invoices

Deploy, then send Step 7 and Step 8 from [33.7](deploy-and-read-the-bill.md).

**What good looks like:** `docs/cost-report.md` with model calls, tokens, active object time and a cost per invoice from `model.ts`.

**Check before you approve.** Leave one approval waiting overnight. Does the object's active time rise while it waits?

#### Task 7. Port it to an economy model

> Task 7. Change only the model setting to the economy model in fixtures/economy. Run the full test suite and the five golden AP questions. Report what passed, and the new cost per invoice from model.ts. Do not change any tool.

**What good looks like:** all seven failure tests still pass, because they test your code, not the model. The golden questions may differ, and the cost per invoice falls. On the recorded answers, the economy model gets 4 of the 5 golden questions right, and the cost falls from about $0.005 to about $0.0015 per invoice.

**Check before you approve.** Did Claude Code change anything except the model setting? Read the diff.

### What you have now

Brightline's AP worker, built from your spec, that survives a crash without paying twice and waits days for a person without paying for the wait. You also have a review checklist and a cost per invoice you measured yourself. Later in this part, this worker hands longer business processes to Workflows, and many conversations learn to share one budget.
