# Giving up a step early

## What you want to do

Some failures can't be mended by trying again: Ned's parcel is heavier than any carrier of the shop collects, and when the bank's network fails during a charge, the rescue step, which mends only an expired card, can't help either. You want the shop to give such a step up at once, before the retry or the rescue the workflow would otherwise run.

## The code

The shipping first learns a rule of the shop's business: the shop ships no parcel over 80 kg, the most its backup carrier collects. It fails such an order with the reason `over-parcel-limit`, before it books anything:

Kotlin, from the program [`ch17_giving_up_a_step_early`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch17_giving_up_a_step_early.kt)

```kotlin
/**
 * Ships an order through the carrier, with its receipt in the parcel, gives back its tracking
 * number, and says when the parcel has left. A parcel the carrier won't collect may be taken on
 * another attempt. A gift card has nothing to ship, and the shop ships no parcel over 80 kg.
 */
@Step
class Ship(
    @Input private val order: String,
    @Input private val weight: Long,
    @Input private val receipt: String,
    @Input private val giftCard: Boolean,
) : ItineraStep {
    fun run(@Service carrier: Carrier, contributor: Contributor, reporter: StepReporter): Outcome {
        if (giftCard) return skipBecause("nothing-to-ship")
        if (weight > 80) return fail("over-parcel-limit")
        val tracking = carrier.book(order, weight)
        contributor.contribute("tracking", tracking)
        if (!carrier.collects(weight)) return fail("too-heavy")
        reporter.info("the parcel leaves with receipt $receipt")
        return succeed()
    }
}

```

The interface `Carrier` it asks for is [chapter 12](https://itinera-dev.org/book/kotlin/12-trying-again.md)'s.

Then the shop gets two new step policies. The first, `heavyParcels`, has a hook marked `@OnStepRetry`, from `org.itineradev.itinera.policy`. It asks for the reason the shipping's attempt failed with, and gives the shipping up when that reason is `over-parcel-limit`, since the limit is the same on every attempt.

Kotlin, from the program [`ch17_giving_up_a_step_early`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch17_giving_up_a_step_early.kt)

```kotlin
/**
 * Gives up the shipping of a parcel over the shop's limit, rather than try it again: the limit is
 * the same on every attempt, whichever carrier comes.
 */
@StepPolicy(name = "heavyParcels")
class HeavyParcels : ItineraStepPolicy {
    @OnStepRetry
    fun beforeRetrying(reason: Reason?): OnFailure =
        if (reason?.code == "over-parcel-limit") failWorkflow("no-carrier-can-take-it")
        else keepDefault()
}

```

The second, `outage`, has a hook marked `@OnStepAbnormalTermination`, from the same package. It asks for nothing, and gives up the step it is attached to whenever an exception escapes it.

Kotlin, from the program [`ch17_giving_up_a_step_early`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch17_giving_up_a_step_early.kt)

```kotlin
/**
 * Gives up a step that ends in an error at once: the bank's network is down, and neither a retry
 * nor a rescue can mend that.
 */
@StepPolicy(name = "outage")
class Outage : ItineraStepPolicy {
    @OnStepAbnormalTermination fun endedInError(): OnFailure = failWorkflow("payment-network-down")
}

```

The workflow attaches `Outage` to the charge, beside the audit, and `HeavyParcels` to the shipping, in the `policies` of each step's `@WithStep`:

Kotlin, from the program [`ch17_giving_up_a_step_early`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch17_giving_up_a_step_early.kt)

```kotlin
/**
 * The shop's workflow: it charges, repairing a charge that fails and auditing one that succeeds,
 * then ships, and shipping may be tried once more.
 */
@Workflow(name = "orders", reporters = [Log::class, Desk::class])
@WithStep(
    Charge::class,
    name = "charge",
    rescue = RenewCard::class,
    policies = [Audit::class, Outage::class],
)
@WithStep(Ship::class, name = "ship", retryBudget = 1, policies = [HeavyParcels::class])
class Orders : ItineraWorkflow {
    /** The bank, as the payment client of the journey. */
    @Provider(key = "payments") fun bank(): Provided<Payments> = provide(Bank(thisYear = 2026))

    /** A carrier for every attempt of a step: the main carrier first, the backup carrier after. */
    @Provider(perAttempt = true, key = "carrier")
    fun carriers(attempt: Attempt): Provided<Carrier> =
        if (attempt.isFirst) provide(MainCarrier()) else provide(BackupCarrier())

    /** Names each journey after its order. */
    @IdGenerator
    fun orderNumber(data: DataBag): String =
        data.read<String>("order").orNull() ?: error("an order without a number")
}

```

The charge and the audit are [chapter 15](https://itinera-dev.org/book/kotlin/15-reacting-to-what-a-step-did.md)'s, `RenewCard` [chapter 14](https://itinera-dev.org/book/kotlin/14-repairing-then-trying-once-more.md)'s and the desk [chapter 11](https://itinera-dev.org/book/kotlin/11-naming-a-journey.md)'s. The bank's provider is [chapter 12](https://itinera-dev.org/book/kotlin/12-trying-again.md)'s, the interface `Payments` it lends [chapter 14](https://itinera-dev.org/book/kotlin/14-repairing-then-trying-once-more.md)'s, and the carriers' provider [chapter 13](https://itinera-dev.org/book/kotlin/13-a-carrier-for-each-attempt.md)'s.

The log is [chapter 11](https://itinera-dev.org/book/kotlin/11-naming-a-journey.md)'s, with [chapter 12](https://itinera-dev.org/book/kotlin/12-trying-again.md)'s `detail` and the branches chapters [14](https://itinera-dev.org/book/kotlin/14-repairing-then-trying-once-more.md) to [16](https://itinera-dev.org/book/kotlin/16-skipping-a-step.md) added. Its branch for `StepGivenUp` now calls a function of its own, which says which policy gave a step up, and why:

Kotlin, from the program [`ch17_giving_up_a_step_early`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch17_giving_up_a_step_early.kt)

```kotlin
/** What the log says about a step given up: which policy gave it up, and why, when one did. */
fun givenUp(event: StepGivenUp): String =
    when (val cause = event.cause) {
        is GiveUpCause.FailWorkflow ->
            "${event.step}: FailWorkflow from ${cause.decidedBy.policy}, ${cause.reason.code}"
        else -> "${event.step}: ${cause.cause}"
    }

```

It runs the same five orders as [chapter 16](https://itinera-dev.org/book/kotlin/16-skipping-a-step.md), and Cat's again, whose 120 is more than the bank's network can take, as in [chapter 12](https://itinera-dev.org/book/kotlin/12-trying-again.md). It prints:

What `ch17_giving_up_a_step_early` prints, recorded in [`ch17_giving_up_a_step_early.txt`](https://github.com/itinera-dev/book/blob/main/kotlin/expected/ch17_giving_up_a_step_early.txt)

```text
Ann's order:
log orders A-1 #1 journey_started order, price, weight, card, giftCard
log orders A-1 #2 attempt_started charge, attempt 1
log orders A-1 #3 service_provided charge gets payments from bank, a ch17_giving_up_a_step_early.Bank
the bank charges 40 for order A-1
log orders A-1 #4 step_succeeded charge
log orders A-1 #5 contribution_committed receipt
log orders A-1 #6 hook_called audit: on step success of charge
log orders A-1 #7 contribution_committed audit
log orders A-1 #8 attempt_started ship, attempt 1
log orders A-1 #9 service_provided ship gets carrier from carriers, a ch17_giving_up_a_step_early.MainCarrier
the main carrier books order A-1
log orders A-1 #10 step_info ship: the parcel leaves with receipt R-A-1
log orders A-1 #11 step_succeeded ship
log orders A-1 #12 contribution_committed tracking
log orders A-1 #13 journey_succeeded
the journey succeeded, with receipt R-A-1 and tracking T-A-1
audit: charge took 40, receipt R-A-1
Gil's order:
log orders A-7 #1 journey_started order, price, weight, card, giftCard
log orders A-7 #2 attempt_started charge, attempt 1
log orders A-7 #3 service_provided charge gets payments from bank, a ch17_giving_up_a_step_early.Bank
the bank charges 45 for order A-7
log orders A-7 #4 step_succeeded charge
log orders A-7 #5 contribution_committed receipt
log orders A-7 #6 hook_called audit: on step success of charge
log orders A-7 #7 contribution_committed audit
log orders A-7 #8 attempt_started ship, attempt 1
log orders A-7 #9 service_provided ship gets carrier from carriers, a ch17_giving_up_a_step_early.MainCarrier
the main carrier books order A-7
log orders A-7 #10 step_failed ship: too-heavy
log orders A-7 #11 hook_called heavyParcels: on step retry of ship
log orders A-7 #12 step_retrying ship
log orders A-7 #13 attempt_started ship, attempt 2
log orders A-7 #14 service_provided ship gets carrier from carriers, a ch17_giving_up_a_step_early.BackupCarrier
the backup carrier books order A-7
log orders A-7 #15 step_info ship: the parcel leaves with receipt R-A-7
log orders A-7 #16 step_succeeded ship
log orders A-7 #17 contribution_committed tracking
log orders A-7 #18 journey_succeeded
the journey succeeded, with receipt R-A-7 and tracking B-A-7
audit: charge took 45, receipt R-A-7
Jo's order:
log orders A-8 #1 journey_started order, price, weight, card, giftCard
log orders A-8 #2 attempt_started charge, attempt 1
log orders A-8 #3 service_provided charge gets payments from bank, a ch17_giving_up_a_step_early.Bank
the bank declines card jo-2024: it has expired
log orders A-8 #4 step_failed charge: card-expired
log orders A-8 #5 step_rescuing charge: failure, rescued by charge_rescue
log orders A-8 #6 attempt_started charge_rescue, attempt 1
the bank says card jo-2024 was replaced by jo-2029
log orders A-8 #7 step_succeeded charge_rescue
log orders A-8 #8 contribution_committed card
log orders A-8 #9 data_overwritten card
log orders A-8 #10 step_rescued charge by charge_rescue
log orders A-8 #11 attempt_started charge, attempt 2
the bank charges 30 for order A-8
log orders A-8 #12 step_succeeded charge
log orders A-8 #13 contribution_committed receipt
log orders A-8 #14 hook_called audit: on step success of charge
log orders A-8 #15 contribution_committed audit
log orders A-8 #16 attempt_started ship, attempt 1
log orders A-8 #17 service_provided ship gets carrier from carriers, a ch17_giving_up_a_step_early.MainCarrier
the main carrier books order A-8
log orders A-8 #18 step_info ship: the parcel leaves with receipt R-A-8
log orders A-8 #19 step_succeeded ship
log orders A-8 #20 contribution_committed tracking
log orders A-8 #21 journey_succeeded
the journey succeeded, with receipt R-A-8 and tracking T-A-8
audit: charge took 30, receipt R-A-8
Kim's order:
log orders A-9 #1 journey_started order, price, weight, card, giftCard
log orders A-9 #2 attempt_started charge, attempt 1
log orders A-9 #3 service_provided charge gets payments from bank, a ch17_giving_up_a_step_early.Bank
the bank charges 25 for order A-9
log orders A-9 #4 step_succeeded charge
log orders A-9 #5 contribution_committed receipt
log orders A-9 #6 contribution_committed code
log orders A-9 #7 hook_called audit: on step success of charge
log orders A-9 #8 contribution_committed audit
log orders A-9 #9 attempt_started ship, attempt 1
log orders A-9 #10 service_provided ship gets carrier from carriers, a ch17_giving_up_a_step_early.MainCarrier
log orders A-9 #11 step_skipped ship: nothing-to-ship
log orders A-9 #12 contributions_discarded ship
log orders A-9 #13 journey_succeeded
the journey succeeded, with receipt R-A-9 and tracking none
audit: charge took 25, receipt R-A-9
Ned's order:
log orders A-12 #1 journey_started order, price, weight, card, giftCard
log orders A-12 #2 attempt_started charge, attempt 1
log orders A-12 #3 service_provided charge gets payments from bank, a ch17_giving_up_a_step_early.Bank
the bank charges 50 for order A-12
log orders A-12 #4 step_succeeded charge
log orders A-12 #5 contribution_committed receipt
log orders A-12 #6 hook_called audit: on step success of charge
log orders A-12 #7 contribution_committed audit
log orders A-12 #8 attempt_started ship, attempt 1
log orders A-12 #9 service_provided ship gets carrier from carriers, a ch17_giving_up_a_step_early.MainCarrier
log orders A-12 #10 step_failed ship: over-parcel-limit
log orders A-12 #11 hook_called heavyParcels: on step retry of ship
log orders A-12 #12 step_given_up ship: FailWorkflow from heavyParcels, no-carrier-can-take-it
log orders A-12 #13 journey_failed ship
desk: order A-12 failed at ship
the journey failed, with receipt R-A-12 and tracking none
audit: charge took 50, receipt R-A-12
Cat's order:
log orders A-3 #1 journey_started order, price, weight, card, giftCard
log orders A-3 #2 attempt_started charge, attempt 1
log orders A-3 #3 service_provided charge gets payments from bank, a ch17_giving_up_a_step_early.Bank
the bank's network fails while charging order A-3
log orders A-3 #4 step_abnormal_termination charge: the payment network is down
log orders A-3 #5 hook_called outage: on step abnormal termination of charge
log orders A-3 #6 step_given_up charge: FailWorkflow from outage, payment-network-down
log orders A-3 #7 journey_failed charge
desk: order A-3 failed at charge
the journey failed, with receipt none and tracking none
audit: none
```

## How it works

1. **A hook can end the journey.** What a hook returns can hold a **lifecycle**: a decision that ends the journey there. `FailWorkflow` fails it, with a reason, as a step's failure has one. `failWorkflow(...)`, which `ItineraStepPolicy` gives the class, returns it, and `keepDefault()`, as in [chapter 15](https://itinera-dev.org/book/kotlin/15-reacting-to-what-a-step-did.md), leaves what happens next unchanged.
2. **`on step retry` runs before a retry.** Gil's parcel of 60 kg is within the limit, but the main carrier won't collect it, so the shipping fails with `too-heavy`, the hook keeps the default, and the backup carrier takes the parcel on attempt 2. Ned's parcel of 90 kg is over the limit, so the shipping fails with `over-parcel-limit`, and the hook fails the journey: the shipping is given up with the cause `FailWorkflow`, decided by `heavyParcels`. **The retry budget says how many retries a step may have; a policy can still decide that none of them can help.**
3. **The step says what happened, and the policy decides what to do about it.** The limit is a rule of the business, so it lives in the shipping; the hook only branches on the reason. It asks for the reason by its type, as the rescue step does in [chapter 14](https://itinera-dev.org/book/kotlin/14-repairing-then-trying-once-more.md), but as a `Reason?`, since an attempt that ended in an exception reports none.
4. **`on step abnormal termination` runs first after an exception.** It is called as soon as an exception escaping `run` ends an attempt, before any retry, rescue or giving up. Cat's charge ended in the bank's `NetworkDown`, [chapter 12](https://itinera-dev.org/book/kotlin/12-trying-again.md)'s, so `outage` failed the journey with the reason `payment-network-down`, and the charge was given up at once. Its rescue step never ran.
5. **A step given up by a hook says who decided.** `StepGivenUp`'s `cause` is a `GiveUpCause`, from `org.itineradev.itinera.event`, here a `GiveUpCause.FailWorkflow`, which carries the `reason` and, as `decidedBy`, the policy and the hook that returned it. `givenUp` matches it with `when`, and writes any other cause as [chapter 12](https://itinera-dev.org/book/kotlin/12-trying-again.md) does. Then `journey_failed` follows, naming the step. Neither step decided to give up: the shipping only reported that the parcel was over the limit, and the charge only let the bank's exception escape.
6. **Each hook may return only some lifecycles, and its type says which.** A method marked `@OnStepRetry` or `@OnStepAbnormalTermination` returns an `OnFailure`, from `org.itineradev.itinera.policy`, whose only values are what `keepDefault()` and `failWorkflow(...)` return, since failing the journey is the only thing either hook may decide; the compiler refuses anything else.

So no order spends a retry or a rescue that can't help it. But none of the shop's journeys tells the customer how it went: the lines that say whether a journey succeeded are printed by `main`, and no customer ever reads them.

## Outcomes

We have shown how a step policy gives a step up early, when no retry or rescue can help it.

| The usual way                                                                                                       | With Itinera                                                                                                                      |
| ------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| Deciding that a retry is pointless usually means more conditions inside the retry loop, beside the call it retries. | A policy's `on step retry` hook decides it from the reason the step reported, attached to the step in the workflow's declaration. |
| What happens after an unexpected error is often decided wherever the error is caught.                               | `on step abnormal termination` decides it for the step, before any retry or rescue, and the event says which policy did.          |
