Skip to content

Commit f0a47b3

Browse files
fix(server): ACP reapplies a model after a switch away from it failed partway (pingdotgg#14723)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent ae5fd18 commit f0a47b3

3 files changed

Lines changed: 96 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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import type * as EffectAcpSchema from "effect-acp/compat";
5858
import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
5959
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
6060
import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
61+
import { ACP_SESSION_MODE_OPTION_ID } from "@t3tools/provider-acp/server/sessionConfig";
6162
import {
6263
extractXAiAcpSubagentEndNotice,
6364
extractXAiAcpSubagentUpdate,
@@ -880,6 +881,95 @@ describe("AcpAdapterV2", () => {
880881
}).pipe(Effect.provide(layerTest), Effect.scoped),
881882
);
882883

884+
it.live("reapplies a selection after a switch away from it failed partway", () =>
885+
Effect.gen(function* () {
886+
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
887+
const path = yield* Path.Path;
888+
const mockAgentPath = yield* path.fromFileUrl(
889+
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
890+
);
891+
const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
892+
const instanceId = ProviderInstanceId.make("acp-test-partial-selection-reapply");
893+
const threadId = ThreadId.make("thread-acp-partial-selection-reapply");
894+
const adapter = yield* makeAcpAdapterV2({
895+
instanceId,
896+
flavor: {
897+
driver: ACP_TEST_DRIVER,
898+
capabilities: AcpProviderCapabilitiesV2,
899+
makeRuntime: makeMockRuntime({
900+
childProcessSpawner,
901+
mockAgentPath,
902+
protocolEvents,
903+
// The model switch lands, then the agent rejects the mode step.
904+
environment: { T3_ACP_FAIL_SET_MODE_CONFIG_OPTION: "1" },
905+
}),
906+
},
907+
selfInvocation: yield* resolveSelfInvocation(),
908+
});
909+
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
910+
runtimeMode: "full-access",
911+
interactionMode: "default",
912+
cwd: process.cwd(),
913+
});
914+
const selectionA = { instanceId, model: "composer-2" } as const;
915+
const selectionB = {
916+
instanceId,
917+
model: "composer-2[fast=true]",
918+
options: [{ id: ACP_SESSION_MODE_OPTION_ID, value: "code" }],
919+
} as const;
920+
const runtime = yield* adapter.openSession({
921+
threadId,
922+
providerSessionId: ProviderSessionId.make("provider-session-acp-partial-selection"),
923+
modelSelection: selectionA,
924+
runtimePolicy,
925+
});
926+
const providerThread = yield* runtime.ensureThread({
927+
threadId,
928+
modelSelection: selectionA,
929+
runtimePolicy,
930+
});
931+
const now = yield* DateTime.now;
932+
const turnInput = (ordinal: number, modelSelection: ModelSelection) =>
933+
makeTurnInput({
934+
threadId,
935+
providerThread,
936+
instanceId,
937+
runtimePolicy,
938+
now,
939+
ordinal,
940+
modelSelection,
941+
});
942+
943+
const switchToB = yield* Effect.exit(runtime.startTurn(turnInput(1, selectionB)));
944+
assert.isTrue(Exit.isFailure(switchToB), "the agent rejected B's mode step");
945+
yield* Queue.clear(protocolEvents);
946+
947+
yield* runtime.startTurn(turnInput(2, selectionA));
948+
yield* runtime.events.pipe(
949+
Stream.takeUntil((event) => event.type === "turn.terminal"),
950+
Stream.runDrain,
951+
);
952+
const modelSetsBeforePrompt: Array<unknown> = [];
953+
while (true) {
954+
const event = yield* Queue.take(protocolEvents);
955+
if (event.direction !== "outgoing") continue;
956+
const method = rawProtocolMethod(event);
957+
if (method === "session/prompt") break;
958+
if (
959+
method === "session/set_config_option" &&
960+
rawProtocolRequestParam(event, "configId") === "model"
961+
) {
962+
modelSetsBeforePrompt.push(rawProtocolRequestParam(event, "value"));
963+
}
964+
}
965+
assert.deepEqual(
966+
modelSetsBeforePrompt,
967+
["composer-2"],
968+
"the native model was left on B, so the turn on A must set it back first",
969+
);
970+
}).pipe(Effect.provide(layerTest), Effect.scoped),
971+
);
972+
883973
it.effect("starts a new replay message after ACP v2 plan boundaries", () =>
884974
Effect.gen(function* () {
885975
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6283,6 +6283,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
62836283
modelSelection: ModelSelection,
62846284
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy,
62856285
) {
6286+
// A step failing below leaves the native config partly changed, so
6287+
// the previously applied selection no longer describes the session.
6288+
// Callers record the new selection only once this completes.
6289+
yield* Ref.set(activeSelection, null);
62866290
const requestedModel = flavor.resolveModelId?.(modelSelection) ?? modelSelection.model;
62876291
let appliedModel: string | undefined;
62886292
if (flavor.applyModelSelection !== undefined) {

0 commit comments

Comments
 (0)