ARCHITECTURE — Cetking Universe system design (v2, 6 Oct 2026)
How the Universe is built and why. Replaces the 3 Oct version (old bot lineup: Rocky, Arena, Guruji, Sutra — dead). Login detail lives in AUTH-CONTRACT.md, UI in DESIGN-SYSTEM.md, releases in RELEASES.md. If code and this file disagree, the code wins and this file is fixed.
1. Requirements
Functional
- Students log in once (mobile OTP or Google) and stay logged in (rolling 30-day login).
- One permanent Cetking ID, one coin wallet per person across every Cetking site.
- Six bots: Oracle (home, coins), Veda (Quant), Shabda (Verbal), Tarka (Logic & DILR), Mocky (mocks, Arena battles, leaderboard), Purva (admissions, GD-PI).
- Live centre timetable (Thane, Vashi, Dadar, Borivali, Online) from CK Backoffice.
- Admissions: exam timelines and college dates (Purva).
- Profile the student completes; coins for actions (rules pending).
- Coming: TCY mocks, DynTube videos, War Room inside Mocky, AI bot chat, per-centre views.
Non-functional
| Need | Target |
|---|---|
| Scale (planning figure) | ~1,000 active students (700 free, 300 paid); 5,000 batch peak |
| Uptime | 99.5% (≈ 3.6 h down a month max) |
| Speed | Public pages static or cached; first screen < 2 s on 4G |
| Cost | ~$220/month all-in at 1,000 users; $100 achievable |
| Privacy | No user counts anywhere; other students by username only |
| SEO | Public pages server-rendered with fixed URLs; personal layer on top, noindex |
| Safety | No secrets in code or chat; every change via blue preview first |
2. Components
flowchart LR
S[Student phone / browser] -->|HTTPS| V[Vercel: Next.js 14 app<br/>cetking-platform.vercel.app]
V -->|service key, server only| CL[(Supabase: Cetking Learn<br/>identity, students, coins,<br/>sessions, timetable)]
S -->|anon key, read only| CA[(Supabase: Cetking One AI<br/>admissions data)]
V -->|verify OTP proof| M[MSG91]
V -->|Google sign-in via Supabase Auth| G[Google]
ST[Staff] --> BO[CK Backoffice MCP] --> CL
WP[cetking.com WordPress<br/>front door 1] --> CL
V -.planned.-> T[TCY mocks]
V -.planned.-> D[DynTube videos]
V -.planned.-> W[War Room / Arena]
V -.planned.-> L[Claude + Groq via LLM router]
| Part | What it is | Notes |
|---|---|---|
| Web app | Next.js 14 App Router, TypeScript, Tailwind, on Vercel | Repo CetkingLearning/cetking-platform, folder cetking-one, live branch cetking-one-nextjs |
| CDN / hosting | Vercel edge network | Static pages served from the edge; server pages run as Vercel functions (Node) |
| Identity DB | Supabase Cetking Learn (suqcijtpfeaystltekfn) |
Shared with cetking.com; the single source of who a student is |
| AI-side DB | Supabase Cetking One AI (jfbauorxtmgvagwoyabr) |
Holds no identity; currently admissions tables |
| OTP | MSG91 widget in the browser, proof re-checked by our server | |
| Supabase Auth Google provider, PKCE | Only for a Gmail already connected after OTP | |
| Staff tools | CK Backoffice MCP (Claude-only staff interface) | Writes timetable, attendance, leads |
| Old front door | cetking.com (WordPress plugin by ChatGPT) | Same identity DB; own cookies on cetking.com |
3. APIs (inside the app)
All under app/api/, server-only code, JSON, Cache-Control: no-store for anything personal.
| Route | Method | Does |
|---|---|---|
/api/auth/otp/verify |
POST | Check MSG91 proof → open session cookie |
/api/auth/me |
GET | Who is signed in (own name, ID, coins only) |
/api/auth/renew |
POST | Daily swap to a fresh 30-day session |
/api/auth/logout |
POST | End this device's session |
/api/auth/google/start, /callback |
GET | Google connect / login |
/api/profile, /api/profile/[section] |
GET/POST | Read / save profile blocks |
/api/timetable |
GET | Week's classes for a centre |
/api/health |
GET | Health of shared services (for playful downtime messages) |
Rules: every POST checks same-origin + a custom header (x-ck-identity: 1); rate limits on
OTP (per IP, per phone, per day) fail closed; error messages never reveal whether a number
is registered.
4. Data model
Rule: Supabase stores. Code calculates. The LLM narrates. The LLM never writes SQL.
| Table (Cetking Learn) | Holds |
|---|---|
ck_leads.people |
Permanent person (one per phone) |
public.students |
Student record (name, centre, exam…) |
ck_leads.person_links |
Student ↔ person link |
public.ck_coin_ledger |
Coins: one row per earn/spend; balance = sum |
ck_identity.sessions |
Universe logins (hashed token, device, idle + absolute expiry) |
ck_identity.used_proofs, .audit_log, rate buckets |
One-time OTP proofs, audit trail, limits |
ck_one_private.google_identities |
Gmail ↔ person (shared with cetking.com) |
ck_one_private.welcome_grants |
One-time 1,000-coin grants per person |
public.ck_class_sessions + backoffice_faculty |
Timetable (published/cancelled only shown) |
| Table (Cetking One AI) | Holds |
|---|---|
admissions_exam_events, MBA_COLLEGES_Dates |
Purva's exam and college dates |
Access: the browser never holds the Cetking Learn key. All identity work goes through
ck_identity_* database functions callable by the service role only. Row-level security on
every student table.
Consistency: identity and coins are strongly consistent (single Postgres, locks per person). Timetable and admissions are read-mostly; slight delay is fine.
5. Rendering and caching
| Page | Mode | Why |
|---|---|---|
/quant /verbal /logic /mocks /admissions /app |
Static (built once) | Fast, SEO; personal bits load on top in the browser |
/ home |
Server per request | Greets the signed-in student by name |
/login /profile /timetable |
Server per request | Personal or live data |
Personal data is never cached at the edge (private, no-store, Vary: Cookie).
6. Scaling
- Web tier: Vercel scales functions automatically; static pages cost nothing extra.
- Database: one Supabase Postgres is ample for thousands of students. Next steps if needed: connection pooling (already built into Supabase), read replica for leaderboards, cache hot reads (timetable for the week) at the edge for a few minutes.
- Hot spots to watch: leaderboard queries (precompute), coin balance (sum per student → keep a running total if the ledger grows large), AI chat cost (LLM router tiers: template → Groq → Claude Haiku → Sonnet).
- No sharding needed at this size.
7. Reliability
- Blue-green releases: every change on a preview branch first; live only on "go live"; Vercel Instant Rollback to any previous version (RELEASES.md).
- Bot silos: each bot owns its page folder and personality file; shared parts are the "shared kit" (login, profile, design components, LLM router, analytics, health). A bot never imports another bot's code. One bot failing shows its own in-character down message; the others keep working.
- Idempotency: OTP proofs are single-use (
used_proofs); welcome coins once per person (welcome_grants); session renew can't double-swap; registration uses per-phone locks so two sites can't create two students. - Fail closed: if rate limits or the database can't answer, login refuses rather than lets people in. "Service trouble" is reported as trouble, never as "signed out" or "0 coins".
- Observability: Vercel build + function logs;
ck_identity.audit_logfor login events;/api/health. Planned: Sentry for front-end errors.
8. Security (summary — full baseline in ../SECURITY.md)
- Secrets only in Vercel environment variables (Production only for identity keys).
- Session cookie
__Host-ck_session: httpOnly, Secure, SameSite=Lax, random token; database keeps only its hash. - Same-origin check on every write; OTP proof re-verified server-side with MSG91.
- Pilot switch:
CK_IDENTITY_PILOT_PHONESlimits login to listed numbers (removed 6 Oct 2026, login open to all).
9. Key trade-offs (decided)
| Choice | Picked | Why / cost |
|---|---|---|
| Bot names/colours | Settings file in code, not Supabase | Faster pages, nothing to break; a rename needs a release |
| One identity DB shared with cetking.com | Yes | No migration, one wallet; both sites depend on it |
| Sessions per site (cookies) | Separate for now | Browser rule; carry-over plan in SESSION-CARRYOVER.md |
| Static bot pages + personal layer on top | Yes | SEO + speed; personal bits appear a moment after load |
| Dark mode | Per-device switch, light default | Keeps the brand look; one extra style file to maintain |
| Monorepo, one Next.js app | Yes | Simple for a small team; bot isolation by folders + rules |
10. Open items
- Admissions page reads the Cetking One AI database from the browser; move behind an API route and personalise by the student's chosen exams.
- Coins rules (1,000 at 100% profile) — waiting for Ravneet.
- Per-centre views (Thane student sees Thane) — SITE-RULES.md.
- TCY, DynTube, War Room, LLM chat — not built.
- cetking.com move to this app — plan only (SESSION-CARRYOVER.md); not moving yet.