Skip to content

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

feat(ci): add check for ci module source

Steven Enamakelaf0876ef7653
diff --git a/scripts/__tests__/ci-module-source.test.mjs b/scripts/__tests__/ci-module-source.test.mjs
new file mode 100644
index 0000000000..58de8f631b
--- /dev/null
+++ b/scripts/__tests__/ci-module-source.test.mjs
@@ -0,0 +1,67 @@
+// Self-test for scripts/ci/check-ci-module-source.mjs (openhuman#7604).
+import assert from "node:assert/strict";
+import { spawnSync } from "node:child_process";
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { dirname, join, resolve } from "node:path";
+import { test } from "node:test";
+import { fileURLToPath } from "node:url";
+
+import { check, scan } from "../ci/check-ci-module-source.mjs";
+
+const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
+const CLI = join(REPO_ROOT, "scripts/ci/check-ci-module-source.mjs");
+
+test("the real tree passes", () => {
+ assert.deepEqual(check(REPO_ROOT), []);
+});
+
+test("the global and per-module env vars are flagged", () => {
+ for (const line of [
+ " OPENHUMAN_MODULE_SOURCE: linked",
+ "export OPENHUMAN_MODULE_SOURCE=source",
+ "env: { OPENHUMAN_MODULE_SOURCE_TINYJUICE: source }",
+ ])
+ assert.equal(scan(line, "w.yml").length, 1, line);
+});
+
+test("linked and source overrides are flagged, release is not", () => {
+ assert.equal(scan('source = "linked"', "c.toml").length, 1);
+ assert.equal(scan('{"source": "source"}', "c.json").length, 1);
+ assert.equal(scan('source = "release"', "c.toml").length, 0);
+ assert.equal(scan('const source = "linkedin"', "x.mjs").length, 0);
+});
+
+test("enabling the linked-modules feature is flagged", () => {
+ assert.equal(
+ scan("cargo test --features linked-modules", "w.yml").length,
+ 1,
+ );
+});
+
+test("comments may explain the rule", () => {
+ assert.deepEqual(
+ scan("# never set OPENHUMAN_MODULE_SOURCE in CI", "w.yml"),
+ [],
+ );
+});
+
+test("the CLI exits non-zero on a violating workflow and zero on a clean one", () => {
+ const root = mkdtempSync(join(tmpdir(), "ci-module-source-"));
+ try {
+ mkdirSync(join(root, ".github/workflows"), { recursive: true });
+ mkdirSync(join(root, "scripts/ci"), { recursive: true });
+ writeFileSync(join(root, ".github/workflows/ok.yml"), "run: cargo test\n");
+ let result = spawnSync(process.execPath, [CLI, root], { encoding: "utf8" });
+ assert.equal(result.status, 0, result.stderr);
+ writeFileSync(
+ join(root, ".github/workflows/bad.yml"),
+ "env:\n OPENHUMAN_MODULE_SOURCE: linked\n",
+ );
+ result = spawnSync(process.execPath, [CLI, root], { encoding: "utf8" });
+ assert.equal(result.status, 1);
+ assert.match(result.stderr, /bad\.yml:2/);
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+});
diff --git a/scripts/ci/check-ci-module-source.mjs b/scripts/ci/check-ci-module-source.mjs
new file mode 100644
index 0000000000..f35101ed6e
--- /dev/null
+++ b/scripts/ci/check-ci-module-source.mjs
@@ -0,0 +1,117 @@
+#!/usr/bin/env node
+// Fails when CI selects anything but the pinned, digest-checked release for
+// loadable modules (tinyhumansai/openhuman#7604, epic #7523).
+//
+// `[modules] source` has three values: `release` (GitHub-released, pinned,
+// digest-checked; the default), and `linked` / `source` (local development and
+// local tests only). CI and shipped builds must run `release`, so nothing under
+// `.github/workflows/` or the CI scripts may set:
+//
+// - OPENHUMAN_MODULE_SOURCE or OPENHUMAN_MODULE_SOURCE_<ID>
+// - a `linked` / `source` override (`source = "linked"`, `"source": "source"`)
+// - the `linked-modules` feature
+//
+// Comment lines are ignored, so a workflow may explain the rule. Pure text
+// scan, no network.
+//
+// Usage: check-ci-module-source.mjs [repo-root]
+import { readdirSync, readFileSync, statSync } from "node:fs";
+import { join, relative, resolve, sep } from "node:path";
+import { pathToFileURL } from "node:url";
+
+const SELF = "scripts/ci/check-ci-module-source.mjs";
+
+export const RULES = [
+ {
+ name: "module-source-env",
+ re: /\bOPENHUMAN_MODULE_SOURCE(?:_[A-Z0-9_]+)?\b/,
+ why: "sets a module source through the environment",
+ },
+ {
+ name: "non-release-override",
+ re: /\bsource["']?\s*[=:]\s*["'](?:linked|source)["']/,
+ why: "selects a linked/source module override",
+ },
+ {
+ name: "linked-modules-feature",
+ re: /\blinked-modules\b/,
+ why: "enables the linked-modules feature",
+ },
+];
+
+const isComment = (line) => /^\s*(#|\/\/|\*|\/\*)/.test(line);
+
+/** Violations in one file's text: `{ file, line, rule, why, text }`. */
+export function scan(text, file) {
+ const found = [];
+ text.split("\n").forEach((line, index) => {
+ if (isComment(line)) return;
+ for (const rule of RULES) {
+ if (rule.re.test(line))
+ found.push({
+ file,
+ line: index + 1,
+ rule: rule.name,
+ why: rule.why,
+ text: line.trim().slice(0, 160),
+ });
+ }
+ });
+ return found;
+}
+
+function walk(dir, out = []) {
+ let entries;
+ try {
+ entries = readdirSync(dir);
+ } catch {
+ return out;
+ }
+ for (const name of entries) {
+ const path = join(dir, name);
+ if (name === "node_modules" || name === "__tests__") continue;
+ if (statSync(path).isDirectory()) walk(path, out);
+ else out.push(path);
+ }
+ return out;
+}
+
+/** Every file CI executes or configures: workflows and `scripts/ci/**`. */
+export function ciFiles(root) {
+ const files = [
+ ...walk(join(root, ".github", "workflows")),
+ ...walk(join(root, "scripts", "ci")),
+ join(root, "scripts", "ci-cancel-aware.sh"),
+ ];
+ return files
+ .map((path) => relative(root, path).split(sep).join("/"))
+ .filter((file) => file !== SELF);
+}
+
+export function check(root) {
+ const violations = [];
+ for (const file of ciFiles(root)) {
+ let text;
+ try {
+ text = readFileSync(join(root, file), "utf8");
+ } catch {
+ continue;
+ }
+ violations.push(...scan(text, file));
+ }
+ return violations;
+}
+
+if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
+ const root = resolve(process.argv[2] ?? ".");
+ const violations = check(root);
+ if (violations.length) {
+ for (const v of violations)
+ console.error(`${v.file}:${v.line}: ${v.why} [${v.rule}]\n ${v.text}`);
+ console.error(
+ `\ncheck-ci-module-source: ${violations.length} violation(s). CI runs the pinned release only; linked/source are for local development.`,
+ );
+ process.exit(1);
+ }
+ console.log("check-ci-module-source: CI selects the pinned release only");
+}