Skip to content
Sefra

Pillar: undo and unknown outcomes

A declared way back

Every state-changing intent declares its compensation or is marked irreversible, and a plan that could not be unwound is refused before it runs.

See a rollback, step by step

What goes wrong without it

  • A vendor exists with active credentials although its first payment was refused. Someone finds it weeks later.
  • A payment call times out. A retry may pay twice, and an undo may cancel an order that was in fact paid.

How it works

A plan is a fixed, ordered list of steps. If a step fails after earlier ones took effect, Sefra calls their declared compensations in reverse order. At most one step may be irreversible, and it must be last, so that if it fails everything before it can still be undone.

A three-step plan
{ "steps": [    { "id": "vendor", "intent": "procurement.create_vendor",      "input": { "legal_name": "Northwind Supplies GmbH", "country": "DE", "tax_id": "DE811907980" } },    { "id": "order", "intent": "procurement.create_purchase_order",      "input": { "vendor_id": { "$ref": "steps.vendor.output.vendor_id" }, "sku": "LAPTOP-14",                 "quantity": 10, "unit_price_minor": 120000, "currency": "EUR", "department": "it-ops" } },    { "id": "deposit", "intent": "procurement.pay_deposit",      "input": { "purchase_order_id": { "$ref": "steps.order.output.po_id" },                 "amount_minor": 360000, "currency": "EUR" } } ],  "idempotency_key": "onboard-northwind-2c9d4e7a51f0", "rationale": "New laptop supplier, 30% deposit agreed." }

Scroll sideways for long lines.

Its timeline when step 3 is refused
  1. Received

    • From agent:procurement-bot on behalf of user:u_jonas, a plan of three steps.execution.received#4120
  2. Policy at submission, held for a person

    • procurement.irreversible_requires_approval asks for a finance_approver. Effect require_approval.policy.evaluated#4123
  3. Approval, allowed

    • user:u_maria, finance_approver, decided approve.approval.decided#4127
  4. Step vendor, allowed

    • POST /vendors, response 201.step.succeeded#4130
  5. Step order, allowed

    • POST /purchase-orders, response 201.step.succeeded#4133
  6. Step deposit, denied or failed

    • POST /payments, response 422, code DOWNSTREAM_REJECTED.step.failed#4136
  7. Compensation

    • Undo of step order succeeded, attempt 2, response 200.compensation.succeeded#4139
    • Undo of step vendor succeeded, attempt 1, response 200.compensation.succeeded#4141
  8. Completed

    • Final status rolled_back with DOWNSTREAM_REJECTED.execution.completed#4142
What the agent receives
{  "execution_id": "exe_…B",  "status": "rolled_back",  "replayed": false,  "steps": [    { "id": "vendor",  "intent": "procurement.create_vendor",         "status": "compensated" },    { "id": "order",   "intent": "procurement.create_purchase_order", "status": "compensated" },    { "id": "deposit", "intent": "procurement.pay_deposit",           "status": "failed" }  ],  "error": {    "code": "DOWNSTREAM_REJECTED", "stage": "execution", "step": "deposit",    "message": "The system of record refused the operation.",    "violations": [ { "rule": "downstream.rejected", "message": "The system of record refused the operation.", "actual": 422 } ],    "retryable": false  }}

Scroll sideways for long lines.

Afterwards the vendor exists and is inactive, the order exists and is cancelled, no payment exists, and the budget is back. The systems are not as if nothing had happened.

The irreversible step goes last

A payment cannot be called back, so its manifest says so. Sefra does not reorder a plan. It refuses one whose irreversible step is not last, with PLAN_INVALID.

An intent marked irreversible
intent: procurement.pay_depositversion: 1description: Pay a deposit on a purchase order. Irreversible; always needs a finance approver.effect: writeirreversible: true…

Scroll sideways for long lines.

When the outcome is unknown

Every attempt ends in one of four classes. A step is declared failed only after a definite answer.

Applied
The effect exists. The step succeeded.
Refused
A 4xx other than 408 and 429. Definitely not applied, and repeating will not help.
Not applied, transient
No connection, 429 or 503. Definitely not applied, and worth repeating.
Ambiguous
Anything else, such as a timeout, a reset after sending, or a 500, 502 or 504. The effect may or may not exist.

If retries and a declared probe cannot settle an ambiguous call, the step is marked unknown and the execution stops in needs_intervention. Nothing is compensated yet, because undoing earlier steps would be wrong if the payment did go through. An operator checks the system of record and settles the step. The payment id below is illustrative.

What the agent receivesisError: true Needs intervention
{
  "execution_id": "exe_…B",
  "status": "needs_intervention",
  "replayed": false,
  "steps": [
    {
      "id": "vendor",
      "intent": "procurement.create_vendor",
      "status": "succeeded",
      "output": {
        "vendor_id": "v_2041"
      }
    },
    {
      "id": "order",
      "intent": "procurement.create_purchase_order",
      "status": "succeeded",
      "output": {
        "po_id": "po_7782",
        "total_minor": 1200000
      }
    },
    {
      "id": "deposit",
      "intent": "procurement.pay_deposit",
      "status": "unknown"
    }
  ],
  "poll_after_s": 300,
  "error": {
    "code": "DOWNSTREAM_OUTCOME_UNKNOWN",
    "stage": "execution",
    "step": "deposit",
    "message": "The call may or may not have taken effect. An operator has to settle it; do not submit it again.",
    "retryable": false
  }
}
The operator recordsValue
Stepdeposit
Outcomesucceeded
Outputpayment_id pay_5120
NoteBank statement shows the payment.

Settled as succeeded, the execution continues and, with no step left, ends succeeded. Settled as failed, any earlier steps are compensated and it ends rolled_back, or needs_intervention if an undo cannot complete. With no earlier effect, it ends failed.

What it does not do

  • Compensation is a business reversal, not a database rollback. The vendor stays as an inactive record and the order as a cancelled one, each with its history.
  • A compensation counts as done on its declared answer. By default that is the HTTP status. With success_when, the body must also hold the declared values, or the undo is retried and then goes to an operator. An undo the system reverses later is not tracked.
  • It does not hide intermediate states from other users of the system, and it does not cover side effects the system triggered on its own.
  • An irreversible step that succeeded is not undone. That is why it runs last and why the starter policy holds it for a person.
  • resolved_manually means a named operator declared the situation handled. Sefra then guarantees nothing about the systems' state.

How it behaves

Automated tests cover each of these behaviours.

  • If the payment is refused and the cancel fails once, the plan still ends rolled_back, with the vendor inactive, the order cancelled and the budget restored.
  • If the cancel keeps failing, the execution enters needs_intervention. Once the fault clears, an operator retries the undo and it ends rolled_back.
  • A plan with the irreversible step first is refused with PLAN_INVALID, and no call is made.
  • If a worker is killed during a call and restarted, the execution succeeds with exactly one purchase order.
  • If a payment commits and the connection drops, the execution enters needs_intervention and nothing is undone. An operator settles it either way.
  • A global pause between steps fails the next step, and earlier steps are undone.
  • If the answer to a vendor creation is lost, a probe finds the vendor and the plan continues with exactly one vendor.
  • An operator can close an intervention with a note. It ends resolved_manually, with the operator and the note in the record.
  • A 503 on every attempt ends as failed after the retries. A 500 on every attempt goes to an operator, because a 500 can follow a commit.

Start with a pilot

Stopping an agent is the easy half. Sefra also finishes the job.