Skip to content

Commit bee4a16

Browse files
fix(server): ACP reapplies a model after a switch away from it failed partway
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent e9298af commit bee4a16

3 files changed

Lines changed: 100 additions & 1 deletion

File tree

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const emitOverlappingXAiPromptCompleteOutOfOrder =
9494
process.env.T3_ACP_EMIT_OVERLAPPING_XAI_PROMPT_COMPLETE_OUT_OF_ORDER === "1";
9595
const failPrompt = process.env.T3_ACP_FAIL_PROMPT === "1";
9696
const failSetConfigOption = process.env.T3_ACP_FAIL_SET_CONFIG_OPTION === "1";
97+
const failSetModeConfigOption = process.env.T3_ACP_FAIL_SET_MODE_CONFIG_OPTION === "1";
9798
const exitOnSetConfigOption = process.env.T3_ACP_EXIT_ON_SET_CONFIG_OPTION === "1";
9899
const omitModelConfigOption = process.env.T3_ACP_OMIT_MODEL_CONFIG_OPTION === "1";
99100
const promptResponseText = process.env.T3_ACP_PROMPT_RESPONSE_TEXT;
@@ -775,7 +776,7 @@ const program = Effect.gen(function* () {
775776
process.exit(7);
776777
});
777778
}
778-
if (failSetConfigOption) {
779+
if (failSetConfigOption || (failSetModeConfigOption && request.configId === "mode")) {
779780
return yield* AcpError.AcpRequestError.invalidParams(
780781
"Mock invalid params for session/set_config_option",
781782
{

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import type * as EffectAcpSchema from "effect-acp/compat";
5656

5757
import * as ServerConfig from "../../config.ts";
5858
import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
59+
import { ACP_SESSION_MODE_OPTION_ID } from "../../provider/acp/AcpSessionConfig.ts";
5960
import * as AcpSessionRuntime from "../../provider/acp/AcpSessionRuntime.ts";
6061
import {
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;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6111,6 +6111,10 @@ export function makeAcpAdapterV2(
61116111
modelSelection: ModelSelection,
61126112
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy,
61136113
) {
6114+
// A step failing below leaves the native config partly changed, so
6115+
// the previously applied selection no longer describes the session.
6116+
// Callers record the new selection only once this completes.
6117+
yield* Ref.set(activeSelection, null);
61146118
const requestedModel = flavor.resolveModelId?.(modelSelection) ?? modelSelection.model;
61156119
let appliedModel: string | undefined;
61166120
if (flavor.applyModelSelection !== undefined) {

0 commit comments

Comments
 (0)