soma.cell

Package tracking — delivered is FOREVER

Couriers under per-stop quotas have been caught marking parcels "delivered" early, then quietly reverting the scan and dropping the box a day later — or never. Amazon, FedEx and UPS all field thousands of "marked delivered, never arrived" claims a year, and every fraudulent rewrite depends on one thing: the tracking record being MUTABLE. Here the journey is a verified state machine and `delivered` is a TERMINAL state: no outgoing transitions exist, so no handler — and no "corrected" rescan — can ever pull a delivered parcel back into transit. A failed doorstep attempt legally loops back to the depot; history rewriting does not. `soma verify` proves it.

A Soma program from the logistics part of the verified corpus: 84 lines, cells PackageTracker, DeliveredIsForever. It passes soma check with soma 3.2.4.

How it is built

The cell PackageTracker declares 3 signals in its face: scan_event(pkg: String, to: String) -> Map, locate(pkg: String) -> String and attempt_count(pkg: String) -> Int; keeps 1 memory slot: attempts (Map<String, Int>, persistent and consistent) under the invariant attempts >= 0 && attempts <= 3. Its state machine journey starts in label_created and allows 6 transitions: label_created -> picked_up, picked_up -> in_transit, in_transit -> out_for_delivery, out_for_delivery -> delivered, out_for_delivery -> in_transit and in_transit -> returned_to_sender. delivered and returned_to_sender are terminal. Handlers: scan_event (line 42), locate (line 55) and attempt_count (line 57).

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

Generated from soma describe on this file.

The source

package_tracking.cell

// ============================================================================
// PACKAGE TRACKING — delivered is FOREVER
// ============================================================================
//
// Couriers under per-stop quotas have been caught marking parcels
// "delivered" early, then quietly reverting the scan and dropping the box
// a day later — or never. Amazon, FedEx and UPS all field thousands of
// "marked delivered, never arrived" claims a year, and every fraudulent
// rewrite depends on one thing: the tracking record being MUTABLE.
//
// Here the journey is a verified state machine and `delivered` is a
// TERMINAL state: no outgoing transitions exist, so no handler — and no
// "corrected" rescan — can ever pull a delivered parcel back into
// transit. A failed doorstep attempt legally loops back to the depot;
// history rewriting does not. `soma verify` proves it.
// ============================================================================

cell PackageTracker {
    face {
        signal scan_event(pkg: String, to: String) -> Map
        signal locate(pkg: String) -> String
        signal attempt_count(pkg: String) -> Int
    }

    memory {
        // doorstep attempts per parcel — carriers cap redelivery at 3
        attempts: Map<String, Int> [persistent, consistent]
        invariant attempts >= 0 && attempts <= 3
    }

    // delivered and returned_to_sender have NO exits — terminal, proven.
    state journey {
        initial: label_created
        label_created    -> picked_up
        picked_up        -> in_transit
        in_transit       -> out_for_delivery
        out_for_delivery -> delivered
        out_for_delivery -> in_transit       // failed attempt: back to depot
        in_transit       -> returned_to_sender
    }

    on scan_event(pkg: String, to: String) {
        if to == "out_for_delivery" {
            let n = attempts.get(pkg) ?? 0
            let w = try { attempts.set(pkg, n + 1) }
            if w.error != () {
                return map("ok", false, "reason", "max delivery attempts reached")
            }
        }
        let r = try { transition(pkg, to) }
        if r.error != () { return map("ok", false, "reason", "{r.error}") }
        return map("ok", true, "pkg", pkg, "state", get_status(pkg))
    }

    on locate(pkg: String) { return get_status(pkg) }

    on attempt_count(pkg: String) { return attempts.get(pkg) ?? 0 }
}

cell test DeliveredIsForever {
    rules {
        // normal journey
        assert scan_event("p1", "picked_up").ok == true
        assert scan_event("p1", "in_transit").ok == true
        assert scan_event("p1", "out_for_delivery").ok == true
        assert scan_event("p1", "delivered").ok == true
        assert locate("p1") == "delivered"

        // THE FRAUD: revert a delivered parcel back into transit. The
        // transition does not exist — it raises.
        assert_fails transition("p1", "in_transit")
        assert_fails transition("p1", "out_for_delivery")
        assert locate("p1") == "delivered"

        // a failed attempt loops legally; a 4th attempt is refused by memory
        assert scan_event("p2", "picked_up").ok == true
        assert scan_event("p2", "in_transit").ok == true
        assert scan_event("p2", "out_for_delivery").ok == true
        assert scan_event("p2", "in_transit").ok == true
        assert attempt_count("p2") == 1
        assert_fails attempts.set("p2", 9)
    }
}

What the toolchain says

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

$ soma check package_tracking.cell
✓ All checks passed.
$ soma test package_tracking.cell
test DeliveredIsForever ...
  ✓ assert scan_event("p1", "picked_up").ok == true
  ✓ assert scan_event("p1", "in_transit").ok == true
  ✓ assert scan_event("p1", "out_for_delivery").ok == true
  ✓ assert scan_event("p1", "delivered").ok == true
  ✓ assert locate("p1") == "delivered"
  ✓ assert_fails transition("p1", "in_transit") — raised invalid transition: delivered → in_transit. Current state: 'delivered'. Valid targets: []
  ✓ assert_fails transition("p1", "out_for_delivery") — raised invalid transition: delivered → out_for_delivery. Current state: 'delivered'. Valid targets: []
  ✓ assert locate("p1") == "delivered"
  ✓ assert scan_event("p2", "picked_up").ok == true
  ✓ assert scan_event("p2", "in_transit").ok == true
  ✓ assert scan_event("p2", "out_for_delivery").ok == true
  ✓ assert scan_event("p2", "in_transit").ok == true
  ✓ assert attempt_count("p2") == 1
  ✓ assert_fails attempts.set("p2", 9) — raised memory invariant violated on 'attempts': attempts >= 0 && attempts <= 3 — rejected write of 9 (key "p2"); the slot is unchanged

14 tests: 14 passed, 0 failed
$ soma verify package_tracking.cell
Verifying package_tracking.cell...

State machine 'journey': 6 states, initial 'label_created'
  States: [delivered, in_transit, label_created, out_for_delivery, picked_up, returned_to_sender]

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

PackageTracker/invariants:

  ✓ invariant attempts >= 0 — writer 'scan_event' proven by induction (writes n + 1)
  ⚠ invariant attempts >= 0 && attempts <= 3 — runtime-checked (computed values): scan_event → attempts [attempts <= 3] because `n + 1` is only known to lie in [1, 4] — narrow it: `require (n + 1) <= 3 else …`

8 passed, 3 warnings, 0 failures

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