diff --git a/docs/DESIGN.md b/docs/DESIGN.md index ab07384..e5450c9 100644 --- a/docs/DESIGN.md +++ b/docs/DESIGN.md @@ -378,8 +378,9 @@ subqueries or grouping are refused with `aggregate_shape_restricted`. Add `"cohorts": {"salaries": {"id_column": "id", "min_difference": 5}}` (the table must also be in `fixed_aggregate_sources`) to make the MCP server compare cohorts across requests. Before each aggregate it lists the ids of the rows the -aggregate covers, in the same transaction (`REPEATABLE READ` on PostgreSQL, the -snapshot of a read-only transaction on MySQL, Oracle and SQLite), keeps only +aggregate covers, in the same transaction (`REPEATABLE READ` on PostgreSQL, an +explicit read transaction on SQLite, and a read-only transaction on MySQL +and Oracle), keeps only keyed digests of them in memory, and refuses with `differencing` when the new set differs from one already served to the same session by fewer than `min_difference` rows. For example `SUM(salary)` followed by the same query diff --git a/src/sqlguardllm/mcp_server.py b/src/sqlguardllm/mcp_server.py index 608e1ec..0191c32 100644 --- a/src/sqlguardllm/mcp_server.py +++ b/src/sqlguardllm/mcp_server.py @@ -362,6 +362,9 @@ def _begin_read_only( millis = max(1, math.ceil(timeout * 1000)) if backend == "sqlite": conn.exec_driver_sql("PRAGMA query_only = ON") + if snapshot and not dbapi.in_transaction: + # SQLite's legacy driver mode does not start a transaction for SELECT. + conn.exec_driver_sql("BEGIN") deadline = time.monotonic() + timeout dbapi.set_progress_handler(lambda: int(time.monotonic() > deadline), 1000) return lambda: dbapi.set_progress_handler(None, 0) diff --git a/tests/test_mcp.py b/tests/test_mcp.py index 0fe2408..87746f5 100644 --- a/tests/test_mcp.py +++ b/tests/test_mcp.py @@ -936,6 +936,40 @@ def _add_salary(db_path: Path, ident: int, amount: int = 50) -> None: conn.execute("INSERT INTO salaries VALUES (?, 'it', ?)", (ident, amount)) +def test_cohort_and_aggregate_share_one_sqlite_snapshot(url: str, db_path: Path) -> None: + with sqlite3.connect(db_path) as writer: + assert writer.execute("PRAGMA journal_mode = WAL").fetchone() == ("wal",) + + engine = create_engine(url) + server = create_server( + make_policy(), + engine, + row_budgets=RowBudgets( + {"salaries": 100}, + 60, + fixed_aggregate_sources=["salaries"], + cohorts={"salaries": ("id", 2)}, + ), + single_session=True, + ) + updated = False + + @event.listens_for(engine, "before_cursor_execute") + def write_after_membership(_conn: Any, _cursor: Any, statement: str, *_args: Any) -> None: + nonlocal updated + if not updated and statement.startswith("SELECT SUM("): + updated = True + _add_salary(db_path, 4, 75) + + total = {"sql": "SELECT SUM(amount) AS total FROM salaries"} + try: + assert call(server, "query", total)["rows"] == [[600]] + assert updated + assert call_error(server, "query", total).startswith("differencing:") + finally: + engine.dispose() + + def test_cohort_refuses_a_total_that_differs_by_one_person(url: str, db_path: Path) -> None: server = _cohort_server(url) total = {"sql": "SELECT SUM(amount) AS total FROM salaries"}