Let `HandleDeferredToolCalls` token handlers and `AskUser` wait durably in an Absurd task

Merged
DO
13h ago
+659-31

Inside an Absurd task, a HandleDeferredToolCalls handler that takes a resume token, and AskUser, now wait for the answer durably instead of being refused with a UserError. While it waits, the task is suspended on an Absurd event, so no worker holds it.

  • AbsurdDurability.wrap_run installs an ExternalEventWaiter for runs inside a task. One step draws the wait's event key (a UUID). Another step calls ask with the token <queue name>:<event key>, so a woken or retried task doesn't ask again. The task then calls AsyncTaskContext.await_event on pydantic_ai__external_event:<event key>.
  • resolve_external_event(client, token, payload) and fail_external_event(client, token, reason) from pydantic_ai_harness.absurd emit that event with an AsyncAbsurd client. The payload is wrapped in an envelope that a strict union validates, so an event emitted any other way ends the wait with an ExternalEventError and is never treated as an answer. Absurd keeps the first event emitted under a name, so the first resolution wins.
  • A timeout becomes Absurd's event timeout, rounded up to whole seconds. When it expires, the wait raises ExternalEventError, and HandleDeferredToolCalls denies the pending calls.
  • Docs:
    • The Absurd page and README get a "Waiting for approvals and answers" section.
    • The lists of engines that support durable waits now include Absurd.
    • THREAT_MODEL.md notes that anyone who can write to the Absurd database can resolve a wait.

Tests (tests/harness/absurd/test_external_events.py) run against Absurd's real schema on PostgreSQL. They cover:

  • approve-and-resume without asking again;
  • a failure that denies;
  • first resolution wins;
  • timeout;
  • an answer in place that doesn't suspend;
  • an event emitted without the envelope;
  • a sub-second timeout rounded up to whole seconds;
  • a token resolved through a client on another queue;
  • a payload that isn't JSON, rejected without waking the task;
  • AskUser with a token answerer: the answer arrives through the token and the woken task doesn't ask again;
  • bad tokens.

Threat model changes:

  • Narrows: on Absurd, whoever can write to the Absurd database can resolve a wait, through resolve_external_event or by emitting the event directly. This adds Absurd to the existing list of who can resolve a token.
  • Adds: none.

The DBOS waiter in #14522 edits the same engine lists in the docs. Whichever PR merges second takes the other's entries.

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

HandleDeferredToolCalls handlers that take a resume token, and AskUser, now wait durably for an answer inside an Absurd task instead of being refused with a UserError. While waiting, the task suspends on an Absurd event, so no worker holds it.

  • AbsurdDurability installs an ExternalEventWaiter: a step draws the wait's event key, another calls ask with a <queue>:<event key> token, and the task awaits the event, so a woken or retried task doesn't ask again. Each wait's event step is named after its key, so a replay that skips a timed-out wait doesn't hand a later wait the earlier one's event.
  • pydantic_ai_harness.absurd.resolve_external_event and fail_external_event emit the event with an AsyncAbsurd client; payloads are wrapped in an envelope, so an event emitted any other way ends the wait with ExternalEventError, and the first resolution wins. A client on another queue resolves the token too (the event goes to the queue the token names), and a non-JSON payload is rejected with a UserError without waking the task.
  • A timeout becomes Absurd's event timeout, rounded up to whole seconds, with a test pinning that a sub-second timeout waits at least the full time; on expiry the wait raises ExternalEventError, a timed_out step records it so a retried task doesn't wait again or take a late resolution, and pending calls are denied.
  • Tests cover approve-and-resume, failures that deny, first resolution wins, timeouts and replays of timed-out waits, cross-queue resolution, non-envelope and non-JSON payloads, and AskUser waiting on the same envelope.
  • Docs list Absurd among the durable-wait engines, including Permissions' approval_ask, which now works on Temporal, AWS Lambda and Absurd alike, and the Absurd page gains a "Waiting for approvals and answers" section.
  • THREAT_MODEL.md notes that anyone who can write to the Absurd database can resolve a wait.

The DBOS waiter in #14522 edits the same engine lists in the docs; whichever PR merges second takes the other's entries.