| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303 |
- import { existsSync } from "node:fs";
- import { createHash } from "node:crypto";
- import { mkdir, mkdtemp, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { basename, dirname, join, sep } from "node:path";
- import type { Logger } from "@zcode/contracts";
- import { afterEach, describe, expect, it, vi } from "vitest";
- const faults = vi.hoisted(() => ({
- afterWriteFile: undefined as ((path: string) => void) | undefined,
- failTemporaryRemoveOnce: undefined as string | undefined,
- watchedSourceRoot: undefined as string | undefined,
- sourcePayloadReads: [] as string[],
- sourceDirectoryReads: [] as string[],
- omitSeedManifest: false,
- fileFactsReads: 0,
- afterFileFacts: undefined as (() => Promise<void>) | undefined,
- afterBackupRename: undefined as (() => Promise<void>) | undefined,
- seedLockTargets: [] as string[],
- afterSeedLock: undefined as (() => void) | undefined,
- crossDeviceBackupTargetSuffix: undefined as string | undefined,
- crossDevicePromoteTargetSuffix: undefined as string | undefined,
- moveTargetSuffix: undefined as string | undefined,
- promoteCurrentTargetSuffix: undefined as string | undefined,
- promoteTargetSuffix: undefined as string | undefined,
- recoverBackupTargetSuffix: undefined as string | undefined,
- removeTargetSuffix: undefined as string | undefined,
- runtimeManifestNotFoundTargetSuffix: undefined as string | undefined,
- runtimeManifestTargetSuffix: undefined as string | undefined,
- seedLockHeldTargetSuffixes: [] as string[],
- seedLockObservedTimeoutsMs: [] as number[],
- }));
- vi.mock("node:fs/promises", async (importOriginal) => {
- const actual = await importOriginal<typeof import("node:fs/promises")>();
- return {
- ...actual,
- async writeFile(...args: Parameters<typeof actual.writeFile>) {
- await actual.writeFile(...args);
- faults.afterWriteFile?.(String(args[0]));
- },
- async rm(...args: Parameters<typeof actual.rm>) {
- if (String(args[0]) === faults.failTemporaryRemoveOnce) {
- faults.failTemporaryRemoveOnce = undefined;
- throw Object.assign(new Error("temporary directory is busy"), { code: "EPERM" });
- }
- return actual.rm(...args);
- },
- async readFile(...args: Parameters<typeof actual.readFile>) {
- const path = String(args[0]);
- if (
- faults.watchedSourceRoot &&
- path.startsWith(faults.watchedSourceRoot + sep) &&
- !path.endsWith(join(".zcode-plugin", "plugin.json"))
- )
- faults.sourcePayloadReads.push(path);
- return actual.readFile(...args);
- },
- async readdir(...args: Parameters<typeof actual.readdir>) {
- const path = String(args[0]);
- if (
- faults.watchedSourceRoot &&
- (path === faults.watchedSourceRoot || path.startsWith(faults.watchedSourceRoot + sep))
- )
- faults.sourceDirectoryReads.push(path);
- return actual.readdir(...args);
- },
- };
- });
- vi.mock("../src/app/official-plugin-cache-fs.js", async (importOriginal) => {
- const actual = await importOriginal<typeof import("../src/app/official-plugin-cache-fs.js")>();
- return {
- ...actual,
- async collectOfficialPluginSourceFiles(
- ...args: Parameters<typeof actual.collectOfficialPluginSourceFiles>
- ) {
- const files = await actual.collectOfficialPluginSourceFiles(...args);
- // 模拟入口检查通过后,目录枚举前 manifest 消失;不能提交残缺版本 marker。
- return faults.omitSeedManifest && args[0] === faults.watchedSourceRoot
- ? files.filter((file) => file.path !== ".zcode-plugin/plugin.json")
- : files;
- },
- async readOfficialPluginFileFacts(paths: readonly string[]) {
- const facts = await actual.readOfficialPluginFileFacts(paths);
- faults.fileFactsReads += 1;
- await faults.afterFileFacts?.();
- return facts;
- },
- async removeOfficialPluginCacheDirectory(
- path: string,
- budget: Parameters<typeof actual.removeOfficialPluginCacheDirectory>[1],
- ): Promise<void> {
- if (faults.removeTargetSuffix && path.endsWith(faults.removeTargetSuffix)) {
- throw exhaustedErrno("EPERM");
- }
- await actual.removeOfficialPluginCacheDirectory(path, budget);
- },
- async renameOfficialPluginCachePath(
- fromPath: string,
- toPath: string,
- budget: ReturnType<typeof actual.createOfficialPluginCacheRetryBudget>,
- ): Promise<void> {
- if (
- faults.recoverBackupTargetSuffix &&
- fromPath === `${toPath}.backup` &&
- toPath.endsWith(faults.recoverBackupTargetSuffix)
- ) {
- throw errno("EOWNER");
- }
- // overlayfs(未启用 redirect_dir)拒绝 rename 位于镜像下层的目录,同一父目录内也返回 EXDEV;
- // 该错误不会因重试消失,与容器内复现到的内核行为一致。
- if (
- faults.crossDeviceBackupTargetSuffix &&
- fromPath.endsWith(faults.crossDeviceBackupTargetSuffix) &&
- toPath.startsWith(`${fromPath}.backup-`)
- ) {
- throw errno("EXDEV");
- }
- if (
- faults.crossDevicePromoteTargetSuffix &&
- fromPath.includes(".tmp-") &&
- toPath.endsWith(faults.crossDevicePromoteTargetSuffix)
- ) {
- throw errno("EXDEV");
- }
- if (
- faults.moveTargetSuffix &&
- fromPath.endsWith(faults.moveTargetSuffix) &&
- toPath.startsWith(`${fromPath}.backup-`)
- ) {
- await actual.removeOfficialPluginCacheDirectory(fromPath, budget);
- throw errno("ENOENT");
- }
- if (
- faults.promoteCurrentTargetSuffix &&
- fromPath.includes(".tmp-") &&
- toPath.endsWith(faults.promoteCurrentTargetSuffix)
- ) {
- await actual.renameOfficialPluginCachePath(fromPath, toPath, budget);
- throw errno("EEXIST");
- }
- if (
- faults.promoteTargetSuffix &&
- fromPath.includes(".tmp-") &&
- toPath.endsWith(faults.promoteTargetSuffix)
- ) {
- throw exhaustedErrno("EPERM");
- }
- await actual.renameOfficialPluginCachePath(fromPath, toPath, budget);
- if (toPath.startsWith(`${fromPath}.backup-`)) await faults.afterBackupRename?.();
- },
- async writeTextFileAtomicallyWithRetry(
- path: string,
- contents: string,
- budget: ReturnType<typeof actual.createOfficialPluginCacheRetryBudget>,
- ): Promise<void> {
- if (
- faults.runtimeManifestNotFoundTargetSuffix &&
- path.endsWith(faults.runtimeManifestNotFoundTargetSuffix)
- ) {
- throw errno("ENOENT");
- }
- if (faults.runtimeManifestTargetSuffix && path.endsWith(faults.runtimeManifestTargetSuffix)) {
- throw exhaustedErrno("EPERM");
- }
- await actual.writeTextFileAtomicallyWithRetry(path, contents, budget);
- },
- };
- });
- vi.mock("../src/app/official-plugin-seed-lock.js", async (importOriginal) => {
- const actual = await importOriginal<typeof import("../src/app/official-plugin-seed-lock.js")>();
- return {
- ...actual,
- async withOfficialPluginSeedLock<T>(
- targetRoot: string,
- action: () => T,
- options?: Parameters<typeof actual.withOfficialPluginSeedLock>[2],
- ): Promise<T> {
- faults.seedLockTargets.push(targetRoot);
- if (!faults.seedLockHeldTargetSuffixes.some((suffix) => targetRoot.endsWith(suffix))) {
- const result = await actual.withOfficialPluginSeedLock(targetRoot, action, options);
- faults.afterSeedLock?.();
- return result;
- }
- faults.seedLockObservedTimeoutsMs.push(options?.timeoutMs ?? -1);
- // 只压缩等待参数并保留调用方预算上限(默认 15s 会拖垮测试),
- // 仍走真实锁竞争与真实超时错误。
- return await actual.withOfficialPluginSeedLock(targetRoot, action, {
- ...options,
- retryDelayMs: 1,
- timeoutMs: Math.min(10, options?.timeoutMs ?? 10),
- });
- },
- };
- });
- import {
- createOfficialPluginPreparation,
- resolveOfficialPluginRoots,
- } from "../src/app/bundled-plugins.js";
- import { OFFICIAL_PLUGIN_DEFINITIONS } from "../src/app/official-plugin-definitions.js";
- import { resolveZCodePlugins } from "../src/plugins.js";
- const OFFICIAL_CACHE_ROOT = join("cache", "zcode-plugins-official");
- const BROWSER_CACHE_SUFFIX = join(OFFICIAL_CACHE_ROOT, "browser-use", "0.5.2");
- const ANDROID_CACHE_SUFFIX = join(OFFICIAL_CACHE_ROOT, "android-emulator", "0.1.3");
- const SKILL_CREATOR_CACHE_SUFFIX = join(OFFICIAL_CACHE_ROOT, "skill-creator", "0.1.0");
- const ANDROID_MANIFEST_SUFFIX = join(ANDROID_CACHE_SUFFIX, ".zcode-plugin", "plugin.json");
- const CUA_DEFINITION = OFFICIAL_PLUGIN_DEFINITIONS.find(({ name }) => name === "computer-use");
- if (!CUA_DEFINITION) throw new Error("computer-use official plugin definition is missing");
- const CUA_CACHE_SUFFIX = join(OFFICIAL_CACHE_ROOT, CUA_DEFINITION.name, CUA_DEFINITION.version);
- describe("bundled official plugin cache resilience", () => {
- const cleanups: string[] = [];
- const originalEntrypoint = process.argv[1];
- afterEach(async () => {
- vi.useRealTimers();
- faults.afterWriteFile = undefined;
- faults.failTemporaryRemoveOnce = undefined;
- faults.watchedSourceRoot = undefined;
- faults.sourcePayloadReads = [];
- faults.sourceDirectoryReads = [];
- faults.omitSeedManifest = false;
- faults.fileFactsReads = 0;
- faults.afterFileFacts = undefined;
- faults.afterBackupRename = undefined;
- faults.seedLockTargets = [];
- faults.afterSeedLock = undefined;
- process.argv[1] = originalEntrypoint;
- faults.crossDeviceBackupTargetSuffix = undefined;
- faults.crossDevicePromoteTargetSuffix = undefined;
- faults.moveTargetSuffix = undefined;
- faults.promoteCurrentTargetSuffix = undefined;
- faults.promoteTargetSuffix = undefined;
- faults.recoverBackupTargetSuffix = undefined;
- faults.removeTargetSuffix = undefined;
- faults.runtimeManifestNotFoundTargetSuffix = undefined;
- faults.runtimeManifestTargetSuffix = undefined;
- faults.seedLockHeldTargetSuffixes = [];
- faults.seedLockObservedTimeoutsMs = [];
- await Promise.all(cleanups.splice(0).map((root) => rm(root, { force: true, recursive: true })));
- });
- it.each([false, true])("keeps SEA payload hash validation (corrupt: %s)", async (corrupt) => {
- const storageRoot = await createStorageRoot(cleanups);
- const payloads: Record<string, string> = {
- ".zcode-plugin/plugin.json": JSON.stringify({ name: "skill-creator", version: "0.1.0" }),
- "skills/demo/SKILL.md": "# original SEA skill",
- };
- const files = Object.entries(payloads).map(([path, text]) => ({
- path,
- sha256: createHash("sha256").update(text).digest("hex"),
- }));
- const prefix = "zcode-official-plugins/zcode-plugins-official/skill-creator/0.1.0/";
- const getBuiltinModule = process.getBuiltinModule.bind(process);
- const builtin = vi.spyOn(process, "getBuiltinModule").mockImplementation((id) => {
- if (id !== "node:sea") return getBuiltinModule(id);
- return {
- isSea: () => true,
- getAsset: () =>
- JSON.stringify({
- version: 1,
- plugins: [
- {
- marketplace: "zcode-plugins-official",
- name: "skill-creator",
- version: "0.1.0",
- files,
- },
- ],
- }),
- getRawAsset: (key: string) => {
- const path = key.slice(prefix.length);
- const text = corrupt && path === "skills/demo/SKILL.md" ? "# corrupted" : payloads[path];
- return new Uint8Array(Buffer.from(text)).buffer;
- },
- };
- });
- try {
- const prepared = resolveOfficialPluginRoots({ storageRoot, logger: createLogger() });
- if (corrupt)
- await expect(prepared).rejects.toMatchObject({ code: "ZCODE_PLUGIN_SOURCES_CHANGED" });
- else await prepared;
- const marker = join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX, ".zcode-plugin-seed.json");
- expect(existsSync(marker)).toBe(!corrupt);
- if (!corrupt)
- expect(JSON.parse(await readFile(marker, "utf8")).hash).toMatch(/^[a-f0-9]{64}$/);
- } finally {
- builtin.mockRestore();
- }
- });
- it("shares a startup deadline and keeps timeout degradation until a new owner", async () => {
- vi.useFakeTimers();
- const preparation = createOfficialPluginPreparation({ startupTimeoutMs: 3_000 });
- const late = Promise.withResolvers<Awaited<ReturnType<typeof resolveZCodePlugins>>>();
- const success = {
- commandRoots: [],
- diagnostics: [],
- hooks: {},
- mcpServers: {},
- plugins: [],
- skillRoots: [],
- };
- const first = preparation.resolvePlugins(() => late.promise);
- await vi.advanceTimersByTimeAsync(2_900);
- const second = preparation.resolvePlugins(() => late.promise);
- await vi.advanceTimersByTimeAsync(100);
- for (const outcome of await Promise.all([first, second])) {
- expect(outcome).toMatchObject({
- ...success,
- diagnostics: [{ code: "plugin_startup_timeout", severity: "warning" }],
- });
- }
- expect(preparation.startupDegraded).toBe(true);
- expect(preparation.signal.aborted).toBe(true);
- late.resolve(success);
- preparation.invalidate();
- const unexpected = vi.fn(async () => success);
- await preparation.resolvePlugins(unexpected);
- expect(unexpected).not.toHaveBeenCalled();
- await preparation.dispose();
- const next = createOfficialPluginPreparation({ startupTimeoutMs: 3_000 });
- await expect(next.resolvePlugins(unexpected)).resolves.toEqual(success);
- expect(unexpected).toHaveBeenCalledOnce();
- expect(next.startupDegraded).toBe(false);
- const pending = next.resolvePlugins(() => new Promise(() => {}));
- const closed = expect(pending).rejects.toMatchObject({ name: "AbortError" });
- await next.dispose();
- await closed;
- expect(vi.getTimerCount()).toBe(0);
- });
- it("skips discovery and fallback scans after seed exceeds the startup budget", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- vi.useFakeTimers();
- const preparation = createOfficialPluginPreparation({ startupTimeoutMs: 3_000 });
- const seed = Promise.withResolvers<[]>();
- const prepare = vi.spyOn(preparation, "prepare").mockImplementation(() => seed.promise);
- const discoverPlugins = vi.fn();
- const options = {
- pluginStorageRoot: storageRoot,
- workingDirectory: dirname(storageRoot),
- skipUserConfig: true,
- pluginPreparation: preparation,
- pluginDiscovery: { discoverPlugins },
- };
- const first = resolveZCodePlugins(options);
- await vi.advanceTimersByTimeAsync(3_000);
- expect((await first).diagnostics).toEqual([
- expect.objectContaining({ code: "plugin_startup_timeout" }),
- ]);
- seed.reject(new Error("late seed failure"));
- await resolveZCodePlugins(options);
- expect(prepare).toHaveBeenCalledOnce();
- expect(discoverPlugins).not.toHaveBeenCalled();
- await preparation.dispose();
- });
- it("lets directory publication finish safely after the startup caller times out", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- await resolveOfficialPluginRoots({ storageRoot });
- const targetRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- await writeFile(join(targetRoot, ".zcode-plugin-seed.json"), "{}");
- vi.useFakeTimers();
- const moved = Promise.withResolvers<void>();
- const release = Promise.withResolvers<void>();
- faults.afterBackupRename = async () => {
- faults.afterBackupRename = undefined;
- moved.resolve();
- await release.promise;
- };
- const preparation = createOfficialPluginPreparation({ startupTimeoutMs: 3_000 });
- const work = preparation.resolvePlugins(async () => {
- await resolveOfficialPluginRoots({ storageRoot, preparation });
- return {
- commandRoots: [],
- diagnostics: [],
- hooks: {},
- mcpServers: {},
- plugins: [],
- skillRoots: [],
- };
- });
- try {
- await moved.promise;
- await vi.advanceTimersByTimeAsync(3_000);
- expect((await work).diagnostics[0]?.code).toBe("plugin_startup_timeout");
- expect(existsSync(`${targetRoot}.seed-lock`)).toBe(true);
- } finally {
- release.resolve();
- await preparation.dispose();
- }
- expect(existsSync(join(targetRoot, ".zcode-plugin", "plugin.json"))).toBe(true);
- expect(existsSync(`${targetRoot}.seed-lock`)).toBe(false);
- await expectNoSeedBackups(targetRoot);
- }, 15_000);
- it("reuses filesystem versions across fresh owners without scanning or hashing source payload", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- const fixture = await createVersionedSource(storageRoot);
- await resolveOfficialPluginRoots({ storageRoot });
- const targetRoot = join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX);
- const markerPath = join(targetRoot, ".zcode-plugin-seed.json");
- const marker = JSON.parse(await readFile(markerPath, "utf8"));
- await writeFile(markerPath, JSON.stringify({ ...marker, hash: "legacy-content-hash" }));
- await writeFile(fixture.payload, "same version, changed source");
- faults.sourcePayloadReads = [];
- faults.sourceDirectoryReads = [];
- await resolveOfficialPluginRoots({ storageRoot });
- expect(faults.sourcePayloadReads).toEqual([]);
- expect(faults.sourceDirectoryReads).toEqual([]);
- expect(await readFile(join(targetRoot, "skills", "demo", "SKILL.md"), "utf8")).toBe(
- "original source",
- );
- expect(JSON.parse(await readFile(markerPath, "utf8")).hash).toBe("legacy-content-hash");
- });
- it("reseeds the same filesystem version when its declared build identity changes", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- const fixture = await createVersionedSource(storageRoot);
- const definition = OFFICIAL_PLUGIN_DEFINITIONS.find((item) => item.name === "skill-creator")!;
- const mutable = definition as { buildIdentityPath?: string };
- const identityPath = join(fixture.root, "dist", "build-identity.json");
- const targetRoot = join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX);
- const markerPath = join(targetRoot, ".zcode-plugin-seed.json");
- const cachedPayload = () => readFile(join(targetRoot, "skills", "demo", "SKILL.md"), "utf8");
- mutable.buildIdentityPath = "dist/build-identity.json";
- try {
- await mkdir(dirname(identityPath), { recursive: true });
- await writeFile(identityPath, '{"cuaHelperBuildId":"pipeline-1"}\n');
- await resolveOfficialPluginRoots({ storageRoot });
- expect(JSON.parse(await readFile(markerPath, "utf8")).buildIdentity).toBe(
- '{"cuaHelperBuildId":"pipeline-1"}\n',
- );
- // 同一构建身份仍按版本规则复用:只读身份小文件,不读取源正文。
- await writeFile(fixture.payload, "same build, edited source");
- faults.sourcePayloadReads = [];
- await resolveOfficialPluginRoots({ storageRoot });
- expect(faults.sourcePayloadReads.length).toBeGreaterThan(0);
- expect(faults.sourcePayloadReads.every((path) => path === identityPath)).toBe(true);
- expect(await cachedPayload()).toBe("original source");
- // 桌面升级换了 Helper build id,版本号不变也必须重建。
- await writeFile(identityPath, '{"cuaHelperBuildId":"pipeline-2"}\n');
- await writeFile(fixture.payload, "pipeline 2 build");
- await resolveOfficialPluginRoots({ storageRoot });
- expect(await cachedPayload()).toBe("pipeline 2 build");
- expect(JSON.parse(await readFile(markerPath, "utf8")).buildIdentity).toBe(
- '{"cuaHelperBuildId":"pipeline-2"}\n',
- );
- // 旧客户端写入的 marker 没有构建身份,同样视为过期。
- const marker = JSON.parse(await readFile(markerPath, "utf8"));
- delete marker.buildIdentity;
- await writeFile(markerPath, JSON.stringify(marker));
- await writeFile(fixture.payload, "after legacy marker");
- await resolveOfficialPluginRoots({ storageRoot });
- expect(await cachedPayload()).toBe("after legacy marker");
- } finally {
- delete mutable.buildIdentityPath;
- }
- });
- it.each([false, true])(
- "cleans a cancelled seed without changing the old target (busy=%s)",
- async (busy) => {
- const storageRoot = await createStorageRoot(cleanups);
- await createVersionedSource(storageRoot);
- await resolveOfficialPluginRoots({ storageRoot });
- const targetRoot = join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX);
- const markerPath = join(targetRoot, ".zcode-plugin-seed.json");
- const marker = JSON.parse(await readFile(markerPath, "utf8"));
- const staleMarker = JSON.stringify({ ...marker, pluginVersion: "0.0.0" });
- await writeFile(markerPath, staleMarker);
- const preparation = createOfficialPluginPreparation();
- let disposal: Promise<void> | undefined;
- faults.afterWriteFile = (path) => {
- if (disposal || !path.startsWith(`${targetRoot}.tmp-`)) return;
- // 写入第一份候选文件后中止,确保真实临时目录存在;取消与预算到期共用此 signal。
- if (busy)
- faults.failTemporaryRemoveOnce = path.slice(0, path.indexOf(sep, targetRoot.length));
- disposal = preparation.dispose();
- };
- try {
- await expect(
- resolveOfficialPluginRoots({ storageRoot, preparation }),
- ).rejects.toBeInstanceOf(Error);
- expect(preparation.signal.aborted).toBe(true);
- await disposal;
- expect(await readdir(dirname(targetRoot))).not.toEqual(
- expect.arrayContaining([expect.stringContaining(".tmp-")]),
- );
- expect(await readFile(markerPath, "utf8")).toBe(staleMarker);
- expect(await readFile(join(targetRoot, "skills", "demo", "SKILL.md"), "utf8")).toBe(
- "original source",
- );
- } finally {
- await preparation.dispose();
- }
- },
- );
- it("copies filesystem payload once when a version marker differs or is missing", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- const fixture = await createVersionedSource(storageRoot);
- await resolveOfficialPluginRoots({ storageRoot });
- const targetRoot = join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX);
- const markerPath = join(targetRoot, ".zcode-plugin-seed.json");
- const marker = JSON.parse(await readFile(markerPath, "utf8"));
- await writeFile(markerPath, JSON.stringify({ ...marker, pluginVersion: "0.0.0" }));
- for (const removeMarker of [false, true]) {
- if (removeMarker) await rm(markerPath);
- await writeFile(fixture.payload, `replacement ${removeMarker}`);
- faults.sourcePayloadReads = [];
- faults.sourceDirectoryReads = [];
- await resolveOfficialPluginRoots({ storageRoot });
- expect(faults.sourcePayloadReads).toEqual([fixture.payload]);
- expect(faults.sourceDirectoryReads.length).toBeGreaterThan(0);
- expect(await readFile(join(targetRoot, "skills", "demo", "SKILL.md"), "utf8")).toBe(
- `replacement ${removeMarker}`,
- );
- expect(JSON.parse(await readFile(markerPath, "utf8"))).toMatchObject({
- pluginVersion: "0.1.0",
- });
- expect(JSON.parse(await readFile(markerPath, "utf8"))).not.toHaveProperty("hash");
- }
- });
- it("seeds a bumped filesystem version while preserving the previous version", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- const fixture = await createVersionedSource(storageRoot);
- await resolveOfficialPluginRoots({ storageRoot });
- const definition = OFFICIAL_PLUGIN_DEFINITIONS.find((item) => item.name === "skill-creator")!;
- const previousVersion = definition.version;
- try {
- definition.version = "0.1.1";
- await writeFile(
- join(fixture.root, ".zcode-plugin", "plugin.json"),
- JSON.stringify({ name: definition.name, version: definition.version }),
- );
- await writeFile(fixture.payload, "new version");
- faults.sourcePayloadReads = [];
- await resolveOfficialPluginRoots({ storageRoot });
- expect(faults.sourcePayloadReads).toEqual([fixture.payload]);
- expect(
- await readFile(
- join(
- storageRoot,
- OFFICIAL_CACHE_ROOT,
- "skill-creator",
- "0.1.1",
- "skills",
- "demo",
- "SKILL.md",
- ),
- "utf8",
- ),
- ).toBe("new version");
- expect(
- await readFile(
- join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX, "skills", "demo", "SKILL.md"),
- "utf8",
- ),
- ).toBe("original source");
- } finally {
- definition.version = previousVersion;
- }
- });
- it("does not publish a version marker for an incomplete staged filesystem source", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- await createVersionedSource(storageRoot);
- faults.omitSeedManifest = true;
- await expect(resolveOfficialPluginRoots({ storageRoot })).rejects.toMatchObject({
- code: "ZCODE_PLUGIN_SOURCES_CHANGED",
- });
- expect(
- existsSync(join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX, ".zcode-plugin-seed.json")),
- ).toBe(false);
- });
- it("reuses only validated successful preparation and invalidates on cache loss or explicit refresh", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- const preparation = createOfficialPluginPreparation();
- try {
- await resolveOfficialPluginRoots({ storageRoot, preparation });
- expect(faults.seedLockTargets.length).toBeGreaterThan(0);
- faults.seedLockTargets = [];
- faults.fileFactsReads = 0;
- await resolveOfficialPluginRoots({ storageRoot, preparation });
- expect(faults.seedLockTargets).toEqual([]);
- expect(faults.fileFactsReads).toBe(2);
- process.argv[1] = join(storageRoot, "changed-cli-entry.cjs");
- await resolveOfficialPluginRoots({ storageRoot, preparation });
- expect(faults.seedLockTargets.length).toBeGreaterThan(0);
- faults.seedLockTargets = [];
- await rm(join(storageRoot, BROWSER_CACHE_SUFFIX, ".zcode-plugin-seed.json"));
- await resolveOfficialPluginRoots({ storageRoot, preparation });
- expect(faults.seedLockTargets.length).toBeGreaterThan(0);
- faults.seedLockTargets = [];
- preparation.invalidate();
- await resolveOfficialPluginRoots({ storageRoot, preparation });
- expect(faults.seedLockTargets.length).toBeGreaterThan(0);
- } finally {
- await preparation.dispose();
- }
- await expect(resolveOfficialPluginRoots({ storageRoot, preparation })).rejects.toMatchObject({
- name: "AbortError",
- });
- }, 15_000);
- it.each(["marker deletion", "manifest replacement"] as const)(
- "revalidates a cache hit against its original facts after %s",
- async (mutation) => {
- const storageRoot = await createStorageRoot(cleanups);
- const preparation = createOfficialPluginPreparation();
- const targetRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const markerPath = join(targetRoot, ".zcode-plugin-seed.json");
- const manifestPath = join(targetRoot, ".zcode-plugin", "plugin.json");
- try {
- await resolveOfficialPluginRoots({ storageRoot, preparation });
- const originalManifest = await readFile(manifestPath, "utf8");
- faults.seedLockTargets = [];
- faults.afterFileFacts = async () => {
- faults.afterFileFacts = undefined;
- // 首次检查已读到旧状态后再改盘,覆盖可复用判定与最终校验之间的窗口。
- if (mutation === "marker deletion") await rm(markerPath);
- else await writeFile(manifestPath, JSON.stringify({ name: "external-replacement" }));
- };
- await expect(resolveOfficialPluginRoots({ storageRoot, preparation })).resolves.toEqual([]);
- expect(faults.seedLockTargets).toContain(targetRoot);
- expect(existsSync(markerPath)).toBe(true);
- expect(await readFile(manifestPath, "utf8")).toBe(originalManifest);
- } finally {
- await preparation.dispose();
- }
- },
- 15_000,
- );
- it("does not remember partial seed failure as successful preparation", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- const preparation = createOfficialPluginPreparation();
- faults.promoteTargetSuffix = BROWSER_CACHE_SUFFIX;
- try {
- await resolveOfficialPluginRoots({ storageRoot, preparation });
- faults.seedLockTargets = [];
- await resolveOfficialPluginRoots({ storageRoot, preparation });
- expect(faults.seedLockTargets.some((path) => path.endsWith(BROWSER_CACHE_SUFFIX))).toBe(true);
- faults.promoteTargetSuffix = undefined;
- await resolveOfficialPluginRoots({ storageRoot, preparation });
- expect(existsSync(join(storageRoot, BROWSER_CACHE_SUFFIX))).toBe(true);
- } finally {
- await preparation.dispose();
- }
- }, 15_000);
- it("discards in-flight preparation when its process owner closes", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- const preparation = createOfficialPluginPreparation();
- let disposed: Promise<void> | undefined;
- faults.afterSeedLock = () => {
- disposed = preparation.dispose();
- faults.afterSeedLock = undefined;
- };
- await expect(resolveOfficialPluginRoots({ storageRoot, preparation })).rejects.toMatchObject({
- name: "AbortError",
- });
- await disposed;
- expect(faults.seedLockTargets).toHaveLength(1);
- });
- it("bounds invalidation during preparation and allows a later retry", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- const preparation = createOfficialPluginPreparation();
- faults.afterSeedLock = () => preparation.invalidate();
- try {
- await expect(resolveOfficialPluginRoots({ storageRoot, preparation })).rejects.toMatchObject({
- code: "ZCODE_PLUGIN_SOURCES_CHANGED",
- });
- faults.afterSeedLock = undefined;
- await expect(resolveOfficialPluginRoots({ storageRoot, preparation })).resolves.toEqual([]);
- } finally {
- await preparation.dispose();
- }
- }, 15_000);
- it("degrades a persistent transient seed failure when the target is absent", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- const logger = createLogger();
- faults.promoteTargetSuffix = BROWSER_CACHE_SUFFIX;
- await expect(
- (async () => await resolveOfficialPluginRoots({ logger, storageRoot }))(),
- ).resolves.not.toBeInstanceOf(Error);
- expect(existsSync(join(storageRoot, BROWSER_CACHE_SUFFIX))).toBe(false);
- expect(existsSync(join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX))).toBe(true);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- attempts: 4,
- degraded: true,
- errorCode: "EPERM",
- operation: "seed_plugin",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- });
- it("keeps degraded cache diagnostics through the ordinary plugin resolver", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- const logger = createLogger();
- faults.promoteTargetSuffix = BROWSER_CACHE_SUFFIX;
- await expect(
- (async () =>
- await resolveZCodePlugins({
- logger,
- pluginStorageRoot: storageRoot,
- skipUserConfig: true,
- workingDirectory: dirname(storageRoot),
- }))(),
- ).resolves.not.toBeInstanceOf(Error);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- degraded: true,
- errorCode: "EPERM",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- });
- it("keeps a stale but usable target when promotion fails", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- await resolveOfficialPluginRoots({ storageRoot });
- const browserRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const markerPath = join(browserRoot, ".zcode-plugin-seed.json");
- const staleMarker = `${await readFile(markerPath, "utf8")}\n`;
- await writeFile(
- markerPath,
- staleMarker.replace('"pluginVersion": "', '"pluginVersion": "stale-'),
- );
- const logger = createLogger();
- faults.promoteTargetSuffix = BROWSER_CACHE_SUFFIX;
- await expect(
- (async () => await resolveOfficialPluginRoots({ logger, storageRoot }))(),
- ).resolves.not.toBeInstanceOf(Error);
- expect(existsSync(browserRoot)).toBe(true);
- expect(await readFile(markerPath, "utf8")).toContain('"pluginVersion": "stale-');
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- errorCode: "EPERM",
- operation: "seed_plugin",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- });
- it("falls back to a previous usable version when the new target cannot be promoted", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- expect(await resolveOfficialPluginRoots({ storageRoot })).toEqual([]);
- const currentRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const staleRoot = join(dirname(currentRoot), "0.2.1");
- await rename(currentRoot, staleRoot);
- faults.promoteTargetSuffix = BROWSER_CACHE_SUFFIX;
- const roots = await resolveOfficialPluginRoots({ storageRoot });
- expect(roots).toContain(staleRoot);
- });
- it("does not reactivate older Android code after a failed security update", async () => {
- const definition = OFFICIAL_PLUGIN_DEFINITIONS.find(({ name }) => name === "android-emulator");
- if (!definition) throw new Error("Android definition is missing");
- const storageRoot = await createStorageRoot(cleanups);
- await resolveOfficialPluginRoots({ storageRoot });
- const previousVersion = definition.version;
- const oldRoot = join(storageRoot, OFFICIAL_CACHE_ROOT, definition.name, previousVersion);
- try {
- definition.version = "0.1.2";
- faults.promoteTargetSuffix = join(OFFICIAL_CACHE_ROOT, definition.name, definition.version);
- const roots = await resolveOfficialPluginRoots({ storageRoot });
- expect(existsSync(oldRoot)).toBe(true);
- expect(roots).not.toContain(oldRoot);
- } finally {
- definition.version = previousVersion;
- }
- });
- it("degrades an incomplete seed source to a usable previous version instead of failing startup", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- expect(await resolveOfficialPluginRoots({ storageRoot })).toEqual([]);
- const currentRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const staleRoot = join(dirname(currentRoot), "0.2.1");
- await rename(currentRoot, staleRoot);
- await rm(join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX), { force: true, recursive: true });
- // 入口目录旁只有一份缺 dist/docs/skills 的 browser-use 拷贝(升级中的桌面包、
- // 未构建 dist 的旧 checkout);其余插件继续从真实 monorepo 源解析。
- const entrypointRoot = join(storageRoot, "stale-entrypoint");
- const incompletePluginRoot = join(entrypointRoot, "packages", "browser-use-plugin");
- await mkdir(join(incompletePluginRoot, ".zcode-plugin"), { recursive: true });
- await writeFile(
- join(incompletePluginRoot, ".zcode-plugin", "plugin.json"),
- JSON.stringify({ name: "browser-use", version: "0.5.2" }),
- );
- process.argv[1] = join(entrypointRoot, "zcode.cjs");
- const logger = createLogger();
- let roots: string[] = [];
- await expect(
- (async () => {
- roots = await resolveOfficialPluginRoots({ logger, storageRoot });
- })(),
- ).resolves.not.toBeInstanceOf(Error);
- expect(roots).toContain(staleRoot);
- // 残缺源绝不能落成当前版本缓存(会永久命中版本),也不能拖垮其他插件的 seeding。
- expect(existsSync(currentRoot)).toBe(false);
- expect(existsSync(join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX))).toBe(true);
- const marketplace = JSON.parse(
- await readFile(
- join(storageRoot, "marketplaces", "zcode-plugins-official", "marketplace.json"),
- "utf8",
- ),
- ) as { plugins: Array<{ name: string }> };
- expect(marketplace.plugins.some((plugin) => plugin.name === "browser-use")).toBe(true);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- degraded: true,
- errorCode: "ZCODE_PLUGIN_SEED_INCOMPLETE",
- // browser-use 不再携带 node_repl 宿主产物(那是 node-repl-host 的),
- // 它自己必需的运行时资产是 browser-client。
- missingSeedPaths: expect.arrayContaining(["scripts/browser-client.mjs"]),
- operation: "seed_plugin",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- });
- it("keeps session bootstrap alive when an incomplete seed source has no usable fallback cache", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- // 全新安装:storageRoot 从未 seed 过,也没有任何旧版本缓存可回退。
- const entrypointRoot = join(storageRoot, "fresh-install-entrypoint");
- const incompletePluginRoot = join(entrypointRoot, "packages", "browser-use-plugin");
- await mkdir(join(incompletePluginRoot, ".zcode-plugin"), { recursive: true });
- await writeFile(
- join(incompletePluginRoot, ".zcode-plugin", "plugin.json"),
- JSON.stringify({ name: "browser-use", version: "0.5.2" }),
- );
- process.argv[1] = join(entrypointRoot, "zcode.cjs");
- const logger = createLogger();
- let outcome!: ReturnType<typeof resolveZCodePlugins>;
- await expect(
- (async () => {
- outcome = await resolveZCodePlugins({
- logger,
- pluginStorageRoot: storageRoot,
- skipUserConfig: true,
- workingDirectory: dirname(storageRoot),
- });
- })(),
- ).resolves.not.toBeInstanceOf(Error);
- // 降级终态之二:无可用旧缓存时该插件本次缺席(不写残缺缓存),只留 warning 级诊断,
- // 其余 defaultEnabled 插件与会话引导不受影响。
- expect(existsSync(join(storageRoot, BROWSER_CACHE_SUFFIX))).toBe(false);
- expect(
- outcome.plugins.find((plugin) => plugin.id === "browser-use@zcode-plugins-official"),
- ).toBeUndefined();
- const rootNotFound = outcome.diagnostics.filter(
- (diagnostic) => diagnostic.code === "plugin_root_not_found",
- );
- expect(rootNotFound).toEqual([
- expect.objectContaining({
- path: join(storageRoot, BROWSER_CACHE_SUFFIX),
- severity: "warning",
- }),
- ]);
- const documentSkills = outcome.plugins.find(
- (plugin) => plugin.id === "documents@zcode-plugins-official",
- );
- expect(documentSkills?.enabled).toBe(true);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- errorCode: "ZCODE_PLUGIN_SEED_INCOMPLETE",
- operation: "seed_plugin",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- });
- it("degrades a held seed lock to a usable previous version instead of failing startup", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- expect(await resolveOfficialPluginRoots({ storageRoot })).toEqual([]);
- const currentRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const staleRoot = join(dirname(currentRoot), "0.2.1");
- await rename(currentRoot, staleRoot);
- const lockRoot = `${currentRoot}.seed-lock`;
- await mkdir(lockRoot, { recursive: true });
- await writeFile(
- join(lockRoot, "owner.json"),
- JSON.stringify({ createdAt: new Date().toISOString(), pid: process.pid }),
- );
- const logger = createLogger();
- faults.seedLockHeldTargetSuffixes = [BROWSER_CACHE_SUFFIX];
- let roots: string[] = [];
- await expect(
- (async () => {
- roots = await resolveOfficialPluginRoots({ logger, storageRoot });
- })(),
- ).resolves.not.toBeInstanceOf(Error);
- expect(roots).toContain(staleRoot);
- // 锁属于「另一个进程」(owner 存活),降级方绝不能顺手删掉它。
- expect(existsSync(lockRoot)).toBe(true);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- degraded: true,
- errorCode: "ZCODE_PLUGIN_SEED_LOCK_TIMEOUT",
- operation: "seed_plugin",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- });
- it("shares one seed lock wait budget across all plugins", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- expect(await resolveOfficialPluginRoots({ storageRoot })).toEqual([]);
- for (const cacheSuffix of [BROWSER_CACHE_SUFFIX, SKILL_CREATOR_CACHE_SUFFIX]) {
- const lockRoot = `${join(storageRoot, cacheSuffix)}.seed-lock`;
- await mkdir(lockRoot, { recursive: true });
- await writeFile(
- join(lockRoot, "owner.json"),
- JSON.stringify({ createdAt: new Date().toISOString(), pid: process.pid }),
- );
- }
- faults.seedLockHeldTargetSuffixes = [BROWSER_CACHE_SUFFIX, SKILL_CREATOR_CACHE_SUFFIX];
- await expect(
- (async () => await resolveOfficialPluginRoots({ storageRoot }))(),
- ).resolves.not.toBeInstanceOf(Error);
- // 两把被持有的锁必须共享同一截止时间:后一次调用拿到的剩余预算严格更小,
- // 而不是每个插件都重置成完整的 15s(否则成组遗留锁会让启动冻结 N×15s)。
- expect(faults.seedLockObservedTimeoutsMs).toHaveLength(2);
- const [firstBudgetMs, secondBudgetMs] = faults.seedLockObservedTimeoutsMs;
- expect(firstBudgetMs).toBeGreaterThan(0);
- expect(firstBudgetMs).toBeLessThanOrEqual(15_000);
- expect(secondBudgetMs).toBeLessThan(firstBudgetMs as number);
- });
- it("keeps an intact current cache discoverable when the seed lock times out", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- expect(await resolveOfficialPluginRoots({ storageRoot })).toEqual([]);
- // 当前版本目录保持完整(不 rename 成旧版本),只把锁交给「另一个存活进程」。
- const currentRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const lockRoot = `${currentRoot}.seed-lock`;
- await mkdir(lockRoot, { recursive: true });
- await writeFile(
- join(lockRoot, "owner.json"),
- JSON.stringify({ createdAt: new Date().toISOString(), pid: process.pid }),
- );
- const logger = createLogger();
- faults.seedLockHeldTargetSuffixes = [BROWSER_CACHE_SUFFIX];
- const outcome = await resolveZCodePlugins({
- logger,
- pluginStorageRoot: storageRoot,
- skipUserConfig: true,
- workingDirectory: dirname(storageRoot),
- });
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- errorCode: "ZCODE_PLUGIN_SEED_LOCK_TIMEOUT",
- operation: "seed_plugin",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- // 锁只保护 seed 写入,不保护读取:等锁超时不能让一份完整的当前缓存从 discovery 消失。
- // 该不变量依赖 bundled marketplace 分片在 seeding 循环之前写入且不按 seed 成败过滤;
- // 若未来把分片写入挪到循环之后,这条断言会失败。
- const browserPlugin = outcome.plugins.find(
- (plugin) => plugin.id === "browser-use@zcode-plugins-official",
- );
- expect(browserPlugin?.rootPath).toBe(currentRoot);
- expect(browserPlugin?.enabled).toBe(true);
- expect(
- outcome.diagnostics.filter((diagnostic) => diagnostic.code === "plugin_root_not_found"),
- ).toEqual([]);
- });
- it("does not mix an old zcode-cua cache with the current consumer contract", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- await resolveOfficialPluginRoots({ storageRoot });
- const currentRoot = join(storageRoot, CUA_CACHE_SUFFIX);
- const staleRoot = join(dirname(currentRoot), "0.5.9");
- await rename(currentRoot, staleRoot);
- faults.promoteTargetSuffix = CUA_CACHE_SUFFIX;
- const roots = await resolveOfficialPluginRoots({ storageRoot });
- expect(roots).not.toContain(staleRoot);
- });
- it("continues when another process moves the target after the existence check", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- await resolveOfficialPluginRoots({ storageRoot });
- const browserRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const markerPath = join(browserRoot, ".zcode-plugin-seed.json");
- const staleMarker = `${await readFile(markerPath, "utf8")}\n`;
- await writeFile(
- markerPath,
- staleMarker.replace('"pluginVersion": "', '"pluginVersion": "stale-'),
- );
- faults.moveTargetSuffix = BROWSER_CACHE_SUFFIX;
- await expect(
- (async () => await resolveOfficialPluginRoots({ storageRoot }))(),
- ).resolves.not.toBeInstanceOf(Error);
- expect(await readFile(markerPath, "utf8")).not.toContain('"pluginVersion": "stale-');
- faults.moveTargetSuffix = undefined;
- await expect(
- (async () => await resolveOfficialPluginRoots({ storageRoot }))(),
- ).resolves.not.toBeInstanceOf(Error);
- await expectNoSeedBackups(browserRoot);
- });
- it("replaces an overlay lower-layer target when moving it to backup returns EXDEV", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- await resolveOfficialPluginRoots({ storageRoot });
- const browserRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const markerPath = join(browserRoot, ".zcode-plugin-seed.json");
- const staleMarker = `${await readFile(markerPath, "utf8")}\n`;
- await writeFile(
- markerPath,
- staleMarker.replace('"pluginVersion": "', '"pluginVersion": "stale-'),
- );
- const logger = createLogger();
- faults.crossDeviceBackupTargetSuffix = BROWSER_CACHE_SUFFIX;
- await expect(
- (async () =>
- await resolveZCodePlugins({
- logger,
- pluginStorageRoot: storageRoot,
- skipUserConfig: true,
- workingDirectory: dirname(storageRoot),
- }))(),
- ).resolves.not.toBeInstanceOf(Error);
- // 镜像下层的旧缓存无法 rename 成 backup,但仍须被替换成当前内容,而不是每次启动都失败。
- expect(await readFile(markerPath, "utf8")).not.toContain('"pluginVersion": "stale-');
- expect(existsSync(join(browserRoot, ".zcode-plugin", "plugin.json"))).toBe(true);
- await expectNoSeedBackups(browserRoot);
- const siblings = await readdir(dirname(browserRoot));
- expect(siblings.filter((entry) => entry.includes(".tmp-"))).toEqual([]);
- expect(logger.warn).not.toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({ pluginId: "browser-use@zcode-plugins-official" }),
- );
- });
- it("degrades a persistent EXDEV promotion to a usable previous version", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- expect(await resolveOfficialPluginRoots({ storageRoot })).toEqual([]);
- const currentRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const staleRoot = join(dirname(currentRoot), "0.2.1");
- await rename(currentRoot, staleRoot);
- const logger = createLogger();
- faults.crossDevicePromoteTargetSuffix = BROWSER_CACHE_SUFFIX;
- let roots: string[] = [];
- await expect(
- (async () => {
- roots = await resolveOfficialPluginRoots({ logger, storageRoot });
- })(),
- ).resolves.not.toBeInstanceOf(Error);
- expect(roots).toContain(staleRoot);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- degraded: true,
- errorCode: "EXDEV",
- operation: "seed_plugin",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- });
- it("drops the seed marker before deleting an EXDEV target so an interrupted delete reseeds", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- await resolveOfficialPluginRoots({ storageRoot });
- const browserRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const markerPath = join(browserRoot, ".zcode-plugin-seed.json");
- const manifestPath = join(browserRoot, ".zcode-plugin", "plugin.json");
- // marker 仍是当前版本,但 manifest 已丢失:走「当前缓存不可用 → 重新 seed」分支。
- await rm(manifestPath, { force: true });
- faults.crossDeviceBackupTargetSuffix = BROWSER_CACHE_SUFFIX;
- // 删除旧 target 中途失败,模拟进程在删除窗口内退出。
- faults.removeTargetSuffix = BROWSER_CACHE_SUFFIX;
- await expect(
- (async () => await resolveOfficialPluginRoots({ logger: createLogger(), storageRoot }))(),
- ).resolves.not.toBeInstanceOf(Error);
- // marker 必须先于目录内容删除;否则残缺目录会带着当前版本被下次启动直接复用。
- expect(existsSync(markerPath)).toBe(false);
- faults.removeTargetSuffix = undefined;
- await expect(
- (async () => await resolveOfficialPluginRoots({ storageRoot }))(),
- ).resolves.not.toBeInstanceOf(Error);
- expect(existsSync(manifestPath)).toBe(true);
- expect(existsSync(markerPath)).toBe(true);
- await expectNoSeedBackups(browserRoot);
- });
- it("converges when another process completes the current seed during promotion", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- await resolveOfficialPluginRoots({ storageRoot });
- const browserRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const markerPath = join(browserRoot, ".zcode-plugin-seed.json");
- const staleMarker = `${await readFile(markerPath, "utf8")}\n`;
- await writeFile(
- markerPath,
- staleMarker.replace('"pluginVersion": "', '"pluginVersion": "stale-'),
- );
- faults.promoteCurrentTargetSuffix = BROWSER_CACHE_SUFFIX;
- await expect(
- (async () => await resolveOfficialPluginRoots({ storageRoot }))(),
- ).resolves.not.toBeInstanceOf(Error);
- expect(await readFile(markerPath, "utf8")).not.toContain('"pluginVersion": "stale-');
- await expectNoSeedBackups(browserRoot);
- });
- it("does not restore a fixed backup that may belong to an active replacement", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- await resolveOfficialPluginRoots({ storageRoot });
- const browserRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const backupRoot = `${browserRoot}.backup`;
- await rename(browserRoot, backupRoot);
- faults.recoverBackupTargetSuffix = BROWSER_CACHE_SUFFIX;
- await expect(
- (async () => await resolveOfficialPluginRoots({ storageRoot }))(),
- ).resolves.not.toBeInstanceOf(Error);
- expect(existsSync(browserRoot)).toBe(true);
- await expectNoSeedBackups(browserRoot);
- });
- it("keeps a current target usable when only runtime manifest refresh is locked", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- await resolveOfficialPluginRoots({ storageRoot });
- const logger = createLogger();
- process.argv[1] = join(storageRoot, "different-zcode.cjs");
- faults.runtimeManifestTargetSuffix = ANDROID_MANIFEST_SUFFIX;
- await expect(
- (async () => await resolveOfficialPluginRoots({ logger, storageRoot }))(),
- ).resolves.not.toBeInstanceOf(Error);
- expect(existsSync(join(storageRoot, ANDROID_CACHE_SUFFIX))).toBe(true);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- degraded: true,
- operation: "seed_plugin",
- pluginId: "android-emulator@zcode-plugins-official",
- }),
- );
- });
- it("keeps a concurrent winner when runtime manifest temporary file disappears", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- await resolveOfficialPluginRoots({ storageRoot });
- const logger = createLogger();
- process.argv[1] = join(storageRoot, "different-zcode.cjs");
- faults.runtimeManifestNotFoundTargetSuffix = ANDROID_MANIFEST_SUFFIX;
- await expect(
- (async () => await resolveOfficialPluginRoots({ logger, storageRoot }))(),
- ).resolves.not.toBeInstanceOf(Error);
- expect(existsSync(join(storageRoot, ANDROID_CACHE_SUFFIX))).toBe(true);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- degraded: true,
- errorCode: "ENOENT",
- operation: "seed_plugin",
- pluginId: "android-emulator@zcode-plugins-official",
- }),
- );
- });
- it("preserves suppressed plugin catalog/cache without attempting a Windows-locked deletion", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- await resolveOfficialPluginRoots({ storageRoot });
- const logger = createLogger();
- faults.removeTargetSuffix = SKILL_CREATOR_CACHE_SUFFIX;
- await expect(
- (async () =>
- await resolveOfficialPluginRoots({
- logger,
- storageRoot,
- suppressedBuiltins: new Set(["skill-creator@zcode-plugins-official"]),
- }))(),
- ).resolves.not.toBeInstanceOf(Error);
- expect(existsSync(join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX))).toBe(true);
- const marketplace = JSON.parse(
- await readFile(
- join(storageRoot, "marketplaces", "zcode-plugins-official", "marketplace.json"),
- "utf8",
- ),
- ) as { plugins: Array<{ name: string }> };
- // 现行契约:suppressedBuiltins 只控制 runtime discovery。catalog/cache 是恢复入口
- // 的不可变资产,不应触发删除;注入的 EPERM 因而不会被命中,也不产生降级告警。
- expect(marketplace.plugins.some((plugin) => plugin.name === "skill-creator")).toBe(true);
- expect(logger.warn).not.toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({ operation: "remove_suppressed_plugin" }),
- );
- });
- });
- async function createStorageRoot(cleanups: string[]): Promise<string> {
- const root = await mkdtemp(join(tmpdir(), "zcode-plugin-cache-resilience-"));
- cleanups.push(root);
- return join(root, "plugins");
- }
- async function createVersionedSource(storageRoot: string) {
- const entrypointRoot = join(storageRoot, "versioned-source");
- const root = join(entrypointRoot, "packages", "skill-creator-plugin");
- await mkdir(join(root, ".zcode-plugin"), { recursive: true });
- await mkdir(join(root, "skills", "demo"), { recursive: true });
- await writeFile(
- join(root, ".zcode-plugin", "plugin.json"),
- JSON.stringify({ name: "skill-creator", version: "0.1.0" }),
- );
- const payload = join(root, "skills", "demo", "SKILL.md");
- await writeFile(payload, "original source");
- process.argv[1] = join(entrypointRoot, "zcode.cjs");
- faults.watchedSourceRoot = root;
- return { root, payload };
- }
- async function expectNoSeedBackups(targetRoot: string): Promise<void> {
- const targetName = basename(targetRoot);
- const siblings = await readdir(dirname(targetRoot));
- expect(siblings.filter((entry) => entry.startsWith(`${targetName}.backup`))).toEqual([]);
- }
- function createLogger(): Logger & { warn: ReturnType<typeof vi.fn> } {
- const logger = {
- child: () => logger,
- debug: vi.fn(),
- error: vi.fn(),
- info: vi.fn(),
- warn: vi.fn(),
- };
- return logger;
- }
- function exhaustedErrno(code: string): NodeJS.ErrnoException {
- return Object.assign(new Error(code), {
- code,
- zcodeOfficialPluginCacheAttempts: 4,
- });
- }
- function errno(code: string): NodeJS.ErrnoException {
- return Object.assign(new Error(code), { code });
- }
|