soma.cell

Feature-flag service: toggle named flags on/off, query state, list all

Booleans stored as "on"/"off" strings in a persistent map.

A Soma program from the web part of the verified corpus: 61 lines, cells Flags, FlagsTest. It passes soma check with soma 3.2.4.

How it is built

The cell Flags declares 4 signals in its face: request(method: String, path: String, body: String) -> Map, set_flag(name: String, enabled: Bool) -> Map, is_on(name: String) -> Bool and all_flags() -> Map; keeps 1 memory slot: flags (Map<String, String>, persistent and consistent). Handlers: set_flag (line 17), is_on (line 22), all_flags (line 26) and request (line 34).

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

Generated from soma describe on this file.

The source

soma.toml

[package]
name = "web-corpus"
version = "0.1.0"
entry = "todo_state.cell"

[verify]
cells = ["TodoService"]
deadlock_free = true
eventually = ["Done", "Cancelled"]

feature_flags.cell

// Feature-flag service: toggle named flags on/off, query state, list all.
// Booleans stored as "on"/"off" strings in a persistent map.

cell Flags {
    face {
        signal request(method: String, path: String, body: String) -> Map
        signal set_flag(name: String, enabled: Bool) -> Map
        signal is_on(name: String) -> Bool
        signal all_flags() -> Map
        promise all_persistent
    }

    memory {
        flags: Map<String, String> [persistent, consistent]
    }

    on set_flag(name: String, enabled: Bool) {
        flags.set(name, if enabled { "on" } else { "off" })
        return map("name", name, "enabled", enabled)
    }

    on is_on(name: String) {
        return flags.get(name) == "on"
    }

    on all_flags() {
        let out = map()
        for k in flags.keys {
            out = with(out, k, flags.get(k) == "on")
        }
        return out
    }

    on request(method: String, path: String, body: String) {
        let req = map("method", method, "path", path)
        match req {
            {method: "GET",  path: "/flags"}        -> all_flags()
            {method: "POST", path: "/on/" + n}      -> set_flag(n, true)
            {method: "POST", path: "/off/" + n}     -> set_flag(n, false)
            {method: "GET",  path: "/flags/" + n}   -> map("name", n, "enabled", is_on(n))
            _                                            -> response(404, map("error", "no route"))
        }
    }
}

cell test FlagsTest {
    rules {
        assert set_flag("dark_mode", true).enabled == true
        assert is_on("dark_mode") == true
        assert is_on("never_set") == false
        assert _summary() == "beta=true,dark=false"
    }

    on _summary() {
        set_flag("beta", true)
        set_flag("dark", false)
        return concat("beta=", concat(to_string(is_on("beta")),
               concat(",dark=", to_string(is_on("dark")))))
    }
}

What the toolchain says

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

$ soma check feature_flags.cell
✓ All checks passed.
$ soma test feature_flags.cell
test FlagsTest ...
  ✓ assert set_flag("dark_mode", true).enabled == true
  ✓ assert is_on("dark_mode") == true
  ✓ assert is_on("never_set") == false
  ✓ assert _summary() == "beta=true,dark=false"

4 tests: 4 passed, 0 failed
$ soma verify feature_flags.cell
Verifying feature_flags.cell...
No state machine in this program: nothing to prove beyond `soma check` (invariants and lifecycles are what verify proves).
note: the soma.toml beside this file declares [verify] properties; none applies here (no state machine)
VERIFY OK (vacuous — no state machine)

Builtins used

concat, map, response, to_string, with

Source file: /corpus/web/feature_flags.cell · examples/corpus/web/feature_flags.cell