Skip to content
The AI Agent Factory

34.3 How work arrives: web events and schedules

Approved by
Panaversity ·
12 min read

In everyday life. A post room does not open every parcel at the front door. It signs for the parcel, checks the label and puts it on the right shelf, where it is logged. The person it is for opens it later. And the post room keeps a calendar: payroll on the 25th, rent on the 1st.

What you will learn. How a web event reaches the right conversation or process exactly once, the three levels of schedule, and the mistakes to look for in review.

Why it matters. A person who sends a message twice is rare. A system that sends an event twice is normal. Your worker must expect it.

Chapter 23 showed schedules and web events for a worker on Claude Managed Agents. This page covers only what changes on the open stack.

Web events: check, queue, reply, then work

When a new invoice arrives in the accounting system, the system sends your Worker a web event: an HTTP request with the invoice ID and a signature. Accounting systems retry when they do not get a quick answer, so the same event can arrive two or three times.

Your endpoint does three things, and only these three, before it replies:

  1. Check the signature. The sender signs each event with a secret you share, as an HMAC: a code computed from the event and the secret, which nobody without the secret can make. An event with a bad signature is refused. Without this check, anyone who finds the URL can send you invoices.
  2. Put the event on a queue, with its event ID.
  3. Reply at once, within a second or two.

The work happens later, in the queue's consumer. It first records the event ID as "seen" in a table with a unique constraint. If the ID is already there and marked "routed", the event is a repeat, and the consumer drops it. Otherwise it decides where the event goes: to the conversation for that supplier's invoices, through getByName as in Chapter 33, or to a new Workflow. It passes the event ID on as the operation ID, so even if the consumer itself is retried, Pi accepts the message once. Only after Pi has accepted it does the consumer mark the ID "routed".

Why record the ID in the consumer and not in the endpoint? Because of the order of failures. If the endpoint recorded the ID and then crashed before queuing, the sender's retry would look like a repeat, and the invoice would be lost. Queuing first means a crash loses nothing: the sender retries, the queue gets a second copy, and the consumer drops it.

In the lab you send Step 1 after you have written spec-reconcile.md, the spec described in 34.4.

Step 1. Read spec-reconcile.md. Build src/webhook.ts: check the accounting system's HMAC signature, send the event to the invoices queue with its event ID, and reply 200. Build the queue consumer: record the event ID as seen with a unique constraint, drop events already routed, route each invoice to its conversation with the event ID as the operation ID, and mark the ID routed only after Pi accepts it. Write the tests first: a bad signature is refused; the same event sent three times reaches the worker once; a crash in the consumer after recording the ID does not lose the invoice.

What to check. Does the endpoint call the model, the ledger, or anything slow? It must not. Is the signature compared in constant time, so the time the comparison takes reveals nothing about the right signature? And read the third test closely. Suppose the consumer records the ID and then crashes before routing. The queue retries, and the ID is there but still "seen", not "routed", so the consumer routes it this time. Check that the code marks the ID "routed" only after Pi has accepted the operation ID, never when it first records it. Ask Claude Code to show you the order.

Schedules at three levels

LevelExampleMechanism on the open stack
The whole worker"Reconcile on the 1st at 06:00"A Cron Trigger on the Worker
One conversation"Remind me Friday if Ana has not answered"A Lifecycle job in that conversation's object
Inside a process"Wait 3 days for the review, then escalate"The Workflow's own step.sleep or step.waitForEvent

The whole worker. A Cron Trigger calls your Worker's scheduled handler at a set time, in UTC. For month-end, the handler starts one reconciliation per client company, each with a stable process ID such as recon-acme-2026-11. Section 34.5 shows why the stable ID matters.

One conversation. A Durable Object has one alarm. Lifecycle already uses it, to wake Pi after a crash and when a deadline passes. So a conversation's own timers must go through Lifecycle's job queue, never through ctx.storage.setAlarm() directly. In agents 0.34, the object pushes a job and handles it in onJob:

await this.lifecycle.jobs.push({ id: "remind-ana", fn: "remind", time: fridayAt9 });

async onJob({ job }) {            // Lifecycle calls this when the job is due
  if (job.fn === "remind") await this.remindAna();
}

Lifecycle sets the one alarm from all its jobs and Pi's deadlines together. We tested a job in a conversation that was waiting for an approval: the job ran, and the approval was still waiting. The job API is young, so read the installed agents documentation (docs/lifecycle.md) before you rely on it, as CLAUDE.md says.

Inside a process. A Workflow waits with its own steps. It needs no alarm and no cron.

Getting it right

A schedule that fires twice starts one reconciliation. Schedulers can fire twice, and people press "run now" while the scheduled run is starting. The stable process ID per client and month acts as a lock: the second start finds the first and stops.

An event that arrives while the worker is revoked is held, not processed. The consumer checks the worker's stop switch (34.7) before it routes an event. If the worker is revoked, it leaves the event in the queue or a holding table, so nothing is lost and nothing is done.

Each job runs in its company's time zone. Cron runs in UTC. "06:00 on the 1st" in Karachi is 01:00 UTC; in New York it is 11:00 or 10:00 UTC, depending on the season. Run the Cron Trigger every hour, and let the handler start each company's job when its local time is due.

Where Claude Code goes wrong here

  • Doing the work inside the web event request. It calls the model and posts the invoice before replying. The sender times out, retries, and the work is done twice. Ask: "What does the endpoint do before it replies?"
  • Skipping the signature check, because the test events have no signature. Write the bad-signature test first.
  • Setting its own alarm in the conversation object, which replaces Lifecycle's. The object then stops waking after crashes and deadlines. Search the code for setAlarm.

The invariant. Take in an event fast, check it, queue it, and do the work later, once. Every schedule has exactly one owner of its clock.

Check yourself. The month-end Cron Trigger fires at 01:00 UTC on the 1st, and again 40 seconds later because of a retry. How many reconciliations run for Acme? (One. Both runs use the process ID recon-acme-2026-11; the second finds the first already started and does nothing.)

Owner
Panaversity