Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/baseline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ jobs:
echo "── $t"
bash "testing/$t.test.sh"
done
# Every component's docs/{overview,architecture,runbook}.md is generated
# from what the component declares (its manifest, Worker config,
# Terraform, migrations, package scripts). A pull request that changes
# one of those facts without regenerating the pages fails here.
- name: Component docs match their manifests
run: python3 tooling/catalog/gen-component-docs.py --check

# Tier 1. THE BLUEPRINT, READ AND PLACED BY THE BINARY A BOOTSTRAP RUNS.
#
Expand Down
44 changes: 44 additions & 0 deletions ai/context/current.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# rag-chatbot — where this repository stands

The entry page for anyone (or any agent) picking this repository up. It is
this repository's own state and does not ship to a product built from it; the
two pages that do are [operations.md](operations.md) and
[deployment.md](deployment.md).

## What it is

Vercel's AI SDK RAG chatbot (`ai-sdk-preview-rag`) on Cloudflare Workers +
Static Assets through `@opennextjs/cloudflare`, its memory a pgvector table in
a Supabase project's Postgres reached through Hyperdrive, packaged as an Orun
baseline: `orun new --blueprint repo-blueprint.yaml` builds a new product in
its own repository and takes it to live on stage and prod in one landing
(`01-product`) — the Supabase project, then the Hyperdrive config, then the
migrations, then the Worker — and records what went live (`02-docs`). A fresh
product chats and embeds on Workers AI through its `AI` binding, with no AI
vendor key.

## Release

| | |
|---|---|
| Latest tag | `baseline-v1` (RG5, [#6](https://github.com/sourceplane/rag-chatbot/pull/6)) |
| On `main` since | RG6 — the cirrus standard (no kiox, generated component docs, this context pack) |
| Proven live | No. Everything short of the platform is proven offline in Baseline CI, with stand-ins for Workers AI and Supabase's Management API. The real models, a deploy with the brokered token and its `AI` binding, the Terraform and db-migrate applies and a live bootstrap are still to come (risks RG-J, RG-K) |

## Where to read

| Question | Page |
|---|---|
| What shipped, and every departure from upstream | `specs/epics/rag-chatbot/IMPLEMENTATION-STATUS.md` |
| Why it is built this way | [decisions.md](decisions.md), then `specs/epics/rag-chatbot/design.md` |
| What could still go wrong | [open-risks.md](open-risks.md), then `specs/epics/rag-chatbot/risks-and-open-questions.md` |
| How to instantiate it | `BOOTSTRAP.md`, `docs/phases/` |
| What each component is | `docs/` beside each `component.yaml` — `apps/web`, `packages/db`, `infra/db-migrate`, `infra/terraform/{supabase,cloudflare-hyperdrive}`, `tests/{web,db}` (generated; `python3 tooling/catalog/gen-component-docs.py`) |

## Next

1. Bootstrap one product from the next tag against a workspace with
Cloudflare and Supabase connected (two free project slots, RG-O): the first
run of the real models and of the `AI` binding under the brokered deploy
token, and the first Terraform and db-migrate applies (closes RG-J, RG-K).
2. Cut the next `baseline-vN` with `tag.yml` so the registry can pin RG6.
20 changes: 20 additions & 0 deletions ai/context/decisions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# rag-chatbot — decisions

The choices that shape this repository, one line each, with where the full
reasoning lives. Superseded decisions stay listed, marked, so the history
reads in order.

| # | Decision | Why | Source |
|---|---|---|---|
| 1 | OpenNext on Workers + Static Assets, the static-assets incremental cache | The template is a Next.js App Router app; the pre-rendered shell needs nothing provisioned | `specs/epics/rag-chatbot/design.md` §2 |
| 2 | `getDb()` builds a `postgres` client per call over the `HYPERDRIVE` binding; no module-level pool | Workers refuse a socket opened in another request; Hyperdrive holds the pool | risk RG-A, design.md §3, §7 |
| 3 | pgvector stays; not Vectorize | The template's retrieval is pgvector SQL; Vectorize would rewrite the schema, both queries and the insert path | design.md §7 |
| 4 | The database comes from Terraform (a Supabase project and a Hyperdrive config per environment) and the schema from db-migrate (`@saas/db`) before the Worker deploys, not from drizzle's migrator inside `next build` | Lumen's roots and runner; the build stays credential-free | design.md §2, §4 |
| 5 | Workers AI through an `AI` binding is the default for both models (Llama 4 Scout chat, bge-base-en-v1.5 at 768 dimensions), each step one whole `AI.run`; `AI_GATEWAY_API_KEY` is optional and switches only the chat. Supersedes RG1–RG4's gateway-only models | The platform brokers no AI vendor; a key that switched the embeddings would orphan every stored vector | risks RG-H, RG-I, design.md §7 |
| 6 | The embeddings column is resized to 768 by a new migration; `010_rag_initial` stays upstream's byte for byte | Migrations are immutable once applied | IMPLEMENTATION-STATUS (RG5) |
| 7 | `apps/web/wrangler.jsonc` is committed as the fixture render; the stage and prod lanes re-render it from the Hyperdrive wiring document | The composition checks the file exists before the build | design.md §4 |
| 8 | Row-level security with no policy on every table in `public` | Supabase's Data API serves `public` to the anon key; the app, as owner, is not subject to it | risk RG-M |
| 9 | One landing (`01-product`) then the record (`02-docs`); one convergence runs supabase → cloudflare-hyperdrive → db-migrate → web, enforced on `main`, advisory on a pull request | A job resolves its secrets when it starts, so each apply must finish before its reader | design.md §4, `repo-blueprint.yaml` |
| 10 | `ci.yml` plans nothing while `intent.yaml` names `ws_CHANGE_ME` | A bootstrap cannot set a repository variable (replaced the `ORUN_CI` gate) | IMPLEMENTATION-STATUS (RG5), `.github/workflows/ci.yml` |
| 11 | stack-tectonic 0.19.0, not 0.20.0 | 0.20.0's lane-scoped install links `node_modules` out of the job tree, which Next 16's Turbopack refuses | `intent.yaml`, `BOOTSTRAP.md` (Troubleshooting) |
| 12 | orun is pinned only where it runs (the `orun-action` `version:` in each workflow); no kiox. Component docs are generated from the manifests and checked in CI | `kiox.yaml` pinned a version nothing ran; a hand-written copy of the dependency graph and bindings drifts silently | IMPLEMENTATION-STATUS (RG6), `tooling/catalog/gen-component-docs.py` |
16 changes: 16 additions & 0 deletions ai/context/open-risks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# rag-chatbot — open risks

What is not yet proven or not yet decided. The full write-up of every risk,
including the accepted and resolved ones, is
`specs/epics/rag-chatbot/risks-and-open-questions.md`; this page lists only
what is still live, so it should be short.

| ID | Risk | State | What closes it |
|---|---|---|---|
| RG-J | The models are verified only against a stand-in: whether Llama 4 Scout calls the tools as prompted (with the `id` the provider needs), whether JSON mode accepts understandQuery's schema, and how well bge-base-en-v1.5 retrieves are unproven | OPEN | A bootstrap against a real Cloudflare account, chatting on the deployed Worker |
| RG-K | No deploy with the brokered `workers-deploy` token has uploaded a script with an `ai` binding; Cloudflare may ask for a Workers AI permission | OPEN | The first live deploy (or the permission group added to the template) |

Accepted, and worth knowing before you change them: RG-N (the e2e's Postgres
stand-in creates pgvector as superuser; a real db-migrate apply is the first
time `postgres` runs that statement), RG-O (two Supabase projects per product
— the organization needs room for both).
28 changes: 28 additions & 0 deletions apps/web/docs/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# web — architecture

Built by the turbo pipeline from `apps/web` and deployed with wrangler.

| | |
| ------------------- | ----------------------------------------------- |
| Worker config | `apps/web/wrangler.template.jsonc` |
| Entry | `.open-next/worker.js` |
| Compatibility date | `2026-10-01` |
| Compatibility flags | `nodejs_compat`, `global_fetch_strictly_public` |

## Bindings

| Binding | Kind | Source |
| ------------ | ---------------------------- | -------------------------------------------- |
| `HYPERDRIVE` | Hyperdrive (pooled Postgres) | `hyperdrive_id` from `cloudflare-hyperdrive` |
| `AI` | Workers AI | account-level; no resource to provision |
| `ASSETS` | Static assets | `.open-next/assets` |

Identifiers marked _from_ a component are filled in at deploy time from
that component's published outputs for the environment being deployed.

## Wiring

Resolved at deploy time from secrets the infrastructure lanes publish (names only):

- `WIRING_CLOUDFLARE_HYPERDRIVE_PROD`
- `WIRING_CLOUDFLARE_HYPERDRIVE_STAGE`
21 changes: 21 additions & 0 deletions apps/web/docs/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# web

Vercel's AI SDK RAG chatbot (Next.js on Cloudflare Workers + Static Assets via @opennextjs/cloudflare, Workers AI for chat and embeddings, per environment)

A Cloudflare Worker with Static Assets (server rendering plus static files), deployed per environment by its CI lane.

| | |
| ------------ | ------------------------------------------------------------------------------------------------------------------ |
| Type | `cloudflare-workers-assets-turbo` |
| Source | `apps/web` |
| Package | `@app/web` |
| Environments | `dev`: `verify` · `stage`: `verify`, `deploy` on a push to `main` · `prod`: `verify`, `deploy` on a push to `main` |

## Depends on

- **cloudflare-hyperdrive** — Provisions Cloudflare Hyperdrive resources for stage and prod Supabase Postgres databases
- **db-migrate** — Applies database migrations to stage and prod Supabase instances

## Depended on by

- (none)
40 changes: 40 additions & 0 deletions apps/web/docs/runbook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# web — runbook

## How it runs

Pull requests verify it; a merge to `main` deploys it to `stage`, `prod`.
CI plans what changed (`orun plan --changed`) and runs each lane with `orun run`;
the plan orders a component after everything it depends on.

## Local commands

```bash
pnpm --filter @app/web dev
pnpm --filter @app/web build
pnpm --filter @app/web typecheck
pnpm --filter @app/web lint
pnpm --filter @app/web preview
```

## Verify

After a deploy the lane runs this smoke against the environment it
deployed (`ORUN_ENVIRONMENT`); `DEPLOYED_URL` overrides the URL:

```bash
html=$(curl -sfL "${DEPLOYED_URL}/") && echo "${html}" | grep -q 'placeholder="Ask me anything..."' && health=$(curl -sf "${DEPLOYED_URL}/api/health") && echo "${health}" | grep -q '"ok":true'
```

## Rollback

Revert the commit on `main`. The next convergence applies the previous desired
state; nothing was changed out of band, so there is nothing else to undo.

## Common failures

- **A `WIRING_*` secret is missing at deploy**: an infrastructure lane upstream
has not applied for this environment yet. Within one convergence the plan
runs it first; on its own, re-run that lane.
- **The smoke fails right after a first deploy**: a new workers.dev route can
answer 4xx for a few seconds and the lane retries. A failure that persists
is a regression in the deployed code.
19 changes: 19 additions & 0 deletions infra/db-migrate/docs/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# db-migrate — architecture

Applies the migrations under `packages/db/src/migrations` in order, recording each one
it applies so a run only ever applies what is new. An already-applied migration
is never edited: a change is a new, later migration.

## Migrations

- `000_control`
- `010_rag_initial`
- `020_workers_ai_embeddings`
- `030_row_level_security`

## Resolved at run time (names only)

- `SUPABASE_ACCESS_TOKEN`
- `SUPABASE_DB_PASSWORD`
- `SUPABASE_DB_URL`
- `SUPABASE_PROJECT_REF`
21 changes: 21 additions & 0 deletions infra/db-migrate/docs/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# db-migrate

Applies database migrations to stage and prod Supabase instances

Applies this repository's ordered database migrations per environment: plan on pull requests, apply on merge to `main`.

| | |
| ------------ | ------------------------------------------------------------------------------------------ |
| Type | `db-migrate` |
| Source | `infra/db-migrate` |
| Environments | `stage`: `plan`, `apply` on a push to `main` · `prod`: `plan`, `apply` on a push to `main` |

## Depends on

- **db** — The database migrations and their runner (built by the db-migrate component before it plans or applies)
- **supabase** — Provisions Supabase projects for stage and prod and wires credentials into orun secrets
- **cloudflare-hyperdrive** — Provisions Cloudflare Hyperdrive resources for stage and prod Supabase Postgres databases

## Depended on by

- **web** — Vercel's AI SDK RAG chatbot (Next.js on Cloudflare Workers + Static Assets via @opennextjs/cloudflare, Workers AI for chat and embeddings, per environment)
19 changes: 19 additions & 0 deletions infra/db-migrate/docs/runbook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# db-migrate — runbook

## How it runs

Pull requests plan it; a merge to `main` applies it to `stage`, `prod`.
CI plans what changed (`orun plan --changed`) and runs each lane with `orun run`;
the plan orders a component after everything it depends on.

## Rollback

Revert the commit on `main`. The next convergence applies the previous desired
state; nothing was changed out of band, so there is nothing else to undo.

## Common failures

- **An applied migration's file changed**: the run stops rather than guess.
Restore the file and write the change as a new migration.
- **The database is unreachable**: the infrastructure lane it depends on has
not applied for this environment; that lane runs first in a convergence.
31 changes: 31 additions & 0 deletions infra/terraform/cloudflare-hyperdrive/docs/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# cloudflare-hyperdrive — architecture

A `terraform` root at `infra/terraform/cloudflare-hyperdrive/terraform`.

- **State** lives in the Orun Cloud HTTP state backend — no local state and
no state bucket to manage.
- **Credentials are brokered per run** from the workspace's provider
connections and resolved by name; none is stored in this repository.
- **Adoption** (`adopt.tf`): when state is empty but the resource already
exists, plan-time import adopts it instead of failing.

## Resources

- `cloudflare_hyperdrive_config.postgres`

## Published outputs

Written as job-output secrets on each environment after an apply; deploy
lanes downstream resolve them by name.

| Secret | Terraform output |
| ------------------------------ | ---------------- |
| `WIRING_CLOUDFLARE_HYPERDRIVE` | `wiring` |

## Resolved at run time (names only)

- `CLOUDFLARE_ACCOUNT_ID`
- `CLOUDFLARE_API_TOKEN`
- `TF_VAR_cloudflare_account_id`
- `TF_VAR_supabase_db_password`
- `TF_VAR_supabase_project_ref`
20 changes: 20 additions & 0 deletions infra/terraform/cloudflare-hyperdrive/docs/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# cloudflare-hyperdrive

Provisions Cloudflare Hyperdrive resources for stage and prod Supabase Postgres databases

Terraform-managed infrastructure, planned and applied per environment by its CI lane.

| | |
| ------------ | ---------------------------------------------------------------------------------------------------- |
| Type | `terraform` |
| Source | `infra/terraform/cloudflare-hyperdrive` |
| Environments | `stage`: `plan-only`, `apply` on a push to `main` · `prod`: `plan-only`, `apply` on a push to `main` |

## Depends on

- **supabase** — Provisions Supabase projects for stage and prod and wires credentials into orun secrets

## Depended on by

- **db-migrate** — Applies database migrations to stage and prod Supabase instances
- **web** — Vercel's AI SDK RAG chatbot (Next.js on Cloudflare Workers + Static Assets via @opennextjs/cloudflare, Workers AI for chat and embeddings, per environment)
20 changes: 20 additions & 0 deletions infra/terraform/cloudflare-hyperdrive/docs/runbook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# cloudflare-hyperdrive — runbook

## How it runs

Pull requests plan it; a merge to `main` applies it to `stage`, `prod`.
CI plans what changed (`orun plan --changed`) and runs each lane with `orun run`;
the plan orders a component after everything it depends on.

## Rollback

Revert the commit on `main`. The next convergence applies the previous desired
state; nothing was changed out of band, so there is nothing else to undo.

## Common failures

- **Provider authentication fails**: the workspace's provider connection is
missing or revoked. Reconnect it in the console; credentials are brokered
from it per run.
- **"Already exists" on a first apply**: state is empty but the resource is
not. `adopt.tf` imports it at plan time; if it still fails, the resource was renamed outside Terraform.
31 changes: 31 additions & 0 deletions infra/terraform/supabase/docs/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# supabase — architecture

A `terraform` root at `infra/terraform/supabase/terraform`.

- **State** lives in the Orun Cloud HTTP state backend — no local state and
no state bucket to manage.
- **Credentials are brokered per run** from the workspace's provider
connections and resolved by name; none is stored in this repository.
- **Adoption** (`adopt.tf`): when state is empty but the resource already
exists, plan-time import adopts it instead of failing.

## Resources

- `random_password.db_password`
- `supabase_project.this`

## Published outputs

Written as job-output secrets on each environment after an apply; deploy
lanes downstream resolve them by name.

| Secret | Terraform output |
| ---------------------- | ------------------- |
| `SUPABASE_PROJECT_REF` | `project_ref` |
| `SUPABASE_DB_PASSWORD` | `database_password` |
| `SUPABASE_DB_URL` | `connection_uri` |

## Resolved at run time (names only)

- `SUPABASE_ACCESS_TOKEN`
- `TF_VAR_supabaseOrgId`
20 changes: 20 additions & 0 deletions infra/terraform/supabase/docs/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# supabase

Provisions Supabase projects for stage and prod and wires credentials into orun secrets

Terraform-managed infrastructure, planned and applied per environment by its CI lane.

| | |
| ------------ | ---------------------------------------------------------------------------------------------------- |
| Type | `terraform` |
| Source | `infra/terraform/supabase` |
| Environments | `stage`: `plan-only`, `apply` on a push to `main` · `prod`: `plan-only`, `apply` on a push to `main` |

## Depends on

- (none)

## Depended on by

- **cloudflare-hyperdrive** — Provisions Cloudflare Hyperdrive resources for stage and prod Supabase Postgres databases
- **db-migrate** — Applies database migrations to stage and prod Supabase instances
Loading
Loading