Skip to content

Commit 79eb316

Browse files
fix(acp): a late session/load reply defers to startup metadata and session open
The runtime now snapshots its change count before applying buffered startup notifications, so metadata newer than the provisional start drops the late response. The adapter holds an adoption that lands during openSession and applies it once the initial configuration is in place, so the real catalog and the thread's selection are not overwritten by the provisional ones. AI-assisted; verified with the tests and tools listed above.
1 parent 8948ec7 commit 79eb316

4 files changed

Lines changed: 91 additions & 19 deletions

File tree

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3915,7 +3915,7 @@ describe("AcpAdapterV2", () => {
39153915
}).pipe(Effect.provide(layerTest), Effect.scoped),
39163916
);
39173917

3918-
it.live("applies the thread's model on the next turn after a late session/load", () =>
3918+
it.live("applies the thread's model after a late session/load that lands during open", () =>
39193919
Effect.gen(function* () {
39203920
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
39213921
const path = yield* Path.Path;
@@ -3924,7 +3924,10 @@ describe("AcpAdapterV2", () => {
39243924
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
39253925
);
39263926
const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
3927+
const adoptionHandled = yield* Deferred.make<void>();
39273928
const adopted = yield* Deferred.make<void>();
3929+
const publishedConfigurations: Array<ReadonlyArray<EffectAcpSchema.SessionConfigOption>> = [];
3930+
let publications = 0;
39283931
let acpRuntime: AcpSessionRuntime.AcpSessionRuntime["Service"] | undefined;
39293932
const instanceId = ProviderInstanceId.make("acp-test-late-session-load-selection");
39303933
const threadId = ThreadId.make("thread-acp-late-session-load-selection");
@@ -3933,6 +3936,21 @@ describe("AcpAdapterV2", () => {
39333936
flavor: {
39343937
driver: ACP_TEST_DRIVER,
39353938
capabilities: AcpProviderCapabilitiesV2,
3939+
onSessionEvent: (event) =>
3940+
event._tag === "SessionSetupAdopted"
3941+
? Deferred.succeed(adoptionHandled, undefined).pipe(Effect.asVoid)
3942+
: Effect.void,
3943+
// Holds openSession's own publication until the adapter has seen the
3944+
// late response, so adoption overlaps session configuration.
3945+
onSessionConfigurationUpdate: (configOptions) =>
3946+
(publications++ === 0 ? Deferred.await(adoptionHandled) : Effect.void).pipe(
3947+
Effect.andThen(Effect.sync(() => publishedConfigurations.push(configOptions))),
3948+
Effect.andThen(
3949+
configOptions.some((option) => option.category === "model")
3950+
? Deferred.succeed(adopted, undefined)
3951+
: Effect.void,
3952+
),
3953+
),
39363954
makeRuntime: makeMockRuntime({
39373955
childProcessSpawner,
39383956
mockAgentPath,
@@ -3944,19 +3962,7 @@ describe("AcpAdapterV2", () => {
39443962
sessionLoadReplayIdleGap: "100 millis",
39453963
wrapRuntime: (runtime) => {
39463964
acpRuntime = runtime;
3947-
return {
3948-
...runtime,
3949-
getEvents: () =>
3950-
runtime
3951-
.getEvents()
3952-
.pipe(
3953-
Stream.tap((event) =>
3954-
event._tag === "SessionSetupAdopted"
3955-
? Deferred.succeed(adopted, undefined)
3956-
: Effect.void,
3957-
),
3958-
),
3959-
};
3965+
return runtime;
39603966
},
39613967
}),
39623968
},
@@ -4007,6 +4013,10 @@ describe("AcpAdapterV2", () => {
40074013
"session/set_config_option",
40084014
"the selected model was only applied to the provisional setup, which offered no model",
40094015
);
4016+
assert.isTrue(
4017+
publishedConfigurations.at(-1)?.some((option) => option.category === "model"),
4018+
"the provisional catalog was published over the late session/load one",
4019+
);
40104020
}).pipe(Effect.provide(layerTest), Effect.scoped),
40114021
);
40124022

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,48 @@ describe("AcpSessionRuntime", () => {
11951195
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
11961196
);
11971197

1198+
it.live("drops a late session/load response that startup metadata superseded", () =>
1199+
Effect.gen(function* () {
1200+
const provisionalLogged = yield* Deferred.make<void>();
1201+
const allowStartup = yield* Deferred.make<void>();
1202+
const lateLoadLogged = yield* Deferred.make<void>();
1203+
const runtime = yield* AcpSessionRuntime.make({
1204+
...mockRuntimeOptions,
1205+
spawn: {
1206+
...mockRuntimeOptions.spawn,
1207+
env: {
1208+
T3_ACP_DELAY_LOAD_SESSION_AFTER_REPLAY: "1",
1209+
T3_ACP_LOAD_SESSION_DELAY_MS: "500",
1210+
},
1211+
},
1212+
resumeSessionId: "mock-session-1",
1213+
sessionLoadReplayIdleGap: "100 millis",
1214+
requestLogger: (event) => {
1215+
if (event.method !== "session/load" || event.status !== "succeeded") {
1216+
return Effect.void;
1217+
}
1218+
return (event.result as { readonly configOptions?: unknown } | undefined)
1219+
?.configOptions === undefined
1220+
? Deferred.succeed(provisionalLogged, undefined).pipe(
1221+
Effect.andThen(Deferred.await(allowStartup)),
1222+
)
1223+
: Deferred.succeed(lateLoadLogged, undefined).pipe(Effect.asVoid);
1224+
},
1225+
});
1226+
const startup = yield* runtime.start().pipe(Effect.forkChild);
1227+
yield* Deferred.await(provisionalLogged);
1228+
yield* runtime.request("_test/startup-metadata", {});
1229+
yield* Deferred.succeed(allowStartup, undefined);
1230+
yield* Fiber.join(startup);
1231+
yield* Deferred.await(lateLoadLogged).pipe(Effect.timeout("5 seconds"));
1232+
1233+
expect(yield* runtime.getModeState).toMatchObject({ currentModeId: "code" });
1234+
expect(
1235+
(yield* runtime.getConfigOptions).find((option) => option.category === "model"),
1236+
).toMatchObject({ currentValue: "gpt-5.4" });
1237+
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
1238+
);
1239+
11981240
it.effect("keeps active configuration after a candidate session load fails", () =>
11991241
Effect.gen(function* () {
12001242
const runtime = yield* AcpSessionRuntime.AcpSessionRuntime;

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,6 +1998,8 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
19981998
// than this response, so the response is dropped instead of applied.
19991999
const followLateSessionLoad = Effect.fn("AcpSessionRuntime.followLateSessionLoad")(function* (
20002000
started: AcpStartedState,
2001+
// Notifications counted after this baseline are newer than the response.
2002+
provisionalChangeCount?: number,
20012003
) {
20022004
const late = yield* Ref.get(lateSessionLoadRef);
20032005
if (Option.isNone(late) || late.value.provisional !== started.sessionSetupResult) {
@@ -2006,7 +2008,7 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
20062008
const { payload, response: lateFiber } = late.value;
20072009
const provisionalConfigOptions = yield* SubscriptionRef.get(configOptionsRef);
20082010
const provisionalModeState = yield* Ref.get(modeStateRef);
2009-
const provisionalChangeCount = yield* Ref.get(sessionChangeCountRef);
2011+
const baselineChangeCount = provisionalChangeCount ?? (yield* Ref.get(sessionChangeCountRef));
20102012
const adoptLateResponse = Effect.fn("AcpSessionRuntime.adoptLateSessionLoad")(function* (
20112013
response: EffectAcpSchema.LoadSessionResponse,
20122014
) {
@@ -2020,7 +2022,7 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
20202022
(yield* Ref.get(stoppingRef)) ||
20212023
(yield* SubscriptionRef.get(configOptionsRef)) !== provisionalConfigOptions ||
20222024
(yield* Ref.get(modeStateRef)) !== provisionalModeState ||
2023-
(yield* Ref.get(sessionChangeCountRef)) !== provisionalChangeCount
2025+
(yield* Ref.get(sessionChangeCountRef)) !== baselineChangeCount
20242026
) {
20252027
return;
20262028
}
@@ -2477,13 +2479,14 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
24772479
return yield* error.value;
24782480
}
24792481
yield* Ref.set(startStateRef, { _tag: "Started", result });
2482+
const changeCountBeforeMetadata = yield* Ref.get(sessionChangeCountRef);
24802483
const metadata = yield* Ref.getAndSet(startupMetadataRef, []);
24812484
for (const notification of metadata) {
24822485
if (notification.sessionId === result.sessionId) {
24832486
yield* processSessionUpdate(notification);
24842487
}
24852488
}
2486-
yield* followLateSessionLoad(result);
2489+
yield* followLateSessionLoad(result, changeCountBeforeMetadata);
24872490
yield* Deferred.succeed(deferred, result);
24882491
}),
24892492
),
@@ -2618,7 +2621,7 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
26182621
return runLoadSessionWithReplayIdle(requestPayload, started.initializeResult);
26192622
}),
26202623
Effect.flatMap((response) => adoptSession(sessionId, response)),
2621-
Effect.tap(followLateSessionLoad),
2624+
Effect.tap((started) => followLateSessionLoad(started)),
26222625
),
26232626
resumeSession: (sessionId, activationOptions) =>
26242627
start.pipe(

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
22822282
let scheduleDeferredFinalize: (context: ActiveAcpTurn) => Effect.Effect<void> = () =>
22832283
Effect.void;
22842284
let refreshModelSwitchCapability: Effect.Effect<void> = Effect.void;
2285+
// openSession configures the session itself; a late setup that lands
2286+
// first waits here and is adopted once that configuration is in place.
2287+
const lateSessionSetup: {
2288+
openSessionConfigured: boolean;
2289+
pending: Effect.Effect<void> | null;
2290+
} = { openSessionConfigured: false, pending: null };
22852291

22862292
const nativeLogging = options.nativeLogging?.(input.threadId);
22872293
const promptInactivityTimeoutMillis =
@@ -6487,7 +6493,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
64876493
event._tag === "ConfigOptionsUpdated"
64886494
? refreshModelSwitchCapability
64896495
: event._tag === "SessionSetupAdopted"
6490-
? adoptLateSessionSetup(targetRuntime, event.result)
6496+
? Effect.suspend(() => {
6497+
const adopt = adoptLateSessionSetup(targetRuntime, event.result);
6498+
if (lateSessionSetup.openSessionConfigured) return adopt;
6499+
lateSessionSetup.pending = adopt;
6500+
return Effect.void;
6501+
})
64916502
: Effect.void,
64926503
flavor.onSessionEvent?.(event) ?? Effect.void,
64936504
),
@@ -7569,6 +7580,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
75697580
};
75707581
yield* emitProviderEvent({ type: "provider_session.updated", driver, providerSession });
75717582
});
7583+
lateSessionSetup.openSessionConfigured = true;
7584+
const pendingLateSessionSetup = lateSessionSetup.pending;
7585+
lateSessionSetup.pending = null;
7586+
if (pendingLateSessionSetup !== null) {
7587+
yield* pendingLateSessionSetup;
7588+
}
75727589

75737590
const providerTurnPayload = (
75747591
context: ActiveAcpTurn,

0 commit comments

Comments
 (0)