# shayantalaei/chess architecture

> Architecture diagram of the GitHub repository shayantalaei/chess, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shayantalaei/chess
- Repository: https://github.com/shayantalaei/chess

## Overview

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: [`src/workflow/team_builder.py`](https://github.com/shayantalaei/chess/blob/main/src/workflow/team_builder.py) (Team builder), [`src/runner/run_manager.py`](https://github.com/shayantalaei/chess/blob/main/src/runner/run_manager.py) (Run manager), [`src/llm/prompts.py`](https://github.com/shayantalaei/chess/blob/main/src/llm/prompts.py) (Prompts), [`src/workflow/agents/candidate_generator/candidate_generator.py`](https://github.com/shayantalaei/chess/blob/main/src/workflow/agents/candidate_generator/candidate_generator.py) (CG agent)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_main["Main<br/>entrypoint<br/>[main.py]"]
  node_run_manager["Run manager<br/>orchestrator<br/>[run_manager.py]"]
  node_task["Task<br/>[task.py]"]
  node_database_manager["DB manager<br/>runtime db layer"]
  node_logger["Logger<br/>logging<br/>[logger.py]"]
  node_statistics_manager["Stats<br/>metrics"]
end

subgraph group_workflow["Workflow"]
  node_team_builder["Team builder<br/>workflow builder<br/>[team_builder.py]"]
  node_system_state["System state<br/>shared state<br/>[system_state.py]"]
  node_sql_meta_info["SQL meta<br/>generation hints<br/>[sql_meta_info.py]"]
end

subgraph group_agents["Agents"]
  node_information_retriever["IR agent<br/>retrieval agent"]
  node_schema_selector["SS agent<br/>schema pruning agent<br/>[schema_selector.py]"]
  node_candidate_generator["CG agent<br/>sql synthesis agent"]
  node_unit_tester["UT agent<br/>verification agent<br/>[unit_tester.py]"]
end

subgraph group_database["Database"]
  node_preprocess["Preprocess<br/>offline indexing<br/>[preprocess.py]"]
  node_db_catalog_preprocess["Catalog prep<br/>schema index build<br/>[preprocess.py]"]
  node_db_catalog_search["Catalog search<br/>schema retrieval<br/>[search.py]"]
  node_db_values_preprocess["Value prep<br/>value index build<br/>[preprocess.py]"]
  node_db_values_search["Value search<br/>value retrieval<br/>[search.py]"]
  node_schema["Schema<br/>[schema.py]"]
  node_execution["Execution<br/>[execution.py]"]
  node_sql_parser["SQL parser<br/>sql parsing<br/>[sql_parser.py]"]
  node_db_info["DB info<br/>db metadata<br/>[db_info.py]"]
end

subgraph group_llm["LLM"]
  node_engine_configs["Engine cfg<br/>llm config<br/>[engine_configs.py]"]
  node_models["Models<br/>llm backend<br/>[models.py]"]
  node_parsers["Parsers<br/>response parsing<br/>[parsers.py]"]
  node_prompts["Prompts<br/>prompt registry<br/>[prompts.py]"]
end

subgraph group_ops["Ops"]
  node_template_generate_candidate["Gen SQL prompt<br/>prompt template"]
  node_template_select_tables["Select tables<br/>prompt template"]
  node_template_generate_unit_tests["Unit tests<br/>prompt template"]
  node_template_evaluate["Evaluate<br/>prompt template"]
  node_config_ir_cg_ut["IR-CG-UT cfg<br/>run config"]
  node_config_ir_ss_cg["IR-SS-CG cfg<br/>run config"]
  node_run_main_ir_cg_ut["Run script<br/>shell runner"]
  node_run_main_ir_ss_cg["Run script<br/>shell runner"]
end

node_main -->|"starts"| node_run_manager
node_run_manager -->|"loads db"| node_database_manager
node_run_manager -->|"loads task"| node_task
node_run_manager -->|"builds team"| node_team_builder
node_team_builder -->|"shares state"| node_system_state
node_team_builder -->|"adds hints"| node_sql_meta_info
node_team_builder -->|"adds IR"| node_information_retriever
node_team_builder -->|"adds SS"| node_schema_selector
node_team_builder -->|"adds CG"| node_candidate_generator
node_team_builder -->|"adds UT"| node_unit_tester
node_information_retriever -->|"retrieves schema"| node_db_catalog_search
node_information_retriever -->|"retrieves values"| node_db_values_search
node_schema_selector -->|"prunes schema"| node_schema
node_candidate_generator -->|"uses prompts"| node_prompts
node_candidate_generator -->|"calls LLM"| node_models
node_candidate_generator -->|"parses output"| node_parsers
node_unit_tester -->|"checks SQL"| node_execution
node_unit_tester -->|"parses verdict"| node_parsers
node_preprocess -->|"builds catalog index"| node_db_catalog_preprocess
node_preprocess -->|"builds value index"| node_db_values_preprocess
node_db_catalog_preprocess -->|"feeds retrieval"| node_db_catalog_search
node_db_values_preprocess -->|"feeds retrieval"| node_db_values_search
node_run_manager -->|"logs"| node_logger
node_run_manager -->|"tracks metrics"| node_statistics_manager
node_engine_configs -->|"configures"| node_models
node_prompts -->|"loads template"| node_template_generate_candidate
node_prompts -->|"loads template"| node_template_select_tables
node_prompts -->|"loads template"| node_template_generate_unit_tests
node_prompts -->|"loads template"| node_template_evaluate
node_config_ir_cg_ut -->|"drives"| node_run_main_ir_cg_ut
node_config_ir_ss_cg -->|"drives"| node_run_main_ir_ss_cg
node_run_main_ir_cg_ut -->|"launches"| node_main
node_run_main_ir_ss_cg -->|"launches"| node_main

click node_main "https://github.com/shayantalaei/chess/blob/main/src/main.py"
click node_run_manager "https://github.com/shayantalaei/chess/blob/main/src/runner/run_manager.py"
click node_task "https://github.com/shayantalaei/chess/blob/main/src/runner/task.py"
click node_database_manager "https://github.com/shayantalaei/chess/blob/main/src/runner/database_manager.py"
click node_logger "https://github.com/shayantalaei/chess/blob/main/src/runner/logger.py"
click node_statistics_manager "https://github.com/shayantalaei/chess/blob/main/src/runner/statistics_manager.py"
click node_team_builder "https://github.com/shayantalaei/chess/blob/main/src/workflow/team_builder.py"
click node_system_state "https://github.com/shayantalaei/chess/blob/main/src/workflow/system_state.py"
click node_sql_meta_info "https://github.com/shayantalaei/chess/blob/main/src/workflow/sql_meta_info.py"
click node_information_retriever "https://github.com/shayantalaei/chess/blob/main/src/workflow/agents/information_retriever/information_retriever.py"
click node_schema_selector "https://github.com/shayantalaei/chess/blob/main/src/workflow/agents/schema_selector/schema_selector.py"
click node_candidate_generator "https://github.com/shayantalaei/chess/blob/main/src/workflow/agents/candidate_generator/candidate_generator.py"
click node_unit_tester "https://github.com/shayantalaei/chess/blob/main/src/workflow/agents/unit_tester/unit_tester.py"
click node_preprocess "https://github.com/shayantalaei/chess/blob/main/src/preprocess.py"
click node_db_catalog_preprocess "https://github.com/shayantalaei/chess/blob/main/src/database_utils/db_catalog/preprocess.py"
click node_db_catalog_search "https://github.com/shayantalaei/chess/blob/main/src/database_utils/db_catalog/search.py"
click node_db_values_preprocess "https://github.com/shayantalaei/chess/blob/main/src/database_utils/db_values/preprocess.py"
click node_db_values_search "https://github.com/shayantalaei/chess/blob/main/src/database_utils/db_values/search.py"
click node_schema "https://github.com/shayantalaei/chess/blob/main/src/database_utils/schema.py"
click node_execution "https://github.com/shayantalaei/chess/blob/main/src/database_utils/execution.py"
click node_sql_parser "https://github.com/shayantalaei/chess/blob/main/src/database_utils/sql_parser.py"
click node_db_info "https://github.com/shayantalaei/chess/blob/main/src/database_utils/db_info.py"
click node_engine_configs "https://github.com/shayantalaei/chess/blob/main/src/llm/engine_configs.py"
click node_models "https://github.com/shayantalaei/chess/blob/main/src/llm/models.py"
click node_parsers "https://github.com/shayantalaei/chess/blob/main/src/llm/parsers.py"
click node_prompts "https://github.com/shayantalaei/chess/blob/main/src/llm/prompts.py"
click node_template_generate_candidate "https://github.com/shayantalaei/chess/blob/main/templates/template_generate_candidate_retrieval.txt"
click node_template_select_tables "https://github.com/shayantalaei/chess/blob/main/templates/template_select_tables.txt"
click node_template_generate_unit_tests "https://github.com/shayantalaei/chess/blob/main/templates/template_generate_unit_tests.txt"
click node_template_evaluate "https://github.com/shayantalaei/chess/blob/main/templates/template_evaluate.txt"
click node_config_ir_cg_ut "https://github.com/shayantalaei/chess/blob/main/run/configs/CHESS_IR_CG_UT.yaml"
click node_config_ir_ss_cg "https://github.com/shayantalaei/chess/blob/main/run/configs/CHESS_IR_SS_CG.yaml"
click node_run_main_ir_cg_ut "https://github.com/shayantalaei/chess/blob/main/run/run_main_ir_cg_ut.sh"
click node_run_main_ir_ss_cg "https://github.com/shayantalaei/chess/blob/main/run/run_main_ir_ss_cg.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_run_manager,node_task,node_database_manager,node_logger,node_statistics_manager toneBlue
class node_team_builder,node_system_state,node_sql_meta_info toneAmber
class node_information_retriever,node_schema_selector,node_candidate_generator,node_unit_tester toneMint
class node_preprocess,node_db_catalog_preprocess,node_db_catalog_search,node_db_values_preprocess,node_db_values_search,node_schema,node_execution,node_sql_parser,node_db_info toneRose
class node_engine_configs,node_models,node_parsers,node_prompts toneIndigo
class node_template_generate_candidate,node_template_select_tables,node_template_generate_unit_tests,node_template_evaluate,node_config_ir_cg_ut,node_config_ir_ss_cg,node_run_main_ir_cg_ut,node_run_main_ir_ss_cg toneTeal
```

## Components

### Runtime

Entry points and execution harness

- [Main](https://github.com/shayantalaei/chess/blob/main/src/main.py): entrypoint · `src/main.py`
- [Run manager](https://github.com/shayantalaei/chess/blob/main/src/runner/run_manager.py): orchestrator · `src/runner/run_manager.py`
- [Task](https://github.com/shayantalaei/chess/blob/main/src/runner/task.py): task model · `src/runner/task.py`
- [DB manager](https://github.com/shayantalaei/chess/blob/main/src/runner/database_manager.py): runtime db layer · `src/runner/database_manager.py`
- [Logger](https://github.com/shayantalaei/chess/blob/main/src/runner/logger.py): logging · `src/runner/logger.py`
- [Stats](https://github.com/shayantalaei/chess/blob/main/src/runner/statistics_manager.py): metrics · `src/runner/statistics_manager.py`

### Workflow

Agent orchestration and shared state

- [Team builder](https://github.com/shayantalaei/chess/blob/main/src/workflow/team_builder.py): workflow builder · `src/workflow/team_builder.py`
- [System state](https://github.com/shayantalaei/chess/blob/main/src/workflow/system_state.py): shared state · `src/workflow/system_state.py`
- [SQL meta](https://github.com/shayantalaei/chess/blob/main/src/workflow/sql_meta_info.py): generation hints · `src/workflow/sql_meta_info.py`

### Agents

Specialized text-to-SQL stages

- [IR agent](https://github.com/shayantalaei/chess/blob/main/src/workflow/agents/information_retriever/information_retriever.py): retrieval agent · `src/workflow/agents/information_retriever/information_retriever.py`
- [SS agent](https://github.com/shayantalaei/chess/blob/main/src/workflow/agents/schema_selector/schema_selector.py): schema pruning agent · `src/workflow/agents/schema_selector/schema_selector.py`
- [CG agent](https://github.com/shayantalaei/chess/blob/main/src/workflow/agents/candidate_generator/candidate_generator.py): sql synthesis agent · `src/workflow/agents/candidate_generator/candidate_generator.py`
- [UT agent](https://github.com/shayantalaei/chess/blob/main/src/workflow/agents/unit_tester/unit_tester.py): verification agent · `src/workflow/agents/unit_tester/unit_tester.py`

### Database

Schema, execution, and retrieval assets

- [Preprocess](https://github.com/shayantalaei/chess/blob/main/src/preprocess.py): offline indexing · `src/preprocess.py`
- [Catalog prep](https://github.com/shayantalaei/chess/blob/main/src/database_utils/db_catalog/preprocess.py): schema index build · `src/database_utils/db_catalog/preprocess.py`
- [Catalog search](https://github.com/shayantalaei/chess/blob/main/src/database_utils/db_catalog/search.py): schema retrieval · `src/database_utils/db_catalog/search.py`
- [Value prep](https://github.com/shayantalaei/chess/blob/main/src/database_utils/db_values/preprocess.py): value index build · `src/database_utils/db_values/preprocess.py`
- [Value search](https://github.com/shayantalaei/chess/blob/main/src/database_utils/db_values/search.py): value retrieval · `src/database_utils/db_values/search.py`
- [Schema](https://github.com/shayantalaei/chess/blob/main/src/database_utils/schema.py): db schema utils · `src/database_utils/schema.py`
- [Execution](https://github.com/shayantalaei/chess/blob/main/src/database_utils/execution.py): sql execution · `src/database_utils/execution.py`
- [SQL parser](https://github.com/shayantalaei/chess/blob/main/src/database_utils/sql_parser.py): sql parsing · `src/database_utils/sql_parser.py`
- [DB info](https://github.com/shayantalaei/chess/blob/main/src/database_utils/db_info.py): db metadata · `src/database_utils/db_info.py`

### LLM

Model configuration and prompt plumbing

- [Engine cfg](https://github.com/shayantalaei/chess/blob/main/src/llm/engine_configs.py): llm config · `src/llm/engine_configs.py`
- [Models](https://github.com/shayantalaei/chess/blob/main/src/llm/models.py): llm backend · `src/llm/models.py`
- [Parsers](https://github.com/shayantalaei/chess/blob/main/src/llm/parsers.py): response parsing · `src/llm/parsers.py`
- [Prompts](https://github.com/shayantalaei/chess/blob/main/src/llm/prompts.py): prompt registry · `src/llm/prompts.py`

### Ops

Run scripts, configs, and outputs

- [Gen SQL prompt](https://github.com/shayantalaei/chess/blob/main/templates/template_generate_candidate_retrieval.txt): prompt template · `templates/template_generate_candidate_retrieval.txt`
- [Select tables](https://github.com/shayantalaei/chess/blob/main/templates/template_select_tables.txt): prompt template · `templates/template_select_tables.txt`
- [Unit tests](https://github.com/shayantalaei/chess/blob/main/templates/template_generate_unit_tests.txt): prompt template · `templates/template_generate_unit_tests.txt`
- [Evaluate](https://github.com/shayantalaei/chess/blob/main/templates/template_evaluate.txt): prompt template · `templates/template_evaluate.txt`
- [IR-CG-UT cfg](https://github.com/shayantalaei/chess/blob/main/run/configs/CHESS_IR_CG_UT.yaml): run config · `run/configs/CHESS_IR_CG_UT.yaml`
- [IR-SS-CG cfg](https://github.com/shayantalaei/chess/blob/main/run/configs/CHESS_IR_SS_CG.yaml): run config · `run/configs/CHESS_IR_SS_CG.yaml`
- [Run script](https://github.com/shayantalaei/chess/blob/main/run/run_main_ir_cg_ut.sh): shell runner · `run/run_main_ir_cg_ut.sh`
- [Run script](https://github.com/shayantalaei/chess/blob/main/run/run_main_ir_ss_cg.sh): shell runner · `run/run_main_ir_ss_cg.sh`

## Connections

- 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
