iterate.com / cloud

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

cloud / core / lib / src / cli / deploy.tsmain at aacbc74

// deploy.ts — `iterate deploy`: A MIND IN A PERSON'S OWN CLOUDFLARE ACCOUNT, by one program that
// asks as little as it can and waits for nothing it does not need. No model and no server of
// iterate's is in between:
//
//   1. access to Cloudflare (./cloudflare-access.ts), and the account: one the person has, or a
//      new one;
//   2. the domain: a zone of the account, a new one that Cloudflare's registrar sells, or one the
//      person brings;
//   3. what the account must have: a workers.dev name, Workers Paid and R2;
//   4. the script's one input (core/alchemy/src/config.ts), written here: for a new mind from the
//      answers, for a mind that is there already from that mind's own config (`deployedMind`),
//      for an earlier deploy whose config is here, from that config (`earlierDeployOf`);
//      and for a new mind its identity (./identity.ts): one root key and the genesis that it
//      signed, in two files on this computer, before anything is deployed;
//   5. the stack's source (core/alchemy and the Workers), fetched and installed in the background
//      while the person signs in, and the stack's own deploy, `pnpm run deploy` of core/alchemy;
//   6. the post of the mind's identity records to the mind, when this computer holds them;
//   7. opening https://onboarding.<zone>/ in the browser to continue setup.
//
// It handles the expected way alone. A run that stops says why, changes nothing more, and can be
// run again on the same computer: every step reads the account's state first, and a deploy keeps
// what it made. Each choice has a flag, so a coding agent can run and supervise it with nobody at
// the terminal.
import { execFile, spawn, spawnSync, type ChildProcess } from "node:child_process";
import {
  appendFileSync,
  createWriteStream,
  existsSync,
  mkdirSync,
  readFileSync,
  rmSync,
  writeFileSync,
} from "node:fs";
import { homedir } from "node:os";
import { join, resolve } from "node:path";
import { setTimeout as sleep } from "node:timers/promises";
import { isDeepStrictEqual, promisify, styleText } from "node:util";
import * as p from "@clack/prompts";
import { z } from "zod";
import { mindAddressAsTyped, mindApiUrlOf, mindMcpNameOf, mindMcpUrlOf } from "../mind-api.ts";
import {
  domainOfOwnerEmailPattern,
  Hostname,
  MindConfig,
  OwnerEmailPattern,
  zoneCoversControlPlaneHostname,
  zoneOf as zoneOfMind,
  zoneOfMailAddress,
} from "../mind-config.ts";
import { discoveredMind, signInToMind } from "../node.ts";
import { isCodingAgent } from "./coding-agent.ts";
import {
  type Answer,
  BILLING_SCOPE,
  briefAccess,
  CARD_SCOPE,
  type Cloudflare,
  cloudflare,
  cloudflareAccess,
  DEPLOY_SCOPES,
  dropKeptAccess,
  endAccess,
  SIGN_IN_ENDED_EXIT,
  SIGN_IN_FILE,
  SignInEnded,
  parsed,
  resultOf,
} from "./cloudflare-access.ts";
import {
  DEFAULT_CONFIG_NAME,
  readConfigFile,
  readSession,
  setConfigMind,
  withSessionsLock,
  writeSession,
} from "./config.ts";
import { loginFlowFor } from "./device-login/flow.ts";
import {
  IDENTITIES,
  identityMadeFor,
  keyFileOf,
  mlDsaRequired,
  newIdentity,
  postIdentity,
} from "./identity.ts";
import {
  addMcpServer,
  MCP_CLIENTS,
  type McpClient,
  mcpClientsOf,
  mcpClientsOnPath,
} from "./mcp-clients.ts";
import { refreshOAuthSession } from "./oauth.ts";
import { openPageCommand } from "./open-page.ts";
import { protocolChangeLine, setZoneProtocols, zoneProtocolChanges } from "./zone-protocols.ts";

const exec = promisify(execFile);
const CACHE = join(process.env.XDG_CACHE_HOME || join(homedir(), ".cache"), "iterate");

export type DeployInput = {
  domain?: string;
  account?: string;
  newAccount?: boolean;
  newSignIn?: boolean;
  owner?: string;
  /** more owners by pattern, `*@<domain>` with commas between; `none` takes them away
   *  (`deployConfigOf`) */
  ownerEmailPatterns?: string;
  /** the method of the config's class that answers the mind's part of its own MCP server;
   *  `none` takes it away (`deployConfigOf`) */
  mcpTarget?: string;
  controlPlaneHostname?: string;
  egressGateway?: string;
  /** where each mail is forwarded as it arrived; `none` stops the forward (`deployConfigOf`) */
  mailForwardTo?: string;
  /** the share of requests whose trace is kept, 0 to 1; `default` is every one (`deployConfigOf`) */
  traceSampling?: string;
  /** a new mind's kernel repo: the address it is cloned from, and what this deploy builds
   *  (`kernelPlanOf`) */
  kernelRepo?: string;
  /** a new mind's config repo: the address it is cloned from; `none` starts the mind's repos
   *  empty (`deployConfigOf`) */
  configRepo?: string;
  /** the mind's whole list of zones, with commas between (`deployConfigOf`) */
  zones?: string;
  yes?: boolean;
  buy?: boolean;
  eraseRecords?: boolean;
  source?: string;
  ref?: string;
  setup?: boolean;
  /** false: this computer is not signed in to the mind after the deploy (`signInHere`) */
  signIn?: boolean;
  /** the coding agents that get the mind's MCP server: names, `all` or `none` (`addMcpHere`) */
  mcp?: string;
  /** the stack's plan alone, over a mind that is there: nothing changes (`planArgsOf`) */
  plan?: boolean;
};

// ---------------------------------------------------------------------------------------------
// What the program decides with no question. Pure, so that a test holds them.

/** THE STAGE OF A MIND, which is the middle of every name the stack gives a resource
 *  (`iterate-<stage>-auth`): inferred from the mind's first zone and never asked. The zone with
 *  `-` for each dot, cut to the 32 characters that the script takes (core/alchemy/src/config.ts),
 *  and with `z` first for a zone that starts with a digit, since a stage starts with a letter. */
export function stageNameOf(zone: string) {
  const name = zone.replaceAll(".", "-");
  return (/^[a-z]/.test(name) ? name : `z${name}`).slice(0, 32).replace(/-+$/, "");
}

/** A deployed mind's config as its Workers hold it. `mindId` may be absent: the Workers of a
 *  mind that was deployed before a mind had an id hold none. Every other rule of `MindConfig`
 *  holds, the control-plane hostname under one of the zones among them: `iterate recover`
 *  prints a sign-in link at that hostname, so a config that names a hostname outside the mind
 *  is no config of a mind. */
const DeployedConfig = z
  .object({ ...MindConfig.shape, mindId: MindConfig.shape.mindId.optional() })
  .refine(zoneCoversControlPlaneHostname, {
    path: ["controlPlaneHostname"],
    message:
      "the apex of one of zones, or one label under one: a zone's certificate covers no deeper name",
  });

/** THE WHOLE CONFIG OF THE DEPLOY THAT A MIND RUNS, as the stack keeps it in its boundary
 *  Worker's plain-text variable `DEPLOY_CONFIG` (core/alchemy/src/config.ts `MindConfig`). It holds
 *  what no Worker reads, such as `cloudflare.containerImage`, the sandboxes' image. Each field is
 *  kept as it is, those that this command does not know too: a deploy over the mind starts from
 *  it, and a field that it lost would plan as a change. */
const KeptDeployConfig = z.looseObject({
  alchemy: z.looseObject({ stageName: z.string() }),
  cloudflare: z.looseObject({ accountId: z.string(), zones: z.array(z.string()) }),
});

/** A mind that is deployed: its stage, its own config, and its deploy's whole config, which a
 *  mind deployed before the stack kept it lacks. */
type DeployedMind = z.infer<typeof DeployedConfig> & {
  stageName: string;
  deployConfig?: z.infer<typeof KeptDeployConfig>;
};

/** THE SCRIPT'S ONE INPUT (core/alchemy/src/config.ts `MindConfig`) BUT FOR A NEW MIND'S ID, or
 *  an error that says which answer it refuses. A new mind takes the zone `domain` whole, has
 *  `owner` as its one owner, and answers at `iterate.<domain>` unless `controlPlaneHostname`
 *  names the zone itself or another name one label under it. A mind that is `deployed` keeps its
 *  zones, its topology, its owners and its hostname: `owner` and `controlPlaneHostname` are then
 *  what a flag said, and each replaces what the mind has. So does `mailForwardTo`, the config's
 *  `mail.forwardTo`, and `none` there removes it: with no flag the mind keeps its forward. So does
 *  `traceSampling`, the config's `telemetry.traceSampling` (a number from 0 to 1), and `default`
 *  there removes it, so that every trace is kept. So does `ownerEmailPatterns`, the config's
 *  field of that name (`*@<domain>`, commas or spaces between), and `none` there removes it. So
 *  does `mcpTarget`, the config's field of that name (a method's name), and `none` there
 *  removes it.
 *
 *  `zones` is the mind's WHOLE list of zones (commas or spaces between), and replaces what the
 *  mind has: the stack then makes each new zone's records, routes and mail, and deletes those
 *  of a zone that left. It holds `domain`, and its first zone stays the first of a mind that is
 *  there: the auth Worker's self-test sends from it.
 *
 *  `mindId` is the deployed mind's own, and nothing here ever gives a mind that has an id
 *  another. A mind with none gets the hash of the genesis that the command signs right before
 *  the deploy (./identity.ts `newIdentity`), which names the hostname that this answers. */
/** THE TWO REPOS THAT A DEPLOY NAMES FOR A NEW MIND, each with the address it is cloned from at
 *  the mind's birth: the kernel's (`--kernel-repo`) and the one that holds the mind's code
 *  (`--config-repo`). Every other repo is named by the config repo's own package.json, and the
 *  mind clones those from beside `--config-repo` (core/mind-worker src/mind/tree.ts
 *  `originBeside`). No address is used after the birth: from then on the repos are the mind's own.
 *
 *  The command gives both a default (`birthReposOf`), so this is told an address or `none`.
 *
 *  A repo with no address is made empty and gets its code by a push. `--config-repo none` says so
 *  for the config repo, and a kernel that is deployed from a checkout (`--source`) has none. That
 *  is how the mind that hosts iterate's own source began, since it had nowhere to clone from. */
function startingRepos(from: {
  kernelRepo: string | undefined;
  configRepo: string | undefined;
}) {
  if (!from.configRepo)
    throw new Error(
      "A new mind needs --config-repo: the address of the repo that its own config starts as (https://git.<a mind's domain>/repos/config-template), or none for a mind whose repos start empty.",
    );
  const origin = (flag: string, address: string | undefined) => {
    if (!address || address === "none") return null;
    if (!address.startsWith("https://") || !z.url().safeParse(address).success)
      throw new Error(
        `${flag} ${address} is no address: give the https address of a git repo, such as https://git.<a mind's domain>/repos/<name>.`,
      );
    return address.replace(/\/$/, "");
  };
  return [
    { path: "/repos/kernel", origin: origin("--kernel-repo", from.kernelRepo) },
    { path: "/repos/config", origin: origin("--config-repo", from.configRepo) },
  ];
}
const STARTING_ENTRY = { repo: "/repos/config", module: "worker.ts" };

/** WHERE A NEW MIND'S REPOS COME FROM WHEN NO FLAG SAYS: iterate's own mind, which serves the
 *  kernel and the config that every mind starts with to anyone. So `iterate deploy` with no flag
 *  makes a mind. */
export const NEW_MIND_REPOS = {
  kernel: "https://git.iterate.com/repos/kernel",
  config: "https://git.iterate.com/repos/config-template",
} as const;

/** The two addresses of a birth: what each flag names, else `NEW_MIND_REPOS`. A kernel that is
 *  deployed from a checkout (`--source`) has no address unless `--kernel-repo` gives one. */
export function birthReposOf(input: { source?: string; kernelRepo?: string; configRepo?: string }) {
  return {
    kernelRepo: input.kernelRepo ?? (input.source ? undefined : NEW_MIND_REPOS.kernel),
    configRepo: input.configRepo ?? NEW_MIND_REPOS.config,
  };
}

/** THE REPOS OF A BIRTH THAT A NEW MIND COULD NOT CLONE, each as its address and what git said:
 *  the two that the deploy names, and each repo that the config repo's package.json names, from
 *  beside the config repo (core/mind-worker src/mind/tree.ts `reposNamedIn`, `originBeside`). A
 *  mind clones with no credential, so this asks with none, and never at the terminal. Empty when
 *  the birth can clone every one. `dir` keeps the config repo's one commit. */
export async function birthReposRefused(named: unknown, dir: string): Promise<string[]> {
  // a config's repo list is read as it comes, as `kernelAddressIn` reads it
  const repos =
    z.array(z.object({ path: z.string(), origin: z.string().nullable() })).safeParse(named).data ||
    [];
  const git = (cwd: string, ...args: string[]) =>
    exec("git", ["-c", "credential.helper=", ...args], {
      cwd,
      env: { ...process.env, GIT_TERMINAL_PROMPT: "0", GIT_ASKPASS: "" },
    });
  const said = (error: unknown) =>
    String((error as { stderr?: unknown }).stderr || error)
      .trim()
      .split("\n")
      .pop();
  const refused: string[] = [];
  const asked = async (origin: string) => {
    try {
      await git(process.cwd(), "ls-remote", "--exit-code", origin, "refs/heads/main");
    } catch (error) {
      refused.push(`${origin} (${said(error) || "it has no main"})`);
    }
  };
  const entry = repos.find(({ path }) => path === STARTING_ENTRY.repo)?.origin;
  await Promise.all(
    repos.flatMap(({ path, origin }) =>
      origin && path !== STARTING_ENTRY.repo ? [asked(origin)] : [],
    ),
  );
  if (!entry) return refused;
  let manifest: unknown;
  try {
    mkdirSync(dir, { recursive: true });
    await git(dir, "init", "-q");
    await git(dir, "fetch", "-q", "--depth", "1", entry, "main");
    manifest = JSON.parse((await git(dir, "show", "FETCH_HEAD:package.json")).stdout);
  } catch (error) {
    return [...refused, `${entry} (${said(error)})`];
  }
  const lines = z
    .object({ dependencies: z.record(z.string(), z.string()) })
    .safeParse(manifest).data?.dependencies;
  await Promise.all(
    Object.entries(lines || {})
      .filter(
        ([name, from]) =>
          /^@repos\/[a-z0-9][a-z0-9-]*$/.test(name) && /^(\.\.?\/|https:\/\/)/.test(from),
      )
      .map(([, from]) => asked(new URL(from, `${entry}/`).href.replace(/\/$/, ""))),
  );
  return refused;
}

export function deployConfigOf(answers: {
  accountId: string;
  domain: string;
  owner?: string;
  extraOwners?: readonly string[];
  ownerEmailPatterns?: string;
  mcpTarget?: string;
  controlPlaneHostname?: string;
  egressGateway?: string;
  mailForwardTo?: string;
  traceSampling?: string;
  kernelRepo?: string;
  configRepo?: string;
  zones?: string;
  deployed?: DeployedMind;
}) {
  const { deployed } = answers;
  // an empty flag is an error, never "the zones it has" by accident
  if (answers.zones === "")
    throw new Error("--zones is empty: give the mind's whole list of domains, commas between.");
  const listed = answers.zones
    ?.toLowerCase()
    .split(/[\s,]+/)
    .filter(Boolean);
  const zones = listed || deployed?.zones || [answers.domain];
  if (listed) {
    const first = deployed?.zones[0] || answers.domain;
    if (
      listed.some((zone) => !Hostname.safeParse(zone).success) ||
      new Set(listed).size !== listed.length ||
      !listed.includes(answers.domain) ||
      listed[0] !== first
    )
      throw new Error(
        `--zones ${answers.zones}: the mind's whole list of domains, each once, with ${first} first${first === answers.domain ? "" : ` and ${answers.domain} among them`}.`,
      );
  }
  const controlPlaneHostname = (
    answers.controlPlaneHostname ||
    deployed?.controlPlaneHostname ||
    `iterate.${answers.domain}`
  ).toLowerCase();
  if (
    !Hostname.safeParse(controlPlaneHostname).success ||
    !zoneCoversControlPlaneHostname({ controlPlaneHostname, zones })
  )
    throw new Error(
      `The control plane cannot answer at ${controlPlaneHostname}: its hostname is ${zones.join(" or ")}, or one label under it. Cloudflare's certificate for a zone covers no deeper name.`,
    );
  const ownerEmails = [
    ...new Set([
      ...(answers.owner ? [answers.owner.toLowerCase()] : deployed?.ownerEmails || []),
      ...(answers.extraOwners || []).map((email) => email.trim().toLowerCase()).filter(Boolean),
    ]),
  ];
  const atTheMind = ownerEmails.find((email) => zoneOfMailAddress({ zones }, email));
  if (atTheMind)
    throw new Error(
      `The owner's address ${atTheMind} is at a domain of the mind, and the mind takes that domain's mail. Pass --owner with an address elsewhere, or choose another domain.`,
    );
  if (!z.array(z.email()).min(1).safeParse(ownerEmails).success)
    throw new Error(
      `The owner is an email address, and ${ownerEmails.join(", ") || "none"} is not.`,
    );
  // an empty flag is an error, never "no patterns" by accident
  const patterns = answers.ownerEmailPatterns;
  if (patterns === "")
    throw new Error(
      "--owner-email-patterns is empty: give *@<domain>, or none to take the patterns away.",
    );
  const named = !patterns || patterns === "none" ? [] : [...new Set(patterns.split(/[\s,]+/))];
  if (named.some((pattern) => !OwnerEmailPattern.safeParse(pattern).success))
    throw new Error(
      `--owner-email-patterns ${patterns}: each pattern is *@<domain> in lower case, with one comma between two, or none to take the patterns away.`,
    );
  const patternAtTheMind = named.find((pattern) =>
    zoneOfMind({ zones }, domainOfOwnerEmailPattern(pattern)),
  );
  if (patternAtTheMind)
    throw new Error(
      `The owner email pattern ${patternAtTheMind} is at a domain of the mind, and the mind takes that domain's mail. Name a domain elsewhere.`,
    );
  const { mcpTarget } = answers;
  // an empty flag is an error, never "no target" by accident
  if (mcpTarget === "" || (mcpTarget && !/^[A-Za-z_$][\w$]{0,63}$/.test(mcpTarget)))
    throw new Error(
      `--mcp-target ${mcpTarget} is no method's name: give the name of a method of the config's class (the default config has mcp), or none to take it away.`,
    );
  // a mind that is there starts from its deploy's whole config: only what a flag names changes
  const kept = deployed?.deployConfig;
  const forwardTo = answers.mailForwardTo;
  if (forwardTo === "")
    throw new Error("--mail-forward-to is empty: give an address, or none to stop the forward.");
  if (
    forwardTo &&
    forwardTo !== "none" &&
    (!z.email().safeParse(forwardTo).success || zoneOfMailAddress({ zones }, forwardTo))
  )
    throw new Error(
      `The mail forward ${forwardTo} is no email address, or one at a domain of the mind, which takes that mail. Pass --mail-forward-to with an address elsewhere, or none.`,
    );
  const sampling = answers.traceSampling;
  const share = Number(sampling);
  // an empty flag is an error, never "back to the default" by accident
  if (
    sampling === "" ||
    (sampling && sampling !== "default" && !(sampling.trim() && share >= 0 && share <= 1))
  )
    throw new Error(
      `--trace-sampling ${sampling} is no share: give a number from 0 to 1 (1 keeps every trace), or default.`,
    );
  return {
    ...kept,
    mail: forwardTo === "none" ? undefined : forwardTo ? { forwardTo } : kept?.mail,
    // the trace sampling is what the flag says, else what the mind has; `default` keeps every trace
    ...(sampling && {
      telemetry: sampling === "default" ? undefined : { traceSampling: share },
    }),
    mindId: deployed?.mindId,
    controlPlaneHostname,
    ownerEmails,
    // A mind that is there keeps the repos it was deployed with: after birth they move by git.
    // One from before a deploy named any has the one repo that held its code.
    repos:
      kept?.repos ||
      (deployed
        ? [{ path: STARTING_ENTRY.repo, origin: null }]
        : startingRepos({ kernelRepo: answers.kernelRepo, configRepo: answers.configRepo })),
    entry: kept?.entry || STARTING_ENTRY,
    // the patterns are what the flag says, else what the mind has; `none` takes them away
    ...(patterns && { ownerEmailPatterns: named.length ? named : undefined }),
    // the MCP target is what the flag says, else what the mind has; `none` takes it away
    ...(mcpTarget && { mcpTarget: mcpTarget === "none" ? undefined : mcpTarget }),
    topology: deployed?.topology || ("full-cloudflare" as const),
    alchemy: { ...kept?.alchemy, stageName: deployed?.stageName || stageNameOf(answers.domain) },
    cloudflare: {
      ...kept?.cloudflare,
      accountId: answers.accountId,
      zones,
      // the egress gateway is what the flag says, else what the mind has; `none` takes it away
      ...(answers.egressGateway && {
        egressGatewayWorker: answers.egressGateway === "none" ? undefined : answers.egressGateway,
      }),
    },
  };
}

/** What Cloudflare answers for a Worker's settings: its bindings, a plain-text one with its text
 *  (https://developers.cloudflare.com/api/resources/workers/subresources/scripts/subresources/script_and_version_settings/methods/get/). */
const WorkerSettings = z.object({
  bindings: z.array(z.object({ type: z.string(), name: z.string(), text: z.string().optional() })),
});

/** A DEPLOYED MIND'S CONFIG OUT OF ITS BOUNDARY WORKER'S SETTINGS: the stack gives every Worker
 *  the same config as the plain-text variable `ITERATE` (iterate/mind-config), and Cloudflare
 *  lists a Worker's plain-text variables with their text. Undefined for settings that hold none. */
export function mindConfigIn(settings: unknown): Omit<DeployedMind, "stageName"> | undefined {
  const text = (name: string) =>
    WorkerSettings.safeParse(settings).data?.bindings.find(
      (binding) => binding.type === "plain_text" && binding.name === name,
    )?.text;
  const config = parsed(DeployedConfig, text("ITERATE") || "");
  const deployConfig = parsed(KeptDeployConfig, text("DEPLOY_CONFIG") || "");
  return config && { ...config, deployConfig };
}

/** WHAT THE BOUNDARY DOES NOT HOLD OF THE CONFIG THAT A DEPLOY GAVE IT, read from its settings
 *  after the deploy, or null when it holds all: the whole config as `DEPLOY_CONFIG`, which the
 *  next deploy starts from, and the mail forward as `MAIL_FORWARD_TO` exactly when the config
 *  names one. */
export function boundaryMisses(settings: unknown, deployed: Record<string, unknown>) {
  const text = (name: string) =>
    WorkerSettings.safeParse(settings).data?.bindings.find((binding) => binding.name === name)
      ?.text;
  const kept = parsed(z.unknown(), text("DEPLOY_CONFIG") || "");
  if (!isDeepStrictEqual(kept, JSON.parse(JSON.stringify(deployed))))
    return "its DEPLOY_CONFIG is not the config that this run deployed, so the next deploy would not start from the mind's own config";
  const forwardTo = MailForward.safeParse(deployed.mail).data?.forwardTo;
  if (text("MAIL_FORWARD_TO") !== forwardTo)
    return forwardTo
      ? `it has no MAIL_FORWARD_TO ${forwardTo}, so the mind keeps each mail and forwards none`
      : "it has a MAIL_FORWARD_TO that the config does not name, so the mind forwards its mail there";
  return null;
}
const MailForward = z.object({ forwardTo: z.string() });

/** The forward address that a config's `mail` names, or undefined. */
export const forwardOf = (mail: unknown) => MailForward.safeParse(mail).data?.forwardTo;

/** The longest wait for Cloudflare to show the boundary's new settings. A NEW WORKER'S SETTINGS
 *  LAG: for more than a minute after Alchemy made it, Cloudflare can answer "does not exist"
 *  (10007), or a 200 without the deployed config (most likely the empty script that Alchemy
 *  makes first). The slowest measured new Worker showed its config at 79 s; a Worker that exists
 *  shows its new settings at the first read. 3 min is more than twice 79 s. */
export const BOUNDARY_WAIT_MS = 3 * 60_000;

/** What each line the command says while it waits for the boundary holds, after the time. */
export const WAITING_FOR_BOUNDARY =
  "waiting for Cloudflare to show the boundary Worker's new settings";

/** One read of the boundary's settings as a line of the deploy's log: the status, Cloudflare's
 *  error, the bindings' names, and the keys at which DEPLOY_CONFIG differs. Never a value. */
export function boundaryRead(
  answer: { status: number; result?: unknown; error?: string },
  deployed: Record<string, unknown>,
) {
  if (answer.status !== 200) return `${answer.status} ${answer.error || ""}`.trim();
  const bindings = WorkerSettings.safeParse(answer.result).data?.bindings || [];
  const text = bindings.find((binding) => binding.name === "DEPLOY_CONFIG")?.text;
  const kept = parsed(z.record(z.string(), z.unknown()), text || "");
  const wanted: Record<string, unknown> = JSON.parse(JSON.stringify(deployed));
  const differs = kept
    ? [...new Set([...Object.keys(kept), ...Object.keys(wanted)])].filter(
        (key) => !isDeepStrictEqual(kept[key], wanted[key]),
      )
    : undefined;
  return `200 bindings ${bindings.map((binding) => binding.name).join(",") || "none"}; DEPLOY_CONFIG ${
    !kept ? "absent" : differs?.length ? `differs at ${differs.join(",")}` : "same"
  }`;
}

/** THE CHECK AFTER A DEPLOY: reads of the boundary's settings, 3 s apart, until one answers 200
 *  and holds all, for at most `BOUNDARY_WAIT_MS`. Each other read is "not yet": a new Worker's
 *  settings lag (`BOUNDARY_WAIT_MS`). It says a line when it begins to wait and each 30 s.
 *  `held`; else, at the bound, the last 200 read's miss or `unread` with the last status, and
 *  each read as a line of the deploy's log (`boundaryRead`). */
export async function boundaryCheck(
  read: () => Promise<{ status: number; result?: unknown; error?: string }>,
  deployed: Record<string, unknown>,
  { say = () => {}, pause = sleep, now = Date.now }: BoundaryWaitDeps = {},
): Promise<
  { held: true } | { miss: string; reads: string[] } | { unread: number; reads: string[] }
> {
  const began = now();
  const reads: string[] = [];
  let miss: string | null = null;
  let status = 0;
  let told = -1;
  for (;;) {
    const answer = await read();
    status = answer.status;
    if (status === 200) miss = boundaryMisses(answer.result, deployed);
    if (status === 200 && !miss) return { held: true };
    const waited = now() - began;
    reads.push(`${duration(waited)}: ${boundaryRead(answer, deployed)}`);
    if (waited >= BOUNDARY_WAIT_MS) return miss ? { miss, reads } : { unread: status, reads };
    if (Math.floor(waited / 30_000) > told) {
      told = Math.floor(waited / 30_000);
      say(
        told
          ? `${duration(waited)}: ${WAITING_FOR_BOUNDARY}`
          : `0 s: ${WAITING_FOR_BOUNDARY}, for at most ${duration(BOUNDARY_WAIT_MS)}`,
      );
    }
    await pause(3000);
  }
}

type BoundaryWaitDeps = {
  say?: (line: string) => void;
  pause?: (ms: number) => Promise<unknown>;
  now?: () => number;
};

/** How long one wait for the mind's first pages lasts (`firstPagesAnswer`). */
export const FIRST_PAGES_WAIT_MS = 10 * 60_000;

/** An address that a person's browser asks after a deploy, and the answer that says it works. */
type FirstPage = {
  address: string;
  works: (answer: { status: number; location: string | null }) => boolean;
};

/** THE PAGES THAT A PERSON'S BROWSER ASKS FIRST AFTER A DEPLOY. The command opens no page until
 *  each of them works from this computer, over HTTPS with no redirect followed:
 *   - `https://<controlPlaneHostname>/`, `/sign-in`, and `/.well-known/oauth-authorization-server`:
 *     proves `iterate.<domain>` (boundary + auth Worker + OAuth issuer metadata) is live;
 *   - the apex of the first zone answers below 400: the mind's own code (that request is also
 *     the one at which a new mind is born);
 *   - `https://onboarding.<zone>/` and `https://dash.<zone>/` (plus on a first deploy
 *     `https://dash.<zone>/.well-known/iterate-app` returning 200): proves the wildcard
 *     record, certificate, route, and config Worker apps are live. Node's fetch sends
 *     `sec-fetch-mode: cors`, so member apps answer 401 here (a browser redirects to sign-in);
 *     404 from the Dash says the app is not live yet. A mind's config may have no onboarding
 *     app (the config that a new mind starts with has none), so its 404 is an answer: the page
 *     proves the name and the route, and `setupHomeOf` says where setup goes on.
 *   - for a new mind, `/sign-in/access`, the one way in for an owner with no passkey yet
 *     (core/auth-worker/src/access-sign-in.ts): Cloudflare Access redirects it to the team's
 *     sign-in. Until the Access application is live at the edge, the auth Worker itself answers
 *     there, with a 403 that would end the first owner's sign-in. */
export function firstPagesOf(mind: {
  controlPlaneHostname: string;
  zone: string;
  firstDeploy: boolean;
}): FirstPage[] {
  const controlPlane = `https://${mind.controlPlaneHostname}`;
  const memberHostWorks = ({ status }: { status: number }) =>
    mind.firstDeploy ? status < 400 || status === 401 : status < 500;
  return [
    { address: `${controlPlane}/`, works: ({ status }) => status < 400 },
    { address: `${controlPlane}/sign-in`, works: ({ status }) => status === 200 },
    {
      address: `${controlPlane}/.well-known/oauth-authorization-server`,
      works: ({ status }) => status === 200,
    },
    { address: `https://${mind.zone}/`, works: ({ status }) => status < 400 },
    { address: onboardingUrlOf(mind.zone), works: ({ status }) => status < 500 },
    { address: `https://dash.${mind.zone}/`, works: memberHostWorks },
    ...(mind.firstDeploy
      ? [
          {
            address: `https://dash.${mind.zone}/.well-known/iterate-app`,
            works: ({ status }: { status: number }) => status === 200,
          },
          {
            address: `${controlPlane}/sign-in/access`,
            works: ({ status, location }: { status: number; location: string | null }) =>
              status >= 300 &&
              status < 400 &&
              // Cloudflare's own absolute address; any other text is no proof
              /^https:\/\/[a-z0-9-]+\.cloudflareaccess\.com\//.test(location || ""),
          },
        ]
      : []),
  ];
}

/** One read of a page: its status and where it redirects, or why no answer came (the name did
 *  not resolve, the certificate was refused, the edge did not connect, 10 s passed). */
async function readFirstPage(page: FirstPage) {
  try {
    const answer = await fetch(page.address, {
      signal: AbortSignal.timeout(10_000),
      redirect: "manual",
    });
    return { status: answer.status, location: answer.headers.get("location") };
  } catch (error) {
    const cause = error instanceof Error && error.cause instanceof Error ? error.cause : error;
    if (cause instanceof Error && "code" in cause && typeof cause.code === "string")
      return { error: cause.code };
    return { error: cause instanceof Error ? cause.message : String(cause) };
  }
}

/** THE WAIT FOR THE FIRST PAGES: every page is read each 3 s, and the mind answers when all of
 *  them worked five times in a row, since a new route reaches Cloudflare's edge within a minute
 *  or two and until then some answers are 522, 500 or 404. At most `FIRST_PAGES_WAIT_MS`; then
 *  `fault` says the first page that did not work at the last read, and its answer. It says the
 *  fault of the moment each 30 s. */
export async function firstPagesAnswer(
  pages: FirstPage[],
  {
    read = readFirstPage,
    say = () => {},
    pause = sleep,
    now = Date.now,
  }: BoundaryWaitDeps & {
    read?: (
      page: FirstPage,
    ) => Promise<{ status: number; location: string | null } | { error: string }>;
  } = {},
): Promise<{ answered: true } | { fault: string }> {
  const began = now();
  let good = 0;
  let told = 0;
  for (;;) {
    const answers = await Promise.all(pages.map((page) => read(page)));
    const fault = pages
      .map((page, index) => {
        const answer = answers[index]!;
        if ("error" in answer) return `${page.address} gave no answer (${answer.error})`;
        if (page.works(answer)) return undefined;
        return `${page.address} answered ${answer.status}${answer.location ? ` to ${new URL(answer.location, page.address).origin}` : ""}`;
      })
      .find(Boolean);
    good = fault ? 0 : good + 1;
    if (good >= 5) return { answered: true };
    const waited = now() - began;
    if (waited >= FIRST_PAGES_WAIT_MS)
      return { fault: fault || "its pages did not answer five times in a row" };
    if (Math.floor(waited / 30_000) > told) {
      told = Math.floor(waited / 30_000);
      if (fault) say(`${duration(waited)}: ${fault}`);
    }
    await pause(3000);
  }
}

/** WHETHER A PRODUCT OF THE ACCOUNT IS ON (Workers Paid, R2), by a read of one of its paths: 200
 *  is on, and Cloudflare's refusal (a 4xx but 429) is off. A READ THAT FAILED IS NOT A FACT: a
 *  429, a 5xx or anything else is read again, up to three reads with `pause` between them, and
 *  then the command stops and says so. `off` keeps Cloudflare's words, so the account's line can
 *  show them. */
export async function productRead(
  what: string,
  read: () => Promise<{ status: number; error: string }>,
  pause: () => Promise<unknown>,
): Promise<{ on: true } | { on: false; why: string }> {
  for (let attempt = 1; ; attempt++) {
    const answer = await read();
    if (answer.status === 200) return { on: true };
    const why = `${answer.status}: ${answer.error}`;
    if (answer.status >= 400 && answer.status < 500 && answer.status !== 429)
      return { on: false, why };
    if (attempt === 3)
      throw new Error(
        `Cloudflare did not answer whether ${what} is on (${why}), three times. A read that failed says nothing about the account. Nothing was changed. Run the command again.`,
      );
    await pause();
  }
}

/** Whether a line of a deploy's output says that the stack's state store refused a request, or
 *  that its workers.dev address answered 404. A store that the same run made does so while
 *  Cloudflare does not serve it yet, and nothing is made without state: the one failure after
 *  which the command waits for the store and deploys again by itself. */
export const saysTheStateStoreRefused = (line: string) =>
  /State store .*failed|StateStoreError|\(404 GET https:\/\/[^\s/]+-state\.[^\s/]+\.workers\.dev\//.test(
    line,
  );

/** The new state store's workers.dev address, where Alchemy reads it. */
export const stateStoreAddress = (stageName: string, workersSubdomain: string) =>
  `https://iterate-${stageName}-state.${workersSubdomain}.workers.dev`;

/** The longest wait for Cloudflare to serve a new state store, after Alchemy's own 2 minutes.
 *  Cloudflare's edge can answer 404 at a new script's workers.dev address for more than 5
 *  minutes after its upload and its subdomain both answered 200, though most stores answer
 *  within seconds: 6 minutes more covers about 8 minutes after the upload. */
export const STORE_WAIT_MS = 6 * 60_000;

/** How many 200 answers in a row, 2 seconds apart, say that Cloudflare serves the store: the
 *  edge may answer from one place before another. */
const STORE_ANSWERS_IN_A_ROW = 3;

/** What each line the command says while it waits for the store holds, after the time waited:
 *  the time comes first, so a spinner cut to the terminal's width still shows it move. */
export const WAITING_FOR_STORE = "waiting for Cloudflare to serve the mind's new state store";

/** Whether the store's address answers its `/version` (no token) with 200. */
const storeAnswers = async (address: string) => {
  const response = await fetch(`${address}/version`, {
    redirect: "manual",
    signal: AbortSignal.timeout(5000),
  }).catch(() => undefined);
  await response?.body?.cancel();
  return response?.status === 200;
};

/** Waits until the store's address answers 200 three times in a row, 2 seconds apart, for at
 *  most `STORE_WAIT_MS`. It says a line at the start and each 30 seconds. */
export async function waitForStore(
  address: string,
  say: (line: string) => void,
  { pause = sleep, now = Date.now, answers = storeAnswers }: StoreWaitDeps = {},
): Promise<boolean> {
  const began = now();
  say(`0 s: ${WAITING_FOR_STORE} at ${address}, for at most ${duration(STORE_WAIT_MS)}`);
  let told = 0;
  let inARow = 0;
  for (;;) {
    inARow = (await answers(address)) ? inARow + 1 : 0;
    if (inARow === STORE_ANSWERS_IN_A_ROW) return true;
    const waited = now() - began;
    if (waited >= STORE_WAIT_MS) return false;
    if (Math.floor(waited / 30_000) > told) {
      told = Math.floor(waited / 30_000);
      say(`${duration(waited)}: ${WAITING_FOR_STORE}`);
    }
    await pause(2000);
  }
}

type StoreWaitDeps = {
  pause?: (ms: number) => Promise<unknown>;
  now?: () => number;
  answers?: (address: string) => Promise<boolean>;
};

/** Whether a line of a deploy's output says that Alchemy did not see the event queue's new
 *  consumer bound to the boundary Worker: the other failure after which the command deploys again
 *  by itself. Alchemy 2.0.0-beta.87 reads a new consumer back for 60 seconds only; when
 *  Cloudflare's reads lag longer, the consumer is attached all the same, and the next run lists
 *  the queue's consumers and finds it. Remove when Alchemy's read-back waits longer or lists the
 *  consumers. */
export const saysTheConsumerIsNotSeenYet = (line: string) => /ConsumerScriptUnbound/.test(line);

/** The end of the line that says the command deploys again by itself. */
export const ONE_MORE_TRY = "one more try in";

/** Deep-clones a JSON-compatible config value, replacing any sensitive key's value with
 *  `"[redacted]"` so `MindConfig` can be printed safely to stdout. */
export function redactMindConfig(value: unknown): unknown {
  if (Array.isArray(value)) return value.map((item) => redactMindConfig(item));
  if (typeof value !== "object" || !value) return value;
  const redacted: Record<string, unknown> = {};
  for (const [key, entry] of Object.entries(value)) {
    if (entry === undefined) continue;
    redacted[key] =
      /(?:token|secret|password|passwd|api[_-]?key|private[_-]?key|credential|authorization)/i.test(
        key,
      )
        ? "[redacted]"
        : redactMindConfig(entry);
  }
  return redacted;
}

/** Pretty-printed JSON of the deployed `MindConfig` with any sensitive values redacted. */
export const formatMindConfigForStdout = (config: Record<string, unknown>) =>
  JSON.stringify(redactMindConfig(config), null, 2);

/** Where setup continues in the browser once the mind is deployed and answering, when the
 *  mind's config has an onboarding app. */
export const onboardingUrlOf = (zone: string) => `https://onboarding.${zone}/`;

/** WHERE SETUP GOES ON FOR THIS MIND: its onboarding app when its config serves one (the app's
 *  description answers 200 to anyone), else its Dash. One read, after the first pages answered. */
export async function setupHomeOf(zone: string, read: typeof fetch = fetch) {
  const onboarding = onboardingUrlOf(zone);
  const described = await read(`${onboarding}.well-known/iterate-app`, {
    signal: AbortSignal.timeout(10_000),
    redirect: "manual",
  }).catch(() => null);
  return described?.status === 200 ? onboarding : `https://dash.${zone}/`;
}

/** THE PAGE THE COMMAND OPENS AT THE END OF A DEPLOY, and the line printed right before
 *  "Press Enter to open in browser...".
 *  - On a first deploy, deep-links straight to `https://<controlPlaneHostname>/sign-in/access`
 *    (Cloudflare Access verifies the owner's email, sends them to add their first passkey, and
 *    `auth-worker` redirects to `onboarding.<zone>/` automatically — saving a click through `/sign-in`).
 *  - On a redeploy, opens `https://onboarding.<zone>/` directly (the owner already has a passkey,
 *    and `/sign-in/access` refuses owners who already have one).
 *  `home` is where setup goes on when it is not the onboarding app (`setupHomeOf`): a first
 *  deploy then names it as `next`, since the auth Worker sends a first passkey to onboarding.
 *  Always prints the owner email address to sign in with before the Enter prompt. */
export function setupDestinationOf(mind: {
  controlPlaneHostname: string;
  zone: string;
  ownerEmails: string[];
  firstDeploy: boolean;
  home?: string;
}) {
  const onboarding = onboardingUrlOf(mind.zone);
  const home = mind.home || onboarding;
  const access = `https://${mind.controlPlaneHostname}/sign-in/access`;
  const first = home === onboarding ? access : `${access}?${new URLSearchParams({ next: home })}`;
  const url = mind.firstDeploy ? first : home;
  const emails = mind.ownerEmails.join(" or ");
  const prompt = emails
    ? `Continue setup in your browser (sign in with ${emails}):`
    : "Continue setup in your browser:";
  return { url, prompt };
}

/** THE PAGE THAT SIGNS THIS COMPUTER IN TO THE MIND, from the issuer's own page for it (the
 *  browser flow's authorization request, or the device flow's approval page with its code). A new
 *  mind's owner has no passkey to approve it with yet, so on a first deploy the way there goes
 *  through `/sign-in/access`: Cloudflare Access proves the owner's email, the owner adds their
 *  first passkey, and the browser goes on to the issuer's page as `next`, signed in
 *  (core/auth-worker/src/access-sign-in.ts, enrolment.ts). On a redeploy the issuer's page is the
 *  way: it asks a browser that is not signed in for its passkey first. */
export function signInPageOf(
  mind: { controlPlaneHostname: string; firstDeploy: boolean },
  issuerPage: string,
) {
  if (!mind.firstDeploy) return issuerPage;
  return `https://${mind.controlPlaneHostname}/sign-in/access?${new URLSearchParams({ next: issuerPage })}`;
}

/** How long the sign-in after a deploy waits for the person: a new owner reads a mailed code and
 *  adds a passkey on the way. */
const SIGN_IN_WAIT_MS = 30 * 60_000;

/** WHAT THE COMMAND DOES NEXT FOR THE ACCOUNT'S ZERO TRUST ORGANIZATION, by Cloudflare's answer to
 *  `GET /accounts/<id>/access/organizations`, or, once `tried`, to the set-up after it. An owner
 *  proves their address through Cloudflare Access, which needs the organization
 *  (core/alchemy/src/access-team.ts). Cloudflare answers:
 *
 *  - 200 with the team's domain: there is one: go on;
 *  - 404, or 200 with no domain: there is none;
 *  - 403 "access.api.error.not_enabled: Access is not enabled" (code 9999): Zero Trust was never
 *    turned on.
 *
 *  For both of the last two the command first tries the set-up that the script would make, the
 *  same `POST` with no name, so Cloudflare picks the team's (`make`). Refused for Access or a plan,
 *  it is a gate that the person clears on Cloudflare's page; any other refusal, a missing
 *  permission say, stops the command with Cloudflare's words. Nobody has yet seen the set-up on
 *  an account that never had Zero Trust: whether Cloudflare allows it there is unknown. */
export function zeroTrustStep(account: string, answer: Answer, tried: boolean) {
  if (answer.status === 200 && resultOf(z.object({ auth_domain: z.string().min(1) }), answer))
    return { next: "go" } as const;
  const none = answer.status === 404 || answer.status === 200;
  const notEnabled = /not_enabled|not enabled|\bplans?\b|subscription/i.test(answer.error);
  if (!tried && (none || notEnabled)) return { next: "make" } as const;
  if (none || notEnabled)
    return {
      next: "gate",
      page: `https://one.dash.cloudflare.com/${account}/`,
      words:
        "Turn on Zero Trust: an owner of the mind proves their email address through Cloudflare Access, which is part of it.\nOn this page choose a team name and the Free plan. Cloudflare may ask for a payment method, and the Free plan charges nothing.",
    } as const;
  return {
    next: "stop",
    words: `Cloudflare refused ${tried ? "to set up" : "to read"} the account's Zero Trust organization (${answer.status}: ${answer.error}). The access needs "Access: Organizations, Identity Providers, and Groups Write". Nothing is deployed.`,
  } as const;
}

/** A word made to end in "erate", as "iterate" does: one final a, e, i, o, u or y goes first,
 *  when more than that letter is there (misha gives misherate, jonas gives jonaserate). */
export const erateOf = (word: string) =>
  `${word.length > 1 ? word.replace(/[aeiouy]$/, "") : word}erate`;

const domainEndings = ["com", "dev", "net", "xyz"] as const;

/** Names a person might like, from their name and a few words, 20 at most: what one question to
 *  Cloudflare's registrar takes. The "erate" names of their first and last name come first, as
 *  .com and .dev; then the other names, .com first. */
export function candidatesOf(name: string, words = ""): string[] {
  const wordsOf = (text: string) =>
    text
      .toLowerCase()
      .normalize("NFKD")
      .replace(/[^a-z0-9 ]/g, "")
      .split(/\s+/)
      .filter(Boolean);
  const parts = wordsOf(name);
  const first = parts[0];
  const last = parts.at(-1) === first ? undefined : parts.at(-1);
  const stems = new Set<string>();
  if (first && last) {
    stems.add(`${first}${last}`);
    stems.add(`${first}-${last}`);
    stems.add(`${first}${last.slice(0, 1)}`);
  }
  if (last) stems.add(last);
  if (first) for (const stem of [`${first}s`, `hey${first}`, `${first}hq`]) stems.add(stem);
  for (const word of wordsOf(words)) {
    stems.add(word);
    if (first) stems.add(`${first}${word}`);
  }
  const others = [...stems].flatMap((stem) => domainEndings.map((ending) => `${stem}.${ending}`));
  const erate = [first, last].flatMap((word) => (word ? [erateOf(word)] : []));
  return [
    ...new Set([
      ...["com", "dev"].flatMap((ending) => erate.map((stem) => `${stem}.${ending}`)),
      ...others.filter((domain) => domain.endsWith(".com")),
      ...others.filter((domain) => !domain.endsWith(".com")),
    ]),
  ].slice(0, 20);
}

/** Candidate names made from a taken domain's stem, reusing `candidatesOf`: first the stem's own
 *  other endings, then `candidatesOf` on the stem's words, without the taken domain itself, 20 at
 *  most. */
export function alternativesOf(domain: string): string[] {
  const lower = domain.toLowerCase();
  const stem = lower.split(".")[0] || "";
  if (!stem) return [];
  const endings = domainEndings.map((ending) => `${stem}.${ending}`);
  return [...new Set([...endings, ...candidatesOf(stem.replaceAll("-", " "))])]
    .filter((candidate) => candidate !== lower)
    .slice(0, 20);
}

/** WHETHER THE ACCOUNT HAS A PAYMENT CARD ON FILE, from AI Gateway's credit balance
 *  (`GET /accounts/<id>/ai-gateway/billing/credit-balance`, read with `CARD_SCOPE`): its
 *  `has_default_payment_method`. ANY OTHER ANSWER IS "unknown", NEVER "none": a sign-in without
 *  the scope is refused the read, and a refusal says nothing about the card. */
export type PaymentCard = "there" | "none" | "unknown";
export function paymentCardOf(answer: Answer): PaymentCard {
  if (answer.status !== 200) return "unknown";
  const said = resultOf(z.object({ has_default_payment_method: z.boolean() }), answer);
  if (!said) return "unknown";
  return said.has_default_payment_method ? "there" : "none";
}

/** The command that goes on with this deploy: THE PERSON'S OWN COMMAND, every flag of it kept
 *  (`--owner` names a new mind's owner, and a command without it would give the mind another),
 *  with the account by its id in place of `--account` and `--new-account`, so that a second run
 *  asks for no account and makes no second new one. `args`: what followed the program's name. */
export function resumeCommandOf(account: string, args: readonly string[] = ["deploy"]) {
  const kept: string[] = [];
  for (let index = 0; index < args.length; index++) {
    const arg = args[index]!;
    if (arg === "--new-account" || arg.startsWith("--account=")) continue;
    if (arg === "--account") index++;
    else kept.push(arg);
  }
  const quoted = (arg: string) =>
    /^[\w@%+=:,./-]+$/.test(arg) ? arg : `'${arg.replaceAll("'", "'\\''")}'`;
  return ["npx", "iterate@latest", ...kept, "--account", account].map(quoted).join(" ");
}

/** THE STEP FOR A PAYMENT CARD, which comes before every other step of the account: only for an
 *  account that Cloudflare says has no card (`paymentCardOf`) and that has no Workers Paid, so one
 *  that has a card, or whose card nobody could read, is asked nothing here. Without a card
 *  Cloudflare sells neither Workers Paid nor a domain, so nothing after it can work. THE COMMAND
 *  NEVER TAKES A CARD AND BUYS NOTHING HERE: the card is typed on Cloudflare's own page. `words`
 *  say how to go on: the command waits and sees the card, and `resume` is the command that goes on
 *  after it stopped. */
export function paymentCardStep(input: {
  account: string;
  card: PaymentCard;
  paid: boolean;
  created: boolean;
  /** the command's own arguments (`resumeCommandOf`) */
  args?: readonly string[];
}) {
  if (input.card !== "none" || input.paid) return undefined;
  const resume = resumeCommandOf(input.account, input.args);
  const lines = [
    "Add a payment card to your Cloudflare account. It is the one step that only you can do: without a card Cloudflare sells neither Workers Paid ($5 a month) nor a domain, and a mind needs both.",
    "Type the card on Cloudflare's page, never here. This command does not ask for a card, and this step buys nothing.",
    `Then come back to this terminal: the command sees the card and goes on. If it has stopped, run ${resume}`,
  ];
  if (input.created)
    lines.push(
      "On a new account, Cloudflare's billing page can say “contact your administrator” for a minute or two. Wait a minute, then open it again.",
    );
  return {
    page: `https://dash.cloudflare.com/${input.account}/billing/payment-info`,
    words: lines.join("\n"),
    resume,
  };
}

/** Instructions for turning on Workers Paid. When this run just created the account, Cloudflare's
 *  dashboard can briefly say "not authorized" until the new account propagates. `card`: with a
 *  card that nobody could read, the words say that Cloudflare's page asks for one. */
export function workersPaidPrompt(created: boolean, card: PaymentCard = "there") {
  const lines = [
    "Turn on Workers Paid ($5 a month). It only counts after you press Purchase, and your bank may ask to confirm the payment.",
    "If Cloudflare declines a card on a new account, try another card or an account you already have.",
  ];
  if (card === "unknown")
    lines.unshift(
      "If the account has no payment card yet, Cloudflare's page asks for one first: type it there, never here.",
    );
  if (created)
    lines.push("If Cloudflare's page says you are not authorized, wait a minute and reload.");
  return lines.join("\n");
}

/** THE PRODUCTS THAT THE COMMAND CAN TURN ON FOR THE PERSON, each with its rate plan in
 *  Cloudflare's catalog (`GET /billing/rate_plans/<plan>`, a public read), Cloudflare's page of
 *  its prices by use, and the page where a person turns it on by hand. */
export const PRODUCTS = {
  "Workers Paid": {
    plan: "workers_paid",
    pricing: "https://developers.cloudflare.com/workers/platform/pricing/",
    manual: (account: string) => `https://dash.cloudflare.com/${account}/workers/plans`,
  },
  R2: {
    plan: "r2_paid",
    pricing: "https://developers.cloudflare.com/r2/pricing/",
    manual: (account: string) => `https://dash.cloudflare.com/${account}/r2/overview`,
  },
} as const;
export type Product = keyof typeof PRODUCTS;

/** WHAT A RATE PLAN COSTS EACH MONTH BEFORE ANY USE, from Cloudflare's catalog: the plan's own
 *  name and the sum of its fixed monthly fees. ANYTHING THAT IS NOT READ FOR CERTAIN IS
 *  undefined, and the command then makes no offer: another plan than the one asked for, a
 *  currency that is not US dollars, a fee that is not a fixed price, or an answer of another
 *  shape. A person is never asked to agree to a price that the command worked out by a guess. */
export type PlanTerms = { plan: string; name: string; monthly: number };
export function planTermsOf(plan: string, answer: Answer | undefined): PlanTerms | undefined {
  if (answer?.status !== 200) return undefined;
  const said = resultOf(
    z.object({
      id: z.string(),
      public_name: z.string().min(1),
      currency: z.string(),
      components: z
        .array(
          z.object({
            chargeType: z.string(),
            defaultQuantity: z.number(),
            tiers: z.array(
              z.object({
                lowerThreshold: z.number(),
                upperThreshold: z.number(),
                pricingType: z.string(),
                price: z.number().nonnegative(),
              }),
            ),
          }),
        )
        .min(1),
    }),
    answer,
  );
  if (!said || said.id.toLowerCase() !== plan || said.currency !== "USD") return undefined;
  let monthly = 0;
  for (const part of said.components) {
    // a part charged by use is no fee of the month: Cloudflare's pricing page says it
    if (part.chargeType === "usage") continue;
    if (part.chargeType !== "subscription") return undefined;
    // the tier of the amount that a new subscription has; an upper end of 0 is no end
    const tier = part.tiers.filter(
      (each) =>
        each.lowerThreshold <= part.defaultQuantity &&
        (each.upperThreshold === 0 || part.defaultQuantity <= each.upperThreshold),
    );
    if (tier.length !== 1 || tier[0]!.pricingType !== "fixed") return undefined;
    monthly += tier[0]!.price;
  }
  return { plan, name: said.public_name, monthly };
}

const dollars = (amount: number) =>
  `$${Number.isInteger(amount) ? amount : amount.toFixed(2)}`;

/** THE OFFER TO TURN A PRODUCT ON FOR THE PERSON: the exact terms, and the question that only a
 *  yes answers. No offer (undefined), so the person turns it on by hand on Cloudflare's page as
 *  before, when: the terms are not known for certain (`planTermsOf`), the account has no payment
 *  card (Cloudflare then sells nothing), or `person` is false: nobody is at the terminal, or the
 *  access is an API token or a kept sign-in, where nobody is at Cloudflare's page to approve. */
export function activationOffer(input: {
  product: Product;
  terms: PlanTerms | undefined;
  account: string;
  card: PaymentCard;
  person: boolean;
}) {
  const { product, terms } = input;
  if (!terms || input.card === "none" || !input.person) return undefined;
  const price = `${dollars(terms.monthly)} a month`;
  const lines = [
    terms.monthly > 0
      ? `${product} is off. This command can turn it on for you: ${terms.name}, ${price}. Cloudflare charges the payment card of the account ${input.account} now and each month until you cancel it at Cloudflare.`
      : `${product} is off. This command can turn it on for you: ${terms.name}, ${price}.`,
    `Use beyond what Cloudflare includes costs more, charged to the same card: ${PRODUCTS[product].pricing}`,
    "After a yes, Cloudflare asks you once to approve billing access for this command. The command uses it for this alone and ends it at once. After a no, you turn it on yourself on Cloudflare's page.",
  ];
  return {
    words: lines.join("\n"),
    question: `Turn on ${product} now, for ${price}?`,
  };
}

/** What makes the subscription: the rate plan by its name in the catalog, renewed each month. */
export const subscriptionBodyOf = (plan: string) => ({
  rate_plan: { id: plan },
  frequency: "monthly",
});

/** WHAT BECAME OF ONE SUBSCRIPTION: on, with the monthly price that Cloudflare's answer names
 *  when it names one; not made, with the reason; or `lost`, when the request went out and no
 *  answer came, so that Cloudflare may have made it and nobody may say "nothing was turned on". */
export type Activation =
  | { on: true; charged?: number }
  | { on: false; why: string }
  | { lost: string };
export function activationOutcomeOf(answer: Answer): Activation {
  if (answer.status === 200 || answer.status === 201) {
    const said = resultOf(z.object({ price: z.number() }), answer);
    return said ? { on: true, charged: said.price } : { on: true };
  }
  // an answer of Cloudflare's own servers in trouble does not say that nothing was made
  if (answer.status >= 500) return { lost: `${answer.status}: ${answer.error}` };
  return { on: false, why: `${answer.status}: ${answer.error}` };
}

/** Whether the terms read right before the subscription are the terms that the person said yes
 *  to: the same plan, name and monthly fee. Terms that changed, or that cannot be read now, make
 *  no subscription. */
export const sameTerms = (agreed: PlanTerms, now: PlanTerms | undefined) =>
  Boolean(now) &&
  now!.plan === agreed.plan &&
  now!.name === agreed.name &&
  now!.monthly === agreed.monthly;

/** What the person is told after the subscriptions, one line for each product, and which
 *  products still need Cloudflare's own page. `stillOff`: for a `lost` one, what a read of the
 *  account said after it; such a product may be bought already, so its line sends the person to
 *  the account's subscriptions BEFORE any purchase by hand. */
export function activationReport(input: {
  account: string;
  outcomes: readonly { product: Product; agreed: PlanTerms; outcome: Activation; onNow: boolean }[];
}) {
  const lines: { kind: "success" | "warn"; text: string }[] = [];
  const byHand: Product[] = [];
  const subscriptions = `https://dash.cloudflare.com/${input.account}/billing/subscriptions`;
  for (const { product, agreed, outcome, onNow } of input.outcomes) {
    if ("lost" in outcome) {
      if (onNow) lines.push({ kind: "success", text: `${product} is on.` });
      else {
        byHand.push(product);
        lines.push({
          kind: "warn",
          text: `Cloudflare's answer for ${product} did not arrive (${outcome.lost}). Cloudflare may have made the subscription all the same. Look at the account's subscriptions before you buy it by hand, so that you do not buy it twice: ${subscriptions}`,
        });
      }
    } else if (outcome.on) {
      lines.push({ kind: "success", text: `${product} is on.` });
      if (outcome.charged !== undefined && outcome.charged !== agreed.monthly)
        lines.push({
          kind: "warn",
          text: `Cloudflare's answer names ${dollars(outcome.charged)} a month for ${product}, not the ${dollars(agreed.monthly)} that you said yes to. If you do not agree, cancel it: ${subscriptions}`,
        });
    } else {
      byHand.push(product);
      lines.push({
        kind: "warn",
        text: `Cloudflare did not turn ${product} on (${outcome.why}). Nothing was bought for it. Turn it on yourself on Cloudflare's page.`,
      });
    }
  }
  return { lines, byHand };
}

// ---------------------------------------------------------------------------------------------
// The terminal.

/** Whether ANSI color styling should be emitted to stdout (respects NO_COLOR, FORCE_COLOR,
 *  TERM=dumb, and TTY). */
export function isColorEnabled(env: NodeJS.ProcessEnv, isTTY: boolean) {
  if (env.NO_COLOR) return false;
  if (env.FORCE_COLOR && env.FORCE_COLOR !== "0") return true;
  if (env.TERM === "dumb") return false;
  return isTTY;
}

const styled = (format: Parameters<typeof styleText>[0], text: string, enabled: boolean) =>
  enabled ? styleText(format, text, { validateStream: false }) : text;

/** Tasteful ANSI palette for `iterate deploy`. Returns plain text when `enabled` is false. */
export function createPalette(enabled: boolean) {
  return {
    /** Iconic `𝑖` logo mark on the very first line. */
    mark: (text: string) => styled(["bold", "magenta"], text, enabled),
    /** Cloudflare account names — distinct warm yellow everywhere they appear. */
    account: (text: string) => styled(["bold", "yellow"], text, enabled),
    /** One-time Cloudflare device verification code. */
    code: (text: string) => styled(["bold", "magenta"], text, enabled),
    /** Domain names and mind hostnames. */
    domain: (text: string) => styled(["bold", "cyan"], text, enabled),
    /** Clickable Cloudflare dashboard and onboarding URLs. */
    url: (text: string) => styled(["underline", "blue"], text, enabled),
    /** Section headings inside the unified domain picker. */
    heading: (text: string) => styled("bold", text, enabled),
    /** Key callouts (titles, button names, bucket names). */
    callout: (text: string) => styled("bold", text, enabled),
  };
}

const palette = () => createPalette(isColorEnabled(process.env, Boolean(process.stdout.isTTY)));

/** Whether a person is at this terminal: requires a TTY, no CI flag, and no coding agent wrapper. */
export const isInteractiveEnv = (env: NodeJS.ProcessEnv, isTTY: boolean) => {
  const inCi = Boolean(env.CI && env.CI !== "0" && env.CI.toLowerCase() !== "false");
  return isTTY && !inCi && !isCodingAgent(env);
};

const interactive = () =>
  isInteractiveEnv(process.env, Boolean(process.stdin.isTTY && process.stdout.isTTY));

/** How the CLI's own sign-in reaches the person from this process (device-login/flow.ts).
 *  "device" says that the person's browser may be on another computer than this terminal (over
 *  SSH, on Linux with no display): no page is opened here then, and a step whose page comes back
 *  to this computer's own loopback address says where to run it instead. */
const signInFlow = () => loginFlowFor({ env: process.env, platform: process.platform });

/** A question for the person. With nobody there to answer, `flag` says what to pass instead. */
async function ask<T extends string | boolean | string[]>(
  flag: string,
  prompt: () => Promise<T | symbol>,
): Promise<T> {
  if (!interactive()) throw new Error(`Nobody is at this terminal to answer: ${flag}.`);
  const answer = await prompt();
  if (typeof answer === "symbol") {
    p.cancel("Stopped. Nothing more was changed.");
    process.exit(130);
  }
  return answer;
}

/** A line while something runs: a spinner for a person, plain lines for a log. */
function wait(title: string) {
  if (!interactive()) {
    p.log.step(title);
    return {
      say: (line: string) => console.log(`│  ${line}`),
      done: (line?: string) => line && p.log.success(line),
      fail: (line?: string) => line && p.log.error(line),
    };
  }
  const spinner = p.spinner();
  spinner.start(title);
  return {
    say: (line: string) =>
      spinner.message(
        `${title}: ${line}`.slice(0, Math.max(20, (process.stdout.columns || 80) - 6)),
      ),
    done: (line?: string) => spinner.stop(line || title),
    fail: (line?: string) => spinner.error(line || title),
  };
}

/** An end that no wait may try again: a sign-in at Cloudflare that was refused or ran out. */
class Stop extends Error {}

/** Asks `check` every few seconds until it answers something. A check that throws is asked
 *  again, and the end of the wait says the last such error. */
export async function until<T>(
  title: string,
  check: () => Promise<T | undefined | false | null>,
  minutes = 30,
  pause: (ms: number) => Promise<unknown> = sleep,
): Promise<T> {
  const waiting = wait(title);
  let failure = "";
  for (const end = Date.now() + minutes * 60_000; Date.now() < end; await pause(3000)) {
    const got = await check().catch((error: unknown) => {
      if (error instanceof Stop) {
        waiting.fail();
        throw error;
      }
      failure = error instanceof Error ? error.message : String(error);
      return undefined;
    });
    if (got) {
      waiting.done();
      return got;
    }
  }
  waiting.fail();
  throw new Error(
    `${title}: not within ${minutes} minutes${failure && ` (the last check failed: ${failure})`}. Run the command again when it is done.`,
  );
}

/** A page the person must act on: printed, and opened once they press Enter when a person is
 *  there, so browser focus is not yanked before they can read the instructions. */
async function page(text: string, url: string) {
  p.log.step(`${text}\n${palette().url(url)}`);
  if (!interactive() || process.env.ITERATE_SKIP_BROWSER_OPEN === "1") return;
  // a browser here is not the one the person looks at
  if (signInFlow() === "device") return;
  await ask<string>("", () =>
    p.text({ message: "Press Enter to open in browser...", defaultValue: "" }),
  );
  const { command, args } = openPageCommand(new URL(url), process.platform);
  execFile(command, args, () => {});
}

const duration = (ms: number) => {
  const all = Math.round(ms / 1000);
  if (all < 60) return `${all} s`;
  return all % 60 ? `${Math.floor(all / 60)} min ${all % 60} s` : `${all / 60} min`;
};
const seconds = (since: number) => duration(Date.now() - since);

// ---------------------------------------------------------------------------------------------
// The stack's source.

/** A checkout of the kernel, where it is from in words for the person, and how pnpm is run in
 *  it: the command and its first arguments. */
type Source = { dir: string; from: string; pnpm: { command: string; first: string[] } };

/** WHICH KERNEL A DEPLOY BUILDS, decided from the flags and from whether the mind is there.
 *
 *    --source <folder>        that checkout, whatever it holds, over any mind: how someone who
 *                             works on the kernel tries it, and how the mind that hosts the
 *                             source is deployed
 *    --kernel-repo <address>  for a new mind: that repo, at `main` or at `--ref`. The mind clones
 *                             the same repo at birth, so its own /repos/kernel holds what it runs
 *    neither                  over a mind that is there: the mind's OWN /repos/kernel. Nothing is
 *                             fetched from outside the mind, so a deploy for another reason (a
 *                             binding, a Worker) builds the same kernel again, and a new kernel
 *                             comes by a pull or a merge into that repo first
 *
 *  `--kernel-repo` and `--config-repo` are for a birth and are refused over a mind that is there.
 *  `begunWith` is the kernel address of a deploy that stopped before the mind was there, whose
 *  config is on this computer: the next run goes on with it. */
export function kernelPlanOf(
  input: DeployInput,
  mind: { deployed: boolean; begunWith: string | null },
):
  | { kind: "checkout"; dir: string }
  | { kind: "address"; origin: string; ref: string }
  | { kind: "own"; ref: string } {
  if (mind.deployed && input.kernelRepo)
    throw new Error(
      "--kernel-repo is for a mind's birth. This mind has a kernel repo of its own: bring the change into /repos/kernel there, then deploy.",
    );
  if (mind.deployed && input.configRepo)
    throw new Error(
      "--config-repo is for a mind's birth. This mind has a config repo of its own: change /repos/config there.",
    );
  if (input.source) return { kind: "checkout", dir: resolve(input.source) };
  const ref = input.ref || "main";
  if (mind.deployed) return { kind: "own", ref };
  const origin = input.kernelRepo || mind.begunWith;
  if (!origin)
    throw new Error(
      "A new mind needs a kernel: --kernel-repo with the address of a kernel repo, or --source with a checkout of one.",
    );
  return { kind: "address", origin, ref };
}

/** The address that a config's repo list gives the kernel's repo, or null: a list that a
 *  deployed mind keeps is read as it comes. */
const kernelAddressIn = (repos: unknown) =>
  z
    .array(z.object({ path: z.string(), origin: z.string().nullable() }))
    .safeParse(repos)
    .data?.find(({ path }) => path === "/repos/kernel")?.origin || null;

/** A MIND'S OWN KERNEL REPO, as git reaches it: the address of its /repos/kernel in Cloudflare
 *  Artifacts, and a token that reads it for ten minutes. The deploy asks Cloudflare with the
 *  access it holds already. The repo is the mind's own and may be private, so it is not read
 *  through the mind's git app. */
export async function ownKernelRepo(cf: Cloudflare, account: string, stageName: string) {
  const namespace = `/accounts/${account}/artifacts/namespaces/iterate-${stageName}-repos`;
  const repo = "repos--kernel";
  const found = await cf("GET", `${namespace}/repos/${repo}`);
  const origin = resultOf(z.object({ remote: z.string().min(1) }), found)?.remote;
  if (!origin)
    throw new Error(
      `This mind has no /repos/kernel to build (${found.error || found.status}). Give --source with a checkout of the kernel.`,
    );
  const made = await cf("POST", `${namespace}/tokens`, { repo, scope: "read", ttl: 600 });
  const token = resultOf(z.object({ plaintext: z.string().min(1) }), made)?.plaintext;
  if (!token)
    throw new Error(
      `Cloudflare gave no token to read this mind's /repos/kernel (${made.error || made.status}).`,
    );
  return { origin, authorization: `Bearer ${token}` };
}

/** The kernel of `origin` at `ref`, checked out in `dir`, and the commit that it is. One shallow
 *  fetch, into a folder that is kept, so a later run fetches only what is new. `authorization`
 *  is the header that reads a repo of the mind's own (`ownKernelRepo`); null for an address
 *  that anyone may read, which a new mind clones at birth with no credential too. No credential
 *  that git has kept is used: one that has ended would refuse a repo anyone may read. The header
 *  goes to git through its environment, so it is in no command line. */
export async function fetchedKernel(input: {
  origin: string;
  ref: string;
  dir: string;
  authorization: string | null;
}): Promise<string> {
  if (spawnSync("git", ["--version"], { stdio: "ignore" }).status !== 0)
    throw new Error(
      "git is not installed: the deploy fetches iterate's kernel with it. Install git and run again.",
    );
  mkdirSync(input.dir, { recursive: true });
  const header = input.authorization && {
    GIT_CONFIG_COUNT: "1",
    GIT_CONFIG_KEY_0: "http.extraHeader",
    GIT_CONFIG_VALUE_0: `Authorization: ${input.authorization}`,
  };
  const git = (...args: string[]) =>
    exec("git", ["-c", "credential.helper=", ...args], {
      cwd: input.dir,
      env: { ...process.env, ...header },
    });
  // in a folder that is a repo already this changes nothing
  await git("init", "-q");
  try {
    await git("fetch", "-q", "--depth", "1", input.origin, input.ref);
  } catch (error) {
    throw new Error(`The kernel could not be fetched from ${input.origin} at ${input.ref}.`, {
      cause: error,
    });
  }
  await git("checkout", "-q", "--detach", "--force", "FETCH_HEAD");
  const head = await git("rev-parse", "HEAD");
  return head.stdout.trim();
}

/** Environment overrides for pnpm child processes so strict engine or packageManager checks do not
 *  block installs or deploys across Node and pnpm versions. */
export const PNPM_COMPAT_ENV = {
  npm_config_engine_strict: "false",
  npm_config_manage_package_manager_versions: "false",
  COREPACK_ENABLE_STRICT: "0",
} as const;

/** Child-process env for `pnpm` vs `npx`: npm/npx warns on unknown `manage-package-manager-versions`,
 *  so omit and strip that pnpm-only variable when invoking via `npx`. */
export function pnpmEnvFor(
  command: string,
  baseEnv: NodeJS.ProcessEnv = process.env,
): NodeJS.ProcessEnv {
  if (command === "npx") {
    const { npm_config_manage_package_manager_versions: _pnpmOnly, ...rest } = baseEnv;
    return {
      ...rest,
      npm_config_engine_strict: PNPM_COMPAT_ENV.npm_config_engine_strict,
      COREPACK_ENABLE_STRICT: PNPM_COMPAT_ENV.COREPACK_ENABLE_STRICT,
    };
  }
  return { ...baseEnv, ...PNPM_COMPAT_ENV };
}

/** Summarizes Alchemy's verbose log stream into a concise progress label (`12 of 48 resources`),
 *  ignoring noisy reconcile lines, plan preview lines, npm warnings, and raw stack output objects. */
export function createDeployProgress() {
  let planned = 0;
  let planEnded = false;
  const done = new Set<string>();
  /** resources that Alchemy said it is making and has not yet said made or failed */
  const working = new Set<string>();
  const count = () =>
    planned > 0
      ? `${Math.min(done.size, planned)} of ${planned} resources`
      : `${done.size} resource${done.size === 1 ? "" : "s"}`;
  return {
    /** A line for a long quiet stretch: the count, and what Alchemy is still making. Alchemy says
     *  nothing while it uploads a Worker, which can take minutes. */
    stillAt(): string | undefined {
      if (!planEnded || working.size === 0) return undefined;
      const names = [...working];
      const more = names.length > 3 ? ` and ${names.length - 3} more` : "";
      return `${count()}; still making ${names.slice(0, 3).join(", ")}${more}`;
    },
    /** Feeds one Alchemy log line and returns a short status label when progress changes. */
    onLine(line: string): string | undefined {
      const planMatch = /\bPlan:\s+(.+)/.exec(line);
      if (planMatch) {
        planEnded = true;
        let total = 0;
        for (const count of planMatch[1]!.matchAll(
          /(\d+)\s+to\s+(?:create|update|adopted|replace|delete|orphaned)\b/g,
        )) {
          total += Number(count[1]);
        }
        if (total > 0) {
          planned = total;
          return `0 of ${planned} resources`;
        }
        return undefined;
      }
      const settled = /\bINFO:\s+\[([^\]]+)\]\s+(created|updated|deleted|replaced|adopted)\b/.exec(
        line,
      );
      if (settled && (settled[2] !== "adopted" || planEnded)) {
        done.add(settled[1]!);
        working.delete(settled[1]!);
        return count();
      }
      const busy = /\bINFO:\s+\[([^\]]+)\]\s+(creating|updating|deleting|replacing)\b/.exec(line);
      if (busy && planEnded) working.add(busy[1]!);
      const failed = /\bERROR:\s+\[([^\]]+)\]\s+fail\b/.exec(line);
      if (failed) working.delete(failed[1]!);
      if (/state store/i.test(line) && /(?:bootstrapping|updating|creating)/i.test(line)) {
        return "preparing state store";
      }
      return undefined;
    },
  };
}

/** How to run pnpm for `packageManager`: use local `pnpm` only when its major version matches
 *  `packageManager`; otherwise run `npx --yes <packageManager>`. */
export function pnpmCommandOf(
  packageManager: string,
  localVersion?: string,
): { command: string; first: string[] } {
  const expectedMajor = /^pnpm@(\d+)/.exec(packageManager.trim())?.[1];
  const localMajor = /^(\d+)/.exec(localVersion?.trim() || "")?.[1];
  if (expectedMajor && localMajor && expectedMajor === localMajor)
    return { command: "pnpm", first: [] };
  return { command: "npx", first: ["--yes", packageManager] };
}

/** THE KERNEL THAT A DEPLOY BUILDS (`kernelPlanOf`), as a folder on this computer with its
 *  dependencies installed, and where it is from in words for the person. A kernel that is
 *  fetched goes into the cache folder. `own` answers the mind's own kernel repo (`ownKernelRepo`),
 *  and is asked only for a mind that is there. A source whose core/alchemy is not the one script
 *  that this command writes the input of is refused. The folder this command happens to run in
 *  is never the kernel by itself. */
async function sourceOf(
  plan: ReturnType<typeof kernelPlanOf>,
  own: () => Promise<{ origin: string; authorization: string }>,
): Promise<Source> {
  const fetched = join(CACHE, "kernel");
  let dir = fetched;
  let from: string;
  if (plan.kind === "checkout") {
    dir = plan.dir;
    from = `the checkout ${dir}`;
  } else if (plan.kind === "address") {
    const commit = await fetchedKernel({ ...plan, dir, authorization: null });
    from = `${plan.origin} at ${commit.slice(0, 12)}`;
  } else {
    const commit = await fetchedKernel({ ...(await own()), ref: plan.ref, dir });
    from = `this mind's own /repos/kernel at ${commit.slice(0, 12)}`;
  }
  const marker = "core/alchemy/alchemy.run.ts";
  if (!existsSync(join(dir, marker)))
    throw new Error(`${from} holds no ${marker}: it is not iterate's kernel.`);
  // the script whose input this command writes names the control plane's hostname in its config
  const scriptConfig = join(dir, "core/alchemy/src/config.ts");
  if (
    !existsSync(scriptConfig) ||
    !readFileSync(scriptConfig, "utf8").includes("controlPlaneHostname")
  )
    throw new Error(
      `The source at ${dir} is a version of iterate before the one Alchemy script (its core/alchemy/src/config.ts names no controlPlaneHostname), and this command writes that script's input alone. Nothing was changed. Pass --source with a checkout that holds it.`,
    );
  // use local pnpm only when its major version matches packageManager; otherwise run via npx
  const manager = z
    .object({ packageManager: z.string().default("pnpm@12") })
    .parse(JSON.parse(readFileSync(join(dir, "package.json"), "utf8"))).packageManager;
  const localPnpm = spawnSync("pnpm", ["--version"], { encoding: "utf8" });
  const pnpm = pnpmCommandOf(manager, localPnpm.status === 0 ? localPnpm.stdout : undefined);
  // a checkout of one's own is installed once; the cache follows its lockfile at every run
  if (dir === fetched || !existsSync(join(dir, "core/alchemy/node_modules/alchemy/bin/cli.js")))
    await exec(pnpm.command, [...pnpm.first, "install", "--frozen-lockfile"], {
      cwd: dir,
      env: { ...pnpmEnvFor(pnpm.command), CI: "1" },
      maxBuffer: 64 << 20,
    });
  // pre-build core-mind-worker in the background while the person signs in and answers prompts
  // so `alchemy deploy` and `alchemy plan` do not have to rebuild it later
  await exec(pnpm.command, [...pnpm.first, "--filter", "core-mind-worker", "build"], {
    cwd: dir,
    env: { ...pnpmEnvFor(pnpm.command), CI: "1" },
    maxBuffer: 64 << 20,
  });
  return { dir, from, pnpm };
}

// ---------------------------------------------------------------------------------------------
// The deploy.

type Run = {
  source: Source;
  stageName: string;
  /** the file that holds the script's one input */
  config: string;
  token: string;
  log: string;
  children: Set<ChildProcess>;
  say?: (line: string) => void;
  /** a deploy that goes on with an earlier one whose config is here (`earlierDeployOf`) */
  adopt?: boolean;
};

/** THE ARGUMENTS OF `alchemy deploy` in core/alchemy (`sourceOf` already built `core-mind-worker`
 *  in the background, so deploys and retries skip redundant rebuilds). A deploy that stopped before
 *  its end can leave a resource that Cloudflare made and Alchemy's state does not hold, since its
 *  creation was under way at the stop: Alchemy then refuses it ("OwnedBySomeoneElse", for a
 *  Pipelines stream, which carries no owner's tag) unless `--adopt` takes it over. The stage's
 *  state store says that such a resource, under the stage's own name, is the stack's own: so a
 *  deploy that goes on adopts. So does a new mind beside a store that reads as a destroy's
 *  leftover: a stack that stopped before its first Worker and its first record reads the same from
 *  outside, with its bucket, tokens and streams there, and after a real destroy nothing is there to
 *  take over. No other deploy adopts. */
export const deployArgsOf = (stageName: string, adopt = false) => [
  "exec",
  "alchemy",
  "deploy",
  "alchemy.run.ts",
  "--stage",
  stageName,
  "--yes",
  ...(adopt ? ["--adopt"] : []),
];

/** THE COMMANDS OF A PLAN of core/alchemy, which changes nothing: Alchemy's plan of the stage
 *  (`sourceOf` already built `core-mind-worker`). Each resource gets one line, `[Name] noop`,
 *  `update`, `create`, `delete` and the like: what a caller with nobody at the terminal reads
 *  before it deploys. */
export const planArgsOf = (stageName: string) => [
  ["exec", "alchemy", "plan", "alchemy.run.ts", "--stage", stageName, "--no-input"],
];

/** One `pnpm run deploy` of core/alchemy (or other arguments of pnpm there): its exit code, and
 *  whether the state store refused it or Alchemy did not see the queue's consumer. Every line goes
 *  to the log file, with any token text replaced, and any inherited
 *  CLOUDFLARE_OAUTH_CLIENT_API_TOKEN stripped from the child env. */
function deployOnce(
  run: Run,
  args = deployArgsOf(run.stageName, run.adopt),
): Promise<{ code: number; storeRefused: boolean; consumerNotSeen: boolean }> {
  return new Promise((done, failed) => {
    const file = createWriteStream(run.log, { flags: "a" });
    const { command, first } = run.source.pnpm;
    // A source of an earlier version (`--ref`) still turns telemetry tables on with a token in
    // CLOUDFLARE_TELEMETRY_API_TOKEN: dropped, so a token of the shell never makes a mind's
    // tables beside the ones that the mind makes itself.
    const {
      CLOUDFLARE_OAUTH_CLIENT_API_TOKEN: _droppedOAuthToken,
      CLOUDFLARE_TELEMETRY_API_TOKEN: _droppedTelemetryToken,
      ...parentEnv
    } = process.env;
    const child = spawn(command, [...first, ...args], {
      cwd: join(run.source.dir, "core/alchemy"),
      env: {
        ...pnpmEnvFor(command, parentEnv),
        CI: "1",
        NO_COLOR: "1",
        FORCE_COLOR: "0",
        CLOUDFLARE_API_TOKEN: run.token,
        ITERATE_MIND_CONFIG: run.config,
      },
      stdio: ["ignore", "pipe", "pipe"],
    });
    run.children.add(child);
    let storeRefused = false;
    let consumerNotSeen = false;
    let rest = "";
    const read = (chunk: Buffer) => {
      const lines = (rest + chunk.toString("utf8")).split("\n");
      rest = lines.pop() ?? "";
      for (const raw of lines) {
        const line = hidden(raw, run).trimEnd();
        file.write(`${line}\n`);
        if (saysTheStateStoreRefused(line)) storeRefused = true;
        if (saysTheConsumerIsNotSeenYet(line)) consumerNotSeen = true;
        if (line.trim()) run.say?.(line.trim());
      }
    };
    child.stdout.on("data", read);
    child.stderr.on("data", read);
    child.on("error", failed);
    child.on("close", (code) => {
      run.children.delete(child);
      file.end(`${hidden(rest, run)}\n`);
      done({ code: code ?? 1, storeRefused, consumerNotSeen });
    });
  });
}

/** A line of the deploy's output with the text of any token replaced. */
const hidden = (line: string, run: Run) =>
  [run.token, process.env.CLOUDFLARE_OAUTH_CLIENT_API_TOKEN].reduce<string>(
    (text, token) => (token ? text.replaceAll(token, "[token]") : text),
    line,
  );

/** The deploy. When the state store refused it (`saysTheStateStoreRefused`), it waits until
 *  Cloudflare serves the store (`waitForStore`); when Alchemy did not yet see the queue's
 *  consumer (`saysTheConsumerIsNotSeenYet`), it waits a minute. Then it runs once more, and only
 *  once. The second run goes on with what the first made, as a person's run of the command does,
 *  so it adopts. `storeNotServed` is the store's address when the deploy failed because
 *  Cloudflare did not serve it. */
export async function deployStack<
  R extends { stageName: string; storeAddress: string; say?: (line: string) => void },
>(
  run: R,
  once: (run: R, args?: string[]) => ReturnType<typeof deployOnce>,
  deps: StoreWaitDeps = {},
): Promise<{ code: number; consumerNotSeen: boolean; storeNotServed?: string }> {
  const { pause = sleep } = deps;
  const first = await once(run);
  if (first.code === 0) return { code: 0, consumerNotSeen: false };
  if (first.storeRefused) {
    const say = (line: string) => run.say?.(line);
    say(
      `The new state store refused the deploy: ${ONE_MORE_TRY} at most ${duration(STORE_WAIT_MS)}, when Cloudflare serves it`,
    );
    if (!(await waitForStore(run.storeAddress, say, deps)))
      return { code: first.code, consumerNotSeen: false, storeNotServed: run.storeAddress };
    const second = await once(run);
    return {
      code: second.code,
      consumerNotSeen: false,
      ...(second.code !== 0 && second.storeRefused && { storeNotServed: run.storeAddress }),
    };
  }
  if (!first.consumerNotSeen) return { code: first.code, consumerNotSeen: false };
  run.say?.(
    `Cloudflare did not yet confirm the boundary Worker as the event queue's consumer: ${ONE_MORE_TRY} a minute`,
  );
  await pause(60_000);
  const second = await once(run, deployArgsOf(run.stageName, true));
  return { code: second.code, consumerNotSeen: second.code !== 0 && second.consumerNotSeen };
}

// ---------------------------------------------------------------------------------------------
// Cloudflare: the account, the domain.

const Account = z.object({ id: z.string(), name: z.string() });
type Account = z.infer<typeof Account>;
const Membership = z.object({ status: z.string().optional(), account: Account.optional() });
const ZoneRow = z.object({
  id: z.string(),
  name: z.string(),
  status: z.string(),
  name_servers: z.array(z.string()).optional(),
});
/** A zone, and the stage of the mind whose boundary Worker its routes name, when one does. */
type Zone = z.infer<typeof ZoneRow> & {
  stageName?: string;
  /** Its routes or records could not be read, so nothing says what lives there. */
  unread?: boolean;
  /** What else uses the zone: a Worker's route, or a record that names a host or takes mail. */
  uses?: string[];
};
const DnsRecord = z.object({
  id: z.string(),
  type: z.string(),
  name: z.string(),
  content: z.string().nullish(),
});
// a route that turns Workers off has `script: null`
const Route = z.object({ id: z.string(), pattern: z.string(), script: z.string().nullish() });

/** WHAT A ZONE HOLDS THAT A MIND WOULD REPLACE: every DNS record (Cloudflare lists no NS or SOA of
 *  its own) and every Workers route. Undefined when Cloudflare did not answer with both. */
async function holdingsOf(cf: Cloudflare, zoneId: string) {
  const routes = resultOf(z.array(Route), await cf("GET", `/zones/${zoneId}/workers/routes`));
  const records: z.infer<typeof DnsRecord>[] = [];
  for (let page = 1; page <= 50; page++) {
    const listed = resultOf(
      z.array(DnsRecord),
      await cf("GET", `/zones/${zoneId}/dns_records?per_page=100&page=${page}`),
    );
    if (!listed) return undefined;
    records.push(...listed);
    if (listed.length < 100) break;
  }
  if (!routes) return undefined;
  return { routes, records };
}

/** Erases them, each by its id, and stops at the first that Cloudflare refuses: the deploy then
 *  does not start, and a second run erases what is left. */
async function eraseHoldings(cf: Cloudflare, zoneId: string) {
  const holdings = await holdingsOf(cf, zoneId);
  if (!holdings) throw new Error("Cloudflare did not answer with the zone's records and routes.");
  for (const route of holdings.routes) {
    const gone = await cf("DELETE", `/zones/${zoneId}/workers/routes/${route.id}`);
    if (gone.status >= 300)
      throw new Error(
        `Cloudflare did not delete the route ${route.pattern} (${gone.status}): ${gone.error}`,
      );
  }
  for (const record of holdings.records) {
    const gone = await cf("DELETE", `/zones/${zoneId}/dns_records/${record.id}`);
    if (gone.status >= 300)
      throw new Error(
        `Cloudflare did not delete the ${record.type} record ${record.name} (${gone.status}): ${gone.error}`,
      );
  }
  return { routes: holdings.routes.length, records: holdings.records.length };
}
const Offer = z.object({
  name: z.string(),
  registrable: z.boolean().optional(),
  pricing: z
    .object({ registration_cost: z.string().optional(), currency: z.string().optional() })
    .optional(),
});
type Offer = z.infer<typeof Offer>;

/** THE PERSON'S ACCOUNTS, AND WHICH OF THEM THIS ACCESS COVERS. A sign-in lists every account
 *  the person belongs to as a membership, also the ones they did not select on Cloudflare's
 *  page; a call to one of those answers 403. So each is asked for once: the account's own read
 *  answers 200 for a covered one and 403 for one that is not. Any other answer stops the run: it
 *  says nothing about the approval. An API token lists no membership, and `GET /accounts` then
 *  names exactly what it reaches. The first 50 accounts are read; `--account <id>` reaches one
 *  beyond them (`pickAccount`). */
async function accountsOf(cf: Cloudflare): Promise<{ all: Account[]; covered: Account[] }> {
  const memberships =
    resultOf(z.array(Membership), await cf("GET", "/memberships?per_page=50")) ?? [];
  const all = memberships.flatMap((membership) =>
    membership.account && (membership.status || "accepted") === "accepted"
      ? [membership.account]
      : [],
  );
  if (all.length === 0) {
    const reached = resultOf(z.array(Account), await cf("GET", "/accounts?per_page=50")) ?? [];
    return { all: reached, covered: reached };
  }
  const covered = await Promise.all(
    all.map(async (account) => {
      const read = await cf("GET", `/accounts/${account.id}`);
      if (read.status === 200) return [account];
      if (read.status === 403 || read.status === 404) return [];
      throw new Error(
        `Cloudflare did not say whether this access covers the account ${palette().account(account.name)} (${read.status}: ${read.error}). Run the command again.`,
      );
    }),
  );
  return { all, covered: covered.flat() };
}

/** The account's zones (its first 50, or the one named `domain`), each with its stack and with
 *  what else uses it. A MIND TAKES A WHOLE ZONE THAT NOTHING ELSE USES: its site, its mail and its
 *  sign-in are the zone's. So a route of another Worker, and a record that names a host or takes
 *  mail (A, AAAA, CNAME, MX), each make a zone one that the command deploys to only after the
 *  person agreed to erase what it holds. A stack's own records (AAAA 100::) and routes are not a
 *  use. */
async function zonesOf(cf: Cloudflare, account: string, domain?: string): Promise<Zone[]> {
  const which = domain ? `&name=${domain}` : "";
  const zones =
    resultOf(
      z.array(ZoneRow),
      await cf("GET", `/zones?account.id=${account}&per_page=50${which}`),
    ) ?? [];
  return Promise.all(
    zones.map(async ({ id, name, status, name_servers }) => {
      const [routes, records] = await Promise.all([
        cf("GET", `/zones/${id}/workers/routes`).then((answer) => resultOf(z.array(Route), answer)),
        cf("GET", `/zones/${id}/dns_records?per_page=100`).then((answer) =>
          resultOf(z.array(DnsRecord), answer),
        ),
      ]);
      if (!routes || !records) return { id, name, status, name_servers, unread: true };
      const stack = /^iterate-(.+)-boundary$/;
      const uses = [
        ...routes
          .filter((route) => !stack.test(route.script || ""))
          .map((route) => `the route ${route.pattern} of ${route.script || "no Worker"}`),
        ...records
          .filter((record) => ["A", "AAAA", "CNAME", "MX"].includes(record.type))
          .filter((record) => !(record.type === "AAAA" && record.content === "100::"))
          .map((record) => `the ${record.type} record ${record.name}`),
        // this list is the zone's first page: a zone that fills it is in use, whatever follows
        ...(records.length >= 100 ? ["100 or more DNS records"] : []),
      ];
      return {
        id,
        name,
        status,
        name_servers,
        stageName: routes.map((route) => stack.exec(route.script || "")?.[1]).find(Boolean),
        uses,
      };
    }),
  );
}

const priceOf = (offer: Offer) =>
  `${offer.pricing?.registration_cost || "?"} ${offer.pricing?.currency || "USD"} a year`;

/** What Cloudflare's registrar says of these names: which it sells, and for how much. */
async function offersOf(cf: Cloudflare, account: string, domains: string[]): Promise<Offer[]> {
  const answer = await cf("POST", `/accounts/${account}/registrar/domain-check`, { domains });
  return resultOf(z.object({ domains: z.array(Offer) }), answer)?.domains ?? [];
}

/** A domain and how the account gets it: `zone` when the account has it; else the command buys
 *  it (`offer`: Cloudflare's registrar sells it, and the person said yes to its price), or the
 *  person adds it on Cloudflare's own page (it is theirs at another registrar). */
type Pick = { domain: string; zone?: Zone; offer?: Offer };

/** Sentinel option value for entering a brand new domain name to buy (or search keywords). */
export const ENTER_NEW_DOMAIN = "\0enter-new-domain";

export type DomainPickerOption = {
  value: string;
  label: string;
  hint?: string;
  disabled?: boolean;
};

/** Builds the single unified domain-picker list:
 *  1. "Buy a brand new domain" — available domains to buy, plus entering a brand new domain name
 *  2. "Use an existing cloudflare zone" — the account's existing Cloudflare zones */
export function domainPickerOptions(input: {
  offers: readonly Offer[];
  zones: readonly Zone[];
  maxOffers?: number;
  color?: boolean;
}): DomainPickerOption[] {
  const c = createPalette(input.color ?? false);
  const readableZones = input.zones.filter((zone) => !zone.unread);
  const shownOffers = input.offers.slice(0, input.maxOffers ?? 6);
  const options: DomainPickerOption[] = [
    { value: "\0heading-buy", label: c.heading("Buy a brand new domain"), disabled: true },
    ...shownOffers.map((offer) => ({
      value: offer.name,
      label: offer.name,
      hint: priceOf(offer),
    })),
    {
      value: ENTER_NEW_DOMAIN,
      label: "Enter a brand new domain name and buy that",
      hint: "type a domain to buy, or keywords for more names",
    },
    {
      value: "\0heading-zones",
      label: c.heading("Use an existing cloudflare zone"),
      disabled: true,
    },
  ];
  if (readableZones.length > 0) {
    for (const zone of readableZones) {
      options.push({
        value: zone.name,
        label: zone.name,
        hint: zone.stageName
          ? "a mind is here already: deploy over it"
          : zone.uses?.length
            ? "in this account, in use: its records would be erased"
            : "in this account, unused",
      });
    }
  } else {
    options.push({
      value: "\0no-zones",
      label: "No existing Cloudflare zones in this account",
      disabled: true,
    });
  }
  return options;
}

type PickDomainUi = {
  select: (args: {
    message: string;
    maxItems: number;
    initialValue: string | undefined;
    options: DomainPickerOption[];
  }) => Promise<string>;
  text: (args: {
    message: string;
    validate: (value: string | undefined) => string | undefined;
  }) => Promise<string>;
  confirm: (flag: string, message: string) => Promise<boolean>;
  warn: (message: string) => void;
};

/** The domain, and how the account gets it: a new one to buy, a zone the account has, or one to
 *  add. With no `--domain`, the person picks from one unified list with "Buy a brand new domain"
 *  at the top (including entering a brand new domain name to buy) and "Use an existing cloudflare
 *  zone" below. `person` is the name the initial candidates are made from. */
export async function pickDomain(
  cf: Cloudflare,
  account: string,
  input: DeployInput,
  zones: Zone[],
  person: string,
  {
    select = (opts) => ask<string>("pass --domain <domain>", () => p.select(opts)),
    text = (opts) => ask<string>("pass --domain <domain>", () => p.text(opts)),
    confirm = (flag, message) => ask<boolean>(flag, () => p.confirm({ message })),
    warn = (message) => p.log.warn(message),
  }: Partial<PickDomainUi> = {},
): Promise<Pick> {
  const free = async (domains: string[]) =>
    (await offersOf(cf, account, domains)).filter((offer) => offer.registrable);
  let offers = input.domain ? [] : await free(candidatesOf(person));
  let maxOffers = 6;
  const addOffers = async (domains: string[], from: string) => {
    const more = await free(domains);
    if (more.length === 0) warn(`No name from "${from}" is free at Cloudflare.`);
    // the new names first, then the ones from before
    offers = [...more, ...offers.filter((old) => more.every((now) => now.name !== old.name))];
    maxOffers = 8;
  };
  for (;;) {
    const options = domainPickerOptions({
      offers,
      zones,
      maxOffers,
      color: isColorEnabled(process.env, Boolean(process.stdout.isTTY)),
    });
    const firstSelectable = options.find((option) => !option.disabled)?.value;
    let domain =
      input.domain?.toLowerCase() ||
      (await select({
        message:
          "Which domain does the mind live at? It takes the whole domain, so the mind can be its site, mail and apps.",
        maxItems: 16,
        initialValue: firstSelectable,
        options,
      }));
    if (domain === ENTER_NEW_DOMAIN) {
      const typed = (
        await text({
          message:
            "Enter a brand new domain name to buy (e.g. ada-labs.com), or a few words for more ideas",
          validate: (value) => (value?.trim() ? undefined : "A domain, or a few words"),
        })
      )
        .trim()
        .toLowerCase();
      if (!Hostname.safeParse(typed).success) {
        await addOffers(candidatesOf(person, typed), typed);
        continue;
      }
      domain = typed;
    }
    if (!Hostname.safeParse(domain).success)
      throw new Error(`${JSON.stringify(domain)} is no domain.`);
    const zone =
      zones.find((candidate) => candidate.name === domain) ||
      (await zonesOf(cf, account, domain))[0];
    if (zone) return { domain, zone };
    const offer =
      offers.find((candidate) => candidate.name === domain) ||
      (await offersOf(cf, account, [domain])).find((candidate) => candidate.name === domain);
    if (offer?.registrable) {
      // THE YES TO SPEND MONEY, asked here, before anything changes. The purchase itself comes
      // after every other check, when the deploy needs the zone (`zoneOf`).
      const buy =
        input.buy ||
        (await confirm(
          `${domain} costs ${priceOf(offer)}; pass --buy to buy it`,
          `Buy ${domain} at Cloudflare for ${priceOf(offer)}? Cloudflare charges the account's payment method, and a registration is not refunded.`,
        ));
      if (buy) return { domain, offer };
    } else {
      const theirs =
        input.yes ||
        (await confirm(
          `${domain} is not for sale at Cloudflare; pass --yes when it is yours at another registrar`,
          `${domain} is not for sale at Cloudflare. Is it yours at another registrar? You then add it to this account and set its nameservers to Cloudflare's.`,
        ));
      if (theirs) return { domain };
      if (input.domain)
        throw new Error(`Run the command again with another domain than ${domain}.`);
      await addOffers(alternativesOf(domain), domain);
      continue;
    }
    // a no to a domain that a flag named has no other answer; a person picks again
    if (input.domain) throw new Error(`Run the command again with another domain than ${domain}.`);
  }
}

/** What Cloudflare says of one registration while it runs, and at its end. */
const Registration = z.object({
  state: z.string(),
  error: z.object({ message: z.string().optional() }).optional(),
  context: z.object({ action: z.unknown().optional() }).optional(),
});

/** BUYS THE DOMAIN at Cloudflare's registrar, for the price that the person said yes to
 *  (`pickDomain`). Cloudflare charges the account's default payment method and registers the
 *  domain to the account's default contact. Both are set on Cloudflare's own pages alone, and no
 *  call reads the contact. An account that has registered a domain has one, so a person whose
 *  account has none is sent to the page first. A purchase that Cloudflare refuses says why and
 *  opens the page where that is set. Cloudflare takes one registration for a domain, so a second
 *  try never buys twice. */
async function buyDomain(cf: Cloudflare, account: string, domain: string, offer: Offer) {
  const registrations = `/accounts/${account}/registrar/registrations`;
  const contactPage = `https://dash.cloudflare.com/${account}/domains/registrations`;
  const earlier = resultOf(z.array(z.unknown()), await cf("GET", `${registrations}?per_page=1`));
  if (earlier?.length === 0 && interactive()) {
    await page(
      "Cloudflare has no contact details for this account yet, and a domain is registered to a person. Add yours and accept the registration agreement on this page.",
      contactPage,
    );
    const saved = await ask<boolean>("add your contact details on that page", () =>
      p.confirm({ message: "Are your contact details saved there?" }),
    );
    if (!saved)
      throw new Error(
        `Nothing was bought. Add your contact details at ${contactPage}, then run the command again.`,
      );
  }
  for (;;) {
    const buying = wait(`Buying ${domain} for ${priceOf(offer)}`);
    let answer = await cf("POST", registrations, { domain_name: domain });
    let registration = resultOf(Registration, answer);
    // Cloudflare answers 202 when the registry takes longer than its own wait: ask for five minutes
    for (
      let asked = 0;
      asked < 100 && ["pending", "in_progress", "blocked"].includes(registration?.state || "");
      asked++
    ) {
      await sleep(3000);
      answer = await cf("GET", `${registrations}/${domain}/registration-status`);
      registration = resultOf(Registration, answer) || registration;
    }
    // the registration itself is the proof, whatever the answers above said
    if (
      registration?.state === "succeeded" ||
      (await cf("GET", `${registrations}/${domain}`)).status === 200
    ) {
      buying.done(`Bought ${domain} for ${priceOf(offer)}.`);
      return;
    }
    // Cloudflare took the purchase and has not ended it: nothing here may call it failed, or
    // buy again. The wait for the domain in the account goes on.
    if (["pending", "in_progress", "blocked"].includes(registration?.state || "")) {
      buying.done(`Cloudflare took the purchase of ${domain} and still registers it.`);
      return;
    }
    const why =
      registration?.error?.message ||
      (registration?.context?.action
        ? `it needs an action of yours (${JSON.stringify(registration.context.action)})`
        : `${answer.status}: ${answer.error}`);
    buying.fail(`Cloudflare did not sell ${domain}: ${why}`);
    const fixPage = /payment|billing|card|quote/i.test(why)
      ? `https://dash.cloudflare.com/${account}/billing/payment-info`
      : contactPage;
    if (!interactive())
      throw new Error(
        `Cloudflare did not sell ${domain}: ${why}. Nothing was bought. A payment method is set at https://dash.cloudflare.com/${account}/billing/payment-info, and the registrant's contact and the registration agreement at ${contactPage}. Then run the command again.`,
      );
    await page(
      "Set what Cloudflare asks for on this page: a payment method, your contact details, or the registration agreement.",
      fixPage,
    );
    const again = await ask<boolean>("run the command again", () =>
      p.confirm({ message: `Try to buy ${domain} again?` }),
    );
    if (!again) throw new Error("Nothing was bought. Run the command again with another domain.");
  }
}

/** The zone of the picked domain, active. When the account does not have it yet, the command buys
 *  it, or the person adds it on Cloudflare's own page (the command holds no right to add a zone),
 *  and this waits for it. A domain bought at Cloudflare becomes a zone of the account by
 *  itself. */
export async function zoneOf(
  cf: Cloudflare,
  account: string,
  pick: Pick,
  {
    say = (line: string) => p.log.info(line),
    pause = sleep,
  }: { say?: (line: string) => void; pause?: (ms: number) => Promise<unknown> } = {},
): Promise<Zone> {
  const { domain } = pick;
  const found = async (): Promise<Zone | undefined> =>
    resultOf(z.array(ZoneRow), await cf("GET", `/zones?name=${domain}&account.id=${account}`))?.[0];
  let zone = pick.zone ?? (await found());
  if (!zone) {
    if (pick.offer) await buyDomain(cf, account, domain, pick.offer);
    else
      await page(
        `Add ${domain} to this account on Cloudflare's page, then set its nameservers at your registrar to the two that Cloudflare shows.`,
        `https://dash.cloudflare.com/${account}/add-site`,
      );
    // a purchase takes minutes; nameservers at another registrar can take a day
    zone = await until(
      `Waiting for ${domain} in the account`,
      found,
      pick.offer ? 60 : 24 * 60,
      pause,
    );
  }
  if (zone.status === "active") return zone;
  let printedNameservers = false;
  const showNameservers = (target: Zone) => {
    if (printedNameservers || pick.offer || !target.name_servers?.length) return;
    printedNameservers = true;
    say(
      `Set ${domain}'s nameservers at your registrar to:\n  ${target.name_servers.join("\n  ")}\nTurn DNSSEC off at your current registrar before changing nameservers.`,
    );
  };
  showNameservers(zone);
  const { id } = zone;
  return until(
    `Waiting for Cloudflare to activate ${domain}`,
    async () => {
      const now = resultOf(ZoneRow, await cf("GET", `/zones/${id}`));
      if (now?.status === "active") return now;
      if (now) showNameservers(now);
      return undefined;
    },
    pick.offer ? 30 : 24 * 60,
    pause,
  );
}

/** The account, by flag or by question. A new one is named after the owner's mailbox, which
 *  `owner` answers. `covered` says whether this access reaches the account: a sign-in's approval
 *  does not cover an account that was just made, or one of the person's that they did not select
 *  on Cloudflare's page, and each needs one more approval. */
async function pickAccount(
  cf: Cloudflare,
  input: DeployInput,
  owner: () => Promise<string>,
): Promise<{ account: Account; covered: boolean; created: boolean }> {
  const NEW = "\0new";
  const { all, covered } = await accountsOf(cf);
  let chosen = input.newAccount ? NEW : input.account;
  if (!chosen && covered.length === 0)
    throw new Error("This access to Cloudflare covers no account.");
  const c = palette();
  chosen ||= await ask<string>("pass --account <id or name>, or --new-account", () =>
    p.select({
      message: "Which Cloudflare account does the mind live in?",
      options: [
        ...covered.map((account) => ({
          value: account.id,
          label: c.account(account.name),
          hint: account.id.slice(0, 8),
        })),
        {
          value: NEW,
          label: "A new account for this mind",
          hint: "one Cloudflare login can own several accounts; a new one is a clean, separate place for the mind (up to five extra)",
        },
      ],
    }),
  );
  const isCovered = (account: Account) => covered.some((candidate) => candidate.id === account.id);
  if (chosen !== NEW) {
    const account = all.find((candidate) => candidate.id === chosen || candidate.name === chosen);
    if (account) return { account, covered: isCovered(account), created: false };
    // an account beyond the first 50 is not in the list: its id still names it
    const byId = /^[0-9a-f]{32}$/.test(chosen)
      ? resultOf(Account, await cf("GET", `/accounts/${chosen}`))
      : undefined;
    if (byId) return { account: byId, covered: true, created: false };
    throw new Error(
      `You have no Cloudflare account ${JSON.stringify(chosen)}. This access covers: ${covered.map((a) => `${c.account(a.name)} (${a.id})`).join(", ") || "none"}.`,
    );
  }
  // an earlier run made it: a second run makes no second account
  const fresh = `iterate-${(await owner()).split("@")[0]!.replace(/[^a-z0-9]+/g, "-")}`;
  const before = all.find((candidate) => candidate.name === fresh);
  if (before) return { account: before, covered: isCovered(before), created: false };
  const made = await cf("POST", "/accounts", { name: fresh, standalone: true });
  const account = resultOf(Account, made);
  if (!account)
    throw new Error(
      `Cloudflare made no new account (${made.status}): ${made.error}. Its rules: the login is at least seven days old, it is Super Administrator of an account, and it has fewer than five extra accounts. Run again with an account you have.`,
    );
  p.log.success(`Made the Cloudflare account ${c.account(fresh)}.`);
  // a sign-in's approval names the accounts it covers, so not this one; a token for all of the
  // person's accounts reaches it at once
  return {
    account,
    covered: (await cf("GET", `/accounts/${account.id}`)).status === 200,
    created: true,
  };
}

/** THE MIND THAT IS DEPLOYED AS THE STAGE `stageName` OF THE ACCOUNT, with its own config, or
 *  undefined when the account has no such mind. The config is read from the boundary Worker's
 *  settings (`mindConfigIn`): a mind's account holds what the mind is, so no file of an earlier
 *  deploy is needed. `iterate recover` finds a mind the same way. */
export async function deployedMind(cf: Cloudflare, account: string, stageName: string) {
  const boundary = `iterate-${stageName}-boundary`;
  const settings = await cf("GET", `/accounts/${account}/workers/scripts/${boundary}/settings`);
  if (settings.status === 404) return undefined;
  // A boundary Worker with no variable `ITERATE` is one of iterate's earlier stack, which kept a
  // mind's config elsewhere: this command neither deploys over it nor erases its domain.
  if (
    settings.status === 200 &&
    !WorkerSettings.safeParse(settings.result).data?.bindings.some(
      (binding) => binding.name === "ITERATE",
    )
  )
    throw new Error(
      `The mind whose boundary Worker is ${boundary} runs iterate's earlier stack, and this command works on a mind of the new stack alone. It does not move a mind from the earlier stack yet: iterate moves it. Nothing was changed.`,
    );
  const config = mindConfigIn(settings.result);
  if (!config)
    throw new Error(
      `Cloudflare did not answer with the config of the mind whose boundary Worker is ${boundary} (${settings.status}: ${settings.error || "settings with no variable ITERATE"}). Nothing was changed.`,
    );
  return { ...config, stageName };
}

/** THE STACK'S OTHER WORKERS, by which a state store with no mind is told apart: the script makes
 *  the state store first, and a destroy erases every other Worker and leaves the store
 *  (core/alchemy/README.md, "What a destroy leaves"). The boundary is read before (`deployedMind`). */
const otherWorkersOf = (stageName: string) =>
  ["telemetry", "auth", "secrets", "mind"].map((part) => `iterate-${stageName}-${part}`);

/** AN EARLIER DEPLOY OF THE STAGE `stageName`, for an account where `deployedMind` found no mind:
 *
 *  - no state store of the stage (`iterate-<stage>-state`): undefined, a new mind;
 *  - the store, and this computer holds the config of that stage, account and domain, with a mind
 *    id, which this command wrote to `configFile` before it deployed: `mind`, which the run goes
 *    on with. That is a deploy that stopped before its end, or a mind destroyed and deployed again
 *    from here; either way that config is the mind's, and its id is kept;
 *  - the store and no such config, with nothing else of the stage (the zone is not in use, and the
 *    account holds none of the stack's other Workers): what a destroy leaves, `leftover`, a new mind
 *    with a new identity, since nothing of the old mind is left;
 *  - the store, no config, and more of the stage: a deploy that stopped, begun on another computer.
 *    It stops with nothing changed, whatever the flags: a second identity for the same mind is a
 *    fault, and the zone's records may be that deploy's own. */
export async function earlierDeployOf(
  cf: Cloudflare,
  account: string,
  stageName: string,
  zone: { domain: string; uses: string[] },
  configFile: string,
) {
  const { domain } = zone;
  const store = `iterate-${stageName}-state`;
  const workers = [store, ...otherWorkersOf(stageName)];
  const answers = await Promise.all(
    workers.map((name) => cf("GET", `/accounts/${account}/workers/scripts/${name}/settings`)),
  );
  const unread = answers.findIndex((answer) => answer.status !== 200 && answer.status !== 404);
  if (unread >= 0)
    throw new Error(
      `Cloudflare did not answer whether the account holds the Worker ${workers[unread]} (${answers[unread]!.status}: ${answers[unread]!.error}). Nothing was changed. Run the command again.`,
    );
  if (answers[0]!.status === 404) return undefined;
  const deployConfig = parsed(
    KeptDeployConfig,
    existsSync(configFile) ? readFileSync(configFile, "utf8") : "",
  );
  // the script's input holds the mind's own config, with its zones under `cloudflare`
  const config =
    deployConfig &&
    DeployedConfig.safeParse({ ...deployConfig, zones: deployConfig.cloudflare.zones }).data;
  if (
    config?.mindId &&
    config.zones.includes(domain) &&
    deployConfig?.cloudflare.accountId === account &&
    deployConfig.alchemy.stageName === stageName
  )
    return { mind: { ...config, stageName, deployConfig } };
  const more = [
    ...workers.slice(1).filter((_, index) => answers[index + 1]!.status === 200),
    ...zone.uses,
  ];
  if (more.length === 0) return { leftover: store };
  throw new Error(
    [
      `The account holds the state store of a mind's stack at ${domain}, the Worker ${store}, and more of that stack (${more.slice(0, 5).join(", ")}${more.length > 5 ? ", and more" : ""}), and no mind answers there yet: a deploy of it stopped before its end. The config that the deploy began with, with the mind's id, is on the computer that began it (${configFile.replace(homedir(), "~")}), and this computer has none for this account. Nothing was changed. Two ways on:`,
      "- Run the command on the computer that began the deploy: it goes on with it.",
      `- Or erase the half-made stack: \`pnpm run destroy --stage ${stageName} --yes\` in core/alchemy with a config of that stage and account (iterate deploy's README, "Erase a mind"). The command then deploys a new mind there. --erase-records erases no record of a half-made stack.`,
    ].join("\n"),
  );
}

const UnfinishedDeploy = z.object({
  accountId: z.string(),
  stageName: z.string(),
  domain: z.string(),
  mindId: z.string(),
  firstDeploy: z.boolean(),
});

/** Reads `deploying.json` when this computer started a deploy of the same mind that stopped before
 *  finishing (including after the boundary Worker existed). Returns the marker when its account,
 *  stage, domain, and mind id match, otherwise `undefined`. */
export function unfinishedDeployIn(
  markerFile: string,
  expected: { accountId: string; stageName: string; domain: string; mindId?: string },
) {
  if (!expected.mindId || !existsSync(markerFile)) return undefined;
  const marker = parsed(UnfinishedDeploy, readFileSync(markerFile, "utf8"));
  if (
    marker &&
    marker.accountId === expected.accountId &&
    marker.stageName === expected.stageName &&
    marker.domain === expected.domain &&
    marker.mindId === expected.mindId
  )
    return marker;
  return undefined;
}

// ---------------------------------------------------------------------------------------------
// The mind's identity.

/** THE LINE THAT NAMES THE MIND'S IDENTITY KEY, for a first deploy, or undefined. It is printed
 *  only when the key file is on this computer, and only when this run gave the mind its id, or
 *  the mind took a record now, or its log had no record before: so a first deploy whose
 *  summary was lost prints it at the next run. */
export function identityKeyLineOf(
  keyFile: string,
  run: { gaveTheId: boolean; posted?: { taken: number; emptyBefore: boolean } },
) {
  if (!existsSync(keyFile)) return undefined;
  if (!run.gaveTheId && !run.posted?.taken && !run.posted?.emptyBefore) return undefined;
  return `Its identity key: ${keyFile.replace(homedir(), "~")}  (not used right now — don't worry about it or if you lose it)`;
}

/** THE IDENTITY OF A MIND THAT HAS NONE (./identity.ts): the one that an earlier run on this
 *  computer made for this control-plane hostname, when a first deploy stopped half way, so that
 *  the mind goes on with that id; else, and always where a destroy left the old mind's store
 *  alone (`fresh`), a new one. Its key is on this computer when this answers. */
async function identityOfNewMind(controlPlaneHostname: string, fresh: boolean) {
  const earlier = fresh ? undefined : await identityMadeFor(controlPlaneHostname, IDENTITIES);
  if (!earlier) return (await newIdentity([controlPlaneHostname], IDENTITIES)).mindId;
  p.log.info(
    `Using the identity made on ${earlier.madeAt.toISOString().slice(0, 16).replace("T", " ")} UTC for ${controlPlaneHostname}.`,
  );
  return earlier.mindId;
}

/** THE MIND'S IDENTITY IS PUBLISHED, when this computer holds its records (./identity.ts): a new
 *  mind's genesis, or what an earlier run did not post, and at every deploy the last record
 *  again, which makes the stack deliver what it still owes the mind's own log. With no records
 *  here it asks nothing and says nothing: another computer has the mind's key. Nothing else
 *  waits for the identity, so a post that fails is a line with the command that posts again,
 *  never a failed deploy. Answers what the post did, or undefined. */
async function identityPublished(mind: { mindId: string; controlPlaneHostname: string }) {
  try {
    const done = await postIdentity(mind, {
      dir: IDENTITIES,
      fetcher: (request) => fetch(request),
      pause: (ms) => sleep(ms),
      say: (line) => p.log.step(line),
    });
    if (done?.taken)
      p.log.success(
        done.published === done.here
          ? "The mind took its identity record and publishes it."
          : "The mind took its identity record. Its published log does not show it yet.",
      );
    return done;
  } catch (error) {
    p.log.warn(
      `The mind's identity is not published: ${error instanceof Error ? error.message : String(error)}. Nothing else waits for it. To post it again: npx iterate@latest identity post ${mindAddressAsTyped(mind.controlPlaneHostname)}`,
    );
    return undefined;
  }
}

// ---------------------------------------------------------------------------------------------
// The command.

/** THIS COMPUTER'S SIGN-IN TO THE MIND, ON THE WAY TO SETUP, so that `iterate` commands work
 *  right after the deploy (cli.ts `login` does the same). In the browser flow the setup page is the
 *  sign-in: the browser goes from the issuer's page (through the first passkey on a new mind,
 *  `signInPageOf`) to this computer's loopback, which sends it on to `onboarding`. The device flow
 *  prints its page instead, for a browser on another computer. A sign-in that fails ends nothing:
 *  the deploy is done, and `iterate login` signs in later.
 *
 *  `opened`: a browser was sent toward setup, so no other page is opened. `signedIn`: the session
 *  is stored. Nobody signs in when `--no-sign-in` says so, when there is no person and no coding
 *  agent to pass the page on (a log in CI), or when a redeploy finds a sign-in kept here already. */
async function signInHere(
  input: DeployInput,
  mind: {
    controlPlaneHostname: string;
    firstDeploy: boolean;
    setup: { url: string; prompt: string };
    onboarding: string;
  },
): Promise<{ opened: boolean; signedIn: boolean }> {
  const c = palette();
  const { controlPlaneHostname } = mind;
  const resource = mindApiUrlOf(controlPlaneHostname);
  if (input.signIn === false || (!interactive() && !isCodingAgent(process.env)))
    return { opened: false, signedIn: false };
  if (!mind.firstDeploy && (await keptSignInLives(resource)))
    return { opened: false, signedIn: true };
  const flow = signInFlow();
  let opened = false;
  let waiting: ReturnType<typeof wait> | undefined;
  try {
    const discovered = await discoveredMind(controlPlaneHostname);
    await signInToMind(discovered, resource, {
      flow,
      // the device flow's page and code, on lines of their own; the first line names the issuer
      print: (line) => {
        if (!line.startsWith("Signing in to ")) console.log(line);
      },
      through: (page) => signInPageOf(mind, page),
      then: mind.onboarding,
      waitMs: SIGN_IN_WAIT_MS,
      openBrowser: async (url) => {
        await page(mind.setup.prompt, url.href);
        opened = true;
        waiting = wait("Waiting for you in the browser");
      },
    });
    waiting?.done();
    p.log.success(`This computer is signed in to ${c.domain(controlPlaneHostname)}.`);
    // a computer with no mind yet takes this one; one with another mind already keeps it
    const minds = Object.values(readConfigFile().configs || {}).map((profile) => profile.mind);
    if (!minds.some(Boolean)) setConfigMind(DEFAULT_CONFIG_NAME, controlPlaneHostname);
    else if (!minds.includes(controlPlaneHostname))
      p.log.info(
        `Your iterate commands still go to the mind they went to. Add --mind ${controlPlaneHostname} to reach this one.`,
      );
    return { opened, signedIn: true };
  } catch (error) {
    waiting?.fail();
    p.log.warn(
      `This computer is not signed in to the mind (${error instanceof Error ? error.message : String(error)}). Run ${c.callout(`iterate login ${controlPlaneHostname}`)} to sign in.`,
    );
    return { opened, signedIn: false };
  }
}

/** Whether this computer's kept sign-in for `resource` still works: the issuer renews it now (a
 *  stored token proves nothing once its grant was ended at the issuer). The renewed one is kept. */
async function keptSignInLives(resource: string) {
  return withSessionsLock(async () => {
    const kept = readSession(resource);
    if (!kept?.refreshToken) return false;
    try {
      writeSession(resource, await refreshOAuthSession({ resource, session: kept }));
      return true;
    } catch {
      return false;
    }
  });
}

/** THE MIND'S MCP SERVER IN THE CODING AGENTS ON THIS COMPUTER (./mcp-clients.ts), on a first
 *  deploy: the ones `--mcp` names, or with a person there, the ones they tick of those found
 *  (all ticked). Each signs in itself on its first use. A redeploy asks nothing:
 *  `iterate mcp <client>` adds it any time. */
async function addMcpHere(input: DeployInput, mind: { controlPlaneHostname: string }) {
  const found = mcpClientsOnPath();
  const named = input.mcp ? mcpClientsOf(input.mcp, found) : null;
  if (!named && (!interactive() || !found.length)) return;
  const chosen =
    named ||
    (await ask<McpClient[]>("", () =>
      p.multiselect<McpClient>({
        message: `Let your coding agents work in the mind? This adds its MCP server to each one you tick.`,
        options: found.map((client) => ({ value: client, label: MCP_CLIENTS[client].title })),
        initialValues: found,
        required: false,
      }),
    ));
  const url = mindMcpUrlOf(mind.controlPlaneHostname);
  const name = mindMcpNameOf(mind.controlPlaneHostname);
  const added: McpClient[] = [];
  for (const client of chosen) {
    const result = addMcpServer(client, url);
    if ("failed" in result)
      p.log.warn(`${MCP_CLIENTS[client].title} did not take the MCP server: ${result.failed}`);
    // another entry of that name, which may be another mind's, stays: this mind's is not added
    else if ("kept" in result)
      p.log.warn(
        `${MCP_CLIENTS[client].title} has another server named ${name} already, and keeps it. To put this mind's in its place: ${MCP_CLIENTS[client].remove(name)}, then iterate mcp ${client} --exec --mind ${mind.controlPlaneHostname}`,
      );
    else added.push(client);
  }
  if (!added.length) return;
  const titles = added.map((client) => MCP_CLIENTS[client].title);
  const who =
    titles.length > 1
      ? `${titles.slice(0, -1).join(", ")} and ${titles.at(-1)} have`
      : `${titles[0]} has`;
  p.log.success(
    `${who} the mind's MCP server as ${name}. Sign each one in once:\n${added.map((client) => `${MCP_CLIENTS[client].title}: ${MCP_CLIENTS[client].signIn(name)}`).join("\n")}`,
  );
}

/** Minimum Node version for `iterate deploy`: Node 24.7+ (ML-DSA-65 identity keys + modern TLS/fetch
 *  so Cloudflare OAuth does not reject older runtimes with HTTP 403 / fetch failed). */
export const MIN_DEPLOY_NODE = [24, 7] as const;

/** Throws a clear upgrade error before opening a browser or calling Cloudflare when running on an
 *  unsupported Node version (< 24.7). */
export function assertSupportedNodeVersion(version = process.versions.node): void {
  const match = /^v?(\d+)\.(\d+)/.exec(version.trim());
  const major = match ? Number(match[1]) : 0;
  const minor = match ? Number(match[2]) : 0;
  const [minMajor, minMinor] = MIN_DEPLOY_NODE;
  if (major > minMajor || (major === minMajor && minor >= minMinor)) return;
  throw new Error(
    `iterate deploy requires Node ${minMajor}.${minMinor} or newer (you are running Node v${version}). Older Node versions can fail Cloudflare sign-in (HTTP 403 / fetch failed) and lack ML-DSA-65 key support. Upgrade Node (e.g. brew install node, nvm install 24, fnm install 24, or https://nodejs.org) and run again.`,
  );
}

/** The owner email patterns of a deploy's config, or none: of one this run made, or of the one
 *  that a mind's boundary keeps, where every field is as it was written. */
const patternsOf = (config: unknown) =>
  z.object({ ownerEmailPatterns: z.array(z.string()) }).safeParse(config).data
    ?.ownerEmailPatterns || [];

/** WHY A SOURCE CANNOT DEPLOY A CONFIG THAT HAS OWNER EMAIL PATTERNS, or null: `scriptConfig` is
 *  the text of the source's core/alchemy/src/config.ts, and one from before the patterns names
 *  no `ownerEmailPatterns` and refuses the field. Said here, before the script runs, in words
 *  that name the two ways on. The patterns are never dropped to make an older source deploy:
 *  that would end every owner by a pattern with no word of it. */
export const patternsRefusalOf = (scriptConfig: string, config: unknown) =>
  patternsOf(config).length > 0 && !scriptConfig.includes("ownerEmailPatterns")
    ? `This mind has owner email patterns (${patternsOf(config).join(", ")}), and the source to deploy is a version of iterate from before them: it would refuse the config. The mind was not deployed. Deploy a source that has them (--source or --ref), or pass --owner-email-patterns none to take the patterns away, which ends every owner by a pattern.`
    : null;

/** Parses optional comma- or space-separated additional owner email addresses into a clean,
 *  deduplicated lowercase list. */
export function parseExtraOwnerEmails(raw: string): string[] {
  return [
    ...new Set(
      raw
        .split(/[\s,]+/)
        .map((part) => part.trim().toLowerCase())
        .filter(Boolean),
    ),
  ];
}

export async function deployMind(input: DeployInput) {
  const began = Date.now();
  const children = new Set<ChildProcess>();
  const c = palette();
  // 𝑖 is U+1D456, the letter of iterate's logo, printed alone on the very first line
  console.log(c.mark("𝑖"));
  p.intro(`${c.callout("iterate deploy")}: a mind in your own Cloudflare account`);

  try {
    if (process.platform === "win32")
      throw new Error("iterate deploy runs on macOS and Linux (on Windows: in WSL).");
    assertSupportedNodeVersion();
    // a wrong `--mcp` stops before anything changes, not after the deploy
    if (input.mcp) mcpClientsOf(input.mcp, []);
    // A kernel that a flag names comes while the person signs in and chooses. A mind's own
    // kernel repo can be read only once the mind is known, further down.
    let sourceBegan = Date.now();
    const noOwn = async (): Promise<never> => {
      throw new Error("no mind is known yet, so none has a kernel repo to read");
    };
    const early =
      input.source || input.kernelRepo
        ? sourceOf(kernelPlanOf(input, { deployed: false, begunWith: null }), noOwn)
        : undefined;
    early?.catch(() => {});
    const signIn = (fresh = false) =>
      cloudflareAccess(
        DEPLOY_SCOPES,
        async ({ url, code, minutes }) => {
          await page(
            `Sign in at Cloudflare and approve the access. Check that the page shows the code ${c.code(code)}. Select the account for the mind. The code works for ${minutes} minutes.`,
            url.href,
          );
        },
        fresh,
        // a kept sign-in (`iterate cloudflare sign-in`) is a sign-in too: the same checks
        process.env[SIGN_IN_FILE]?.trim() || undefined,
        // asked for and not needed: whether the account has a payment card (`paymentCardOf`)
        [CARD_SCOPE],
      );
    if (input.newSignIn) {
      // a new sign-in is the device flow's: a token from the environment would still be used
      if (process.env.CLOUDFLARE_API_TOKEN?.trim() || process.env[SIGN_IN_FILE]?.trim())
        throw new Error(
          `--new-sign-in starts a new sign-in at Cloudflare, but CLOUDFLARE_API_TOKEN or ${SIGN_IN_FILE} is set and would be used instead. Unset it, or leave out --new-sign-in.`,
        );
      const notRevoked = await dropKeptAccess();
      if (notRevoked)
        p.log.warn(
          `Cloudflare did not revoke the earlier sign-in (${notRevoked}). It ends by itself within the hour.`,
        );
    }
    let access = await signIn(Boolean(input.newSignIn));
    // THE ACCESS ENDS WITHIN THE HOUR, and a wait for a card or a domain can be longer. So each
    // call takes the access anew: the kept one, or a new sign-in once it has too little left
    // (cloudflare-access.ts). One sign-in at a time, whatever calls wait for it.
    let renewing: Promise<typeof access> | undefined;
    const renewed = () =>
      (renewing ||= signIn().finally(() => {
        renewing = undefined;
      }));
    const cf: Cloudflare = async (method, path, body) => {
      access = await renewed().catch((error: unknown) => {
        throw new Stop(error instanceof Error ? error.message : String(error), { cause: error });
      });
      return cloudflare(access.token)(method, path, body);
    };

    // A sign-in that lacks a required scope cannot deploy: end the access and tell the user to
    // re-run and approve all requested permissions.
    if (access.lacks.length > 0) {
      const why = `The sign-in at Cloudflare did not grant ${access.lacks.join(", ")}, and a deploy needs it.`;
      await endAccess(access);
      if (access.keptIn)
        throw new SignInEnded(
          `${why} Nothing was changed. Sign out of the kept sign-in in ${access.keptIn} (iterate cloudflare sign-out), and sign in again.`,
        );
      throw new Error(
        `${why} Nothing was changed. Run the command again and grant all requested permissions.`,
      );
    }

    const user = await cf("GET", "/user");
    const said = resultOf(
      z.object({
        email: z.string(),
        first_name: z.string().nullish(),
        last_name: z.string().nullish(),
      }),
      user,
    );
    const login = said?.email.toLowerCase();
    // an account's own API token reads no user: the owner then comes by the flag
    // a refusal ends a kept sign-in; any other answer (an outage, a rate limit) leaves it as it is
    if ((user.status === 401 || user.status === 403) && access.keptIn)
      throw new SignInEnded(
        `Cloudflare refused the kept sign-in in ${access.keptIn} (${user.status}). Sign in again: iterate cloudflare sign-in.`,
      );
    if (user.status !== 200 && access.keptIn)
      throw new Error(
        `Cloudflare did not answer who the kept sign-in in ${access.keptIn} is (${user.status}: ${user.error}). The file is as it was. Run the command again later.`,
      );
    if (user.status !== 200 && access.from === "sign-in") {
      await endAccess(access);
      throw new Error(
        `Cloudflare refused the kept access (${user.status}). Run the command again to sign in.`,
      );
    }
    const signedIn = `Signed in to Cloudflare${login ? ` as ${login}` : ""}.`;
    p.log.info(
      access.from === "sign-in" && !access.keptIn
        ? `${signedIn}\nWrong Cloudflare login? Run again with --new-sign-in.`
        : signedIn,
    );
    // THE OWNER OF A NEW MIND: `--owner`, else the Cloudflare login, else a question. Asked for
    // only where it is needed: a mind that is there already keeps its owners.
    let asked: string | undefined;
    const newOwner = async () =>
      input.owner?.toLowerCase() ||
      login ||
      (asked ||= (
        await ask<string>("pass --owner <email>", () =>
          p.text({
            message: "The owner's email address",
            validate: (value) =>
              /^\S+@\S+\.\S+$/.test(value || "") ? undefined : "An email address",
          }),
        )
      ).toLowerCase());

    const { account, covered, created } = await pickAccount(cf, input, newOwner);
    // An approval covers the accounts that the person selected on Cloudflare's page: a new
    // account, or one they did not select, takes one more approval.
    if (!covered) {
      if (access.from !== "sign-in")
        throw new Error(
          `The token in CLOUDFLARE_API_TOKEN does not reach the account ${c.account(account.name)} (${account.id}).`,
        );
      // nobody may be at Cloudflare's page: the kept sign-in is the whole approval
      if (access.keptIn)
        throw new SignInEnded(
          `The kept sign-in in ${access.keptIn} does not cover the account ${c.account(account.name)} (${account.id}). Sign in again and select that account on Cloudflare's page: iterate cloudflare sign-in.`,
        );
      await endAccess(access);
      p.log.info(
        `Cloudflare asks once more, for the account ${c.account(account.name)}: select it on its page.`,
      );
      access = await signIn(true);
      if ((await cf("GET", `/accounts/${account.id}`)).status !== 200)
        throw new Error(
          `The approval does not cover the account ${c.account(account.name)}. Run the command again and select it on Cloudflare's page.`,
        );
    }
    // `productRead`: on, off with Cloudflare's words, or a stop when the read itself failed
    const product = (what: string, path: string) =>
      productRead(
        what,
        () => cf("GET", `/accounts/${account.id}${path}`),
        () => sleep(2000),
      );
    const paidPath = "/containers/applications";
    const r2Path = "/r2/buckets?per_page=1";
    const zeroTrustPath = `/accounts/${account.id}/access/organizations`;
    // a read that fails is "unknown", as a refusal is: the card stops no deploy by itself
    const cardRead = () =>
      cf("GET", `/accounts/${account.id}/ai-gateway/billing/credit-balance`).then(
        paymentCardOf,
        (error: unknown): PaymentCard => {
          if (error instanceof Stop) throw error;
          return "unknown";
        },
      );
    const [paidRead, r2Read, zones, subdomain, zeroTrustRead, card] = await Promise.all([
      product("Workers Paid", paidPath),
      product("R2", r2Path),
      zonesOf(cf, account.id),
      cf("GET", `/accounts/${account.id}/workers/subdomain`),
      cf("GET", zeroTrustPath),
      cardRead(),
    ]);
    const { on: paid } = paidRead;
    const { on: r2 } = r2Read;
    let zeroTrust = zeroTrustStep(account.id, zeroTrustRead, false);
    if (zeroTrust.next === "stop") throw new Error(zeroTrust.words);
    const state = (read: typeof paidRead) => (read.on ? "on" : `off (Cloudflare: ${read.why})`);
    const cardWords = { there: "on file", none: "none", unknown: "not known" }[card];
    p.log.info(
      `Account ${c.account(account.name)}: payment card ${cardWords}, Workers Paid ${state(paidRead)}, R2 ${state(r2Read)}, ${zones.length} domain${zones.length === 1 ? "" : "s"}.`,
    );
    // THE PAYMENT CARD FIRST (`paymentCardStep`): before the domain, which may be a purchase, and
    // before Workers Paid. Nothing was changed yet, but for an account that this run made.
    const cardStep = input.plan
      ? undefined
      : paymentCardStep({
          account: account.id,
          card,
          paid,
          created,
          args: process.argv.slice(2),
        });
    if (cardStep) {
      if (!interactive())
        throw new Error(`${cardStep.words}\n${cardStep.page}\nNothing is deployed.`);
      await page(cardStep.words, cardStep.page);
      // Workers Paid that comes on ends the wait too: Cloudflare sold it, so it has a card
      await until(
        "Waiting for the payment card",
        async () =>
          (await cardRead()) === "there" || (await product("Workers Paid", paidPath)).on,
      );
      p.log.success("The account has a payment card.");
    }

    // the names offered come from the person's name at Cloudflare, else from the owner's address
    const person = input.domain
      ? ""
      : [said?.first_name, said?.last_name].filter(Boolean).join(" ") ||
        (await newOwner())
          .split("@")[0]!
          .split("+")[0]!
          .replace(/[^a-z0-9]+/g, " ");
    const pick = await pickDomain(cf, account.id, input, zones, person);
    const { domain } = pick;
    if (pick.zone?.unread)
      throw new Error(
        `Cloudflare did not answer with the Workers routes and DNS records of ${domain}, so nothing says what lives there already. Nothing was changed. Run the command again.`,
      );
    // A MIND THAT IS THERE ALREADY: the zone's routes name its boundary Worker, and that Worker's
    // settings hold the mind's own config. The command deploys over it with what the mind has,
    // but for what a flag names.
    const stageName = pick.zone?.stageName || stageNameOf(domain);
    const work = join(CACHE, "deploys", stageName);
    const configFile = join(work, "config.json");
    const markerFile = join(work, "deploying.json");
    const deployed = await deployedMind(cf, account.id, stageName);
    if (deployed && !deployed.zones.includes(domain))
      throw new Error(
        `The account has a mind at ${deployed.controlPlaneHostname} whose stack is named ${stageName}, the name that a mind at ${domain} gets, and ${domain} is none of its domains (${deployed.zones.join(", ")}). Choose another domain.`,
      );
    if (deployed && !deployed.deployConfig)
      throw new Error(
        `The mind at ${deployed.controlPlaneHostname} was deployed before its stack kept the deploy's whole config, so a deploy from here could drop what no Worker holds, such as its sandboxes' image. Nothing was changed. Deploy it once with its own config by core/alchemy's script (core/alchemy/README.md), then run the command again.`,
      );
    const resuming = deployed
      ? unfinishedDeployIn(markerFile, {
          accountId: account.id,
          stageName,
          domain,
          mindId: deployed.mindId,
        })
      : undefined;
    // AN EARLIER DEPLOY (`earlierDeployOf`): one whose config is here goes on with that config, its
    // mind's id among it, but for what a flag names; a destroy's leftover store is a new mind
    const earlier = deployed
      ? undefined
      : await earlierDeployOf(
          cf,
          account.id,
          stageName,
          { domain, uses: pick.zone?.uses || [] },
          configFile,
        );
    const unfinished = earlier?.mind;
    const mind = deployed || unfinished;
    const primaryOwner = mind ? input.owner : await newOwner();
    const extraOwners =
      !mind && !input.owner && interactive() && primaryOwner
        ? parseExtraOwnerEmails(
            await ask<string>("", () =>
              p.text({
                message: `Add additional owner email addresses beside ${primaryOwner}? (comma-separated, or press Enter to skip)`,
                placeholder: "e.g. you@personal.com",
                defaultValue: "",
                validate: (value) => {
                  const trimmed = (value || "").trim();
                  if (!trimmed) return undefined;
                  const bad = parseExtraOwnerEmails(trimmed).find(
                    (email) => !/^\S+@\S+\.\S+$/.test(email),
                  );
                  return bad ? `${bad} is not a valid email address` : undefined;
                },
              }),
            ),
          )
        : [];
    const config = deployConfigOf({
      accountId: account.id,
      domain,
      owner: primaryOwner,
      extraOwners,
      controlPlaneHostname: input.controlPlaneHostname,
      egressGateway: input.egressGateway,
      ownerEmailPatterns: input.ownerEmailPatterns,
      mcpTarget: input.mcpTarget,
      mailForwardTo: input.mailForwardTo,
      traceSampling: input.traceSampling,
      ...birthReposOf(input),
      zones: input.zones,
      deployed: mind,
    });
    // WHICH KERNEL THIS DEPLOY BUILDS (`kernelPlanOf`): the flags of a birth are refused over a
    // mind that is there, and with no flag over such a mind it is the mind's own kernel repo
    const plan = kernelPlanOf(input, {
      deployed: Boolean(deployed),
      begunWith: kernelAddressIn(config.repos),
    });
    if (!early) sourceBegan = Date.now();
    const source =
      early || sourceOf(plan, () => ownKernelRepo(cf, account.id, stageName));
    source.catch(() => {});
    const { controlPlaneHostname } = config;
    // A PLAN CHANGES NOTHING: only over a mind that is there, whose account needs no step that
    // writes (each one below is skipped when this holds), and that has its id
    if (input.plan) {
      const missing = [
        ...(deployed ? [] : [`no mind is at ${domain}`]),
        ...(config.mindId ? [] : ["the mind has no id"]),
        ...(paid ? [] : ["Workers Paid is off"]),
        ...(r2 ? [] : ["R2 is off"]),
        ...(zeroTrust.next === "go" ? [] : ["Zero Trust is not set up"]),
        ...(resultOf(z.object({ subdomain: z.string().min(1) }), subdomain)
          ? []
          : ["the account has no workers.dev name"]),
      ];
      if (missing.length > 0)
        throw new Error(
          `--plan reads a mind that is there and changes nothing, and here ${missing.join(", ")}. A deploy comes first. Nothing was changed.`,
        );
    }
    // A new mind needs an ML-DSA-65 identity key: check before mutating the account or buying a domain.
    if (!config.mindId) await mlDsaRequired();
    // A new mind clones its repos at birth, with no credential: one that does not answer would
    // leave a mind with no code, so ask each now.
    if (!deployed) {
      const refused = await birthReposRefused(config.repos, join(CACHE, "config"));
      if (refused.length > 0)
        throw new Error(
          `A new mind clones its repos with no credential, and ${refused.length === 1 ? "this one" : "these"} did not answer: ${refused.join("; ")}. Nothing was changed. --kernel-repo and --config-repo name repos that anyone may read.`,
        );
    }
    if (deployed) {
      p.log.info(
        [
          `A mind is at ${domain} already. Its own config, from Cloudflare:`,
          `  control plane   ${deployed.controlPlaneHostname}`,
          `  owners          ${[...deployed.ownerEmails, ...patternsOf(deployed.deployConfig)].join(", ")}`,
          `  domains         ${deployed.zones.join(", ")}`,
          `  the mind runs   ${deployed.topology === "full-cloudflare" ? "in Cloudflare" : "elsewhere, and connects in"}`,
        ].join("\n"),
      );
      const patternsBefore = patternsOf(deployed.deployConfig).join(", ");
      const patternsAfter = patternsOf(config).join(", ");
      const changes = [
        ...(controlPlaneHostname === deployed.controlPlaneHostname
          ? []
          : [`its control plane at ${controlPlaneHostname}`]),
        ...(config.ownerEmails.join() === deployed.ownerEmails.join()
          ? []
          : [`${config.ownerEmails.join(", ")} as its only owner`]),
        ...(patternsBefore === patternsAfter
          ? []
          : [
              patternsAfter
                ? `EVERY address at ${patternsAfter} as an owner`
                : `no owner by a pattern (was ${patternsBefore})`,
            ]),
        // a zone that joins gets its records, routes and mail; one that leaves loses them
        ...config.cloudflare.zones
          .filter((zone) => !deployed.zones.includes(zone))
          .map((zone) => `${zone} as a domain of the mind, whole`),
        ...deployed.zones
          .filter((zone) => !config.cloudflare.zones.includes(zone))
          .map((zone) => `${zone} no longer its domain, its records, routes and mail deleted`),
      ];
      const forwardBefore = forwardOf(deployed.deployConfig?.mail);
      const forwardAfter = forwardOf(config.mail);
      const forwardWords = forwardAfter
        ? `${forwardAfter === forwardBefore ? "keeps forwarding" : "forwards"} each mail to ${forwardAfter}`
        : forwardBefore && `stops forwarding mail (was ${forwardBefore})`;
      const how = `${
        changes.length > 0 ? `with ${changes.join(" and ")}` : "with the same owners and hostname"
      }${forwardWords ? `; it ${forwardWords}` : ""}`;
      if (resuming && changes.length === 0 && forwardAfter === forwardBefore) {
        p.log.info(`Resuming the unfinished deploy of ${domain} (${how}).`);
      } else {
        if (
          !input.yes &&
          !(await ask<boolean>(`a mind is at ${domain} already; pass --yes to deploy over it`, () =>
            p.confirm({ message: `Deploy over it, ${how}?` }),
          ))
        )
          throw new Error("Nothing was changed.");
        p.log.info(`Deploying over it, ${how}.`);
      }
    } else if (unfinished)
      p.log.info(
        `This computer holds the config of an earlier deploy of this mind (${configFile.replace(homedir(), "~")}), and the account holds its stack's state store: going on with that config. Its owners: ${config.ownerEmails.join(", ")}; its control plane: ${controlPlaneHostname}.`,
      );
    else if (earlier?.leftover)
      p.log.info(
        `The account holds ${earlier.leftover}, the state store that a destroy of a mind at ${domain} leaves, and nothing else of that mind: deploying a new mind there, with a new identity. Its owner is ${config.ownerEmails.join(", ")}.`,
      );
    else p.log.info(`The mind's owner is ${config.ownerEmails.join(", ")}.`);
    // A ZONE IN USE: the person says yes to losing everything on it before anything changes. The
    // erase itself is the last thing before the deploy (below). Asked here for a zone that the
    // account has, and after the wait for a zone that arrives during the run: Cloudflare copies
    // the records it finds when a domain is added.
    // The domain of a mind, or of an earlier deploy whose config is here, is never erased.
    const holdingsToErase = async (uses: string[], zoneId: string) => {
      if (mind || uses.length === 0) return undefined;
      const holdings = await holdingsOf(cf, zoneId);
      if (!holdings)
        throw new Error(
          `Cloudflare did not answer with the records of ${domain}. Nothing was changed. Run the command again.`,
        );
      const what = `${holdings.records.length} DNS record${holdings.records.length === 1 ? "" : "s"} and ${holdings.routes.length} Workers route${holdings.routes.length === 1 ? "" : "s"}`;
      const some = `${uses.slice(0, 5).join(", ")}${uses.length > 5 ? ", and more" : ""}`;
      const erase =
        input.eraseRecords ||
        (await ask<boolean>(
          `${domain} is in use (${some}); pass --erase-records to erase its ${what}`,
          () =>
            p.confirm({
              message: `${domain} is in use: ${some}. A mind takes the whole domain. I will erase all ${what} there: its site and its mail stop working. Go on?`,
              initialValue: false,
            }),
        ));
      if (!erase)
        throw new Error("Nothing was changed. Run the command again with another domain.");
      return holdings;
    };
    let holdings = pick.zone
      ? await holdingsToErase(pick.zone.uses || [], pick.zone.id)
      : undefined;

    // the account's workers.dev name: a new account has none, and a Worker needs one
    let workersSubdomain =
      resultOf(z.object({ subdomain: z.string().min(1) }), subdomain)?.subdomain ?? "";
    if (!workersSubdomain) {
      let refusal = "";
      for (const candidate of [
        `iterate-${stageName}`,
        `iterate-${stageName}-${Math.random().toString(36).slice(2, 6)}`,
      ]) {
        const put = await cf("PUT", `/accounts/${account.id}/workers/subdomain`, {
          subdomain: candidate,
        });
        refusal = put.status === 200 ? "" : `${put.status}: ${put.error}`;
        workersSubdomain = candidate;
        if (!refusal) break;
      }
      if (refusal)
        throw new Error(
          `Cloudflare gave the account no workers.dev name (${refusal}). Set one at https://dash.cloudflare.com/${account.id}/workers-and-pages and run the command again.`,
        );
    }

    const waiting = wait("Getting iterate's kernel");
    const ready = await source.catch((error: unknown) => {
      waiting.fail("iterate's kernel did not install");
      throw error;
    });
    waiting.done(`iterate's kernel is ready (${seconds(sourceBegan)}): ${ready.from}`);
    const tooOld = patternsRefusalOf(
      readFileSync(join(ready.dir, "core/alchemy/src/config.ts"), "utf8"),
      config,
    );
    if (tooOld) throw new Error(tooOld);
    mkdirSync(work, { recursive: true, mode: 0o700 });
    const log = join(work, "deploy.log");
    writeFileSync(log, "");
    // WORKERS PAID AND R2, FOR THE PERSON (`activationOffer`): only a product that is off, only
    // after its exact terms and a yes, and with an access that is made for this alone and ended
    // at once (`briefAccess`). A no, no offer, or a subscription that Cloudflare refuses leaves
    // the way by hand, as before: Cloudflare's own page, and the wait for it.
    const missing: Product[] = [
      ...(paid ? [] : ["Workers Paid" as const]),
      ...(r2 ? [] : ["R2" as const]),
    ];
    const atThePage = interactive() && access.from === "sign-in" && !access.keptIn;
    const agreed: { product: Product; terms: PlanTerms }[] = [];
    // the plan's terms in Cloudflare's catalog now, or undefined when they are not read
    const termsNow = async (plan: string) =>
      planTermsOf(
        plan,
        await cf("GET", `/billing/rate_plans/${plan}`).catch((error: unknown) => {
          if (error instanceof Stop) throw error;
          return undefined;
        }),
      );
    for (const each of atThePage ? missing : []) {
      const terms = await termsNow(PRODUCTS[each].plan);
      const offer = activationOffer({
        product: each,
        terms,
        account: account.name,
        card,
        person: atThePage,
      });
      if (!offer || !terms) continue;
      p.log.step(offer.words);
      const yes = await ask<boolean>("", () =>
        p.confirm({ message: offer.question, initialValue: false }),
      );
      if (yes) agreed.push({ product: each, terms });
    }
    const turnedOn = new Set<Product>();
    if (agreed.length > 0) {
      const names = agreed.map(
        ({ product, terms }) => `${product} (${dollars(terms.monthly)} a month)`,
      );
      const isOn = async (each: Product) =>
        (await product(each, each === "R2" ? r2Path : paidPath)).on;
      // ONE PRODUCT'S SUBSCRIPTION. It never throws: every end is an `Activation`, so one
      // product's failure loses no other's result. Read again first: the approval took a while,
      // and a product that came on in that time is never subscribed to twice; then the terms
      // again, so that the request follows, within the moment, a price that the person agreed to.
      const subscribe = async (
        token: string,
        { product: each, terms }: (typeof agreed)[number],
      ): Promise<Activation> => {
        try {
          if (await isOn(each)) return { on: true };
          if (!sameTerms(terms, await termsNow(terms.plan)))
            return {
              on: false,
              why: "Cloudflare's terms for it changed, or were not read, after your yes",
            };
        } catch (error) {
          if (error instanceof Stop) throw error;
          return { on: false, why: error instanceof Error ? error.message : String(error) };
        }
        return cloudflare(token)(
          "POST",
          `/accounts/${account.id}/subscriptions`,
          subscriptionBodyOf(terms.plan),
        ).then(activationOutcomeOf, (error: unknown) => ({
          lost: error instanceof Error ? error.message : String(error),
        }));
      };
      const made = await briefAccess(
        [BILLING_SCOPE],
        async ({ url, code, minutes }) => {
          await page(
            `Approve the billing access at Cloudflare. This command uses it for ${names.join(" and ")} alone. Check that the page shows the code ${c.code(code)}. Select the account ${c.account(account.name)}. The code works for ${minutes} minutes.`,
            url.href,
          );
        },
        async (token) => {
          const outcomes: Activation[] = [];
          for (const each of agreed) outcomes.push(await subscribe(token, each));
          return outcomes;
        },
      ).catch((error: unknown) => {
        if (error instanceof Stop) throw error;
        return error instanceof Error ? error.message : String(error);
      });
      // a sign-in that failed, or an approval without the scope: no request for a subscription
      // went out, so "nothing was bought" is a fact
      if (typeof made === "string")
        p.log.warn(`${made}\nNothing was bought. Turn it on yourself on Cloudflare's page.`);
      else if (!made.done)
        p.log.warn(
          `The approval at Cloudflare did not grant ${made.lacks.join(", ")}. Nothing was bought. Turn it on yourself on Cloudflare's page.`,
        );
      else {
        // an answer that did not arrive: Cloudflare may still have made the subscription, so
        // the account is read for up to two minutes before the person is sent to buy by hand
        const outcomes = [];
        for (const [index, outcome] of made.done.entries()) {
          const { product: each, terms } = agreed[index]!;
          const onNow =
            "lost" in outcome
              ? await until(`Asking Cloudflare whether ${each} is on`, () => isOn(each), 2).catch(
                  (error: unknown) => {
                    if (error instanceof Stop) throw error;
                    return false;
                  },
                )
              : false;
          outcomes.push({ product: each, agreed: terms, outcome, onNow });
        }
        const report = activationReport({ account: account.id, outcomes });
        for (const line of report.lines) p.log[line.kind](line.text);
        for (const { product: each } of agreed)
          if (!report.byHand.includes(each)) turnedOn.add(each);
      }
      if (typeof made !== "string" && made.notEnded)
        p.log.warn(
          `Cloudflare did not confirm the end of the billing access (${made.notEnded}). An access token ends by itself within the hour. Remove this application's access in your Cloudflare profile now.`,
        );
    }
    if (!paid) {
      if (!turnedOn.has("Workers Paid"))
        await page(workersPaidPrompt(created, card), PRODUCTS["Workers Paid"].manual(account.id));
      await until(
        "Waiting for Workers Paid",
        async () => (await product("Workers Paid", paidPath)).on,
      );
    }
    if (!r2) {
      if (!turnedOn.has("R2"))
        await page("Turn on R2: it is free at this size.", PRODUCTS.R2.manual(account.id));
      await until("Waiting for R2", async () => (await product("R2", r2Path)).on);
    }
    // ZERO TRUST (`zeroTrustStep`): set up by the command where Cloudflare allows it, else a gate
    if (zeroTrust.next === "make") {
      const made = await cf("POST", zeroTrustPath, {});
      const after = await cf("GET", zeroTrustPath);
      const set = zeroTrustStep(account.id, after, true).next === "go";
      zeroTrust = zeroTrustStep(account.id, set ? after : made, true);
      if (set)
        p.log.success(
          "Zero Trust is set up in the account, with a team name that Cloudflare chose.",
        );
    }
    if (zeroTrust.next === "stop") throw new Error(zeroTrust.words);
    if (zeroTrust.next === "gate") {
      if (!interactive())
        throw new Error(
          `${zeroTrust.words}\n${zeroTrust.page}\nThen run the command again. Nothing is deployed.`,
        );
      await page(zeroTrust.words, zeroTrust.page);
      await until(
        "Waiting for Zero Trust",
        async () => zeroTrustStep(account.id, await cf("GET", zeroTrustPath), true).next === "go",
      );
    }
    const zone = await zoneOf(cf, account.id, pick);
    p.log.success(`${c.domain(zone.name)} is active in the account.`);
    if (!pick.zone) {
      const [arrived] = await zonesOf(cf, account.id, domain);
      if (!arrived || arrived.unread)
        throw new Error(
          `Cloudflare did not answer with the Workers routes and DNS records of ${domain}, so nothing says what lives there. Nothing was changed. Run the command again.`,
        );
      // a route of a mind's boundary came with the domain: the run's checks of a mind come first
      if (arrived.stageName)
        throw new Error(
          `${domain} came with the route of a mind's boundary Worker, iterate-${arrived.stageName}-boundary. Nothing was changed. Run the command again.`,
        );
      holdings = await holdingsToErase(arrived.uses || [], zone.id);
    }

    // A NEW MIND'S IDENTITY (./identity.ts), made right before the deploy: its key and its signed
    // genesis are on this computer before the script gets the id. A mind that has an id keeps it,
    // and this run signs nothing.
    const mindId =
      config.mindId || (await identityOfNewMind(controlPlaneHostname, Boolean(earlier?.leftover)));
    const deployedConfig = { ...config, mindId };
    writeFileSync(configFile, `${JSON.stringify(deployedConfig, null, 2)}\n`);
    access = await renewed();
    // HTTP/3 on and 0-RTT off on each zone of the mind (./zone-protocols.ts): read at every run,
    // and written before the deploy, and before anything of the domain is erased
    const protocolChanges = await zoneProtocolChanges(cf, account.id, config.cloudflare.zones);
    if (input.plan) {
      for (const change of protocolChanges) p.log.info(protocolChangeLine(change, false));
      const plan = join(work, "plan.log");
      writeFileSync(plan, "");
      const planning = wait(`Planning the deploy of the mind at ${c.domain(controlPlaneHostname)}`);
      const run = {
        source: ready,
        stageName,
        config: configFile,
        token: access.token,
        log: plan,
        children,
      };
      for (const args of planArgsOf(stageName)) {
        const { code } = await deployOnce(run, args);
        if (code !== 0) {
          planning.fail(`The plan failed (exit ${code}).`);
          throw new Error(
            `The plan failed (exit ${code}). Its whole log: ${plan}. Nothing was changed.`,
          );
        }
      }
      planning.done(`The plan: ${plan}`);
      p.outro(`Nothing was changed. Done in ${seconds(began)}.`);
      return;
    }
    // before the erase: a write that Cloudflare refuses stops the run with the domain as it was
    await setZoneProtocols(cf, protocolChanges);
    for (const change of protocolChanges) p.log.success(protocolChangeLine(change, true));
    if (holdings) {
      const erased = await eraseHoldings(cf, zone.id);
      p.log.success(
        `Erased ${erased.records} DNS records and ${erased.routes} Workers routes of ${domain}.`,
      );
    }
    const deployBegan = Date.now();
    const deploying = wait(`Deploying the mind at ${c.domain(controlPlaneHostname)}`);
    const progress = createDeployProgress();
    // Keep Alchemy's verbose log in `deploy.log` only: in interactive terminals update the
    // single spinner line with concise resource progress; in non-interactive runs stay quiet
    // except for retry notices and a line each 30 seconds that says what is still being made
    // (tail of `deploy.log` is still printed on failure).
    const onDeployLine = (line: string) => {
      if (line.includes(ONE_MORE_TRY) || line.includes(WAITING_FOR_STORE)) {
        deploying.say(line);
        return;
      }
      const status = progress.onLine(line);
      if (status && interactive()) deploying.say(status);
    };
    const stillAt = setInterval(() => {
      const line = progress.stillAt();
      if (line) deploying.say(`${seconds(deployBegan)}: ${line}`);
    }, 30_000);
    writeFileSync(
      markerFile,
      `${JSON.stringify(
        {
          accountId: account.id,
          stageName,
          domain,
          mindId,
          firstDeploy: !deployed || Boolean(resuming?.firstDeploy),
        },
        null,
        2,
      )}\n`,
    );
    const { code, consumerNotSeen, storeNotServed } = await deployStack(
      {
        source: ready,
        stageName,
        storeAddress: stateStoreAddress(stageName, workersSubdomain),
        config: configFile,
        token: access.token,
        log,
        children,
        say: onDeployLine,
        adopt: Boolean(earlier || resuming),
      },
      deployOnce,
    ).finally(() => clearInterval(stillAt));
    if (code !== 0) {
      deploying.fail(`The deploy failed (exit ${code}).`);
      if (storeNotServed)
        throw new Error(
          `The mind's new state store is at ${storeNotServed}, and Cloudflare does not serve this address yet; run the command again in a few minutes. It goes on with this deploy's config. The deploy's whole log: ${log}.`,
        );
      console.log(readFileSync(log, "utf8").trimEnd().split("\n").slice(-40).join("\n"));
      throw new Error(
        `The deploy failed (exit ${code}). Its whole log: ${log}. A deploy keeps what it made: run the command again on this computer, and it goes on with this deploy's config.${consumerNotSeen ? " The queue may already have its consumer: run the command again." : ""}`,
      );
    }
    rmSync(markerFile, { force: true });
    deploying.done(`Deployed (${seconds(deployBegan)}). Full log: ${log.replace(homedir(), "~")}`);
    const boundary = `/accounts/${account.id}/workers/scripts/iterate-${stageName}-boundary/settings`;
    const checking = wait("Checking that the boundary Worker holds this deploy's config");
    const check = await boundaryCheck(() => cf("GET", boundary), deployedConfig, {
      say: checking.say,
    });
    if ("reads" in check)
      appendFileSync(
        log,
        `\nThe check's reads of the boundary Worker's settings after the deploy:\n${check.reads.join("\n")}\n`,
      );
    // A miss after the whole wait: Cloudflare was slower still, or another deploy of the mind ran
    // in between. A next run starts from the config that the boundary holds then and checks
    // again, so it mends the one and keeps the other's config. The command still ends every later
    // step (the mind's identity, its first sign-in) and then fails.
    const missed =
      "miss" in check
        ? `The deploy finished and the mind runs. But after ${duration(BOUNDARY_WAIT_MS)} of reads, its boundary Worker does not hold what this run deployed: ${check.miss}. Cloudflare may show a new Worker's settings late, or another deploy of this mind may have run since this one began. Run the command again in a few minutes: it starts from the config that the boundary Worker holds then, and checks again. If this message comes again, please report it to iterate with this message. Each read is in the deploy's log: ${log}`
        : undefined;
    if (missed) checking.fail(missed);
    else if ("unread" in check)
      checking.fail(
        `Could not read the boundary Worker's settings in ${duration(BOUNDARY_WAIT_MS)} (the last status ${check.unread}), so this run did not check that it holds the deploy's config. The deploy itself succeeded. Each read is in the deploy's log: ${log}`,
      );
    else {
      const forwardTo = forwardOf(config.mail);
      checking.done(
        forwardTo
          ? `The boundary holds this deploy's config, and forwards each mail to ${forwardTo}.`
          : "The boundary holds this deploy's config (no mail forward).",
      );
    }

    // NO PAGE IS OPENED UNTIL THE PAGES THAT THE BROWSER ASKS FIRST WORK FROM THIS COMPUTER
    // (`firstPagesOf`). A person may wait again as often as they like; with nobody there, or
    // after their no, the command still ends every later step, opens nothing and then fails.
    const isFirstDeploy = !deployed || Boolean(resuming?.firstDeploy);
    const primaryZone = config.cloudflare.zones[0] || domain;
    let setup = setupDestinationOf({
      controlPlaneHostname,
      zone: primaryZone,
      ownerEmails: config.ownerEmails,
      firstDeploy: isFirstDeploy,
    });
    const firstPages = firstPagesOf({
      controlPlaneHostname,
      zone: primaryZone,
      firstDeploy: isFirstDeploy,
    });
    const answersBegan = Date.now();
    let unanswered: string | undefined;
    for (;;) {
      const answering = wait(`Waiting for ${c.domain(controlPlaneHostname)} to answer steadily`);
      const answer = await firstPagesAnswer(firstPages, { say: answering.say });
      if ("answered" in answer) {
        answering.done(`The mind answers (${seconds(answersBegan)}).`);
        unanswered = undefined;
        break;
      }
      unanswered = `The deploy finished, but after ${seconds(answersBegan)} ${answer.fault}. A new domain's DNS records, certificate and routes can reach a computer late. Setup was not opened: open ${setup.url} once it loads in your browser, or run the command again, which deploys over the mind and checks again.`;
      answering.fail(`After ${seconds(answersBegan)}: ${answer.fault}.`);
      if (!interactive()) break;
      const again = await ask<boolean>("", () =>
        p.confirm({ message: `Wait ${duration(FIRST_PAGES_WAIT_MS)} more for it?` }),
      );
      if (!again) break;
    }

    await endAccess(access);
    const posted = await identityPublished({ mindId, controlPlaneHostname });
    p.log.success(
      deployed && !resuming?.firstDeploy
        ? `Your mind at ${c.domain(controlPlaneHostname)} is updated.`
        : `Your mind is deployed at ${c.domain(controlPlaneHostname)}.`,
    );
    p.log.info(`${c.callout("MindConfig:")}\n${formatMindConfigForStdout(deployedConfig)}`);
    const keyLine = identityKeyLineOf(keyFileOf(IDENTITIES, mindId), {
      gaveTheId: !config.mindId,
      posted,
    });
    if (keyLine) p.log.info(keyLine);
    // the pages that did not answer end the command non-zero, with setup's address and no open
    if (unanswered) throw new Error(unanswered);
    // setup goes on in the onboarding app when this mind's config has one, else in its Dash
    const onboarding = await setupHomeOf(primaryZone);
    setup = setupDestinationOf({
      controlPlaneHostname,
      zone: primaryZone,
      ownerEmails: config.ownerEmails,
      firstDeploy: isFirstDeploy,
      home: onboarding,
    });
    const here =
      input.setup === false
        ? { opened: false, signedIn: false }
        : await signInHere(input, {
            controlPlaneHostname,
            firstDeploy: isFirstDeploy,
            setup,
            onboarding,
          });
    // signed in, the owner has a passkey: setup is the onboarding page itself (or the Dash)
    const next = here.signedIn ? { ...setup, url: onboarding } : setup;
    // a browser that went toward setup already is not sent there twice
    if (!here.opened) {
      if (input.setup !== false) await page(next.prompt, next.url);
      else p.log.step(`${next.prompt}\n${c.url(next.url)}`);
    }
    if (isFirstDeploy || input.mcp) await addMcpHere(input, { controlPlaneHostname });
    // the check's miss, said above, ends the command non-zero once every step is done
    if (missed) throw new Error(missed);
    p.outro(`Done in ${seconds(began)}.`);
  } catch (error) {
    // a stop is an expected end: its one line, with no stack, and a failed exit code
    for (const child of children) child.kill();
    p.log.error(error instanceof Error ? error.message : String(error));
    p.outro(`Stopped after ${seconds(began)}.`);
    // a kept sign-in that no longer works needs a person: its own exit code says so
    const ended =
      error instanceof SignInEnded ||
      (error instanceof Error && error.cause instanceof SignInEnded);
    process.exit(ended ? SIGN_IN_ENDED_EXIT : 1);
  }
}