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 @@ -52,6 +52,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
43 changes: 43 additions & 0 deletions ai/context/current.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# ai-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 chatbot built from it; the
two pages that do are [operations.md](operations.md) and
[deployment.md](deployment.md).

## What it is

Vercel's Next.js AI Chatbot on Cloudflare Workers + Static Assets (through
`@opennextjs/cloudflare`): a Supabase Postgres reached through Hyperdrive,
attachments in R2, and Workers AI as the default model provider. It is
packaged as an Orun baseline: `orun new --blueprint repo-blueprint.yaml`
builds a new chatbot in its own repository and takes it to live on stage and
prod in one landing (`01-product`: Supabase, then Hyperdrive, then the
migrations, then the app, with the R2 bucket beside them), then records what
went live (`02-docs`).

## Release

| | |
|---|---|
| Latest tag | `baseline-v1` (AC5, [#6](https://github.com/sourceplane/ai-chatbot/pull/6)) |
| On `main` since | AC6 — 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: the e2e tier builds the product in orun's staged job tree, migrates a local Postgres with its own runner and serves the Worker over local R2 with a stand-in AI binding. No bootstrap from a tag has run against a workspace yet: the three applies, db-migrate against a real project, a real deploy, Workers AI itself (AC-K) and the `BLOB` binding under the deploy token (AC-L) are unproven |

## Where to read

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

## Next

1. Bootstrap one chatbot from a tag against a workspace with Cloudflare and
Supabase connected, and record its timings: the first proof of the
applies, the deploy, Workers AI's models and tools (AC-K) and the R2
binding under the deploy token (AC-L).
2. Cut the next `baseline-vN` with `tag.yml` so the registry can pin AC6.
20 changes: 20 additions & 0 deletions ai/context/decisions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# ai-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, `next` pinned to 16.3.8 | The newest Next the adapter boots; the Worker serves SSR, streaming routes and `proxy.ts` | `specs/epics/ai-chatbot/design.md` §2, epic README |
| 2 | Supabase Postgres through Hyperdrive, one `postgres` client per `getDb()` call; the schema in `packages/db`, applied by db-migrate | A Workers socket cannot outlive its request; drizzle-kit stays for local databases | design.md §2, risk AC-A |
| 3 | Attachments in R2 behind `/api/files/<key>`, public by unguessable URL | As Vercel Blob's `access: "public"`: the model provider and `next/image` fetch without the cookie | design.md §2, risk AC-B |
| 4 | Static-assets incremental cache; no Redis (resumable streams and the IP rate limit off) | Nothing revalidates or is `"use cache"`; both Redis features are opt-in upstream | design.md §4, risks AC-C, AC-D |
| 5 | Workers AI through the `AI` binding is the default model provider (Kimi K2.6 chat, GLM 4.7 Flash titles); the AI Gateway's models only with `AI_GATEWAY_API_KEY`. *Supersedes "the gateway, unchanged" (AC1–AC4)* | The platform brokers no AI vendor: a fresh product must chat with nothing configured | IMPLEMENTATION-STATUS (AC5), risk AC-K |
| 6 | The session key is `AUTH_SECRET` if set, else one migration `020_app_secrets` generated into `app_private.secrets`. *Supersedes "Worker secrets set by hand" (AC-H)* | A bootstrap cannot set a Worker secret; `app_private` is not a schema the Data API exposes | IMPLEMENTATION-STATUS (AC5), risk AC-N |
| 7 | Row-level security with no policy on every table in `public` (`030_row_level_security`) | Supabase's Data API serves `public` to the anon key; the app owns the tables | risk AC-M |
| 8 | `apps/web/wrangler.jsonc` is committed as the fixture render; every lane re-renders it (stage/prod from wiring, dev from the fixture) | The lane's structure check needs it in a fresh checkout, before the build | IMPLEMENTATION-STATUS (AC5), `apps/web/component.yaml` |
| 9 | One landing (`01-product`) then the record (`02-docs`); infra before app enforced on main, advisory on a pull request; the R2 root on its own `r2-data` key | A landing has a fixed cost; the plan carries deploy order; the deploy token does not cover R2 | `repo-blueprint.yaml`, `epic.yaml` (AC5), risk AC-L |
| 10 | stack-tectonic 0.19.0, not 0.20.0 | 0.20.0 links each job's `node_modules` out of its tree, which Next 16's Turbopack refuses | `intent.yaml`, BOOTSTRAP.md |
| 11 | orun is pinned only where it runs (the `orun-action` `version:` in each workflow); no kiox | `kiox.yaml` pinned a version (v2.15.0) nothing ran | IMPLEMENTATION-STATUS (AC6) |
| 12 | Component docs are generated from the manifests and checked in CI | A hand-written copy of the dependency graph and bindings drifts silently | `tooling/catalog/gen-component-docs.py` |
17 changes: 17 additions & 0 deletions ai/context/open-risks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ai-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/ai-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 |
|---|---|---|---|
| AC-K | Workers AI is the default but cannot be called offline: that Kimi K2.6 and GLM 4.7 Flash are served on the account, that their tool calls drive the artifact tools, and that the suggestions tool's structured output is honoured are unproven (the e2e uses a stand-in binding) | OPEN | A live bootstrap that chats, titles and edits an artifact |
| AC-L | The Worker's `BLOB` R2 binding is attached by `wrangler deploy` under the `workers-deploy` token; whether Cloudflare accepts it is unverified, and the fix would be the platform's token template | OPEN (platform) | The first live `web` deploy |

Not yet exercised live either, though resolved offline: that the Management
API runs migrations as `postgres`, so it owns the tables (AC-M; the guard
stops the migration if not). Accepted, and worth knowing before you change
them: AC-C (static-assets cache), AC-D (no Redis), AC-G (no BotID), AC-I
(bcrypt at module load), AC-O (a prerender/resume mismatch logged on `/`).
31 changes: 31 additions & 0 deletions apps/web/docs/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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 |
| ------------ | ---------------------------- | -------------------------------------------- |
| `BLOB` | R2 bucket | `r2_bucket_name` from `cloudflare-r2` |
| `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`
- `WIRING_CLOUDFLARE_R2_PROD`
- `WIRING_CLOUDFLARE_R2_STAGE`
22 changes: 22 additions & 0 deletions apps/web/docs/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# web

Vercel's Next.js AI Chatbot (Next.js on Cloudflare Workers + Static Assets via @opennextjs/cloudflare, 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
- **cloudflare-r2** — Provisions the Cloudflare R2 bucket the app binds (stage and prod)
- **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
pong=$(curl -sf "${DEPLOYED_URL}/ping") && echo "${pong}" | grep -qx pong && 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_initial`
- `020_app_secrets`
- `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 Next.js AI Chatbot (Next.js on Cloudflare Workers + Static Assets via @opennextjs/cloudflare, 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 Next.js AI Chatbot (Next.js on Cloudflare Workers + Static Assets via @opennextjs/cloudflare, 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.
29 changes: 29 additions & 0 deletions infra/terraform/cloudflare-r2/docs/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# cloudflare-r2 — architecture

A `terraform` root at `infra/terraform/cloudflare-r2/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_r2_bucket.app`

## 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_R2` | `wiring` |

## Resolved at run time (names only)

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

Provisions the Cloudflare R2 bucket the app binds (stage and prod)

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

| | |
| ------------ | ---------------------------------------------------------------------------------------------------- |
| Type | `terraform` |
| Source | `infra/terraform/cloudflare-r2` |
| 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

- **web** — Vercel's Next.js AI Chatbot (Next.js on Cloudflare Workers + Static Assets via @opennextjs/cloudflare, per environment)
Loading
Loading