soma.cell

Cold-chain custody — a warm vaccine never gets cold again

In 2022 the WHO estimated half of all vaccines are wasted globally, much of it to cold-chain failure — and the dangerous half of that is the failure NOBODY LOGS: a pallet of mRNA doses sits on a hot tarmac during a handoff, the temperature recorder is reset, and the shipment continues as if nothing happened. The doses are inert; the syringes still go in. Two structural fixes. Every custody handoff is a recorded transition — no anonymous gaps. And `breached` is a TERMINAL state: the moment any custodian logs an excursion, there is no transition that puts the pallet back on the cold chain. You cannot "re-chill" the record. soma verify proves both: every path is named, and breached has no exit.

A Soma program from the logistics part of the verified corpus: 105 lines, cells ColdChain, NoRechilledRecords. It passes soma check with soma 3.2.4.

How it is built

The cell ColdChain declares 4 signals in its face: handoff(pallet: String, to: String) -> Map, log_excursion(pallet: String, celsius: Int) -> Map, custody_of(pallet: String) -> String and handoff_total(pallet: String) -> Int; keeps 2 memory slots: handoffs (Map<String, Int>, persistent and consistent) under the invariant handoffs >= 0 and excursion_c (Map<String, Int>, persistent and consistent). Its state machine custody starts in at_origin_depot and allows 8 transitions: at_origin_depot -> with_carrier, with_carrier -> at_transit_hub, at_transit_hub -> with_courier, with_courier -> delivered_cold, at_origin_depot -> breached, with_carrier -> breached, at_transit_hub -> breached and with_courier -> breached. breached and delivered_cold are terminal. Handlers: handoff (line 48), log_excursion (line 59), custody_of (line 70) and handoff_total (line 72).

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

Generated from soma describe on this file.

The source

cold_chain_custody.cell

// ============================================================================
// COLD-CHAIN CUSTODY — a warm vaccine never gets cold again
// ============================================================================
//
// In 2022 the WHO estimated half of all vaccines are wasted globally, much
// of it to cold-chain failure — and the dangerous half of that is the
// failure NOBODY LOGS: a pallet of mRNA doses sits on a hot tarmac during
// a handoff, the temperature recorder is reset, and the shipment continues
// as if nothing happened. The doses are inert; the syringes still go in.
//
// Two structural fixes. Every custody handoff is a recorded transition —
// no anonymous gaps. And `breached` is a TERMINAL state: the moment any
// custodian logs an excursion, there is no transition that puts the pallet
// back on the cold chain. You cannot "re-chill" the record. soma verify
// proves both: every path is named, and breached has no exit.
// ============================================================================

cell ColdChain {
    face {
        signal handoff(pallet: String, to: String) -> Map
        signal log_excursion(pallet: String, celsius: Int) -> Map
        signal custody_of(pallet: String) -> String
        signal handoff_total(pallet: String) -> Int
    }

    memory {
        // recorded handoffs per pallet — the custody ledger never shrinks
        handoffs: Map<String, Int> [persistent, consistent]
        invariant handoffs >= 0

        // last recorded excursion temperature, for the audit
        excursion_c: Map<String, Int> [persistent, consistent]
    }

    // breached has no outgoing edges. delivered_cold is the good terminal.
    state custody {
        initial: at_origin_depot
        at_origin_depot -> with_carrier
        with_carrier    -> at_transit_hub
        at_transit_hub  -> with_courier
        with_courier    -> delivered_cold
        at_origin_depot -> breached
        with_carrier    -> breached
        at_transit_hub  -> breached
        with_courier    -> breached
    }

    on handoff(pallet: String, to: String) {
        if to == "breached" {
            return map("ok", false, "reason", "use log_excursion to declare a breach")
        }
        let r = try { transition(pallet, to) }
        if r.error != () { return map("ok", false, "reason", "{r.error}") }
        let n = handoffs.get(pallet) ?? 0
        handoffs.set(pallet, n + 1)
        return map("ok", true, "custody", to, "handoffs_recorded", n + 1)
    }

    on log_excursion(pallet: String, celsius: Int) {
        // pharma cold chain is 2–8 C; outside that, the pallet is dead
        if celsius >= 2 && celsius <= 8 {
            return map("ok", true, "note", "within range, chain intact")
        }
        let r = try { transition(pallet, "breached") }
        if r.error != () { return map("ok", false, "reason", "{r.error}") }
        excursion_c.set(pallet, celsius)
        return map("ok", true, "custody", "breached", "recorded_c", celsius)
    }

    on custody_of(pallet: String) { return get_status(pallet) }

    on handoff_total(pallet: String) { return handoffs.get(pallet) ?? 0 }
}

cell test NoRechilledRecords {
    rules {
        // a clean run: every handoff lands in the ledger
        assert handoff("PAL-1", "with_carrier").ok == true
        assert handoff("PAL-1", "at_transit_hub").ok == true
        assert handoff("PAL-1", "with_courier").ok == true
        assert handoff("PAL-1", "delivered_cold").ok == true
        assert handoff_total("PAL-1") == 4

        // no skipping custodians: origin straight to courier doesn't exist
        assert handoff("PAL-2", "with_courier").ok == false
        assert_fails transition("PAL-2", "with_courier")

        // the tarmac incident: 19 C at the hub — chain dead
        assert handoff("PAL-2", "with_carrier").ok == true
        assert handoff("PAL-2", "at_transit_hub").ok == true
        assert log_excursion("PAL-2", 19).ok == true
        assert custody_of("PAL-2") == "breached"

        // THE COVER-UP: put the breached pallet back with a courier.
        // The transition does not exist; neither does re-delivery.
        assert_fails transition("PAL-2", "with_courier")
        assert_fails transition("PAL-2", "delivered_cold")
        assert custody_of("PAL-2") == "breached"

        // an in-range reading breaches nothing
        assert log_excursion("PAL-1x", 5).ok == true
        assert custody_of("PAL-1x") == "at_origin_depot"
    }
}

What the toolchain says

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

$ soma check cold_chain_custody.cell
✓ All checks passed.
$ soma test cold_chain_custody.cell
test NoRechilledRecords ...
  ✓ assert handoff("PAL-1", "with_carrier").ok == true
  ✓ assert handoff("PAL-1", "at_transit_hub").ok == true
  ✓ assert handoff("PAL-1", "with_courier").ok == true
  ✓ assert handoff("PAL-1", "delivered_cold").ok == true
  ✓ assert handoff_total("PAL-1") == 4
  ✓ assert handoff("PAL-2", "with_courier").ok == false
  ✓ assert_fails transition("PAL-2", "with_courier") — raised invalid transition: at_origin_depot → with_courier. Current state: 'at_origin_depot'. Valid targets: [with_carrier, breached]
  ✓ assert handoff("PAL-2", "with_carrier").ok == true
  ✓ assert handoff("PAL-2", "at_transit_hub").ok == true
  ✓ assert log_excursion("PAL-2", 19).ok == true
  ✓ assert custody_of("PAL-2") == "breached"
  ✓ assert_fails transition("PAL-2", "with_courier") — raised invalid transition: breached → with_courier. Current state: 'breached'. Valid targets: []
  ✓ assert_fails transition("PAL-2", "delivered_cold") — raised invalid transition: breached → delivered_cold. Current state: 'breached'. Valid targets: []
  ✓ assert custody_of("PAL-2") == "breached"
  ✓ assert log_excursion("PAL-1x", 5).ok == true
  ✓ assert custody_of("PAL-1x") == "at_origin_depot"

16 tests: 16 passed, 0 failed
$ soma verify cold_chain_custody.cell
Verifying cold_chain_custody.cell...

State machine 'custody': 6 states, initial 'at_origin_depot'
  States: [at_origin_depot, at_transit_hub, breached, delivered_cold, with_carrier, with_courier]

  ✓ 6 states, 8 transitions
  ✓ all states reachable from 'at_origin_depot'
  ✓ terminal states: [delivered_cold, breached]
  ✓ no deadlocks
  ✓ liveness: every state can eventually reach a terminal state
  ⚠ dynamic transition targets: handler `handoff` uses a dynamic (computed) transition target — the state these handlers move to is computed at run time (from input or an LLM answer), so the machine's safety is checked at run time for them, not proven
  ✓ termination: all 4 handlers structurally terminate
  ⚠ refinement (partial — dynamic targets fall back to runtime check): handler `handoff` calls transition() with a computed target — which edge it takes is checked at run time (invalid_transition), not proven; write the target as a literal state to have it verified
  ✓ refinement (partial — dynamic targets fall back to runtime check): handler `handoff` ⟶ <dynamic>
  ✓ refinement (partial — dynamic targets fall back to runtime check): handler `log_excursion` ⟶ {breached}
  · transition sources: `log_excursion` calls 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 handoffs >= 0 — writer 'handoff' proven by induction (writes n + 1)

9 passed, 2 warnings, 0 failures

  Temporal properties for 'custody':
  ✓ deadlock_free — no deadlocks in any reachable state
Temporal: 1 passed, 0 failed
VERIFY OK

Builtins used

get_status, map, transition

Source file: /corpus/logistics/cold_chain_custody.cell · examples/corpus/logistics/cold_chain_custody.cell