@@ -279,7 +279,11 @@ interface ChildContext {
279279 nextOrdinal : number ;
280280 flushScheduled : boolean ;
281281 status : OrchestrationV2TurnItemStatus ;
282+ /** True until the first transcript read settles; the result waits for it. */
283+ transcriptLoading : boolean ;
282284 lastAssistantText ?: string ;
285+ /** The subagent's final result, once it settled. */
286+ result ?: string ;
283287}
284288
285289const nativeRef = ( nativeId : string ) => ( {
@@ -692,30 +696,57 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
692696 } ) ;
693697 yield * emit ( { type : "turn_item.updated" , driver : MUSE_PROVIDER , turnItem } ) ;
694698 } ) ;
699+ /**
700+ * Shows a settled subagent's result in its child thread, unless the child's
701+ * transcript already ends with it. Waits for the first transcript read so a
702+ * slow read cannot show the final answer twice.
703+ */
704+ const publishChildResult = Effect . fnUntraced ( function * ( context : ChildContext ) {
705+ const result = context . result ;
706+ if ( context . transcriptLoading || ! result ?. trim ( ) ) return ;
707+ if ( context . lastAssistantText ?. trim ( ) === result . trim ( ) ) return ;
708+ yield * publishChildMessage ( context , "assistant" , result , "result" , yield * DateTime . now ) ;
709+ } ) ;
710+ /** A child session's items; fails when the host cannot address it. */
711+ const readChildItems = ( context : ChildContext ) =>
712+ request (
713+ "session/read" ,
714+ { sessionId : context . childSessionId , excludeItems : false } ,
715+ false ,
716+ ) . pipe (
717+ Effect . flatMap ( ( value ) => decode ( MuseSessionResult , value ) ) ,
718+ Effect . map ( ( result ) =>
719+ result . session . sessionId === context . childSessionId
720+ ? ( result . history ?. items ?? result . history ?. snapshot ?. state . items ?? [ ] )
721+ : [ ] ,
722+ ) ,
723+ ) ;
724+ /** Publishes read-back child items newer than what the thread shows, then the result. */
725+ const applyChildItems = ( context : ChildContext , items : ReadonlyArray < MuseItem > ) =>
726+ Effect . gen ( function * ( ) {
727+ for ( const item of items ) {
728+ const previous = context . items . get ( item . itemId ) ;
729+ if ( previous && previous . revision >= item . revision ) continue ;
730+ context . items . set ( item . itemId , item ) ;
731+ yield * publishChildItem ( context , item ) ;
732+ }
733+ context . transcriptLoading = false ;
734+ yield * publishChildResult ( context ) ;
735+ } ) . pipe ( eventPermit . withPermits ( 1 ) ) ;
695736 /**
696737 * Follows a child's own session: subscribe first so nothing is missed, then
697738 * read its history back. Hosts that cannot address an inline child session
698739 * yet (sessionNotFound) leave the prompt/result-only thread in place.
699740 */
700741 const attachChildTranscript = ( context : ChildContext ) : Effect . Effect < void > =>
701- Effect . gen ( function * ( ) {
702- yield * request ( "view/subscribe" , { sessionId : context . childSessionId } , false ) ;
703- const result = yield * request (
704- "session/read" ,
705- { sessionId : context . childSessionId , excludeItems : false } ,
706- false ,
707- ) . pipe ( Effect . flatMap ( ( value ) => decode ( MuseSessionResult , value ) ) ) ;
708- if ( result . session . sessionId !== context . childSessionId ) return ;
709- const items = result . history ?. items ?? result . history ?. snapshot ?. state . items ?? [ ] ;
710- yield * Effect . gen ( function * ( ) {
711- for ( const item of items ) {
712- const previous = context . items . get ( item . itemId ) ;
713- if ( previous && previous . revision >= item . revision ) continue ;
714- context . items . set ( item . itemId , item ) ;
715- yield * publishChildItem ( context , item ) ;
716- }
717- } ) . pipe ( eventPermit . withPermits ( 1 ) ) ;
718- } ) . pipe ( Effect . ignore , Effect . forkIn ( scope ) , Effect . asVoid ) ;
742+ request ( "view/subscribe" , { sessionId : context . childSessionId } , false ) . pipe (
743+ Effect . andThen ( readChildItems ( context ) ) ,
744+ Effect . orElseSucceed ( ( ) : ReadonlyArray < MuseItem > => [ ] ) ,
745+ Effect . flatMap ( ( items ) => applyChildItems ( context , items ) ) ,
746+ Effect . ignore ,
747+ Effect . forkIn ( scope ) ,
748+ Effect . asVoid ,
749+ ) ;
719750 /** Creates the child thread for a native subagent the first time its session id is known. */
720751 const ensureChildContext = Effect . fnUntraced ( function * (
721752 scope : SubagentParentScope ,
@@ -768,6 +799,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
768799 nextOrdinal : 1 ,
769800 flushScheduled : false ,
770801 status,
802+ transcriptLoading : true ,
771803 } ;
772804 childrenBySessionId . set ( childSessionId , context ) ;
773805 childrenByItemId . set ( item . itemId , context ) ;
@@ -883,14 +915,10 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
883915 } ) ;
884916 if ( context ) {
885917 if ( context . status !== status ) yield * publishChildRoot ( context , status , time ) ;
886- // The result is already the child's last assistant message when its
887- // transcript attached; only synthesize it when it did not.
888- const resultShown =
889- result !== null &&
890- context . lastAssistantText !== undefined &&
891- context . lastAssistantText . trim ( ) === result . trim ( ) ;
892- if ( settled && result !== null && result . trim ( ) && ! resultShown )
893- yield * publishChildMessage ( context , "assistant" , result , "result" , time ) ;
918+ if ( settled && result !== null ) {
919+ context . result = result ;
920+ yield * publishChildResult ( context ) ;
921+ }
894922 }
895923 } ) ;
896924 /** Lists running workflows and subagents on the thread, so it shows it still waits on them. */
@@ -1520,6 +1548,15 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
15201548 method : string ,
15211549 params : Record < string , unknown > ,
15221550 ) {
1551+ if ( method === "view/gap" ) {
1552+ // Some child notifications were lost; read the child session back to catch up.
1553+ yield * readChildItems ( context ) . pipe (
1554+ Effect . flatMap ( ( items ) => applyChildItems ( context , items ) ) ,
1555+ Effect . ignore ,
1556+ Effect . forkIn ( scope ) ,
1557+ ) ;
1558+ return ;
1559+ }
15231560 if ( [ "item/started" , "item/updated" , "item/completed" ] . includes ( method ) ) {
15241561 const item = ( yield * decode ( MuseItemEvent , params ) ) . item ;
15251562 const previous = context . items . get ( item . itemId ) ;
0 commit comments