@@ -220,7 +220,8 @@ interface ActiveTurn {
220220 flushScheduled : boolean ;
221221 interruptRequested : boolean ;
222222 compact : boolean ;
223- compactAdmitted : boolean ;
223+ /** Muse admits compaction only when idle; while waiting, its compaction items are not ours. */
224+ compactPhase : "sending" | "waiting" | "admitted" ;
224225}
225226
226227interface PendingRequest {
@@ -1150,8 +1151,16 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
11501151 */
11511152 const publishRequest = Effect . fnUntraced ( function * ( native : PendingRequest [ "native" ] ) {
11521153 const child = native . type === "approval" && native . value . subagentOrigin !== undefined ;
1154+ const nativeId =
1155+ native . type === "approval" ? native . value . approvalId : native . value . userInputId ;
1156+ const previous = [ ...pending . values ( ) ] . find (
1157+ ( entry ) =>
1158+ entry . native . type === native . type &&
1159+ entry . request . nativeRequestRef ?. nativeId === nativeId ,
1160+ ) ;
11531161 // Compaction never asks, so requests during it come from runs it waits on.
1154- const runless = child || active ?. compact === true ;
1162+ const runless =
1163+ child || active ?. compact === true || previous ?. request . providerTurnId === null ;
11551164 const turn = runless ? undefined : active ;
11561165 if (
11571166 ! thread ||
@@ -1160,14 +1169,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
11601169 ( turn && native . value . turnId && ! owns ( turn , native . value . turnId ) )
11611170 )
11621171 return ;
1163- const nativeId =
1164- native . type === "approval" ? native . value . approvalId : native . value . userInputId ;
11651172 if ( turn ?. settledRequests . has ( `${ native . type } :${ nativeId } ` ) ) return ;
1166- const previous = [ ...pending . values ( ) ] . find (
1167- ( entry ) =>
1168- entry . native . type === native . type &&
1169- entry . request . nativeRequestRef ?. nativeId === nativeId ,
1170- ) ;
11711173 const providerTurnId = turn ?. providerTurn . id ?? null ;
11721174 const threadId = turn ?. input . threadId ?? thread . appThreadId ?? input . threadId ;
11731175 const requestId =
@@ -1397,20 +1399,17 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
13971399 // Child approvals use their own run IDs and can outlive turns; bypass turn routing.
13981400 if ( params . subagentOrigin !== undefined && method . startsWith ( "approval/" ) )
13991401 return yield * handleRequestEvent ( method , params , undefined ) ;
1400- // Runless requests outlive their turn, so their settlement does too.
1401- const settled =
1402- method === "approval/resolved"
1403- ? params . approvalId
1404- : method === "userInput/settled"
1405- ? params . userInputId
1406- : undefined ;
1402+ // Requests during compaction come from runs it waits on, so they must reach the
1403+ // user now; runless requests outlive their turn.
14071404 if (
1408- settled !== undefined &&
1409- [ ...pending . values ( ) ] . some (
1410- ( entry ) =>
1411- entry . request . providerTurnId === null &&
1412- entry . request . nativeRequestRef ?. nativeId === settled ,
1413- )
1405+ REQUEST_NOTIFICATIONS . has ( method ) &&
1406+ ( active ?. compact ||
1407+ [ ...pending . values ( ) ] . some (
1408+ ( entry ) =>
1409+ entry . request . providerTurnId === null &&
1410+ entry . request . nativeRequestRef ?. nativeId ===
1411+ ( params . approvalId ?? params . userInputId ) ,
1412+ ) )
14141413 )
14151414 return yield * handleRequestEvent ( method , params , undefined ) ;
14161415 if (
@@ -1442,14 +1441,16 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
14421441 }
14431442 if ( wake && active ?. nativeId !== wake . nativeId ) {
14441443 const turnId = typeof params . turnId === "string" ? params . turnId : itemEvent ?. turnId ;
1445- if ( turnId === wake . nativeId ) {
1446- // Compaction waits for this run, so its requests must reach the user now.
1447- if ( active ?. compact && REQUEST_NOTIFICATIONS . has ( method ) )
1448- return yield * handleRequestEvent ( method , params , undefined ) ;
1449- if ( ! ( active ?. compact && itemEvent ?. kind === "compaction" ) ) {
1450- wake . events . push ( [ method , data ] ) ;
1451- return ;
1452- }
1444+ if (
1445+ turnId === wake . nativeId &&
1446+ ! (
1447+ active ?. compact &&
1448+ active . compactPhase !== "waiting" &&
1449+ itemEvent ?. kind === "compaction"
1450+ )
1451+ ) {
1452+ wake . events . push ( [ method , data ] ) ;
1453+ return ;
14531454 }
14541455 }
14551456 const turn = active ;
@@ -1464,7 +1465,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
14641465 if (
14651466 item . turnId &&
14661467 ! owns ( turn , item . turnId ) &&
1467- ! ( turn . compact && item . kind === "compaction" )
1468+ ! ( turn . compact && turn . compactPhase !== "waiting" && item . kind === "compaction" )
14681469 )
14691470 return ;
14701471 const previous = turn . items . get ( item . itemId ) ;
@@ -2074,9 +2075,15 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
20742075 let commandId = firstCommandId ;
20752076 while ( true ) {
20762077 const ended = hostRunsEnded ;
2078+ turn . compactPhase = "sending" ;
20772079 const admission = yield * request ( "session/compact" , { } , true , commandId ) . pipe (
20782080 Effect . flatMap ( ( value ) => decode ( MuseCompactResult , value ) ) ,
2079- Effect . catchIf ( isRunActive , ( busy ) => Effect . succeed ( { busy } ) ) ,
2081+ Effect . catchIf ( isRunActive , ( busy ) =>
2082+ Effect . sync ( ( ) => {
2083+ turn . compactPhase = "waiting" ;
2084+ return { busy } ;
2085+ } ) ,
2086+ ) ,
20802087 ) ;
20812088 if ( ! ( "busy" in admission ) ) {
20822089 if ( admission . status === "noop" ) {
@@ -2089,7 +2096,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
20892096 return yield * protocolError (
20902097 `Muse returned an unsupported compaction status: ${ admission . status } ` ,
20912098 ) ;
2092- turn . compactAdmitted = true ;
2099+ turn . compactPhase = "admitted" ;
20932100 return ;
20942101 }
20952102 const next = yield * eventPermit . withPermits ( 1 ) (
@@ -2186,7 +2193,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
21862193 flushScheduled : false ,
21872194 interruptRequested : false ,
21882195 compact,
2189- compactAdmitted : false ,
2196+ compactPhase : "sending" ,
21902197 } ;
21912198 yield * Effect . gen ( function * ( ) {
21922199 thread = turnInput . providerThread ;
@@ -2408,7 +2415,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
24082415 Effect . gen ( function * ( ) {
24092416 if ( active !== turn ) return ;
24102417 // Nothing runs on the host before admission, so the host can stay.
2411- if ( ! turn . compactAdmitted ) return yield * finish ( turn , "interrupted" ) ;
2418+ if ( turn . compactPhase !== "admitted" ) return yield * finish ( turn , "interrupted" ) ;
24122419 hostEpoch ++ ;
24132420 yield * Effect . tryPromise ( {
24142421 try : ( ) => host . close ( ) ,
0 commit comments