@@ -834,28 +834,205 @@ 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 (
915+ "stops waiting when a shown approval settles before a workflow child's hidden one" ,
916+ ( ) =>
917+ Effect . gen ( function * ( ) {
918+ const fake = yield * makeFakeMuse ( ) ;
919+ const harness = yield * makeHarness ( fake ) ;
920+ const { nativeId } = yield * startConversation ( harness , fake ) ;
921+ const acknowledgement = pendingPromise < Record < string , unknown > > ( ) ;
922+ fake . queueResponse ( "approval/decide" , acknowledgement . promise ) ;
923+ const childApproval = { ...approval ( "child-run-1" ) , subagentOrigin : { subagentId : "a7" } } ;
924+ yield * fake . emit ( "approval/requested" , childApproval ) ;
925+ yield * fake . takeCall ( "approval/decide" ) ;
926+ const turnApproval = {
927+ ...approval ( nativeId ) ,
928+ approvalId : "approval-2" ,
929+ currentRequirementId : { approvalId : "approval-2" , sourceIndex : 0 } ,
930+ } ;
931+ yield * fake . emit ( "approval/requested" , turnApproval ) ;
932+ yield * harness . takeEvent (
933+ "provider_session.updated" ,
934+ ( event ) => event . providerSession . status === "waiting" ,
935+ ) ;
936+ // The shown approval settles while T3's answer to the child is still in flight.
937+ yield * fake . emit ( "approval/resolved" , turnApproval ) ;
938+ acknowledgement . resolve ( { } ) ;
939+ yield * fake . emit ( "approval/resolved" , childApproval ) ;
940+ yield * harness . takeEvent (
941+ "runtime_request.updated" ,
942+ ( event ) => event . runtimeRequest . nativeRequestRef ?. nativeId === "approval-1" ,
943+ ) ;
944+ yield * harness . takeEvent ( "provider_session.updated" ) ;
945+ const sequence = harness . allEvents . flatMap ( ( event ) =>
946+ event . type === "runtime_request.updated"
947+ ? [ `${ event . runtimeRequest . nativeRequestRef ?. nativeId } ${ event . runtimeRequest . status } ` ]
948+ : event . type === "provider_session.updated"
949+ ? [ `session ${ event . providerSession . status } ` ]
950+ : [ ] ,
951+ ) ;
952+ assert . deepStrictEqual ( sequence . slice ( sequence . indexOf ( "approval-2 pending" ) ) , [
953+ "approval-2 pending" ,
954+ "session waiting" ,
955+ "approval-2 resolved" ,
956+ "session running" ,
957+ "approval-1 resolved" ,
958+ "session running" ,
959+ ] ) ;
960+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
961+ ) ;
962+
963+ it . effect ( "shows a workflow child's approval when Muse rejects T3's automatic answer" , ( ) =>
964+ Effect . gen ( function * ( ) {
965+ const fake = yield * makeFakeMuse ( ) ;
966+ const harness = yield * makeHarness ( fake ) ;
967+ yield * startConversation ( harness , fake ) ;
968+ fake . queueResponse ( "approval/decide" , new Error ( "approval requirement changed" ) ) ;
845969 const childApproval = { ...approval ( "child-run-1" ) , subagentOrigin : { subagentId : "a7" } } ;
846970 yield * fake . emit ( "approval/requested" , childApproval ) ;
847971 const asked = yield * harness . takeEvent ( "runtime_request.updated" ) ;
848972 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" ) ;
973+ yield * harness . takeEvent (
974+ "provider_session.updated" ,
975+ ( event ) => event . providerSession . status === "waiting" ,
976+ ) ;
977+ yield * harness . runtime . respondToRuntimeRequest ( {
978+ requestId : asked . runtimeRequest . id ,
979+ decision : "decline" ,
980+ } ) ;
981+ assert . deepStrictEqual (
982+ fake . calls
983+ . filter ( ( call ) => call . method === "approval/decide" )
984+ . map ( ( call ) => call . params . choiceId ) ,
985+ [ "once" , "deny" ] ,
986+ ) ;
852987 yield * fake . emit ( "approval/resolved" , childApproval ) ;
853988 const resolved = yield * harness . takeEvent ( "runtime_request.updated" ) ;
854989 assert . strictEqual ( resolved . runtimeRequest . status , "resolved" ) ;
855- assert . strictEqual ( resolved . runtimeRequest . decision , "accept " ) ;
990+ assert . strictEqual ( resolved . runtimeRequest . decision , "decline " ) ;
856991 } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
857992 ) ;
858993
994+ it . effect (
995+ "does not show a workflow child's approval that Muse settled before T3's answer failed" ,
996+ ( ) =>
997+ Effect . gen ( function * ( ) {
998+ const fake = yield * makeFakeMuse ( ) ;
999+ const harness = yield * makeHarness ( fake ) ;
1000+ yield * startConversation ( harness , fake ) ;
1001+ const acknowledgement = pendingPromise < Record < string , unknown > > ( ) ;
1002+ fake . queueResponse ( "approval/decide" , acknowledgement . promise ) ;
1003+ const childApproval = { ...approval ( "child-run-1" ) , subagentOrigin : { subagentId : "a7" } } ;
1004+ yield * fake . emit ( "approval/requested" , childApproval ) ;
1005+ yield * fake . takeCall ( "approval/decide" ) ;
1006+ yield * fake . emit ( "approval/resolved" , childApproval ) ;
1007+ yield * harness . takeEvent (
1008+ "runtime_request.updated" ,
1009+ ( event ) => event . runtimeRequest . status === "resolved" ,
1010+ ) ;
1011+ acknowledgement . reject ( new Error ( "approval already resolved" ) ) ;
1012+ // A later child request whose answer fails is shown, after the first failure was handled.
1013+ fake . queueResponse ( "approval/decide" , new Error ( "approval requirement changed" ) ) ;
1014+ yield * fake . emit ( "approval/requested" , {
1015+ ...childApproval ,
1016+ approvalId : "approval-2" ,
1017+ currentRequirementId : { approvalId : "approval-2" , sourceIndex : 0 } ,
1018+ } ) ;
1019+ yield * harness . takeEvent (
1020+ "runtime_request.updated" ,
1021+ ( event ) => event . runtimeRequest . status === "pending" ,
1022+ ) ;
1023+ assert . deepStrictEqual (
1024+ harness . allEvents . flatMap ( ( event ) =>
1025+ event . type === "runtime_request.updated"
1026+ ? [
1027+ `${ event . runtimeRequest . nativeRequestRef ?. nativeId } ${ event . runtimeRequest . status } ` ,
1028+ ]
1029+ : [ ] ,
1030+ ) ,
1031+ [ "approval-1 resolved" , "approval-2 pending" ] ,
1032+ ) ;
1033+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
1034+ ) ;
1035+
8591036 it . effect ( "keeps a workflow child's approval pending for the user past the turn's end" , ( ) =>
8601037 Effect . gen ( function * ( ) {
8611038 const fake = yield * makeFakeMuse ( ) ;
0 commit comments