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
73 changes: 73 additions & 0 deletions .baseline/product/AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# AGENTS.md — {{ .productname }}

How to work in `{{ .githuborg }}/{{ .reponame }}`, for coding agents and
the people running them.

## What this is

{{ .productname }} is a multi-tenant SaaS on Cloudflare alone.
`apps/api-edge` is the only public Worker; it reaches twelve bounded-context
Workers (`apps/*-worker`: identity, membership, projects, policy, events,
config, metering, billing, notifications, webhooks, integrations, admin)
over service bindings. `apps/web-console-next` is the console, and it talks
only to the edge. The data plane is a D1 database per environment and a KV
namespace for the edge's idempotency store. `stage` and `prod` deploy; `dev`
only verifies.

## Read first, in this order

1. [ai/context/operations.md](ai/context/operations.md) — the operating
contract: tenancy, how a merge deploys, the secrets model, how to verify.
2. [ai/context/deployment.md](ai/context/deployment.md) — the live URLs, the
workspace and the secrets by name. It reads "Not rendered yet" until the
bootstrap's last phase has written it.
3. [intent.yaml](intent.yaml) — environments, lanes, the pinned composition
stack and the workspace CI runs against.
4. [apps/api-edge/docs/overview.md](apps/api-edge/docs/overview.md) — the
edge and what it depends on. Every component carries the same three pages
in its `docs/`: overview, architecture, runbook.
5. `apps/web-console-next` — the console.
6. `packages/contracts` — the API, tenancy, event and error types the
Workers and the console share. A change to an API's shape starts here.

## How a change lands

- Everything deployable is an Orun component: a directory under `apps/`,
`infra/`, `packages/` or `tests/` with a `component.yaml`. Orun discovers
it, and `intent.yaml` says where it deploys.
- Before a pull request: `pnpm install`, `pnpm typecheck`, `pnpm lint`,
`pnpm test`; then `orun validate --intent intent.yaml` and
`orun plan --changed --intent intent.yaml` to see what CI will run.
- One task, one pull request. CI plans the changed components and verifies
them; the merge to `main` deploys them, `stage` first and then `prod`.
- A green run that planned no lanes deployed nothing. Check the outcome —
the URLs, the published secrets — as operations.md shows.
- A schema change is a new numbered directory with an `up.sql` under
`packages/db/src/migrations/`, registered with its checksum in
`packages/db/src/manifest.ts`. Never edit one that has been applied.
`infra/db-migrate` applies them on merge. D1 has no interactive
transactions: see the README.

## Do not hand-edit

- `ai/context/deployment.md`, and the README section between
`<!-- 08-docs:begin -->` and `<!-- 08-docs:end -->`: both are regenerated
from probed state.
- `apps/*/wrangler.jsonc` of a Worker that has a `wrangler.template.jsonc`:
it is rendered at deploy time and gitignored. Edit the template.
- `pnpm-lock.yaml` (let pnpm write it), `.rebrand/values.json` (the record
of the inputs this product was built with) and, if present,
`apps/web-console-next/console-artifact.pin.json` (the prebuilt-console
pin).
- `execution.state.workspace` in `intent.yaml`: every remote operation,
CI's included, runs against that workspace.
- The last line of a `component.yaml` that reads `# orun: redeploy …` is a
deploy trigger, not configuration; nothing reads it back.

## Secrets

Never print or commit a secret value, and never put one in `ai/context/`.
The Cloudflare credentials are brokered per run from the workspace's
connection; runtime secrets are set with
`orun secrets set <KEY> --org <workspace> --env <env>` and inspected by
name only.
96 changes: 96 additions & 0 deletions .baseline/product/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# {{ .productname }}

A multi-tenant SaaS on Cloudflare alone, kept as an
[Orun](https://github.com/sourceplane/orun) desired-state repository:
twelve bounded-context Workers behind one public edge API, a Next.js console
on Workers + Static Assets, and Cloudflare D1 and KV as the data plane.
Merges to `main` deploy it to `stage` and `prod`.

## Live deployment

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

## What is here

| Surface | Where |
|---|---|
| The public HTTP API — the only Worker with a public route | `apps/api-edge` |
| Bounded-context Workers (identity, membership, projects, policy, events, config, metering, billing, notifications, webhooks, integrations, admin), reached only through the edge | `apps/*-worker` |
| The console | `apps/web-console-next` |
| API, tenancy, event and error types shared by every Worker and the console | `packages/contracts` |
| D1 executor, repositories and migrations | `packages/db` |
| SDK and CLI | `packages/sdk`, `packages/cli` |
| Data plane (D1 per environment, the edge's KV) and the parked custom domain | `infra/terraform/*` |
| Migration runner, applied on merge | `infra/db-migrate` |
| Tests, one suite per component | `tests/*` |
| Environments, lanes, the pinned composition stack, the workspace | `intent.yaml` |
| Each component's overview, architecture and runbook | `apps/*/docs`, `packages/*/docs` |

Working with a coding agent? Start it at [AGENTS.md](AGENTS.md).

## Getting started

Node.js 20 or later and pnpm 10 or later.

```bash
pnpm install
pnpm typecheck
pnpm lint
pnpm test
pnpm build
```

With the `orun` CLI at the version `.github/workflows/ci.yml` pins:

```bash
orun validate --intent intent.yaml
orun plan --changed --intent intent.yaml --output plan.json
```

## How it deploys

`.github/workflows/ci.yml` runs `orun plan --changed`, then one lane per
group of planned jobs with `orun run --remote-state` against the workspace
`intent.yaml` declares. A pull request plans and verifies; a merge to `main`
deploys the changed components to `stage` and then `prod`. `dev` only
verifies. The deploy credentials are brokered per run from the workspace's
Cloudflare connection, CI signs in over GitHub OIDC, and Terraform state is
kept by Orun Cloud — nothing provider-shaped is stored in this repository or
in GitHub. [ai/context/operations.md](ai/context/operations.md) is the full
operating contract.

## Working with D1

D1 is SQLite, and the code is explicit about what that costs:

- **No interactive transactions.** `executor.transaction(...)` runs its
statements in order with no rollback. Where all-or-nothing matters, use a
single statement with `RETURNING`, or an upsert with `ON CONFLICT`.
- **SQLite types.** Timestamps and JSON documents are text, booleans are
0/1; `@saas/db/json` owns the decoding.
- **No schemas.** A bounded context's tables share a name prefix
(`identity_users`, `membership_organizations`), enforced by the
repositories.

## What needs your credentials

Sign-in with GitHub or Google needs OAuth client secrets; billing needs a
Polar access token, a webhook secret and `POLAR_PRODUCT_MAP` pointed at your
own Polar products; email needs Cloudflare Email Service set up for a
verified sending domain. The header comments of
`apps/identity-worker/wrangler.template.jsonc` and
`apps/billing-worker/wrangler.template.jsonc` say how each secret is set,
and `orun secrets set <KEY> --org <workspace> --env <env>` sets a runtime
secret in the workspace.

## Custom domain

The product answers on workers.dev from its first deploy. To serve it on
your own domain, follow "Setting the domain later" in
[ai/context/operations.md](ai/context/operations.md).
7 changes: 7 additions & 0 deletions .baseline/product/deployment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# {{ .reponame }} — live deployment manifest

> Not rendered yet. The bootstrap's `08-docs` phase probes every live URL and
> regenerates this file from what answered — the workspace, the URLs and
> their status, and the secrets inventory by name. Until then, nothing here
> claims the product is live. The operating contract is
> [operations.md](operations.md).
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# cirrus-baseline

Reusable **Cloudflare-only** multi-tenant SaaS starter, built as an
[Orun](https://opencode.ai/docs) component-native desired-state repo. Identity,
[Orun](https://github.com/sourceplane/orun) component-native desired-state repo. Identity,
organizations, projects, RBAC, audit, metering, billing, webhooks, and
notifications ship as separate bounded-context Cloudflare Workers behind a single
public edge API, with a Next.js console on Workers + Static Assets — and
Expand Down Expand Up @@ -62,12 +62,13 @@ it rather than quiet:

This baseline births new products from one artifact — `repo-blueprint.yaml`,
run by `orun new` (**[BOOTSTRAP.md](BOOTSTRAP.md)** →
[docs/phases/](docs/phases/README.md)). It declares nine idempotent phases —
scaffold, foundation, infrastructure, workers, workers-restore, edge,
console, optional domain, docs — each landing a verified slice, each
selectable with `--phase <name>`. Products receive **product-only content**
(source, infra, CI, their own docs); none of this baseline's machinery
ships, and a product's docs speak only about the product.
[docs/phases/](docs/phases/README.md)). It declares two idempotent phases —
`01-product`, the whole product in one landing, and `08-docs`, the record of
what went live — each selectable with `--phase <name>`. Products receive
**product-only content** (source, infra, CI, their own README, `AGENTS.md`
and context pack, from `.baseline/product/` and `ai/context/`); none of this
baseline's machinery ships, and a product's docs speak only about the
product.

## Prerequisites

Expand Down Expand Up @@ -135,7 +136,7 @@ released there, and adopted here by bumping the pinned tag.

## CI

CI is powered by [Orun](https://opencode.ai/docs) with the local Stack Tectonic
CI is powered by [Orun](https://github.com/sourceplane/orun) with the published Stack Tectonic
composition stack. `.github/workflows/ci.yml` calls only `orun plan` and
`orun run` — no direct `pnpm`, `turbo`, Wrangler, or Terraform commands run in
GitHub Actions. The Orun runtime is pinned where it runs: the `orun-action`
Expand Down
28 changes: 15 additions & 13 deletions ai/context/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,25 @@ orun integrations list --org <ws> --json && orun cloud check --org <ws>
| headless/CI credentials | `ORUN_TOKEN` (workspace-scoped) + `GITHUB_TOKEN` |

The ops phases (docs refresh and the original deployment sequence) all live
in ONE versioned artifact: `repo-blueprint.yaml` in the
baseline this product was born from. Clone it at a tag and select the phase
— from anywhere, with the two tokens above exported:
in ONE versioned artifact: `repo-blueprint.yaml` in the baseline this
product was built from, at the tag the Orun Cloud registry publishes for it.
`orun baseline new` fetches it at that tag and places one phase into a
checkout of this repository — from anywhere, with the two tokens above
exported:

```bash
git clone --depth 1 https://github.com/OPSFLOWS baseline && cd baseline

orun new --blueprint repo-blueprint.yaml --out <product-checkout> \
--run-hooks --phase 08-docs --values <values.yaml> # docs refresh
orun baseline list # the baselines your workspace can build
orun baseline new <baseline> --local --out <product-checkout> --run-hooks \
--phase 08-docs --values <product-checkout>/.rebrand/values.json # docs refresh
```

`<values.yaml>` carries this product's identity — at minimum `reponame`,
`productname`, `githuborg` and `orunWorkspace`, which the blueprint validates
before anything runs. `--run-hooks` is what makes a phase
land and verify rather than just place files; `--status` previews without
writing. Every phase is idempotent — re-running a completed one is a no-op
that re-verifies.
`<baseline>` is the registry id of the baseline this product was built
from, one of those `orun baseline list` names. `.rebrand/values.json` is
this product's identity as its build recorded it — `reponame`,
`productname`, `githuborg`, `orunWorkspace` and the rest — which the
blueprint validates before anything runs. `--run-hooks` is what makes a phase land and verify rather than just
place files. Every phase is idempotent — re-running a completed one is a
no-op that re-verifies.

## Setting the domain later

Expand Down
13 changes: 7 additions & 6 deletions blueprint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ metadata:
Multi-tenant SaaS on Cloudflare alone: 12 bounded-context Workers behind
one edge API, a Next.js console, D1 + KV data plane, migrations, and CI
that converges on merge. One provider, one consent, one token. Live on
stage+prod in ~25-30 minutes.
stage and prod in about fifteen minutes.
tags: [saas, cloudflare, d1, nextjs]
spec:
source:
Expand Down Expand Up @@ -129,7 +129,7 @@ spec:
from: connection.cloudflare.workersSubdomain

# ── PREVIEW ONLY. `repo-blueprint.yaml`'s `integrations/reconcile` hooks
# mint these, brokered, during phase 02. ────────────────────────────────
# mint these, brokered, during 01-product. ──────────────────────────────
# Transcribed from those hooks' own `keys:` and `template:` values, so
# `testing/manifest.test.sh` compares this list to the document that mints
# them rather than to a copy of itself — it used to compare against a shell
Expand Down Expand Up @@ -179,10 +179,11 @@ spec:
# this repo at its pinned tag would have nothing to run. `manifestPath` in
# the registry row names THIS file, the console card; it is not the build.
blueprint: repo-blueprint.yaml
# Was 60, then 30; baseline-v21 measured 14m51s over five landings
# (docs/phases/TIMINGS.md). One landing and one deploy per worker should
# bring it to about 9 — an estimate until a run measures it.
expectedMinutes: 12
# Was 60, then 30; baseline-v21 measured 14m51s over five landings.
# Fifteen is the one-landing build's budget until a run measures it: the
# sum of the phases' own `expectedMinutes` (01-product 13, 08-docs 2),
# which testing/manifest.test.sh holds equal to this figure.
expectedMinutes: 15
tokenChannel: ORUN_TOKEN_FILE # how a runner hands over its refreshed token
verify:
urls:
Expand Down
5 changes: 3 additions & 2 deletions docs/phases/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ modes are the same files.

`--resume` places every phase not already derived as done, in dependency
order, honouring each barrier. Expected wall-clock on a clean run: **about
9 minutes** (an estimate until a run measures it; baseline-v21 measured 14m51s
over five landings, and it was 50–65 before that):
15 minutes** — `01-product` 13, `08-docs` 2, the figure the console card
shows (a budget until a one-landing run measures it; baseline-v21 measured
14m51s over five landings, and it was 50–65 before that):

- **One landing builds the product.** `01-product` places the whole tree and
lands it as one PR; the convergence that follows deploys everything in
Expand Down
19 changes: 9 additions & 10 deletions hooks/render-deployment-docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,16 @@ mkdir -p ai/context
if [ ! -f ai/context/operations.md ] && [ -f "$L/ai/context/operations.md" ]; then
cp "$L/ai/context/operations.md" ai/context/operations.md
# The template speaks the source repo's name; the product copy must
# speak the product's. Reuse the rebrand values, then fill the
# rebrand-inert OPSFLOWS placeholder with the baseline this product came
# from — the repo whose repo-blueprint.yaml holds the ops phases.
# speak the product's. Reuse the rebrand values — after staging the copy,
# because rebrand.mjs rewrites only what `git ls-files` lists.
#
# Nothing else needs filling. The guide used to carry an `OPSFLOWS`
# placeholder that this step replaced with the baseline's repository (its
# default the retired `sourceplane/cirrus`); it now names no repository and
# tells the reader to fetch the phases with `orun baseline new`, which asks
# the registry where the baseline lives.
git add ai/context/operations.md >/dev/null 2>&1 || true
node "$L/tooling/rebrand/rebrand.mjs" --values "$vals" --allow-dirty >/dev/null 2>&1 || true
opsref="${ORUN_BASELINE_REPO:-sourceplane/cirrus}"
python3 - "$opsref" <<'PYOPS'
import sys
p="ai/context/operations.md"
s=open(p).read()
open(p,"w").write(s.replace("OPSFLOWS", sys.argv[1]))
PYOPS
echo "render-docs: healed ai/context/operations.md"
fi

Expand Down
Loading
Loading