Fail a `CodeMode` run when a DBOS replay diverges inside `run_code`, and drop the elapsed-time limit under every durable engine

Merged
AD
6d ago
+438-74

Why we make these changes

Under DBOSDurability and AWSLambdaDurability, a recovered execution could succeed with wrong results when tool calls came from Code Mode's run_code:

  1. DBOS raises DBOSUnexpectedStepError, and the AWS Lambda SDK (2.0 and later) NonDeterministicExecutionError, to fail a replay that diverged from its recorded steps. Code Mode delivered it into the sandbox, so a snippet's try/except hid it, and an uncaught one became a model retry.
  2. The elapsed-time limit was dropped only under Temporal. DBOS and Lambda replay run_code too, so a snippet that hit the limit on one side of a crash called its tools in another order, and a call received another call's saved result.

Part 2 follows the maintainer comment on the issue: use in_durable_execution(ctx) for every engine. That comment also asks for crash-and-replay tests on DBOS and Lambda; both are here.

New public surface

  • DurabilityEngineSpec.nondeterminism_error_types, next to cancellation_error_types. Defaults to (). DBOS sets (DBOSUnexpectedStepError,); AWS Lambda sets (NonDeterministicExecutionError,), which exists at the >=1.7.0 floor but is raised only by SDK 2.0 and later.
  • Other engines keep () until a divergence reaching run_code is reproduced on them: Temporal, Prefect, Azure Durable Task, Absurd, pgtask and render were not reproduced here.

User-visible behavior

 code_mode/_toolset.py :: CodeModeToolset._execute_code()
  └─ _monty_exec.py :: MontyExecutor.run()
     └─ _await_external() / _wrap_gathered() / _close_pending()
-       └─ DBOSUnexpectedStepError raised inside the snippet
+       └─ DBOSUnexpectedStepError ends the feed
  └─ _feed_failure_retry()
-    └─ ModelRetry
+ └─ re-raised: the workflow fails, as for a direct tool call

Verification

  • test_dbos.py: crash and resume a DBOS workflow whose replay diverges (parallel and sequential tools); the time limit is off in a workflow; outside a workflow the snippet still catches the error.
  • test_reraise.py: awaited, gathered, sequential and never-awaited calls, plus cancellation is kept.
  • test_aws_lambda.py test_a_diverging_step_inside_run_code_fails_the_execution: the test fake now raises the SDK's NonDeterministicExecutionError on a name mismatch, as SDK 2.0's validate_checkpoint does. Without the spec field the snippet catches it and the replay returns the first run's answer.
  • The issue's SIGKILL repro.py on this branch: code and code_try now fail like native; timeout (paused) is now correct.

What changes for existing users

[!WARNING]
Compatibility impact, permitted as a bug fix under the version policy. Only durable runs change:

  • Under DBOS and AWS Lambda, a replay that diverges inside run_code now fails the run instead of returning a result built from another step's checkpoint. Migration: deploy changed agent code as a new DBOS application version or Lambda published version, as the engine docs already require.
  • CodeMode's max_duration_seconds is now off under every durable engine, not only Temporal, including an explicit override. Migration: move time-bounded work into a tool, or use a remote pool's request_timeout as the wall-clock bound. Monty has no replay-safe CPU budget to use instead.

Follow-ups (not reproduced here, to be filed): DBOS's DBOSStepNondeterminismError (absent from the dbos>=2.10.0 floor), and DynamicWorkflow, which wraps sub-agent errors in RuntimeError.

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.