Skip to content

Commit 9d6926b

Browse files
fix(acp): a selection sent before a late session/load reply keeps it from being adopted
The late-response guard only saw mode/config writes once the agent answered them, so an agent that serves requests in order could have its stale load response adopted over a pending set_mode/set_config_option/set_model. Count those writes when they are sent and drop the late response if any were sent after the provisional start. The opening model-switch capability also keeps the negotiated value instead of possibly opening lower. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent edc4496 commit 9d6926b

4 files changed

Lines changed: 78 additions & 8 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ const emitLoadReplay = process.env.T3_ACP_EMIT_LOAD_REPLAY === "1";
8787
const hangLoadSessionAfterReplay = process.env.T3_ACP_HANG_LOAD_SESSION_AFTER_REPLAY === "1";
8888
const delayLoadSessionAfterReplay = process.env.T3_ACP_DELAY_LOAD_SESSION_AFTER_REPLAY === "1";
8989
const loadSessionDelayMs = Number(process.env.T3_ACP_LOAD_SESSION_DELAY_MS ?? "5000");
90+
const holdSetConfigOptionUntilLoadReply =
91+
process.env.T3_ACP_HOLD_SET_CONFIG_OPTION_UNTIL_LOAD_REPLY === "1";
9092
const emitStaleXAiPromptCompleteBeforeSecondHang =
9193
process.env.T3_ACP_EMIT_STALE_XAI_PROMPT_COMPLETE_BEFORE_SECOND_HANG === "1";
9294
const emitOverlappingXAiPromptCompleteOutOfOrder =
@@ -533,6 +535,7 @@ const program = Effect.gen(function* () {
533535
const resumeRelease = yield* Deferred.make<void>();
534536
const nativeCancelRequested = yield* Deferred.make<void>();
535537
const nativeCancelRelease = yield* Deferred.make<void>();
538+
const delayedLoadReplied = yield* Deferred.make<void>();
536539
const publishAntigravityCommands = (targetSessionId: string) =>
537540
agent.client.sessionUpdate({
538541
sessionId: targetSessionId,
@@ -764,6 +767,7 @@ const program = Effect.gen(function* () {
764767
// response was built before later selections landed.
765768
const loadedConfigOptions = configOptions();
766769
yield* Effect.sleep(loadSessionDelayMs);
770+
yield* Deferred.succeed(delayedLoadReplied, undefined);
767771
return {
768772
configOptions: loadedConfigOptions,
769773
};
@@ -861,6 +865,12 @@ const program = Effect.gen(function* () {
861865

862866
yield* agent.handleSetSessionConfigOption((request) =>
863867
Effect.gen(function* () {
868+
if (holdSetConfigOptionUntilLoadReply) {
869+
// Like an agent that serves requests in order: the delayed load reply
870+
// goes out first, then this one.
871+
yield* Deferred.await(delayedLoadReplied);
872+
yield* Effect.sleep(100);
873+
}
864874
if (exitOnSetConfigOption) {
865875
return yield* Effect.sync(() => {
866876
process.exit(7);

‎apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,41 @@ describe("AcpSessionRuntime", () => {
11011101
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
11021102
);
11031103

1104+
it.live("drops a late session/load response when a selection was sent before it", () =>
1105+
Effect.gen(function* () {
1106+
const lateLoadLogged = yield* Deferred.make<void>();
1107+
const runtime = yield* AcpSessionRuntime.make({
1108+
...mockRuntimeOptions,
1109+
spawn: {
1110+
...mockRuntimeOptions.spawn,
1111+
env: {
1112+
T3_ACP_DELAY_LOAD_SESSION_AFTER_REPLAY: "1",
1113+
T3_ACP_LOAD_SESSION_DELAY_MS: "500",
1114+
T3_ACP_HOLD_SET_CONFIG_OPTION_UNTIL_LOAD_REPLY: "1",
1115+
},
1116+
},
1117+
resumeSessionId: "mock-session-1",
1118+
sessionLoadReplayIdleGap: "100 millis",
1119+
requestLogger: (event) =>
1120+
event.method === "session/load" &&
1121+
event.status === "succeeded" &&
1122+
(event.result as { readonly configOptions?: unknown } | undefined)?.configOptions !==
1123+
undefined
1124+
? Deferred.succeed(lateLoadLogged, undefined).pipe(Effect.asVoid)
1125+
: Effect.void,
1126+
});
1127+
const started = yield* runtime.start();
1128+
// The agent answers the load before this request, so no config change
1129+
// is visible when the late response arrives.
1130+
yield* runtime.setConfigOption("model", "gpt-5.4");
1131+
yield* Deferred.await(lateLoadLogged).pipe(Effect.timeout("5 seconds"));
1132+
1133+
expect((yield* runtime.start()).sessionSetupResult).toBe(started.sessionSetupResult);
1134+
const model = (yield* runtime.getConfigOptions).find((option) => option.id === "model");
1135+
expect(model?.currentValue).toBe("gpt-5.4");
1136+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1137+
);
1138+
11041139
it.effect("keeps active configuration after a candidate session load fails", () =>
11051140
Effect.gen(function* () {
11061141
const runtime = yield* AcpSessionRuntime.AcpSessionRuntime;

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,10 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
13201320
>;
13211321
}>
13221322
>(Option.none());
1323+
// Counts mode, config, and model writes sent to the agent. A late
1324+
// session/load response predates any write sent after the provisional
1325+
// start, even one whose reply is still pending.
1326+
const sessionWriteCountRef = yield* Ref.make(0);
13231327

13241328
const ensureConnected = Effect.gen(function* () {
13251329
const error = yield* Ref.get(terminationErrorRef);
@@ -1379,6 +1383,15 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
13791383
const logRequest = (event: AcpSessionRequestLogEvent) =>
13801384
options.requestLogger ? options.requestLogger(event) : Effect.void;
13811385

1386+
const runLoggedSessionWrite = <A>(
1387+
method: string,
1388+
payload: unknown,
1389+
effect: Effect.Effect<A, EffectAcpErrors.AcpError>,
1390+
): Effect.Effect<A, EffectAcpErrors.AcpError> =>
1391+
Ref.update(sessionWriteCountRef, (count) => count + 1).pipe(
1392+
Effect.andThen(runLoggedRequest(method, payload, effect)),
1393+
);
1394+
13821395
const runLoggedRequest = <A>(
13831396
method: string,
13841397
payload: unknown,
@@ -1974,6 +1987,7 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
19741987
const { payload, response: lateFiber } = late.value;
19751988
const provisionalConfigOptions = yield* Ref.get(configOptionsRef);
19761989
const provisionalModeState = yield* Ref.get(modeStateRef);
1990+
const provisionalWriteCount = yield* Ref.get(sessionWriteCountRef);
19771991
const adoptLateResponse = Effect.fn("AcpSessionRuntime.adoptLateSessionLoad")(function* (
19781992
response: EffectAcpSchema.LoadSessionResponse,
19791993
) {
@@ -1986,7 +2000,8 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
19862000
startState.result !== started ||
19872001
(yield* Ref.get(stoppingRef)) ||
19882002
(yield* Ref.get(configOptionsRef)) !== provisionalConfigOptions ||
1989-
(yield* Ref.get(modeStateRef)) !== provisionalModeState
2003+
(yield* Ref.get(modeStateRef)) !== provisionalModeState ||
2004+
(yield* Ref.get(sessionWriteCountRef)) !== provisionalWriteCount
19902005
) {
19912006
return;
19922007
}
@@ -2185,7 +2200,7 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
21852200
configId,
21862201
value: String(value),
21872202
} satisfies EffectAcpSchema.SetSessionConfigOptionRequest);
2188-
return runLoggedRequest(
2203+
return runLoggedSessionWrite(
21892204
"session/set_config_option",
21902205
requestPayload,
21912206
acp.agent.setSessionConfigOption(requestPayload),
@@ -2791,7 +2806,11 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
27912806
if (modeConfigOption === undefined && modeState !== undefined) {
27922807
const started = yield* getStartedState;
27932808
const payload = { sessionId: started.sessionId, modeId };
2794-
yield* runLoggedRequest("session/set_mode", payload, acp.agent.setSessionMode(payload));
2809+
yield* runLoggedSessionWrite(
2810+
"session/set_mode",
2811+
payload,
2812+
acp.agent.setSessionMode(payload),
2813+
);
27952814
yield* updateCurrentModeId(modeId);
27962815
return {} satisfies EffectAcpSchema.SetSessionModeResponse;
27972816
}
@@ -2812,7 +2831,11 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
28122831
modelId,
28132832
...(meta === undefined ? {} : { _meta: meta }),
28142833
};
2815-
return runLoggedRequest("session/set_model", payload, acp.agent.setSessionModel(payload));
2834+
return runLoggedSessionWrite(
2835+
"session/set_model",
2836+
payload,
2837+
acp.agent.setSessionModel(payload),
2838+
);
28162839
}),
28172840
),
28182841
setConfigOption,

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6914,10 +6914,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
69146914
...capabilities,
69156915
sessions: {
69166916
...capabilities.sessions,
6917-
supportsModelSwitchInSession: canSwitchModelInSession(
6918-
yield* runtime.getConfigOptions,
6919-
(yield* Ref.get(activeSessionSetup))?.sessionSetupResult.models,
6920-
),
6917+
supportsModelSwitchInSession:
6918+
capabilities.sessions.supportsModelSwitchInSession ||
6919+
canSwitchModelInSession(
6920+
yield* runtime.getConfigOptions,
6921+
(yield* Ref.get(activeSessionSetup))?.sessionSetupResult.models,
6922+
),
69216923
},
69226924
},
69236925
createdAt,

0 commit comments

Comments
 (0)