Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 58 additions & 5 deletions apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8088,7 +8088,7 @@ describe("AcpAdapterV2", () => {
);

it.effect(
"staggered pre-settle completion keeps midTurn marks until last background task ends post-finalize",
"staggered pre-settle completion waits for native wake output after the last background task ends",
() =>
Effect.gen(function* () {
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
Expand Down Expand Up @@ -8284,12 +8284,19 @@ describe("AcpAdapterV2", () => {
"mutation-only end-notice must not count as post-settle wake evidence",
);
yield* sessionUpdateHandler!(bEndNotice);
yield* Effect.yieldNow;
yield* Effect.yieldNow;
assert.lengthOf(continuationRequests, 0, "a task end notice alone cannot start a wake");
assert.isTrue(yield* runtime.hasPendingBackgroundWork!);
yield* sessionUpdateHandler!({
sessionId: "mock-session-1",
update: {
sessionUpdate: "agent_message_chunk",
content: { type: "text", text: "Both background tasks finished." },
},
});
assert.lengthOf(
continuationRequests,
1,
"exactly one continuation when the last running task ends post-finalize with kept midTurn marks",
"the native report must start exactly one continuation",
);
}).pipe(Effect.provide(layerTest), Effect.scoped),
);
Expand Down Expand Up @@ -8953,6 +8960,11 @@ describe("AcpAdapterV2", () => {
yield* complete("early");
assert.lengthOf(continuationRequests, 0);

yield* update({
sessionUpdate: "agent_message_chunk",
content: { type: "text", text: "EARLY_SUCCESS_DONE" },
});

let backgroundItem: ProviderAdapter.ProviderAdapterV2Event | undefined;
let checkpoint = 0;
const assertCommand = Effect.fnUntraced(function* (expectedStatus: string, output: string) {
Expand Down Expand Up @@ -9015,14 +9027,55 @@ describe("AcpAdapterV2", () => {
assert.equal(event.status, "completed");
break;
}
yield* Deferred.await(continuationOffered);
yield* complete("early");
yield* complete("blocker");
assert.lengthOf(
continuationRequests,
0,
"a task completion without native wake output must not replace the root answer",
);
assert.isTrue(yield* runtime.hasPendingBackgroundWork!);

yield* update({
sessionUpdate: "agent_thought_chunk",
content: { type: "text", text: "Preparing the background report" },
});
yield* complete("early");
assert.lengthOf(continuationRequests, 0, "reasoning alone is not a result-bearing wake");
yield* update({
sessionUpdate: "agent_message_chunk",
content: { type: "text", text: "LATE_NATIVE_ANSWER" },
});
yield* Deferred.await(continuationOffered);
assert.lengthOf(
continuationRequests,
1,
"duplicate completions must not offer another continuation",
);
yield* runtime.startTurn(
makeTurnInput({
threadId,
providerThread,
instanceId,
runtimePolicy,
now: yield* DateTime.now,
ordinal: 2,
messageCreatedBy: "agent",
messageCreationSource: "provider",
messageText: "Background task completed.",
}),
);
yield* TestClock.adjust(yield* Deferred.await(finalizeScheduled));
let nativeAnswerSeen = false;
while (true) {
const event = yield* Queue.take(events);
if (event.type === "message.updated" && event.message.text === "LATE_NATIVE_ANSWER") {
nativeAnswerSeen = true;
}
if (event.type === "turn.terminal") break;
}
assert.isTrue(nativeAnswerSeen);
assert.isFalse(yield* runtime.hasPendingBackgroundWork!);
}).pipe(Effect.provide(layerTest), Effect.scoped),
);

Expand Down
16 changes: 8 additions & 8 deletions packages/provider-acp/src/server/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4635,8 +4635,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
postSettleContinuationEnabled &&
(yield* Ref.get(activeSessionId)) === sessionId &&
(yield* Ref.get(runningBackgroundTaskIds)).size === 0 &&
((yield* Ref.get(wakeBuffer)).length > 0 ||
(yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0)
(yield* Ref.get(wakeBuffer)).some((notification) =>
acpPostSettleContinuationOfferEvidence(notification, flavor),
)
) {
yield* offerContinuationRun(sessionId);
}
Expand Down Expand Up @@ -7740,18 +7741,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
settledStatus === "completed" &&
!directStopQuarantine &&
(yield* Ref.get(runningBackgroundTaskIds)).size === 0 &&
((yield* Ref.get(wakeBuffer)).length > 0 ||
(yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0)
(yield* Ref.get(wakeBuffer)).some((notification) =>
acpPostSettleContinuationOfferEvidence(notification, flavor),
)
) {
const sessionId = yield* Ref.get(activeSessionId);
if (sessionId !== null) {
yield* offerContinuationRun(sessionId);
}
}
if (
settledStatus !== "completed" ||
(yield* Ref.get(runningBackgroundTaskIds)).size === 0
) {
if (settledStatus !== "completed") {
yield* Ref.set(midTurnUnreportedCompletedTaskIds, new Set());
}
yield* Deferred.succeed(context.completed, undefined).pipe(Effect.ignore);
Expand Down Expand Up @@ -8592,6 +8591,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
)
return true;
if ((yield* Ref.get(wakeBuffer)).length > 0) return true;
if ((yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0) return true;
if (yield* Ref.get(continuationRequested)) return true;
if ((yield* Ref.get(runningBackgroundTaskIds)).size > 0) return true;
const active = yield* Ref.get(activeTurn);
Expand Down
Loading