Let `HandleDeferredToolCalls` token handlers and `AskUser` wait durably in a pgtask task

Merged
DO
1d ago
+548-9

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_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 <task id>:<event key>. The task then calls Task.wait_for_signal on pydantic_ai__external_event:<event key>, numbered like the harness's other pgtask steps.
  • resolve_external_event(client, token, payload) and fail_external_event(client, token, reason) from pydantic_ai_harness.pgtask emit that signal with a pgtask.Client. The payload is wrapped in an envelope that a strict union validates, so a signal emitted any other way ends the wait with an ExternalEventError and 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_run installs an ExternalEventWaiter for runs inside a task.
  • New resolve_external_event and fail_external_event helpers emit the resolution signal with a pgtask.Client; a signal emitted by other means fails the wait with ExternalEventError.
  • 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.