Production RAG, line by line
The data model
The demo Worker keeps its data in one D1 database, one Vectorize index and one KV namespace. A demo account is one row in the demos table; it owns its files, the chunks cut from them, each chunk's vector and the questions asked of them. The daily job rolls the questions up into three health tables. The site's own D1 database, for visit tracking, is outside this schema.
Tables and how they connect
Every row of files, chunks, questions and archived_pages carries the demo_id of the account that owns it. A chunk's id is also its vector's id in Vectorize, and the account's id names the Vectorize namespace that holds its vectors, so a query searches one account's vectors only. The schema is demo/schema.sql, with later changes in demo/migrations/.
A demo account and its documents
When a visitor verifies an email address, the site calls createDemo() (POST /internal/demos), which inserts the demos row, or gives the address's existing row a new key, and returns the access key; the browser keeps it in the demo cookie, and the row stores only the key's SHA-256 hash, which signedIn() looks up on every request. An upload claims its name in files, then INGEST 3 adds its pages and characters to the account's counts, INGEST 5 writes its chunks, and INGEST 6 upserts their vectors.
| Store | Key | Holds | Written by | Read by |
|---|---|---|---|---|
demos | id (also the Vectorize namespace) | key_hash, card_fingerprint (email:<SHA-256 of the address>, unique: one demo per email), engagement_id, company, contact, asks_used, files, pages, chars, uploads, created_at, used_at, cleared_at, purged_at (no longer set) | createDemo() inserts the row, or gives an existing one a new key_hash; openDemo() gives a new key_hash; canaryDemo() inserts the canary's row; upload() and remove() update the counts and used_at; ask() at ASK 2 adds to asks_used (answering() gives it back on a model failure); serveMcp() sets used_at; purge() zeroes files, pages and chars and sets cleared_at | signedIn() on every request; serveMcp() (by id, for each MCP request); canaryDemo(); the admin routes (adminDemos(), adminDemo(), golden(), whichDemos()) |
files | (demo_id, name) | pages, chars, skipped (pages with no text) | upload() at INGEST 2; deleted by remove() and purge() | session(); the MCP server's resources; the document store |
chunks | id = <demo id>-<upload>-<n>, also the vector's id | demo_id, file, page, text, seq | upload() at INGEST 5; deleted by remove() and purge() | retrieval() at ASK 3–4, which reads all of the account's chunks; the MCP server's pages |
archived_pages | (demo_id, file, page) | each page's text, archived_at | upload() at INGEST 5, with consent; deleted by remove() and after 25 months by expire() | golden(), the golden-set export; the admin routes |
Vectorize index | vector id = chunk id; namespace = demos.id | 1,024-number bge-m3 vectors | upload() at INGEST 6 (models.vectors.upsert); deleted by remove() and purge() (models.vectors.deleteByIds) | retrieval() at ASK 3–4 (models.vectors.query) |
Questions and the pipeline's health
ASK 2 adds one to demos.asks_used, and the finished answer is recorded as one row in questions with its outcome. Once a day the scheduled job counts yesterday's questions into pipeline_days, compares each fallback rate and the p95 time with the 14 days before, and writes any that drifted to pipeline_flags, and asks the canary's six cases through the real pipeline.
| Store | Key | Holds | Written by | Read by |
|---|---|---|---|---|
questions | id | demo_id, question, answer, sources, checked, fallback, ms, first_ms, keep, via | answering(), once per question; deleted by purge() (keep = 0, after 10 idle days), by expire() (after 25 months) and by runCanary() (the canary's rows after 2 days) | session() (the 20 newest checked answers); rollUp() for the daily counts; golden(); the admin routes |
pipeline_days | (day, via) | asks, answered, not_found, refused, trouble, stopped, ms_p50, ms_p95, first_p50 | rollUp(), daily | checkDrift(); healthRoute() for /admin |
pipeline_flags | (day, via, metric) | value, baseline, threshold | checkDrift(), runCanary() and dailyHealth() (when the canary cannot run), daily | healthRoute(), which /admin and the mailer's pipeline alert read |
canary_runs | (day, name) | passed, found, ms, answer (first 300 characters) | runCanary(), daily | healthRoute() for /admin |
The MCP server's counters and OAuth store
The MCP server counts each call in mcp_hits under a key for the account, its searches or the caller's address, in a minute or day slot, and the daily job deletes old slots. The OAuth library keeps every registered client, grant and token hash in the KV namespace OAUTH_KV; token exchange stores a record for each short-lived document-store token there, under exchanged:<SHA-256 of the token>, which KV deletes when it expires.
| Store | Key | Holds | Written by | Read by |
|---|---|---|---|---|
mcp_hits | (key, slot) | hits | hit(), on each MCP call | hit() itself, against the limits; forgetHits() deletes old slots daily |
OAUTH_KV (KV) | library-defined keys; exchanged:<SHA-256 of the token> for token exchange | OAuth clients, grants (for the account id, which their encrypted props also carry), token hashes, and one record per exchanged document-store token | the OAuth library; authorize() (completeAuthorization); tokenExchange(); grantsRoute() DELETE and revokeGrants() in purge() revoke grants | the OAuth library on each MCP request; appsOf() and grantsRoute() (Connected apps); the document store's token check |
Pinned to joelstevick-demo-rag-pipeline at 6a9c6c7.