GeezerVison is showing github.com — fix(acp): session-model-state agents switch models live and keep thei… · pingdotgg/t3code@76c4589
File treeExpand file treeCollapse file tree Open diff view settings orchestration-v2/Adapters
provider-acp-registry/src/server
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +184Lines changed: 184 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -73,6 +73,11 @@ import { | 73 | 73 | } from "@t3tools/provider-grok/testing"; | 74 | 74 | import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; | 75 | 75 | import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; | | 76 | +import { | | 77 | + decodeAcpReplayTranscript, | | 78 | + makeAcpReplayCompletenessAssertion, | | 79 | + makeAcpReplayRuntime, | | 80 | +} from "./AcpAdapterV2.testkit.ts"; | 76 | 81 | import type * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; | 77 | 82 | import { | 78 | 83 | AcpProviderCapabilitiesV2, | @@ -975,6 +980,185 @@ describe("AcpAdapterV2", () => { | 975 | 980 | }).pipe(Effect.provide(layerTest), Effect.scoped), | 976 | 981 | ); | 977 | 982 |
| | 983 | + describe("an agent that advertises models only as session model state", () => { | | 984 | + type Frame = Record<string, unknown>; | | 985 | + const outbound = (method: string, params: unknown = "<any>"): Frame => ({ | | 986 | + type: "expect_outbound", | | 987 | + frame: { kind: "request", method, params }, | | 988 | + }); | | 989 | + const answer = (method: string, result: unknown): Frame => ({ | | 990 | + type: "emit_inbound", | | 991 | + frame: { kind: "response", method, result }, | | 992 | + }); | | 993 | + const setModel = (modelId: string): ReadonlyArray<Frame> => [ | | 994 | + outbound("session/set_model", { sessionId: "agent-session", modelId }), | | 995 | + answer("session/set_model", {}), | | 996 | + ]; | | 997 | + const prompt: ReadonlyArray<Frame> = [ | | 998 | + outbound("session/prompt"), | | 999 | + answer("session/prompt", { stopReason: "end_turn" }), | | 1000 | + ]; | | 1001 | + | | 1002 | + // A scripted ACP v1 agent (Hermes) whose session/new advertises | | 1003 | + // `availableModelIds` and `currentModelId` with no model config option. | | 1004 | + // Opens on `openModel`, runs one turn per `turnModels` entry, and requires | | 1005 | + // exactly `turnFrames` after session/new. Returns the adapter's plan for | | 1006 | + // each consecutive in-session switch. | | 1007 | + const runTurns = Effect.fn("runSessionModelStateTurns")(function* (input: { | | 1008 | + readonly availableModelIds: ReadonlyArray<string>; | | 1009 | + readonly currentModelId: string; | | 1010 | + readonly openModel: string; | | 1011 | + readonly turnModels: ReadonlyArray<string>; | | 1012 | + readonly turnFrames: ReadonlyArray<Frame>; | | 1013 | + }) { | | 1014 | + const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; | | 1015 | + const fileSystem = yield* FileSystem.FileSystem; | | 1016 | + const path = yield* Path.Path; | | 1017 | + const replayDir = yield* fileSystem.makeTempDirectoryScoped({ | | 1018 | + prefix: "t3-acp-session-model-state-", | | 1019 | + }); | | 1020 | + const statusPath = path.join(replayDir, "status.json"); | | 1021 | + const transcript = yield* decodeAcpReplayTranscript( | | 1022 | + { | | 1023 | + provider: ACP_TEST_DRIVER, | | 1024 | + protocol: "acp.ndjson-jsonrpc", | | 1025 | + version: "1", | | 1026 | + scenario: "session-model-state", | | 1027 | + entries: [ | | 1028 | + outbound("initialize"), | | 1029 | + answer("initialize", { | | 1030 | + protocolVersion: 1, | | 1031 | + agentCapabilities: { loadSession: false }, | | 1032 | + authMethods: [{ id: "replay", name: "Replay" }], | | 1033 | + }), | | 1034 | + outbound("session/new"), | | 1035 | + answer("session/new", { | | 1036 | + sessionId: "agent-session", | | 1037 | + models: { | | 1038 | + currentModelId: input.currentModelId, | | 1039 | + availableModels: input.availableModelIds.map((modelId) => ({ | | 1040 | + modelId, | | 1041 | + name: modelId, | | 1042 | + })), | | 1043 | + }, | | 1044 | + }), | | 1045 | + ...input.turnFrames, | | 1046 | + ] as never, | | 1047 | + }, | | 1048 | + ACP_TEST_DRIVER, | | 1049 | + ); | | 1050 | + const instanceId = ProviderInstanceId.make("acp-test-session-model-state"); | | 1051 | + const threadId = ThreadId.make("thread-acp-session-model-state"); | | 1052 | + const adapter = yield* makeAcpAdapterV2({ | | 1053 | + instanceId, | | 1054 | + flavor: { | | 1055 | + driver: ACP_TEST_DRIVER, | | 1056 | + capabilities: AcpProviderCapabilitiesV2, | | 1057 | + makeRuntime: makeAcpReplayRuntime({ | | 1058 | + transcript, | | 1059 | + statusPath, | | 1060 | + scriptPath: yield* path.fromFileUrl( | | 1061 | + new URL("../../../scripts/acp-replay-agent.ts", import.meta.url), | | 1062 | + ), | | 1063 | + childProcessSpawner, | | 1064 | + fileSystem, | | 1065 | + }), | | 1066 | + }, | | 1067 | + selfInvocation: yield* resolveSelfInvocation(), | | 1068 | + }); | | 1069 | + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ | | 1070 | + runtimeMode: "approval-required", | | 1071 | + interactionMode: "default", | | 1072 | + cwd: replayDir, | | 1073 | + }); | | 1074 | + const selection = (model: string): ModelSelection => ({ instanceId, model }); | | 1075 | + const plans = yield* Effect.gen(function* () { | | 1076 | + const runtime = yield* adapter.openSession({ | | 1077 | + threadId, | | 1078 | + providerSessionId: ProviderSessionId.make("provider-session-acp-session-model-state"), | | 1079 | + modelSelection: selection(input.openModel), | | 1080 | + runtimePolicy, | | 1081 | + }); | | 1082 | + const providerThread = yield* runtime.ensureThread({ | | 1083 | + threadId, | | 1084 | + modelSelection: selection(input.openModel), | | 1085 | + runtimePolicy, | | 1086 | + }); | | 1087 | + const now = yield* DateTime.now; | | 1088 | + const plans: Array<string> = []; | | 1089 | + let current = selection(input.openModel); | | 1090 | + for (const [index, model] of input.turnModels.entries()) { | | 1091 | + const target = selection(model); | | 1092 | + const plan = yield* adapter.planSelectionTransition({ | | 1093 | + current, | | 1094 | + target, | | 1095 | + sessionCapabilities: runtime.providerSession.capabilities, | | 1096 | + }); | | 1097 | + plans.push(plan.type); | | 1098 | + yield* runtime.startTurn( | | 1099 | + makeTurnInput({ | | 1100 | + threadId, | | 1101 | + providerThread, | | 1102 | + instanceId, | | 1103 | + runtimePolicy, | | 1104 | + now, | | 1105 | + ordinal: index + 1, | | 1106 | + modelSelection: target, | | 1107 | + }), | | 1108 | + ); | | 1109 | + yield* runtime.events.pipe( | | 1110 | + Stream.takeUntil((event) => event.type === "turn.terminal"), | | 1111 | + Stream.runDrain, | | 1112 | + ); | | 1113 | + current = target; | | 1114 | + } | | 1115 | + return plans; | | 1116 | + }).pipe(Effect.scoped); | | 1117 | + // Closing the session stops the agent, which writes its replay status in | | 1118 | + // the same tick as its last answer. The script must be consumed exactly. | | 1119 | + yield* makeAcpReplayCompletenessAssertion(fileSystem, statusPath, transcript); | | 1120 | + return plans; | | 1121 | + }); | | 1122 | + | | 1123 | + it.effect("switches models within the session from A to B and back to A", () => | | 1124 | + Effect.gen(function* () { | | 1125 | + const plans = yield* runTurns({ | | 1126 | + availableModelIds: ["hermes-4-70b", "nous/hermes-4-405b"], | | 1127 | + currentModelId: "hermes-4-70b", | | 1128 | + openModel: "hermes-4-70b", | | 1129 | + turnModels: ["nous/hermes-4-405b", "hermes-4-70b"], | | 1130 | + turnFrames: [ | | 1131 | + ...setModel("nous/hermes-4-405b"), | | 1132 | + ...prompt, | | 1133 | + ...setModel("hermes-4-70b"), | | 1134 | + ...prompt, | | 1135 | + ], | | 1136 | + }); | | 1137 | + assert.deepEqual(plans, ["apply_on_next_turn", "apply_on_next_turn"]); | | 1138 | + }).pipe(Effect.provide(layerTest), Effect.scoped), | | 1139 | + ); | | 1140 | + | | 1141 | + it.effect("selects advertised models whose ids are literally default or auto", () => | | 1142 | + runTurns({ | | 1143 | + availableModelIds: ["hermes-4-70b", "auto", "default"], | | 1144 | + currentModelId: "hermes-4-70b", | | 1145 | + openModel: "hermes-4-70b", | | 1146 | + turnModels: ["auto", "default"], | | 1147 | + turnFrames: [...setModel("auto"), ...prompt, ...setModel("default"), ...prompt], | | 1148 | + }).pipe(Effect.provide(layerTest), Effect.scoped), | | 1149 | + ); | | 1150 | + | | 1151 | + it.effect("keeps the agent's model when an unadvertised Default is picked", () => | | 1152 | + runTurns({ | | 1153 | + availableModelIds: ["hermes-4-70b", "nous/hermes-4-405b"], | | 1154 | + currentModelId: "hermes-4-70b", | | 1155 | + openModel: "default", | | 1156 | + turnModels: ["nous/hermes-4-405b", "default"], | | 1157 | + turnFrames: [...setModel("nous/hermes-4-405b"), ...prompt, ...prompt], | | 1158 | + }).pipe(Effect.provide(layerTest), Effect.scoped), | | 1159 | + ); | | 1160 | + }); | | 1161 | + | 978 | 1162 | it.effect("starts a new replay message after ACP v2 plan boundaries", () => | 979 | 1163 | Effect.gen(function* () { | 980 | 1164 | const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/AcpRegistryProbe.test.ts +36-2Lines changed: 36 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -348,7 +348,7 @@ describe("ACP Registry probe", () => { | 348 | 348 | ]); | 349 | 349 | }); | 350 | 350 |
| 351 | | - it("omits a current model that falls outside the bounded model catalog", () => { | | 351 | + it("keeps a current model configuration option inside the bounded model catalog", () => { | 352 | 352 | const result = acpRegistryProbeResult(instanceId, { | 353 | 353 | sessionId: "probe-session", | 354 | 354 | initializeResult: { protocolVersion: 1 }, | @@ -372,9 +372,43 @@ describe("ACP Registry probe", () => { | 372 | 372 | } satisfies AcpSessionRuntime.AcpSessionRuntimeStartResult); | 373 | 373 |
| 374 | 374 | expect(result.models).toHaveLength(256); | 375 | | - expect(result.currentModelId).toBeNull(); | | 375 | + expect(result.models.at(-1)).toEqual({ id: "model-256", name: "Model 256", description: null }); | | 376 | + expect(result.currentModelId).toBe("model-256"); | 376 | 377 | }); | 377 | 378 |
| | 379 | + it.each([ | | 380 | + { current: "model-300", currentModelId: "model-300", last: "model-300" }, | | 381 | + { current: "model-3", currentModelId: "model-3", last: "model-255" }, | | 382 | + { current: "model-999", currentModelId: null, last: "model-255" }, | | 383 | + { current: " model-300", currentModelId: null, last: "model-255" }, | | 384 | + ])( | | 385 | + "bounds session model state to 256 models around current model $current", | | 386 | + ({ current, currentModelId, last }) => { | | 387 | + const result = acpRegistryProbeResult(instanceId, { | | 388 | + sessionId: "probe-session", | | 389 | + initializeResult: { protocolVersion: 1 }, | | 390 | + sessionSetupResult: { | | 391 | + sessionId: "probe-session", | | 392 | + models: { | | 393 | + currentModelId: current, | | 394 | + availableModels: Array.from({ length: 400 }, (_, index) => ({ | | 395 | + modelId: `model-${index}`, | | 396 | + name: `Model ${index}`, | | 397 | + })), | | 398 | + }, | | 399 | + }, | | 400 | + modelConfigId: undefined, | | 401 | + } satisfies AcpSessionRuntime.AcpSessionRuntimeStartResult); | | 402 | + | | 403 | + expect(result.models).toHaveLength(256); | | 404 | + expect(result.models.slice(0, 255).map((model) => model.id)).toEqual( | | 405 | + Array.from({ length: 255 }, (_, index) => `model-${index}`), | | 406 | + ); | | 407 | + expect(result.models.at(-1)?.id).toBe(last); | | 408 | + expect(result.currentModelId).toBe(currentModelId); | | 409 | + }, | | 410 | + ); | | 411 | + | 378 | 412 | it("bounds and de-duplicates advertised commands", () => { | 379 | 413 | const commands = normalizeAcpRegistryCommands([ | 380 | 414 | { | |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-acp-registry/src/server/probe.ts +29-10Lines changed: 29 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -72,24 +72,43 @@ interface AdvertisedModel { | 72 | 72 | readonly description: string | null; | 73 | 73 | } | 74 | 74 |
| | 75 | +function normalizedModel(id: string, candidate: AdvertisedModel): AcpRegistryProbeModel { | | 76 | + return { | | 77 | + id, | | 78 | + name: boundedText(candidate.name, MAX_NAME_LENGTH) || id, | | 79 | + description: | | 80 | + candidate.description === null | | 81 | + ? null | | 82 | + : boundedText(candidate.description, MAX_DESCRIPTION_LENGTH) || null, | | 83 | + }; | | 84 | +} | | 85 | + | | 86 | +/** | | 87 | + * Bounds an advertised catalog to MAX_MODELS. A valid current model listed past | | 88 | + * the cap takes the last slot, so the catalog still shows what the session runs on. | | 89 | + */ | 75 | 90 | function normalizeModels( | 76 | 91 | candidates: ReadonlyArray<AdvertisedModel>, | | 92 | + currentModelId: string | null, | 77 | 93 | ): ReadonlyArray<AcpRegistryProbeModel> { | 78 | 94 | const seen = new Set<string>(); | 79 | 95 | const models: Array<AcpRegistryProbeModel> = []; | | 96 | + let scanned = 0; | 80 | 97 | for (const candidate of candidates) { | | 98 | + if (models.length === MAX_MODELS) break; | | 99 | + scanned += 1; | 81 | 100 | const id = boundedOpaqueValue(candidate.id, MAX_ID_LENGTH); | 82 | 101 | if (id === undefined || seen.has(id)) continue; | 83 | 102 | seen.add(id); | 84 | | - models.push({ | 85 | | - id, | 86 | | - name: boundedText(candidate.name, MAX_NAME_LENGTH) || id, | 87 | | - description: | 88 | | - candidate.description === null | 89 | | - ? null | 90 | | - : boundedText(candidate.description, MAX_DESCRIPTION_LENGTH) || null, | 91 | | - }); | 92 | | - if (models.length === MAX_MODELS) break; | | 103 | + models.push(normalizedModel(id, candidate)); | | 104 | + } | | 105 | + if (currentModelId !== null && models.length === MAX_MODELS && !seen.has(currentModelId)) { | | 106 | + const current = candidates.find( | | 107 | + (candidate, index) => index >= scanned && candidate.id === currentModelId, | | 108 | + ); | | 109 | + if (current !== undefined) { | | 110 | + models[MAX_MODELS - 1] = normalizedModel(currentModelId, current); | | 111 | + } | 93 | 112 | } | 94 | 113 | return models; | 95 | 114 | } | @@ -207,7 +226,7 @@ export function normalizeAcpRegistryLiveConfiguration( | 207 | 226 | advertised.currentModelId === null | 208 | 227 | ? null | 209 | 228 | : (boundedOpaqueValue(advertised.currentModelId, MAX_ID_LENGTH) ?? null); | 210 | | - const models = normalizeModels(advertised.candidates); | | 229 | + const models = normalizeModels(advertised.candidates, boundedCurrentModelId); | 211 | 230 | return { | 212 | 231 | models, | 213 | 232 | currentModelId: models.some((model) => model.id === boundedCurrentModelId) | |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-acp/src/server/adapter.ts +18-10Lines changed: 18 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -648,15 +648,18 @@ function negotiatedCapabilities( | 648 | 648 | const agent = started.initializeResult.agentCapabilities ?? {}; | 649 | 649 | const session = agent.sessionCapabilities; | 650 | 650 | const setup = started.sessionSetupResult; | 651 | | - const hasModelConfig = | 652 | | - setup.configOptions?.some((option) => option.category === "model") === true; | | 651 | + // Mirrors configureSession: a model config option, or ACP v1 session model | | 652 | + // state switched with session/set_model. | | 653 | + const canSwitchModel = | | 654 | + setup.configOptions?.some((option) => option.category === "model") === true || | | 655 | + setup.models != null; | 653 | 656 | const canLoad = agent.loadSession === true; | 654 | 657 | const canFork = session?.fork != null; | 655 | 658 | return { | 656 | 659 | ...base, | 657 | 660 | sessions: { | 658 | 661 | ...base.sessions, | 659 | | - supportsModelSwitchInSession: hasModelConfig, | | 662 | + supportsModelSwitchInSession: canSwitchModel, | 660 | 663 | }, | 661 | 664 | threads: { | 662 | 665 | ...base.threads, | @@ -6301,19 +6304,24 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( | 6301 | 6304 | startResult, | 6302 | 6305 | modelSelection, | 6303 | 6306 | }); | 6304 | | - } else if ( | 6305 | | - requestedModel.length > 0 && | 6306 | | - requestedModel !== "auto" && | 6307 | | - requestedModel !== "default" | 6308 | | - ) { | | 6307 | + } else if (requestedModel.length > 0) { | | 6308 | + // "auto" and "default" stand for the agent's own choice unless the | | 6309 | + // agent advertises a model under that exact id. | | 6310 | + const isPlaceholder = requestedModel === "auto" || requestedModel === "default"; | 6309 | 6311 | const hasModelConfig = | 6310 | 6312 | startResult.sessionSetupResult.configOptions?.some( | 6311 | 6313 | (option) => option.category === "model", | 6312 | 6314 | ) === true; | 6313 | 6315 | const modelState = startResult.sessionSetupResult.models; | 6314 | 6316 | if (hasModelConfig) { | 6315 | | - yield* runtime.setModel(requestedModel); | 6316 | | - } else if (modelState != null) { | | 6317 | + if (!isPlaceholder) { | | 6318 | + yield* runtime.setModel(requestedModel); | | 6319 | + } | | 6320 | + } else if ( | | 6321 | + modelState != null && | | 6322 | + (!isPlaceholder || | | 6323 | + modelState.availableModels.some((model) => model.modelId === requestedModel)) | | 6324 | + ) { | 6317 | 6325 | // ACP v1 agents without a model config option (Hermes) advertise | 6318 | 6326 | // their models as session model state and switch with | 6319 | 6327 | // session/set_model. | |
|