Reserve DBOS handler step positions during tool `ctx.emit()`
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 -
- Decision: Use fresh pre-push review and a plain independent final check; do not invoke review-branch.
- Why: The requested review-branch pass was conditional on the current lifecycle requiring it; the tracked pushing skill does not.
- Source: https://github.com/pydantic/pydantic-ai/issues/10300#issuecomment-6368038904
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 -
- Decision: Reserved first independent review dispatch.
- Why: Reviewed tree 75f1a99b69111f770cef89210f48477dce112d3d; full branch d64b3453e4dcee78e6051fefc084062baf6f9beb..3d88ebc0224b1174c41b53566e7a333459cd0b06; policy-base 332d3011d1a1bada4ae7210d5416ed5b31be3346.
- Source: https://github.com/pydantic/pydantic-ai/tree/3d88ebc0224b1174c41b53566e7a333459cd0b06
2026-10-23 · pre-push review 1 outcome at 3d88ebc0224b1174c41b53566e7a333459cd0b06 · iter -
- Decision: Remediate caught immediate-listener errors suppressing buffered durability delivery.
- Why: Baseline passes focused reproduction, candidate fails; remove only the reservation for a handler that never started. First review-discovered regression.
- Source: https://github.com/pydantic/pydantic-ai/tree/3d88ebc0224b1174c41b53566e7a333459cd0b06
2026-10-23 · pre-push review 2 of 3 at 5d3d04d60881eecf3fefa3169ef67b0c1cac8859 · iter -
- Decision: Reserved scoped follow-up independent review dispatch.
- Why: Tree f8f998132adbc101c7f14f93aa9ebabfa8895477; scope 3d88ebc0224b1174c41b53566e7a333459cd0b06..5d3d04d60881eecf3fefa3169ef67b0c1cac8859; full branch supplied as context; policy-base 332d3011d1.
- Source: https://github.com/pydantic/pydantic-ai/tree/5d3d04d60881eecf3fefa3169ef67b0c1cac8859
2026-10-23 · pre-push review 2 outcome at 5d3d04d60881eecf3fefa3169ef67b0c1cac8859 · iter -
- Decision: Dismiss reservation-race finding: the stream waits immediate listener settlement before durability dispatch.
- Why: Event-gated check observes consumer waiting, reservation still present, original listener error caught and one handler delivery. No runtime change needed; reviewed tree f8f998132adbc101c7f14f93aa9ebabfa8895477 remains exact.
- Source: https://github.com/pydantic/pydantic-ai/tree/5d3d04d60881eecf3fefa3169ef67b0c1cac8859
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 -
- Decision: Remove the unversioned DBOS section from the V2 migration guide; retain the integration-guide warning and PR migration guidance.
- Why: The changelog page documents V2 migration, while this minor-release recorded-step ordering fix belongs with DBOS streaming semantics.
- Source: https://github.com/pydantic/pydantic-ai/pull/11157#discussion_r4362954226
2026-10-23 · pre-push review 3 of 3 at ccad8bcddffd484420d535e883ebe635325dd02d · iter -
- Decision: Reserve final fresh independent reviewer dispatch on the scope correction.
- Why: Candidate tree bc5de5b5c477beb3a5f54b4e9bc7b7d2443cf043; follow-up 5d3d04d60881eecf3fefa3169ef67b0c1cac8859..ccad8bcddffd484420d535e883ebe635325dd02d; full branch context supplied; policy-base 7bbfd9de450849e4ca6ba46bb931b08ded66e718.
- Source: https://github.com/pydantic/pydantic-ai/pull/11157
2026-10-23 · pre-push review 3 outcome at ccad8bcddffd484420d535e883ebe635325dd02d · iter -
- Decision: Fresh independent reviewer returned current on the committed candidate.
- Why: Reviewed tree bc5de5b5c477beb3a5f54b4e9bc7b7d2443cf043 and clean unchanged candidate verified; scoped delta 5d3d04d60881eecf3fefa3169ef67b0c1cac8859..ccad8bcddffd484420d535e883ebe635325dd02d; policy-base 7bbfd9de450849e4ca6ba46bb931b08ded66e718. Additional mixed-dispatch runtime probe pending before push.
- Source: https://github.com/pydantic/pydantic-ai/pull/11157
2026-10-23 · Stop after mixed-dispatch ordering regression · iter -
- Decision: Stop before push at ccad8bcddffd484420d535e883ebe635325dd02d pending direction on mixed immediate/stream emission ordering.
- Why: Single tool emits immediate A then stream B: baseline handler step order A/B, candidate B/A, stream A/B in both. Candidate assertion fails. This is a new verified regression under the dispatch stop rule; no fork/recovery failure claimed.
- Source: https://github.com/pydantic/pydantic-ai/pull/6258#discussion_r3918870096
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 -
- Decision: Reserve the one additional fresh review David authorized for the mixed-dispatch correction.
- Why: Candidate tree bf6da7e7c676519065139375add124ba34a4491b; scope ccad8bcddffd484420d535e883ebe635325dd02d..250857635db4f0f65c81efa526315280402191cc; full branch context supplied; policy-base 715651ef7fd3994395e85070ba3d6096c5b211c9. Original three-review ledger remains unchanged.
- Source: https://github.com/pydantic/pydantic-ai/pull/11157
2026-10-26 · Additional pre-push review outcome at 250857635db4f0f65c81efa526315280402191cc · iter -
- Decision: Fresh independent reviewer returned current on the new committed head; readiness remains subject to the final recovery probe.
- Why: Reviewed clean unchanged tree bf6da7e7c676519065139375add124ba34a4491b; scoped ccad8bcdd..250857635 with policy-base 715651ef7fd3994395e85070ba3d6096c5b211c9. The one additional authorized review is consumed; no push yet.
- Source: https://github.com/pydantic/pydantic-ai/pull/11157
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 -
- Decision: Reserve the one fresh pre-push review David authorized for the blind redesign.
- Why: Candidate tree 5e5dd343b20de9601ddaab03a3afa9910239cd51; full redesign diff against main 715651ef7fd3994395e85070ba3d6096c5b211c9. Old option-1 review ledgers remain historical. Redesign regression counter is zero.
- Source: https://github.com/pydantic/pydantic-ai/pull/11157
2026-10-26 · Redesign review outcome at 6d072ba433456773e4de7d31c4ae98fd96b72ff2 · iter -
- Decision: Fresh independent reviewer returned current on the full redesign.
- Why: Candidate and policy worktrees remain clean and unchanged; reviewed tree 5e5dd343b20de9601ddaab03a3afa9910239cd51, main715651e..6d072ba43. One authorized redesign review consumed; regression counter remains zero.
- Source: https://github.com/pydantic/pydantic-ai/pull/11157
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.