From f4176b7fe14282117af78c31d2a7f7e37d88c428 Mon Sep 17 00:00:00 2001 From: christopher pellegrini <106196537+mrtoaf@users.noreply.github.com> Date: Sun, 22 Nov 2026 04:29:22 -0600 Subject: [PATCH] fix(acp): harden prompt suggestion handoff stripping, continuation check, and session-switch cancellation --- .../Adapters/AntigravityAdapterV2.test.ts | 100 ++++++++++++++---- packages/provider-acp/src/server/adapter.ts | 71 ++++++++----- 2 files changed, 125 insertions(+), 46 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index 21de67ad95d23..c328d647e2c72 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts @@ -2569,8 +2569,8 @@ describe("AntigravityAdapterV2 prompt suggestions", () => { } return startResult; }), - loadSession: () => Effect.succeed(startResult), - resumeSession: () => Effect.succeed(startResult), + loadSession: (sessionId) => Effect.succeed({ ...startResult, sessionId }), + resumeSession: (sessionId) => Effect.succeed({ ...startResult, sessionId }), forkSession: () => Effect.succeed(startResult), listSessions: () => Effect.succeed({ sessions: [] }), closeSession: () => Effect.succeed({}), @@ -2678,6 +2678,7 @@ describe("AntigravityAdapterV2 prompt suggestions", () => { ordinal: number, overrides?: { readonly text?: string; + readonly inlineContextLength?: number; readonly delegated?: boolean; readonly continuation?: boolean; }, @@ -2693,6 +2694,9 @@ describe("AntigravityAdapterV2 prompt suggestions", () => { }); return { ...turn, + ...(overrides?.inlineContextLength === undefined + ? {} + : { inlineContextLength: overrides.inlineContextLength }), runId: RunId.make(`run-suggestion-${ordinal}`), runOrdinal: ordinal, providerTurnOrdinal: ordinal, @@ -2749,7 +2753,11 @@ describe("AntigravityAdapterV2 prompt suggestions", () => { }), }); - const turn1 = makeTurn(1, { text: "Add a subtract function." }); + const handoffPrefix = "Earlier handoff summary\n\n"; + const turn1 = makeTurn(1, { + text: `${handoffPrefix}Add a subtract function.`, + inlineContextLength: handoffPrefix.length, + }); yield* session.startTurn(turn1); const events1 = yield* until((event) => event.type === "prompt_suggestion.updated"); const suggestionEvent = events1.find((event) => event.type === "prompt_suggestion.updated"); @@ -2770,11 +2778,22 @@ describe("AntigravityAdapterV2 prompt suggestions", () => { }, ]); - yield* Ref.set(enabled, false); - const turn2 = makeTurn(2, { text: "Now add divide." }); + // Later turns still strip the earlier handoff turn's inline context prefix. + const turn2 = makeTurn(2, { text: "Now add multiply." }); yield* session.startTurn(turn2); + yield* until((event) => event.type === "prompt_suggestion.updated"); + assert.deepEqual(sampled[1]?.messages, [ + { role: "user", text: "Add a subtract function." }, + { role: "assistant", text: "Added subtract in math.ts." }, + { role: "user", text: "Now add multiply." }, + { role: "assistant", text: "Added subtract in math.ts." }, + ]); + + yield* Ref.set(enabled, false); + const turn3 = makeTurn(3, { text: "Now add divide." }); + yield* session.startTurn(turn3); yield* until((event) => event.type === "turn.terminal"); - assert.equal(sampled.length, 1); + assert.equal(sampled.length, 2); }).pipe(Effect.provide(layerSession)), ); @@ -2801,7 +2820,7 @@ describe("AntigravityAdapterV2 prompt suggestions", () => { ); it.effect( - "cancels an in-flight suggestion on next turn, rollback, forced session replacement, or admitted continuation", + "cancels an in-flight suggestion on next turn, rollback, forced session replacement, resume, snapshot switch, or admitted continuation", () => Effect.gen(function* () { const started = [ @@ -2809,12 +2828,16 @@ describe("AntigravityAdapterV2 prompt suggestions", () => { yield* Deferred.make(), yield* Deferred.make(), yield* Deferred.make(), + yield* Deferred.make(), + yield* Deferred.make(), ] as const; const interrupted = [ yield* Deferred.make(), yield* Deferred.make(), yield* Deferred.make(), yield* Deferred.make(), + yield* Deferred.make(), + yield* Deferred.make(), ] as const; const continuationOffered = yield* Deferred.make(); @@ -2836,7 +2859,7 @@ describe("AntigravityAdapterV2 prompt suggestions", () => { Effect.gen(function* () { const index = callCount; callCount += 1; - if (index < 4) { + if (index < 6) { yield* Deferred.succeed(started[index]!, undefined); return yield* Effect.never.pipe( Effect.onInterrupt(() => @@ -2888,12 +2911,49 @@ describe("AntigravityAdapterV2 prompt suggestions", () => { }); yield* Deferred.await(interrupted[2]); - // 4. Admitting a late continuation cancels the fourth in-flight suggestion and suppresses - // sampling while the continuation remains pending. + // 4. Resuming a different native session cancels the fourth in-flight suggestion. yield* session.startTurn(makeTurn(4, { text: "Fourth turn" })); yield* until((event) => event.type === "turn.terminal"); yield* Deferred.await(started[3]); + const otherProviderThread = { + ...providerThread, + nativeThreadRef: { + driver: providerThread.driver, + nativeId: "mock-other-session", + strength: "strong" as const, + }, + }; + yield* session.resumeThread({ + providerThread: otherProviderThread, + modelSelection, + }); + yield* Deferred.await(interrupted[3]); + yield* session.resumeThread({ + providerThread, + modelSelection, + }); + + // 5. Reading a snapshot for a different native session cancels the fifth in-flight suggestion. + yield* session.startTurn(makeTurn(5, { text: "Fifth turn" })); + yield* until((event) => event.type === "turn.terminal"); + yield* Deferred.await(started[4]); + + yield* session.readThreadSnapshot({ + providerThread: otherProviderThread, + }); + yield* Deferred.await(interrupted[4]); + yield* session.resumeThread({ + providerThread, + modelSelection, + }); + + // 6. Admitting a late continuation cancels the sixth in-flight suggestion and suppresses + // sampling while the continuation remains pending. + yield* session.startTurn(makeTurn(6, { text: "Sixth turn" })); + yield* until((event) => event.type === "turn.terminal"); + yield* Deferred.await(started[5]); + yield* emitSessionUpdate({ sessionId: "mock-suggestion-session", update: { @@ -2902,25 +2962,25 @@ describe("AntigravityAdapterV2 prompt suggestions", () => { }, }); const offeredRequest = yield* Deferred.await(continuationOffered); - yield* Deferred.await(interrupted[3]); + yield* Deferred.await(interrupted[5]); - yield* session.startTurn(makeTurn(5, { text: "Fifth turn while continuation pending" })); + yield* session.startTurn(makeTurn(7, { text: "Seventh turn while continuation pending" })); yield* until((event) => event.type === "turn.terminal"); - assert.equal(callCount, 4); + assert.equal(callCount, 6); - // 5. Once the pending continuation clears, a subsequent normal turn emits its suggestion. + // 7. Once the pending continuation clears, a subsequent normal turn emits its suggestion. yield* offeredRequest.clearIfCurrent?.() ?? Effect.void; - const turn6 = makeTurn(6, { text: "Sixth turn" }); - yield* session.startTurn(turn6); - const events6 = yield* until((event) => event.type === "prompt_suggestion.updated"); + const turn8 = makeTurn(8, { text: "Eighth turn" }); + yield* session.startTurn(turn8); + const events8 = yield* until((event) => event.type === "prompt_suggestion.updated"); assert.deepEqual( - events6.filter((event) => event.type === "prompt_suggestion.updated"), + events8.filter((event) => event.type === "prompt_suggestion.updated"), [ { type: "prompt_suggestion.updated", driver: "antigravity", - threadId: turn6.threadId, - runId: turn6.runId, + threadId: turn8.threadId, + runId: turn8.runId, text: "run the test suite", }, ], diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index c091d3336476e..4ba275d419f66 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -1987,6 +1987,8 @@ interface SnapshotMessageState { readonly messages: Map; /** Messages loaded from thought chunks; `messages` stores them as assistant text. */ readonly reasoningIds: ReadonlySet; + /** Leading handoff context length per stored user message, stripped for prompt suggestions. */ + readonly inlineContextLengths: Map; loadingRole: "user" | "assistant" | "thought" | null; loadingMessageId: string | null; loadingIndex: number; @@ -1996,6 +1998,7 @@ const emptySnapshotState = (): SnapshotMessageState => ({ order: [], messages: new Map(), reasoningIds: new Set(), + inlineContextLengths: new Map(), loadingRole: null, loadingMessageId: null, loadingIndex: 0, @@ -2650,10 +2653,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( ) { continue; } + const inlineContextLength = + state.inlineContextLengths.get(key) ?? + (message.id === context.input.message.messageId + ? context.input.inlineContextLength + : undefined); const rawText = - message.id === context.input.message.messageId && - context.input.inlineContextLength !== undefined - ? message.text.slice(context.input.inlineContextLength) + inlineContextLength !== undefined + ? message.text.slice(inlineContextLength) : message.text; const text = rawText.trim(); if (text.length === 0) continue; @@ -3205,11 +3212,19 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( : undefined; }); - const rememberSnapshotMessage = (message: OrchestrationV2ConversationMessage) => + const rememberSnapshotMessage = ( + message: OrchestrationV2ConversationMessage, + inlineContextLength?: number, + ) => Ref.update(snapshot, (current) => { const key = String(message.id); if (!current.messages.has(key)) current.order.push(key); current.messages.set(key, message); + if (inlineContextLength !== undefined && inlineContextLength > 0) { + current.inlineContextLengths.set(key, inlineContextLength); + } else { + current.inlineContextLengths.delete(key); + } return current; }); @@ -8837,8 +8852,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( ) { const sessionId = yield* Ref.get(activeSessionId); if (sessionId !== null) { - offeredContinuation = true; - yield* offerContinuationRun(sessionId); + offeredContinuation = yield* offerContinuationRun(sessionId); } } if (settledStatus !== "completed") { @@ -9619,26 +9633,29 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( driver, providerThread: activeProviderThread, }); - yield* rememberSnapshotMessage({ - createdBy: turnInput.message.createdBy, - creationSource: turnInput.message.creationSource, - ...(turnInput.message.scheduledTaskId === undefined - ? {} - : { scheduledTaskId: turnInput.message.scheduledTaskId }), - ...(turnInput.message.senderThreadId === undefined - ? {} - : { senderThreadId: turnInput.message.senderThreadId }), - id: turnInput.message.messageId, - threadId: turnInput.threadId, - runId: turnInput.runId, - nodeId: turnInput.rootNodeId, - role: "user", - text: turnInput.message.text, - attachments: [...turnInput.message.attachments], - streaming: false, - createdAt: startedAt, - updatedAt: startedAt, - }); + yield* rememberSnapshotMessage( + { + createdBy: turnInput.message.createdBy, + creationSource: turnInput.message.creationSource, + ...(turnInput.message.scheduledTaskId === undefined + ? {} + : { scheduledTaskId: turnInput.message.scheduledTaskId }), + ...(turnInput.message.senderThreadId === undefined + ? {} + : { senderThreadId: turnInput.message.senderThreadId }), + id: turnInput.message.messageId, + threadId: turnInput.threadId, + runId: turnInput.runId, + nodeId: turnInput.rootNodeId, + role: "user", + text: turnInput.message.text, + attachments: [...turnInput.message.attachments], + streaming: false, + createdAt: startedAt, + updatedAt: startedAt, + }, + turnInput.inlineContextLength, + ); if (followsAgentWorkState) yield* runtimeCallbackPermit.withPermit(syncNativeChildren(context)); const attached = turnAttached; @@ -10102,6 +10119,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( threadInput.providerThread.appThreadId, ).pipe(Effect.tapError(() => restorePreviousItemIdentity)); if ((yield* Ref.get(activeSessionId)) !== sessionId || restartAfterInterrupt) { + yield* cancelPromptSuggestion(); yield* Ref.set(snapshot, emptySnapshotState()); const activated = yield* activateSession( sessionId, @@ -10642,6 +10660,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( detail: "ACP driver does not support session/load snapshots", }); } + yield* cancelPromptSuggestion(); yield* Ref.set(snapshot, emptySnapshotState()); const mcpContext = yield* readMcpContext( snapshotInput.providerThread.appThreadId,