Architecture
What v2 is built from, what each folder of the repository holds, where each kind of logic lives and how the pieces talk. A CTO session keeps this page; the decisions it cites outrank it (how we work).
It moved here on 2026-10-12 from two Google docs, [Spec] v2 monorepo architecture and [Ref] v2 architecture overview, merged into one page (#53, #54). M0 is being rebuilt from an empty folder (D25), so where this page describes the app shell or the schema, it describes the target until the timeline's M0 exit check passes. The repository itself is the record of what exists so far.
The stack
Section titled “The stack”| Area | Choice | Per |
|---|---|---|
| Database | Its own Supabase project per environment, Postgres 17, in Singapore like v1. Schema V13 as hand-written migration files. | D5, D12 |
| Business rules | In Postgres: constraints, triggers, security-definer functions, and RLS on every table. | D3 |
| Refusals | A stable error key from the enum public.error_key, raised through app.refuse or app.deny. | D10, D30 |
| Auth | Supabase Auth (email and password), one profiles row per login, roles as data in the access tables. Publishable and secret keys; anon, authenticated and service_role are Postgres role names only. | D13 |
| Access | Roles with pool scopes, enforced by RLS. Permissions come from the access tables, never from the token. | D3, D13 |
| Files | Supabase Storage, private buckets, at paths that follow the owning row. No base64 in rows. | D14 |
| Data shape | Columns for anything filtered, joined, ruled on, summed or hidden; jsonb only where the shape truly varies. | D32 |
| Tenancy | organization_id on every table from day one, for SaaS later. | |
| Web app | Next.js 16 (App Router) and React 19 in apps/web, with @supabase/ssr. | D5, D22 |
| Language | Indonesian first; every string is built in apps/web with t() and an Indonesian default. | D7, D29 |
| Design | The Opleet design system inside apps/web, behind the @ui/ alias. | D8 |
| Styling | Tailwind CSS v4 in apps/web for layout and spacing, in its own cascade layer beneath the design system's op-* classes. | D35 |
| Docs | Astro Starlight in apps/docs: the source of truth for what v2 does, written before tests and code. | D27, D28 |
| Tests | pgTAP on the real local Supabase stack for every rule and every RPC; Playwright for the per-role menu check and a short list of journeys. | D12, D19 |
| Errors and traces | Sentry, one project per deployed app, with staging and production as its environments. | D24 |
| Hosting | apps/web on Vercel (Singapore function region, paid plan); apps/docs on Cloudflare Pages behind Cloudflare Access. | D5 |
| Payments | Recorded by hand, no gateway. | |
| GPS | Easygo, pulled by a scheduled job. |
Environments
Section titled “Environments”| Environment | Database | App | Data |
|---|---|---|---|
| Local | The local Supabase stack, started with the CLI | pnpm dev | The seed: system rows and the sealed E2E organization |
| v2 staging | Its own Supabase project (TL-M0-20) | The Vercel project, pointed at staging (TL-M0-21) | Rebuilt from main every night by the rehearsal job, from a copy of v1 production, plus the sealed E2E organization (D11) |
| v2 production | Its own Supabase project, made before cutover, with a nightly pg_dump and MFA for super_admin (D16) | The same Vercel project | Empty until the cutover run fills it once |
| v1 | The old Supabase project | The old Vercel project | Still developing (D75); read-only after cutover, then archived |
There is no Supabase branching: the nightly rehearsal wipes and rebuilds staging, which is a project-level operation (D5). The rehearsal job holds staging credentials only. Nothing is pushed to production before cutover week, and every push to production is made by zuki (D11). Staging and production secrets live in GitHub environments that only main can use (D28).
The repository
Section titled “The repository”apps/ web/ Next.js 16 app. Frontend Engineer. docs/ Astro Starlight: decisions, rules, generated reference, build and contributing pages.packages/ db-types/ @opleet/db-types: the generated database types and nothing else.supabase/ migrations/ schema V13, hand-written SQL tests/ pgTAP seed.sql system rows and the sealed E2E organization config.tomltools/ db/ gen_rls.py, gen_seed.py, signing-key.sh docs/ check-rules.mts, behind pnpm docs:rules migration/ the v1 to v2 migration and the photo copy (data phase; not created yet)AGENTS.mdturbo.json · pnpm-workspace.yaml · biome.json · lefthook.yml · commitlint.config.mjs- supabase/ sits at the root, not in packages/. It is not a JavaScript package, nothing imports it, and the CLI finds config.toml from the working directory. That is also why CI starts the database job from a path filter, not from Turborepo's affected graph (D12).
- tools/ is outside the pnpm workspace. It holds Python, SQL and one-shot migration code with their own dependencies; as workspace members, Turborepo would try to build things that aren't builds.
- packages/db-types holds the generated types only (D22). Each app builds its own Supabase clients: apps/web with @supabase/ssr and Next's cookies, and a future backend or driver app its own.
- The plain-Postgres stub never enters the repository (D25). The 44 checks the delivered M0 had are the acceptance list for the new pgTAP tests.
- Versions are pinned: Node 24 through .node-version and engines, pnpm 12 through packageManager (D25). The repository moves to Node 26 once Vercel supports it.
Where logic lives
Section titled “Where logic lives”| Kind of logic | Lives in | Examples |
|---|---|---|
| Invariants and business rules | Postgres: constraints, triggers, security-definer functions | Double booking, transfer first, allocation limits, status only through functions, document numbers, the audit log |
| Multi-step actions | Postgres functions called by RPC, one transaction each | book_rental, handover, return, route work order, void payment |
| Who sees what | RLS policies on every table, and Storage policies that ask the owning row | Every table; files at D14 paths |
| What a rule says | A written rule with an ID in apps/docs, before its test and its code | The rules (D27) |
| Page composition, forms, labels | apps/web server components and server actions | The app |
| Visual language | apps/web, behind the @ui/ alias | design/opleet and components/opleet (D8); layout utilities with Tailwind (D35) |
| Scheduled work | pg_cron, or Supabase Edge Functions | The rent generator, the weekly ETLE scan, the GPS pull, checkpoint reminders |
| Heavy or external work | The backend, once it exists (below) | Integrations, heavy reports, the driver app's API |
How a request flows
Section titled “How a request flows”This is the app shell the Frontend Engineer builds in M0's FE packs B and C (#16, #17).
- proxy.ts runs before every page. It checks the session with getClaims(), which verifies the token locally without a round trip to Auth (D13), and sends signed-out visitors to /login. Public paths: /login, /auth, /antrian (the queue join page behind the static QR), /bastk (links already sent on WhatsApp) and /design-preview in development.
- Server components build a Supabase client from the request cookies and ask the database once for the caller's permissions per pool.
- The shell builds the sidebar from one list of routes, each with its label, icon, permission and module. A page the user can't open shows a refusal; the landing page is the first page the role may open. The route guard reads the same list.
- The pool switcher keeps the chosen pool, or all pools, in a cookie, set by a server action. The language comes from profiles.locale, with the opleet-locale cookie as the per-request fast path (D29).
- Reads go to PostgREST with the user's token, and RLS decides which rows come back. Writes that change status, money or numbers call RPC. A refusal comes back as an error key, and one mapper in apps/web turns it into text (D29, D30).
How a change moves through the repository
Section titled “How a change moves through the repository”- The rule first. What the change does is written as a rule with an ID in apps/docs, by the Docs Engineer (D27).
- Then the test, then the code. A database change: a test whose description starts with the rule's ID, then the migration, then pnpm supabase db reset and pnpm supabase test db (D19). Until cutover, migration files may be edited in place (D11).
- The types follow. pnpm db:types regenerates packages/db-types, and any page that reads a changed table fails typecheck until it is updated. The migration's author regenerates them in the same pull request; when that breaks a page in apps/web, the Frontend Engineer fixes it on the same branch (D33).
- Commit. Lefthook runs Biome on the staged files and checks the commit message: Conventional Commits, scoped web, docs, db-types, supabase or tools (D6).
- Pull request. An agent works on a branch through the Claude GitHub app and opens a pull request that names the issue it closes (D28). CI's checks job runs Biome, pnpm docs:rules, and typecheck and build on the affected packages; the pr-title job checks the title. The database job and the end-to-end job join at timeline rows 23 and 30 (TL-M0-18, TL-M0-19).
- Merge. zuki reviews and squash-merges; the ruleset on main allows nothing else. The gate is the pull request into main: its checks pass before zuki merges it. Vercel deploys apps/web from main waiting only for its own Lint and Typecheck, not for the GitHub checks, and the push to main runs only the checks job, because the pull request has already run database and e2e (#732). Cloudflare Pages deploys apps/docs (TL-M0-23).
Rules the repository enforces
Section titled “Rules the repository enforces”These are checked, not only written down, because much of the code is drafted by agents and a rule in a markdown file doesn't survive a long session.
- The schema changes only through supabase/migrations (D4). No Dashboard edits; if one happens, capture it with supabase db diff.
- No ORM, and apps/web never opens a Postgres connection (D4). Biome's restricted imports block pg, postgres and drizzle-orm outside tools/.
- packages/ never import from apps/.
- Any script that can write to a database lives in tools/, never in apps/ or packages/.
- Agents never run anything against staging or production. Applying migrations there is a step zuki runs (D11, D28).
- Every built rule has a test, and every test cites a rule that exists. pnpm docs:rules fails CI otherwise (D27).
- The generated types match the schema. The database job regenerates them and fails on any diff (D12).
- AGENTS.md at the root holds the map and these rules, under 100 lines; smaller ones sit in apps/web and apps/docs.
Why there is no ORM
Section titled “Why there is no ORM”D4 decides it; the reasons are kept here so the question doesn't come back.
- The schema is hand-written SQL. An ORM's schema file would be a second definition of the same tables, kept in sync by hand, and its diff engine would try to correct what it can't model: partial indexes, the generated RLS policies, security-definer functions.
- Transactions are already handled. Every multi-step action is one Postgres function called by RPC.
- An ORM connects past RLS. Where RLS is the access control, that is a liability.
Integrations
Section titled “Integrations”| Integration | Direction | Notes |
|---|---|---|
| Easygo GPS | Pull | Positions into gps_positions on a schedule; the raw payload kept as jsonb (D32) |
| ETLE site | Weekly check per plate | Scraping; alert when the site changes |
| Outgoing links only | /bastk links sent to drivers keep resolving after cutover | |
| Grab and Gojek | None | Platform account status is entered at the interview |
| Banks | None | Payments recorded by the collection team, with proof |
Not built yet
Section titled “Not built yet”- The backend (apps/api). Created when two of these outgrow pg_cron and Edge Functions, or when the driver app needs an API: ETLE scraping, contract and BASTK PDFs, the Easygo pull, heavy reports. It would be a Hono app on Node, deployed as a container next to the web app, calling the same database functions. It never becomes a second place where business rules live (D3), and it gets its own Sentry project (D24) and its own role in how we work before its first session.
- The driver app (apps/driver). Expo, after cutover. The schema already has a driver role with own-rows access. The design system won't help it: React Native doesn't use DOM components.
- A job queue. pg_cron is enough while the jobs are the rent generator, the GPS pull, the weekly ETLE scan and checkpoint reminders. Revisit when a job needs retries with backoff rather than a rerun.
- Load tests. k6 scripts would go in tools/load/ when there is a reason.
- Publishing. Nothing is published to a registry: no Changesets, no semantic-release.
Revisit later
Section titled “Revisit later”Not decisions; checks to make when the time comes.
- packages/shared. lib/format.ts and the route list stay in apps/web on the one-consumer rule that keeps the design system there (D8). Promote them only when a second app needs them.
- packages/ itself. If nothing joins db-types by M4, check whether the generated types belong in apps/web until the backend exists.
- The rehearsal's report goes to Slack, or email if Slack costs anything (D36); Sentry cron check-ins are a candidate for noticing a missed night.