Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore(scripts): add module pin status and repin helpers

Steven Enamakel46dfd49ad166
diff --git a/scripts/lib/module-pin-status.mjs b/scripts/lib/module-pin-status.mjs
new file mode 100644
index 0000000000..b53c22971b
--- /dev/null
+++ b/scripts/lib/module-pin-status.mjs
@@ -0,0 +1,107 @@
+// Pure pin-status logic for scripts/modules/pin-status.mjs
+// (tinyhumansai/openhuman#7615, epic #7523).
+//
+// check-module-pins.mjs is the strict CI gate and says only pass/fail. This is
+// the local, advisory view of the same two pins: it tells a developer WHICH way
+// a module's gitlink differs from the registry `version`, because "ahead" means
+// they are running a locally compiled module that CI will refuse.
+
+import { classifyPin } from "./module-pins.mjs";
+
+/** `vMAJOR.MINOR.PATCH[-N-gHASH]` from `git describe --tags`, or null. */
+export function parseDescribe(describe) {
+ const m = String(describe ?? "")
+ .trim()
+ .match(/^(v(\d+)\.(\d+)\.(\d+))(?:-(\d+)-g([0-9a-f]+))?$/);
+ if (!m) return null;
+ return {
+ tag: m[1],
+ parts: [Number(m[2]), Number(m[3]), Number(m[4])],
+ commitsPast: m[5] ? Number(m[5]) : 0,
+ };
+}
+
+/** Negative, zero or positive as `a` is older than, equal to or newer than `b`. */
+export function compareParts(a, b) {
+ for (let i = 0; i < 3; i++) {
+ if (a[i] !== b[i]) return a[i] - b[i];
+ }
+ return 0;
+}
+
+export const AHEAD_MESSAGE =
+ "gitlink ahead of released pin — you're running a locally compiled module; " +
+ "publish a release and re-pin before CI will pass";
+
+/**
+ * Classify one module.
+ *
+ * `actual` is `git describe --tags` of the submodule; `wantedIsAncestor` says
+ * whether the registry's tag is an ancestor of the submodule HEAD (`null` when
+ * unknown, for instance the tag is not fetched); `exemption` is the matching
+ * module-pin-exemptions.json entry.
+ *
+ * Kinds: `in-sync`, `ahead`, `behind`, `diverged`, `exempt`.
+ */
+export function classifyStatus({
+ id,
+ version,
+ submodulePath,
+ actual,
+ wantedIsAncestor = null,
+ exemption,
+}) {
+ const strict = classifyPin({ id, version, submodulePath, actual, exemption });
+ if (strict.kind === "exempt") {
+ return {
+ id,
+ kind: "exempt",
+ actual,
+ message: `exempt (declared drift accepted): ${strict.reason}`,
+ };
+ }
+ const note =
+ strict.kind === "stale-exemption"
+ ? " (its module-pin-exemptions.json entry is stale; delete it)"
+ : strict.kind === "exemption-widened"
+ ? " (the drift moved past its module-pin-exemptions.json entry)"
+ : "";
+ if (strict.kind === "match" || strict.kind === "stale-exemption") {
+ return { id, kind: "in-sync", actual, message: `in sync${note}` };
+ }
+
+ const wanted = parseDescribe(`v${version}`);
+ const got = parseDescribe(actual);
+ const diverged = {
+ id,
+ kind: "diverged",
+ actual,
+ message:
+ `behind/diverged: registry pins v${version} but ${submodulePath} is at ` +
+ `${actual}, which does not descend from it${note}`,
+ };
+ if (!wanted || !got || wantedIsAncestor === false) return diverged;
+
+ const order = compareParts(got.parts, wanted.parts);
+ if (order > 0 || (order === 0 && got.commitsPast > 0)) {
+ return { id, kind: "ahead", actual, message: `${AHEAD_MESSAGE}${note}` };
+ }
+ return {
+ id,
+ kind: "behind",
+ actual,
+ message:
+ `behind/diverged: registry pins v${version} but ${submodulePath} is at ` +
+ `${actual}; check out the pinned release or re-pin${note}`,
+ };
+}
+
+/** Ids whose gitlink is ahead of the registry pin, for the runtime warning. */
+export function aheadIds(statuses) {
+ return statuses.filter((s) => s.kind === "ahead").map((s) => s.id);
+}
+
+/** One printable line per module. */
+export function formatStatus(status) {
+ return `${status.id.padEnd(16)} ${status.kind.padEnd(9)} ${status.message}`;
+}
diff --git a/scripts/lib/module-repin.mjs b/scripts/lib/module-repin.mjs
new file mode 100644
index 0000000000..eb6ef6fb6e
--- /dev/null
+++ b/scripts/lib/module-repin.mjs
@@ -0,0 +1,137 @@
+// Pure helpers for scripts/modules/release-and-repin.mjs
+// (tinyhumansai/openhuman#7615, epic #7523).
+//
+// Every digest written here is copied VERBATIM from a published release's
+// checksum.toml. Nothing in this file hashes anything: a digest computed from a
+// local build agrees with itself no matter what was served, which defeats the
+// pin. A test pins that by scanning this file for any hashing import.
+
+/** `[sha256]` table of `"archive" = "hex"` pairs -> Map(archive -> hex). */
+export function parseChecksumToml(text) {
+ const sums = new Map();
+ let inTable = false;
+ for (const raw of String(text).split("\n")) {
+ const line = raw.replace(/#.*$/, "").trim();
+ if (!line) continue;
+ const table = line.match(/^\[([^\]]+)\]$/);
+ if (table) {
+ inTable = table[1].trim() === "sha256";
+ continue;
+ }
+ if (!inTable) continue;
+ const pair = line.match(/^"([^"]+)"\s*=\s*"([^"]*)"$/);
+ if (!pair) throw new Error(`checksum.toml: cannot parse line: ${line}`);
+ if (!/^[0-9a-f]{64}$/.test(pair[2])) {
+ throw new Error(
+ `checksum.toml: ${pair[1]} is not a 64-hex lowercase sha256`,
+ );
+ }
+ sums.set(pair[1], pair[2]);
+ }
+ if (sums.size === 0) throw new Error("checksum.toml: no [sha256] entries");
+ return sums;
+}
+
+/** `X.Y.Z` bumped by patch|minor|major. */
+export function bumpVersion(version, bump) {
+ const m = String(version).match(/^(\d+)\.(\d+)\.(\d+)$/);
+ if (!m) throw new Error(`not a plain X.Y.Z version: ${version}`);
+ let [major, minor, patch] = m.slice(1).map(Number);
+ if (bump === "major") [major, minor, patch] = [major + 1, 0, 0];
+ else if (bump === "minor") [minor, patch] = [minor + 1, 0];
+ else if (bump === "patch") patch += 1;
+ else throw new Error(`--bump must be patch, minor or major, got ${bump}`);
+ return `${major}.${minor}.${patch}`;
+}
+
+const RECORD_RE =
+ /(const [A-Z0-9_]+: ModuleRecord = ModuleRecord \{)([\s\S]*?)(\n\};)/g;
+const ASSET_RE =
+ /(PlatformAsset \{\s*host_key: ")([^"]+)(",\s*archive: ")([^"]+)(",\s*sha256: ")([^"]+)(",\s*\})/g;
+
+/**
+ * Rewrite one record in a records_*.rs source to release `newVersion`.
+ *
+ * `version`, `release_url` and each asset's `archive` and `sha256` change;
+ * the archive name is the old one with the version swapped and must exist in
+ * `checksums`. Returns `{ src, changed }`, or `{ src, changed: false }` when
+ * the file does not hold the record. Throws when an asset has no checksum.
+ */
+export function rewriteRecord(src, id, newVersion, checksums) {
+ let changed = false;
+ const out = src.replace(RECORD_RE, (whole, head, body, tail) => {
+ if (!new RegExp(`^\\s*id: "${id}",`, "m").test(body)) return whole;
+ const oldVersion = body.match(/^\s*version: "([^"]+)"/m)?.[1];
+ if (!oldVersion) throw new Error(`${id}: record has no version`);
+ changed = true;
+ const missing = [];
+ let next = body
+ .replace(/^(\s*version: ")[^"]+(")/m, `$1${newVersion}$2`)
+ .replace(
+ /^(\s*release_url: ")([^"]*)(")/m,
+ (_, a, url, c) =>
+ `${a}${url.replace(/\/tag\/v[^/]+$/, `/tag/v${newVersion}`)}${c}`,
+ );
+ next = next.replace(
+ ASSET_RE,
+ (_, a, hostKey, b, archive, c, _oldSha, d) => {
+ const renamed = archive.replace(oldVersion, newVersion);
+ const sha = checksums.get(renamed);
+ if (!sha) {
+ missing.push(renamed);
+ return _;
+ }
+ return `${a}${hostKey}${b}${renamed}${c}${sha}${d}`;
+ },
+ );
+ if (missing.length > 0) {
+ throw new Error(
+ `${id}: v${newVersion} checksum.toml has no entry for ${missing.join(", ")}`,
+ );
+ }
+ return `${head}${next}${tail}`;
+ });
+ return { src: out, changed };
+}
+
+/** Archive names in `checksums` that no asset of the record uses. */
+export function unusedChecksums(src, id, checksums) {
+ const used = new Set();
+ for (const [, head, body] of src.matchAll(
+ /(const [A-Z0-9_]+: ModuleRecord = ModuleRecord \{)([\s\S]*?)\n\};/g,
+ ).map((m) => [m[0], m[1], m[2]])) {
+ void head;
+ if (!new RegExp(`^\\s*id: "${id}",`, "m").test(body)) continue;
+ for (const a of body.matchAll(ASSET_RE)) used.add(a[4]);
+ }
+ return [...checksums.keys()].filter((name) => !used.has(name));
+}
+
+/**
+ * Update module-provider-pins.json for a provider record: its release version
+ * and the commit it was built against. Other entries are left untouched.
+ */
+export function updateProviderPins(json, id, version, builtAgainst) {
+ const providers = json.providers ?? {};
+ if (!providers[id]) return { json, changed: false };
+ return {
+ changed: true,
+ json: {
+ ...json,
+ providers: {
+ ...providers,
+ [id]: { ...providers[id], version, built_against: builtAgainst },
+ },
+ },
+ };
+}
+
+/** Drop an exemption for `id` once its pins agree. */
+export function dropExemption(json, id) {
+ const exemptions = json.exemptions ?? [];
+ const kept = exemptions.filter((e) => e.id !== id);
+ return {
+ json: { ...json, exemptions: kept },
+ changed: kept.length !== exemptions.length,
+ };
+}