Skip to content

Commit 3cb9e04

Browse files
fix(grok): reject stale queued child result wakes
1 parent 67ce876 commit 3cb9e04

2 files changed

Lines changed: 55 additions & 17 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ describe("Grok child results", () => {
900900
message: {
901901
createdBy: "agent",
902902
creationSource: "server",
903-
messageId: MessageId.make("message-grok-child-continuation"),
903+
messageId: request.messageId!,
904904
text: request.detail!,
905905
attachments: [],
906906
},

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

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import {
44
type ChatAttachment,
55
type ModelSelection,
6-
type MessageId,
6+
MessageId,
77
type OrchestrationV2ConversationMessage,
88
type OrchestrationV2ExecutionNode,
99
type OrchestrationV2PlanArtifact,
@@ -1807,6 +1807,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
18071807
});
18081808
const continuationRequested = yield* Ref.make(false);
18091809
const continuationGeneration = yield* Ref.make(0);
1810+
// The namespace survives owner invalidation so durably queued stale text stays recognizable.
1811+
const textContinuationMessageIdPrefix = `message:acp-text-continuation:${encodeURIComponent(input.providerSessionId)}:`;
18101812
// Native frames can arrive after a text wake is offered; its message owns their drain.
18111813
const textContinuationMessageId = yield* Ref.make<MessageId | null>(null);
18121814
const continuationPermit = yield* Semaphore.make(1);
@@ -3506,7 +3508,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
35063508
threadId: route.threadId,
35073509
ordinal: (yield* Ref.get(snapshot)).order.length + 1,
35083510
})
3509-
.pipe(Effect.orDie);
3511+
.pipe(
3512+
Effect.map((id) =>
3513+
MessageId.make(`${textContinuationMessageIdPrefix}${id}`),
3514+
),
3515+
Effect.orDie,
3516+
);
35103517
yield* Ref.set(textContinuationMessageId, messageId);
35113518
return Option.some({ route, generation, reports, detail, messageId });
35123519
}),
@@ -6589,20 +6596,33 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
65896596
if (!isAppOwnedWakeTurn) {
65906597
yield* Ref.set(midTurnUnreportedCompletedTaskIds, new Set());
65916598
}
6592-
const { continuationWasRequested, isTextContinuationTurn } =
6599+
const { continuationWasRequested, isTextContinuationTurn, isStaleTextTurn } =
65936600
yield* continuationPermit.withPermit(
65946601
Effect.gen(function* () {
65956602
const wasRequested = yield* Ref.get(continuationRequested);
65966603
const textMessageId = yield* Ref.get(textContinuationMessageId);
65976604
const isTextContinuationTurn =
65986605
isAppOwnedWakeTurn && textMessageId === turnInput.message.messageId;
6606+
const isStaleTextTurn =
6607+
isAppOwnedWakeTurn &&
6608+
turnInput.message.messageId.startsWith(textContinuationMessageIdPrefix) &&
6609+
!isTextContinuationTurn;
6610+
if (isStaleTextTurn) {
6611+
return {
6612+
continuationWasRequested: wasRequested,
6613+
isTextContinuationTurn,
6614+
isStaleTextTurn,
6615+
};
6616+
}
65996617
const isUserTurn = !isContinuationTurn && !isAppOwnedWakeTurn;
66006618
const invalidatesTextContinuation = isUserTurn && textMessageId !== null;
6601-
const preserveBufferedContinuation = acpTurnStartShouldPreserveContinuation({
6602-
continuationRequested: wasRequested,
6603-
isContinuationTurn: isContinuationTurn || isTextContinuationTurn,
6604-
wakeBufferLength: (yield* Ref.get(wakeBuffer)).length,
6605-
});
6619+
const preserveBufferedContinuation =
6620+
(isAppOwnedWakeTurn && !isTextContinuationTurn && textMessageId !== null) ||
6621+
acpTurnStartShouldPreserveContinuation({
6622+
continuationRequested: wasRequested,
6623+
isContinuationTurn: isContinuationTurn || isTextContinuationTurn,
6624+
wakeBufferLength: (yield* Ref.get(wakeBuffer)).length,
6625+
});
66066626
if (isUserTurn) {
66076627
yield* Ref.set(pendingWillWakeSubagents, new Map());
66086628
if (!preserveBufferedContinuation) {
@@ -6620,12 +6640,17 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
66206640
yield* Ref.set(continuationRequested, false);
66216641
yield* Ref.set(textContinuationMessageId, null);
66226642
}
6623-
return { continuationWasRequested: wasRequested, isTextContinuationTurn };
6643+
return {
6644+
continuationWasRequested: wasRequested,
6645+
isTextContinuationTurn,
6646+
isStaleTextTurn,
6647+
};
66246648
}),
66256649
);
6626-
const promptParts = isContinuationTurn
6627-
? null
6628-
: yield* resolvePromptParts(turnInput, requestedSessionId);
6650+
const promptParts =
6651+
isContinuationTurn || isStaleTextTurn
6652+
? null
6653+
: yield* resolvePromptParts(turnInput, requestedSessionId);
66296654
const startedAt = yield* DateTime.now;
66306655
const nativeTurnId = `${requestedSessionId}:turn:${turnInput.providerTurnOrdinal}`;
66316656
const providerTurnId = deriveProviderTurnId(nativeTurnId);
@@ -6751,11 +6776,24 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
67516776
yield* projectDeferredCarryoverTerminals(rehydratedCarryoverSubagents);
67526777
// Keep projected terminals addressable through the wake drain so
67536778
// emitSubagent's monotonic guard can reject replayed spawn frames.
6779+
if (isStaleTextTurn) {
6780+
yield* finalizeTurn(context, "completed");
6781+
return;
6782+
}
67546783
if (isContinuationTurn || isTextContinuationTurn) {
6755-
yield* Ref.update(wakeReports, ({ reports, offered }) => ({
6756-
reports: new Map([...reports].filter(([key]) => !offered.has(key))),
6757-
offered: noWakeReports.offered,
6758-
}));
6784+
yield* Ref.set(midTurnUnreportedCompletedTaskIds, new Set());
6785+
yield* continuationPermit.withPermit(
6786+
Effect.gen(function* () {
6787+
// Cancel native offers made during setup; a newer text offer owns separate detail.
6788+
if ((yield* Ref.get(textContinuationMessageId)) !== null) return;
6789+
yield* Ref.update(continuationGeneration, (value) => value + 1);
6790+
yield* Ref.set(continuationRequested, false);
6791+
yield* Ref.update(wakeReports, ({ reports, offered }) => ({
6792+
reports: new Map([...reports].filter(([key]) => !offered.has(key))),
6793+
offered: noWakeReports.offered,
6794+
}));
6795+
}),
6796+
);
67596797
const drainedWakeCount = yield* Ref.modify(wakeBuffer, (current) => {
67606798
const next: Array<EffectAcpSchema.SessionNotification> = [];
67616799
return [

0 commit comments

Comments
 (0)