Skip to content

claimKey steps over a live idempotency claim mid-create, double-running an approved gate decision #989

Done

Description

@cwiklik

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:

  1. Resume A runs, appendGateDecision records the decision, the continuation runs.
  2. Resume B runs. planLeafGate → computeGateState sees gate 0 already decided, so there is no pending gate.
  3. decideSeed's no-pending-gate branch re-derives continuationPrompt(lastDecision…) (harness/src/gate.ts), and planLeafGate returns it as continuation because 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:

  1. claimedAt plus a grace. Write a timestamp beside turnId on the claim. Treat an unreadable holder as live while now - claimedAt is 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.
  2. 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 to resultTtlSeconds cannot 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.
  3. 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 failed record 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 in claimKey removed along with the window.

Activity

  1. cwiklik commented on Nov 23, 2026

    @cwiklik
    ContributorAuthor

    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 to resultTtlSeconds (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's HSETNX hands the key to the next submission while the turn it names is still running, and nothing downstream refuses that: gate_pending guards a gated submission, so it never sees POST /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_S with a margin rather than from resultTtlSeconds, and the acceptance tests need one that fails if the two are ever configured within a turn's length of each other.

    Candidate 1 (claimedAt plus 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 own shortcut: comment now says this (9137cc0e).

  2. added a commit that references this issue on Nov 24, 2026
    e667445
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions