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
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,5 @@ multiplies rows). In the default mode a join that multiplies rows shows up as a
- The result policy (`mask`, `hash`, `deny`), the review policy and the per-agent and per-session budgets are
set by the human. Do not split or disguise a query to get under a limit.
- Credentials and budgets are set by the human in `gulliver.toml` or environment variables.
- A config error saying a section "must be a table" means it needs a TOML section such as `[budget]`
with keys, rather than a plain value such as `budget = 3`. Fix the named section before retrying.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ agent_day_usd = 5 # per agent per day (optional)
The example file also shows the settings for the read-only gate (`allowed_paths`, `allow_remote`,
`allow_functions`), the result guard (`policy`), the runaway guard, `[lint]`, `[review]` and `[approval]`.
An unknown top-level key or section is refused, so a typo such as `[budjet]` cannot silently fall back to the
default budget.
default budget. Sections such as `[budget]`, `[connections.local]`, `[approval]`, `[lint]` and `[review]`
must be TOML tables. A value such as `budget = 3` produces a configuration error naming the section,
with exit code 1 and advice to fix `gulliver.toml` or the flags.

### A read-only Postgres role

Expand Down
2 changes: 2 additions & 0 deletions llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,5 @@ Reasons that come with a status:
- The result policy (`mask`, `hash`, `deny`), the review policy and the per-agent and per-session budgets are
set by the human. Do not split or disguise a query to get under a limit.
- Credentials and budgets are set by the human in `gulliver.toml` or environment variables.
- A config error saying a section "must be a table" means it needs a TOML section such as `[budget]`
with keys, rather than a plain value such as `budget = 3`. Fix the named section before retrying.
3 changes: 3 additions & 0 deletions skills/gulliver/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,6 @@ and returns a counterexample when rows differ; a test is not a proof, so labels
Connections and budgets live in `gulliver.toml` (see `gulliver.example.toml` in the repo) or in environment
variables such as `GULLIVER_DSN=warehouse.duckdb`. Try it with `gulliver demo`, which builds a 5-million-row
DuckDB file.

If a config error says a section "must be a table", use a TOML section such as `[budget]` with keys,
rather than a plain value such as `budget = 3`. Fix the named section before retrying.
4 changes: 4 additions & 0 deletions src/gulliver/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,10 @@ def _main(parser: argparse.ArgumentParser, args: argparse.Namespace) -> int:
raise ValueError("Give one SQL statement in quotes, or existing .sql files and folders")
args.sql = args.sql[0] if args.sql else None
if args.command == "lint":
from .lint_files import load_lint_config

# Validate [lint] for SQL strings as well as file linting.
load_lint_config(args.config)
dialect = args.dialect
if dialect is None:
try:
Expand Down
19 changes: 15 additions & 4 deletions src/gulliver/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ def find_config_file(explicit: str | os.PathLike[str] | None = None) -> Path | N
return None


def table_section(data: dict[str, Any], key: str, *, name: str | None = None) -> dict[str, Any]:
"""Read an optional TOML table without treating falsey values or arrays as tables."""
value = data.get(key, {})
if not isinstance(value, dict):
section = name or key
raise ConfigError(f"[{section}] must be a table (a [{section}] section with keys), "
f"not {type(value).__name__}.")
return dict(value)


def load_settings(
config_path: str | os.PathLike[str] | None = None,
dsn: str | None = None,
Expand All @@ -254,8 +264,9 @@ def load_settings(
)

connections: dict[str, Connection] = {}
for conn_name, conn_data in (data.get("connections") or {}).items():
conn_data = dict(conn_data)
connections_data = table_section(data, "connections")
for conn_name in connections_data:
conn_data = table_section(connections_data, conn_name, name=f"connections.{conn_name}")
if "dsn" in conn_data and "engine" not in conn_data:
conn = connection_from_dsn(str(conn_data.pop("dsn")), conn_name)
conn = replace(conn, params={**conn.params, **conn_data})
Expand All @@ -279,7 +290,7 @@ def load_settings(
if not default:
default = next(iter(connections))

budget_data = dict(data.get("budget") or {})
budget_data = table_section(data, "budget")
for env_key, attr in _ENV_BUDGET.items():
if os.environ.get(env_key):
budget_data[attr] = os.environ[env_key]
Expand All @@ -297,7 +308,7 @@ def load_settings(
checks[check_name] = parse_check_config(str(check_name), check_data)

return Settings(connections=connections, default_connection=default, budget=budget, state_dir=state_dir,
approval_channel=_approval_channel(data.get("approval") or {}), checks=checks)
approval_channel=_approval_channel(table_section(data, "approval")), checks=checks)


APPROVAL_CHANNELS = ("cli", "elicitation")
Expand Down
4 changes: 2 additions & 2 deletions src/gulliver/lint_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from sqlglot.errors import SqlglotError
from sqlglot.tokens import Token, TokenType

from .config import ConfigError, find_config_file
from .config import ConfigError, find_config_file, table_section
from .lineage import BLOCK_RE, COMMENT_RE, CONFIG_RE, EXPR_RE, REF_RE, SOURCE_RE, THIS_RE
from .lint import RULES, SEVERITY_ORDER, Finding, lint
from .sqlsafety import classify
Expand Down Expand Up @@ -147,7 +147,7 @@ def load_lint_config(config_path: str | os.PathLike[str] | None = None) -> LintC
return LintConfig()
with path.open("rb") as fh:
data = tomllib.load(fh)
return lint_config_from_mapping(dict(data.get("lint") or {}), path.resolve().parent)
return lint_config_from_mapping(table_section(data, "lint"), path.resolve().parent)


# -- finding files ------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/gulliver/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from sqlglot.errors import SqlglotError

from . import __version__, lineage
from .config import ConfigError, Settings, find_config_file
from .config import ConfigError, Settings, find_config_file, table_section
from .engines import EngineError, dialect_for
from .lint import lint
from .sqlsafety import classify
Expand Down Expand Up @@ -103,7 +103,7 @@ def load_policy(config_path: str | os.PathLike[str] | None = None,
path = find_config_file(config_path)
if path:
with path.open("rb") as fh:
data = dict(tomllib.load(fh).get("review") or {})
data = table_section(tomllib.load(fh), "review")
data.update({k: v for k, v in (overrides or {}).items() if v is not None})
return Policy.from_mapping(data)

Expand Down
75 changes: 75 additions & 0 deletions tests/test_config_sections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import json

import pytest

from gulliver.cli import main
from gulliver.config import ConfigError, load_settings
from gulliver.lint_files import load_lint_config
from gulliver.review import Policy, load_policy

CASES = [
("budget", "budget = {value}\n", load_settings, ["status"]),
("connections", "connections = {value}\n", load_settings, ["status"]),
("connections.local", "[connections]\nlocal = {value}\n", load_settings, ["status"]),
("approval", "approval = {value}\n", load_settings, ["status"]),
("lint", "lint = {value}\n", load_lint_config, ["lint", "SELECT 1"]),
("review", "review = {value}\n", load_policy, ["review", "--models", "models"]),
]


@pytest.mark.parametrize("section,template,loader,args", CASES)
@pytest.mark.parametrize("value", ["3", "0", "false", '\"\"', "[]", '[["ignored", 1]]'])
def test_non_table_sections_raise_config_error(tmp_path, section, template, loader, args, value):
config = tmp_path / "bad.toml"
config.write_text(template.format(value=value))
with pytest.raises(ConfigError) as exc:
loader(config)
assert f"[{section}] must be a table" in str(exc.value)


@pytest.mark.parametrize("section,template,loader,args", CASES)
def test_cli_reports_bad_section_without_traceback(state, tmp_path, capsys, section, template, loader, args):
config = tmp_path / "bad.toml"
config.write_text(template.format(value="3"))
assert main(["--config", str(config), "--json", *args]) == 1
captured = capsys.readouterr()
result = json.loads(captured.out)
assert result["status"] == "error"
assert f"[{section}] must be a table" in result["error"]
assert result["next_step"] == "Fix gulliver.toml or the flags."
assert "Traceback" not in captured.out + captured.err


@pytest.mark.parametrize("file_mode", [False, True])
def test_lint_reports_bad_section_with_explicit_dialect(state, tmp_path, capsys, file_mode):
config = tmp_path / "bad.toml"
config.write_text("lint = false\n")
query = tmp_path / "query.sql"
query.write_text("SELECT 1;")
sql = str(query) if file_mode else "SELECT 1"
assert main(["--config", str(config), "--json", "lint", sql, "--dialect", "duckdb"]) == 1
result = json.loads(capsys.readouterr().out)
assert result["status"] == "error"
assert "[lint] must be a table" in result["error"]
assert result["next_step"] == "Fix gulliver.toml or the flags."


def test_empty_and_inline_tables_keep_defaults(tmp_path):
config = tmp_path / "valid.toml"
config.write_text('connections = {}\nbudget = {}\napproval = {}\nlint = {}\nreview = {}\n')
assert load_settings(config).budget.max_query_usd == 1.0
assert load_settings(config).approval_channel == "cli"
assert load_lint_config(config).fail_on == "error"
assert load_policy(config).runs_per_day == Policy().runs_per_day


def test_inline_tables_keep_values_and_overrides(tmp_path):
config = tmp_path / "valid.toml"
config.write_text('connections = { local = { dsn = "duckdb://:memory:" } }\n'
'budget = { max_query_usd = 2.5 }\napproval = { channel = "cli" }\n'
'lint = { fail_on = "warn" }\nreview = { runs_per_day = 2 }\n')
settings = load_settings(config, budget_overrides={"max_query_usd": 3.0})
assert settings.connection("local").params["path"] == ":memory:"
assert settings.budget.max_query_usd == 3.0
assert load_lint_config(config).fail_on == "warn"
assert load_policy(config, {"runs_per_day": 4}).runs_per_day == 4
Loading