From 360c17710e9e84684866654fc7f4008695661281 Mon Sep 17 00:00:00 2001 From: Kameron Smith Date: Tue, 24 Nov 2026 23:18:56 -0500 Subject: [PATCH 1/2] fix(acp): stop an agent's open question from wedging the thread When an Antigravity agent waits on its own ask_question, it rejects every new session/prompt. A steer such as a delegated-task notification was sent anyway, failed the turn, and turn.terminal dismissed the question on the server while the adapter never answered the agent. The agent stayed blocked, and every later prompt failed with "Provider turn failed" until the session was torn down. - finalizeTurn now resolves the turn's pending runtime requests as cancelled, so the agent gets a native answer matching the server-side dismissal. - steerWithPrompt declines a steer while the turn has a pending request, so the orchestrator queues it after the active run instead of failing the turn. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/AntigravityAdapterV2.test.ts | 88 ++++++++++++++++++- packages/provider-acp/src/server/adapter.ts | 49 +++++++++-- 2 files changed, 131 insertions(+), 6 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index bf650a8e6882e..50f76e8622aec 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts @@ -1049,7 +1049,7 @@ const openSessionWithRunningCommand = Effect.fn("openSessionWithRunningCommand") readonly released: Deferred.Deferred | undefined; }> = []; const steered = yield* Deferred.make(); - const releaseSteer = yield* Deferred.make(); + const releaseSteer = yield* Deferred.make(); let terminate: AntigravityAdapterV2RuntimeInput["onTermination"]; const usage = { sessionUpdate: "usage_update", used: 9000, size: 1048576 } as const; const offers: Array = []; @@ -1234,6 +1234,11 @@ const openSessionWithRunningCommand = Effect.fn("openSessionWithRunningCommand") cancels: () => cancels, prompts, releaseSteer: Deferred.succeed(releaseSteer, undefined), + /** agy rejects a prompt that arrives while its own question waits for an answer. */ + rejectSteer: Deferred.fail( + releaseSteer, + new EffectAcpErrors.AcpRequestError({ code: -32603, errorMessage: "agent is busy" }), + ), runtimeDies: Effect.suspend( () => terminate?.( @@ -1243,6 +1248,8 @@ const openSessionWithRunningCommand = Effect.fn("openSessionWithRunningCommand") }; }); +type OpenedSession = Effect.Success>; + describe("AntigravityAdapterV2 work after the prompt ends", () => { it.effect("wakes for a late workspace edit only on the bound ACP session", () => Effect.gen(function* () { @@ -1552,6 +1559,85 @@ describe("AntigravityAdapterV2 work after the prompt ends", () => { }).pipe(Effect.provide(layerSession)), ); + // agy 1.5.x rejects every session/prompt while its own ask_question waits for an answer, and + // stays blocked until that question gets a native response. + const askQuestion = (requestPermission: OpenedSession["requestPermission"]) => + requestPermission( + { + sessionId: "mock-session-1", + toolCall: { toolCallId: "interaction_1", title: "Which branch?" }, + options: [ + { optionId: "main", name: "main", kind: "allow_once" }, + { optionId: "dev", name: "dev", kind: "allow_once" }, + ], + }, + { requestId: "question-1", method: "session/request_permission" }, + ).pipe(Effect.forkScoped({ startImmediately: true })); + const steerInput = ( + providerThread: OpenedSession["providerThread"], + providerTurnId: ProviderAdapter.ProviderAdapterV2SteerInput["providerTurnId"], + ): ProviderAdapter.ProviderAdapterV2SteerInput => ({ + threadId: ThreadId.make("thread-antigravity-wake"), + runId: RunId.make("run-antigravity-wake-1"), + providerThread, + providerTurnId, + message: { + messageId: MessageId.make("message-antigravity-steer"), + text: "A delegated task finished", + attachments: [], + createdBy: "agent", + creationSource: "provider", + }, + }); + + it.effect("declines a steer while the agent waits on its own question", () => + Effect.gen(function* () { + const opened = yield* openSessionWithRunningCommand({ WaitMsBeforeAsync: 10000 }, undefined, { + agentVersion: "1.5.11", + holdsFirstPrompt: false, + }); + yield* opened.startTurn(1, false); + const prompt = yield* opened.throughPromptSettled; + const providerTurnId = prompt.flatMap((event) => + event.type === "provider_turn.updated" ? [event.providerTurn.id] : [], + )[0]!; + const question = yield* askQuestion(opened.requestPermission); + yield* opened.until((event) => event.type === "runtime_request.updated"); + + const declined = yield* opened.session + .steerTurn(steerInput(opened.providerThread, providerTurnId)) + .pipe(Effect.flip); + assert.equal(declined._tag, "ProviderAdapterSteerDeclinedError", "the app queues it instead"); + assert.lengthOf(opened.prompts, 1, "the waiting agent would reject a steer prompt"); + assert.equal(opened.cancels(), 0); + assert.isUndefined(question.pollUnsafe(), "the question stays open for the user"); + }).pipe(Effect.provide(layerSession)), + ); + + it.effect("answers the agent's open question when its turn fails", () => + Effect.gen(function* () { + const opened = yield* openSessionWithRunningCommand({ WaitMsBeforeAsync: 10000 }, undefined, { + agentVersion: "1.5.11", + holdsFirstPrompt: false, + }); + yield* opened.startTurn(1, false); + const prompt = yield* opened.throughPromptSettled; + const providerTurnId = prompt.flatMap((event) => + event.type === "provider_turn.updated" ? [event.providerTurn.id] : [], + )[0]!; + yield* opened.session.steerTurn(steerInput(opened.providerThread, providerTurnId)); + assert.lengthOf(opened.prompts, 2); + // The agent asks while the steer prompt is in flight, then rejects that prompt. + const question = yield* askQuestion(opened.requestPermission); + yield* opened.until((event) => event.type === "runtime_request.updated"); + yield* opened.rejectSteer; + const failed = (yield* opened.throughTerminal).at(-1); + assert.equal(failed?.type === "turn.terminal" && failed.status, "failed"); + // The app dismisses the turn's questions at turn.terminal; the agent must hear it too. + assert.deepEqual(yield* Fiber.join(question), { outcome: { outcome: "cancelled" } }); + }).pipe(Effect.provide(layerSession)), + ); + it.effect("steers a run held open by a detached subagent without stopping it", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index fd19f4c602ffd..64e8b4c5b21e6 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -6519,10 +6519,31 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const waiting = turnAttached; turnAttached = yield* Deferred.make(); yield* Deferred.succeed(waiting, false); - const requests = yield* Ref.modify(pendingRuntimeRequests, (current) => [ - [...current.values()], - new Map(), - ]); + yield* cancelPendingRuntimeRequestEntries(); + }); + + /** + * Resolves pending requests as cancelled, so a live agent gets a native answer. + * Pass a provider turn to cancel only the requests it raised. + */ + const cancelPendingRuntimeRequestEntries = Effect.fnUntraced(function* ( + providerTurnId?: OrchestrationV2RuntimeRequest["providerTurnId"], + ) { + const requests = yield* Ref.modify(pendingRuntimeRequests, (current) => { + const cancelled: Array = []; + const kept = new Map(); + for (const [key, request] of current) { + if ( + providerTurnId === undefined || + request.runtimeRequest.providerTurnId === providerTurnId + ) { + cancelled.push(request); + } else { + kept.set(key, request); + } + } + return [cancelled, kept]; + }); if (requests.length === 0) return; const now = yield* DateTime.now; @@ -8514,6 +8535,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( }, }); } + // turn.terminal cancels this turn's pending requests on the server; answer the + // agent too, or a native question keeps it blocked and it rejects every prompt. + yield* cancelPendingRuntimeRequestEntries(context.providerTurnId); const turn = providerTurnPayload(context, settledStatus, now); yield* Ref.update(providerTurns, (current) => { const updated = new Map(current); @@ -9567,6 +9591,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( generation, Effect.gen(function* () { if (context.finalized || context.interrupted) return false; + // An agent waiting on its own question or approval rejects a new prompt, + // which would fail the turn; the orchestrator queues a declined steer instead. + for (const request of (yield* Ref.get(pendingRuntimeRequests)).values()) { + if (request.runtimeRequest.providerTurnId === context.providerTurnId) { + return "declined" as const; + } + } // The steer prompt carries the turn on, also past a prompt that already settled. const released = context.foregroundIdle; context.foregroundIdle = yield* Deferred.make(); @@ -9614,6 +9645,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( return true; }), ); + if (Option.contains(steered, "declined")) { + return yield* new ProviderAdapter.ProviderAdapterSteerDeclinedError({ + driver, + providerThreadId: turnInput.providerThread.id, + providerTurnId: turnInput.providerTurnId, + }); + } if (!Option.contains(steered, true)) { return yield* new ProviderAdapter.ProviderAdapterProtocolError({ driver, @@ -9965,7 +10003,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( (effect, turnInput) => effect.pipe( Effect.mapError((cause) => - cause._tag === "ProviderAdapterSteerRunUnsupportedError" + cause._tag === "ProviderAdapterSteerRunUnsupportedError" || + cause._tag === "ProviderAdapterSteerDeclinedError" ? cause : new ProviderAdapter.ProviderAdapterSteerRunError({ driver, From 94cda9b8a5c7fdbb5301176c77aff5a0666e87ae Mon Sep 17 00:00:00 2001 From: Kameron Smith Date: Tue, 24 Nov 2026 23:39:24 -0500 Subject: [PATCH 2/2] fix(acp): cancel only the turn's questions when it ends Review follow-up. The server dismisses a turn's pending user-input requests at turn.terminal, not its approvals, so the adapter now answers only those. Approvals keep their existing behaviour. The regression test also checks for exactly one cancelled update per question. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/AntigravityAdapterV2.test.ts | 8 +++++++- packages/provider-acp/src/server/adapter.ts | 20 ++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index 50f76e8622aec..7c72425538290 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts @@ -1631,8 +1631,14 @@ describe("AntigravityAdapterV2 work after the prompt ends", () => { const question = yield* askQuestion(opened.requestPermission); yield* opened.until((event) => event.type === "runtime_request.updated"); yield* opened.rejectSteer; - const failed = (yield* opened.throughTerminal).at(-1); + const run = yield* opened.throughTerminal; + const failed = run.at(-1); assert.equal(failed?.type === "turn.terminal" && failed.status, "failed"); + const cancelled = run.filter( + (event) => + event.type === "runtime_request.updated" && event.runtimeRequest.status === "cancelled", + ); + assert.lengthOf(cancelled, 1, "one cancelled update for the one question"); // The app dismisses the turn's questions at turn.terminal; the agent must hear it too. assert.deepEqual(yield* Fiber.join(question), { outcome: { outcome: "cancelled" } }); }).pipe(Effect.provide(layerSession)), diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 64e8b4c5b21e6..fbc888e39695a 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -6524,19 +6524,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( /** * Resolves pending requests as cancelled, so a live agent gets a native answer. - * Pass a provider turn to cancel only the requests it raised. + * Pass a filter to cancel only the requests it selects. */ const cancelPendingRuntimeRequestEntries = Effect.fnUntraced(function* ( - providerTurnId?: OrchestrationV2RuntimeRequest["providerTurnId"], + selects?: (request: PendingRuntimeRequest) => boolean, ) { const requests = yield* Ref.modify(pendingRuntimeRequests, (current) => { const cancelled: Array = []; const kept = new Map(); for (const [key, request] of current) { - if ( - providerTurnId === undefined || - request.runtimeRequest.providerTurnId === providerTurnId - ) { + if (selects === undefined || selects(request)) { cancelled.push(request); } else { kept.set(key, request); @@ -8535,9 +8532,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( }, }); } - // turn.terminal cancels this turn's pending requests on the server; answer the - // agent too, or a native question keeps it blocked and it rejects every prompt. - yield* cancelPendingRuntimeRequestEntries(context.providerTurnId); + // At turn.terminal the server dismisses the turn's pending questions (not its + // approvals); answer the agent too, or its question keeps it blocked and it + // rejects every later prompt. + yield* cancelPendingRuntimeRequestEntries( + (request) => + request.type === "user_input" && + request.runtimeRequest.providerTurnId === context.providerTurnId, + ); const turn = providerTurnPayload(context, settledStatus, now); yield* Ref.update(providerTurns, (current) => { const updated = new Map(current);