soma.cell

Agent with persistent memory via remember()/recall()

Demonstrates: the remember/recall builtins backed by a [persistent] slot, a knowledge-base lifecycle, and recall() returning () for unknown keys (null-checked with ??). Run: SOMA_LLM_MOCK=echo soma run memory_agent.cell --signal learn "capital" "Paris" Test: SOMA_LLM_MOCK=echo soma test memory_agent.cell

A Soma program from the agents part of the verified corpus: 61 lines, cells Librarian, LibrarianTests. It passes soma check with soma 3.2.4.

How it is built

The cell Librarian declares 3 signals in its face: learn(key: String, value: String) -> Map, lookup(key: String) -> String and answer(question: String) -> Map; keeps 1 memory slot: kb (Map<String, String>, persistent). Its state machine task starts in idle and allows 3 transitions: idle -> working, working -> done and * -> failed. done and failed are terminal. Handlers: learn (line 27), lookup (line 33) and answer (line 38).

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

Generated from soma describe on this file.

The source

soma.toml

[package]
name = "agents-corpus"
version = "0.1.0"
description = "Verified AI-agent example cells (training corpus)"

# Agent LLM config. `mock = "echo"` keeps every example in this directory
# hermetic: `soma test` / `soma run` need no network and no API key, and
# results are deterministic. For a live run, delete the `mock` line (or
# override with SOMA_LLM_MOCK) and provide the key via env.
[agent]
provider = "anthropic"
model    = "claude-opus-4-6"
key      = "${ANTHROPIC_API_KEY}"
mock     = "echo"

# Termination proof for verified_termination.cell's `workflow` machine:
# every path eventually reaches a terminal, no deadlocks.
# `cells` scopes these properties to Solver — the other agents in this
# directory have their own state names and must not inherit them.
[verify]
cells = ["Solver"]
deadlock_free = true
eventually    = ["done", "failed"]

[verify.after.understanding]
eventually = ["solving", "failed"]

[verify.after.solving]
eventually = ["verifying", "failed"]

[verify.after.verifying]
eventually = ["done", "failed"]

memory_agent.cell

// Agent with persistent memory via remember()/recall().
// Demonstrates: the remember/recall builtins backed by a [persistent] slot,
// a knowledge-base lifecycle, and recall() returning () for unknown keys
// (null-checked with ??).
//
// Run:  SOMA_LLM_MOCK=echo soma run memory_agent.cell --signal learn "capital" "Paris"
// Test: SOMA_LLM_MOCK=echo soma test memory_agent.cell

cell agent Librarian {
    face {
        signal learn(key: String, value: String) -> Map
        signal lookup(key: String) -> String
        signal answer(question: String) -> Map
    }

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

    state task {
        initial: idle
        idle    -> working
        working -> done
        *       -> failed
    }

    on learn(key: String, value: String) {
        remember(key, value)
        kb.set(key, value)
        map("stored", key, "value", value)
    }

    on lookup(key: String) {
        let v = recall(key)
        v ?? "unknown"
    }

    on answer(question: String) {
        set_budget(2000)
        let id = to_string(next_id())
        transition(id, "working")
        let r = try { think("Answer using stored facts: {question}") }
        if r.error != () {
            transition(id, "failed")
            return map("status", "failed", "error", r.error)
        }
        transition(id, "done")
        map("status", "done", "answer", r.value)
    }
}

cell test LibrarianTests {
    rules {
        assert learn("capital", "Paris").stored == "capital"
        assert lookup("capital") == "Paris"
        // recall on an unknown key falls back via ??.
        assert lookup("nope") == "unknown"
        assert answer("hi").status == "done"
    }
}

What the toolchain says

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

$ soma check memory_agent.cell
✓ All checks passed.
$ soma test memory_agent.cell
test LibrarianTests ...
  ✓ assert learn("capital", "Paris").stored == "capital"
  ✓ assert lookup("capital") == "Paris"
  ✓ assert lookup("nope") == "unknown"
  ✓ assert answer("hi").status == "done"

4 tests: 4 passed, 0 failed
$ soma verify memory_agent.cell
Verifying memory_agent.cell...
note: soma.toml [verify] cells names 'Solver', which this file does not define — its properties are not checked here

State machine 'task': 4 states, initial 'idle'
  States: [done, failed, idle, working]

  ✓ 4 states, 5 transitions
  ✓ all states reachable from 'idle'
  ✓ terminal states: [failed]
  ✓ no deadlocks
  ✓ liveness: every state can eventually reach a terminal state
  ✓ wildcard transitions: * -> [failed]
  ⚠ `* -> failed` also adds done -> failed — so done is not final. If it should stay final, write `* -> failed except [done]`
  ✓ think-isolated: CTL safety properties hold regardless of LLM output (1 handlers, 3 literal transitions, 0 dynamic)
  ✓ termination: all 3 handlers structurally terminate
  ✓ refinement: handler `answer` ⟶ {working, failed [if r.error != ()], done}
  · transition sources: `answer` 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)
  ✓ effects: handler `answer` calls think() at 2 sites (the cost line counts loop rounds) — no tools available

10 passed, 1 warnings, 0 failures

  Temporal properties for 'task':
  ✓ deadlock_free — no deadlocks in any reachable state
soma.toml: 4 user-defined properties loaded — NONE applies to this file ([verify] cells = ["Solver"] names no cell of it)
Temporal: 1 passed, 0 failed
VERIFY OK

Builtins used

map, next_id, recall, remember, set_budget, think, to_string, transition

Source file: /corpus/agents/memory_agent.cell · examples/corpus/agents/memory_agent.cell