06 — Agents and protocols¶
Status: runtime support shipped in v0.7; agents run on the current tokio executor with per-agent mailboxes.
spawn AgentName()creates a long-lived actor;agent!Msg(args)enqueues fire-and-forget;agent?Msg(args) @durationenqueues a deadline-bounded ask. Seedocs/internals/runtime.mdfor the executor model.
An agent is an isolated unit of state, concurrency, failure, and capability. Agents communicate by typed messages described by a protocol.
A trivial agent¶
protocol Echodeclares a typed message contract: one message,Ping, taking aStrand replying with aStr.agent Echoer: Echodeclares an agent that implementsEcho. The:is the canonical short form for "implements".on Ping(msg) -> msgis the compact form for an expression-body handler. The parameter types and reply type are taken from the protocol.
Try it¶
Stateful agents¶
n = 0is agent state. The type is inferred from the initializer (I64here). State is private to the agent — there is no way to read or write it from outside.- The handler body
{ n += 1; n }is a block with two statements; the last expression (n) is its value and so is the reply toInc. - Each invocation of
Incruns to completion before the next message is processed; state mutations are race-free by construction.
Isolation rules¶
From spec §12.5:
- Agent state is isolated.
- Messages crossing agents must be owned, immutable shared, copyable, or serialized (collectively: Sendable).
- Managed heap references cannot cross agents.
- Capabilities must be explicitly passed.
- Agent failure does not corrupt other agents.
All five are enforced by the type / borrow / effect checkers as of
v0.10. Cross-agent Sendable violations surface as MT3011; capability
narrowing as MT4010.
Next¶
Continue to 07 — Send, ask, deadlines.