diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index bf650a8e6882e..7c72425538290 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,91 @@ 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 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)), + ); + 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..fbc888e39695a 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -6519,10 +6519,28 @@ 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 filter to cancel only the requests it selects. + */ + const cancelPendingRuntimeRequestEntries = Effect.fnUntraced(function* ( + 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 (selects === undefined || selects(request)) { + cancelled.push(request); + } else { + kept.set(key, request); + } + } + return [cancelled, kept]; + }); if (requests.length === 0) return; const now = yield* DateTime.now; @@ -8514,6 +8532,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( }, }); } + // 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); @@ -9567,6 +9593,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 +9647,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 +10005,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,