Skip to content
The AI Agent Factory

34.7 Many conversations, one worker

Approved by
Panaversity ·
12 min read

In everyday life. A team of five shares one company card with a monthly limit. Each person buys things on their own. If each checks only their own spending, the card goes over the limit by the 20th. Someone has to hold the total.

What you will learn. Why one conversation per object makes a worker a distributed system, the six guarantees that must hold across all its conversations, and how a per-worker coordinator keeps them.

Why it matters. Chapter 33 made each conversation safe on its own. A worker can still overspend, overwrite its own memory, or start two payment runs for one supplier, from two conversations that are each correct.

Three conversations, one supplier

Brightline's AP worker has three conversations open. In the first, Ana tells it that Acme's bank details have changed. In the second, it is processing Acme's invoices. In the third, the month-end Workflow it started is about to publish a payment list that includes Acme.

Each conversation is its own Durable Object. Each one saves every step and survives crashes. But none of them knows what the others are doing. Without something shared, the second conversation may pay to the old bank account, and the third may publish a list built from both.

The six guarantees

The mechanism is your choice. The guarantees are not.

GuaranteeHow it is kept
The worker never spends more than its budgetBefore each model call, learning job or Workflow, reserve the cost from the worker's budget in one step; settle or release it afterwards
Two changes to the worker's memory do not overwrite each otherEach memory record has a version; a change applies only if the version is still the one it read (compare-and-swap); a conflict goes to review
An assignment is never processed twiceStable assignment IDs, with a unique constraint
Conflicting business actions never run togetherA lock on the business record, such as one open payment run per supplier
Stopping the worker stops it everywhereOne revocation flag, checked by every conversation and every Workflow before each step that has consequences
Every assignment uses one release from start to endEach assignment pins the release it started with; a new release is switched on by compare-and-swap, for new assignments only

You have met two of these already. Stable IDs were the fix in 34.5. Pinning the release was in Chapter 33. The other four need one place that every conversation and every Workflow of the worker can ask.

The coordinator object

That place is one more Durable Object, one per worker: getByName("coordinator-brightline-ap"). A Durable Object runs its code one piece at a time. Code that does not stop to wait for something (an await) runs to its end before any other request starts. So "check the budget and reserve $0.02" happens in one step, with no other conversation in between, as long as nothing in that step waits. That is what makes it a good home for budgets and locks.

Do not wait in the middle. Suppose the code reads the budget, then waits for a reply from outside, such as Postgres or a model. Other requests can run during that wait. Two of them can then read the same total. Read, decide and write with the object's own storage, which does not need an await, and call anything outside only after the reservation is saved. We tested this locally: twenty reservations at once against room for fifteen gave exactly fifteen. With a 20-millisecond wait between the read and the write, all twenty succeeded, and the saved total was wrong.

Step 6. Build src/coordinator.ts, one Durable Object per worker, with these methods: reserve(amount, purpose) and settle(reservationId, actual); lock(resource, holder) and unlock(resource, holder); isRevoked() and revoke(reason); activeRelease() and activateRelease(newId, expectedCurrentId). Store everything in the object's SQLite. Make every conversation's model call reserve first, every post_invoice take the supplier lock, and every consequential step check isRevoked(). Write the concurrency tests first.

The heart of it, shortened:

reserve(amount: number, purpose: string) {
  const { spent, limit } = this.budget();          // object storage, no await:
  if (spent + amount > limit) return { ok: false }; // no other call can run in between
  const id = this.newReservation(amount, purpose);
  return { ok: true, id };
}

activateRelease(newId: string, expectedCurrentId: string) {
  if (this.currentRelease() !== expectedCurrentId) return { ok: false }; // compare...
  this.setCurrentRelease(newId);                                         // ...and swap
  return { ok: true };
}

Where the record lives. The coordinator holds the fast, live state: reservations, locks, the flag. Postgres holds the lasting record: what was spent, which releases existed, who revoked the worker and why. The coordinator writes its events to Postgres through an outbox, as in 34.5. The paper allows either store, or both. This split keeps the frequent checks fast and the history queryable.

One flag that stops everything. revoke sets the flag. Every conversation checks it before a consequential tool call, and every Workflow checks it before a consequential step. Checks before consequential steps are enough: a conversation that reads an invoice after the stop does no harm; one that posts it does.

Three boxes on the left all call one coordinator. Conversation 1, where Ana says Acme's bank details changed, asks lock("acme-payments") and is granted it. Conversation 2, processing Acme's invoices, asks for the same lock and is told it is busy, so it waits. The month-end Workflow, about to publish a payment list, calls isRevoked() before publishing. In the middle, the coordinator, one per worker, named coordinator-brightline-ap, offers four pairs of calls: reserve and settle, which check and reserve the budget in one step; lock and unlock, one payment run per supplier, with an expiry; isRevoked and revoke, one flag that stops every conversation; and activateRelease, a compare-and-swap that affects new work only. A note says there is no await between reading and writing. An arrow labelled outbox goes to Postgres, the lasting record of what was spent, which releases existed, and who revoked the worker and why. A line at the foot says two more guarantees need no coordinator: stable assignment IDs stop double processing (34.5), and each assignment keeps the release it started with (Chapter 33).

Figure 34.2. Four of the six guarantees need one place every conversation and Workflow can ask. That place is the coordinator.

The tests that matter

  • Twenty conversations reserve at once against a budget that fits fifteen: exactly fifteen succeed.
  • Two conversations try to lock Acme's payment run: one gets it.
  • Two releases are switched on at once from the same current release: one succeeds, the other must be re-evaluated.
  • The worker is revoked while a Workflow waits for review: after the review, the Workflow stops before publishing.
  • A conversation crashes holding a lock: the lock expires after a set time, or the holder's replay finds and releases it.

What to check. Is there one coordinator per worker, not per conversation or per client? Does every model call reserve, including the model step inside the Workflow? Does every lock have an expiry? Is there an await between reading the budget and saving the reservation? Is the flag checked at the step that acts, not only at the start of a run?

The invariant. Each conversation is safe on its own. The worker is safe only when the six guarantees hold across all of them.

Check yourself. The budget check reads the total from Postgres, then each conversation adds its own spend when its call finishes. Twenty calls start in the same second. What goes wrong? (All twenty read the same total, all see room, and all spend. The check and the reservation must happen in one step, which the coordinator object gives you.)

  • Building Affordable, Self-Improving AI Workers (Panaversity, 29 November 2026), section 6.3, and companion appendix A6
Owner
Panaversity