Read how `CodeMode` runs in workflow code from `DurabilityEngineSpec`, instead of checking engine names
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 code | reads | Temporal | Azure | DBOS, Lambda, Absurd, pgtask | Prefect, Render |
|---|---|---|---|---|---|
| refused unless turns run as activities | replays_orchestrator_code and not thread_wakeable_loop and not bounds_blocking_orchestrator_code | no | yes | no | no |
| elapsed-time limit dropped | replays_orchestrator_code | yes | yes | yes | no (was yes) |
| Monty called through a portal | not thread_wakeable_loop | yes | yes | no | no |
| remote turns run as activities | same as the portal | yes | yes | no | no |
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.pyparametrizes 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_secondsapplies 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 -
- Decision: reserved; dispatched
- Why: tree 39e410cb7670d55e12104605cceb7b7df450e224; scope 590ee7ed34..f7ff3f2828 (stacked on #14527); first review
- Source: https://github.com/pydantic/pydantic-ai/tree/engine-spec-code-mode-fields
2026-11-26 · pre-push review 1 of 3 at f7ff3f28288651be42f2091dee7d3da3b1218743 · iter -
- Decision: finished, but the harness never delivered its report to the implementer; outcome unknown
- Why: tree 39e410cb7670d55e12104605cceb7b7df450e224; task e4d11a4e850d44b0bf13f0e6ad4a35e9
- Source: https://github.com/pydantic/pydantic-ai/tree/engine-spec-code-mode-fields
2026-11-26 · pre-push review 2 of 3 at f7ff3f28288651be42f2091dee7d3da3b1218743 · iter -
- Decision: reserved; dispatched (same scope, because call 1's report was lost)
- Why: tree 39e410cb7670d55e12104605cceb7b7df450e224; scope 590ee7ed34..f7ff3f2828
- Source: https://github.com/pydantic/pydantic-ai/tree/engine-spec-code-mode-fields
2026-11-27 · pre-push review 2 of 3 at f7ff3f28288651be42f2091dee7d3da3b1218743 · iter -
- Decision: remediated: Azure orchestration fallback in durable_engine_spec for agents without a capability (keeps CodeMode portal+timer for capability-less sub-agents); try_import gating in test_engine_spec; fake durabilities shared in _recording_durability
- Why: tree 39e410cb7670d55e12104605cceb7b7df450e224; scope 590ee7ed34..f7ff3f2828
- Source: https://github.com/pydantic/pydantic-ai/tree/engine-spec-code-mode-fields
2026-11-27 · pre-push review 3 of 3 at 11fb5585781f9a5fcb316d73c81e5ab19a0a34ee · iter -
- Decision: reserved; dispatched (follow-up)
- Why: tree 2e9fd8bb34801eb4328f371aadad26d619f5238e; scope f7ff3f2828..11fb558578 plus prior findings
- Source: https://github.com/pydantic/pydantic-ai/tree/engine-spec-code-mode-fields
2026-11-27 · pre-push review 3 of 3 at 11fb5585781f9a5fcb316d73c81e5ab19a0a34ee · iter -
- Decision: no findings
- Why: tree 2e9fd8bb34801eb4328f371aadad26d619f5238e; follow-up scope f7ff3f2828..11fb558578; verified review-2 findings fixed
- Source: https://github.com/pydantic/pydantic-ai/tree/engine-spec-code-mode-fields
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 -
- Decision: test_engine_spec drops Any (the handler that needed it is gone); in_orchestrator_code takes RunContext[object]
- Why: valid tests-and-dependencies findings
- Source: https://github.com/pydantic/pydantic-ai/pull/14855#discussion_r4714659739
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 -
- Decision: merge #14523 (with #14975's fields); spec files, waiver, Azure README and DynamicWorkflow resolved to #14523's side; code_mode/_toolset.py taken from #14523 with only the spec-driven replays/portal reapplied (my moved in_temporal_workflow guard would have disabled #14523's Azure turns-as-activities)
- Why: #14527 closed; fields come from #14975 via #14523; #14855 keeps only Code Mode consumers
- Source: https://github.com/pydantic/pydantic-ai/pull/14855
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 -
- Decision: ReplayingDurability and test_code_mode's _ORCHESTRATOR_SPEC set bounds_blocking_orchestrator_code=True (like Temporal)
- Why: otherwise #14523's refusal fires before the timer path the tests exercise
- Source: https://github.com/pydantic/pydantic-ai/pull/14855
2026-11-27 · #14525 scope · iter -
- Decision: body says Part of #14525; DynamicWorkflow's in_temporal_workflow() stays until its orchestrator-code path follows bounds_blocking_orchestrator_code
- Why: Douwe's decision; #14525 stays open, no new issue
- Source: https://github.com/pydantic/pydantic-ai/issues/14525
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 -
- Decision: no 4th review; local checks only (ruff, manifest, API check)
- Why: calls 1-3 of 3 used (log); the skill forbids a fourth
- Source: https://github.com/pydantic/pydantic-ai/pull/14855
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 -
- Decision: budget spent (3/3); targeted checks only: ruff, manifest, API check v2.76.0
- Why: skill forbids a fourth pre-push review
- Source: https://github.com/pydantic/pydantic-ai/pull/14855
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 -
- Decision: merged origin/azure-codemode-13205 (2cced4164f) then origin/durable-spec-orchestrator-fields (ad6cb3b83f); conflicts resolved to spec-driven replays/portal plus #14523's reraise/check_monty_sandbox/worker_failure
- Why: #14523's base had only the first #14975 commit; corrected DBOS/Lambda/Absurd/pgtask values needed for the rule
- Source: https://github.com/pydantic/pydantic-ai/pull/14855
2026-11-30 · Pre-push budget for this task · iter -
- Decision: previous task spent 3/3; Douwe's delegated task asks for one fresh review, counted as call 1 of 3 for this task
- Why: skill counts per PR during one task; user instruction
- Source: https://github.com/pydantic/pydantic-ai/pull/14855
2026-11-30 · pre-push review 1 of 3 at 69cb00a704ff1bb3e18d2fe6659eacf6b1b1f115 · iter -
- Decision: reserved; dispatched
- Why: tree 28fd16ac3430b9081cd82db6d3bf5ab414b9878b; scope faad5683d4..HEAD (ruling) with 2cced4164f...HEAD as context
- Source: https://github.com/pydantic/pydantic-ai/pull/14855
2026-11-30 · pre-push review 1 of 3 at 69cb00a704ff1bb3e18d2fe6659eacf6b1b1f115 · iter -
- Decision: remediated in 1c378a1990: Prefect/Render docs wording (Prefect reruns flow code; caches by input) in code-mode.md, README, skill ref; new test that turns run as activities only on unwakeable loops (fails with the old gate)
- Why: tree 28fd16ac34; scope faad5683d4..69cb00a704; first dispatch (task 0086fe6e) got no file access and was cancelled before reviewing, re-dispatched as the same call (d08f5936)
- Source: https://github.com/pydantic/pydantic-ai/pull/14855
2026-11-30 · No second review for call-1 remediation · iter -
- Decision: push 1c378a1990 without another local review
- Why: Douwe's task asked for one fresh reviewer and one push; remediation applies the reviewer's exact findings (docs wording + one test); hosted douwebot reviews the pushed head
- Source: https://github.com/pydantic/pydantic-ai/pull/14855
2026-11-30 · pre-push review 2 of 3 at 95bcfe10bb1ac6631decd37e3cd7aa2983978d5a · iter -
- Decision: reserved; dispatched (follow-up)
- Why: tree 4d97bd3f1c7a10a371153770c70ce0ac8eb6d021; scope 1c378a1990..95bcfe10bb1ac6631decd37e3cd7aa2983978d5a (douwebot findings on 1c378a1990: shared unwrapped lookup, inline uses_portal); Douwe authorized a 2nd push
- Source: https://github.com/pydantic/pydantic-ai/pull/14855
2026-11-30 · pre-push review 2 of 3 at 95bcfe10bb1ac6631decd37e3cd7aa2983978d5a · iter -
- Decision: no findings
- Why: tree 4d97bd3f1c; follow-up scope 1c378a1990..95bcfe10bb; call-1 findings verified still fixed
- Source: https://github.com/pydantic/pydantic-ai/pull/14855
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.