Skip to content
wbic16Public

About

The Prequel to SQLite, 24 years later

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

176 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Esque

SQ: The Simple Query Tool

SQ is a modern database, written from the ground-up to take advantage of Phext: 11-dimensional plain hypertext. Like normal SQL databases, it has multiple pronunciations: "esque", "S-Q", "Seek", "Super Quine", and "Self Query".

Getting Started

you can either clone this repo and run cargo build, or just install the latest stable build: cargo install sq. You may have also arrived here from Choose Your Own Adventure...

A pre-built x86_64 container is available via Docker as well: docker pull wbic16/sq.

Commands

SQ is designed to keep abstractions to a minimum. You can interact with phexts via shared memory (daemon "esque" mode) or a TCP socket ("seek" mode) with a simple REST API.

  • sq help: displays online help
  • sq version: displays the current version
  • sq share: : launches a server that hosts a phext file via shared memory
  • sq status: Displays daemon statistics (loaded phext, size, connection count)
  • sq toc: Displays a textmap (list of available scrolls) of the currently-loaded phext
  • sq checksum: Displays the checksum of the current phext
  • sq delta: Displays the hierarchical network of checksums for the current phext
  • sq seed host:port : Prints the seed cache of a hosted phext (client mode, see below)
  • sq fetch host:port : Cache-aware fetch of one scroll from a hosted phext (client mode, see below)
  • sq push : Overwrites the specified scroll with the local file
  • sq pull : Fetches the specified scroll to a local file
  • sq select : Fetches content from the current phext
  • sq insert "text": Appends text at the specified coordinate
  • sq update "text": Overwrites text at the specified coordinate
  • sq delete : Removes all content from the specified coordinate
  • sq save : Writes the current phext back to disk
  • sq json-export : Dumps the contents of the current phext as json
  • sq init: Fast initialization for hosting world.phext from any state
  • sq shutdown: Instruct the daemon to terminate

Modes of Operation

  • Daemon Mode: If you supply a filename parameter to sq, it will launch in daemon mode - communicating with local system processes via shared memory
  • Listening Mode: If you supply a port number to sq, it will launch in web server mode - listening on the TCP socket requested
  • Router Mode (NEW in v0.5.5): Token-based multi-tenant routing layer - see ROUTER.md for details
  • API Proxy Mode (NEW in v0.6.0): OpenAI-compatible prompt proxy - triages each prompt to cache, a local Ollama model, or an upstream provider

Router Mode (v0.5.5)

The SQ Router enables multi-tenant deployments with token-based authentication and request routing. Instead of exposing each tenant's SQ instance directly, you run:

  • One router on a public port (e.g., 443 or 1337)
  • Multiple SQ instances on private ports (one per tenant)

The router reads the Authorization header, looks up the tenant's backend port, and proxies the request transparently.

Quick Start:

# Start backend SQ instances
sq host 1338 --key pmb-v1-user1-abc123 --data-dir /data/user1
sq host 1339 --key pmb-v1-user2-def456 --data-dir /data/user2

# Start router
sq route router-config.json 1337

# Clients use Authorization header
curl -H "Authorization: pmb-v1-user1-abc123" \
     http://localhost:1337/select/1.1.1/1.1.1/1.1.1

Features:

  • Token-based authentication (pmb-v1 format)
  • Per-tenant data isolation
  • Path traversal prevention
  • Transparent proxy (same SQ API)
  • Production-ready (systemd services, TLS via nginx/Caddy)

📖 Full documentation: See ROUTER.md for complete setup guide, security features, and production deployment.

Authentication

When SQ is started with --key <token> (or with a tenant config), every request must carry the token in one of these headers:

  • Authorization: <token> or Authorization: Bearer <token>
  • X-SQ-API-Key: <token> (used by Phext Notepad)

Requests without a valid token receive 401 Unauthorized. Unknown routes return 404 Not Found and are logged to stderr with their request line. Clients must include the full base URI, e.g. http://host:1337/api/v2.

API Proxy Mode (v0.6.0)

sq api <config.json> [port] (default port 8100) runs an OpenAI-compatible chat endpoint that routes each prompt to the cheapest tier that can handle it:

  1. Cache - SHA256-keyed prompt cache with LRU eviction and TTL expiry
  2. Local - an Ollama model (e.g. qwen3-coder-next) for formatting, translation, and quick tasks
  3. Upstream - a hosted provider for complex reasoning, architecture, and phext/Exocortex work

Prompts are scored against local and upstream signal patterns (src/triage.rs). If a local request fails it falls back to upstream, and a feedback loop routes all local-tier traffic upstream while the local failure rate over the last feedback_window requests exceeds escalation_threshold. The full conversation history is forwarded to whichever tier answers; cache lookups are keyed on the last user message.

Config fields: local_url, local_model, local_timeout_secs, upstream_url, upstream_api_key, upstream_model, cache_max_entries, cache_ttl_secs, signal_threshold, escalation_threshold, feedback_window. Keep upstream_api_key out of version control.

SQ Design Philosophy

SQ is a complete ground-up rewrite of database concepts. It probably doesn't have features you expect from a database. What it does offer is simplicity. SQ is designed to mirror computer architecture in 2025, not 1970. Databases are stored in phext files using variable-length scrolls. Essentially, everything in a phext database is a string.

SQ leverages Rust and libphext-rs to provide the core data store. All database primitives in SQ are built in terms of phext. For more information about the phext encoding format, refer to https://phext.io.

Developing

In daemon mode, SQ uses shared memory to ensure that data transfers to/from the database engine are done as quickly as possible. The shared memory segments are managed by files stored in the .sq directory where you invoked SQ from. It is expected that you will run the client and the server from the same directory.

In listening mode, SQ reads and writes phexts via REST.

REST API Endpoint

SQ offers a simple CRUD-style REST API. The API allows you to interact with multiple phexts from CURL or your web browser. Saving is automatic - if a command changes the content of a phext, it will be saved to disk immediately. Note that if you change the loaded phext without issuing a load command, SQ will automatically reload from disk first.

  • /api/v2/version: Displays the current version of SQ
  • /api/v2/load?p=: Loads the entire contents of phext.phext into the current context
  • /api/v2/select?p=&c=: Fetches the scroll of text found at coordinate in phext.phext
  • /api/v2/insert?p=&c=&s=: Appends a scroll of text at coordinate in phext.phext
  • /api/v2/update?p=&c=&s=: Overwrites the contents of the scroll at coordinate in phext.phext
  • /api/v2/delete?p=&c=: Clears the contents of the scroll at coordinate in phext.phext
  • /api/v2/delta?p=: Returns the hierarchical map of checksums for the given phext
  • /api/v2/toc?p=: Returns the table of contents for the given phext
  • /api/v2/get?p=: Returns a complete copy of the given phext
  • /api/v2/query?p=&from=&to=: Returns every non-empty scroll in the inclusive coordinate range (v0.6.0)
  • /api/v2/query?p=&set=,,...: Returns the scrolls at an explicit set of coordinates (v0.6.0)
  • /api/v2/seed?p=: Returns the seed cache for the given phext — the xxh3_128 hash of every populated scroll, and nothing else (seed cache)
  • /api/v2/fetch?p=&c=&h=&has=<0|1>: Cache-aware scroll fetch — a small unchanged confirmation when the client holds the scroll at the current hash, otherwise the full scroll plus its current hash (seed cache)

Coordinate Query API (v0.6.0)

Query a phext by coordinate range, by coordinate set, or both at once:

# Range: every non-empty scroll from the first coordinate to the second, inclusive
curl -H "X-SQ-API-Key: $KEY" "http://localhost:1337/api/v2/query?p=world&from=1.1.1/1.1.1/1.1.1&to=1.1.1/1.1.2/1.1.1"

# Set: specific coordinates (missing ones come back with an empty scroll)
curl -H "X-SQ-API-Key: $KEY" "http://localhost:1337/api/v2/query?p=world&set=1.1.1/1.1.1/1.1.1,2.1.1/1.1.1/1.1.1"

# Large sets: POST the coordinates as the body, separated by commas or newlines
curl -X POST -H "X-SQ-API-Key: $KEY" --data-binary @coords.txt "http://localhost:1337/api/v2/query?p=world"

The response is a JSON array of {"coordinate": "...", "scroll": "..."}, sorted by coordinate from library down to scroll. When a range and a set are combined, the results are merged with duplicates removed. Add &limit=N to cap the number of results. Coordinates must be complete (a.b.c/d.e.f/g.h.i, every part at least 1); a malformed coordinate, a range with only one end, or a range whose start comes after its end returns 400 Bad Request with the reason.

Seed Cache + Cache-Aware Fetch

A seed cache is the hash envelope of a phext: for every populated coordinate, the xxh3_128 hash of its scroll (the same per-scroll hash /api/v2/delta diffs with, as 32 lowercase hex characters) — and nothing else. Its size is a fixed constant times the number of scrolls, independent of how large any scroll is, and the envelope compresses well because the coordinates are sorted and clustered. A client downloads the seed on load, builds its navigation locally, and then streams scrolls in as they are viewed, verifying each against the seed.

# The seed envelope: {"algorithm":"xxh3_128","scrolls":N,"hashes":[{"coordinate":"...","hash":"..."}, ...]}
curl -H "X-SQ-API-Key: $KEY" "http://localhost:1337/api/v2/seed?p=world"

Cache-aware fetch lets a client avoid re-downloading scrolls it already holds. The client presents three things — the coordinate, its current hash for that scroll, and a flag saying whether it holds the content:

curl -H "X-SQ-API-Key: $KEY" "http://localhost:1337/api/v2/fetch?p=world&c=1.1.1/1.1.1/1.1.1&h=<hash>&has=1"
  • Client holds the content and the hash matches the server's current hash → a small confirmation with no content: {"coordinate":"...","hash":"...","unchanged":true}
  • Anything else (no local content, or a stale hash) → the normal fetch: {"coordinate":"...","hash":"...","unchanged":false,"scroll":"..."}

Batches use the same logic per entry: POST /api/v2/fetch?p=<phext> with a JSON array body of {"coordinate": "...", "hash": "...", "has_content": true|false} returns a JSON array of the same two shapes.

The sq seed and sq fetch commands are the reference client: sq fetch keeps a disk-backed scroll cache (default .sq/seed-cache.json, override with --cache), consults the seed first, serves unchanged scrolls locally with no content transfer, and hash-verifies everything it downloads before caching it.

Trivia

SQ was bundled into CYOA on 6/12/2025 and 7/15/2025.

About

The Prequel to SQLite, 24 years later

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages