Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@ Thumbs.db
*.sqlite-wal
*.build.json
semantic-weekly-report.json
# Generated benchmark reports: raw JSON is regenerable, so it is not tracked.
# The summarized, versioned evidence lives in docs/optimization/baselines.md
# (the comment here previously pointed at docs/optimization/step-16-acceptance.md,
# which does not exist in the repository).
# Generated benchmark reports; regenerate with scripts/benchmark_agent.py.
evaluation/reports/
4 changes: 2 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ the relevant area.
| Configuration / env vars | `docs/configuration.md` |
| Semantic model authoring | `docs/semantic_authoring.md`, `docs/semantic_contracts.md` |
| REST / MCP surfaces | `docs/api_reference.md`, `docs/mcp_server.md` |
| Evaluation method + frozen numbers | `docs/nl2sql_evaluation.md` |
| Evaluation method + benchmark commands | `docs/nl2sql_evaluation.md` |
| Database backends | `docs/database_adapters.md` |
| Release process | `docs/github_release.md` |
| Development checks | `docs/README.md` |
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ make web-check # Studio lint, types, build and tests
```

Evaluation separates deterministic governance checks from real-model behaviour.
See the [evaluation guide](docs/nl2sql_evaluation.md) for methods, results and limits.
See the [evaluation guide](docs/nl2sql_evaluation.md) for benchmark commands and result interpretation.

## Documentation

Expand All @@ -417,7 +417,6 @@ See the [evaluation guide](docs/nl2sql_evaluation.md) for methods, results and l
| Evaluation | [NL2SQL evaluation](docs/nl2sql_evaluation.md) |
| Reports | [Report artifacts](docs/report_artifact.md) |
| Subject scoping | [Subject tree](docs/subject_tree.md) |
| GitHub release | [First-publish checklist](docs/github_release.md) |
| Documentation index | [All guides](docs/README.md) |

## Contributing and Security
Expand Down
3 changes: 1 addition & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ make semantic-check # 语义模型漂移
make web-check # Studio lint、类型、构建与测试
```

评测区分确定性治理测试和真实模型表现,方法、结果与限制见
评测区分确定性治理测试和真实模型表现,运行方法与结果解读见
[评测文档](docs/nl2sql_evaluation.md)。

## 项目文档
Expand All @@ -399,7 +399,6 @@ make web-check # Studio lint、类型、构建与测试
| 评测 | [NL2SQL 评测](docs/nl2sql_evaluation.md) |
| 报告 | [报告产物](docs/report_artifact.md) |
| 主题域裁剪 | [Subject Tree](docs/subject_tree.md) |
| GitHub 发布 | [首次发布清单](docs/github_release.md) |
| 文档索引 | [全部指南](docs/README.md) |

## 贡献与安全
Expand Down
21 changes: 14 additions & 7 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# QueryForge Documentation

Start with whatever matches what you are trying to do.
Installation and quick-start commands are in the [project README](../README.md#quick-start).
Use the guides below for your next task.

## Start Here

Expand All @@ -19,8 +20,6 @@ Start with whatever matches what you are trying to do.

- [Agent team architecture](agent_team_architecture.md) — entry routing, orchestration, workflow
- [Database adapters](database_adapters.md) — SQLite default; DuckDB and PostgreSQL optional
- [Data Agent optimization roadmap](optimization_roadmap.md) — phased tasks, acceptance criteria and verification evidence
- [Four-stage optimization results](optimization_results.md) — delivered capabilities, measured changes and remaining acceptance

## Data

Expand All @@ -29,9 +28,17 @@ Start with whatever matches what you are trying to do.

## Evaluation

- [NL2SQL evaluation](nl2sql_evaluation.md) — gold sets, metrics, tiers, how to reproduce a run
- [End-to-end acceptance demos](demo/README.md) — runnable scenarios, no API key required
- [Evaluation guide](nl2sql_evaluation.md) — offline checks, model benchmarks and result contracts
- [Runnable examples](demo/README.md) — data publication, analysis and recovery without an API key

## Contributing
## Development Checks

- [GitHub release checklist](github_release.md)
Run from the repository root after installing the development dependencies:

```bash
./init.sh # environment and offline tests
make check # repository hygiene and offline acceptance
make semantic-check # sample semantic-model drift
make web-check # Studio lint, types, build and tests
make package # Python source and wheel distributions
```
1 change: 0 additions & 1 deletion docs/agent_team_architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Every public transport reaches one of **two** application services:
only execution kernel: the planner path reaches `DatabaseTool` through
`ToolRegistry`. Both paths share `DatabaseTool` and the SQLGlot AST policy as their
execution boundary and their safety invariants; the orchestration above them differs.
Unifying that upper layer is tracked work, not a current property.

## Five Stages

Expand Down
100 changes: 15 additions & 85 deletions docs/database_adapters.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# Database adapter contract (Step 18)
# Database Adapters

This document freezes the **read-only analytics database adapter contract** and
records the evidence behind the second backend (DuckDB) and the server-type backend
(PostgreSQL). The deployment/security view of the same feature is in
`database-adapters.md`; the plan item is
the QueryForge database-adapter programme (step 18).
QueryForge supports SQLite by default and optional DuckDB and PostgreSQL backends.
All analysis uses read-only connections and the shared DatabaseTool policy boundary.
This guide describes adapter capabilities, connection requirements and extension points.

Files:

| File | Role |
| --- | --- |
| `queryforge/infrastructure/db/adapter.py` | the frozen contract, capability registry, error taxonomy, value/type normalization |
| `queryforge/infrastructure/db/sqlite_connector.py` | unchanged SQLite default path (pre-contract connector) |
| `queryforge/infrastructure/db/adapter.py` | adapter contract, capability registry, error taxonomy, value/type normalization |
| `queryforge/infrastructure/db/sqlite_connector.py` | SQLite default backend |
| `queryforge/infrastructure/db/duckdb_connector.py` | DuckDB backend implementing the contract (optional driver) |
| `queryforge/infrastructure/db/postgres_connector.py` | PostgreSQL server backend implementing the contract (optional `psycopg` driver) |
| `queryforge/infrastructure/db/adapters.py` | factory: SQLite/DuckDB file paths, plus PostgreSQL DSNs and `*.pg` marker files; never imports a driver eagerly |
| `queryforge/infrastructure/db/__init__.py` | lazy exports: importing the package never imports duckdb |
| `tests/test_db_adapter_contract.py` | parameterised conformance suite + 18-N1 cross-backend equivalence (SQLite/DuckDB) |
| `tests/test_db_adapter_contract.py` | SQLite/DuckDB conformance and result-equivalence tests |
| `tests/test_postgres_adapter_contract.py` | always-run wiring/dialect checks + server-gated PostgreSQL conformance suite |

## 1. The contract surface
Expand Down Expand Up @@ -145,12 +143,11 @@ both spellings to `decimal`. Cross-backend comparisons must use
that reach it. But the engine role is not a complete boundary on SQLite:
`mode=ro` protects the main database only, so the engine still accepts
`ATTACH` of another file (writes *into* an attached database are refused by
`PRAGMA query_only`). That is exactly why the AST layer is load-bearing, and it
is asserted by `test_18_s1_sqlite_role_boundary_is_documented`.
`PRAGMA query_only`). The AST policy is therefore required alongside the engine boundary.
* **`execute_sql` is a trusted primitive.** A caller that invokes it directly has
already waived the policy layer; it exists because `DatabaseTool` owns the
policy check for the existing callers.
* **No credentials, no vault, no DSN passthrough.** SQLite and DuckDB adapters open
* **Local-file permissions.** SQLite and DuckDB adapters open
local files with the OS user's permissions. There is no credential vault and no
per-domain authorisation in this layer.
* **The server backend takes a DSN and stores nothing.** `open_postgres(dsn)` /
Expand All @@ -174,13 +171,11 @@ both spellings to `decimal`. Cross-backend comparisons must use
read-only transactions still allow writes to *temporary* tables (the AST policy
refuses `CREATE` outright, so only the engine layer is relaxed there), and
`SELECT ... INTO hacked FROM fact_orders` parses as a plain `SELECT` for the AST
policy, so on this backend the **engine** is what refuses it
(`test_18_s1_ast_policy_misses_select_into_and_the_server_catches_it`). The
reverse also holds: `ATTACH` is SQLite syntax, so on the PostgreSQL dialect the
policy, so on this backend the **engine** is what refuses it. `ATTACH` is SQLite
syntax, so on the PostgreSQL dialect the
refusal is a typed parse error before the server is contacted.
* **No pooling.** One adapter owns one connection and is never shared between
domains or runs; callers must `close()` it. There is no session reuse, therefore
no session-state crossover (18-C1 is out of scope for this step).
domains or runs; callers must `close()` it. There is no session reuse between runs.
* **No cost model.** `explain` returns a plan (access paths), not a calibrated
cost or wall-time estimate. PostgreSQL's plain `EXPLAIN` does print planner
`cost=` units, which is why the declaration documents them as plan output rather
Expand All @@ -200,7 +195,7 @@ both spellings to `decimal`. Cross-backend comparisons must use
than `information_schema`, whose constraint views hide rows from a non-owner — a
read-only role would otherwise appear to have no primary keys at all.

## 5. Adding a third backend
## 5. Adding a backend

1. Decide whether the engine really fits "read-only analytics database"; if it
needs credentials, pooling or per-user sessions, extend this contract first.
Expand All @@ -220,12 +215,12 @@ both spellings to `decimal`. Cross-backend comparisons must use
full fetch because the engine LIMIT already bounds the result).
6. Run the parameterised suite for the new backend: subclass the conformance mixin
in `tests/test_db_adapter_contract.py`, add its fixture builder (same DDL and
the same deterministic data) and extend the 18-N1 equivalence test.
the same deterministic data) and extend the cross-backend equivalence test.
7. A pre-contract connector (like `SQLiteConnector`) can be published unchanged
through `adapt_connector(connector)`, which adds the contract seam without
touching the connector's own behaviour.

### Adding a *server* backend (what PostgreSQL needed on top)
### Server backends

8. Route the target in `adapters.py` **without importing the driver**: a DSN scheme
and/or a marker-file suffix (see `is_postgres_target`/`resolve_postgres_dsn`), so
Expand Down Expand Up @@ -283,68 +278,3 @@ read-only role), `QUERYFORGE_TEST_POSTGRES_SCHEMA` (fixture schema prefix, defau
superuser exists — the role layer is then explicitly not claimed). Setting
`QUERYFORGE_REQUIRE_POSTGRES=1` turns a missing DSN into a **failure** instead of a
skip, so a tier-2 job cannot go green without a server.

**Unverified without a server.** The `psycopg`-side engine half of the PostgreSQL
conformance suite (18-N1 against SQLite, 18-S1's engine layer, cancellation,
`explain`, the streaming bound) executes **only** when
`QUERYFORGE_TEST_POSTGRES_DSN` is set. Without it those cases are skipped with that
reason in the skip text, which is why this document does not call them verified by
the default `python -m unittest discover -s tests -q` run. Everything that *can* be
checked without a server is checked unconditionally instead: the module and factory
import without the driver, the capability declaration is registered and consistent
with the frozen vocabulary, DSN/marker routing is asserted, a DSN read from a marker
file never appears in an error message, the SQLite/DuckDB paths stay driver-free, and
the postgres dialect's capability/bound behaviour is executed against the real
declaration.

The suite proves, against real engines:

* **18-N1** the same fixture (360-row fact table + dimension + date column, built
from the same DDL and the same deterministic data in both engines) answers the
same seven queries identically: `count`, `sum`, `group_by_join`, `window`
(`SUM(...) OVER (PARTITION BY ... ORDER BY ...)`), `cte`, `date_range`,
`empty` — compared on columns, row count and every row value with no tolerance;
plus schema-metadata parity and type-conversion parity.
* **18-S1** `INSERT`/`UPDATE`/`DELETE`/`DROP`/`CREATE`/`ATTACH`/`PRAGMA` are
refused by `AdapterPolicyError` (`decision.rule == "read_only_ast"`) before the
engine runs, the engine role independently refuses the writes on both backends,
and the fixture is verified unchanged afterwards.
* **18-R1** the SQLite path (query, schema, preview, explain, policy refusal)
keeps working while `duckdb` is made unimportable via monkeypatched
`sys.modules`/`find_spec`, the backend fails loudly only when used, and a
subprocess asserts that importing `queryforge.infrastructure.db` never imports
the driver.
* capabilities, typed-error parity (`AdapterQueryError` for an unknown table and
an unknown column from both backends), `EXPLAIN` plans, and cancellation: a
deadline (`AdapterTimeoutError`) and a client `cancel()`
(`AdapterCancelledError`) both interrupt a multi-billion-row join in well under
a second and leave the connection usable; DuckDB's bounded fetch stops at the
limit instead of materializing the whole result.

The PostgreSQL suite (`tests/test_postgres_adapter_contract.py`) runs the same
conformance mixin against a server and adds:

* **18-N1 across an embedded and a server engine**: the seven portable queries return
*identical* normalized rows from SQLite and PostgreSQL, plus logical-schema,
primary-key, nullability and type-conversion parity (the last one through the
documented `Decimal` comparison, since the two drivers report different Python
types for `DECIMAL`).
* **18-S1 with three layers**: 14 write/admin statements (including
`SET default_transaction_read_only = off`, `COPY`, `VACUUM`, `CREATE ROLE`) are
refused by `AdapterPolicyError` before the server sees them; the same statements
through the trusted `execute_sql` are refused by the server with SQLSTATE `25006`
(read-only transaction) or `42501` (insufficient privilege) — SQLSTATE, not message
text, so a localized server cannot turn the evidence green; and the
`SELECT ... INTO` gap in the AST layer is demonstrated to be closed by the engine.
* **the boundary is reported**: `SHOW default_transaction_read_only` is read back from
the server, the role identity is cross-checked against `current_user`, and
`readonly_role_verified` is asserted to mean exactly "this session is not a
superuser" (a run using `QUERYFORGE_TEST_POSTGRES_ALLOW_SUPERUSER=1` asserts that the
role layer is *not* claimed instead).
* **the bounded read really streams**: a named server cursor returns 4 of 20 000 000
rows well inside the time budget a client-side materialization could not meet, and
leaves no cursor allocated (`pg_cursors`), including on a second use of the same
fixed cursor name.
* **the declaration is probed, not asserted**: every declared date function is run on
the server, and the always-run half of the module fails if a declaration appears
without a probe.
Loading
Loading