文章摘要
Inenterprise-leveldataanalysis,enablinglargelanguagemodelstodirectlygeneratecorrectSQLfromnaturallan

In enterprise-level data analysis, enabling large language models to directly generate correct SQL from natural language queries has long been an elusive goal. Traditional Text-to-SQL methods simplify the task to a single-round text generation: given a query and static database schema, the model outputs the final SQL in one step. However, real-world databases often contain hundreds of tables, thousands of fields, and millions of data entries. Experienced analysts do not "guess" the SQL directly; instead, they use command-line interfaces or IDEs to execute commands like `SHOW TABLES`, explore table structures via DDL, run test queries, and revise logic repeatedly until the query works.

On long-interaction benchmarks such as Spider2.0, BIRD-Interact, and LiveSQLBench, traditional static fine-tuned models show critical flaws: they only learn to output the final answer during training, but lack the ability to explore, trial-and-error, track state, or recover from errors when deployed in interactive environments—this is known as the "training-deployment mismatch" problem.

To address this bottleneck, a team from institutions including a Hong Kong university and Microsoft has developed the first original long-interaction training framework HarnessSQL, which performs long trajectory supervised fine-tuning (SFT) and execution reward reinforcement learning (RL) in closed sandboxes and dedicated SQL Harness environments.

On the Spider 2.0-SQLite benchmark, HarnessSQL with only 8B parameters achieved an execution accuracy of 45.2%—nearly three times the baseline. This performance far exceeds specialized 32B models like OmniSQL (14.8%), Arctic-Text2SQL (16.3%), and multi-agent systems like MARS-SQL (12.6%). For 14B parameter models, the accuracy further rose to 54.8%.

Problem Formalization and the Training-Deployment Mismatch

Interactive Text-to-SQL can be formalized as a Partially Observable Markov Decision Process (POMDP) mediated by an Execution Harness. A task instance is defined as a quadruple x = (q, D, h, v), where q is the natural language query, D is the underlying database, h is the execution Harness, and v is the hidden ground-truth verifier.

In the t-th round of interaction (t ∈ [1, T]), the agent generates reasoning and tool-use actions a_t based on environmental observation o_t via policy π_θ. The complete trajectory consists of sequential observations, actions, and the final SQL submitted at the termination step. The task success is evaluated by a combined reward function with two key components: result accuracy (verifying if the SQL execution matches the ground truth) and protocol compliance (checking if the interaction follows the Harness rules like read-only access and timely submission).

Existing post-training frameworks still rely on traditional static supervised loss, which leads to three core mismatches: 1. Discarding intermediate exploration states: Only the final SQL is targeted, ignoring the interaction process of exploring table structures and verifying hypotheses; 2. Blindness to execution feedback: Models never encounter runtime errors or empty results during training, leading to crashes when facing errors in inference; 3. Lack of action boundary constraints: Models do not understand permission boundaries or termination protocols of database tools, leading to tool format hallucinations.

Forging Agents in Real Execution Sandboxes

To break these mismatches, HarnessSQL builds three core foundations: high-fidelity closed execution sandboxes, a minimalist SQL Harness contract, and a two-stage long-interaction post-training pipeline.

1. Closed SQL Execution Sandbox and Blueprint Synthesis

The team constructed 79 complex database sandboxes based on Spider 2.0-Lite (30 SQLite databases) and Spider 2.0-DBT (49 DuckDB databases, zero-loss migrated to SQLite). They adopted the SQL-First blueprint synthesis method: extract schema, sample values, and foreign key paths from active engines, sample executable SQL with controllable difficulty, and reverse-generate natural language queries and verification oracles.

The sandbox features read-only binding, single-statement constraints, and 60-second timeout protection, and ensures data quality via syntax, non-null, and semantic mutation tests.

2. Minimalist Harness Contract (DSH-SQL) for SQL Tasks

General-purpose programming agents' complex Bash tools often distract models. HarnessSQL modified DeepSeek-Harness (DSH) to extract a four-element minimalist action contract: - sql_list_tables: Enumerate all tables in the database and their row counts; - sql_schema: Explore the complete DDL definition, field types, and real sample data of a specified table; - sql_exec: Try executing read-only SQL queries to get front-row outputs or runtime error stacks; - sql_submit: Finalize and submit the verified correct SQL and exit the long-interaction session.

The environment middleware also includes three safeguards: deterministic row count truncation to prevent large outputs from exceeding context limits, circular call alarms to actively correct repeated invalid queries, and pressure-triggered context compression to retain core facts during long exploration.

3. Native Harness Two-Stage Post-Training: SFT Foundation + RL Optimization

First Stage: Full-Trajectory Long SFT (Cold Start) Using a teacher model to explore sandboxes autonomously, high-quality multi-round trajectories (average 11.8K tokens, 16 interactions, 14 real executions) verified by oracles are selected for full-sequence supervision. The key mechanism is: during loss calculation, all environment observation tokens are masked out, and gradients are only computed on the model's generated reasoning, tool calls, and final submissions, forcing the model to learn to deduce next actions based on environmental feedback.

Second Stage: Execution Reward Reinforcement Learning (Self-Evolution) To endow models with trial-and-error ability for unknown databases, HarnessSQL uses Dynamic Adaptive Policy Optimization (DAPO) to continuously align in sandboxes, calculating standardized advantages for trajectory groups. The reward signal purely relies on real execution comparisons with hidden oracles: 1 point for equivalent and compliant results, 0 points for errors, timeouts, or mismatched results. No artificial intermediate rewards are set, forcing models to develop robust self-correction strategies.

Experimental Evaluation

The team conducted evaluations on three real database benchmarks: the in-domain long-interaction benchmark Spider 2.0-SQLite, the out-domain dynamic interaction benchmark BIRD-Interact Mini with user simulators, and the cross-domain complex reasoning benchmark LiveSQLBench Base-Lite SQLite.

Baselines include general programming models (Qwen2.5-Coder-32B), state-of-the-art SQL-specific models (OmniSQL 7B/32B, Arctic-Text2SQL 7B/32B), and multi-agent systems (MARS-SQL 3×7B), all evaluated using execution accuracy.

Three core experimental results: 1. Small 8B models outperform 32B models: On Spider 2.0-SQLite, HarnessSQL-8B reached 45.2% accuracy, nearly three times the baseline (15.5%), surpassing OmniSQL-32B (14.8%), Arctic-Text2SQL-32B (16.3%), and Qwen2.5-Coder-32B (16.3%). 2. 14B models approach 55% accuracy: Scaling to Qwen3-14B, accuracy rose to 54.8%, verifying the excellent scalability of native Harness training. 3. Strong out-domain generalization: On unseen BIRD-Interact Mini and LiveSQLBench, HarnessSQL also led. On LiveSQLBench, 8B (24.8%) and 14B (28.5%) models achieved 3-4 times the performance of all 32B baselines (7.0%-8.9%), proving the model learned general exploration strategies rather than memorizing schemas mechanically.

In-depth Analysis

For interactive tasks, is direct reinforcement learning (Direct RL) better or trajectory fine-tuning (SFT) first? Controlled experiments show clear results: - Cold-start Direct RL has limited improvement: 8B base model only increased from 15.5% to 20.0% (+4.5%). Without interaction priors, sparse binary rewards struggle to allocate credit effectively in long trajectories. - Full-trajectory SFT lays interaction foundation: After fine-tuning on verified trajectories, 8B and 14B models rose to 30.4% (+14.9%) and 37.8% (+15.6%) respectively, quickly mastering tool calls and error correction norms. - SFT+RL achieves qualitative performance leap: Adding DAPO RL on SFT, 8B increased by 14.8% to 45.2%, and 14B increased by 17.0% to 54.8% (gain over three times Direct RL). This establishes the gold standard for long-interaction agents: SFT provides interaction frameworks and exploration common sense, while RL releases adaptive optimization potential in unknown environments.

Four Ablation Studies

Focusing on key design choices for long-interaction agent training, ablation studies yield four core findings: 1. Strong Alignment Between Training and Inference Interfaces: If inference uses DSH-SQL interactive environment but training uses non-interactive SFT data, Spider2.0 accuracy drops to only 9.6%, even lower than the untuned base (15.5%). Native Harness SFT reaches 30.4%. This proves static training breaks model-environment collaboration, so training and deployment environments must be strictly isomorphic. 2. Full-Trajectory Organization Outperforms Single-Step Decomposition: When building SFT data, full-sequence supervision retaining complete trajectory context outperforms single-step decomposition on all three benchmarks. Long-interaction tasks rely on causal connections across multiple rounds; splitting samples disrupts global decision coherence. 3. Ultra-Long Context is the Lifeline of Long Exploration: Real database exploration involves dense schemas and intermediate results, with trajectories often containing tens of thousands of tokens. When training and inference contexts expand from 8K to 16K and 32K, Spider2.0 accuracy jumps from 27.4% to 45.2%—truncating early history deprives models of review and traceability memory. 4. DAPO Algorithm is More Suitable for Long Policy Optimization Under Sparse Rewards: Comparing GRPO (40.7%), GSPO (43.7%), and DAPO (45.2%) under same budget and sandboxes, DAPO performs best. Its dynamic filtering of zero-variance trajectory groups, decoupled clipping range, and full-batch token normalization effectively reduce policy oscillation under sparse rewards.

Notable Boundaries and Future Evolution

The team also points out the system's future directions: 1. Cross-Database Dialect Expansion: Extend beyond current SQLite to enterprise cloud-native dialects like Snowflake, BigQuery, and PostgreSQL, building better permission isolation and ecological adaptation. 2. Multimodal and Diverse Analysis Tools: Beyond command-line queries, integrate visual charts, document reports, and interactive BI dashboards into the environment.

Conclusion and Outlook

Large models are accelerating from "conversation assistants" to "autonomous execution agents". HarnessSQL proves that for complex tasks requiring tool calls and trial-and-error feedback, only teaching static "correct answers" is insufficient. The interactive environment (Harness) must be integrated into the core training loop, allowing models to naturally learn long-interaction exploration and fault tolerance in dynamic environments.

Currently, HarnessSQL's code, sandbox environment, and synthetic trajectory assets are all open-sourced, providing a reusable benchmark for Text-to-SQL, database agents, and general agent research.

以上内容不代表本平台立场,仅供读者参考