Skip to content

Commit 881fa9f

Browse files
committed
refactor: reorganize documentation structure and enhance navigation
- Updated mkdocs.yml to streamline the navigation structure, consolidating sections for clarity and ease of access. - Introduced new documentation files for Getting Started, Architecture, and API Overview, providing comprehensive guides for users. - Enhanced README.md to better reflect the system's capabilities and architecture, including a clearer description of the NL2SQL engine's features. - Added detailed specifications for pipeline nodes and agent architecture, improving the overall documentation quality and usability.
1 parent 9468b95 commit 881fa9f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1054
-1653
lines changed

README.md

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,88 @@
1-
# Enterprise NL2SQL Engine
1+
# NL2SQL Engine
22

3-
> **A Production-Grade Natural Language to SQL Engine built on the principles of Zero Trust and Deterministic Execution.**
3+
> **Production-grade Natural Language SQL runtime with deterministic orchestration.**
44
5-
This platform treats "Text-to-SQL" not as a prompt engineering problem, but as a **Distributed Systems** problem. It replaces fragile one-shot generation with a robust, compiled pipeline that bridges the gap between Unstructured Intention (User Language) and Structured Execution (SQL Databases).
5+
NL2SQL treats text-to-SQL as a **distributed systems** problem. The engine compiles a user query into a validated plan, executes via adapters, and aggregates results through a graph-based pipeline.
66

77
---
88

9+
## 🧭 What you get
10+
11+
- Graph-based orchestration (`LangGraph`) with explicit state (`GraphState`)
12+
- Deterministic planning and validation before SQL generation
13+
- Adapter-based execution with sandbox isolation
14+
- Observability hooks (metrics, logs, audit events)
15+
916
## 🏗️ System Topology
1017

11-
The architecture is composed of three distinct planes, ensuring separation of concerns and failure isolation.
18+
The runtime is organized around a LangGraph orchestration pipeline and supporting registries. It is designed for fault isolation and deterministic execution.
19+
20+
```mermaid
21+
flowchart TD
22+
User[User Query] --> Resolver[DatasourceResolverNode]
23+
Resolver --> Decomposer[DecomposerNode]
24+
Decomposer --> Planner[GlobalPlannerNode]
25+
Planner --> Router[Layer Router]
26+
27+
subgraph SQLAgent["SQL Agent Subgraph"]
28+
Schema[SchemaRetrieverNode] --> AST[ASTPlannerNode]
29+
AST -->|ok| Logical[LogicalValidatorNode]
30+
AST -->|retry| Retry[retry_node]
31+
Logical -->|ok| Generator[GeneratorNode]
32+
Logical -->|retry| Retry
33+
Generator --> Executor[ExecutorNode]
34+
Retry --> Refiner[RefinerNode]
35+
Refiner --> AST
36+
end
37+
38+
Router --> Schema
39+
Executor --> Router
40+
Router --> Aggregator[EngineAggregatorNode]
41+
Aggregator --> Synthesizer[AnswerSynthesizerNode]
42+
```
1243

1344
### 1. The Control Plane (The Graph)
1445

1546
**Responsibility**: Reasoning, Planning, and Orchestration.
1647

17-
* **Agentic Graph**: Implemented as a Directed Cyclic Graph (LangGraph) to enable "Refinement Loops". If a plan fails validation, the system self-corrects.
18-
* **State Management**: Deterministic state transitions ensure auditability and reproducibility of every decision.
48+
* **Agentic Graph**: Implemented as a Directed Cyclic Graph (LangGraph) to enable refinement loops. If a plan fails validation, the system self-corrects.
49+
* **State Management**: Shared `GraphState` ensures auditability and reproducibility of every decision.
1950

2051
### 2. The Security Plane (The Firewall)
2152

2253
**Responsibility**: Invariants Enforcement.
2354

24-
* **Valid-by-Construction**: The LLM *never* executes SQL directly. It generates an **Abstract Syntax Tree (AST)**.
25-
* **Static Analysis**: The [Validator Node](docs/core/nodes.md#4-logical-validator) enforces **Row-Level Security (RLS)** and type safety on the AST *before* compilation.
26-
* **Intent Classification**: Upstream detection of adversarial prompts (Jailbreaks/Injections).
55+
* **Valid-by-Construction**: The LLM generates an **Abstract Syntax Tree (AST)** rather than executing SQL.
56+
* **Static Analysis**: The [Logical Validator](docs/agents/nodes.md) enforces RBAC and schema constraints before SQL generation.
2757

2858
### 3. The Data Plane (The Sandbox)
2959

3060
**Responsibility**: Semantic Search and Execution.
3161

32-
* **Blast Radius Isolation**: SQL Drivers (ODBC/C-Ext) run in a dedicated **[Sandboxed Process Pool](docs/architecture/decisions/ADR-001_sandboxed_execution.md)**. A segfault in a driver kills a disposable worker, not the Agent.
33-
* **Partitioned Retrieval**: The [Orchestrator](docs/core/indexing.md) uses Partitioned MMR to inject only relevant schema context, preventing context window overflow.
62+
* **Blast Radius Isolation**: SQL drivers run in a dedicated **[Sandboxed Process Pool](docs/adr/adr-001-sandboxed-execution.md)**. A segfault in a driver kills a disposable worker, not the Agent.
63+
* **Partitioned Retrieval**: The [Schema Store + Retrieval](docs/schema/store.md) flow injects relevant schema context, preventing context window overflow.
3464

3565
### 4. The Reliability Plane (The Guard)
3666

3767
**Responsibility**: Fault Tolerance and Stability.
3868

39-
* **Layered Defense**: A combination of **[Retries, Circuit Breakers, and Sandboxing](docs/core/reliability.md)** ensures the system stays up even when LLMs or Databases go down.
69+
* **Layered Defense**: A combination of **[Circuit Breakers](docs/observability/error-handling.md)** and **[Sandboxing](docs/execution/sandbox.md)** keeps the system stable during outages.
4070
* **Fail-Fast**: We stop processing immediately if a dependency is unresponsive, preserving resources.
4171

4272
### 5. The Observability Plane (The Watchtower)
4373

4474
**Responsibility**: Visibility, Forensics, and Compliance.
4575

46-
* **Full-Stack Telemetry**: Native [OpenTelemetry](docs/ops/observability.md) integration provides distributed tracing (Jaeger) and metrics (Prometheus) for every node execution.
47-
* **Forensic Audit Logs**: A tamper-evident, persistent [Audit Log](docs/ops/observability.md#3-persistent-audit-log) records every AI decision (Prompt/Response/Reasoning) for compliance and debugging.
76+
* **Full-Stack Telemetry**: Native [OpenTelemetry](docs/observability/stack.md) integration provides distributed tracing (Jaeger) and metrics (Prometheus) for every node execution.
77+
* **Forensic Audit Logs**: A persistent [Audit Log](docs/observability/stack.md) records AI decisions for compliance and debugging.
4878

4979
---
5080

5181
## 📐 Architectural Invariants
5282

5383
| Invariant | Rationale | Mechanism |
5484
| :--- | :--- | :--- |
55-
| **No Unvalidated SQL** | Prevent Hullucinations & Data Leaks | All plans pass through `LogicalValidator` (AST) + `PhysicalValidator` (Dry Run) before execution. |
85+
| **No Unvalidated SQL** | Prevent hallucinations & data leaks | All plans pass through `LogicalValidator` (AST). `PhysicalValidator` exists but is not wired into the default SQL subgraph. |
5686
| **Zero Shared State** | Crash Safety | Execution happens in isolated processes; no shared memory with the Control Plane. |
5787
| **Fail-Fast** | Reliability | Circuit Breakers and Strict Timeouts prevent cascading failures (Retry Storms). |
5888
| **Determinism** | Debuggability | Temperature-0 generation + Strict Typing (Pydantic) for all LLM outputs. |
@@ -64,7 +94,8 @@ The architecture is composed of three distinct planes, ensuring separation of co
6494
### Prerequisites
6595

6696
* Python 3.10+
67-
* Docker (Optional, for full integration environment)
97+
* A configured datasource (`configs/datasources.yaml`)
98+
* A configured LLM (`configs/llm.yaml`)
6899

69100
### 1. Installation
70101

@@ -76,30 +107,31 @@ cd nl2sql
76107
python -m venv venv
77108
source venv/bin/activate
78109

79-
# Install Core Engine & CLI
110+
# Install core engine and adapter SDK
80111
pip install -e packages/core
81-
pip install -e packages/cli
82112
pip install -e packages/adapter-sdk
83113
```
84114

85-
### 2. Run Demo (Lite Mode)
115+
### 2. Run a query (Python API)
86116

87-
Boot the engine with an in-memory SQLite database (No Docker required).
117+
```python
118+
from nl2sql.context import NL2SQLContext
119+
from nl2sql.pipeline.runtime import run_with_graph
88120

89-
```bash
90-
nl2sql setup --demo
91-
```
121+
ctx = NL2SQLContext()
122+
result = run_with_graph(ctx, "Top 5 customers by revenue last quarter?")
92123

93-
---
124+
print(result.get("final_answer"))
125+
```
94126

95-
## 📚 Technical Documentation
127+
## 📚 Documentation
96128

97-
* **[System Architecture](docs/core/architecture.md)**: Deep dive into the Control, Security, and Data planes.
98-
* **[Component Reference](docs/core/nodes.md)**: Detailed specs for Planner, Validator, Executor, etc.
99-
* **[Security Model](docs/safety/security.md)**: Defense-in-depth strategy against prompt injection and unauthorized access.
100-
* **[Security Model](docs/safety/security.md)**: Defense-in-depth strategy against prompt injection and unauthorized access.
101-
* **[Reliability & Fault Tolerance](docs/core/reliability.md)**: Guide to Circuit Breakers, Sandbox isolation, and Recovery strategies.
102-
* **[Observability & Operations](docs/ops/observability.md)**: Configuring OpenTelemetry, Logging, and Audit Trails.
129+
- **[System Architecture](docs/architecture/high-level.md)**: runtime topology and core flows
130+
- **[Agent Nodes](docs/agents/nodes.md)**: node-by-node specs and responsibilities
131+
- **[Schema Store + Retrieval](docs/schema/store.md)**: schema snapshots and vector retrieval
132+
- **[Execution Sandbox](docs/execution/sandbox.md)**: process isolation and failures
133+
- **[Observability](docs/observability/stack.md)**: metrics, logging, audit events
134+
103135

104136
---
105137

@@ -108,7 +140,6 @@ nl2sql setup --demo
108140
```text
109141
packages/
110142
├── core/ # The Engine (Graph, State, Logic)
111-
├── cli/ # Terminal Interface & Ops Tools
112143
├── adapter-sdk/ # Interface Contract for new Databases
113144
└── adapters/ # Official Dialects (Postgres, MSSQL, MySQL)
114145
configs/ # Runtime Configuration (Policies, Prompts)

docs/adapters/architecture.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Plugin / Adapter Architecture
2+
3+
Adapters are discovered via Python entry points (`nl2sql.adapters`) and registered in `DatasourceRegistry`. All adapters implement the `DatasourceAdapterProtocol` and return a standardized `ResultFrame`.
4+
5+
## Discovery and registration
6+
7+
```mermaid
8+
flowchart TD
9+
Config[configs/datasources.yaml] --> Registry[DatasourceRegistry]
10+
Registry --> Discovery[discover_adapters()]
11+
Discovery --> EntryPoints[entry_points('nl2sql.adapters')]
12+
EntryPoints --> AdapterClass[Adapter Class]
13+
AdapterClass --> AdapterInstance[DatasourceAdapterProtocol instance]
14+
```
15+
16+
## Core contracts
17+
18+
```mermaid
19+
classDiagram
20+
class DatasourceAdapterProtocol {
21+
+capabilities() Set
22+
+connect()
23+
+fetch_schema_snapshot()
24+
+execute(AdapterRequest) ResultFrame
25+
+get_dialect() str
26+
}
27+
class AdapterRequest {
28+
+plan_type
29+
+payload
30+
+parameters
31+
+limits
32+
+trace_id
33+
}
34+
class ResultFrame {
35+
+success
36+
+columns
37+
+rows
38+
+row_count
39+
+error
40+
}
41+
```
42+
43+
## Executor integration
44+
45+
Execution nodes resolve the executor via `ExecutorRegistry`, which maps datasource capabilities to executor implementations (e.g., `SqlExecutorService` for SQL).
46+
47+
## Source references
48+
49+
- Adapter protocol and contracts: `packages/adapter-sdk/src/nl2sql_adapter_sdk/protocols.py`, `packages/adapter-sdk/src/nl2sql_adapter_sdk/contracts.py`
50+
- Adapter discovery: `packages/core/src/nl2sql/datasources/discovery.py`
51+
- Datasource registry: `packages/core/src/nl2sql/datasources/registry.py`
52+
- Executor registry: `packages/core/src/nl2sql/execution/executor/registry.py`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# ADR-001: Sandboxed Execution and Indexing
2+
3+
## Status
4+
5+
Accepted (implemented in `SandboxManager`).
6+
7+
## Context
8+
9+
SQL drivers and indexing operations can crash or block the main process. The runtime needs isolation between orchestration and execution.
10+
11+
## Decision
12+
13+
Use **ProcessPoolExecutor** pools managed by `SandboxManager`:
14+
15+
- `get_execution_pool()` for latency-sensitive SQL execution.
16+
- `get_indexing_pool()` for background indexing tasks.
17+
18+
All sandbox calls are wrapped in `execute_in_sandbox()` to standardize timeouts and error handling.
19+
20+
## Consequences
21+
22+
- Worker crashes and timeouts are contained and converted into structured errors.
23+
- Execution concurrency is bounded by settings (`sandbox_exec_workers`, `sandbox_index_workers`).
24+
25+
```mermaid
26+
flowchart TD
27+
Orchestrator[run_with_graph] --> Sandbox[SandboxManager]
28+
Sandbox --> ExecPool[get_execution_pool]
29+
Sandbox --> IndexPool[get_indexing_pool]
30+
```
31+
32+
## Source references
33+
34+
- `packages/core/src/nl2sql/common/sandbox.py`
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# ADR-002: Circuit Breakers for External Dependencies
2+
3+
## Status
4+
5+
Accepted (implemented in `resilience.py`).
6+
7+
## Context
8+
9+
LLM providers, vector stores, and databases can experience outages. Unbounded retries degrade system reliability.
10+
11+
## Decision
12+
13+
Use `pybreaker` circuit breakers for each dependency class:
14+
15+
- `LLM_BREAKER`
16+
- `VECTOR_BREAKER`
17+
- `DB_BREAKER`
18+
19+
All breakers are created with `create_breaker()` and emit log events via `ObservabilityListener`.
20+
21+
## Consequences
22+
23+
- Fail-fast behavior when dependencies are down.
24+
- Retry loops in the SQL agent only apply to retryable errors, not open circuits.
25+
26+
```mermaid
27+
flowchart TD
28+
Call[Dependency Call] --> Breaker[create_breaker()]
29+
Breaker -->|closed| Execute[Execute]
30+
Breaker -->|open| FailFast[Fail Fast]
31+
```
32+
33+
## Source references
34+
35+
- `packages/core/src/nl2sql/common/resilience.py`

docs/adr/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Architecture Decision Records
2+
3+
This section captures architectural decisions that are reflected in the current codebase.
4+
5+
- `adr-001-sandboxed-execution.md`
6+
- `adr-002-circuit-breakers.md`

docs/agents/architecture.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Agent Architecture
2+
3+
NL2SQL implements agent behavior as **LangGraph subgraphs**. The primary subgraph today is the SQL agent, built by `build_sql_agent_graph()`. Each node is a class with a `__call__` method that consumes and returns Pydantic state models (`SubgraphExecutionState`).
4+
5+
## SQL Agent subgraph
6+
7+
The SQL agent subgraph is built in `nl2sql.pipeline.subgraphs.sql_agent.build_sql_agent_graph`. It orchestrates schema retrieval, planning, validation, generation, execution, and optional refinement.
8+
9+
```mermaid
10+
flowchart LR
11+
Schema[SchemaRetrieverNode] --> AST[ASTPlannerNode]
12+
AST -->|ok| Logical[LogicalValidatorNode]
13+
AST -->|retry| Retry[retry_node]
14+
Logical -->|ok| Generator[GeneratorNode]
15+
Logical -->|retry| Retry
16+
Generator --> Executor[ExecutorNode]
17+
Retry --> Refiner[RefinerNode]
18+
Refiner --> AST
19+
```
20+
21+
### Node responsibilities
22+
23+
- `SchemaRetrieverNode`: retrieves schema context from `VectorStore` and `SchemaStore`.
24+
- `ASTPlannerNode`: produces a structured plan (AST) for the sub-query.
25+
- `LogicalValidatorNode`: enforces schema and policy constraints on the AST.
26+
- `GeneratorNode`: renders SQL from the plan.
27+
- `ExecutorNode`: dispatches SQL to `ExecutorRegistry` and stores artifacts.
28+
- `RefinerNode`: refines the plan when validation fails.
29+
30+
## Subgraph execution state
31+
32+
`SubgraphExecutionState` tracks per-subgraph execution details including `sub_query`, `relevant_tables`, planner output, validator output, executor responses, errors, and retry counters.
33+
34+
## Source references
35+
36+
- SQL agent graph: `packages/core/src/nl2sql/pipeline/subgraphs/sql_agent.py`
37+
- Node classes: `packages/core/src/nl2sql/pipeline/nodes/`
38+
- Subgraph state: `packages/core/src/nl2sql/pipeline/state.py`

0 commit comments

Comments
 (0)