Deployment profiles — VPS and Cloudflare, from one source
Goal (owner, 2026-11-18): the same product deployable to a VPS and to Cloudflare, at the same time, without forking the application. This document defines what "both" means, what is shared, what is allowed to differ, and what is forbidden because it would quietly produce two different products.
1. What "at the same time" can and cannot mean
| Reading | Possible? |
|---|---|
| Two deployments of the same build artifact family, each with its own database | yes — this is the design |
| One deployment whose data lives in two databases at once | no — no sync layer is being built, and D1 cannot replicate Convex |
| One database, two frontends | only if both frontends talk to the same backend; that is the current state (Convex), not the target |
| Automatic failover between Convex and D1 | no — different storage models, different write semantics; a manual cutover is the design |
So: two products from one source, separate data, one API contract. A user on the VPS instance and a user on the Cloudflare instance are different users with different rows. Nothing in the design syncs them, and nothing should pretend to.
2. The two profiles
vps (today) | cloudflare (target) | |
|---|---|---|
| Frontend host | Node standalone container (app/Dockerfile), port 8080 | Cloudflare Pages, static export |
| Frontend render | Next server: proxy.ts rewrites + nonce CSP at request time | static files + _headers + a Pages Function for /api/auth/* |
| Frontend config | runtime env (FRONTEND_*, CSP_*) | build-time NEXT_PUBLIC_* (inlined) |
| Backend | Convex (hosted or self-hosted) | Worker + D1 + R2 + Queues + Cron |
| Database | Convex's document store | 41 SQLite tables (DATA-MODEL-PROPOSAL.md) |
| Objects | Convex file storage | R2 |
| Auth | Better Auth + Bearer JWT, cookies proxied same-origin | same, via a Pages Function proxy |
| Realtime | Convex subscriptions | bounded polling (no subscriptions on D1) |
| Caps | server-owned mutations | guarded batch() statements |
| Deploy command | docker build -f app/Dockerfile … && compose up | wrangler pages deploy + wrangler deploy |
Both profiles must satisfy the same contract, publish the same honesty rules, and render the same screens. Differences are host capabilities, never product behaviour.
3. The portability boundary: one contract, two implementations
server/openapi.yaml (58 paths) is the boundary. Rules:
- The frontend never branches on the target. No
if (isCloudflare)in a component, a service or the store. The only target-aware code is configuration:apiBase(),backendOn(), and the host assets (proxy.tsvs_headers+ Functions). - Every backend implementation satisfies the same path set, request bodies and response
envelopes — including the
outcomefield on send and the error envelope codes. A behaviour that exists only on one profile is a contract change and needs a spec delta. - A conformance suite runs the same assertions against both backends. Until it exists,
"parity" is an opinion. It is B12 in
migration.md. - Honesty rules are profile-independent: dry-run defaults, caps visible and real, a
parked/
unknowndraft never reported as "not sent", no claim of an effect that did not happen.
4. Config, and the one asymmetry that is accepted
The VPS build reads its backend at runtime; the Pages build bakes it at build time. That asymmetry is unavoidable (static HTML cannot consult the environment) and it is accepted — with guards:
- The VPS image refuses to ship against the shared development deployment
(
lib/deploymentGuard.ts,NEXT_PUBLIC_REQUIRE_DEPLOYMENT=1). - A Pages build with no backend env is allowed and honest (demo mode), but a Pages build
with a wrong host must fail visibly, not fall back (
migration.mdF2.4/F15.3). - One place per profile decides the base URL; nothing else reads the env.
5. What would make the two profiles diverge (and is therefore forbidden)
| Temptation | Why it is refused |
|---|---|
A D1-only route (/api/v1/corpus/...) used by the frontend | breaks the 58-path contract and forces a frontend fork |
| Real-time-only UI on VPS (live subscriptions) | the Cloudflare profile cannot serve it; polling must be the common path |
| Storing a token in KV only on Cloudflare | the VPS profile has no KV; tokens stay in the database/account table |
| A "Cloudflare mode" flag in the store | configuration, not product logic |
| Two schema sources of truth | Convex schema stays as-is (frozen); D1 schema lives in backend/migrations and is reconciled by tools/check_table_inventory.py |
Copying server/ into backend/ | different runtimes; the contract is shared, the code is not |
6. Cutover between profiles (per deployment, not global)
A deployment moves from vps to cloudflare by: building with the new base URL (F2.3),
deploying the Pages bundle, pointing the API seam at the Worker (B1), and re-authenticating
users (owner decision O3). Rollback is the reverse and is irreversible once D1 has taken
its first write of real data — the data does not travel back. A VPS deployment may simply
stay where it is; nothing about the Cloudflare work forces a VPS instance to move.
7. CI matrix (what "both" costs)
Every change that touches the contract, the frontend shell or a service must pass:
| Job | Runs |
|---|---|
app-vps | npm run check && npm test && npm run build (standalone) |
app-pages | PAGES_EXPORT=1 npm run check && npm test && npm run build + route/asset assertions |
server-convex | npm run check && npm test (existing) |
backend-d1 | npm run check && npm test + sqlite_master coverage test (later) |
contract | the openapi parity test; later the cross-backend conformance suite |
A change that is green on one profile and red on the other is not done.
8. Open decisions
| # | Question | Where it is answered |
|---|---|---|
| P1 | Does the VPS profile keep the Convex backend indefinitely, or migrate too? | owner — no code depends on the answer; the vps profile composes either way |
| P2 | Self-hosted Convex on the VPS, or Convex Cloud? | deployment choice, not code |
| P3 | Does the Pages app ever call Convex directly (no proxy)? | no — cookies must stay first-party |
| P4 | Conformance suite: shell script or a real test runner? | B12 |