Skip to content

👾 Open Computer Use – Open-Source Alternative to Codex Computer Use

Harden npm release publish retry

Leo1ee66ec4f9e4
diff --git a/docs/histories/2026-04/20260422-1205-harden-npm-release-retry.md b/docs/histories/2026-04/20260422-1205-harden-npm-release-retry.md
new file mode 100644
index 0000000..164b935
--- /dev/null
+++ b/docs/histories/2026-04/20260422-1205-harden-npm-release-retry.md
@@ -0,0 +1,26 @@
+## [2026-04-22 12:05] | Task: 修复 0.1.27 release npm publish 失败
+
+### 背景
+
+- `v0.1.27` tag 触发的 GitHub Actions release 在 `package-npm` job 失败。
+- 失败点是 `Publish packages to npm`,registry 对 `[email protected]` 的 publish PUT 返回 404;同一 run 的 Cursor Motion DMG job 已成功。
+- npm registry 当前仍能看到三个包的 `0.1.26`,但看不到 `0.1.27`。
+
+### 变更
+
+- **[Publish Recovery]**: `scripts/npm/publish-packages.mjs` 发布每个 staged package 前先用 `npm view <name>@<version>` 检查同版本是否已经存在;存在时直接跳过。
+- **[Retry]**: npm publish 失败后最多重试 3 次,并在每次失败后再次检查版本是否已经对 registry 可见,用于覆盖 registry 短暂错误或部分发布成功场景。
+- **[Release Guide]**: 在发版指南中补充 npm publish 404 的排查方式,以及 tag 重发时脚本如何处理已存在版本。
+
+### 验证
+
+- 通过:`node ./scripts/npm/build-packages.mjs --skip-build --out-dir dist/release/npm-staging-check`,staging package version 为 `0.1.27`
+- 通过:`node ./scripts/npm/publish-packages.mjs --skip-build --out-dir dist/release/npm-staging-check --dry-run`
+- 通过:`node --check scripts/npm/publish-packages.mjs`
+- 通过:`swift test`
+
+### 影响文件
+
+- `scripts/npm/publish-packages.mjs`
+- `docs/releases/RELEASE_GUIDE.md`
+- `docs/histories/2026-04/20260422-1205-harden-npm-release-retry.md`
diff --git a/docs/releases/RELEASE_GUIDE.md b/docs/releases/RELEASE_GUIDE.md
index a98c820..1f7eba3 100644
--- a/docs/releases/RELEASE_GUIDE.md
+++ b/docs/releases/RELEASE_GUIDE.md
@@ -104,6 +104,9 @@ gh run view -R iFurySt/open-codex-computer-use <run-id> --log-failed
- `npm error 403 ... You cannot publish over the previously published versions`
- 通常不是 token 权限问题,而是 staging 包版本仍然是旧版本。
- 先回头检查 `plugin.json` 的 `version`,再检查 staging 包实际产出的 `package.json`。
+- `npm error 404 Not Found - PUT https://registry.npmjs.org/<package>`
+ - 先确认 registry 上目标包旧版本是否仍可见:`npm view <package> versions --json`。
+ - 当前 publish 脚本会在发布前跳过已经存在的同版本 package,并对 publish 失败做短暂重试;如果 tag 重发前某个 package 已经部分发布成功,重新跑同一个 release 不会因为该 package 已存在而中断。
- 构建阶段失败
- 优先看 `Build npm release artifacts`、`Build Cursor Motion DMG` 或 Swift 编译错误。
- GitHub Release 资产上传失败
diff --git a/scripts/npm/publish-packages.mjs b/scripts/npm/publish-packages.mjs
index 83bd3e0..a228ec7 100644
--- a/scripts/npm/publish-packages.mjs
+++ b/scripts/npm/publish-packages.mjs
@@ -1,11 +1,13 @@
#!/usr/bin/env node
import { spawnSync } from "node:child_process";
+import { readFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(__dirname, "..", "..");
+const maxPublishAttempts = 3;
function printHelp() {
process.stdout.write(`Usage: node ./scripts/npm/publish-packages.mjs [options]
@@ -81,6 +83,73 @@ function run(command, args, options = {}) {
}
}
+function sleep(ms) {
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
+}
+
+function readPackageMetadata(packageDir) {
+ const packageJSON = JSON.parse(readFileSync(path.join(packageDir, "package.json"), "utf-8"));
+ return {
+ name: packageJSON.name,
+ version: packageJSON.version,
+ };
+}
+
+function npmPackageVersionExists(packageName, version, env) {
+ const result = spawnSync("npm", ["view", `${packageName}@${version}`, "version", "--json"], {
+ cwd: repoRoot,
+ encoding: "utf-8",
+ env,
+ });
+
+ if (result.status === 0) {
+ return true;
+ }
+
+ const output = `${result.stdout ?? ""}\n${result.stderr ?? ""}`;
+ if (output.includes("E404") || output.includes("404 Not Found")) {
+ return false;
+ }
+
+ process.stderr.write(`Could not confirm whether ${packageName}@${version} already exists; attempting publish.\n`);
+ return false;
+}
+
+function publishWithRetry(args, npmEnv, packageName, version) {
+ if (npmPackageVersionExists(packageName, version, npmEnv)) {
+ process.stdout.write(`${packageName}@${version} already exists on npm; skipping publish.\n`);
+ return;
+ }
+
+ for (let attempt = 1; attempt <= maxPublishAttempts; attempt += 1) {
+ const result = spawnSync("npm", args, {
+ cwd: repoRoot,
+ stdio: "inherit",
+ env: npmEnv,
+ });
+
+ if (result.status === 0) {
+ return;
+ }
+
+ if (npmPackageVersionExists(packageName, version, npmEnv)) {
+ process.stdout.write(`${packageName}@${version} is visible on npm after publish failure; continuing.\n`);
+ return;
+ }
+
+ if (attempt < maxPublishAttempts) {
+ const delayMs = attempt * 5000;
+ process.stderr.write(
+ `npm publish ${packageName}@${version} failed with exit code ${result.status ?? "unknown"}; retrying in ${delayMs / 1000}s.\n`
+ );
+ sleep(delayMs);
+ continue;
+ }
+
+ throw new Error(`npm ${args.join(" ")} failed with exit code ${result.status ?? "unknown"}`);
+ }
+}
+
function main() {
const options = parseArgs(process.argv.slice(2));
const usingTrustedPublishing = Boolean(process.env.ACTIONS_ID_TOKEN_REQUEST_URL);
@@ -111,6 +180,7 @@ function main() {
.sort();
for (const packageDir of packageDirs) {
+ const { name: packageName, version } = readPackageMetadata(packageDir);
const args = ["publish", packageDir, "--access", "public"];
if (options.tag) {
args.push("--tag", options.tag);
@@ -134,7 +204,7 @@ function main() {
delete npmEnv.NPM_ID_TOKEN;
}
- run("npm", args, { env: npmEnv });
+ publishWithRetry(args, npmEnv, packageName, version);
}
}