Skip to content

Build an application with capabilities

Write a Prog application over Lema v0.2's six byte-level capability tags and opaque secret exchanges, configure lema-cap.json, format a 4 KiB BlockStore device, and package TLS-bound credentials with lema secret bundle.

In Lema v0.2, applications interact with the outside world through a small, typed capability boundary (crates/lema-cap, version 2 LC\x02 frames). Instead of linking C networking or TLS libraries into the trusted runtime, the host provides only six flat-byte capability tags (tcp, udp, block, random, clock, and secret), while cryptography (ChaCha20Poly1305, X25519, Ed25519, P256Verify, RsaVerify), TLS 1.3 (Tls13), HTTP/1.1 (Http1), S3 (S3, S3d), and the copy-on-write 4 KiB block file store (BlockStore) are all written and proved in Lema above them (How programs reach the outside world).

This guide walks through choosing your application’s command families, configuring all six capability sections in lema-cap.json, formatting a 4 KiB BlockStore volume, packaging TLS-bound credentials with lema secret bundle, and running your application through the release gate.

Before you start

1. Choose the command families for your application

A capability application’s entry point is a pure function Nat → ByteArray → Prog AppCmd AppReply ByteArray (where task 0 is the main task). Depending on what your application does, its lema.exec/v2 record declares a subset of three groups of families:

Family groupFamiliesWhen to use them
1. The six byte-level capability tagstcp, udp, block, random, clock, secretUse block for durable storage over 4 KiB copy-on-write blocks (BlockStore), clock for monotonic/wall time, random for OS entropy, and tcp / udp for raw byte transport. Application code never calls secret directly; secrets are accessed only inside the pinned exchange and serve sub-runs below.
2. Opaque credential drivers (LEMA-384)exchange, serveUse exchange (SecretExchange.signed for client SigV4 or SecretExchange.bearer for Bearer tokens, used by the Collector and Escrow Ledger) or serve (ServeVerified for server TLS 1.3 Ed25519 handshakes + inbound SigV4 verification, used by S3d). The VM worker runs each exchange as a pinned sub-run and returns only the outcome to your app, so your application code never holds a raw credential.
3. Concurrency and coordinationtasks, actors, sessionsSupervised child tasks (start, await, cancel, spawn), stateful actors (actor, send, take, receive, reply), and protocol sessions (session, send, recv, close). These coordinate execution inside the VM without granting extra I/O permissions.

When you run lema run or lema release inside a project, Lema automatically infers the required families from your entry’s type (or reads them from your lema.exec-source/v2 manifest).

2. Write the deployment configuration (lema-cap.json)

Every deployment directory contains a lema-cap.json file with schema lema.cap-config/v1. It maps the logical endpoint, device, and secret IDs named by your Lema program to concrete host addresses, file paths, and limits.

Here is a complete lema.cap-config/v1 reference showing all six configuration sections supported in Lema v0.2:

$DEPLOYMENT/lema-cap.json
{
  "schema": "lema.cap-config/v1",
  "max_open_handles": 64,
  "tcp_origins": {
    "api": {
      "address": "203.0.113.10:443",
      "max_recv_bytes": 65536,
      "max_deadline_ms": 30000
    },
    "s3": {
      "address": "203.0.113.20:443",
      "max_recv_bytes": 1048576,
      "max_deadline_ms": 30000
    }
  },
  "tcp_listeners": {
    "s3d": {
      "address": "0.0.0.0:8443",
      "max_recv_bytes": 65536,
      "max_deadline_ms": 30000
    }
  },
  "udp_endpoints": {
    "telemetry": {
      "bind": "127.0.0.1:0",
      "peer": "127.0.0.1:9125",
      "max_datagram_bytes": 1472,
      "max_deadline_ms": 5000
    }
  },
  "block_devices": {
    "journal": {
      "path": "/srv/collector/journal.img"
    }
  },
  "secrets": {
    "dir": "/srv/collector/deploy/secrets",
    "grants": {
      "api": ["root"],
      "storage": ["root"]
    }
  }
}

Include only the sections your application uses (for example, a block-only app needs only schema and block_devices; legacy v0.1 fields such as origins or files.state_dir are refused as unknown fields):

  • max_open_handles: Optional cap on simultaneous open transport sockets/handles for the run.
  • tcp_origins: Named outbound TCP peers (address as host:port, max_recv_bytes, and max_deadline_ms).
  • tcp_listeners: Named inbound TCP bind addresses (address, max_recv_bytes, and max_deadline_ms, which must be at least as large as the app’s per-call accept deadline, such as 10000 ms for S3d).
  • udp_endpoints: Named UDP socket configurations.
  • block_devices: Maps each logical device ID (1..64 bytes of [a-z0-9._-]) to a canonical absolute file path (without . or .., no path named twice).
  • secrets: Names the owner-only absolute directory (dir) holding secret-<version>.bundle files and maps each secret ID in grants to the actor codes (such as ["root"]) permitted to invoke its exchange.

3. Provision a 4 KiB BlockStore volume (block_devices)

Applications that keep crash-safe durable state (such as S3d, the Escrow Ledger’s journal, or the Collector’s resume store) use Lema.Cap.BlockStore (LEMA-382), a proved two-barrier copy-on-write file store over 4096-byte blocks.

Before the first run, create the backing file as a blank regular file owned by your operator user (mode 0600) under protected parent directories, sized to the exact multiple of 4096 bytes your application deployment expects (for example, 1024 blocks = 4 MiB):

Shell
install -d -m 700 /srv/collector /srv/collector/deploy
install -m 600 /dev/null /srv/collector/journal.img
truncate -s $((1024 * 4096)) /srv/collector/journal.img

How lema-cap and BlockStore guard the volume:

  • Before starting the VM worker, lema run checks that the device path is canonical, a regular file with hardlink count 1, mode 0600 (not writable by group or others) under protected parent directories, and outside the Lean distribution, runtime output, and deployment directories. It binds only that device file read-write into the application sandbox and holds an exclusive lock on it for the duration of the run (a concurrent run against the same device is refused as busy).
  • Because the device file holds mutable application state, its bytes are not part of the lema-cap.json deployment digest. Back up a volume by copying the .img file while no run holds its lock.

4. Package TLS-bound credentials (lema secret bundle)

An application that uses exchange (client SigV4 or BearerHeader) or serve (S3d’s Ed25519Sign TLS key + SigV4VerifyServer) never reads raw key files directly. Instead, you package credentials into a versioned bundle (secret-<version>.bundle) using lema secret bundle.

Client credentials (sigv4 and bearer_header) bound to a TLS identity

Every client credential in a secret bundle must include a tls block naming the exact lowercase server_name and X.509 trust anchors (subject, kind: "ed25519" | "p256" | "rsa", and key hex) that the application’s Tls13Concrete session authenticates. The pinned SecretExchange sub-run releases the credential only on a TLS 1.3 connection whose handshake verified that exact server identity:

client-secrets-spec.json
{
  "schema": "lema.secret-bundle/v1",
  "version": 1,
  "entries": {
    "api": {
      "role": {
        "kind": "bearer_header",
        "allowed_origins": ["api"]
      },
      "tls": {
        "server_name": "api.example.com",
        "anchors": [
          {
            "subject": "301a3118301606035504030c0f6170692e6578616d706c652e636f6d",
            "kind": "ed25519",
            "key": "d75a980182b1…"
          }
        ]
      },
      "secret_file": "/abs/path/to/api.token"
    }
  }
}

Server credentials (ed25519_sign and sigv4_verify_server for S3d)

For an S3d server (apps/s3d-vm.json), generate the Ed25519 TLS leaf key and client SigV4 secret, write the lema.secret-bundle/v1 spec, and run lema secret bundle:

Shell
openssl genpkey -algorithm ed25519 -out tls.pem
openssl pkey -in tls.pem -outform DER | tail -c 32 > tls.key
openssl req -new -x509 -key tls.pem -subj /CN=s3.example.com \
  -addext subjectAltName=DNS:s3.example.com -days 365 -outform DER -out tls.der
openssl rand -base64 30 | tr -d '\n' > sigv4.secret

cat > spec.json <<'EOF'
{
  "schema": "lema.secret-bundle/v1",
  "version": 1,
  "entries": {
    "s3d-tls": {
      "role": {"kind": "ed25519_sign"},
      "secret_file": "/abs/path/to/tls.key"
    },
    "s3d-sigv4": {
      "role": {
        "kind": "sigv4_verify_server",
        "listener_id": "s3d",
        "expected_access_key_id": "AKIDEXAMPLE",
        "region": "us-east-1",
        "service": "s3",
        "max_skew_ms": 900000
      },
      "secret_file": "/abs/path/to/sigv4.secret"
    }
  }
}
EOF

install -d -m 700 /srv/s3d/deploy /srv/s3d/deploy/secrets
lema secret bundle --spec spec.json --out /srv/s3d/deploy/secrets
shred -u tls.key tls.pem

Why can you rotate secrets without cutting a new software release? Because lema release digests lema-cap.json (which names secrets.dir and grants), never the secret-<version>.bundle files inside secrets.dir. When you write a new bundle with "version": 2 into /srv/s3d/deploy/secrets, a running or restarted host picks up the newest bundle version automatically on its next secret lookup.

5. Build, release, and run the capability application

Because any application that names exchange or serve uses secrets under the hood, its build checks ConstantTime and SecretFlow.<role>, and it must be executed through a release (Release an application):

Shell
# Inside a project:
lema release
lema run --release --input app.input

# Or with explicit store/artifact/deployment flags:
lema exec import --manifest apps/s3d-vm.json --entry s3d.vm --expect none
lema build s3d.vm --name s3d --worker /usr/local/bin/lema-vm-runner
lema release create --artifact s3d --deployment /srv/s3d/deploy
lema release select --release $RELEASE_ID --artifact s3d --deployment /srv/s3d/deploy
lema run --release $RELEASE_ID --artifact s3d --deployment /srv/s3d/deploy --input s3d.input

Common refusals

Refusal message containsCause and fix
unknown field: origins or unknown field: fileslema-cap.json contains legacy v0.1 fields. Replace them with v0.2’s tcp_origins, tcp_listeners, udp_endpoints, block_devices, and secrets.
unbound client credentialA sigv4 or bearer_header entry in lema secret bundle is missing its tls (server_name and non-empty anchors) block.
busy (on a block device)Another lema-vm-runner process currently holds the exclusive lock on the .img block device file. Wait for the first run to finish.
volume_errorThe block device file is neither all-zero (blank) nor a valid formatted BlockStore volume (for example, an initial format was interrupted mid-block-0). Zero the file with truncate and run again.

Lema v0.3.2 · Updated 29 October 2026 · Version and compatibility