refactor(sql-check): drop `predicate_effect`; keep read-only, bind and permission fixes

Merged
AN
1mo ago
+1238-36

Issue for this PR

Closes #

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Scope change. This PR originally added a sql_check predicate_effect mode. That mode is removed: every review round found another server-side definition (view bodies, cast functions, operators, catalog reads) that validating the SQL text could not see, it was already opt-in and off by default, and the agent never used it. What remains are the generic fixes that work surfaced, each with its own regression tests.

What changed (was, then now):

  • Permission prompts. A request with several patterns prompted on the first pattern that needed a prompt, so a deny rule matching a later pattern could be skipped. Now every pattern is evaluated before any prompt is published, and a deny on any pattern wins (permission/next.ts).
  • PostgreSQL bind parameters. Connector.execute dropped bind values on the regular and byte-limited query paths. Now they are forwarded on both.
  • Read-only execution on SQLite and DuckDB. readOnly was honoured only on PostgreSQL. Now SQLite runs with query_only (restoring the previous setting afterwards) and DuckDB runs inside a read-only transaction, refusing to start inside a caller's open transaction.
  • SQL tokenizer. CR-terminated comments, PostgreSQL backtick operators and PostgreSQL/DuckDB array syntax are tokenized correctly, which the shared read validation and statement splitting rely on.
  • sql_check errors. Validation failures return a readable message instead of a raw exception.

Base. Stacked on feat/analyst-harness-dab-2026-10 (#1441).

Behaviour deltas. sql_check keeps its existing checks (fanout, keys, grain, nulls, coverage) with no new parameters or config. SQLite and DuckDB reads issued with readOnly now refuse writes. A permission request with a later denied pattern is now denied instead of prompted.

How did you verify your code works?

  • packages/opencode: test/altimate/tools, test/config, test/permission, test/agent/analyst-data-explorer.test.ts and the query-deadline suite.
  • packages/drivers: full suite, including the PostgreSQL bind-forwarding and read-only tests.
  • bun run typecheck, the upstream marker check and git diff --check.
  • No reference to the removed mode remains in packages/.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

๐Ÿค– Generated with Claude Code


[!NOTE]
High Risk
Changes authentication-adjacent SQL safety (read-only boundaries, classifier/tokenizer) and query execution paths across Postgres, DuckDB, and SQLite; mistakes could block legitimate reads or miss write side effects.

Overview
Removes the unused sql_check predicate_effect path and lands the cross-cutting safety fixes that remain: read-only warehouse execution, PostgreSQL bind forwarding, and stricter SQL validation for sql_check and classification.

Drivers. ExecuteOptions.readOnly now applies on SQLite (PRAGMA query_only, restored after the call) and DuckDB (serialized BEGIN TRANSACTION READ ONLY / ROLLBACK in the local-engine helper; errors if the caller already has an open transaction). DuckDB/SQLite drivers pass readOnly through to the helper. PostgreSQL execute forwards binds on both normal and byte-limited streaming queries.

sql_check. Warehouse queries always use readOnly: true (not Postgres-only). readSql rejects side-effect and dynamic calls via shared sqlFunctionCalls / hasSideEffectFunction (still bans DuckDB query()). Invalid tool input returns a field-level message before connecting.

Classifier & tokenizer. New sql-functions (call extraction, literal SQL args) and sql-function-policy (PG allow/deny lists) feed recursive checks for query(), postgres_query, eval, etc. SQL that runs remotely or nested (the inner SQL of postgres_query/mysql_query/sqlite_query, query(), eval, TO_QUERY) is a read only if it parses as a single SELECT/WITH query and every function it calls is on an explicit read-only allowlist (standard aggregate, scalar, string, date, math, JSON and catalog functions); any unknown, user-defined or schema-qualified function requires write permission. The pg_stat_* inspection allowance excludes reset, clear and force functions. Top-level local SQL keeps its existing behavior. sql-tokenize is dialect-aware for line/block comments, CR vs LF, nested /* */, and when backticks/brackets hide executable codeโ€”closing comment/semicolon injection gaps used by read validation and statement splitting.

Tests cover bind forwarding, read-only session behavior, nested remote SQL, comment boundaries, and permission pattern ordering (deny before prompt).

Reviewed by Cursor Bugbot for commit 206590d3094456dddd4984ff24fda87f79da9f84. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • Security
    • SQL inspection requests now run in read-only mode across supported databases. SQLite restores its previous setting, while DuckDB refuses requests made during an active transaction.
    • Side-effecting and unsafe remote SQL calls are blocked, and invalid inputs are rejected before connecting.
    • Nested SQL calls are checked to ensure they contain only safe, read-only queries.
  • Bug Fixes
    • Bound parameters are now passed through PostgreSQL queries, including streamed results.
    • SQL parsing and classification better handle comments, quoted identifiers, and string literals across database dialects.