iterate.com / kernel

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

kernel / core / alchemy / README.mdmain at 949894f

# core/alchemy

One Alchemy script that deploys one mind into its owner's own Cloudflare account. Read
`src/stack.ts` from top to bottom: it declares every resource, and its header says what each
Worker is. `alchemy.run.ts` is the entry that Alchemy's CLI runs. This file says only what the
script does not.

[`iterate deploy`](../lib/src/cli/README.md#deploy-a-mind) is the command for a person: it signs in
to Cloudflare, writes the one input below and runs this script. `iterate recover` gives an owner
with no passkey left a way back in. What follows is the deploy by hand.

## What it makes

- Five Workers: telemetry, auth, secrets, mind and boundary. Only the boundary has routes. With
  the topology `external-mind-connects-into-cloudflare-boundary`, the stack has no mind Worker
  and nothing that only a mind Worker binds.
- `src/stack-bindings.snapshot.json` lists each Worker's bindings, by name and kind and never by
  value, as `src/stack.ts` declares them for three fixed configs. A pull request that adds,
  removes or renames a binding must change that file, so the change shows in its diff:
  `src/stack-bindings.test.ts` fails until it does. If the change is meant, rewrite the file with
  `UPDATE_STACK_BINDINGS=1 pnpm --filter core-alchemy test`.
- Every resource of the account is named `iterate-<alchemy.stageName>-…` (`namesOf` in
  `src/config.ts`), but the Access application: it is named by the control plane's hostname,
  because Cloudflare's code page shows its name to the owner ("Log in to iterate.example.com").
- The stack's state, with the generated keys, the telemetry API token's value and the secrets of
  the SFU app and the TURN key, is in the Worker `iterate-<alchemy.stageName>-state` of the same account.
- No OAuth client. A member connects the mind to its own account through Cloudflare's MCP
  server, for which the mind is its own client (core/configs/default/cloudflare.ts). A stack
  deployed before 19 Nov 2026 has a client `iterate-<alchemy.stageName>-cloudflare`: its row
  leaves the state at the next deploy with no call (`src/retired-cloudflare-oauth-client.ts`),
  and the client stays in the account until a member deletes it (Manage Account, OAuth
  clients).
- One Realtime SFU app, `iterate-<alchemy.stageName>-realtime-sfu`, and one TURN key,
  `iterate-<alchemy.stageName>-turn`, by Alchemy's own resources. The secrets Worker alone holds
  their secrets, and serves them as `/secrets/cloudflare-realtime-sfu` and
  `/secrets/cloudflare-turn`. They cost nothing until a call uses them.
- An egress gateway, only when the input names one (`cloudflare.egressGatewayWorker`): a Worker
  of the account that the stack does not make. The vault and the boundary get it as
  `EGRESS_GATEWAY` and send through it what a mind's `itx.fetch` sends out of the stack, each
  request with its secret in. The field's doc in `src/config.ts` lists what does not go through
  it. The boundary alone gets the gateway's entrypoint `EgressGatewayProviders` as
  `EGRESS_GATEWAY_PROVIDERS`. Give each stack a gateway Worker of its own: one gateway serves
  one stack. An input that names none binds nothing, and Cloudflare's own fetch sends.
  - **Never roll the vault or the boundary back to a Worker version from before the deploy that
    named the gateway.** A version holds a Worker's code, bindings and variables together, so
    that version has no gateway: its requests leave by Cloudflare's own fetch, the vault's with
    the secret in. The way back from a bad gateway is `--egress-gateway none` or a new deploy,
    never a version id.
  - **Never edit the vault's or the boundary's bindings by hand** (the dashboard, or an upload
    that keeps the variables). A version that is written without the gateway's binding sends by
    Cloudflare's own fetch, as the version from before the gateway does.
  - A gateway that throws, that answers an error, or that was deleted fails the sends. Nothing
    then leaves another way.
  - A plan or a deploy that names a Worker which does not exist fails, and changes nothing:
    Alchemy looks the Worker up by its name. A destroy does not look it up: it passes when the
    gateway Worker is gone. Nothing checks that the Worker has the entrypoint
    `EgressGatewayProviders`: without it the deploy passes, and a dial of `/api/egress-gateway`
    is a 500.
- Our own providers beside the script handle what Alchemy does not do yet (read in 2.0.0-beta.85
  and beta.87, the version that `pnpm-workspace.yaml` pins), along with thin wrappers of Alchemy's
  providers (`src/realtime.ts`, `src/telemetry-token.ts`). The header of each file says what, and
  when the file can go.

## The one input

A JSON file that `ITERATE_MIND_CONFIG` names, parsed by `MindConfig` in `src/config.ts`, where
each field has its doc. No environment variable changes what it means. The boundary Worker keeps
the whole input as its variable `DEPLOY_CONFIG`, which no Worker reads: `iterate deploy` over a
mind starts from it, so a field that no Worker holds, such as `cloudflare.containerImage`, stays.

```json
{
  "mindId": "<the mind's id: 64 hex characters>",
  "controlPlaneHostname": "iterate.example.com",
  "ownerEmails": ["ada@example.org"],
  "topology": "full-cloudflare",
  "alchemy": { "stageName": "example" },
  "cloudflare": { "accountId": "<the account's id>", "zones": ["example.com"] }
}
```

A mind takes each of its zones whole: nothing else may use them.

`"mcpTarget": "mcp"` points the mind's own MCP server (`iterate.<zone>/mcp`) at a method of the
config worker's class: that method answers a class which gives the server its instructions, its
description of `run`, more tools and the mind's skills (`mcp.ts` of the default config). Absent,
the platform's defaults answer; and they answer too whenever the method fails, so the field
cannot break the server. `iterate deploy --mcp-target mcp` sets it, and `--mcp-target none` takes
it away. A stack built before this field refuses it.

`"ownerEmailPatterns": ["*@company.example"]` names more owners by pattern: every address at
exactly that domain (no sub-domain) is an owner, with all that an owner may do. Such a person
proves the address through Cloudflare Access, whose policy then names the whole domain, and adds a
passkey. So whoever holds a mailbox at the domain, or runs its mail, can become an owner: name a
domain only when its mail is the company's own, and never a zone of the mind. Each person who
proves an address takes a seat of the account's Zero Trust team. A person who left keeps the
passkey that they added, whatever their mail provider does: an owner blocks that address on
`/members`, which ends that one person (no member, no session, no way back in) while the block is
there. `iterate deploy --owner-email-patterns none` takes the pattern away for everyone. A mail or a Slack message from a verified address
at the domain counts as a member's. Absent: the owners are `ownerEmails` alone.

Every Worker of the stack keeps every log line and, by default, every trace in Cloudflare's own
Workers Logs. `"telemetry": { "traceSampling": 0.1 }` keeps the trace of one request in ten (from 0
to 1; Cloudflare's head sampling rate). `iterate deploy --trace-sampling 0.1` writes it, and
`--trace-sampling default` goes back to every trace. On iterate.com on 21 Nov 2026, spans were
about 86 % of what the store took in.

Each mail to an address at a zone reaches the mind: the boundary has the mind store its files
under `/email/` and append `events.iterate.com/email/received` on `/integrations/email`. A failure
there fails the delivery, so Cloudflare delivers again. No code of the mind's config runs in the
delivery: the config gets the mail as that event, in `processEvent`. To send the original on, as
it arrived, the config appends `events.iterate.com/email/forward-requested` (`{ raw, to }`) there.
The boundary's handler stays open for such requests for 10 s after the receipt, and forwards
with Email Routing's own forward: its From is the sender's, so a reply goes to the sender.

- The address must be a verified destination address of the account's Email Routing: add it in
  the dashboard (Email Routing, Destination addresses) and confirm the link Cloudflare mails to it.
- Cloudflare forwards only a mail whose sender is proven. A forward that does not happen is an
  `events.iterate.com/email/forward-failed` fact with why: Cloudflare's words, or the boundary's
  own, which start "not forwarded:" (a mail never goes to an address that its `X-Forwarded-To`
  names, so an address that leads back makes no loop; a request that no code at the root wrote).
- Each forward is an `events.iterate.com/email/forwarded` fact, one for each mail and address.
  Cloudflare answered a forward after 3 to 10 s on 19 Nov 2026. A forward whose answer does not
  come within 20 s after the requests' time is the same fact with `answered: false`.
- When the 10 s are up, the mind appends `events.iterate.com/email/forward-requests-closed` for
  the mail. A request on the log before that fact is served; one after it gets no answer: the
  message is gone. Nothing
  that the config does or fails to do fails a delivery.

**The older way, which goes:** with `"mail": { "forwardTo": "team@example.org" }` the boundary
forwards each mail there itself, before the mind's requests and through the same forward, so a
request for that address forwards nothing more. A refused forward fails the delivery after the
mail is on the log. The config refuses an address at a zone of the
mind: its mail would come back. Each forward that fails is an
`events.iterate.com/email/forward-failed` fact, with Cloudflare's words; a mail whose sender is not
proven stays in the mind alone, and the delivery stands. **The cost of a forward address that does
not work:** while the address is not verified, is removed from the account, or refuses mail, every
mail to the mind fails for its sender: the mind keeps the mail, the delivery fails, the sender gets
it back. A mind's config asks for its forwards by event instead.

`mindId` is the SHA-256 of the mind's signed genesis record (`iterate/mind-identity`). The stack
signs and checks nothing with it: every Worker gets it as config, and the auth Worker accepts the
genesis that hashes to it at `POST https://<controlPlaneHostname>/.well-known/iterate`. Until a
record lists a working key of the stack, the mind signs nothing as itself; all else works.

## Deploy and destroy

From `core/alchemy`, with the config in `mind.config.local.json` (git ignores that name):

```sh
export ITERATE_MIND_CONFIG=mind.config.local.json
pnpm exec alchemy login                        # or CLOUDFLARE_API_TOKEN, a token of the config's account
pnpm run deploy --stage <alchemy.stageName>
pnpm run destroy --stage <alchemy.stageName>   # --yes with nobody at the terminal
```

- `deploy` is the mind Worker's build (`core/mind-worker/scripts/build.ts`: the SDK and the
  templates that the Worker carries), then `alchemy deploy alchemy.run.ts`. No step comes before
  or after. Alchemy's other commands and flags work the same way:
  `pnpm exec alchemy plan alchemy.run.ts --stage <alchemy.stageName>`.
- A command for another stage or another account than the config's is refused before it opens
  the state. This holds for a deploy and for a destroy.
- What a sign-in or an API token must hold for a deploy is one list with a reason for each entry:
  `STACK_SCOPES` in [cloudflare-access.ts](../lib/src/cli/cloudflare-access.ts). A sign-in's
  scope is an API token's permission under another spelling (`workers-scripts.write` is "Workers
  Scripts Write").
- Telemetry tables (`events`, `logs`, `spans`, and `metrics` SQL) have three modes, read from the
  stack's state (`src/telemetry-token.ts`, `telemetryModeOf`). A deploy takes no telemetry token.
  - **`minted`:** the state holds `TelemetryCatalogToken` or `TelemetryAnalyticsToken`: the stack
    declares the catalog token's `AccountApiToken`, the catalog, sinks and pipelines, so its sinks
    keep their credentials. The analytics token is no longer declared, and the next deploy deletes
    it: `metrics` is read on the Analytics SQL binding, with no token. When Cloudflare refuses that
    delete, the deploy fails and the token stays in state: its log line names the token and the
    dashboard page to delete it on, and the next deploy deletes it again.
  - **`given`:** the state holds `TelemetryToken`, an owner-created token: the stack keeps using
    it. **Do not delete or roll it:** the sinks keep the token they were made with.
  - **`off`:** neither, which is every stack that a deploy makes now. The stack has no `events`,
    `logs` or `spans` table, so it makes nothing that only fills them: no telemetry bucket, no
    Pipelines stream, sink or pipeline, no OTLP destination and no token. Its mind reads
    Cloudflare's own datasets and `metrics` through `itx.telemetry.query`, with no token. A
    stack that an earlier deploy gave a bucket, three streams and two destinations loses them at
    its next deploy, with what a mind made on them with a member's token
    (`src/mind-made-telemetry-tables.ts`).
- The deploy makes a Realtime SFU app and a TURN key (`src/realtime.ts`), which need the
  permission to make Realtime (Calls) resources: "Calls Write" (`calls.write`), which the deploy
  asks for, or "Realtime" or "Realtime Admin". A credential without it is no failure either: the deploy makes neither,
  says so in one line, and the secrets Worker answers `/secrets/cloudflare-realtime-sfu` and
  `/secrets/cloudflare-turn` as not set, with that reason. Each later deploy asks again.
- Every zone must be in the account already. Alchemy does not take over a resource that exists,
  such as a DNS record of the zone: remove it, or pass `--adopt`.

Two refusals of a first deploy are known:

- **The new state store.** A first deploy makes the state store, and a new store can refuse
  requests in its first seconds ("State store request failed"), also in the middle of the
  apply. It did so for every new store in the proofs of 18 Nov 2026. Nothing is made without
  state: run the deploy again.
- **Another stack's records in the store.** The live minds' earlier stack used the same store's
  name. A deploy over such a stack's records, not moved by hand, stops in its plan with
  "OwnedBySomeoneElse" for the zone's records and routes, and changes nothing. Destroy that
  stack, or move it with its runbook; never pass `--adopt` to get past it: the old records
  would then delete what the new ones took over. The first deploy over a Worker record of the
  earlier stack ends with Alchemy's "Reconciling worker routes (0)": it deletes every route of
  the boundary in the zone, also the two that the Route resources adopted in the same run: the
  mind answered 522 for 54 s on one move and for 2 s on the other (18 and 19 Nov 2026). After that
  deploy the record holds no route, and later deploys delete none.

## What a destroy leaves

A destroy erases the mind: its contexts, files, repos, members and secrets. It leaves:

- The state store: the Worker `iterate-<alchemy.stageName>-state`, and its two secrets
  in the account's Secrets Store, which are named after the Worker. A Secrets Store holds a
  limited number.
- The TXT record `_dmarc.<zone>`, which Cloudflare's Email Sending wrote.
- R2's record of the telemetry bucket's data catalog, as inactive. No API removes it.
- The OAuth client of a stack deployed before 19 Nov 2026: a member deletes it in the
  dashboard.
- The SFU app and the TURN key, when the destroy's credential lacks the permission to delete Realtime (Calls)
  resources: the destroy
  says so and goes on.
- A sandbox's saved snapshots: tags in the registry repository
  `<account>/cloudchamber-snapshots/<id>`, which may also hold other applications' snapshots.
  Cloudflare's container registry refuses their delete, and Cloudflare keeps them after the
  container application is deleted (measured 20 Nov 2026). The destroy names each repository with
  the number of this mind's tags and goes on. Each tag holds a copy of a sandbox's files
  (`src/container-snapshots.ts`).
- The account's Zero Trust organization and its one-time PIN login. They belong to the account,
  and other things in it may use them (`src/access-team.ts`).
- The zones.

A deploy of the same stage one second after its destroy can fail with "This Worker does not
exist": run it again.