Repository navigation
fix(eval): open the benchmark DB read-only and cap every query with a budget - #142
Open
ddaihao2022 wants to merge 1 commit into
Open
ddaihao2022 wants to merge 1 commit into
ddaihao2022 wants to merge 1 commit into
Conversation
… budget Closes LLMSQL#96 Evaluation runs SQL that was extracted from model output against a database that is cached under `workdir_path` and reused by every later run. Two ways that goes wrong: 1. Nothing stops a destructive statement. `connect_sqlite()` handed out a regular read-write connection, so one `DROP TABLE` / `DELETE FROM` candidate silently corrupts `sqlite_tables.db` for good — and the issue notes the extractor is the only thing keeping `find_sql()` results SELECT-only, which is not a guarantee. 2. Nothing bounds a single statement. A pathological query (self-join, unbounded recursive CTE) hangs evaluation forever with no way back short of killing the process. Changes: - `connect_sqlite()` opens `file:<abs path>?mode=ro` via URI mode and turns on `PRAGMA query_only`. `Path.resolve().as_uri()` takes care of absolute paths and escaping, so paths with spaces or `#` don't break the URI. Write attempts now fail, which `execute_sql` already reports as `None`. - `execute_sql(conn, sql, query_timeout=DEFAULT_QUERY_TIMEOUT)` installs a `set_progress_handler` callback with a monotonic deadline; returning non-zero aborts the statement, and the existing `except Exception` turns that into `None`. The handler lives on the connection, so it is always removed again in a `finally` — otherwise the next statement on that connection would inherit an expired deadline. `query_timeout <= 0` restores the previous unbounded behaviour. - The budget is threaded through `evaluate_sample(..., query_timeout=...)` and `evaluate(..., query_timeout=...)`, defaulting to 5s per statement. Tests (tests/evaluation/test_db_safety.py, 16 cases): - `DROP`/`DELETE`/`UPDATE`/`INSERT`/`ALTER` all fail and leave the data intact. - An unbounded recursive CTE is cut off within its budget, the connection stays usable afterwards, and it is counted as a SQL error by `evaluate_sample`. - The progress handler is installed and removed on both success and failure, and not installed at all when the budget is disabled. - `evaluate()` forwards `query_timeout` down to `evaluate_sample`. Verified: full suite goes 197 -> 213 passed with the same 7 pre-existing failures (all `transformers`-missing or Windows-path related in this environment). Reverting the read-only connection makes all 5 destructive statement tests fail, confirming they bite.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #96
What this fixes
Evaluation runs SQL extracted from model output against a database that is
cached under
workdir_pathand reused by every later run. Two things wereunguarded:
connect_sqlite()handed out aregular read-write connection, so a single
DROP TABLE/DELETE FROMcandidate silently corrupts
sqlite_tables.dbpermanently. As the issuenotes, the only thing keeping statements read-only today is
find_sql()requiring a leading
SELECT— that is a property of the extractor, not aguarantee from the execution layer, and it disappears the moment the
extractor changes.
unbounded recursive CTE) hangs evaluation forever, with no way back short of
killing the process.
Changes
1. Read-only connection (
llmsql/utils/evaluation_utils.py)resolve().as_uri()gives an absolute, properly escaped URI, so paths withspaces or
#don't break it. Write attempts now fail, whichexecute_sqlalready reports as
None→ counted as a SQL error. Reads are unaffected.FileNotFoundErrorbehaviour for a missing DB is unchanged.2. Per-statement time budget
execute_sql(conn, sql, query_timeout=DEFAULT_QUERY_TIMEOUT)installs aset_progress_handlercallback with a monotonic deadline; returning non-zeroaborts the statement, which the existing
except Exceptionturns intoNone.The handler lives on the connection, so it is always removed again in a
finally— otherwise the next statement on that connection would inherit analready-expired deadline and abort immediately.
query_timeout <= 0restoresthe previous unbounded behaviour.
Default is
DEFAULT_QUERY_TIMEOUT = 5.0seconds, checked everyPROGRESS_HANDLER_OPS = 1000VM instructions.3. Wired through
evaluate_sample(..., query_timeout=...)andevaluate(..., query_timeout=...), so callers can tune or disable it.Tests
New
tests/evaluation/test_db_safety.py— 16 cases:DROP TABLE/DELETE FROM/UPDATE/INSERT/ALTER TABLEall failand leave the data intact (DB is reopened independently to confirm).
FileNotFoundError.usable afterwards, and
evaluate_samplecounts it as a SQL error.and not installed at all when the budget is disabled.
evaluate()forwardsquery_timeoutdown toevaluate_sample.Verification
5 collection errors as on unmodified
mainin my environment (all caused bytransformersnot being installed here, plus one Windows-path assertion intests/utils/test_inference_utils.py). No new failures.connect_sqlite()tosqlite3.connect(db_path)makes all 5 destructive-statement tests fail, so they really bite.
ruff checkclean on the three touched files.Not done / notes
evaluate(..., query_timeout=...)); happy to add--query-timeouttollmsql evaluateifyou want it exposed on the command line.
PRAGMA query_only = ONalso blocksCREATE TEMP TABLE. Nothing inllmsql/uses temp tables (checked), and it is the extra belt-and-braces the issue
suggests — say the word if you'd rather rely on
mode=roalone.