# usnistgov/afl-automation architecture

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

- Interactive diagram: https://gitdiagram.com/usnistgov/afl-automation
- Repository: https://github.com/usnistgov/afl-automation

## Overview

1. **Overall shape**
   - This is a Python automation platform for the NIST Autonomous Formulation Laboratory.
   - The repository is organized around a reusable HTTP-facing server framework plus many device- and protocol-specific modules.

2. **Core runtime: DeviceServer / APIServer**
   - The architectural center is `AFL/automation/APIServer`.
   - It provides the common server pattern: expose Python objects over HTTP, manage request/response handling, queue work, render outputs, and support automatic UI generation.
   - `shared.launcher` is the entry point layer that starts these servers, typically with `waitress` in production or Flask’s built-in server for local use.

3. **Driver layer: hardware and service adapters**
   - `APIServer/Driver.py` and related mixins define the contract for concrete devices.
   - Drivers encapsulate hardware operations and present them through the common API.
   - There are dummy drivers and virtual drivers alongside real ones, so the same API works for production hardware and offline simulation.

4. **Protocol and workflow subsystems**
   - `prepare`, `mixcalc`, and `orchestrator` sit above individual devices and assemble them into lab workflows.
   - `mixcalc` handles formulation logic and mass-balance calculations.
   - `prepare` focuses on execution planning for preparation robots like OT-2.
   - `orchestrator` coordinates multi-step closed-loop workflows across devices.

5. **Hardware domains**
   - `instrument` covers measurement devices such as scattering instruments, spectrometers, cameras, and beamline-specific adapters.
   - `loading` covers fluid handling and sample loading hardware such as pumps, valves, relays, pressure controllers, sensors, and sample cells.
   - `manipulate` includes robot/arm-style physical manipulation.
   - `sample_env` models environmental control around samples, such as temperature decks.

6. **Data flow and persistence**
   - The `APIServer/data` package defines how server results are packaged and stored.
   - Data can be represented as JSON, structured packets, trash/discard placeholders, or sent to Tiled-backed storage.
   - This creates a boundary between live device control and durable experiment/event recording.

7. **Tiled integration and browser tooling**
   - The repo includes Tiled client adapters and a `tiled` config, indicating Tiled is a key external data service.
   - `apps/tiled_browser` and `APIServer/data/TiledClients` provide user-facing browsing and catalog access to stored experimental data.
   - This is the main persistence/query plane separate from the live control plane.

8. **User interface layer**
   - `AFL/automation/apps` contains browser-facing HTML/JS/CSS apps for server control, configuration, status, mix doctor workflows, OT-2 deck management, and Tiled browsing.
   - `APIServer/static/jl` also embeds a JupyterLite environment with notebooks and extensions, giving an in-browser analysis/scripting surface.
   - UI assets are mostly static and talk to the HTTP APIs exposed by the servers.

9. **Shared infrastructure**
   - `AFL/automation/shared` holds cross-cutting utilities: serialization, units, queue wrappers, persistent config, server discovery, widgets, warnings, and helper clients.
   - This layer is the glue between drivers, UIs, and data backends.
   - It also suggests that configuration is partly user-local, with `~/.afl/config.json` used by virtual server setup.

10. **Offline/virtual operation**
   - The repo deliberately supports a full software-only stack under `server_scripts/virtual_instrument` and dummy modules in `prepare`, `APIServer`, and `instrument`.
   - The same HTTP interfaces are preserved, so workflows can be tested without hardware.
   - This is an important architectural boundary: hardware implementation is swappable behind the same API.

11. **Deployment and environment**
   - There are Dockerfiles for major service groups (`instrument`, `mixcalc`, `orchestrator`, `tiled`) and a `docker-compose.yml` for multi-service deployment.
   - `launchers/` and `deploy/` contain operational scripts and platform-specific setup, including vendored packages for constrained environments.
   - The architecture is therefore service-oriented even though the code is packaged as one repository.

12. **Quality, docs, and CI**
   - Tests are organized with `pytest` and cover drivers, orchestration, Tiled integration, web app behavior, and math/model logic.
   - GitHub Actions runs test, integration, docs, and publish workflows.
   - Sphinx docs under `docs/` describe architecture, data model, launcher behavior, and UI usage, reinforcing that the repo is intended to be deployed as a coordinated automation platform rather than a single application.

13. **Primary end-to-end flow**
   - A user or launcher starts one or more DeviceServer instances.
   - UI or client code sends HTTP requests to those servers.
   - Drivers execute hardware or virtual actions.
   - Results are queued, rendered, and optionally persisted to JSON/Tiled.
   - Higher-level workflow services like `prepare` and `orchestrator` compose multiple device actions into complete lab protocols.

Main entry points: [`AFL/automation/APIServer/APIServer.py`](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/APIServer.py) (APIServer), [`AFL/automation/APIServer/Driver.py`](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/Driver.py) (Driver), [`server_scripts/virtual_instrument/DummySampleServer.sh`](https://github.com/usnistgov/afl-automation/blob/main/server_scripts/virtual_instrument/DummySampleServer.sh) (Virtual scripts), [`AFL/automation/orchestrator/OrchestratorDriver.py`](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/orchestrator/OrchestratorDriver.py) (Orchestrator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core runtime"]
  node_apiserver["APIServer<br/>http server framework<br/>[APIServer.py]"]
  node_driver["Driver<br/>device contract<br/>[Driver.py]"]
  node_driver_webapps["WebApp mixin<br/>api/ui mixin"]
  node_queue_daemon(("Queue daemon<br/>async worker<br/>[QueueDaemon.py]"))
end

subgraph group_workflow["Workflows"]
  node_mixcalc["Mixcalc<br/>formulation engine<br/>[MassBalance.py]"]
  node_prepare["Prepare<br/>prep workflow<br/>[PrepareDriver.py]"]
  node_orchestrator["Orchestrator<br/>closed-loop coordinator"]
end

subgraph group_hardware["Hardware domains"]
  node_instrument["Instrument<br/>measurement adapters"]
  node_loading["Loading<br/>fluid handling<br/>[SyringePump.py]"]
  node_manipulate["Manipulate<br/>robotics<br/>[XArmManipulator.py]"]
  node_sample_env["Sample env<br/>environment control<br/>[TemperatureDeck.py]"]
end

subgraph group_data["Data plane"]
  node_data_json["JSON data<br/>result packaging<br/>[DataJSON.py]"]
  node_data_packet["Data packet<br/>event envelope<br/>[DataPacket.py]"]
  node_data_tiled[("Tiled data<br/>persistence adapter<br/>[DataTiled.py]")]
  node_tiled_clients["Tiled clients<br/>catalog adapters"]
end

subgraph group_ui["User interface"]
  node_apps_afl["AFL app<br/>control UI<br/>[webapp.html]"]
  node_apps_tiled["Tiled browser<br/>data UI<br/>[tiled_browser.html]"]
  node_jupyterlite["JupyterLite<br/>in-browser analysis<br/>[index.html]"]
end

subgraph group_infra["Shared infra"]
  node_launcher["Launcher<br/>startup layer<br/>[launcher.py]"]
  node_shared_util["Shared utils<br/>common library<br/>[utilities.py]"]
  node_persistent_config["Config store<br/>local config"]
  node_deploy["Deploy<br/>ops assets<br/>[docker-compose.yml]"]
  node_virtual_scripts["Virtual scripts<br/>offline runtime"]
end

node_launcher -->|"starts"| node_apiserver
node_apiserver -->|"hosts"| node_driver
node_driver -->|"extends"| node_driver_webapps
node_apiserver -->|"queues work"| node_queue_daemon
node_apiserver -->|"uses"| node_shared_util
node_shared_util -->|"persists"| node_persistent_config
node_driver -->|"implements"| node_instrument
node_driver -->|"implements"| node_loading
node_driver -->|"implements"| node_manipulate
node_driver -->|"implements"| node_sample_env
node_mixcalc -->|"feeds"| node_prepare
node_prepare -->|"coordinates"| node_orchestrator
node_orchestrator -->|"controls"| node_instrument
node_orchestrator -->|"controls"| node_loading
node_orchestrator -->|"controls"| node_sample_env
node_apiserver -->|"returns"| node_data_json
node_apiserver -->|"emits"| node_data_packet
node_data_packet -->|"stores"| node_data_tiled
node_data_tiled -->|"queries"| node_tiled_clients
node_apps_afl -->|"calls"| node_apiserver
node_apps_tiled -->|"browses"| node_data_tiled
node_jupyterlite -->|"analyzes"| node_data_tiled
node_deploy -->|"packages"| node_launcher
node_virtual_scripts -.->|"mirrors"| node_apiserver
node_virtual_scripts -.->|"simulates"| node_instrument
node_virtual_scripts -.->|"simulates"| node_loading
node_virtual_scripts -.->|"simulates"| node_prepare

click node_apiserver "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/APIServer.py"
click node_driver "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/Driver.py"
click node_driver_webapps "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/DriverWebAppsMixin.py"
click node_queue_daemon "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/QueueDaemon.py"
click node_launcher "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/shared/launcher.py"
click node_shared_util "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/shared/utilities.py"
click node_persistent_config "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/shared/PersistentConfig.py"
click node_instrument "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/instrument/ScatteringInstrument.py"
click node_loading "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/loading/SyringePump.py"
click node_manipulate "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/manipulate/XArmManipulator.py"
click node_sample_env "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/sample_env/TemperatureDeck.py"
click node_mixcalc "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/mixcalc/MassBalance.py"
click node_prepare "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/prepare/PrepareDriver.py"
click node_orchestrator "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/orchestrator/OrchestratorDriver.py"
click node_data_json "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/data/DataJSON.py"
click node_data_packet "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/data/DataPacket.py"
click node_data_tiled "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/data/DataTiled.py"
click node_tiled_clients "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/data/TiledClients/CatalogOfAFLEvents.py"
click node_apps_afl "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/apps/afl_app/webapp.html"
click node_apps_tiled "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/apps/tiled_browser/tiled_browser.html"
click node_jupyterlite "https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/static/jl/index.html"
click node_deploy "https://github.com/usnistgov/afl-automation/blob/main/docker-compose.yml"
click node_virtual_scripts "https://github.com/usnistgov/afl-automation/blob/main/server_scripts/virtual_instrument/DummySampleServer.sh"

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_apiserver,node_driver,node_driver_webapps,node_queue_daemon toneBlue
class node_mixcalc,node_prepare,node_orchestrator toneAmber
class node_instrument,node_loading,node_manipulate,node_sample_env toneMint
class node_data_json,node_data_packet,node_data_tiled,node_tiled_clients toneRose
class node_apps_afl,node_apps_tiled,node_jupyterlite toneIndigo
class node_launcher,node_shared_util,node_persistent_config,node_deploy,node_virtual_scripts toneTeal
```

## Components

### Core runtime

HTTP server framework and driver contract.

- [APIServer](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/APIServer.py): http server framework · Exposes Python objects over HTTP, queues work, renders responses, and supports UI generation. · `AFL/automation/APIServer/APIServer.py`
- [Driver](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/Driver.py): device contract · Base interface for concrete hardware and service adapters. · `AFL/automation/APIServer/Driver.py`
- [WebApp mixin](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/DriverWebAppsMixin.py): api/ui mixin · Adds browser-facing behaviors and searchable server actions. · `AFL/automation/APIServer/DriverWebAppsMixin.py`
- [Queue daemon](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/QueueDaemon.py): async worker · Background execution and request queue management for server tasks. · `AFL/automation/APIServer/QueueDaemon.py`

### Workflows

Higher-level automation and formulation logic.

- [Mixcalc](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/mixcalc/MassBalance.py): formulation engine · Mass-balance calculations, solution models, and formulation planning. · `AFL/automation/mixcalc/MassBalance.py`
- [Prepare](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/prepare/PrepareDriver.py): prep workflow · OT-2 and preparation-robot planning and execution helpers. · `AFL/automation/prepare/PrepareDriver.py`
- [Orchestrator](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/orchestrator/OrchestratorDriver.py): closed-loop coordinator · Coordinates multi-step workflows across devices and services. · `AFL/automation/orchestrator/OrchestratorDriver.py`

### Hardware domains

Device adapters grouped by lab function.

- [Instrument](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/instrument/ScatteringInstrument.py): measurement adapters · Scattering, spectroscopy, imaging, and beamline-facing instrument drivers. · `AFL/automation/instrument/ScatteringInstrument.py`
- [Loading](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/loading/SyringePump.py): fluid handling · Pumps, valves, sensors, relays, pressure controllers, and sample-cell hardware. · `AFL/automation/loading/SyringePump.py`
- [Manipulate](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/manipulate/XArmManipulator.py): robotics · Physical manipulation adapters such as arm-style motion control. · `AFL/automation/manipulate/XArmManipulator.py`
- [Sample env](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/sample_env/TemperatureDeck.py): environment control · Environmental control around samples, such as temperature decks. · `AFL/automation/sample_env/TemperatureDeck.py`

### Data plane

Result packaging, storage, and Tiled integration.

- [JSON data](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/data/DataJSON.py): result packaging · Structured in-memory and serialized server results. · `AFL/automation/APIServer/data/DataJSON.py`
- [Data packet](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/data/DataPacket.py): event envelope · Typed packet representation for server outputs and records. · `AFL/automation/APIServer/data/DataPacket.py`
- [Tiled data](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/data/DataTiled.py): persistence adapter · Stores or forwards experiment data into Tiled-backed storage. · `AFL/automation/APIServer/data/DataTiled.py`
- [Tiled clients](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/data/TiledClients/CatalogOfAFLEvents.py): catalog adapters · Client wrappers for event catalogs and driver-task access. · `AFL/automation/APIServer/data/TiledClients/CatalogOfAFLEvents.py`

### User interface

Browser apps and embedded JupyterLite surface.

- [AFL app](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/apps/afl_app/webapp.html): control UI · Main browser control surface for servers, queues, and status. · `AFL/automation/apps/afl_app/webapp.html`
- [Tiled browser](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/apps/tiled_browser/tiled_browser.html): data UI · Browser tooling for querying, plotting, and inspecting stored datasets. · `AFL/automation/apps/tiled_browser/tiled_browser.html`
- [JupyterLite](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/APIServer/static/jl/index.html): in-browser analysis · Embedded notebook and REPL environment served from static assets. · `AFL/automation/APIServer/static/jl/index.html`

### Shared infra

Cross-cutting utilities, launchers, and deployment.

- [Launcher](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/shared/launcher.py): startup layer · Starts server processes locally or in production. · `AFL/automation/shared/launcher.py`
- [Shared utils](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/shared/utilities.py): common library · Serialization, units, queues, configs, discovery, warnings, and helper clients. · `AFL/automation/shared/utilities.py`
- [Config store](https://github.com/usnistgov/afl-automation/blob/main/AFL/automation/shared/PersistentConfig.py): local config · Persistent user-local configuration used by virtual and deployed server setups. · `AFL/automation/shared/PersistentConfig.py`
- [Deploy](https://github.com/usnistgov/afl-automation/blob/main/docker-compose.yml): ops assets · Docker, compose, environment, and launcher assets for multi-service deployment. · `docker-compose.yml`
- [Virtual scripts](https://github.com/usnistgov/afl-automation/blob/main/server_scripts/virtual_instrument/DummySampleServer.sh): offline runtime · Standalone virtual instrument and dummy server entrypoints for software-only operation. · `server_scripts/virtual_instrument/DummySampleServer.sh`

## Connections

- Launcher → APIServer: starts · Launches HTTP server instances.
- APIServer → Driver: hosts · Wraps device implementations behind the common API.
- Driver → WebApp mixin: extends · Drivers can expose richer browser-facing actions and views.
- APIServer → Queue daemon: queues work · Delegates long-running actions and async execution.
- APIServer → Shared utils: uses · Shares serialization, discovery, config, and helper utilities.
- Shared utils → Config store: persists · Reads and writes local configuration state.
- Driver → Instrument: implements · Instrument adapters satisfy the driver contract.
- Driver → Loading: implements · Loading hardware is exposed through the same driver abstraction.
- Driver → Manipulate: implements · Robot/manipulator control is wrapped as drivers.
- Driver → Sample env: implements · Environmental hardware is exposed via drivers.
- Mixcalc → Prepare: feeds · Formulation outputs inform preparation planning.
- Prepare → Orchestrator: coordinates · Preparation steps participate in broader workflows.
- Orchestrator → Instrument: controls · Coordinates measurements and acquisitions.
- Orchestrator → Loading: controls · Coordinates pumps, valves, and sample loading.
- Orchestrator → Sample env: controls · Coordinates environmental conditions during workflows.
- APIServer → JSON data: returns · Packages responses and server results.
- APIServer → Data packet: emits · Converts actions into structured records.
- Data packet → Tiled data: stores · Persistent experiment/event storage path.
- Tiled data → Tiled clients: queries · Uses client adapters to access Tiled catalogs.
- AFL app → APIServer: calls · Control UI talks to the HTTP server APIs.
- Tiled browser → Tiled data: browses · Data browser queries persisted datasets.
- JupyterLite → Tiled data: analyzes · Notebook tooling can inspect stored data.
- Deploy → Launcher: packages · Deployment assets wire together server startup and services.
- Virtual scripts → APIServer: mirrors · Offline entrypoints preserve the same server shape without hardware.
- Virtual scripts → Instrument: simulates · Virtual instrument entrypoints stand in for real hardware.
- Virtual scripts → Loading: simulates · Dummy loaders and sample servers model loading hardware.
- Virtual scripts → Prepare: simulates · Virtual OT-2 and prep flows support offline testing.
