@@ -331,6 +331,8 @@ describe("WaitpointSystem create/block write routing", () => {
331331 store : wired ? destination : undefined ,
332332 isStoreEnabled : isEnabled ,
333333 mintPolicy : { resolveMintMode : ( ) => "redis" , isCapabilityReady : ( ) => true } ,
334+ withLegacyRegistrationLock : ( runId , operation ) =>
335+ resources . runLock . lock ( "routing-test" , [ runId ] , operation ) ,
334336 } ) ;
335337 const system = new WaitpointSystem ( {
336338 ...engine . waitpointSystem [ "options" ] ,
@@ -342,8 +344,9 @@ describe("WaitpointSystem create/block write routing", () => {
342344 }
343345 : undefined ,
344346 } ) ;
345- const expectOneFlagCheck = ( before : number ) => {
346- expect ( checkedOrganizations . slice ( before ) ) . toEqual ( wired ? [ organizationId ] : [ ] ) ;
347+ const expectFlagChecks = ( before : number , enabledCount = 1 ) => {
348+ const count = ! wired ? 0 : mode === "enabled" ? enabledCount : 1 ;
349+ expect ( checkedOrganizations . slice ( before ) ) . toEqual ( Array ( count ) . fill ( organizationId ) ) ;
347350 } ;
348351 for ( const native of [ false , true ] ) {
349352 const id = native ? generateWaitpointId ( "DATETIME" ) : WaitpointId . generate ( ) . id ;
@@ -372,23 +375,20 @@ describe("WaitpointSystem create/block write routing", () => {
372375 let beforeFlagChecks = checkedOrganizations . length ;
373376 if ( mode === "enabled" && native ) {
374377 await expect ( router . registerBlocks ( params ) ) . rejects . toThrow ( "three-step barrier" ) ;
375- expectOneFlagCheck ( beforeFlagChecks ) ;
378+ expectFlagChecks ( beforeFlagChecks ) ;
376379 beforeFlagChecks = checkedOrganizations . length ;
377380 await expect ( router . registerBlocksLockless ( params ) ) . rejects . toThrow (
378381 "three-step barrier"
379382 ) ;
380- expectOneFlagCheck ( beforeFlagChecks ) ;
383+ expectFlagChecks ( beforeFlagChecks ) ;
381384 expect ( legacyStore . calls ) . toEqual ( [ ] ) ;
382385 } else {
383- expect ( await router . registerBlocks ( params ) ) . toEqual (
384- mode === "enabled"
385- ? { pendingCount : 1 , storeRoutingEnabled : true }
386- : { pendingCount : 1 }
387- ) ;
388- expectOneFlagCheck ( beforeFlagChecks ) ;
386+ expect ( await router . registerBlocks ( params ) ) . toEqual ( { pendingCount : 1 } ) ;
387+ // The cleanup fence has its own independent gate after registration's routing gate.
388+ expectFlagChecks ( beforeFlagChecks , 2 ) ;
389389 beforeFlagChecks = checkedOrganizations . length ;
390390 await router . registerBlocksLockless ( params ) ;
391- expectOneFlagCheck ( beforeFlagChecks ) ;
391+ expectFlagChecks ( beforeFlagChecks , 3 ) ;
392392 // Direct delegation preserves the original collection, including duplicates.
393393 if ( mode !== "enabled" ) expect ( legacyStore . blockInputs . at ( - 1 ) ) . toBe ( ids ) ;
394394 expect ( legacyStore . calls ) . toEqual ( [
@@ -404,7 +404,8 @@ describe("WaitpointSystem create/block write routing", () => {
404404 projectId : env . projectId ,
405405 organizationId,
406406 } ) ;
407- expectOneFlagCheck ( beforeFlagChecks ) ;
407+ // System admission, absorption, and cleanup-fence gates remain separate stages.
408+ expectFlagChecks ( beforeFlagChecks , 3 ) ;
408409 expect ( destinationStore . calls ) . toEqual ( [ ] ) ;
409410 }
410411 // Ensure a fan-out edge even when enabled registration correctly rejected this parent.
@@ -432,16 +433,16 @@ describe("WaitpointSystem create/block write routing", () => {
432433 if ( ! nativeDispatch ) {
433434 // Complete-before-register uses the same gated payload on the immediate unblock path.
434435 beforeFlagChecks = checkedOrganizations . length ;
435- // A flag change after the check affects the next operation, not this registration's
436- // route or derived continuation payload. There is no decision cache across operations .
436+ // A flag change after system admission is rechecked by absorption. The system still
437+ // carries its admitted org onto the continuation; the next operation evaluates afresh .
437438 disableAfterNextCheck = mode === "enabled" ;
438439 await system . blockRunWithWaitpoint ( {
439440 runId : run . id ,
440441 waitpoints : ids ,
441442 projectId : env . projectId ,
442443 organizationId,
443444 } ) ;
444- expectOneFlagCheck ( beforeFlagChecks ) ;
445+ expectFlagChecks ( beforeFlagChecks , 2 ) ;
445446 const immediate = await resources . worker . getJob ( `continueRunIfUnblocked:${ run . id } ` ) ;
446447 expect ( immediate ?. item ) . toEqual (
447448 mode === "enabled" ? { runId : run . id , organizationId } : { runId : run . id }
@@ -450,7 +451,7 @@ describe("WaitpointSystem create/block write routing", () => {
450451 if ( mode === "enabled" ) {
451452 beforeFlagChecks = checkedOrganizations . length ;
452453 await system . completeWaitpoint ( { id, organizationId } ) ;
453- expectOneFlagCheck ( beforeFlagChecks ) ;
454+ expectFlagChecks ( beforeFlagChecks ) ;
454455 const disabledJob = await resources . worker . getJob (
455456 `continueRunIfUnblocked:${ run . id } `
456457 ) ;
@@ -463,7 +464,7 @@ describe("WaitpointSystem create/block write routing", () => {
463464 beforeFlagChecks = checkedOrganizations . length ;
464465 const direct = await router . complete ( { waitpointId : id , organizationId } ) ;
465466 expect ( direct . waitpoint . status ) . toBe ( "COMPLETED" ) ;
466- expectOneFlagCheck ( beforeFlagChecks ) ;
467+ expectFlagChecks ( beforeFlagChecks ) ;
467468 const missing = generateWaitpointId ( "DATETIME" ) ;
468469 legacyStore . calls . length = 0 ;
469470 destinationStore . calls . length = 0 ;
@@ -489,7 +490,7 @@ describe("WaitpointSystem create/block write routing", () => {
489490 completedAfter : new Date ( ) ,
490491 mintContext : { organizationId, parentResidency : "redis-primary" } ,
491492 } ) ;
492- expectOneFlagCheck ( beforeFlagChecks ) ;
493+ expectFlagChecks ( beforeFlagChecks ) ;
493494 expect ( mode === "enabled" ? legacyStore . calls : destinationStore . calls ) . toEqual ( [ ] ) ;
494495 expect ( mode === "enabled" ? destinationStore . calls : legacyStore . calls ) . toEqual ( [
495496 "upsertWaitpoint" ,
0 commit comments