SQLHarness

From business questions to inspectable, governed SQL.

SQLHarness translates business questions into a structured query description and SQL building blocks, then checks the resulting SQL before allowing execution. The research asks whether this structure can produce more accurate queries for a given cost than a language model generating SQL directly.

System architecture

The pipeline separates research on query representation and retrieval from the checks that control execution.

StageInput or responsibility
Business requestNatural language, database schema, and task evidence.
Intent graphExplicit entities, constraints, operations, and relations.
Fragment retrievalSQL building blocks retrieved using the intent graph for lightweight assembly.
Authority boundarySQL syntax-tree (AST), schema, policy, timeout, and execution-limit checks.

Representation and retrieval

  • Teacher-model output distribution → semantic intent graph
  • Intent-graph fragments → retrieval of SQL building blocks
  • Component-removal tests (ablations) isolate parser, retriever, and assembler errors

Execution constraints

  • Model proposes; governed boundary decides
  • Read-only SQLite execution with time and row limits
  • Every proposal, decision, and result becomes a trace

Making each query stage inspectable

A generated query can be valid SQL and still answer the wrong question. SQLHarness separates the request’s meaning, the SQL structures available for retrieval, and permission to execute. This makes a failure attributable to a stage instead of treating every wrong result as a prompt problem.

Following one request through the representation

The repository’s version 0.2 example represents “Which region has the most customers?” with a customer entity, a region attribute, a count operation grouped by region, and an argmax operation limited to one result. Directed edges connect the count to its customer target and region grouping; the output returns the region constrained by that maximum.

Retrieval uses the graph to select parameterized SQL building blocks; the assembler receives the graph, selected fragments, and database schema to propose SQL. The necessary operations here are joining customers to regions, grouping, counting, and selecting the largest group. The saved graph is an illustrative contract example, not a recorded model-generated retrieval trace. The executable fixture below separately demonstrates the resulting SQL and authority checks.

Inspect the complete graph, source spans, and schema candidates. Read the architecture contract and proposed training path.

One execution authority across interfaces

The Model Context Protocol (MCP) server and command-line evaluator call the same DatabaseAuthority. Every exposed execution tool uses these checks: database IDs resolve inside a configured root, SQL is parsed and checked against the schema and read-only policy, and allowed execution uses a read-only SQLite connection with a deadline and row cap.

MCP toolResponsibility
list_databasesList databases inside the configured authority root.
inspect_schemaReturn the selected database’s tables and columns.
validate_sqlCheck syntax, schema references, and policy without executing the proposal.
explain_sqlValidate the proposal before returning a bounded SQLite query plan.
execute_readonly_sqlValidate again, then execute under time and row limits.

Decisions include structured errors and deterministic receipt IDs for tracing. A receipt is an identifier for a decision, not a claim of immutable storage. Read-only enforcement limits database effects; it does not establish that an allowed query answers the business question correctly. Inspect the safety boundary and its limits.

Example execution trace

This cached run is produced by the repository’s real validation and execution code. It needs no API key.

Successful query

Business request: Which region has the most customers?

Task evidence: Count customers by region and return only the largest group.

SELECT r.name, COUNT(*) AS customer_count FROM customers AS c JOIN regions AS r ON c.region_id = r.id GROUP BY r.id, r.name ORDER BY customer_count DESC LIMIT 1
Validation and executionRecorded result
Decisionexecuted
Receipt0a59a1292a6ef895eea3
Tablescustomers, regions
Columnsc.region_id, customer_count, r.id, r.name
ErrorsNone
ResultMatched reference result; Northeast, 3 customers
Latency0.21 ms

Blocked write

Requested operation: Clean up purchases below $10.

DELETE FROM purchases WHERE amount < 10

Policy rejection: non_readonly_operations: DELETE. Decision: blocked. Database untouched. Receipt: 794c586a63e3e8bfd993.

Offline demonstration fixture, not a score on the BIRD text-to-SQL benchmark. Its purpose is to prove the harness is executable and the safety path is inspectable.

Evaluation design

Every comparison method receives the same fixed BIRD examples, database schema, and evaluation conditions.

Arm A

Direct model baseline

Question + schema → SQL generated directly by a frontier language model.

Arm B

SQLHarness system

Intent graph → retrieved fragments → lightweight assembly.

Arm C

Ablations

Remove each component to measure its actual contribution.

Evaluation roleMeasure
PrimaryExecution accuracy
EfficiencyCost per correct query
OperationsEnd-to-end latency
DiagnosisErrors classified by pipeline stage

Direct generation leads on the five-query comparison.

The structured system did not improve the accuracy–cost tradeoff on this small comparison. That negative result is preserved beside the component evidence and governed execution path.

Frozen comparison · 20 train / 5 held out

Five-query result: direct 4/5; structured arms 3/5.

MeasureObserved
Direct model (Astra) correct4/5
SQLHarness with dense retrieval correct3/5
SQLHarness with learned retrieval correct3/5
Queries left unanswered by parser1
MethodReported usage and latency
Direct generation5.61 s mean model latency · 77,327 tokens
SQLHarness with learned retrieval67.23 s mean observed latency · 142,764 reported tokens

Five-example development comparison. SQLHarness tokens cover four completed pipelines and omit the timed-out parser, so they understate true consumption. The latency fields also differ: direct generation reports model latency, while the structured result combines recorded parser and assembler latency. The fixed-version BIRD per-query evaluator reproduces the 80% versus 60% scores. Read the complete result and error decomposition.

First held-out vertical slice · 3 examples

Initial slice: both arms 2/3; structured inference was slower.

MeasureObserved
Direct model correct2/3
SQLHarness correct2/3
Parser-timeout abstention1
Training examples → fragments7 → 48
MethodReported usage and latency
Direct generation6.35 s/query · 45,546 tokens across 3
SQLHarness v0.146.19 s/completed query · 70,197 tokens across 2

Three-example development comparison. The third direct query followed conflicting benchmark evidence (COUNT > 2) instead of the question and reference SQL (COUNT >= 2); SQLHarness timed out before answering it. Read the full failure analysis.

Fragment retrieval isolated · lexical / dense / learned · 20/5 split

Retrieval result: the dense encoder leads at Recall@3.

Measure (Recall@3: share of required SQL structures found within the top 3 candidates)Observed
Required exact SQL structures present in catalog58.82%
Lexical Recall@315.38%
Dense Recall@323.08%
Learned Recall@315.38%

Five-example development comparison; four graphs were available and three queries had a relevant exact structure in the catalog. Dense BGE-small reaches 23.08% Recall@12; the learned ranker improves Recall@1 but falls to 15.38% at rank 12. Only 58.82% of the required exact SQL structures exist in the catalog, limiting what any retrieval method can return. Read the retrieval analysis.

What the failures changed

The five-query comparison exposed different ceilings: one parser timeout prevented assembly, and the 20-example fragment catalog contained only 58.82% of the exact SQL structures required by the held-out set. Ranking cannot recover a structure absent from the catalog. The learned ranker improved the first result but lost relevant structures farther down the list, without changing final correctness.

Those observations favor expanding the catalog and distilling the parser before further ranker tuning. Repeated teacher samples also had much less agreement on graph edges than nodes, so teacher graphs are treated as candidates to refine rather than unquestioned training labels. These are next research hypotheses, not measured improvements.

Keeping the evaluation populations separate

The 500-example preflight below checks whether reference queries can run within local limits. The five-query CLI pilot checks the direct generation path. The initial seven-train/three-held-out slice tests an early structured pipeline; the later 20-train/five-held-out comparison tests the expanded system and retrieval variants. Their scores describe different development experiments and are not pooled into one benchmark result.

Arm A preflight

Direct-model reproducibility preflight

Preflight checkObserved
Frozen examples500
SQLite databases11
Reference queries executed within local limits99.6% (498/500)
Result truncations0

This preflight tests reproducibility rather than model performance. The official BIRD evaluator determines the final score.

Development pilot · Codex CLI / GPT-6 Astra

Codex CLI development pilot

MeasureObserved
Locally correct4/5
Valid SQL5/5
Mean model latency6.34s
CLI-reported tokens75,206

Five-query diagnostic run. The single rejection selected the correct entity but returned an extra aggregate column, causing a strict result-shape mismatch. CLI usage is not assigned API cost because it runs through the authenticated Codex product runtime.