Skip to content

Persistent Memory

Share declared state across invocations.

Local variables belong to the current execution. memory scope declares persistent agent fields; strict requires declared fields. This counter reads the stored value, increments it and saves it.

Complete example

Save the example as memory.aal

memory.aal
agent Tutorial is Agent
  memory scope "tutorial" strict
    count: Int = 0

  public
    ability run() -> Int effects(memory.read, memory.write)
      count = memory.require("count") + 1
      memory.store("count", count)
      return count
Download .aal

Syntax explained

  1. Defaults initialize missing fields only; they never overwrite stored values.

  2. memory.require("count") reads a current value and fails if missing or stale. memory.store writes it. Declare memory.read and memory.write.

  3. Keep the same --state-dir and use a new --invocation for the next request. Reusing an invocation ID resumes or replays it; it does not create a fresh request.

Build and run

Terminal
cargo run --locked --bin aalc -- check memory.aal

cargo run --locked --bin aalc -- build memory.aal -o memory.aalc

cargo run --locked --bin aalc -- run memory.aalc \
  --agent Tutorial --ability run \
  --args '{}' \
  --state-dir .aal-tutorial/memory --invocation memory-1

Run with a fresh state directory. The returned value is shown below; the CLI also prints execution information.

Expected result
1

Try it yourself

Keep the state directory and use memory-2 as the invocation ID. The result is 2. Reusing memory-2 replays that result instead of incrementing again.

Language source and specification