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
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ const openSessionWithRunningCommand = Effect.fn("openSessionWithRunningCommand")
readonly released: Deferred.Deferred<void> | undefined;
}> = [];
const steered = yield* Deferred.make<void>();
const releaseSteer = yield* Deferred.make<void>();
const releaseSteer = yield* Deferred.make<void, EffectAcpErrors.AcpRequestError>();
let terminate: AntigravityAdapterV2RuntimeInput["onTermination"];
const usage = { sessionUpdate: "usage_update", used: 9000, size: 1048576 } as const;
const offers: Array<ProviderContinuationRequests.ProviderContinuationRequest> = [];
Expand Down Expand Up @@ -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?.(
Expand All @@ -1243,6 +1248,8 @@ const openSessionWithRunningCommand = Effect.fn("openSessionWithRunningCommand")
};
});

type OpenedSession = Effect.Success<ReturnType<typeof openSessionWithRunningCommand>>;

describe("AntigravityAdapterV2 work after the prompt ends", () => {
it.effect("wakes for a late workspace edit only on the bound ACP session", () =>
Effect.gen(function* () {
Expand Down Expand Up @@ -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;
Expand Down
51 changes: 46 additions & 5 deletions packages/provider-acp/src/server/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6519,10 +6519,28 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
const waiting = turnAttached;
turnAttached = yield* Deferred.make<boolean>();
yield* Deferred.succeed(waiting, false);
const requests = yield* Ref.modify(pendingRuntimeRequests, (current) => [
[...current.values()],
new Map<string, PendingRuntimeRequest>(),
]);
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<PendingRuntimeRequest> = [];
const kept = new Map<string, PendingRuntimeRequest>();
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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<void>();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading