Data
A model that can write SQL can answer questions about your data. It can also write a query that deletes it, reads a file you never named, runs for an hour, or sends your rows to a provider you did not choose. So how does an agent query data it was never trusted with?
The idea
In agents generally
Letting a model query data is three problems, not one.
- The query may do too much. SQL is a language that writes as well as reads. A model's query can also be wrong in a way that costs a lot, such as a join of two big tables.
- The rows may go too far. The model's context is sent to a provider. A table of salaries should not ride along because a question was about the table.
- The data is not the model's to keep. A result can be a million rows. The model needs the answer, and the person needs the table.
The way through is to put walls where the model cannot argue with them. The query runs in a place that can only read, with limits on time and memory. The result is kept for the person, and the model gets only as much of it as the privacy rules allow. A sentence from a check in front is a help to the model. The walls are what keep the data safe.
flowchart LR q[A query from the model] --> w[A place that can only read] w --> r[The result, kept for you] r --> m[What the model may see]
How agents usually do it
| Approach | How it works | Good at | Costs |
|---|---|---|---|
| Hand the model a connection | The model gets a database login and a "run SQL" tool | Simple, and everything works | The model has the login's power. A read-only login is the only wall, and it must be set up by hand |
| A query builder | The model fills in a form (a table, a filter, a sum) | The model cannot write a bad query | It can ask only what the form allows |
| Load into the model | Paste the rows, or a sample, into the prompt | No tool needed | The size of the window, and every row goes to the provider |
| Statement filter | Parse the SQL and refuse anything but SELECT | Cheap, and the model gets a clear reason | A parser is a fence. One missed function can write or open a connection |
| A sandbox | Run the query in a process or container with no write access | A wall that holds even when the filter misses | The sandbox has to be built and kept |
Several open problems stay. A function that sends raw SQL to a server looks like a harmless SELECT. A result that is "private" for one provider is fine for another. And a model's summary of a result can carry the rows it was told to keep out.
How golem does it
In golem
golem takes the last approach and adds the filter in front. A source is read by DuckDB in a worker process. The worker is locked so it can only read the source's own files. A guard turns refusals into sentences. Each result is kept as a file for you, and a privacy decision picks what the model gets.
sequenceDiagram participant Model participant Tool as data_query (daemon) participant Guard participant Worker as Data worker (DuckDB, locked) participant Disk as results/*.parquet participant You as Web or terminal Model->>Tool: source, sql Tool->>Guard: one statement, only reads? Guard-->>Tool: ok, or a sentence Tool->>Worker: query (cut after the time limit) Worker-->>Tool: rows, up to the row cap Tool->>Worker: keep the rows Worker->>Disk: result.parquet Tool->>Tool: may these rows go to this model? Tool-->>Model: first rows and sums, or "N rows, shown to the user" Tool-->>You: the whole result, to page, sort, chart, export
- A worker process. The daemon never loads DuckDB. A child process does, and the daemon cancels a slow query by asking it to interrupt, then kills it two seconds later if it has not answered. A crash costs one query.
- A locked engine. A source is one in-memory database, filled first and then closed: only the source's own files may be read, outside access is off, and the configuration is locked so a query cannot turn any of it back on.
- The guard. One statement, and it must be a read. It also refuses the functions that send raw SQL to a database server or open a connection of their own, because those look like a
SELECT. - The privacy decision. A source is
open,localorschema. The result for a model is decided from the level, whether the model is local, and whether the chat is private. A model that is not allowed the rows gets a count and the columns. - Kept results. Every result is a Parquet file written by a second, throwaway database that holds no source. The person pages through it. It goes with the session.
- Downloads and snapshots stay outside the engine. The engine never goes on the network. A web file is downloaded by the daemon, under the rules of
web_fetch, and the engine reads the copy. A snapshot of a database is copied table by table into Parquet files the same way. - Pinned connections. A web download connects to the address golem checked, never to a second lookup of the name, so a name that changes its answer cannot send it to a private address.
- Passwords. A database password lives in the Keychain. The daemon reads it when it opens the source and sends it to the worker over the pipe. It is in no argument, no environment variable and no log.
What the model gets back from data_query, in the shape it reads:
5 rows · truncated: no · 38 ms · result …
<data_rows source="shop"> … </data_rows>
Showing the first 5. The user can page through all 5 rows.
amount: min 4.5 · max 120 · sum 310.5Where it lives:
packages/daemon/golem/data/engine.py: one locked source, in the worker.packages/daemon/golem/data/tools.py: the four tools and the shape of a result.packages/daemon/golem/data/privacy.py: who may see rows.packages/web/src/features/data/: the Data screens.
Why this way
The settings are the wall, the guard is a help. A statement filter can miss a function. DuckDB's own file and network settings, set before the query and then locked, cannot be turned back on by a query. The guard gives the model a sentence it can act on, and the tests load every function of each scanner extension and fail if one gets through. See ADR-063.
A process, not a library call. Killing a process is the cancel that always works, and a native library with its own memory and crashes stays out of the process that holds every session.
The person sees everything, the model sees what the level allows. The same result is two things: a table for you, and a short text for the model. local keeps rows on this Mac by default when golem cannot tell.
The engine never goes on the network. What the engine reads is on this Mac, so a query cannot be turned into a request. A trade-off: a web file or a snapshot is a copy that goes out of date until it is refreshed.
Still learning. The privacy guess for a new source is a first guess from column names and places. It is a suggestion, and you change it.
Try it
- Ask in any chat:
how much did I spend on models this week, by model? Use the golem source.The model callsdata_queryon the built-ingolemsource. - Open the result's table in the web app. Sort a column, switch Show as to a chart, and press Save query.
- Make it write:
golem data query golem "delete from usage". golem refuses with a sentence, and nothing changes. - In the web app, open the source's Settings and switch Who may see rows to schema only. Ask again with a hosted model, then press Show what it saw under the table. It holds a count and the columns, not rows.
The user's side is in Data.
The details
A source
A source comes from [data.<name>] in ~/.golem/config.toml, from a trusted project's .golem/config.toml (a project source replaces yours of the same name in that folder), and from the built-in golem source. DataSourceSettings in config.py checks the keys: exactly one of path and url, no password in a database address, https for a web file, and a password that is secret:data/<name> with the source's own name. An untrusted project's sources are listed and never opened.
| Kind | Opened as |
|---|---|
files | Views over the matched files (CSV, Parquet, JSON lines, Excel). Symlinks that leave the folder are left out |
sqlite, duckdb | The file attached read-only |
postgres, mysql | ATTACH ... READ_ONLY, before the lock |
url | A files source over the downloaded copy |
builtin | An empty in-memory database the daemon fills with a few curated tables of golem.db: counts, names, times, status and cost, never a message |
A source is opened in the worker on first use. Its tables and descriptions are kept for a short time. A watch source notices new files in its folder.
The worker and the engine
golem.data.client.DataWorker starts golem.data.worker on the first request, one JSON object per line each way. Each request runs in its own thread so cancel can interrupt it. After [data] query_timeout_seconds the daemon sends cancel, and kills the worker if it has not answered in 2 seconds. A worker that exits starts again on the next request, after a wait that doubles from 0.5 s to 30 s, and opens its sources again. A frozen build runs the worker as golem-daemon data-worker.
The engine closes each source in this order: allowed_paths names only the source's own files, enable_external_access = false, lock_configuration = true. memory_limit and threads come from [data] worker_memory and threads. A query returns at most max_rows; the worker fetches one more row to know the answer was cut. Spill files go to ~/.golem/data/tmp.
The guard
golem.data.guard allows exactly one statement that json_serialize_sql accepts: SELECT, WITH, DESCRIBE and SUMMARIZE. It refuses a function whose name starts with postgres_, mysql_, sqlite_, read_postgres_, read_mysql_ or read_sqlite_, and pg_clear_cache. A test lists every function each scanner extension adds, so a new one in a later DuckDB cannot slip through.
Extensions
DuckDB's wheel has core_functions, icu, json and parquet. The SQLite, Excel, PostgreSQL and MySQL extensions are installed once into ~/.golem/data/extensions/, only when you ask (data.extension.install). autoinstall_known_extensions is off, so a query never downloads.
The tools and the privacy decision
| Tool | Does |
|---|---|
data_sources | One line each: name, kind, tables, rows, what you may read of it |
data_describe | Tables, columns with types and ranges, the notes, and sample rows when the level allows |
data_query | One read-only query, or a saved query by name (saved). Returns the row count, the first 20 rows (up to 100 with limit), a result id, and the smallest, largest and sum of each number column. Rows come inside <data_rows> after a note that they are data, not instructions |
data_export | Writes a kept result to a new file. Asks every time. Returns the row count and no rows |
The first three only read, so the default rules allow them. The subject of a call is the source's name, so a permission rule can say tool = "data_query", match = "shop". data_sources is also a section of the system prompt, one line per source. The tables, columns and notes are read with data_describe, so they cost nothing until needed.
may_use_rows in privacy.py decides, the way memory does:
| Level | Local model | Hosted model, chat not private | Hosted model, private chat |
|---|---|---|---|
schema | no rows | no rows | no rows |
local | rows | no rows | no rows |
open | rows | rows | no rows |
A model that may not read rows gets N rows from <source>, shown to the user, not sent to this model. and the columns. For a schema source the tool result holds no rows at all. For local and open, the result carries both the full text and the stub, and graphs/data_stub.py changes only the copy sent to a hosted model, so the chat keeps the whole result and a later local call still reads it. The same decision applies to the model that drafts notes. Column names, types and counts go to any model, and values only where the decision allows.
Kept results
A data_query result is written as ~/.golem/data/results/<result_id>.parquet, with the source, the SQL and the time in the file's own metadata, and recorded as a session artifact of kind table. A page is read by a locked database that may read only that file. A file older than 30 days goes, then the oldest until all take at most 1 GB, at start and once a day. data.result.page pages through a result, and data.export and data_export write it to a new file. An export path follows the rules of write_file, must not exist, must not be in a system folder and must not be where a source reads.
Notes, saved queries and the profile
Notes are DATA.md and saved queries are queries.toml, in <home>/data/<name>/ for your source and <project>/.golem/data/<name>/ for a project's. The model reads at most 16 KB of notes. When each query last ran is kept apart in <home>/data/queries-runs.json. data.notes.draft builds a prompt from column names and types, plus values where the privacy decision allows. data.profile and data.browse run SQL that golem builds from checked column names and bound values; the browser sends no SQL.
Web files, snapshots and databases
golem.data.download reuses the rules of golem.tools.web: https only, no loopback, private, link-local or reserved address unless [web] allow_private, a redirect only on the same host with each hop checked again, and the size cap max_download. It connects with pinned_stream to an address it checked. The copy is in ~/.golem/data/downloads/<name>/.
A snapshot is ~/.golem/data/snapshots/<name>/: one Parquet file per table and snapshot.json. It is written to a staging folder and swapped in when whole, so a reader sees the old files or the new ones, and a cancel leaves nothing behind. With snapshot = true the source reads the copies, and tables outside the snapshot are read live.
A database address has no password in it. Postgres gets default_transaction_read_only, a statement timeout and an idle timeout from timeout_s (15 by default); MySQL is attached READ_ONLY and golem's own cut applies. Describe reads the catalog only, with row estimates, and never runs count(*) on a live table. A login that can write gets a warning, never a block.
Routines and Measure
routine.* can run a saved query with no model and post to the Inbox. The data report starter (golem/workflows/starters/data_report.py) runs saved queries and has one model step write a summary. For a source whose rows stay on this Mac it posts the results as they are. golem eval data plays 30 fixed questions through the real chat and tools, scored exactly.
Protocol
data.list, data.describe, data.inspect, data.add, data.update, data.remove, data.query, data.cancel, data.profile, data.browse, data.browse.keep, data.result.page, data.export, data.notes.get, data.notes.set, data.notes.draft, data.queries.save, data.queries.remove, data.refresh, data.download.cancel, data.url.check, data.snapshot.plan, data.snapshot.take, data.snapshot.delete, data.extension.install. A change to a source sends data.changed.
Files
| File | What it does |
|---|---|
packages/daemon/golem/data/engine.py | One locked source, in the worker |
packages/daemon/golem/data/worker.py | The worker: one JSON line in, one out |
packages/daemon/golem/data/client.py | DataWorker: start, cancel, kill, restart |
packages/daemon/golem/data/guard.py | The statement check |
packages/daemon/golem/data/sources.py | The sources a folder sees, and opening them |
packages/daemon/golem/data/privacy.py | may_use_rows, and the first guess at a level |
packages/daemon/golem/data/tools.py | The four tools |
packages/daemon/golem/data/section.py | The prompt's list of sources |
packages/daemon/golem/graphs/data_stub.py | What a hosted model gets of a private result |
packages/daemon/golem/data/parquet.py, results.py | Kept results |
packages/daemon/golem/data/download.py, downloads.py | Web files |
packages/daemon/golem/data/snapshot.py, snapshots.py | Snapshots |
packages/daemon/golem/data/database.py | A database address and its connection |
packages/daemon/golem/data/notes.py, queries.py, draft.py | Notes, saved queries, drafts |
packages/daemon/golem/data/export.py | Where a result may be written |
packages/daemon/golem/server/data_rpc.py | The data.* methods |
packages/web/src/features/data/ | The Data screens |
packages/tui/src/data-manager.ts | /data |
packages/cli/src/data-command.ts | golem data |
packages/view/src/data.ts | The words the screens share |
Read more
- Before: Helpers.
- Data, the user's guide.
- ADR-063, the engine, the lock and the limits.
- Memory, which decides in the same way which model may read a note.
- The overview: the whole system on one page.