diff --git a/specs/epics/README.md b/specs/epics/README.md index 29155e3..389c79b 100644 --- a/specs/epics/README.md +++ b/specs/epics/README.md @@ -7,5 +7,6 @@ folder is the adoption; the merge creates the epic in the workspace. | Epic | Cluster | Status | What it is | |------|---------|--------|------------| +| [`algolia-search/`](./algolia-search/) | **AL** | In progress (AL0) | Vercel's Algolia Integration Example, its `query()` on a pg client per request over Hyperdrive to Supabase Postgres, its Algolia keys the Worker's secrets and its table and demo products applied by db-migrate — and an Orun baseline that instantiates it in one landing (AL2) | As-built ≠ intent: what shipped lives in each epic's `IMPLEMENTATION-STATUS.md`. diff --git a/specs/epics/algolia-search/IMPLEMENTATION-STATUS.md b/specs/epics/algolia-search/IMPLEMENTATION-STATUS.md new file mode 100644 index 0000000..1c56494 --- /dev/null +++ b/specs/epics/algolia-search/IMPLEMENTATION-STATUS.md @@ -0,0 +1,11 @@ +# algolia-search — implementation status + +| Milestone | State | Landed | +|---|---|---| +| AL0 — the spec | ✅ | AL-1 | +| AL1 — the search on Workers and Supabase | planned | AL-2 | +| AL2 — a baseline | planned | AL-3 | + +## Departures from the upstream template + +Recorded as they land, from AL1 on. diff --git a/specs/epics/algolia-search/README.md b/specs/epics/algolia-search/README.md new file mode 100644 index 0000000..74dba65 --- /dev/null +++ b/specs/epics/algolia-search/README.md @@ -0,0 +1,33 @@ +# Epic: algolia-search (AL) + +**Algolia's starter is search as a client concern: InstantSearch in the browser asks Algolia's index, which Algolia's own Supabase connector fills from a Postgres `products` table, and the one server read is the product page, which shows the fields the index never gets. On Cloudflare the socket goes through Hyperdrive to a Supabase Postgres project — postgres-drizzle-starter's data plane — and the code that changes is where the database client and the Algolia keys come from: upstream's lazy module-level `pg.Pool` becomes one `pg` client per request from the `HYPERDRIVE` binding, and the keys the template inlined into the browser bundle at build become the Worker's secrets, read when the page renders.** + +The port keeps the page, the InstantSearch UI and its facets, the hit cards, the product page and its query, and the connector's transformation. Upstream's `demo/seed.sql`, pasted into Supabase's SQL Editor by hand, becomes two `packages/db` migrations — the table, and its twenty demo products byte for byte — applied by `db-migrate` with row-level security before the Worker serves; a local database gets the very same files through `pnpm db:migrate`. Until the owner connects Algolia the page keeps the template's notice and lists the catalogue from Postgres under it. It adds the Workers build, tests (InstantSearch rendered against an Algolia stand-in, in jsdom and in a headless Chrome), the Supabase and Hyperdrive Terraform roots and the deploy contract, and makes the repository an Orun baseline. + +## Status + +| Field | Value | +|-------|-------| +| Status | In progress — AL0 on main | +| Cluster | **AL** (AL0–AL2) | +| Owner(s) | `apps/web` (the pages, the search UI, `lib/db.ts`, `lib/algolia.ts`, `components/catalogue.tsx`, `scripts/migrate.mjs`) · `packages/db` (the migrations and their runner) · `tests/web`, `tests/db` · `infra/terraform/{supabase,cloudflare-hyperdrive}`, `infra/db-migrate` | +| Builds on | `postgres-kysely` (its scaffolding, Supabase and Hyperdrive roots, migrations, health check and baseline machinery), `prisma-nextjs-blog` (the Worker-secrets push), and through them `postgres-drizzle-starter` and `lumen` | +| Upstream | [algolia/algolia-supabase-vercel-starter](https://github.com/algolia/algolia-supabase-vercel-starter) at `da3f27b` (2026-09-04), the Vercel template "Algolia Integration Example" | +| Changes | The client resolution (module-level pool → a pg client per request over Hyperdrive, behind upstream's `query()`), the Algolia keys read per request from Worker secrets, the seed as migrations, the catalogue while not connected, the Workers build, tests, two Terraform roots, the deploy contract; page copy says Cloudflare | +| Decisions locked | (1) `query()` stays upstream's name and API; a WeakMap by the request's ExecutionContext keeps one connected client per request. (2) The Algolia keys are optional Worker secrets, never `NEXT_PUBLIC_`, never in a build. (3) The table, RLS and the demo rows are `db-migrate`'s. (4) The connector is the owner's step in Algolia's dashboard. (5) Infra before app on main: supabase → cloudflare-hyperdrive → db-migrate → web, enforced; advisory on pull requests. | +| Shipped as | `baseline-v1`; proven offline (Baseline CI: migrated into a real Postgres and served through the built Worker under `wrangler dev`, searched in a headless Chrome), deploy gated until a workspace is linked | + +## Read order + +1. `design.md` — what maps to what, the connection and key flow, what is out of scope +2. `implementation-plan.md` — the milestones and what "done" means for each +3. `risks-and-open-questions.md` — what could go wrong and what was decided +4. `IMPLEMENTATION-STATUS.md` — what actually shipped (kept distinct from intent) + +## Milestones at a glance + +| Milestone | What it lands | Done when | +|---|---|---| +| AL0 — the spec | this doc set and the task contracts | merged on `main` | +| AL1 — the search on Workers and Supabase | `apps/web`, `packages/db`, the two Terraform roots, `infra/db-migrate`, `tests/web`, `tests/db` | green; `wrangler dev` lists the catalogue over a migrated database, and the search runs against a stand-in | +| AL2 — a baseline | card, build document, rename, bootstrap tooling, the product's README and context pack, the baseline's tests | a new product goes from nothing to live on stage and prod in one landing; Baseline CI green | diff --git a/specs/epics/algolia-search/design.md b/specs/epics/algolia-search/design.md new file mode 100644 index 0000000..b4b8255 --- /dev/null +++ b/specs/epics/algolia-search/design.md @@ -0,0 +1,82 @@ +# algolia-search — design + +## 1. What the template is + +[algolia/algolia-supabase-vercel-starter](https://github.com/algolia/algolia-supabase-vercel-starter) +at `da3f27b` (2026-09-04) — the Vercel template "Algolia Integration Example": +Next.js 16.3.0 (App Router), `pg` 8.22, `algoliasearch` 5 (the lite client) +and `react-instantsearch` 7, Tailwind 4, Geist. Three moving parts: + +- **The home page** renders the template's hero and `SearchExperience`, a + client component: InstantSearch with a search box, category, price and + rating facets, a "hide out of stock" toggle, stats and highlighted hit + cards. It reads `NEXT_PUBLIC_ALGOLIA_APP_ID` and + `NEXT_PUBLIC_ALGOLIA_SEARCH_API_KEY`, which `next.config`'s `env` inlines + into the browser bundle at build (mapping the bare `ALGOLIA_*` names the + Vercel integration may inject); without them it shows a notice. +- **The product page** (`/products/[id]`) reads one row from Postgres with + `select * from products where id = $1` through a lazy module-level + `pg.Pool` (`lib/db.ts`, with an `ssl` override for Supabase's pooler) and + shows the fields only the database holds: cost price, supplier, stock + location, internal notes. +- **The data**: `demo/seed.sql` (a 12-column table and twenty upserted + products) pasted into the Supabase SQL Editor, and Algolia's Supabase + connector — configured in Algolia's dashboard with `demo/transform.js` — + which maps `id` to `objectID`, keeps the searchable fields, derives + `in_stock` and drops the internal ones. + +## 2. The data plane + +| Upstream | Here | +|---|---| +| Vercel's Supabase integration provisions a project and injects `POSTGRES_URL` | `infra/terraform/supabase` creates one project per environment; `infra/terraform/cloudflare-hyperdrive` one Hyperdrive config over it; their applies publish the credentials and the config id as orun secrets | +| A lazy module-level `pg.Pool` over `POSTGRES_URL`, `sslmode` stripped and `rejectUnauthorized: false` | `lib/db.ts`: `query(text, params)` unchanged, run on one `pg.Client` per request over `env.HYPERDRIVE.connectionString`, kept in a WeakMap keyed by the request's ExecutionContext and connected once (the Workers runtime refuses to reuse a socket across requests). No `ssl` override: the Worker talks to Hyperdrive, Hyperdrive talks TLS to Supabase. Without a Cloudflare context, upstream's own pool over `POSTGRES_URL` | +| `demo/seed.sql` pasted by hand | `packages/db/src/migrations/010_products` (its CREATE TABLE), `020_row_level_security` (RLS with no policy, behind a guard), `030_demo_products` (its twenty upserts, byte for byte), applied by `infra/db-migrate` per environment before the Worker deploys; `pnpm db:migrate` applies the same files to a local database (`apps/web/scripts/migrate.mjs`, ledger in schema `local_migrations`) | +| `pg` on Node | `pg` on workerd under `nodejs_compat`, its socket `pg-cloudflare`'s `workerd` export. `pg` is a Next server-external package, so the build traces it for Node and copies only pg-cloudflare's empty default export; `next.config.ts` ships the package whole (`outputFileTracingIncludes`) | + +## 3. The keys + +Orun never puts a SaaS key in a build. The application id and the +search-only key are browser-safe by Algolia's design — the template hands them +to every visitor — but inlining them at build would mean a bundle per key, a +rebuild per rotation, and a build that has to hold the product's secrets. So: + +- They are the product's optional Orun secrets `ALGOLIA_APP_ID` and + `ALGOLIA_SEARCH_API_KEY` (Algolia's own bare names, which upstream's + next.config already read), resolved by the `web` component's + `optionalSecretEnv`. +- The build runs with them unset (`env -u`); after it, + `tooling/wire/worker-secrets.mjs` pushes the ones that are set to the + Worker with `wrangler secret bulk` (a verify lane, with no Cloudflare + credential, pushes nothing). +- The page (`force-dynamic`) reads them from `getCloudflareContext().env` + per request (`lib/algolia.ts`) and hands them to `SearchExperience` as + props; the component builds its lite client from them. An optional + `ALGOLIA_HOST` replaces Algolia's own hosts (the e2e's stand-in). +- Unset, the page renders the template's notice — naming the secrets — and + `components/catalogue.tsx` under it: the products read from Postgres in the + hits' own cards, only the fields the connector would index. The page wraps + both states in `data-search="connected" | "not-connected"`, which the + deploy's smoke reads. +- The write/admin key is never read: the connector indexes from Algolia's + dashboard, as upstream. + +## 4. The deploy contract + +postgres-kysely's, unchanged in shape: `web` (cloudflare-workers-assets-turbo) +depends on `cloudflare-hyperdrive` and `db-migrate`, which depend on +`supabase`; every edge `mode: enforced` on a push to main and advisory on a +pull request; `wrangler.jsonc` committed as the fixture render of +`wrangler.template.jsonc`, re-rendered by the stage and prod lanes from their +wiring document. The smoke reads the home page's `data-search=` and +`/api/health`'s `"ok":true`. + +## 5. Out of scope + +- Algolia's connector and index settings: the owner's, in Algolia's + dashboard (the product's README, "Connect Algolia"). There is no Orun + composition for Algolia, and the connector needs the Supabase project's + credentials, which it asks Supabase for itself. +- Server-side InstantSearch (`react-instantsearch-nextjs`): the template is + client-only, and so is this. +- Write paths: the template is read-only. diff --git a/specs/epics/algolia-search/epic.yaml b/specs/epics/algolia-search/epic.yaml new file mode 100644 index 0000000..d3bfab8 --- /dev/null +++ b/specs/epics/algolia-search/epic.yaml @@ -0,0 +1,31 @@ +# The epic's declaration. metadata.name equals the directory name; metadata.key +# is the task-key prefix this epic reserves (AL → AL-1, AL-2, …). Owner, target +# date and health are the platform's — never written here. +apiVersion: orun.io/v1 +kind: Epic +metadata: + name: algolia-search + key: AL +spec: + title: "Vercel's Algolia Integration Example on Cloudflare Workers and Supabase" + summary: "Vercel's Algolia Integration Example — Algolia's InstantSearch over a Postgres products table, and a product page that reads the fields only the database holds — runs on Cloudflare Workers + Static Assets with its Postgres a Supabase project reached through Hyperdrive, the table and the template's demo products applied by db-migrate before the app serves and the Algolia keys the Worker's own secrets, inside the Cirrus baseline's repository conventions, and becomes an Orun baseline that takes a new product from nothing to live on stage and prod in one landing." + state: started + milestones: + - name: "AL0 — the spec" + exitCriteria: + - "the repository is born with its intent, CI, tooling and spec index, and this doc set and the task contracts are on main" + tasks: [AL-1] + - name: "AL1 — the search on Workers and Supabase" + exitCriteria: + - "apps/web is the upstream template on Next.js 16.3.8, built by @opennextjs/cloudflare into one Worker with Static Assets; lib/db.ts keeps upstream's `query()`, run on one pg client per request over the HYPERDRIVE binding" + - "the Algolia application id and search-only key are the Worker's secrets, read per request and handed to InstantSearch as props, never in a build; without them the page shows the template's notice and the catalogue from Postgres" + - "the products table, row-level security and upstream's twenty demo products are packages/db migrations that infra/db-migrate applies before the Worker deploys, after the Supabase project and the Hyperdrive config Terraform creates" + - "`pnpm typecheck`, `pnpm lint`, `pnpm test` and `pnpm build` are green; `wrangler dev` over the build lists the catalogue and serves the product pages from a migrated database, and with the Algolia secrets set a browser's InstantSearch renders hits from a stand-in" + - "the README says how to run, test, deploy and connect it, and how every Vercel piece maps to Cloudflare and Supabase" + tasks: [AL-2] + - name: "AL2 — a baseline" + exitCriteria: + - "blueprint.yaml (the console card) and repo-blueprint.yaml (the build) take a new product from nothing to live on stage and prod in one landing (supabase → cloudflare-hyperdrive → db-migrate → web), then record it; the card asks for no Algolia key" + - "a product built from it carries the product only, renamed throughout to its own name, repository, Worker, Supabase project and Hyperdrive config, with the upstream template's links and Algolia's own key names left alone" + - "Baseline CI is green: contract tests, generated component docs, the blueprint read by the pinned orun, the leak gate, the offline bootstrap with the product's own plans, and the end-to-end tier — migrated into a real Postgres, built with no Algolia key reaching the bundle, dry-deployed and served without Algolia and with it (a headless Chrome running the search against a stand-in), and its table shut to the Data API's roles" + tasks: [AL-3] diff --git a/specs/epics/algolia-search/implementation-plan.md b/specs/epics/algolia-search/implementation-plan.md new file mode 100644 index 0000000..ee3dda5 --- /dev/null +++ b/specs/epics/algolia-search/implementation-plan.md @@ -0,0 +1,60 @@ +# algolia-search — implementation plan + +Three milestones, one pull request each, squash-merged when green. + +## AL0 — the spec (AL-1) + +The repository is born (intent with the placeholder workspace, Orun CI byte +for byte turso-starter's, the shared TypeScript and ESLint configuration, the +wrangler renderer, the migrations manifest tool, the spec index), then this +doc set and the three task contracts land. + +**Done when** the epic and the contracts are on `main`. + +## AL1 — the search on Workers and Supabase (AL-2) + +1. The template in `apps/web` on Next.js 16.3.8 with `@opennextjs/cloudflare`: + `output: standalone`, the monorepo tracing root, `initOpenNextCloudflareForDev()`, + pg-cloudflare shipped whole through `outputFileTracingIncludes`; Geist from + the `geist` package; `instantsearch.js` a direct dependency (pnpm does not + hoist it). +2. `lib/db.ts`: upstream's `query()`, on one `pg` client per request (a + WeakMap keyed by the request's ExecutionContext, Hyperdrive's connection + string, connected once), upstream's own pool over `POSTGRES_URL` without a + context. +3. `lib/algolia.ts` and the search UI: the application id and search-only key + (and an optional host) read from the Worker per request, handed to + `SearchExperience` as props; next.config's `env` inlining removed. +4. `components/catalogue.tsx`: the products from Postgres in the hits' cards + while search is not connected; the page marks its state (`data-search`). +5. The table, row-level security and the demo products as `packages/db` + migrations (010, 020, 030) with their manifest; `infra/db-migrate` applies + them; `pnpm db:migrate` applies the same files locally. +6. `/api/health`: 200 only with the table migrated and RLS on; it reports + whether search is connected. +7. The Algolia secrets as `optionalSecretEnv`, unset for the build and pushed + to the Worker after it (`tooling/wire/worker-secrets.mjs`). +8. `infra/terraform/supabase` and `infra/terraform/cloudflare-hyperdrive`, the + wiring template and its committed fixture render, `apps/web/component.yaml`. +9. `tests/web` (InstantSearch in jsdom against an in-process Algolia stand-in) + and `tests/db` (PGlite; the migrations held to upstream's seed). + +**Done when** typecheck, lint, test and build are green and `wrangler dev` +over the build lists the catalogue and serves the product pages from a +migrated database, says search is not connected without the secrets, and with +them a headless Chrome's InstantSearch renders hits from a stand-in. + +## AL2 — a baseline (AL-3) + +postgres-kysely's machinery on this tree: `blueprint.yaml` (the console card: +Cloudflare and Supabase, five brokered keys, the subdomain read from the +connection, no domain and no Algolia key asked), `repo-blueprint.yaml` (one +landing, `01-product`, then the record, `02-docs`), `.baseline/` (identity, +the product's README with "Connect Algolia", task contracts), the rename +engine (the upstream template's links protected, Algolia's own key names +allowed by the leftover sweep), the bootstrap helpers, the deployment-record +renderer, the phase pages, the context pack, and the baseline's tests — the +e2e tier running the search in a headless Chrome against an Algolia stand-in. + +**Done when** Baseline CI is green on the pull request and on `main`, then +`baseline-v1` is tagged. diff --git a/specs/epics/algolia-search/risks-and-open-questions.md b/specs/epics/algolia-search/risks-and-open-questions.md new file mode 100644 index 0000000..72d945f --- /dev/null +++ b/specs/epics/algolia-search/risks-and-open-questions.md @@ -0,0 +1,16 @@ +# algolia-search — risks and open questions + +| ID | Risk | State | Notes | +|---|---|---|---| +| AL-A | One `pg` client per request, never ended explicitly | ACCEPTED | The Workers runtime closes a request's sockets when the request's I/O context ends, and Hyperdrive pools on Cloudflare's side; a client whose connect fails is dropped so the request's next query reconnects (tests/web) | +| AL-B | The Algolia keys are Worker secrets, read per request, not `NEXT_PUBLIC_` variables | ACCEPTED (decision) | No key is in a build (the e2e builds with a key in the lane's environment and greps the output for it); a rotation reaches the Worker on the next deploy. The search key still reaches the browser, as upstream's did — that is what a search-only key is for | +| AL-C | The index is filled by Algolia's connector, configured by the owner in Algolia's dashboard | ACCEPTED | As upstream. There is no Orun composition for Algolia; until it is connected the product is live and whole, its catalogue listed from Postgres | +| AL-D | Two Supabase projects per product (stage and prod) | ACCEPTED | The free plan's limit per org; a BOOTSTRAP prerequisite | +| AL-E | Supabase's Data API serves `public` to the anon key — the internal-only cost prices, suppliers and notes included | RESOLVED | Migration 020 enables row-level security with no policy on every table in `public`, behind a guard that refuses to lock the app out; tests/db and the e2e show anon and authenticated read and write nothing | +| AL-F | This baseline's env-var prefix (`ALGOLIA_SEARCH_`) is Algolia's own spelling for its search key, and the rebrand's leftover sweep treats the prefix as residue | ACCEPTED | `tooling/rebrand/rebrand.mjs` allows exactly `ALGOLIA_SEARCH_API_KEY` and `NEXT_PUBLIC_ALGOLIA_SEARCH_API_KEY`; any other `ALGOLIA_SEARCH_…` still fails (testing/rebrand.test.sh) | +| AL-G | Hyperdrive's read cache can serve a product page or the catalogue up to about a minute stale after a write behind the app's back | ACCEPTED | The template is read-only; the index lags the database by the connector's sync anyway | +| AL-H | The migrations package is named `@saas/db` | ACCEPTED | stack-tectonic's db-migrate composition builds `--filter=@saas/db` | +| AL-I | The e2e needs Google Chrome or Chromium | ACCEPTED | InstantSearch is client-only, so only a browser proves the search; GitHub's ubuntu runners carry google-chrome | +| AL-J | No live bootstrap yet: real Terraform applies, a real db-migrate apply, a real deploy and secret push, Hyperdrive's cache, a real Algolia application and connector | OPEN | One bootstrap from a tag against a workspace, then Algolia connected to it | +| AL-K | `pg-cloudflare` is shipped by a trace include that names pnpm's store layout (`../../node_modules/.pnpm/pg-cloudflare@*/…`) | ACCEPTED (watch) | Without it OpenNext's bundle fails "Could not resolve pg-cloudflare"; the e2e builds in a staged job tree, so a layout change fails there | +| AL-L | Product images are hot-linked from Unsplash, as upstream's seed has them | ACCEPTED | The demo data is the template's; an owner replaces it with a migration of their own | diff --git a/tasks/AL-1.TaskContract.yaml b/tasks/AL-1.TaskContract.yaml new file mode 100644 index 0000000..3955420 --- /dev/null +++ b/tasks/AL-1.TaskContract.yaml @@ -0,0 +1,14 @@ +apiVersion: orun.io/v1 +kind: TaskContract +metadata: + name: AL-1 + title: "The algolia-search epic doc set" +spec: + goal: The epic (epic.yaml, README, design, implementation plan, risks, as-built status) and the three task contracts are on main, so every later change lands against a declared milestone + affects: + - specs/epics/algolia-search + - tasks + doneWhen: + - specs/epics/algolia-search has the six documents and tasks/ the three contracts + - the epic index lists the epic + gates: [] diff --git a/tasks/AL-2.TaskContract.yaml b/tasks/AL-2.TaskContract.yaml new file mode 100644 index 0000000..5da6a7f --- /dev/null +++ b/tasks/AL-2.TaskContract.yaml @@ -0,0 +1,20 @@ +apiVersion: orun.io/v1 +kind: TaskContract +metadata: + name: AL-2 + title: "The search on Workers and Supabase" +spec: + goal: Vercel's Algolia Integration Example runs on Cloudflare Workers + Static Assets with its Postgres a Supabase project reached through Hyperdrive — upstream's query() on a pg client per request, the Algolia keys the Worker's secrets read per request, the table, row-level security and the demo products applied by db-migrate before the Worker serves — with its tests, and its README saying how it maps to Vercel + affects: + - apps/web + - packages/db + - infra + - tests + - tooling/wire + - pnpm-lock.yaml + - pnpm-workspace.yaml + - README.md + doneWhen: + - pnpm typecheck, lint, test and build are green + - wrangler dev over the build lists the twenty demo products and serves their pages from a migrated database, says search is not connected without the Algolia secrets, and with them a browser's InstantSearch renders hits from a stand-in + gates: [] diff --git a/tasks/AL-3.TaskContract.yaml b/tasks/AL-3.TaskContract.yaml new file mode 100644 index 0000000..57766f8 --- /dev/null +++ b/tasks/AL-3.TaskContract.yaml @@ -0,0 +1,33 @@ +apiVersion: orun.io/v1 +kind: TaskContract +metadata: + name: AL-3 + title: "algolia-search becomes an Orun baseline" +spec: + goal: blueprint.yaml and repo-blueprint.yaml take a new product from nothing to live on stage and prod in one landing (Supabase, then Hyperdrive, then the migrations, then the app), branded as its own, its schema and demo products applied before the app serves and shut to Supabase's Data API, its Algolia keys never in a build, held by Baseline CI + affects: + - blueprint.yaml + - repo-blueprint.yaml + - BOOTSTRAP.md + - .baseline + - .github/workflows/baseline.yml + - .github/workflows/tag.yml + - ai/context + - docs/phases + - hooks + - testing + - tooling/rebrand + - tooling/bootstrap + - tooling/catalog + - apps/web/docs + - packages/db/docs + - infra + - tests/web/docs + - tests/db/docs + - specs/epics/algolia-search + - README.md + doneWhen: + - every Baseline CI tier is green + - the product's own push-to-main plan orders supabase → cloudflare-hyperdrive → db-migrate → web, enforced + - a product placed from it carries the product only, branded, with the upstream template's links and Algolia's own key names unrenamed + gates: []