@@ -1408,6 +1408,7 @@ interface ActiveAcpTurn {
14081408 promptSettledStatus : "completed" | "interrupted" | "failed" | "cancelled" | null ;
14091409 workAccepted : boolean ;
14101410 readonly promptAcceptanceText : string | null ;
1411+ readonly continuationBufferLength : number ;
14111412 readonly promptWireSettled : Deferred . Deferred < void , never > ;
14121413 /** Completed once `session/prompt` is the runtime's active prompt. */
14131414 readonly promptDispatched : Deferred . Deferred < void , never > ;
@@ -1906,6 +1907,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
19061907 EffectAcpSchema . SessionUpdate ,
19071908 { sessionUpdate : "state_update" }
19081909 > | null = null ;
1910+ // Replay mutates agentWork; retain the newest native foreground state separately.
1911+ let foregroundWork : Extract <
1912+ EffectAcpSchema . SessionUpdate ,
1913+ { sessionUpdate : "state_update" }
1914+ > | null = null ;
19091915 const agentRequests = new Set < object > ( ) ;
19101916 const nativeChildren = new Map < string , AcpNativeChildSession > ( ) ;
19111917 const extractSubagentUpdate = ( tool : AcpToolCallState ) =>
@@ -1942,6 +1948,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
19421948 flavor . followAgentWorkState === true &&
19431949 setup . initializeResult . protocolVersion === 2 ;
19441950 agentWork = null ;
1951+ foregroundWork = null ;
19451952 agentRequests . clear ( ) ;
19461953 nativeChildren . clear ( ) ;
19471954 yield * Ref . set ( activeSessionSetup , setup ) ;
@@ -6846,6 +6853,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
68466853 ) {
68476854 context . workAccepted = true ;
68486855 agentWork = { sessionUpdate : "state_update" , state : "running" } ;
6856+ foregroundWork = agentWork ;
68496857 const continuationAlreadyRequested = yield * Ref . get ( continuationRequested ) ;
68506858 yield * Ref . update ( wakeBuffer , ( buffer ) =>
68516859 ! continuationAlreadyRequested &&
@@ -6859,15 +6867,17 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
68596867 }
68606868 return ;
68616869 }
6870+ const update = {
6871+ sessionUpdate : "state_update" as const ,
6872+ state : notice . state ,
6873+ ...( notice . state === "idle" && notice . stopReason !== undefined
6874+ ? { stopReason : notice . stopReason }
6875+ : { } ) ,
6876+ } ;
6877+ foregroundWork = update ;
68626878 yield * handleSessionUpdate ( {
68636879 sessionId : notice . sessionId ,
6864- update : {
6865- sessionUpdate : "state_update" ,
6866- state : notice . state ,
6867- ...( notice . state === "idle" && notice . stopReason !== undefined
6868- ? { stopReason : notice . stopReason }
6869- : { } ) ,
6870- } ,
6880+ update,
68716881 } ) ;
68726882 } ) ,
68736883 ) . pipe (
@@ -8297,6 +8307,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
82978307 promptSettled : false ,
82988308 promptSettledStatus : null ,
82998309 workAccepted : false ,
8310+ continuationBufferLength : continuationWasRequested
8311+ ? ( yield * Ref . get ( wakeBuffer ) ) . length
8312+ : 0 ,
83008313 promptAcceptanceText :
83018314 promptParts ?. prompt . find ( ( block ) => block . type === "text" ) ?. text ?? null ,
83028315 promptWireSettled,
@@ -8400,14 +8413,20 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
84008413 yield * runtimeCallbackPermit . withPermit (
84018414 Effect . gen ( function * ( ) {
84028415 const liveWork = agentWork ;
8416+ const latestWork = canFollowForegroundEvents ? foregroundWork : liveWork ;
84038417 const buffered = yield * takeWakeBuffer ;
84048418 yield * Effect . forEach ( buffered , handleSessionUpdate , { discard : true } ) ;
84058419 const bufferedWork = canFollowForegroundEvents
84068420 ? buffered . findLast ( ( item ) => item . update . sessionUpdate === "state_update" )
84078421 ?. update
84088422 : undefined ;
8423+ // A later user's cancelled idle cannot cancel a wake that already ended.
84098424 agentWork =
8410- bufferedWork ?. sessionUpdate === "state_update" ? bufferedWork : liveWork ;
8425+ latestWork ?. state === "idle" &&
8426+ bufferedWork ?. sessionUpdate === "state_update" &&
8427+ bufferedWork . state === "idle"
8428+ ? bufferedWork
8429+ : latestWork ;
84118430 context . workAccepted = true ;
84128431 yield * settleIdleWork ( context ) ;
84138432 agentWork = liveWork ;
@@ -8523,12 +8542,21 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
85238542 return ;
85248543 if ( canFollowForegroundEvents && ! context . workAccepted ) {
85258544 context . workAccepted = true ;
8526- if ( ! ( yield * Ref . get ( continuationRequested ) ) ) {
8527- const buffered = yield * Ref . getAndSet ( wakeBuffer , [ ] ) ;
8528- for ( const notification of buffered ) {
8545+ const preservedLength = ( yield * Ref . get ( continuationRequested ) )
8546+ ? context . continuationBufferLength
8547+ : 0 ;
8548+ const buffered = yield * Ref . modify ( wakeBuffer , ( current ) => [
8549+ current . slice ( preservedLength ) ,
8550+ current . slice ( 0 , preservedLength ) ,
8551+ ] ) ;
8552+ for ( const notification of buffered ) {
8553+ if ( notification . update . sessionUpdate !== "state_update" )
85298554 yield * handleSessionUpdate ( notification ) ;
8530- }
85318555 }
8556+ const latestState = buffered . findLast (
8557+ ( notification ) => notification . update . sessionUpdate === "state_update" ,
8558+ ) ;
8559+ if ( latestState !== undefined ) yield * handleSessionUpdate ( latestState ) ;
85328560 if ( context . finalized ) return ;
85338561 }
85348562 // ACP v2 reports a failed prompt as an idle `error`, not an RPC error.
0 commit comments