Skip to main content

Locked Decision Rounds

View as Markdown

Locked decision rounds rank every event admitted before a decision by business priority. Use them when a higher-priority event must win even if it arrives later, such as a claimant's withdrawal over a reviewer's approval of the same insurance claim.

The graph shows the main path. It omits the failure route from ArbitrateDecisionRound to RecoverDecisionRound, and the submission RPCs' writes to the candidates map and their wake-up publication.

How it works

Each round has a round ID that is unique within one Flow run. The Python, Java, and TypeScript entries pin the run the start call returned. Go and Rust entries cannot: do not reuse a Flow ID while a late request can arrive, or use a round key unique across runs. Two AttributeMaps are keyed by it: the round's window and its candidates. Every RPC and Step that reads or changes them locks the round's instance of both maps. A Step gets these locks from its movement options. A typed submit entry derives the round ID from its input and passes both locks and both loads. Call the raw RPC only through that entry.

  1. OpenDecisionRound opens the round's window under the round's locks, then moves to ArbitrateDecisionRound.
  2. Each submission names its round. It admits the event only while that round's window is open. While the Flow is active, a submission for a closed or unknown round answers "closed" and never enters a newer round.
  3. An admitted event becomes a candidate. Its key is the round ID, the event type, and the event ID. It also holds a payload reference, a rank, and its admission order. The RPC answers "received", which confirms admission only. A retried submission finds its key and adds nothing.
  4. The first candidate of a round publishes one wake-up in the same commit. Later admissions only append.
  5. ArbitrateDecisionRound waits for AnyOf the 5-day deadline Timer and the round's wake-up. Its Execute takes the round's locks and closes the window. It picks the highest-ranked candidate, and the earlier admission breaks a tie. With no candidate, the decision is the deadline branch. It records the decision and an answer for every candidate in one commit.
  6. AnswerRoundCandidates holds no lock. The example records the answers, then routes: an approval proceeds to the approved branch, and a withdrawal or a denial closes the claim. A deadline opens the next round, up to two rounds, then escalates.

Arbitrate by business priority

Choose which meaning of "at the same time" the design uses:

RankingUse
The first committed event winsResponse Window
Channels ready in the same check, by declaration orderDecision with Deadline
Every event admitted before the decision, by rankLocked decision rounds

Lock order is commit order, not business priority. A locked RPC therefore never decides between ranked events; it only admits them.

The closed window fixes the candidates, and later events change nothing. Do not wait for newer events with a clock or a Timer grace window.

Rules

  • The decision comes only from the round's Attribute state, so a retried Execute reaches the same decision. Execute keeps the locks through its retries and backoff, so no candidate joins meanwhile.
  • The cutoff is the committed locked close, not the Timer: a candidate admitted before it is eligible.
  • A round already closed or missing ends the branch with DeadEnd.
  • When the arbitration Execute exhausts its retries, neither the close nor the decision commits. Give it a short retry budget and a failure route. The recovery Step takes the same locks and checks the window again, then closes the round in one commit. It arbitrates again from the stored candidates or, as the example does, records the failure, then moves to the unlocked answer Step.
  • Send answers after the locks are released, because the submission RPCs wait for them. Key each notification by the business claim, the round, and the candidate key. The receiver deduplicates, because a Step retry can send it again.
  • Submit with a stable request ID. A same-ID retry to the same run returns the recorded result while Dex retains that request. Java cannot choose the ID, so each Java call is new. After the Flow closes, a new submission gets an error.
  • Keep the arbitration and recovery Execute SYNC even when the Flow default is ASYNC. The decision and the answers are then stored before the answer Step runs. See Durability.
  • Read the candidates and the outcome from the round's Attributes, never from the wake-up. A Timer win can leave the wake-up pending.
  • Start one arbitration wait per round.

The Developer reference covers lock conflicts, request ID retention, wake-up cleanup, and the Rust failure route.

Test it

The integration tests cover these scenarios:

  • An admitted approval decides the round.
  • A withdrawal outranks an earlier reviewer decision; the earlier admission breaks a tie.
  • A submission for a closed or unknown round answers "closed".
  • After the Flow closed, a same-ID retry to the same run returns the recorded result, except in Java; a new submission gets an error.
  • A candidate admitted after the Timer fired, but before the locked close, is eligible.
  • An arbitration failure leaves none of its staged writes; recovery closes the round and answers every candidate.

Core implementation

ClaimDecisionFlow starts with a claim ID. Callers submit through SubmitClaimWithdrawalWithRoundLocks and SubmitReviewerDecisionWithRoundLocks.

claim_decision_flow.pyPython
def round_invoke_options(round_id: str, request_id: str | None = None) -> RPCInvokeOptions:
"""Returns the options of a submission call: the named round's locks and loads."""
return RPCInvokeOptions(
lock_attribute_map_instances=round_locks(round_id),
load_attribute_map_instances=round_loads(round_id),
request_id=request_id,
)


def admit_candidate(context: Context, key: CandidateKey, rank: int) -> RPCResult[str]:
"""Appends one candidate while its round is open, under the round's locks.

The answer confirms admission only; the arbitration Step decides the outcome.
"""
if read_round_window(context, key.round_id) != ROUND_WINDOW_OPEN:
return RPCResult(SUBMISSION_ROUND_CLOSED)
candidates = read_round_candidates(context, key.round_id)
if any(candidate.key == key for candidate in candidates):
# A retried submission adds nothing.
return RPCResult(SUBMISSION_RECEIVED)
candidates.append(
RoundCandidate(
key=key,
payload_ref=key.event_id,
rank=rank,
admission_order=len(candidates) + 1,
)
)
ROUND_CANDIDATES.set(context, key.round_id, candidates)
if len(candidates) == 1:

Example: examples/python/dex_examples/patterns/locked-decision-rounds/claim_decision_flow.py

Next