/** * The plugin owning the plane routes, on the real host (tiers A and B; D13). * * The P-R1 rows of the plan's test matrix that the characterization does not * already carry: who owns the planes and how often pi-ai restarts for it, the * claim's lifecycle, how an edit flows through it, which routes are * registered, where a key comes from (D20), and that nothing of a sibling * route leaks into a plane. Every case boots its own host and reads the * registry, the profile file and the wire — never the plugin's own state. * * This file holds who owns the planes and the claim's lifecycle; its siblings * hold the config flow and the route set (`host-planes-config`) and the * credentials and isolation (`host-planes-credentials`). The shared boot and * readers are `support/host-planes.ts`. * * @module test/host-planes.test */ import { readFileSync, writeFileSync } from "node:fs"; import { readProfilePatches, reconcileProfilePatches, } from "@deepseek-ai/dsh-app-boot"; import { afterEach, describe, expect, it, onTestFinished, vi } from "vitest"; import { rawConfigOf } from "./support/config-shape.ts"; import { appendTo, bootPlanes, disposeBooted, offline, routeIds, settle, stateOf, turn, wire, } from "./support/host-planes.ts"; import { type ApplyOrder, bootTierB, CHARACTERIZED_MODELS, characterizedProviders, drainStream, fakeCredentials, FIBER_STATE, hostModulesForTests, logLines, mountTierA, ownerOf, PLANES, S0_TODAY, s0Providers, SAVE_CANDIDATE, type TierA, type TierB, until, waitForPlaneOwner, } from "./support/host.ts"; import { apply, Config, inject, LEGACY_NAME, LEGACY_PKG, name, replaceHostModuleSource, } from "./support/src.ts"; let tierA: TierA | undefined; let tierB: TierB | undefined; afterEach(async () => { try { await tierA?.dispose(); await tierB?.dispose(); await disposeBooted(); } finally { tierA = undefined; tierB = undefined; vi.restoreAllMocks(); vi.unstubAllGlobals(); } }); describe( "host planes · ownership and restart counts (tier B)", { timeout: 30_000 }, () => { it.each([ ["pi-ai-first", 1], ["plugin-first", 0], ] as const)( "%s: pi-ai restarts %i time(s), both planes are the plugin's, and no registration is ever refused", async (order, expected) => { const { logged, restarts, tier } = await bootPlanes({ order }); await waitForPlaneOwner(tier.llm); await settle(); expect(restarts()).toBe(expected); expect(routeIds(tier.llm)).toEqual(["deepseek", ...PLANES]); // A DUPLICATE_ADAPTER that escaped into pi-ai's apply fails pi-ai; one // the claim caught is logged as "still held elsewhere", and any other // refusal as "did not register". None of them happens in either order. expect(tier.piFiber.state).toBe(FIBER_STATE.ACTIVE); expect(tier.pluginEntry?.fiber?.state).toBe(FIBER_STATE.ACTIVE); tier.ctx.logger.info("[host-planes] the log is read"); const lines = logged(); expect(lines.at(-1)).toContain("[host-planes] the log is read"); expect( lines.filter((line) => /still held elsewhere|plane routes did not register|DUPLICATE_ADAPTER/u.test( line ) ) ).toEqual([]); // A row filed under the current id and package is not told to rename. expect(lines).not.toContainEqual( expect.stringContaining("has been renamed") ); } ); it.each([ [ "plugin-first", "an invalid plugin row", { pluginRow: { config: { debug: "not-a-boolean" } } }, ], [ "pi-ai-first", "an invalid pi-ai profile", { providers: { deepseek: { displayName: 42 } } }, ], ] as const)( "tier B, %s with %s: the first row FAILS without applying, the held row is still released, and the boot finishes promptly", async (order, _what, rows) => { // Before the fallback release, the held row waited for an `apply` that // never came, and the boot hung until the hook timed out. vi.spyOn(process.stderr, "write").mockImplementation(() => true); offline(); tierB = await bootTierB({ order: order as ApplyOrder, ...rows }); const [failedEntry, heldEntry] = order === "plugin-first" ? [tierB.pluginEntry, tierB.piEntry] : [tierB.piEntry, tierB.pluginEntry]; expect(failedEntry?.fiber?.state).toBe(FIBER_STATE.FAILED); // The real error is readable, not a hook timeout. await expect(failedEntry?.fiber?.await()).rejects.toThrow( order === "plugin-first" ? /debug/u : /displayName/u ); expect(heldEntry?.fiber?.state).toBe(FIBER_STATE.ACTIVE); }, 15_000 ); it("pi-ai FAILED for a sibling route while its plane entries are valid: nothing is claimed, the plugin stays active, and no plane is listed", async () => { const providers = characterizedProviders(); // A route-level error on `deepseek` fails pi-ai's whole fiber; the // plane entries themselves are well formed and stay in its raw config. providers.deepseek = { ...providers.deepseek, api: "nope" }; const { tier } = await bootPlanes({ order: "pi-ai-first", providers }); await until( () => stateOf(tier.piFiber) === FIBER_STATE.FAILED && stateOf(tier.pluginEntry?.fiber) === FIBER_STATE.ACTIVE, "pi-ai fails and the plugin is active" ); // The claim reconciles on every registry change and after its mount; // let every one of them run before reading the registry. await settle(); const raw = (rawConfigOf(tier.piFiber) as { providers: object }) .providers; expect(Object.keys(raw)).toEqual(expect.arrayContaining([...PLANES])); expect( tier.llm .listProviders() .filter((provider) => (PLANES as readonly string[]).includes(provider.id) ) ).toEqual([]); }); } ); describe("host planes · lifecycle", { timeout: 30_000 }, () => { // Either half of the old filing is enough: the notice fires on the legacy // id OR the legacy package, so each is booted on its own (the other half is // the current one). it.each([ ["id", { id: LEGACY_NAME }], ["package", { name: LEGACY_PKG }], ] as const)( "a row still filed under the pre-rename %s is told to rename, and serves the planes all the same (tier B)", async (_half, pluginRow) => { const { logged, tier } = await bootPlanes({ pluginRow }); await waitForPlaneOwner(tier.llm); // The row really is filed the way the case says. expect(tier.pluginEntry?.options.id).toBe( "id" in pluginRow ? pluginRow.id : name ); tier.ctx.logger.info("[host-planes] the log is read"); const lines = logged(); expect(lines.at(-1)).toContain("[host-planes] the log is read"); expect(lines).toContainEqual( expect.stringContaining(`"${LEGACY_PKG}" has been renamed to "${name}"`) ); } ); it("disabling the plugin's row hands both planes back to pi-ai, and the next Settings save of a plane holding a gap id is refused with the file unchanged (tier B)", async () => { const { tier } = await bootPlanes(); await waitForPlaneOwner(tier.llm); const { pluginEntry } = tier; if (pluginEntry === undefined) { throw new Error("no plugin row"); } // Disable the row in the profile patch and reload the profile, the way // a profile edit reaches a running host: the loader disposes its fiber. const rows = JSON.parse( readFileSync(tier.profile.patchPath, "utf8") ) as unknown[]; writeFileSync( tier.profile.patchPath, `${JSON.stringify([...rows, { disabled: true, id: pluginEntry.options.id }], null, 2)}\n` ); await reconcileProfilePatches( tier.ctx as never, readProfilePatches("dsh", tier.profile), "dsh" ); await until( () => pluginEntry.fiber === undefined || pluginEntry.fiber.uid === null, "the plugin row is disposed" ); await waitForPlaneOwner(tier.llm, PLANES, "PiAiAdapter"); expect(routeIds(tier.llm)).toEqual(["deepseek", ...PLANES]); const before = readFileSync(tier.profile.patchPath); await expect( tier.ctx.configEditor.edit( tier.piEntry, appendTo("opencode", SAVE_CANDIDATE) ) ).rejects.toThrow(S0_TODAY.saveBlockedByStoredGap); expect(readFileSync(tier.profile.patchPath)).toEqual(before); }); it("dispose, then apply again (tier A): the planes go back to the plugin", async () => { offline(); tierA = await mountTierA({ plugin: {}, providers: s0Providers() }); await waitForPlaneOwner(tierA.llm); expect(globalThis.fetch, "the mounted plugin patches fetch").not.toBe( wire.fetch ); await tierA.pluginFiber?.dispose(); await waitForPlaneOwner(tierA.llm, PLANES, "PiAiAdapter"); // The real composition's teardown — the claim restarting pi-ai while the // patch unwinds — leaves the wire's own fetch in place. expect(globalThis.fetch, "the disposed plugin restored fetch").toBe( wire.fetch ); await tierA.ctx.plugin( { apply, Config, inject, name } as never, {} as never ); await waitForPlaneOwner(tierA.llm); expect(routeIds(tierA.llm)).toEqual(["deepseek", ...PLANES]); }); it("the plugin's row disposed while its mount's pi-ai restart is in flight: the restart resolves, nothing is logged at error, and the planes go back to pi-ai (tier A)", async () => { offline(); // pi-ai alone first: it resolves holding both planes, so the plugin's // mount restarts it once to take them. tierA = await mountTierA({ providers: s0Providers() }); await waitForPlaneOwner(tierA.llm, PLANES, "PiAiAdapter"); // Mounted by hand below, so the host's modules are handed in here, as // `mountTierA` does for a plugin it mounts itself. const restoreSource = replaceHostModuleSource(hostModulesForTests()); onTestFinished(restoreSource); const { ctx, piFiber } = tierA; const log = logLines(ctx); // Known answer: the exporter reads what a logger writes. ctx.logger("known").error("known answer"); let restarted: Promise | undefined; const row: { plugin?: { dispose: () => Promise } } = {}; const original = piFiber.restart.bind(piFiber); vi.spyOn(piFiber, "restart").mockImplementationOnce(async () => { restarted = original(); // The row is disabled while that restart runs: the plugin's fiber is // disposed (uid null) and still LOADING, so cordis refuses any effect // it registers until its teardown has run. void row.plugin?.dispose(); await restarted; }); const plugin = ctx.plugin( { apply, Config, inject, name } as never, {} as never ); row.plugin = plugin; await until(() => restarted !== undefined, "the mount restarts pi-ai"); // pi-ai's own restart ends in ACTIVE, whose status emit reaches the // claim's listener: it must not throw into pi-ai's lifecycle. await expect(restarted).resolves.toBeUndefined(); await plugin.dispose(); await waitForPlaneOwner(tierA.llm, PLANES, "PiAiAdapter"); expect(stateOf(piFiber)).toBe(FIBER_STATE.ACTIVE); const lines = log.lines(); expect(lines).toContain("error known: known answer"); expect( lines.filter( (line) => line !== "error known: known answer" && (line.startsWith("error ") || line.includes("did not mount")) ) ).toEqual([]); }); it("an edit of the plugin's own row restarts pi-ai zero times and keeps the planes the plugin's (tier B)", async () => { const { restarts, tier } = await bootPlanes(); await waitForPlaneOwner(tier.llm); const before = restarts(); const { pluginEntry } = tier; if (pluginEntry === undefined) { throw new Error("no plugin row"); } await tier.ctx.configEditor.edit(pluginEntry, (current) => ({ ...current, debug: true, })); await until( () => (pluginEntry.fiber?.config as { debug?: boolean })?.debug === true, "the plugin row reloads" ); await waitForPlaneOwner(tier.llm); expect(restarts() - before).toBe(0); }); it("a volatile edit of the plugin's own row commits in place, and the plugin follows it only once re-applied (tier B; the card's re-apply line)", async () => { const { tier } = await bootPlanes(); await waitForPlaneOwner(tier.llm); const { pluginEntry } = tier; if (pluginEntry === undefined) { throw new Error("no plugin row"); } const volatile: unknown[] = []; tier.ctx.on( "loader/volatile-update", (paths: unknown) => { volatile.push(paths); }, { global: true } ); const model = CHARACTERIZED_MODELS.opencode[0] ?? ""; // The plane headers of one turn's first request. `x-opencode-client` // (`injectOriginHeaders`, on by default) is the positive control: it goes // out on every turn the plugin decorates, so a missing project header // means the plugin read its row, not that it stopped decorating. const planeHeaders = async (): Promise<{ client: unknown; project: unknown; }> => { const { requests } = await turn(tier.llm, "opencode", model); expect(requests.length, "the turn reached the wire").toBeGreaterThan(0); const headers = requests[0]?.headers ?? {}; return { client: headers["x-opencode-client"], project: headers["x-opencode-project"], }; }; // A known answer first: the default sends the header. expect(await planeHeaders()).toEqual({ client: "cli", project: "global" }); const { fiber } = pluginEntry; // The write the card's save performs: Settings' `mutate` on the row's // namespace, which checks the path is volatile before the config editor // commits it. await tier.ctx.settings.mutate(pluginEntry.options.id, [ { op: "set", path: ["injectProject"], value: false }, ]); // The loader committed it as a volatile edit: one update naming the key, // and the same fiber, so nothing re-ran the plugin's apply. expect(volatile).toEqual([[["injectProject"]]]); expect(pluginEntry.fiber).toBe(fiber); expect(stateOf(fiber)).toBe(FIBER_STATE.ACTIVE); // The plugin read its row once, at apply: the header still goes out. expect(await planeHeaders()).toEqual({ client: "cli", project: "global" }); // Re-applied, it follows the row. await fiber?.restart(); await until( () => stateOf(pluginEntry.fiber) === FIBER_STATE.ACTIVE, "the plugin row applies again" ); await waitForPlaneOwner(tier.llm); expect(await planeHeaders()).toEqual({ client: "cli", project: undefined }); }); it("an llm restart registers every route again, the planes the plugin's (tier B)", async () => { const { tier } = await bootPlanes(); await waitForPlaneOwner(tier.llm); const llmEntry = tier.ctx.configEditor .entries() .find((entry) => entry.options.id === "llm"); await llmEntry?.fiber?.restart(); await until( () => routeIds(tier.ctx.llm).length === 3, "every route registers again" ); await waitForPlaneOwner(tier.ctx.llm); expect(routeIds(tier.ctx.llm)).toEqual(["deepseek", ...PLANES]); }); it("pi-ai restarting itself while claimed keeps the planes the plugin's, and throws nothing (tier B)", async () => { const { tier } = await bootPlanes(); await waitForPlaneOwner(tier.llm); await expect(tier.piFiber.restart()).resolves.toBeUndefined(); await waitForPlaneOwner(tier.llm); expect(ownerOf(tier.llm, "deepseek")).toBe("PiAiAdapter"); }); it("a turn in flight completes after the plugin is disposed under it (tier A)", async () => { let release: (() => void) | undefined; const held = new Promise((resolve) => { release = resolve; }); offline({ respond: (request) => { const chunk = (delta: object, finish: string | null) => `data: ${JSON.stringify({ choices: [{ delta, finish_reason: finish, index: 0 }], created: 1, id: "inflight", model: request.model, object: "chat.completion.chunk", })}\n\n`; const encoder = new TextEncoder(); const body = new ReadableStream({ async start(controller) { controller.enqueue( encoder.encode(chunk({ content: "hi", role: "assistant" }, null)) ); await held; controller.enqueue(encoder.encode(chunk({}, "stop"))); controller.enqueue(encoder.encode("data: [DONE]\n\n")); controller.close(); }, }); return new Response(body, { headers: { "content-type": "text/event-stream" }, status: 200, }); }, }); tierA = await mountTierA({ credentials: fakeCredentials({ refs: { OPENCODE_API_KEY: "k" } }), plugin: {}, providers: { opencode: { apiKeyEnv: "OPENCODE_API_KEY", models: [{ id: "deepseek-v4-pro" }], }, }, }); await waitForPlaneOwner(tierA.llm, ["opencode"]); const inFlight = drainStream(tierA.llm, "opencode", "deepseek-v4-pro"); await until(() => wire.requests.length > 0, "the turn is sent"); await tierA.pluginFiber?.dispose(); release?.(); const drained = await inFlight; expect(drained.error).toBeUndefined(); expect(drained.finish?.kind).not.toBe("error"); expect(drained.types).toContain("text-delta"); }); }); describe( "host planes · pi-ai's row removed (tier B)", { timeout: 30_000 }, () => { it("withdraws both planes and stays quiet when the loader forgets pi-ai's entry before it reports the dispose", async () => { const { tier, logged, restarts } = await bootPlanes(); await waitForPlaneOwner(tier.llm); await settle(); const before = restarts(); const linesBefore = logged().length; const { piEntry } = tier; // The loader's own removal: it deletes the row's entry from its store, // disposes the fiber and only then emits `loader/partial-dispose`, so the // claim's reconcile cannot find the entry it served. Known answer: the // entry is in the store before the removal. const group = piEntry.parent as unknown as { remove: (id: string) => void; tree: { store: Record }; }; expect(group.tree.store[piEntry.options.id]).toBe(piEntry); group.remove(piEntry.options.id); expect(group.tree.store[piEntry.options.id]).toBeUndefined(); await until( () => routeIds(tier.llm).length === 0, "pi-ai's routes are withdrawn" ); await settle(); expect(stateOf(tier.pluginEntry?.fiber)).toBe(FIBER_STATE.ACTIVE); // Nothing is handed back to a removed row, and nothing is reported. expect(restarts()).toBe(before); expect( logged() .slice(linesBefore) .filter((line) => /^(warn|error) /u.test(line)) ).toEqual([]); }); } );