Single-Database Rewards Are Hackable: Counterfactual Execution for Trustworthy Text-to-SQL RL
Organizations: Artificial Intelligence and Machine Learning Society (AIMS-DTU) Delhi Technological University, New Delhi, India
Abstract
Reinforcement learning (RL) for text-to-SQL agents pays a query when its result matches the reference on one database. However, a wrong query can match by luck when the data lack the rows that expose its bug. We study a counterfactual-database (CF) reward that also runs the final query on a few hidden, code-generated copies of the database and pays only if all results match (strict form) or by the share matched (mean form). On 1,302 synthetic audit questions, the strict form lowers the share of buggy queries paid in full from 29.5% to 16.8% and of reward hacks from 54.1% to 1.9%, rejecting 0 of 2,847 correct queries; copies from a family-blind generator do about as well. In a controlled GRPO comparison on a 3.35B model with three seeds per arm, training on the mean form did not improve in-domain accuracy (8.4% vs 11.7%), because the two rewards disagreed on only 5-7% of rollouts; on Spider it gained 1.6 points. One seed reverses both signs, so single-seed reward comparisons mislead. Code: https://github.com/RishiiGamer2201/counterfactual-sql-rewards ; data, models, demo: https://huggingface.co/counterfactual-sql
Figures & tables
| Paid in full (%) | Rejected (%) | |||
| Reward comparator | Databases graded | Mutants | Hacks | Correct |
| Set match (BIRD/SkyRL style) | 37.5 [35.3, 39.7] | 54.1 [52.3, 56.0] | 0.0 [0.0, 0.1] | |
| Strict multiset match, | 29.5 [27.4, 31.6] | 54.1 [52.3, 56.0] | 0.0 [0.0, 0.1] | |
| Set match | , blind | 27.2 [25.2, 29.3] | 1.9 [1.5, 2.5] | 0.0 [0.0, 0.1] |
| Strict multiset match, (ours) | , blind | 16.8 [15.2, 18.6] | 1.9 [1.5, 2.5] | 0.0 [0.0, 0.1] |
| Strict multiset match, (bound) † | , informed | 1.5 [1.0, 2.2] | 2.0 [1.6, 2.6] | 0.0 [0.0, 0.1] |
| Our test set, 331 items 8 | Spider dev, 1034 4 | |||||
|---|---|---|---|---|---|---|
| Policy | Training reward | Verified | Single-DB | Lucky | TS | Lucky share |
| Base model | – | 0.9 [0.6, 1.4] | 1.4 | 0.5 | 12.6 | 18.3 |
| SFT | – | 9.4 [8.4, 10.6] | 14.5 | 5.1 | 29.1 | 24.4 |
| GRPO, main | (single-DB) | 12.7 [11.5, 14.0] | 17.8 | 5.1 | 27.9 | 26.4 |
| GRPO, main (ours) | (CF) | 9.0 [7.9, 10.1] | 14.0 | 5.1 | 29.9 | 23.6 |
| GRPO, curriculum | (single-DB) | 7.1 [6.2, 8.2] | 11.6 | 4.4 | 27.6 | 28.3 |
Appendix figures & tables7 assets
Supplementary material from the paper’s appendix.
Appendix
| Edit | What it does | Targets |
|---|---|---|
| e_dup_order | extra active order for existing customers | F1 duplicates |
| e_new_customers | new customers in the slice, with orders | most aggregates (changing) |
| e_zero_order_customers | customers without orders | all_customers bugs |
| e_cancelled_orders | cancelled orders for existing customers | F3 missing status filter |
| e_later_tier | later tier row that changes the current tier | F4 latest row |
| e_superseded_tier | historical tier row between the first two | F4 any-row, first-row |
| Error | Category | All 48 | Lucky 18 |
|---|---|---|---|
| order date used for payment date | Attribute | 11 | 4 |
| category matched to customer columns, or hallucinated value | Value | 7 | 0 |
| extra status filter | Condition | 6 | 5 |
| no latest-row filter | Condition | 5 | 2 |
| average over items instead of orders | Clause | 5 | 0 |
| invoice adjustments left out | Table | 4 | 3 |
| Setting | SFT | GRPO (both arms) |
| Base weights | CohereLabs/tiny-aya-global , bf16 | continues the SFT adapter |
| LoRA | , , dropout 0.05, all projections | same adapter |
| Learning rate | ||
| Batch | effective 16 | 6 rollouts 4 prompts |
| Length | 4,096 tokens, loss on assistant turns | 2,048 new tokens (curriculum: 768) |
| Schedule | 2 epochs over 353 trajectories | 60 steps |
| Mutant | Family | Set, | , blind | ||
|---|---|---|---|---|---|
| all_customers | F1 | 40 | 40.0 | 40.0 | 40.0 |
| no_distinct | F1 | 203 | 100.0 | 28.6 | 7.4 |
| count_orders | F1 | 134 | 24.6 | 24.6 | 10.4 |
| count_rows | F1 | 174 | 8.6 | 8.6 | 1.1 |
| join_fanout | F2 | 208 | 30.3 | 30.3 | 20.2 |
| missing_refunds | F3 | 77 | 54.5 | 54.5 | 37.7 |
| System | EX (%) | TS (%) | Lucky / EX passes | Lucky share (%) |
|---|---|---|---|---|
| Claude | 80.6 | 68.6 | 124/833 | 14.9 [12.6, 17.5] |
| C3 | 79.7 | 68.5 | 116/824 | 14.1 [11.9, 16.6] |
| DIN | 81.5 | 72.2 | 96/843 | 11.4 [9.4, 13.7] |
| GPT4Turbo | 72.3 | 64.7 | 79/748 | 10.6 [8.6, 13.0] |
| DAIL | 82.7 | 75.3 | 76/855 | 8.9 [7.2, 11.0] |
| RASAT+PICARD | 76.5 | 70.1 | 66/791 | 8.3 [6.6, 10.5] |
| Seed | B | C | C B | B SFT | C SFT | Spider C B |
|---|---|---|---|---|---|---|
| 0 | 12.7 | 9.0 | [ , ] | [ , ] | [ , ] | [ , ] |
| 1 | 7.6 | 8.8 | [ , ] | [ , ] | [ , ] | [ , ] |
| 2 | 14.7 | 7.6 | [ , ] | [ , ] | [ , ] | [ , ] |
| Pooled | 11.7 | 8.4 | [ , ] | [ , ] | [ , ] | [ , ] |
| Seed level | [ , ] | [ , ] | [ , ] | [ , ] |
Reward Design for RL
Reward design for reinforcement learning studies how to specify, learn, and adapt signals that guide agents toward intended behavior.
See all 297 papers