From 197d7a52b356e533a66cf3d6e1e713405a580a27 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 08:02:07 +0000 Subject: [PATCH 1/7] test(server): Antigravity command still running at end_turn is not marked completed Failing reproduction for #16442: the ACP adapter finalizes an Antigravity turn on end_turn and marks an execute tool that is still in progress as completed, and Stop afterwards cannot reach it. AI-assisted; verified with the tests and tools listed above. --- .../Adapters/AntigravityAdapterV2.test.ts | 161 ++++++++++++++++++ 1 file changed, 161 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index 0945a282cee2..89594fd65668 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,166 @@ describe("AntigravityAdapterV2 html_render pages", () => { ); }); +describe("AntigravityAdapterV2 running commands", () => { + it.effect("does not mark a command completed while it is still running at end_turn", () => + 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))), + // The agent starts a watcher, answers, and ends its turn while it runs. + 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: { + sessionUpdate: "tool_call", + toolCallId: "watcher", + title: "Running run_command", + kind: "execute", + status: "in_progress", + rawInput: { CommandLine: "tail -f app.log" }, + }, + }), + ); + 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 commandStatus: string | null = null; + while (providerTurnId === null || commandStatus === 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 === "watcher" + ) { + commandStatus = event.turnItem.status; + } + } + yield* Deferred.await(promptReturned); + yield* session.interruptTurn({ providerThread, providerTurnId, requestRuntimeRestart: true }); + + // Whether the turn waits for the command or leaves it running past the reply, + // it never reported finishing, so it must not be shown as completed. + let terminalSeen = false; + while (!terminalSeen) { + const event = yield* Queue.take(events); + if ( + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === "watcher" + ) { + commandStatus = event.turnItem.status; + } + terminalSeen = event.type === "turn.terminal"; + } + assert.notEqual(commandStatus, "completed"); + }).pipe(Effect.provide(layerSession), Effect.scoped), + ); +}); + describe("AntigravityAdapterV2 subagent reads", () => { it.effect("completes a read T3 served when the agent later fails it with a step error", () => Effect.gen(function* () { From 942f398432b6e6796b0e4b0926162bd7c9706207 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 09:10:37 +0000 Subject: [PATCH 2/7] fix(server): Antigravity commands still running after the reply keep the turn open, and Stop ends them At end_turn the Antigravity turn finalized as completed and marked every in-progress command completed, so Stop had no turn to end and the command kept running. Antigravity commands now count as background work: the turn stays open until they report finishing, and Stop restarts the runtime, which now runs in its own process group, so the commands end too. Fixes pingdotgg/t3code#16442 AI-assisted; verified with the tests and tools listed above. --- .../src/orchestration-v2/Adapters/AntigravityAdapterV2.ts | 6 ++++++ apps/server/src/provider/acp/AntigravityAcpSupport.ts | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts index 1490140c1e6e..2b06a97243f3 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 66eab95c9fb3..dd93207bc959 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", From a53c4580bf435c19c883587c91e0e6b6bb7f0d5c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 09:27:38 +0000 Subject: [PATCH 3/7] fix(server): Antigravity turns with a launched subagent batch still settle Holding the turn for background work also waited on running subagents, but Antigravity marks launched subagents running until finalizeTurn idles them, so a finished turn never settled. Subagents that the flavor idles at turn completion no longer hold the deferred finalize. AI-assisted; verified with the tests and tools listed above. --- .../Adapters/AntigravityAdapterV2.test.ts | 330 ++++++++++-------- packages/provider-acp/src/server/adapter.ts | 2 + 2 files changed, 182 insertions(+), 150 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index 89594fd65668..a80520e4c29a 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts @@ -840,163 +840,193 @@ describe("AntigravityAdapterV2 html_render pages", () => { ); }); -describe("AntigravityAdapterV2 running commands", () => { - it.effect("does not mark a command completed while it is still running at end_turn", () => - 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))), - // The agent starts a watcher, answers, and ends its turn while it runs. - 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: { - sessionUpdate: "tool_call", - toolCallId: "watcher", - title: "Running run_command", - kind: "execute", - status: "in_progress", - rawInput: { CommandLine: "tail -f app.log" }, - }, - }), - ); - 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({ +/** 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", - 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, - }); + 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 commandStatus: string | null = null; - while (providerTurnId === null || commandStatus === 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 === "watcher" - ) { - commandStatus = event.turnItem.status; - } + 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); + } + yield* Deferred.await(promptReturned); + if (options.stopAfterReply) { yield* session.interruptTurn({ providerThread, providerTurnId, requestRuntimeRestart: true }); + } - // Whether the turn waits for the command or leaves it running past the reply, - // it never reported finishing, so it must not be shown as completed. - let terminalSeen = false; - while (!terminalSeen) { - const event = yield* Queue.take(events); - if ( - event.type === "turn_item.updated" && - event.turnItem.nativeItemRef?.nativeId === "watcher" - ) { - commandStatus = event.turnItem.status; - } - terminalSeen = event.type === "turn.terminal"; + 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; } - assert.notEqual(commandStatus, "completed"); - }).pipe(Effect.provide(layerSession), Effect.scoped), + 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"); + }), ); }); diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 6f818c442e0c..3c290a145255 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; From ba04569482e0e80607c1d261d5e97885c1c67291 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 14 Nov 2026 19:25:49 +1100 Subject: [PATCH 4/7] fix(server): Stop ends detached Antigravity commands on Linux Enable Linux descendant ownership so interrupting Antigravity also ends detached command groups. Extend the existing adapter regression with real processes and independent cleanup. Co-Authored-By: GPT-6.1 Sol --- apps/server/scripts/acp-mock-agent.ts | 18 +++ apps/server/scripts/acpMockDetachedCommand.ts | 74 +++++++++++ .../Adapters/AntigravityAdapterV2.test.ts | 123 +++++++++++++++--- .../src/provider/acp/AntigravityAcpSupport.ts | 1 + 4 files changed, 198 insertions(+), 18 deletions(-) create mode 100644 apps/server/scripts/acpMockDetachedCommand.ts diff --git a/apps/server/scripts/acp-mock-agent.ts b/apps/server/scripts/acp-mock-agent.ts index 547f1ad8bcf8..7cffcd79bc70 100644 --- a/apps/server/scripts/acp-mock-agent.ts +++ b/apps/server/scripts/acp-mock-agent.ts @@ -14,9 +14,12 @@ import * as AcpError from "effect-acp/errors"; import * as AcpSchema from "effect-acp/schema"; import type * as AcpCompat from "effect-acp/compat"; +import { startDetachedCommand } from "./acpMockDetachedCommand.ts"; + import { beginAcpMockPrompt } from "./acpMockCancellationState.ts"; const requestLogPath = process.env.T3_ACP_REQUEST_LOG_PATH; +const detachedReceipt = process.env.T3_ACP_DETACHED_COMMAND_RECEIPT; const antigravityProfile = process.env.T3_ACP_ANTIGRAVITY === "1"; // GitHub Copilot CLI advertises URL-shaped session mode ids. const copilotModesProfile = process.env.T3_ACP_COPILOT_MODES === "1"; @@ -839,6 +842,21 @@ const program = Effect.gen(function* () { const requestedSessionId = String(request.sessionId ?? sessionId); beginAcpMockPrompt(cancelledSessions, requestedSessionId); promptCount += 1; + if (detachedReceipt !== undefined) { + yield* Effect.promise(() => startDetachedCommand(detachedReceipt)); + yield* agent.client.sessionUpdate({ + sessionId: requestedSessionId, + update: { + sessionUpdate: "tool_call_update", + toolCallId: "watcher", + title: "Running run_command", + kind: "execute", + status: "in_progress", + rawInput: { CommandLine: "sleep 53" }, + }, + }); + return yield* finishPrompt(requestedSessionId, "end_turn"); + } if (idleError === "grok-background" && promptCount === 1) { // The structured completion lands before the prompt settles, so it is buffered. const taskId = "call-idle-error-background"; diff --git a/apps/server/scripts/acpMockDetachedCommand.ts b/apps/server/scripts/acpMockDetachedCommand.ts new file mode 100644 index 000000000000..b6bf9bc24068 --- /dev/null +++ b/apps/server/scripts/acpMockDetachedCommand.ts @@ -0,0 +1,74 @@ +// @effect-diagnostics nodeBuiltinImport:off -- the fixture captures real Linux process identities. +import * as NodeChildProcess from "node:child_process"; +import * as NodeFS from "node:fs"; +import * as NodeReadline from "node:readline"; +import * as NodeURL from "node:url"; + +const identity = (pid: number | undefined) => { + if (pid === undefined) throw new Error("Command did not spawn"); + const stat = NodeFS.readFileSync(`/proc/${pid}/stat`, "utf8"); + const fields = stat + .slice(stat.lastIndexOf(")") + 2) + .trim() + .split(/\s+/); + return { + pid, + ppid: Number(fields[1]), + pgid: Number(fields[2]), + sid: Number(fields[3]), + startTime: fields[19], + }; +}; + +const writeReceipt = ( + pidFile: string, + receipt: { processes: Array> }, +) => { + NodeFS.writeFileSync(`${pidFile}.tmp`, JSON.stringify(receipt)); + NodeFS.renameSync(`${pidFile}.tmp`, pidFile); +}; + +if (process.argv[2] === "command") { + const pidFile = process.argv[3]; + if (pidFile === undefined) throw new Error("Missing process receipt path"); + const sleep = NodeChildProcess.spawn("/bin/sleep", ["53"], { stdio: "ignore" }); + await new Promise((resolve, reject) => { + sleep.once("spawn", resolve); + sleep.once("error", reject); + }); + const receipt = { + processes: [identity(process.ppid), identity(process.pid), identity(sleep.pid)], + }; + writeReceipt(pidFile, receipt); + process.stdout.on("error", (error: NodeJS.ErrnoException) => { + if (error.code !== "EPIPE") throw error; + }); + process.stdout.write(JSON.stringify(receipt) + "\n"); + await new Promise((resolve) => sleep.once("exit", () => resolve())); + process.exit(0); +} + +// Readiness follows the command's atomic receipt, independently of the ACP reader. +export async function startDetachedCommand(pidFile: string) { + const shell = NodeChildProcess.spawn( + "/bin/sh", + [ + "-c", + 'read -r start || exit 0; "$1" "$2" command "$3" & wait', + "fixture", + process.execPath, + NodeURL.fileURLToPath(import.meta.url), + pidFile, + ], + { detached: true, stdio: ["pipe", "pipe", "ignore"] }, + ); + writeReceipt(pidFile, { processes: [identity(shell.pid)] }); + shell.stdin.end("start\n"); + const lines = NodeReadline.createInterface({ input: shell.stdout }); + try { + const ready = await lines[Symbol.asyncIterator]().next(); + if (ready.done) throw new Error("Command exited before acknowledging child readiness"); + } finally { + lines.close(); + } +} diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index a80520e4c29a..2b09f626bf7f 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts @@ -12,6 +12,7 @@ import { RunId, ThreadId, } from "@t3tools/contracts"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import { resolveSelfInvocation } from "@t3tools/shared/nodeRuntime"; import { htmlRenderFromToolItem } from "@t3tools/shared/toolOutput"; import * as SecureRandom from "effect/crypto/SecureRandom"; @@ -23,6 +24,7 @@ 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 Schema from "effect/Schema"; import * as Stream from "effect/Stream"; import { ChildProcessSpawner } from "effect/process"; import type * as EffectAcpSchema from "effect-acp/compat"; @@ -31,7 +33,7 @@ import * as EffectAcpErrors from "effect-acp/errors"; import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost"; import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost"; -import type * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime"; +import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime"; import { makeAntigravityAcpRuntime } from "../../provider/acp/AntigravityAcpSupport.ts"; import { normalizeAntigravitySessionUpdate } from "../../provider/acp/AntigravityProtocol.ts"; import { ANTIGRAVITY_SIGN_IN_REQUIRED_MESSAGE } from "../../provider/antigravityAuthSupport.ts"; @@ -843,7 +845,7 @@ 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 }, + options: { readonly stopAfterReply: boolean; readonly detachedCommand?: boolean }, ) => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; @@ -852,6 +854,67 @@ const endTurnAfterTool = ( const mockAgentPath = yield* path.fromFileUrl( new URL("../../../scripts/acp-mock-agent.ts", import.meta.url), ); + const fs = yield* FileSystem.FileSystem; + const scratch = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-stop-" }); + const receiptPath = path.join(scratch, "processes.json"); + const controller = options.detachedCommand + ? AcpSessionRuntime.makePosixProcessTreeController("linux") + : undefined; + const decodeReceipt = Schema.decodeUnknownEffect( + Schema.fromJsonString( + Schema.Struct({ + processes: Schema.NonEmptyArray( + Schema.Struct({ + pid: Schema.Int, + ppid: Schema.Int, + pgid: Schema.Int, + sid: Schema.Int, + startTime: Schema.String, + }), + ), + }), + ), + ); + const readReceipt = () => + fs.readFileString(receiptPath).pipe( + Effect.flatMap(decodeReceipt), + Effect.map((receipt) => receipt.processes), + ); + const executing = (owned: { readonly pid: number; readonly startTime: string }) => { + const current = controller?.identity(owned.pid); + return ( + current?.startTime === owned.startTime && current.state !== "Z" && current.state !== "X" + ); + }; + // Cleanup uses captured identities even if Stop loses the parent chain or setup fails. + yield* Effect.addFinalizer(() => + Effect.gen(function* () { + if (controller === undefined || !(yield* fs.exists(receiptPath))) return; + const processes = yield* readReceipt(); + const owned = processes.map((entry) => ({ + ...entry, + executable: undefined, + parentExecutable: undefined, + parentStartTime: processes.find((parent) => parent.pid === entry.ppid)?.startTime ?? "", + })); + let signals = 0; + yield* AcpSessionRuntime.terminatePosixOwnedProcessTree({ + controller: { + ...controller, + signalProcess: (pid, signal) => { + signals += 1; + controller.signalProcess(pid, signal); + }, + }, + rootPid: processes[0].pid, + root: { value: owned[0], captureAttempted: true }, + ledger: new Map(owned.map((entry) => [`${entry.pid}:${entry.startTime}`, entry])), + grace: 0, + }); + assert.isFalse(processes.some(executing), "Fixture cleanup retained a captured process"); + yield* Effect.logInfo("DETACHED_CLEANUP", { signals, survivors: [] }); + }).pipe(Effect.orDie), + ); type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"]; let sessionUpdate: Parameters[0] | undefined; const promptReturned = yield* Deferred.make(); @@ -864,11 +927,16 @@ const endTurnAfterTool = ( makeAntigravityAcpRuntime({ ...input, childProcessSpawner, + // Keep the regression on the /proc path even on hosts with cgroup delegation. + ...(options.detachedCommand ? { linuxCgroupController: null } : {}), spawn: { command: process.execPath, args: [mockAgentPath], cwd: input.cwd, - env: { T3_ACP_ANTIGRAVITY: "1" }, + env: { + T3_ACP_ANTIGRAVITY: "1", + ...(options.detachedCommand ? { T3_ACP_DETACHED_COMMAND_RECEIPT: receiptPath } : {}), + }, }, }).pipe( Effect.provideService(SecureRandom.SecureRandom, crypto), @@ -878,19 +946,22 @@ const endTurnAfterTool = ( 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))), + prompt: (payload, promptOptions) => + (options.detachedCommand + ? runtime.prompt(payload, promptOptions) + : 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, @@ -975,6 +1046,16 @@ const endTurnAfterTool = ( } } yield* Deferred.await(promptReturned); + const processes = options.detachedCommand ? yield* readReceipt() : []; + if (options.detachedCommand) { + assert.equal(processes.length, 3); + const [shell, command, sleep] = processes; + assert.equal(command!.ppid, shell!.pid); + assert.equal(sleep!.ppid, command!.pid); + assert.equal(shell!.pgid, shell!.pid); + assert.isTrue(processes.every(executing), "Readiness must include executing children"); + yield* Effect.logInfo("DETACHED_BEFORE_STOP", { processes }); + } if (options.stopAfterReply) { yield* session.interruptTurn({ providerThread, providerTurnId, requestRuntimeRestart: true }); } @@ -990,11 +1071,17 @@ const endTurnAfterTool = ( } if (event.type === "turn.terminal") terminalStatus = event.status; } + if (options.detachedCommand) { + const survivors = processes.filter(executing); + yield* Effect.logInfo("DETACHED_AFTER_STOP", { survivors, itemStatus, terminalStatus }); + assert.deepEqual(survivors, [], "Stop left a captured command process alive"); + assert.equal(terminalStatus, "interrupted"); + } 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", () => + it.live("does not mark a command completed while it is still running at end_turn", () => Effect.gen(function* () { const result = yield* endTurnAfterTool( { @@ -1005,7 +1092,7 @@ describe("AntigravityAdapterV2 running commands", () => { status: "in_progress", rawInput: { CommandLine: "tail -f app.log" }, }, - { stopAfterReply: true }, + { stopAfterReply: true, detachedCommand: (yield* HostProcess.Platform) === "linux" }, ); // It never reported finishing, so it must not be shown as completed. assert.notEqual(result.itemStatus, "completed"); diff --git a/apps/server/src/provider/acp/AntigravityAcpSupport.ts b/apps/server/src/provider/acp/AntigravityAcpSupport.ts index dd93207bc959..0d3964bd5ca2 100644 --- a/apps/server/src/provider/acp/AntigravityAcpSupport.ts +++ b/apps/server/src/provider/acp/AntigravityAcpSupport.ts @@ -132,6 +132,7 @@ export const makeAntigravityAcpRuntime = Effect.fn("makeAntigravityAcpRuntime")( ...input, // Stop tears down the agent's process group to end commands session/cancel leaves running. ownDetachedProcessGroup: true, + ownDescendantProcessGroups: processGroupPlatform === "linux", processGroupPlatform, authMethodId: input.authMethod ?? "oauth-personal", authenticateEagerly: true, From 4ecec225211b1d72d00317bf1ed066c8571a04da Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:34:25 +1100 Subject: [PATCH 5/7] fix(server): Stop ends detached Antigravity commands on macOS --- apps/server/package.json | 1 + .../acp/AcpSessionRuntime.processTree.test.ts | 82 +++++++++++++++++- .../src/provider/acp/AntigravityAcpSupport.ts | 3 +- knip.jsonc | 1 + packages/provider-acp/package.json | 3 +- .../src/server/AcpSessionRuntime.ts | 2 + .../src/server/darwinProcessTree.ts | 85 +++++++++++++++++++ pnpm-lock.yaml | 6 ++ 8 files changed, 178 insertions(+), 5 deletions(-) create mode 100644 packages/provider-acp/src/server/darwinProcessTree.ts diff --git a/apps/server/package.json b/apps/server/package.json index d16b8d4e19f2..f15c4ec7ea57 100644 --- a/apps/server/package.json +++ b/apps/server/package.json @@ -66,6 +66,7 @@ "diff": "8.0.3", "effect": "catalog:", "ffi-rs": "1.3.2", + "koffi": "3.3.6", "node-pty": "^1.2.0-beta.15", "playwright-core": "1.64.1", "proper-lockfile": "4.1.2", diff --git a/apps/server/src/provider/acp/AcpSessionRuntime.processTree.test.ts b/apps/server/src/provider/acp/AcpSessionRuntime.processTree.test.ts index 0c6bddbbf856..c8db49e820e2 100644 --- a/apps/server/src/provider/acp/AcpSessionRuntime.processTree.test.ts +++ b/apps/server/src/provider/acp/AcpSessionRuntime.processTree.test.ts @@ -2,6 +2,7 @@ import * as NodeChildProcess from "node:child_process"; import * as NodeFS from "node:fs"; import * as NodePath from "node:path"; +import * as NodeEvents from "node:events"; import { describe, expect, it, vi } from "@effect/vitest"; import * as HostProcess from "@t3tools/shared/HostProcess"; @@ -813,13 +814,88 @@ describe("terminatePosixOwnedProcessTree", () => { }), ); + it.live.runIf(HostProcess.Platform.defaultValue() === "darwin")( + "terminates Darwin descendants in separate sessions before independent cleanup", + () => + Effect.gen(function* () { + const controller = AcpSessionRuntime.makePosixProcessTreeController("darwin"); + const captured = new Map(); + yield* Effect.addFinalizer(() => + Effect.sync(() => { + for (const old of [...captured.values()].reverse()) { + const current = controller.identity(old.pid); + if (current?.startTime === old.startTime && current.state !== "Z") { + try { + process.kill(old.pid, "SIGKILL"); + } catch (error) { + if (!(error instanceof Error && "code" in error && error.code === "ESRCH")) { + throw error; + } + } + } + } + }), + ); + const root = NodeChildProcess.spawn( + process.execPath, + [ + "-e", + `const {spawn}=require("node:child_process"); + const child=spawn("/bin/sleep",["60"],{detached:true,stdio:"ignore"}); + child.on("spawn",()=>console.log(child.pid)); + process.stdin.on("end",()=>{ + if(child.exitCode!==null||child.signalCode!==null)process.exit(0); + child.once("exit",()=>process.exit(0)); + child.kill("SIGKILL"); + }); + process.stdin.resume();`, + ], + { detached: true, stdio: ["pipe", "pipe", "pipe"] }, + ); + const rootExit = NodeEvents.EventEmitter.once(root, "exit"); + yield* Effect.addFinalizer(() => + Effect.promise(async () => { + root.stdin.end(); + await rootExit; + }), + ); + const rootIdentity = controller.identity(root.pid!); + expect(rootIdentity).toBeDefined(); + captured.set(root.pid!, rootIdentity!); + const [chunk] = yield* Effect.promise(() => + NodeEvents.EventEmitter.once(root.stdout, "data"), + ); + const childPid = Number(String(chunk).trim()); + const child = controller.identity(childPid); + expect(child).toBeDefined(); + captured.set(childPid, child!); + expect(child!.ppid).toBe(root.pid); + expect(child!.pgid).toBe(childPid); + expect(child!.sid).toBe(childPid); + expect(controller.childPidsOf(root.pid!)).toContain(childPid); + expect(controller.snapshot()).toEqual(expect.arrayContaining([rootIdentity, child])); + yield* AcpSessionRuntime.terminatePosixOwnedProcessTree({ + controller, + grace: 0, + rootPid: root.pid!, + }); + for (const old of captured.values()) { + const current = controller.identity(old.pid); + expect(current?.startTime !== old.startTime || current.state === "Z").toBe(true); + } + const [code, signal] = yield* Effect.promise(() => rootExit); + expect(code).toBeNull(); + expect(signal).toBe("SIGTERM"); + }).pipe(Effect.scoped), + ); + it("fails closed where a stable POSIX identity provider is unavailable", () => { try { - AcpSessionRuntime.makePosixProcessTreeController("darwin"); - throw new Error("Expected Darwin process ownership to fail closed"); + AcpSessionRuntime.makePosixProcessTreeController("freebsd"); + throw new Error("Expected FreeBSD process ownership to fail closed"); } catch (error) { expect(error).toMatchObject({ - detail: "Detached ACP descendant ownership is unsupported on darwin", + detail: "Detached ACP descendant ownership is unsupported on freebsd", }); } }); diff --git a/apps/server/src/provider/acp/AntigravityAcpSupport.ts b/apps/server/src/provider/acp/AntigravityAcpSupport.ts index 0d3964bd5ca2..6f1a5dd4039f 100644 --- a/apps/server/src/provider/acp/AntigravityAcpSupport.ts +++ b/apps/server/src/provider/acp/AntigravityAcpSupport.ts @@ -132,7 +132,8 @@ export const makeAntigravityAcpRuntime = Effect.fn("makeAntigravityAcpRuntime")( ...input, // Stop tears down the agent's process group to end commands session/cancel leaves running. ownDetachedProcessGroup: true, - ownDescendantProcessGroups: processGroupPlatform === "linux", + ownDescendantProcessGroups: + processGroupPlatform === "linux" || processGroupPlatform === "darwin", processGroupPlatform, authMethodId: input.authMethod ?? "oauth-personal", authenticateEagerly: true, diff --git a/knip.jsonc b/knip.jsonc index e3589f643c32..c6fbd0dccd15 100644 --- a/knip.jsonc +++ b/knip.jsonc @@ -73,6 +73,7 @@ "@t3tools/web", "@napi-rs/keyring", "ffi-rs", + "koffi", "transcribe-cpp", ], }, diff --git a/packages/provider-acp/package.json b/packages/provider-acp/package.json index 83f556e4c44e..0e75ee8bebdf 100644 --- a/packages/provider-acp/package.json +++ b/packages/provider-acp/package.json @@ -49,7 +49,8 @@ "@t3tools/provider-core": "workspace:*", "@t3tools/shared": "workspace:*", "diff": "8.0.3", - "effect": "catalog:" + "effect": "catalog:", + "koffi": "3.3.6" }, "devDependencies": { "@effect/platform-node": "catalog:", diff --git a/packages/provider-acp/src/server/AcpSessionRuntime.ts b/packages/provider-acp/src/server/AcpSessionRuntime.ts index 514ac9116415..5db8cc99ffb7 100644 --- a/packages/provider-acp/src/server/AcpSessionRuntime.ts +++ b/packages/provider-acp/src/server/AcpSessionRuntime.ts @@ -34,6 +34,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess"; import { signalProcessGroup } from "@t3tools/provider-core/server/processGroup"; import { appendAcpStderrTail, sanitizeAcpStderrExcerpt } from "./stderr.ts"; +import { makeDarwinProcessTreeController } from "./darwinProcessTree.ts"; import { collectSessionConfigOptionValues, decideToolCallUpdateEmission, @@ -748,6 +749,7 @@ export function makePosixProcessTreeController( platform: NodeJS.Platform, ): AcpPosixProcessTreeController { if (platform === "linux") return makeLinuxProcessTreeController(); + if (platform === "darwin") return makeDarwinProcessTreeController(); throw new AcpProcessGroupTerminationError({ detail: `Detached ACP descendant ownership is unsupported on ${platform}`, }); diff --git a/packages/provider-acp/src/server/darwinProcessTree.ts b/packages/provider-acp/src/server/darwinProcessTree.ts new file mode 100644 index 000000000000..df5a7504c253 --- /dev/null +++ b/packages/provider-acp/src/server/darwinProcessTree.ts @@ -0,0 +1,85 @@ +// @effect-diagnostics nodeBuiltinImport:off +import * as NodeModule from "node:module"; + +import type { + AcpPosixProcessIdentity, + AcpPosixProcessTreeController, +} from "./AcpSessionRuntime.ts"; + +const requireForFfi = NodeModule.createRequire(import.meta.url); + +export function makeDarwinProcessTreeController(): AcpPosixProcessTreeController { + const koffi = requireForFfi("koffi") as typeof import("koffi"); + const libproc = koffi.load("/usr/lib/libproc.dylib"); + const libc = koffi.load("/usr/lib/libSystem.B.dylib"); + const pidInfo = libproc.func( + "int proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int size)", + ); + const pidPath = libproc.func("int proc_pidpath(int pid, void *buffer, uint32_t size)"); + const listPids = libproc.func( + "int proc_listpids(uint32_t type, uint32_t info, void *buffer, int size)", + ); + const getSid = libc.func("int getsid(int pid)"); + + const pids = (type: number, info: number): ReadonlyArray => { + const read = (buffer: Buffer | null, size: number): number => { + // libproc reports errors as zero bytes and errno, also allowing an empty child list. + koffi.errno(0); + const bytes: number = listPids(type, info, buffer, size); + if (bytes < 0 || (bytes === 0 && koffi.errno() !== 0) || bytes % 4 !== 0) { + throw new Error("Failed to read Darwin process list"); + } + return bytes; + }; + const needed = read(null, 0); + // proc_listpids returns bytes; spare entries absorb forks between sizing and reading. + let size = needed + 4096; + for (let attempt = 0; attempt < 3; attempt += 1) { + const buffer = Buffer.alloc(size); + const bytes = read(buffer, size); + if (bytes < size) { + const result: Array = []; + for (let offset = 0; offset < bytes; offset += 4) { + const pid = buffer.readInt32LE(offset); + if (pid > 0) result.push(pid); + } + return result; + } + size *= 2; + } + throw new Error("Darwin process list exceeded its buffer"); + }; + + const identity = (pid: number): AcpPosixProcessIdentity | undefined => { + // SDK proc_bsdinfo uses this 136-byte layout on both Darwin arm64 and x64. + const info = Buffer.alloc(136); + if (pidInfo(pid, 3, 0, info, info.length) !== info.length) return undefined; + const sid: number = getSid(pid); + if (sid < 0 || info.readUInt32LE(12) !== pid) return undefined; + const path = Buffer.alloc(4096); + const pathLength: number = pidPath(pid, path, path.length); + return { + pid, + ppid: info.readUInt32LE(16), + pgid: info.readUInt32LE(100), + sid, + startTime: `${info.readBigUInt64LE(120)}.${info.readBigUInt64LE(128).toString().padStart(6, "0")}`, + state: info.readUInt32LE(4) === 5 ? "Z" : "live", + executable: + pathLength > 0 ? path.toString("utf8", 0, pathLength).replace(/\0.*$/s, "") : undefined, + }; + }; + const identities = (pids: ReadonlyArray) => + pids.flatMap((pid) => { + const observed = identity(pid); + return observed === undefined ? [] : [observed]; + }); + + return { + identity, + childPidsOf: (pid) => pids(6, pid), + childrenOf: (pid) => identities(pids(6, pid)), + snapshot: () => identities(pids(1, 0)), + signalProcess: (pid, signal) => process.kill(pid, signal), + }; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ef71a0a049f0..3725dcf9a1bb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -930,6 +930,9 @@ importers: ffi-rs: specifier: 1.3.2 version: 1.3.2 + koffi: + specifier: 3.3.6 + version: 3.3.6 node-pty: specifier: 1.2.0-beta.15 version: 1.2.0-beta.15(patch_hash=f1e680afec85f650699ea756f91f469844ec4c976f4f168492a45c6b3f67d651) @@ -1642,6 +1645,9 @@ importers: effect: specifier: 4.1.2 version: 4.1.2 + koffi: + specifier: 3.3.6 + version: 3.3.6 devDependencies: '@effect/platform-node': specifier: 4.1.2 From 838001957dcad2af951dd4ac3d0defb0952c36cc Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:47:10 +1100 Subject: [PATCH 6/7] docs(provider-acp): explain Darwin FFI diagnostic exception --- packages/provider-acp/src/server/darwinProcessTree.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/provider-acp/src/server/darwinProcessTree.ts b/packages/provider-acp/src/server/darwinProcessTree.ts index df5a7504c253..f28780dd4be7 100644 --- a/packages/provider-acp/src/server/darwinProcessTree.ts +++ b/packages/provider-acp/src/server/darwinProcessTree.ts @@ -1,4 +1,4 @@ -// @effect-diagnostics nodeBuiltinImport:off +// @effect-diagnostics nodeBuiltinImport:off -- Node module loading anchors Koffi's Darwin FFI to the packaged runtime. import * as NodeModule from "node:module"; import type { From 27c4d89c0b57b02557d120397de9d6b8030f7724 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:46:48 +1100 Subject: [PATCH 7/7] fix(antigravity): fail deferred turns when the runtime dies --- .../Adapters/AntigravityAdapterV2.test.ts | 109 ++++++++++++++++-- packages/provider-acp/src/server/adapter.ts | 37 +++++- 2 files changed, 134 insertions(+), 12 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index 62d269c29431..6ea11aadae79 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts @@ -36,6 +36,10 @@ import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost"; import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost"; import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime"; +import { + makeAcpAdapterV2, + type AcpAdapterV2RuntimeInput, +} from "@t3tools/provider-acp/server/adapter"; import { makeAntigravityAcpRuntime } from "../../provider/acp/AntigravityAcpSupport.ts"; import { normalizeAntigravitySessionUpdate } from "../../provider/acp/AntigravityProtocol.ts"; import { ANTIGRAVITY_SIGN_IN_REQUIRED_MESSAGE } from "../../provider/antigravityAuthSupport.ts"; @@ -876,7 +880,11 @@ 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; readonly detachedCommand?: boolean }, + options: { + readonly stopAfterReply: boolean; + readonly detachedCommand?: boolean; + readonly terminateAfterReply?: "wire-settled" | "deferred"; + }, ) => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; @@ -948,12 +956,19 @@ const endTurnAfterTool = ( ); type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"]; let sessionUpdate: Parameters[0] | undefined; - const promptReturned = yield* Deferred.make(); + const promptSettled = yield* Deferred.make(); + const promptCallback = yield* Deferred.make(); + const runtimeInputs: AcpAdapterV2RuntimeInput[] = []; const instanceId = ProviderInstanceId.make("antigravity-running-command-test"); const threadId = ThreadId.make("thread-antigravity-running-command"); - const adapter = yield* makeAntigravityAdapterV2({ + const selfInvocation = yield* resolveSelfInvocation(); + const adapterFlavor = makeAntigravityAcpAdapterFlavor({ + random: crypto, + fileSystem: fs, + path, + host: yield* ProviderHost.ProviderHost, instanceId, - selfInvocation: yield* resolveSelfInvocation(), + selfInvocation, makeRuntime: (input) => makeAntigravityAcpRuntime({ ...input, @@ -978,7 +993,7 @@ const endTurnAfterTool = ( sessionUpdate = handler; }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))), prompt: (payload, promptOptions) => - (options.detachedCommand + options.detachedCommand ? runtime.prompt(payload, promptOptions) : Effect.gen(function* () { if (sessionUpdate === undefined) { @@ -991,13 +1006,37 @@ const endTurnAfterTool = ( }), ); return { stopReason: "end_turn" as const }; - }) - ).pipe(Effect.ensuring(Deferred.succeed(promptReturned, undefined))), + }), })), ), withProcess: (_stop, task) => task, defaultModel: Effect.undefined, }); + const adapter = yield* makeAcpAdapterV2({ + instanceId, + selfInvocation, + flavor: { + ...adapterFlavor, + makeRuntime: (input) => { + runtimeInputs.push(input); + return adapterFlavor.makeRuntime(input); + }, + }, + testHooks: { + afterPromptWireSettled: () => + options.terminateAfterReply === "wire-settled" + ? runtimeInputs[0]!.onTermination( + new EffectAcpErrors.AcpTransportError({ + detail: "Injected runtime death", + cause: "test", + }), + ) + : Effect.void, + afterPromptCallback: () => Deferred.succeed(promptCallback, undefined).pipe(Effect.asVoid), + afterPromptSettledWithBackgroundWork: () => + Deferred.succeed(promptSettled, undefined).pipe(Effect.asVoid), + }, + }); const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ runtimeMode: "full-access", interactionMode: "default", @@ -1076,7 +1115,11 @@ const endTurnAfterTool = ( itemStatus = event.turnItem.status; } } - yield* Deferred.await(promptReturned); + if (options.terminateAfterReply === "wire-settled") { + yield* Deferred.await(promptCallback); + } else if (options.stopAfterReply || options.terminateAfterReply) { + yield* Deferred.await(promptSettled); + } const processes = options.detachedCommand ? yield* readReceipt() : []; if (options.detachedCommand) { assert.equal(processes.length, 3); @@ -1090,6 +1133,27 @@ const endTurnAfterTool = ( if (options.stopAfterReply) { yield* session.interruptTurn({ providerThread, providerTurnId, requestRuntimeRestart: true }); } + if (options.terminateAfterReply) { + if (options.terminateAfterReply === "deferred") { + yield* runtimeInputs[0]!.onTermination( + new EffectAcpErrors.AcpTransportError({ + detail: "Injected runtime death", + cause: "test", + }), + ); + } + const snapshot = yield* session.readThreadSnapshot({ providerThread }); + assert.equal( + snapshot.providerTurns.find((turn) => turn.id === providerTurnId)?.status, + "failed", + ); + assert.lengthOf(runtimeInputs, 2, "runtime death must require a replacement runtime"); + yield* runtimeInputs[0]!.onTermination( + new EffectAcpErrors.AcpTransportError({ detail: "Stale runtime death", cause: "test" }), + ); + yield* session.readThreadSnapshot({ providerThread }); + assert.lengthOf(runtimeInputs, 2, "a stale death must not poison the replacement runtime"); + } let terminalStatus: string | null = null; while (terminalStatus === null) { @@ -1100,7 +1164,10 @@ const endTurnAfterTool = ( ) { itemStatus = event.turnItem.status; } - if (event.type === "turn.terminal") terminalStatus = event.status; + if (event.type === "turn.terminal") { + terminalStatus = event.status; + if (options.terminateAfterReply) assert.equal(event.failure?.class, "transport_error"); + } } if (options.detachedCommand) { const survivors = processes.filter(executing); @@ -1125,11 +1192,31 @@ describe("AntigravityAdapterV2 running commands", () => { }, { stopAfterReply: true, detachedCommand: (yield* HostProcess.Platform) === "linux" }, ); - // It never reported finishing, so it must not be shown as completed. - assert.notEqual(result.itemStatus, "completed"); + assert.equal(result.itemStatus, "interrupted"); + assert.equal(result.terminalStatus, "interrupted"); }), ); + it.live.each(["wire-settled", "deferred"] as const)( + "fails a command and turn when its runtime dies after %s", + (terminateAfterReply) => + 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: false, terminateAfterReply }, + ); + assert.equal(result.itemStatus, "failed"); + assert.equal(result.terminalStatus, "failed"); + }), + ); + it.effect("completes the turn at end_turn while a launched subagent batch runs", () => Effect.gen(function* () { const result = yield* endTurnAfterTool( diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 97cda912397c..92923a064856 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -1513,6 +1513,7 @@ interface ActiveAcpTurn { finalized: boolean; finalizedStatus: "completed" | "interrupted" | "failed" | "cancelled" | null; promptSettled: boolean; + runtimeTerminated: boolean; promptSettledStatus: "completed" | "interrupted" | "failed" | "cancelled" | null; workAccepted: boolean; readonly promptWireSettled: Deferred.Deferred; @@ -2514,12 +2515,38 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( ? notePromptProgress() : Effect.void, ); + const failTurnOnRuntimeTermination = Effect.fn("AcpAdapterV2.failTurnOnRuntimeTermination")( + function* (context: ActiveAcpTurn, runtimeGeneration: number) { + yield* quarantineNativeTransportAtGeneration(runtimeGeneration); + yield* advanceRuntimeCallbackGeneration; + yield* cancelPendingRuntimeRequests(); + for (const tool of context.tools.values()) { + const status = toolStatus(tool.status); + if (status === "pending" || status === "running" || status === "waiting") { + yield* emitTool(context, tool, context.interrupted ? "interrupted" : "failed"); + } + } + yield* finalizeTurn( + context, + "failed", + makeProviderFailure({ class: "transport_error" }), + ); + }, + ); const handleRuntimeTerminationAtGeneration = (runtimeGeneration: number) => runRuntimeCallbackAtGeneration( runtimeGeneration, Effect.gen(function* () { yield* Ref.set(runtimeRestartRequired, true); - if (!followsAgentWorkState) return; + if (!followsAgentWorkState) { + const context = yield* Ref.get(activeTurn); + if (context === null || context.finalized) return; + context.runtimeTerminated = true; + if (context.promptSettled) { + yield* failTurnOnRuntimeTermination(context, runtimeGeneration); + } + return; + } agentWork = { sessionUpdate: "state_update", state: "idle", stopReason: "error" }; for (const child of nativeChildren.values()) { if (child.status !== "running") continue; @@ -8629,6 +8656,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( finalized: false, finalizedStatus: null, promptSettled: false, + runtimeTerminated: false, promptSettledStatus: null, workAccepted: false, promptWireSettled, @@ -8860,6 +8888,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( }); } if (context.finalized || followsAgentWorkState) return; + if ( + context.runtimeTerminated && + flavor.deferFinalizeForBackgroundWork === true + ) { + yield* failTurnOnRuntimeTermination(context, promptGeneration); + return; + } // ACP v2 reports a failed prompt through its idle stop reason, not an RPC error. const hiddenFailure = checkPromptOutcome === undefined ||