Skip to content

Commit b9f889c

Browse files
committed
fix(provider-acp): preserve Grok answers after early task completion
1 parent 32a0531 commit b9f889c

2 files changed

Lines changed: 66 additions & 13 deletions

File tree

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

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8088,7 +8088,7 @@ describe("AcpAdapterV2", () => {
80888088
);
80898089

80908090
it.effect(
8091-
"staggered pre-settle completion keeps midTurn marks until last background task ends post-finalize",
8091+
"staggered pre-settle completion waits for native wake output after the last background task ends",
80928092
() =>
80938093
Effect.gen(function* () {
80948094
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
@@ -8284,12 +8284,19 @@ describe("AcpAdapterV2", () => {
82848284
"mutation-only end-notice must not count as post-settle wake evidence",
82858285
);
82868286
yield* sessionUpdateHandler!(bEndNotice);
8287-
yield* Effect.yieldNow;
8288-
yield* Effect.yieldNow;
8287+
assert.lengthOf(continuationRequests, 0, "a task end notice alone cannot start a wake");
8288+
assert.isTrue(yield* runtime.hasPendingBackgroundWork!);
8289+
yield* sessionUpdateHandler!({
8290+
sessionId: "mock-session-1",
8291+
update: {
8292+
sessionUpdate: "agent_message_chunk",
8293+
content: { type: "text", text: "Both background tasks finished." },
8294+
},
8295+
});
82898296
assert.lengthOf(
82908297
continuationRequests,
82918298
1,
8292-
"exactly one continuation when the last running task ends post-finalize with kept midTurn marks",
8299+
"the native report must start exactly one continuation",
82938300
);
82948301
}).pipe(Effect.provide(layerTest), Effect.scoped),
82958302
);
@@ -8953,6 +8960,11 @@ describe("AcpAdapterV2", () => {
89538960
yield* complete("early");
89548961
assert.lengthOf(continuationRequests, 0);
89558962

8963+
yield* update({
8964+
sessionUpdate: "agent_message_chunk",
8965+
content: { type: "text", text: "EARLY_SUCCESS_DONE" },
8966+
});
8967+
89568968
let backgroundItem: ProviderAdapter.ProviderAdapterV2Event | undefined;
89578969
let checkpoint = 0;
89588970
const assertCommand = Effect.fnUntraced(function* (expectedStatus: string, output: string) {
@@ -9015,14 +9027,55 @@ describe("AcpAdapterV2", () => {
90159027
assert.equal(event.status, "completed");
90169028
break;
90179029
}
9018-
yield* Deferred.await(continuationOffered);
90199030
yield* complete("early");
90209031
yield* complete("blocker");
9032+
assert.lengthOf(
9033+
continuationRequests,
9034+
0,
9035+
"a task completion without native wake output must not replace the root answer",
9036+
);
9037+
assert.isTrue(yield* runtime.hasPendingBackgroundWork!);
9038+
9039+
yield* update({
9040+
sessionUpdate: "agent_thought_chunk",
9041+
content: { type: "text", text: "Preparing the background report" },
9042+
});
9043+
yield* complete("early");
9044+
assert.lengthOf(continuationRequests, 0, "reasoning alone is not a result-bearing wake");
9045+
yield* update({
9046+
sessionUpdate: "agent_message_chunk",
9047+
content: { type: "text", text: "LATE_NATIVE_ANSWER" },
9048+
});
9049+
yield* Deferred.await(continuationOffered);
90219050
assert.lengthOf(
90229051
continuationRequests,
90239052
1,
90249053
"duplicate completions must not offer another continuation",
90259054
);
9055+
yield* runtime.startTurn(
9056+
makeTurnInput({
9057+
threadId,
9058+
providerThread,
9059+
instanceId,
9060+
runtimePolicy,
9061+
now: yield* DateTime.now,
9062+
ordinal: 2,
9063+
messageCreatedBy: "agent",
9064+
messageCreationSource: "provider",
9065+
messageText: "Background task completed.",
9066+
}),
9067+
);
9068+
yield* TestClock.adjust(yield* Deferred.await(finalizeScheduled));
9069+
let nativeAnswerSeen = false;
9070+
while (true) {
9071+
const event = yield* Queue.take(events);
9072+
if (event.type === "message.updated" && event.message.text === "LATE_NATIVE_ANSWER") {
9073+
nativeAnswerSeen = true;
9074+
}
9075+
if (event.type === "turn.terminal") break;
9076+
}
9077+
assert.isTrue(nativeAnswerSeen);
9078+
assert.isFalse(yield* runtime.hasPendingBackgroundWork!);
90269079
}).pipe(Effect.provide(layerTest), Effect.scoped),
90279080
);
90289081

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4635,8 +4635,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
46354635
postSettleContinuationEnabled &&
46364636
(yield* Ref.get(activeSessionId)) === sessionId &&
46374637
(yield* Ref.get(runningBackgroundTaskIds)).size === 0 &&
4638-
((yield* Ref.get(wakeBuffer)).length > 0 ||
4639-
(yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0)
4638+
(yield* Ref.get(wakeBuffer)).some((notification) =>
4639+
acpPostSettleContinuationOfferEvidence(notification, flavor),
4640+
)
46404641
) {
46414642
yield* offerContinuationRun(sessionId);
46424643
}
@@ -7740,18 +7741,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
77407741
settledStatus === "completed" &&
77417742
!directStopQuarantine &&
77427743
(yield* Ref.get(runningBackgroundTaskIds)).size === 0 &&
7743-
((yield* Ref.get(wakeBuffer)).length > 0 ||
7744-
(yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0)
7744+
(yield* Ref.get(wakeBuffer)).some((notification) =>
7745+
acpPostSettleContinuationOfferEvidence(notification, flavor),
7746+
)
77457747
) {
77467748
const sessionId = yield* Ref.get(activeSessionId);
77477749
if (sessionId !== null) {
77487750
yield* offerContinuationRun(sessionId);
77497751
}
77507752
}
7751-
if (
7752-
settledStatus !== "completed" ||
7753-
(yield* Ref.get(runningBackgroundTaskIds)).size === 0
7754-
) {
7753+
if (settledStatus !== "completed") {
77557754
yield* Ref.set(midTurnUnreportedCompletedTaskIds, new Set());
77567755
}
77577756
yield* Deferred.succeed(context.completed, undefined).pipe(Effect.ignore);
@@ -8592,6 +8591,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
85928591
)
85938592
return true;
85948593
if ((yield* Ref.get(wakeBuffer)).length > 0) return true;
8594+
if ((yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0) return true;
85958595
if (yield* Ref.get(continuationRequested)) return true;
85968596
if ((yield* Ref.get(runningBackgroundTaskIds)).size > 0) return true;
85978597
const active = yield* Ref.get(activeTurn);

0 commit comments

Comments
 (0)