The best way to control Claude Code, Codex and more from any computer or phone
perf(acp): long ACP threads reopen without per-chunk history copies (#23724)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Victor committed
Nov 14, 2026 at 02:45 UTC
35d01dc291a0552bfd26577d9d6721d18d8f974f
2 files changed
+93
-16
apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
+81
@@ -3180,6 +3180,87 @@ describe("AcpAdapterV2", () => {
3180
}).pipe(Effect.provide(layerTest)),
3181
);
3182
3183
+ it.effect("replays history in order, with stable earlier snapshots", () =>
3184
+ Effect.gen(function* () {
3185
+ const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
3186
+ const path = yield* Path.Path;
3187
+ const selfInvocation = yield* resolveSelfInvocation();
3188
+ const mockAgentPath = yield* path.fromFileUrl(
3189
+ new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
3190
+ );
3191
+ type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"];
3192
+ let sessionUpdateHandler: Parameters<RuntimeService["handleSessionUpdate"]>[0] | undefined;
3193
+ const instanceId = ProviderInstanceId.make("acp-test-v2-replay-order");
3194
+ const adapter = yield* makeAcpAdapterV2({
3195
+ instanceId,
3196
+ flavor: {
3197
+ driver: ACP_TEST_DRIVER,
3198
+ capabilities: AcpProviderCapabilitiesV2,
3199
+ makeRuntime: makeMockRuntime({
3200
+ childProcessSpawner,
3201
+ mockAgentPath,
3202
+ wrapRuntime: (runtime) => ({
3203
+ ...runtime,
3204
+ handleSessionUpdate: (handler) =>
3205
+ Effect.sync(() => {
3206
+ sessionUpdateHandler = handler;
3207
+ }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))),
3208
+ }),
3209
+ }),
3210
+ },
3211
+ selfInvocation,
3212
+ });
3213
+ const threadId = ThreadId.make("thread-acp-v2-replay-order");
3214
+ const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3215
+ runtimeMode: "full-access",
3216
+ interactionMode: "default",
3217
+ cwd: process.cwd(),
3218
+ });
3219
+ const modelSelection = { instanceId, model: "default" } as const;
3220
+ const runtime = yield* adapter.openSession({
3221
+ threadId,
3222
+ providerSessionId: ProviderSessionId.make("provider-session-acp-v2-replay-order"),
3223
+ modelSelection,
3224
+ runtimePolicy,
3225
+ });
3226
+ const providerThread = yield* runtime.ensureThread({
3227
+ threadId,
3228
+ modelSelection,
3229
+ runtimePolicy,
3230
+ });
3231
+ assert.isDefined(sessionUpdateHandler);
3232
+ const replay = (
3233
+ sessionUpdate: "user_message_chunk" | "agent_message_chunk",
3234
+ messageId: string,
3235
+ text: string,
3236
+ ) =>
3237
+ sessionUpdateHandler!({
3238
+ sessionId: "mock-session-1",
3239
+ update: { sessionUpdate, messageId, content: { type: "text", text } },
3240
+ });
3241
+
3242
+ yield* replay("user_message_chunk", "u1", "hel");
3243
+ yield* replay("user_message_chunk", "u1", "lo");
3244
+ yield* replay("agent_message_chunk", "a1", "hi");
3245
+ yield* replay("user_message_chunk", "u2", "next");
3246
+ yield* replay("agent_message_chunk", "a1", " there");
3247
+ const first = yield* runtime.readThreadSnapshot({ providerThread });
3248
+ yield* replay("agent_message_chunk", "a2", "done");
3249
+ yield* replay("user_message_chunk", "u2", " one");
3250
+ const second = yield* runtime.readThreadSnapshot({ providerThread });
3251
+
3252
+ const texts = (snapshot: typeof first) =>
3253
+ snapshot.messages.map((message) => `${message.role}:${message.text}`);
3254
+ assert.deepEqual(texts(first), ["user:hello", "assistant:hi there", "user:next"]);
3255
+ assert.deepEqual(texts(second), [
3256
+ "user:hello",
3257
+ "assistant:hi there",
3258
+ "user:next one",
3259
+ "assistant:done",
3260
+ ]);
3261
+ }).pipe(Effect.provide(layerTest), Effect.scoped),
3262
+ );
3263
+
3264
it.effect("persists a bounded subset of streamed root and child tool updates", () =>
3265
Effect.gen(function* () {
3266
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
packages/provider-acp/src/server/adapter.ts
+12
-16
@@ -1729,6 +1729,7 @@ type PendingRuntimeRequest = {
1729
}
1730
);
1731
1732
+/** Writers update `order` and `messages` in place; readers copy them out. */
1733
interface SnapshotMessageState {
1734
readonly order: Array<string>;
1735
readonly messages: Map<string, OrchestrationV2ConversationMessage>;
@@ -2781,14 +2782,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
2782
const rememberSnapshotMessage = (message: OrchestrationV2ConversationMessage) =>
2783
Ref.update(snapshot, (current) => {
2784
const key = String(message.id);
2784
- const exists = current.messages.has(key);
2785
- const messages = new Map(current.messages);
2786
- messages.set(key, message);
2787
- return {
2788
- ...current,
2789
- order: exists ? current.order : [...current.order, key],
2790
- messages,
2791
- };
2785
+ if (!current.messages.has(key)) current.order.push(key);
2786
+ current.messages.set(key, message);
2787
+ return current;
2788
});
2789
2790
const textStreamFor = (
@@ -4664,8 +4660,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
4660
const messageId = providerMessageId(nativeItemId);
4661
const key = String(messageId);
4662
const previous = current.messages.get(key);
4667
- const messages = new Map(current.messages);
4668
- messages.set(key, {
4663
+ if (previous === undefined) current.order.push(key);
4664
+ current.messages.set(key, {
4665
createdBy: previous?.createdBy ?? (role === "user" ? "user" : "agent"),
4666
creationSource: previous?.creationSource ?? "provider",
4667
id: messageId,
@@ -4680,8 +4676,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
4676
updatedAt: now,
4677
});
4678
return {
4683
- order: current.order.includes(key) ? current.order : [...current.order, key],
4684
- messages,
4679
+ ...current,
4680
reasoningIds:
4681
role === "thought" && !current.reasoningIds.has(messageId)
4682
? new Set(current.reasoningIds).add(messageId)
@@ -9517,6 +9512,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
9512
yield* Ref.set(activeInteractionMode, null);
9513
}
9514
const state = yield* Ref.get(snapshot);
9515
+ const messages = state.order.flatMap((key) => {
9516
+ const message = state.messages.get(key);
9517
+ return message === undefined ? [] : [message];
9518
+ });
9519
const now = yield* DateTime.now;
9520
return {
9521
providerThread: {
@@ -9526,10 +9525,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
9525
updatedAt: now,
9526
},
9527
providerTurns: [...(yield* Ref.get(providerTurns)).values()],
9529
- messages: state.order.flatMap((key) => {
9530
- const message = state.messages.get(key);
9531
- return message === undefined ? [] : [message];
9532
- }),
9528
+ messages,
9529
reasoningMessageIds: [...state.reasoningIds],
9530
runtimeRequests: [],
9531
providerPayload: { protocol: ACP_PROTOCOL, sessionId },