diff --git a/packages/provider-acp-registry/src/server/driver.test.ts b/packages/provider-acp-registry/src/server/driver.test.ts index 684eb21d7456b..564486b9d8269 100644 --- a/packages/provider-acp-registry/src/server/driver.test.ts +++ b/packages/provider-acp-registry/src/server/driver.test.ts @@ -2,6 +2,7 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { AcpRegistryOperationError, + DEFAULT_SERVER_SETTINGS, ProviderInstanceId, type ServerProvider, } from "@t3tools/contracts"; @@ -19,6 +20,7 @@ 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 type * as Scope from "effect/Scope"; import * as Stream from "effect/Stream"; import * as AcpRegistryRuntimeCoordinator from "./AcpRegistryRuntimeCoordinator.ts"; @@ -949,6 +951,61 @@ createInterface({ input: process.stdin }).on("line", (line) => { }); `; +// Reports `effort` levels for its current model only and logs each model switch. +const perModelEffortAgentSource = ` +import { appendFileSync, readFileSync } from "node:fs"; +import { createInterface } from "node:readline"; +const [logPath, modelsPath] = process.argv.slice(2); +const levels = JSON.parse(readFileSync(modelsPath, "utf8")); +const modelIds = Object.keys(levels); +let current = modelIds[0]; +const send = (message) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", ...message }) + "\\n"); +const configOptions = () => [ + { + id: "model", + name: "Model", + category: "model", + type: "select", + currentValue: current, + options: modelIds.map((value) => ({ value, name: value })), + }, + ...(levels[current] === null + ? [] + : [ + { + id: "effort", + name: "Effort", + category: "thought_level", + type: "select", + currentValue: levels[current][0], + options: levels[current].map((value) => ({ value, name: value })), + }, + ]), +]; +createInterface({ input: process.stdin }).on("line", (line) => { + const { id, method, params } = JSON.parse(line); + if (id === undefined) return; + if (method === "initialize") { + send({ id, result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [] } }); + } else if (method === "session/new") { + send({ id, result: { sessionId: "probe-session", configOptions: configOptions() } }); + send({ + method: "session/update", + params: { + sessionId: "probe-session", + update: { sessionUpdate: "available_commands_update", availableCommands: [] }, + }, + }); + } else if (method === "session/set_config_option" && params.configId === "model") { + appendFileSync(logPath, params.value + "\\n"); + current = params.value; + send({ id, result: { configOptions: configOptions() } }); + } else { + send({ id, result: {} }); + } +}); +`; + const driverLayer = TestProviderHost.layer({ runBackgroundWork: false }).pipe( Layer.provideMerge(AcpRegistryRuntimeCoordinator.layer), Layer.provideMerge(IdAllocator.layer), @@ -1188,4 +1245,224 @@ it.layer(driverLayer, { excludeTestServices: true })("AcpRegistryDriver", (it) = ); }), ); + + const makePerModelEffortAgent = Effect.fn("makePerModelEffortAgent")(function* ( + levels: Readonly | null>>, + ) { + 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 logPath = path.join(directory, "switches.log"); + const modelsPath = path.join(directory, "models.json"); + yield* fileSystem.writeFileString(agentPath, perModelEffortAgentSource); + yield* fileSystem.writeFileString(logPath, ""); + yield* fileSystem.writeFileString(modelsPath, JSON.stringify(levels)); + return { + spawn: { command: process.execPath, args: [agentPath, logPath, modelsPath] }, + modelSwitches: fileSystem + .readFileString(logPath) + .pipe(Effect.map((log) => log.split("\n").filter((line) => line.length > 0))), + }; + }); + const createRegistryInstance = ( + instanceId: ProviderInstanceId, + spawn: { readonly command: string; readonly args: ReadonlyArray }, + version: string, + ) => + AcpRegistryDriver.create({ + instanceId, + displayName: identity.displayName, + enabled: true, + environment: [], + config: decodeSettings({ agentId: "test-agent" }), + }).pipe( + Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, { + ...catalogWithInspection({ + status: "ready", + agentId: "test-agent", + version, + distribution: "npx", + }), + resolve: () => Effect.succeed({ distribution: "npx" as const, spawn }), + }), + ); + const effortLevels = (snapshot: ServerProvider) => + snapshot.models + .map((model) => { + const effort = model.capabilities?.optionDescriptors?.find( + (option) => option.id === "effort", + ); + const levels = effort?.type === "select" ? effort.options.map((option) => option.id) : []; + return `${model.slug}:${levels.join("+")}`; + }) + .join(); + const refreshUntilEffortLevels = ( + instance: Effect.Success>, + expected: string, + ) => + Effect.gen(function* () { + const reached = yield* instance.snapshot.streamChanges.pipe( + Stream.filter((snapshot) => effortLevels(snapshot) === expected), + Stream.runHead, + Effect.forkScoped({ startImmediately: true }), + ); + yield* instance.snapshot.refresh; + yield* Fiber.join(reached); + }); + + // A session's report: the model select plus the current model's own effort levels. + const liveSession = ( + models: ReadonlyArray, + model: string, + levels: ReadonlyArray, + ) => + normalizeAcpRegistryLiveConfiguration([ + { + id: "model", + name: "Model", + category: "model", + type: "select", + currentValue: model, + options: models.map((value) => ({ value, name: value })), + }, + { + id: "effort", + name: "Effort", + category: "thought_level", + type: "select", + currentValue: levels[0]!, + options: levels.map((value) => ({ value, name: value })), + }, + ]); + + it.effect("reads each visible model's own reasoning levels before its first session", () => + Effect.gen(function* () { + const instanceId = ProviderInstanceId.make("acpRegistry_discovery"); + yield* (yield* TestProviderHost.TestProviderHostSettings).set({ + ...DEFAULT_SERVER_SETTINGS, + favorites: [{ provider: instanceId, model: "fav" }], + providerModelPreferences: { [instanceId]: { hiddenModels: ["hidden"], modelOrder: [] } }, + }); + const agent = yield* makePerModelEffortAgent({ + default: ["medium", "high"], + plain: null, + other: ["low", "high"], + hidden: ["low"], + fav: ["none", "max"], + }); + const instance = yield* createRegistryInstance(instanceId, agent.spawn, "1.0.0"); + + yield* refreshUntilEffortLevels( + instance, + "default:medium+high,plain:,other:low+high,hidden:,fav:none+max", + ); + // Favorites first, hidden models skipped, then the agent's own model restored. + expect(yield* agent.modelSwitches).toEqual(["fav", "plain", "other", "default"]); + + // What a live session reports for a model outranks what discovery read for it. + const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator; + const models = ["default", "plain", "other", "hidden", "fav"]; + const live = yield* instance.snapshot.streamChanges.pipe( + Stream.filter( + (snapshot) => + effortLevels(snapshot) === "default:high,plain:,other:xhigh,hidden:,fav:none+max", + ), + Stream.runHead, + Effect.forkScoped({ startImmediately: true }), + ); + yield* coordinator.publishLiveConfiguration( + instanceId, + liveSession(models, "other", ["xhigh"]), + ); + yield* coordinator.publishLiveConfiguration( + instanceId, + liveSession(models, "default", ["high"]), + ); + expect(Option.isSome(yield* Fiber.join(live))).toBe(true); + }), + ); + + it.effect("reads every model of a catalog larger than one discovery batch in one refresh", () => + Effect.gen(function* () { + const instanceId = ProviderInstanceId.make("acpRegistry_large_catalog"); + const others = Array.from({ length: 30 }, (_, index) => `model-${index}`); + yield* (yield* TestProviderHost.TestProviderHostSettings).set({ + ...DEFAULT_SERVER_SETTINGS, + providerModelPreferences: { + [instanceId]: { hiddenModels: [], modelOrder: [...others, "opus"] }, + }, + }); + const agent = yield* makePerModelEffortAgent({ + sonnet: ["medium"], + ...Object.fromEntries(others.map((slug) => [slug, ["low"]])), + opus: ["high", "max"], + }); + const instance = yield* createRegistryInstance(instanceId, agent.spawn, "1.0.0"); + + yield* refreshUntilEffortLevels( + instance, + ["sonnet:medium", ...others.map((slug) => `${slug}:low`), "opus:high+max"].join(), + ); + expect(yield* agent.modelSwitches).toEqual([...others, "opus", "sonnet"]); + }), + ); + + it.effect("keeps discovered reasoning levels across restarts until the agent updates", () => + Effect.gen(function* () { + const instanceId = ProviderInstanceId.make("acpRegistry_persisted"); + const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator; + const agent = yield* makePerModelEffortAgent({ + default: ["medium", "high"], + other: ["low", "high"], + }); + const runInstance = ( + version: string, + expected: string, + then: ( + instance: Effect.Success>, + ) => Effect.Effect = () => Effect.void, + ) => + Effect.scoped( + createRegistryInstance(instanceId, agent.spawn, version).pipe( + Effect.tap((instance) => refreshUntilEffortLevels(instance, expected)), + Effect.flatMap(then), + ), + ); + + yield* runInstance("1.0.0", "default:medium+high,other:low+high"); + expect(yield* agent.modelSwitches).toEqual(["other", "default"]); + + // Reused after a restart; a live session then replaces the stored levels. + yield* runInstance("1.0.0", "default:medium+high,other:low+high", (instance) => + Effect.gen(function* () { + const replaced = yield* instance.snapshot.streamChanges.pipe( + Stream.filter((snapshot) => effortLevels(snapshot) === "default:high,other:xhigh"), + Stream.runHead, + Effect.forkScoped({ startImmediately: true }), + ); + const models = ["default", "other"]; + yield* coordinator.publishLiveConfiguration( + instanceId, + liveSession(models, "other", ["xhigh"]), + ); + yield* coordinator.publishLiveConfiguration( + instanceId, + liveSession(models, "default", ["high"]), + ); + yield* Fiber.join(replaced); + // The session ends with this instance; only the stored levels remain. + yield* coordinator.clearLiveConfiguration(instanceId); + }), + ); + expect(yield* agent.modelSwitches).toEqual(["other", "default"]); + + yield* runInstance("1.0.0", "default:medium+high,other:xhigh"); + expect(yield* agent.modelSwitches).toEqual(["other", "default"]); + + // An agent update makes every stored level stale. + yield* runInstance("2.0.0", "default:medium+high,other:low+high"); + expect(yield* agent.modelSwitches).toEqual(["other", "default", "other", "default"]); + }), + ); }); diff --git a/packages/provider-acp-registry/src/server/driver.ts b/packages/provider-acp-registry/src/server/driver.ts index 46d8ed3219aa4..33c5ab9a46663 100644 --- a/packages/provider-acp-registry/src/server/driver.ts +++ b/packages/provider-acp-registry/src/server/driver.ts @@ -53,6 +53,7 @@ import { type AcpRegistryConfigurationProbe, type AcpRegistryConfigurationProbeResult, type AcpRegistryLiveConfiguration, + type AcpRegistryModelDiscovery, deleteAcpRegistrySession, disableAcpRegistryProvider, listAcpRegistrySessions, @@ -65,6 +66,12 @@ import * as AcpRegistrySupport from "./AcpRegistrySupport.ts"; import * as AcpRegistryRuntimeCoordinator from "./AcpRegistryRuntimeCoordinator.ts"; import * as AcpRegistryAuth from "./auth.ts"; import * as AcpRegistryAuthenticationState from "./authenticationState.ts"; +import { + acpRegistryCurrentModelOptions, + acpRegistryModelsToDiscover, + makeAcpRegistryLearnedModelOptionsStore, + withAcpRegistryLearnedModelOptions, +} from "./learnedModelOptions.ts"; const DRIVER_KIND = ProviderDriverKind.make("acpRegistry"); const decodeSettings = Schema.decodeSync(AcpRegistrySettings); @@ -518,6 +525,9 @@ export const checkAcpRegistryProviderStatus = Effect.fn("AcpRegistryDriver.check readonly settings: AcpRegistrySettings; readonly cwd: string; readonly environment: NodeJS.ProcessEnv; + readonly modelDiscovery?: Omit & { + readonly onProbed: (provider: ServerProvider) => Effect.Effect>; + }; }, probeConfiguration: AcpRegistryConfigurationProbe, ): Effect.fn.Return { @@ -544,12 +554,25 @@ export const checkAcpRegistryProviderStatus = Effect.fn("AcpRegistryDriver.check inspection: inspected.success, }); } + const inspection = inspected.success; + const modelDiscovery = input.modelDiscovery; const probed = yield* Effect.result( probeConfiguration({ instanceId: input.instanceId, settings: input.settings, cwd: input.cwd, environment: providerProbeEnvironment(input.environment), + ...(modelDiscovery === undefined + ? {} + : { + modelDiscovery: { + onProbed: (probe) => + modelDiscovery.onProbed( + buildCheckedAcpRegistrySnapshot({ ...input, checkedAt, inspection, probe }), + ), + onDiscovered: modelDiscovery.onDiscovered, + }, + }), }), ); return Result.isFailure(probed) @@ -688,26 +711,31 @@ export const AcpRegistryDriver: ProviderDriver>([]); - const applyLiveConfiguration = ( + // reported by sessions and probe discovery are kept here and on disk. + const learnedModelOptions = yield* makeAcpRegistryLearnedModelOptionsStore({ + cacheDir: host.paths.providerStatusCacheDir, + instanceId, + settings: effectiveConfig, + }); + const applyLiveConfiguration = Effect.fn("AcpRegistryDriver.applyLiveConfiguration")(function* ( provider: ServerProvider, configuration: AcpRegistryLiveConfiguration, - ) => - Ref.modify(learnedModels, (learned) => { - const next = applyAcpRegistryLiveConfiguration( - provider, - configuration, - effectiveConfig.customModels, - learned.length > 0 ? learned : provider.models, - ); - return [next, next.models === provider.models ? learned : next.models]; - }); - const clearLearnedModels = Ref.set(learnedModels, []); + ) { + const reported = acpRegistryCurrentModelOptions(configuration); + if (reported !== undefined) { + yield* learnedModelOptions.learn(reported.modelId, reported.options, "live"); + } + return withAcpRegistryLearnedModelOptions( + applyAcpRegistryLiveConfiguration(provider, configuration, effectiveConfig.customModels), + yield* learnedModelOptions.get, + ); + }); + const clearLearnedModels = learnedModelOptions.clear; const withLiveRuntimeState = Effect.fn("AcpRegistryDriver.withLiveRuntimeState")(function* ( input: ServerProvider, ) { if (input.auth.status === "unauthenticated") yield* confirmedAuthentication.set(false); + if (input.installed) yield* learnedModelOptions.setVersion(input.version); const provider = input.enabled && input.installed && @@ -715,26 +743,22 @@ export const AcpRegistryDriver: ProviderDriver { - const withCommands = applyAcpRegistryAvailableCommands(provider, commands); - return Option.match(configuration, { - onNone: () => Effect.succeed(withCommands), - onSome: (liveConfiguration) => - applyLiveConfiguration(withCommands, liveConfiguration), - }).pipe( - Effect.map((withConfiguration) => - applyAcpRegistryUrlAuthAction(withConfiguration, authAction), - ), - ); - }), - ); + if (Option.isNone(runtimeCoordinator)) { + return withAcpRegistryLearnedModelOptions(provider, yield* learnedModelOptions.get); + } + const coordinator = runtimeCoordinator.value; + const withCommands = applyAcpRegistryAvailableCommands( + provider, + yield* coordinator.getAvailableCommands(instanceId), + ); + const configuration = yield* coordinator.getLiveConfiguration(instanceId); + const withConfiguration = Option.isSome(configuration) + ? yield* applyLiveConfiguration(withCommands, configuration.value) + : withAcpRegistryLearnedModelOptions(withCommands, yield* learnedModelOptions.get); + return applyAcpRegistryUrlAuthAction( + withConfiguration, + yield* coordinator.getUrlAuthAction(instanceId), + ); }); // A local executable's version comes only from probing it, so readiness checks reuse the last report. const reportedLocalVersion = yield* Ref.make(null); @@ -751,19 +775,48 @@ export const AcpRegistryDriver: ProviderDriver