@@ -169,6 +169,17 @@ export interface EventSinkV2Shape {
169169 } ,
170170 EventSinkV2Error
171171 > ;
172+ /** Commits only if the stored turn item still equals expected. */
173+ readonly writeIfTurnItemUnchanged : ( input : {
174+ readonly expected : OrchestrationV2TurnItem ;
175+ readonly events : ReadonlyArray < OrchestrationV2DomainEvent > ;
176+ } ) => Effect . Effect <
177+ {
178+ readonly committed : boolean ;
179+ readonly storedEvents : ReadonlyArray < OrchestrationV2StoredEvent > ;
180+ } ,
181+ EventSinkV2Error
182+ > ;
172183 readonly commitCommand : ( input : {
173184 readonly commandId : CommandId ;
174185 readonly threadId : ThreadId ;
@@ -634,31 +645,23 @@ const layerBase: Layer.Layer<
634645 ) ;
635646 } ) ;
636647
637- const writeIfProviderThreadUnchangedEffect = Effect . fn (
638- "orchestrationV2.EventSink.writeIfProviderThreadUnchanged" ,
639- ) ( function * ( input : Parameters < EventSinkV2Shape [ "writeIfProviderThreadUnchanged" ] > [ 0 ] ) {
640- yield * Effect . annotateCurrentSpan ( {
641- "orchestration_v2.event_count" : input . events . length ,
642- "orchestration_v2.provider_thread_id" : input . expected . id ,
643- } ) ;
644- const expectedJson = yield * ProjectionStore . encodeProviderThreadPayload ( input . expected ) ;
645-
646- return yield * commitThenPublish (
648+ // Appends the events only if the stored payload still matches, in one transaction.
649+ const writeIfStoredPayloadMatches = < E > (
650+ readStoredPayload : Effect . Effect < ReadonlyArray < { readonly payload_json : string } > , E > ,
651+ expectedJson : string ,
652+ events : ReadonlyArray < OrchestrationV2DomainEvent > ,
653+ ) =>
654+ commitThenPublish (
647655 Effect . gen ( function * ( ) {
648- const rows = yield * sql < { readonly payload_json : string } > `
649- SELECT payload_json
650- FROM orchestration_v2_projection_provider_threads
651- WHERE provider_thread_id = ${ input . expected . id }
652- LIMIT 1
653- ` ;
656+ const rows = yield * readStoredPayload ;
654657 if ( rows [ 0 ] ?. payload_json !== expectedJson ) {
655658 return {
656659 committed : false as const ,
657660 storedEvents : [ ] as ReadonlyArray < OrchestrationV2StoredEvent > ,
658661 } ;
659662 }
660663
661- const normalized = yield * normalizeEvents ( input . events ) ;
664+ const normalized = yield * normalizeEvents ( events ) ;
662665 const storedEvents = yield * eventStore . append ( { events : normalized } ) ;
663666 const outcomes = yield * applyStoredEvents ( storedEvents ) ;
664667 return { committed : true as const , storedEvents, outcomes } ;
@@ -668,6 +671,45 @@ const layerBase: Layer.Layer<
668671 ? publishStoredEvents ( result . storedEvents , result . outcomes )
669672 : Effect . void ,
670673 ) ;
674+
675+ const writeIfProviderThreadUnchangedEffect = Effect . fn (
676+ "orchestrationV2.EventSink.writeIfProviderThreadUnchanged" ,
677+ ) ( function * ( input : Parameters < EventSinkV2Shape [ "writeIfProviderThreadUnchanged" ] > [ 0 ] ) {
678+ yield * Effect . annotateCurrentSpan ( {
679+ "orchestration_v2.event_count" : input . events . length ,
680+ "orchestration_v2.provider_thread_id" : input . expected . id ,
681+ } ) ;
682+ const expectedJson = yield * ProjectionStore . encodeProviderThreadPayload ( input . expected ) ;
683+ return yield * writeIfStoredPayloadMatches (
684+ sql < { readonly payload_json : string } > `
685+ SELECT payload_json
686+ FROM orchestration_v2_projection_provider_threads
687+ WHERE provider_thread_id = ${ input . expected . id }
688+ LIMIT 1
689+ ` ,
690+ expectedJson ,
691+ input . events ,
692+ ) ;
693+ } ) ;
694+
695+ const writeIfTurnItemUnchangedEffect = Effect . fn (
696+ "orchestrationV2.EventSink.writeIfTurnItemUnchanged" ,
697+ ) ( function * ( input : Parameters < EventSinkV2Shape [ "writeIfTurnItemUnchanged" ] > [ 0 ] ) {
698+ yield * Effect . annotateCurrentSpan ( {
699+ "orchestration_v2.event_count" : input . events . length ,
700+ "orchestration_v2.turn_item_id" : input . expected . id ,
701+ } ) ;
702+ const expectedJson = yield * ProjectionStore . encodeTurnItemPayload ( input . expected ) ;
703+ return yield * writeIfStoredPayloadMatches (
704+ sql < { readonly payload_json : string } > `
705+ SELECT payload_json
706+ FROM orchestration_v2_projection_turn_items
707+ WHERE turn_item_id = ${ input . expected . id }
708+ LIMIT 1
709+ ` ,
710+ expectedJson ,
711+ input . events ,
712+ ) ;
671713 } ) ;
672714
673715 const existingCommandResult =
@@ -1070,6 +1112,12 @@ const layerBase: Layer.Layer<
10701112 ( cause ) => new EventSinkWriteError ( { eventCount : input . events . length , cause } ) ,
10711113 ) ,
10721114 ) ,
1115+ writeIfTurnItemUnchanged : ( input ) =>
1116+ writeIfTurnItemUnchangedEffect ( input ) . pipe (
1117+ Effect . mapError (
1118+ ( cause ) => new EventSinkWriteError ( { eventCount : input . events . length , cause } ) ,
1119+ ) ,
1120+ ) ,
10731121 commitCommand : ( input ) =>
10741122 commitCommandEffect ( input ) . pipe (
10751123 Effect . mapError (
0 commit comments