Topic 4 of 5 · 4 min read
Durable waits for human approval
Goal: Model an agent workflow that waits up to three days for a human approval, with a timeout path and no busy waiting.
When an agent needs a person to approve something, make the workflow sleep durably until a signal arrives or a timer fires, instead of holding a process open or polling a database in a loop. The state is saved, the worker is free, and the wait can last minutes or weeks.
Check yourself · 1 of 3
What is the main problem with while (!approved) { await sleep(5000); check(); } for a three-day approval?
Why it matters
Agents should ask before they do something risky: send a large refund, delete data, email a customer list. A human might answer in two minutes or on Monday. A while (!approved) { sleep(5) } loop pins a process for days, dies on a deploy, and loses the wait. Durable execution engines treat the wait as a recorded state, not a running process.
The two building blocks
- Signal (or event). An outside message sent to a running workflow: "approved by Sam" or "rejected". Temporal calls it a signal, Inngest an event waited on with
step.waitForEvent, Step Functions a task token, LangGraph an interrupt that you resume with a value. - Durable timer. A timeout stored by the engine. It fires even if every worker restarted in between.
A workflow waits for the signal with a timeout. Whichever comes first wins.
The shape of the workflow
// Temporal TypeScript sketch
const approved = defineSignal<[{ by: string; ok: boolean }]>("approval");
export async function refundWorkflow(req: RefundRequest) {
let decision: { by: string; ok: boolean } | undefined;
setHandler(approved, (d) => { decision = d; });
await notifyApprover(req); // activity: Slack message with a link
const got = await condition(() => decision !== undefined, "3 days");
if (!got) return await escalateAndClose(req); // timeout path
if (!decision!.ok) return await closeRejected(req, decision!.by);
return await issueRefund(req); // side effect, with idempotency key
}
condition(..., "3 days") returns false if the time runs out. The code handles three outcomes: approved, rejected, and no answer.
Before and after
Before: a worker polls an approvals table every 10 seconds for three days. 25,920 queries per request, a held worker slot, and the wait is lost if the process restarts.
After: the workflow sleeps. The approval link calls an API that sends the signal. No polling, no held process.
What goes wrong
- No timeout path. If the approver never answers, the workflow waits forever and the customer is never told. Decide the default: escalate, reject, or cancel.
- Approving a stale request. Three days later the data has changed. Re-check the facts after the wait, before the side effect.
- Unauthenticated signal endpoint. Anyone who can call it can approve. Check the caller's identity and that they are allowed to approve this request.
- Double approval. Two people click approve. Make the signal handler ignore everything after the first decision.
Examples
Same idea in Inngest
const approval = await step.waitForEvent("wait-for-approval", {
event: "refund/approved",
timeout: "3d",
match: "data.requestId",
});
if (!approval) { /* timed out: escalate */ }
The function is suspended between steps and resumes on the event or after three days.
LangGraph interrupt
A node calls interrupt(...) with the proposed action. The checkpointer saves the state. Later, graph.invoke(Command(resume={"ok": True}), config) with the same thread id continues from that point.
Key points
- A durable wait is saved state that ends on a signal or a timer, so no process stays running.
- Always pair the wait for approval with a timeout, and write the code for approved, rejected and timed out.
- Re-check the facts after the wait, because the world may have changed during three days.
- Authenticate whoever sends the approval signal and accept only the first decision.
- Polling a table for days holds resources, adds load and loses the wait on restart.
Common mistakes
- Writing a polling loop with sleep instead of using a signal or event.
- Waiting with no timeout and no default outcome.
- Issuing the refund from the stale data read before the wait.
- Letting any caller send the approval signal.
Check yourself · 2 of 3
A workflow waits 3 days for approval and the timer fires with no answer. What should the code do?
Check yourself · 3 of 3
Two approvers click the approve link within a second of each other. How should the workflow behave?
Practice items, not counted towards your mastery.
Start here
- Temporal documentation (opens in a new tab)DocsFree
Workflows, activities, signals and timers, with the replay model explained from the source.
- Persistence (LangGraph docs) (opens in a new tab)DocsFree
How checkpointers save graph state after each step so a run can resume or wait.
- Restate documentation (opens in a new tab)DocsFree
A durable execution engine that journals each step; useful for comparing replay designs.
More for this topic
- What is AWS Step Functions? (opens in a new tab)DocsFree
The managed cloud option: state machines with retries, waits and task tokens.