Skip to content

Commit 9fe74cf

Browse files
committed
fix(acp): reconcile live prompt echoes with the submitted message
1 parent 5b35402 commit 9fe74cf

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import * as AcpRegistrySupport from "@t3tools/provider-acp-registry/server/AcpRe
4141
import * as AcpRegistryRuntimeCoordinator from "@t3tools/provider-acp-registry/server/AcpRegistryRuntimeCoordinator";
4242
import { ACP_SESSION_MODE_OPTION_ID } from "@t3tools/provider-acp/server/sessionConfig";
4343
import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
44+
import { t3AcpPromptWithInstructions } from "@t3tools/provider-core/server/orchestrationInstructions";
45+
import { buildRuntimeInstructions } from "@t3tools/provider-core/server/runtimeInstructions";
4446
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
4547
import {
4648
decodeAcpReplayTranscript,
@@ -1007,6 +1009,92 @@ describe("AcpRegistryAdapterV2", () => {
10071009
}).pipe(Effect.provide(layerTest), Effect.scoped),
10081010
);
10091011

1012+
it.effect.each(["before", "after"] as const)(
1013+
"reconciles a full prompt echo %s acknowledgment",
1014+
(ordering) =>
1015+
Effect.gen(function* () {
1016+
const runtimeInstructions = {
1017+
type: "text",
1018+
text: buildRuntimeInstructions({ harness: "acpRegistry", model: "default" }),
1019+
};
1020+
const promptBlocks = (ordinal: number) => [
1021+
{
1022+
type: "text",
1023+
text:
1024+
ordinal === 1
1025+
? t3AcpPromptWithInstructions({
1026+
prompt: "test prompt",
1027+
state: { interactionMode: "default", hasT3Mcp: false },
1028+
})
1029+
: "test prompt",
1030+
},
1031+
runtimeInstructions,
1032+
];
1033+
const echo = (messageId: string, content: ReadonlyArray<unknown>) =>
1034+
claudeFrames.update(CLAUDE_ROOT_SESSION, {
1035+
sessionUpdate: "user_message",
1036+
messageId,
1037+
content,
1038+
});
1039+
const promptText = (ordinal: number) =>
1040+
promptBlocks(ordinal)
1041+
.map((block) => block.text)
1042+
.join("\n");
1043+
const frames = (ordinal: number) => {
1044+
const [request, , acknowledgment] = claudeFrames.prompt(ordinal);
1045+
const matching = echo(`echo-${ordinal}`, promptBlocks(ordinal));
1046+
return [
1047+
request,
1048+
echo("different-blocks", [
1049+
{
1050+
type: "text",
1051+
text: promptText(ordinal),
1052+
},
1053+
]),
1054+
...(ordering === "before" ? [matching, acknowledgment] : [acknowledgment, matching]),
1055+
claudeFrames.update(CLAUDE_ROOT_SESSION, {
1056+
sessionUpdate: "user_message_chunk",
1057+
messageId: `echo-${ordinal}`,
1058+
content: { type: "text", text: "updated echo" },
1059+
}),
1060+
echo(`echo-${ordinal}`, [{ type: "text", text: "updated echo" }]),
1061+
echo("second-id", promptBlocks(ordinal)),
1062+
echo("unmatched", [{ type: "text", text: "Continue the review." }]),
1063+
claudeFrames.idle("end_turn"),
1064+
];
1065+
};
1066+
const session = yield* openClaudeReplaySession(
1067+
[
1068+
...claudeFrames.open,
1069+
...frames(1),
1070+
...frames(2),
1071+
claudeFrames.closeRequest,
1072+
claudeFrames.closed,
1073+
],
1074+
undefined,
1075+
false,
1076+
);
1077+
for (const ordinal of [1, 2]) {
1078+
yield* session.runtime.startTurn(session.turnInput(ordinal));
1079+
const events = yield* session.until((event) => event.type === "turn.terminal");
1080+
const messages = events.flatMap((event) =>
1081+
event.type === "message.updated" && event.message.role === "user"
1082+
? [event.message]
1083+
: [],
1084+
);
1085+
const items = events.flatMap((event) =>
1086+
event.type === "turn_item.updated" && event.turnItem.type === "user_message"
1087+
? [event.turnItem]
1088+
: [],
1089+
);
1090+
const expected = new Set([promptText(ordinal), "Continue the review."]);
1091+
assert.deepEqual(new Set(messages.map((message) => message.text)), expected);
1092+
assert.equal(new Set(messages.map((message) => message.id)).size, 3);
1093+
assert.equal(new Set(items.map((item) => item.id)).size, 3);
1094+
}
1095+
}).pipe(Effect.provide(layerTest), Effect.scoped),
1096+
);
1097+
10101098
it.effect("settles Claude's accepted foreground reply while retaining child output", () =>
10111099
Effect.gen(function* () {
10121100
const gate = makeProviderReplayGate(["accepted", "child-output", "wake"]);

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import * as DateTime from "effect/DateTime";
4444
import * as Deferred from "effect/Deferred";
4545
import type * as Duration from "effect/Duration";
4646
import * as Effect from "effect/Effect";
47+
import * as Equal from "effect/Equal";
4748
import * as Exit from "effect/Exit";
4849
import * as FileSystem from "effect/FileSystem";
4950
import * as Fiber from "effect/Fiber";
@@ -1029,6 +1030,8 @@ interface ActiveAcpTurn {
10291030
readonly startedAt: DateTime.Utc;
10301031
readonly completed: Deferred.Deferred<void, never>;
10311032
readonly itemOrdinals: Map<string, number>;
1033+
readonly outgoingPrompt?: ReadonlyArray<EffectAcpSchema.ContentBlock> | undefined;
1034+
consumedPromptMessageId?: string;
10321035
readonly user: ActiveTextStream;
10331036
readonly assistant: ActiveTextStream;
10341037
readonly reasoning: ActiveTextStream;
@@ -4840,6 +4843,25 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
48404843
) {
48414844
yield* emitProviderRetry(context, "completed");
48424845
}
4846+
if (
4847+
context.outgoingPrompt !== undefined &&
4848+
(update.sessionUpdate === "user_message" ||
4849+
update.sessionUpdate === "user_message_chunk")
4850+
) {
4851+
// ACP v2 prompt acknowledgments lack a messageId; correlate one full echo by content.
4852+
if (
4853+
context.consumedPromptMessageId === undefined &&
4854+
update.sessionUpdate === "user_message" &&
4855+
Equal.equals(update.content, context.outgoingPrompt)
4856+
) {
4857+
context.consumedPromptMessageId = update.messageId;
4858+
}
4859+
if (
4860+
context.consumedPromptMessageId !== undefined &&
4861+
context.consumedPromptMessageId === update.messageId
4862+
)
4863+
return;
4864+
}
48434865
switch (update.sessionUpdate) {
48444866
case "state_update": {
48454867
const toolCallId = `${context.nativeTurnId}:requires-action`;
@@ -7315,6 +7337,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
73157337
startedAt,
73167338
completed,
73177339
itemOrdinals: new Map(),
7340+
outgoingPrompt:
7341+
(yield* Ref.get(activeSessionSetup))?.initializeResult.protocolVersion === 2
7342+
? promptParts?.prompt
7343+
: undefined,
73187344
user: { current: null, nextSegment: 0 },
73197345
assistant: { current: null, nextSegment: 0 },
73207346
reasoning: { current: null, nextSegment: 0 },

0 commit comments

Comments
 (0)