From 4994f6c5bf1f5300f3c7c1940adf23c8aeac5324 Mon Sep 17 00:00:00 2001 From: LVC-Chakresh Date: Sun, 22 Nov 2026 18:40:41 +0530 Subject: [PATCH 1/5] fix(antigravity): bound silent post-settle background command holds A run_command that outlives session/prompt holds the turn open. If its terminal update never reaches T3, the thread stays in Working forever with an empty pending list. Add an optional postSettleBackgroundHoldTimeout to the ACP flavor and opt Antigravity into a 30-minute bound, after which the settled turn finalizes instead of hanging. --- .../Adapters/AntigravityAdapterV2.test.ts | 34 +++++++++++++++++++ .../Adapters/AntigravityAdapterV2.ts | 4 +++ packages/provider-acp/src/server/adapter.ts | 34 +++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index fca472c6c433e..a3dbe41b0ccd1 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts @@ -1349,6 +1349,40 @@ describe("AntigravityAdapterV2 work after the prompt ends", () => { }).pipe(Effect.provide(layerSession)), ); + // A command whose terminal update never arrives would hold the run in Working forever with + // an empty pending list and a finished reply above Stop. + it.effect("settles a silent background command after the post-settle hold timeout", () => + Effect.gen(function* () { + const { command, offers, startTurn, until, throughPromptSettled } = + yield* openSessionWithRunningCommand({ WaitMsBeforeAsync: 10000 }); + yield* startTurn(1, false); + const prompt = yield* throughPromptSettled; + + // A running tool keeps the inactivity watchdog re-armed, so 20 silent minutes do not + // settle this run on their own. + yield* TestClock.adjust("20 minutes"); + yield* TestClock.adjust("11 minutes"); + const run = yield* until((event) => event.type === "turn.terminal"); + const terminal = run.at(-1); + assert.equal( + terminal?.type === "turn.terminal" && terminal.status, + "completed", + "the bounded hold finalizes the turn instead of leaving it running forever", + ); + assert.equal(terminal?.type === "turn.terminal" && terminal.failure, null); + assert.lengthOf(offers, 0, "a silent command produces no wake on its own"); + const commandItem = [...prompt, ...run].findLast( + (event) => + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === command.toolCallId, + ); + assert.isTrue( + commandItem?.type === "turn_item.updated" && commandItem.turnItem.status === "completed", + "the silent command is no longer left as live activity on a settled run", + ); + }).pipe(Effect.provide(layerSession)), + ); + // Command 2 finishes during reply 1; agy dequeues command 2 after reply 1's usage_update and // needs >3 s before streaming reply 2. Wait for reply 2 (bounded by the 2-minute quiet limit), // but still finalize at 3 s when both commands were batched into reply 1. diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts index 3f629c813b615..7c0c2fd65ab0c 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts @@ -244,6 +244,10 @@ export function makeAntigravityAcpAdapterFlavor( wakeOnDetachedRequest: true, replyEndsWithUsageUpdate: true, toolOutlivesPrompt: antigravityToolOutlivesPrompt, + // agy sometimes loses the terminal update or wake for a background command. Do not hold + // the turn in Working forever on a silent command; a 30-minute bound still leaves long + // runs room, and the command remains visible as pending background work after settlement. + postSettleBackgroundHoldTimeout: "30 minutes", // session/cancel ends the agent's running commands, so only Stop sends it once the prompt // has ended. A message sent while a command runs leaves the command alone. preserveRuntimeOnSettledInterrupt: true, diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 21fe311588906..ec878856f0920 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -637,6 +637,11 @@ export interface AcpAdapterV2Flavor { * tool ends. With `deferFinalizeForBackgroundWork`, the turn stays open until then. */ readonly toolOutlivesPrompt?: (toolCall: AcpToolCallState) => boolean; + /** + * Bound a settled turn held open only by an outliving tool; finalize after this quiet period + * so a command that never reports back cannot leave the thread in Working forever. + */ + readonly postSettleBackgroundHoldTimeout?: Duration.Input; readonly isProviderWakeNotification?: ( notification: EffectAcpSchema.SessionNotification, ) => boolean; @@ -8835,6 +8840,33 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( yield* options.testHooks?.onDeferredFinalizeScheduled?.(debounce) ?? Effect.void; }); + /** + * Bound a settled turn held only by an outliving tool that never reports back, so the + * run cannot stay in Working forever with no visible background work. + */ + const schedulePostSettleHoldExpiry = Effect.fn("AcpAdapterV2.schedulePostSettleHoldExpiry")( + function* (context: ActiveAcpTurn) { + const timeout = flavor.postSettleBackgroundHoldTimeout; + if (timeout === undefined) return; + if (!context.promptSettled || context.finalized) return; + if (!hasDeferredBackgroundWork(context)) return; + yield* Effect.gen(function* () { + yield* Effect.sleep(timeout); + if (context.finalized || !context.promptSettled) return; + if (!hasDeferredBackgroundWork(context)) return; + // Only an outliving tool gets bounded here. Queued wakes, hydration and detached + // subagents still have their own settlement paths. + const heldCommand = [...context.tools.values()].some( + (tool) => + toolStatus(tool.status) === "running" && + flavor.toolOutlivesPrompt?.(tool) === true, + ); + if (!heldCommand) return; + yield* finalizeTurn(context, context.promptSettledStatus ?? "completed"); + }).pipe(Effect.forkIn(sessionScope), Effect.asVoid); + }, + ); + const promptInactivityPaused = Effect.fn("AcpAdapterV2.promptInactivityPaused")(function* ( context: ActiveAcpTurn, ) { @@ -9265,6 +9297,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( if (context.finalized) return; if (deferredBackgroundWork || pendingReply) { yield* closeTextStreams(context); + if (deferredBackgroundWork) yield* schedulePostSettleHoldExpiry(context); if (pendingReply) { context.replyOpen = true; yield* scheduleDeferredFinalize(context); @@ -9665,6 +9698,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( return; } if (!context.finalized) { + yield* schedulePostSettleHoldExpiry(context); if (hasDeferredBackgroundWork(context)) { yield* rearmDeferredFinalize(context); } else { From 3cb62c3ffdf2038f951b20d1b5b429d9a1e15c6d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 14:55:43 +0000 Subject: [PATCH 2/5] refactor(acp): describe the post-settle hold bound as it behaves The bound counts from when the prompt settles and marks the held command completed; the comments said quiet period and pending. Drop a guard the helper already checks. Co-Authored-By: Claude Opus 5.5 --- .../orchestration-v2/Adapters/AntigravityAdapterV2.ts | 5 ++--- packages/provider-acp/src/server/adapter.ts | 10 +++------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts index 7c0c2fd65ab0c..749ca1c8a6cf4 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts @@ -244,9 +244,8 @@ export function makeAntigravityAcpAdapterFlavor( wakeOnDetachedRequest: true, replyEndsWithUsageUpdate: true, toolOutlivesPrompt: antigravityToolOutlivesPrompt, - // agy sometimes loses the terminal update or wake for a background command. Do not hold - // the turn in Working forever on a silent command; a 30-minute bound still leaves long - // runs room, and the command remains visible as pending background work after settlement. + // agy sometimes loses the terminal update or wake for a background command, which would + // hold the turn in Working forever. 30 minutes still leaves long commands room. postSettleBackgroundHoldTimeout: "30 minutes", // session/cancel ends the agent's running commands, so only Stop sends it once the prompt // has ended. A message sent while a command runs leaves the command alone. diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index ec878856f0920..e1ffe6a4418d7 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -638,8 +638,8 @@ export interface AcpAdapterV2Flavor { */ readonly toolOutlivesPrompt?: (toolCall: AcpToolCallState) => boolean; /** - * Bound a settled turn held open only by an outliving tool; finalize after this quiet period - * so a command that never reports back cannot leave the thread in Working forever. + * Finalize a settled turn this long after the prompt settles when an outliving tool still holds + * it, so a command that never reports back cannot leave the thread in Working forever. */ readonly postSettleBackgroundHoldTimeout?: Duration.Input; readonly isProviderWakeNotification?: ( @@ -8840,10 +8840,6 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( yield* options.testHooks?.onDeferredFinalizeScheduled?.(debounce) ?? Effect.void; }); - /** - * Bound a settled turn held only by an outliving tool that never reports back, so the - * run cannot stay in Working forever with no visible background work. - */ const schedulePostSettleHoldExpiry = Effect.fn("AcpAdapterV2.schedulePostSettleHoldExpiry")( function* (context: ActiveAcpTurn) { const timeout = flavor.postSettleBackgroundHoldTimeout; @@ -9297,7 +9293,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( if (context.finalized) return; if (deferredBackgroundWork || pendingReply) { yield* closeTextStreams(context); - if (deferredBackgroundWork) yield* schedulePostSettleHoldExpiry(context); + yield* schedulePostSettleHoldExpiry(context); if (pendingReply) { context.replyOpen = true; yield* scheduleDeferredFinalize(context); From 29ff9c5481e5dc03b4eb6e05b38ab96dffbec9c9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 15:04:59 +0000 Subject: [PATCH 3/5] fix(acp): arm the post-settle hold bound when a prompt settles awaiting a reply A reply still pending at settlement can start the outliving command afterwards; the bound returned early and never armed, so that turn could hold forever. The expiry callback already rechecks for a held command. Co-Authored-By: Claude Opus 5.5 --- packages/provider-acp/src/server/adapter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index e1ffe6a4418d7..e434704dbb680 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -8845,7 +8845,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const timeout = flavor.postSettleBackgroundHoldTimeout; if (timeout === undefined) return; if (!context.promptSettled || context.finalized) return; - if (!hasDeferredBackgroundWork(context)) return; + // Armed even without held work yet: a pending reply can still start the command. yield* Effect.gen(function* () { yield* Effect.sleep(timeout); if (context.finalized || !context.promptSettled) return; From c89375ff8f1fc73701082ec35555d1b90913d854 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 15:15:23 +0000 Subject: [PATCH 4/5] fix(acp): bound a held turn by session silence, not time since settlement A command that keeps reporting progress past 30 minutes was marked completed and its run ended before the result and reply arrived. The bound now restarts on every session update, which the adapter already records in lastProgressAt; that tracking is enabled whenever the bound is configured. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/AntigravityAdapterV2.test.ts | 10 ++++++++++ packages/provider-acp/src/server/adapter.ts | 20 +++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index a3dbe41b0ccd1..227117ba1893c 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts @@ -1327,6 +1327,16 @@ describe("AntigravityAdapterV2 work after the prompt ends", () => { yield* openSessionWithRunningCommand({ WaitMsBeforeAsync: 10000 }); yield* startTurn(1, false); yield* throughPromptSettled; + // Progress keeps a command past the post-settle hold bound, which counts silence only. + for (const minute of [10, 20, 30]) { + yield* TestClock.adjust("10 minutes"); + yield* send({ + sessionUpdate: "tool_call_update", + ...command, + status: "in_progress", + rawOutput: { output: `fetched ${minute} pages` }, + }); + } yield* TestClock.adjust("10 minutes"); yield* send({ sessionUpdate: "tool_call_update", diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index e434704dbb680..500cdfabce4a6 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -638,8 +638,8 @@ export interface AcpAdapterV2Flavor { */ readonly toolOutlivesPrompt?: (toolCall: AcpToolCallState) => boolean; /** - * Finalize a settled turn this long after the prompt settles when an outliving tool still holds - * it, so a command that never reports back cannot leave the thread in Working forever. + * Finalize a settled turn held by an outliving tool once the session has been silent this long, + * so a command that never reports back cannot leave the thread in Working forever. */ readonly postSettleBackgroundHoldTimeout?: Duration.Input; readonly isProviderWakeNotification?: ( @@ -2747,7 +2747,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const notePromptProgress = /* Provider events include streaming deltas; avoid a span for each update. */ Effect.fnUntraced(function* (notification?: EffectAcpSchema.SessionNotification) { - if (promptInactivityTimeoutMillis === undefined) return; + if ( + promptInactivityTimeoutMillis === undefined && + flavor.postSettleBackgroundHoldTimeout === undefined + ) { + return; + } if ( notification !== undefined && !ACP_PROMPT_ACTIVITY_UPDATES.has(notification.update.sessionUpdate) @@ -8845,9 +8850,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const timeout = flavor.postSettleBackgroundHoldTimeout; if (timeout === undefined) return; if (!context.promptSettled || context.finalized) return; + const timeoutMillis = Duration.toMillis(timeout); + const armedAt = yield* Clock.currentTimeMillis; // Armed even without held work yet: a pending reply can still start the command. yield* Effect.gen(function* () { - yield* Effect.sleep(timeout); + while (true) { + const quietSince = Math.max(armedAt, context.lastProgressAt); + const remaining = quietSince + timeoutMillis - (yield* Clock.currentTimeMillis); + if (remaining <= 0) break; + yield* Effect.sleep(Duration.millis(remaining)); + } if (context.finalized || !context.promptSettled) return; if (!hasDeferredBackgroundWork(context)) return; // Only an outliving tool gets bounded here. Queued wakes, hydration and detached From f193ef6c41d7f322b2762ffaf10cb559c1b5c2f7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 15:27:14 +0000 Subject: [PATCH 5/5] fix(acp): only expire a held turn when a silent command is its last hold The bound also ended turns held by an unanswered question or a scheduled wake, which dismissed the question or dropped the wake's reply. The flavor now names which tools are commands (Antigravity excludes schedules), and the bound finalizes only when nothing but those commands holds the turn; otherwise it waits for a fresh silent stretch. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/AntigravityAdapterV2.test.ts | 75 +++++++++++++++++++ .../Adapters/AntigravityAdapterV2.ts | 6 +- .../src/provider/acp/AntigravityProtocol.ts | 6 ++ packages/provider-acp/src/server/adapter.ts | 63 +++++++++------- 4 files changed, 123 insertions(+), 27 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts index 227117ba1893c..5cbb4975e5e57 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts @@ -1393,6 +1393,81 @@ describe("AntigravityAdapterV2 work after the prompt ends", () => { }).pipe(Effect.provide(layerSession)), ); + it.effect("keeps a silent scheduled wake in its run past the command hold bound", () => + Effect.gen(function* () { + const { usage, offers, send, startTurn, until, throughPromptSettled } = + yield* openSessionWithRunningCommand({}, undefined, undefined, { + initialToolCalls: [ + { + sessionUpdate: "tool_call", + toolCallId: "call_schedule", + kind: "other", + title: "Run schedule", + status: "in_progress", + rawInput: { DurationSeconds: 3600, Prompt: "Continue the task" }, + }, + ], + }); + yield* startTurn(1, false); + yield* throughPromptSettled; + yield* TestClock.adjust("61 minutes"); + yield* send({ + sessionUpdate: "tool_call_update", + toolCallId: "call_schedule", + status: "completed", + }); + yield* send({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "Picked the task back up." }, + }); + yield* send(usage); + yield* TestClock.adjust("3 seconds"); + const run = yield* until((event) => event.type === "turn.terminal"); + assert.include( + JSON.stringify(run), + "Picked the task back up.", + "the wake reply stays in the run", + ); + assert.lengthOf(offers, 0); + }).pipe(Effect.provide(layerSession)), + ); + + it.effect("keeps an unanswered question open past the command hold bound", () => + Effect.gen(function* () { + const { command, usage, send, startTurn, until, throughPromptSettled, requestPermission } = + yield* openSessionWithRunningCommand({ WaitMsBeforeAsync: 10000 }); + yield* startTurn(1, false); + yield* throughPromptSettled; + const question = yield* requestPermission( + { + sessionId: "mock-session-1", + toolCall: { toolCallId: "interaction_1", kind: "other", title: "Which port?" }, + options: [ + { optionId: "3000", name: "3000", kind: "allow_once" }, + { optionId: "8080", name: "8080", kind: "allow_once" }, + ], + }, + { requestId: "question-1", method: "session/request_permission" }, + ).pipe(Effect.forkScoped({ startImmediately: true })); + yield* TestClock.adjust("31 minutes"); + yield* Fiber.interrupt(question); + yield* send({ + sessionUpdate: "tool_call_update", + ...command, + status: "completed", + rawOutput: { output: "Command exited with code 0.", exit_code: 0 }, + }); + yield* send({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "Here is the chart." }, + }); + yield* send(usage); + yield* TestClock.adjust("3 seconds"); + const run = yield* until((event) => event.type === "turn.terminal"); + assert.include(JSON.stringify(run), "Here is the chart.", "the question kept the run open"); + }).pipe(Effect.provide(layerSession)), + ); + // Command 2 finishes during reply 1; agy dequeues command 2 after reply 1's usage_update and // needs >3 s before streaming reply 2. Wait for reply 2 (bounded by the 2-minute quiet limit), // but still finalize at 3 s when both commands were batched into reply 1. diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts index 749ca1c8a6cf4..c20afa04e31b8 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts @@ -41,6 +41,7 @@ import { watchAntigravityPromptOutcome } from "../../provider/acp/AntigravityTur import { antigravityApprovalOptions, antigravityToolOutlivesPrompt, + antigravityCommandOutlivesPrompt, antigravitySubagentOutput, classifyAntigravitySubagentToolCall, extractAntigravityUserInputQuestion, @@ -246,7 +247,10 @@ export function makeAntigravityAcpAdapterFlavor( toolOutlivesPrompt: antigravityToolOutlivesPrompt, // agy sometimes loses the terminal update or wake for a background command, which would // hold the turn in Working forever. 30 minutes still leaves long commands room. - postSettleBackgroundHoldTimeout: "30 minutes", + postSettleCommandHold: { + timeout: "30 minutes", + isCommand: antigravityCommandOutlivesPrompt, + }, // session/cancel ends the agent's running commands, so only Stop sends it once the prompt // has ended. A message sent while a command runs leaves the command alone. preserveRuntimeOnSettledInterrupt: true, diff --git a/apps/server/src/provider/acp/AntigravityProtocol.ts b/apps/server/src/provider/acp/AntigravityProtocol.ts index c718667dcf42e..2e59a78e376b4 100644 --- a/apps/server/src/provider/acp/AntigravityProtocol.ts +++ b/apps/server/src/provider/acp/AntigravityProtocol.ts @@ -55,6 +55,12 @@ const ANTIGRAVITY_SCHEDULE_TITLES = new Set(["Run schedule", "Running schedule"] */ export function antigravityToolOutlivesPrompt(toolCall: AcpToolCallState): boolean { if (toolCall.title !== undefined && ANTIGRAVITY_SCHEDULE_TITLES.has(toolCall.title)) return true; + return antigravityCommandOutlivesPrompt(toolCall); +} + +/** A non-daemon command the agent awaits; unlike a schedule, it reports progress while it runs. */ +export function antigravityCommandOutlivesPrompt(toolCall: AcpToolCallState): boolean { + if (toolCall.title !== undefined && ANTIGRAVITY_SCHEDULE_TITLES.has(toolCall.title)) return false; const input = toolCall.data.rawInput; return toolCall.kind === "execute" && !(Predicate.isObject(input) && input.IsDaemon === true); } diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 500cdfabce4a6..908c99e4eaeb3 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -638,10 +638,14 @@ export interface AcpAdapterV2Flavor { */ readonly toolOutlivesPrompt?: (toolCall: AcpToolCallState) => boolean; /** - * Finalize a settled turn held by an outliving tool once the session has been silent this long, - * so a command that never reports back cannot leave the thread in Working forever. + * Finalize a settled turn held only by running commands once the session has been silent for + * `timeout`, so a command that never reports back cannot leave the thread in Working forever. + * Other holds (questions, schedules, subagents) keep the turn open as before. */ - readonly postSettleBackgroundHoldTimeout?: Duration.Input; + readonly postSettleCommandHold?: { + readonly timeout: Duration.Input; + readonly isCommand: (toolCall: AcpToolCallState) => boolean; + }; readonly isProviderWakeNotification?: ( notification: EffectAcpSchema.SessionNotification, ) => boolean; @@ -2749,7 +2753,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( Effect.fnUntraced(function* (notification?: EffectAcpSchema.SessionNotification) { if ( promptInactivityTimeoutMillis === undefined && - flavor.postSettleBackgroundHoldTimeout === undefined + flavor.postSettleCommandHold === undefined ) { return; } @@ -4284,11 +4288,15 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const isMonitorEndNoticeText = (text: string): boolean => /Monitor\s+["']?[0-9a-f-]{8,}/i.test(text) && /ended/i.test(text); - const hasDeferredBackgroundWork = (context: ActiveAcpTurn): boolean => { + const hasDeferredBackgroundWork = ( + context: ActiveAcpTurn, + ignoreTool?: (tool: AcpToolCallState) => boolean, + ): boolean => { if (flavor.wakeOnDetachedRequest && agentRequests.size > 0) return true; if (flavor.toolOutlivesPrompt !== undefined) { // A tool that never started has nothing to report later. for (const tool of context.tools.values()) { + if (ignoreTool?.(tool) === true) continue; if (toolStatus(tool.status) === "running" && flavor.toolOutlivesPrompt(tool)) return true; } @@ -8847,30 +8855,33 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const schedulePostSettleHoldExpiry = Effect.fn("AcpAdapterV2.schedulePostSettleHoldExpiry")( function* (context: ActiveAcpTurn) { - const timeout = flavor.postSettleBackgroundHoldTimeout; - if (timeout === undefined) return; + const hold = flavor.postSettleCommandHold; + if (hold === undefined) return; if (!context.promptSettled || context.finalized) return; - const timeoutMillis = Duration.toMillis(timeout); - const armedAt = yield* Clock.currentTimeMillis; - // Armed even without held work yet: a pending reply can still start the command. + const timeoutMillis = Duration.toMillis(hold.timeout); + const isRunningCommand = (tool: AcpToolCallState) => + toolStatus(tool.status) === "running" && hold.isCommand(tool); + let quietSince = yield* Clock.currentTimeMillis; + // Armed even without a command yet: a pending reply can still start one. yield* Effect.gen(function* () { - while (true) { - const quietSince = Math.max(armedAt, context.lastProgressAt); - const remaining = quietSince + timeoutMillis - (yield* Clock.currentTimeMillis); - if (remaining <= 0) break; - yield* Effect.sleep(Duration.millis(remaining)); + while (!context.finalized) { + quietSince = Math.max(quietSince, context.lastProgressAt); + const now = yield* Clock.currentTimeMillis; + const remaining = quietSince + timeoutMillis - now; + if (remaining > 0) { + yield* Effect.sleep(Duration.millis(remaining)); + continue; + } + if ( + context.tools.values().some(isRunningCommand) && + !hasDeferredBackgroundWork(context, isRunningCommand) + ) { + yield* finalizeTurn(context, context.promptSettledStatus ?? "completed"); + return; + } + // Something else still holds the turn; count silence again once it clears. + quietSince = now; } - if (context.finalized || !context.promptSettled) return; - if (!hasDeferredBackgroundWork(context)) return; - // Only an outliving tool gets bounded here. Queued wakes, hydration and detached - // subagents still have their own settlement paths. - const heldCommand = [...context.tools.values()].some( - (tool) => - toolStatus(tool.status) === "running" && - flavor.toolOutlivesPrompt?.(tool) === true, - ); - if (!heldCommand) return; - yield* finalizeTurn(context, context.promptSettledStatus ?? "completed"); }).pipe(Effect.forkIn(sessionScope), Effect.asVoid); }, );