Single-Database Rewards Are Hackable: Counterfactual Execution for Trustworthy Text-to-SQL RL: what it means for business leaders
Grading a text-to-SQL agent's final query on hidden, code-generated copies of the database removes nearly all constructed reward hacks and many lucky-correct bugs, but in this study it did not train a measurably better agent.
1. What the method is
The paper studies a reward for reinforcement learning of text-to-SQL agents that checks the agent's final query on more than one database. The usual reward executes the query on a single database and pays when its rows match the reference query's rows. The counterfactual-database reward also runs the query on two, rarely three, hidden copies of that database, each produced by code with one rule-based edit to the slice of data the question reads. The strict form pays only if the result matches on every database; the mean form pays the share of databases matched. The agent can explore the original database read-only but never sees the copies or the grader. The authors audit the reward against fixed sets of correct queries, realistic buggy queries and constructed hacks before training, then run a controlled training comparison in which only the reward differs.
2. Why the method was developed
A wrong SQL query that runs and returns a plausible number is the dangerous kind, because it ends up in a business report. Execution-match rewards assume one database separates right from wrong queries, and that fails when a question reads a small slice of data. The authors' example is an agent counting distinct orders when asked for distinct customers: on the database it explored each buyer had one order, so both answers were 10 and the reward paid in full. In their synthetic retail data, 29.2% of realistic buggy queries pass the single-database check at 50 customers, against 0.8% at 500. A single database also pays outright exploits, such as hard-coding an answer seen during exploration. Earlier work caught lucky-correct programs mostly at evaluation time with test suites; an LLM judge would add a second model that can itself be gamed.
3. Who should care
Teams that fine-tune models with execution-based rewards should care most, whether for SQL generation or for any agent graded by running its output against one fixture. Data and analytics leaders who buy or build natural-language query tools should read the audit numbers as a statement about evaluation: 8.1 to 14.9% of the answers counted correct by standard execution accuracy for eight published Spider systems fail a test-suite database, so headline accuracy overstates what we would get in production. ML platform and evaluation owners get a concrete audit procedure to run on any reward before spending training budget. Vendor assessment is touched as well, because the authors found the same hole in public reward code, including an exploit in AgentTune's SQL reward that its maintainers have since fixed. Finance and reporting functions are the downstream users exposed to silently wrong numbers.
4. How the method works
Each copy applies one edit and keeps the business rules intact. Answer-changing edits, such as adding new customers with orders to the slice, alter the reference answer and so defeat hard-coded answers and hard-coded row IDs. Answer-preserving edits leave the reference answer unchanged but add rows that expose one family of bug: an extra active order for duplicates, payments split into instalments for join fan-out, cancellations or refunds for missing filters, and superseded tier rows for queries that ignore the latest record. Results are compared as strict multisets of rows, ordered when the reference is ordered, in a read-only sandbox with a five-second timeout. Reference answers are checked against an independent pandas implementation. Copies are chosen either blind, one random copy of each kind, or informed, picked greedily to kill known buggy variants. The copies add about one millisecond of CPU time per rollout.
5. How it was evaluated
The audit used 1,302 synthetic questions over seeded eight-table retail SQLite databases, scoring 2,847 correct queries, 1,814 realistic buggy variants of thirteen types and 2,839 constructed hacks, and reporting how many each reward paid in full or wrongly rejected. Training used a 3.35-billion-parameter model, Tiny Aya Global, with a rank-16 LoRA adapter: supervised fine-tuning on 353 scripted trajectories, then 60 GRPO steps on 445 prompts, with two arms sharing everything except the reward, over three seeds. The in-domain test set held 331 items from seven templates unseen in training, each with four copies from a separate, family-blind generator; an answer counted as verified only if it matched on all five databases. Transfer was measured on the 1,034 Spider development questions with test-suite accuracy. The main training pair took about seven hours on one L4 GPU, roughly five dollars.
6. How it performed
As a check, the reward works: with blind copies the strict form cut buggy queries paid in full from 29.5% to 16.8% and hacks from 54.1% to 1.9%, rejecting none of 2,847 correct queries, though the 43% relative cut missed the 50% the authors pre-registered. As a training signal it did not help: pooled over three seeds, the counterfactual arm verified 8.4% of test samples against 11.7% for the single-database arm, because the two rewards disagreed on only 5 to 7% of rollouts. On Spider it gained 1.6 points, within seed noise, and one seed reverses both signs. A larger single database catches accidental bugs but still pays every hard-coded hack. The limits are substantial: one synthetic schema, one small model, three seeds, 60 training steps, English only, no human red-team. Our call is to pilot the audit on our own evaluation and reward fixtures now, and watch the training claim. (Source: arXiv 2611.34363, 2026)
← Back to dossier index