Give durable model request hooks the guarded run context, so `ctx.enqueue()` refuses as in other durable hooks

Merged
DO
3d ago
+32-1

Written by Claude Code on behalf of @DouweM.

A @durable_operation model request hook (such as a durable before_model_request) received the unguarded run context, unlike every other durable unit. So ctx.enqueue() inside it didn't raise. The message reached the model on the first run and was silently dropped on replay, because a replayed unit returns its recorded result without re-running the hook.

The model-request branch of the capability operation handler in durable_exec/_base.py now passes the guarded durable_ctx that _durable_model_scope yields, as the non-model branch already does. ctx.enqueue() (and ctx.cancel() / ctx.request_compaction()) in such a hook now raise the standard UserError, matching what docs/durable_execution/*.md already say about durable units. The ambient run context inside the unit was already the guarded one; only the explicit ctx argument bypassed the guard.

This is the "Related, same code path, separate fix" from #15338. The main bug there (history changes from a durable before_model_request aren't kept) stays open.

Verification

  • New regression test test_decorated_model_request_hook_rejects_enqueue on the in-process JSON-codec RecordingDurability engine. It passes with the fix and fails without it (DID NOT RAISE UserError; before the fix the run completed with the enqueued UserPromptPart('later') in history). The asserted message is the same one the non-model branch raises on that engine.
  • All argument-free async tests in tests/durable_exec/test_capability_durable_operation.py pass with the fix (run in-process).
  • No existing caller relied on the unguarded context: no durable model request hook in pydantic_ai_slim, pydantic_ai_harness or the tests calls enqueue/cancel/request_compaction, and no shipped capability declares a durable operation with a ModelRequestContext parameter.
  • ruff check and ruff format --check clean; scripts/durable_exec_contract.py --write produced no change. No public API change.
PR decisions
  • pre-push review 1 of 3 at ae091dcf37ffb2bf19d0dd42881aa8f96e8edc13: no findings (current at ae091dcf37ffb2bf19d0dd42881aa8f96e8edc13). A fresh read-only reviewer checked the full diff against an origin/main policy base, tree 644d9915780308d92d44e43e429a0ba1ec86c318. It confirmed that recover_capability and usage accounting should stay as they are, and that Temporal already guards its own reconstructed context.

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.

Summary by cubic

Fixes durable model request hooks receiving the unguarded run context, so ctx.enqueue() now raises UserError as in other durable units.

Bug Fixes

  • The model-request branch of the capability operation handler now passes the guarded durable_ctx that _durable_model_scope yields, matching the non-model branch.
  • Previously, an enqueued message reached the model on the first run and was silently dropped on replay, because a replayed unit returns its recorded result without re-running the hook.
  • ctx.cancel() and ctx.request_compaction() are also refused now in these hooks.
  • Adds a regression test covering enqueue() rejection in a durable before_model_request.