@@ -205,6 +205,7 @@ export type ExactReviewLifecycleProjection = {
205205 revision : number ;
206206 admission : {
207207 deliveryId : string ;
208+ sourceDeliveryId ?: string ;
208209 sourceAction : string ;
209210 commandOriginated : boolean ;
210211 statusMarker : string | null ;
@@ -297,6 +298,7 @@ export class ExactReviewLifecycleProjectionStore {
297298 recordAdmission (
298299 input : ProjectionIdentity & {
299300 deliveryId : string ;
301+ sourceDeliveryId ?: string ;
300302 sourceAction : string ;
301303 commandOriginated : boolean ;
302304 statusMarker : string | null ;
@@ -308,6 +310,9 @@ export class ExactReviewLifecycleProjectionStore {
308310 if ( ! validText ( input . deliveryId , 1 , 300 ) || ! validText ( input . sourceAction , 1 , 200 ) ) {
309311 throw new Error ( "invalid lifecycle admission fact" ) ;
310312 }
313+ if ( input . sourceDeliveryId !== undefined && ! validText ( input . sourceDeliveryId , 1 , 200 ) ) {
314+ throw new Error ( "invalid lifecycle source delivery identity" ) ;
315+ }
311316 if ( input . statusMarker !== null && ! validText ( input . statusMarker , 1 , 300 ) ) {
312317 throw new Error ( "invalid lifecycle status marker" ) ;
313318 }
@@ -322,6 +327,7 @@ export class ExactReviewLifecycleProjectionStore {
322327 const admission = existing . admission ;
323328 if (
324329 admission . deliveryId !== input . deliveryId ||
330+ admission . sourceDeliveryId !== input . sourceDeliveryId ||
325331 admission . sourceAction !== input . sourceAction ||
326332 admission . commandOriginated !== input . commandOriginated ||
327333 admission . statusMarker !== input . statusMarker ||
@@ -338,6 +344,7 @@ export class ExactReviewLifecycleProjectionStore {
338344 revision : input . revision ,
339345 admission : {
340346 deliveryId : input . deliveryId ,
347+ ...( input . sourceDeliveryId ? { sourceDeliveryId : input . sourceDeliveryId } : { } ) ,
341348 sourceAction : input . sourceAction ,
342349 commandOriginated : input . commandOriginated ,
343350 statusMarker : input . statusMarker ,
@@ -681,16 +688,24 @@ export class ExactReviewLifecycleProjectionStore {
681688
682689 observeCommandAcknowledgement ( input : {
683690 canonicalTargetKey : string ;
691+ fenceKey ?: string ;
692+ revision ?: number ;
684693 statusMarker : string | null ;
685694 commandCommentId : number ;
686695 completionCommentId : number ;
696+ statusCommentId ?: number ;
697+ requireExactStatusComment ?: boolean ;
687698 observedAt : number ;
688699 } ) {
689700 if (
690701 ! validCanonicalTargetKey ( input . canonicalTargetKey ) ||
702+ ( input . fenceKey === undefined ) !== ( input . revision === undefined ) ||
703+ ( input . fenceKey !== undefined && ! validFenceKey ( input . fenceKey ) ) ||
704+ ( input . revision !== undefined && ! positiveInteger ( input . revision ) ) ||
691705 ( input . statusMarker !== null && ! validText ( input . statusMarker , 1 , 300 ) ) ||
692706 ! positiveInteger ( input . commandCommentId ) ||
693- ! positiveInteger ( input . completionCommentId )
707+ ! positiveInteger ( input . completionCommentId ) ||
708+ ( input . statusCommentId !== undefined && ! positiveInteger ( input . statusCommentId ) )
694709 ) {
695710 throw new Error ( "invalid lifecycle acknowledgement receipt" ) ;
696711 }
@@ -702,14 +717,51 @@ export class ExactReviewLifecycleProjectionStore {
702717 input . canonicalTargetKey ,
703718 ) ,
704719 ) ;
720+ const matchedProjections : ExactReviewLifecycleProjection [ ] = [ ] ;
705721 for ( const row of rows ) {
706722 const projection = projectionFromRow ( String ( row . projection_json || "" ) ) ;
723+ if (
724+ input . fenceKey !== undefined &&
725+ ( projection . fenceKey !== input . fenceKey || projection . revision !== input . revision )
726+ ) {
727+ continue ;
728+ }
707729 const attempted = projection . acknowledgement . attempts . some (
708730 ( attempt ) =>
709- attempt . statusCommentId === input . completionCommentId ||
710- ( input . statusMarker !== null && attempt . statusMarker === input . statusMarker ) ,
731+ ( input . statusCommentId === undefined ||
732+ attempt . statusCommentId === input . statusCommentId ) &&
733+ ( ( attempt . statusCommentId === input . completionCommentId &&
734+ ( attempt . statusMarker === input . statusMarker ||
735+ attempt . statusMarker === null ||
736+ ( ! input . requireExactStatusComment && input . statusMarker === null ) ) ) ||
737+ ( input . requireExactStatusComment &&
738+ attempt . statusCommentId === null &&
739+ input . statusMarker !== null &&
740+ attempt . statusMarker === input . statusMarker ) ||
741+ ( ! input . requireExactStatusComment &&
742+ input . statusMarker !== null &&
743+ attempt . statusMarker === input . statusMarker ) ) ,
711744 ) ;
712- if ( ! attempted || ! projection . acknowledgement . required ) continue ;
745+ if ( attempted && projection . acknowledgement . required ) matchedProjections . push ( projection ) ;
746+ }
747+ const exactStatusCommentMatches =
748+ input . fenceKey === undefined && matchedProjections . length > 1
749+ ? matchedProjections . filter ( ( projection ) =>
750+ projection . acknowledgement . attempts . some (
751+ ( attempt ) => attempt . statusCommentId === input . completionCommentId ,
752+ ) ,
753+ )
754+ : [ ] ;
755+ if (
756+ input . fenceKey === undefined &&
757+ matchedProjections . length > 1 &&
758+ exactStatusCommentMatches . length !== 1
759+ ) {
760+ return { accepted : false , projection : null , state : null , acknowledgement : null } ;
761+ }
762+ for ( const projection of exactStatusCommentMatches . length
763+ ? exactStatusCommentMatches
764+ : matchedProjections ) {
713765 const observed = {
714766 statusMarker : input . statusMarker ,
715767 commandCommentId : input . commandCommentId ,
@@ -1655,6 +1707,8 @@ function validDurableLifecycleBayProjection(value: ExactReviewLifecycleProjectio
16551707 ! positiveInteger ( value . revision ) ||
16561708 ! finiteTimestamp ( value . updatedAt ) ||
16571709 ! validText ( value . admission . deliveryId , 1 , 300 ) ||
1710+ ( value . admission . sourceDeliveryId !== undefined &&
1711+ ! validText ( value . admission . sourceDeliveryId , 1 , 200 ) ) ||
16581712 ! validText ( value . admission . sourceAction , 1 , 200 ) ||
16591713 ( value . admission . statusMarker !== null && ! validText ( value . admission . statusMarker , 1 , 300 ) ) ||
16601714 ( value . admission . statusCommentId !== null &&
0 commit comments