@@ -30,6 +30,7 @@ import * as Layer from "effect/Layer";
3030import * as Logger from "effect/Logger" ;
3131import * as Queue from "effect/Queue" ;
3232import * as Schema from "effect/Schema" ;
33+ import * as Scope from "effect/Scope" ;
3334import * as Sink from "effect/Sink" ;
3435import * as Stream from "effect/Stream" ;
3536import * as ChildProcess from "effect/process/ChildProcess" ;
@@ -3683,8 +3684,8 @@ describe("PiAdapterV2", () => {
36833684 } ) . pipe ( Effect . scoped , Effect . provide ( layerTest ) ) ,
36843685 ) ;
36853686
3686- it . effect . each ( [ "settled" , "interrupted -id" , "interrupted -idless" ] ) (
3687- "keeps a settled turn's late prompt rejection off the next turn: %s" ,
3687+ it . effect . each ( [ "settled" , "outer -id" , "outer-idless" , "install-id" , "install -idless"] ) (
3688+ "waits for a settled turn's late prompt ACK before accepting the next turn: %s" ,
36883689 ( boundary ) =>
36893690 Effect . gen ( function * ( ) {
36903691 const fake = yield * makeFakePi ;
@@ -3695,27 +3696,59 @@ describe("PiAdapterV2", () => {
36953696 runtimePolicy,
36963697 } ) ;
36973698 const interrupted = boundary !== "settled" ;
3698- // Hold a competing start past the outer guard, before the first turn owns the session.
3699- if ( interrupted ) fake . deferNextModelSelection ( ) ;
3700- const competing = interrupted
3699+ const atInstall = boundary . startsWith ( "install-" ) ;
3700+ // Suspend the next start before another turn acquires and releases ownership.
3701+ if ( atInstall ) fake . deferNextModelSelection ( ) ;
3702+ let next = atInstall
37013703 ? yield * startTurn (
37023704 runtime ,
37033705 providerThread ,
37043706 "default" ,
37053707 [ ] ,
3706- "Competing turn" ,
3708+ "Second turn" ,
37073709 modelSelection ( "xai/grok-4.6" ) ,
37083710 2 ,
37093711 ) . pipe ( Effect . result , Effect . forkScoped )
37103712 : null ;
3711- const modelRequest = interrupted ? yield * fake . takeRequest ( "set_model" ) : null ;
3713+ const modelRequest = atInstall ? yield * fake . takeRequest ( "set_model" ) : null ;
37123714 if ( interrupted ) fake . deferNextLifecycle ( "prompt" ) ;
37133715 yield * startTurn ( runtime , providerThread , "default" , [ ] , "/my-command" ) ;
37143716 const firstPrompt = yield * fake . takeRequest ( "prompt" ) ;
37153717 const running = yield * takeEvent ( ( event ) => event . type === "provider_turn.updated" ) ;
37163718 assert . isTrue ( running . type === "provider_turn.updated" ) ;
37173719 if ( running . type !== "provider_turn.updated" ) return ;
37183720 if ( interrupted ) {
3721+ const active = yield * startTurn ( runtime , providerThread ) . pipe ( Effect . result ) ;
3722+ assert . isTrue (
3723+ active . _tag === "Failure" &&
3724+ isProviderProtocolError ( active . failure . cause ) &&
3725+ active . failure . cause . reason === "previous_turn_active" ,
3726+ ) ;
3727+ fake . deferNextLifecycle ( "prompt" ) ;
3728+ yield * runtime . steerTurn ( {
3729+ providerThread,
3730+ providerTurnId : running . providerTurn . id ,
3731+ threadId : THREAD_ID ,
3732+ runId : RunId . make ( "run:thread-pi-test:1" ) ,
3733+ message : {
3734+ messageId : "message:old-steer" as never ,
3735+ text : "Old steer" ,
3736+ attachments : [ ] ,
3737+ createdBy : "user" ,
3738+ creationSource : "web" ,
3739+ } ,
3740+ } ) ;
3741+ yield * fake . takeRequest ( "prompt" ) ;
3742+ yield * fake . emit ( {
3743+ type : "extension_ui_request" ,
3744+ id : "old-dialog" ,
3745+ method : "confirm" ,
3746+ title : "Old dialog" ,
3747+ } ) ;
3748+ yield * takeEvent (
3749+ ( event ) =>
3750+ event . type === "runtime_request.updated" && event . runtimeRequest . status === "pending" ,
3751+ ) ;
37193752 yield * runtime . interruptTurn ( { providerThread, providerTurnId : running . providerTurn . id } ) ;
37203753 } else {
37213754 yield * fake . emit ( { type : "agent_start" } ) ;
@@ -3727,41 +3760,28 @@ describe("PiAdapterV2", () => {
37273760 firstTerminal . status === ( interrupted ? "interrupted" : "completed" ) ,
37283761 ) ;
37293762
3730- if ( competing !== null && modelRequest !== null ) {
3731- const blocked = yield * startTurn (
3763+ fake . deferNextLifecycle ( "prompt" ) ;
3764+ if ( next === null ) {
3765+ next = yield * startTurn (
37323766 runtime ,
37333767 providerThread ,
37343768 "default" ,
37353769 [ ] ,
3736- "Blocked turn" ,
3770+ "Second turn" ,
37373771 undefined ,
37383772 2 ,
3739- ) . pipe ( Effect . result ) ;
3740- assert . equal ( blocked . _tag , "Failure" ) ;
3741- assert . isTrue (
3742- blocked . _tag === "Failure" &&
3743- isProviderProtocolError ( blocked . failure . cause ) &&
3744- blocked . failure . cause . reason === "previous_turn_active" ,
3745- ) ;
3773+ ) . pipe ( Effect . result , Effect . forkScoped ( { startImmediately : true } ) ) ;
3774+ }
3775+ if ( modelRequest !== null ) {
37463776 yield * fake . emit ( {
37473777 type : "response" ,
37483778 id : modelRequest [ "id" ] ,
37493779 command : "set_model" ,
37503780 success : true ,
37513781 data : { provider : "xai" , id : "grok-4.6" } ,
37523782 } ) ;
3753- assert . equal ( ( yield * Fiber . join ( competing ) ) . _tag , "Failure" ) ;
3754- assert . lengthOf (
3755- fake . allRequests ( ) . filter ( ( request ) => request [ "type" ] === "prompt" ) ,
3756- 1 ,
3757- ) ;
3758- yield * fake . emit ( {
3759- type : "response" ,
3760- command : "prompt" ,
3761- success : false ,
3762- ...( boundary === "interrupted-idless" ? { } : { id : firstPrompt [ "id" ] } ) ,
3763- error : "late command rejection" ,
3764- } ) ;
3783+ }
3784+ if ( interrupted ) {
37653785 yield * fake . emit ( {
37663786 type : "extension_ui_request" ,
37673787 id : "old-ack-barrier" ,
@@ -3776,10 +3796,41 @@ describe("PiAdapterV2", () => {
37763796 decision : "decline" ,
37773797 } ) ;
37783798 yield * fake . takeRequest ( "extension_ui_response" ) ;
3799+ assert . isUndefined ( next . pollUnsafe ( ) ) ;
3800+ assert . lengthOf (
3801+ fake . allRequests ( ) . filter ( ( request ) => request [ "type" ] === "prompt" ) ,
3802+ 2 ,
3803+ ) ;
3804+ yield * fake . emit ( {
3805+ type : "response" ,
3806+ command : "prompt" ,
3807+ success : false ,
3808+ ...( boundary . endsWith ( "idless" ) ? { } : { id : firstPrompt [ "id" ] } ) ,
3809+ error : "late command rejection" ,
3810+ } ) ;
3811+ // Duplicate and unknown IDs cannot consume the remaining old steer.
3812+ for ( const id of [ firstPrompt [ "id" ] , "unknown-prompt" ] ) {
3813+ yield * fake . emit ( { type : "response" , id, command : "prompt" , success : true } ) ;
3814+ }
3815+ yield * fake . emit ( {
3816+ type : "extension_ui_request" ,
3817+ id : "partial-ack-barrier" ,
3818+ method : "confirm" ,
3819+ title : "Partial ACK" ,
3820+ } ) ;
3821+ const partial = yield * takeEvent (
3822+ ( event ) =>
3823+ event . type === "runtime_request.updated" && event . runtimeRequest . status === "pending" ,
3824+ ) ;
3825+ assert . isTrue ( partial . type === "runtime_request.updated" ) ;
3826+ assert . isUndefined ( next . pollUnsafe ( ) ) ;
3827+ assert . lengthOf (
3828+ fake . allRequests ( ) . filter ( ( request ) => request [ "type" ] === "prompt" ) ,
3829+ 2 ,
3830+ ) ;
3831+ yield * fake . emit ( { type : "response" , command : "prompt" , success : true } ) ;
37793832 }
3780-
3781- fake . deferNextLifecycle ( "prompt" ) ;
3782- yield * startTurn ( runtime , providerThread , "default" , [ ] , "Second turn" , undefined , 2 ) ;
3833+ assert . equal ( ( yield * Fiber . join ( next ) ) . _tag , "Success" ) ;
37833834 yield * fake . takeRequest ( "prompt" ) ;
37843835 yield * fake . emit ( {
37853836 type : "response" ,
@@ -3798,6 +3849,137 @@ describe("PiAdapterV2", () => {
37983849 } ) . pipe ( Effect . scoped , Effect . provide ( layerTest ) ) ,
37993850 ) ;
38003851
3852+ it . effect . each ( [
3853+ { boundary : "outer" , end : "session" } ,
3854+ { boundary : "outer" , end : "transport" } ,
3855+ { boundary : "outer" , end : "caller" } ,
3856+ { boundary : "install" , end : "session" } ,
3857+ { boundary : "install" , end : "transport" } ,
3858+ { boundary : "install" , end : "caller" } ,
3859+ ] ) (
3860+ "settles an ACK-blocked start on $end cancellation at $boundary admission" ,
3861+ ( { boundary, end } ) =>
3862+ Effect . gen ( function * ( ) {
3863+ const fake = yield * makeFakePi ;
3864+ const runtimeScope = yield * Scope . make ( ) ;
3865+ yield * Effect . addFinalizer ( ( ) => Scope . close ( runtimeScope , Exit . void ) ) ;
3866+ const { runtime, takeEvent } = yield * openRuntime ( fake ) . pipe ( Scope . provide ( runtimeScope ) ) ;
3867+ const providerThread = yield * runtime . ensureThread ( {
3868+ threadId : THREAD_ID ,
3869+ modelSelection : modelSelection ( "default" ) ,
3870+ runtimePolicy,
3871+ } ) ;
3872+ if ( boundary === "install" ) fake . deferNextModelSelection ( ) ;
3873+ let next =
3874+ boundary === "install"
3875+ ? yield * startTurn (
3876+ runtime ,
3877+ providerThread ,
3878+ "default" ,
3879+ [ ] ,
3880+ "Second turn" ,
3881+ modelSelection ( "xai/grok-4.6" ) ,
3882+ 2 ,
3883+ ) . pipe ( Effect . result , Effect . forkScoped ( { startImmediately : true } ) )
3884+ : null ;
3885+ const modelRequest = next === null ? null : yield * fake . takeRequest ( "set_model" ) ;
3886+ fake . deferNextLifecycle ( "prompt" ) ;
3887+ yield * startTurn ( runtime , providerThread ) ;
3888+ const oldPrompt = yield * fake . takeRequest ( "prompt" ) ;
3889+ const running = yield * takeEvent ( ( event ) => event . type === "provider_turn.updated" ) ;
3890+ assert . isTrue ( running . type === "provider_turn.updated" ) ;
3891+ if ( running . type !== "provider_turn.updated" ) return ;
3892+ yield * runtime . interruptTurn ( { providerThread, providerTurnId : running . providerTurn . id } ) ;
3893+ yield * fake . emit ( { type : "agent_settled" } ) ;
3894+ yield * takeEvent ( ( event ) => event . type === "turn.terminal" ) ;
3895+ if ( next === null ) {
3896+ next = yield * startTurn (
3897+ runtime ,
3898+ providerThread ,
3899+ "default" ,
3900+ [ ] ,
3901+ "Second turn" ,
3902+ undefined ,
3903+ 2 ,
3904+ ) . pipe ( Effect . result , Effect . forkScoped ( { startImmediately : true } ) ) ;
3905+ }
3906+ if ( modelRequest !== null ) {
3907+ yield * fake . emit ( {
3908+ type : "response" ,
3909+ id : modelRequest [ "id" ] ,
3910+ command : "set_model" ,
3911+ success : true ,
3912+ data : { provider : "xai" , id : "grok-4.6" } ,
3913+ } ) ;
3914+ }
3915+ yield * fake . emit ( {
3916+ type : "extension_ui_request" ,
3917+ id : "lifecycle-barrier" ,
3918+ method : "confirm" ,
3919+ title : "Pending ACK" ,
3920+ } ) ;
3921+ const dialog = yield * takeEvent (
3922+ ( event ) =>
3923+ event . type === "runtime_request.updated" && event . runtimeRequest . status === "pending" ,
3924+ ) ;
3925+ assert . isTrue ( dialog . type === "runtime_request.updated" ) ;
3926+ assert . isUndefined ( next . pollUnsafe ( ) ) ;
3927+ assert . lengthOf (
3928+ fake . allRequests ( ) . filter ( ( request ) => request [ "type" ] === "prompt" ) ,
3929+ 1 ,
3930+ ) ;
3931+ if ( end === "caller" ) {
3932+ yield * Fiber . interrupt ( next ) ;
3933+ assert . isTrue ( Exit . hasInterrupts ( yield * Fiber . await ( next ) ) ) ;
3934+ const replacement = yield * startTurn (
3935+ runtime ,
3936+ providerThread ,
3937+ "default" ,
3938+ [ ] ,
3939+ "Replacement" ,
3940+ undefined ,
3941+ 3 ,
3942+ ) . pipe ( Effect . result , Effect . forkScoped ( { startImmediately : true } ) ) ;
3943+ assert . isUndefined ( replacement . pollUnsafe ( ) ) ;
3944+ assert . lengthOf (
3945+ fake . allRequests ( ) . filter ( ( request ) => request [ "type" ] === "prompt" ) ,
3946+ 1 ,
3947+ ) ;
3948+ yield * fake . emit ( {
3949+ type : "response" ,
3950+ id : oldPrompt [ "id" ] ,
3951+ command : "prompt" ,
3952+ success : true ,
3953+ } ) ;
3954+ assert . equal ( ( yield * Fiber . join ( replacement ) ) . _tag , "Success" ) ;
3955+ yield * fake . takeRequest ( "prompt" ) ;
3956+ yield * fake . emit ( { type : "agent_start" } ) ;
3957+ yield * fake . emit ( { type : "agent_settled" } ) ;
3958+ const terminal = yield * takeEvent ( ( event ) => event . type === "turn.terminal" ) ;
3959+ assert . isTrue ( terminal . type === "turn.terminal" && terminal . status === "completed" ) ;
3960+ } else {
3961+ if ( end === "session" ) yield * Scope . close ( runtimeScope , Exit . void ) ;
3962+ else yield * fake . closeStdout ;
3963+ const failed = yield * Fiber . join ( next ) ;
3964+ assert . isTrue ( failed . _tag === "Failure" && isProviderProtocolError ( failed . failure . cause ) ) ;
3965+ yield * fake . emit ( {
3966+ type : "response" ,
3967+ id : oldPrompt [ "id" ] ,
3968+ command : "prompt" ,
3969+ success : true ,
3970+ } ) ;
3971+ assert . equal (
3972+ ( yield * startTurn ( runtime , providerThread ) . pipe ( Effect . result ) ) . _tag ,
3973+ "Failure" ,
3974+ ) ;
3975+ assert . lengthOf (
3976+ fake . allRequests ( ) . filter ( ( request ) => request [ "type" ] === "prompt" ) ,
3977+ 1 ,
3978+ ) ;
3979+ }
3980+ } ) . pipe ( Effect . scoped , Effect . provide ( layerTest ) ) ,
3981+ ) ;
3982+
38013983 it . effect ( "shows compaction progress and completes the same activity row" , ( ) =>
38023984 Effect . gen ( function * ( ) {
38033985 const fake = yield * makeFakePi ;
0 commit comments