How Swarmie Works
Swarmie is event-driven: frontends send Op messages to core, and core emits Event messages back.
Core model
- Frontend -> core:
Op(for exampleUserMessage,SwitchModel,Shutdown) - Core -> frontend:
Event(for exampleTextDelta,ToolCallStart,TurnComplete,Idle)
SwarmCore starts a background session task and relays events to subscribers (TUI, server, or headless harnesses).
Session task states
The session loop has two modes:
Idle: waiting for operationsActive turn: running an agentic turn and multiplexing turn output + incoming ops
While a turn is active, some ops execute immediately (Interrupt, PermissionReply, Shutdown), while others are deferred until idle.
Turn loop lifecycle
For each user message:
- Build a
TurnContextsnapshot (messages, tools, mode, model/provider, limits). - Call provider streaming API.
- Emit deltas (
TextDelta,ThinkingDelta, tool-call start/input events). - Append assistant response to local message history.
- If stop reason is tool-use, execute tools through registry, run permission checks/hooks, append tool results, and continue.
- If end-turn condition is reached, emit
TurnComplete, update context usage, and emitIdle.
Tools and permissions
Tool calls are executed through the shared tool registry. Permission-gated calls emit PermissionRequest; user responses (y/n/a/!) map to PermissionReplyDecision.
Context and compaction
Core tracks token usage and emits ContextUpdate during and after turns. Compaction can run with a separate compact model when configured.
Persistence and resume
Core records rollout/state metadata as turns complete. Sessions can be resumed from persisted records via swarmie resume or server APIs.
Modes, thinking, and hooks
- Mode is included in prompt assembly and can be switched at runtime.
- Thinking level is part of request shaping.
- Hooks can fire across lifecycle events (for example prompt submit, tool phases, config changes).