Add a DBOS `ExternalEventWaiter` so `HandleDeferredToolCalls` token handlers and `AskUser` wait durably in a workflow
- Closes #13200
Under DBOSDurability, a HandleDeferredToolCalls handler that takes a resume token, and Pydantic AI Harness's AskUser, now wait durably inside a DBOS workflow instead of being refused with a UserError. This mirrors the Temporal waiter (#13215).
A wait records:
- a
{agent}__external_event.{name}step that draws the event key; - a
{agent}__external_event.{name}.askstep that calls the handler with the token<workflow ID>:<event key>. Its result is recorded, so a recovered workflow doesn't ask again, and an answer the handler returns in place is reused; - unless the handler answered, a
DBOS.recv_asyncon a topic unique to the wait. DBOS records the message, and the timeout as aDBOS.sleep.
resolve_external_event(token, payload, *, client=None) and fail_external_event(token, reason, *, client=None) in pydantic_ai.durable_exec.dbos send the message. They use DBOS.send_async, or a DBOSClient from a process that didn't launch DBOS, and send in DBOS's portable JSON format. The first message ends the wait. A message sent before the wait starts is kept until it does.
There is no compatibility impact: these waits were refused on DBOS until now, so no in-flight workflow has a different recorded step sequence for them. The DBOS docs describe the steps a wait records, and what changing a wait's name does to in-flight workflows.
Tests (SQLite):
tests/durable_exec/test_dbos_external_events.py:- a stop mid-wait, then a resume that doesn't ask again;
- a fork that replays the recorded message;
- approve through a
DBOSClient, and deny throughfail_external_event; - a timeout;
- a message of the wrong shape;
- an answer in place, reused on replay;
- a late or duplicate resolution, which is ignored;
- a bad token, and an unknown workflow.
- An
AskUsercase, a nested Code Mode approval, and aBackgroundToolsstep-based call that is still running across the wait and through a recovery. That last one is for #14443.
Choices a reviewer may want to weigh in on:
- Resolver signature. The resolvers take the client as an optional keyword,
(token, payload, *, client=None), while Temporal's take it first and required,(client, token, payload). DBOS can send from a launched app without a client. - No retries for the handler step. It takes DBOS's default step options. Retrying could send the approval request twice.
- No
send_to_forks. A workflow forked in the middle of a wait doesn't receive answers sent to the original.
Known limit, not caused by this PR: if a background tool starts a second DBOS step while the wait is pending, recovery can number that step differently and fail with DBOSUnexpectedStepError. DBOS numbers steps in the order they start. A wait makes that gap long, but any long gap in workflow code would do the same. This is being followed up under #14443.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.
🤖 Generated with Claude Code
Summary by cubic
Adds a DBOS ExternalEventWaiter so HandleDeferredToolCalls token handlers and AskUser wait durably in a workflow. Under DBOSDurability, these waits suspend and resume when an answer arrives or the timeout elapses, instead of being refused with a UserError. Closes #13200.
How a wait records
- A step draws the event key, another step calls the handler with the token
<workflow ID>:<event key>, and a third step receives the answer withDBOS.recv_asyncon a topic of the wait's own. - The handler's step result is recorded, so a recovered workflow doesn't ask again; a message sent before the wait starts stays buffered until it does.
resolve_external_eventandfail_external_eventinpydantic_ai.durable_exec.dbos, re-exported frompydantic_ai_harness.dbos, send the answer in DBOS's portable JSON format; an optionalDBOSClientcovers processes that didn't launch DBOS.- The message type is a union of two closed TypedDicts, so only those resolvers' messages resolve the wait. A
DeferredToolResultsdict sent with a plainDBOS.sendno longer resolves it withNone. - No compatibility impact: these waits were refused on DBOS until now, so no in-flight workflow has a different recorded step sequence.
Reviewer notes
- Resolver signature:
(token, payload, *, client=None)with the client optional, while Temporal's is required and first. - No retries for the handler step: retrying could send the approval request twice.
- No
send_to_forks: a workflow forked mid-wait doesn't receive answers sent to the original. - If a background tool starts a second DBOS step while the wait is pending, recovery can number that step differently and fail with
DBOSUnexpectedStepError; followed up under #14443.