33.5 Waiting for a person
- Approved by
- Panaversity ·
In everyday life. You leave your car at a garage and say, "Call me before you replace anything expensive." The mechanic does not stand by the phone for two days. They put a note on the car, work on other cars, and pick up the job when you call back. If you never call, the note says what to do on Friday.
What you will learn. How a worker waits for a person without paying for the wait, the three kinds of wait, and the four rules that make an approval safe.
Why it matters. Most AP workers spend more time waiting than working. A worker that is awake while it waits pays for hours of nothing, and a worker that loses its place makes a person approve twice.
How a tool waits
A Pi Durable tool can return a result, or it can return a waiting status: "I am waiting for this signal, until this time." Pi saves the run. The Durable Object has nothing left to do, so it hibernates. A hibernating object is not billed for time.
When the signal arrives, or the time passes, Pi wakes the object, loads the run and calls the same tool again. This time the tool finds the answer and returns a normal result. The model never knows that three days passed. It asked for an approval and got one.

Figure 33.3. The same tool runs twice: once to ask and once to read the answer. Every path ends in a result the model can read.
Here is the approval tool Claude Code built, close to the version we tested:
const Approval = defineSignal({
name: "app.approval", version: 1,
schema: obj({ approved: bool(), by: str(), reason: str() }),
});
execute: async ({ invoice, amount }, api, context) => {
const state = await api.readSignal(Approval, "answer", context);
if (state?.kind === "value") {
const a = state.value;
return text(a.approved ? `approved by ${a.by}` : `rejected by ${a.by}: ${a.reason}`);
}
if (state?.kind === "expired") {
return text("not approved: no answer within 3 days; flagged for a person");
}
await approvals.record(api.taskId, { invoice, amount, reviewer: "ana.morales" });
return { kind: "sleep", signals: [Approval.key("answer")],
until: api.now() + 3 * 24 * 3600_000 };
},Read it from the bottom. The first time it runs, there is no answer yet. The tool records the pending approval in a table, keyed by the task's ID (api.taskId, the number Pi gives this tool call, which stays the same when the call runs again), and returns sleep with the signal it waits for and a deadline three days away. Recording by task ID is replay-safe: doing it twice writes the same row.
When Ana answers, your endpoint delivers the answer as a signal. Pi calls the tool again, readSignal returns the value, and the tool turns it into a sentence for the model. If the deadline passes first, readSignal returns expired, and the tool says so.
Every path ends in a result, not an error. That is spec rule 3. The model reads "rejected by ana.morales: PO-778 is closed", and can tell the conversation why the invoice was not paid.
Three kinds of wait
A person approves or rejects. The case above. The signal carries the answer and who gave it.
A deadline passes. The same tool, with until. When the time comes, Lifecycle wakes the object through its alarm, with no request from outside. What the worker does next is a business rule: escalate to a second reviewer, cancel, or flag for a person. The spec says flag.
Another session finishes. A worker can ask a second conversation to do part of the job, such as checking a supplier's tax number, and wait for it to finish. Pi Durable can notify a waiting session when another one settles, that is, finishes with a result or a failure. Read the installed Pi docs for the exact API before you ask Claude Code to build it; it is the part of Pi most likely to change.
The endpoint and the link
Step 6. Build the approval properly. request_approval parks as in the docs. Add src/approvals.ts: a table of pending approvals; a signed approval link for the named reviewer that expires with the wait and works once; and a POST /approve endpoint that checks the link, checks the reviewer, then delivers the answer with harness.signal. A separate step sends the link to the reviewer. Run tests 2, 3, 4 and 5.
The endpoint, shortened:
async approve(token: string, approved: boolean, reason: string) {
const t = await verifyToken(token, this.env.APPROVAL_SECRET); // signature + expiry
if (!t) return { error: "invalid or expired link" };
if (!(await approvals.claim(t.tokenId))) return { error: "link already used" };
const res = await this.harness.signal(t.taskId, Approval, "answer",
{ approved, by: t.reviewer, reason });
return { result: res };
}harness.signal answers with one of five outcomes: delivered, duplicate, invalid, closed or not found. Your endpoint should show each to the reviewer in plain words: "Thank you, the invoice will be posted", "This approval was already answered", "This invoice is no longer waiting".
Getting it right: four rules
1. Only the holder of the named reviewer's link can approve. Pi checks that the signal has the right shape. It does not check who sent it. Anyone who can reach your endpoint can answer, unless your code stops them. The link carries a token signed with a secret only the Worker knows. It names the task, the reviewer and the expiry, and the endpoint takes by from the token, never from the request body. A forged or altered link fails the signature check and is refused. A signal alone is not proof of who approved, and the link proves only that its holder has it.
2. A link works once. approvals.claim marks the token as used, in one step, before the signal is sent. If Ana clicks twice, the second click gets "link already used". Even without that check, Pi refuses a second answer to a call that has already finished: when we tested it, the second signal came back closed.
3. A rejection is a result. Covered above. Test 3 checks the model's input, not only the final message.
4. Approving twice never pays twice. Rules 2 and the idempotency key on post_invoice protect this from two sides. Test 4 approves twice and counts the ledger's posts.
The run survives a redeploy
A three-day wait will often cross a deploy. Because the waiting run is saved in the object's storage, and nothing is running, a new version of the Worker simply loads it. When we tested it locally, we restarted the runtime while the tool was waiting. Afterwards the approval was still listed, and the answer was delivered.
Two cautions. A redeploy must not change the tool's name or the signal's name and version, or the saved run will wait for a signal nobody can send. And a new release should not change the tools of a conversation already started, which is why 33.4 saves the release in the conversation.
What the other foundations do
Claude Managed Agents and the OpenAI Agents SDK both let a tool call wait for approval. With the SDK, you save RunState as a string and resume it later. The difference is where the run waits and what the wait costs: on Pi Durable, inside a sleeping Durable Object that is not billed for time. Checked on 29 November 2026. Check how your platform bills a waiting session before you compare.
The invariant. A worker that waits is saved and asleep. It wakes on an answer it can trust, or on a deadline it chose.
Check yourself. Ana forwards the approval email to a colleague, who clicks the link first. What happens? (The colleague's click approves as Ana, because the token names Ana. The link is the proof, so treat it like a password: short expiry, single use, and for large payments, sign-in before the click counts.)
Sources
- Building Affordable, Self-Improving AI Workers (Panaversity, 29 November 2026), sections 4.1 and 6.1
- Pi harness (Cloudflare Agents documentation, verified 2026-11-29)
- Owner
- Panaversity