Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(ci): 有限重试 npm 依赖下载中断

longmeidao70eaa5a741b9
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 53f62ac5b..19aaa2899 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -29,6 +29,7 @@ verify-linux:
rules:
- if: '$CI_COMMIT_BRANCH || $CI_PIPELINE_SOURCE == "merge_request_event"'
cache:
+ when: always
key:
files: [uv.lock, frontend/package-lock.json]
paths: [.uv-cache/, .npm/]
@@ -37,8 +38,8 @@ verify-linux:
- apt-get install -y --no-install-recommends python3-pip chromium ffmpeg xvfb xauth
- python3 -m pip install --break-system-packages uv==0.13.3
- uv sync --locked --all-extras --python 3.14
- - npm ci --ignore-scripts --prefer-offline
- - npm --prefix frontend ci --prefer-offline
+ - python3 -X utf8 scripts/npm_ci.py --ignore-scripts
+ - python3 -X utf8 scripts/npm_ci.py --prefix frontend
- export PEACH_E2E_CHROME=/usr/bin/chromium
- test -x "$PEACH_E2E_CHROME"
- ffmpeg -version
diff --git a/docs/REUSE.md b/docs/REUSE.md
index 53a9641d6..da9641406 100644
--- a/docs/REUSE.md
+++ b/docs/REUSE.md
@@ -8,6 +8,8 @@ GitLab API、CI 认证和 Release 创建复用官方 [glab 1.124.0](https://gitl
细粒度凭据缺少公开 CI 元数据的读取权限时,发版查询可在官方 API 上进行一次匿名 GET,使用标准库 HTTPS 与项目共用 User-Agent;不扩大凭据权限,也不携带本机认证头。私有资源仍须通过服务端权限检查。
+npm 安装沿用 Node.js 24 LTS 附带的官方 [`npm ci`](https://docs.npmjs.com/cli/v11/commands/npm-ci/),锁文件与完整性校验由 npm 维护。`npm_ci.py` 复用既有 `npm_audit.py` 的有限重试方式及 Python 3.14 标准库 `subprocess`(PSF 许可,无额外安装体积),只为明确的 `ECONNRESET`、`ETIMEDOUT`、`EAI_AGAIN` 下载错误重试,最多三次;锁文件、完整性与生命周期错误保留失败。Linux、Windows、macOS CI 和 Windows 打包入口共用此策略,不重试测试失败、不变更包版本。真实锁定安装与注入失败回归覆盖成功、重试耗尽和非网络错误,无新增应用依赖、生产入口或账本改动。
+
## 真实样本界面预览
用户验收预览复用项目记忆登记的真实账本小样本和已有缓存。启动入口强制调用 `scripts/check_ui_preview.py`,使用标准库 SQLite 的只读连接核对作品、实体、关联和来源清单,产出样本内容摘要;通用契约见 `docs/UI_PREVIEW.md`。核验与图像查看分别记录,隔离自动化测试夹具保留在测试范围内。没有新增依赖或生产数据写入。
diff --git a/scripts/build_windows.ps1 b/scripts/build_windows.ps1
index dfffe1d21..7a08200a7 100644
--- a/scripts/build_windows.ps1
+++ b/scripts/build_windows.ps1
@@ -47,7 +47,7 @@ $Npm = Get-Command npm -ErrorAction SilentlyContinue
if (-not $Npm) {
throw 'npm not found. Frontend bundles must be rebuilt before packaging; install Node 24+.'
}
-& $Npm.Source --prefix $FrontendPath ci
+& $Python -X utf8 (Join-Path $ProjectRoot 'scripts/npm_ci.py') --prefix $FrontendPath
if ($LASTEXITCODE -ne 0) { throw 'npm ci failed in frontend/.' }
& $Npm.Source --prefix $FrontendPath run build
if ($LASTEXITCODE -ne 0) { throw 'Frontend bundle build failed (frontend/).' }
diff --git a/scripts/ci_macos.sh b/scripts/ci_macos.sh
index 6b837bc8a..4fd817ae9 100644
--- a/scripts/ci_macos.sh
+++ b/scripts/ci_macos.sh
@@ -69,8 +69,8 @@ uv sync --locked --all-extras --python "$python"
python="$root/.venv/bin/python"
"$python" -c 'import platform,sys; assert platform.machine() == sys.argv[1], (platform.machine(),sys.argv[1])' "$architecture"
uv pip check --python "$python"
-npm ci
-npm ci --prefix frontend
+"$python" -X utf8 scripts/npm_ci.py --ignore-scripts
+"$python" -X utf8 scripts/npm_ci.py --prefix frontend
if [[ "$mode" == verify ]]; then
npm run build --prefix frontend
diff --git a/scripts/ci_windows.ps1 b/scripts/ci_windows.ps1
index 31fff6ab5..3876fa181 100644
--- a/scripts/ci_windows.ps1
+++ b/scripts/ci_windows.ps1
@@ -10,9 +10,9 @@ if ($Mode -eq 'verify' -or $Mode -eq 'build') {
Assert-Exit '锁定依赖安装失败'
uv pip check --python .venv/Scripts/python.exe
Assert-Exit '依赖检查失败'
- npm ci --ignore-scripts --prefer-offline
+ & .venv/Scripts/python.exe -X utf8 scripts/npm_ci.py --ignore-scripts
Assert-Exit '根目录 npm 安装失败'
- npm --prefix frontend ci --prefer-offline
+ & .venv/Scripts/python.exe -X utf8 scripts/npm_ci.py --prefix frontend
Assert-Exit '前端依赖安装失败'
}
diff --git a/scripts/npm_ci.py b/scripts/npm_ci.py
new file mode 100644
index 000000000..f501cd56e
--- /dev/null
+++ b/scripts/npm_ci.py
@@ -0,0 +1,41 @@
+"""安装锁定的 npm 依赖,仅对明确的临时下载错误有限重试。"""
+from __future__ import annotations
+
+import argparse
+from pathlib import Path
+import re
+import shutil
+import subprocess
+import sys
+import time
+
+TRANSPORT_ERROR = re.compile(r"(?m)^npm (?:error|ERR!) code (?:ECONNRESET|ETIMEDOUT|EAI_AGAIN)\s*$")
+
+
+def install(project: Path, *, ignore_scripts: bool = False, runner=subprocess.run, sleep=time.sleep) -> int:
+ command = [shutil.which("npm") or "npm", "ci", "--prefer-offline"]
+ if ignore_scripts:
+ command.append("--ignore-scripts")
+ for attempt in range(3):
+ result = runner(command, cwd=project, capture_output=True, text=True, encoding="utf-8", errors="replace")
+ print(result.stdout, end="", flush=True)
+ print(result.stderr, end="", file=sys.stderr, flush=True)
+ if result.returncode == 0:
+ return 0
+ if attempt == 2 or not TRANSPORT_ERROR.search(result.stderr):
+ return result.returncode
+ print(f"npm 依赖下载中断,保留已校验缓存重试:{attempt + 1}/3", flush=True)
+ sleep(5 * (attempt + 1))
+ return 1
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--prefix", type=Path, default=Path("."))
+ parser.add_argument("--ignore-scripts", action="store_true")
+ args = parser.parse_args()
+ return install(args.prefix.resolve(), ignore_scripts=args.ignore_scripts)
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/tests/test_dependency_policy.py b/tests/test_dependency_policy.py
index ad48e0896..a70fac40a 100644
--- a/tests/test_dependency_policy.py
+++ b/tests/test_dependency_policy.py
@@ -17,6 +17,42 @@ from tests.support.gitrepo import seed_repository
ROOT = Path(__file__).resolve().parents[1]
+class NpmInstallTests(unittest.TestCase):
+ def test_transport_errors_retry_the_same_locked_install(self):
+ from scripts import npm_ci
+ for style in ("error", "ERR!"):
+ for code in ("ECONNRESET", "ETIMEDOUT", "EAI_AGAIN"):
+ with self.subTest(style=style, code=code):
+ interrupted = subprocess.CompletedProcess([], 152, "", f"npm {style} code {code}\n")
+ clean = subprocess.CompletedProcess([], 0, "installed\n", "")
+ run, sleep = mock.Mock(side_effect=[interrupted, clean]), mock.Mock()
+ self.assertEqual(npm_ci.install(ROOT, ignore_scripts=True, runner=run, sleep=sleep), 0)
+ self.assertEqual(run.call_count, 2)
+ self.assertEqual(run.call_args_list[0], run.call_args_list[1])
+ self.assertIn("--ignore-scripts", run.call_args.args[0])
+ self.assertEqual(run.call_args.kwargs["cwd"], ROOT)
+ sleep.assert_called_once_with(5)
+
+ def test_transport_exhaustion_keeps_the_failure(self):
+ from scripts import npm_ci
+ run = mock.Mock(return_value=subprocess.CompletedProcess([], 152, "", "npm error code ECONNRESET\n"))
+ sleep = mock.Mock()
+ self.assertEqual(npm_ci.install(ROOT, runner=run, sleep=sleep), 152)
+ self.assertEqual(run.call_count, 3)
+ self.assertEqual(sleep.call_args_list, [mock.call(5), mock.call(10)])
+
+ def test_lock_and_lifecycle_failures_are_not_retried(self):
+ from scripts import npm_ci
+ for code in ("EUSAGE", "ELIFECYCLE", "EINTEGRITY"):
+ with self.subTest(code=code):
+ run = mock.Mock(return_value=subprocess.CompletedProcess(
+ [], 1, "a lifecycle mentioned ECONNRESET", f"npm error code {code}\n"))
+ sleep = mock.Mock()
+ self.assertEqual(npm_ci.install(ROOT, runner=run, sleep=sleep), 1)
+ run.assert_called_once()
+ sleep.assert_not_called()
+
+
class NpmAuditTests(unittest.TestCase):
def test_docker_dependency_updates_have_a_takeover_recipe(self):
for manifest in ('Dockerfile', '.github/actions/actionlint/Dockerfile'):