Practical technical guides / Agent-era architecture: sagas and compensation (failed compensations)

When a Compensation Fails: Retry It Idempotently, Park the Saga for Repair, and Keep the Agent Out of the Undo

Compensations fail too. Six scripted orders: fire and forget left 4 wrong, an improvising agent returned money twice, idempotent retries with a repair state left 0 wrong.

By Sandeep Belgavi · Published 2026-11-18

A compensation that fails must not be reported as done
Original explanatory diagram; synthetic values where shown.

Compensations fail too. Six scripted orders: fire and forget left 4 wrong, an improvising agent returned money twice, idempotent retries with a repair state left 0 wrong.

The undo path is ordinary code that can fail

A saga replaces one distributed transaction with a sequence of local steps, each paired with a compensation that semantically undoes it. At Belgavi's Book Store an order saga reserves stock, buys a shipping label and captures the payment. If the fourth step, handing the parcel to the warehouse, fails, the orchestrator runs the compensations in reverse: reverse the payment, void the label, release the stock.

Most designs spend their care on the forward path and treat compensations as if they always succeed. They are remote calls like any other. The payment service can be down for longer than the retry budget. The carrier can refuse to void a label after its daily cut-off. A reversal can time out after the money has already moved. When Geetha's team added an agent to the help desk, the agent was given tools that could also start and steer these sagas, which raised a new question: when a compensation fails, should the agent work out another way to make things right?

The companion example answers that with six scripted orders and three undo policies. Every service is an in-memory fake with a ledger of what actually happened, so the saga's belief can be compared with the world.

Six orders, three policies

OrderScripted failure during the undo
BB-1501None
BB-1502Payment reversal fails twice, then works
BB-1503Payment reversal times out, but the reversal happened
BB-1504Label void is refused permanently
BB-1505Stock release times out with no effect, then works
BB-1506Payment reversal fails six times in a row

The policies:

  • Fire and forget. Call each compensation once, log failures and mark the saga compensated.
  • Agent improvises. When a compensation fails, the agent picks an alternative: a store credit instead of a failed payment reversal, a note in the case instead of a failed void or release.
  • Deterministic recovery. Retry each compensation with its own idempotency key, check the status after an unknown outcome before sending again, and open a repair task for anything that fails permanently or exhausts its retries. Run the remaining independent compensations either way, and end in a repair_required state rather than claiming success.
PolicyMarked compensated but wrongMoney returned twiceParked for repairFully consistent
Fire and forget4002
Agent improvises3103
Deterministic recovery0024

Fire and forget is the failure most systems already have. Four orders were marked compensated while the world disagreed: two customers never got their money back, one label stayed live and one reservation was never released. Nothing in the saga's own state shows it.

The improvising agent looks better on consistency, 3 orders against 2, and it is worse in the way that matters. On BB-1503 the reversal timed out after it had already gone through. The agent treated the timeout as a failure and issued a store credit, so the customer was paid back twice. An unknown outcome is not a failure, and no amount of model judgement can tell the two apart without asking the payment service. On BB-1502 and BB-1506 the store credit happened to leave the order consistent, but it also replaced a payment reversal with a different financial instrument, which is a business decision the saga was never authorised to make.

The deterministic recovery loop

def undo(svc, order, done, max_attempts=4):
    repairs = []
    for step in reversed(done):
        name, key = UNDO[step], f'{order}:{UNDO[step]}'
        for attempt in range(1, max_attempts + 1):
            try:
                svc.call(name, order, key=key)
                break
            except Transient:
                continue
            except Unknown:
                if svc.status(name, order) == 'done':
                    break
                continue
            except Permanent:
                repairs.append(name)
                break
        else:
            repairs.append(name)
    return 'repair_required' if repairs else 'compensated'

Three rules carry the weight. The idempotency key is per compensation and stable across retries, so a retry after a lost response cannot reverse the payment twice. An unknown outcome triggers a status query before any resend; on BB-1503 that query found the reversal already done. A failure that retries cannot fix becomes a repair task with the order, the compensation, the error and the attempts, and the saga stops claiming it is finished. On BB-1504 the payment was reversed and the stock released, and only the label is left for a person. On BB-1506 the label and stock were handled and the payment reversal waits for the outage to end.

What a repair task must carry

A parked saga is only useful if the person who picks it up can act without reconstructing the history. The repair task should name the order and the saga, the compensation that failed, the classified error with the service's own message, every attempt with its timestamp and idempotency key, and the state of the other compensations. It should also say what the customer has been told. In the example, BB-1506's task would show four failed attempts at the reversal, each with the same idempotency key, a voided label and released stock, so the person knows only the payment remains.

Where the agent belongs

The agent is useful around the saga, not inside its undo path. It can explain the state to the customer: the payment reversal is pending and will be retried, or a colleague is reviewing the label. It can draft the repair task's summary for the person who picks it up. It can propose an alternative remedy, such as a store credit, as a new command that goes through the normal approval route and is recorded as a separate decision. What it must not do is choose a compensation at run time, because compensations must be known when the forward step is designed. Each one needs its own idempotency rules, its own status query and its own permanent-failure handling, written and tested in advance.

What stays deterministic

  • The mapping from each forward step to its compensation, fixed in code.
  • The retry budget, backoff and the classification of errors into transient, permanent and unknown.
  • The status query after an unknown outcome.
  • The saga states, including repair_required, and which transitions are allowed.
  • The repair queue and who may resolve an item in it.

Migration steps

  1. Inventory every compensation and ask, for each, what happens on a timeout. If the service has no way to query whether the effect happened, add one before anything else.
  2. Give every compensation an idempotency key derived from the saga and the step, and make the receiving service honour it.
  3. Classify errors explicitly. A refusal with a stable reason is permanent; a connection failure is transient; a timeout after sending is unknown.
  4. Add a repair_required state and a queue that people actually watch, with an age alert. A parked saga that nobody sees is fire and forget with extra steps.
  5. Audit existing sagas against the systems of record to find the ones already marked compensated but inconsistent.
  6. Remove any agent tool that can call compensation endpoints directly. Replace it with a read-only saga status tool and a tool that proposes a new remedy for approval.

When this is unnecessary

If every step is local to one database, use a database transaction instead of a saga. If a step's effect is harmless to leave behind, such as a cached quote, it may not need a compensation at all, only expiry. If the volume is small and a person reviews every failed order anyway, a clear failure state and an alert may be enough; the retry and status logic pays off when failures are frequent enough that people cannot keep up.

Failure modes to test

  • The status query reads a replica and says not found for an effect that already happened.
  • The idempotency key includes the attempt number, so every retry is a new request.
  • A compensation is retried after the forward step's effect was already changed by someone else, such as a partial refund issued by a person.
  • The repair queue grows silently because the alert counts sagas instead of their age.
  • The forward step itself had an unknown outcome, so the saga compensates a step that never took effect; the compensation must tolerate that and report it.

Primary references and authorship

Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/saga_compensation_repair. Views are my own and do not represent any employer, past or present.