Read how `CodeMode` runs in workflow code from `DurabilityEngineSpec`, instead of checking engine names

DO
4d ago
+521-104

Part of #14525

Stacked on #14975 and #14523. The base branch is azure-codemode-13205 (#14523), and this branch also merges #14975's durable-spec-orchestrator-fields, which sets replays_orchestrator_code for DBOS, AWS Lambda, Absurd and pgtask. Merge #14975 and #14523 first.

Why we make these changes

CodeMode checked which engine was running (in_temporal_workflow() or in_durable_task_orchestration()) to decide how to run Monty in workflow code. Engine differences belong on the engine spec (#14525). With #14975's values, #14523's refusal (replays and doesn't stop code that blocks) would also refuse snippets under DBOS, AWS Lambda, Absurd and pgtask. Following Douwe's ruling, a snippet is refused only where it could block workflow code forever, so every engine that can run Code Mode keeps running it.

New public surface

None.

User-visible behavior

CodeMode in workflow codereadsTemporalAzureDBOS, Lambda, Absurd, pgtaskPrefect, Render
refused unless turns run as activitiesreplays_orchestrator_code and not thread_wakeable_loop and not bounds_blocking_orchestrator_codenoyesnono
elapsed-time limit droppedreplays_orchestrator_codeyesyesyesno (was yes)
Monty called through a portalnot thread_wakeable_loopyesyesnono
remote turns run as activitiessame as the portalyesyesnono

Every check reads the run's engine through one lookup, which unwraps a wrapped capability such as prefix_tools(). No engine-name checks remain. Two fallbacks stay: the deprecated TemporalAgent, which has no durability capability, gets Temporal's spec, and a capability-less agent in an Azure orchestration gets Azure's. _durable_turn still calls in_temporal_workflow(), but only to reach Temporal's workflow.patched.

Verification

  • tests/harness/code_mode/test_engine_spec.py parametrizes fake engines that copy the field values of all 8 built-in engines. Each built-in engine's own test module pins its real values (#14975). The tests check:
    • refusal: Azure only
    • elapsed-time limit: dropped only where code is replayed
    • portal: Temporal-like loops only
    • turns as activities: unwakeable loops only, with a durability capability on the agent or for the run, bare or wrapped
    • a wrapped engine counts as durable execution: speculation and the REPL store stay off, the limit is dropped, and its nondeterminism error fails the run
  • Each new test fails when its clause is removed. Code Mode tests under AWS Lambda and Azure pass.

What changes for existing users

  • Under Prefect and Render Workflows, max_duration_seconds applies to snippets again, because neither engine needs a rerun to take the recorded path.
  • A durability capability wrapped by prefix_tools() now counts as durable execution in Code Mode, as it already did for its durable operations. Speculation and the REPL store turn off, and a diverged replay fails the run. Before, Code Mode treated it as non-durable.
PR decisions

2026-11-26 · Field names and defaults · iter -

  • Decision: replays_orchestrator_code: bool = False, thread_wakeable_loop: bool = True on DurabilityEngineSpec; Temporal and Azure Durable Task set True/False; every other engine keeps the defaults, so nothing changes
  • Why: Names decided by Douwe (11/26); defaults chosen so every engine not listed keeps today's path (DBOS/Lambda keep the timer, #12539)
  • Source: https://github.com/pydantic/pydantic-ai/issues/14525

2026-11-26 · DynamicWorkflow reads only thread_wakeable_loop (webinar-aws-lambda's call) · iter -

  • Decision: DynamicWorkflow takes its portal from the spec but keeps in_durable_task_orchestration() for its elapsed-time limit, with a comment linking #14528; CodeMode reads both fields
  • Why: DW drops the limit only under Azure; reading replays_orchestrator_code would drop it under Temporal too. No behaviour change wins; #14528 decides Temporal and then flips DW to the flag. Decided by webinar-aws-lambda, scoping Douwe's field names to no behaviour change
  • Source: https://github.com/pydantic/pydantic-ai/issues/14525

2026-11-26 · Legacy TemporalAgent and Temporal-only remote turns keep in_temporal_workflow() · iter -

  • Decision: durable_engine_spec(ctx) reads the run's durability capability via BaseDurabilityCapability.from_agent and falls back to TEMPORAL_ENGINE_SPEC in a Temporal workflow with no capability; CodeMode's _durable_turn keeps an in_temporal_workflow() guard before workflow.patched
  • Why: The deprecated TemporalAgent attaches no durability capability but runs CodeMode in workflow code (tested in tests/harness/code_mode/test_temporal.py); remote turns as activities are a Temporal-only feature calling the Temporal API, not one of the two spec behaviours
  • Source: https://github.com/pydantic/pydantic-ai/issues/14525

2026-11-26 · Base: stacked on #14527 (590ee7ed34) · iter -

  • Decision: Branch from origin/azure-dynamic-workflow, which contains #14523; PR targets main and lists both as prerequisites
  • Why: Both are open; #14527 introduces the DW Azure checks and contains #14523's in_durable_task_orchestration, so removing the engine checks from both capabilities needs both
  • Source: https://github.com/pydantic/pydantic-ai/issues/14525

2026-11-26 · pre-push review 1 of 3 at f7ff3f28288651be42f2091dee7d3da3b1218743 · iter -

2026-11-26 · pre-push review 1 of 3 at f7ff3f28288651be42f2091dee7d3da3b1218743 · iter -

2026-11-26 · pre-push review 2 of 3 at f7ff3f28288651be42f2091dee7d3da3b1218743 · iter -

2026-11-27 · pre-push review 2 of 3 at f7ff3f28288651be42f2091dee7d3da3b1218743 · iter -

2026-11-27 · pre-push review 3 of 3 at 11fb5585781f9a5fcb316d73c81e5ab19a0a34ee · iter -

2026-11-27 · pre-push review 3 of 3 at 11fb5585781f9a5fcb316d73c81e5ab19a0a34ee · iter -

2026-11-27 · pre-push review 1 of 3 at f7ff3f28288651be42f2091dee7d3da3b1218743 (late report) · iter -

  • Decision: remediated after budget: replays_orchestrator_code docstring + backends.md now say DBOS/Lambda keep the default for now (#12539)
  • Why: call 1's report arrived after call 3; docs/docstring only; budget exhausted, local checks only
  • Source: https://github.com/pydantic/pydantic-ai/pull/14855

2026-11-27 · CI: portal probe in test_engine_spec · iter -

  • Decision: probe the portal with a nested tool that records open portal threads (as the DynamicWorkflow test does), not by the os_access handler's thread
  • Why: CI: without a portal Monty also calls a plain handler on the caller's thread, so the wakeable case failed; the Coder line in the log was an earlier step's script, not the failure
  • Source: https://github.com/pydantic/pydantic-ai/pull/14855

2026-11-27 · CI: API waiver retargeted to v2.76.0 · iter -

  • Decision: ba54486b... waiver now against v2.76.0 (the PR's only waiver); local check shows engine_spec as waived
  • Why: CI's baseline moved to v2.76.0; local ModelSchemaTooComplexError errors come from the branch predating v2.76.0, absent in CI's merge-ref run
  • Source: https://github.com/pydantic/pydantic-ai/pull/14855

2026-11-27 · threads 4714659739/4714659742: object instead of Any · iter -

2026-11-27 · CI coverage: probe's defensive branch · iter -

  • Decision: replace the untaken 'if portals is not None' in the test's probe tool with an assert
  • Why: coverage 81->83 partial; only _through_portal's snippet calls probe() and it always passes portals; test-only
  • Source: https://github.com/pydantic/pydantic-ai/pull/14855

2026-11-27 · Restack on #14523 e0671f4354 (Douwe ruling 24) · iter -

2026-11-27 · Removal commit: #14527 and DynamicWorkflow consumers · iter -

  • Decision: restore #14523's DW README, dynamic-workflow.md, azure worker.py, DW tests; delete azure test_dynamic_workflow.py; drop test_built_in_engine_spec_flags (pinned in #14975)
  • Why: Douwe: drop all DW consumers, DW keeps use_portal=in_temporal; #14975 pins spec values per engine module
  • Source: https://github.com/pydantic/pydantic-ai/pull/14855

2026-11-27 · durable_engine_spec: drop the Azure fallback · iter -

  • Decision: only the Temporal (TemporalAgent) fallback stays; the capability-less Azure test goes
  • Why: with #14523, a capability-less Code Mode agent in an Azure orchestration is always refused by _refuse_unbounded_snippets (no bound turn operation), so the fallback is unreachable
  • Source: https://github.com/pydantic/pydantic-ai/pull/14855

2026-11-27 · Fake replaying engines bound blocking code · iter -

2026-11-27 · #14525 scope · iter -

2026-11-27 · Durable review at f19976595a: one shared engine lookup · iter -

  • Decision: durable_engine_spec and unbounded_replay_engine both read ctx.capabilities via _durability_spec, so a durability capability passed with run(capabilities=...) counts; TemporalAgent fallback kept; regression test on-agent vs for-run asserts turns run as monty_turn operations
  • Why: from_agent saw only the agent's own capabilities, so a per-run Azure-like engine passed the refusal but ran the snippet in orchestrator code with no portal and no limit
  • Source: https://github.com/pydantic/pydantic-ai/pull/14855#issuecomment-7224066930

2026-11-27 · pre-push review budget spent · iter -

2026-11-27 · Wrapped durability capabilities left out · iter -

  • Decision: the shared lookup does not unwrap WrapperCapability, matching #14523's unbounded_replay_engine
  • Why: from_agent unwraps, ctx.capabilities may list only the wrapper; widening it changes #14523's refusal and needs a wrapped-durability test; raised for the reviewer instead
  • Source: https://github.com/pydantic/pydantic-ai/pull/14855

2026-11-27 · Durable review at b668588c6a: unwrap wrapped engines · iter -

  • Decision: _durability_spec unwraps WrapperCapability chains as from_agent and _reject_second_of_a_kind do (inline loop: core has no shared helper); test adds a wrapped (prefix_tools) case, on the agent and for the run
  • Why: ctx.capabilities lists the wrapper, so a wrapped Azure-like engine left durable_engine_spec None while the refusal passed via the Azure fallback: same unbounded hang
  • Source: https://github.com/pydantic/pydantic-ai/pull/14855#issuecomment-7224194393

2026-11-27 · No review for the unwrap fix · iter -

2026-11-30 · Ruling 271=A: refusal rule · iter -

  • Decision: refuse snippets in orchestrator code only when replays_orchestrator_code and not thread_wakeable_loop and not bounds_blocking_orchestrator_code and turns can't run as activities (= Azure only)
  • Why: Douwe 11/30 '271 = A': support every engine we possibly can; DBOS/Lambda/Absurd/pgtask/Prefect/Render keep working
  • Source: https://github.com/pydantic/pydantic-ai/pull/14855

2026-11-30 · Consumer mapping · iter -

  • Decision: elapsed-time drop: replays_orchestrator_code (Prefect/Render regain the limit #13527 dropped); portal: not thread_wakeable_loop; turns-as-activities: same as portal (not replays), so DBOS etc. keep running turns in workflow code
  • Why: Douwe ruling: drop exactly where code is replayed; turns leave workflow code only where the workflow thread blocks on the portal
  • Source: https://github.com/pydantic/pydantic-ai/pull/14855

2026-11-30 · Stacked on #14975 + #14523 · iter -

2026-11-30 · Pre-push budget for this task · iter -

2026-11-30 · pre-push review 1 of 3 at 69cb00a704ff1bb3e18d2fe6659eacf6b1b1f115 · iter -

2026-11-30 · pre-push review 1 of 3 at 69cb00a704ff1bb3e18d2fe6659eacf6b1b1f115 · iter -

2026-11-30 · No second review for call-1 remediation · iter -

2026-11-30 · pre-push review 2 of 3 at 95bcfe10bb1ac6631decd37e3cd7aa2983978d5a · iter -

2026-11-30 · pre-push review 2 of 3 at 95bcfe10bb1ac6631decd37e3cd7aa2983978d5a · iter -

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.