A deploy takes its kernel from where the new mind's repos come from
Misha Kaletsky, 2026-11-25 14:16 UTC · f2093f0f06aa8a7dd35a8c76c7cacab48677e65c · parent 5caf098
4 files changed
- M core/lib/src/cli/README.md
- M core/lib/src/cli/cli.ts
- M core/lib/src/cli/deploy.test.ts
- M core/lib/src/cli/deploy.ts
M core/lib/src/cli/README.md
@@ -138,11 +138,17 @@ - `mindId`: for a new mind, the id of the identity that the command makes right before the deploy ([The mind's identity key](#the-minds-identity-key)). A mind that is there already keeps its own. -7. **The source.** It fetches [iterate/core](https://github.com/iterate/core) into - `${XDG_CACHE_HOME:-~/.cache}/iterate/core` and installs it with pnpm (using local `pnpm` when - its major version matches `packageManager`, otherwise `npx --yes <packageManager>`), while you - sign in. In a checkout of the source it deploys that checkout. - `--source <folder>` names a checkout, `--ref <branch or commit>` another version. It needs git. +7. **The kernel.** It comes from the same place as the new mind's repos: the kernel repo under + `--repos-from`, at its `main`. The mind clones that repo at birth, so the kernel that runs is + the commit that the mind's own `/repos/kernel` holds. The command fetches it into + `${XDG_CACHE_HOME:-~/.cache}/iterate/kernel` with git, sending no credential, and installs it + with pnpm (using local `pnpm` when its major version matches `packageManager`, otherwise + `npx --yes <packageManager>`), while you sign in. `--ref <branch>` takes another branch of + that repo. + `--source <folder>` deploys a checkout on this computer instead, whatever it holds: for + someone who works on the kernel, and for the mind that hosts the source, whose + `--repos-from none` names no mind to take a kernel from. The folder the command runs in is + never the kernel by itself. A source from before the one Alchemy script is refused, before anything changes. When the source holds `core/repos/onboarding`, the command builds the mind Worker with `ITERATE_SEED_REPOS` naming it, and one line says that a new mind starts with @@ -315,7 +321,7 @@ files, repos, members and secrets are gone. ```sh -cd "${XDG_CACHE_HOME:-$HOME/.cache}/iterate/core/core/alchemy" +cd "${XDG_CACHE_HOME:-$HOME/.cache}/iterate/kernel/core/alchemy" export ITERATE_MIND_CONFIG="${XDG_CACHE_HOME:-$HOME/.cache}/iterate/deploys/<stage>/config.json" export CLOUDFLARE_API_TOKEN=… # a token of the mind's account, or: pnpm exec alchemy login pnpm run destroy --stage <stage> --yes
M core/lib/src/cli/cli.ts
@@ -257,7 +257,7 @@ .string() .optional() .describe( - "Where a new mind's repos are cloned from: the address under which a mind serves its repos (https://git.<its domain>/repos); none starts the mind with empty repos, which get their code by pushes (default: iterate's own repos; a mind that is there already keeps the repos it has)", + "Where a new mind's repos are cloned from: the address under which a mind serves its repos (https://git.<its domain>/repos); none starts the mind with empty repos, which get their code by pushes, and needs --source. The kernel that is deployed comes from the kernel repo there too (default: iterate's own repos; a mind that is there already keeps the repos it has)", ), traceSampling: z .string() @@ -285,9 +285,12 @@ .string() .optional() .describe( - "A checkout of iterate's core to deploy from (default: the one this runs in, else iterate/core's main)", + "A checkout of iterate's kernel on this computer to deploy, whatever it holds (default: none; the kernel is fetched from the kernel repo under --repos-from)", ), - ref: z.string().optional().describe("A branch or commit of iterate/core to deploy"), + ref: z + .string() + .optional() + .describe("A branch of that kernel repo to deploy (default: main)"), plan: z .boolean() .optional()
M core/lib/src/cli/deploy.test.ts
@@ -1,4 +1,5 @@ -import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import { execFileSync } from "node:child_process"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { expect, test } from "vitest"; @@ -21,6 +22,7 @@ earlierDeployOf, ENTER_NEW_DOMAIN, erateOf, + fetchedKernel, FIRST_PAGES_WAIT_MS, firstPagesAnswer, firstPagesOf, @@ -28,6 +30,7 @@ identityKeyLineOf, isColorEnabled, isInteractiveEnv, + kernelOriginOf, mindConfigIn, onboardingUrlOf, parseExtraOwnerEmails, @@ -165,6 +168,34 @@ expect(() => reposOf("somewhere")).toThrow("--repos-from somewhere is no address"); }); +test("a deploy's kernel comes from where the new mind's repos come from: the kernel repo under --repos-from, iterate's own with no flag, and nowhere with none", () => { + expect(kernelOriginOf("https://git.ada-labs.com/repos/")).toBe( + "https://git.ada-labs.com/repos/kernel", + ); + expect(kernelOriginOf(undefined)).toBe("https://git.mindlew.com/repos/kernel"); + // the mind that hosts the source has no mind to take a kernel from: it is deployed with --source + expect(kernelOriginOf("none")).toBeNull(); + expect(() => kernelOriginOf("somewhere")).toThrow("--repos-from somewhere is no address"); +}); + +test("the kernel is fetched at the branch asked for into a folder that is kept, and the deploy learns which commit it holds", async () => { + using upstream = kernelRepo(); + const main = upstream.commit("core/alchemy/alchemy.run.ts", "// the script on main"); + upstream.git("switch", "-q", "-c", "next"); + const next = upstream.commit("core/alchemy/alchemy.run.ts", "// the script on next"); + const dir = join(mkdtempSync(join(tmpdir(), "deploy-")), "kernel"); + const script = () => readFileSync(join(dir, "core/alchemy/alchemy.run.ts"), "utf8"); + + expect(await fetchedKernel({ origin: upstream.url, ref: "main", dir })).toBe(main); + expect(script()).toBe("// the script on main"); + // the same folder takes the next kernel asked for + expect(await fetchedKernel({ origin: upstream.url, ref: "next", dir })).toBe(next); + expect(script()).toBe("// the script on next"); + await expect(fetchedKernel({ origin: upstream.url, ref: "gone", dir })).rejects.toThrow( + `The kernel could not be fetched from ${upstream.url} at gone`, + ); +}); + const deployed = { stageName: "ada", mindId: "cd".repeat(32), @@ -1432,6 +1463,30 @@ }; const ADDRESS = stateStoreAddress("blunfin-com", "iterate-dev-preview"); + +/** A mind's kernel repo, as git itself serves it from a folder: `commit` writes one file and + * answers the new commit, and `url` is what a deploy is told to fetch from. */ +function kernelRepo() { + const dir = mkdtempSync(join(tmpdir(), "kernel-repo-")); + const git = (...args: string[]) => + execFileSync("git", ["-c", "user.name=Ada", "-c", "user.email=ada@example.org", ...args], { + cwd: dir, + encoding: "utf8", + }).trim(); + git("init", "-q", "-b", "main"); + return { + url: `file://${dir}`, + git, + commit(path: string, content: string) { + mkdirSync(join(dir, path, ".."), { recursive: true }); + writeFileSync(join(dir, path), content); + git("add", "-A"); + git("commit", "-q", "-m", `${path} changes`); + return git("rev-parse", "HEAD"); + }, + [Symbol.dispose]: () => rmSync(dir, { recursive: true, force: true }), + }; +} /** A clock that only `pause` moves, and a store that answers as `answers` says, read by read. */ function storeClock(answers: boolean[]) {
M core/lib/src/cli/deploy.ts
@@ -32,7 +32,7 @@ writeFileSync, } from "node:fs"; import { homedir } from "node:os"; -import { dirname, join, resolve } from "node:path"; +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"; @@ -880,14 +880,45 @@ // --------------------------------------------------------------------------------------------- // The stack's source. -/** A checkout, and how pnpm is run in it: the command and its first arguments. */ -type Source = { dir: string; pnpm: { command: string; first: string[] } }; +/** 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[] } }; -function findUp(relative: string, from: string): string | undefined { - for (let dir = resolve(from); ; dir = dirname(dir)) { - if (existsSync(join(dir, relative))) return dir; - if (dirname(dir) === dir) return undefined; +/** WHERE A DEPLOY'S KERNEL COMES FROM when `--source` names no checkout: the kernel repo of the + * mind that a new mind's repos are cloned from (`startingRepos`). The new mind clones that same + * repo at birth, so its own /repos/kernel holds the commit that it runs. Null with `none`: the + * mind that hosts the source has no mind to take a kernel from. */ +export const kernelOriginOf = (reposFrom: string | undefined) => + startingRepos(reposFrom || SOURCE_OF_REPOS).find(({ path }) => path === "/repos/kernel")!.origin; + +/** 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. It sends no + * credential: the new mind clones this repo at birth with none, and a token for the host that + * git has kept and that has ended would refuse a repo anyone may read. */ +export async function fetchedKernel(input: { + origin: string; + ref: string; + dir: string; +}): 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 git = (...args: string[]) => + exec("git", ["-c", "credential.helper=", ...args], { cwd: input.dir }); + // 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 @@ -986,33 +1017,34 @@ return { command: "npx", first: ["--yes", packageManager] }; } -/** A checkout that holds core/alchemy, with its dependencies installed: `--source`, else the - * checkout this command runs in, else iterate/core at `ref` in the cache folder. A source whose +/** THE KERNEL THAT A DEPLOY BUILDS, as a folder on this computer. With no flag it is the kernel + * repo of the mind that the new mind's repos are cloned from (`kernelOriginOf`), at `main` or at + * `--ref`, fetched into the cache folder: what runs is then what the mind's own /repos/kernel + * holds. `--source` names a checkout instead, which is how someone who works on the kernel + * deploys what is on their disk, and how the mind that hosts the source is deployed. The folder + * this command happens to run in is never the kernel by itself. */ +async function kernelCheckout(input: DeployInput) { + if (input.source) { + const dir = resolve(input.source); + return { dir, from: `the checkout ${dir}` }; + } + const origin = kernelOriginOf(input.reposFrom); + if (!origin) + throw new Error( + "--repos-from none names no mind to take the kernel from: give a checkout of the kernel with --source.", + ); + const dir = join(CACHE, "kernel"); + const commit = await fetchedKernel({ origin, ref: input.ref || "main", dir }); + return { dir, from: `${origin} at ${commit.slice(0, 12)}` }; +} + +/** The kernel's checkout (`kernelCheckout`) with its dependencies installed. A source whose * core/alchemy is not the one script that this command writes the input of is refused. */ async function sourceOf(input: DeployInput): Promise<Source> { + const { dir, from } = await kernelCheckout(input); const marker = "core/alchemy/alchemy.run.ts"; - const own = input.source - ? resolve(input.source) - : input.ref - ? undefined - : findUp(marker, process.cwd()); - if (own && !existsSync(join(own, marker))) - throw new Error(`${own} holds no ${marker}: not a checkout of iterate's core.`); - const dir = own || join(CACHE, "core"); - if (!own) { - if (spawnSync("git", ["--version"], { stdio: "ignore" }).status !== 0) - throw new Error( - "git is not installed: the deploy fetches iterate's source with it. Install git and run again.", - ); - mkdirSync(dir, { recursive: true }); - const git = (...args: string[]) => exec("git", args, { cwd: dir }); - if (!existsSync(join(dir, ".git"))) { - await git("init", "-q"); - await git("remote", "add", "origin", "https://github.com/iterate/core"); - } - await git("fetch", "-q", "--depth", "1", "origin", input.ref || "main"); - await git("checkout", "-q", "--detach", "--force", "FETCH_HEAD"); - } + 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 ( @@ -1029,7 +1061,7 @@ 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 (!own || !existsSync(join(dir, "core/alchemy/node_modules/alchemy/bin/cli.js"))) + if (!input.source || !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" }, @@ -1042,7 +1074,7 @@ env: { ...pnpmEnvFor(pnpm.command), CI: "1" }, maxBuffer: 64 << 20, }); - return { dir, pnpm }; + return { dir, from, pnpm }; } // --------------------------------------------------------------------------------------------- @@ -2257,12 +2289,12 @@ ); } - const waiting = wait("Getting iterate's source"); + const waiting = wait("Getting iterate's kernel"); const ready = await source.catch((error: unknown) => { - waiting.fail("iterate's source did not install"); + waiting.fail("iterate's kernel did not install"); throw error; }); - waiting.done(`iterate's source is ready (${seconds(sourceBegan)}): ${ready.dir}`); + waiting.done(`iterate's kernel is ready (${seconds(sourceBegan)}): ${ready.from}`); mkdirSync(work, { recursive: true, mode: 0o700 }); const log = join(work, "deploy.log"); writeFileSync(log, "");