Aex Brain
Concepts

Agentloop

The placed WebAssembly Component that owns a turn's policy.

An Agentloop decides how one user message becomes a completed turn. Brain owns mechanism: the journal, model credential, Tool catalogue, effect dispatch, deadlines, and cancellation. The Agentloop owns policy: prompts, context selection, compaction, Tool sequencing, and the final transcript and extension state.

Place a Component

Brain accepts an already-built WebAssembly Component. It does not compile TypeScript or another source language.

import { agentloop, brainEnv, component } from "@aexhq/brain";
import { z } from "zod";

const assistant = agentloop({
  options: z.object({ compactAt: z.number().min(0).max(1) }),
  implementation: component(new URL("./assistant.wasm", import.meta.url)),
});

const placed = assistant({ env: brainEnv({ name: "brain" }), compactAt: 0.8 });

component(...) accepts a URL or Uint8Array. The client reads the raw bytes, admits them, and creates the session with the resulting id, the parsed options, and the Environment. Every Agentloop factory is placed with { env, ...options }. Dependencies are packaged with the implementation or prepared by its Environment; resource grants belong to that Environment’s configuration.

The Component contract

The source of truth is crates/brain-env/wit/agentloop/agentloop.wit. The Component exports one turn function. Its input contains:

  • the user input;
  • the current transcript and the loop's kv (a key-value map it keeps between activations);
  • committed events since its previous activation;
  • its configuration, system prompt, Tool definitions, and deterministic runtime envelope.

During the activation it may call Brain's events, model, dispatch, emit, set-transcript, kv-read, kv-put, kv-delete, and telemetry imports. model, dispatch, and emit cross the Brain boundary. Brain commits an external-effect intent before dispatch, sends it once, and commits its terminal or unknown outcome before returning it.

KV reads distinguish absence from JSON null. Put creates or replaces one value; delete removes a key from the current projection without erasing journal history. Missing-key deletion is a no-op. JavaScript bindings expose ctx.kv.read(key), ctx.kv.put(key, value), and ctx.kv.delete(key).

The Component saves transcript and kv changes inline through the state imports, each committed before returning. Turn output carries only the result. Every turn uses a fresh Wasm instance, while compiled and prelinked code is reused.

Where it runs

An Agentloop may be placed in any Environment. The brain env runs it in a fresh Wasmtime instance per activation with its Environment’s configured grants, the same way it runs a Tool. An Environment reached over HTTP receives each turn with the address of Brain's turn routes for that turn and the token that opens them, and reaches the same services through them; Brain journals every call the same way. The factory always requires an explicit env so the placement is sealed into the session instead of inferred; Brain does not fall back to another placement.

State and failures

Saved transcript, kv, and effect records survive an activation. Guest memory and open connections do not. An activation interrupted by restart is recorded as failed and is not replayed. The next turn rebuilds its inputs from the canonical journal.

On this page