From 67f08d2010400e6d54a38c0e3a8a859adb5c9559 Mon Sep 17 00:00:00 2001 From: Paka-418 Date: Wed, 18 Nov 2026 20:29:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(ci):=20=E6=AF=8F=E6=97=A5=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E7=BA=BF=E4=B8=8A=E7=B4=A2=E5=BC=95=E5=81=A5=E5=BA=B7?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 索引与站点是两条链路:站点部署成功不代表 MCP 读的 search-index.json 也跟着更新。本次 xburn / tros / model_zoo 的索引停在 10 月,就是因为 没有人在看这条链路。 脚本直接复用 MCP 自己的加载路径,检查用户实际会遇到的东西: - 索引能否加载,记录数是否为 0 - 记录数是否跌破包内快照基线的 80% - 索引 last-modified 是否比站点首页(≈最近一次部署)旧半天以上 - 绝对年龄超过 14 天记为警告 每日 09:00 跑一次,有失败时 job 变红;报告同时写入 step summary。 Co-Authored-By: Claude Code --- .github/workflows/health-index.yml | 36 ++++++++ mcp/scripts/check-index-health.mjs | 144 +++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 .github/workflows/health-index.yml create mode 100644 mcp/scripts/check-index-health.mjs diff --git a/.github/workflows/health-index.yml b/.github/workflows/health-index.yml new file mode 100644 index 0000000..01fea5c --- /dev/null +++ b/.github/workflows/health-index.yml @@ -0,0 +1,36 @@ +name: Index health check + +# 每日检查 MCP 依赖的线上索引(对应 metric H5)。 +# 失败条件:索引加载失败、记录数为 0、记录数跌破包内快照基线, +# 或索引比站点最近一次部署旧半天以上(说明索引步骤没跑成)。 +on: + schedule: + - cron: "0 1 * * *" # 09:00 CST + workflow_dispatch: + +permissions: + contents: read + +jobs: + check: + runs-on: ubuntu-latest + timeout-minutes: 15 + defaults: + run: + working-directory: mcp + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: npm + cache-dependency-path: mcp/package-lock.json + - name: Install + run: npm ci + - name: Build + run: npm run build + - name: Check every manual's live index + run: node scripts/check-index-health.mjs + env: + INDEX_STALE_DAYS: "14" + INDEX_MIN_RATIO: "0.8" diff --git a/mcp/scripts/check-index-health.mjs b/mcp/scripts/check-index-health.mjs new file mode 100644 index 0000000..5f58340 --- /dev/null +++ b/mcp/scripts/check-index-health.mjs @@ -0,0 +1,144 @@ +#!/usr/bin/env node +/** + * 每日索引健康检查(对应 metric H5 / A2)。 + * + * 直接调用 MCP 自己的索引加载路径,检查的就是用户会遇到的东西: + * 1. 索引能否加载(Docusaurus / Sphinx / Rspress 三种格式) + * 2. 记录数是否为 0,或跌破包内快照基线的 INDEX_MIN_RATIO + * 3. 静态索引的 last-modified 是否超过 INDEX_STALE_DAYS 天 + * + * 用法:cd mcp && npm run build && node scripts/check-index-health.mjs + * 环境变量:INDEX_STALE_DAYS(默认 14)、INDEX_MIN_RATIO(默认 0.8) + * 有 fail 时退出码为 1,供 CI 告警。 + */ +import fs from "node:fs"; +import path from "node:path"; +import zlib from "node:zlib"; +import { fileURLToPath } from "node:url"; +import { MANUALS, origin } from "../dist/catalog.js"; +import { loadIndexFromOrigin } from "../dist/service.js"; +import { fetchText } from "../dist/http.js"; + +const here = path.dirname(fileURLToPath(import.meta.url)); +const prebuiltDir = path.resolve(here, "../prebuilt"); +const staleDays = Number(process.env.INDEX_STALE_DAYS ?? 14); +const minRatio = Number(process.env.INDEX_MIN_RATIO ?? 0.8); + +function baselineCount(id) { + try { + const raw = zlib.gunzipSync(fs.readFileSync(path.join(prebuiltDir, `${id}.json.gz`))); + const snapshot = JSON.parse(raw.toString("utf8")); + return snapshot.docCount ?? snapshot.docs?.length ?? 0; + } catch { + return 0; + } +} + +/** 静态索引取 last-modified;Rspress 的索引哈希由构建产物决定,跳过。 */ +async function freshness(manual) { + if (!manual.indexPath) return { skipped: true }; + const url = `${origin()}${manual.indexPath}`; + const res = await fetch(url, { method: "HEAD", signal: AbortSignal.timeout(30_000) }); + if (!res.ok) return { error: `HTTP ${res.status}` }; + const lastModified = res.headers.get("last-modified"); + const ts = lastModified ? Date.parse(lastModified) : NaN; + return { + lastModified, + ts: Number.isFinite(ts) ? ts : null, + ageDays: Number.isFinite(ts) ? (Date.now() - ts) / 86_400_000 : null, + }; +} + +/** + * 站点首页的 last-modified ≈ 最近一次部署时间(每次部署都会重写首页)。 + * 索引由部署后的 workflow 生成,正常应比首页新;落后半天以上说明索引步骤没跑成。 + */ +async function siteBuiltAt(manual) { + if (manual.indexKind !== "docusaurus") return null; + const url = `${origin()}${manual.basePath}/`; + const res = await fetch(url, { method: "HEAD", signal: AbortSignal.timeout(30_000) }); + if (!res.ok) return null; + const lastModified = res.headers.get("last-modified"); + const ts = lastModified ? Date.parse(lastModified) : NaN; + return Number.isFinite(ts) ? ts : null; +} + +const manuals = MANUALS.filter((manual) => manual.searchable); +const rows = []; +let failures = 0; +let warnings = 0; + +for (const manual of manuals) { + const row = { + id: manual.id, + title: manual.title, + live: null, + base: baselineCount(manual.id), + ageDays: null, + status: "ok", + notes: [], + }; + + try { + const docs = await loadIndexFromOrigin(manual, fetchText); + row.live = docs.length; + if (docs.length === 0) { + row.status = "fail"; + row.notes.push("0 条记录"); + } else if (row.base > 0 && docs.length < row.base * minRatio) { + row.status = "fail"; + row.notes.push(`记录数 ${docs.length} < 基线 ${row.base} 的 ${Math.round(minRatio * 100)}%`); + } + } catch (error) { + row.status = "fail"; + row.notes.push(`加载失败:${error.message}`); + } + + try { + const fresh = await freshness(manual); + if (fresh.error) { + row.status = "fail"; + row.notes.push(fresh.error); + } else if (!fresh.skipped && fresh.ageDays != null) { + row.ageDays = fresh.ageDays; + const builtAt = await siteBuiltAt(manual); + if (fresh.ts != null && builtAt != null && fresh.ts + 12 * 3_600_000 < builtAt) { + row.status = "fail"; + const days = (builtAt - fresh.ts) / 86_400_000; + row.notes.push(`索引比站点部署旧 ${Math.round(days)} 天`); + } else if (fresh.ageDays > staleDays) { + if (row.status === "ok") row.status = "warn"; + row.notes.push(`索引 ${Math.round(fresh.ageDays)} 天未更新`); + } + } + } catch (error) { + row.status = "fail"; + row.notes.push(`HEAD 失败:${error.message}`); + } + + if (row.status === "fail") failures += 1; + if (row.status === "warn") warnings += 1; + rows.push(row); +} + +rows.sort((a, b) => (a.id < b.id ? -1 : 1)); + +const lines = []; +lines.push("| 手册 | 记录数 | 基线 | 索引年龄 | 状态 | 备注 |"); +lines.push("| --- | ---: | ---: | ---: | --- | --- |"); +for (const row of rows) { + lines.push( + `| ${row.title}(${row.id}) | ${row.live ?? "—"} | ${row.base || "—"} | ${ + row.ageDays == null ? "—" : `${Math.round(row.ageDays)} 天` + } | ${row.status === "ok" ? "✅" : row.status === "warn" ? "⚠️" : "❌"} | ${row.notes.join(";")} |`, + ); +} +lines.push(""); +lines.push(`共 ${rows.length} 本手册:${failures} 失败,${warnings} 警告。`); + +const report = lines.join("\n"); +console.log(report); +if (process.env.GITHUB_STEP_SUMMARY) { + fs.appendFileSync(process.env.GITHUB_STEP_SUMMARY, `${report}\n`); +} +process.exit(failures > 0 ? 1 : 0);