soma.cell

Recall quarantine — the recalled lot that ships anyway

The 2008-09 Peanut Corporation of America scandal: lots that TESTED POSITIVE for salmonella were re-tested until a lab said yes, then shipped. Nine people died, 714 fell ill, and the CEO went to prison for 28 years. The enabling failure was procedural: "on hold" was a status field someone could flip back. Here a lot's lifecycle is a verified machine. A recall moves the lot to `quarantined`, and from quarantine the ONLY exits are `destroyed` or `returned_to_supplier` — both TERMINAL. There is no edge back to `sellable`, so no re-test, no override, and no commercial pressure can put a recalled lot back on a truck. soma verify proves the absence.

A Soma program from the logistics part of the verified corpus: 110 lines, cells RecallControl, PcaCannotHappen. It passes soma check with soma 3.2.4.

How it is built

The cell RecallControl declares 7 signals in its face: receive_lot(lot: String, units: Int) -> Map, release_lot(lot: String) -> Map, recall_lot(lot: String) -> Map, destroy_lot(lot: String) -> Map, return_lot(lot: String) -> Map, ship_from_lot(lot: String, units: Int) -> Map and lot_status(lot: String) -> String; keeps 1 memory slot: units_left (Map<String, Int>, persistent and consistent) under the invariant units_left >= 0. Its state machine lot_flow starts in received and allows 5 transitions: received -> sellable, received -> quarantined, sellable -> quarantined, quarantined -> destroyed and quarantined -> returned_to_supplier. destroyed and returned_to_supplier are terminal. Handlers: receive_lot (line 44), release_lot (line 50), recall_lot (line 51), destroy_lot (line 52), return_lot (line 57), move_lot (line 59), ship_from_lot (line 65) and lot_status (line 77).

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

Generated from soma describe on this file.

The source

recall_quarantine.cell

// ============================================================================
// RECALL QUARANTINE — the recalled lot that ships anyway
// ============================================================================
//
// The 2008-09 Peanut Corporation of America scandal: lots that TESTED
// POSITIVE for salmonella were re-tested until a lab said yes, then
// shipped. Nine people died, 714 fell ill, and the CEO went to prison
// for 28 years. The enabling failure was procedural: "on hold" was a
// status field someone could flip back.
//
// Here a lot's lifecycle is a verified machine. A recall moves the lot
// to `quarantined`, and from quarantine the ONLY exits are `destroyed`
// or `returned_to_supplier` — both TERMINAL. There is no edge back to
// `sellable`, so no re-test, no override, and no commercial pressure can
// put a recalled lot back on a truck. soma verify proves the absence.
// ============================================================================

cell RecallControl {
    face {
        signal receive_lot(lot: String, units: Int) -> Map
        signal release_lot(lot: String) -> Map
        signal recall_lot(lot: String) -> Map
        signal destroy_lot(lot: String) -> Map
        signal return_lot(lot: String) -> Map
        signal ship_from_lot(lot: String, units: Int) -> Map
        signal lot_status(lot: String) -> String
    }

    memory {
        units_left: Map<String, Int> [persistent, consistent]
        invariant units_left >= 0
    }

    // quarantined exits ONLY downward; destroyed/returned are terminal.
    state lot_flow {
        initial: received
        received    -> sellable
        received    -> quarantined
        sellable    -> quarantined
        quarantined -> destroyed
        quarantined -> returned_to_supplier
    }

    on receive_lot(lot: String, units: Int) {
        if units <= 0 { return map("ok", false, "reason", "units must be positive") }
        units_left.set(lot, units)
        return map("ok", true, "lot", lot, "units", units)
    }

    on release_lot(lot: String) { return move_lot(lot, "sellable") }
    on recall_lot(lot: String) { return move_lot(lot, "quarantined") }
    on destroy_lot(lot: String) {
        let r = move_lot(lot, "destroyed")
        if r.ok { units_left.set(lot, 0) }
        return r
    }
    on return_lot(lot: String) { return move_lot(lot, "returned_to_supplier") }

    on move_lot(lot: String, to: String) {
        let r = try { transition(lot, to) }
        if r.error != () { return map("ok", false, "reason", "{r.error}") }
        return map("ok", true, "lot", lot, "state", to)
    }

    on ship_from_lot(lot: String, units: Int) {
        // the ONLY state that ships is sellable — quarantine is a wall
        if get_status(lot) != "sellable" {
            let s = get_status(lot)
            return map("ok", false, "reason", "lot {lot} is {s}, not sellable")
        }
        if units <= 0 { return map("ok", false, "reason", "units must be positive") }
        let w = try { units_left.set(lot, (units_left.get(lot) ?? 0) - units) }
        if w.error != () { return map("ok", false, "reason", "insufficient units in lot") }
        return map("ok", true, "shipped", units, "left", units_left.get(lot))
    }

    on lot_status(lot: String) { return get_status(lot) }
}

cell test PcaCannotHappen {
    rules {
        // a lot is received, released, and ships normally
        assert receive_lot("LOT-A", 500).ok == true
        assert release_lot("LOT-A").ok == true
        assert ship_from_lot("LOT-A", 100).ok == true

        // salmonella positive: RECALL
        assert recall_lot("LOT-A").ok == true
        assert lot_status("LOT-A") == "quarantined"

        // THE PCA MOVE: re-test until friendly, flip back to sellable,
        // ship. The edge quarantined->sellable does not exist.
        assert_fails transition("LOT-A", "sellable")
        assert ship_from_lot("LOT-A", 50).ok == false
        assert lot_status("LOT-A") == "quarantined"

        // destruction is terminal — a destroyed lot never resurfaces
        assert destroy_lot("LOT-A").ok == true
        assert_fails transition("LOT-A", "sellable")
        assert_fails transition("LOT-A", "quarantined")
        assert ship_from_lot("LOT-A", 1).ok == false

        // over-shipping a healthy lot is refused by the units ledger
        assert receive_lot("LOT-B", 10).ok == true
        assert release_lot("LOT-B").ok == true
        assert ship_from_lot("LOT-B", 11).ok == false
        assert_fails units_left.set("LOT-B", -1)
    }
}

What the toolchain says

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

$ soma check recall_quarantine.cell
✓ All checks passed.
$ soma test recall_quarantine.cell
test PcaCannotHappen ...
  ✓ assert receive_lot("LOT-A", 500).ok == true
  ✓ assert release_lot("LOT-A").ok == true
  ✓ assert ship_from_lot("LOT-A", 100).ok == true
  ✓ assert recall_lot("LOT-A").ok == true
  ✓ assert lot_status("LOT-A") == "quarantined"
  ✓ assert_fails transition("LOT-A", "sellable") — raised invalid transition: quarantined → sellable. Current state: 'quarantined'. Valid targets: [destroyed, returned_to_supplier]
  ✓ assert ship_from_lot("LOT-A", 50).ok == false
  ✓ assert lot_status("LOT-A") == "quarantined"
  ✓ assert destroy_lot("LOT-A").ok == true
  ✓ assert_fails transition("LOT-A", "sellable") — raised invalid transition: destroyed → sellable. Current state: 'destroyed'. Valid targets: []
  ✓ assert_fails transition("LOT-A", "quarantined") — raised invalid transition: destroyed → quarantined. Current state: 'destroyed'. Valid targets: []
  ✓ assert ship_from_lot("LOT-A", 1).ok == false
  ✓ assert receive_lot("LOT-B", 10).ok == true
  ✓ assert release_lot("LOT-B").ok == true
  ✓ assert ship_from_lot("LOT-B", 11).ok == false
  ✓ assert_fails units_left.set("LOT-B", -1) — raised memory invariant violated on 'units_left': units_left >= 0 — rejected write of -1 (key "LOT-B"); the slot is unchanged

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

State machine 'lot_flow': 5 states, initial 'received'
  States: [destroyed, quarantined, received, returned_to_supplier, sellable]

  ✓ 5 states, 5 transitions
  ✓ all states reachable from 'received'
  ✓ terminal states: [destroyed, returned_to_supplier]
  ✓ no deadlocks
  ✓ liveness: every state can eventually reach a terminal state
  ⚠ dynamic transition targets: handler `move_lot` 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 8 handlers structurally terminate
  ⚠ refinement (partial — dynamic targets fall back to runtime check): handler `move_lot` 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 `move_lot` ⟶ <dynamic>

RecallControl/invariants:

  ✓ invariant units_left >= 0 — writer 'destroy_lot' proven (writes 0)
  ✓ invariant units_left >= 0 — writer 'receive_lot' proven by induction (writes units)
  ⚠ invariant units_left >= 0 — runtime-checked (computed values): ship_from_lot → units_left because `units_left` has no known range

9 passed, 3 warnings, 0 failures

  Temporal properties for 'lot_flow':
  ✓ 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/recall_quarantine.cell · examples/corpus/logistics/recall_quarantine.cell