Skip to content

Commit 4dcea59

Browse files
committed
fix(acp): keep Copilot replies and wake state in their own turns
1 parent 8ce7bba commit 4dcea59

2 files changed

Lines changed: 76 additions & 24 deletions

File tree

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

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3618,6 +3618,7 @@ describe("AcpAdapterV2", () => {
36183618
stateOnlyWake: false,
36193619
queuedWake: false,
36203620
stillRunning: false,
3621+
steered: false,
36213622
},
36223623
{
36233624
name: "image-only input",
@@ -3626,6 +3627,7 @@ describe("AcpAdapterV2", () => {
36263627
stateOnlyWake: false,
36273628
queuedWake: false,
36283629
stillRunning: false,
3630+
steered: false,
36293631
},
36303632
{
36313633
name: "an unmatched message",
@@ -3634,6 +3636,7 @@ describe("AcpAdapterV2", () => {
36343636
stateOnlyWake: false,
36353637
queuedWake: false,
36363638
stillRunning: false,
3639+
steered: false,
36373640
},
36383641
{
36393642
name: "a state-only pre-acceptance wake",
@@ -3642,6 +3645,7 @@ describe("AcpAdapterV2", () => {
36423645
stateOnlyWake: true,
36433646
queuedWake: false,
36443647
stillRunning: false,
3648+
steered: false,
36453649
},
36463650
{
36473651
name: "an unmatched echo overtaking a queued wake",
@@ -3650,6 +3654,7 @@ describe("AcpAdapterV2", () => {
36503654
stateOnlyWake: false,
36513655
queuedWake: true,
36523656
stillRunning: false,
3657+
steered: false,
36533658
},
36543659
{
36553660
name: "a queued wake followed by newer running work",
@@ -3658,10 +3663,20 @@ describe("AcpAdapterV2", () => {
36583663
stateOnlyWake: false,
36593664
queuedWake: true,
36603665
stillRunning: true,
3666+
steered: false,
3667+
},
3668+
{
3669+
name: "a queued wake with steering acceptance",
3670+
imageOnly: false,
3671+
matches: true,
3672+
stateOnlyWake: false,
3673+
queuedWake: true,
3674+
stillRunning: false,
3675+
steered: true,
36613676
},
36623677
])(
36633678
"handles Copilot acceptance for $name without a spurious continuation",
3664-
({ imageOnly, matches, stateOnlyWake, queuedWake, stillRunning }) =>
3679+
({ imageOnly, matches, stateOnlyWake, queuedWake, stillRunning, steered }) =>
36653680
Effect.gen(function* () {
36663681
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
36673682
const path = yield* Path.Path;
@@ -3803,7 +3818,8 @@ describe("AcpAdapterV2", () => {
38033818
},
38043819
});
38053820
yield* Queue.take(dispatched);
3806-
if (queuedWake) yield* event!({ sessionId: "mock-session-1", type: "assistant.idle" });
3821+
if (queuedWake && !steered)
3822+
yield* event!({ sessionId: "mock-session-1", type: "assistant.idle" });
38073823
if (stateOnlyWake) {
38083824
yield* event!({ sessionId: "mock-session-1", type: "assistant.turn_start" });
38093825
yield* event!({ sessionId: "mock-session-1", type: "assistant.idle" });
@@ -3836,16 +3852,15 @@ describe("AcpAdapterV2", () => {
38363852
Stream.runCollect,
38373853
),
38383854
);
3839-
if (!queuedWake)
3840-
assert.isTrue(
3841-
observed.some(
3842-
(value) =>
3843-
value.type === "message.updated" &&
3844-
value.message.role === "assistant" &&
3845-
value.message.runId === input.runId &&
3846-
value.message.text === "answer",
3847-
),
3848-
);
3855+
assert.isTrue(
3856+
observed.some(
3857+
(value) =>
3858+
value.type === "message.updated" &&
3859+
value.message.role === "assistant" &&
3860+
value.message.runId === input.runId &&
3861+
value.message.text === "answer",
3862+
),
3863+
);
38493864
assert.deepEqual(
38503865
offered,
38513866
queuedWake ? [String(threadId)] : [],
@@ -3894,6 +3909,15 @@ describe("AcpAdapterV2", () => {
38943909
message.text.includes("queued wake output"),
38953910
),
38963911
);
3912+
assert.isFalse(
3913+
completed.messages.some(
3914+
(message) =>
3915+
message.runId === continuation.runId &&
3916+
message.role === "assistant" &&
3917+
message.text.includes("answer"),
3918+
),
3919+
"the user's answer must not appear in the background continuation",
3920+
);
38973921
if (stillRunning)
38983922
assert.isTrue(
38993923
completed.messages.some(

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

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,7 @@ interface ActiveAcpTurn {
14081408
promptSettledStatus: "completed" | "interrupted" | "failed" | "cancelled" | null;
14091409
workAccepted: boolean;
14101410
readonly promptAcceptanceText: string | null;
1411+
readonly continuationBufferLength: number;
14111412
readonly promptWireSettled: Deferred.Deferred<void, never>;
14121413
/** Completed once `session/prompt` is the runtime's active prompt. */
14131414
readonly promptDispatched: Deferred.Deferred<void, never>;
@@ -1906,6 +1907,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
19061907
EffectAcpSchema.SessionUpdate,
19071908
{ sessionUpdate: "state_update" }
19081909
> | null = null;
1910+
// Replay mutates agentWork; retain the newest native foreground state separately.
1911+
let foregroundWork: Extract<
1912+
EffectAcpSchema.SessionUpdate,
1913+
{ sessionUpdate: "state_update" }
1914+
> | null = null;
19091915
const agentRequests = new Set<object>();
19101916
const nativeChildren = new Map<string, AcpNativeChildSession>();
19111917
const extractSubagentUpdate = (tool: AcpToolCallState) =>
@@ -1942,6 +1948,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
19421948
flavor.followAgentWorkState === true &&
19431949
setup.initializeResult.protocolVersion === 2;
19441950
agentWork = null;
1951+
foregroundWork = null;
19451952
agentRequests.clear();
19461953
nativeChildren.clear();
19471954
yield* Ref.set(activeSessionSetup, setup);
@@ -6846,6 +6853,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
68466853
) {
68476854
context.workAccepted = true;
68486855
agentWork = { sessionUpdate: "state_update", state: "running" };
6856+
foregroundWork = agentWork;
68496857
const continuationAlreadyRequested = yield* Ref.get(continuationRequested);
68506858
yield* Ref.update(wakeBuffer, (buffer) =>
68516859
!continuationAlreadyRequested &&
@@ -6859,15 +6867,17 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
68596867
}
68606868
return;
68616869
}
6870+
const update = {
6871+
sessionUpdate: "state_update" as const,
6872+
state: notice.state,
6873+
...(notice.state === "idle" && notice.stopReason !== undefined
6874+
? { stopReason: notice.stopReason }
6875+
: {}),
6876+
};
6877+
foregroundWork = update;
68626878
yield* handleSessionUpdate({
68636879
sessionId: notice.sessionId,
6864-
update: {
6865-
sessionUpdate: "state_update",
6866-
state: notice.state,
6867-
...(notice.state === "idle" && notice.stopReason !== undefined
6868-
? { stopReason: notice.stopReason }
6869-
: {}),
6870-
},
6880+
update,
68716881
});
68726882
}),
68736883
).pipe(
@@ -8297,6 +8307,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
82978307
promptSettled: false,
82988308
promptSettledStatus: null,
82998309
workAccepted: false,
8310+
continuationBufferLength: continuationWasRequested
8311+
? (yield* Ref.get(wakeBuffer)).length
8312+
: 0,
83008313
promptAcceptanceText:
83018314
promptParts?.prompt.find((block) => block.type === "text")?.text ?? null,
83028315
promptWireSettled,
@@ -8400,14 +8413,20 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
84008413
yield* runtimeCallbackPermit.withPermit(
84018414
Effect.gen(function* () {
84028415
const liveWork = agentWork;
8416+
const latestWork = canFollowForegroundEvents ? foregroundWork : liveWork;
84038417
const buffered = yield* takeWakeBuffer;
84048418
yield* Effect.forEach(buffered, handleSessionUpdate, { discard: true });
84058419
const bufferedWork = canFollowForegroundEvents
84068420
? buffered.findLast((item) => item.update.sessionUpdate === "state_update")
84078421
?.update
84088422
: undefined;
8423+
// A later user's cancelled idle cannot cancel a wake that already ended.
84098424
agentWork =
8410-
bufferedWork?.sessionUpdate === "state_update" ? bufferedWork : liveWork;
8425+
latestWork?.state === "idle" &&
8426+
bufferedWork?.sessionUpdate === "state_update" &&
8427+
bufferedWork.state === "idle"
8428+
? bufferedWork
8429+
: latestWork;
84118430
context.workAccepted = true;
84128431
yield* settleIdleWork(context);
84138432
agentWork = liveWork;
@@ -8523,12 +8542,21 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
85238542
return;
85248543
if (canFollowForegroundEvents && !context.workAccepted) {
85258544
context.workAccepted = true;
8526-
if (!(yield* Ref.get(continuationRequested))) {
8527-
const buffered = yield* Ref.getAndSet(wakeBuffer, []);
8528-
for (const notification of buffered) {
8545+
const preservedLength = (yield* Ref.get(continuationRequested))
8546+
? context.continuationBufferLength
8547+
: 0;
8548+
const buffered = yield* Ref.modify(wakeBuffer, (current) => [
8549+
current.slice(preservedLength),
8550+
current.slice(0, preservedLength),
8551+
]);
8552+
for (const notification of buffered) {
8553+
if (notification.update.sessionUpdate !== "state_update")
85298554
yield* handleSessionUpdate(notification);
8530-
}
85318555
}
8556+
const latestState = buffered.findLast(
8557+
(notification) => notification.update.sessionUpdate === "state_update",
8558+
);
8559+
if (latestState !== undefined) yield* handleSessionUpdate(latestState);
85328560
if (context.finalized) return;
85338561
}
85348562
// ACP v2 reports a failed prompt as an idle `error`, not an RPC error.

0 commit comments

Comments
 (0)