soma.cell

Vaccine cold-chain — a temperature breach is PERMANENT, not "probably fine"

In 2018 a UK pharmacy revaccinated hundreds of patients after a fridge failure: the mRNA/protein vaccines had spent hours out of the 2–8 °C window and nobody could prove they were still potent. The temptation in software is always the same — log the excursion, let a human "assess" it, quietly keep using the vials. That is how spoiled doses reach arms. Here a vial's life is a state machine whose `discarded` state is TERMINAL: once a reading leaves [2,8] °C the vial transitions to discarded and there is NO path back to administrable. `soma verify` proves discarded is a sink — no sequence of "re-inspect" or "override" signals can resurrect it. A second wall: the recorded temperature slot only accepts plausible readings, so a stuck sensor reporting 999 °C can't be silently stored as in-range.

A Soma program from the medical part of the verified corpus: 126 lines, cells ColdChain, ColdChainTests. It passes soma check with soma 3.2.4.

How it is built

The cell ColdChain declares 5 signals in its face: receive(vial: String) -> Map, reading(vial: String, celsius: Int) -> Map, administer(vial: String) -> Map, vial_state(vial: String) -> String and demo() -> String; keeps 1 memory slot: last_temp (Map<String, Int>, persistent and consistent) under the invariant last_temp >= -40 && last_temp <= 60. Its state machine vial starts in in_transit and allows 5 transitions: in_transit -> stored, stored -> stored, stored -> administered, stored -> discarded and in_transit -> discarded. administered and discarded are terminal. Handlers: receive (line 52), reading (line 59), administer (line 78), vial_state (line 87) and demo (line 91).

The file also holds 1 test cell (ColdChainTests) with 12 assertions, run by soma test.

Generated from soma describe on this file.

The source

coldchain.cell

// ============================================================================
// VACCINE COLD-CHAIN — a temperature breach is PERMANENT, not "probably fine"
// ============================================================================
//
// In 2018 a UK pharmacy revaccinated hundreds of patients after a fridge
// failure: the mRNA/protein vaccines had spent hours out of the 2–8 °C
// window and nobody could prove they were still potent. The temptation in
// software is always the same — log the excursion, let a human "assess"
// it, quietly keep using the vials. That is how spoiled doses reach arms.
//
// Here a vial's life is a state machine whose `discarded` state is
// TERMINAL: once a reading leaves [2,8] °C the vial transitions to
// discarded and there is NO path back to administrable. `soma verify`
// proves discarded is a sink — no sequence of "re-inspect" or "override"
// signals can resurrect it. A second wall: the recorded temperature slot
// only accepts plausible readings, so a stuck sensor reporting 999 °C
// can't be silently stored as in-range.
//
//   soma verify examples/corpus/medical/coldchain.cell   # discarded is terminal
//   soma test   examples/corpus/medical/coldchain.cell
//   soma run    examples/corpus/medical/coldchain.cell demo
// ============================================================================

cell ColdChain {
    face {
        signal receive(vial: String) -> Map
        signal reading(vial: String, celsius: Int) -> Map
        signal administer(vial: String) -> Map
        signal vial_state(vial: String) -> String
        signal demo() -> String
    }

    memory {
        // Last recorded temperature. A medical fridge sensor reports
        // roughly -40..60; anything outside that is a sensor fault, not a
        // reading to be trusted. The invariant refuses to store garbage.
        last_temp: Map<String, Int> [persistent, consistent]
        invariant last_temp >= -40 && last_temp <= 60
    }

    // `discarded` is TERMINAL. A breached vial is gone — verify proves no
    // edge leaves it. The vaccine cannot re-enter the administrable set.
    state vial {
        initial: in_transit
        in_transit -> stored          // arrived, logged into the fridge
        stored -> stored              // a fresh in-range reading
        stored -> administered        // given to a patient (also terminal)
        stored -> discarded           // a breach: 2–8 °C window left
        in_transit -> discarded       // breach detected on arrival
    }

    on receive(vial: String) {
        let r = try { transition(vial, "stored") }
        if r.error != () { return map("ok", false, "reason", "{r.error}") }
        last_temp.set(vial, 5)
        return map("ok", true, "state", get_status(vial))
    }

    on reading(vial: String, celsius: Int) {
        let st = get_status(vial)
        if st == "discarded" || st == "administered" {
            return map("ok", false, "reason", "vial is {st} — readings ignored")
        }
        // Record the reading first; a sensor fault is rejected by the
        // invariant and the slot keeps its last trusted value.
        let w = try { last_temp.set(vial, celsius) }
        if w.error != () {
            return map("ok", false, "reason", "implausible reading: {w.error}")
        }
        // Out of the 2–8 °C window => irreversible discard.
        if celsius < 2 || celsius > 8 {
            transition(vial, "discarded")
            return map("ok", true, "breach", true, "state", get_status(vial))
        }
        return map("ok", true, "breach", false, "state", get_status(vial))
    }

    on administer(vial: String) {
        let st = get_status(vial)
        if st != "stored" {
            return map("ok", false, "reason", "cannot administer a {st} vial")
        }
        transition(vial, "administered")
        return map("ok", true, "state", get_status(vial))
    }

    on vial_state(vial: String) {
        return get_status(vial)
    }

    on demo() {
        receive("v-1")
        let good = reading("v-1", 6)
        let bad = reading("v-1", 14)            // breach: power cut overnight
        let resurrect = try { transition("v-1", "stored") }   // refused
        let give = administer("v-1")            // refused — it's discarded
        print("good reading: {good}")
        print("breach:       {bad}")
        print("resurrect:    error = {resurrect.error}")
        print("administer:   {give}")
        return "demo done"
    }
}

cell test ColdChainTests {
    rules {
        // a vial that stays in-range can be administered
        assert receive("a").ok == true
        assert reading("a", 4).breach == false
        assert administer("a").ok == true
        assert vial_state("a") == "administered"

        // a breach is terminal: no re-storing, no administering
        assert receive("b").ok == true
        assert reading("b", 11).breach == true
        assert vial_state("b") == "discarded"
        assert_fails transition("b", "stored")
        assert_fails transition("b", "administered")
        assert administer("b").ok == false

        // a stuck sensor reporting 999 °C is rejected at the write
        assert receive("c").ok == true
        assert_fails last_temp.set("c", 999)
    }
}

What the toolchain says

Run on a fresh copy of the program at build time; the outputs are pasted unedited.

$ soma check coldchain.cell
✓ All checks passed.
$ soma test coldchain.cell
test ColdChainTests ...
  ✓ assert receive("a").ok == true
  ✓ assert reading("a", 4).breach == false
  ✓ assert administer("a").ok == true
  ✓ assert vial_state("a") == "administered"
  ✓ assert receive("b").ok == true
  ✓ assert reading("b", 11).breach == true
  ✓ assert vial_state("b") == "discarded"
  ✓ assert_fails transition("b", "stored") — raised invalid transition: discarded → stored. Current state: 'discarded'. Valid targets: []
  ✓ assert_fails transition("b", "administered") — raised invalid transition: discarded → administered. Current state: 'discarded'. Valid targets: []
  ✓ assert administer("b").ok == false
  ✓ assert receive("c").ok == true
  ✓ assert_fails last_temp.set("c", 999) — raised memory invariant violated on 'last_temp': last_temp >= -40 && last_temp <= 60 — rejected write of 999 (key "c"); the slot is unchanged

12 tests: 12 passed, 0 failed
$ soma verify coldchain.cell
Verifying coldchain.cell...

State machine 'vial': 4 states, initial 'in_transit'
  States: [administered, discarded, in_transit, stored]

  ✓ 4 states, 5 transitions
  ✓ all states reachable from 'in_transit'
  ✓ terminal states: [administered, discarded]
  ✓ no deadlocks
  ✓ liveness: every state can eventually reach a terminal state
  ✓ think-isolated: CTL safety properties hold regardless of LLM output (4 handlers, 4 literal transitions, 0 dynamic)
  ✓ termination: all 5 handlers structurally terminate
  ✓ refinement: handler `receive` ⟶ {stored}
  ✓ refinement: handler `reading` ⟶ {discarded [if celsius < 2 || celsius > 8]}
  ✓ refinement: handler `administer` ⟶ {stored → administered}
  ✓ refinement: handler `demo` ⟶ {stored}
  · transition sources: `receive`, `reading`, `demo` call transition(id, target) without declaring the source — the runtime refuses a move the graph forbids, but the proof does not see where the handler starts; `transition(id, from, to)` names the edge (checked by `soma check`, refused at run time from any other state)

ColdChain/invariants:

  ✓ invariant last_temp >= -40 && last_temp <= 60 — writer 'receive' proven (writes 5)
  ⚠ invariant last_temp >= -40 && last_temp <= 60 — runtime-checked (computed values): reading → last_temp because `celsius` is a parameter (narrow it: `require celsius >= -40 && celsius <= 60 else …`)

12 passed, 1 warnings, 0 failures

  Temporal properties for 'vial':
  ✓ deadlock_free — no deadlocks in any reachable state
Temporal: 1 passed, 0 failed
VERIFY OK
$ soma run coldchain.cell demo
good reading: {"ok": true, "breach": false, "state": "stored"}
breach:       {"ok": true, "breach": true, "state": "discarded"}
resurrect:    error = invalid transition: discarded → stored. Current state: 'discarded'. Valid targets: []
administer:   {"ok": false, "reason": "cannot administer a discarded vial"}
demo done

Builtins used

get_status, map, print, transition

Source file: /corpus/medical/coldchain.cell · examples/corpus/medical/coldchain.cell