@@ -605,6 +605,108 @@ describe("CodexSessionRuntime collab integration", () => {
605605 } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
606606 ) ;
607607
608+ // it.live: the runtime talks to a real child process; under it.effect's
609+ // TestClock the internal timers freeze and the join never completes.
610+ it . live ( "Stop answers a parked app-permission approval with a withheld grant" , ( ) =>
611+ Effect . gen ( function * ( ) {
612+ // Interrupting a turn whose app-permission prompt is still parked must
613+ // settle that prompt: the handler resumes with "cancel", the peer gets
614+ // an empty grant (permission withheld), and nothing hangs until close.
615+ const script = {
616+ rootThreadId : ROOT ,
617+ holdTurnOpen : true ,
618+ notifications : [ ] ,
619+ serverRequests : [
620+ {
621+ method : "item/permissions/requestApproval" ,
622+ label : "perm-1" ,
623+ params : {
624+ cwd : "/tmp/project" ,
625+ itemId : "app_1" ,
626+ permissions : { network : { enabled : true } } ,
627+ reason : "Fetch data from api.example.com" ,
628+ startedAtMs : 1_778_000_000_000 ,
629+ threadId : "${threadId}" ,
630+ turnId : "${turnId}" ,
631+ } ,
632+ } ,
633+ ] ,
634+ } ;
635+ // @effect -diagnostics-next-line preferSchemaOverJson:off
636+ NodeFS . writeFileSync ( scriptPath , JSON . stringify ( script ) , "utf8" ) ;
637+ const responsesPath = `${ scriptPath } .approvalResponses` ;
638+ NodeFS . rmSync ( responsesPath , { force : true } ) ;
639+ yield * Effect . addFinalizer ( ( ) =>
640+ Effect . sync ( ( ) => {
641+ NodeFS . rmSync ( scriptPath , { force : true } ) ;
642+ NodeFS . rmSync ( responsesPath , { force : true } ) ;
643+ } ) ,
644+ ) ;
645+
646+ const runtime = yield * makeCodexSessionRuntime ( {
647+ threadId : ThreadId . make ( "thread-codex-permission-stop" ) ,
648+ binaryPath : peerPath ,
649+ cwd : "/tmp" ,
650+ runtimeMode : "full-access" ,
651+ environment : { ...process . env , T3_CODEX_COLLAB_SCRIPT : scriptPath } ,
652+ } ) ;
653+
654+ // One consumer for the whole stream: `events` is a plain queue stream,
655+ // so two forks would compete for events and each could starve the
656+ // other's filter. Signal the two milestones through Deferreds instead.
657+ const requestedReady = yield * Deferred . make < ProviderEvent > ( ) ;
658+ const settledReady = yield * Deferred . make < ProviderEvent > ( ) ;
659+ yield * runtime . events . pipe (
660+ Stream . runForEach ( ( event ) => {
661+ if ( event . method === "item/permissions/requestApproval" ) {
662+ return Deferred . succeed ( requestedReady , event ) ;
663+ }
664+ if ( event . method === "serverRequest/resolved" && event . requestKind === "permission" ) {
665+ return Deferred . succeed ( settledReady , event ) ;
666+ }
667+ return Effect . void ;
668+ } ) ,
669+ Effect . forkScoped ,
670+ ) ;
671+
672+ yield * runtime . start ( ) ;
673+ yield * runtime . sendTurn ( { input : "use the connected app" } ) ;
674+ const requested = yield * Deferred . await ( requestedReady ) . pipe (
675+ Effect . timeoutOption ( "15 seconds" ) ,
676+ ) ;
677+ assert . isTrue ( requested . _tag === "Some" , "permission approval request never arrived" ) ;
678+
679+ yield * runtime . interruptTurn ( ) ;
680+
681+ // The peer emits serverRequest/resolved only AFTER recording the
682+ // runtime's answer, so awaiting this receipt makes reading the sidecar
683+ // race-free. The runtime correlates that receipt back to the canonical
684+ // request (requestKind + requestId) — the same event chain the adapter
685+ // folds into approval.resolved, so the card actually closes.
686+ const settled = yield * Deferred . await ( settledReady ) . pipe ( Effect . timeoutOption ( "15 seconds" ) ) ;
687+ assert . isTrue ( settled . _tag === "Some" , "interrupt did not settle the parked approval" ) ;
688+ const settledEvent = settled . _tag === "Some" ? settled . value : undefined ;
689+ assert . isDefined ( settledEvent ) ;
690+ assert . isDefined (
691+ settledEvent ?. requestId ,
692+ "receipt must correlate back to the canonical approval request" ,
693+ ) ;
694+
695+ const recorded = NodeFS . readFileSync ( responsesPath , "utf8" )
696+ . trim ( )
697+ . split ( "\n" )
698+ . map ( ( line ) => JSON . parse ( line ) as { id : number ; label : string ; result : unknown } ) ;
699+ assert . equal ( recorded . length , 1 ) ;
700+ const answer = recorded [ 0 ] ;
701+ assert . isDefined ( answer ) ;
702+ assert . equal ( answer . label , "perm-1" ) ;
703+ // Cancelled approvals withhold the grant: an empty permission profile.
704+ assert . deepEqual ( answer . result , { permissions : { } } ) ;
705+
706+ yield * runtime . close ;
707+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
708+ ) ;
709+
608710 it . live ( "Stop targets the active turn when Codex has accepted a queued follow-up" , ( ) =>
609711 Effect . gen ( function * ( ) {
610712 const activeTurnId = "019fe3e8-f908-7f31-8d51-283f4a47897a" ;
0 commit comments