@@ -12,6 +12,7 @@ import {
1212 type ProjectId ,
1313 ThreadId ,
1414} from "@t3tools/contracts" ;
15+ import * as KeyedLock from "@t3tools/shared/KeyedLock" ;
1516import * as Context from "effect/Context" ;
1617import * as DateTime from "effect/DateTime" ;
1718import * as Effect from "effect/Effect" ;
@@ -223,6 +224,7 @@ const layerBase: Layer.Layer<
223224 Effect . gen ( function * ( ) {
224225 const sql = yield * SqlClient . SqlClient ;
225226 const commandReceipts = yield * CommandReceiptStore . CommandReceiptStoreV2 ;
227+ const commandLocks = yield * KeyedLock . make < CommandId > ( ) ;
226228 const effectOutbox = yield * EffectOutbox . EffectOutboxV2 ;
227229 const eventStore = yield * EventStore . EventStoreV2 ;
228230 const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
@@ -719,6 +721,16 @@ const layerBase: Layer.Layer<
719721
720722 const commitProjectCommandEffect = Effect . fn ( "orchestrationV2.EventSink.commitProjectCommand" ) (
721723 function * ( input : Parameters < EventSinkV2Shape [ "commitProjectCommand" ] > [ 0 ] ) {
724+ const existing = yield * commandReceipts . getProjectByCommandId ( input . commandId ) ;
725+ if ( Option . isSome ( existing ) ) return { receipt : existing . value , committed : false } ;
726+ if ( Option . isSome ( yield * commandReceipts . getByCommandId ( input . commandId ) ) ) {
727+ return yield * Effect . fail (
728+ `Command ${ input . commandId } was already used by a thread command.` ,
729+ ) ;
730+ }
731+ // Settings takes its own lock before reading SQL. Never hold the SQL writer while
732+ // waiting for it. The command lock keeps concurrent collisions from rerunning this write.
733+ if ( input . beforeCommit !== undefined ) yield * input . beforeCommit ;
722734 const result = yield * commitThenPublish (
723735 Effect . gen ( function * ( ) {
724736 const reserved : CommandReceiptStore . ProjectCommandReceiptV2 = {
@@ -735,9 +747,6 @@ const layerBase: Layer.Layer<
735747 }
736748 const event = yield * eventStore . appendProjectEvent ( input . event ) ;
737749 yield * projectStore . apply ( event ) ;
738- // A failed settings write must leave no accepted receipt to suppress its retry.
739- // Existing receipts return above, so successful replays never rerun this write.
740- if ( input . beforeCommit !== undefined ) yield * input . beforeCommit ;
741750 const receipt = { ...reserved , resultSequence : event . sequence } ;
742751 yield * commandReceipts . upsert ( receipt ) ;
743752 return { receipt, event } ;
@@ -903,7 +912,7 @@ const layerBase: Layer.Layer<
903912 ) ,
904913 ) ,
905914 commitCommand : ( input ) =>
906- commitCommandEffect ( input ) . pipe (
915+ commandLocks . withLock ( input . commandId , commitCommandEffect ( input ) ) . pipe (
907916 Effect . mapError (
908917 ( cause ) =>
909918 new EventSinkWriteError ( {
@@ -914,7 +923,7 @@ const layerBase: Layer.Layer<
914923 ) ,
915924 ) ,
916925 commitRejectedCommand : ( input ) =>
917- commitRejectedCommandEffect ( input ) . pipe (
926+ commandLocks . withLock ( input . commandId , commitRejectedCommandEffect ( input ) ) . pipe (
918927 Effect . mapError (
919928 ( cause ) =>
920929 new EventSinkWriteError ( {
@@ -925,19 +934,23 @@ const layerBase: Layer.Layer<
925934 ) ,
926935 ) ,
927936 commitProjectCommand : ( input ) =>
928- commitProjectCommandEffect ( input ) . pipe (
929- Effect . mapError (
930- ( cause ) =>
931- new EventSinkWriteError ( { commandId : input . commandId , eventCount : 1 , cause } ) ,
937+ commandLocks
938+ . withLock ( input . commandId , commitProjectCommandEffect ( input ) )
939+ . pipe (
940+ Effect . mapError (
941+ ( cause ) =>
942+ new EventSinkWriteError ( { commandId : input . commandId , eventCount : 1 , cause } ) ,
943+ ) ,
932944 ) ,
933- ) ,
934945 commitRejectedProjectCommand : ( input ) =>
935- commitRejectedProjectCommandEffect ( input ) . pipe (
936- Effect . mapError (
937- ( cause ) =>
938- new EventSinkWriteError ( { commandId : input . commandId , eventCount : 0 , cause } ) ,
946+ commandLocks
947+ . withLock ( input . commandId , commitRejectedProjectCommandEffect ( input ) )
948+ . pipe (
949+ Effect . mapError (
950+ ( cause ) =>
951+ new EventSinkWriteError ( { commandId : input . commandId , eventCount : 0 , cause } ) ,
952+ ) ,
939953 ) ,
940- ) ,
941954 stream : ( input ) =>
942955 stream ( input ) . pipe (
943956 Stream . mapError (
0 commit comments