TypeScript
fix(acp): reject models unavailable to the active session (#26623)
Tejas Rahanee4a067b526a1
diff --git a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.tsindex 9c7ea03fe75..7cfd1973ebe 100644--- a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts+++ b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts@@ -28,6 +28,7 @@ import * as HttpClientResponse from "effect/http/HttpClientResponse";import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";import * as EffectAcpErrors from "effect-acp/errors";+import { makeProviderFailure } from "@t3tools/provider-core/server/failure";import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";import {@@ -468,6 +469,7 @@ describe("AcpRegistryAdapterV2", () => {readonly storedModel?: string;readonly storedModePick?: string;readonly turnModels?: ReadonlyArray<string>;+ readonly rejectedModel?: string;}) {const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;// Scope release gives the replay agent at most a second to exit, so the status@@ -605,7 +607,19 @@ describe("AcpRegistryAdapterV2", () => {configurations.push(published);current = target;}- }).pipe(Effect.scoped);+ }).pipe(+ Effect.match({+ onFailure: (error) => {+ assert.isDefined(input.rejectedModel);+ assert.equal(+ makeProviderFailure({ cause: error }).message,+ `The agent did not accept the model "${input.rejectedModel}". Choose another model for this provider, then retry.`,+ );+ },+ onSuccess: () => assert.isUndefined(input.rejectedModel),+ }),+ Effect.scoped,+ );yield* Effect.all(agentExits, { discard: true });yield* makeAcpReplayCompletenessAssertion(fileSystem, statusPath, transcript);return { configurations };@@ -772,8 +786,70 @@ describe("AcpRegistryAdapterV2", () => {}).pipe(Effect.provide(layerTest), Effect.scoped),);+ it.effect.each([+ "config",+ "legacy",+ "updated-config",+ "removed-config",+ "configuration-update",+ "unavailable-before-configuration",+ ] as const)(+ "rejects an unavailable explicit model in a %s catalog before inference",+ (catalog) => {+ const duringTurn = catalog === "updated-config" || catalog === "removed-config";+ const duringConfiguration = catalog === "configuration-update";+ const changesOptions =+ duringConfiguration || catalog === "unavailable-before-configuration";+ const model = duringTurn || duringConfiguration ? "hermes" : "missing";+ const restrictedOption = {+ ...modelOption("model", "other"),+ options: [{ value: "other", name: "Other" }],+ };+ return openWithStoredPicks({+ setup: {+ ...(catalog === "legacy" || catalog === "removed-config"+ ? {+ models: {+ currentModelId: "other",+ availableModels: [{ modelId: "other", name: "Other" }],+ },+ }+ : {}),+ configOptions:+ catalog === "legacy"+ ? []+ : [+ {+ ...modelOption("model", "hermes"),+ options: [{ value: "hermes", name: "Hermes" }],+ },+ ...(changesOptions ? [permissionModeOption("ask")] : []),+ ],+ },+ storedModel: model,+ ...(duringTurn ? { turnModels: [model, model] } : {}),+ ...(changesOptions ? { storedModePick: "auto" } : {}),+ rejectedModel: model,+ pickFrames: duringTurn+ ? prompt(configOptionUpdate(catalog === "removed-config" ? [] : [restrictedOption]))+ : duringConfiguration+ ? [+ outbound("session/set_config_option", {+ sessionId: "agent-session",+ configId: "permission-mode",+ value: "auto",+ }),+ answer("session/set_config_option", {+ configOptions: [restrictedOption, permissionModeOption("auto")],+ }),+ ]+ : [],+ }).pipe(Effect.provide(layerTest), Effect.scoped);+ },+ );+// regression: #19519 models come from session model state, not a hardcoded list- it.effect("leaves unadvertised model IDs alone in both model selection protocols", () =>+ it.effect("keeps unadvertised default and auto selections agent-controlled", () =>Effect.gen(function* () {const { configurations } = yield* openWithStoredPicks({setup: {@@ -783,15 +859,7 @@ describe("AcpRegistryAdapterV2", () => {},},storedModel: "default",- turnModels: [- "auto",- "removed-model",- "other",- "default",- "auto",- "hermes",- "removed-model",- ],+ turnModels: ["auto", "default", "other", "default", "auto", "default", "auto"],pickFrames: [...prompt(),...prompt(),diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.tsindex e7b67238b07..3551623d7b1 100644--- a/packages/provider-acp/src/server/adapter.ts+++ b/packages/provider-acp/src/server/adapter.ts@@ -97,7 +97,11 @@ import {resolveEmbeddedTerminalContent,type AcpClientTerminals,} from "./clientTerminals.ts";-import { ACP_EMPTY_OPTION_VALUE, ACP_SESSION_MODE_OPTION_ID } from "./sessionConfig.ts";+import {+ ACP_EMPTY_OPTION_VALUE,+ ACP_SESSION_MODE_OPTION_ID,+ flattenSelectChoices,+} from "./sessionConfig.ts";import * as AcpSessionRuntime from "./AcpSessionRuntime.ts";import {t3AcpPromptWithInstructions,@@ -6733,12 +6737,41 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (return activated;});+ const validateModelSelection = Effect.fn("AcpAdapterV2.validateModelSelection")(function* (+ modelSelection: ModelSelection,+ ) {+ const model = flavor.resolveModelId?.(modelSelection) ?? modelSelection.model;+ if (+ flavor.applyModelSelection !== undefined ||+ model.length === 0 ||+ model === "default" ||+ model === "auto" ||+ flavor.customModels?.includes(model) === true+ )+ return;+ const option = findModelConfigOption(yield* runtime.getConfigOptions);+ const models = (yield* Ref.get(activeSessionSetup))?.sessionSetupResult.models;+ const available =+ option?.type === "select"+ ? flattenSelectChoices(option.options).some((choice) => choice.value === model)+ : models?.availableModels.some((choice) => choice.modelId === model);+ if (available === false) {+ return yield* new AcpModelSelectionError({+ model: modelSelection.model,+ cause: EffectAcpErrors.AcpRequestError.invalidParams(+ "The requested model is not available in this session.",+ ),+ });+ }+ });+const configureSession = Effect.fnUntraced(function* (startResult: AcpSessionRuntime.AcpSessionRuntimeStartResult,modelSelection: ModelSelection,runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy,) {yield* Ref.set(activeSelection, null);+ yield* validateModelSelection(modelSelection);const requestedModel = flavor.resolveModelId?.(modelSelection) ?? modelSelection.model;// Invalid params is the agent refusing the value, and the runtime// raises the same code when the session's model option does not offer@@ -6766,10 +6799,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (if (modelOption?.type === "select") {if (isCustomModel ||- modelOption.options.some((entry) =>- "value" in entry- ? entry.value === requestedModel- : entry.options.some((choice) => choice.value === requestedModel),+ flattenSelectChoices(modelOption.options).some(+ (choice) => choice.value === requestedModel,)) {yield* runtime@@ -6941,6 +6972,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (nativeBuildConfigurationBySessionId.delete(startResult.sessionId);}}+ yield* validateModelSelection(modelSelection);yield* (flavor.onSessionConfigurationUpdate?.(yield* runtime.getConfigOptions,@@ -7448,6 +7480,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (yield* Ref.set(activeInteractionMode, turnInput.runtimePolicy.interactionMode);}}+ yield* validateModelSelection(turnInput.modelSelection);yield* Ref.set(lastTurnRoute, {threadId: turnInput.threadId,providerThreadId: turnInput.providerThread.id,