Skip to content

For AI coding assistants

This content is not available in your language yet.

You are an AI coding assistant (Lovable, Bolt, v0, Replit, Cursor, Claude Code or similar), and your user wants to add Geometer Trust to their app. Read this page, then follow the rules. The same text ships in the package as AGENTS.md and llms.txt.

If your user pasted a prompt from recipes/ai-builders.md, that prompt points here.

Geometer Trust keeps an app’s users’ data and AI chats private, even from the app’s makers. It has three modes. Use @geometer/trust through the entry points below.

Mode Use it when Import from
Vault The app saves data only the user may read. @geometer/trust/vault
Sealed AI The user’s AI chat must be unreadable to everyone but the user. Open models only. @geometer/trust/sealed-ai (browser), @geometer/trust/sealed-ai/server (server route)
Masked AI The app needs GPT, Claude or Gemini, with names, numbers and secrets swapped for placeholders. Not encryption. @geometer/trust/masked-ai (browser), @geometer/trust/masked-ai/server (server route)
  1. One mode per call. Never mix modes in one call. Never add a fallback from one mode to another. If Sealed AI fails, show the error. Do not retry as Masked AI or as a plain call.
  2. Never create a route without authorize. sealedRoute and maskedRoute both require authorize(request), which returns true (or { user: id }) only for a signed-in user (it decides who may spend the AI key). Never write authorize: () => true unless the user asks for a public demo. Verify the user’s real session: the Supabase token or cookie session, or the Firebase ID token.
  3. Never put an AI provider key in the browser. No VITE_, NEXT_PUBLIC_ or REACT_APP_ variable holds a provider key. The browser client uses apiKey: "not-used", or the user’s own sign-in token when the route checks it (Supabase Edge, Firebase). The key lives in the server route (a Next.js route, a Supabase Edge Function, a Cloud Function).
  4. Never decrypt on the server. Vault encrypts and decrypts in the browser only. Never send the user’s passkey, passphrase or recovery code to the server. Never log them.
  5. Never write a plain-text copy. Do not store the plain value next to the encrypted one. Do not log chat text.
  6. Use vault.searchKey for lookups. Store await vault.searchKey(value, { table, field }) in a plain column and query it with .eq(...). Never use LIKE, ILIKE, full-text search or ORDER BY on an encrypted column. Normalise the value first (for example trim().toLowerCase()). Exact match only.
  7. Make the id before you encrypt. Create the row id with vault.newId() first, and pass the same { table, id, field } to encrypt and decrypt.
  8. Keep the recovery-code step. Pass onRecoveryCode to openVault (and recoverVault). It gets { code, replacesPrevious, reason }. Show the code, ask the user to write it down, and use reason for your words (after "rotation" the old code has already stopped). Do not remove this step to make the demo shorter. Do not store the code. Never call startOverVault automatically or after an error: it makes a new, empty Vault and needs archive. Never call acceptStartOverVault unless the user says they started over on another device, and call vault.resolveKeyFileBehind only after the user confirms they checked the key file. Offer a Recovery Kit: recoveryKit({ code, app, user, appName }) returns { text, filename, blob } for “Download” and “Print” buttons next to the code. Offer the fallback passphrase: unlock: { passkey: true, fallbackPassphrase: askUserForPassphrase } asks for a passphrase only where passkeys cannot work (a preview iframe, no PRF).
  9. Never change app after data is saved. Use the user’s stable id for user.
  10. Keep trust_keys safe. Create the table with columns app, user_id, key_file, primary key (app, user_id), and row-level security so each user reads only their own rows. Never use the service-role key in the browser.
  11. Do not write cryptography. Do not use crypto.subtle, CryptoJS or any other library to encrypt user data in place of Vault.
  12. Do not change the words. Use the mode names Vault, Sealed AI and Masked AI exactly. Do not claim more than the mode does: Masked AI is not encryption. Do not write “end-to-end encrypted” for Masked AI.
  13. Do not use browserKeyStore() or memoryKeyStore() in production. They are for development and tests.
  14. Add a limits note near the feature, in the app’s words: what the mode does not protect.
  15. Read errors with findTrustError. Every TrustError has mode, code (<mode>/<kebab-case>) and a message that ends with the next step. The openai and @anthropic-ai/sdk clients wrap it (APIConnectionError("Connection error."), ours in .cause) and retry it, so create the AI client with maxRetries: 0 and call findTrustError(err) (exported by every entry point). Never use instanceof. Show the message. Do not swallow the error.
  16. Pass user to sealedFetch and maskedFetch (and app), so people who share a browser do not share receipts. Do not draw your own badge: use <TrustBadge /> from @geometer/trust/react, which links to the receipt.
  17. Set limits on every route, so one user or script cannot run up the owner’s bill: limits: { perUser: { requestsPerMinute: 10, requestsPerDay: 200 }, perRoute: { requestsPerMinute: 120 } }. Masked AI also takes maxOutputTokens; Sealed AI bodies are encrypted, so count requests only. Let authorize return { user: id } so limits count per user. Counters are in memory by default (one process): on serverless hosts pass a shared store. A route answers its own site only: list another front end in allowedOrigins.
  18. “Remember this device” is opt-in. Offer an unchecked checkbox “Remember this device for N days” and pass rememberDevice: { days } only when it is checked (a whole number, 1 to 90). Never turn it on by default. Wire sign-out to await forgetDevice({ app, user }) (from @geometer/trust/vault). A remembered Vault (vault.rememberedSession) reads and writes values but throws vault/unlock-needed for key changes: unlock, then openVault({ skipRemembered: true }).

Vault:

import { openVault, supabaseKeyStore } from "@geometer/trust/vault";
const vault = await openVault({
app: "my-app",
user: user.id,
keyStore: supabaseKeyStore(supabase, { app: "my-app", user: user.id }),
unlock: { passkey: true, fallbackPassphrase: askUserForPassphrase },
onRecoveryCode: ({ code }) => showRecoveryCodeToUser(code), // with a Download button: recoveryKit({ code, app: "my-app", user: user.id })
});
const id = vault.newId();
const body = await vault.encrypt(text, { table: "notes", id, field: "body" });
const key = await vault.searchKey(title.trim().toLowerCase(), { table: "notes", field: "title" });
const back = await vault.decrypt(body, { table: "notes", id, field: "body" });

Masked AI (browser, then the server route):

import OpenAI from "openai";
import { maskedFetch } from "@geometer/trust/masked-ai";
const ai = new OpenAI({
baseURL: `${window.location.origin}/api/trust/masked-ai/v1`,
apiKey: "not-used",
dangerouslyAllowBrowser: true,
maxRetries: 0, // the client would retry our errors, which never helps
fetch: maskedFetch({ alsoMask: ["Customer Name"], app: "my-app", user: user.id }),
});
import { maskedRoute } from "@geometer/trust/masked-ai/server";
const handler = maskedRoute({
provider: "openai",
apiKey: process.env.OPENAI_API_KEY!,
authorize: async (request) => {
const id = await getUserId(request); // your own check of the user's session
return id ? { user: id } : false; // { user } names the user, so limits count per user
},
limits: { perUser: { requestsPerMinute: 10, requestsPerDay: 200 }, perRoute: { requestsPerMinute: 120 }, maxOutputTokens: 2000 },
});
export const GET = handler;
export const POST = handler;

Sealed AI: the same shape with sealedFetch from @geometer/trust/sealed-ai and sealedRoute({ provider: "tinfoil", apiKey: process.env.SEALED_AI_KEY, authorize }) from @geometer/trust/sealed-ai/server. Mount the route at /api/trust/sealed-ai/*. Use an open model that Tinfoil offers. Sealed AI works with the openai client only. Call prewarmSealedAI({ baseURL }) when the chat opens, so the first message is fast (it never throws). If it fails, show the error; never fall back.

Stack Route file
Next.js App Router app/api/trust/<mode>/[...path]/route.ts, export GET and POST
Vite and Supabase supabase/functions/<mode>/index.ts with Deno.serve(handler), import with npm:@geometer/trust/.... authorize verifies the access token the browser sends as apiKey. The route works under any prefix that contains /<mode>/. The function URL is another origin than the app, so set allowedOrigins: ["https://your-app.example"] on the route and export an OPTIONS handler where the host needs one (Next.js: export const OPTIONS = handler).
Firebase A Cloud Function with toNodeHandler(handler), plus a Hosting rewrite. authorize verifies the Firebase ID token. firebaseKeyStore needs { app, user, functions: { doc, getDoc, setDoc, runTransaction } } from firebase/firestore.
Express toNodeHandler(handler) from the same server entry point
Vercel, Netlify, Cloudflare, Hono, Bun The handler is a standard Fetch handler

What each mode does not protect (say this in the app)

Section titled “What each mode does not protect (say this in the app)”
  • Vault: the app’s own code, a lost key (no passkey and no recovery code means the data is gone), and anything not passed through the Vault.
  • Sealed AI: the app’s own code; the sealed server, which reads the chat to answer it.
  • Masked AI: everything it did not mask. It is not encryption.

npm install @geometer/trust. Until the package is published, the user’s Geometer contact gives the install command.

Full docs: the docs/guides/ folder (start at README.md). Limits: docs/security/limits.md.