@@ -834,28 +834,156 @@ describe("MuseAdapterV2", () => {
834834 } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
835835 ) ;
836836
837- it . effect ( "approves a workflow child's approval in full access after its turn ended" , ( ) =>
837+ it . effect . each ( [
838+ { when : "after its turn ended" , turnEnded : true } ,
839+ { when : "while its turn runs" , turnEnded : false } ,
840+ ] as const ) (
841+ "approves a workflow child's approval in full access $when without showing it" ,
842+ ( { turnEnded } ) =>
843+ Effect . gen ( function * ( ) {
844+ const fake = yield * makeFakeMuse ( ) ;
845+ const harness = yield * makeHarness ( fake ) ;
846+ const { nativeId } = yield * startConversation ( harness , fake ) ;
847+ if ( turnEnded ) {
848+ yield * fake . emit ( "turn/completed" , { turnId : nativeId , terminal : "completed" } ) ;
849+ yield * harness . takeEvent ( "turn.terminal" ) ;
850+ }
851+ const acknowledgement = pendingPromise < Record < string , unknown > > ( ) ;
852+ fake . queueResponse ( "approval/decide" , acknowledgement . promise ) ;
853+ // Muse runs workflow children without the session's allowAll mode.
854+ const childApproval = { ...approval ( "child-run-1" ) , subagentOrigin : { subagentId : "a7" } } ;
855+ yield * fake . emit ( "approval/requested" , childApproval ) ;
856+ const decision = yield * fake . takeCall ( "approval/decide" ) ;
857+ assert . strictEqual ( decision . params . choiceId , "once" ) ;
858+ const snapshot = yield * harness . runtime . readThreadSnapshot ! ( {
859+ providerThread : harness . providerThread ,
860+ } ) ;
861+ assert . deepStrictEqual ( snapshot . runtimeRequests , [ ] ) ;
862+ yield * fake . emit ( "approval/resolved" , childApproval ) ;
863+ const resolved = yield * harness . takeEvent ( "runtime_request.updated" ) ;
864+ assert . strictEqual ( resolved . runtimeRequest . status , "resolved" ) ;
865+ assert . strictEqual ( resolved . runtimeRequest . decision , "accept" ) ;
866+ assert . isNull ( resolved . runtimeRequest . providerTurnId ) ;
867+ acknowledgement . resolve ( { } ) ;
868+ assert . isFalse (
869+ harness . allEvents . some (
870+ ( event ) =>
871+ ( event . type === "runtime_request.updated" &&
872+ event . runtimeRequest . status === "pending" ) ||
873+ ( event . type === "provider_session.updated" &&
874+ event . providerSession . status === "waiting" ) ,
875+ ) ,
876+ ) ;
877+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
878+ ) ;
879+
880+ it . effect ( "answers a workflow child's updated approval in full access without showing it" , ( ) =>
838881 Effect . gen ( function * ( ) {
839882 const fake = yield * makeFakeMuse ( ) ;
840883 const harness = yield * makeHarness ( fake ) ;
841- const { nativeId } = yield * startConversation ( harness , fake ) ;
842- yield * fake . emit ( "turn/completed" , { turnId : nativeId , terminal : "completed" } ) ;
843- yield * harness . takeEvent ( "turn.terminal" ) ;
844- // Muse runs workflow children without the session's allowAll mode.
884+ yield * startConversation ( harness , fake ) ;
885+ const acknowledgement = pendingPromise < Record < string , unknown > > ( ) ;
886+ fake . queueResponse ( "approval/decide" , acknowledgement . promise ) ;
887+ const childApproval = { ...approval ( "child-run-1" ) , subagentOrigin : { subagentId : "a7" } } ;
888+ yield * fake . emit ( "approval/requested" , childApproval ) ;
889+ yield * fake . takeCall ( "approval/decide" ) ;
890+ // Muse moves to the next requirement while T3's first answer is in flight.
891+ yield * fake . emit ( "approval/updated" , {
892+ ...childApproval ,
893+ currentRequirementId : { approvalId : "approval-1" , sourceIndex : 1 } ,
894+ } ) ;
895+ const second = yield * fake . takeCall ( "approval/decide" ) ;
896+ assert . deepStrictEqual ( second . params . requirementId , {
897+ approvalId : "approval-1" ,
898+ sourceIndex : 1 ,
899+ } ) ;
900+ acknowledgement . reject ( new Error ( "approval requirement changed" ) ) ;
901+ yield * fake . emit ( "approval/resolved" , childApproval ) ;
902+ const resolved = yield * harness . takeEvent ( "runtime_request.updated" ) ;
903+ assert . strictEqual ( resolved . runtimeRequest . status , "resolved" ) ;
904+ assert . strictEqual ( resolved . runtimeRequest . decision , "accept" ) ;
905+ assert . isFalse (
906+ harness . allEvents . some (
907+ ( event ) =>
908+ event . type === "runtime_request.updated" && event . runtimeRequest . status === "pending" ,
909+ ) ,
910+ ) ;
911+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
912+ ) ;
913+
914+ it . effect ( "shows a workflow child's approval when Muse rejects T3's automatic answer" , ( ) =>
915+ Effect . gen ( function * ( ) {
916+ const fake = yield * makeFakeMuse ( ) ;
917+ const harness = yield * makeHarness ( fake ) ;
918+ yield * startConversation ( harness , fake ) ;
919+ fake . queueResponse ( "approval/decide" , new Error ( "approval requirement changed" ) ) ;
845920 const childApproval = { ...approval ( "child-run-1" ) , subagentOrigin : { subagentId : "a7" } } ;
846921 yield * fake . emit ( "approval/requested" , childApproval ) ;
847922 const asked = yield * harness . takeEvent ( "runtime_request.updated" ) ;
848923 assert . strictEqual ( asked . runtimeRequest . status , "pending" ) ;
849- assert . isNull ( asked . runtimeRequest . providerTurnId ) ;
850- const decision = yield * fake . takeCall ( "approval/decide" ) ;
851- assert . strictEqual ( decision . params . choiceId , "once" ) ;
924+ yield * harness . takeEvent (
925+ "provider_session.updated" ,
926+ ( event ) => event . providerSession . status === "waiting" ,
927+ ) ;
928+ yield * harness . runtime . respondToRuntimeRequest ( {
929+ requestId : asked . runtimeRequest . id ,
930+ decision : "decline" ,
931+ } ) ;
932+ assert . deepStrictEqual (
933+ fake . calls
934+ . filter ( ( call ) => call . method === "approval/decide" )
935+ . map ( ( call ) => call . params . choiceId ) ,
936+ [ "once" , "deny" ] ,
937+ ) ;
852938 yield * fake . emit ( "approval/resolved" , childApproval ) ;
853939 const resolved = yield * harness . takeEvent ( "runtime_request.updated" ) ;
854940 assert . strictEqual ( resolved . runtimeRequest . status , "resolved" ) ;
855- assert . strictEqual ( resolved . runtimeRequest . decision , "accept " ) ;
941+ assert . strictEqual ( resolved . runtimeRequest . decision , "decline " ) ;
856942 } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
857943 ) ;
858944
945+ it . effect (
946+ "does not show a workflow child's approval that Muse settled before T3's answer failed" ,
947+ ( ) =>
948+ Effect . gen ( function * ( ) {
949+ const fake = yield * makeFakeMuse ( ) ;
950+ const harness = yield * makeHarness ( fake ) ;
951+ yield * startConversation ( harness , fake ) ;
952+ const acknowledgement = pendingPromise < Record < string , unknown > > ( ) ;
953+ fake . queueResponse ( "approval/decide" , acknowledgement . promise ) ;
954+ const childApproval = { ...approval ( "child-run-1" ) , subagentOrigin : { subagentId : "a7" } } ;
955+ yield * fake . emit ( "approval/requested" , childApproval ) ;
956+ yield * fake . takeCall ( "approval/decide" ) ;
957+ yield * fake . emit ( "approval/resolved" , childApproval ) ;
958+ yield * harness . takeEvent (
959+ "runtime_request.updated" ,
960+ ( event ) => event . runtimeRequest . status === "resolved" ,
961+ ) ;
962+ acknowledgement . reject ( new Error ( "approval already resolved" ) ) ;
963+ // A later child request whose answer fails is shown, after the first failure was handled.
964+ fake . queueResponse ( "approval/decide" , new Error ( "approval requirement changed" ) ) ;
965+ yield * fake . emit ( "approval/requested" , {
966+ ...childApproval ,
967+ approvalId : "approval-2" ,
968+ currentRequirementId : { approvalId : "approval-2" , sourceIndex : 0 } ,
969+ } ) ;
970+ yield * harness . takeEvent (
971+ "runtime_request.updated" ,
972+ ( event ) => event . runtimeRequest . status === "pending" ,
973+ ) ;
974+ assert . deepStrictEqual (
975+ harness . allEvents . flatMap ( ( event ) =>
976+ event . type === "runtime_request.updated"
977+ ? [
978+ `${ event . runtimeRequest . nativeRequestRef ?. nativeId } ${ event . runtimeRequest . status } ` ,
979+ ]
980+ : [ ] ,
981+ ) ,
982+ [ "approval-1 resolved" , "approval-2 pending" ] ,
983+ ) ;
984+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
985+ ) ;
986+
859987 it . effect ( "keeps a workflow child's approval pending for the user past the turn's end" , ( ) =>
860988 Effect . gen ( function * ( ) {
861989 const fake = yield * makeFakeMuse ( ) ;
0 commit comments