PHEMIUS GUIDE

Agent SQL documentation

Agent SQL: how it works

The agent can use SQL to answer questions about the music library. It does not run model-written SQL against the persistent library database: after the request passes the active-account check, the first database tool call reads a limited set of library metadata into a fresh in-memory copy. Further tool calls in that same request reuse the isolated copy; it is discarded when the request ends.

Request path

The system prompt describes two tools. db_discovery reports the schema the model may query; db_query requests one SQL statement. The model chooses whether the question needs a tool, and its reply must identify one of those tools in JSON.

Signed-in users can also open the SQL tab beside Chat to write and run a query directly. It calls the same db_query implementation as the agent, so the parser checks, temporary sandbox, exposed columns, and result cap are identical; user-written SQL is not executed against the persistent library database.

DETERMINISTICDETERMINISTICUser asks about thelibraryModel chooses a toolnot deterministicdb_discoverydb_query + SQLmodel-generated; notdeterministicParse + AST guardRefuseAcceptRead ready-trackmetadata from persistentlibrary DuckDBguest reads excludeprofanity-marked tracksRequest-scopedin-memory DuckDBBulk-copy at most 20,000rows onceSchema lookup or runguarded querySchema / query resultCapped JSON to modelFinal answernot deterministic

The separators mark the part the application can enforce and test: SQL validation, the limited read from the persistent library, sandboxing, and result caps are deterministic code. Guest accounts see only tracks allowed by the guest visibility filter. The model's final wording remains non-deterministic.

For db_query, the Rust code passes the SQL through guard_read_only_sql before DuckDB prepares it. Each database tool runs on Actix's blocking pool. A turn can make up to eight tool calls before the model is asked to answer with what it has.

Example query the model might request:

SELECT title, artist
FROM songs
WHERE artist ILIKE '%Miles Davis%'
ORDER BY title
LIMIT 20

SQL validation and injection safeguards

  • The parser checks the whole statement. Empty SQL, SQL over 8,000 bytes, parse failures, and anything other than exactly one statement are rejected. A DuckDB-oriented dialect parses the input, then an AST visitor checks nested queries, subqueries, CTEs, and expressions as well as the top-level statement. Writes and other non-read-only statements are refused.

The model prompt is guidance, not the security boundary. The parser and sandbox enforce the restrictions even when a user asks the model to ignore its instructions. This is defense in depth, not a guarantee against bugs in the parser or database engine; keep the policy and DuckDB version reviewed as they evolve.

What the sandbox exposes

  • The first database tool call in an Actix request creates an in-memory DuckDB with external access disabled and a 500 MB memory limit. Further tool calls in that request reuse it; a separate request gets a fresh copy.
  • It copies only ready tracks, capped at 20,000 rows, and only these user-facing columns: title, artist, album, year, and genre.
  • Internals are not copied
  • Query results contain at most 200 rows.
  • The temporary database is dropped when the request ends.