Skip to content

Idempotency and dedupe contracts

An assignment can reach a node more than once: a watch redelivers it, a retry brings it back, a report is sent twice. The node answers each delivery with the same result by keying execution on an idempotency key it derives from the assignment, and by recording each execution's outcome under that key. This page is how the key is derived, what the execution record holds, and the three places a duplicate is caught.

Where each duplicate is caught

flowchart TB
  subgraph NODE["XRATUD, one process"]
    direction LR
    C1[("Execution record<br/>assignment, attempt, execution key")]
    C2[("Status outbox<br/>assignment, attempt, stage, sequence")]
  end
  subgraph CP["CONTROL PLANE"]
    direction LR
    C3[("Report dedupe<br/>assignment, action, report hash")]
  end
  D1["A delivery that already ran"] --> C1
  D2["A report produced twice"] --> C2
  D3["A report delivered twice"] --> C3

Each check has its own key, fitted to what it guards. A second delivery of an assignment stops at the execution record, a report produced twice stops at the status outbox, and a report that arrives twice is recognised by the control plane from its hash. The following execution key is the one the first check uses.

Deriving the key

The effective key is the first of these that yields a value:

  1. The node idempotency expression, idempotency.node_idempotency_key_expr, evaluated against the assignment.
  2. The assignment's own idempotency_key.
  3. The run request identifier, idempotency.run_request_id.
  4. <run_id>:<node_id>:<attempt>.

The expression is carried on the assignment and evaluated against six sources:

input       the assignment payload
state       the payload's state
artifacts   the payload's artifacts
result      the payload's result
meta        workspace_uuid, assignment_id, run_id, node_id, attempt,
            trace_id, session_id, run_request_id
request     idempotency_key, run_request_id, session_id

A template substitutes values into text. This expression is an example:

${input.request_id}:${meta.node_id}      resolves to  req-abc:node-sample-001

An expression that is a single ${...} is evaluated as a value, and is limited to comparisons, boolean and arithmetic operators, and constants. An expression that fails to evaluate yields nothing, and the next source in the order is used. A faulty expression therefore falls back and assignment handling continues.

The execution record

Each execution is recorded under <assignment_id>:<attempt>:<key>, contract xratud.assignment_execution_idempotency.v1:

Field Holds
assignment_id, attempt, idempotency_key The record's identity
run_id, node_id, assignment_kind The assignment correlation identity and the closed assignment kind that the node handled
status started, then succeeded or failed
result, error The outcome reported at completion
first_seen_at, last_seen_at When it was first and last delivered
replay_count How many deliveries arrived after it finished

Preparation opens the record before the route runs, and completion closes it:

The record is Preparation does
Absent Creates it as started, and the assignment runs
started Updates it, and the assignment runs again
succeeded or failed Counts a replay and reuses the terminal outcome

A started record means an earlier delivery began and did not finish, so it is run again. Completion records the outcome: a status of failed, error or blocked is recorded as failed, and any other as succeeded.

A replay reuses the recorded result

A delivery that finds a terminal record emits an assignment.replay_deduped event and the log line assignment replay deduped before execution, both carrying:

contract_version    xratud.assignment_execution_idempotency.v1
assignment_id, assignment_kind, attempt, idempotency_key
terminal_status     the outcome already recorded
replay_count        how many replays have arrived

A rising replay_count on one assignment means the control plane is delivering something the node has already finished. The corresponding completion report is still outstanding at the control-plane boundary.

What each check covers

The execution record lives in the process's lease state, so it catches duplicates for as long as the process runs. The status outbox catches a report staged twice by the same process. The control plane's record of each report's hash per assignment and action, together with the fence, covers deliveries across processes and nodes. See Status ack runtime and Recovering the control plane link.