Dispatch Temporal tool-call events to the event stream handler in one activity per burst
Ā
DO
13h ago+1081+1081+0123456789012345678901234567890123456789-42-42-01234567890123456789
Part of #7631: it batches the tool-call events; each tool result still crosses in an event-handler activity of its own (see below), so #7631 stays open for that half.
Under Temporal, every workflow-side event delivered to an event_stream_handler crossed the durable boundary as its own activity, awaited serially. #7631 measured ~0.15 s and ~25 KB of history per event, and the tools of a model response could not start until every call event's activity had completed (thanks @tarik-gtlaw for the measurements and the liveness analysis).
What changes
- Temporal dispatches tool-call events in bursts. While a node's stream runs, the
FunctionToolCallEvents that become ready during one event-handler activity are sent together in the next one. Every other event, including each tool result and aModelResponseRejectedEvent, still gets an activity of its own, so N parallel tool results still take N event-handler activities before the next model request; they can now overlap with tools that are still running. Batching results too would need a bound that stays stable across upgrades, which is left to #7631. The handler is still invoked once per event, in order. Tools start while call events are being handled, because the stream is pulled ahead rather than drained up front (draining would withhold every tool call until all tools had finished, as the issue explains). - Burst boundaries are replay-stable. Which events share a burst depends on which were ready when the previous activity completed. Temporal replays the workflow's scheduling exactly, so a replay forms the same bursts. In-flight histories recorded before this change replay per event, selected by the
pydantic-ai-batched-event-dispatchpatch marker. - Handler order is preserved across model requests. A model-request activity hands its events to the handler live, so before one starts, the workflow waits for the events pulled before it to be handled. If one of those handler activities failed, the model request doesn't start: it raises a
RuntimeErrorchained from the handler's error, and the event stream raises the handler's error itself. - Every other engine keeps one durable unit per event, unchanged. On DBOS, Prefect and the harness engines, replay runs on a regular event loop where recorded steps return immediately, so timing-based bursts could form differently on replay and change the step sequence. DBOS also reserves each tool-emitted event's step position at
ctx.emit(), which needs one step per event.BaseDurabilityCapability._batch_event_stream_dispatchis off by default, and only Temporal enables it. - Wire compatibility. A one-event unit keeps the single-event wire shape in every transport, so per-event dispatch is byte-for-byte what it was, and Prefect's cache keys are unchanged. Temporal writes a burst of several events as a list and decodes both shapes.
- The public
EventStreamHandlerParamsis unchanged frommain. It still carries oneevent, so a custom durable backend's event-handler operation sees exactly what it did before. A burst of several events travels in a private subclass that only Temporal's batched dispatch builds, so custom backends never see a burst.
Tests
- Tests:
tests/durable_exec/test_durable_operation.pypins the burst behaviour (accumulation while a dispatch is in flight, handler and producer failures, model-delta backpressure, tools that keep running during a dispatch) and the wait before model requests (including when an earlier dispatch failed, and under cancellation). - A non-batching engine is pinned to one unit per event.
- A backend that dispatches per event receives one public
EventStreamHandlerParamsper event, withparams.eventset, and the class's fields are pinned toeventandrun_context. - Temporal replay of patched and unpatched histories, plus the existing completion-order and delta-event replay tests, pass. The completion-order test now reads each event-handler activity's input as a burst.
- The DBOS step-reservation and fork tests pass unchanged.
š¤ Generated with Claude Code