@@ -854,27 +854,204 @@ describe("MuseAdapterV2", () => {
854854 } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
855855 ) ;
856856
857- it . effect ( "approves a workflow child's approval in full access after its turn ended" , ( ) =>
857+ it . effect . each ( [
858+ { when : "after its turn ended" , turnEnded : true } ,
859+ { when : "while its turn runs" , turnEnded : false } ,
860+ ] as const ) (
861+ "approves a workflow child's approval in full access $when without showing it" ,
862+ ( { turnEnded } ) =>
863+ Effect . gen ( function * ( ) {
864+ const fake = yield * makeFakeMuse ( ) ;
865+ const harness = yield * makeHarness ( fake ) ;
866+ const { nativeId } = yield * startConversation ( harness , fake ) ;
867+ if ( turnEnded ) {
868+ yield * fake . emit ( "turn/completed" , { turnId : nativeId , terminal : "completed" } ) ;
869+ yield * harness . takeEvent ( "turn.terminal" ) ;
870+ }
871+ const acknowledgement = pendingPromise < Record < string , unknown > > ( ) ;
872+ fake . queueResponse ( "approval/decide" , acknowledgement . promise ) ;
873+ const childApproval = { ...approval ( "child-run-1" ) , subagentOrigin : { subagentId : "a7" } } ;
874+ yield * fake . emit ( "approval/requested" , childApproval ) ;
875+ const decision = yield * fake . takeCall ( "approval/decide" ) ;
876+ assert . strictEqual ( decision . params . choiceId , "once" ) ;
877+ const snapshot = yield * harness . runtime . readThreadSnapshot ! ( {
878+ providerThread : harness . providerThread ,
879+ } ) ;
880+ assert . deepStrictEqual ( snapshot . runtimeRequests , [ ] ) ;
881+ yield * fake . emit ( "approval/resolved" , childApproval ) ;
882+ const resolved = yield * harness . takeEvent ( "runtime_request.updated" ) ;
883+ assert . strictEqual ( resolved . runtimeRequest . status , "resolved" ) ;
884+ assert . strictEqual ( resolved . runtimeRequest . decision , "accept" ) ;
885+ assert . isNull ( resolved . runtimeRequest . providerTurnId ) ;
886+ acknowledgement . resolve ( { } ) ;
887+ assert . isFalse (
888+ harness . allEvents . some (
889+ ( event ) =>
890+ ( event . type === "runtime_request.updated" &&
891+ event . runtimeRequest . status === "pending" ) ||
892+ ( event . type === "provider_session.updated" &&
893+ event . providerSession . status === "waiting" ) ,
894+ ) ,
895+ ) ;
896+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
897+ ) ;
898+
899+ it . effect ( "answers a workflow child's updated approval in full access without showing it" , ( ) =>
858900 Effect . gen ( function * ( ) {
859901 const fake = yield * makeFakeMuse ( ) ;
860902 const harness = yield * makeHarness ( fake ) ;
861- const { nativeId } = yield * startConversation ( harness , fake ) ;
862- yield * fake . emit ( "turn/completed" , { turnId : nativeId , terminal : "completed" } ) ;
863- yield * harness . takeEvent ( "turn.terminal" ) ;
903+ yield * startConversation ( harness , fake ) ;
904+ const acknowledgement = pendingPromise < Record < string , unknown > > ( ) ;
905+ fake . queueResponse ( "approval/decide" , acknowledgement . promise ) ;
906+ const childApproval = { ...approval ( "child-run-1" ) , subagentOrigin : { subagentId : "a7" } } ;
907+ yield * fake . emit ( "approval/requested" , childApproval ) ;
908+ yield * fake . takeCall ( "approval/decide" ) ;
909+ // Muse moves to the next requirement while T3's first answer is in flight.
910+ yield * fake . emit ( "approval/updated" , {
911+ ...childApproval ,
912+ currentRequirementId : { approvalId : "approval-1" , sourceIndex : 1 } ,
913+ } ) ;
914+ const second = yield * fake . takeCall ( "approval/decide" ) ;
915+ assert . deepStrictEqual ( second . params . requirementId , {
916+ approvalId : "approval-1" ,
917+ sourceIndex : 1 ,
918+ } ) ;
919+ acknowledgement . reject ( new Error ( "approval requirement changed" ) ) ;
920+ yield * fake . emit ( "approval/resolved" , childApproval ) ;
921+ const resolved = yield * harness . takeEvent ( "runtime_request.updated" ) ;
922+ assert . strictEqual ( resolved . runtimeRequest . status , "resolved" ) ;
923+ assert . strictEqual ( resolved . runtimeRequest . decision , "accept" ) ;
924+ assert . isFalse (
925+ harness . allEvents . some (
926+ ( event ) =>
927+ event . type === "runtime_request.updated" && event . runtimeRequest . status === "pending" ,
928+ ) ,
929+ ) ;
930+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
931+ ) ;
932+
933+ it . effect (
934+ "stops waiting when a shown approval settles before a workflow child's hidden one" ,
935+ ( ) =>
936+ Effect . gen ( function * ( ) {
937+ const fake = yield * makeFakeMuse ( ) ;
938+ const harness = yield * makeHarness ( fake ) ;
939+ const { nativeId } = yield * startConversation ( harness , fake ) ;
940+ const acknowledgement = pendingPromise < Record < string , unknown > > ( ) ;
941+ fake . queueResponse ( "approval/decide" , acknowledgement . promise ) ;
942+ const childApproval = { ...approval ( "child-run-1" ) , subagentOrigin : { subagentId : "a7" } } ;
943+ yield * fake . emit ( "approval/requested" , childApproval ) ;
944+ yield * fake . takeCall ( "approval/decide" ) ;
945+ const turnApproval = {
946+ ...approval ( nativeId ) ,
947+ approvalId : "approval-2" ,
948+ currentRequirementId : { approvalId : "approval-2" , sourceIndex : 0 } ,
949+ } ;
950+ yield * fake . emit ( "approval/requested" , turnApproval ) ;
951+ yield * harness . takeEvent (
952+ "provider_session.updated" ,
953+ ( event ) => event . providerSession . status === "waiting" ,
954+ ) ;
955+ // The shown approval settles while T3's answer to the child is still in flight.
956+ yield * fake . emit ( "approval/resolved" , turnApproval ) ;
957+ acknowledgement . resolve ( { } ) ;
958+ yield * fake . emit ( "approval/resolved" , childApproval ) ;
959+ yield * harness . takeEvent (
960+ "runtime_request.updated" ,
961+ ( event ) => event . runtimeRequest . nativeRequestRef ?. nativeId === "approval-1" ,
962+ ) ;
963+ yield * harness . takeEvent ( "provider_session.updated" ) ;
964+ const sequence = harness . allEvents . flatMap ( ( event ) =>
965+ event . type === "runtime_request.updated"
966+ ? [ `${ event . runtimeRequest . nativeRequestRef ?. nativeId } ${ event . runtimeRequest . status } ` ]
967+ : event . type === "provider_session.updated"
968+ ? [ `session ${ event . providerSession . status } ` ]
969+ : [ ] ,
970+ ) ;
971+ assert . deepStrictEqual ( sequence . slice ( sequence . indexOf ( "approval-2 pending" ) ) , [
972+ "approval-2 pending" ,
973+ "session waiting" ,
974+ "approval-2 resolved" ,
975+ "session running" ,
976+ "approval-1 resolved" ,
977+ "session running" ,
978+ ] ) ;
979+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
980+ ) ;
981+
982+ it . effect ( "shows a workflow child's approval when Muse rejects T3's automatic answer" , ( ) =>
983+ Effect . gen ( function * ( ) {
984+ const fake = yield * makeFakeMuse ( ) ;
985+ const harness = yield * makeHarness ( fake ) ;
986+ yield * startConversation ( harness , fake ) ;
987+ fake . queueResponse ( "approval/decide" , new Error ( "approval requirement changed" ) ) ;
864988 const childApproval = { ...approval ( "child-run-1" ) , subagentOrigin : { subagentId : "a7" } } ;
865989 yield * fake . emit ( "approval/requested" , childApproval ) ;
866990 const asked = yield * harness . takeEvent ( "runtime_request.updated" ) ;
867991 assert . strictEqual ( asked . runtimeRequest . status , "pending" ) ;
868- assert . isNull ( asked . runtimeRequest . providerTurnId ) ;
869- const decision = yield * fake . takeCall ( "approval/decide" ) ;
870- assert . strictEqual ( decision . params . choiceId , "once" ) ;
992+ yield * harness . takeEvent (
993+ "provider_session.updated" ,
994+ ( event ) => event . providerSession . status === "waiting" ,
995+ ) ;
996+ yield * harness . runtime . respondToRuntimeRequest ( {
997+ requestId : asked . runtimeRequest . id ,
998+ decision : "decline" ,
999+ } ) ;
1000+ assert . deepStrictEqual (
1001+ fake . calls
1002+ . filter ( ( call ) => call . method === "approval/decide" )
1003+ . map ( ( call ) => call . params . choiceId ) ,
1004+ [ "once" , "deny" ] ,
1005+ ) ;
8711006 yield * fake . emit ( "approval/resolved" , childApproval ) ;
8721007 const resolved = yield * harness . takeEvent ( "runtime_request.updated" ) ;
8731008 assert . strictEqual ( resolved . runtimeRequest . status , "resolved" ) ;
874- assert . strictEqual ( resolved . runtimeRequest . decision , "accept " ) ;
1009+ assert . strictEqual ( resolved . runtimeRequest . decision , "decline " ) ;
8751010 } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
8761011 ) ;
8771012
1013+ it . effect (
1014+ "does not show a workflow child's approval that Muse settled before T3's answer failed" ,
1015+ ( ) =>
1016+ Effect . gen ( function * ( ) {
1017+ const fake = yield * makeFakeMuse ( ) ;
1018+ const harness = yield * makeHarness ( fake ) ;
1019+ yield * startConversation ( harness , fake ) ;
1020+ const acknowledgement = pendingPromise < Record < string , unknown > > ( ) ;
1021+ fake . queueResponse ( "approval/decide" , acknowledgement . promise ) ;
1022+ const childApproval = { ...approval ( "child-run-1" ) , subagentOrigin : { subagentId : "a7" } } ;
1023+ yield * fake . emit ( "approval/requested" , childApproval ) ;
1024+ yield * fake . takeCall ( "approval/decide" ) ;
1025+ yield * fake . emit ( "approval/resolved" , childApproval ) ;
1026+ yield * harness . takeEvent (
1027+ "runtime_request.updated" ,
1028+ ( event ) => event . runtimeRequest . status === "resolved" ,
1029+ ) ;
1030+ acknowledgement . reject ( new Error ( "approval already resolved" ) ) ;
1031+ // A later child request whose answer fails is shown, after the first failure was handled.
1032+ fake . queueResponse ( "approval/decide" , new Error ( "approval requirement changed" ) ) ;
1033+ yield * fake . emit ( "approval/requested" , {
1034+ ...childApproval ,
1035+ approvalId : "approval-2" ,
1036+ currentRequirementId : { approvalId : "approval-2" , sourceIndex : 0 } ,
1037+ } ) ;
1038+ yield * harness . takeEvent (
1039+ "runtime_request.updated" ,
1040+ ( event ) => event . runtimeRequest . status === "pending" ,
1041+ ) ;
1042+ assert . deepStrictEqual (
1043+ harness . allEvents . flatMap ( ( event ) =>
1044+ event . type === "runtime_request.updated"
1045+ ? [
1046+ `${ event . runtimeRequest . nativeRequestRef ?. nativeId } ${ event . runtimeRequest . status } ` ,
1047+ ]
1048+ : [ ] ,
1049+ ) ,
1050+ [ "approval-1 resolved" , "approval-2 pending" ] ,
1051+ ) ;
1052+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
1053+ ) ;
1054+
8781055 it . effect ( "keeps a workflow child's approval pending for the user past the turn's end" , ( ) =>
8791056 Effect . gen ( function * ( ) {
8801057 const fake = yield * makeFakeMuse ( ) ;
0 commit comments