Metakit / What a proof of one step cannot check
Discharge an obligation with a walk
A walk runs the steps of a scenario in many orders and checks a property after every step. You write the steps and say which must come before which. A seed fixes the walk's random choices so you can rerun it. A generated pair obligation needs passing samples in both relative orders.
This scenario is a page that shows a balance. It sends two deposits, and each reply carries the balance the server held after that deposit. The page must not go back to an older balance.
import { Option } from 'effect'
interface Page {
readonly shown: number
readonly latest: number
}
const reply =
(request: number, balance: number) =>
(page: Page): Page =>
request < page.latest ? page : { shown: balance, latest: request }
const sendFirst: Temporal.Step<Page, string> = { name: 'send first', run: page => page }
const sendSecond: Temporal.Step<Page, string> = {
name: 'send second',
after: [sendFirst],
run: page => page,
}
const twoDeposits: Temporal.Scenario<Page, string, 'Deposit'> = {
initial: () => ({ shown: 0, latest: 0 }),
steps: [
sendFirst,
sendSecond,
{ name: 'reply first', of: 'Deposit', after: [sendFirst], run: reply(1, 10) },
{ name: 'reply second', of: 'Deposit', after: [sendSecond], run: reply(2, 30) },
],
holds: (page, ran) =>
ran.includes('reply second') && page.shown !== 30
? Option.some(`shows ${page.shown} after the second reply`)
: Option.none(),
}
const deposits = report.obligations.find(obligation => obligation.name === 'Deposit/Deposit')
const discharges = deposits === undefined ? [] : [Temporal.discharge(deposits, twoDeposits)]
// Held over 202 runs, with both orders of reply first and reply second observed.after lists the steps, as values, that must run first; a step that is not in the scenario is refused when it is walked. holds receives the names of the steps run so far and returns a reason if the property fails. of matches the replies to the transitions in the obligation. Temporal.discharge picks the first distinct matching occurrences in the scenario and tries each feasible direction before its seeded walk.
Held means every tried order passed. coverage names the matched steps and marks each direction as observed, causally-forbidden, or unsampled. A generated pair stays open when an after dependency prevents one direction.
Remove the request < page.latest check from reply and the same walk answers:
{ _tag: 'Broken', seed: 1,
interleaving: ['send first', 'send second', 'reply second', 'reply first'],
minimal: ['send first', 'send second', 'reply second', 'reply first'],
reason: 'shows 10 after the second reply' }Repeat Temporal.discharge with the same scenario and options to replay the failure. For a failure returned directly by Temporal.walk, pass its seed with runs: 1. interleaving lists the steps through the failure, and minimal keeps the steps the failure depends on.
Fail the build while an obligation is open
Temporal.undischarged lists the obligations that still need evidence. A generated pair needs a passing sample of both relative orders. Assert that the list is empty.
const open = Temporal.undischarged(report.obligations, discharges).map(({ name }) => name)
// ['Deposit/Withdraw', 'Withdraw/Withdraw']: two walks still to writeIn a test, expect(open).toEqual([]) fails while any obligation is open. To close Deposit/Withdraw, mark one reply step of: 'Deposit' and the other of: 'Withdraw'. Let the replies arrive in either order and use holds to check what the page displays. Any failing walk keeps the obligation open.
What this does not do
- A walk tries the orders its seeds give (200 unless you pass
runs), not every order. It finds races; it does not prove their absence. Store.mounttrusts the facts you give it, and where you say a rule is judged. It does not inspect the database or your writer.- The steps of a scenario are your model of the page and the server. A walk says nothing about code the steps do not call.
Next
- Verify a state machine in CI: the proofs these obligations sit beside.
- Model domain types and row aggregates:
Rows.sumand the other aggregates.