@@ -363,6 +363,8 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
363363 let compactWait :
364364 | { readonly turn : ActiveTurn ; readonly ready : Deferred . Deferred < void > }
365365 | undefined ;
366+ // Before Muse answers session/compact, a compaction item may be ours or a wake's.
367+ const compactCandidates : Array < [ string , unknown ] > = [ ] ;
366368 const emit = ( event : ProviderAdapter . ProviderAdapterV2Event ) =>
367369 Queue . offer ( events , event ) . pipe ( Effect . asVoid ) ;
368370 const decode = < A , I > ( schema : Schema . Codec < A , I > , data : unknown ) =>
@@ -1116,6 +1118,12 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
11161118 } ) ;
11171119 const owns = ( turn : ActiveTurn , turnId : string ) =>
11181120 turnId === turn . nativeId || turn . joined . has ( turnId ) ;
1121+ // A live run no turn owns, such as one started while compaction waited, still asks.
1122+ const unownedRun = ( turnId : unknown ) =>
1123+ typeof turnId === "string" &&
1124+ hostRuns . has ( turnId ) &&
1125+ turnId !== wake ?. nativeId &&
1126+ ! ( active && owns ( active , turnId ) ) ;
11191127 const decideApproval = Effect . fnUntraced ( function * (
11201128 entry : PendingRequest ,
11211129 decision : ProviderApprovalDecision | undefined ,
@@ -1160,7 +1168,10 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
11601168 ) ;
11611169 // Compaction never asks, so requests during it come from runs it waits on.
11621170 const runless =
1163- child || active ?. compact === true || previous ?. request . providerTurnId === null ;
1171+ child ||
1172+ active ?. compact === true ||
1173+ previous ?. request . providerTurnId === null ||
1174+ unownedRun ( native . value . turnId ) ;
11641175 const turn = runless ? undefined : active ;
11651176 if (
11661177 ! thread ||
@@ -1404,6 +1415,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
14041415 if (
14051416 REQUEST_NOTIFICATIONS . has ( method ) &&
14061417 ( active ?. compact ||
1418+ unownedRun ( params . turnId ) ||
14071419 [ ...pending . values ( ) ] . some (
14081420 ( entry ) =>
14091421 entry . request . providerTurnId === null &&
@@ -1439,13 +1451,23 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
14391451 } ) ;
14401452 }
14411453 }
1454+ if (
1455+ active ?. compact &&
1456+ active . compactPhase === "sending" &&
1457+ itemEvent ?. kind === "compaction"
1458+ ) {
1459+ const entry : [ string , unknown ] = [ method , data ] ;
1460+ compactCandidates . push ( entry ) ;
1461+ if ( wake && itemEvent . turnId === wake . nativeId ) wake . events . push ( entry ) ;
1462+ return ;
1463+ }
14421464 if ( wake && active ?. nativeId !== wake . nativeId ) {
14431465 const turnId = typeof params . turnId === "string" ? params . turnId : itemEvent ?. turnId ;
14441466 if (
14451467 turnId === wake . nativeId &&
14461468 ! (
14471469 active ?. compact &&
1448- active . compactPhase !== "waiting " &&
1470+ active . compactPhase === "admitted " &&
14491471 itemEvent ?. kind === "compaction"
14501472 )
14511473 ) {
@@ -1465,7 +1487,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
14651487 if (
14661488 item . turnId &&
14671489 ! owns ( turn , item . turnId ) &&
1468- ! ( turn . compact && turn . compactPhase !== "waiting " && item . kind === "compaction" )
1490+ ! ( turn . compact && turn . compactPhase === "admitted " && item . kind === "compaction" )
14691491 )
14701492 return ;
14711493 const previous = turn . items . get ( item . itemId ) ;
@@ -2076,11 +2098,13 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
20762098 while ( true ) {
20772099 const ended = hostRunsEnded ;
20782100 turn . compactPhase = "sending" ;
2101+ compactCandidates . length = 0 ;
20792102 const admission = yield * request ( "session/compact" , { } , true , commandId ) . pipe (
20802103 Effect . flatMap ( ( value ) => decode ( MuseCompactResult , value ) ) ,
20812104 Effect . catchIf ( isRunActive , ( busy ) =>
20822105 Effect . sync ( ( ) => {
20832106 turn . compactPhase = "waiting" ;
2107+ compactCandidates . length = 0 ;
20842108 return { busy } ;
20852109 } ) ,
20862110 ) ,
@@ -2096,7 +2120,18 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
20962120 return yield * protocolError (
20972121 `Muse returned an unsupported compaction status: ${ admission . status } ` ,
20982122 ) ;
2099- turn . compactPhase = "admitted" ;
2123+ yield * eventPermit . withPermits ( 1 ) (
2124+ Effect . gen ( function * ( ) {
2125+ turn . compactPhase = "admitted" ;
2126+ const candidates = compactCandidates . splice ( 0 ) ;
2127+ if ( active !== turn ) return ;
2128+ for ( const entry of candidates ) {
2129+ const index = wake ? wake . events . indexOf ( entry ) : - 1 ;
2130+ if ( index >= 0 ) wake ?. events . splice ( index , 1 ) ;
2131+ yield * handleNotification ( ...entry ) . pipe ( Effect . catch ( failHost ) ) ;
2132+ }
2133+ } ) ,
2134+ ) ;
21002135 return ;
21012136 }
21022137 const next = yield * eventPermit . withPermits ( 1 ) (
0 commit comments