@@ -17,6 +17,7 @@ import * as Effect from "effect/Effect";
1717import * as Layer from "effect/Layer" ;
1818import * as Option from "effect/Option" ;
1919import * as PubSub from "effect/PubSub" ;
20+ import * as Semaphore from "effect/Semaphore" ;
2021import * as Schema from "effect/Schema" ;
2122import * as Stream from "effect/Stream" ;
2223import * as SqlClient from "effect/unstable/sql/SqlClient" ;
@@ -224,6 +225,40 @@ const baseLayer: Layer.Layer<
224225 ) ;
225226 }
226227 } ) ;
228+ const publishStoredEvents = ( events : ReadonlyArray < OrchestrationV2StoredEvent > ) =>
229+ eventStore . publishCommitted ( events ) . pipe ( Effect . andThen ( publishLiveEvents ( events ) ) ) ;
230+
231+ // Transactions commit one at a time, but each writer publishes after its
232+ // commit. If a writer is descheduled in between, a later commit reaches
233+ // subscribers first, and clients drop any event at or below the newest
234+ // sequence they have applied. So a writer takes this lane as the last step
235+ // of its transaction and holds it until it has published. Publishing never
236+ // waits, so a writer that holds the transaction while it waits for the
237+ // lane is not blocked for long.
238+ const publishLane = yield * Semaphore . make ( 1 ) ;
239+ const commitThenPublish = < A , E , R > (
240+ transaction : Effect . Effect < A , E , R > ,
241+ publish : ( committed : A ) => Effect . Effect < void > ,
242+ ) =>
243+ Effect . suspend ( ( ) => {
244+ let holdsLane = false ;
245+ const takeLane = publishLane . take ( 1 ) . pipe (
246+ Effect . andThen (
247+ Effect . sync ( ( ) => {
248+ holdsLane = true ;
249+ } ) ,
250+ ) ,
251+ Effect . uninterruptible ,
252+ ) ;
253+ return sql
254+ . withTransaction ( Effect . tap ( transaction , ( ) => takeLane ) )
255+ . pipe (
256+ Effect . tap ( publish ) ,
257+ Effect . ensuring (
258+ Effect . suspend ( ( ) => ( holdsLane ? publishLane . release ( 1 ) : Effect . void ) ) ,
259+ ) ,
260+ ) ;
261+ } ) ;
227262
228263 // A user can answer after terminal normalization reads the pending request.
229264 // Recheck inside the write transaction so stale cleanup cannot erase answers.
@@ -332,7 +367,7 @@ const baseLayer: Layer.Layer<
332367 "orchestration_v2.thread_id" : input . events [ 0 ] ?. threadId ?? null ,
333368 } ) ;
334369
335- const storedEvents = yield * sql . withTransaction (
370+ return yield * commitThenPublish (
336371 Effect . gen ( function * ( ) {
337372 const normalized = yield * normalizeEvents (
338373 input . guardPendingUserInputCancellations === true
@@ -347,13 +382,14 @@ const baseLayer: Layer.Layer<
347382 yield * effectOutbox . enqueue ( input . effects ) ;
348383 return committed ;
349384 } ) ,
385+ ( storedEvents ) =>
386+ Effect . gen ( function * ( ) {
387+ if ( input . effects . length > 0 ) {
388+ yield * effectOutbox . notifyAvailable ( input . effects . length ) ;
389+ }
390+ yield * publishStoredEvents ( storedEvents ) ;
391+ } ) ,
350392 ) ;
351- if ( input . effects . length > 0 ) {
352- yield * effectOutbox . notifyAvailable ( input . effects . length ) ;
353- }
354- yield * eventStore . publishCommitted ( storedEvents ) ;
355- yield * publishLiveEvents ( storedEvents ) ;
356- return storedEvents ;
357393 } ) ;
358394
359395 const writeIfRunCurrentEffect = Effect . fn ( "orchestrationV2.EventSink.writeIfRunCurrent" ) (
@@ -365,7 +401,7 @@ const baseLayer: Layer.Layer<
365401 "orchestration_v2.thread_id" : input . threadId ,
366402 } ) ;
367403
368- const result = yield * sql . withTransaction (
404+ return yield * commitThenPublish (
369405 Effect . gen ( function * ( ) {
370406 const rows = yield * sql < {
371407 readonly status : string ;
@@ -403,12 +439,8 @@ const baseLayer: Layer.Layer<
403439 yield * applyStoredEvents ( storedEvents ) ;
404440 return { committed : true as const , storedEvents } ;
405441 } ) ,
442+ ( result ) => ( result . committed ? publishStoredEvents ( result . storedEvents ) : Effect . void ) ,
406443 ) ;
407- if ( result . committed ) {
408- yield * eventStore . publishCommitted ( result . storedEvents ) ;
409- yield * publishLiveEvents ( result . storedEvents ) ;
410- }
411- return result ;
412444 } ,
413445 ) ;
414446
@@ -424,7 +456,7 @@ const baseLayer: Layer.Layer<
424456 "orchestration_v2.expected_last_run_ordinal" : input . expectedLastRunOrdinal ,
425457 } ) ;
426458
427- const result = yield * sql . withTransaction (
459+ return yield * commitThenPublish (
428460 Effect . gen ( function * ( ) {
429461 const rows = yield * sql < {
430462 readonly active_attempt_id : string | null ;
@@ -464,12 +496,8 @@ const baseLayer: Layer.Layer<
464496 yield * applyStoredEvents ( storedEvents ) ;
465497 return { committed : true as const , storedEvents } ;
466498 } ) ,
499+ ( result ) => ( result . committed ? publishStoredEvents ( result . storedEvents ) : Effect . void ) ,
467500 ) ;
468- if ( result . committed ) {
469- yield * eventStore . publishCommitted ( result . storedEvents ) ;
470- yield * publishLiveEvents ( result . storedEvents ) ;
471- }
472- return result ;
473501 } ) ;
474502
475503 const existingCommandResult = ( commandId : CommandId ) =>
@@ -490,7 +518,7 @@ const baseLayer: Layer.Layer<
490518 const commitCommandEffect = Effect . fn ( "orchestrationV2.EventSink.commitCommand" ) ( function * (
491519 input : Parameters < EventSinkV2Shape [ "commitCommand" ] > [ 0 ] ,
492520 ) {
493- const result = yield * sql . withTransaction (
521+ const result = yield * commitThenPublish (
494522 Effect . gen ( function * ( ) {
495523 const reserved = yield * commandReceipts . insertIfAbsent ( {
496524 commandId : input . commandId ,
@@ -538,15 +566,15 @@ const baseLayer: Layer.Layer<
538566 } ) ;
539567 return { receipt, storedEvents, committed : true as const , cancelledEffectIds } ;
540568 } ) ,
569+ ( result ) =>
570+ Effect . gen ( function * ( ) {
571+ yield * effectOutbox . signalCancellations ( result . cancelledEffectIds ) ;
572+ if ( result . committed && input . effects . length > 0 ) {
573+ yield * effectOutbox . notifyAvailable ( input . effects . length ) ;
574+ }
575+ if ( result . committed ) yield * publishStoredEvents ( result . storedEvents ) ;
576+ } ) ,
541577 ) ;
542- yield * effectOutbox . signalCancellations ( result . cancelledEffectIds ) ;
543- if ( result . committed && input . effects . length > 0 ) {
544- yield * effectOutbox . notifyAvailable ( input . effects . length ) ;
545- }
546- if ( result . committed ) {
547- yield * eventStore . publishCommitted ( result . storedEvents ) ;
548- yield * publishLiveEvents ( result . storedEvents ) ;
549- }
550578 return {
551579 receipt : result . receipt ,
552580 storedEvents : result . storedEvents ,
@@ -593,7 +621,7 @@ const baseLayer: Layer.Layer<
593621
594622 const commitProjectCommandEffect = Effect . fn ( "orchestrationV2.EventSink.commitProjectCommand" ) (
595623 function * ( input : Parameters < EventSinkV2Shape [ "commitProjectCommand" ] > [ 0 ] ) {
596- const result = yield * sql . withTransaction (
624+ const result = yield * commitThenPublish (
597625 Effect . gen ( function * ( ) {
598626 const reserved : CommandReceiptStore . ProjectCommandReceiptV2 = {
599627 commandId : input . commandId ,
@@ -613,10 +641,9 @@ const baseLayer: Layer.Layer<
613641 yield * commandReceipts . upsert ( receipt ) ;
614642 return { receipt, event } ;
615643 } ) ,
644+ ( result ) =>
645+ result . event === undefined ? Effect . void : eventStore . publishCommitted ( [ result . event ] ) ,
616646 ) ;
617- if ( result . event !== undefined ) {
618- yield * eventStore . publishCommitted ( [ result . event ] ) ;
619- }
620647 return { receipt : result . receipt , committed : result . event !== undefined } ;
621648 } ,
622649 ) ;
0 commit comments