Stopping an agent is the easy half. A policy says no, the request ends, and nothing has changed. The harder case is the plan that was allowed, did two things correctly, and failed on the third.

By then the systems hold real changes. A vendor exists. A purchase order is open. Something has to take them back, and the worst moment to work out what that something is comes after they exist.

So Sefra moves the question forward. Before an agent can use a write at all, the write has to say how it is undone, or say plainly that it cannot be.

The rule

In Sefra the governed unit is an intent: a named business operation with a schema, defined in a manifest in a bundle. Every intent has one of three classes.

Class How it is declared What follows
Read effect: read No compensation. May appear anywhere in a plan
Compensable effect: write with a compensate block The compensation operation must be idempotent
Irreversible effect: write, irreversible: true At most one per plan, in last position

There is no fourth class. A write with neither a compensate block nor irreversible: true fails bundle validation:

an intent with effect: write needs a compensate block or irreversible: true

Such an intent can never appear in a tool list, so no agent can call it. sefra bundle test exits non-zero on a bundle that holds one, so the mistake is caught before anything is deployed.

The check runs when a bundle is validated, so it comes before any agent can see the intent. Nobody has to remember it during an incident.

What a declaration looks like

Here is the way back for creating a vendor, from an example procurement bundle:

compensate:
  connector: erp
  method: POST
  path: "/vendors/${output.vendor_id}/deactivate"
  idempotency: { mode: natural }
  success: [200]
  already_undone: [404]

The declaration has four properties.

It is data, not code. The manifest names an operation on a connector. Its ${…} lookups have no functions, arithmetic or conditionals. A reviewer can read it, and no model is involved in choosing or building the undo.

It uses the system’s own API. Deactivating a vendor is a business operation the ERP already offers. Sefra does not reach into a database or restore a snapshot.

It draws on what the forward step returned. The vendor_id comes from the recorded output of the step being undone. That is why the outcome of a step has to be known before it can be compensated.

It must be safe to repeat. A compensation may need several attempts, so bundle validation refuses mode none on one. An undo that could apply twice would be a new hazard in the middle of a recovery.

The deposit payment in the same bundle takes the other path. It declares irreversible: true and has no compensate block, so the plan treats a paid deposit as final. That is a property of this intent and not of payments in general: where a system offers a real reversal, the author can declare it, as the bundle for Stripe does with a refund.

Why it has to come first

An undo written after the fact has three problems, and declaring it up front removes each of them.

It can be reviewed. A compensation sits in the same bundle as its write, so one review in git can cover both. Sefra does not enforce that review today. Whoever approves the ability to create a vendor can also see what happens when that creation has to be taken back.

The plan can be judged before it runs. Because each step’s class is known, Sefra can refuse a plan that could not be unwound before anything has happened. Refusal comes before any side effect, and compensation comes after one. The first is much cheaper.

No model improvises the recovery. If an agent were left to clean up its own failed plan, the recovery would be a fresh set of model decisions, taken while several systems sit half-changed. In Sefra, retries and compensation are deterministic code working from the declaration.

One irreversible step, and it goes last

Some operations cannot be undone. Forbidding them would exclude many payments, the most valuable case. Reordering them automatically would change the meaning of plans whose steps depend on each other.

Sefra allows at most one irreversible step per plan and requires it to be the last. In saga terms it is the pivot. If it fails, everything before it can still be undone, and nothing runs after it. That gives a simple property you can check by reading a plan: before the last step, every effect is undoable.

A plan with the irreversible step first is rejected with PLAN_INVALID, and no call reaches any system.

The cost is real. Some workflows need two irreversible steps and cannot be expressed as one plan. They have to be split into separate executions, with the agent responsible for what lies between them.

What happens when the undo runs

Take the three-step plan again. The payment is refused, and the first attempt to cancel the order answers 503. The compensations run in reverse order of completion. The cancel is retried and succeeds. The execution ends rolled_back: the vendor is inactive, the order is cancelled, no payment exists, and the budget is restored.

Compensations are not evaluated against policy and are not stopped by a pause or a suspension. An undo of something Sefra itself did, under a mapping reviewed in advance, is not a new business decision, and blocking it would strand the systems half-undone. Every compensation is still audited.

A compensation can also fail for good. If the cancel keeps answering 503 for the whole retry window, the execution enters needs_intervention. If the alert webhook is configured, an operator is alerted. The vendor has already been deactivated, because the remaining compensations are still attempted. Once the fault is cleared, retry-compensation takes the execution to rolled_back.

What rolled_back does not mean

The guarantee is narrow on purpose. rolled_back means that every step that took effect has had its declared compensation return success.

It does not mean the systems look as if nothing had happened. After that rollback, the vendor and the order still exist, as an inactive vendor and a cancelled order with their own history. Compensation is a business reversal. It does not restore earlier state bit for bit, it does not hide intermediate states from other users of the system, and it does not cover what the system of record triggered on its own, such as an email or a sync job.

Those limits are a reason to declare the undo carefully. An author marking a step compensable is stating that the reversal is acceptable to the business, including everything it leaves behind. If it is not, the honest declaration is irreversible: true.

You can see the rule for yourself: run sefra bundle test on a bundle with a write that declares neither a compensation nor irreversible: true, and it refuses the bundle.