Run tool calls as DBOS child workflows via `child_workflow` tool metadata in `DBOSDurability`

Merged
MP
1mo ago
+550-28

No issue: this is item 1 of the harness durable-parity audit (DBOS child-workflow tool calls, for parity with Temporal's #7329).

Why

Temporal can run a tool call as a child workflow (#7329), but DBOS had no opt-in for this. A nested DBOSDurability agent run from a tool recorded its steps in the parent's history.

New public surface

  • pydantic_ai.durable_exec.dbos.ChildWorkflowConfig (one option, workflow_timeout_sec, which is passed to DBOS's SetWorkflowTimeout)
  • Tool metadata {'dbos': {'child_workflow': ChildWorkflowConfig(...)}}, set on the tool or through SetToolMetadata

User-visible behavior

 delegation = FunctionToolset(id='delegation')

 @delegation.tool_plain(
+    metadata={'dbos': {'child_workflow': ChildWorkflowConfig(workflow_timeout_sec=600)}}
 )
 async def research(topic: str) -> str:
     return (await researcher.run(f'Research: {topic}')).output
-# researcher's steps are recorded in the parent `lead` workflow's history
+# each call runs as workflow `lead__function_toolset__delegation.call_tool__child_workflow`, holding researcher's steps

An async function tool marked this way runs as the DBOS workflow {agent}__function_toolset__{toolset_id}.call_tool__child_workflow. Its inputs are the tool name, args, the tool definition the parent already prepared, the serialized run context and deps. Inside the workflow the tool is rebuilt with FunctionToolset.tool_for_tool_def (so prepare doesn't run again), and the run context and deadline are restored, so the nested agent's steps go into the child's history. On recovery the child resumes from its own checkpoints. Invalid metadata, sync tools, and marked tools in a toolset with no registered child workflow raise UserError.

Verification

What changes for existing users

Nothing: child workflows are registered, and toolsets wrapped, only for toolsets that select child_workflow when the agent is constructed.

PR decisions
  • tool_config_key stays None. Setting it would turn on {'dbos': False} opt-outs and per-tool step-config merging for existing users. The only metadata read is child_workflow.
  • A child workflow is registered only for opted-in toolsets, when the agent is bound: the toolset holds a tool whose metadata selects child_workflow, or a construction-time SetToolMetadata selects it. DBOS needs the workflow registered before DBOS.launch() to recover it, and hashes registered workflows into the application version, so unmarked agents register nothing. A tool selected after construction (@agent.tool(metadata=...) after Agent(...)) raises a UserError in a workflow, explaining how to select it at construction. I didn't add a DBOSDurability flag for that case (YAGNI). This replaces the first version, which registered a workflow for every toolset with an id; douwebot flagged that it changed every user's app version.
  • Toolsets are only wrapped at run time when needed. That is, when a toolset holds a marked tool or a SetToolMetadata marks child_workflow, so unmarked runs keep the original toolset tree.
  • Shared selection helpers: selects_child_workflow, capability_selects_child_workflow and toolset_selects_child_workflow in durable_exec/_base.py are now used by both Temporal and DBOS, so their detection rules can't drift apart. Temporal's behavior is unchanged.
  • ChildWorkflowConfig is validated with a TypeAdapter (extra='forbid'), the same way as Temporal's ChildWorkflowConfig.
  • The child workflow body delegates to a method, because DBOS hashes registered workflow source into the application version.
  • ChildWorkflowConfig has one option, a per-invocation timeout, because DBOS has no other per-child options. Registration-level options (max_recovery_attempts) are shared per toolset.
  • Known limitation: a SetToolMetadata passed at run time, or a prepare function that adds child_workflow, is only honored in toolsets that already register a child workflow. In any other toolset the tool runs inline. Selecting at dispatch would mean registering and wrapping every function toolset.
  • Bot findings: a prepare that reads the full run context failed in the child, now fixed via tool_for_tool_def. Declined, with replies on each thread: re-checking deps serialization (it's DBOS's behavior for every workflow input) and a parent check on the child workflow (it would block DBOS recovery or cost a database read on every call; starting workflows by name already requires system-DB access).
  • Out of scope: parallel marked tools starting child workflows concurrently rely on DBOS's step-ordering rules. That is item 2 (parallel tool calls).
  • Pre-push review: 3 local reviews were run (the 1st couldn't read its inputs). All findings were fixed: contract manifest, loud error for unregistered toolsets including via SetToolMetadata, recovered-child deadline, docs, unmarked-path test.

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.