import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { AcpRegistryOperationError, ProviderInstanceId, type ServerProvider, } from "@t3tools/contracts"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers"; import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost"; import { toCustomModelSetting } from "@t3tools/shared/model"; import { AcpRegistrySettings } from "../settings.ts"; import { describe, expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; import * as Stream from "effect/Stream"; import * as AcpRegistryRuntimeCoordinator from "./AcpRegistryRuntimeCoordinator.ts"; import * as AcpRegistrySupport from "./AcpRegistrySupport.ts"; import { AcpRegistryDriver, applyAcpRegistryAvailableCommands, applyAcpRegistryLiveConfiguration, buildCheckedAcpRegistrySnapshot, checkAcpRegistryProviderReadiness, checkAcpRegistryProviderStatus, isAcpRegistryCacheValid, updateAcpRegistryEnrichmentCache, } from "./driver.ts"; import { acpRegistryAdvertisesModelCatalog, acpRegistryProbeResult, normalizeAcpRegistryLiveConfiguration, } from "./probe.ts"; const decodeSettings = Schema.decodeSync(AcpRegistrySettings); const identity = { instanceId: ProviderInstanceId.make("acpRegistry_test"), displayName: "Test ACP", accentColor: undefined, continuationKey: "acpRegistry:instance:acpRegistry_test", }; const noSessionManagement = { canList: false, canLoad: false, canResume: false, canLogout: false, canDelete: false, canConfigureProviders: false, } as const; function catalogWithInspection( inspection: AcpRegistrySupport.AcpRegistryInspection, ): AcpRegistrySupport.AcpRegistryCatalog["Service"] { return { search: () => Effect.die("unused search"), prepare: () => Effect.die("unused prepare"), inspect: () => Effect.succeed(inspection), resolve: () => Effect.die("unused resolve"), uninstallManagedBinary: () => Effect.die("unused uninstall"), }; } function providerWithModels( models: ReadonlyArray<{ readonly id: string; readonly name: string; readonly description: string | null; }>, currentModelId: string | null, ): ReturnType { return buildCheckedAcpRegistrySnapshot({ ...identity, settings: decodeSettings({ agentId: "test-agent" }), checkedAt: "2026-08-13T10:00:00.000Z", inspection: { status: "ready", agentId: "test-agent", version: "1.0.0", distribution: "npx", }, probe: { probe: { instanceId: identity.instanceId, ready: true, icon: null, authMethods: [], models, currentModelId, configOptions: [], sessionManagement: noSessionManagement, }, slashCommands: [], skills: [], }, }); } describe("acpRegistrySnapshotReadiness", () => { it("keeps the custom model display name and options saved by the Settings editor", () => { const fastMode = { id: "fast", label: "Fast mode", type: "boolean" } as const; const snapshot = buildCheckedAcpRegistrySnapshot({ ...identity, settings: decodeSettings({ agentId: "test-agent", customModels: [ toCustomModelSetting({ slug: "gateway/my-model", name: "My model", capabilities: { optionDescriptors: [fastMode] }, }), ], }), checkedAt: "2026-08-13T10:00:00.000Z", inspection: { status: "ready", agentId: "test-agent", version: "1.0.0", distribution: "npx" }, }); const model = snapshot.models.find((entry) => entry.slug === "gateway/my-model"); expect(model?.name).toBe("My model"); expect(model?.capabilities?.optionDescriptors).toEqual([fastMode]); }); it("shows the version reported by a local ACP agent", () => { const probe = acpRegistryProbeResult(identity.instanceId, { sessionId: "test-session", initializeResult: { protocolVersion: 1, agentInfo: { name: "Example agent", version: "1.5.9" }, }, sessionSetupResult: {}, modelConfigId: undefined, }); const snapshot = buildCheckedAcpRegistrySnapshot({ ...identity, settings: decodeSettings({ source: "local", commandPath: "example" }), checkedAt: "2026-08-13T10:00:00.000Z", inspection: { status: "ready", agentId: "example", version: null, distribution: "local" }, probe: { probe, slashCommands: [], skills: [] }, }); expect(snapshot.version).toBe("1.5.9"); const withoutVersion = acpRegistryProbeResult(identity.instanceId, { sessionId: "test-session", initializeResult: { protocolVersion: 1 }, sessionSetupResult: {}, modelConfigId: undefined, }); expect( buildCheckedAcpRegistrySnapshot({ ...identity, settings: decodeSettings({ source: "local", commandPath: "example" }), checkedAt: "2026-08-13T10:00:00.000Z", inspection: { status: "ready", agentId: "example", version: null, distribution: "local" }, probe: { probe: withoutVersion, slashCommands: [], skills: [] }, }).version, ).toBeNull(); }); it("treats a live empty command advertisement as an authoritative replacement", () => { const provider = buildCheckedAcpRegistrySnapshot({ ...identity, settings: decodeSettings({ agentId: "test-agent" }), checkedAt: "2026-08-13T10:00:00.000Z", inspection: { status: "ready", agentId: "test-agent", version: "1.0.0", distribution: "npx", documentationUrl: "https://example.test/agent/setup", }, probe: { probe: { instanceId: identity.instanceId, ready: true, icon: null, authMethods: [], models: [], currentModelId: null, configOptions: [], sessionManagement: noSessionManagement, }, slashCommands: [{ name: "stale" }], skills: [{ name: "stale-skill", path: "stale", enabled: true }], }, }); const replaced = applyAcpRegistryAvailableCommands( provider, Option.some({ slashCommands: [], skills: [] }), ); expect(replaced.slashCommands).toEqual([]); expect(replaced.skills).toEqual([]); expect(applyAcpRegistryAvailableCommands(provider, Option.none()).slashCommands).toEqual([ { name: "stale" }, ]); expect(provider.iconUrl).toBe( "https://cdn.agentclientprotocol.com/registry/v1/latest/test-agent.svg", ); expect(provider.setup).toMatchObject({ canAuthenticate: false, documentationUrl: "https://example.test/agent/setup", }); }); it("keeps authentication unknown when an agent permits a discovery session before sign-in", () => { const snapshot = buildCheckedAcpRegistrySnapshot({ ...identity, settings: decodeSettings({ agentId: "test-agent" }), checkedAt: "2026-08-13T10:00:00.000Z", inspection: { status: "ready", agentId: "test-agent", version: "1.0.0", distribution: "binary", }, probe: { probe: { instanceId: identity.instanceId, ready: true, icon: null, authMethods: [{ id: "browser", name: "Browser", description: null, type: "agent" }], models: [], currentModelId: null, configOptions: [], sessionManagement: noSessionManagement, }, slashCommands: [], skills: [], }, }); expect(snapshot.auth.status).toBe("unknown"); expect(snapshot.setup?.canAuthenticate).toBe(true); expect( applyAcpRegistryLiveConfiguration( snapshot, { models: [], currentModelId: null, configOptions: [], modelsFromModelSelect: false }, [], ).auth.status, ).toBe("unknown"); }); it("overlays live configuration without dropping probe-owned session capabilities", () => { const provider = buildCheckedAcpRegistrySnapshot({ ...identity, settings: decodeSettings({ agentId: "test-agent" }), checkedAt: "2026-08-13T10:00:00.000Z", inspection: { status: "ready", agentId: "test-agent", version: "1.0.0", distribution: "npx", }, probe: { probe: { instanceId: identity.instanceId, ready: true, icon: null, authMethods: [], models: [{ id: "probe-model", name: "Probe model", description: null }], currentModelId: "probe-model", configOptions: [], sessionManagement: { canList: true, canLoad: true, canResume: true, canLogout: true, canDelete: true, canConfigureProviders: true, }, }, slashCommands: [], skills: [], }, }); expect( applyAcpRegistryLiveConfiguration( provider, { models: [{ id: "live-model", name: "Live model", description: null }], currentModelId: "live-model", configOptions: [], modelsFromModelSelect: false, }, [], ), ).toMatchObject({ auth: { status: "unknown", canLogout: true }, nativeSessions: { canList: true, canLoad: true, canResume: true }, models: [{ slug: "live-model", isDefault: true }], }); expect( applyAcpRegistryLiveConfiguration( provider, { models: [], currentModelId: null, configOptions: [], modelsFromModelSelect: false }, [], ).models, ).toEqual(provider.models); const liveOptions = [ { id: "effort", label: "Effort", type: "select" as const, options: [{ id: "high", label: "High" }], currentValue: "high", }, ]; expect( applyAcpRegistryLiveConfiguration( provider, { models: [], currentModelId: null, configOptions: liveOptions, modelsFromModelSelect: false, }, [], ).models, ).toMatchObject([ { slug: "probe-model", isDefault: true, capabilities: { optionDescriptors: [{ id: "effort", currentValue: "high" }] }, }, ]); // Session configOptions describe the current model only. const sharedMode = { id: "mode", label: "Mode", type: "select" as const, options: [{ id: "ask", label: "Ask" }], currentValue: "ask", }; const twoModelList = [ { id: "glm", name: "GLM", description: null }, { id: "opus", name: "Opus", description: null }, ]; const afterGlm = applyAcpRegistryLiveConfiguration( provider, { models: twoModelList, currentModelId: "glm", configOptions: [...liveOptions, sharedMode], modelOptionIds: ["effort"], }, [], ); const twoModels = afterGlm.models; expect(twoModels.find((model) => model.slug === "glm")).toMatchObject({ capabilities: { optionDescriptors: [{ id: "effort" }, { id: "mode" }] }, }); expect( twoModels.find((model) => model.slug === "opus")?.capabilities?.optionDescriptors ?? [], ).toEqual([sharedMode]); // A later session on another model keeps the options learned for this one. const afterOpus = applyAcpRegistryLiveConfiguration( afterGlm, { models: twoModelList, currentModelId: "opus", configOptions: [{ ...liveOptions[0]!, id: "thinking" }, sharedMode], modelOptionIds: ["thinking"], }, [], ).models; expect(afterOpus.find((model) => model.slug === "glm")).toMatchObject({ capabilities: { optionDescriptors: [{ id: "mode" }, { id: "effort" }] }, }); expect(afterOpus.find((model) => model.slug === "opus")).toMatchObject({ capabilities: { optionDescriptors: [{ id: "thinking" }, { id: "mode" }] }, }); // Other models take the levels they advertise in `_meta`. const effort = { id: "effort", name: "Effort", category: "thought_level", type: "select" as const, currentValue: "high", options: [{ value: "high", name: "High" }], }; const advertised = applyAcpRegistryLiveConfiguration( provider, normalizeAcpRegistryLiveConfiguration([effort], undefined, { currentModelId: "opus", availableModels: [ { modelId: "opus", name: "Opus" }, { modelId: "haiku", name: "Haiku", _meta: { reasoningEfforts: [{ value: "low", label: "Low" }], reasoningEffort: "low" }, }, { modelId: "glm", name: "GLM", _meta: { supportsReasoningEffort: false } }, ], }), [], ).models.map((model) => model.capabilities?.optionDescriptors ?? []); expect(advertised).toEqual([ [{ ...liveOptions[0]!, role: "reasoning" }], [ { ...liveOptions[0]!, role: "reasoning", options: [{ id: "low", label: "Low" }], currentValue: "low", }, ], [], ]); }); it("keeps each Mistral Vibe model's own thinking levels", () => { const provider = buildCheckedAcpRegistrySnapshot({ ...identity, settings: decodeSettings({ agentId: "mistral-vibe" }), checkedAt: "2026-11-18T10:00:00.000Z", inspection: { status: "ready", agentId: "mistral-vibe", version: "3.1.1", distribution: "binary", }, probe: { probe: { instanceId: identity.instanceId, ready: true, icon: null, authMethods: [], models: [], currentModelId: null, configOptions: [], sessionManagement: noSessionManagement, }, slashCommands: [], skills: [], }, }); // Mistral Vibe 3.1.1 sends its levels under category "thinking", for the session's model only. const vibeSession = (model: string, levels: ReadonlyArray) => normalizeAcpRegistryLiveConfiguration([ { id: "model", name: "Model", category: "model", type: "select", currentValue: model, options: ["mistral-large-4-0-3", "mistral-medium-3.5"].map((value) => ({ value, name: value, })), }, { id: "thinking", name: "Thinking", category: "thinking", type: "select", currentValue: "high", options: levels.map((value) => ({ value, name: value })), }, ]); const thinking = (levels: ReadonlyArray) => ({ id: "thinking", label: "Thinking", type: "select", options: levels.map((id) => ({ id, label: id })), currentValue: "high", role: "reasoning", }); const descriptors = (snapshot: typeof provider) => snapshot.models.map((model) => [model.slug, model.capabilities?.optionDescriptors ?? []]); const onLarge = applyAcpRegistryLiveConfiguration( provider, vibeSession("mistral-large-4-0-3", ["low", "medium", "high"]), [], ); expect(descriptors(onLarge)).toEqual([ ["mistral-large-4-0-3", [thinking(["low", "medium", "high"])]], ["mistral-medium-3.5", []], ]); const onMedium = applyAcpRegistryLiveConfiguration( onLarge, vibeSession("mistral-medium-3.5", ["off", "high"]), [], ); expect(descriptors(onMedium)).toEqual([ ["mistral-large-4-0-3", [thinking(["low", "medium", "high"])]], ["mistral-medium-3.5", [thinking(["off", "high"])]], ]); }); it("projects discovery without claiming authentication when no login methods are advertised", () => { const snapshot = buildCheckedAcpRegistrySnapshot({ ...identity, settings: decodeSettings({ agentId: "test-agent", customModels: [" custom-model ", "gpt-discovered"], }), checkedAt: "2026-08-13T10:00:00.000Z", inspection: { status: "ready", agentId: "test-agent", version: "1.0.0", distribution: "npx", }, probe: { probe: { instanceId: identity.instanceId, ready: true, icon: null, authMethods: [], models: [{ id: "gpt-discovered", name: "GPT Discovered", description: null }], currentModelId: "gpt-discovered", configOptions: [], sessionManagement: noSessionManagement, }, slashCommands: [{ name: "plan", description: "Create a plan", input: { hint: "topic" } }], skills: [{ name: "workspace-skill", path: "acp://skill/workspace-skill", enabled: true }], }, }); expect(snapshot.auth).toEqual({ status: "unknown", canLogout: false }); expect(snapshot.supportsTextGeneration).toBe(false); expect( snapshot.models.map(({ slug, name, isCustom, isDefault }) => ({ slug, name, isCustom, isDefault, })), ).toEqual([ { slug: "gpt-discovered", name: "GPT Discovered", isCustom: false, isDefault: true, }, { slug: "custom-model", name: "custom-model", isCustom: true, isDefault: undefined, }, ]); expect(snapshot.slashCommands).toEqual([ { name: "plan", description: "Create a plan", input: { hint: "topic" } }, ]); expect(snapshot.skills).toEqual([ { name: "workspace-skill", path: "acp://skill/workspace-skill", enabled: true }, ]); }); it("keeps a default model only when the agent advertises none", () => { const snapshot = buildCheckedAcpRegistrySnapshot({ ...identity, settings: decodeSettings({ agentId: "test-agent" }), checkedAt: "2026-08-13T10:00:00.000Z", inspection: { status: "ready", agentId: "test-agent", version: "1.0.0", distribution: "uvx", }, probe: { probe: { instanceId: identity.instanceId, ready: true, icon: null, authMethods: [], models: [], currentModelId: null, configOptions: [], sessionManagement: noSessionManagement, }, slashCommands: [], skills: [], }, }); expect(snapshot.models.map((model) => model.slug)).toEqual(["default"]); expect(snapshot.models[0]?.isDefault).toBe(true); }); it.each(["registry", "local"] as const)( "reports failed authentication after successful %s inspection", (source) => { const snapshot = buildCheckedAcpRegistrySnapshot({ ...identity, settings: decodeSettings({ source, ...(source === "local" ? { commandPath: "test-agent" } : { agentId: "test-agent" }), authMethodId: "grok-login", }), checkedAt: "2026-08-13T10:00:00.000Z", inspection: { status: "ready", agentId: "test-agent", version: source === "local" ? null : "1.0.0", distribution: source === "local" ? "local" : "binary", }, probeError: new AcpRegistryOperationError({ reason: "authentication_failed", message: "Login required.", authMethods: [ { id: "api-key", name: "API key", description: null, type: "env_var", }, { id: "grok-login", name: "Log in with Grok", description: null, type: "agent", }, ], }), }); expect(snapshot).toMatchObject({ installed: true, version: source === "local" ? null : "1.0.0", status: "warning", setup: { canAuthenticate: true }, auth: { status: "unauthenticated", type: "agent", label: "Log in with Grok", }, message: 'Sign in in provider settings using "Log in with Grok".', }); }, ); it.effect("runs the disposable probe only after local inspection is ready", () => Effect.gen(function* () { const settings = decodeSettings({ agentId: "test-agent" }); const environment = { PATH: "/provider/bin" }; let receivedEnvironment: NodeJS.ProcessEnv | undefined; const snapshot = yield* checkAcpRegistryProviderStatus( { ...identity, settings, cwd: "/workspace", environment, }, (input) => Effect.sync(() => { receivedEnvironment = input.environment; return { probe: { instanceId: identity.instanceId, ready: true as const, icon: null, authMethods: [], models: [{ id: "agent-model", name: "Agent Model", description: null }], currentModelId: "agent-model", configOptions: [], sessionManagement: noSessionManagement, }, slashCommands: [{ name: "review" }], skills: [], }; }), ).pipe( Effect.provideService( AcpRegistrySupport.AcpRegistryCatalog, catalogWithInspection({ status: "ready", agentId: "test-agent", version: "1.0.0", distribution: "npx", }), ), ); expect(receivedEnvironment).toEqual({ ...environment, MISE_OFFLINE: "1" }); expect(snapshot).toMatchObject({ auth: { status: "unknown" }, models: [{ slug: "agent-model" }], slashCommands: [{ name: "review" }], skills: [], }); }), ); it.effect("publishes concrete local readiness before background ACP discovery", () => Effect.gen(function* () { const snapshot = yield* checkAcpRegistryProviderReadiness({ ...identity, settings: decodeSettings({ agentId: "test-agent" }), environment: { PATH: "/provider/bin" }, }).pipe( Effect.provideService( AcpRegistrySupport.AcpRegistryCatalog, catalogWithInspection({ status: "ready", agentId: "test-agent", version: "1.0.0", distribution: "npx", }), ), ); expect(snapshot).toMatchObject({ installed: true, status: "ready", version: "1.0.0", auth: { status: "unknown" }, message: "Checking ACP authentication, models, and commands in the background...", }); }), ); it("keeps a richer probed catalog when a live session reports one provisional model", () => { const provider = providerWithModels( [ { id: "auto", name: "Auto", description: null }, { id: "qmodel", name: "Qwen", description: null }, { id: "kmodel", name: "Kimi", description: null }, ], "auto", ); const fallbackSelected = applyAcpRegistryLiveConfiguration( provider, { models: [{ id: "smodel", name: "Default", description: null }], currentModelId: "smodel", configOptions: [], modelsFromModelSelect: false, }, [], ); expect(fallbackSelected.models.map((m) => m.slug)).toEqual(["auto", "qmodel", "kmodel"]); expect(fallbackSelected.models.find((m) => m.slug === "auto")?.isDefault).toBe(true); const liveSelected = applyAcpRegistryLiveConfiguration( provider, { models: [{ id: "auto", name: "Auto", description: null }], currentModelId: "qmodel", configOptions: [], modelsFromModelSelect: false, }, [], ); expect(liveSelected.models.map((m) => m.slug)).toEqual(["auto", "qmodel", "kmodel"]); expect(liveSelected.models.find((m) => m.slug === "qmodel")?.isDefault).toBe(true); expect(liveSelected.models.find((m) => m.slug === "auto")?.isDefault).not.toBe(true); }); it("honors an explicit single-model select even when it is absent from the probe catalog", () => { const provider = providerWithModels( [ { id: "auto", name: "Auto", description: null }, { id: "qmodel", name: "Qwen", description: null }, { id: "kmodel", name: "Kimi", description: null }, ], "auto", ); const entitled = applyAcpRegistryLiveConfiguration( provider, { models: [{ id: "gmodel", name: "GPT-Pro", description: null }], currentModelId: "gmodel", configOptions: [], modelsFromModelSelect: true, }, [], ); expect(entitled.models.map((m) => m.slug)).toEqual(["gmodel"]); expect(entitled.models.find((m) => m.slug === "gmodel")?.isDefault).toBe(true); const narrowed = applyAcpRegistryLiveConfiguration( provider, { models: [{ id: "qmodel", name: "Qwen", description: null }], currentModelId: "qmodel", configOptions: [], modelsFromModelSelect: true, }, [], ); expect(narrowed.models.map((m) => m.slug)).toEqual(["qmodel"]); expect(narrowed.models.find((m) => m.slug === "qmodel")?.isDefault).toBe(true); }); }); describe("acpRegistryEnrichmentCache", () => { it("reuses a local probe until expiry even when inspection has no version", () => { const entry = { provider: providerWithModels([], null), expiresAt: 1_000 }; expect(isAcpRegistryCacheValid(entry, null, true, 999)).toBe(true); expect(isAcpRegistryCacheValid(entry, null, true, 1_000)).toBe(false); expect(isAcpRegistryCacheValid(entry, "2.0.0", false, 999)).toBe(false); expect(isAcpRegistryCacheValid(entry, "1.0.0", false, 999)).toBe(true); }); const settledConfiguration = { models: [ { id: "auto", name: "Auto", description: null }, { id: "qmodel", name: "Qwen", description: null }, { id: "kmodel", name: "Kimi", description: null }, ], currentModelId: "auto", configOptions: [], modelsFromModelSelect: true, }; it("does not renew the cache deadline when live configuration is replayed unchanged", () => { const provider = providerWithModels(settledConfiguration.models, "auto"); const cache = { generation: 1, entry: { provider, expiresAt: 1_000 } }; const replayed = updateAcpRegistryEnrichmentCache({ cache, configuration: settledConfiguration, customModels: [], now: 2_000, }); expect(replayed).toBe(cache); expect(replayed.entry?.expiresAt).toBe(1_000); }); it("updates the cache and promotes the expiry when live configuration discovers a catalog", () => { const provider = providerWithModels( [{ id: "smodel", name: "Default", description: null }], "smodel", ); const cache = { generation: 1, entry: { provider, expiresAt: 1_000 } }; const updated = updateAcpRegistryEnrichmentCache({ cache, configuration: settledConfiguration, customModels: [], now: 5_000, }); expect(updated.entry?.provider.models.map((m) => m.slug)).toEqual(["auto", "qmodel", "kmodel"]); expect((updated.entry?.expiresAt ?? 0) - 5_000).toBe(15 * 60 * 1_000); expect(updated.generation).toBe(2); }); it("advances the cache epoch when a live update arrives before any probe lands", () => { const cache = { generation: 4, entry: null }; const updated = updateAcpRegistryEnrichmentCache({ cache, configuration: settledConfiguration, customModels: [], now: 5_000, }); expect(updated.generation).toBe(5); expect(updated.entry).toBeNull(); }); }); describe("acpRegistryAdvertisesModelCatalog", () => { it("counts choices inside option groups and available model state", () => { expect( acpRegistryAdvertisesModelCatalog([ { id: "model", name: "Model", type: "select" as const, category: "model" as const, currentValue: "auto", options: [ { groupId: "qwen", name: "Qwen", options: [ { value: "auto", name: "Auto" }, { value: "qmodel", name: "Qwen3.8-Max" }, ], }, ], }, ]), ).toBe(true); expect( acpRegistryAdvertisesModelCatalog([ { id: "model", name: "Model", type: "select" as const, category: "model" as const, currentValue: "smodel", options: [{ value: "smodel", name: "Sonus" }], }, ]), ).toBe(false); expect(acpRegistryAdvertisesModelCatalog(undefined)).toBe(false); expect( acpRegistryAdvertisesModelCatalog(undefined, { availableModels: [ { modelId: "auto", name: "Auto", description: null }, { modelId: "qmodel", name: "Qwen", description: null }, ], currentModelId: "auto", }), ).toBe(true); }); }); const fakeAgentSource = ` import { readFileSync } from "node:fs"; import { createInterface } from "node:readline"; const send = (message) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", ...message }) + "\\n"); createInterface({ input: process.stdin }).on("line", (line) => { const { id, method } = JSON.parse(line); if (id === undefined) return; if (method === "initialize") { send({ id, result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [], agentInfo: { name: "fake", version: "1.5.9" } }, }); } else if (method === "session/new") { const modelIds = JSON.parse(readFileSync(process.argv[2], "utf8")); send({ id, result: { sessionId: "probe-session", models: { currentModelId: modelIds[0], availableModels: modelIds.map((modelId) => ({ modelId, name: modelId })), }, }, }); send({ method: "session/update", params: { sessionId: "probe-session", update: { sessionUpdate: "available_commands_update", availableCommands: [] }, }, }); } else { send({ id, result: {} }); } }); `; const driverLayer = TestProviderHost.layer({ runBackgroundWork: false }).pipe( Layer.provideMerge(AcpRegistryRuntimeCoordinator.layer), Layer.provideMerge(IdAllocator.layer), Layer.provideMerge(McpProviderSessions.layer), Layer.provideMerge( Layer.succeed( ProviderEventLoggers.ProviderEventLoggers, ProviderEventLoggers.NoOpProviderEventLoggers, ), ), Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), ); it.layer(driverLayer, { excludeTestServices: true })("AcpRegistryDriver", (it) => { it.effect("creates without inspecting the agent, which may fetch the registry", () => Effect.gen(function* () { const instance = yield* AcpRegistryDriver.create({ instanceId: identity.instanceId, displayName: identity.displayName, enabled: true, environment: [], config: decodeSettings({ agentId: "test-agent" }), }).pipe( Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, { ...catalogWithInspection({ status: "unconfigured" }), inspect: () => Effect.die("inspected during creation"), }), ); expect(yield* instance.snapshot.getSnapshot).toMatchObject({ installed: false, message: "Checking ACP Registry agent readiness...", }); }), ); it.effect("drops cached and session-reported models when its caches are invalidated", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "acp-driver-" }); const agentPath = path.join(directory, "agent.mjs"); const modelsPath = path.join(directory, "models.json"); yield* fileSystem.writeFileString(agentPath, fakeAgentSource); yield* fileSystem.writeFileString(modelsPath, JSON.stringify(["old-model", "other-model"])); const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator; const instance = yield* AcpRegistryDriver.create({ instanceId: identity.instanceId, displayName: identity.displayName, enabled: true, environment: [], config: decodeSettings({ agentId: "test-agent" }), }).pipe( Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, { ...catalogWithInspection({ status: "ready", agentId: "test-agent", version: "1.0.0", distribution: "npx", }), resolve: () => Effect.succeed({ distribution: "npx" as const, spawn: { command: process.execPath, args: [agentPath, modelsPath] }, }), }), ); const modelSlugs = instance.snapshot.streamChanges.pipe( Stream.map((snapshot) => snapshot.models.map((model) => model.slug)), ); const waitForModels = (expected: ReadonlyArray) => modelSlugs.pipe( Stream.filter((slugs) => slugs.join() === expected.join()), Stream.runHead, ); const probed = yield* waitForModels(["old-model", "other-model"]).pipe( Effect.forkScoped({ startImmediately: true }), ); yield* instance.snapshot.refresh; yield* Fiber.join(probed); yield* coordinator.publishLiveConfiguration( identity.instanceId, normalizeAcpRegistryLiveConfiguration([], undefined, { currentModelId: "old-model", availableModels: [ { modelId: "old-model", name: "old-model" }, { modelId: "session-model", name: "session-model" }, ], }), ); yield* fileSystem.writeFileString(modelsPath, JSON.stringify(["old-model", "new-model"])); const refreshed = yield* waitForModels(["old-model", "new-model"]).pipe( Effect.forkScoped({ startImmediately: true }), ); yield* instance.invalidateCaches ?? Effect.void; yield* instance.snapshot.refresh; expect(Option.isSome(yield* Fiber.join(refreshed))).toBe(true); }), ); it.effect("keeps a local agent's reported version through the next readiness check", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "acp-driver-" }); const agentPath = path.join(directory, "agent.mjs"); const modelsPath = path.join(directory, "models.json"); yield* fileSystem.writeFileString(agentPath, fakeAgentSource); yield* fileSystem.writeFileString(modelsPath, JSON.stringify(["model-a"])); const instance = yield* AcpRegistryDriver.create({ instanceId: identity.instanceId, displayName: identity.displayName, enabled: true, environment: [], config: decodeSettings({ source: "local", commandPath: "agent" }), }).pipe( Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, { ...catalogWithInspection({ status: "ready", agentId: "agent", version: null, distribution: "local", }), resolve: () => Effect.succeed({ distribution: "local" as const, spawn: { command: process.execPath, args: [agentPath, modelsPath] }, }), }), ); const probed = yield* instance.snapshot.streamChanges.pipe( Stream.filter((snapshot) => snapshot.version === "1.5.9"), Stream.runHead, Effect.forkScoped({ startImmediately: true }), ); yield* instance.snapshot.refresh; yield* Fiber.join(probed); const nextPublished = yield* instance.snapshot.streamChanges.pipe( Stream.runHead, Effect.forkScoped({ startImmediately: true }), ); yield* instance.invalidateCaches ?? Effect.void; yield* instance.snapshot.refresh; const next = yield* Fiber.join(nextPublished); expect(Option.map(next, (snapshot) => snapshot.version)).toEqual(Option.some("1.5.9")); }), ); it.effect("keeps options learned for each model across a refresh", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "acp-driver-" }); const agentPath = path.join(directory, "agent.mjs"); const modelsPath = path.join(directory, "models.json"); yield* fileSystem.writeFileString(agentPath, fakeAgentSource); yield* fileSystem.writeFileString(modelsPath, JSON.stringify(["model-a", "model-b"])); const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator; const instance = yield* AcpRegistryDriver.create({ instanceId: identity.instanceId, displayName: identity.displayName, enabled: true, environment: [], config: decodeSettings({ agentId: "test-agent" }), }).pipe( Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, { ...catalogWithInspection({ status: "ready", agentId: "test-agent", version: "1.0.0", distribution: "npx", }), resolve: () => Effect.succeed({ distribution: "npx" as const, spawn: { command: process.execPath, args: [agentPath, modelsPath] }, }), }), ); const effortBySlug = (snapshot: ServerProvider) => snapshot.models .map( (model) => `${model.slug}:${(model.capabilities?.optionDescriptors ?? []).map((option) => option.id).join("+")}`, ) .join(); const waitFor = (expected: string) => instance.snapshot.streamChanges.pipe( Stream.filter((snapshot) => effortBySlug(snapshot) === expected), Stream.runHead, ); const session = (currentModelId: string) => normalizeAcpRegistryLiveConfiguration( [ { id: "effort", name: "Effort", category: "thought_level", type: "select", currentValue: "high", options: [ { value: "low", name: "Low" }, { value: "high", name: "High" }, ], }, ], undefined, { currentModelId, availableModels: [ { modelId: "model-a", name: "model-a" }, { modelId: "model-b", name: "model-b" }, ], }, ); const probed = yield* waitFor("model-a:,model-b:").pipe( Effect.forkScoped({ startImmediately: true }), ); yield* instance.snapshot.refresh; yield* Fiber.join(probed); const learned = yield* waitFor("model-a:effort,model-b:effort").pipe( Effect.forkScoped({ startImmediately: true }), ); yield* coordinator.publishLiveConfiguration(identity.instanceId, session("model-b")); yield* coordinator.publishLiveConfiguration(identity.instanceId, session("model-a")); yield* Fiber.join(learned); yield* instance.snapshot.refresh; expect(effortBySlug(yield* instance.snapshot.getSnapshot)).toBe( "model-a:effort,model-b:effort", ); }), ); });