@@ -1163,6 +1163,11 @@ interface ActiveAcpTurn {
11631163 readonly pendingSubagentNotifications : Map < string , Array < EffectAcpSchema . SessionNotification > > ;
11641164 /** Background monitor/task id → toolCallId for synthetic root text updates. */
11651165 readonly toolCallIdsByBackgroundTaskId : Map < string , string > ;
1166+ /** Command ends retained until prompt settlement, without marking them reported. */
1167+ readonly backgroundCommandCompletions : Map <
1168+ string ,
1169+ { readonly status : "completed" | "failed" ; readonly output ?: string }
1170+ > ;
11661171 /**
11671172 * Persistent monitors registered this turn. Excluded from deferred-finalize
11681173 * holds so the root turn can settle while they keep streaming post-settle.
@@ -3104,9 +3109,19 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
31043109 yield * closeTextStream ( context , "user" ) ;
31053110 const previous = context . tools . get ( incoming . toolCallId ) ;
31063111 const merged = mergeToolCallState ( previous , incoming ) ;
3107- const toolCall = flavor . normalizeToolCall ?.( merged ) ?? merged ;
3112+ const normalized = flavor . normalizeToolCall ?.( merged ) ?? merged ;
3113+ const backgroundTaskId = flavor . extractBackgroundTaskId ?.( normalized ) ;
3114+ const completion =
3115+ context . promptSettled && backgroundTaskId !== undefined
3116+ ? context . backgroundCommandCompletions . get ( backgroundTaskId )
3117+ : undefined ;
3118+ // A start ACK can still normalize to running after the task ended.
3119+ const toolCall =
3120+ completion !== undefined &&
3121+ ( normalized . status === "pending" || normalized . status === "inProgress" )
3122+ ? { ...normalized , status : completion . status }
3123+ : normalized ;
31083124 context . tools . set ( toolCall . toolCallId , toolCall ) ;
3109- const backgroundTaskId = flavor . extractBackgroundTaskId ?.( toolCall ) ;
31103125 if ( backgroundTaskId !== undefined ) {
31113126 context . toolCallIdsByBackgroundTaskId . set ( backgroundTaskId , toolCall . toolCallId ) ;
31123127 if ( flavor . isPersistentBackgroundTool ?.( toolCall ) === true ) {
@@ -3863,10 +3878,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
38633878
38643879 // A structured task end (Grok `task_completed`) is authoritative for the
38653880 // tool that registered the task: its own updates only ever say running.
3866- // Finish the row while deferred finalize holds a settled root turn open
3867- // for it, so the turn can settle. While the prompt is still open the
3868- // agent reports the end itself (TaskOutput hydration), and an unreported
3869- // end must keep its post-finalize continuation offer.
3881+ // Retain early ends until settlement: closing them via emitTool while
3882+ // the prompt is open would mark an unreported result as handled and
3883+ // suppress its post-finalize continuation.
38703884 const finishRegisteredBackgroundTool = Effect . fnUntraced ( function * ( mutation : {
38713885 readonly taskId : string ;
38723886 readonly status : "completed" | "failed" ;
@@ -5978,6 +5992,15 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
59785992 }
59795993 yield * applyLateBackgroundMutation ( mutation . sessionId , mutation ) ;
59805994 if ( mutation . status !== "running" ) {
5995+ if ( mutation . report ?. kind === "command" ) {
5996+ const context = yield * Ref . get ( activeTurn ) ;
5997+ if ( context !== null && ! context . finalized ) {
5998+ context . backgroundCommandCompletions . set ( mutation . taskId , {
5999+ status : mutation . status ,
6000+ ...( mutation . output === undefined ? { } : { output : mutation . output } ) ,
6001+ } ) ;
6002+ }
6003+ }
59816004 yield * finishRegisteredBackgroundTool ( {
59826005 taskId : mutation . taskId ,
59836006 status : mutation . status ,
@@ -7024,6 +7047,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
70247047 subagentsBySessionId : new Map ( ) ,
70257048 pendingSubagentNotifications : new Map ( ) ,
70267049 toolCallIdsByBackgroundTaskId : new Map ( ) ,
7050+ backgroundCommandCompletions : new Map ( ) ,
70277051 persistentBackgroundTaskIds : new Set ( ) ,
70287052 awaitingBackgroundHydration : new Set ( ) ,
70297053 pendingInjectedReport : new Set ( ) ,
@@ -7191,6 +7215,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
71917215 ? "interrupted"
71927216 : "cancelled"
71937217 : "completed" ;
7218+ if ( flavor . deferFinalizeForBackgroundWork === true && ! context . interrupted ) {
7219+ context . promptSettled = true ;
7220+ context . promptSettledStatus = status ;
7221+ for ( const [ taskId , completion ] of context . backgroundCommandCompletions ) {
7222+ yield * finishRegisteredBackgroundTool ( { taskId, ...completion } ) ;
7223+ }
7224+ }
71947225 // Grok monitors (and async subagents) keep working after the root
71957226 // prompt RPC returns. Defer finalize so their later updates and
71967227 // wake-turn traffic still project onto this run.
@@ -7199,8 +7230,6 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
71997230 ! context . interrupted &&
72007231 hasDeferredBackgroundWork ( context )
72017232 ) {
7202- context . promptSettled = true ;
7203- context . promptSettledStatus = status ;
72047233 // The agent finished this prompt's reply. Background work
72057234 // holds the run open, not the text it already sent.
72067235 yield * closeTextStreams ( context ) ;
0 commit comments