SQL Agent Subgraph
How one sub-query is planned, validated, generated and executed, for contributors.
Overview
- Purpose: plan, validate, generate, and execute SQL for a single sub-query.
- Why it exists architecturally: isolates SQL-capable execution into a reusable subgraph that can be scheduled per scan node in the global execution DAG.
- When it is invoked: when
layer_routerresolves a datasource withSUPPORTS_SQLcapability and sends a scan-layer payload.
Defining function: build_sql_agent_graph()
Source file path: packages/nl2sql/src/nl2sql/pipeline/subgraphs/sql_agent.py
Boundary Definition
This subgraph encapsulates the SQL planning and execution loop for a single SubQuery. It does NOT perform cross-subquery aggregation, answer synthesis, or datasource resolution; those responsibilities are owned by the main pipeline graph and upstream nodes.
Entry Conditions
Required state:
- SubgraphExecutionState.trace_id
- SubgraphExecutionState.sub_query (must include id, datasource_id, and intent)
Preconditions:
- sub_query.datasource_id must map to a registered adapter/executor.
- Schema store and vector store may be used if configured; otherwise schema retrieval falls back to full schema snapshot.
Triggering parent graph:
- layer_router in the main pipeline graph sends build_scan_payload() to the subgraph via wrap_subgraph().
Exit Conditions
Successful completion criteria:
- Reaches END after executor and produces executor_response.
- Or reaches END after clarifier, which asked which reading of the question was meant (NEEDS_CLARIFICATION, INFO): no SQL is generated or run, and the wrapper reports the sub-query with status needs_clarification.
Failure exits:
- check_retriever returns end when schema_retriever returned a non-retryable error: retrieval failed and there was no snapshot to fall back to (SCHEMA_RETRIEVAL_FAILED, CRITICAL). The planner is never called.
- check_planner returns end when planner output is missing and retries are exhausted or errors are non-retryable.
- check_logical_validation returns end when validation errors are non-retryable or retries are exhausted.
- check_execution returns end after executor unless the database rejected the query as wrong (EXECUTION_FAILED) and retries remain: a success, a refusal (DB_EXECUTION_ERROR: permission, timeout, connection) and any other executor error all end the subgraph.
- Cancellation forces end in router checks and in the retry handler. The run's CancellationToken is read from the RunnableConfig (configurable.cancellation_token); when no token is present the subgraph behaves as not cancelled.
Partial completion behavior:
- If planner or logical validation errors are retryable and retry budget remains, execution loops via retry_handler → refiner → ast_planner.
- So does a query the database rejected as wrong (an unknown column or table, a type mismatch, bad syntax): the adapter marks the error retryable and the executor reports it as EXECUTION_FAILED, with the database's own first line as the message.
Internal Node Composition
Execution order (nominal path):
- schema_retriever — SchemaRetrieverNode — packages/nl2sql/src/nl2sql/pipeline/nodes/schema_retriever/node.py — retrieve relevant schema tables/columns.
- ast_planner — ASTPlannerNode — packages/nl2sql/src/nl2sql/pipeline/nodes/ast_planner/node.py — create SQL AST plan.
- logical_validator — LogicalValidatorNode — packages/nl2sql/src/nl2sql/pipeline/nodes/validator/node.py — validate plan correctness and policy constraints.
- clarifier — ClarifierNode — packages/nl2sql/src/nl2sql/pipeline/nodes/clarifier/node.py — validate the planner's alternative readings and, when two or three remain, end the sub-query by asking which was meant. See ClarifierNode.
- generator — GeneratorNode — packages/nl2sql/src/nl2sql/pipeline/nodes/generator/node.py — generate SQL text from AST.
- executor — ExecutorNode — packages/nl2sql/src/nl2sql/pipeline/nodes/executor/node.py — execute SQL and produce artifacts.
- retry_handler — retry_node() — packages/nl2sql/src/nl2sql/pipeline/subgraphs/sql_agent.py — apply exponential backoff and increment retry count.
- refiner — RefinerNode — packages/nl2sql/src/nl2sql/pipeline/nodes/refiner/node.py — generate feedback for the planner.
Mermaid diagram (sql_agent only):
flowchart TD
schema_retriever -->|ok| ast_planner
schema_retriever -->|end| END
ast_planner -->|ok| logical_validator
ast_planner -->|retry| retry_handler
ast_planner -->|end| END
logical_validator -->|ok| clarifier
clarifier -->|ok| generator
clarifier -->|end| END
logical_validator -->|retry| retry_handler
logical_validator -->|end| END
retry_handler --> refiner --> ast_planner
generator --> executor
executor -->|retry| retry_handler
executor -->|end| END
State Contract
State model: SubgraphExecutionState (packages/nl2sql/src/nl2sql/pipeline/state.py).
Field ownership, reducers, and lifecycle are defined in ../graph_state.md.
Step-by-Step Execution Flow
schema_retrieverbuilds a semantic query fromsub_queryand retrieves relevant schema context; if none found, or the search fails, it falls back to full schema snapshot. A table cut to the searched columns keeps its keys, join columns and label.check_retrieverroutesendwhen retrieval failed with no snapshot (a non-retryableSCHEMA_RETRIEVAL_FAILED), elseok.ast_plannerruns an LLM planning chain to producePlanModel.check_plannerroutes:okif a plan exists.retryif errors are retryable and retry budget remains.endif errors are non-retryable or retries exhausted.logical_validatorvalidates the plan for structural and policy compliance.check_logical_validationroutes:okwhen no blocking errors exist.retrywhen retryable errors exist and retry budget remains.endwhen errors are non-retryable or retries exhausted.clarifierpasses straight through when the planner gave no alternative readings. Otherwise it runs each alternative throughLogicalValidatorNode.check(the static checks and the caller's scope), drops those that fail, merges those that render to SQL another option has, and keeps the rest onast_planner_response.alternatives.check_clarifierroutesendwhen it asked (two or three options, a named phrase,state.clarifyandsettings.clarify.enabledon), elseok. Every path from the planner to the generator still passes the validator.generatorconverts the plan to SQL for the target datasource dialect.executorruns SQL via the datasource adapter and returns artifacts/errors.check_executionroutesretrywhen every executor error is a retryableEXECUTION_FAILEDand retry budget remains, otherwiseend. It also routesretrywhen the query returned no rows on a filter value the column does not have but a close one exists (VALUE_NOT_FOUNDERROR, raised once per sub-query; see ExecutorNode), and when aCOUNTcounted rows a join repeated (JOIN_FAN_OUTERROR, raised once per sub-query; see ExecutorNode). Only a finding of this attempt (itsdetails.attempt) routesretry.- If routed to retry:
retry_handlerwaits with exponential backoff and jitter, then incrementsretry_count. refineruses LLM feedback to enrich error context, then loops back toast_planner. Both are told about the latest attempt only, as{node, error_code, message}(nl2sql.pipeline.retry_feedback): never an earlier attempt's errors again, never a stack trace ordetails.- Subgraph terminates at
END.
Determinism Guarantees
Determinism guarantees and non-determinism sources are documented in ../determinism.md.
Error Propagation
Failure domains, retry scope, and recovery limitations are centralized in ../failure_recovery.md.
Retry + Recovery
See ../failure_recovery.md for retry scope and recovery behavior.
Performance Characteristics
- Blocking LLM calls:
ASTPlannerNodeandRefinerNode. - External I/O: vector store retrieval and schema store access in
SchemaRetrieverNode; datasource execution inExecutorNode. - CPU-bound steps: SQL generation via sqlglot in
GeneratorNode. - Retry loop introduces sleep-based delays on failures.
Observability
- Node-specific loggers are used (
schema_retriever,planner,logical_validator,generator,executor,refiner). trace_idis stored in state and forwarded to executor requests; logging context is not explicitly set.- No explicit metrics or tracing spans are emitted by the subgraph code.
Configuration
Read from the runtime's settings.pipeline.retries when the subgraph is built:
max(default3)base_delay_s(default1.0)max_delay_s(default10.0)jitter_s(default0.5)
Extension Points
- Modify
build_sql_agent_graph()to insert, remove, or rewire nodes and edges. - Add new subgraphs by constructing them in
build_graph()and extendingresolve_subgraph()with their required capabilities. - Replace node implementations via dependency injection in
Runtime.
Known Limitations
- Generator errors are not routed into the retry loop; a failure proceeds to
executor, which reportsMISSING_SQL, and terminates atEND. - No subgraph-specific metrics or trace spans are emitted.
- The executor retries only what the adapter marks
retryable. An adapter that raises instead of returning a failedResultFrameends the sub-query withEXECUTOR_CRASH.
Because the generator has no retry edge, the invariant the loop depends on is
that logical_validator rejects every plan generator would refuse to
build. LogicalValidatorNode._validate_buildable() enforces it by running the
generator's own _build_query() — see
Buildability.
Without it, a plan whose tables are not joined reached generator and ended the
sub-query with a terminal SQL_GEN_FAILED.
Related Code
- Subgraph definition:
packages/nl2sql/src/nl2sql/pipeline/subgraphs/sql_agent.py - Subgraph selection:
packages/nl2sql/src/nl2sql/pipeline/graph_utils.py - State model:
packages/nl2sql/src/nl2sql/pipeline/state.py - Node implementations:
packages/nl2sql/src/nl2sql/pipeline/nodes/schema_retriever/node.pypackages/nl2sql/src/nl2sql/pipeline/nodes/ast_planner/node.pypackages/nl2sql/src/nl2sql/pipeline/nodes/validator/node.pypackages/nl2sql/src/nl2sql/pipeline/nodes/clarifier/node.pypackages/nl2sql/src/nl2sql/pipeline/nodes/generator/node.pypackages/nl2sql/src/nl2sql/pipeline/nodes/executor/node.pypackages/nl2sql/src/nl2sql/pipeline/nodes/refiner/node.py- Tests:
packages/nl2sql/tests/unit/test_sql_agent_subgraph.pypackages/nl2sql/tests/unit/test_subgraph_resolution.pypackages/nl2sql/tests/unit/test_clarifier.py