Skip to content

Commit e4a067b

Browse files
trahanemaria-rcks
andauthored
fix(acp): reject models unavailable to the active session (#26623)
Co-authored-by: maria-rcks <[email protected]>
1 parent 461df46 commit e4a067b

2 files changed

Lines changed: 117 additions & 16 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts‎

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import * as HttpClientResponse from "effect/http/HttpClientResponse";
2828
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
2929
import * as EffectAcpErrors from "effect-acp/errors";
3030

31+
import { makeProviderFailure } from "@t3tools/provider-core/server/failure";
3132
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
3233
import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
3334
import {
@@ -468,6 +469,7 @@ describe("AcpRegistryAdapterV2", () => {
468469
readonly storedModel?: string;
469470
readonly storedModePick?: string;
470471
readonly turnModels?: ReadonlyArray<string>;
472+
readonly rejectedModel?: string;
471473
}) {
472474
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
473475
// Scope release gives the replay agent at most a second to exit, so the status
@@ -605,7 +607,19 @@ describe("AcpRegistryAdapterV2", () => {
605607
configurations.push(published);
606608
current = target;
607609
}
608-
}).pipe(Effect.scoped);
610+
}).pipe(
611+
Effect.match({
612+
onFailure: (error) => {
613+
assert.isDefined(input.rejectedModel);
614+
assert.equal(
615+
makeProviderFailure({ cause: error }).message,
616+
`The agent did not accept the model "${input.rejectedModel}". Choose another model for this provider, then retry.`,
617+
);
618+
},
619+
onSuccess: () => assert.isUndefined(input.rejectedModel),
620+
}),
621+
Effect.scoped,
622+
);
609623
yield* Effect.all(agentExits, { discard: true });
610624
yield* makeAcpReplayCompletenessAssertion(fileSystem, statusPath, transcript);
611625
return { configurations };
@@ -772,8 +786,70 @@ describe("AcpRegistryAdapterV2", () => {
772786
}).pipe(Effect.provide(layerTest), Effect.scoped),
773787
);
774788

789+
it.effect.each([
790+
"config",
791+
"legacy",
792+
"updated-config",
793+
"removed-config",
794+
"configuration-update",
795+
"unavailable-before-configuration",
796+
] as const)(
797+
"rejects an unavailable explicit model in a %s catalog before inference",
798+
(catalog) => {
799+
const duringTurn = catalog === "updated-config" || catalog === "removed-config";
800+
const duringConfiguration = catalog === "configuration-update";
801+
const changesOptions =
802+
duringConfiguration || catalog === "unavailable-before-configuration";
803+
const model = duringTurn || duringConfiguration ? "hermes" : "missing";
804+
const restrictedOption = {
805+
...modelOption("model", "other"),
806+
options: [{ value: "other", name: "Other" }],
807+
};
808+
return openWithStoredPicks({
809+
setup: {
810+
...(catalog === "legacy" || catalog === "removed-config"
811+
? {
812+
models: {
813+
currentModelId: "other",
814+
availableModels: [{ modelId: "other", name: "Other" }],
815+
},
816+
}
817+
: {}),
818+
configOptions:
819+
catalog === "legacy"
820+
? []
821+
: [
822+
{
823+
...modelOption("model", "hermes"),
824+
options: [{ value: "hermes", name: "Hermes" }],
825+
},
826+
...(changesOptions ? [permissionModeOption("ask")] : []),
827+
],
828+
},
829+
storedModel: model,
830+
...(duringTurn ? { turnModels: [model, model] } : {}),
831+
...(changesOptions ? { storedModePick: "auto" } : {}),
832+
rejectedModel: model,
833+
pickFrames: duringTurn
834+
? prompt(configOptionUpdate(catalog === "removed-config" ? [] : [restrictedOption]))
835+
: duringConfiguration
836+
? [
837+
outbound("session/set_config_option", {
838+
sessionId: "agent-session",
839+
configId: "permission-mode",
840+
value: "auto",
841+
}),
842+
answer("session/set_config_option", {
843+
configOptions: [restrictedOption, permissionModeOption("auto")],
844+
}),
845+
]
846+
: [],
847+
}).pipe(Effect.provide(layerTest), Effect.scoped);
848+
},
849+
);
850+
775851
// regression: #19519 models come from session model state, not a hardcoded list
776-
it.effect("leaves unadvertised model IDs alone in both model selection protocols", () =>
852+
it.effect("keeps unadvertised default and auto selections agent-controlled", () =>
777853
Effect.gen(function* () {
778854
const { configurations } = yield* openWithStoredPicks({
779855
setup: {
@@ -783,15 +859,7 @@ describe("AcpRegistryAdapterV2", () => {
783859
},
784860
},
785861
storedModel: "default",
786-
turnModels: [
787-
"auto",
788-
"removed-model",
789-
"other",
790-
"default",
791-
"auto",
792-
"hermes",
793-
"removed-model",
794-
],
862+
turnModels: ["auto", "default", "other", "default", "auto", "default", "auto"],
795863
pickFrames: [
796864
...prompt(),
797865
...prompt(),

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ import {
9797
resolveEmbeddedTerminalContent,
9898
type AcpClientTerminals,
9999
} from "./clientTerminals.ts";
100-
import { ACP_EMPTY_OPTION_VALUE, ACP_SESSION_MODE_OPTION_ID } from "./sessionConfig.ts";
100+
import {
101+
ACP_EMPTY_OPTION_VALUE,
102+
ACP_SESSION_MODE_OPTION_ID,
103+
flattenSelectChoices,
104+
} from "./sessionConfig.ts";
101105
import * as AcpSessionRuntime from "./AcpSessionRuntime.ts";
102106
import {
103107
t3AcpPromptWithInstructions,
@@ -6733,12 +6737,41 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
67336737
return activated;
67346738
});
67356739

6740+
const validateModelSelection = Effect.fn("AcpAdapterV2.validateModelSelection")(function* (
6741+
modelSelection: ModelSelection,
6742+
) {
6743+
const model = flavor.resolveModelId?.(modelSelection) ?? modelSelection.model;
6744+
if (
6745+
flavor.applyModelSelection !== undefined ||
6746+
model.length === 0 ||
6747+
model === "default" ||
6748+
model === "auto" ||
6749+
flavor.customModels?.includes(model) === true
6750+
)
6751+
return;
6752+
const option = findModelConfigOption(yield* runtime.getConfigOptions);
6753+
const models = (yield* Ref.get(activeSessionSetup))?.sessionSetupResult.models;
6754+
const available =
6755+
option?.type === "select"
6756+
? flattenSelectChoices(option.options).some((choice) => choice.value === model)
6757+
: models?.availableModels.some((choice) => choice.modelId === model);
6758+
if (available === false) {
6759+
return yield* new AcpModelSelectionError({
6760+
model: modelSelection.model,
6761+
cause: EffectAcpErrors.AcpRequestError.invalidParams(
6762+
"The requested model is not available in this session.",
6763+
),
6764+
});
6765+
}
6766+
});
6767+
67366768
const configureSession = Effect.fnUntraced(function* (
67376769
startResult: AcpSessionRuntime.AcpSessionRuntimeStartResult,
67386770
modelSelection: ModelSelection,
67396771
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy,
67406772
) {
67416773
yield* Ref.set(activeSelection, null);
6774+
yield* validateModelSelection(modelSelection);
67426775
const requestedModel = flavor.resolveModelId?.(modelSelection) ?? modelSelection.model;
67436776
// Invalid params is the agent refusing the value, and the runtime
67446777
// raises the same code when the session's model option does not offer
@@ -6766,10 +6799,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
67666799
if (modelOption?.type === "select") {
67676800
if (
67686801
isCustomModel ||
6769-
modelOption.options.some((entry) =>
6770-
"value" in entry
6771-
? entry.value === requestedModel
6772-
: entry.options.some((choice) => choice.value === requestedModel),
6802+
flattenSelectChoices(modelOption.options).some(
6803+
(choice) => choice.value === requestedModel,
67736804
)
67746805
) {
67756806
yield* runtime
@@ -6941,6 +6972,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
69416972
nativeBuildConfigurationBySessionId.delete(startResult.sessionId);
69426973
}
69436974
}
6975+
yield* validateModelSelection(modelSelection);
69446976
yield* (
69456977
flavor.onSessionConfigurationUpdate?.(
69466978
yield* runtime.getConfigOptions,
@@ -7448,6 +7480,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
74487480
yield* Ref.set(activeInteractionMode, turnInput.runtimePolicy.interactionMode);
74497481
}
74507482
}
7483+
yield* validateModelSelection(turnInput.modelSelection);
74517484
yield* Ref.set(lastTurnRoute, {
74527485
threadId: turnInput.threadId,
74537486
providerThreadId: turnInput.providerThread.id,

0 commit comments

Comments
 (0)