@@ -457,8 +457,161 @@ describe("orchestrator replay fixtures", () => {
457457 } ) ,
458458 ) ;
459459 }
460+
461+ // Pi and OpenCode report a request answered after T3 sends its answer. The
462+ // orchestrator's record (decision, decline status) must survive that
463+ // acknowledgement.
464+ it . effect . each (
465+ (
466+ [
467+ [ "opencode" , "opencode_child_approval" , "accept" ] ,
468+ [ "opencode" , "opencode_child_approval" , "decline" ] ,
469+ [ "pi" , "simple" , "accept" ] ,
470+ [ "pi" , "simple" , "decline" ] ,
471+ ] as const
472+ ) . flatMap ( ( [ driver , fixtureName , decision ] ) => {
473+ const fixture = ORCHESTRATOR_REPLAY_FIXTURES . find (
474+ ( candidate ) => candidate . name === fixtureName ,
475+ ) ;
476+ const provider = fixture ?. providers . find ( ( candidate ) => candidate . driver === driver ) ;
477+ return fixture === undefined || provider === undefined
478+ ? [ ]
479+ : [ [ driver , decision , fixture , provider ] as const ] ;
480+ } ) ,
481+ ) (
482+ "keeps the answer through %s's acknowledgement (%s)" ,
483+ ( [ driver , decision , fixture , provider ] ) => {
484+ const prompt = fixture . buildInput ( ) . steps [ 0 ] ;
485+ return runFixtureProviderWithRegisteredHarness ( {
486+ fixtureName : fixture . name ,
487+ buildInput : ( ) => ( {
488+ steps : [
489+ ...( prompt === undefined ? [ ] : [ prompt ] ) ,
490+ { type : "approve_next_runtime_request" , decision } ,
491+ ] ,
492+ } ) ,
493+ driver : {
494+ ...provider ,
495+ assertOutput : ( result ) => {
496+ const status = decision === "accept" ? "completed" : "cancelled" ;
497+ const projection = [ ...result . projections . values ( ) ] . find (
498+ ( candidate ) => candidate . thread . lineage . parentThreadId === null ,
499+ ) ;
500+ const item = projection ?. turnItems . find (
501+ ( candidate ) => candidate . type === "approval_request" ,
502+ ) ;
503+ assert . deepInclude ( item , { status } ) ;
504+ assert . deepInclude ( projection ?. runtimeRequests [ 0 ] , { status : "resolved" , decision } ) ;
505+ // The last word on the card and the request, as every client and a replay read them.
506+ const card = result . domainEvents . findLast (
507+ ( event ) => event . type === "turn-item.updated" && event . payload . id === item ?. id ,
508+ ) ;
509+ assert . deepInclude ( card ?. payload , { status } ) ;
510+ const request = result . domainEvents . findLast (
511+ ( event ) => event . type === "runtime-request.updated" ,
512+ ) ;
513+ assert . deepInclude ( request ?. payload , { status : "resolved" , decision } ) ;
514+ } ,
515+ } ,
516+ transformTranscript : ( transcript ) =>
517+ driver === "pi"
518+ ? withPiConfirm ( transcript , decision )
519+ : withOpenCodeReply ( transcript , decision ) ,
520+ } ) ;
521+ } ,
522+ ) ;
523+
524+ it . effect ( "keeps a decline through muse's acknowledgement" , ( ) => {
525+ const fixture = ORCHESTRATOR_REPLAY_FIXTURES . find (
526+ ( candidate ) => candidate . name === "muse_permission" ,
527+ ) ;
528+ const provider = fixture ?. providers . find ( ( candidate ) => candidate . driver === "muse" ) ;
529+ assert . isDefined ( fixture ) ;
530+ assert . isDefined ( provider ) ;
531+ return runFixtureProviderWithRegisteredHarness ( {
532+ fixtureName : fixture . name ,
533+ buildInput : fixture . buildInput ,
534+ driver : {
535+ ...provider ,
536+ assertOutput : ( result ) => {
537+ const projection = [ ...result . projections . values ( ) ] . find (
538+ ( candidate ) => candidate . thread . lineage . parentThreadId === null ,
539+ ) ;
540+ // Each request's card ends as the orchestrator recorded its answer.
541+ assert . deepEqual (
542+ ( projection ?. runtimeRequests ?? [ ] )
543+ . toSorted ( ( left , right ) => ( left . createdAt < right . createdAt ? - 1 : 1 ) )
544+ . map ( ( request ) => [
545+ request . decision ,
546+ projection ?. turnItems . find (
547+ ( item ) => item . type === "approval_request" && item . nodeId === request . nodeId ,
548+ ) ?. status ,
549+ ] ) ,
550+ [
551+ [ "accept" , "completed" ] ,
552+ [ "decline" , "cancelled" ] ,
553+ ] ,
554+ ) ;
555+ } ,
556+ } ,
557+ } ) ;
558+ } ) ;
460559} ) ;
461560
561+ /** Adds an extension confirm dialog to the turn and the answer T3 sends for it. */
562+ function withPiConfirm (
563+ transcript : ProviderReplayTranscript ,
564+ decision : "accept" | "decline" ,
565+ ) : ProviderReplayTranscript {
566+ const at = transcript . entries . findIndex (
567+ ( entry ) => entry . type === "emit_inbound" && entry . label === "turn_start" ,
568+ ) ;
569+ const id = "ui-approval" ;
570+ return {
571+ ...transcript ,
572+ entries : [
573+ ...transcript . entries . slice ( 0 , at + 1 ) ,
574+ {
575+ type : "emit_inbound" ,
576+ label : "extension_ui_request.confirm" ,
577+ frame : { type : "extension_ui_request" , id, method : "confirm" , title : "Allow bash?" } ,
578+ } ,
579+ {
580+ type : "expect_outbound" ,
581+ label : "extension_ui_response" ,
582+ frame : { type : "extension_ui_response" , id, confirmed : decision === "accept" } ,
583+ } ,
584+ ...transcript . entries . slice ( at + 1 ) ,
585+ ] ,
586+ } ;
587+ }
588+
589+ /** The recorded permission reply and its echo, rejected instead when declining. */
590+ function withOpenCodeReply (
591+ transcript : ProviderReplayTranscript ,
592+ decision : "accept" | "decline" ,
593+ ) : ProviderReplayTranscript {
594+ if ( decision === "accept" ) return transcript ;
595+ return {
596+ ...transcript ,
597+ entries : transcript . entries . map ( ( entry ) => {
598+ if ( entry . type === "expect_outbound" && entry . label === "permission.reply" ) {
599+ const frame = entry . frame as { readonly input : object } ;
600+ return { ...entry , frame : { ...frame , input : { ...frame . input , reply : "reject" } } } ;
601+ }
602+ if ( entry . type === "emit_inbound" && entry . label === "child.permission.replied" ) {
603+ const frame = entry . frame as { readonly event : { readonly properties : object } } ;
604+ const event = {
605+ ...frame . event ,
606+ properties : { ...frame . event . properties , reply : "reject" } ,
607+ } ;
608+ return { ...entry , frame : { ...frame , event } } ;
609+ }
610+ return entry ;
611+ } ) ,
612+ } ;
613+ }
614+
462615/** The same event with an envelope this build cannot decode, as a newer OpenCode may send. */
463616function undecodableEvent ( entry : ProviderReplayEntry ) : ProviderReplayEntry {
464617 if ( entry . type !== "emit_inbound" ) return entry ;
0 commit comments