ENG-13803: workflow traces mark a step that ran again because the worker running it died
When a worker dies mid-step, its lease runs out, another worker claims the row, and the step runs again with the same attempt number. In traces that showed up only as two spans with the same reflex.workflow.attempt, which a reader had to notice and interpret.
An attempt whose claim took the row over from a lease whose step did not finish under it now carries reflex.workflow.reclaimed=true on its span (absent otherwise): the lease expired (worker died or overran it), or the worker cancelled the step while draining at shutdown.
Drained steps. Runner.drain cancels in-flight steps and gives their leases back; the cancelled attempt never commits, so the next claim re-runs it under the same attempt number. release(..., interrupted=<claim version>), used only by drain, now writes claimed_until = now() instead of NULL when the row is still at the claim's version (case((wf_version == version, now()), else_=NULL)). Claimability is unchanged (claimed_until < now() holds for the next claim), but the next claim sees a prior lease and marks the repeat. A row that run, deliver or cancel moved on while the step ran is cleared to NULL as before, so its next claim is not marked and a cancelled row leaves nothing in the claimed_until IS NOT NULL lease index. Fenced releases still clear it.
Claim delay. With the prior lease in hand, Claimed.late now counts from the later of wake_at and the end of the replaced lease, so it means time due with no worker holding the row. A re-claim after a dead worker's lease reads (claim time β lease expiry), not the earlier attempt's run.
How the claim knows: RETURNING on the lease update only sees the values it wrote, so the claim's pick CTE (chosen, already MATERIALIZED and FOR UPDATE SKIP LOCKED) now also selects each row's claimed_until as wf_prior_lease, and the update returns it from the CTE. It is read under the pick's row lock, so it is the lease the claim replaces. Only an expired lease is claimable, and a step that ends (commit or release) clears claimed_until, so a non-null previous lease means a worker that never finished. Usually that is the step running again under the same attempt number. It does not prove a worker died or that the step repeats, and the docstring, reflex-otel README and news say so: run(), deliver() and cancel() move a row on without touching its lease, and keep_lease renews only while wf_version matches, so a long step outliving its lease after that marks its replacement reclaimed while the original is still running (it is fenced at commit), and if that step's worker then dies, its stale lease stays until a claim replaces it, so the first claim after a later run() is marked too. Claimed gains reclaimed, and the runner passes it to execute() and on to step_span.
Stacked on #8112 (ENG-13799), which extends the same RETURNING; this PR's base is that branch.
Choices the ticket left open:
- A lease left behind because giving it back failed (the worker lost its connection while releasing) also reads as reclaimed. In both cases the row was taken over from a lease nobody gave back.
- The attribute is set only when true, so spans of ordinary claims are unchanged.
Tests: under the Postgres fixture, test_claim.py claims a row whose lease is in the past (reclaimed set) and a fresh row (not set). test_claim.py also checks, through both claim() and claim_group(), that a re-claim after an expired lease reads its delay as exactly the claim's statement_timestamp() minus the lease's end. test_runner.py claims and executes a row left leased and checks the span attribute, and drains a worker mid-step and checks the next claim is reclaimed with its delay exactly taken.at - ended. It also moves a row on mid-step (by cancel and by run), drains, and checks the lease is cleared and the next claim is not reclaimed; that test fails with the drain's unconditional now(). test_otel.py covers the attribute being present and absent. pytest tests/units/reflex_workflow passes with REFLEX_TEST_POSTGRES set with tests/units/reflex_otel (1291 passed, 2 skipped).
Linear: ENG-13803
π€ Generated with Claude Code