import { createHash } from "node:crypto"; import { chmodSync, existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync, } from "node:fs"; import { dirname, join, resolve, sep } from "node:path"; import { writeBundledOfficialMarketplacePartitionSync } from "@zcode/adapters"; import { ZCODE_OFFICIAL_PLUGIN_MARKETPLACE, type Logger } from "@zcode/contracts"; import { isZCodeCuaInternalFeatureEnabled, ZCODE_CUA_OFFICIAL_PLUGIN_ID } from "@zcode/shared"; import { createOfficialPluginCacheRetryBudget, getOfficialPluginCacheRetryAttempts, isTransientOfficialPluginCacheFsError, type OfficialPluginCacheRetryBudget, removeOfficialPluginCacheDirectory, renameOfficialPluginCachePath, } from "./official-plugin-cache-fs.js"; import { OFFICIAL_PLUGIN_DEFINITIONS, type OfficialPluginDefinition, } from "./official-plugin-definitions.js"; import { writeOfficialPluginRuntimeManifest } from "./official-plugin-runtime.js"; import { isOfficialPluginSeedLockTimeoutError, withOfficialPluginSeedLock, } from "./official-plugin-seed-lock.js"; const OFFICIAL_PLUGIN_MARKETPLACE = ZCODE_OFFICIAL_PLUGIN_MARKETPLACE; const SEA_PLUGIN_ASSET_PREFIX = "zcode-official-plugins/"; const SEA_PLUGIN_MANIFEST_ASSET_KEY = `${SEA_PLUGIN_ASSET_PREFIX}manifest.json`; const SEED_MARKER_FILE = ".zcode-plugin-seed.json"; const SEED_LOCK_TOTAL_BUDGET_MS = 15_000; const includedTopLevelPaths = new Set([ ".mcp.json", ".zcode-plugin", "README.md", // 官方内容插件新增 agents 后,filesystem seed 的顶层白名单未同步,目录被静默裁掉。 "agents", "commands", "dist", "docs", "hooks", "output-styles", "package.json", // Browser skill 会从官方插件根目录动态导入 scripts/browser-client.mjs。 // filesystem seed 若漏掉 scripts,Dev 会连接 node_repl 成功却在首次 Browser Use 时导入失败。 "scripts", "skills", "templates", ]); interface OfficialPluginSeedFile { mode?: number; path: string; sha256: string; sourcePath?: string; } interface OfficialPluginSeedPluginSource { definition: OfficialPluginDefinition; files: OfficialPluginSeedFile[]; hash: string; missingSeedPaths: string[]; rootPath?: string; } interface OfficialPluginSeedSource { kind: "filesystem" | "sea"; plugins: OfficialPluginSeedPluginSource[]; } interface SeaOfficialPluginManifest { hash: string; plugins: Array<{ files: OfficialPluginSeedFile[]; marketplace: string; name: string; version: string; }>; version: 1; } type SeaModule = typeof import("node:sea"); function seedBundledOfficialPlugins(input: { logger?: Logger; storageRoot: string; }): OfficialPluginDefinition[] { const source = resolveSeedSource(); if (!source) return []; // Catalog/cache 是内置插件的不可变产品资产;Runtime 是否加载由 discovery 的抑制态决定, // 不能在 seed 阶段删除或过滤,否则卸载后详情页无法读取组件,也无法恢复。 writeOfficialMarketplace(input.storageRoot, source); const retryBudget = createOfficialPluginCacheRetryBudget(); // 等锁超时降级后循环会继续;若每个插件独立重置 15s 等待预算,成组遗留的 // 锁会让启动同步冻结 N×15s。全部插件共享同一截止时间:无争用的锁仍瞬时获取(mkdir // 一次成功不查预算),预算耗尽后有争用的锁立即降级,seeding 总等待封顶 15s。 const seedLockDeadlineAt = Date.now() + SEED_LOCK_TOTAL_BUDGET_MS; const failedSeeds: OfficialPluginDefinition[] = []; for (const plugin of source.plugins) { const pluginId = `${plugin.definition.name}@${OFFICIAL_PLUGIN_MARKETPLACE}`; const targetRoot = officialPluginCacheRoot(input.storageRoot, plugin.definition); // 入口旁的插件拷贝可能与新定义错配(升级中的桌面包、旧 checkout 未构建 dist)。 // 缺 requiredSeedPaths 时 seed 源解析曾直接抛错,一个残缺插件把全部插件连同会话恢复 // 一起炸成 resumeFailed。残缺只作用于单插件:拒绝写缓存,按既有降级协议告警并回退到可用旧缓存。 if (plugin.missingSeedPaths.length > 0) { warnCacheDegraded(input.logger, { error: Object.assign( new Error( `Bundled official plugin ${plugin.definition.name} is missing required seed assets: ${plugin.missingSeedPaths.join(", ")}`, ), { code: "ZCODE_PLUGIN_SEED_INCOMPLETE" }, ), missingSeedPaths: plugin.missingSeedPaths, operation: "seed_plugin", pluginId, targetRoot, }); failedSeeds.push(plugin.definition); continue; } try { withOfficialPluginSeedLock( targetRoot, () => { // 桌面会并发预热多个 workspace Agent;复制插件资源时, // 多进程会互删 target 并在 Windows rename 时触发 EPERM。拿锁后必须二次检查, // 让等待者直接复用首个进程已经提交的完整缓存。 if (isSeedCurrent(targetRoot, plugin)) { cleanupLegacySeedBackup(targetRoot, retryBudget); const manifestWritten = tryWriteOfficialPluginRuntimeManifest({ pluginName: plugin.definition.name, retryBudget, rootPath: targetRoot, }); if (manifestWritten) return; } const temporaryRoot = `${targetRoot}.tmp-${process.pid}-${Date.now()}`; removeOfficialPluginCacheDirectory(temporaryRoot, retryBudget); mkdirSync(temporaryRoot, { recursive: true }); try { for (const file of plugin.files) { const bytes = readSeedFileBytes(source, plugin, file); if (hashBytes(bytes) !== file.sha256) { throw new Error( `Bundled plugin asset hash mismatch: ${plugin.definition.name}/${file.path}`, ); } const outputPath = join(temporaryRoot, ...file.path.split("/")); mkdirSync(dirname(outputPath), { recursive: true }); writeFileSync(outputPath, bytes); chmodSync(outputPath, modeForSeedFile(file.path, file.mode)); } writeFileSync( join(temporaryRoot, SEED_MARKER_FILE), JSON.stringify(seedMarker(source, plugin), null, 2), ); replaceSeedRoot(temporaryRoot, targetRoot, plugin, retryBudget); writeOfficialPluginRuntimeManifest({ pluginName: plugin.definition.name, retryBudget, rootPath: targetRoot, }); } catch (error) { try { removeOfficialPluginCacheDirectory(temporaryRoot, retryBudget); } catch { // 临时目录清理失败不能覆盖真正的 seed 错误;目录名唯一,不会污染后续加载。 } throw error; } }, { timeoutMs: Math.max(0, seedLockDeadlineAt - Date.now()) }, ); } catch (error) { if ( isTransientOfficialPluginCacheFsError(error) || // seed lock 等待超时只说明同版本缓存锁被别的进程持有或遗留(Windows 上 // 删不掉的遗留锁 + PID 复用会让接管长期不触发)。seeding 只是刷新缓存,超时必须 // 走既有降级协议回退到可用缓存并告警,不能把会话恢复整体炸成 resumeFailed。 isOfficialPluginSeedLockTimeoutError(error) || // 多个 workspace app 会并发 seed 同一份官方插件缓存。当前进程 // 写 runtime manifest 时,并发赢家可能已经原子替换整个 targetRoot,连同本进程 // 的临时文件一起移走,rename 因此返回 ENOENT。只在新 target 已由 marker 证明 // 完整时降级;目标缺失或仍旧时继续抛错,不能掩盖真实缓存损坏。 (isNotFoundFsError(error) && isSeedCurrent(targetRoot, plugin)) ) { warnCacheDegraded(input.logger, { error, operation: "seed_plugin", pluginId, targetRoot, }); failedSeeds.push(plugin.definition); continue; } throw error; } } return failedSeeds; } function tryWriteOfficialPluginRuntimeManifest(input: { pluginName: string; retryBudget: OfficialPluginCacheRetryBudget; rootPath: string; }): boolean { try { writeOfficialPluginRuntimeManifest(input); return true; } catch (error) { if (isTransientOfficialPluginCacheFsError(error)) throw error; return false; } } export function resolveOfficialPluginRoots(input: { env?: NodeJS.ProcessEnv; extraRoots?: string[]; logger?: Logger; storageRoot: string; suppressedBuiltins?: ReadonlySet; }): string[] { const suppressedBuiltins = new Set(input.suppressedBuiltins ?? []); // zcode-cua 内置 plugin 默认不启用,由 feature flag 控制加载。在 seed/discovery 层门控 // (而非只隐藏某个 UI 面),这样开关关闭时用户无法经 plugin 列表/marketplace/MCP 设置/CLI 命令看到它。 if (!isZCodeCuaInternalFeatureEnabled(input.env ?? process.env)) { suppressedBuiltins.add(ZCODE_CUA_OFFICIAL_PLUGIN_ID); } const failedSeeds = seedBundledOfficialPlugins({ logger: input.logger, storageRoot: input.storageRoot, }); const fallbackRoots = failedSeeds.flatMap((definition) => { // CUA 的 frame contract 随 wrapper 与 producer 原子升级。加载旧版本 // cache 会把旧 block 布局接到新 consumer 上;当前 cache 不可用时宁可不注册 CUA。 if (`${definition.name}@${OFFICIAL_PLUGIN_MARKETPLACE}` === ZCODE_CUA_OFFICIAL_PLUGIN_ID) { return []; } const fallbackRoot = findUsableOfficialPluginFallback(input.storageRoot, definition); return fallbackRoot ? [fallbackRoot] : []; }); return uniquePaths([...(input.extraRoots ?? []), ...fallbackRoots]); } function resolveSeedSource(): OfficialPluginSeedSource | undefined { const seaSource = resolveSeaSeedSource(); if (seaSource) return seaSource; return resolveFilesystemSeedSource(); } function resolveSeaSeedSource(): OfficialPluginSeedSource | undefined { const sea = getSeaModule(); if (!sea?.isSea()) return undefined; const manifest = readSeaManifest(sea); if (!manifest) return undefined; const plugins = OFFICIAL_PLUGIN_DEFINITIONS.flatMap((definition) => { const plugin = manifest.plugins.find( (item) => item.marketplace === OFFICIAL_PLUGIN_MARKETPLACE && item.name === definition.name && item.version === definition.version, ); if (!plugin) return []; return [ { definition, files: plugin.files, hash: hashSeedFiles(plugin.files), missingSeedPaths: findMissingOfficialPluginSeedPaths(definition, plugin.files), }, ]; }); if (plugins.length === 0) return undefined; return { kind: "sea", plugins, }; } function resolveFilesystemSeedSource(): OfficialPluginSeedSource | undefined { const plugins = OFFICIAL_PLUGIN_DEFINITIONS.flatMap((definition) => { const rootPath = resolveFilesystemPluginRoot(definition); if (!rootPath) return []; const files = collectFilesystemPluginFiles(rootPath, definition); return [ { definition, files, hash: hashSeedFiles(files), missingSeedPaths: findMissingOfficialPluginSeedPaths(definition, files), rootPath, }, ]; }); if (plugins.length === 0) return undefined; return { kind: "filesystem", plugins, }; } function findMissingOfficialPluginSeedPaths( definition: Pick, files: ReadonlyArray<{ path: string }>, ): string[] { const availablePaths = new Set(files.map((file) => file.path)); return (definition.requiredSeedPaths ?? []).filter( (requiredPath) => !availablePaths.has(requiredPath), ); } function getSeaModule(): SeaModule | undefined { const getBuiltinModule = process.getBuiltinModule as ((id: "node:sea") => SeaModule) | undefined; try { return getBuiltinModule?.("node:sea"); } catch { return undefined; } } function readSeaManifest(sea: SeaModule): SeaOfficialPluginManifest | undefined { try { const raw = sea.getAsset(SEA_PLUGIN_MANIFEST_ASSET_KEY, "utf8"); const manifest = JSON.parse(raw) as SeaOfficialPluginManifest; return manifest.version === 1 && Array.isArray(manifest.plugins) ? manifest : undefined; } catch { return undefined; } } function resolveFilesystemPluginRoot(definition: OfficialPluginDefinition): string | undefined { for (const baseDir of candidateBaseDirs()) { for (const relativePath of definition.rootCandidates) { const rootPath = resolve(baseDir, relativePath); if (existsSync(join(rootPath, ".zcode-plugin", "plugin.json"))) return rootPath; } } return undefined; } function collectFilesystemPluginFiles( rootPath: string, definition: OfficialPluginDefinition, ): OfficialPluginSeedFile[] { const files: OfficialPluginSeedFile[] = []; const allowedTopLevelPaths = new Set([ ...includedTopLevelPaths, ...(definition.runtimeTopLevelPaths ?? []), ]); for (const sourcePath of walkFiles(rootPath, allowedTopLevelPaths)) { const relativePath = toPosixPath(sourcePath.slice(rootPath.length + 1)); if (!shouldIncludePluginFile(relativePath, allowedTopLevelPaths)) continue; const bytes = readFileSync(sourcePath); files.push({ mode: modeForSeedFile(relativePath, statSync(sourcePath).mode), path: relativePath, sha256: hashBytes(bytes), sourcePath, }); } return files.sort((left, right) => left.path.localeCompare(right.path)); } function* walkFiles( directory: string, allowedTopLevelPaths: ReadonlySet, depth = 0, ): Generator { for (const entry of readdirSync(directory, { withFileTypes: true })) { if (shouldSkipDirectory(entry.name, depth, allowedTopLevelPaths)) continue; const fullPath = join(directory, entry.name); if (entry.isDirectory()) { yield* walkFiles(fullPath, allowedTopLevelPaths, depth + 1); continue; } if (entry.isFile()) yield fullPath; } } function readSeedFileBytes( source: OfficialPluginSeedSource, plugin: OfficialPluginSeedPluginSource, file: OfficialPluginSeedFile, ): Buffer { if (source.kind === "filesystem" && file.sourcePath) return readFileSync(file.sourcePath); const sea = getSeaModule(); if (!sea?.isSea()) throw new Error("SEA plugin asset is unavailable outside SEA runtime."); return Buffer.from( sea.getRawAsset( `${SEA_PLUGIN_ASSET_PREFIX}${OFFICIAL_PLUGIN_MARKETPLACE}/${plugin.definition.name}/${plugin.definition.version}/${file.path}`, ), ); } function writeOfficialMarketplace(storageRoot: string, source: OfficialPluginSeedSource): void { writeBundledOfficialMarketplacePartitionSync({ manifest: { name: OFFICIAL_PLUGIN_MARKETPLACE, plugins: source.plugins.map((plugin) => { // 商店信息(listing)与描述随目录条目下发:键名与 CDN 目录 schema 一致, // 由 adapter 的同一套 parseEntryStoreListing 解析,UI 才能给内置插件渲染 // 显示名/分类/作者/示例提示词。描述取自插件包内 plugin.json(单一事实源)。 const description = readSeedPluginDescription(source, plugin); return { cachePath: officialPluginCacheRoot(storageRoot, plugin.definition), ...(description ? { description } : {}), name: plugin.definition.name, source: source.kind, version: plugin.definition.version, ...(plugin.definition.listing ?? {}), }; }), version: 1, }, storageRoot, }); } /** 从 seed 文件集中读插件 plugin.json 的 description;读取/解析失败按 undefined 降级。 */ function readSeedPluginDescription( source: OfficialPluginSeedSource, plugin: OfficialPluginSeedPluginSource, ): string | undefined { const manifestFile = plugin.files.find((file) => file.path === ".zcode-plugin/plugin.json"); if (!manifestFile) return undefined; try { const parsed = JSON.parse(readSeedFileBytes(source, plugin, manifestFile).toString("utf8")) as { description?: unknown; }; return typeof parsed.description === "string" && parsed.description.trim().length > 0 ? parsed.description : undefined; } catch { return undefined; } } function isSeedCurrent(targetRoot: string, plugin: OfficialPluginSeedPluginSource): boolean { const markerPath = join(targetRoot, SEED_MARKER_FILE); if (!existsSync(markerPath)) return false; try { const marker = JSON.parse(readFileSync(markerPath, "utf8")) as ReturnType; return marker.hash === plugin.hash && marker.pluginVersion === plugin.definition.version; } catch { return false; } } /** * 旧版本缓存只要插件清单和运行所需文件完整,就可以继续服务当前会话。 * marker hash 不匹配只表示需要升级,不能把一个可用的旧缓存当成启动失败。 */ function isSeedUsable(targetRoot: string, definition: OfficialPluginDefinition): boolean { try { const manifest = JSON.parse( readFileSync(join(targetRoot, ".zcode-plugin", "plugin.json"), "utf8"), ) as { name?: unknown }; if (manifest.name !== definition.name) return false; } catch { return false; } return (definition.requiredSeedPaths ?? []).every((requiredPath) => existsSync(join(targetRoot, ...requiredPath.split("/"))), ); } function findUsableOfficialPluginFallback( storageRoot: string, definition: OfficialPluginDefinition, ): string | undefined { const targetRoot = officialPluginCacheRoot(storageRoot, definition); if (isSeedUsable(targetRoot, definition)) return undefined; let entries; try { entries = readdirSync(dirname(targetRoot), { withFileTypes: true }); } catch (error) { if (isNotFoundFsError(error)) return undefined; throw error; } return entries .filter( (entry) => entry.isDirectory() && entry.name !== definition.version && !entry.name.includes(".backup") && // 锁目录(含 .seed-lock.stale-*)与版本目录同级;超时降级后锁必然在场, // 不能依赖 isSeedUsable 的内容检查兜底,按名字直接排除。 !entry.name.includes(".seed-lock") && !entry.name.includes(".tmp-"), ) .sort((left, right) => right.name.localeCompare(left.name, undefined, { numeric: true, sensitivity: "base" }), ) .map((entry) => join(dirname(targetRoot), entry.name)) .find((rootPath) => isSeedUsable(rootPath, definition)); } function replaceSeedRoot( temporaryRoot: string, targetRoot: string, plugin: OfficialPluginSeedPluginSource, retryBudget: OfficialPluginCacheRetryBudget, ): void { const backupRoot = createSeedBackupRoot(targetRoot); let movedTargetToBackup = false; mkdirSync(dirname(targetRoot), { recursive: true }); if (existsSync(targetRoot)) { try { renameOfficialPluginCachePath(targetRoot, backupRoot, retryBudget); movedTargetToBackup = true; } catch (error) { // 官方插件缓存由桌面窗口、协议与 CLI 入口共享。existsSync 之后, // 另一个进程可能先移走 target;此处只收敛这个 TOCTOU 的 ENOENT,随后继续 // promote 或由 isSeedCurrent 识别并发赢家,其他缺失错误仍保持原有 fatal 语义。 if (!isNotFoundFsError(error)) throw error; } } try { renameOfficialPluginCachePath(temporaryRoot, targetRoot, retryBudget); } catch (error) { if (isSeedCurrent(targetRoot, plugin)) { removeOfficialPluginCacheDirectory(temporaryRoot, retryBudget); if (movedTargetToBackup) { removeOfficialPluginCacheDirectory(backupRoot, retryBudget); } cleanupLegacySeedBackup(targetRoot, retryBudget); return; } if (movedTargetToBackup && !existsSync(targetRoot) && existsSync(backupRoot)) { renameOfficialPluginCachePath(backupRoot, targetRoot, retryBudget); } throw error; } if (movedTargetToBackup) { removeOfficialPluginCacheDirectory(backupRoot, retryBudget); } cleanupLegacySeedBackup(targetRoot, retryBudget); } function cleanupLegacySeedBackup( targetRoot: string, retryBudget: OfficialPluginCacheRetryBudget, ): void { const backupRoot = `${targetRoot}.backup`; if (!existsSync(backupRoot)) return; // 旧版固定 backup 没有事务归属,target 暂时缺失时可能属于另一个仍在 // promote 的进程,不能把它恢复回去。仅在当前 seed 已确认可用后清理这个遗留目录。 removeOfficialPluginCacheDirectory(backupRoot, retryBudget); } function createSeedBackupRoot(targetRoot: string): string { // 固定 backup 会被并发启动进程共同当作 rollback 点;每次替换使用唯一目录, // catch 分支只恢复自己移动出的 target,避免一个进程窃取另一个进程的事务状态。 return `${targetRoot}.backup-${process.pid}-${Date.now()}`; } function isNotFoundFsError(error: unknown): error is NodeJS.ErrnoException { return ( typeof error === "object" && error !== null && String((error as NodeJS.ErrnoException).code) === "ENOENT" ); } function warnCacheDegraded( logger: Logger | undefined, input: { error: NodeJS.ErrnoException; missingSeedPaths?: readonly string[]; operation: "remove_suppressed_plugin" | "seed_plugin"; pluginId: string; targetRoot: string; }, ): void { logger?.warn("Official plugin cache operation degraded", { attempts: getOfficialPluginCacheRetryAttempts(input.error), degraded: true, errorCode: input.error.code, ...(input.missingSeedPaths ? { missingSeedPaths: input.missingSeedPaths } : {}), module: "bootstrap.official_plugin_cache", operation: input.operation, pluginId: input.pluginId, targetRoot: input.targetRoot, }); } function seedMarker(source: OfficialPluginSeedSource, plugin: OfficialPluginSeedPluginSource) { return { hash: plugin.hash, marketplace: OFFICIAL_PLUGIN_MARKETPLACE, plugin: plugin.definition.name, pluginVersion: plugin.definition.version, source: source.kind, version: 1, }; } function officialPluginCacheRoot( storageRoot: string, definition: OfficialPluginDefinition, ): string { return join( storageRoot, "cache", OFFICIAL_PLUGIN_MARKETPLACE, definition.name, definition.version, ); } /** 内置技能包(bundled-skills.ts)沿同一组候选目录定位,保证两类内置资产在每种运行布局下同进同出。 */ export function candidateBaseDirs(): string[] { // 修复原因:Electron app-server 运行在 resources/glm/zcode.cjs,官方插件资源也随桌面包 // stage 到同级 packages/*-plugin。候选目录必须优先看入口文件目录,避免生产态退回到 // monorepo-only 的 __dirname 查找假设。 return [entrypointDir(), runtimeDir(), process.cwd()].filter( (dir): dir is string => typeof dir === "string", ); } function runtimeDir(): string | undefined { return typeof __dirname === "string" ? __dirname : undefined; } function entrypointDir(): string | undefined { return process.argv[1] ? dirname(process.argv[1]) : undefined; } function shouldSkipDirectory( name: string, depth: number, allowedTopLevelPaths: ReadonlySet, ): boolean { if (name === ".turbo" || name === "coverage" || name === ".venv" || name === "__pycache__") { return true; } return name === "node_modules" && !(depth === 0 && allowedTopLevelPaths.has(name)); } function shouldIncludePluginFile( relativePath: string, allowedTopLevelPaths: ReadonlySet, ): boolean { const segments = relativePath.split("/"); if (segments.includes(".DS_Store") || segments.some((segment) => segment.endsWith(".pyc"))) { return false; } const [topLevel] = relativePath.split("/"); return topLevel !== undefined && allowedTopLevelPaths.has(topLevel); } function modeForSeedFile(filePath: string, sourceMode?: number): number { if (sourceMode !== undefined && (sourceMode & 0o111) !== 0) return 0o755; const normalizedPath = toPosixPath(filePath); // official plugin seed 会重写缓存文件权限。部分插件通过 polyglot shell wrapper // 直接执行 hook 脚本,若落盘成 0644 会 permission denied。这里保留源码执行位, // 并对 SEA/旧 manifest 缺少 mode 的 hook 脚本兜底。 if (/(?:^|\/)dist\/mcp\/server\.js$/i.test(normalizedPath)) return 0o755; if (/^hooks\//u.test(normalizedPath) && !/\.(json|md|txt)$/iu.test(normalizedPath)) { return 0o755; } return 0o644; } function hashSeedFiles(files: OfficialPluginSeedFile[]): string { return hashText( JSON.stringify( files.map((file) => [file.path, file.sha256, modeForSeedFile(file.path, file.mode)]), ), ); } function toPosixPath(value: string): string { return value.split(sep).join("/"); } function uniquePaths(paths: string[]): string[] { return paths.filter((path, index) => paths.indexOf(path) === index); } function hashBytes(bytes: Buffer): string { return createHash("sha256").update(bytes).digest("hex"); } function hashText(text: string): string { return hashBytes(Buffer.from(text)); }