undo

infix fun undo(compensation: suspend CompensationScope<R, S>.() -> Unit): StatefulSagaStepConfiguration<C, R, S>

Defines compensation logic with "and undo { ... }" syntax.

The compensation is executed with a CompensationScope receiver, providing:

  • result: The result from this step's forward action

  • state: Current saga state (includes updates from later steps!)

This enables "smart compensation" where earlier steps can make decisions based on the execution progress of later steps.

Example:

and undo {
// `state` contains updates from ALL completed steps
when {
state.escrowCaptured -> cashManager.dispenseCashRefund(state.paymentAmount)
state.paymentAmount > 0 -> cashManager.returnEscrow()
else -> cashManager.disableAcceptors()
}
}