Make durable-exec MCP `get_tools` caching replay-deterministic

Merged
DS
5mo ago
+262-1034

Problem

The Temporal and DBOS MCP toolset wrappers cached tool definitions on the process-shared toolset instance (self._cached_tool_defs) and consulted that cache in workflow code to decide whether to schedule the get_tools activity/step.

Because a durable agent is defined at module level and shared by every workflow execution in the worker process, whether a workflow emitted a get_tools command depended on what earlier runs had warmed in that process — not on the workflow's own history. Durable replay/recovery requires that decision to depend only on the workflow's recorded history, so a history recorded with a warm cache fails replay on a cold process, and one recorded cold fails after the shared cache warms:

  • Temporal: TMPRL1100 nondeterminism (activity type of the scheduled event doesn't match the command).
  • DBOS: the analogous DBOSUnexpectedStepError — DBOS matches steps positionally, so a conditionally-skipped get_tools step shifts every subsequent step id on recovery.

Triggers: a worker restart (deploy, crash, scale-in) mid-workflow, or a same-process replay of the first run after worker boot (e.g. sticky-cache eviction). Exposure is anyone running MCP toolsets under Temporal/DBOS with the default cache_tools=True.

Fix

Move the cache off the process-shared instance and onto per-run state:

  • New GraphAgentState.mcp_tool_defs_cache, exposed by reference as the private RunContext._mcp_tool_defs_cache field (repr=False) and wired in build_run_context. It's a durable-exec implementation detail, not public API — hence the _ prefix; the durable-exec wrappers reach it under a # pyright: ignore[reportPrivateUsage].
  • Each wrapper's get_tools reads/writes ctx._mcp_tool_defs_cache[...]: on a miss it schedules the activity/step and stores the result; on a hit it returns the cached defs and schedules nothing. The shared caching/scheduling logic is centralized in the two base classes behind a new _cache_tools hook.

GraphAgentState is recreated per agent.run() and reconstructed identically when the workflow re-executes from the start on replay/recovery, so mcp_tool_defs_cache is empty at the start of every execution attempt and repopulates in identical get_tools call order. The decision to schedule get_tools therefore depends only on the workflow's own history and is replay-deterministic — while preserving the per-run optimization from #4331 (the per-step get_tools calls within a run still collapse to a single activity/step).

Tests

tests/test_temporal.py::test_temporal_mcp_get_tools_replay_deterministic is a Replayer-based regression test (adapted from the issue's repro) covering both directions — a warm-recorded history replayed on a freshly-constructed cold instance, and a cold-recorded history replayed after the process cache warms — asserting no nondeterminism and that each run independently records exactly one get_tools activity. It fails on main (the warm run records zero get_tools events) and passes here. Companion tests assert cache_tools=False schedules get_tools every model request (Temporal + DBOS), and a DBOS unit test covers the run-cache-hit path.

Behavior change

The cache was process-wide across runs on main; it is now per-run. A long-lived worker therefore re-lists an MCP server's tools once per run rather than once per process — the intended cost of replay determinism. Within a run, behavior is unchanged (tools listed once, reused for every model request).

Note for reviewers

This touches the cache added in #4331 (cc @DouweM), so it should park for Douwe's review.

Known adjacent follow-ups (out of scope here)

Found during review of this change, kept out of scope:

  • #5884 — get_instructions has the same replay-nondeterminism bug class (Temporal + DBOS).
  • #5885 — DBOS id-less MCP leaf toolsets collide on the per-run tool-defs cache key.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • PR title is fit for the release changelog.