[core] Complete a due wait even when its run is already terminal - #3749
Open
pranaygp wants to merge 1 commit into
Open
[core] Complete a due wait even when its run is already terminal#3749pranaygp wants to merge 1 commit into
pranaygp wants to merge 1 commit into
Conversation
A pending wait parks a delayed continuation message on the workflow queue;
when it fires, the delivery's job is to record the wait's `wait_completed`.
If the run finished first — a `Promise.race([sleep('1h'), hook])` whose hook
won, a cancellation, a failure — every path that discovers the terminal state
returned and acked without writing it, so the wait stayed open in the log
forever: replay, `inspect`, and the dashboard timeline all read a run that
terminated while still sleeping.
The completion is now unconditional on run status. On a terminal run it is all
the delivery does — no replay, no suspension dispatch — and the message is
acked only after the write settles, so a transient World failure redelivers
instead of dropping the completion. Not-yet-due waits are left to their own
continuations.
The Worlds refused the write too: a terminal transition reaps the run's waits
(`run.resources-released`), and `wait_completed` against a reaped wait was
`Wait "…" not found`. world-local, world-postgres, and world-sim now accept it
and record the event without resurrecting the entity, deduplicating against
the surviving `.completed` claim (local) or the event log (postgres, sim).
Older backends that still refuse are logged and acked rather than nacked —
redelivery cannot change that verdict.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Pranay Prakash <1797812+pranaygp@users.noreply.github.com>
🦋 Changeset detectedLatest commit: fde1165 The changes in this PR will be included in the next version bump. This PR includes changesets to release 18 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Verification
wait_completedwas prevented from being posted when the run is terminal, at both layers:run_startedrejected withRunExpiredError/EntityConflictError,run_startedanswering with a terminal run, the lazy-hook preload's terminal scan, and the node replay loop'shasRecordedTerminalRunEventcheck) returned and acked before the elapsed-wait pass.deleteAllWaitsForRun/DELETE FROM waits/releaseRunResources, enforced by the sim'srun.resources-releasedinvariant), sowait_completedagainst a reaped wait failed withWorkflowWorldError: Wait "…" not found. Reproduced directly against world-local before touching anything.So a wait whose run finished first stayed open in the event log forever. Everything that reads the log — replay,
inspect, the dashboard timeline,openHookAndWaitState— saw a run that terminated while still sleeping.Change
@workflow/core— newruntime/due-waits.ts. A queue message created for a wait now always marks the wait completed when it is due, regardless of run status. On a terminal run that is all the delivery does — no replay, nohandleSuspension— and the message is acked only after the write settles, so a transient World failure redelivers rather than dropping the completion. Waits that are not yet due are left to their own continuations. A World that still refuses the write is logged and acked, not nacked: redelivery cannot change that verdict, and burning every delivery on it would end inMAX_DELIVERIES_EXCEEDED.@workflow/world-local,@workflow/world-postgres,world-sim— acceptwait_completedfor a wait the terminal transition reaped. The event is recorded, the entity is not resurrected (the reap invariant still holds). Dedup moves to whatever survived the reap: the.completedclaim under.locks/in world-local, the event log in world-postgres and world-sim. Without a matchingwait_createdthe write is still rejected — that is a completion for a wait that never existed.Tests
packages/core/src/runtime/due-waits.test.ts— due/not-due/already-completed selection,resumeAtpassthrough, log loading, the conflict / unrecordable / retryable-rethrow outcomes, legacy write shape.packages/core/src/runtime/wait-completion-terminal.test.ts— drives the real queue handler against a fake World for all four terminal-discovery paths; asserts thewait_completedis written, nothing is replayed or re-queued, redelivery does not double-complete, an unrecordable completion still acks, and a transient failure does not ack. 6 of its 8 cases fail without the runtime change.packages/core2171 pass,world-sim74 pass,world-postgres142 pass,world-local546 pass (its 4 failures are pre-existingchmod-based permission tests that also fail onmainin this sandbox).🤖 Generated with Claude Code