Skip to content

When the outcome is unknown

Scenario. Sefra sends a request to your ERP. The ERP does the work, then the connection drops. Did it happen? Retrying might pay twice, and giving up might lose a payment.

Sefra never guesses. It has two ways to find out, and you choose which by how you write the intent.

The intent has What Sefra does
An idempotency key the system honours Retries safely
A probe, a read that shows whether it happened Asks the system, then continues
Neither Stops and asks a person

Start from a clean sandbox, in its directory:

./sandbox destroy
./sandbox up

The starter’s create_vendor has a probe that looks the vendor up by tax id. Make the ERP create the vendor and then drop the connection:

AGENT=$(./sandbox token agent)
./sandbox erp fail '{"operation": "POST /vendors", "mode": "commit_then_drop"}'
RESPONSE=$(curl -sS -X POST http://127.0.0.1:18180/v1/executions \
  -H "Authorization: Bearer $AGENT" -H "Content-Type: application/json" \
  -H "Idempotency-Key: unknown-example-vendor" \
  -d '{"intent": "procurement.create_vendor",
       "input": {"legal_name": "Baltic Freight OU", "country": "EE", "tax_id": "EE100000001"}}')
SEFRA_OPERATOR_TOKEN=$(./sandbox token approver) ./sandbox sefra approvals approve "$(echo "$RESPONSE" | jq -r .approval.approval_id)" --comment "Checked"
./sandbox wait "$(echo "$RESPONSE" | jq -r .execution_id)" | jq '{status, output: .steps[0].output}'
./sandbox erp state | jq '[.vendors[] | select(.legal_name == "Baltic Freight OU")] | length'

The execution succeeded with the vendor’s id, and the ERP holds exactly one such vendor. Sefra found it with the probe and did not create a second.

Payment systems often have neither a key nor a way to look a payment up. Add an irreversible payment intent like that:

rm -rf payments && cp -R bundle payments
cat > payments/intents/procurement.pay_deposit.yaml <<'EOF'
intent: procurement.pay_deposit
version: 1
description: Pay a deposit on a purchase order. Irreversible; always needs a finance approver.
effect: write
irreversible: true
tags: [procurement, payments]
input_schema:
  type: object
  additionalProperties: false
  required: [purchase_order_id, amount_minor, currency]
  properties:
    purchase_order_id: { type: string, minLength: 1, maxLength: 64 }
    amount_minor:      { type: integer, minimum: 1, x-sefra-money: true }
    currency:          { type: string, enum: [EUR, USD] }
output_schema:
  type: object
  required: [payment_id]
  properties:
    payment_id: { type: string }
execute:
  connector: erp
  method: POST
  path: /payments
  body: { purchase_order_id: "${input.purchase_order_id}", amount_minor: "${input.amount_minor}", currency: "${input.currency}" }
  idempotency: { mode: none }
  success: [201]
  output: { payment_id: "${response.body.id}" }
summary_template: "Pay deposit: ${input.amount_minor} ${input.currency} (minor units) for purchase order ${input.purchase_order_id}"
EOF
./sandbox sefra bundle test payments
SEFRA_TOKEN=$(./sandbox token admin) ./sandbox sefra bundle apply payments

The test passes, with a warning that says exactly what is about to happen: an ambiguous result will need a person.

Place an order to pay against, then make the payment’s response disappear:

PO=$(curl -sS -X POST http://127.0.0.1:18180/v1/executions \
  -H "Authorization: Bearer $AGENT" -H "Content-Type: application/json" \
  -H "Idempotency-Key: unknown-example-order" \
  -d '{"intent": "procurement.create_purchase_order",
       "input": {"vendor_id": "v_1001", "sku": "FORKLIFT", "quantity": 1,
                 "unit_price_minor": 1500000, "currency": "EUR", "department": "it-ops"}}' \
  | jq -r '.steps[0].output.po_id')
./sandbox erp fail '{"operation": "POST /payments", "mode": "commit_then_drop"}'
RESPONSE=$(curl -sS -X POST http://127.0.0.1:18180/v1/executions \
  -H "Authorization: Bearer $AGENT" -H "Content-Type: application/json" \
  -H "Idempotency-Key: unknown-example-payment" \
  -d '{"intent": "procurement.pay_deposit",
       "input": {"purchase_order_id": "'"$PO"'", "amount_minor": 300000, "currency": "EUR"},
       "rationale": "Deposit agreed with the supplier"}')
EXECUTION=$(echo "$RESPONSE" | jq -r .execution_id)
SEFRA_OPERATOR_TOKEN=$(./sandbox token approver) ./sandbox sefra approvals approve "$(echo "$RESPONSE" | jq -r .approval.approval_id)" --comment "Deposit agreed"
./sandbox wait "$EXECUTION" | jq '{status, step: .steps[0].status, code: .error.code}'

The execution is frozen. Sefra will not retry it, and the agent is told not to resubmit it. An operator.alert notification went out:

./sandbox erp state | jq -r '[.webhooks[].body | select(.type == "operator.alert")] | length'

The operator checks the real system and finds the payment:

PAYMENT=$(./sandbox erp state | jq -r '.payments[-1].id')
echo "$PAYMENT"
SEFRA_OPERATOR_TOKEN=$(./sandbox token admin) ./sandbox sefra executions settle-step "$EXECUTION" \
  --step main --outcome succeeded --output '{"payment_id": "'"$PAYMENT"'"}' --note "Found in the ERP payment ledger"
./sandbox wait "$EXECUTION" | jq '{status, output: .steps[0].output}'

If the ERP had shown no payment, the operator would use --outcome failed, and the execution would end as failed. Either way, the decision and its note are in the audit trail.

When you write an intent for a system with no idempotency support, add a probe if the system offers any way to look the result up. Each probe you add is one less 3 a.m. page. Where none exists, the operator runbooks cover what to do.

To return to the starter bundle:

SEFRA_TOKEN=$(./sandbox token admin) ./sandbox sefra bundle apply bundle