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
9 changes: 9 additions & 0 deletions .baseline/identity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"_comment": "THIS TREE'S OWN IDENTITY — the FROM side of every rebrand. tooling/rebrand/rebrand.mjs reads it to know what to rename AWAY from; a product's identity is the --values file, which is the TO side. Change a value here only together with the tree: these strings must be what the tree actually contains, or the rename silently misses and the product ships carrying this baseline's name. `productName` is the SITE_NAME seam in apps/web/shared/site.ts; `domain` and `subdomain` are placeholders, because this baseline has never deployed under a name of its own; `githubOwner` is the owner in the source links the docs carry.",
"slug": "nitro-websockets",
"pascal": "NitroWebsockets",
"productName": "Nitro WebSockets Starter",
"domain": "nitro-websockets.example.com",
"subdomain": "your-workers-subdomain",
"githubOwner": "sourceplane"
}
145 changes: 145 additions & 0 deletions .baseline/product/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# nitro-websockets

A realtime room on one **Cloudflare Worker**: everyone on the page sees each
other's cursors live, with a presence bar and emoji reactions, over a single
WebSocket. The page is a [Nitro v3](https://nitro.build/) + React app built by
Vite; every WebSocket upgrade to `/api/ws` goes to one **Durable Object**, the
room, which holds each connection as a hibernatable WebSocket — so a quiet
room costs nothing while its sockets stay open. Every merge to `main` deploys
it to stage, then prod.

Built from Vercel's Nitro + WebSockets Starter (vercel/examples, MIT — the
licence is `apps/web/LICENSE.md`).

## Live deployment

<!-- 02-docs:begin -->
Not recorded yet. The bootstrap's last phase writes the live URLs, the
workspace and the secrets the app uses into
[ai/context/deployment.md](ai/context/deployment.md); how the app deploys
and how to operate it is in
[ai/context/operations.md](ai/context/operations.md).
<!-- 02-docs:end -->

## Routes

| Route | Answers |
|---|---|
| `GET /` (and any other page path) | the app: `index.html`, titled with the site's name (`SITE_NAME`), and its script from `/assets/` |
| `WS /api/ws` | the room: `welcome` (you and everyone already here), then `join`, `leave`, `cursor` and `reaction` frames from the others, and `pong` to your `ping` (`apps/web/shared/types/realtime.ts`) |
| `GET /api/health` | `{"status":"ok","name":"<SITE_NAME>","environment":"<env>"}` — what the deploy's smoke and an operator's probe read |
| a missing file (`/assets/old.js`, `/x.png`) | `404` |

## What is here

| Surface | Where |
|---|---|
| The page — cursors, presence bar, reactions | `apps/web/app/` (`App.tsx`; the connection, reconnect and heartbeat in `apps/web/app/hooks/use-realtime.ts`) |
| The room | `apps/web/server/api/ws.ts` |
| Each connection's identity, kept with its socket | `apps/web/server/utils/presence.ts` |
| The wire protocol | `apps/web/shared/types/realtime.ts` |
| The site's name (the page's title and share card, and `/api/health`) | `apps/web/shared/site.ts` |
| Nitro's config (the `cloudflare_durable` preset, WebSockets on) | `apps/web/vite.config.ts` |
| Worker config (name, compatibility date, the room's Durable Object and its migration, the build output as the Worker and its Static Assets, per-environment vars) | `apps/web/wrangler.jsonc` |
| Deploy contract | `apps/web/component.yaml` (`cloudflare-worker-turbo`) |
| Tests | `tests/web` (vitest: the room driven through crossws's own Cloudflare adapter over a stand-in Durable Object, a hibernation included) |

## Getting started

```bash
pnpm install
pnpm typecheck
pnpm lint
pnpm test
pnpm build # vite build → apps/web/.output (the Worker and the page's assets)
```

Run it with Vite's dev server (Node), or the built Worker under workerd with
its Durable Object, and open it in two tabs:

```bash
pnpm --filter @app/web dev # vite dev, http://localhost:3000
pnpm --filter @app/web preview # wrangler dev over the build, http://localhost:8787
curl http://localhost:8787/api/health # {"status":"ok",…,"environment":"local"}
```

## How the room keeps its state

There is one room, one Durable Object (Nitro's preset names the class and its
binding `$DurableObject`). When nobody has said anything for a while the
runtime evicts it and keeps only the open sockets; the next message wakes it.
A wake loses module memory and each peer's `context`, so nothing the room
needs may live there: each connection's identity is written into its socket's
serialized attachment (`rememberIdentity` in `server/utils/presence.ts`), and
the roster is read from the room's live sockets (`othersOf`). Keep that rule
when you add to the room — give an authenticated user's id to
`rememberIdentity` in place of `createIdentity()`, and read it back with
`recallIdentity`, not from a module variable.

## How it deploys

Merges to `main` converge through [Orun](https://github.com/sourceplane/orun):
`.github/workflows/ci.yml` plans the changed components and runs each lane
against the workspace in `intent.yaml`. The app deploys to `stage`, then to
`prod` once stage has converged on the same commit; `dev` only verifies. The
Worker is `nitro-websockets-web-<env>` on your account's workers.dev subdomain, and
every deploy is smoke-tested: `/api/health` must answer `"status":"ok"` for that
environment, and `/api/ws` must answer a WebSocket handshake with `101` (the
smoke's connection is dropped after 3 seconds, so anyone on the page may see a
passer-by join and leave). A pull request runs the same pipeline's verify lanes:
the Worker built, type-checked and dry-deployed for each environment, with
nothing deployed. They do not lint or run the tests, so run `pnpm typecheck`,
`pnpm lint` and `pnpm test` before you push. (A push to `main` builds,
deploys and smoke-tests.)

The pipeline holds one credential, GitHub's own `GITHUB_TOKEN`. The
Cloudflare token and account id are brokered per run from the workspace's
Cloudflare connection; nothing provider-shaped is stored in the repository
or in GitHub.

**Deploy by hand.** Orunbase's **Deploy** button on a component, or **Run
workflow** on the CI workflow in GitHub's Actions tab (on `main`), runs
`ci.yml` for the environment and components you pick, and each one deploys
exactly as a merge would deploy it. Leave the component blank to redeploy
every component in that environment, with nothing changed: after rotating a
secret, say. A component with nothing to run in that environment (a test
suite in `prod`) is left out with a notice.

## Adding a binding or a secret

The Worker binds the room's Durable Object (declared by class, created by the
deploy from the migration in `wrangler.jsonc`) and nothing that needs a
resource id, so `wrangler.jsonc` is committed as it deploys. To give it a KV
namespace, a D1 database or an R2 bucket, add the resource under `infra/` as
its own component and have `apps/web/component.yaml` depend on it;
`tooling/wire/` renders a wrangler template from the ids that component
publishes. A binding goes in every `env` block too: wrangler does not inherit
bindings into an environment. A runtime secret (an API key for a service the
app calls) is an Orun secret: `orun secrets set <KEY> --org <ws> --env <env>`,
declared in the component's `optionalSecretEnv` and named in its
`runtimeSecrets`, then pushed to the Worker after each deploy. Until it is set,
the route that needs it should say so rather than fail. In a route a request's
bindings are `event.req.runtime.cloudflare.env` (as `/api/health` reads
`ENVIRONMENT`).

## Custom domain

The app answers on workers.dev from its first deploy. To serve it on your own
domain, add the domain to the Cloudflare account the app deploys into and
attach it to the Worker (dashboard → Workers & Pages → `nitro-websockets-web-prod` →
Settings → Domains & Routes → Add → Custom domain); do the same for stage
with a subdomain if you want one. The workers.dev URL keeps answering, and the
deploy's smoke keeps reading it — unless the environment sets `DEPLOYED_URL`
in `intent.yaml`, which the smoke reads instead:

```yaml
environments:
prod:
env:
DEPLOYED_URL: https://nitro-websockets.example.com
```

The repository names the domain in one place, `BASE_DOMAIN` in `intent.yaml`
(one line per environment), which the app was born with as the reserved
placeholder `nitro-websockets.example.com` — the example above carries it too.
Replace it with your domain in the same change that attaches it.
23 changes: 23 additions & 0 deletions .baseline/tasks/01-product.TaskContract.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Task contract template for the 01-product landing — the whole app in one
# pull request. Unbound to a key on purpose: `orun.task/ensure@v1` seals and
# attaches it to the key the allocator issues. It lives under `.baseline/`,
# not `tasks/`, because `tasks/` is this repository's own work tree
# (`intent.yaml` `work:`), where an unkeyed contract is an error. gates: [] is
# a DECLARATION — merge alone finishes this task; the main convergence is the
# gate the bootstrap watches, not one the plane observes as a PR check.
#
# `affects` names COMPONENTS, by metadata.name: the phase's retouch hook reads
# this list to decide which component.yaml files get the redeploy marker, so
# the deploy set and the task plane's statement of the work are one list.
apiVersion: orun.io/v1
kind: TaskContract
spec:
goal: The app's repository is born and linked, the whole app lands in one pull request, and one convergence deploys it to stage and prod
affects:
- web
- web-tests
doneWhen:
- repo pushed, main pinned as default, workspace linked
- main convergence green (build, deploy and smoke on stage and prod)
- /api/health answers 200 on both workers.dev URLs
gates: []
13 changes: 13 additions & 0 deletions .baseline/tasks/02-docs.TaskContract.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Task contract template for the 02-docs landing. See 01-product's header.
apiVersion: orun.io/v1
kind: TaskContract
spec:
goal: The live deployment is recorded from verified facts as agent-readable state in the app's repo
affects:
- ai/context/deployment.md
- ai/context/operations.md
- README.md
doneWhen:
- committed deployment manifest matches probed reality
- prod waits on stage again, and dev verifies every merge to main again
gates: []
138 changes: 138 additions & 0 deletions .github/workflows/baseline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
name: Baseline CI

# THE FACTORY'S OWN LANES — this repository tested as a BASELINE: that its
# build document parses with the orun a bootstrap actually runs, that its card
# agrees with it, that every component is placed by exactly one phase, that a
# placed tree comes up branded and carrying nothing of the factory, and that
# the app it produces installs, builds, deploys (dry) and serves.
#
# ─── WHY THIS IS NOT IN ci.yml ────────────────────────────────────────────
#
# `repo-blueprint.yaml`'s `github-workflows` module copies the WHOLE `.github`
# directory into every app built from this repository. These jobs run
# `testing/*.sh`, and `testing/` is deliberately not placed — so in a product
# they would be steps that cannot pass, red on its very first pull request. This
# file is in the blueprint's `ignore` list, and `testing/coverage.test.sh`
# fails on any workflow that reads `testing/` and is not.

on:
push:
branches: [main]
pull_request:

concurrency:
group: baseline-ci-${{ github.event_name == 'pull_request' && github.ref || github.run_id }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

permissions:
contents: read

jobs:
# Tier 0. bash, python3 and git; node for the tools. No workspace install,
# no network, no credential — the job that most certainly fails a PR.
baseline-contract:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-node@v5
with:
node-version: 22
# setup-node v5 turns on the pnpm cache by itself when package.json
# names a packageManager, and then fails on a runner with no pnpm.
# This job installs nothing.
package-manager-cache: false
- name: Contract tests (manifest, phases, coverage, rebrand, retouch, bootstrap-defer, render)
run: |
set -euo pipefail
python3 -c 'import yaml' 2>/dev/null || pip install --quiet pyyaml
for t in manifest phases coverage rebrand retouch bootstrap-defer render; do
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.
#
# Pinned at the bootstrap floor (BOOTSTRAP.md), not the app's lane pin:
# every test here reads YAML with PyYAML, which parses anything well-formed;
# only the pinned orun can say whether the document is one a bootstrap can
# run. `--status` parses it, validates every hook against the action
# registry, compiles every `when` and narration template, and writes
# nothing. Then the leak gate (what an app would carry, and what its files
# say) and the placement tier (the offline bootstrap: both phases placed and
# finished by the blueprint's own hooks, then the app's own `orun plan` for
# the push that merges 01-product).
blueprint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: sourceplane/orun-action@v1.2.0
with:
version: v2.88.0
- uses: pnpm/action-setup@v4
with:
version: 10.12.1
- uses: actions/setup-node@v5
with:
node-version: 22
# Nothing in this job installs (the offline bootstrap runs no pnpm
# since the lockfile hook went), so there is no pnpm store to
# cache — and setup-node v5's own pnpm cache, on by itself because
# package.json names a packageManager, fails the job in its post
# step when it finds none.
package-manager-cache: false
- name: repo-blueprint.yaml, read by the pinned CLI
run: |
set -euo pipefail
orun --version
python3 -c 'import yaml' 2>/dev/null || pip install --quiet pyyaml
# Required inputs must be supplied for the document to resolve; the
# values are arbitrary and nothing is written.
orun new --blueprint repo-blueprint.yaml --status \
--out "$(mktemp -d)" \
--set reponame=acme-room \
--set "productname=Acme Room" \
--set githuborg=acme-inc
- name: the leak gate — what an app carries
run: bash testing/leak.test.sh
- name: placement — the offline bootstrap, and the app's own plan
run: |
git config --global user.email ci@example.invalid
git config --global user.name "baseline ci"
bash testing/placement.test.sh

# Tier 2. THE APP IT PRODUCES WORKS: both landings installed, their
# fixtures rendered, type-checked, linted, tested and built, every run step
# of the stage lane's pull-request plan (the structure check before any
# build, the build, the typecheck, the dry deploy) in the job tree orun
# stages, and the Worker served by `wrangler dev --env stage` and
# smoke-tested with the component's own smoke command. Pinned at the
# bootstrap floor (v2.88.0): the offline bootstrap reads the blueprint (its
# one `secrets` reconcile, its background task set-up, its landings checked
# in the box), and the floor is newer than the app's lane pin (v2.78.4),
# which is all the lane steps need.
product:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: sourceplane/orun-action@v1.2.0
with:
version: v2.88.0
- uses: pnpm/action-setup@v4
with:
version: 10.12.1
- uses: actions/setup-node@v5
with:
node-version: 22
cache: pnpm
- name: e2e — the bootstrapped app builds, deploys (dry) and serves
run: |
python3 -c 'import yaml' 2>/dev/null || pip install --quiet pyyaml
git config --global user.email ci@example.invalid
git config --global user.name "baseline ci"
bash testing/e2e.test.sh
Loading
Loading