Cotton code
129 lines · 6.3 KB · TypeScriptRaw
import { spawnSync } from "node:child_process";import { createHash } from "node:crypto";import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { afterEach, describe, expect, it } from "vitest";import { linuxHostPath, publishDiskReading } from "./helpers/disk-reading";/** * The lane ledger (storage note c7d20613, 10 Oct 2026): what a lane makes is * claimed at the moment it is made, so the Mac app's ledger pass can list it * and release it by rule. `scripts/lane-claim.sh` writes the claim; the * scripts that make things call it. */const SCRIPTS = join(__dirname, "..");const CLAIM = join(SCRIPTS, "lane-claim.sh");const scratches: string[] = [];afterEach(() => {  while (scratches.length) rmSync(scratches.pop() as string, { recursive: true, force: true });});function scratch(): string {  const dir = realpathSync(mkdtempSync(join(tmpdir(), "lane-claim-")));  scratches.push(dir);  return dir;}function claim(claims: string, args: string[], env: Record<string, string> = {}) {  return spawnSync("bash", [CLAIM, ...args], {    encoding: "utf8",    env: { ...process.env, COTTON_CLAIMS_DIR: claims, COTTON_LANE: "", COTTON_LANE_SESSION: "", COTTON_SESSION_ROW_ID: "", ...env },  });}describe("lane-claim.sh", () => {  it("writes one claim per path, named as the ledger names it, with the kind's rule", () => {    const root = scratch();    const claims = join(root, "claims");    const target = join(root, 'a "quoted" target');    mkdirSync(target);    const ran = claim(claims, [target, "--kind", "cargo_target", "--lane", "feat/x"]);    expect(ran.status, ran.stderr).toBe(0);    const id = `claim-${createHash("sha256").update(target).digest("hex").slice(0, 24)}`;    expect(readdirSync(claims)).toEqual([`${id}.json`]);    const written = JSON.parse(readFileSync(join(claims, `${id}.json`), "utf8"));    expect(written).toMatchObject({ path: target, kind: "cargo_target", lane: "feat/x", rule: "budget_lru" });    // Claimed again by the same lane: the claim stands as it was.    claim(claims, [target, "--kind", "cargo_target", "--lane", "feat/x"]);    expect(readdirSync(claims)).toEqual([`${id}.json`]);  });  it("ties a claim to its session when there is one, and to idle hours when there is not", () => {    const root = scratch();    const claims = join(root, "claims");    const withSession = join(root, "evidence");    const without = join(root, "scratch");    mkdirSync(withSession);    mkdirSync(without);    claim(claims, [withSession, "--kind", "evidence"], { COTTON_LANE_SESSION: "s-1", COTTON_LANE: "ledger" });    claim(claims, [without, "--kind", "scratch", "--lane", "ledger"]);    const rules = readdirSync(claims).map((file) => JSON.parse(readFileSync(join(claims, file), "utf8")));    expect(rules.find((c) => c.path === withSession)).toMatchObject({ rule: "session_ends", session: "s-1" });    expect(rules.find((c) => c.path === without)).toMatchObject({ rule: "idle_hours:72" });  });  // RULE: Explicit keep_last:N remains available for regenerable output; walk evidence is owner_only and is protected by the evidence guards.  it("preserves an explicit keep_last rule and refuses a zero retention count", () => {    const root = scratch();    const claims = join(root, "claims");    const run = join(root, "board-20261010T010000Z");    mkdirSync(run);    const ran = claim(claims, [run, "--kind", "derived", "--rule", "keep_last:3", "--lane", "walk"]);    expect(ran.status, ran.stderr).toBe(0);    const [file] = readdirSync(claims);    expect(JSON.parse(readFileSync(join(claims, file), "utf8"))).toMatchObject({ path: run, rule: "keep_last:3", lane: "walk" });    const zero = join(root, "zero");    mkdirSync(zero);    claim(claims, [zero, "--kind", "derived", "--rule", "keep_last:0"]);    expect(readdirSync(claims)).toHaveLength(1);  });  it("never fails its caller, and claims nothing it cannot stand behind", () => {    const root = scratch();    const claims = join(root, "claims");    for (const args of [      [join(root, "not-there"), "--kind", "scratch"],      [root, "--kind", "everything"],      [root, "--kind", "scratch", "--rule", "whenever"],      [],    ]) {      const ran = claim(claims, args);      expect(ran.status).toBe(0);    }    expect(() => readdirSync(claims)).toThrow();  });  // RULE: a worktree is claimed session_ends whether or not a session is named, never idle_hours — the ledger keeps a folder while something lives in it and reads that off the Mac, so nobody claims a live lane owner_only to keep it; and a rule asked for by name replaces the rule a claim has.  // Incident, 10 Oct 2026: a live lane's worktree was removed on one Mac, and every live lane was then claimed owner_only by hand as a stopgap.  it("claims a worktree session_ends with no session, and puts a stopgap owner_only back by name", () => {    const root = scratch();    const claims = join(root, "claims");    const tree = join(root, "tree");    mkdirSync(tree);    claim(claims, [tree, "--kind", "worktree", "--lane", "walk"]);    const file = join(claims, `claim-${createHash("sha256").update(tree).digest("hex").slice(0, 24)}.json`);    const read = () => JSON.parse(readFileSync(file, "utf8"));    expect(read()).toMatchObject({ kind: "worktree", rule: "session_ends", lane: "walk" });    expect(read().session).toBeUndefined();    claim(claims, [tree, "--kind", "worktree", "--lane", "walk", "--rule", "owner_only"]);    expect(read().rule).toBe("owner_only");    // The default rule never replaces one a claim already has…    claim(claims, [tree, "--kind", "worktree", "--lane", "walk"]);    expect(read().rule).toBe("owner_only");    // …a rule named does.    claim(claims, [tree, "--kind", "worktree", "--lane", "walk", "--rule", "session_ends"]);    expect(read().rule).toBe("session_ends");  });});describe("worktree-new.sh", () => {  // The session-start claim hook went with the project hooks (22 Oct 2026): a  // lane folder is claimed by the script that makes it, not by every session.  it("worktree:new claims session_ends by name", () => {    expect(readFileSync(join(SCRIPTS, "worktree-new.sh"), "utf8")).toMatch(/lane-claim\.sh" "\$WORKTREE_PATH" --kind worktree --rule session_ends/);  });});