# swappy-ops/baton architecture

> Architecture diagram of the GitHub repository swappy-ops/baton, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/swappy-ops/baton
- Repository: https://github.com/swappy-ops/baton

## Overview

Baton has two connected runtime surfaces: a FastAPI observability server for intent, session state, traces, and live telemetry; and an optional agent workflow package for routing tasks, retrieval, specialist execution, and distilled memory. The server workflow is shown from user input through persistence and broadcasts. The agent graph's stages and retrieval/memory interactions are supported by source excerpts; some adjacent runtime capabilities are documented but their callers are not shown, so no speculative links are added.

Main entry points: [`baton_server/main.py`](https://github.com/swappy-ops/baton/blob/main/baton_server/main.py) (FastAPI App), [`baton/agents/nodes.py`](https://github.com/swappy-ops/baton/blob/main/baton/agents/nodes.py) (Agent Nodes), [`baton_server/services/event_bus.py`](https://github.com/swappy-ops/baton/blob/main/baton_server/services/event_bus.py) (Event Bus), [`baton_server/websocket/manager.py`](https://github.com/swappy-ops/baton/blob/main/baton_server/websocket/manager.py) (WebSocket Hub)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_server["Observatory Server"]
  node_main["FastAPI App<br/>[main.py]"]
  node_api["REST API<br/>[__init__.py]"]
  node_ws["WebSocket Hub<br/>[manager.py]"]
  node_session[("Session Memory<br/>[session_memory.py]")]
  node_db[("Trace Database<br/>[manager.py]")]
end

subgraph group_events["Events &amp; Telemetry"]
  node_orchestrator["Telemetry Engine<br/>[engine.py]"]
  node_bus["Event Bus<br/>[event_bus.py]"]
  node_attention["Attention Scoring"]
  node_event_schema["Event Schemas<br/>[events.py]"]
end

subgraph group_agents["Agent Workflow"]
  node_router_graph["Workflow Graph<br/>[router_graph.py]"]
  node_agent_nodes["Agent Nodes<br/>[nodes.py]"]
  node_distillation["Memory Distillation"]
end

subgraph group_memory["Retrieval &amp; Memory"]
  node_retrieval[("Retrieval Pipeline<br/>[pipeline.py]")]
  node_structural_index["Structural Index"]
  node_memory_manager["Memory Manager<br/>[manager.py]"]
  node_memory_governance["Memory Governance<br/>[governance.py]"]
end

subgraph group_runtime["Task Runtime"]
  node_budget["Context Budget<br/>[context_budget.py]"]
  node_contract["Task Contracts<br/>[task_contract.py]"]
  node_stability["Stability Manager<br/>[stability.py]"]
  node_dos["Terminology Sync<br/>[dos_sync.py]"]
  node_runtime_workflows["Runtime Workflows<br/>[workflows.py]"]
end

node_user(("User"))
node_ui["Observatory UI"]
node_ollama["Ollama LLM"]
node_chroma[("ChromaDB")]

node_user -->|"submits intent"| node_main
node_main -->|"saves state"| node_session
node_main -->|"broadcasts events"| node_ws
node_main -->|"logs traces"| node_db
node_main -->|"mounts routes"| node_api
node_api -->|"reads state"| node_session
node_api -->|"reads connections"| node_ws
node_main -->|"starts streams"| node_orchestrator
node_orchestrator -->|"publishes events"| node_bus
node_orchestrator -->|"constructs events"| node_event_schema
node_bus -->|"scores events"| node_attention
node_bus -->|"handles events"| node_event_schema
node_bus -->|"broadcasts events"| node_ws
node_ws -->|"streams updates"| node_ui
node_ui -->|"shows telemetry"| node_user
node_router_graph -->|"dispatches stages"| node_agent_nodes
node_agent_nodes -->|"selects contract"| node_contract
node_agent_nodes -->|"sets retrieval budget"| node_budget
node_agent_nodes -->|"checks limits"| node_stability
node_agent_nodes -->|"queries context"| node_retrieval
node_agent_nodes -->|"invokes model"| node_ollama
node_router_graph -->|"dispatches stage"| node_distillation
node_distillation -->|"stores distilled memory"| node_memory_manager
node_memory_manager -->|"normalizes terminology"| node_dos
node_memory_manager -->|"indexes memory"| node_retrieval
node_retrieval -->|"queries and writes"| node_chroma
node_structural_index -->|"supports indexing"| node_retrieval

click node_main "https://github.com/swappy-ops/baton/blob/main/baton_server/main.py"
click node_api "https://github.com/swappy-ops/baton/blob/main/baton_server/api/__init__.py"
click node_ws "https://github.com/swappy-ops/baton/blob/main/baton_server/websocket/manager.py"
click node_session "https://github.com/swappy-ops/baton/blob/main/baton_server/services/session_memory.py"
click node_db "https://github.com/swappy-ops/baton/blob/main/baton_server/db/manager.py"
click node_orchestrator "https://github.com/swappy-ops/baton/blob/main/baton_server/orchestration/engine.py"
click node_bus "https://github.com/swappy-ops/baton/blob/main/baton_server/services/event_bus.py"
click node_attention "https://github.com/swappy-ops/baton/blob/main/baton_server/services/attention_engine.py"
click node_event_schema "https://github.com/swappy-ops/baton/blob/main/baton_server/schemas/events.py"
click node_router_graph "https://github.com/swappy-ops/baton/blob/main/baton/graphs/router_graph.py"
click node_agent_nodes "https://github.com/swappy-ops/baton/blob/main/baton/agents/nodes.py"
click node_distillation "https://github.com/swappy-ops/baton/blob/main/baton/agents/distillation_node.py"
click node_retrieval "https://github.com/swappy-ops/baton/blob/main/baton/retrieval/pipeline.py"
click node_structural_index "https://github.com/swappy-ops/baton/blob/main/baton/retrieval/structural_index.py"
click node_memory_manager "https://github.com/swappy-ops/baton/blob/main/baton/memory/manager.py"
click node_memory_governance "https://github.com/swappy-ops/baton/blob/main/baton/memory/governance.py"
click node_budget "https://github.com/swappy-ops/baton/blob/main/baton/runtime/context_budget.py"
click node_contract "https://github.com/swappy-ops/baton/blob/main/baton/runtime/task_contract.py"
click node_stability "https://github.com/swappy-ops/baton/blob/main/baton/runtime/stability.py"
click node_dos "https://github.com/swappy-ops/baton/blob/main/baton/runtime/dos_sync.py"
click node_runtime_workflows "https://github.com/swappy-ops/baton/blob/main/baton/runtime/workflows.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_api,node_ws,node_session,node_db,node_user,node_ui toneBlue
class node_orchestrator,node_bus,node_attention,node_event_schema,node_chroma toneAmber
class node_router_graph,node_agent_nodes,node_distillation toneMint
class node_retrieval,node_structural_index,node_memory_manager,node_memory_governance toneRose
class node_budget,node_contract,node_stability,node_dos,node_runtime_workflows,node_ollama toneIndigo
```

## Components

### Observatory Server

- [FastAPI App](https://github.com/swappy-ops/baton/blob/main/baton_server/main.py): `baton_server/main.py`
- [REST API](https://github.com/swappy-ops/baton/blob/main/baton_server/api/__init__.py): `baton_server/api/__init__.py`
- [WebSocket Hub](https://github.com/swappy-ops/baton/blob/main/baton_server/websocket/manager.py): `baton_server/websocket/manager.py`
- [Session Memory](https://github.com/swappy-ops/baton/blob/main/baton_server/services/session_memory.py): `baton_server/services/session_memory.py`
- [Trace Database](https://github.com/swappy-ops/baton/blob/main/baton_server/db/manager.py): `baton_server/db/manager.py`

### Events & Telemetry

- [Telemetry Engine](https://github.com/swappy-ops/baton/blob/main/baton_server/orchestration/engine.py): `baton_server/orchestration/engine.py`
- [Event Bus](https://github.com/swappy-ops/baton/blob/main/baton_server/services/event_bus.py): `baton_server/services/event_bus.py`
- [Attention Scoring](https://github.com/swappy-ops/baton/blob/main/baton_server/services/attention_engine.py): `baton_server/services/attention_engine.py`
- [Event Schemas](https://github.com/swappy-ops/baton/blob/main/baton_server/schemas/events.py): `baton_server/schemas/events.py`

### Agent Workflow

- [Workflow Graph](https://github.com/swappy-ops/baton/blob/main/baton/graphs/router_graph.py): `baton/graphs/router_graph.py`
- [Agent Nodes](https://github.com/swappy-ops/baton/blob/main/baton/agents/nodes.py): `baton/agents/nodes.py`
- [Memory Distillation](https://github.com/swappy-ops/baton/blob/main/baton/agents/distillation_node.py): `baton/agents/distillation_node.py`

### Retrieval & Memory

- [Retrieval Pipeline](https://github.com/swappy-ops/baton/blob/main/baton/retrieval/pipeline.py): `baton/retrieval/pipeline.py`
- [Structural Index](https://github.com/swappy-ops/baton/blob/main/baton/retrieval/structural_index.py): `baton/retrieval/structural_index.py`
- [Memory Manager](https://github.com/swappy-ops/baton/blob/main/baton/memory/manager.py): `baton/memory/manager.py`
- [Memory Governance](https://github.com/swappy-ops/baton/blob/main/baton/memory/governance.py): `baton/memory/governance.py`

### Task Runtime

- [Context Budget](https://github.com/swappy-ops/baton/blob/main/baton/runtime/context_budget.py): `baton/runtime/context_budget.py`
- [Task Contracts](https://github.com/swappy-ops/baton/blob/main/baton/runtime/task_contract.py): `baton/runtime/task_contract.py`
- [Stability Manager](https://github.com/swappy-ops/baton/blob/main/baton/runtime/stability.py): `baton/runtime/stability.py`
- [Terminology Sync](https://github.com/swappy-ops/baton/blob/main/baton/runtime/dos_sync.py): `baton/runtime/dos_sync.py`
- [Runtime Workflows](https://github.com/swappy-ops/baton/blob/main/baton/runtime/workflows.py): `baton/runtime/workflows.py`

### Other components

- **User**
- **Observatory UI**
- **Ollama LLM**
- **ChromaDB**

## Connections

- User → FastAPI App: submits intent (evidence: `baton_server/main.py`)
- FastAPI App → Session Memory: saves state (evidence: `baton_server/main.py`)
- FastAPI App → WebSocket Hub: broadcasts events (evidence: `baton_server/main.py`)
- FastAPI App → Trace Database: logs traces (evidence: `baton_server/main.py`)
- FastAPI App → REST API: mounts routes (evidence: `baton_server/main.py`)
- REST API → Session Memory: reads state (evidence: `baton_server/api/__init__.py`)
- REST API → WebSocket Hub: reads connections (evidence: `baton_server/api/__init__.py`)
- FastAPI App → Telemetry Engine: starts streams (evidence: `baton_server/main.py`)
- Telemetry Engine → Event Bus: publishes events (evidence: `baton_server/orchestration/engine.py`)
- Telemetry Engine → Event Schemas: constructs events (evidence: `baton_server/orchestration/engine.py`)
- Event Bus → Attention Scoring: scores events (evidence: `baton_server/services/event_bus.py`)
- Event Bus → Event Schemas: handles events (evidence: `baton_server/services/event_bus.py`)
- Event Bus → WebSocket Hub: broadcasts events (evidence: `baton_server/services/event_bus.py`)
- WebSocket Hub → Observatory UI: streams updates (evidence: `README.md`)
- Observatory UI → User: shows telemetry (evidence: `README.md`)
- Workflow Graph → Agent Nodes: dispatches stages (evidence: `baton/graphs/router_graph.py`)
- Agent Nodes → Task Contracts: selects contract (evidence: `baton/agents/nodes.py`)
- Agent Nodes → Context Budget: sets retrieval budget (evidence: `baton/agents/nodes.py`)
- Agent Nodes → Stability Manager: checks limits (evidence: `baton/agents/nodes.py`)
- Agent Nodes → Retrieval Pipeline: queries context (evidence: `baton/agents/nodes.py`)
- Agent Nodes → Ollama LLM: invokes model (evidence: `baton/agents/nodes.py`)
- Workflow Graph → Memory Distillation: dispatches stage (evidence: `baton/graphs/router_graph.py`)
- Memory Distillation → Memory Manager: stores distilled memory (evidence: `baton/agents/distillation_node.py`)
- Memory Manager → Terminology Sync: normalizes terminology (evidence: `baton/memory/manager.py`)
- Memory Manager → Retrieval Pipeline: indexes memory (evidence: `baton/memory/manager.py`)
- Retrieval Pipeline → ChromaDB: queries and writes (evidence: `baton/retrieval/pipeline.py`)
- Structural Index → Retrieval Pipeline: supports indexing (evidence: `baton/retrieval/structural_index.py`)
