How shayantalaei/chess works
CHESS is a multi-agent text-to-SQL system organized as a pipeline around four specialized agents: Information Retriever, Schema Selector, Candidate Generator, and Unit Tester. The repository is split into an offline preparation path and an online inference/evaluation path, with shared utilities for database access, prompt construction, and LLM calls.
1. Top-level flow
The main runtime starts from src/main.py and is driven by src/runner/run_manager.py. For each question/database task, CHESS loads the task, builds the agent team, runs the agents in sequence, and records the final SQL plus intermediate statistics/logs.
2. Inputs and configuration
The system is configured through .env values and YAML files under run/configs/. Inputs are the BIRD-style dataset files under data/ and the database root directory. The repo also includes a sub-sampled dev set for ablations. Shell scripts in run/ select the experiment mode and wiring of agents.
3. Offline preprocessing boundary
src/preprocess.py and the modules under src/database_utils/db_catalog/ and src/database_utils/db_values/ build the retrieval assets used at runtime. This step creates searchable representations of schema/catalog metadata and database values, including minhash/LSH/vector-style indexes mentioned in the README. This preprocessing is required before inference can retrieve relevant context efficiently.
4. Database utility layer
src/database_utils/ is the low-level database boundary. It contains schema extraction/generation, SQL parsing, execution, and catalog/value search utilities. This layer isolates direct interaction with database files and query execution from the LLM agents, so the agents consume structured context rather than raw database internals.
5. Information Retriever agent
The IR agent under src/workflow/agents/information_retriever/ extracts question keywords and retrieves relevant schema/value context. Its tool kit is responsible for finding entities and contextual evidence from the prebuilt indexes, reducing the amount of database context passed downstream.
6. Schema Selector agent
The SS agent under src/workflow/agents/schema_selector/ prunes large schemas into a smaller working sub-schema. Its tools select tables, select columns, and filter columns. This is the main mechanism for handling large databases and is a key architectural boundary between “full catalog” and “candidate schema” stages.
7. Candidate Generator agent
The CG agent under src/workflow/agents/candidate_generator/ generates SQL candidates and revises them iteratively. It has separate tool logic for initial generation and revision, and it depends on the reduced context produced by IR/SS. This stage is where most prompt-driven synthesis happens.
8. Unit Tester agent
The UT agent under src/workflow/agents/unit_tester/ validates candidate SQL using LLM-generated natural-language unit tests. It generates tests and evaluates whether a candidate satisfies them, acting as a verification gate before final output. This is the main correctness-checking subsystem.
9. LLM and prompt abstraction
src/llm/ centralizes model configuration, prompt templates, and parsing of model responses. The templates/ directory stores the actual prompt text for each agent and tool. This separation makes the system model-agnostic and lets the same workflow target different LLM backends.
10. Workflow coordination
src/workflow/ contains the orchestration logic that connects agents into a shared stateful process. team_builder.py assembles the agent team, system_state.py tracks intermediate context, and sql_meta_info.py likely stores metadata used to steer SQL generation. The workflow layer is the boundary that turns isolated agent capabilities into a multi-step synthesis pipeline.
11. Execution, logging, and metrics
src/runner/ provides the operational harness: database loading, task management, statistics collection, and logging. results/ stores outputs, and run/ contains scripts for reproducible runs. This layer is responsible for experiment bookkeeping rather than SQL reasoning.
12. External/service dependencies
The architecture assumes access to a local or remote retrieval/index service via INDEX_SERVER_HOST and INDEX_SERVER_PORT, plus an LLM provider configured through API keys and cloud credentials. In practice, CHESS combines local database/index processing with external LLM inference to generate and validate SQL.
Main entry points: team_builder.py Team builder · run_manager.py Run manager · prompts.py Prompts · candidate_generator.py CG agent
Components 34
Runtime
Entry points and execution harness
- Main
src/main.py - Run manager
src/runner/run_manager.py - Task
src/runner/task.py - DB manager
src/runner/database_manager.py - Logger
src/runner/logger.py - Stats
src/runner/statistics_manager.py
Workflow
Agent orchestration and shared state
- Team builder
src/workflow/team_builder.py - System state
src/workflow/system_state.py - SQL meta
src/workflow/sql_meta_info.py
Agents
Specialized text-to-SQL stages
Database
Schema, execution, and retrieval assets
- Preprocess
src/preprocess.py - Catalog prep
src/database_utils/db_catalog/preprocess.py - Catalog search
src/database_utils/db_catalog/search.py - Value prep
src/database_utils/db_values/preprocess.py - Value search
src/database_utils/db_values/search.py - Schema
src/database_utils/schema.py - Execution
src/database_utils/execution.py - SQL parser
src/database_utils/sql_parser.py - DB info
src/database_utils/db_info.py
LLM
Model configuration and prompt plumbing
- Engine cfg
src/llm/engine_configs.py - Models
src/llm/models.py - Parsers
src/llm/parsers.py - Prompts
src/llm/prompts.py
Ops
Run scripts, configs, and outputs
- Gen SQL prompt
templates/template_generate_candidate_retrieval.txt - Select tables
templates/template_select_tables.txt - Unit tests
templates/template_generate_unit_tests.txt - Evaluate
templates/template_evaluate.txt - IR-CG-UT cfg
run/configs/CHESS_IR_CG_UT.yaml - IR-SS-CG cfg
run/configs/CHESS_IR_SS_CG.yaml - Run script
run/run_main_ir_cg_ut.sh - Run script
run/run_main_ir_ss_cg.sh
Connections 33
- Main → Run manager: starts
- Run manager → DB manager: loads db
- Run manager → Task: loads task
- Run manager → Team builder: builds team
- Team builder → System state: shares state
- Team builder → SQL meta: adds hints
- Team builder → IR agent: adds IR
- Team builder → SS agent: adds SS
- Team builder → CG agent: adds CG
- Team builder → UT agent: adds UT
- IR agent → Catalog search: retrieves schema
- IR agent → Value search: retrieves values
- SS agent → Schema: prunes schema
- CG agent → Prompts: uses prompts
- CG agent → Models: calls LLM
- CG agent → Parsers: parses output
- UT agent → Execution: checks SQL
- UT agent → Parsers: parses verdict
- Preprocess → Catalog prep: builds catalog index
- Preprocess → Value prep: builds value index
- Catalog prep → Catalog search: feeds retrieval
- Value prep → Value search: feeds retrieval
- Run manager → Logger: logs
- Run manager → Stats: tracks metrics
- Engine cfg → Models: configures
- Prompts → Gen SQL prompt: loads template
- Prompts → Select tables: loads template
- Prompts → Unit tests: loads template
- Prompts → Evaluate: loads template
- IR-CG-UT cfg → Run script: drives
- IR-SS-CG cfg → Run script: drives
- Run script → Main: launches
- Run script → Main: launches