@@ -763,7 +763,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
763763 } ) ,
764764 ) ;
765765
766- it . effect ( "replays shared provider-session payloads across every bound thread" , ( ) =>
766+ it . effect ( "replays provider-session payloads independently for each bound thread" , ( ) =>
767767 Effect . gen ( function * ( ) {
768768 const eventSink = yield * EventSink . EventSinkV2 ;
769769 const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
@@ -819,12 +819,20 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
819819 ] ,
820820 } ) ;
821821
822- assert . equal (
823- ( yield * projectionStore . getThreadProjection ( firstThreadId ) ) . providerSessions [ 0 ] ?. cwd ,
824- secondSession . cwd ,
825- ) ;
822+ const assertBindings = Effect . gen ( function * ( ) {
823+ assert . deepEqual (
824+ ( yield * projectionStore . getThreadProjection ( firstThreadId ) ) . providerSessions ,
825+ [ firstSession ] ,
826+ ) ;
827+ assert . deepEqual (
828+ ( yield * projectionStore . getThreadProjection ( secondThreadId ) ) . providerSessions ,
829+ [ secondSession ] ,
830+ ) ;
831+ } ) ;
832+ yield * assertBindings ;
826833 assert . isTrue ( ( yield * maintenance . verify ) . valid ) ;
827834 assert . isTrue ( ( yield * maintenance . rebuild ) . valid ) ;
835+ yield * assertBindings ;
828836 } ) ,
829837 ) ;
830838
@@ -3666,3 +3674,88 @@ it.effect.each(["cancel transaction", "fail transaction", "cancel publication"]
36663674 ) ;
36673675 } ) . pipe ( Effect . provide ( layerDatabase ) ) ,
36683676) ;
3677+
3678+ it . effect . each ( [ "cancel transaction" , "fail transaction" , "cancel publication" ] as const ) (
3679+ "hands off committed ownership only after a successful transaction during %s" ,
3680+ ( scenario ) =>
3681+ Effect . gen ( function * ( ) {
3682+ const paused = yield * Deferred . make < void > ( ) ;
3683+ const resume = yield * Deferred . make < void > ( ) ;
3684+ const pause = Deferred . succeed ( paused , undefined ) . pipe (
3685+ Effect . andThen ( Deferred . await ( resume ) ) ,
3686+ ) ;
3687+ const committed = scenario === "cancel publication" ;
3688+ const stores = Layer . mergeAll (
3689+ layerStoresProvided ,
3690+ Layer . effect (
3691+ EventStore . EventStoreV2 ,
3692+ Effect . gen ( function * ( ) {
3693+ const delegate = yield * EventStore . EventStoreV2 ;
3694+ return EventStore . EventStoreV2 . of ( {
3695+ ...delegate ,
3696+ publishCommitted : ( events ) =>
3697+ ( committed ? pause : Effect . void ) . pipe (
3698+ Effect . andThen ( delegate . publishCommitted ( events ) ) ,
3699+ ) ,
3700+ } ) ;
3701+ } ) ,
3702+ ) . pipe ( Layer . provide ( layerEventStoreProvided ) ) ,
3703+ Layer . effect (
3704+ EffectOutbox . EffectOutboxV2 ,
3705+ Effect . gen ( function * ( ) {
3706+ const delegate = yield * EffectOutbox . EffectOutboxV2 ;
3707+ return EffectOutbox . EffectOutboxV2 . of ( {
3708+ ...delegate ,
3709+ enqueue : ( effects ) =>
3710+ delegate
3711+ . enqueue ( effects )
3712+ . pipe (
3713+ Effect . andThen ( committed ? Effect . void : pause ) ,
3714+ Effect . andThen (
3715+ scenario === "fail transaction"
3716+ ? Effect . die ( "Injected rollback" )
3717+ : Effect . void ,
3718+ ) ,
3719+ ) ,
3720+ } ) ;
3721+ } ) ,
3722+ ) . pipe ( Layer . provide ( layerEffectOutboxProvided ) ) ,
3723+ layerCommandReceiptStoreProvided ,
3724+ ProjectStore . layer . pipe ( Layer . provide ( layerDatabase ) ) ,
3725+ TurnItemPositionStore . layer . pipe ( Layer . provide ( layerDatabase ) ) ,
3726+ ) ;
3727+ yield * Effect . gen ( function * ( ) {
3728+ const sink = yield * EventSink . EventSinkV2 ;
3729+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
3730+ const eventStore = yield * EventStore . EventStoreV2 ;
3731+ const now = yield * DateTime . now ;
3732+ const thread = makeThread ( ThreadId . make ( "thread:commit-handoff" ) , now ) ;
3733+ const events = [ threadCreatedEvent ( { id : "event:commit-handoff" , thread, now } ) ] ;
3734+ const handedOff : Array < OrchestrationV2DomainEvent > = [ ] ;
3735+ const writer = yield * sink
3736+ . write ( {
3737+ events,
3738+ onCommitted : ( stored ) => {
3739+ handedOff . push ( ...stored . map ( ( entry ) => entry . event ) ) ;
3740+ } ,
3741+ } )
3742+ . pipe ( Effect . forkScoped ) ;
3743+ yield * Deferred . await ( paused ) ;
3744+ // The handoff precedes publication, and never sees transaction work that can roll back.
3745+ assert . deepEqual ( handedOff , committed ? events : [ ] ) ;
3746+ if ( scenario !== "fail transaction" ) writer . interruptUnsafe ( ) ;
3747+ if ( scenario !== "cancel transaction" ) yield * Deferred . succeed ( resume , undefined ) ;
3748+ assert . equal ( ( yield * Fiber . await ( writer ) ) . _tag , "Failure" ) ;
3749+ assert . deepEqual ( handedOff , committed ? events : [ ] ) ;
3750+ assert . equal (
3751+ ( yield * projections . getThreadShell ( thread . id ) ) ?. id ?? null ,
3752+ committed ? thread . id : null ,
3753+ ) ;
3754+ const stored = yield * eventStore . read ( { threadId : thread . id } ) . pipe ( Stream . runCollect ) ;
3755+ assert . deepEqual (
3756+ Array . from ( stored , ( entry ) => entry . event ) ,
3757+ committed ? events : [ ] ,
3758+ ) ;
3759+ } ) . pipe ( Effect . provide ( EventSink . layerFromStores . pipe ( Layer . provideMerge ( stores ) ) ) ) ;
3760+ } ) . pipe ( Effect . provide ( layerDatabase ) ) ,
3761+ ) ;
0 commit comments