# 0x-nan/nexus architecture

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

- Interactive diagram: https://gitdiagram.com/0x-nan/nexus
- Repository: https://github.com/0x-nan/nexus

## Overview

#### Purpose and Entry Points
Nexus is a self-hosted, real-time governance control plane for AI agents that initiate payments. It evaluates transaction requests inline, records every decision, can globally revoke activity, and streams events to an operator dashboard.

Primary runtime entry points are FastAPI in `backend/app/main.py`, the React client in `frontend/src/main.jsx`, and local multi-service orchestration in `docker-compose.yml`.

#### API and Control Plane
FastAPI routers expose the external governance surface: agent registration/listing, transaction evaluation and audit export, kill-switch control, and simulation controls. The API is deliberately agent-framework-agnostic: any caller that can submit a transaction can be governed.

Core route boundaries are in:
- `backend/app/routers/transactions.py`
- `backend/app/routers/agents.py`
- `backend/app/routers/kill_switch.py`

Authentication-specific endpoints and service logic are separated in `backend/app/routers/auth.py` and `backend/app/services/auth.py`.

#### Policy Evaluation Pipeline
Transaction evaluation is the central synchronous control flow. A request passes through configured checks in order: global kill-switch state, agent scope/category, spend-cap thresholds, and burst/rate detection. The first denial wins; flags are recorded without halting subsequent checks.

The policy engine owns this enforcement boundary, while deployment-time YAML defines permitted agent behavior and rules:
- `backend/app/services/policy_engine.py`
- `backend/config/policy_rules.yaml`
- `backend/config/agents.yaml`

Rules are loaded at startup rather than edited through an API, preserving version-controlled policy ownership.

#### Persistent Audit and State
PostgreSQL is the authoritative persistence layer for agents, evaluated transactions, decision reasons, and operational state. The transaction logger provides the audit-writing boundary; schema and initialization data establish append-oriented storage and initial fleet configuration.

Relevant paths:
- `backend/app/database.py`
- `backend/app/services/transaction_logger.py`
- `backend/db/schema.sql`

The backend uses async PostgreSQL access; migrations under `backend/db/migrations/` evolve schema behavior.

#### Kill Switch and Fleet Revocation
The kill switch is a dedicated service and API concern rather than a dashboard-only state. Its state is consulted during every policy evaluation, allowing fleet-wide denial before downstream policy checks. State changes are also audit-worthy events.

Relevant paths:
- `backend/app/services/kill_switch.py`
- `backend/app/routers/kill_switch.py`
- `backend/app/services/policy_engine.py`

#### Real-Time Event Distribution and Dashboard
After decisions/state changes are persisted, the backend broadcasts server-push updates over WebSockets. The frontend consumes these updates to render live agent status, audit activity, controls, and developer-oriented views; it is not the enforcement authority.

Relevant paths:
- `backend/app/ws_manager.py`
- `frontend/src/App.jsx`
- `frontend/src/components/DashboardLayout.jsx`

Client session/auth state is centralized at `frontend/src/context/AuthContext.jsx`.

#### Simulation and Optional LLM Traffic
A built-in simulator produces ambient agent transactions and can inject overspend, off-scope, or burst behavior to exercise the same public enforcement path. The Travel Agent can optionally use a local Ollama endpoint, with deterministic fallback behavior when unavailable.

Relevant paths:
- `backend/app/simulator/engine.py`
- `backend/app/simulator/llm_agent.py`
- `backend/app/routers/simulator.py`

#### Deployment and External Dependencies
Docker Compose coordinates frontend, FastAPI backend, PostgreSQL 17, and optional locally reachable Ollama. Frontend production serving is containerized behind Nginx; Prometheus configuration exists for metrics scraping.

Relevant paths:
- `docker-compose.yml`
- `backend/Dockerfile`
- `frontend/Dockerfile`
- `prometheus/prometheus.yml`

Main entry points: [`backend/app/main.py`](https://github.com/0x-nan/nexus/blob/main/backend/app/main.py) (FastAPI control plane), [`backend/app/services/policy_engine.py`](https://github.com/0x-nan/nexus/blob/main/backend/app/services/policy_engine.py) (Policy engine), [`frontend/src/App.jsx`](https://github.com/0x-nan/nexus/blob/main/frontend/src/App.jsx) (React dashboard), [`backend/app/services/kill_switch.py`](https://github.com/0x-nan/nexus/blob/main/backend/app/services/kill_switch.py) (Fleet kill switch)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Operator UI"]
  node_frontend["React dashboard<br/>frontend<br/>[App.jsx]"]
  node_dashboard_layout["Dashboard layout<br/>UI shell"]
  node_auth_context["Auth context<br/>client session state<br/>[AuthContext.jsx]"]
end

subgraph group_api["Governance API"]
  node_api_app["FastAPI control plane<br/>API runtime<br/>[main.py]"]
  node_transaction_api["Transaction API<br/>router<br/>[transactions.py]"]
  node_agent_api["Agent API<br/>router<br/>[agents.py]"]
  node_kill_switch_api["Kill-switch API<br/>router<br/>[kill_switch.py]"]
  node_auth_service["Authentication<br/>router and service<br/>[auth.py]"]
  node_simulator_api["Simulator API<br/>router<br/>[simulator.py]"]
end

subgraph group_enforcement["Enforcement services"]
  node_policy_engine["Policy engine<br/>synchronous evaluator<br/>[policy_engine.py]"]
  node_policy_rules["Versioned policy rules<br/>YAML policy<br/>[policy_rules.yaml]"]
  node_agent_config["Agent fleet config<br/>YAML configuration<br/>[agents.yaml]"]
  node_kill_switch["Fleet kill switch<br/>revocation service<br/>[kill_switch.py]"]
  node_simulator["Agent simulator<br/>traffic generator<br/>[engine.py]"]
  node_llm_agent["Travel LLM agent<br/>optional agent behavior<br/>[llm_agent.py]"]
end

subgraph group_state["State and events"]
  node_transaction_logger["Transaction logger<br/>audit writer"]
  node_websocket_manager["WebSocket broadcaster<br/>event distribution<br/>[ws_manager.py]"]
  node_postgres[("PostgreSQL<br/>authoritative store<br/>[database.py]")]
  node_schema["Audit schema<br/>database schema<br/>[schema.sql]"]
end

subgraph group_runtime["Runtime dependencies"]
  node_compose{{"Docker Compose<br/>orchestration<br/>[docker-compose.yml]"}}
  node_ollama["Ollama<br/>optional local LLM"]
end

node_compose -->|"runs"| node_frontend
node_compose -->|"runs"| node_api_app
node_compose -->|"runs"| node_postgres
node_frontend -->|"renders"| node_dashboard_layout
node_frontend -->|"uses"| node_auth_context
node_frontend -->|"API requests"| node_api_app
node_websocket_manager -->|"live updates"| node_frontend
node_api_app -->|"mounts"| node_transaction_api
node_api_app -->|"mounts"| node_agent_api
node_api_app -->|"mounts"| node_kill_switch_api
node_api_app -->|"authenticates via"| node_auth_service
node_api_app -->|"mounts"| node_simulator_api
node_transaction_api -->|"evaluates transaction"| node_policy_engine
node_policy_engine -->|"checks first"| node_kill_switch
node_policy_engine -->|"loads at startup"| node_policy_rules
node_policy_engine -->|"loads at startup"| node_agent_config
node_policy_engine -->|"records decision"| node_transaction_logger
node_kill_switch_api -->|"changes state"| node_kill_switch
node_kill_switch -->|"records state change"| node_transaction_logger
node_agent_api -->|"manages agents"| node_postgres
node_transaction_logger -->|"writes audit"| node_postgres
node_schema -->|"defines"| node_postgres
node_transaction_logger -->|"broadcasts persisted decisions"| node_websocket_manager
node_kill_switch -->|"broadcasts state changes"| node_websocket_manager
node_simulator_api -->|"controls"| node_simulator
node_simulator -->|"submits transactions"| node_transaction_api
node_simulator -->|"uses travel behavior"| node_llm_agent
node_llm_agent -.->|"optional inference"| node_ollama

click node_compose "https://github.com/0x-nan/nexus/blob/main/docker-compose.yml"
click node_frontend "https://github.com/0x-nan/nexus/blob/main/frontend/src/App.jsx"
click node_dashboard_layout "https://github.com/0x-nan/nexus/blob/main/frontend/src/components/DashboardLayout.jsx"
click node_auth_context "https://github.com/0x-nan/nexus/blob/main/frontend/src/context/AuthContext.jsx"
click node_api_app "https://github.com/0x-nan/nexus/blob/main/backend/app/main.py"
click node_transaction_api "https://github.com/0x-nan/nexus/blob/main/backend/app/routers/transactions.py"
click node_agent_api "https://github.com/0x-nan/nexus/blob/main/backend/app/routers/agents.py"
click node_kill_switch_api "https://github.com/0x-nan/nexus/blob/main/backend/app/routers/kill_switch.py"
click node_auth_service "https://github.com/0x-nan/nexus/blob/main/backend/app/services/auth.py"
click node_simulator_api "https://github.com/0x-nan/nexus/blob/main/backend/app/routers/simulator.py"
click node_policy_engine "https://github.com/0x-nan/nexus/blob/main/backend/app/services/policy_engine.py"
click node_policy_rules "https://github.com/0x-nan/nexus/blob/main/backend/config/policy_rules.yaml"
click node_agent_config "https://github.com/0x-nan/nexus/blob/main/backend/config/agents.yaml"
click node_kill_switch "https://github.com/0x-nan/nexus/blob/main/backend/app/services/kill_switch.py"
click node_transaction_logger "https://github.com/0x-nan/nexus/blob/main/backend/app/services/transaction_logger.py"
click node_websocket_manager "https://github.com/0x-nan/nexus/blob/main/backend/app/ws_manager.py"
click node_postgres "https://github.com/0x-nan/nexus/blob/main/backend/app/database.py"
click node_schema "https://github.com/0x-nan/nexus/blob/main/backend/db/schema.sql"
click node_simulator "https://github.com/0x-nan/nexus/blob/main/backend/app/simulator/engine.py"
click node_llm_agent "https://github.com/0x-nan/nexus/blob/main/backend/app/simulator/llm_agent.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_frontend,node_dashboard_layout,node_auth_context toneBlue
class node_api_app,node_transaction_api,node_agent_api,node_kill_switch_api,node_auth_service,node_simulator_api toneAmber
class node_policy_engine,node_policy_rules,node_agent_config,node_kill_switch,node_simulator,node_llm_agent toneMint
class node_transaction_logger,node_websocket_manager,node_postgres,node_schema toneRose
class node_compose,node_ollama toneIndigo
```

## Components

### Operator UI

- [React dashboard](https://github.com/0x-nan/nexus/blob/main/frontend/src/App.jsx): frontend · Operator-facing live governance console. · `frontend/src/App.jsx`
- [Dashboard layout](https://github.com/0x-nan/nexus/blob/main/frontend/src/components/DashboardLayout.jsx): UI shell · `frontend/src/components/DashboardLayout.jsx`
- [Auth context](https://github.com/0x-nan/nexus/blob/main/frontend/src/context/AuthContext.jsx): client session state · `frontend/src/context/AuthContext.jsx`

### Governance API

- [FastAPI control plane](https://github.com/0x-nan/nexus/blob/main/backend/app/main.py): API runtime · Framework-agnostic external governance surface. · `backend/app/main.py`
- [Transaction API](https://github.com/0x-nan/nexus/blob/main/backend/app/routers/transactions.py): router · `backend/app/routers/transactions.py`
- [Agent API](https://github.com/0x-nan/nexus/blob/main/backend/app/routers/agents.py): router · `backend/app/routers/agents.py`
- [Kill-switch API](https://github.com/0x-nan/nexus/blob/main/backend/app/routers/kill_switch.py): router · `backend/app/routers/kill_switch.py`
- [Authentication](https://github.com/0x-nan/nexus/blob/main/backend/app/services/auth.py): router and service · `backend/app/services/auth.py`
- [Simulator API](https://github.com/0x-nan/nexus/blob/main/backend/app/routers/simulator.py): router · `backend/app/routers/simulator.py`

### Enforcement services

- [Policy engine](https://github.com/0x-nan/nexus/blob/main/backend/app/services/policy_engine.py): synchronous evaluator · First denial wins; later checks may still record flags. · `backend/app/services/policy_engine.py`
- [Versioned policy rules](https://github.com/0x-nan/nexus/blob/main/backend/config/policy_rules.yaml): YAML policy · `backend/config/policy_rules.yaml`
- [Agent fleet config](https://github.com/0x-nan/nexus/blob/main/backend/config/agents.yaml): YAML configuration · `backend/config/agents.yaml`
- [Fleet kill switch](https://github.com/0x-nan/nexus/blob/main/backend/app/services/kill_switch.py): revocation service · Globally denies activity before downstream checks. · `backend/app/services/kill_switch.py`
- [Agent simulator](https://github.com/0x-nan/nexus/blob/main/backend/app/simulator/engine.py): traffic generator · Exercises the same public transaction enforcement path. · `backend/app/simulator/engine.py`
- [Travel LLM agent](https://github.com/0x-nan/nexus/blob/main/backend/app/simulator/llm_agent.py): optional agent behavior · Uses Ollama when reachable, otherwise deterministic fallback. · `backend/app/simulator/llm_agent.py`

### State and events

- [Transaction logger](https://github.com/0x-nan/nexus/blob/main/backend/app/services/transaction_logger.py): audit writer · `backend/app/services/transaction_logger.py`
- [WebSocket broadcaster](https://github.com/0x-nan/nexus/blob/main/backend/app/ws_manager.py): event distribution · `backend/app/ws_manager.py`
- [PostgreSQL](https://github.com/0x-nan/nexus/blob/main/backend/app/database.py): authoritative store · Stores agents, decisions, reasons, and operational state. · `backend/app/database.py`
- [Audit schema](https://github.com/0x-nan/nexus/blob/main/backend/db/schema.sql): database schema · Append-oriented storage and initial fleet data. · `backend/db/schema.sql`

### Runtime dependencies

- [Docker Compose](https://github.com/0x-nan/nexus/blob/main/docker-compose.yml): orchestration · Coordinates the local service stack. · `docker-compose.yml`
- **Ollama**: optional local LLM

## Connections

- Docker Compose → React dashboard: runs
- Docker Compose → FastAPI control plane: runs
- Docker Compose → PostgreSQL: runs
- React dashboard → Dashboard layout: renders
- React dashboard → Auth context: uses
- React dashboard → FastAPI control plane: API requests
- WebSocket broadcaster → React dashboard: live updates
- FastAPI control plane → Transaction API: mounts
- FastAPI control plane → Agent API: mounts
- FastAPI control plane → Kill-switch API: mounts
- FastAPI control plane → Authentication: authenticates via
- FastAPI control plane → Simulator API: mounts
- Transaction API → Policy engine: evaluates transaction
- Policy engine → Fleet kill switch: checks first
- Policy engine → Versioned policy rules: loads at startup
- Policy engine → Agent fleet config: loads at startup
- Policy engine → Transaction logger: records decision
- Kill-switch API → Fleet kill switch: changes state
- Fleet kill switch → Transaction logger: records state change
- Agent API → PostgreSQL: manages agents
- Transaction logger → PostgreSQL: writes audit
- Audit schema → PostgreSQL: defines
- Transaction logger → WebSocket broadcaster: broadcasts persisted decisions
- Fleet kill switch → WebSocket broadcaster: broadcasts state changes
- Simulator API → Agent simulator: controls
- Agent simulator → Transaction API: submits transactions
- Agent simulator → Travel LLM agent: uses travel behavior
- Travel LLM agent → Ollama: optional inference
