# naveenkai/agentml architecture

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

- Interactive diagram: https://gitdiagram.com/naveenkai/agentml
- Repository: https://github.com/naveenkai/agentml

## Overview

#### Purpose and Entry Points
AgentML is a local-first AutoML assistant: users upload CSV data or start a dataset-free ML chat; LLM agents generate and execute Python, stream results, and optionally run an approval-gated ML pipeline. Backend startup is `backend/main.py`; the React root is `src/index.js` → `src/App.js`. The primary interactive boundary is WebSocket streaming at `backend/api/routes/ws.py`, with HTTP for upload, sessions, settings, and model discovery.

#### Browser Client and State
The React frontend renders chat, pipeline progress, session navigation, and rich execution results. `src/store/chatStore.js` is the client-side source of truth for sessions, messages, WebSocket events, streaming state, and HITL actions. `src/api/client.js` centralizes Axios and WebSocket connectivity; `src/components/chat/ChatLayout.jsx` and `src/components/pipeline/PipelineProgress.jsx` project that state into the UI.

#### API, Session, and Persistence Boundary
FastAPI composes CORS, lifecycle, rate limiting, and route registration in `backend/main.py`. Upload endpoints validate and ingest CSVs in `backend/api/routes/upload.py`; WebSocket requests drive runs in `backend/api/routes/ws.py`. Active session lifecycle, per-session concurrency, expiry, and execution availability are managed in `backend/utils/session_manager.py`; durable session/message/experiment history is stored locally through SQLite in `backend/db/database.py` and `backend/db/models.py`.

#### Agent Graph and Control Flow
`backend/agent/driver.py` is the streaming execution entry point: it constructs run context, coordinates session locking, invokes the graph, and relays events. `backend/agent/pipeline.py` defines the LangGraph topology and transitions. A deterministic entry router selects general chat versus autonomous pipeline behavior; pipeline control proceeds through preprocessing, EDA, feature engineering, modeling, evaluation, plus a Hungry quality-review gate. `backend/agent/routing.py` provides ordered and backward stage navigation, while `backend/agent/hitl.py` pauses stage transitions for approve/modify/reject decisions and timeout fallback.

#### LLM, Prompts, and Structured Output
Agent nodes share LLM acquisition, retries, parsing, and execution conventions in `backend/agent/common.py`, `backend/agent/llm_utils.py`, and `backend/agent/parser.py`. Stage-specific prompts reside under `backend/agent/prompts/`, including pipeline prompts and modeling/debate specialists. Provider selection is configured in `backend/config.py`: Ollama is local by default; OpenAI and Anthropic are opt-in external services. Agent output is converted into typed decisions and streamable message parts through `backend/agent/schemas_llm.py` and `backend/agent/events.py`.

#### Code Execution and Recovery
The sole agent tool is `backend/agent/tools.py`, which validates generated Python then sends it to a persistent, session-scoped Jupyter kernel in `backend/executor/notebook.py`. The kernel runs in a child process via ZMQ, isolating kernel crashes from FastAPI. `backend/agent/ledger.py` tracks variables and execution state outside the kernel to aid recovery; plots and tables become rich client events.

#### Security and Operating Constraints
The intended runtime is a single trusted user on localhost, without authentication. `backend/guardrails/code_validator.py` applies AST denylist checks before kernel execution, but is explicitly not a sandbox. `backend/guardrails/prompt_sanitizer.py` neutralizes agent-protocol tags in user/data input, and `backend/guardrails/rate_limiter.py` protects HTTP and WebSocket routes. Deployment beyond loopback requires external authentication, OS-level isolation, and secrets management.

Main entry points: [`backend/agent/pipeline.py`](https://github.com/naveenkai/agentml/blob/main/backend/agent/pipeline.py) (LangGraph pipeline), [`backend/main.py`](https://github.com/naveenkai/agentml/blob/main/backend/main.py) (FastAPI application), [`backend/api/routes/ws.py`](https://github.com/naveenkai/agentml/blob/main/backend/api/routes/ws.py) (Streaming run route), [`backend/agent/driver.py`](https://github.com/naveenkai/agentml/blob/main/backend/agent/driver.py) (Streaming driver)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Browser client"]
  node_react_app["React app<br/>frontend root<br/>[App.js]"]
  node_chat_store["Chat state store<br/>client state<br/>[chatStore.js]"]
  node_api_client["API client<br/>Axios + WebSocket<br/>[client.js]"]
  node_chat_ui["Chat and pipeline UI<br/>React views<br/>[ChatLayout.jsx]"]
end

subgraph group_api["FastAPI boundary"]
  node_fastapi["FastAPI application<br/>backend entry<br/>[main.py]"]
  node_websocket_route["Streaming run route<br/>WebSocket API<br/>[ws.py]"]
  node_upload_route["CSV upload route<br/>HTTP API<br/>[upload.py]"]
  node_session_manager["Session manager<br/>runtime sessions<br/>[session_manager.py]"]
  node_sqlite_history[("SQLite history<br/>durable persistence<br/>[database.py]")]
end

subgraph group_agents["Agent orchestration"]
  node_driver["Streaming driver<br/>run coordinator<br/>[driver.py]"]
  node_agent_graph["LangGraph pipeline<br/>agent topology<br/>[pipeline.py]"]
  node_stage_control["Stage navigation and HITL<br/>pipeline control<br/>[hitl.py]"]
  node_llm_runtime["LLM and structured output<br/>agent runtime<br/>[llm_utils.py]"]
  node_prompts["Stage prompts<br/>prompt library<br/>[base.py]"]
end

subgraph group_execution["Execution and safety"]
  node_agent_tool["Python execution tool<br/>agent tool<br/>[tools.py]"]
  node_code_validator["AST code validator<br/>guardrail<br/>[code_validator.py]"]
  node_notebook_kernel{{"Session Jupyter kernel<br/>child-process executor<br/>[notebook.py]"}}
  node_execution_ledger["Execution ledger<br/>recovery state<br/>[ledger.py]"]
  node_input_safety["Input protections<br/>route guardrails"]
end

node_react_app -->|"initializes"| node_chat_store
node_chat_store -->|"connects and sends"| node_api_client
node_chat_store -->|"projects state"| node_chat_ui
node_api_client -->|"HTTP"| node_fastapi
node_api_client -->|"streaming WebSocket"| node_websocket_route
node_fastapi -->|"registers"| node_upload_route
node_fastapi -->|"registers"| node_websocket_route
node_upload_route -->|"ingests session data"| node_session_manager
node_websocket_route -->|"starts run"| node_driver
node_websocket_route -->|"checks lifecycle"| node_session_manager
node_session_manager -->|"persists history"| node_sqlite_history
node_driver -->|"invokes"| node_agent_graph
node_driver -->|"acquires session lock"| node_session_manager
node_agent_graph -->|"stage transitions and approval"| node_stage_control
node_agent_graph -->|"agent decisions"| node_llm_runtime
node_llm_runtime -->|"uses"| node_prompts
node_agent_graph -->|"generated Python"| node_agent_tool
node_agent_tool -->|"validates first"| node_code_validator
node_code_validator -->|"approved code"| node_notebook_kernel
node_notebook_kernel -->|"execution state"| node_execution_ledger
node_notebook_kernel -->|"results, plots, tables"| node_driver
node_input_safety -.->|"sanitization and rate limits"| node_fastapi

click node_react_app "https://github.com/naveenkai/agentml/blob/main/src/App.js"
click node_chat_store "https://github.com/naveenkai/agentml/blob/main/src/store/chatStore.js"
click node_api_client "https://github.com/naveenkai/agentml/blob/main/src/api/client.js"
click node_chat_ui "https://github.com/naveenkai/agentml/blob/main/src/components/chat/ChatLayout.jsx"
click node_fastapi "https://github.com/naveenkai/agentml/blob/main/backend/main.py"
click node_websocket_route "https://github.com/naveenkai/agentml/blob/main/backend/api/routes/ws.py"
click node_upload_route "https://github.com/naveenkai/agentml/blob/main/backend/api/routes/upload.py"
click node_session_manager "https://github.com/naveenkai/agentml/blob/main/backend/utils/session_manager.py"
click node_sqlite_history "https://github.com/naveenkai/agentml/blob/main/backend/db/database.py"
click node_driver "https://github.com/naveenkai/agentml/blob/main/backend/agent/driver.py"
click node_agent_graph "https://github.com/naveenkai/agentml/blob/main/backend/agent/pipeline.py"
click node_stage_control "https://github.com/naveenkai/agentml/blob/main/backend/agent/hitl.py"
click node_llm_runtime "https://github.com/naveenkai/agentml/blob/main/backend/agent/llm_utils.py"
click node_prompts "https://github.com/naveenkai/agentml/blob/main/backend/agent/prompts/base.py"
click node_agent_tool "https://github.com/naveenkai/agentml/blob/main/backend/agent/tools.py"
click node_code_validator "https://github.com/naveenkai/agentml/blob/main/backend/guardrails/code_validator.py"
click node_notebook_kernel "https://github.com/naveenkai/agentml/blob/main/backend/executor/notebook.py"
click node_execution_ledger "https://github.com/naveenkai/agentml/blob/main/backend/agent/ledger.py"
click node_input_safety "https://github.com/naveenkai/agentml/blob/main/backend/guardrails/prompt_sanitizer.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_react_app,node_chat_store,node_api_client,node_chat_ui toneBlue
class node_fastapi,node_websocket_route,node_upload_route,node_session_manager,node_sqlite_history toneAmber
class node_driver,node_agent_graph,node_stage_control,node_llm_runtime,node_prompts toneMint
class node_agent_tool,node_code_validator,node_notebook_kernel,node_execution_ledger,node_input_safety toneRose
```

## Components

### Browser client

- [React app](https://github.com/naveenkai/agentml/blob/main/src/App.js): frontend root · `src/App.js`
- [Chat state store](https://github.com/naveenkai/agentml/blob/main/src/store/chatStore.js): client state · Owns sessions, messages, stream state, and HITL actions. · `src/store/chatStore.js`
- [API client](https://github.com/naveenkai/agentml/blob/main/src/api/client.js): Axios + WebSocket · `src/api/client.js`
- [Chat and pipeline UI](https://github.com/naveenkai/agentml/blob/main/src/components/chat/ChatLayout.jsx): React views · `src/components/chat/ChatLayout.jsx`

### FastAPI boundary

- [FastAPI application](https://github.com/naveenkai/agentml/blob/main/backend/main.py): backend entry · Composes lifecycle, CORS, rate limiting, and routes. · `backend/main.py`
- [Streaming run route](https://github.com/naveenkai/agentml/blob/main/backend/api/routes/ws.py): WebSocket API · `backend/api/routes/ws.py`
- [CSV upload route](https://github.com/naveenkai/agentml/blob/main/backend/api/routes/upload.py): HTTP API · `backend/api/routes/upload.py`
- [Session manager](https://github.com/naveenkai/agentml/blob/main/backend/utils/session_manager.py): runtime sessions · Controls per-session locking, expiry, and execution availability. · `backend/utils/session_manager.py`
- [SQLite history](https://github.com/naveenkai/agentml/blob/main/backend/db/database.py): durable persistence · Stores sessions, messages, and experiments locally. · `backend/db/database.py`

### Agent orchestration

- [Streaming driver](https://github.com/naveenkai/agentml/blob/main/backend/agent/driver.py): run coordinator · Builds run context, invokes the graph, and relays events. · `backend/agent/driver.py`
- [LangGraph pipeline](https://github.com/naveenkai/agentml/blob/main/backend/agent/pipeline.py): agent topology · Routes general chat or autonomous ML stages. · `backend/agent/pipeline.py`
- [Stage navigation and HITL](https://github.com/naveenkai/agentml/blob/main/backend/agent/hitl.py): pipeline control · Navigates stages and pauses for approve, modify, reject, or timeout. · `backend/agent/hitl.py`
- [LLM and structured output](https://github.com/naveenkai/agentml/blob/main/backend/agent/llm_utils.py): agent runtime · Acquires providers, parses outputs, and emits typed stream events. · `backend/agent/llm_utils.py`
- [Stage prompts](https://github.com/naveenkai/agentml/blob/main/backend/agent/prompts/base.py): prompt library · Pipeline and modeling/debate specialist instructions. · `backend/agent/prompts/base.py`

### Execution and safety

- [Python execution tool](https://github.com/naveenkai/agentml/blob/main/backend/agent/tools.py): agent tool · Validates generated Python and dispatches it to the session kernel. · `backend/agent/tools.py`
- [AST code validator](https://github.com/naveenkai/agentml/blob/main/backend/guardrails/code_validator.py): guardrail · Denylist validation; not a security sandbox. · `backend/guardrails/code_validator.py`
- [Session Jupyter kernel](https://github.com/naveenkai/agentml/blob/main/backend/executor/notebook.py): child-process executor · Persistent ZMQ kernel isolates crashes from FastAPI. · `backend/executor/notebook.py`
- [Execution ledger](https://github.com/naveenkai/agentml/blob/main/backend/agent/ledger.py): recovery state · Tracks variables and execution state outside the kernel. · `backend/agent/ledger.py`
- [Input protections](https://github.com/naveenkai/agentml/blob/main/backend/guardrails/prompt_sanitizer.py): route guardrails · Sanitizes protocol tags and rate-limits HTTP and WebSocket traffic. · `backend/guardrails/prompt_sanitizer.py`

## Connections

- React app → Chat state store: initializes
- Chat state store → API client: connects and sends
- Chat state store → Chat and pipeline UI: projects state
- API client → FastAPI application: HTTP
- API client → Streaming run route: streaming WebSocket
- FastAPI application → CSV upload route: registers
- FastAPI application → Streaming run route: registers
- CSV upload route → Session manager: ingests session data
- Streaming run route → Streaming driver: starts run
- Streaming run route → Session manager: checks lifecycle
- Session manager → SQLite history: persists history
- Streaming driver → LangGraph pipeline: invokes
- Streaming driver → Session manager: acquires session lock
- LangGraph pipeline → Stage navigation and HITL: stage transitions and approval
- LangGraph pipeline → LLM and structured output: agent decisions
- LLM and structured output → Stage prompts: uses
- LangGraph pipeline → Python execution tool: generated Python
- Python execution tool → AST code validator: validates first
- AST code validator → Session Jupyter kernel: approved code
- Session Jupyter kernel → Execution ledger: execution state
- Session Jupyter kernel → Streaming driver: results, plots, tables
- Input protections → FastAPI application: sanitization and rate limits
