# nearmeter/nrfclaw architecture

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

- Interactive diagram: https://gitdiagram.com/nearmeter/nrfclaw
- Repository: https://github.com/nearmeter/nrfclaw

## Overview

nRFClaw is an embedded platform whose host-side compiler turns user-described behavior into deterministic VM bytecode, programs devices over BLE, and lets low-power firmware execute autonomously. The README documents a standalone semantic path and optional AI-agent path, plus Direct BLE/NDP and NinaLink/Home Assistant capabilities. Firmware initialization and event dispatch are partly visible in `src/main.c`; most other firmware and Home Assistant links below reflect README-documented behavior rather than inspected implementation details.

Main entry points: [`src/nrfclaw_vm.c`](https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_vm.c) (Bytecode VM), [`src/main.c`](https://github.com/nearmeter/nrfclaw/blob/main/src/main.c) (Firmware coordinator), [`NRFCLAW_CLI/nrfclaw_cli.py`](https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_cli.py) (nRFClaw CLI), [`NRFCLAW_CLI/nrfclaw_hybrid_semantic.py`](https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_hybrid_semantic.py) (Standalone semantics)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_host["Host programming"]
  node_cli["nRFClaw CLI<br/>[nrfclaw_cli.py]"]
end

subgraph group_semantic["Semantic compilation"]
  node_standalone["Standalone semantics"]
  node_agent["Optional AI agent<br/>[core.py]"]
  node_router["Agent router<br/>[router.py]"]
  node_ir["Semantic IR"]
  node_pseudo["Pseudo compiler<br/>[nrfclaw_pseudo.py]"]
  node_compiler["Text compiler"]
  node_scheduler_sem["Schedule semantics"]
end

subgraph group_device["Device runtime"]
  node_firmware["Firmware coordinator<br/>[main.c]"]
  node_vm["Bytecode VM<br/>[nrfclaw_vm.c]"]
  node_schedule["RTC scheduler"]
  node_state[("Persistent state<br/>[nrfclaw_flash.c]")]
  node_events["Event system<br/>[nrfclaw_event.c]"]
  node_sensors["Sensors and inputs<br/>[nrfclaw_lis2dh12.c]"]
end

subgraph group_radio["Connectivity"]
  node_ble["BLE programming<br/>[nrfclaw_ble_app.c]"]
  node_lora["LoRa transport<br/>[nrfclaw_lora.c]"]
  node_direct["Direct BLE/NDP<br/>[nrfclaw_ndp.c]"]
  node_ninalink["NinaLink network"]
end

subgraph group_ha["Home Assistant"]
  node_ha["Home Assistant<br/>[__init__.py]"]
  node_ha_client["HA device client<br/>[client.py]"]
  node_ha_runtime["NinaLink runtime"]
end

node_user(("Device author"))
node_radio_services(("External radios"))

node_user -->|"describes behavior"| node_cli
node_cli -->|"requests compile"| node_standalone
node_cli -->|"routes agent request"| node_router
node_router -->|"calls provider"| node_agent
node_router -->|"falls back"| node_standalone
node_standalone -.->|"produces plan"| node_ir
node_agent -->|"emits semantic plan"| node_ir
node_ir -->|"lowers to pseudo"| node_pseudo
node_pseudo -->|"assembles bytecode"| node_compiler
node_standalone -->|"lowers deterministically"| node_compiler
node_scheduler_sem -.->|"normalizes schedules"| node_compiler
node_cli -->|"programs over BLE"| node_ble
node_ble -->|"delivers program"| node_firmware
node_firmware -->|"initializes VM"| node_vm
node_firmware -->|"initializes scheduler"| node_schedule
node_firmware -->|"recovers program"| node_state
node_firmware -->|"dispatches events"| node_events
node_events -->|"notifies VM"| node_vm
node_vm -.->|"reads and controls"| node_sensors
node_vm -.->|"sends and receives"| node_lora
node_vm -.->|"uses BLE services"| node_direct
node_vm -.->|"uses schedules"| node_schedule
node_lora -->|"transmits LoRa"| node_radio_services
node_direct -->|"serves Direct BLE/NDP"| node_ha_client
node_ninalink -->|"bridges LoRa"| node_radio_services
node_ha -.->|"configures integration"| node_ha_client
node_ha -.->|"uses NinaLink"| node_ha_runtime
node_ha_runtime -.->|"communicates with nodes"| node_ninalink

click node_cli "https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_cli.py"
click node_standalone "https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_hybrid_semantic.py"
click node_agent "https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_agent/core.py"
click node_router "https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_agent/router.py"
click node_ir "https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_semantic_ir.py"
click node_pseudo "https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_pseudo.py"
click node_compiler "https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_text_compiler.py"
click node_scheduler_sem "https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_schedule_semantics.py"
click node_ble "https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_ble_app.c"
click node_firmware "https://github.com/nearmeter/nrfclaw/blob/main/src/main.c"
click node_vm "https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_vm.c"
click node_schedule "https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_scheduler.c"
click node_state "https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_flash.c"
click node_events "https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_event.c"
click node_sensors "https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_lis2dh12.c"
click node_lora "https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_lora.c"
click node_direct "https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_ndp.c"
click node_ninalink "https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_ninalink_bridge.c"
click node_ha "https://github.com/nearmeter/nrfclaw/blob/main/integrations/home-assistant/custom_components/nrfclaw/__init__.py"
click node_ha_client "https://github.com/nearmeter/nrfclaw/blob/main/integrations/home-assistant/custom_components/nrfclaw/client.py"
click node_ha_runtime "https://github.com/nearmeter/nrfclaw/blob/main/integrations/home-assistant/custom_components/nrfclaw/ninalink_runtime.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_cli toneBlue
class node_standalone,node_agent,node_router,node_ir,node_pseudo,node_compiler,node_scheduler_sem toneAmber
class node_firmware,node_vm,node_schedule,node_state,node_events,node_sensors toneMint
class node_ble,node_lora,node_direct,node_ninalink toneRose
class node_ha,node_ha_client,node_ha_runtime,node_user,node_radio_services toneIndigo
```

## Components

### Host programming

- [nRFClaw CLI](https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_cli.py): `NRFCLAW_CLI/nrfclaw_cli.py`

### Semantic compilation

- [Standalone semantics](https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_hybrid_semantic.py): `NRFCLAW_CLI/nrfclaw_hybrid_semantic.py`
- [Optional AI agent](https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_agent/core.py): `NRFCLAW_CLI/nrfclaw_agent/core.py`
- [Agent router](https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_agent/router.py): `NRFCLAW_CLI/nrfclaw_agent/router.py`
- [Semantic IR](https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_semantic_ir.py): `NRFCLAW_CLI/nrfclaw_semantic_ir.py`
- [Pseudo compiler](https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_pseudo.py): `NRFCLAW_CLI/nrfclaw_pseudo.py`
- [Text compiler](https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_text_compiler.py): `NRFCLAW_CLI/nrfclaw_text_compiler.py`
- [Schedule semantics](https://github.com/nearmeter/nrfclaw/blob/main/NRFCLAW_CLI/nrfclaw_schedule_semantics.py): `NRFCLAW_CLI/nrfclaw_schedule_semantics.py`

### Device runtime

- [Firmware coordinator](https://github.com/nearmeter/nrfclaw/blob/main/src/main.c): `src/main.c`
- [Bytecode VM](https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_vm.c): `src/nrfclaw_vm.c`
- [RTC scheduler](https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_scheduler.c): `src/nrfclaw_scheduler.c`
- [Persistent state](https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_flash.c): `src/nrfclaw_flash.c`
- [Event system](https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_event.c): `src/nrfclaw_event.c`
- [Sensors and inputs](https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_lis2dh12.c): `src/nrfclaw_lis2dh12.c`

### Connectivity

- [BLE programming](https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_ble_app.c): `src/nrfclaw_ble_app.c`
- [LoRa transport](https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_lora.c): `src/nrfclaw_lora.c`
- [Direct BLE/NDP](https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_ndp.c): `src/nrfclaw_ndp.c`
- [NinaLink network](https://github.com/nearmeter/nrfclaw/blob/main/src/nrfclaw_ninalink_bridge.c): `src/nrfclaw_ninalink_bridge.c`

### Home Assistant

- [Home Assistant](https://github.com/nearmeter/nrfclaw/blob/main/integrations/home-assistant/custom_components/nrfclaw/__init__.py): `integrations/home-assistant/custom_components/nrfclaw/__init__.py`
- [HA device client](https://github.com/nearmeter/nrfclaw/blob/main/integrations/home-assistant/custom_components/nrfclaw/client.py): `integrations/home-assistant/custom_components/nrfclaw/client.py`
- [NinaLink runtime](https://github.com/nearmeter/nrfclaw/blob/main/integrations/home-assistant/custom_components/nrfclaw/ninalink_runtime.py): `integrations/home-assistant/custom_components/nrfclaw/ninalink_runtime.py`

### Other components

- **Device author**
- **External radios**

## Connections

- Device author → nRFClaw CLI: describes behavior
- nRFClaw CLI → Standalone semantics: requests compile (evidence: `NRFCLAW_CLI/nrfclaw_cli.py`)
- nRFClaw CLI → Agent router: routes agent request (evidence: `NRFCLAW_CLI/nrfclaw_cli.py`)
- Agent router → Optional AI agent: calls provider (evidence: `NRFCLAW_CLI/nrfclaw_agent/router.py`)
- Agent router → Standalone semantics: falls back (evidence: `NRFCLAW_CLI/nrfclaw_agent/router.py`)
- Standalone semantics → Semantic IR: produces plan
- Optional AI agent → Semantic IR: emits semantic plan (evidence: `NRFCLAW_CLI/nrfclaw_agent/core.py`)
- Semantic IR → Pseudo compiler: lowers to pseudo (evidence: `NRFCLAW_CLI/nrfclaw_semantic_ir.py`)
- Pseudo compiler → Text compiler: assembles bytecode (evidence: `NRFCLAW_CLI/nrfclaw_pseudo.py`)
- Standalone semantics → Text compiler: lowers deterministically (evidence: `NRFCLAW_CLI/nrfclaw_hybrid_semantic.py`)
- Schedule semantics → Text compiler: normalizes schedules
- nRFClaw CLI → BLE programming: programs over BLE
- BLE programming → Firmware coordinator: delivers program
- Firmware coordinator → Bytecode VM: initializes VM (evidence: `src/main.c`)
- Firmware coordinator → RTC scheduler: initializes scheduler (evidence: `src/main.c`)
- Firmware coordinator → Persistent state: recovers program (evidence: `src/main.c`)
- Firmware coordinator → Event system: dispatches events (evidence: `src/main.c`)
- Event system → Bytecode VM: notifies VM
- Bytecode VM → Sensors and inputs: reads and controls
- Bytecode VM → LoRa transport: sends and receives
- Bytecode VM → Direct BLE/NDP: uses BLE services
- Bytecode VM → RTC scheduler: uses schedules
- LoRa transport → External radios: transmits LoRa
- Direct BLE/NDP → HA device client: serves Direct BLE/NDP
- NinaLink network → External radios: bridges LoRa
- Home Assistant → HA device client: configures integration
- Home Assistant → NinaLink runtime: uses NinaLink
- NinaLink runtime → NinaLink network: communicates with nodes
