Skip to content
The AI Agent Factory

Lab 32: One worker, two foundations, one memo

Approved by
Panaversity ·
12 min read

What you do. You build the same small AP worker twice, with Claude Code. The first is on Pi Durable in a Durable Object, the second on the OpenAI Agents SDK in a Durable Object. You crash both in the same place and see which one carries on. You move the Pi Durable worker to an economy model and compare. Then you write a one-page memo that chooses Brightline's foundation, with numbers.

What you need. Claude Code, Node.js 24 or later, a free Cloudflare account with Wrangler, and a free Neon account. You do not use Neon until a later lab, but set it up now. No API key: every test runs on recorded model answers.

Download. brightline-lab-ch32.zip. It ships a spec template, a test ledger, recorded model answers, model.ts, and the CLAUDE.md from 32.8. It also ships test helpers and scripts to send an invoice, answer an approval, crash the worker and read the ledger. Each task's answer key is its own download, such as Task 1's answer key. For another task, change the number at the end of that address. The complete solution is one more download, brightline-lab-ch32-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 and breaks. Sitting 2 measures and decides.

Core path. Tasks 0 to 4, and Task 6, the memo, which Lab 37 comes back to. Extension. Task 5. 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: build it twice, break it twice

Task 0. Set up, and set a spending limit

Goal: nothing in this part can cost you money without warning.

Sign in to Cloudflare in your browser and set a billing notification at $1. Then:

Task 0. Read CLAUDE.md and the lab README. Install the pinned packages. Run npm test and show me which tests fail and why. Change nothing else.

What good looks like: the install shows agents@0.34.x and @earendil-works/pi-durable@1.6.x. Every test fails, because no worker exists yet.

Check before you approve. Open package.json. Are the versions pinned to the minor version, not latest?

Task 1. Write the spec and the failure tests

Goal: decide what "correct" means before any code exists.

Fill in spec.md yourself. The worker has three tools: lookup_invoice (reads, changes nothing), request_approval (waits for Ana Morales's answer, up to three days) and post_invoice (posts to the ledger, changes something outside). Then:

Task 1. From spec.md, write failure tests in tests/crash.test.ts: (a) if the object is killed right after post_invoice calls the ledger, the invoice is posted exactly once; (b) a run waiting for approval is still waiting after the runtime restarts; (c) a rejection reaches the model as a result, not an error. Use the recorded model answers. Write no worker code yet.

What good looks like: three tests, all failing, none needing a key.

Check before you approve. Predict: which of the three tests is the hardest to make pass, and why?

Task 2. Build the worker on Pi Durable

PiHarness and Lifecycle are the parts of Cloudflare's agents package that host Pi Durable in a plain Durable Object (32.5).

Task 2. Build the worker in src/pi-worker.ts on PiHarness and Lifecycle, one Durable Object per conversation, until the three tests pass. Read the installed Pi docs first, as CLAUDE.md says.

What good looks like: all three tests pass. post_invoice is marked replay: "safe" and saves its idempotency key with api.memo() before calling the ledger.

Check before you approve. Use the checklist in 32.8. Where is the registry created? Where is the key made? Kill the object yourself: start the worker with npm run dev, and in another terminal run npm run crash, then npm run send -- "Pay invoice INV-1001" c1, and answer its approval with npm run answer -- c1 yes. Then read the ledger with npm run ledger: how many posts, how many post calls?

Task 3. Build it again on the OpenAI Agents SDK

Task 3. From the same spec.md, build src/sdk-worker.ts on @openai/agents in a Durable Object, using needsApproval and RunState for the approval. Do not add any other durability. Run the same three tests.

What good looks like: tests (b) and (c) pass. Test (a) fails: after the crash, the run does not resume. The invoice was posted, and no record says the step had finished.

Check before you approve. Explain in one sentence why (b) passes and (a) fails.

Sitting 2: measure it, then decide

Task 4. Make the SDK worker durable, and count the cost

Task 4. Make src/sdk-worker.ts pass test (a), with the smallest change that is correct: a checkpoint before each tool call, a saved idempotency key, and an alarm that resumes an unfinished run. Then report how many lines you added, and list them by purpose.

What good looks like: test (a) passes. The report lists the three additions.

Check before you approve. Would you trust this code with a real payment tomorrow? What test is still missing?

Task 5. Change one model

Task 5. Run the five golden AP questions in golden/ against the Pi Durable worker on the recorded Claude Haiku answers and on the recorded economy-model answers. Change only the model setting. Report how many each gets right, and the token cost of each from model.ts.

What good looks like: a small table: right answers and cost for each model.

Check before you approve. If the economy model gets one question fewer right, what would that cost in review minutes a month for a medium worker? Use the review numbers in 32.3.

Task 6. Write the memo

Goal: a decision someone can act on.

Write docs/foundation-memo.md yourself, in one page. Use the cost table in 32.4 and your numbers from Tasks 4 and 5. Answer four questions:

  1. Which foundation should Brightline use for this worker, today?
  2. How much would it save a month, against the cost-tuned Claude setup?
  3. What does Brightline give up?
  4. What would change your decision? Name one fact about cost, one about quality and one about data.

Then:

Task 6. Read docs/foundation-memo.md. Check every number against model.ts and my results. List any number that does not match, and any claim the evidence does not support. Do not rewrite the memo.

Check before you approve. Does your memo name the data rule from 32.7? A memo that ignores where the data may go is not finished.

What you have now

A worker that survives a crash, on a foundation you chose with evidence. And a method you will use in every lab of this part: spec and failure tests first, Claude Code builds, you review. The next chapter builds the full AP worker on this foundation.

Owner
Panaversity