iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / internal-packages / installer / README.mdmain at aacbc74

# iterate Cloud

The wizard that puts a mind's stack into a person's own Cloudflare account, at its own domain, and
the pages that update or replace it later. The Cloud keeps no long-lived token to a deployment
(`worker/access.js`), and `GET /kept` lists every row it keeps.

**The Cloud does not install the stack as it is now.** It installs the stack from before
core/alchemy's one Alchemy script, whose source is gone (last at `e5ac1986d`): `container/run.sh`
and `worker/stack-config.js` write that stack's config and run its scripts. Five tests that hold
the Cloud against that source are out of `pnpm test`, under the package script
`test:awaits-the-one-alchemy-script` (`knip.ts` names them). The step that switches the Cloud to
the one script brings them back. What follows describes the Cloud as it was.

## Who signs up

For now, Jonas sets up each new deployment together with its owner, in a personal walkthrough that
goes through this wizard. So the wizard stays open to everybody, and a person with no deployment
is told where the way in is: the wizard's first step and an empty "Your deployments" show a notice
that links to the waiting list on iterate.com (`src/parts.tsx` `WalkthroughOnly`). A person whose
accounts hold a deployment does not see it.

## Sign-in and a run

- A person signs in at Cloudflare with OAuth and PKCE. The Cloud takes no refresh token: one that
  Cloudflare sends is revoked at once. The access token lives about an hour in KV, and the page
  holds a session cookie alone (`worker/cloudflare-access.js`).
- The consent asks for the account scopes a deploy needs (`worker/scopes.js`). The wizard's
  consent also asks the scope that makes API tokens, which the person ticks on Cloudflare's page:
  with it, the first deploy turns on telemetry and gives the mind a Cloudflare connection. A
  mind without them gets them at an update: the card's "Update iterate" offers the same consent
  first, and the update without it (`src/update-consent.ts`).
- A run happens in a container that holds the stack's source at one commit
  (`container/pack-stack.sh`). Its steps: check, erase (for "Erase and replace" alone), deploy,
  answer, verify (`container/run.sh`). The run revokes the token at its end.
- The owner signs in to the new mind with a passkey, added through a one-use link to
  `https://iterate.<domain>/keys/first`. The Cloud stores neither the link nor its hash
  (`worker/first-owner.js`).

## Your deployments, and Update

The list is read live from the person's account and from each deployment (`worker/deployments.js`).

Update deploys again from the config that the stack keeps beside its state, with its members. It
stops before any change when the stack keeps no config, when the config names another domain or
account, when the vault's key is outside Cloudflare, or when the config asks for a sandbox image
built on the machine. A config from before `mind.images` that names no sandbox image gets
`mind.sandboxImage: "published"`, the older word for iterate's public image.
A config with no domain yet (a stack deployed before its domain) gets the run's domain, with mail
and the mailed sign-in code, and loses `cloudflarePlan`. A stack with the Workers of before the
rename to "mind" cannot be updated: erase and replace it (`container/run.sh`).

### What Update refuses

An update puts the iterate of the Cloud's image over the iterate that the deployment runs. The run
checks before it changes anything, and each refusal says "Nothing was changed."

- **An iterate that this Cloud does not have.** A deploy writes its commit as a tag on the stack's
  boundary Worker: `iterate-commit:<40 hex>`, never a branch's name (`core/alchemy/src/stack.ts`).
  The image holds the list of its own commit and every commit behind it (`/stack/COMMITS`, from
  `container/pack-stack.sh`), and the Worker gets the same list at its build
  (`worker/stack-commits.js`). A commit that is not in the list is newer than the Cloud, or from
  another branch. The card says so and shows no "Update iterate" (`worker/deployments.js`), and
  the run stops (`container/run.sh` `not_unknown_stack`).
- **"Update anyway".** The owner of a fork, or a person who wants the Cloud's iterate back, can
  go on with one more press. Its button says what it does: "Update anyway: this replaces
  `<theirs>` with `<the Cloud's>`". The page sends the commit that the person saw. The run goes on
  only when the stack still runs that commit, and it records which commit it replaced (`replaced`
  in the run's result).
  The button stays after a run from here that failed, the run that this check stopped included:
  what hides it is what stops every update (a stack from before the rename, an incomplete stack, a
  vault key outside Cloudflare). Such a run is an update: the stack keeps its owner and its config.
- **A stack with no tag** was deployed before 12 November 2026. The Cloud's own record of its last
  deploy there stands in for the tag; a run that fails keeps that record. With no tag and no
  record the update runs as before, with a note in its log. For a stack from main that is safe: an
  iterate that writes no tag is older than this check, so it is older than a Cloud that has the
  check. A stack from a branch that was cut before the check, and deployed later, is not caught.
- **An answer that the run cannot read** (no list in the image, two commit tags, a tag that is no
  commit, a refused read) stops the run. It never updates on a guess.
- **An older stack** (the Workers of before the rename to "mind"), **a vault key outside
  Cloudflare**, **a sandbox image built on the machine**, and **a config for another domain or
  account**: as above.

"Erase and replace" asks none of this: it replaces whatever is there. It never runs on
`iterate.com`, `iterate.app`, `garple.com` or `templestein.family`
(`container/takeover.mjs`).

## Erase and replace

The Cloud reads from the person's account what is on the domain and under the stack's names, and
shows it. The person types the domain to agree. The run erases that list and no other, and then
deploys as on an empty domain (`container/takeover.mjs`, `src/ErasePage.tsx`, `src/erase.ts`).

- **Removed:** the sites and Worker routes on the domain, its mail, DNS records and rules, and the
  earlier iterate: the stack's Workers, databases, KV, buckets, repos, state, telemetry and its
  API tokens, with its members, passkeys, files, config repo, secrets, logs and traces.
- **Never removed:** the domain itself, its plan, nameservers, DNSSEC and registration; other
  domains; Email Routing's destination addresses; a tunnel, a Pages project or an R2 bucket that
  only has a name on this domain; a Worker that is not the stack's (only its routes and addresses
  on this domain go); the account's members, billing and other tokens.
- An item that the run cannot remove stops the erase before it erases anything: the person removes
  it first.
- A bucket that R2 refuses to delete because of an unfinished upload is left, with a rule that
  removes the upload after a day. The next erase takes it.

## Chat installations

The Cloud's app in the iterate mind runs an install as a chat. It holds no token, ticket or key:
it calls the Cloud's operations (`worker/service.js`), and sends the person to the Cloud's own
pages for what they do in their browser (`/i/<id>/<kind>`, single-use links). It calls the same
operations in one of two ways:

- **`itx.services.cloud.<method>(…)`**: the mind's stack binds the entrypoint `Service`
  (`worker/main.js`). The mind Worker puts who calls first, and a call of any mind but
  `SERVICE_MIND` is refused (`FORBIDDEN`). The installation keeps who asked (`asked`). Methods:
  `installationCreate`, `installationStatus`, `installationChoose`, `installationLink`,
  `installationPoke`, `installationResources`, `deploymentAt`, `handover`,
  `installationDeployedAt`, `ownerInstallations`, `installationForget`, `enrol`, `cloudKey`,
  `forgetEnrolment`.
- **The bearer routes `/service/*`**, for the holder of `SERVICE_TOKEN`.

- **The run starts at the consent**, with no domain and no card. One Durable Object per
  installation (`inst:<id>`, `worker/installation.js`) runs the phases: `prep` (the container,
  the account's workers.dev name, the state store, the build) and then `early`, the stack before
  its domain, at once; the deploy when Workers Paid, R2 and an active domain are all there.
  `early` deploys with no domain and no mail, with `cloudflarePlan: "free"` while Workers Paid or
  R2 is off, and only under a name that holds no stack; the deploy after it gives the same stack
  its domain and the rest (core/alchemy README, "Before the domain and before Workers Paid"). An
  account that has all three at the consent gets one deploy. An alarm polls the waits (5 s,
  doubling to 60 s). `prep` and `early` only shorten the deploy: a failure of one is a note
  (`container/run.sh` `ITERATE_PHASE`).
- **Sandboxes start on the Cloudflare-managed image.** A chat installation's `early` says
  `mind.containers: false` (a Free account has no Containers). The deploy at the domain names no
  image, and run.sh gives a stack that `early` made its containers (`stack_updatable`): sandboxes
  start at once on `cloudflare/debian-trixie`, and the mind installs what it needs there
  (`tasks/iterate-cloud/designs/phased-deploy.md`, phase D). The wizard's deploy pushes iterate's
  image, as before.
- **The token ends while the run waits.** The installation then waits for a fresh consent, which
  resumes it; nothing done is done again. The deploy needs 30 minutes of token. A deploy that
  failed is tried again after a fresh consent, three tries at most.
- **The owner's address is not at the mind's domain.** The stack refuses a member's address at
  its own domain or under it (the mind receives that mail), so `choose` refuses such a domain,
  or such an owner, on the field that changed, and `domainBuy` does not buy such a domain. Once
  `early` ran, the owner is locked.
- **One installation per deployment** of the app (`{ deployment, owner }`), of the mind named
  by `SERVICE_MIND`; at most 10 for one owner. The id is a hash of the mind and the deployment.
- **The consent is the owner's.** The first must come from the Cloudflare login with the owner's
  email; its Cloudflare user is kept, and later consents must be that user's. Another login is
  refused, its token revoked, and the page names the owner's email. The owner's consent also sets
  a cookie, which the pages that spend money (buy a domain), pick for the person (an account), or
  give the owner's one-use passkey link need.
- **A deployment made elsewhere** (the app's own Alchemy run) is tied to its installation with
  `installationDeployedAt`, once the Cloud enrolled it: one zone for an installation, one
  installation for a zone (an object `zone:<domain>`). The handover then goes to it as to one the
  Cloud deployed.
- **Forgetting.** `installationForget` forgets an installation between phases and frees the
  owner's place; the deployment is not touched. `forgetEnrolment` forgets the enrolment of a
  deployment whose `/keys/stack.json` no longer answers.
- **The handover** to the new mind's onboarding agent goes once, after the Cloud enrolled the
  deployment, to the installation's own domain (`access.js` `handOver`).
- The pretend Cloudflare's tag `auto` steps a world through Workers Paid and R2 with nobody at
  the dashboard (`pretend/scenarios.js`).

## The Cloud's key in a mind's secret

A mind trusts the Cloud's P-256 key (`CLOUD_KEY`, a private JWK) with actor `iterate-cloud` and
paths `/integrations/` once a member allowed it: the Cloud's `/enrol` links to the mind's
`/keys/enrol` with `cloud=<the Cloud's origin>`, and the mind gives that name only to the key the
origin publishes at `/.well-known/iterate-cloud` (core/auth-worker `cloud-key.ts`). A mind's secret can hold that same key and sign the
same statements (`itx.secrets.signStatement`, core/lib `api.ts`), so code on the iterate.com mind
can sign for the Cloud. To move the key in, once:

1. An agent on the iterate.com mind asks for the form:
   `itx.secrets.collectFromUser({ path: "/secrets/iterate-cloud-key", egress: { urls: ["https://iterate.iterate.com"] }, fields: [{ name: "privateKey", label: "CLOUD_KEY (the private JWK)", multiline: true }], operations: ["signStatement", "seal", "unseal", "signHmac", "verifyHmac"], statementPaths: ["/integrations/"] })`.
   The urls are the pin every secret has; a signing secret is never sent.
2. A member opens the link, copies "Iterate Cloud (prd) signing key CLOUD_KEY" from 1Password, and
   pastes it. The secrets Worker refuses anything but a P-256 private JWK.
3. `itx.secrets.describe("/secrets/iterate-cloud-key")` answers `public.keyId`: it must equal the
   `key` of a statement the Cloud signs today (`GET https://cloud.iterate.com/.well-known/iterate-cloud`
   publishes the public key). From then on the key is in no answer, log or fact.

The same secret derives the Cloud's two other keys, so it opens and makes what the Cloud makes:
`itx.secrets.seal` and `unseal` with `info: "iterate-cloud/seal"` are the sealed refresh tokens,
and `signHmac` and `verifyHmac` with `derive: { info: "iterate-cloud/proof" }` are the proofs (hex;
the Cloud writes them as base64url). `src/secrets-compat.test.ts` in `cloud-clients` checks both
against `keys.ts`. The operations are fixed when the secret is made: a key collected with
`signStatement` alone is deleted and collected again to get the others.

The Worker here still signs with its own `CLOUD_KEY` until it moves; the two copies are the same
key.

## Analytics

With `POSTHOG_KEY` in the Worker's config, the pages send anonymous events to PostHog: a page
viewed, a wizard step reached, a deploy, an update or an erase asked, a deploy done or failed
(`src/analytics.ts`). An event carries no email, domain, account id or token. There is no
autocapture and no session recording, the URL is cut to its origin and path, and the IP address is
not kept.

## Production

`iterate-cloud-prd` carries every provider's webhook to every mind, so a fault in it is a mind that
hears nothing. It goes to production in one way:

```sh
doppler run --project _shared --config prd --only-secrets CLOUDFLARE_API_TOKEN -- pnpm run deploy:prd --plan
doppler run --project _shared --config prd --only-secrets CLOUDFLARE_API_TOKEN -- pnpm run deploy:prd \
  --commit <commit> --expect-live <version id> --lock ~/.cache/beelink-heavy-check.lock
```

`--plan` changes nothing: it names the commit of the tree and the live version, and lists the
commits that a deploy releases since the live one. Whoever approves the deploy passes both back,
as `--commit` and `--expect-live`.

The deploy (`scripts/deploy-prd.mjs`) refuses a tree that is not a clean commit of `origin/main`,
packs the stack for the Deployer's image (`container/pack-stack.sh`, which needs a full clone and
Docker), uploads with `main <commit>` as the version's message, reads the Deployer's image, and
reads the Worker's log for three minutes after the upload. `--check --from <ISO time>` runs that
last step alone, read-only.

It uploads the approved commit over the approved version, and nothing else. It takes the lock file
itself and holds it to its end (`flock` on a file that the process keeps open, so do not wrap it
in `flock` on the same file). It reads the tree and the live version before the build, and again
after the build, right before the upload. It refuses when:

- `--commit`, `--expect-live` or `--lock` is missing;
- the tree is not at the approved commit, before the build or after it;
- the Worker does not serve one version at 100% (a split);
- the live version is not the approved one (somebody deployed, or changed a secret, since the plan);
- the live code names no commit of this clone, or one that is not behind the commit to deploy;
- the Worker serves this commit and the Deployer's image is not known to be of it (a partial
  rollout, below), unless the run has `--repair-image`.

The first deploy over a version that this script did not upload, and a deploy that goes back to an
older commit, are therefore not this script's: they need a person.

The exit status says whether production may have changed:

| Exit | Meaning                                                                                                                                                                                                                                                                                                                                                             |
| ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 0    | Live and verified by the log. Or the commit was live already at 100%, the Deployer's image too: nothing built, nothing uploaded. Or a plan.                                                                                                                                                                                                                         |
| 1    | Uploaded and live, and the log shows an error line, a thrown invocation or no invocation. It prints the `wrangler rollback` line for the approved version.                                                                                                                                                                                                          |
| 2    | Nothing was uploaded: a refusal (a partial rollout with no `--repair-image` is one), or a step before the upload failed.                                                                                                                                                                                                                                            |
| 3    | The upload was attempted and the live state is not verified: the upload failed part-way, a read after it failed (of the deployments, the image or the log), the deployments are not what this upload should leave, or the Deployer's image is not of this commit. Its last lines say what a read after the failure found. Read the deployments before any new plan. |

### A partial rollout

One `wrangler deploy` makes two changes, one after the other: it uploads the Worker's version,
then it pushes the Deployer's image and rolls the container application out to it. When the
second fails, the Worker serves the new commit and the Deployer still runs the image of an older
one. On 27 Nov 2026 a registry timeout during the push left production so, and a second run for
the same commit would have answered "already live".

The script reads the container application, its rollouts, and the registry (no change at
Cloudflare; for the registry it asks for a credential that may pull alone and ends after 15
minutes). The application names its image by digest alone. `wrangler deploy` pushes each image
under the tag of the Worker version that it uploaded first, so an image is of a commit when the
registry holds its digest under the tag of an upload of that commit (`imageOf`). No time and no
order of events stands in for that. An image whose commit is not known (a rollout that has not
ended, or a digest under the tag of none of the uploads that Cloudflare lists) is never called in
step.

- **In the run that fails:** exit 3. After `READ AFTER THE FAILURE:` it says which it is: the
  approved version still serves (nothing went live), `PARTIAL ROLLOUT`, or not known. It retries
  nothing and rolls nothing back.
- **In a later run or plan for the same commit:** `--plan` prints `PARTIAL ROLLOUT` and the
  repair's flags. A deploy without `--repair-image` refuses with exit 2.
- **The repair:** the same command with `--repair-image`, on its own approval of exactly the
  printed `--commit` and `--expect-live`. It is the same guarded deploy (lock, tree, live version
  before and after the build, image, log), of the commit that the Worker serves already. It
  uploads the Worker's code once more as a new version of that commit, because that is the one
  tested upload path; the image is then pushed and rolled out. It refuses when the live commit is
  not this commit, and it uploads nothing when the image is of this commit already.
- **An image whose commit is not known names no repair.** The run refuses with exit 2, with
  `--repair-image` too: the repair is verified by the same read, and a read that does not know
  the image before the upload may not know it after. When a rollout has not ended, plan again
  after it. Else a person reads the application and the registry, and decides.
- After a good upload the script waits for the image, and then reads the live version once
  more: a deployment by another hand during the wait is exit 3.
- A deploy of a newer commit needs no repair first: it pushes and rolls out its own image.
- A Worker that was rolled back keeps the newer image (`wrangler rollback` moves no image). The
  plan says that the image is not of the live commit; the repair covers that case only when the
  tree is at the live commit.

What it does not stop: Cloudflare has no upload that names the version it replaces. A deployment
by another hand in the seconds between the last read and the upload is replaced all the same. The
script sees it only after the fact, as exit 3: the deployment right before the new one is then not
the approved version. The rollback line names the approved version, which is the replaced one only
when no such deployment landed.

Never call `wrangler deploy --env prd` by hand: it skips all of that, and nothing at Cloudflare
stops it. On 23 Nov 2026 a deploy by hand from an unmerged branch, four days behind main, released
six merged pull requests unseen; one of them stopped every delivery to iterate.com for three
hours.

A change to what one Durable Object of this Worker sends another, or to a stored row, goes out in
two deploys ([the rule](../../rules/structure/calls-between-objects-survive-a-rollout.md)): old
and new objects run side by side for some seconds of every deploy.

The prd fault alarm (`apps/prd-fault-alarm` of the iterate mind's `/repos/config`) reads this Worker's log every 15 minutes
and pages #error-pulse on an error or a thrown invocation.

## Layout

`src/` is the pages (Vite), `worker/` the Cloud's Worker, `container/` the run, and `pretend/` a
stand-in for Cloudflare, for work on the pages (`wrangler.pretend.jsonc`).