Support `CodeMode` under Azure Durable Task

DO
2d ago
+618-53

Merge after #14975.

CodeMode under AzureDurableTaskDurability failed on its first run_code. Monty's async bindings complete an awaited call by waking the loop from Monty's I/O thread. The Durable Task orchestrator loop (_TrampolineLoop) can't be woken that way, so the orchestration failed with "Agent code awaited non-durable work inside a Durable Task orchestration".

Fixing that alone would leave a worse problem. Durable Task replays orchestrator code, so a snippet that runs there can't have an elapsed-time limit (replay could take a different branch), and unlike Temporal, whose deadlock detector fails a workflow task that doesn't yield within 2 seconds, Durable Task never stops orchestrator code that blocks. A model-written while True: pass would hold the orchestrator thread forever. So under Azure, a snippet never runs in orchestrator code:

  • Qualifying configs run each turn as an activity. This reuses the existing monty_turn durable operation, which the Azure backend already registers as an activity. It needs a remote monty_sandbox on a server that stores sessions, no os_access, no 'overlay' or write_bytes_limit mounts, and a CodeMode with an id that is on the agent when it's built (not from a DynamicCapability). These are the same conditions Temporal uses to run turns as activities. max_duration_seconds applies inside the activity, so a CPU-bound turn is stopped and the model gets the usual time-limit retry.
  • Every other config raises UserError on the first run_code, naming each change it needs. So does a server that stores no sessions: under Temporal that case falls back to running the snippet in workflow code, which isn't safe here.
  • DynamicWorkflow is always refused under the same condition. Its scripts run local Monty in orchestrator code. Follow-up: #14974.

The condition comes from the engine spec fields added in #14975: replays_orchestrator_code and not bounds_blocking_orchestrator_code. Today only Azure Durable Task meets it. An agent with no durability capability of its own that runs inside an orchestration, such as one an inline tool runs, gets Azure's spec. workflow.patched stays Temporal-only, since Azure has no in-flight runs from before turns became activities.

The portal fix stays for the code that still runs in orchestrator code under either engine:

  • in_durable_task_orchestration() in _monty_exec.py checks the bridge's current_loop() through sys.modules, so the optional extra is never imported.
  • get_session(in_temporal_workflow=...) / restore(...) are renamed to use_portal. These are private helpers.

Tests (tests/harness/azure_durable_task/test_code_mode.py, on the stored-session server double used by the Temporal remote-turn tests):

  • A snippet with gathered nested calls, a durable sleep, and a dependent call. Each turn is a monty_turn activity with a pinned name. The run parks on the timer and resumes on a new worker without repeating a turn or tool call, and no portal thread is opened in orchestrator code.
  • while True: pass under max_duration_seconds=0.1 is stopped inside its activity: the model sees the time-limit retry and a reset session.
  • Parametrized refusals: local sandbox, os_access, overlay mount, write_bytes_limit mount, id=None, DynamicCapability. Further tests cover a config with several problems (all are named), a server that stores no sessions, an agent with no durability capability running inside an orchestration, and DynamicWorkflow.
  • The Temporal remote-turn suite is unchanged and green.

The Code Mode, Azure and Dynamic Workflow docs pages and READMEs, the skill reference, and the CodeMode field docstrings describe the Azure behavior.

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

Adds CodeMode support under AzureDurableTaskDurability. Monty's async bindings complete calls by waking the loop from its own I/O thread, but the Durable Task orchestrator loop can't be woken that way, and since Durable Task replays orchestrator code with no way to stop code that blocks, snippets can't run there. Each CodeMode turn now runs as an activity, so max_duration_seconds applies.

Changes

  • Routes Monty calls through the anyio blocking portal Temporal workflows use, driven by three new DurabilityEngineSpec fields: replays_orchestrator_code, thread_wakeable_loop, and bounds_blocking_orchestrator_code.
  • Runs each turn as an Azure activity and raises UserError on the first run_code for configs that can't: a local sandbox, os_access, overlay or write-limited mounts, no capability id, a DynamicCapability, or a server without a session store.
  • Refuses DynamicWorkflow under Azure, where a script in orchestrator code would have no time limit.
  • Adds in_durable_task_orchestration() and renames the private get_session/restore argument to use_portal; Temporal-only behavior is unchanged.
  • Redacts a failed turn activity's error so the Durable Task SDK records no context that could carry a pool's URL or credentials, and checks for portal-thread leaks by anyio's thread naming.
  • Tests cover resume on a new worker after a durable timer with no repeated calls or leaked portal threads, a CPU-bound turn stopped by max_duration_seconds, and engine-spec defaults; docs and READMEs document the Azure path.