---
title: "Countdown: a bounded loop"
description: Prove that a countdown terminates, follow its manager commands, and
  see the checker reject a measure that cannot prove progress.
url: https://docs.rastok.com/examples/07-countdown
source_revision: v0.2.0-alpha
---

# Countdown: a bounded loop

This program counts down from an integer and emits one final message.

A conversation keeps a resource alive until closure. A countdown asks the
opposite question: can the program prove that repeated work will finish?
The answer is written beside the Operator, without a backwards graph edge.

The [file formats](/guide/09-command-line.md#file-formats) are `.rkg` for
the program and Lab, `.rkc` for a reviewed scenario reference and `.rkr`
for a real execution recording.

## Run

From the repository root, with `rastok` installed:

```sh
rastok simulate examples/countdown.rkg --compare examples/traces/countdown
```

```text
PASS proof.positive
PASS proof.already_finished
2 scenario(s), 0 failed
```

Add `--ui` to inspect each admitted application and its manager transition.

```sh
rastok simulate examples/countdown.rkg --compare examples/traces/countdown --ui
```

This requires `hosted-ui`. Open the private loopback URL printed by the CLI.

For the controls and source view, see [the debugger tour](/debugger.md).

## Read the program

The App wires `in -> count -> out`. Inside `count`, a positive integer produces
`Loop.next = data - 1`; otherwise the Operator emits `liftoff`. `Loop.next`
is a command to the Loop manager, not a port or a graph edge. The manager
records the command and admits the next application after the previous one
has finished.

`Loop.state` is the envelope admitted for the current application.
`Loop.measure = Loop.state.data` selects its integer DATA as the decreasing
quantity. The positivity guard and subtraction by one provide the proof.
The terminal `out` supplies the final Done verdict.

This example needs no exhaustion output because its measure proves termination.
With an explicit fuel bound, possible exhaustion instead produces
`LoopExpired { last_state: EntryType }` on the host's declared terminal,
normally `err` for an Operator or `exit` for a Call. It must have a destination.
Loop never adds a synthetic expiry port. See [`docs/spec/loop.md`](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/loop.md) §§8 and 10.

There is no Connector in this program. The [Lab reference](/reference/lab.md)
explains expectations and observation windows; `advance` supplies virtual time.

<a id="graph"></a>

Source: `examples/countdown.rkg`

```rkg
// A passive App wires one finite Operator application. The measure proves
// termination; Loop.next is a manager command, not a spatial back-edge.
graph countdown : App {
  input in: int
  output out: string

  operator count : Loop {
    input in: int
    output out: string
    Loop.measure = Loop.state.data

    on in match {
      data > 0 => Loop.next = data - 1
      _        => out = "liftoff"
    }
  }

  in -> count -> out
}

lab proof {
  scenario positive {
    app.in = 3
    expect app.out == "liftoff"
  }
  scenario already_finished {
    app.in = 0
    expect app.out == "liftoff"
  }
}
```

## Expected result

`proof.positive` admits states 3, 2, 1 and 0, then emits one `liftoff`.
`proof.already_finished` admits 0 and emits the same result without recurrence.
The CLI ends with `2 scenario(s), 0 failed`.

Inspect the positive trace: ordinary Operator transactions produce commands;
Manager transactions, labeled `l:loop` in the trace, record its progress. Four applications and
three recurrences are different counts. Each application has its own causal
identity even when it belongs to the same run.

## Break it

Replace `Loop.measure = Loop.state.data` with `Loop.measure = 3`, then run:

```sh
rastok check examples/countdown.rkg
```

The checker reports exactly:

```text
error[E-LOOP]: this Loop cannot be proved finite: the Loop measure is outside the supported direct integer-path fragment
  --> examples/countdown.rkg:7:3
  |
7 |   operator count : Loop {
  |   ^^^^^^^^^^^^^^^^^^^^^^^
  = detail: this Loop cannot be proved finite: the Loop measure is outside the supported direct integer-path fragment
```

Restore the measure before trying the next edit (this discards edits to the example):

```sh
git checkout -- examples/countdown.rkg
```

For a runtime comparison, change only the Operator's
`out = "liftoff"` to `out = "changed"` and run the complete comparison below.

```sh
rastok simulate examples/countdown.rkg --compare examples/traces/countdown
```

```text
FAIL proof.positive
     expect app.out == "liftoff"  (countdown.rkg:24)
     observed app.out: "changed"
     expected 1 matching delivery on app.out, but observed 0
     golden divergence: record 14: emissions(23)[0][2] . data(5)[1] (ccbor value)[1]; expected "liftoff"; got "changed"; cause: TRANSACTION "count" · trigger "p:in" · cause_id absent
FAIL proof.already_finished
     expect app.out == "liftoff"  (countdown.rkg:28)
     observed app.out: "changed"
     expected 1 matching delivery on app.out, but observed 0
     golden divergence: record 5: emissions(23)[0][2] . data(5)[1] (ccbor value)[1]; expected "liftoff"; got "changed"; cause: TRANSACTION "count" · trigger "p:in" · cause_id absent
2 scenario(s), 2 failed
```

Restore the checked-in source (this discards your edits to this example):

```sh
git checkout -- examples/countdown.rkg
```

## Read the golden difference

The invalid measure is rejected before execution, so it creates no new trace.
The changed output, by contrast, changes an emission: the golden comparison
names the first differing record, expected and actual values, and its cause.
The unchanged scenario expectation also rejects the changed text. Restore the
output rather than saving over the reference.

<details id="golden" class="raw-trace">
<summary>Inspect the raw sealed trace (.rkc)</summary>

The versioned reference stores one hex-encoded causal record per line, with an optional final seal line. Use the chapter's comparison commands to explain a divergence.

Source: `examples/traces/countdown/proof.positive.rkc`

```rastok-trace
a6070108a40150f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c804010582024382020309636c61620a000b000d81826c2e696e3e636f756e742e696e8249828165636f756e748000
a7070211601282438280800013676c3a737461727414801780181a50f3df1105112054d397a04ed0666ca4c8
a807021165636f756e74128249828165636f756e74800013676c3a737461727414801780181a50f3df1105112054d397a04ed0666ca4c81822824382808000
a807021165636f756e74128249828165636f756e74800013666c3a6c6f6f7014801780181a50f3df1105112054d397a04ed0666ca4c81828a105818701502fcfad2f040f633a2eaa6ac52c55d15c8249828165636f756e74800082025009426b9ccc12398e2552b10d29a250f762696e04a40150f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c8040105820243820203
a807021165636f756e74128249828165636f756e74800013666c3a6c6f6f7014801780181a50f3df1105112054d397a04ed0666ca4c81828a105818503502fcfad2f040f633a2eaa6ac52c55d15c005009426b9ccc12398e2552b10d29a250f7a40150f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c8040105820243820203
a707021165636f756e74128249828165636f756e7480001364703a696e1481836c2e696e3e636f756e742e696e8243828080000117801828a20181502fcfad2f040f633a2eaa6ac52c55d15c03818450bce978a7369910794ccef6fd5aaee307502fcfad2f040f633a2eaa6ac52c55d15c008201a501501acf1fd1704b6331132d400037b54c3a0250f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c8040105820243820202
a807021165636f756e74128249828165636f756e74800013666c3a6c6f6f7014801780181a501acf1fd1704b6331132d400037b54c3a1828a2048150bce978a7369910794ccef6fd5aaee30705818702502fcfad2f040f633a2eaa6ac52c55d15c00010350bce978a7369910794ccef6fd5aaee307a501501acf1fd1704b6331132d400037b54c3a0250f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c8040105820243820202
a907021165636f756e74128249828165636f756e74800013666c3a6c6f6f7014801780181981835020946af912fea1c531b9a8c156c5c0067829406c6f6f703a32666366616432663034306636333361326561613661633532633535643135633a696e8249828165636f756e748000181a5020946af912fea1c531b9a8c156c5c0061828a105818503502fcfad2f040f633a2eaa6ac52c55d15c015020946af912fea1c531b9a8c156c5c006a5015020946af912fea1c531b9a8c156c5c00602501acf1fd1704b6331132d400037b54c3a0350f3df1105112054d397a04ed0666ca4c8040105820243820202
a707021165636f756e74128249828165636f756e7480001364703a696e1481837829406c6f6f703a32666366616432663034306636333361326561613661633532633535643135633a696e8249828165636f756e7480000117801828a20181502fcfad2f040f633a2eaa6ac52c55d15c03818450dc759cce07b7cd08e3c541091909eac2502fcfad2f040f633a2eaa6ac52c55d15c018201a50150af90e571f28b0c57cc303bb4639eb522025020946af912fea1c531b9a8c156c5c0060350f3df1105112054d397a04ed0666ca4c8040105820243820201
a807021165636f756e74128249828165636f756e74800013666c3a6c6f6f7014801780181a50af90e571f28b0c57cc303bb4639eb5221828a2048150dc759cce07b7cd08e3c541091909eac205818702502fcfad2f040f633a2eaa6ac52c55d15c01020250dc759cce07b7cd08e3c541091909eac2a50150af90e571f28b0c57cc303bb4639eb522025020946af912fea1c531b9a8c156c5c0060350f3df1105112054d397a04ed0666ca4c8040105820243820201
a907021165636f756e74128249828165636f756e74800013666c3a6c6f6f7014801780181981835001149eb903f2a42a411bd2a5c99fda297829406c6f6f703a32666366616432663034306636333361326561613661633532633535643135633a696e8249828165636f756e748000181a5001149eb903f2a42a411bd2a5c99fda291828a105818503502fcfad2f040f633a2eaa6ac52c55d15c025001149eb903f2a42a411bd2a5c99fda29a5015001149eb903f2a42a411bd2a5c99fda290250af90e571f28b0c57cc303bb4639eb5220350f3df1105112054d397a04ed0666ca4c8040105820243820201
a707021165636f756e74128249828165636f756e7480001364703a696e1481837829406c6f6f703a32666366616432663034306636333361326561613661633532633535643135633a696e8249828165636f756e7480000217801828a20181502fcfad2f040f633a2eaa6ac52c55d15c0381845070adccffc97318389abff806c642078f502fcfad2f040f633a2eaa6ac52c55d15c028201a501506f63ca530a3ff07e0a2e0b98b1c07fdb025001149eb903f2a42a411bd2a5c99fda290350f3df1105112054d397a04ed0666ca4c8040105820243820200
a807021165636f756e74128249828165636f756e74800013666c3a6c6f6f7014801780181a506f63ca530a3ff07e0a2e0b98b1c07fdb1828a204815070adccffc97318389abff806c642078f05818702502fcfad2f040f633a2eaa6ac52c55d15c0203015070adccffc97318389abff806c642078fa501506f63ca530a3ff07e0a2e0b98b1c07fdb025001149eb903f2a42a411bd2a5c99fda290350f3df1105112054d397a04ed0666ca4c8040105820243820200
a907021165636f756e74128249828165636f756e74800013666c3a6c6f6f70148017801819818350a08ecae7e45001287125498fcbb858fa7829406c6f6f703a32666366616432663034306636333361326561613661633532633535643135633a696e8249828165636f756e748000181a50a08ecae7e45001287125498fcbb858fa1828a105818503502fcfad2f040f633a2eaa6ac52c55d15c0350a08ecae7e45001287125498fcbb858faa50150a08ecae7e45001287125498fcbb858fa02506f63ca530a3ff07e0a2e0b98b1c07fdb0350f3df1105112054d397a04ed0666ca4c8040105820243820200
a707021165636f756e74128249828165636f756e7480001364703a696e1481837829406c6f6f703a32666366616432663034306636333361326561613661633532633535643135633a696e8249828165636f756e74800003178183636f757400a501507b1488260cb97cfd8331c5237d727d300250a08ecae7e45001287125498fcbb858fa0350f3df1105112054d397a04ed0666ca4c804010582024a8206676c6966746f66661828a20181502fcfad2f040f633a2eaa6ac52c55d15c0381845050b117f43155e440bad41482136d4336502fcfad2f040f633a2eaa6ac52c55d15c03820382507b1488260cb97cfd8331c5237d727d3001
a807021165636f756e74128249828165636f756e74800013666c3a6c6f6f7014801780181a507b1488260cb97cfd8331c5237d727d301828a204815050b117f43155e440bad41482136d433605818404502fcfad2f040f633a2eaa6ac52c55d15c0301
seal 104ac3b13f441a7ad2128e83fbff1abc3e3f4aeb9f9a4505d38e4a9dc2ecfbb1
```

</details>

## Carry forward

Previous: [Agent gateway](/examples/06-agent-gateway.md). Separate a resource's lifetime
from a finite algorithm's progress. A Loop advances authored state; a Retry
repeats a prepared operation. Next: [HTTP model adapter](/examples/08-model-adapter.md),
which adds typed usage evidence to an external response.

## Limits

This is a pure integer countdown, with no external program or elapsed-time
claim. The direct proof fragment is deliberately small; unsupported proofs
require a finite fuel bound and an explicit exhaustion disposition.
The journal remains in memory; a golden trace is comparison evidence, not
durable recovery.
