SQLite · PostgreSQL

Verified SQL generation
for developers

Describe a database function. Define how it should be tested. Codoken's peer-to-peer network generates and verifies the SQL in a real sandboxed database process.

One function. One fixture. Verified.

Rather than prompting for an entire schema migration or ORM layer, you isolate each piece of database logic into an atom — a pure function or view with a clear contract. The network generates it, a real database runs your tests, and you get back SQL that is provably correct before it touches your schema.

Step 1

Isolate the query

Extract one pure function or view from your design — a discount calculation, an aggregation, a ranking window, a data transform. No application logic, no ORM dependencies.

Step 2

Set up the schema in tests

The test harness creates the minimal schema and inserts fixture rows. Then SELECT assertions check exact outputs using got, expected, and ok columns.

Step 3

Submit to the network

A distributed worker generates the SQL. Independent validators run it in isolated database processes. You receive a function that passed real execution — not just a plausible snippet.

Step 4

Drop it into your schema

Paste the verified CREATE FUNCTION or CREATE VIEW into your migration. Because it was written and tested in isolation, it slots in without surprising schema conflicts.

Let your local agent drive it

Give your local coding agent a one-time system prompt describing Codoken. From there it can decompose any database feature into atoms, submit each one to the network, collect the verified SQL, and assemble the final migration — all without leaving your IDE.

System prompt — paste into your agent's context once
# Codoken — verified SQL generation (MCP)

You have the codoken MCP server available as a set of tools.
Use it whenever you need a pure SQL function, view, or query transform.

## Tools

submit_atom(task_id, language, prompt, test_code, timeout=120)
  language: "sqlite" or "postgresql"
  Submits one atom and blocks until APPROVED or REJECTED.
  Returns the verified SQL on APPROVED.

list_languages()     — all supported languages
get_balance()        — current credit balance

## Rules

1. One function or view per atom — no full schema migrations.
2. prompt: state the exact name, input types, return type, and edge cases.
3. test_code must contain:
   a. Schema setup: CREATE TABLE + INSERT statements (if the function queries data).
   b. SELECT assertions, one per line:
      SELECT expr AS got, literal AS expected, expr = literal AS ok;
4. generated_code is ONLY the CREATE FUNCTION / CREATE VIEW definition.
5. On APPROVED, copy only the returned SQL into your migration.
6. On REJECTED, revise the prompt and retry.

## Example (PostgreSQL)

submit_atom(
  task_id   = "clamp_fn",
  language  = "postgresql",
  prompt    = """Write a PostgreSQL function named clamp(val NUMERIC,
    lo NUMERIC, hi NUMERIC) RETURNS NUMERIC that returns val
    clamped to [lo, hi]. IMMUTABLE LANGUAGE sql.
    Return only the CREATE OR REPLACE FUNCTION statement.""",
  test_code = """SELECT clamp(5,1,10) AS got, 5 AS expected, clamp(5,1,10)=5 AS ok;
SELECT clamp(-1,0,10) AS got, 0 AS expected, clamp(-1,0,10)=0 AS ok;
SELECT clamp(15,0,10) AS got, 10 AS expected, clamp(15,0,10)=10 AS ok"""
)
Agent task

Decompose the feature

Ask your agent to list every pure SQL function or view a new feature needs — discount logic, ranking queries, aggregation helpers, data transforms. Each becomes its own atom.

Agent task

Draft description + fixture

The agent writes the function signature, the minimal schema fixture, and concrete SELECT assertions before any SQL exists. The test harness is the spec.

Agent task

Submit, poll, retry

The agent posts each atom, polls until resolved, and retries rejected ones with a revised description. No human in the loop unless it fails twice.

Agent task

Assemble the migration

With all functions verified, the agent writes only the migration glue — the small part that wires the verified functions into your application schema.