Give durable model request hooks the guarded run context, so `ctx.enqueue()` refuses as in other durable hooks
Written by Claude Code on behalf of @DouweM.
- Part of #15338
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_enqueueon the in-process JSON-codecRecordingDurabilityengine. It passes with the fix and fails without it (DID NOT RAISE UserError; before the fix the run completed with the enqueuedUserPromptPart('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.pypass 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_harnessor the tests callsenqueue/cancel/request_compaction, and no shipped capability declares a durable operation with aModelRequestContextparameter. ruff checkandruff format --checkclean;scripts/durable_exec_contract.py --writeproduced 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 anorigin/mainpolicy base, tree644d9915780308d92d44e43e429a0ba1ec86c318. It confirmed thatrecover_capabilityand 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_ctxthat_durable_model_scopeyields, 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()andctx.request_compaction()are also refused now in these hooks.- Adds a regression test covering
enqueue()rejection in a durablebefore_model_request.