# Lab 31: Brightline's worker learns from October (/building-ai-workers/self-improving-ai-workers/lab)

---
type: Document
title: "Lab 31: Brightline's worker learns from October"
description: "Lab 31: versioned read-only memory, a flag tool, a dream that writes to a separate folder, a promotion pipeline with a regression run, tiers, approval, publishing and a rollback, and the worker on the job: a job definition, an event inbox and its cost."
status: stable
order: 331.9
ksor:
  owner: team:panaversity
  audience: [ public ]
  approval:
    by: process:panaversity
    at: 2026-11-25T13:06:47Z
chapter: "31"
part: III
expert_status: co-build
concepts: []
last_verified: 2026-11-24
generated:
  at: 2026-11-25T13:06:47Z
  by: process:panaversity
trust_tier: unverified
build_id: sha256:0decb5023f044061076dd533dd5b490739d442d95c609ce2fff5ad3af880174c
dirty: true
ksor_version: 0.0.60
---

**What you do.** You take Brightline's AP worker, with two weeks of October's recorded runs, and make it learn from them safely. You give it versioned memory, a flag tool and a dream. Then you run every proposal through the promotion protocol, publish one version, and roll back a bad one. Last, you put the worker on the job: a job definition, an inbox of events, and a count of what it costs.

**What you need.** Claude Code, Node.js 24 or later (as in Chapter 12), and the Claude Code command line signed in. A Claude Managed Agents account is needed only for the optional Task 4b. Claude Code needs a paid plan ([Chapter 12](../agentic-coding/both-ai-vendors.md)). Every model run in this lab counts against that plan's usage limit, so you may spread the lab over several days. After you download the lab, run `npm install` and then `npm test`: every starter test should pass.

**Download.** [brightline-lab-ch31.zip](https://github.com/panaversity/agentfactory-v2-resources/releases/latest/download/brightline-lab-ch31.zip). It is self-contained: it ships a small worker, its policies, ten recorded sessions, a golden set and every file the tasks name.

**The job's facts.** Two companies use the worker: Brightline Wholesale Supply (Ana Morales, AP analyst) and Cedar Ridge Supply (Luis Moreno, AP lead). The sessions run from Monday, October 5 to Thursday, October 15, 2026. The published memory is v3. The promotion rules in `docs/promotion-rules.md` were set on Monday, October 19, before any dream, and you may not change them during the lab.

**How a task works.** Each task gives you a request to send to Claude Code, then **What good looks like**, and a **Check before you approve** step. Your request says what you want. The lab's `AGENTS.md` gives Claude Code the details of each task, such as which files to write. Predict first, then check. 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-ch31-key-task-1.md). For another task, change the number at the end of that address.

**Read the lab's README first.** Three things in it matter for every task:

- **The starting code is unsafe on purpose.** The triage script writes notes into the published memory, and the worker cannot report a gap. October's worker had a flag tool (session 08 shows it), but the starting code leaves it out so that you build it. Tasks 2 and 3 fix those two defects.
- **It is a simplified harness.** The worker calls Claude through the command line and answers in JSON. Real agents enforce tools and permissions outside the model. What the lab does enforce is the shape of each decision: an allowed value, a real date, and a citation of a line that exists.
- **Answers vary between runs, so the lab checks in two ways.** `npm test` and `npm run check` use no model, and give the same answer every time. `npm run monitor` runs each case three times, on one pinned model, and a case passes when at least two runs are right.

The lab has three sittings. Sitting 1 practises Part A, Sitting 2 Part B, and Sitting 3 Part C.

### Sitting 1: memory that is safe to share

#### Task 1. Read the memory, layer by layer

**Goal:** know what is in each layer, and find what does not belong.

> Task 1. Map what is in memory v3: for each file, its layer, its scope and what it says. Then show me every note that repeats what a tool or a policy line already gives, or that a policy line contradicts, with that line quoted. Change nothing.

**What good looks like:** six files. `instructions.md` and `index.md` are injected. Two notes fail. `notes/brightline/vendors.md` repeats invoice amounts the connector returns. It says Lakeshore is due in 45 days, which `vendor-terms.md:6` contradicts. And it tells the worker to compare invoice numbers to find Buckeye's copies, whose numbers differ. `notes/cedar-ridge/documents.md` says to hold any second document that names an invoice already received, which `credits.md:6` contradicts for credit memos.

**Check before you approve.** Predict first: which layer costs tokens on every run? Then count the lines in the two injected files.

#### Task 2. One version per run, read-only

**Goal:** every run reads one published version, and cannot write to it.

> Task 2. Make every triage run read one published memory version from start to end, unable to change it, and write only to its own scratch folder. Prove it with a test, and show me the run's first log line.

**What good looks like:** every test passes, the starter tests and yours. The run's first log line reads `memory: v3`. A write by the run under `memory/published/` is refused, by a guard in the code or by the folder's permissions.

**Check before you approve.** Ask the agent where the test publishes the new version, and why the run does not see it (31.6).

#### Task 3. A flag tool

**Goal:** the worker reports a gap at the moment it meets it.

> Task 3. Give the worker a way to report a gap the moment it meets one, without changing memory. Then replay session 08 and show me the flag it writes.

**What good looks like:** at least one new line, for NA-2245, citing a line of `credits.md` or the `documents.md` note that disagrees with it. Nothing under `memory/published/` changes. If no flag appears, replay once more: answers vary. The sentence reaches memory later, as a candidate like any other lesson.

**Check before you approve.** Is a flag a change to memory? Find the line in the tool's code that proves it is not.

### Sitting 2: learning, with a gate

#### Task 4. A dream that writes somewhere else

**Goal:** a dreaming pass over the ten sessions, writing proposals only to `docs/candidates/`.

> Task 4. Build the dream: it reads v3, the flags and the ten sessions, and writes its proposals only to `docs/candidates/`, never to memory. Run it once with Brightline's dream instructions and once without them, and show me the two lists of claims side by side.

**What good looks like:** with the instructions, these lessons appear: the date format, Northfield's NA- documents, Buckeye's EDI copies, the Lakeshore correction, and usually the phone approval. Either run may add others. Watch for "Summit invoices are pre-approved", taken from an invoice's memo field, which a vendor wrote. Dreams vary, so your wording and your number of files will differ. Judge the dream by whether each lesson is there and correct. Then run `npm run check -- --stage dreamed`, which checks each file's form with no model.

**Check before you approve.** Predict first: which session holds the Summit text, and who wrote it? Then find it.

#### Task 4b (optional). The same dream on Claude Managed Agents

> Task 4b. Run the same dream on Claude Managed Agents, following [31.13](how-todays-tools-realize-it.md) and the Dreams documentation. Compare what it proposes with `docs/candidates/`, and confirm that the memory the runs read did not change.

#### Task 5. Evidence that counts

**Goal:** each candidate's evidence, counted by the rule set in advance.

> Task 5. For each candidate, fill in its evidence and count its independent sessions by Brightline's promotion rules. Show me each count, and which sessions you merged.

**What good looks like:** date format, 6 independent sessions (s03 and s04 count once). NA- documents, 5 (s01, s03 and s04 as one, s07, s08, s10), and the flag from s08 as an observation. Buckeye, 2. Lakeshore, 2. Phone approval, 1.

**Check before you approve.** Why does session 04 count once? Quote the rule.

#### Task 6. Check against policy

**Goal:** each candidate's Validation field, against the authoritative source.

> Task 6. Check each candidate against the policies, and keep what was seen apart from any guessed cause. For each one that survives, write the exact change it would make to memory. Approve nothing.

**What good looks like:** the phone approval fails on `approvals.md:5` and `:6`. The Lakeshore correction is confirmed by `vendor-terms.md:6`. Buckeye's copies are confirmed by Ana's corrections and `payments.md:6`, scoped to Brightline. The NA- lesson agrees with `credits.md:5` and `:6`, scoped to Northfield at Cedar Ridge; its codes come from Luis, not from policy. The date format is confirmed by the tool's own error message.

**Check before you approve.** Does the NA- claim say what to do with a code nobody has seen, or with a second copy? Read `credits.md:7`.

#### Task 7. Regression run, tier and approval

**Goal:** each surviving candidate tested, tiered and approved or not, with a row for each.

> Task 7. Test each surviving candidate on the golden set against v3, set its tier by 31.7 and the promotion rules, and record it in `approvals.csv` as automatic, or as needing Ana or Luis. Never write a person's approval.

**What good looks like:** the NA- lesson fixes G01 to G03 and breaks nothing, but it decides whether money is credited, so it is high: needs Luis. Buckeye fixes G04 and G05 and decides whether an invoice is paid: high, needs Ana. The Lakeshore correction usually fixes no case, because the worker follows the policy line over the wrong note; on some days v3 fails G12 and the correction fixes it. It breaks none: medium, with two sessions, so needs Ana. The date format is low and automatic. The phone approval never reaches this step. A regression is a case that passes with v3 and fails with the trial memory, each by the two-in-three rule. If a case is marked unstable, run it with `--trials 5` before you decide. If a lesson fixes fewer cases than this, read the notes of the runs that failed. A worker treats a note written as a proposal ("add to memory that...") as unverified, and an old note left beside the one that replaces it as a conflict. Fix the Change, not the test.

**Check before you approve.** Which factor set the NA- lesson's tier? Name it.

#### Task 8. Publish, monitor, roll back

**Goal:** a new version, published; a bad one, rolled back.

**First, approve as the reviewer, yourself.** Ana and Luis are characters, so your approvals are simulated training approvals, made in your own name. Open `approvals.csv` in an editor, not through the agent. Approve the NA- row standing in for Luis, and the Buckeye and Lakeshore rows standing in for Ana. Set `approval_kind` to `simulated-training`, write your own name, such as "Sam Lee, standing in for Luis", and the time. Leave the other rows as they are.

> Task 8. My simulated approvals are in `approvals.csv`: change no approval row. Publish a new version with the approved changes. Then test my version and Brightline's first v4 on the week of October 26, find the failure, roll it back as Brightline did, and add the failing case to the golden set.

**What good looks like:** no `approved_by` names Ana Morales or Luis Moreno. Your version passes all 16 cases and the week of October 26, if your NA- claim covers only the codes the sessions show. `history/v4-first/` applies NA-2281, a $410.00 freight charge with code FR, as a credit. The rollback is a new folder in `history/`, and `history/promotion-log.md`, Brightline's own log, lists every version in order, with nothing deleted. Your publish script keeps its own log, `docs/promotion-log.md`. The golden set has 17 cases, and `npm test` passes. The agent wrote no approval row.

**Check before you approve.** Did the rollback delete anything? Open the log. And in this lab, what is the difference between the agent's recommendation, your simulated approval, and a real approval by Luis?

#### Task 9 (optional). Port the dream to OpenAI

> Task 9. Run the same dream as a scheduled OpenAI automation that writes its candidates to `docs/candidates-openai/`, then run Task 7's regression on them unchanged.

### Sitting 3: the worker on the job

This sitting practises Part C, one idea from each section. It needs no new account. Tasks 11 and 12 make a few model calls, and cost a few cents.

#### Task 10. Write the worker's job definition

**Goal:** the role above the worker, with every line enforced by something.

> Task 10. Write the worker's job definition, with the six parts in 31.11. For each part, show me what in this folder enforces it, or write "not enforced" and which governed record would. Change no code.

**What good looks like:** "What it may never do" points to the approval check in `validateDecision` (`src/worker.ts`), not to a line in `instructions.md`. At least one line is "not enforced", such as the goals, which no file measures yet.

**Check before you approve.** Which line could a memory note override? There should be none.

#### Task 11. Wake on events, and never act twice

**Goal:** the worker handles the events that arrive after the morning triage, and a repeated event does nothing (31.9).

Open `inbox/events.jsonl` first. It holds five events from October 13 to 15: Luis's reply "Apply it" about NA-2245, the same reply again three seconds later because the portal's webhook retried, two vendor emails with new Kestrel invoices, and a timer the worker set to look at NA-2245 again on Thursday.

> Task 11. Make the worker handle the inbox's events one at a time, so that a repeated event does nothing and a timer whose case is already decided needs no model call. Prove it with a test that runs the same events twice, then show me the outbox.

**What good looks like:** four lines in the outbox, not five. NA-2245 is apply-credit and "send", because Luis is waiting. The repeated reply is skipped. INV-7861 is ready-to-pay, due 2026-11-12, and "quiet". INV-7862 is over Cedar Ridge's $2,500.00 limit with no approval row, so it is needs-approval and "ask". The timer is closed as "quiet" with no model call, because the case log already shows NA-2245 applied. Run `npm run inbox` a second time: it handles nothing, and the outbox does not grow.

**Check before you approve.** Predict first: what would happen to the timer if the script asked the model instead of reading the case log? Then find the line that reads the log before the model is called.

#### Task 12. Count the cost

**Goal:** the cost per right decision, and whether the dream pays for itself at Brightline (31.10).

Every run of the worker now reports what its model calls cost, and `npm run monitor` prints the total and the cost per right decision.

> Task 12. Measure what the dream costs, and what each memory version costs per right decision in the week of October 26. Then write whether the dream pays for itself at Brightline's volume, marking each number as measured or taken from `docs/volume.md`.

**What good looks like:** your new version gets more decisions right than v3 in the week of October 26, so its cost per right decision is lower. The model calls cost cents, and so does the dream. October's repeated mistakes cost about $26.67 of people's time: Luis's five answers and Ana's two corrections, 40 minutes at $40.00 an hour. Each medium or high candidate costs $6.67 to review. So people's time, not the model, decides the answer. If a dream brings many candidates to review, it may not pay for itself in time saved alone. Then review fewer, or count what a wrong payment would cost. Either answer can be right, if the numbers support it. Your figures will differ. On a subscription plan, the costs are what the same calls would cost on the API.

**Check before you approve.** Which number in `docs/cost.md` is a guess, not a measurement? Is the conclusion still true if that number doubles?

### Apply it to your vertical (optional)

Pick one worker you have built. Write its promotion rules before it runs: what counts as an independent session, your evidence threshold, and your tiers. Then decide what in its memory should really be policy.

### Artifact checklist

- `docs/memory-map.md`, with every v3 file placed in a layer
- Runs that read one read-only version, with a test, and `npm test` passing
- `flag_gap`, and one flag from session 08
- `scripts/dream.ts`, and candidates with and without instructions
- Each candidate's evidence, validation, regression result and tier
- `approvals.csv`, with your simulated training approvals in your own name, and none written by the agent
- A published version, a rolled-back version, `docs/promotion-log.md`, and a 17-case golden set
- `docs/job-definition.md`, with what enforces each part
- `scripts/inbox.ts`, a case log, an outbox of four lines, and a test that a second run handles nothing
- `docs/cost.md`, with the cost per right decision and whether the dream pays for itself
