Reserve DBOS handler step positions during tool `ctx.emit()`

Merged
DS
1mo ago
+706-39

This pull request was posted by codex-tui using gpt-6.1-sol on behalf of David.

Closes #10300

Why we make these changes

A tool and its stream consumer draw DBOS step IDs concurrently, so handler I/O can change the recorded order on recovery. Reserve a tool-emitted event's handler position synchronously during ctx.emit(), then execute the handler later from the stream consumer. emit keeps returning without waiting for the handler.

[!WARNING]
Compatibility impact: Tools that emit events and make durable calls record a new handler-step order; older in-flight workflows may fail recovery across this upgrade.

Why this can ship in a minor release: This fixes recovery relying on undocumented, timing-dependent journal interleaving.

Migration: Finish in-flight workflows before upgrading, or deploy a new DBOS application version. See the DBOS upgrade warning.

New public surface

None.

User-visible behavior

progress takes its handler step's position before fetch(). The handler still runs live at stream position, after immediate listeners settle, and can wait for later tool work.

await ctx.emit(progress)
await fetch()

Verification

  • Issue regression: fork every journal step, wrapped/unwrapped capabilities, repeated event objects and journal-order pin; exact main fails replay.
  • Immediate + stream + durable calls and parallel immediate order: every-step forks, stable A/B order and consumer context. Mixed-replay live/fork, no-handler journal, nested ownership, error/cancellation cleanup, legacy/in-step behavior and core event compatibility also pass.
  • Planning + SQLite StepPersistence: pins live handler engagement and delivery order before the fork; recovery preserves journaled effects. Exact main fails. Targeted tests, Pyright, Ruff and commit hooks pass.

What changes for existing users

Only workflow-level tool-call emissions reserve handler positions. Hook emissions outside tool calls, no-handler runs, legacy recordings, in-step side effects, listener timing and stream delivery retain their existing behavior.

DBOS API coupling

Maintainer review: please weigh the private API dependency on dbos._context.snapshot_step_context, dbos._core.run_step_async and dbos._dbos._get_dbos_instance. Their signatures and parallel-immediate live/fork behavior were checked on DBOS 2.10.0, 2.29.0 and 3.3.0. The call sites carry a TODO linking the public API request.
PR #7873 introduces per-burst dispatch on the same DBOS dispatch path; whichever PR lands second will adapt to the first.

PR decisions

2026-10-23 · Scope review to tracked lifecycle · iter -

2026-10-23 · DBOS workflow-only dispatch · iter -

  • Decision: Await the durability event-handler step during workflow-level emit, after immediate listeners; retain legacy wrapper and in-step delivery.
  • Why: The workflow task must number emitted handler steps before later durable calls, while the existing legacy step sequence and in-step side effects remain compatible.
  • Source: https://github.com/pydantic/pydantic-ai/issues/10300#issuecomment-6199314009

2026-10-23 · pre-push review 1 of 3 at 3d88ebc0224b1174c41b53566e7a333459cd0b06 · iter -

2026-10-23 · pre-push review 1 outcome at 3d88ebc0224b1174c41b53566e7a333459cd0b06 · iter -

2026-10-23 · pre-push review 2 of 3 at 5d3d04d60881eecf3fefa3169ef67b0c1cac8859 · iter -

2026-10-23 · pre-push review 2 outcome at 5d3d04d60881eecf3fefa3169ef67b0c1cac8859 · iter -

2026-10-23 · Stop after second verified regression · iter -

  • Decision: Stop fixes, pushes and metadata corrections at 5d3d04d608 pending direction on concurrent handler ordering.
  • Why: Gated baseline records A then B handler steps; candidate records B then A while the stream remains A then B. First caught-listener regression was fixed; this second verified change remains unfixed under the user stop rule.
  • Source: https://github.com/pydantic/pydantic-ai/pull/11157

2026-10-23 · Limit DBOS emission dispatch to stream events · iter -

  • Decision: Dispatch stream-dispatched events in the emitting workflow task; preserve baseline immediate CapabilityEvent handling and all listener task semantics.
  • Why: Concurrent immediate listeners can settle B before A; handling them in emit reversed durable step allocation while baseline stream settlement preserves A/B order.
  • Source: https://github.com/pydantic/pydantic-ai/pull/6258#discussion_r3918870096
  • Supersedes: DBOS workflow-only dispatch

2026-10-23 · Own emission dispatch in DBOS capability · iter -

  • Decision: Use a base durability emission hook with DBOS-owned run-scoped settlement signals; retain separate stream-listener and durability deduplication.
  • Why: Reusing immediate-listener markers would suppress stream-position listeners. DBOS run scope owns reset and cleanup without mutating EventStreamBuffer or installing runtime callbacks.
  • Source: https://github.com/pydantic/pydantic-ai/pull/11157#discussion_r4362953666

2026-10-23 · Keep DBOS upgrade warning in integration guide · iter -

2026-10-23 · pre-push review 3 of 3 at ccad8bcddffd484420d535e883ebe635325dd02d · iter -

2026-10-23 · pre-push review 3 outcome at ccad8bcddffd484420d535e883ebe635325dd02d · iter -

2026-10-23 · Stop after mixed-dispatch ordering regression · iter -

2026-10-26 · David overrides mixed-dispatch stop · iter -

  • Decision: David authorizes fixing the mixed-dispatch ordering regression on top of ccad8bcdd, preserving its existing fixes, and one more fresh pre-push review on the new head only. "If any review or probe finds another regression (an input that works on main and fails or changes meaning on the PR head), stop again and report. This override covers only the mixed-dispatch ordering regression."
  • Why: David chose continuation from manager-authored options; the known immediate-A/stream-B regression is the only stop override. Relayed in local-notes/dispatch-20261026T000454Z.md.
  • Source: https://github.com/pydantic/pydantic-ai/pull/11157
  • Supersedes: Stop after mixed-dispatch ordering regression

2026-10-26 · Preserve stream delivery behind immediate emissions · iter -

  • Decision: Reserve immediate emissions and following emissions for stream-position durability delivery until that queue drains; keep awaited in-emitter delivery when no such emission is pending.
  • Why: This preserves mixed A/B handler order and immediate listener task semantics without waiting across producer/listener tasks. Stream-only CustomEvent and StepPersistence fixes retain their awaited-handler path.
  • Source: https://github.com/pydantic/pydantic-ai/pull/6258#discussion_r3918870096

2026-10-26 · Additional pre-push review 1 of 1 at 250857635db4f0f65c81efa526315280402191cc · iter -

2026-10-26 · Additional pre-push review outcome at 250857635db4f0f65c81efa526315280402191cc · iter -

2026-10-26 · Stop after mixed-dispatch replay regression · iter -

  • Decision: Stop corrections and pushes at 250857635db4f0f65c81efa526315280402191cc after a new confirmed replay regression. Do not run another pre-push review.
  • Why: Original-source baseline fork succeeds with immediate A handler at step 3, fetch at step 4, B handler at step 5. Candidate live succeeds but fork expects B handler at step 4 instead of recorded fetch. The one additional reviewer returned current before this runtime probe; its authorization is consumed.
  • Source: https://github.com/pydantic/pydantic-ai/pull/11157

2026-10-26 · David holds pending blind design review · iter -

  • Decision: David holds PR 11157 pending the blind design review of issue 10300. Its design will arrive in the next dispatch. No fixes, pushes or reviews.
  • Why: The confirmed mixed-dispatch replay regression remains unresolved; preserve the clean unpushed commit while awaiting the design dispatch.
  • Source: https://github.com/pydantic/pydantic-ai/issues/10300
  • Supersedes: Stop after mixed-dispatch replay regression

2026-10-26 · David authorizes blind redesign · iter -

  • Decision: David replaces approved option 1 and the three fix rounds with the blind design. "The design is the spec. If the real code forces a deviation, report it before you build it."
  • Why: The blind review showed option 1 cannot meet the criteria: in-emit handling deadlocks on the mixed-replay shape, and avoiding it requires a runtime-state-dependent path choice.
  • Source: https://github.com/pydantic/pydantic-ai/pull/11157
  • Supersedes: David holds pending blind design review

2026-10-26 · David authorizes DBOS private functions · iter -

  • Decision: Use the DBOS private functions and flag the coupling in the PR body for maintainer review; do not hold the PR for approval first.
  • Why: David selected the blind design with explicit authorization for its private-API dependency.
  • Source: https://github.com/pydantic/pydantic-ai/pull/11157

2026-10-26 · Replace DBOS private calls when public API lands · iter -

  • Decision: Carry a TODO at the private-API call site linking dbos-transact-py issue 915 and replace the calls when the public API lands.
  • Why: A public DBOS API has been requested upstream; no posts to that issue are authorized.
  • Source: https://github.com/dbos-inc/dbos-transact-py/issues/915

2026-10-26 · Redesign pre-push review 1 of 1 at 6d072ba433456773e4de7d31c4ae98fd96b72ff2 · iter -

2026-10-26 · Redesign review outcome at 6d072ba433456773e4de7d31c4ae98fd96b72ff2 · iter -

2026-10-26 · Bind reserved execution to the registered DBOS operation · iter -

  • Decision: DBOSBoundOperation owns the raw step function and one name/config mapping shared by ordinary and reserved execution; private SDK imports occur only at reservation and reserved execution call sites.
  • Why: The bound operation preserves the registered wire signature and configuration without a nullable backend event callback, and importing the integration no longer requires the private reservation helpers.
  • Source: https://github.com/pydantic/pydantic-ai/pull/11157#discussion_r4379094424

2026-10-26 · Count private helper caller change as first redesign regression · iter -

  • Decision: Count the direct private-helper test call whose meaning changed from unsupported-operation validation to argument-binding failure as the first redesign regression; update that internal caller for the authorized binding refactor. On the second, stop and report.
  • Why: Exact-main and candidate import paths and errors are verified in the preserved baseline probe; this applies the standing input-based regression rule and corrects the earlier application-only interpretation.
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/40352999220/job/121677909473

2026-10-26 · Leave direct enqueue event reservations unchanged after fork comparison · iter -

  • Decision: Net-neutral: skip production changes for direct enqueue producers. Both asap and when_idle every-step DBOS fork probes pass on main715651ef7 and0b452be85 with identical journals and handler traces; retain the existing reservation scope.
  • Why: Both actual producers and DBOS handler engagement are verified. Delivery occurs after the enqueueing tool finishes its two durable calls, with no tool_call_id, before a later tool makes two more durable calls. The sibling-defect condition for widening scope was not reproduced.
  • Source: https://github.com/pydantic/pydantic-ai/pull/11157#issuecomment-6421275164

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.