Add Azure Durable Task durability to `pydantic_ai_harness`
Closed
TA
1mo ago+5080+5080+0123456789012345678901234567890123456789-7-7-0123456789
Fixes #4764. First of a small stacked series, as discussed with @DouweM on that issue: this PR is the core capability; #11204 adds continue-as-new. Azure Functions hosting will ship from an Azure owned package that depends on this extra, not from the harness (#11205 was closed for that reason). Landing via #11332, which carries these commits plus the maintainer side CI changes; review comments below track the changes made since the first revision.### Checklist- [x] No **breaking changes** in accordance with the [version policy](https://github.com/pydantic/pydantic-ai/blob/main/docs/version-policy.md).- [x] Any permitted **compatibility impact** has a label, warning, migration, release note, and exact API-check waiver. *(Not applicable: opt-in harness integration, no existing behaviour changes.)*- [x] **PR title** is fit for the [release changelog](https://github.com/pydantic/pydantic-ai/releases).### SummaryAdds `AzureDurableTaskDurability`, a durable execution engine for Azure Durable Task (Durable Task Scheduler, and any host that runs a `durabletask` worker), built on the public `pydantic_ai.durable_exec` builder: `BaseDurabilityCapability` + a `RegisteredOperationBackend` with `JSON_CODEC`, `ParameterTransports` and a scheduler-safe `DurableOperationNamer`. Targeting the [`durabletask`](https://github.com/microsoft/durabletask-python) layer means one capability covers the standalone SDK and the Scheduler.Model requests, tool discovery/calls, MCP calls, capability operations and event handling run as activities. Users write their own orchestrator and call the agent through `run_durable`; starting runs, awaiting results and raising events go through the SDK's own client. Outside an orchestration, agents behave exactly as before.### Public API```pythonfrom pydantic_ai_harness.azure_durable_task import ( AzureDurableTaskDurability, # Agent(..., capabilities=[AzureDurableTaskDurability(tool_options=RetryPolicy(...))]); .register(worker) registers the activities run_durable, # result = yield from run_durable(ctx, lambda: agent.run(prompt)) inside your orchestrator)```Only the capability is lazily re-exported from the harness root.### Design notes- **Async bridge.** Durable Task Python orchestrators are generators; Pydantic AI is async throughout. `_bridge.py` drives the agent coroutine inside the orchestrator and surfaces durable tasks as awaitables. Parallel tool calls resume per completion via `when_any`; timers map to durable timers; the drain loop processes one ready snapshot per turn so a self-rescheduling task cannot starve cleanup.- **Dependencies are serializable** and rebuilt on the worker per attempt (Temporal parity). A worker-local resource factory is planned as a follow-up. Activities get a snapshot of the `RunContext` via the shared `DurableRunContext`, not live run state.- **Approvals and deferred tools are an orchestrator pattern**, documented with a complete example: the agent returns `DeferredToolRequests`, the orchestrator waits on external events, then continues with `DeferredToolResults` (`ToolApproved`, `ToolDenied`, `ModelRetry`, results). The docs cover the two sharp edges the orchestrator author owns: Durable Task folds event names (derive them from an encoded call id) and decisions should be bound to the pause they were reviewed in.- **Persisted names are scheduler-safe.** The Scheduler accepts 1-100 characters from `[A-Za-z0-9_.`+-:]`; the namer escapes upstream's journal names injectively, reserving the hyphen (so the agent toolset id `` becomes `-x3C-agent-x3E-` and underscores pass through unchanged), caps length with a stable hash suffix, and the full name set is pinned by test. Pinning the emulator to `v0.0.9` is what exposed this: it validates names strictly, the current `latest` build does not.- **Guarantees.** Activities are at-least-once; deterministic failures (serialization, `UserError`) are not retried; the docs ask for idempotent tools.- **Configuration mirrors Render**: per-role `model_options`, `tool_options`, `event_options`, `capability_options` and `resolve_tool_options`, with `tool_config_key='azure_durable_task'` so a single function tool can opt out of durable execution through `metadata={'azure_durable_task': False}`.### Tests and CI- `tests/harness/azure_durable_task/`: in-process, deterministic, SDK-backed fixture (real `durabletask` executor, synthetic history), `TestModel`/`FunctionModel`, no network, no subprocesses. Covers replay, retries in both directions, approvals/denials/deferred results via the orchestrator pattern, strict/`Json` outputs, timers, parallel wakeups, failure propagation, workspace reconstruction, run-context transport policy, Python 3.14 loop handling, operation-name pins. 100% statement + branch coverage including the tests themselves.- `src/pydantic_ai_harness/integration_tests/azure_durable_task/` + `make integration-azure-durable-task`: live tests against the Durable Task Scheduler emulator pinned to `mcr.microsoft.com/dts/dts-emulator:v0.0.9` (also verified against the current `latest` digest), same `*_TEST_ENDPOINT` / `*_REQUIRE_LIVE` conventions as the MongoDB/Postgres/Redis suites. Includes a replacement-worker replay scenario and the approval pattern end to end. Skips when no emulator is reachable.- The emulator CI job and detector line for `harness-integration-changes` are in a comment below for a maintainer to carry (external PRs can't touch `.github/`).### Docs`docs/harness/azure-durable-task.md` (+ mirrored package README), row in the durable execution overview, navigation entry, harness index. Sections: run locally with the emulator, deploy to Azure Durable Task Scheduler (identity, task hubs, worker/client topology), replay and side effects, dependencies, approvals and deferred calls, retries and inline tools, persisted names, known limitation.### Validation| Check | Result || --- | --- || `make format` / `make lint` | clean || `pyright` (strict) on package + tests + live tests, `mypy` on package | 0 errors || `pytest tests/harness/azure_durable_task` with coverage `--fail-under=100` (source and tests) | 116 passed, 100% || docs parity / navigation / import-lightness / doc snippets | passed || `make integration-azure-durable-task` against the emulator (`v0.0.9` in upstream CI, current `latest` locally) | 9 passed || `git diff --check`, `uv lock --check` | clean |