33 exactReviewQueueHasCommandContext ,
44 exactReviewQueueIsBatchablePublication ,
55 exactReviewQueueIsPublication ,
6+ exactReviewQueueUsesLegacyBatchPath ,
67 isLowPriorityExactReviewDecision ,
78} from "./exact-review-decision.ts" ;
89import { numberFrom } from "./exact-review-queue-shared.ts" ;
@@ -187,6 +188,7 @@ type ExactReviewBayProjectionItem = {
187188 created_at : string ;
188189 updated_at : string ;
189190 next_attempt_at : string ;
191+ legacy_batch_path : boolean ;
190192 batch_id ?: string ;
191193 batch_created_at ?: string ;
192194} ;
@@ -651,9 +653,24 @@ function observeExactReviewBayCandidate(
651653 updatedAt,
652654 } ;
653655 const previous = projected . get ( itemKey ) ;
654- const latest = previous ?. [ 0 ] ;
655- if ( ! latest || updatedAt > latest . updatedAt ) {
656+ if ( ! previous ) {
656657 projected . set ( itemKey , [ candidate ] ) ;
658+ return true ;
659+ }
660+ const legacyBatchPath = exactReviewQueueUsesLegacyBatchPath ( item ) ;
661+ const samePath = previous . filter (
662+ ( existing ) => exactReviewQueueUsesLegacyBatchPath ( existing . item ) === legacyBatchPath ,
663+ ) ;
664+ const latest = samePath [ 0 ] ;
665+ if ( ! latest ) {
666+ previous . push ( candidate ) ;
667+ } else if ( updatedAt > latest . updatedAt ) {
668+ projected . set ( itemKey , [
669+ ...previous . filter (
670+ ( existing ) => exactReviewQueueUsesLegacyBatchPath ( existing . item ) !== legacyBatchPath ,
671+ ) ,
672+ candidate ,
673+ ] ) ;
657674 } else if ( updatedAt === latest . updatedAt ) {
658675 previous . push ( candidate ) ;
659676 }
@@ -674,6 +691,7 @@ export function exactReviewQueueBayProjection(
674691 priorityItemKeys : string [ ] = [ ] ,
675692 batchByItemKey : ReadonlyMap < string , ExactReviewBayBatchOwner > = new Map ( ) ,
676693 activeItemKeys : string [ ] = [ ] ,
694+ activeLegacyItemKeys : string [ ] = [ ] ,
677695) {
678696 const census = buildExactReviewQueueCensus ( items , {
679697 state : { items : { } } ,
@@ -684,6 +702,7 @@ export function exactReviewQueueBayProjection(
684702 priorityItemKeys ,
685703 batchByItemKey ,
686704 activeItemKeys ,
705+ activeLegacyItemKeys ,
687706 ) ;
688707}
689708
@@ -692,14 +711,17 @@ function exactReviewQueueBayProjectionFromCensus(
692711 priorityItemKeys : string [ ] = [ ] ,
693712 batchByItemKey : ReadonlyMap < string , ExactReviewBayBatchOwner > = new Map ( ) ,
694713 activeItemKeys : string [ ] = [ ] ,
714+ activeLegacyItemKeys : string [ ] = [ ] ,
695715) {
696716 if ( ! census . bayComplete ) {
697717 return {
698718 complete : false as const ,
699719 sample_limit : EXACT_REVIEW_BAY_SAMPLE_LIMIT ,
700720 total : null ,
701721 stages : null ,
722+ legacy_batch_stages : null ,
702723 active_overlaps : null ,
724+ legacy_batch_active_overlaps : null ,
703725 items : [ ] ,
704726 } ;
705727 }
@@ -708,6 +730,9 @@ function exactReviewQueueBayProjectionFromCensus(
708730 ExactReviewBayStage ,
709731 number
710732 > ;
733+ const legacyBatchStages = Object . fromEntries (
734+ EXACT_REVIEW_BAY_STAGES . map ( ( stage ) => [ stage , 0 ] ) ,
735+ ) as Record < ExactReviewBayStage , number > ;
711736 const rowsByStage : Record < ExactReviewBayStage , ExactReviewBayProjectionItem [ ] > = {
712737 arriving : [ ] ,
713738 "setting-up" : [ ] ,
@@ -717,7 +742,10 @@ function exactReviewQueueBayProjectionFromCensus(
717742 repairing : [ ] ,
718743 } ;
719744 for ( const candidates of census . bayCandidates . values ( ) ) {
720- const [ first , ...remaining ] = candidates ;
745+ const directCandidates = candidates . filter (
746+ ( candidate ) => ! exactReviewQueueUsesLegacyBatchPath ( candidate . item ) ,
747+ ) ;
748+ const [ first , ...remaining ] = directCandidates . length > 0 ? directCandidates : candidates ;
721749 if ( ! first ) continue ;
722750 let selected = first ;
723751 let selectedStage = exactReviewQueueBayStage ( selected . item , batchByItemKey ) ;
@@ -740,10 +768,12 @@ function exactReviewQueueBayProjectionFromCensus(
740768 created_at : new Date ( selected . item . createdAt ) . toISOString ( ) ,
741769 updated_at : new Date ( selected . item . updatedAt ) . toISOString ( ) ,
742770 next_attempt_at : new Date ( selected . item . nextAttemptAt ) . toISOString ( ) ,
771+ legacy_batch_path : exactReviewQueueUsesLegacyBatchPath ( selected . item ) ,
743772 ...( batch ? { batch_id : batch . batchId } : { } ) ,
744773 } ;
745774 projected . set ( row . item_key , row ) ;
746775 stages [ row . stage ] += 1 ;
776+ if ( row . legacy_batch_path ) legacyBatchStages [ row . stage ] += 1 ;
747777 rowsByStage [ row . stage ] . push ( row ) ;
748778 }
749779 for ( const stage of EXACT_REVIEW_BAY_STAGES ) {
@@ -756,37 +786,71 @@ function exactReviewQueueBayProjectionFromCensus(
756786 const activeOverlaps = Object . fromEntries (
757787 EXACT_REVIEW_BAY_STAGES . map ( ( stage ) => [ stage , 0 ] ) ,
758788 ) as Record < ExactReviewBayStage , number > ;
759- for ( const itemKey of exactReviewQueueBayActiveKeys ( activeItemKeys ) ) {
760- const item = projected . get ( itemKey ) ;
761- if ( item ) activeOverlaps [ item . stage ] += 1 ;
789+ const legacyBatchActiveOverlaps = Object . fromEntries (
790+ EXACT_REVIEW_BAY_STAGES . map ( ( stage ) => [ stage , 0 ] ) ,
791+ ) as Record < ExactReviewBayStage , number > ;
792+ for ( const [ itemKeys , legacyBatchPath ] of [
793+ [ activeItemKeys , false ] ,
794+ [ activeLegacyItemKeys , true ] ,
795+ ] as const ) {
796+ for ( const itemKey of exactReviewQueueBayActiveKeys ( itemKeys ) ) {
797+ const item = projected . get ( itemKey ) ;
798+ if ( item && item . legacy_batch_path === legacyBatchPath ) {
799+ activeOverlaps [ item . stage ] += 1 ;
800+ if ( legacyBatchPath ) legacyBatchActiveOverlaps [ item . stage ] += 1 ;
801+ }
802+ }
762803 }
763804 const priorityRows = exactReviewQueueBayPriorityKeys ( priorityItemKeys )
764805 . map ( ( itemKey ) => projected . get ( itemKey ) )
765- . filter ( ( item ) : item is ExactReviewBayProjectionItem => Boolean ( item ) )
766- . slice ( 0 , EXACT_REVIEW_BAY_SAMPLE_LIMIT ) ;
767- const priorityKeys = new Set ( priorityRows . map ( ( item ) => item . item_key ) ) ;
768- const sample = [ ...priorityRows ] ;
769- const longestStage = Math . max (
770- ...EXACT_REVIEW_BAY_STAGES . map ( ( stage ) => rowsByStage [ stage ] . length ) ,
771- ) ;
772- for (
773- let index = 0 ;
774- sample . length < EXACT_REVIEW_BAY_SAMPLE_LIMIT && index < longestStage ;
775- index += 1
776- ) {
777- for ( const stage of EXACT_REVIEW_BAY_STAGES ) {
778- const item = rowsByStage [ stage ] [ index ] ;
779- if ( ! item || priorityKeys . has ( item . item_key ) ) continue ;
780- sample . push ( item ) ;
781- if ( sample . length === EXACT_REVIEW_BAY_SAMPLE_LIMIT ) break ;
806+ . filter ( ( item ) : item is ExactReviewBayProjectionItem => Boolean ( item ) ) ;
807+ const sampledKeys = new Set < string > ( ) ;
808+ const sample : ExactReviewBayProjectionItem [ ] = [ ] ;
809+ const append = ( item : ExactReviewBayProjectionItem | undefined , legacyBatchPath : boolean ) => {
810+ if (
811+ ! item ||
812+ item . legacy_batch_path !== legacyBatchPath ||
813+ sampledKeys . has ( item . item_key ) ||
814+ sample . length >= EXACT_REVIEW_BAY_SAMPLE_LIMIT
815+ ) {
816+ return ;
817+ }
818+ sampledKeys . add ( item . item_key ) ;
819+ sample . push ( item ) ;
820+ } ;
821+ // The default Bay view hides legacy proof/batch journeys. Sample every
822+ // available normal direct journey before legacy rows so hidden records
823+ // cannot consume the bounded public-reference budget.
824+ for ( const legacyBatchPath of [ false , true ] ) {
825+ const categoryRowsByStage = Object . fromEntries (
826+ EXACT_REVIEW_BAY_STAGES . map ( ( stage ) => [
827+ stage ,
828+ rowsByStage [ stage ] . filter ( ( item ) => item . legacy_batch_path === legacyBatchPath ) ,
829+ ] ) ,
830+ ) as Record < ExactReviewBayStage , ExactReviewBayProjectionItem [ ] > ;
831+ const longestCategoryStage = Math . max (
832+ ...EXACT_REVIEW_BAY_STAGES . map ( ( stage ) => categoryRowsByStage [ stage ] . length ) ,
833+ ) ;
834+ for ( const item of priorityRows ) append ( item , legacyBatchPath ) ;
835+ for (
836+ let index = 0 ;
837+ sample . length < EXACT_REVIEW_BAY_SAMPLE_LIMIT && index < longestCategoryStage ;
838+ index += 1
839+ ) {
840+ for ( const stage of EXACT_REVIEW_BAY_STAGES ) {
841+ append ( categoryRowsByStage [ stage ] [ index ] , legacyBatchPath ) ;
842+ if ( sample . length === EXACT_REVIEW_BAY_SAMPLE_LIMIT ) break ;
843+ }
782844 }
783845 }
784846 return {
785847 complete : true as const ,
786848 sample_limit : EXACT_REVIEW_BAY_SAMPLE_LIMIT ,
787849 total : projected . size ,
788850 stages,
851+ legacy_batch_stages : legacyBatchStages ,
789852 active_overlaps : activeOverlaps ,
853+ legacy_batch_active_overlaps : legacyBatchActiveOverlaps ,
790854 items : sample ,
791855 } ;
792856}
@@ -1205,6 +1269,7 @@ export function exactReviewQueueBayProjectionFromStats(
12051269 priorityItemKeys : string [ ] = [ ] ,
12061270 batchByItemKey : ReadonlyMap < string , ExactReviewBayBatchOwner > = new Map ( ) ,
12071271 activeItemKeys : string [ ] = [ ] ,
1272+ activeLegacyItemKeys : string [ ] = [ ] ,
12081273) {
12091274 const census = stats [ EXACT_REVIEW_STATS_CENSUS ] ;
12101275 if ( ! census ) throw new Error ( "exact-review queue stats are missing their census" ) ;
@@ -1213,6 +1278,7 @@ export function exactReviewQueueBayProjectionFromStats(
12131278 priorityItemKeys ,
12141279 batchByItemKey ,
12151280 activeItemKeys ,
1281+ activeLegacyItemKeys ,
12161282 ) ;
12171283 Object . defineProperty ( stats , "bay_projection" , {
12181284 value : projection ,
0 commit comments