#!/usr/bin/env node /** * 综合 harness 检查脚本 * 运行所有 harness 检查并生成报告 * * 并行执行只读检查,按 CPU 核心数限制并发;会原地构建 workspace * 产物的检查单独串行,避免测试读取半写入的 lib/ 文件。 * 设 HARNESS_SEQUENTIAL=1 回退到串行模式(调试用)。 */ import { exec } from 'node:child_process'; import { availableParallelism } from 'node:os'; import * as path from 'node:path'; import { fileURLToPath } from 'node:url'; import { printHarnessError } from './harness-error-output.mjs'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const repoRoot = path.resolve(__dirname, '..'); const skipUnitTests = process.env.HARNESS_SKIP_TEST === '1'; const sequential = process.env.HARNESS_SEQUENTIAL === '1'; const concurrency = sequential ? 1 : Math.min(availableParallelism(), 4); const HEAVY_CHECKS = new Set([ 'Unit Tests', 'Install Size (IM core)', 'Lint', 'Type Check', 'Plugin Runtime Migration Verify', 'L4-CI (deterministic subset)', 'Stable Smoke', ]); const EXCLUSIVE_CHECKS = new Set([ // check:install-size runs workspace builds before packing. Running it beside // native-import tests can expose partially rewritten lib/ modules. 'Install Size (IM core)', ]); const checks = [ { name: 'Agent Config Inventory', command: 'pnpm check:agent-config-inventory', description: 'ai.agent.* 开关清单完整且数量不超基线(#735)', }, { name: 'Unit Tests', command: 'pnpm test', description: '全量 Vitest(pnpm test)', }, { name: 'Install Size (IM core)', command: 'pnpm check:install-size', description: 'zhin.js production node_modules ≤10MB(ADR 0019)', }, { name: 'Lint', command: 'pnpm lint:ci', description: 'ESLint(.ts/.tsx)', retries: 1, }, { name: 'Type Check', command: 'pnpm type-check', description: 'tsc --noEmit(tsconfig.typecheck.json)', }, { name: 'Plugin Runtime Migration Verify', command: 'pnpm check:plugin-runtime-migration-verify', description: '离线 E2E:cutover 后构建,公开包 tarball 含 JS entry 与 manifest 契约', }, { name: 'L4-CI (deterministic subset)', command: 'pnpm check:l4-ci', description: 'PR 门禁 L4 子集;全量 check:l4 见 nightly full-validation', }, { name: 'Stable Smoke', command: 'pnpm check:stable', description: 'Stable 路径 smoke(Sandbox + Agent 核心单测 + minimal-bot 契约)', }, { name: 'Console UI Styles', command: 'pnpm check:console-ui-styles', description: '@zhin.js/console-ui stylelint:只用令牌与尺度内取值;生成物与 tokens/tokens.mjs 一致(ADR 0063 D17、D20)', }, { name: 'IM Send Path', command: 'pnpm check:harness-paths', description: '检查是否绕过 Adapter.sendMessage 链路', }, { name: 'No Koa Import', command: 'pnpm check:no-koa', description: '检查插件是否直接 import koa', }, { name: 'Production Config', command: 'pnpm check:prod', description: '检查生产环境配置(无调试代码)', }, { name: 'Plugin Spec', command: 'pnpm check:plugin', description: '检查插件是否符合标准规范(含官方插件页面 UI 门禁,ADR 0063)', }, { name: 'Plugin UI Audit', command: 'pnpm check:plugin-ui-audit', description: '官方插件页面的 Playwright 全项审计门禁(ADR 0063;默认用 @zhin.js/client 的渲染器挂载模块,需先构建 @zhin.js/console-ui 并装好 Chromium)', }, { name: 'Plugin Capability Publish', command: 'pnpm check:plugin-capability-publish', description: '插件能力目录 npm 发布清单(files、prepublishOnly、peer 依赖)', }, { name: 'Publish Repository', command: 'pnpm check:publish-repository', description: '可发布包 repository.url 须匹配 github.com/zhinjs/zhin(npm provenance)', }, { name: 'Agent Tool Schema', command: 'pnpm check:agent-tool-schema', description: 'tools inputSchema 与 defineAgentTool/execute 类型字段一致', }, { name: 'Skill Authoring Boundaries', command: 'pnpm check:skill-authoring-boundaries', description: 'Skill 只使用 skills//SKILL.md,并显式发布目录、依赖和挂载 Feature', }, { name: 'Agent Authoring Boundaries', command: 'pnpm check:agent-authoring-boundaries', description: '主 Agent 使用 AGENTS.md,子 Agent 只使用 agents// 目录契约', }, { name: 'Hook Authoring Boundaries', command: 'pnpm check:hook-authoring-boundaries', description: 'Hook 只使用 hooks//index.ts,并支持 Agent 与 Skill 私有作用域', }, { name: 'Architecture Layers', command: 'pnpm check:architecture', description: '检查架构层级依赖是否正确', }, { name: 'Domain Module Boundaries', command: 'pnpm check:domain-module-boundaries', description: '深模块只能经 canonical index.ts 进入,禁止恢复旧平面入口或外部深层导入', }, { name: 'Adapter Endpoint Boundaries', command: 'pnpm check:adapter-endpoint-boundaries', description: '检查 Adapter definition 与 Endpoint instance 职责不混淆', }, { name: 'Runtime Config Boundaries', command: 'pnpm check:runtime-config-boundaries', description: '正常运行与配置路径只接受 instanceKey map;旧 plugins 数组仅供迁移和诊断读取', }, { name: 'Console Client Boundaries', command: 'pnpm check:console-client-boundaries', description: 'Console 路由、运行时环境与网络连接必须归属显式 ConsoleClient 实例', }, { name: 'Console Protocol Boundaries', command: 'pnpm check:console-protocol-boundaries', description: 'Console 只接受 dot-named RPC 与顶层 camelCase payload,不恢复兼容归一化', }, { name: '60s Runtime Boundaries', command: 'pnpm check:sixty-s-runtime-boundaries', description: '60s API transport 必须归属当前 Plugin owner,不使用进程级注册栈', }, { name: 'Agent Tool Authoring Boundaries', command: 'pnpm check:agent-tool-authoring-boundaries', description: 'Agent Tool 只使用 @zhin.js/tool 与 tools//index.ts,并显式挂载 Feature', }, { name: 'IM Session SSOT', command: 'pnpm check:im-session-ssot', description: '检查 IM 场景/session 身份解析是否使用 core SSOT', }, { name: 'LLM Runtime Boundaries', command: 'pnpm check:llm-runtime-boundaries', description: 'agent/zhin 运行时代码须经 owner-scoped LlmApiRuntime 解析模型', }, { name: 'Speech Runtime Boundaries', command: 'pnpm check:speech-runtime-boundaries', description: 'Speech 实现仅由 CLI composition root 装配,并以 owner-scoped 端口注入', }, { name: 'Legacy AI Exports', command: 'pnpm check:legacy-ai-exports', description: '禁止 @zhin.js/ai 再导出 SessionManager / resolveIMSessionId* / convertLegacy* / getModel', }, { name: 'Adapter Docs Sync', command: 'pnpm check:adapter-docs', description: '检查平台适配器文档是否与 plugins/adapters README 同步', }, { name: 'Publishing Manifest', command: 'pnpm check:publishing-manifest', description: 'docs/contributing/publishing.md 包清单覆盖全部非 private 包(可信发布者)', }, { name: 'Operability Docs', command: 'pnpm check:operability-docs', description: '检查适配器、Console 与插件交付文档是否形成可验收闭环', }, { name: 'Generated API Docs', command: 'pnpm check:api-docs', description: '检查公开入口、源码注释链接与 API Reference 生成契约', }, { name: 'Generated Config Reference', command: 'pnpm check:config-reference', description: '检查配置字段参考与 Runtime 源码、插件 JSON Schema 无漂移', }, { name: 'Source-owned Config Enums', command: 'pnpm check:config-enums', description: '检查源码既定配置枚举在 Runtime/插件 Schema、生成参考和叙事文档间无漂移', }, { name: 'Troubleshooting Center', command: 'pnpm check:troubleshooting', description: '检查结构化故障目录与中英文排查页面无漂移', }, { name: 'Deployment Templates', command: 'pnpm check:deployment-templates', description: '校验 Compose、systemd、Kubernetes 模板与中英文下载入口', }, { name: 'Platform Tiers SSOT', command: 'pnpm check:platform-tiers-ssot', description: '能力分档/适配器索引与 scripts/adapter-meta.mjs 一致', }, { name: 'Doc Links', command: 'pnpm check:doc-links', description: '检查文档相对链接是否断裂', }, { name: 'Install Tiers SSOT', command: 'pnpm check:install-tiers-ssot', description: 'README Install tiers 表与 docs/snippets/install-tiers.md 一致', }, { name: 'Dependency Policy', command: 'pnpm check:dependency-policy', description: '校验脚手架依赖策略、Changesets 配置与内部 peer 版本范围', }, { name: 'Release Plan', command: 'pnpm check:release-plan', description: '所有 Changesets 声明与最终发布计划都只允许 patch', }, { name: 'API Surface', command: 'pnpm check:api-surface', description: '检查 public API surface 快照', }, { name: 'Plugin Config Schemas', command: 'pnpm check:plugin-config-schemas', description: '由 Schema 声明生成的插件 schema.json 与 src/config.ts 保持同步', }, { name: 'Plugin Runtime API', command: 'pnpm check:plugin-runtime-api', description: '检查约定式插件运行时 API surface 快照', }, { name: 'Doc Orphans', command: 'pnpm check:doc-orphans', description: '检查站点 Markdown 是否在侧栏或 allowlist', }, { name: 'README Exports', command: 'pnpm check:readme-exports', description: '检查 README import 与包导出一致', }, { name: 'Config Docs', command: 'pnpm check:config-docs', description: '配置文档与 DEFAULT_CONFIG 关键字段对齐', }, { name: 'No Platform Branches', command: 'pnpm check:no-platform-branches', description: 'core/agent/ai/adapter/cli 运行时不得出现平台名分支,只读适配器能力(ADR 0044)', }, { name: 'Removed Plugin API', command: 'pnpm check:no-removed-plugin-api', description: '生产源码不得调用已删除的 Plugin lookup 或 latest-generation store API', }, { name: 'Plugin Runtime Migration Readiness', command: 'pnpm check:plugin-runtime-migration-readiness', description: '已迁移插件必须保持 native manifest,且函数体不得调用 legacy usePlugin/getPlugin', }, { name: 'Feature Peer Deps', command: 'pnpm check:feature-peers', description: 'zhin.features 引用的包须出现在 peerDependencies(runtime 1.0.12+)', }, { name: 'Outbound Media Policies', command: 'pnpm check:outbound-media-policies', description: '各 adapter 在 adapters/*.ts 声明 segments.outboundMedia(或豁免)', }, { name: 'AI Outbound Adapters', command: 'pnpm check:ai-outbound', description: '声明 aiOutboundExtensions 的 adapter 含契约测试', }, { name: 'Interactive Segments', command: 'pnpm check:interactive-segments', description: '各 adapter 在 adapters/*.ts 声明 segments.interactive(或豁免)', }, { name: 'Segment Adapters', command: 'pnpm check:segments', description: '各 adapter 在 adapters/*.ts 声明 defineAdapter segments(或豁免)', }, { name: 'Provider Gateway', command: 'pnpm check:provider-gateway', description: '已知 LLM 网关 sdk/contextWindow 预设与 OpenCode 等契约', }, { name: 'A2A Mesh', command: 'pnpm check:a2a-mesh', description: '禁止残留 MCP Agent Mesh v1 符号', }, ].filter((c) => !(skipUnitTests && c.name === 'Unit Tests')); function executeCheck(check) { return new Promise((resolve) => { exec(check.command, { cwd: repoRoot, maxBuffer: 32 * 1024 * 1024, }, (error, stdout, stderr) => resolve({ error, stdout, stderr })); }); } async function runCheck(check) { const start = performance.now(); const attempts = 1 + (check.retries ?? 0); for (let attempt = 1; attempt <= attempts; attempt++) { const { error, stdout, stderr } = await executeCheck(check); const elapsed = ((performance.now() - start) / 1000).toFixed(1); if (!error) { console.log(` ✓ ${check.name} (${elapsed}s)`); return { name: check.name, status: 'PASSED', elapsed }; } if (attempt < attempts) { console.log(` ↻ ${check.name} failed; retrying once`); continue; } const output = [stdout, stderr] .map((value) => value.trim()) .filter(Boolean) .join('\n'); const details = [ output, error.message, `exit code: ${String(error.code ?? 'unknown')}`, `signal: ${String(error.signal ?? 'none')}`, ].filter(Boolean).join('\n'); console.log(` ✗ ${check.name} FAILED (${elapsed}s)`); return { name: check.name, status: 'FAILED', elapsed, error: details }; } throw new Error(`unreachable check state: ${check.name}`); } async function runPool(items, limit) { const results = []; let idx = 0; async function worker() { while (idx < items.length) { const check = items[idx++]; results.push(await runCheck(check)); } } await Promise.all(Array.from({ length: Math.min(limit, items.length) }, () => worker())); return results; } async function main() { if (skipUnitTests) { console.log('HARNESS_SKIP_TEST=1 — skipping Unit Tests (expect a separate coverage/test job)\n'); } const exclusive = checks.filter((c) => EXCLUSIVE_CHECKS.has(c.name)); const heavy = checks.filter((c) => ( HEAVY_CHECKS.has(c.name) && !EXCLUSIVE_CHECKS.has(c.name) )); const light = checks.filter((c) => !HEAVY_CHECKS.has(c.name)); const mode = sequential ? 'sequential' : `parallel (concurrency=${concurrency})`; console.log(`Running ${checks.length} harness checks [${mode}]...`); console.log(` Phase 1: ${light.length} lightweight checks`); console.log(` Phase 2: ${heavy.length} parallel-safe heavyweight checks`); console.log(` Phase 3: ${exclusive.length} exclusive workspace build checks\n`); const totalStart = performance.now(); console.log('── Phase 1: lightweight ──'); const lightResults = await runPool(light, concurrency); console.log('\n── Phase 2: heavyweight ──'); const heavyResults = await runPool(heavy, Math.min(concurrency, 2)); console.log('\n── Phase 3: exclusive workspace builds ──'); const exclusiveResults = await runPool(exclusive, 1); const results = [...lightResults, ...heavyResults, ...exclusiveResults]; const totalElapsed = ((performance.now() - totalStart) / 1000).toFixed(1); const cpuTotal = results.reduce((s, r) => s + parseFloat(r.elapsed), 0).toFixed(1); const allPassed = results.every((r) => r.status === 'PASSED'); console.log('\n' + '='.repeat(60)); console.log(`Harness Check Summary (${totalElapsed}s wall, ${cpuTotal}s CPU)`); console.log('='.repeat(60)); for (const result of results) { const status = result.status === 'PASSED' ? '✓' : '✗'; console.log(`${status} ${result.name}: ${result.status} (${result.elapsed}s)`); } console.log('='.repeat(60)); if (!allPassed) { console.error('\nSome harness checks failed. See above for details.\n'); console.error('Detailed errors:'); for (const result of results) { if (result.status === 'FAILED') { console.error(`\n${result.name}:`); printHarnessError(result.error); } } process.exit(1); } console.log('\nAll harness checks passed! ✓\n'); } main();