Repository navigation
Description
claimKey (packages/control-plane/src/turn-handlers.ts) steps over a holder whose turn record it cannot read, treating "unreadable" as "terminal". A claim is not atomic with the record it names — TurnStore.claimIdempotencyKey writes the key hash, and turns.create writes the record a round trip later — so a holder stalled between the two reads exactly like one whose record expired. The stepover then releases a live claim, the second claim wins, and both submissions create and enqueue a turn for one idempotency key: the duplicate fan-out §4.3 exists to prevent.
Found by review on #972 (gates v2), where clawgenti reproduced it against a fake Redis: with <sessionId>:gate:0 held by a turn whose record does not exist yet, POST /v1/sessions/{id}/decision returns 202 with a new turn id instead of the holder's.
Why the gate key makes it worse
On an ordinary submission key the duplicate is two runs of the same envelope. On a gate key it is two resumes carrying the same decision, and the second one re-runs an approved continuation:
- Resume A runs,
appendGateDecisionrecords the decision, the continuation runs. - Resume B runs.
planLeafGate→computeGateStatesees gate 0 already decided, so there is no pending gate. decideSeed's no-pending-gate branch re-derivescontinuationPrompt(lastDecision…)(harness/src/gate.ts), andplanLeafGatereturns it ascontinuationbecause the envelope carries a decision.
So one human approval produces two executions of the approved action — double spend, double side effects. A human decision and the deadline sweep race on this key by design (#393 §3), which is a new and regular way to reach a window that until now only a client retry could.
Scope
Pre-existing: claimKey is on main and ordinary submissions share the window. #972 added a code comment recording it (4237341a) rather than changing the idempotency protocol inside a gates PR.
Why the obvious fix is not obvious
The mirror reading is not free. Treat an unreadable holder as live and a crash between a claim and its create wedges that key with no record and no release — and the default key is the session id, so every later turn of that session is answered with the ghost's id, with no self-heal.
Telling a stalled claimant from a crashed one needs a lease, and §5.1 rules out the Lua/WATCH/MULTI that would make claim-plus-record one step. Three candidates:
claimedAtplus a grace. Write a timestamp besideturnIdon the claim. Treat an unreadable holder as live whilenow - claimedAtis inside a grace (seconds — orders of magnitude above the one round trip a create takes), and as stale after. No wedge beyond the grace, no change to the happy path. Theft needs a claimant stalled longer than the grace, which is indistinguishable from dead by any means short of a lease renewal. Smallest change; recommended.- A TTL on the key hash, shorter than the record TTL, plus never stepping over an unreadable holder. Self-heals an orphan; a key is only held while its turn is non-terminal (≤
MOCA_TURN_MAX_S), so a TTL tied toresultTtlSecondscannot expire under a live turn, and since the record's TTL starts at the terminal transition the record always outlives its key. But an orphaned claim wedges that key for the whole TTL. - Create the record before the claim, unwinding on a lost claim. Makes "unreadable" unambiguous, but contradicts §4.3's promise that a pre-enqueue refusal leaves nothing behind: every duplicate retry would leave a
failedrecord in the session index.
Acceptance
- A unit test that pins the stolen-claim window: key held by a turn whose record is absent, and the claimant does not create a second turn for it.
- A unit test that an orphaned claim (crash between claim and create) still self-heals, so the default session key cannot wedge.
- The
shortcut:note inclaimKeyremoved along with the window.
Activity
docs(control-plane): point the claimKey shortcut at issue rossoctl#989
docs(control-plane): fix the claim-window upgrade note and an orphane…
cwiklik commentedon Nov 23, 2026
Note on candidate 2 (a TTL on the key hash), from review of #972: its safety rests entirely on the TTL never firing under a live turn, and that premise is load-bearing in a way a reader will skim past.
It does hold today — a key is held only while its turn is non-terminal (
MOCA_TURN_MAX_S, default 1800 s), the record's own TTL starts at the terminal transition, and the key TTL would be tied toresultTtlSeconds(default 86 400 s) — but it holds because of a two-order-of-magnitude margin, not because anything enforces it. If the TTL ever does fire,claimIdempotencyKey'sHSETNXhands the key to the next submission while the turn it names is still running, and nothing downstream refuses that:gate_pendingguards a gated submission, so it never seesPOST /decision, and an ordinary turn has only this claim between it and a duplicate fan-out. That is strictly worse than the window this issue is about.So if candidate 2 is taken, the TTL must be derived from
MOCA_TURN_MAX_Swith a margin rather than fromresultTtlSeconds, and the acceptance tests need one that fails if the two are ever configured within a turn's length of each other.Candidate 1 (
claimedAtplus a grace) remains the recommendation: it states the rule instead of relying on the margin, and a stepover decision made from a readable age is reviewable.claimKey's ownshortcut:comment now says this (9137cc0e).fix(control-plane): stop claimKey stepping over a live claim mid-create