@@ -3660,89 +3660,205 @@ it.effect(
36603660 } ) ,
36613661) ;
36623662
3663- it . effect ( "ProviderSessionManagerV2 persists session-scoped runtime requests without a run" , ( ) =>
3664- Effect . gen ( function * ( ) {
3665- const state = yield * Ref . make ( emptyState ) ;
3666- const effect = Effect . gen ( function * ( ) {
3667- const eventSink = yield * EventSink . EventSinkV2 ;
3668- const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
3669- const manager = yield * ProviderSessionManager . ProviderSessionManagerV2 ;
3670- const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
3671- const now = yield * DateTime . now ;
3672- const projectId = yield * idAllocator . allocate . project ( {
3673- fixtureName : "provider-session-manager-session-request" ,
3674- } ) ;
3675- const threadId = yield * idAllocator . allocate . thread ( {
3676- fixtureName : "provider-session-manager-session-request" ,
3677- projectId,
3678- } ) ;
3679- const providerSessionId = yield * idAllocator . allocate . providerSession ( {
3680- providerInstanceId : modelSelection . instanceId ,
3681- threadId,
3682- } ) ;
3683- const providerThread = makeProviderThread ( {
3684- idAllocator,
3685- threadId,
3686- providerSessionId,
3687- now,
3688- } ) ;
3663+ it . effect (
3664+ "ProviderSessionManagerV2 persists session-scoped requests and recovered commands without a run" ,
3665+ ( ) =>
3666+ Effect . gen ( function * ( ) {
3667+ const state = yield * Ref . make ( emptyState ) ;
3668+ const effect = Effect . gen ( function * ( ) {
3669+ const eventSink = yield * EventSink . EventSinkV2 ;
3670+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
3671+ const manager = yield * ProviderSessionManager . ProviderSessionManagerV2 ;
3672+ const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
3673+ const now = yield * DateTime . now ;
3674+ const projectId = yield * idAllocator . allocate . project ( {
3675+ fixtureName : "provider-session-manager-session-request" ,
3676+ } ) ;
3677+ const threadId = yield * idAllocator . allocate . thread ( {
3678+ fixtureName : "provider-session-manager-session-request" ,
3679+ projectId,
3680+ } ) ;
3681+ const providerSessionId = yield * idAllocator . allocate . providerSession ( {
3682+ providerInstanceId : modelSelection . instanceId ,
3683+ threadId,
3684+ } ) ;
3685+ const providerThread = makeProviderThread ( {
3686+ idAllocator,
3687+ threadId,
3688+ providerSessionId,
3689+ now,
3690+ } ) ;
36893691
3690- yield * eventSink . write ( {
3691- events : [ yield * makeThreadCreatedEvent ( { idAllocator, threadId, now } ) ] ,
3692- } ) ;
3693- yield * manager . open ( {
3694- threadId,
3695- providerSessionId,
3696- modelSelection,
3697- runtimePolicy,
3698- } ) ;
3699- const pendingRequest = yield * makePendingRuntimeRequestEvents ( {
3700- idAllocator,
3701- threadId,
3702- providerSessionId,
3703- providerThread,
3704- now,
3705- } ) ;
3706- const afterSequence = yield * eventSink . latestSequence ( { threadId } ) ;
3707- const persistedFiber = yield * eventSink . stream ( { threadId, afterSequence } ) . pipe (
3708- Stream . filter (
3709- ( stored ) =>
3710- stored . event . type === "runtime-request.updated" ||
3711- stored . event . type === "node.updated" ||
3712- stored . event . type === "turn-item.updated" ,
3713- ) ,
3714- Stream . take ( 3 ) ,
3715- Stream . runCollect ,
3716- Effect . forkScoped ,
3717- ) ;
3718- const adapterEvents = ( yield * Ref . get ( state ) ) . eventQueues . get ( String ( providerSessionId ) ) ;
3719- assert . isDefined ( adapterEvents ) ;
3720- yield * Queue . offerAll ( adapterEvents ! , pendingRequest . providerEvents ) ;
3721- const persisted = Array . from ( yield * Fiber . join ( persistedFiber ) ) ;
3692+ const otherThreadId = yield * idAllocator . allocate . thread ( {
3693+ fixtureName : "unattached-shell-owner" ,
3694+ projectId,
3695+ } ) ;
3696+ yield * eventSink . write ( {
3697+ events : [
3698+ yield * makeThreadCreatedEvent ( { idAllocator, threadId, now } ) ,
3699+ yield * makeThreadCreatedEvent ( { idAllocator, threadId : otherThreadId , now } ) ,
3700+ ] ,
3701+ } ) ;
3702+ yield * manager . open ( {
3703+ threadId,
3704+ providerSessionId,
3705+ modelSelection,
3706+ runtimePolicy,
3707+ } ) ;
3708+ const pendingRequest = yield * makePendingRuntimeRequestEvents ( {
3709+ idAllocator,
3710+ threadId,
3711+ providerSessionId,
3712+ providerThread,
3713+ now,
3714+ } ) ;
3715+ const shellId = "sh_recovered" ;
3716+ const command = {
3717+ id : idAllocator . derive . turnItemFromProviderItem ( {
3718+ driver : CODEX_DRIVER ,
3719+ nativeItemId : shellId ,
3720+ } ) ,
3721+ threadId,
3722+ runId : null ,
3723+ nodeId : null ,
3724+ providerThreadId : providerThread . id ,
3725+ providerTurnId : null ,
3726+ nativeItemRef : { driver : CODEX_DRIVER , nativeId : shellId , strength : "strong" as const } ,
3727+ parentItemId : null ,
3728+ ordinal : 0 ,
3729+ status : "running" as const ,
3730+ title : null ,
3731+ startedAt : now ,
3732+ completedAt : null ,
3733+ updatedAt : now ,
3734+ type : "command_execution" as const ,
3735+ input : "echo retained" ,
3736+ output : "retained" ,
3737+ } ;
3738+ const anchored = {
3739+ ...command ,
3740+ id : idAllocator . derive . turnItemFromProviderItem ( {
3741+ driver : CODEX_DRIVER ,
3742+ nativeItemId : "sh_anchored" ,
3743+ } ) ,
3744+ runId : idAllocator . derive . run ( { threadId, ordinal : 1 } ) ,
3745+ } ;
3746+ const commandEvents = [
3747+ {
3748+ type : "turn_item.updated" as const ,
3749+ driver : CODEX_DRIVER ,
3750+ turnItem : anchored ,
3751+ sessionScoped : true ,
3752+ } ,
3753+ {
3754+ type : "turn_item.updated" as const ,
3755+ driver : CODEX_DRIVER ,
3756+ turnItem : command ,
3757+ sessionScoped : true ,
3758+ } ,
3759+ {
3760+ type : "provider_thread.updated" as const ,
3761+ driver : CODEX_DRIVER ,
3762+ providerThread : {
3763+ ...providerThread ,
3764+ pendingBackgroundTasks : [ { taskId : shellId , kind : "command" as const } ] ,
3765+ } ,
3766+ sessionScoped : true ,
3767+ } ,
3768+ {
3769+ type : "turn_item.updated" as const ,
3770+ driver : CODEX_DRIVER ,
3771+ sessionScoped : true ,
3772+ turnItem : {
3773+ ...command ,
3774+ status : "completed" as const ,
3775+ output : "retained\nfinished" ,
3776+ completedAt : now ,
3777+ } ,
3778+ } ,
3779+ {
3780+ type : "provider_thread.updated" as const ,
3781+ driver : CODEX_DRIVER ,
3782+ providerThread : { ...providerThread , pendingBackgroundTasks : [ ] } ,
3783+ sessionScoped : true ,
3784+ } ,
3785+ ] ;
3786+ const afterSequence = yield * eventSink . latestSequence ( { threadId } ) ;
3787+ const persistedFiber = yield * eventSink . stream ( { threadId, afterSequence } ) . pipe (
3788+ Stream . filter (
3789+ ( stored ) =>
3790+ stored . event . type === "runtime-request.updated" ||
3791+ stored . event . type === "node.updated" ||
3792+ ( stored . event . type === "turn-item.updated" &&
3793+ stored . event . payload . type === "approval_request" ) ,
3794+ ) ,
3795+ Stream . take ( 3 ) ,
3796+ Stream . runCollect ,
3797+ Effect . forkScoped ,
3798+ ) ;
3799+ const adapterEvents = ( yield * Ref . get ( state ) ) . eventQueues . get ( String ( providerSessionId ) ) ;
3800+ assert . isDefined ( adapterEvents ) ;
3801+ yield * Queue . offerAll ( adapterEvents ! , [
3802+ ...commandEvents ,
3803+ {
3804+ type : "turn_item.updated" ,
3805+ driver : CODEX_DRIVER ,
3806+ sessionScoped : true ,
3807+ turnItem : { ...command , threadId : otherThreadId } ,
3808+ } ,
3809+ {
3810+ type : "provider_thread.updated" ,
3811+ driver : CODEX_DRIVER ,
3812+ providerThread : { ...providerThread , appThreadId : otherThreadId } ,
3813+ sessionScoped : true ,
3814+ } ,
3815+ ...pendingRequest . providerEvents ,
3816+ ] ) ;
3817+ const persisted = Array . from ( yield * Fiber . join ( persistedFiber ) ) ;
37223818
3723- assert . sameMembers (
3724- persisted . map ( ( stored ) => stored . event . type ) ,
3725- [ "runtime-request.updated" , "node.updated" , "turn-item.updated" ] ,
3726- ) ;
3727- const projection = yield * projectionStore . getThreadProjection ( threadId ) ;
3728- const request = projection . runtimeRequests . find (
3729- ( candidate ) => candidate . id === pendingRequest . requestId ,
3730- ) ;
3731- const node = projection . nodes . find ( ( candidate ) => candidate . id === pendingRequest . nodeId ) ;
3732- const turnItem = projection . turnItems . find (
3733- ( candidate ) =>
3734- candidate . type === "approval_request" && candidate . requestId === pendingRequest . requestId ,
3735- ) ;
3736- assert . equal ( request ?. status , "pending" ) ;
3737- assert . equal ( request ?. providerTurnId , null ) ;
3738- assert . equal ( node ?. runId , null ) ;
3739- assert . equal ( node ?. status , "waiting" ) ;
3740- assert . equal ( turnItem ?. runId , null ) ;
3741- assert . equal ( turnItem ?. status , "waiting" ) ;
3742- } ) ;
3819+ assert . sameMembers (
3820+ persisted . map ( ( stored ) => stored . event . type ) ,
3821+ [ "runtime-request.updated" , "node.updated" , "turn-item.updated" ] ,
3822+ ) ;
3823+ const projection = yield * projectionStore . getThreadProjection ( threadId ) ;
3824+ const request = projection . runtimeRequests . find (
3825+ ( candidate ) => candidate . id === pendingRequest . requestId ,
3826+ ) ;
3827+ const node = projection . nodes . find ( ( candidate ) => candidate . id === pendingRequest . nodeId ) ;
3828+ const turnItem = projection . turnItems . find (
3829+ ( candidate ) =>
3830+ candidate . type === "approval_request" &&
3831+ candidate . requestId === pendingRequest . requestId ,
3832+ ) ;
3833+ const recovered = projection . turnItems . find ( ( item ) => item . id === command . id ) ;
3834+ assert . deepInclude ( recovered , {
3835+ runId : null ,
3836+ status : "completed" ,
3837+ output : "retained\nfinished" ,
3838+ } ) ;
3839+ assert . deepEqual (
3840+ projection . providerThreads . find ( ( item ) => item . id === providerThread . id )
3841+ ?. pendingBackgroundTasks ,
3842+ [ ] ,
3843+ ) ;
3844+ assert . deepInclude (
3845+ projection . turnItems . find ( ( item ) => item . id === anchored . id ) ,
3846+ { runId : anchored . runId , output : "retained" } ,
3847+ ) ;
3848+ assert . isEmpty ( projection . runs ) ;
3849+ const unrelated = yield * projectionStore . getThreadProjection ( otherThreadId ) ;
3850+ assert . isEmpty ( unrelated . turnItems ) ;
3851+ assert . isEmpty ( unrelated . providerThreads ) ;
3852+ assert . equal ( request ?. status , "pending" ) ;
3853+ assert . equal ( request ?. providerTurnId , null ) ;
3854+ assert . equal ( node ?. runId , null ) ;
3855+ assert . equal ( node ?. status , "waiting" ) ;
3856+ assert . equal ( turnItem ?. runId , null ) ;
3857+ assert . equal ( turnItem ?. status , "waiting" ) ;
3858+ } ) ;
37433859
3744- yield * effect . pipe ( Effect . provide ( layerTest ( { state, idleTimeoutMs : 1000 } ) ) ) ;
3745- } ) ,
3860+ yield * effect . pipe ( Effect . provide ( layerTest ( { state, idleTimeoutMs : 1000 } ) ) ) ;
3861+ } ) ,
37463862) ;
37473863
37483864it . effect (
0 commit comments