AAnvio
Architecture

Event Flow

Anvio's event backbone — in-process by default, CloudEvents over NATS JetStream at Level 3+ — and the core event types agents and channels emit.

4 min read

Purpose

Understand how state changes propagate through Anvio without tight coupling between the runtime, channels, and automation. This is the mechanism that lets the agent runtime stay channel-agnostic (see Data Flow) and lets features like the learning loop and automation hooks react to what agents do without being called directly.

Transport

  • Default (Level 1–2): in-process LocalEventBus — no external broker, events are dispatched synchronously/in-memory within a single process.
  • Level 3+ (opt-in): NATS JetStream, configured via events.provider: nats in workspace/anvio.yaml and the NATS_URL environment variable.

The envelope format is CloudEvents 1.0 regardless of transport, so consumers (automation hooks, the learning loop, channel adapters) don't need to know whether they're running against the local bus or NATS.

Core event types

EventFired when
anvio.session.started.v1A new session begins
anvio.session.ended.v1A session completes or is stopped
anvio.agent.run.requested.v1An agent run is queued (interactive or detached)
anvio.agent.run.completed.v1An agent run finishes (success or failure)
anvio.agent.run.chunk.v1A streaming chunk of model output is produced
anvio.tool.executed.v1A tool call completes
anvio.memory.stored.v1A memory entry is persisted

These are the events referenced throughout the runtime and learning-loop design. Progress notifications during a run (AGENT_RUN_PROGRESS) and inbox injections (AGENT_INBOX_INJECTED) build on this same backbone — see Data Flow for how the worker consumes them.

Event flow diagram

Rendering diagram…

Why this matters for specific features

  • Channel-agnostic runtime. The agent runtime never calls a channel adapter directly — it emits events, and the Channel Hub decides how to format and deliver them per channel (Telegram, Slack, Web Chat, and the other 14 adapters).
  • Learning loop. Session-end and tool-success events trigger the learning engine: a memory nudge, an LLM-generated session summary, and pattern analysis that can produce skill drafts. This is asynchronous and decoupled from the request/response path — a slow summarizer never blocks the user's reply.
Rendering diagram…
  • Automation and event hooks. workspace/hooks/hooks.yaml registers handlers that react to specific event types — for example, running a blueprint whenever a session ends, or notifying a channel when a tool fails. See Automation Engine.
  • Approval workflow. The runtime pauses at awaiting_approval and resumes only after an APPROVAL_DECIDED event is observed — approvals are just another event on the same bus.

Switching to NATS JetStream

# workspace/anvio.yaml
spec:
  events:
    provider: nats
export NATS_URL=nats://localhost:4222

This is a Level 3 change — it typically accompanies moving storage to PostgreSQL and enabling JWT auth for a multi-user deployment. See NATS JetStream for operational detail and Local Event Bus for the in-process default.

Best practices

  • Don't build features that call other subsystems directly when an event already exists for the transition you care about — subscribe instead, to keep the channel-agnostic guarantee intact.
  • Treat event names as a stable contract (anvio.*.v1) — the versioned suffix exists so consumers can be migrated deliberately if a payload shape changes.
  • When debugging a run that seems to "hang," check whether it's actually waiting on an event (e.g., awaiting_approval) rather than assuming the runtime crashed.

Common mistakes

  • Assuming NATS is required. It's an opt-in Level 3 transport; the in-process bus handles everything at Level 1–2, including a fully working learning loop and channel harness.
  • Polling instead of subscribing. For anything that needs to react to agent activity (custom automation, external integrations), hook into the event bus or workspace/hooks/hooks.yaml rather than polling anvio sessions list in a loop.
  • Conflating CloudEvents envelope with transport. The envelope format (CloudEvents 1.0) is constant; only the underlying transport (local bus vs. NATS) changes with the level.

References

Edit on GitHub

Last updated on

On this page