Let `HandleDeferredToolCalls` token handlers and `AskUser` wait durably in a pgtask task
- Part of #12975
Inside a pgtask 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 a pgtask signal.
PGTaskDurability.wrap_runinstalls anExternalEventWaiterfor runs inside a task. One step draws the wait's event key (a UUID). Another step callsaskwith the token<task id>:<event key>. The task then callsTask.wait_for_signalonpydantic_ai__external_event:<event key>, numbered like the harness's other pgtask steps.resolve_external_event(client, token, payload)andfail_external_event(client, token, reason)frompydantic_ai_harness.pgtaskemit that signal with apgtask.Client. The payload is wrapped in an envelope that a strict union validates, so a signal emitted any other way ends the wait with anExternalEventErrorand is never treated as an answer.- Behaviour observed against a real pgtask worker and Postgres, and pinned in tests:
- The first signal wins.
- A signal emitted before the task reaches the wait is delivered without suspending.
- A timeout is recorded as the wait step's checkpoint. A later attempt that replays doesn't wait again, and a resolution emitted after the timeout doesn't change the outcome.
- Docs: the pgtask page and README get a "Waiting for approvals and answers" section. The shared lists of engines that support durable waits get pgtask once #15486 (Absurd) merges, since both PRs edit the same lines.
Tests (tests/harness/pgtask/test_external_events.py, 12 cases) run against pgtask on PostgreSQL. They cover:
- approve-and-resume without asking again;
- a failure that denies;
- first resolution wins;
- timeout;
- replay after a timeout, with and without a late resolution;
- an answer in place;
- a signal emitted without the envelope;
- bad tokens.
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
Makes HandleDeferredToolCalls token handlers and AskUser wait durably for an answer inside a pgtask task, instead of being refused with a UserError. While waiting, the task suspends on a pgtask signal, so no worker is held and the woken task replays its steps without asking again.
PGTaskDurability.wrap_runinstalls anExternalEventWaiterfor runs inside a task.- New
resolve_external_eventandfail_external_eventhelpers emit the resolution signal with apgtask.Client; a signal emitted by other means fails the wait withExternalEventError. - Tokens are
<task id>:<event key>, so an earlier wait's token never resolves a later one. - Tests pin both signal orderings: emitted after the task suspends (waking it) and emitted before it reaches the wait (still delivered).
- The first signal wins; a timeout is checkpointed and replays don't wait again.
- Docs for waiting on approvals and answers added to the pgtask page and README; the contract file lists pgtask with engines that support durable waits.
Tests
12 new cases in tests/harness/pgtask/test_external_events.py run against a real pgtask worker and Postgres, covering approve-and-resume, denials, first-resolution-wins, timeout, replay-after-timeout, in-place answers, invalid signals, and bad tokens.