@@ -23,6 +23,7 @@ import * as Cause from "effect/Cause";
2323import * as DateTime from "effect/DateTime" ;
2424import * as Deferred from "effect/Deferred" ;
2525import * as Effect from "effect/Effect" ;
26+ import * as Option from "effect/Option" ;
2627import * as FileSystem from "effect/FileSystem" ;
2728import * as Queue from "effect/Queue" ;
2829import * as Schema from "effect/Schema" ;
@@ -218,6 +219,8 @@ interface ActiveTurn {
218219 readonly input : ProviderAdapterV2TurnInput ;
219220 providerTurn : OrchestrationV2ProviderTurn ;
220221 readonly nativeId : string ;
222+ /** Turns Muse started on its own while this run was active, which this run shows. */
223+ readonly joined : Set < string > ;
221224 readonly items : Map < string , MuseItem > ;
222225 readonly ordinals : Map < string , number > ;
223226 readonly started : Map < string , DateTime . Utc > ;
@@ -685,12 +688,14 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
685688 const failed = host ;
686689 yield * Effect . tryPromise ( ( ) => failed . close ( ) ) . pipe ( Effect . ignore , Effect . forkIn ( scope ) ) ;
687690 } ) ;
691+ const owns = ( turn : ActiveTurn , turnId : string ) =>
692+ turnId === turn . nativeId || turn . joined . has ( turnId ) ;
688693 const publishRequest = Effect . fnUntraced ( function * ( native : PendingRequest [ "native" ] ) {
689694 const turn = active ;
690695 if (
691696 ! turn ||
692697 native . value . sessionId !== nativeSessionId ||
693- ( native . value . turnId && native . value . turnId !== turn . nativeId )
698+ ( native . value . turnId && ! owns ( turn , native . value . turnId ) )
694699 )
695700 return ;
696701 const nativeId =
@@ -847,19 +852,34 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
847852 }
848853 }
849854 // Muse starts a turn on its own when a workflow finishes, to report its result.
850- // Hold it and ask the orchestrator for a run; that run takes the held events.
851- if ( method === "turn/started" && typeof params . turnId === "string" && ! active && ! wake ) {
852- wake = { nativeId : params . turnId , events : [ ] } ;
853- const reports = finishedBackground . splice ( 0 ) ;
854- const notification = backgroundWorkNotification ( reports ) ;
855- if ( thread ?. appThreadId && options . continuationRequests )
856- yield * options . continuationRequests . offer ( {
857- threadId : thread . appThreadId ,
858- providerThreadId : thread . id ,
859- driver : MUSE_PROVIDER ,
860- detail : null ,
861- ...( notification ? { notification } : { } ) ,
862- } ) ;
855+ // During a run, that run shows it. Otherwise hold it and ask the orchestrator for
856+ // a run; that run takes the held events, unless a user turn takes them first.
857+ if (
858+ method === "turn/started" &&
859+ typeof params . turnId === "string" &&
860+ params . turnId !== active ?. nativeId &&
861+ params . turnId !== wake ?. nativeId
862+ ) {
863+ if ( active && ! active . compact ) active . joined . add ( params . turnId ) ;
864+ else if ( ! active && ! wake ) {
865+ const held = { nativeId : params . turnId , events : [ ] } ;
866+ wake = held ;
867+ const notification = backgroundWorkNotification ( finishedBackground . splice ( 0 ) ) ;
868+ if ( thread ?. appThreadId && options . continuationRequests )
869+ yield * options . continuationRequests . offer ( {
870+ threadId : thread . appThreadId ,
871+ providerThreadId : thread . id ,
872+ driver : MUSE_PROVIDER ,
873+ detail : null ,
874+ ...( notification ? { notification } : { } ) ,
875+ dispatchIfCurrent : ( dispatch ) =>
876+ wake === held ? Effect . map ( dispatch , Option . some ) : Effect . succeed ( Option . none ( ) ) ,
877+ clearIfCurrent : ( ) =>
878+ Effect . sync ( ( ) => {
879+ if ( wake === held ) wake = undefined ;
880+ } ) ,
881+ } ) ;
882+ }
863883 }
864884 if ( wake && active ?. nativeId !== wake . nativeId ) {
865885 const turnId = typeof params . turnId === "string" ? params . turnId : itemEvent ?. turnId ;
@@ -870,7 +890,7 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
870890 }
871891 const turn = active ;
872892 if ( ! turn ) return ;
873- if ( typeof params . turnId === "string" && params . turnId !== turn . nativeId && ! turn . compact )
893+ if ( typeof params . turnId === "string" && ! owns ( turn , params . turnId ) && ! turn . compact )
874894 return ;
875895 switch ( method ) {
876896 case "item/started" :
@@ -879,7 +899,7 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
879899 const item = itemEvent ! ;
880900 if (
881901 item . turnId &&
882- item . turnId !== turn . nativeId &&
902+ ! owns ( turn , item . turnId ) &&
883903 ! ( turn . compact && item . kind === "compaction" )
884904 )
885905 return ;
@@ -933,6 +953,8 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
933953 case "turn/completed" : {
934954 if ( turn . compact ) return ;
935955 const result = yield * decode ( MuseTurnCompleted , params ) ;
956+ // A joined Muse turn ending does not end this run; its own turn does.
957+ if ( result . turnId !== turn . nativeId ) break ;
936958 if ( result . usage ) {
937959 const usage = result . usage ;
938960 turn . providerTurn = {
@@ -1487,6 +1509,7 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
14871509 input : turnInput ,
14881510 providerTurn,
14891511 nativeId,
1512+ joined : new Set ( ) ,
14901513 items : new Map ( ) ,
14911514 ordinals : new Map ( ) ,
14921515 started : new Map ( ) ,
@@ -1513,13 +1536,14 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
15131536 lastRunOrdinal : turnInput . runOrdinal ,
15141537 } ) ;
15151538 yield * updateSession ( "running" ) ;
1516- if ( continuation ) {
1517- wake = undefined ;
1518- // Its turn may have ended already; then the held turn/completed ends this run.
1519- if ( ! adopted ) return yield * finish ( turn , "completed" ) ;
1520- for ( const [ method , data ] of adopted . events )
1521- yield * handleNotification ( method , data ) . pipe ( Effect . catch ( failHost ) ) ;
1522- }
1539+ // A user turn takes a held Muse turn: Muse runs it first, so this run shows it
1540+ // (and its approvals), and the continuation asked for it is no longer needed.
1541+ const held = wake ;
1542+ wake = undefined ;
1543+ if ( continuation && ! held ) return yield * finish ( turn , "completed" ) ;
1544+ if ( held && ! continuation ) turn . joined . add ( held . nativeId ) ;
1545+ for ( const [ method , data ] of held ?. events ?? [ ] )
1546+ yield * handleNotification ( method , data ) . pipe ( Effect . catch ( failHost ) ) ;
15231547 } ) . pipe ( eventPermit . withPermits ( 1 ) ) ;
15241548 if ( continuation ) return ;
15251549 yield * Effect . gen ( function * ( ) {
@@ -1732,6 +1756,9 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
17321756 ) ;
17331757 return ;
17341758 }
1759+ // Muse runs a joined turn first, so Stop must end it too.
1760+ for ( const joined of turn . joined )
1761+ yield * request ( "turn/interrupt" , { turnId : joined } ) . pipe ( Effect . ignore ) ;
17351762 yield * request ( "turn/interrupt" , { turnId : turn . nativeId } ) ;
17361763 yield * Deferred . await ( turn . done ) . pipe (
17371764 Effect . timeout ( options . requestTimeoutMs ?? 30_000 ) ,
0 commit comments