verify.mjs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import { spawnSync } from "node:child_process";
  2. import {
  3. appendFileSync,
  4. mkdirSync,
  5. mkdtempSync,
  6. renameSync,
  7. rmSync,
  8. writeFileSync,
  9. } from "node:fs";
  10. import { tmpdir } from "node:os";
  11. import path from "node:path";
  12. import { fileURLToPath } from "node:url";
  13. import { parseArgs } from "node:util";
  14. // Single definition of the full verification pipeline. GitHub CI runs `pnpm verify`
  15. // so a local run executes the same gates in the same order; previously the order
  16. // only existed inside the workflow file and could not be reproduced locally.
  17. // Platform-conditional gates declare the platforms they support instead of being
  18. // filtered by workflow `if:` expressions.
  19. const root = fileURLToPath(new URL("../", import.meta.url));
  20. const { values } = parseArgs({
  21. options: {
  22. profile: { type: "string", default: "full" },
  23. list: { type: "boolean", default: false },
  24. },
  25. });
  26. const profile = values.profile;
  27. if (!["full", "platform", "docs", "archive"].includes(profile))
  28. throw new Error(`Unknown verification profile: ${profile}`);
  29. // Listing must not leave a temporary export directory behind.
  30. const temporary = values.list
  31. ? undefined
  32. : mkdtempSync(path.join(tmpdir(), "mcode-verify-"));
  33. const preview = path.join(temporary ?? tmpdir(), "minimax-code-source.tar.gz");
  34. const steps = [
  35. { name: "check:source", script: "check:source", docs: true },
  36. { name: "check:tsconfig", script: "check:tsconfig", docs: true },
  37. {
  38. name: "export source preview",
  39. docs: true,
  40. requiresGit: true,
  41. command: ["scripts/export-source-preview.mjs", "--out", preview],
  42. },
  43. { name: "test:release-tools", script: "test:release-tools", docs: true },
  44. // Compiler inputs are identical across the matrix. One Linux job runs this;
  45. // all platforms still build and validate native artifacts on their own platform.
  46. { name: "typecheck", script: "typecheck", fullOnly: true },
  47. { name: "build", script: "build" },
  48. { name: "check:standalone", script: "check:standalone" },
  49. { name: "test:artifact", script: "test:artifact" },
  50. { name: "test:capabilities", script: "test:capabilities" },
  51. { name: "test:status-contract", script: "test:status-contract" },
  52. { name: "test:smoke", script: "test:smoke" },
  53. { name: "test:byok", script: "test:byok" },
  54. // The permission facade uses POSIX process and filesystem semantics.
  55. {
  56. name: "test:policy",
  57. script: "test:policy",
  58. platforms: ["darwin", "linux"],
  59. },
  60. // Seatbelt sandbox backend; only macOS provides the native helper.
  61. { name: "test:sandbox", script: "test:sandbox", platforms: ["darwin"] },
  62. ];
  63. function skipReason(step) {
  64. if (profile === "docs" && !step.docs) return "documentation-only change";
  65. if (profile === "archive" && step.requiresGit)
  66. return "validating an already exported archive";
  67. if (step.platforms && !step.platforms.includes(process.platform))
  68. return "not applicable on this platform";
  69. if (step.fullOnly && profile === "platform")
  70. return "covered by the full profile";
  71. return null;
  72. }
  73. const planned = steps.filter((step) => !skipReason(step));
  74. if (values.list) {
  75. for (const step of planned) console.log(step.name);
  76. process.exit(0);
  77. }
  78. // `npm_execpath` may be a JavaScript entry point or a native package-manager
  79. // binary depending on how pnpm was installed, so dispatch on what it actually is
  80. // instead of assuming it can be passed to node.
  81. function runScript(name) {
  82. const execPath = process.env.npm_execpath;
  83. if (execPath && /\.[cm]?js$/u.test(execPath))
  84. return spawnSync(process.execPath, [execPath, "run", name], {
  85. stdio: "inherit",
  86. cwd: root,
  87. });
  88. const command = execPath ?? "pnpm";
  89. const shell = process.platform === "win32";
  90. return spawnSync(shell ? `"${command}"` : command, ["run", name], {
  91. stdio: "inherit",
  92. cwd: root,
  93. shell,
  94. });
  95. }
  96. function run(step) {
  97. const group = Boolean(process.env.GITHUB_ACTIONS);
  98. if (group) console.log(`::group::${step.name}`);
  99. else console.log(`\n=== ${step.name} ===`);
  100. try {
  101. return step.script
  102. ? runScript(step.script)
  103. : spawnSync(process.execPath, step.command, {
  104. stdio: "inherit",
  105. cwd: root,
  106. });
  107. } finally {
  108. if (group) console.log("::endgroup::");
  109. }
  110. }
  111. const started = performance.now();
  112. const revision = spawnSync("git", ["rev-parse", "HEAD"], {
  113. cwd: root,
  114. encoding: "utf8",
  115. });
  116. const report = {
  117. schemaVersion: 1,
  118. // The candidate workflow authenticates its receipt before running this
  119. // profile in a source-only directory that deliberately contains no .git.
  120. revision:
  121. revision.status === 0
  122. ? revision.stdout.trim()
  123. : profile === "archive" &&
  124. /^[a-f0-9]{40}$/u.test(process.env.MCODE_VERIFY_REVISION ?? "")
  125. ? process.env.MCODE_VERIFY_REVISION
  126. : null,
  127. platform: process.platform,
  128. arch: process.arch,
  129. node: process.version,
  130. profile,
  131. startedAt: new Date().toISOString(),
  132. durationMs: 0,
  133. status: "RUNNING",
  134. gates: steps.map((step) => ({
  135. name: step.name,
  136. status: skipReason(step) ? "SKIP" : "NOT_RUN",
  137. reason: skipReason(step),
  138. durationMs: null,
  139. })),
  140. };
  141. const reportDir = process.env.MCODE_VERIFY_REPORT_DIR;
  142. function saveReport() {
  143. report.durationMs = Math.round(performance.now() - started);
  144. if (!reportDir) return;
  145. mkdirSync(reportDir, { recursive: true });
  146. const target = path.join(reportDir, "verification.json");
  147. // Keep the last complete JSON readable even if the runner is interrupted.
  148. writeFileSync(`${target}.tmp`, `${JSON.stringify(report, null, 2)}\n`);
  149. renameSync(`${target}.tmp`, target);
  150. }
  151. function summary() {
  152. return [
  153. `## Verification: ${report.status}`,
  154. "",
  155. `${report.platform}/${report.arch}, Node ${report.node}, profile ${profile}`,
  156. `Revision: ${report.revision ?? "unavailable"}`,
  157. `Elapsed: ${(report.durationMs / 1000).toFixed(1)}s`,
  158. "",
  159. "| Gate | Result | Seconds | Detail |",
  160. "| --- | --- | ---: | --- |",
  161. ...report.gates.map((gate) => {
  162. const detail =
  163. gate.reason ??
  164. (gate.errorCode
  165. ? `spawn error ${gate.errorCode}`
  166. : gate.signal
  167. ? `signal ${gate.signal}`
  168. : gate.exitCode != null
  169. ? `exit ${gate.exitCode}`
  170. : "");
  171. return `| ${gate.name} | ${gate.status} | ${gate.durationMs == null ? "—" : (gate.durationMs / 1000).toFixed(1)} | ${detail} |`;
  172. }),
  173. "",
  174. "Reports contain gate metadata only. Failure output remains in the corresponding job log.",
  175. "",
  176. ].join("\n");
  177. }
  178. let failed;
  179. try {
  180. saveReport();
  181. for (const step of planned) {
  182. const gate = report.gates.find((entry) => entry.name === step.name);
  183. gate.status = "RUNNING";
  184. saveReport();
  185. const gateStarted = performance.now();
  186. let result;
  187. try {
  188. result = run(step);
  189. } catch (error) {
  190. result = { error, status: null, signal: null };
  191. }
  192. gate.durationMs = Math.round(performance.now() - gateStarted);
  193. gate.exitCode = result.status;
  194. gate.signal = result.signal;
  195. gate.errorCode = result.error ? (result.error.code ?? "SPAWN_ERROR") : null;
  196. gate.status = result.status === 0 && !result.error ? "PASS" : "FAIL";
  197. console.log(
  198. `${step.name}: ${gate.status} (${(gate.durationMs / 1000).toFixed(1)}s)`,
  199. );
  200. saveReport();
  201. if (gate.status === "FAIL") {
  202. failed = step.name;
  203. break;
  204. }
  205. }
  206. report.status = failed ? "FAIL" : "PASS";
  207. } finally {
  208. rmSync(temporary, { recursive: true, force: true });
  209. for (const gate of report.gates)
  210. if (gate.status === "NOT_RUN")
  211. gate.reason = "verification stopped before this gate";
  212. saveReport();
  213. const markdown = summary();
  214. if (reportDir)
  215. writeFileSync(path.join(reportDir, "verification.md"), markdown);
  216. if (process.env.GITHUB_STEP_SUMMARY)
  217. appendFileSync(process.env.GITHUB_STEP_SUMMARY, markdown);
  218. }
  219. if (failed) {
  220. console.error(`\nVerification failed at: ${failed}`);
  221. process.exitCode = 1;
  222. } else {
  223. const skipped = steps
  224. .filter((step) => !planned.includes(step))
  225. .map((step) => `${step.name} (${skipReason(step)})`);
  226. console.log(
  227. `\nVerification passed: ${planned.length} gates on ${process.platform}.` +
  228. (skipped.length ? ` Skipped: ${skipped.join(", ")}.` : ""),
  229. );
  230. }