import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { fauxProvider } from "@earendil-works/pi-ai"; import { afterEach, describe, expect, it } from "vitest"; import { createPiSession, type PiSession, } from "../../../test/fixture/create-pi-session.ts"; import { __test } from "../index.ts"; const extensionPath = path.resolve(import.meta.dirname, "../index.ts"); const pairs = [{ executor: "openai/gpt-4o", advisor: "openai/gpt-4o" }]; interface NotifyCall { args: unknown[]; } /** * Install a recording UI context so `hasUI` is true (the extension sees an * interactive UI) and `ctx.ui.notify` calls are captured. */ function installRecordingUi(session: PiSession): NotifyCall[] { const calls: NotifyCall[] = []; session.session.extensionRunner.setUIContext( { ...session.session.extensionRunner.createContext().ui, notify: (...args: unknown[]) => calls.push({ args }), }, "print", ); return calls; } type Case = { user?: unknown; project?: unknown; untrusted?: boolean; legacy?: unknown; }; describe("dilemma settings resolution", () => { let session: PiSession | undefined; let root: string | undefined; afterEach(async () => { await session?.dispose(); session = undefined; if (root) rmSync(root, { recursive: true, force: true }); root = undefined; }); async function start(input: Case) { root = mkdtempSync(path.join(tmpdir(), "pi-ext-dilemma-settings-")); if ("project" in input) { mkdirSync(path.join(root, ".pi"), { recursive: true }); writeFileSync( path.join(root, ".pi", "settings.json"), JSON.stringify({ dilemma: input.project }), ); } session = await createPiSession({ cwd: root, extensionPaths: [extensionPath], userSettings: { ...("user" in input ? { dilemma: input.user } : {}), ...("legacy" in input ? { angel: input.legacy } : {}), }, }); if (input.untrusted) session.session.settingsManager.setProjectTrusted(false); // Angel activates only when its configured pairs resolve to real models; // provide openai/gpt-4o through a deterministic provider. const faux = fauxProvider({ provider: "openai", models: [{ id: "gpt-4o", maxTokens: 8_192 }], }); const registry = session.session.extensionRunner.createContext().modelRegistry; registry.registerProvider("openai", { api: faux.api, apiKey: "test-key", models: faux.models, streamSimple: (model, context, options) => faux.provider.streamSimple(model, context, options), }); await registry.refresh({ allowNetwork: false }); // Angel resolves its pair against the current model; make it openai/gpt-4o. const current = registry.find("openai", "gpt-4o"); if (!current) throw new Error("missing faux openai/gpt-4o model"); await session.session.setModel(current); const notifications = installRecordingUi(session); await session.session.extensionRunner.emit({ type: "session_start", reason: "startup", }); const warnings = notifications .filter((call) => call.args[1] === "warning") .map((call) => call.args[0]); return { active: session.session.getActiveToolNames().includes("angel"), warnings, }; } it("resolves exact registered advisor identities and inherits the executor provider for bare IDs", async () => { await start({ user: { pairs } }); const ctx = session!.session.extensionRunner.createContext(); const provider = "https://models.example/v1"; const faux = fauxProvider({ provider, models: [{ id: "org/advisor" }, { id: "advisor" }], }); ctx.modelRegistry.registerProvider(faux.provider); const advisor = ctx.modelRegistry.find(provider, "org/advisor")!; const executor = ctx.modelRegistry.find(provider, "advisor")!; for (const ref of [`${provider}/org/advisor`, "advisor"]) { const binding = __test.resolveBinding( ctx, { pairs: [{ executor: executor.id, advisor: ref }] }, executor, ); expect(binding?.advisor).toBe(ref === "advisor" ? executor : advisor); } for (const ref of [ "org/advisor", `${provider}/org/adv`, `${provider}/org/advisor:high`, `${provider.toUpperCase()}/org/advisor`, `${provider}/missing`, `/${provider}/org/advisor`, ]) { expect( __test.resolveBinding( ctx, { pairs: [{ executor: executor.id, advisor: ref }] }, executor, ), ).toBeUndefined(); } }); it.each([ ["default unconfigured", {}, false], ["legacy config ignored", { legacy: { pairs } }, false], ["custom Devil threshold", { user: { pairs, devilThreshold: 2 } }, true], ["user pairs", { user: { pairs } }, true], ["project pairs", { project: { pairs } }, true], [ "project enabled false merged over user pairs", { user: { pairs }, project: { enabled: false } }, false, ], [ "project enabled true merged over user enabled false", { user: { pairs, enabled: false }, project: { enabled: true } }, true, ], [ "project pairs array replaces user pairs", { user: { pairs }, project: { pairs: [] } }, false, ], [ "untrusted project ignored", { user: { pairs }, project: { enabled: false }, untrusted: true }, true, ], ] satisfies [string, Case, boolean][])( "%s", async (_name, input, expected) => { const { active, warnings } = await start(input); expect(active).toBe(expected); expect(warnings).toEqual([]); }, ); it.each([ [ "non-object project block", { user: { pairs }, project: true }, "Invalid dilemma from project settings: expected an object", ], [ "merged enabled string", { user: { pairs }, project: { enabled: "false" } }, "Invalid dilemma from merged project and user settings: enabled must be a boolean", ], [ "user thinkingLevel", { user: { pairs, thinkingLevel: "extreme" } }, "Invalid dilemma from user settings: thinkingLevel must be one of off, minimal, low, medium, high, xhigh, max", ], [ "user pair shape", { user: { pairs: [{ executor: "openai/gpt-4o" }] } }, "Invalid dilemma from user settings: pairs must be an array of { executor, advisor } strings", ], ...[0, -1, 1.5, "5", Number.MAX_SAFE_INTEGER + 1].map( (devilThreshold): [string, Case, string] => [ `invalid Devil threshold ${devilThreshold}`, { user: { pairs, devilThreshold } }, "Invalid dilemma from user settings: devilThreshold must be a positive safe integer", ], ), [ "user subagentExtensions", { user: { pairs, subagentExtensions: "web" } }, 'Invalid dilemma from user settings: subagentExtensions must be "all", "none", or a Pi source array', ], ] satisfies [string, Case, string][])( "rejects %s without falling through", async (_name, input, message) => { const { active, warnings } = await start(input); expect(active).toBe(false); expect(warnings).toEqual([`${message}; Angel unconfigured`]); }, ); }); describe("dilemma sub-agent extensions", () => { it("defaults sub-agent extension loading to none", () => { expect(__test.resolveSubagentExtensions(undefined)).toBeUndefined(); }); it("preserves unique Pi sources without resolving sibling paths", () => { expect( __test.resolveSubagentExtensions([ " ./custom-extension.ts ", "./custom-extension.ts", ]), ).toEqual(["./custom-extension.ts"]); }); it("preserves the all selection", () => { expect(__test.resolveSubagentExtensions("all")).toBe("all"); }); });