diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index 0945a282cee2c..a80520e4c29af 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts @@ -22,6 +22,7 @@ import * as Exit from "effect/Exit"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; +import * as Queue from "effect/Queue"; import * as Stream from "effect/Stream"; import { ChildProcessSpawner } from "effect/process"; import type * as EffectAcpSchema from "effect-acp/compat"; @@ -839,6 +840,196 @@ describe("AntigravityAdapterV2 html_render pages", () => { ); }); +/** The agent reports one tool, answers end_turn, and the user optionally presses Stop after. */ +const endTurnAfterTool = ( + tool: Extract, + options: { readonly stopAfterReply: boolean }, +) => + Effect.gen(function* () { + const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const path = yield* Path.Path; + const crypto = yield* SecureRandom.SecureRandom; + const mockAgentPath = yield* path.fromFileUrl( + new URL("../../../scripts/acp-mock-agent.ts", import.meta.url), + ); + type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"]; + let sessionUpdate: Parameters[0] | undefined; + const promptReturned = yield* Deferred.make(); + const instanceId = ProviderInstanceId.make("antigravity-running-command-test"); + const threadId = ThreadId.make("thread-antigravity-running-command"); + const adapter = yield* makeAntigravityAdapterV2({ + instanceId, + selfInvocation: yield* resolveSelfInvocation(), + makeRuntime: (input) => + makeAntigravityAcpRuntime({ + ...input, + childProcessSpawner, + spawn: { + command: process.execPath, + args: [mockAgentPath], + cwd: input.cwd, + env: { T3_ACP_ANTIGRAVITY: "1" }, + }, + }).pipe( + Effect.provideService(SecureRandom.SecureRandom, crypto), + Effect.map((runtime): RuntimeService => ({ + ...runtime, + handleSessionUpdate: (handler) => + Effect.sync(() => { + sessionUpdate = handler; + }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))), + prompt: () => + Effect.gen(function* () { + if (sessionUpdate === undefined) { + return yield* Effect.die("the adapter must handle session updates"); + } + yield* sessionUpdate( + normalizeAntigravitySessionUpdate({ + sessionId: "mock-session-1", + update: tool, + }), + ); + return { stopReason: "end_turn" as const }; + }).pipe(Effect.ensuring(Deferred.succeed(promptReturned, undefined))), + })), + ), + withProcess: (_stop, task) => task, + defaultModel: Effect.undefined, + }); + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd: process.cwd(), + }); + const modelSelection = { instanceId, model: "gemini-test-low" } as const; + const session = yield* adapter.openSession({ + threadId, + providerSessionId: ProviderSessionId.make("provider-session-antigravity-running-command"), + modelSelection, + runtimePolicy, + }); + const events = yield* Queue.unbounded(); + yield* session.events.pipe( + Stream.runForEach((event) => Queue.offer(events, event)), + Effect.forkScoped, + ); + const providerThread = yield* session.ensureThread({ + threadId, + modelSelection, + runtimePolicy, + }); + const now = yield* DateTime.now; + yield* session.startTurn({ + appThread: { + createdBy: "user", + creationSource: "web", + id: threadId, + projectId: ProjectId.make("project-antigravity-running-command"), + title: "Antigravity running command", + providerInstanceId: instanceId, + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: process.cwd(), + activeProviderThreadId: providerThread.id, + lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId }, + forkedFrom: null, + createdAt: now, + updatedAt: now, + archivedAt: null, + settledOverride: null, + settledAt: null, + lastVisitedAt: null, + deletedAt: null, + }, + threadId, + runId: RunId.make("run-antigravity-running-command"), + runOrdinal: 1, + providerTurnOrdinal: 1, + attemptId: RunAttemptId.make("attempt-antigravity-running-command"), + rootNodeId: NodeId.make("node-antigravity-running-command"), + providerThread, + message: { + createdBy: "user", + creationSource: "web", + messageId: MessageId.make("message-antigravity-running-command"), + text: "watch the log", + attachments: [], + }, + modelSelection, + runtimePolicy, + }); + + let providerTurnId: ProviderAdapter.ProviderAdapterV2InterruptInput["providerTurnId"] | null = + null; + let itemStatus: string | null = null; + while (providerTurnId === null || itemStatus === null) { + const event = yield* Queue.take(events); + if (event.type === "provider_turn.updated") providerTurnId = event.providerTurn.id; + if ( + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === tool.toolCallId + ) { + itemStatus = event.turnItem.status; + } + } + yield* Deferred.await(promptReturned); + if (options.stopAfterReply) { + yield* session.interruptTurn({ providerThread, providerTurnId, requestRuntimeRestart: true }); + } + + let terminalStatus: string | null = null; + while (terminalStatus === null) { + const event = yield* Queue.take(events); + if ( + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === tool.toolCallId + ) { + itemStatus = event.turnItem.status; + } + if (event.type === "turn.terminal") terminalStatus = event.status; + } + return { itemStatus, terminalStatus }; + }).pipe(Effect.provide(layerSession), Effect.scoped); + +describe("AntigravityAdapterV2 running commands", () => { + it.effect("does not mark a command completed while it is still running at end_turn", () => + Effect.gen(function* () { + const result = yield* endTurnAfterTool( + { + sessionUpdate: "tool_call", + toolCallId: "watcher", + title: "Running run_command", + kind: "execute", + status: "in_progress", + rawInput: { CommandLine: "tail -f app.log" }, + }, + { stopAfterReply: true }, + ); + // It never reported finishing, so it must not be shown as completed. + assert.notEqual(result.itemStatus, "completed"); + }), + ); + + it.effect("completes the turn at end_turn while a launched subagent batch runs", () => + Effect.gen(function* () { + const result = yield* endTurnAfterTool( + { + sessionUpdate: "tool_call", + toolCallId: "batch", + title: "Running start_subagent", + kind: "other", + status: "completed", + rawOutput: "Started two agents.", + }, + { stopAfterReply: false }, + ); + assert.equal(result.terminalStatus, "completed"); + }), + ); +}); + describe("AntigravityAdapterV2 subagent reads", () => { it.effect("completes a read T3 served when the agent later fails it with a step error", () => Effect.gen(function* () { diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts index 1490140c1e6e2..2b06a97243f39 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts @@ -233,6 +233,12 @@ export function makeAntigravityAcpAdapterFlavor( ), // Loading history replays tool calls; resume avoids replay. subagentsIdleOnTurnCompletion: true, + // A command can outlive end_turn; session/cancel leaves it running, so Stop ends the process group. + extractBackgroundTaskId: (toolCall) => + toolCall.kind === "execute" ? toolCall.toolCallId : undefined, + deferFinalizeForBackgroundWork: true, + restartRuntimeAfterInterrupt: true, + terminateRuntimeProcessGroupOnInterrupt: true, ...(options.onSessionEvent === undefined ? {} : { onSessionEvent: options.onSessionEvent }), applyModelSelection: Effect.fn("AntigravityAdapterV2.applyModelSelection")(function* ({ runtime, diff --git a/apps/server/src/provider/acp/AntigravityAcpSupport.ts b/apps/server/src/provider/acp/AntigravityAcpSupport.ts index 66eab95c9fb39..dd93207bc9591 100644 --- a/apps/server/src/provider/acp/AntigravityAcpSupport.ts +++ b/apps/server/src/provider/acp/AntigravityAcpSupport.ts @@ -126,9 +126,13 @@ export const makeAntigravityAcpRuntime = Effect.fn("makeAntigravityAcpRuntime")( EffectAcpErrors.AcpError, SecureRandom.SecureRandom | Scope.Scope > { + const processGroupPlatform = yield* HostProcess.Platform; const context = yield* Layer.build( AcpSessionRuntime.layer({ ...input, + // Stop tears down the agent's process group to end commands session/cancel leaves running. + ownDetachedProcessGroup: true, + processGroupPlatform, authMethodId: input.authMethod ?? "oauth-personal", authenticateEagerly: true, resumeMethod: "resume", diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 6f818c442e0cd..3c290a1452551 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -3782,6 +3782,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const status = toolStatus(tool.status); if (status === "pending" || status === "running") return true; } + // finalizeTurn idles these subagents, so waiting on them would never settle. + if (flavor.subagentsIdleOnTurnCompletion === true) return false; for (const subagent of context.subagents.values()) { if (acpSubagentStatusBlocksTurnSettlement(subagent.task.status)) { return true;