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: 3 additions & 2 deletions docs/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/sqlguardllm/mcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions tests/test_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Loading