Fail a durable run on a `UserError` from a `CodeMode` sandboxed call instead of sending the model a retry

Merged
DO
2d ago
+138-15

Written by Claude Code on behalf of @DouweM.

Closes #13201

Why we make these changes

On an engine without an external-event waiter (DBOS, Prefect, Azure, Absurd, pgtask), the refusal UserError from wait_for_external_event reached the CodeMode sandbox when a sandboxed tool needed an approval resolved through a token-taking HandleDeferredToolCalls handler. The model saw a run_code retry about an unsupported engine, and a snippet's try/except Exception hid it completely, so the run succeeded. The same tool called directly fails the run. Per Douwe's decision on the issue, the run now fails.

In a durable run, UserError joins the errors CodeMode's Monty executor re-raises instead of delivering into the sandbox. This is the mechanism #12539 added for engine nondeterminism errors. The feed stops, the session resets, and the run fails with the original UserError. Plain (non-durable) runs build an empty re-raise set, as before.

New public surface

none

User-visible behavior

agent = Agent(model, toolsets=[FunctionToolset([Tool(delete_file, requires_approval=True)])],
              capabilities=[CodeMode(tools=['delete_file']),
                            HandleDeferredToolCalls(handler=lambda ctx, requests, token: None),
                            DBOSDurability()])
# inside a DBOS workflow, the model runs: try: await delete_file(...) except Exception: ...
# before: the run succeeds; the snippet got "caught: DBOS does not support waiting for external events yet ..."
# after:  UserError: DBOS does not support waiting for external events yet ...

Verification

  • tests/harness/code_mode/test_dbos.py::test_refused_wait_in_nested_call_fails_the_workflow: fails without the fix (DID NOT RAISE UserError). With the fix, the workflow fails with the refusal, and the model is asked only once, even though the snippet wraps the call in try/except.
  • test_snippet_still_catches_the_error_outside_a_workflow[misconfigured]: outside a workflow (DBOSDurability transparent), a nested UserError still reaches the snippet as before.
  • One engine file is enough for the refusal: the change sits in engine-agnostic CodeMode code keyed on an active durability capability, not on an engine. DBOS on SQLite runs in-process.
  • tests/harness/code_mode/test_temporal.py::test_nested_approval_waits_for_the_decision_and_the_snippet_gets_the_result: the test from the issue's second comment, using HandleDeferredToolCalls with a token-taking handler (the replacement for the deprecated RequestInput). Under TemporalDurability, a snippet's nested call to an approval-required tool waits in the workflow. The approver resolves the token with pydantic_ai.durable_exec.temporal.resolve_external_event, the snippet gets the tool's result and the run finishes. The history then replays without asking again.

What changes for existing users

In a durable run, every UserError from a sandboxed call fails the run instead of reaching the model as a retry. That includes CodeMode's own error for a deferred or approval-required call that no handler resolved, and a host external_modules function raising UserError. Plain CodeMode runs are unchanged.

PR decisions

2026-12-02 · scope: durable runs only; UserError joins the executor's reraise · iter -

  • Decision: In durable runs a UserError from any sandbox call (nested tool call, CodeMode's unresolved-deferral wrapper, host module function) fails the run; non-durable CodeMode unchanged
  • Why: Douwe's ruling on #13201 (fail the run, re-raise the UserError); durable-only keeps the wider plain-CodeMode surface unchanged per AGENTS.md; delegating agent approved option (A) without a marker path
  • Source: https://github.com/pydantic/pydantic-ai/issues/13201

2026-12-02 · pre-push review 1 of 3 at 1e5752a3f2f3b4be8c18ce320b5359e106e5e2f9 · iter -

2026-12-02 · pre-push review 1 of 3 at 1e5752a3f2f3b4be8c18ce320b5359e106e5e2f9 · iter -

2026-12-02 · pre-push review 2 of 3 at 54644433e70e5559429036468dbaac155049d5ee · iter -

2026-12-02 · pre-push review 2 of 3 at 54644433e70e5559429036468dbaac155049d5ee · iter -

2026-12-02 · Temporal nested-approval test added · iter -

  • Decision: Include the Temporal positive test from the issue's 2nd comment in this PR (supersedes the earlier deferral)
  • Why: Douwe asked for it alongside the fix; the one-test-file rule limits local runs per step, not files a PR touches (delegating agent)
  • Source: https://github.com/pydantic/pydantic-ai/issues/13201

2026-12-02 · pre-push review 3 of 3 at 841369a3494dcd02ed290fd6524495a05b184c81 · iter -

2026-12-02 · pre-push review 3 of 3 at 841369a3494dcd02ed290fd6524495a05b184c81 · iter -

2026-12-02 · pre-push remediation review at 21e8007de487d499b425952f544a8fed0da594f9 · iter -

2026-12-02 · pre-push remediation review at 21e8007de487d499b425952f544a8fed0da594f9 · 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.

Summary by cubic

Fixes #13201: in a durable run, a UserError from a CodeMode sandboxed call now fails the run instead of reaching the sandbox, where a try/except could hide it and let the run succeed. This matches direct tool calls, which already fail the run; non-durable CodeMode runs are unchanged.

A Temporal test covers the positive path: a sandboxed approval-required call waits, is recorded in the workflow, and resolves through the handler's token.