Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Architecture

micro is a Rust workspace split by runtime responsibility. Provider translation, agent control flow, tools, persistence, extensions, and the terminal interface are separate crates.

Main request path

micro-cli
  -> micro-agent
    -> micro-provider
      -> model API

micro-cli resolves the workspace, model, credential, settings, trust decision, sandbox policy, tools, and session. It then builds the runtime used by interactive, print, or RPC mode.

micro-agent owns the model/tool loop. It builds a provider-neutral Context, records request metadata, streams a response, executes requested tools, appends tool results, and repeats until the model returns a response with no more tool calls.

micro-provider translates the shared context into the selected provider protocol and translates the streamed response back into StreamEvent values.

Core crates

CrateResponsibility
micro-typesMessages, content blocks, usage, stream events, agent events, and ledger events.
micro-dirsConfiguration and data-path resolution.
micro-configSettings, project trust, and persisted configuration.
micro-authStored credentials and environment fallback.
micro-modelsBundled, user, and live model catalogs.
micro-contextSystem prompt, project instructions, skills, and compaction.
micro-providerProvider request formats and streaming parsers.
micro-toolsModel-callable tools.
micro-sandboxCommand wrapping and file-policy checks.
micro-agentProvider-neutral agent loop.
micro-sessionAppend-only conversations, ledger events, metadata, and blobs.
micro-commandsSlash-command parsing and outcomes.
micro-extensionsBun extension host and capability broker.
micro-mcpMCP servers over stdio and streamable HTTP, OAuth sign-in, and their tools.
micro-codemodeThe codemode tool's QuickJS sandbox, script globals, and tool bridge.
micro-skillsSkill discovery and loading.
micro-promptsPrompt-template discovery and argument expansion.
micro-mermaidMermaid diagram rendering requested by a response.
micro-remotePhone pairing, encryption, relay protocol, and session mirroring.
micro-rpcJSON-line control protocol.
micro-tuiTerminal rendering and input.
micro-cliStartup, subcommands, runtime assembly, and print mode.
micro-testkitScripted providers, fake tools, and session fixtures.

Lower-level crates do not depend on the UI or CLI.

Provider boundary

Providers receive a Model, a Context, and a credential. Context contains the system prompt, conversation messages, and JSON-schema tool definitions. It describes tools but cannot execute them.

That boundary keeps provider differences out of the agent loop. Anthropic Messages, OpenAI-compatible completions, OpenAI Responses, Google APIs, Vertex, and Bedrock each implement request assembly and stream parsing. The agent consumes one event vocabulary.

The provider prepares one serialized request value. The agent hashes and enqueues that body for persistence before network I/O, then passes the same value to the provider transport. Request inspection reads the retained body and verifies its hash. A process crash can lose queued records that have not reached disk; reconstruction is the fallback for sessions that did not retain the body.

Agent loop

For each turn, the agent:

  1. builds the current context;
  2. applies context and extension hooks;
  3. records turn_request data;
  4. asks the provider to stream a response;
  5. emits AgentEvent values for the caller;
  6. records usage and the completed assistant message;
  7. executes valid tool calls;
  8. appends tool results and starts another provider turn when needed.

A response cut off by the output-token limit does not execute incomplete tool calls. Tool errors are returned to the model as error results so the turn can recover.

Transient provider failures may be retried before any output is shown. Once content has streamed to the user, micro does not issue the same request again automatically.

A transient failure is a 408, 409, 425, 429, 500, 502, 503, or 504 response, retried up to five attempts. micro waits as long as the provider asks through retry-after-ms or Retry-After (seconds or an HTTP date); when the header is absent or unreadable it backs off exponentially from one second up to thirty. A provider that asks for more than sixty seconds is not waited out, and the failure is reported with the wait it asked for.

Streaming

StreamEvent carries deltas rather than rebuilt partial messages. Consumers that need the response-so-far use PartialResponse to accumulate them.

Each provider stream ends with one Done or Error event. The agent layer similarly ends with AgentEnd.

This lets the TUI, print renderer, RPC server, and tests consume the same loop.

Persistence

The agent sends messages, compaction records, and ledger events through one recorder channel. A separate task appends them to the session file in receive order.

Session observability lives behind the shared session reader. The CLI and TUI use the same billing, request-inspection, prefix-diff, sandbox-event, and extension-event records. The TUI may cache totals for rendering, but it does not keep a second accounting source.

Writing during the run limits data loss if the process exits unexpectedly. The agent does not wait for individual filesystem writes, but shutdown waits for the recorder channel to drain.

Compaction adds a summary and moves the active conversation head. Original messages remain in the log.

See Ledger format.

Project startup

The CLI decides project trust before loading .micro/ resources. It then resolves the sandbox and builds tools around that policy.

This keeps untrusted extensions, settings, prompts, and skills out of the runtime rather than adding checks at each later use.

Extension capabilities are enforced separately at the host boundary. The Bun host has an empty inherited environment, no network access, and read-only access to the active workspace and loaded packages. State changes use brokered APIs. Commands requested through the broker still pass through the session sandbox policy.

Tests

micro-testkit provides a scripted provider and deterministic tools for agent-loop tests. CLI tests run the built binary against scratch configuration and workspaces. Terminal behavior is tested under a pseudo-terminal and, when necessary, by replaying escape sequences into a screen grid.

See Testing extensions for the extension-specific harnesses.