The best way to control Claude Code, Codex and more from any computer or phone
fix(provider-acp): Grok early task completion no longer replaces the child's answer (#37162)
Julius Marminge committed
Nov 15, 2026 at 03:46 UTC
649d9fc1ab16fd28f8d927155c4315ab8034963a
2 files changed
+77
-33
apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
+66
-25
@@ -9461,9 +9461,9 @@ describe("AcpAdapterV2", () => {
9461
}).pipe(Effect.provide(layerTest)),
9462
);
9463
9464
- it.effect(
9465
- "staggered pre-settle completion keeps midTurn marks until last background task ends post-finalize",
9466
- () =>
9464
+ it.effect.each(["unpolled", "polled"] as const)(
9465
+ "staggered pre-settle completion waits for native wake output when task B is %s",
9466
+ (taskB) =>
9467
Effect.gen(function* () {
9468
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
9469
const idAllocator = yield* IdAllocator.IdAllocatorV2;
@@ -9615,6 +9615,16 @@ describe("AcpAdapterV2", () => {
9615
0,
9616
"pre-settle unhandled completion must not offer while the turn is active",
9617
);
9618
+ if (taskB === "polled") {
9619
+ yield* sessionUpdateHandler!({
9620
+ sessionId: "mock-session-1",
9621
+ update: {
9622
+ sessionUpdate: "tool_call_update",
9623
+ toolCallId: "tool-call-monitor-b",
9624
+ status: "completed",
9625
+ },
9626
+ });
9627
+ }
9628
9629
yield* Deferred.succeed(promptGate, { stopReason: "end_turn" });
9630
const providerTurnId = idAllocator.derive.providerTurn({
@@ -9632,35 +9642,44 @@ describe("AcpAdapterV2", () => {
9642
assert.lengthOf(
9643
continuationRequests,
9644
0,
9635
- "finalize must not offer while B is still running",
9645
+ "finalize must not offer without native wake output",
9646
);
9647
9638
- const bEndNotice = {
9648
+ if (taskB === "unpolled") {
9649
+ const bEndNotice = {
9650
+ sessionId: "mock-session-1",
9651
+ update: {
9652
+ sessionUpdate: "user_message_chunk" as const,
9653
+ content: {
9654
+ type: "text" as const,
9655
+ text: 'Monitor "task-monitor-b" ended: [monitor ended: exit 0]',
9656
+ },
9657
+ },
9658
+ };
9659
+ assert.isFalse(
9660
+ acpPostSettleWakeEvidence(bEndNotice, {
9661
+ extractBackgroundToolMutation: (text) =>
9662
+ text.includes('Monitor "task-monitor-b" ended')
9663
+ ? [{ taskId: "task-monitor-b", status: "completed", appendOutput: "" }]
9664
+ : [],
9665
+ }),
9666
+ "mutation-only end-notice must not count as post-settle wake evidence",
9667
+ );
9668
+ yield* sessionUpdateHandler!(bEndNotice);
9669
+ assert.lengthOf(continuationRequests, 0, "a task end notice alone cannot start a wake");
9670
+ }
9671
+ assert.isTrue(yield* runtime.hasPendingBackgroundWork!);
9672
+ yield* sessionUpdateHandler!({
9673
sessionId: "mock-session-1",
9674
update: {
9641
- sessionUpdate: "user_message_chunk" as const,
9642
- content: {
9643
- type: "text" as const,
9644
- text: 'Monitor "task-monitor-b" ended: [monitor ended: exit 0]',
9645
- },
9675
+ sessionUpdate: "agent_message_chunk",
9676
+ content: { type: "text", text: "Both background tasks finished." },
9677
},
9647
- };
9648
- assert.isFalse(
9649
- acpPostSettleWakeEvidence(bEndNotice, {
9650
- extractBackgroundToolMutation: (text) =>
9651
- text.includes('Monitor "task-monitor-b" ended')
9652
- ? [{ taskId: "task-monitor-b", status: "completed", appendOutput: "" }]
9653
- : [],
9654
- }),
9655
- "mutation-only end-notice must not count as post-settle wake evidence",
9656
- );
9657
- yield* sessionUpdateHandler!(bEndNotice);
9658
- yield* Effect.yieldNow;
9659
- yield* Effect.yieldNow;
9678
+ });
9679
assert.lengthOf(
9680
continuationRequests,
9681
1,
9663
- "exactly one continuation when the last running task ends post-finalize with kept midTurn marks",
9682
+ "the native report must start exactly one continuation",
9683
);
9684
}).pipe(Effect.provide(layerTest)),
9685
);
@@ -10311,6 +10330,11 @@ describe("AcpAdapterV2", () => {
10330
yield* complete("early");
10331
assert.lengthOf(continuationRequests, 0);
10332
10333
+ yield* update({
10334
+ sessionUpdate: "agent_message_chunk",
10335
+ content: { type: "text", text: "EARLY_SUCCESS_DONE" },
10336
+ });
10337
+
10338
let backgroundItem: ProviderAdapter.ProviderAdapterV2Event | undefined;
10339
let checkpoint = 0;
10340
const assertCommand = Effect.fnUntraced(function* (expectedStatus: string, output: string) {
@@ -10387,9 +10411,26 @@ describe("AcpAdapterV2", () => {
10411
}
10412
break;
10413
}
10390
- yield* Deferred.await(continuationOffered);
10414
yield* complete("early");
10415
yield* complete("blocker");
10416
+ assert.lengthOf(
10417
+ continuationRequests,
10418
+ 0,
10419
+ "a task completion without native wake output must not replace the root answer",
10420
+ );
10421
+ assert.isTrue(yield* runtime.hasPendingBackgroundWork!);
10422
+
10423
+ yield* update({
10424
+ sessionUpdate: "agent_thought_chunk",
10425
+ content: { type: "text", text: "Preparing the background report" },
10426
+ });
10427
+ yield* complete("early");
10428
+ assert.lengthOf(continuationRequests, 0, "reasoning alone is not a result-bearing wake");
10429
+ yield* update({
10430
+ sessionUpdate: "agent_message_chunk",
10431
+ content: { type: "text", text: "LATE_NATIVE_ANSWER" },
10432
+ });
10433
+ yield* Deferred.await(continuationOffered);
10434
assert.lengthOf(
10435
continuationRequests,
10436
1,
packages/provider-acp/src/server/adapter.ts
+11
-8
@@ -4948,8 +4948,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
4948
postSettleContinuationEnabled &&
4949
(yield* Ref.get(activeSessionId)) === sessionId &&
4950
(yield* Ref.get(runningBackgroundTaskIds)).size === 0 &&
4951
- ((yield* Ref.get(wakeBuffer)).length > 0 ||
4952
- (yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0)
4951
+ (yield* Ref.get(wakeBuffer)).some((notification) =>
4952
+ acpPostSettleContinuationOfferEvidence(notification, flavor),
4953
+ )
4954
) {
4955
yield* offerContinuationRun(sessionId);
4956
}
@@ -5069,8 +5070,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
5070
}
5071
const backgroundWorkRunning = (yield* Ref.get(runningBackgroundTaskIds)).size > 0;
5072
const handledInTurnCount = (yield* Ref.get(handledBackgroundTaskIdsInActiveTurn)).size;
5073
+ // Chunks carry no task id: while an unpolled completion awaits its report, keep them.
5074
const isInTurnHandledAgentChatter =
5075
handledInTurnCount > 0 &&
5076
+ (yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size === 0 &&
5077
(update.sessionUpdate === "agent_message_chunk" ||
5078
update.sessionUpdate === "agent_thought_chunk");
5079
// Grok re-prompts for each post-settle monitor event; retaining them would replay the whole burst.
@@ -5401,6 +5404,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
5404
context !== null &&
5405
context.finalized &&
5406
(yield* Ref.get(handledBackgroundTaskIdsInActiveTurn)).size > 0 &&
5407
+ (yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size === 0 &&
5408
(update.sessionUpdate === "agent_message_chunk" ||
5409
update.sessionUpdate === "agent_thought_chunk")
5410
) {
@@ -8186,18 +8190,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
8190
settledStatus === "completed" &&
8191
!directStopQuarantine &&
8192
(yield* Ref.get(runningBackgroundTaskIds)).size === 0 &&
8189
- ((yield* Ref.get(wakeBuffer)).length > 0 ||
8190
- (yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0)
8193
+ (yield* Ref.get(wakeBuffer)).some((notification) =>
8194
+ acpPostSettleContinuationOfferEvidence(notification, flavor),
8195
+ )
8196
) {
8197
const sessionId = yield* Ref.get(activeSessionId);
8198
if (sessionId !== null) {
8199
yield* offerContinuationRun(sessionId);
8200
}
8201
}
8197
- if (
8198
- settledStatus !== "completed" ||
8199
- (yield* Ref.get(runningBackgroundTaskIds)).size === 0
8200
- ) {
8202
+ if (settledStatus !== "completed") {
8203
yield* Ref.set(midTurnUnreportedCompletedTaskIds, new Set());
8204
}
8205
yield* Deferred.succeed(context.completed, undefined).pipe(Effect.ignore);
@@ -9098,6 +9100,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
9100
)
9101
return true;
9102
if ((yield* Ref.get(wakeBuffer)).length > 0) return true;
9103
+ if ((yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0) return true;
9104
if (yield* Ref.get(continuationRequested)) return true;
9105
if ((yield* Ref.get(runningBackgroundTaskIds)).size > 0) return true;
9106
const active = yield* Ref.get(activeTurn);