Cotton code
139 lines · 6.8 KB · TypeScriptRaw
/** * A WALK ON A STALE CORE IS NOT EVIDENCE. * * On 22 Oct 2026 a lane's Todos deck read "Nothing here" on its simulator, and * the bug hunt began on main. The deck was right: the lane's xcframework was * built before the card landed, so no row carried `@card`, and nothing could * tell. `scripts/core-fresh.sh` reads the stamp `crates/cotton-ffi/build.rs` * bakes into the archive and fails unless it names this checkout's crates/. * This drives the script over fake archives holding only a stamp. */import { spawnSync } from "node:child_process";import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join, resolve } from "node:path";import { afterEach, describe, expect, it } from "vitest";const ROOT = resolve(__dirname, "../..");const SCRIPT = join(ROOT, "scripts/core-fresh.sh");let scratch = "";afterEach(() => {  if (scratch) rmSync(scratch, { recursive: true, force: true });  scratch = "";});function cratesNow(): string {  const tree = spawnSync("bash", [join(ROOT, "scripts/lib/crates-tree.sh"), ROOT], { encoding: "utf8" });  expect(tree.status, tree.stderr).toBe(0);  return tree.stdout.trim();}/** An xcframework of one slice whose archive holds `stamp` among other bytes. */function framework(stamp: string | null, headers = true): string {  scratch ||= mkdtempSync(join(tmpdir(), "core-fresh-"));  const slice = join(scratch, "CottonFFI.xcframework", "ios-arm64-simulator");  mkdirSync(slice, { recursive: true });  if (headers) mkdirSync(join(slice, "Headers"));  const bytes = Buffer.concat([Buffer.from([0, 1, 2, 255]), Buffer.from(stamp ?? "no stamp here"), Buffer.from([0, 7])]);  writeFileSync(join(slice, "libcotton_ffi.a"), bytes);  return join(scratch, "CottonFFI.xcframework");}function check(target: string, ...flags: string[]) {  const { COTTON_CORE_KIT: _unset, ...env } = process.env;  return spawnSync("bash", [SCRIPT, ...flags, target], { encoding: "utf8", env });}/** These crates/ with one more file at `path` (under crates/): a tree written into this clone's objects. */function cratesWith(path: string): string {  scratch ||= mkdtempSync(join(tmpdir(), "core-fresh-"));  const env = { ...process.env, GIT_INDEX_FILE: join(scratch, "index") };  const git = (...args: string[]) => {    const run = spawnSync("git", ["-C", ROOT, ...args], { encoding: "utf8", env });    expect(run.status, run.stderr).toBe(0);    return run.stdout.trim();  };  git("read-tree", cratesNow());  const blob = spawnSync("git", ["-C", ROOT, "hash-object", "-w", "--stdin"], { input: "// core-fresh test\n", encoding: "utf8" }).stdout.trim();  git("update-index", "--add", "--cacheinfo", `100644,${blob},${path}`);  return git("write-tree");}const COMMIT = "1a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d";const OTHER_TREE = "0".repeat(40);describe("core-fresh.sh", () => {  // RULE: a core stamped with this checkout's crates/ passes, whatever commit built it.  it("passes a core built from these crates/", () => {    const result = check(framework(`cotton-core-build:${COMMIT}:0:${cratesNow()}`), "--stamp");    expect(result.status, result.stderr).toBe(0);    expect(result.stdout.trim()).toBe("1a2b3c4d5");  });  // RULE: a slice with no Headers is a half-written XCFramework (a create-xcframework cut short): stale, however fresh its stamp.  it("fails a fresh archive whose slice has no Headers", () => {    const result = check(framework(`cotton-core-build:${COMMIT}:0:${cratesNow()}`, false));    expect(result.status).toBe(1);    expect(result.stderr).toContain("half-written XCFramework");  });  // RULE: a core built from other crates/ fails the check: its walk is not evidence.  it("fails a core built from other crates/", () => {    const result = check(framework(`cotton-core-build:${COMMIT}:1:${OTHER_TREE}`));    expect(result.status).toBe(1);    expect(result.stderr).toContain("STALE");    expect(result.stderr).toContain("1a2b3c4d5.dirty");  });  // RULE: crates/ that differ only under a crate the core never links (cotton-mac's tests, 789da43115)  // leave the core fresh: the xcframework cache keys on the same closure and keeps that core in place.  it("passes a core whose crates/ differ only outside the core", () => {    const result = check(framework(`cotton-core-build:${COMMIT}:0:${cratesWith("cotton-mac/tests/core_fresh_probe.rs")}`));    expect(result.status, result.stderr).toBe(0);    expect(result.stderr).toContain("only outside the core (cotton-mac)");  });  // RULE: a change under a crate the core links (cotton-store, through cotton-ffi's path dependencies) is stale.  it("fails a core whose crates/ differ inside the core", () => {    const result = check(framework(`cotton-core-build:${COMMIT}:0:${cratesWith("cotton-store/tests/core_fresh_probe.rs")}`));    expect(result.status).toBe(1);    expect(result.stderr).toContain("STALE");  });  // RULE: a core built for one app's kit carries that kit's pictures alone, so it is fresh only for a build that  // wants that kit (COTTON_CORE_KIT): a walk of another app, or of Cotton, on it is refused; the whole core is  // refused where a kit's is wanted, and a core from before the kit stamp is a whole one.  it("fails a core built for another kit than the one wanted", () => {    const stamp = `cotton-core-build:${COMMIT}:0:${cratesNow()}`;    const wanting = (kit: string | null, target: string) => {      const { COTTON_CORE_KIT: _unset, ...env } = process.env;      return spawnSync("bash", [SCRIPT, target], { encoding: "utf8", env: kit === null ? env : { ...env, COTTON_CORE_KIT: kit } });    };    const target = framework(stamp);    // The same framework, its one archive rewritten with the kit stamp beside the build stamp.    const stamped = (kit: string | null) => {      writeFileSync(join(target, "ios-arm64-simulator", "libcotton_ffi.a"), kit === null ? stamp : `${stamp}\0cotton-core-kit:${kit};`);      return target;    };    const books = stamped("books");    expect(wanting("books", books).status).toBe(0);    const whole = wanting(null, books);    expect(whole.status).toBe(1);    expect(whole.stderr).toContain("carries the books kit's pictures alone, and this build wants the whole core");    expect(wanting("money", books).stderr).toContain("this build wants money");    const every = stamped("");    expect(wanting(null, every).status).toBe(0);    const wanted = wanting("books", every);    expect(wanted.status).toBe(1);    expect(wanted.stderr).toContain("carries every kit's pictures, and this build wants books");    expect(wanting(null, stamped(null)).status).toBe(0);  });  // RULE: a core with no stamp at all (built before the stamp) fails the check.  it("fails an unstamped core", () => {    const result = check(framework(null));    expect(result.status).toBe(1);    expect(result.stderr).toContain("no build stamp");  });});