GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(acp): session-model-state agents switch models live and keep thei… · pingdotgg/t3code@76c4589

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src
    • orchestration-v2/Adapters
      • AcpAdapterV2.test.ts
    • provider
      • AcpRegistryProbe.test.ts
  • packages
    • provider-acp-registry/src/server
      • probe.ts
    • provider-acp/src/server
      • adapter.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

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 numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ import {
7373
} from "@t3tools/provider-grok/testing";
7474
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
7575
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
76+
import {
77+
decodeAcpReplayTranscript,
78+
makeAcpReplayCompletenessAssertion,
79+
makeAcpReplayRuntime,
80+
} from "./AcpAdapterV2.testkit.ts";
7681
import type * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
7782
import {
7883
AcpProviderCapabilitiesV2,
@@ -975,6 +980,185 @@ describe("AcpAdapterV2", () => {
975980
}).pipe(Effect.provide(layerTest), Effect.scoped),
976981
);
977982

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+
9781162
it.effect("starts a new replay message after ACP v2 plan boundaries", () =>
9791163
Effect.gen(function* () {
9801164
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
Collapse file

‎apps/server/src/provider/AcpRegistryProbe.test.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/provider/AcpRegistryProbe.test.ts
+36-2Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ describe("ACP Registry probe", () => {
348348
]);
349349
});
350350

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", () => {
352352
const result = acpRegistryProbeResult(instanceId, {
353353
sessionId: "probe-session",
354354
initializeResult: { protocolVersion: 1 },
@@ -372,9 +372,43 @@ describe("ACP Registry probe", () => {
372372
} satisfies AcpSessionRuntime.AcpSessionRuntimeStartResult);
373373

374374
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");
376377
});
377378

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+
378412
it("bounds and de-duplicates advertised commands", () => {
379413
const commands = normalizeAcpRegistryCommands([
380414
{
Collapse file

‎packages/provider-acp-registry/src/server/probe.ts‎

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 numberDiff line numberDiff line change
@@ -72,24 +72,43 @@ interface AdvertisedModel {
7272
readonly description: string | null;
7373
}
7474

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+
*/
7590
function normalizeModels(
7691
candidates: ReadonlyArray<AdvertisedModel>,
92+
currentModelId: string | null,
7793
): ReadonlyArray<AcpRegistryProbeModel> {
7894
const seen = new Set<string>();
7995
const models: Array<AcpRegistryProbeModel> = [];
96+
let scanned = 0;
8097
for (const candidate of candidates) {
98+
if (models.length === MAX_MODELS) break;
99+
scanned += 1;
81100
const id = boundedOpaqueValue(candidate.id, MAX_ID_LENGTH);
82101
if (id === undefined || seen.has(id)) continue;
83102
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+
}
93112
}
94113
return models;
95114
}
@@ -207,7 +226,7 @@ export function normalizeAcpRegistryLiveConfiguration(
207226
advertised.currentModelId === null
208227
? null
209228
: (boundedOpaqueValue(advertised.currentModelId, MAX_ID_LENGTH) ?? null);
210-
const models = normalizeModels(advertised.candidates);
229+
const models = normalizeModels(advertised.candidates, boundedCurrentModelId);
211230
return {
212231
models,
213232
currentModelId: models.some((model) => model.id === boundedCurrentModelId)
Collapse file

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

Copy file name to clipboardExpand all lines: packages/provider-acp/src/server/adapter.ts
+18-10Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -648,15 +648,18 @@ function negotiatedCapabilities(
648648
const agent = started.initializeResult.agentCapabilities ?? {};
649649
const session = agent.sessionCapabilities;
650650
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;
653656
const canLoad = agent.loadSession === true;
654657
const canFork = session?.fork != null;
655658
return {
656659
...base,
657660
sessions: {
658661
...base.sessions,
659-
supportsModelSwitchInSession: hasModelConfig,
662+
supportsModelSwitchInSession: canSwitchModel,
660663
},
661664
threads: {
662665
...base.threads,
@@ -6301,19 +6304,24 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
63016304
startResult,
63026305
modelSelection,
63036306
});
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";
63096311
const hasModelConfig =
63106312
startResult.sessionSetupResult.configOptions?.some(
63116313
(option) => option.category === "model",
63126314
) === true;
63136315
const modelState = startResult.sessionSetupResult.models;
63146316
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+
) {
63176325
// ACP v1 agents without a model config option (Hermes) advertise
63186326
// their models as session model state and switch with
63196327
// session/set_model.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.