@@ -18,10 +18,13 @@ const ACTIVE_RECOVERY_REASONS = new Set(["fresh_review_already_active", "publica
1818const IDEMPOTENCY_KEY = / ^ [ A - Z a - z 0 - 9 : . _ - ] { 1 , 200 } $ / ;
1919
2020class DeadLetterInventoryChangedError extends Error {
21- constructor ( summary ) {
21+ constructor ( summary , rowIds , targetKeys , blockedGroups ) {
2222 super ( "dead-letter cleanup changed during reconciliation; refusing stale recovery" ) ;
2323 this . name = "DeadLetterInventoryChangedError" ;
2424 this . summary = summary ;
25+ this . rowIds = [ ...new Set ( rowIds ) ] ;
26+ this . targetKeys = [ ...new Set ( targetKeys . filter ( Boolean ) . map ( normalizeRecoveryTargetKey ) ) ] ;
27+ this . blockedGroups = blockedGroups ?? [ { rowIds : this . rowIds , targetKeys : this . targetKeys } ] ;
2528 }
2629}
2730
@@ -69,7 +72,14 @@ async function main(argv) {
6972 }
7073
7174 if ( args . action === "reconcile" ) {
72- const progress = { summary : null } ;
75+ const progress = {
76+ summary : null ,
77+ blockedRows : new Set ( ) ,
78+ blockedTargets : new Set ( ) ,
79+ countedSkippedTargets : new Set ( ) ,
80+ inspectedTargetIds : new Set ( ) ,
81+ pendingRecoveryTargetIds : new Set ( ) ,
82+ } ;
7383 for ( let refreshes = 0 ; refreshes <= MAX_RECONCILE_INVENTORY_REFRESHES ; refreshes += 1 ) {
7484 try {
7585 await reconcileDeadLetters ( { inventory, queueUrl, secret, args, progress } ) ;
@@ -78,12 +88,17 @@ async function main(argv) {
7888 if ( ! ( error instanceof DeadLetterInventoryChangedError ) ) throw error ;
7989 // Guarded resolution is one Worker transaction: an inventory race skips
8090 // every requested row. Refuse recovery if that safety contract changes.
81- if ( error . summary . resolved !== 0 || error . summary . unparked !== 0 ) {
91+ if (
92+ error . summary . resolved !== 0 ||
93+ error . summary . unparked !== 0 ||
94+ error . summary . skipped !== error . rowIds . length
95+ ) {
8296 throw new Error ( "guarded dead-letter cleanup was not atomic; refusing stale recovery" ) ;
8397 }
8498 if (
8599 refreshes === MAX_RECONCILE_INVENTORY_REFRESHES ||
86- progress . summary . inspected_targets >= args . maxTargets
100+ ( progress . summary . inspected_targets >= args . maxTargets &&
101+ progress . pendingRecoveryTargetIds . size === 0 )
87102 ) {
88103 // Never recover against stale aliases if producers keep changing the
89104 // inventory faster than this bounded operator can inspect it. Keep
@@ -100,6 +115,24 @@ async function main(argv) {
100115 secret,
101116 maxPages : MAX_RECONCILE_INVENTORY_PAGES ,
102117 } ) ;
118+ const openRowIds = new Set ( inventory . dead_letters . map ( ( row ) => row . dead_letter_id ) ) ;
119+ for ( const blocked of error . blockedGroups ) {
120+ const unchangedRowIds = blocked . rowIds . filter ( ( id ) => openRowIds . has ( id ) ) ;
121+ for ( const id of unchangedRowIds ) progress . blockedRows . add ( id ) ;
122+ if ( unchangedRowIds . length ) {
123+ if (
124+ blocked . targetKeys . every ( ( target ) => ! / ^ ( [ ^ / ] + ) \/ ( [ ^ # ] + ) # ( [ 1 - 9 ] \d * ) $ / . test ( target ) )
125+ ) {
126+ for ( const row of inventory . dead_letters ) {
127+ const target = row . fresh_recovery . item_key ;
128+ if ( ! target || ! / ^ ( [ ^ / ] + ) \/ ( [ ^ # ] + ) # ( [ 1 - 9 ] \d * ) $ / . test ( target ) ) {
129+ progress . blockedRows . add ( row . dead_letter_id ) ;
130+ }
131+ }
132+ }
133+ for ( const target of blocked . targetKeys ) progress . blockedTargets . add ( target ) ;
134+ }
135+ }
103136 await writeFile ( args . output , `${ JSON . stringify ( inventory , null , 2 ) } \n` , "utf8" ) ;
104137 }
105138 }
@@ -253,26 +286,71 @@ async function reconcileDeadLetters({ inventory, queueUrl, secret, args, progres
253286 } ) ;
254287 summary . inventory_complete = inventory . complete ;
255288 summary . queue_pressure = initialPressure . status ;
289+ progress . pendingRecoveryTargetIds . clear ( ) ;
256290 const groups = new Map ( ) ;
257291 const invalidRows = [ ] ;
258292 for ( const row of inventory . dead_letters ) {
259293 const target = row . fresh_recovery . item_key ;
260294 if ( ! target || ! / ^ ( [ ^ / ] + ) \/ ( [ ^ # ] + ) # ( [ 1 - 9 ] \d * ) $ / . test ( target ) ) {
261- invalidRows . push ( row ) ;
295+ if ( ! progress . blockedRows . has ( row . dead_letter_id ) ) invalidRows . push ( row ) ;
262296 continue ;
263297 }
264298 const key = normalizeRecoveryTargetKey ( target ) ;
265299 const group = groups . get ( key ) ?? { target, rows : [ ] } ;
266300 group . rows . push ( row ) ;
267301 groups . set ( key , group ) ;
268302 }
303+ const blockedResolutions = [ ] ;
304+ const resolveForReconciliation = async ( options ) => {
305+ try {
306+ return await reconcileResolve ( options ) ;
307+ } catch ( error ) {
308+ if (
309+ ! ( error instanceof DeadLetterInventoryChangedError ) ||
310+ error . summary . resolved !== 0 ||
311+ error . summary . unparked !== 0 ||
312+ error . summary . skipped !== error . rowIds . length
313+ ) {
314+ throw error ;
315+ }
316+ blockedResolutions . push ( error ) ;
317+ return { ...error . summary , blocked : true } ;
318+ }
319+ } ;
320+ const refreshBlockedInventory = ( ) => {
321+ if ( ! blockedResolutions . length ) return ;
322+ throw new DeadLetterInventoryChangedError (
323+ {
324+ resolved : 0 ,
325+ skipped : blockedResolutions . reduce ( ( total , error ) => total + error . summary . skipped , 0 ) ,
326+ unparked : 0 ,
327+ } ,
328+ blockedResolutions . flatMap ( ( error ) => error . rowIds ) ,
329+ blockedResolutions . flatMap ( ( error ) => error . targetKeys ) ,
330+ blockedResolutions . flatMap ( ( error ) => error . blockedGroups ) ,
331+ ) ;
332+ } ;
333+ const accountSkippedTarget = ( nodeId ) => {
334+ if ( progress . countedSkippedTargets . has ( nodeId ) ) return ;
335+ progress . countedSkippedTargets . add ( nodeId ) ;
336+ summary . skipped_targets += 1 ;
337+ } ;
338+ const canInspectTarget = ( nodeId ) =>
339+ progress . inspectedTargetIds . has ( nodeId ) || summary . inspected_targets < args . maxTargets ;
340+ const reserveTargetInspection = ( nodeId ) => {
341+ if ( progress . inspectedTargetIds . has ( nodeId ) ) return true ;
342+ if ( summary . inspected_targets >= args . maxTargets ) return false ;
343+ progress . inspectedTargetIds . add ( nodeId ) ;
344+ summary . inspected_targets += 1 ;
345+ return true ;
346+ } ;
269347
270348 // A partial page window cannot prove that a transferred alias or an active
271349 // sibling was observed. Invalid rows are independently terminal and safe to
272350 // drain; every GitHub-targeted mutation waits for a complete inventory.
273351 if ( ! inventory . complete ) {
274352 if ( invalidRows . length ) {
275- const resolution = await reconcileResolve ( {
353+ const resolution = await resolveForReconciliation ( {
276354 queueUrl,
277355 secret,
278356 rows : invalidRows . slice ( 0 , MAX_RESOLUTION_IDS ) ,
@@ -283,6 +361,7 @@ async function reconcileDeadLetters({ inventory, queueUrl, secret, args, progres
283361 summary . resolved_rows += resolution . resolved ;
284362 summary . invalid_rows += resolution . resolved ;
285363 }
364+ refreshBlockedInventory ( ) ;
286365 summary . skipped_targets += groups . size ;
287366 printResult ( summary ) ;
288367 return ;
@@ -293,7 +372,7 @@ async function reconcileDeadLetters({ inventory, queueUrl, secret, args, progres
293372 identities = await inspectCanonicalTargets ( [ ...groups . values ( ) ] , args . maxTargets ) ;
294373 } catch {
295374 if ( invalidRows . length ) {
296- const resolution = await reconcileResolve ( {
375+ const resolution = await resolveForReconciliation ( {
297376 queueUrl,
298377 secret,
299378 rows : invalidRows . slice ( 0 , MAX_RESOLUTION_IDS ) ,
@@ -304,6 +383,7 @@ async function reconcileDeadLetters({ inventory, queueUrl, secret, args, progres
304383 summary . resolved_rows += resolution . resolved ;
305384 summary . invalid_rows += resolution . resolved ;
306385 }
386+ refreshBlockedInventory ( ) ;
307387 summary . skipped_targets += groups . size ;
308388 printResult ( summary ) ;
309389 return ;
@@ -342,31 +422,35 @@ async function reconcileDeadLetters({ inventory, queueUrl, secret, args, progres
342422 ...( live . canonical_target ? [ normalizeRecoveryTargetKey ( live . canonical_target ) ] : [ ] ) ,
343423 ] ) ,
344424 ] ;
425+ if ( groupAliases . some ( ( alias ) => progress . blockedTargets . has ( alias ) ) ) {
426+ accountSkippedTarget ( live . node_id ) ;
427+ continue ;
428+ }
345429 if (
346430 hasActiveWork ||
347431 ( live . state === "open" && ! rows . some ( ( row ) => row . fresh_recovery . eligible ) )
348432 ) {
349- summary . skipped_targets += 1 ;
433+ accountSkippedTarget ( live . node_id ) ;
350434 continue ;
351435 }
352436 if ( live . state === "closed" ) {
353- if ( summary . inspected_targets >= args . maxTargets ) {
354- summary . skipped_targets += 1 ;
437+ if ( ! canInspectTarget ( live . node_id ) ) {
438+ accountSkippedTarget ( live . node_id ) ;
355439 continue ;
356440 }
357441 let current ;
358442 try {
359443 current = await inspectRecoveryTarget ( canonicalTarget ) ;
360444 } catch {
361- summary . skipped_targets += 1 ;
445+ accountSkippedTarget ( live . node_id ) ;
362446 continue ;
363447 }
364448 if ( current . state !== "closed" || current . node_id !== live . node_id ) {
365- summary . skipped_targets += 1 ;
449+ accountSkippedTarget ( live . node_id ) ;
366450 continue ;
367451 }
368- summary . inspected_targets += 1 ;
369- const resolution = await reconcileResolve ( {
452+ reserveTargetInspection ( live . node_id ) ;
453+ const resolution = await resolveForReconciliation ( {
370454 queueUrl,
371455 secret,
372456 rows : rows . slice ( 0 , MAX_RESOLUTION_IDS ) ,
@@ -376,6 +460,9 @@ async function reconcileDeadLetters({ inventory, queueUrl, secret, args, progres
376460 canonicalTarget : current . canonical_target ,
377461 aliases : groupAliases ,
378462 } ) ;
463+ if ( resolution . blocked ) {
464+ continue ;
465+ }
379466 summary . resolved_rows += resolution . resolved ;
380467 summary . closed_rows += resolution . resolved ;
381468 if ( resolution . unparked ) {
@@ -391,24 +478,23 @@ async function reconcileDeadLetters({ inventory, queueUrl, secret, args, progres
391478 row . fresh_recovery . source_head_sha === live . head_sha ) ,
392479 ) ;
393480 if ( ! primary || summary . recovered_targets + recoveries . length >= args . maxRecoveries ) {
394- summary . skipped_targets += 1 ;
481+ accountSkippedTarget ( live . node_id ) ;
395482 continue ;
396483 }
397484 const pressure = await readQueuePressure ( queueUrl ) ;
398485 summary . queue_pressure = pressure . status ;
399486 if ( pressure . status !== "idle" || pressure . availableSlots <= recoveries . length ) {
400- summary . skipped_targets += 1 ;
487+ accountSkippedTarget ( live . node_id ) ;
401488 continue ;
402489 }
403- if ( summary . inspected_targets >= args . maxTargets ) {
404- summary . skipped_targets += 1 ;
490+ if ( ! reserveTargetInspection ( live . node_id ) ) {
491+ accountSkippedTarget ( live . node_id ) ;
405492 continue ;
406493 }
407- summary . inspected_targets += 1 ;
408494 const duplicates = rows . filter ( ( row ) => row . dead_letter_id !== primary . dead_letter_id ) ;
409495 if ( duplicates . length ) {
410496 const selectedDuplicates = duplicates . slice ( 0 , MAX_RESOLUTION_IDS ) ;
411- const resolution = await reconcileResolve ( {
497+ const resolution = await resolveForReconciliation ( {
412498 queueUrl,
413499 secret,
414500 rows : selectedDuplicates ,
@@ -418,14 +504,17 @@ async function reconcileDeadLetters({ inventory, queueUrl, secret, args, progres
418504 canonicalTarget : live . canonical_target ,
419505 aliases : groupAliases ,
420506 } ) ;
507+ if ( resolution . blocked ) {
508+ continue ;
509+ }
421510 summary . resolved_rows += resolution . resolved ;
422511 summary . duplicate_rows += resolution . resolved ;
423512 if (
424513 resolution . unparked ||
425514 resolution . resolved !== selectedDuplicates . length ||
426515 duplicates . length > MAX_RESOLUTION_IDS
427516 ) {
428- summary . skipped_targets += 1 ;
517+ accountSkippedTarget ( live . node_id ) ;
429518 if ( resolution . unparked ) {
430519 printResult ( summary ) ;
431520 return ;
@@ -439,10 +528,11 @@ async function reconcileDeadLetters({ inventory, queueUrl, secret, args, progres
439528 live,
440529 aliases : groupAliases ,
441530 } ) ;
531+ progress . pendingRecoveryTargetIds . add ( live . node_id ) ;
442532 }
443533
444534 if ( invalidRows . length ) {
445- const resolution = await reconcileResolve ( {
535+ const resolution = await resolveForReconciliation ( {
446536 queueUrl,
447537 secret,
448538 rows : invalidRows . slice ( 0 , MAX_RESOLUTION_IDS ) ,
@@ -458,6 +548,7 @@ async function reconcileDeadLetters({ inventory, queueUrl, secret, args, progres
458548 }
459549 }
460550
551+ refreshBlockedInventory ( ) ;
461552 if ( recoveries . length ) {
462553 for ( const recovery of recoveries ) {
463554 let current ;
@@ -601,7 +692,15 @@ async function reconcileResolve({
601692 } ) ;
602693 const summary = mutationSummary ( "resolve" , result ) ;
603694 if ( summary . resolved !== rows . length || summary . skipped !== 0 ) {
604- throw new DeadLetterInventoryChangedError ( summary ) ;
695+ throw new DeadLetterInventoryChangedError (
696+ summary ,
697+ rows . map ( ( row ) => row . dead_letter_id ) ,
698+ [
699+ ...rows . map ( ( row ) => row . fresh_recovery . item_key ) ,
700+ ...( canonicalTarget ? [ canonicalTarget ] : [ ] ) ,
701+ ...aliases ,
702+ ] ,
703+ ) ;
605704 }
606705 for ( const row of rows ) openIds ?. delete ( row . dead_letter_id ) ;
607706 return summary ;
0 commit comments