@@ -56,6 +56,7 @@ import type * as EffectAcpSchema from "effect-acp/compat";
5656
5757import * as ServerConfig from "../../config.ts";
5858import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
59+ import { ACP_SESSION_MODE_OPTION_ID } from "../../provider/acp/AcpSessionConfig.ts";
5960import * as AcpSessionRuntime from "../../provider/acp/AcpSessionRuntime.ts";
6061import {
6162 extractXAiAcpSubagentEndNotice,
@@ -907,6 +908,99 @@ describe("AcpAdapterV2", () => {
907908 }).pipe(Effect.provide(testLayer), Effect.scoped),
908909 );
909910
911+ it.live("reapplies a selection after a switch away from it failed partway", () =>
912+ Effect.gen(function* () {
913+ const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
914+ const path = yield* Path.Path;
915+ const mockAgentPath = yield* path.fromFileUrl(
916+ new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
917+ );
918+ const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
919+ const instanceId = ProviderInstanceId.make("acp-test-partial-selection-reapply");
920+ const threadId = ThreadId.make("thread-acp-partial-selection-reapply");
921+ const adapter = makeAcpAdapterV2({
922+ crypto: yield* Crypto.Crypto,
923+ instanceId,
924+ flavor: {
925+ driver: ACP_TEST_DRIVER,
926+ capabilities: AcpProviderCapabilitiesV2,
927+ makeRuntime: makeMockRuntime({
928+ childProcessSpawner,
929+ mockAgentPath,
930+ protocolEvents,
931+ // The model switch lands, then the agent rejects the mode step.
932+ environment: { T3_ACP_FAIL_SET_MODE_CONFIG_OPTION: "1" },
933+ }),
934+ },
935+ fileSystem: yield* FileSystem.FileSystem,
936+ idAllocator: yield* IdAllocator.IdAllocatorV2,
937+ serverConfig: yield* ServerConfig.ServerConfig,
938+ selfInvocation: yield* resolveSelfInvocation(),
939+ });
940+ const runtimePolicy = ProviderAdapterV2RuntimePolicy.make({
941+ runtimeMode: "full-access",
942+ interactionMode: "default",
943+ cwd: process.cwd(),
944+ });
945+ const selectionA = { instanceId, model: "composer-2" } as const;
946+ const selectionB = {
947+ instanceId,
948+ model: "composer-2[fast=true]",
949+ options: [{ id: ACP_SESSION_MODE_OPTION_ID, value: "code" }],
950+ } as const;
951+ const runtime = yield* adapter.openSession({
952+ threadId,
953+ providerSessionId: ProviderSessionId.make("provider-session-acp-partial-selection"),
954+ modelSelection: selectionA,
955+ runtimePolicy,
956+ });
957+ const providerThread = yield* runtime.ensureThread({
958+ threadId,
959+ modelSelection: selectionA,
960+ runtimePolicy,
961+ });
962+ const now = yield* DateTime.now;
963+ const turnInput = (ordinal: number, modelSelection: ModelSelection) =>
964+ makeTurnInput({
965+ threadId,
966+ providerThread,
967+ instanceId,
968+ runtimePolicy,
969+ now,
970+ ordinal,
971+ modelSelection,
972+ });
973+
974+ const switchToB = yield* Effect.exit(runtime.startTurn(turnInput(1, selectionB)));
975+ assert.isTrue(Exit.isFailure(switchToB), "the agent rejected B's mode step");
976+ yield* Queue.clear(protocolEvents);
977+
978+ yield* runtime.startTurn(turnInput(2, selectionA));
979+ yield* runtime.events.pipe(
980+ Stream.takeUntil((event) => event.type === "turn.terminal"),
981+ Stream.runDrain,
982+ );
983+ const modelSetsBeforePrompt: Array<unknown> = [];
984+ while (true) {
985+ const event = yield* Queue.take(protocolEvents);
986+ if (event.direction !== "outgoing") continue;
987+ const method = rawProtocolMethod(event);
988+ if (method === "session/prompt") break;
989+ if (
990+ method === "session/set_config_option" &&
991+ rawProtocolRequestParam(event, "configId") === "model"
992+ ) {
993+ modelSetsBeforePrompt.push(rawProtocolRequestParam(event, "value"));
994+ }
995+ }
996+ assert.deepEqual(
997+ modelSetsBeforePrompt,
998+ ["composer-2"],
999+ "the native model was left on B, so the turn on A must set it back first",
1000+ );
1001+ }).pipe(Effect.provide(testLayer), Effect.scoped),
1002+ );
1003+
9101004 it.effect("starts a new replay message after ACP v2 plan boundaries", () =>
9111005 Effect.gen(function* () {
9121006 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
0 commit comments