RubikSQL-HB: Agentic Text-to-SQL with Knowledge in the Loop

Haorui Jia, Zihan Qu, Yundi Dong, Ziyang Mei, Chi Zhang, Zezhi Zheng, Dousheng Zhao, Jiahao Zhang, Zui Chen, Han LiarXiv 2026

TL;DR

Writing accurate SQL requires more than just knowing table names; it requires discovering business context and data quirks while you build the query. RubikSQL-HB turns static database documentation into an interactive workspace that AI agents can query mid-thought, both when generating candidate answers and when acting as a "Judge" to settle disputes between conflicting queries. By keeping background knowledge and live database probes "in the loop" at every decision point, the system significantly outperforms standard majority-voting approaches.

What Problem Does This Paper Address?

Imagine trying to answer a business question using an unfamiliar database. A schema tells you the structure—column names and data types—but not what the data means. You might not realize you have a misunderstanding until your first test query returns zero rows, weird formatting, or unexpected duplicates.

Traditional Text-to-SQL pipelines often front-load all retrieved context into a massive initial prompt before the LLM writes a single line of code. Worse, if you generate multiple candidate queries to boost reliability and simply let them vote on the final answer, the majority can easily agree on the same subtle semantic mistake (like dividing by active months instead of a full 12-month calendar year). Agents need a way to dynamically look up definitions, inspect reference patterns, and probe live data as their reasoning unfolds—especially when adjudicating disagreements.

Methods

RubikSQL-HB (built on a platform called HeavenBase) separates offline knowledge organization from online iterative reasoning via a shared Model Context Protocol (MCP) tool layer.

Figure 1: RubikSQL-HB overview. Offline preparation organizes a per-database knowledge workspace; online Candidates query that resource within their retrieval–reasoning–execution loops. The Judge reuses the same interfaces, followed by optional risk-gated review. Bidirectional links denote tool access under each agent’s permissions; activities inside agent boxes do not prescribe a fixed call sequence.
  1. Per-Database Knowledge Workspace: Offline, the system indexes six distinct types of knowledge per database: tables, column profiles (combining human docs with observed data formats/null rates), categorical values, vocabulary synonyms, interpretation rules, and reference question–SQL pairs.
  2. Two-Stage Example Retrieval: Instead of dumping reference SQL straight into the agent's context—which can bias or clutter its thinking—example lookup happens in two steps. First, the agent views a lightweight menu of similar questions and SQL lengths; then, it explicitly chooses which IDs to expand into full SQL. Two-stage example retrieval separating question discovery from SQL expansion.

  3. Parallel Candidates & Tool-Assisted Judge: Online, five independent Candidate agents interleave knowledge lookups with live SQL execution. Their final queries and execution previews are handed off to an Agentic Judge. Crucially, the Judge isn't passive: it has access to the exact same workspace tools and live database execution to investigate why candidates disagree, and can either pick a winner or rewrite the SQL entirely.

  4. Risk-Gated Review & Evolution: An optional Reviewer checks outputs only when heuristic risk signals trigger (e.g., empty results or null cells). Across system versions, a development agent (META) analyzes recurring semantic bugs and compiles them into deterministic runtime execution checks.
Figure 2: Two-stage example retrieval. Discovery exposes only a session-local ID, question, and SQL character count; selected IDs resolve to retained question–SQL pairs in the same session and database. The agent chooses which reference SQL enters context. IDs and payloads are schematic. Candidate coverage and final accuracy under this interface are evaluated separately in Section 5.3.

Datasets

  • BIRD Mini-Dev (500-question subset): The primary evaluation uses a cleaned 500-question development set (minidev_500_20251106) spanning 11 databases, emphasizing messy database contents and external domain knowledge. Reference examples in the workspace are transferred from BIRD training sources without exposing target gold SQL.
  • Stratified 330-question BIRD subset: An older cohort from the 2024-06-27 Mini-Dev release used separately to track automated system evolution across versions. No new public benchmarks are introduced.

Key Insights and Ideas

  • Generating more candidates helps coverage, not voting: Expanding from 1 to 5 candidates boosts the chance that at least one answer is right by over 7 percentage points, yet majority-vote accuracy barely budges (+0.38 points). Enlarging or mixing model pools often strengthens an incorrect majority group, proving that active, evidence-backed adjudication is essential to rescue smart minority answers.
  • Information access is a reasoning decision: In the two-stage example setup, agents viewed the question menu over 80% of the time, but chose to expand the actual SQL in only ~30% of runs. Letting the agent decide what enters its context window prevents premature anchoring on irrelevant SQL patterns.
  • Unconditional "fixing" breaks good answers: Dedicated semantic interventions (like auditing join contracts or aggregation grain) and unconditional review often introduce as many regressions on correct queries as repairs on broken ones. Interventions work best behind strict risk gates or explicit routing.

Key Results

  • Main Pipeline Accuracy: On the 500-question cohort, the full pipeline reaches 75.2% execution accuracy (EX). While a correct answer exists somewhere in the 5-candidate pool 79.0% of the time (Oracle), naive execution-majority voting gets only 70.4% and picking the first candidate gets 71.2%.
  • Value of the Workspace & Judge: Stripping away the structured workspace drops accuracy by 3.0 points (to 72.2%). Giving the Judge observation tools and SQL revision permissions lifts accuracy from 71.6% (passive selection) to 74.6%, with risk-gated review pushing it to 75.2% (whereas reviewing all outputs slightly hurts at 74.4%).
  • System Evolution: On the separate 330-question development cohort, turning recurring errors (like unrequested COUNT(DISTINCT) or bad percentage denominators) into narrow execution checks improved accuracy from 77.88% to 81.82% (+13 net correct answers with zero regressions across the five check updates).

Limitations and Future Work

  • Development Bias & Variance: Evaluations were conducted on public BIRD development subsets that informed the system's design, rather than an unseen holdout benchmark. On 500 questions, a 1.0% gap equals just five questions, placing small ablation differences (like two-stage vs. one-stage retrieval) within question-level noise.
  • Single-Instance Blind Spots: Execution accuracy checks whether two queries return the same table on one database snapshot, meaning semantically flawed queries can occasionally pass by coincidence.
  • Richer Agent Handoffs: Currently, Candidates only pass their final SQL and result summaries to the Judge, forcing the Judge to re-investigate disputed assumptions from scratch. Future designs could attach the specific workspace evidence each Candidate found directly to the SQL clauses it supports.

Summary updated

Abstract

Database agents need more than schema structure: business meanings, stored values and reference queries supply knowledge that becomes relevant at different points in reasoning. We introduce RubikSQL-HB, which exposes these materials through queryable, database-specific knowledge workspaces built on the HeavenBase platform. Its knowledge interface remains available as an agent constructs and investigates an answer. Candidate agents interleave knowledge access with SQL construction and execution; a downstream Judge can reuse the same interfaces to investigate disagreements. A two-stage example interface separates discovering a reference question from expanding its SQL. On a 500-question BIRD Mini-Dev evaluation, the full pipeline reaches 75.2% execution accuracy, against 71.2% for first-candidate selection and 70.4% for execution-majority voting, with a correct candidate available on 79.0% of questions. The raw-knowledge condition, which also regenerates candidates without the built workspace, is 3.0 points lower, and adjudication with tools and revision accounts for most of the gain over selection baselines. A recorded execution probe shows how continued investigation resolves a candidate disagreement. Separate archived runs show semantic revision recovering answers absent from their original candidate pools. A versioned development study further illustrates how recurring corrections become reusable execution checks. The system and results connect knowledge-interface design to agent decision quality: queryable knowledge supports the construction of useful alternatives, and evidence-aware adjudication connects those alternatives to a final answer. Keeping knowledge in the loop lets agents acquire relevant observations as their reasoning develops.
BibTeX
@misc{jia2026rubiksql,
  author = {Haorui Jia and Zihan Qu and Yundi Dong and Ziyang Mei and Chi Zhang and Zezhi Zheng and Dousheng Zhao and Jiahao Zhang and Zui Chen and Han Li},
  title = {{RubikSQL-HB: Agentic Text-to-SQL with Knowledge in the Loop}},
  year = {2026},
  eprint = {2611.32772},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.32772},
}
Figure