Locked Decision Rounds
View as MarkdownLocked 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.
OpenDecisionRoundopens the round's window under the round's locks, then moves toArbitrateDecisionRound.- 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.
- 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.
- The first candidate of a round publishes one wake-up in the same commit. Later admissions only append.
ArbitrateDecisionRoundwaits forAnyOfthe 5-day deadline Timer and the round's wake-up. ItsExecutetakes 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.AnswerRoundCandidatesholds 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:
| Ranking | Use |
|---|---|
| The first committed event wins | Response Window |
| Channels ready in the same check, by declaration order | Decision with Deadline |
| Every event admitted before the decision, by rank | Locked 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
Executereaches the same decision.Executekeeps 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
Executeexhausts 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
ExecuteSYNC 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.
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
// RoundInvokeOptions returns the options of a submission call: the named round's locks and loads.
func RoundInvokeOptions(roundID string) dex.RPCInvokeOptions {
return dex.RPCInvokeOptions{
LockAttributeMapInstances: RoundLocks(roundID),
LoadAttributeMapInstances: roundLoads(roundID),
}
}
// SubmitClaimWithdrawalWithRoundLocks submits a withdrawal under the locks and loads of the round it names.
// It returns SubmissionReceived, or SubmissionRoundClosed for a closed or unknown round of an active Flow.
// A retry with the same requestID returns the recorded result while the Flow ID names the same run
// and Dex retains that request. This SDK cannot pin the run, so do not reuse the Flow ID while a late
// retry can arrive. A new request to a closed Flow, and an exhausted lock wait, return the client error.
func SubmitClaimWithdrawalWithRoundLocks(
ctx context.Context, client *dex.Client, flowID string, requestID string, withdrawal ClaimWithdrawal,
) (string, error) {
return submitWithRoundLocks(ctx, client, flowID, requestID, NewClaimDecisionFlow().SubmitClaimWithdrawal, withdrawal.RoundID, withdrawal)
}
func submitWithRoundLocks(
ctx context.Context, client *dex.Client, flowID string, requestID string, rpc any, roundID string, submission any,
) (string, error) {
options := RoundInvokeOptions(roundID)
options.RequestID = &requestID
var outcome string
err := client.InvokeRPCWithOptions(ctx, flowID, rpc, submission, &outcome, options)
return outcome, err
}
// SubmitClaimWithdrawal admits the claimant's withdrawal into the named round.
Example: examples/go/patterns/locked-decision-rounds/workflow.go
/** Returns the options of a submission call: the named round's locks and loads. */
public static RPCInvokeOptions roundInvokeOptions(final String roundId) {
return RPCInvokeOptions.newBuilder()
.addLockAttributeMapInstance(roundWindows, roundId)
.addLockAttributeMapInstance(roundCandidates, roundId)
.addLoadAttributeMapInstance(roundWindows, roundId)
.addLoadAttributeMapInstance(roundCandidates, roundId)
.build();
}
public static String submitClaimWithdrawalWithRoundLocks(
final Client client,
final String flowId,
final String runId,
final ClaimWithdrawal withdrawal) {
final ClaimDecisionFlow stub = client.newRpcStub(ClaimDecisionFlow.class, flowId, runId);
return client.invokeRPC(
stub::submitClaimWithdrawal, withdrawal, roundInvokeOptions(withdrawal.roundId()));
}
@RPC
public RPCResult<String> submitClaimWithdrawal(
final Context context, final ClaimWithdrawal withdrawal) {
return RPCResult.of(admitClaimWithdrawal(context, withdrawal));
}
/**
* Appends one candidate while its round is open, under the round's locks. The answer confirms
* admission only; the arbitration Step decides the outcome.
*/
Example: examples/java/src/main/java/io/superdurable/dex/patterns/lockeddecisionrounds/ClaimDecisionFlow.java
/** Returns the options of a submission call: the named round's locks and loads. */
export function roundInvokeOptions(roundId: string, requestId?: string): RPCInvokeOptions {
return {
lockAttributeMapInstances: roundLocks(roundId),
loadAttributeMapInstances: roundLoads(roundId),
...(requestId === undefined ? {} : { requestId }),
};
}
/**
* Submits a withdrawal to one run under the locks and loads of the round it names.
* Pass the run ID that startFlow returned, so a late call never reaches a newer run.
* Resolves to SUBMISSION_RECEIVED, or SUBMISSION_ROUND_CLOSED for a closed or unknown round of an active Flow.
* A retry with the same request ID resolves to the recorded result while Dex retains that request.
* A new request to a closed Flow, and an exhausted lock wait, reject with the client error.
*/
export function submitClaimWithdrawalWithRoundLocks(
client: Client,
flowId: string,
runId: string,
requestId: string,
withdrawal: ClaimWithdrawal,
): Promise<string> {
return client.invokeRPCWithOptions(
claimDecisionFlow.submitClaimWithdrawal,
flowId,
withdrawal,
roundInvokeOptions(withdrawal.roundId, requestId),
runId,
);
Example: examples/typescript/src/patterns/locked-decision-rounds/claim-decision-flow.ts
/// Returns the options of a submission call: the named round's locks and loads.
pub fn round_invoke_options(round_id: &str) -> RpcInvokeOptions {
RpcInvokeOptions::new()
.lock_attribute_map_instance(ROUND_WINDOWS.lock(round_id))
.lock_attribute_map_instance(ROUND_CANDIDATES.lock(round_id))
.load_attribute_map_instance(ROUND_WINDOWS.load(round_id))
.load_attribute_map_instance(ROUND_CANDIDATES.load(round_id))
}
/// Submits a withdrawal under the locks and loads of the round it names.
///
/// Returns `SUBMISSION_RECEIVED`, or `SUBMISSION_ROUND_CLOSED` for a closed or
/// unknown round of an active Flow. A retry with the same `request_id` returns
/// the recorded result while the Flow ID names the same run and Dex retains
/// that request. This SDK cannot pin the run, so do not reuse the Flow ID
/// while a late retry can arrive. A new request to a closed Flow, and an
/// exhausted lock wait, return the client error.
pub fn submit_claim_withdrawal_with_round_locks(
client: &Client,
flow_id: &str,
request_id: &str,
withdrawal: ClaimWithdrawal,
) -> SdkResult<String> {
let options = round_invoke_options(&withdrawal.round_id).request_id(request_id);
client.invoke_rpc_with_options(flow_id, SUBMIT_CLAIM_WITHDRAWAL, withdrawal, options)
}
/// Appends one candidate while its round is open, under the round's locks.
/// The answer confirms admission only; the arbitration Step decides the outcome.
fn admit_candidate(context: &mut Context, key: CandidateKey, rank: u32) -> HandlerResult<String> {
Example: examples/rust/src/control_patterns/locked_decision_rounds/flow.rs