@@ -462,6 +462,14 @@ const EXACT_REVIEW_QUEUE_ROLLBACK_CLOCK_SKEW_MS = 5 * 60 * 1000;
462462const EXACT_REVIEW_QUEUE_LEGACY_GENERATION_PREFIX = "__clawsweeper_sql_generation:" ;
463463const EXACT_REVIEW_QUEUE_STATE_KEY = "exact-review-queue" ;
464464const EXACT_REVIEW_SOURCE_AUTHORITY_SEQUENCE_KEY = "exact-review-source-authority-sequence:v1" ;
465+ const TARGET_FANOUT_CURSOR_KEY_PREFIX = "target-fanout-cursor:v1:" ;
466+ type TargetFanoutMode = "hot-intake" | "normal-review" | "audit" ;
467+ type TargetFanoutCursor = {
468+ mode : TargetFanoutMode ;
469+ nextCursor : number ;
470+ revision : number ;
471+ updatedAt : number ;
472+ } ;
465473const EXACT_REVIEW_SOURCE_AUTHORITY_RESERVATION_PREFIX =
466474 "exact-review-source-authority-reservation:v1:" ;
467475const EXACT_REVIEW_SOURCE_AUTHORITY_RETRY_LIMIT = 16 ;
@@ -593,6 +601,13 @@ export class ExactReviewQueue {
593601 this . reviewCoverageCache = null ;
594602 return json ( { ok : true , accepted : true } , 202 ) ;
595603 }
604+ const targetFanoutMode = targetFanoutModeFromPath ( url . pathname ) ;
605+ if ( targetFanoutMode && request . method === "GET" ) {
606+ return this . readTargetFanoutCursor ( targetFanoutMode ) ;
607+ }
608+ if ( targetFanoutMode && request . method === "PUT" ) {
609+ return this . writeTargetFanoutCursor ( targetFanoutMode , await request . json ( ) . catch ( ( ) => null ) ) ;
610+ }
596611 if ( request . method === "POST" && url . pathname === "/source-authority" ) {
597612 const body = objectValue ( await request . json ( ) . catch ( ( ) => null ) ) ;
598613 const deliveryId = String ( body . delivery_id || "" ) . trim ( ) ;
@@ -2941,6 +2956,64 @@ export class ExactReviewQueue {
29412956 return new Response ( "not found" , { status : 404 } ) ;
29422957 }
29432958
2959+ private readTargetFanoutCursor ( mode : TargetFanoutMode ) {
2960+ const stored = readTargetFanoutCursor ( this . storage . kv . get ( targetFanoutCursorKey ( mode ) ) , mode ) ;
2961+ if ( stored === "invalid" ) return json ( { error : "fanout_cursor_store_invalid" } , 500 ) ;
2962+ return json ( targetFanoutCursorJson ( stored ?? emptyTargetFanoutCursor ( mode ) ) ) ;
2963+ }
2964+
2965+ private writeTargetFanoutCursor ( mode : TargetFanoutMode , value : unknown ) {
2966+ const body = objectValue ( value ) ;
2967+ const nextCursor = Number ( body . next_cursor ) ;
2968+ const expectedRevision = Number ( body . expected_revision ) ;
2969+ if (
2970+ ! Number . isSafeInteger ( nextCursor ) ||
2971+ nextCursor < 0 ||
2972+ ! Number . isSafeInteger ( expectedRevision ) ||
2973+ expectedRevision < 0
2974+ ) {
2975+ return json ( { error : "invalid_fanout_cursor" } , 400 ) ;
2976+ }
2977+ try {
2978+ const result = this . storage . transactionSync ( ( ) => {
2979+ const stored = readTargetFanoutCursor (
2980+ this . storage . kv . get ( targetFanoutCursorKey ( mode ) ) ,
2981+ mode ,
2982+ ) ;
2983+ if ( stored === "invalid" ) return { kind : "invalid" as const } ;
2984+ const current = stored ?? emptyTargetFanoutCursor ( mode ) ;
2985+ if ( current . revision !== expectedRevision ) {
2986+ return { kind : "conflict" as const , current } ;
2987+ }
2988+ if ( current . revision >= Number . MAX_SAFE_INTEGER ) {
2989+ return { kind : "invalid" as const } ;
2990+ }
2991+ const next : TargetFanoutCursor = {
2992+ mode,
2993+ nextCursor,
2994+ revision : current . revision + 1 ,
2995+ updatedAt : Date . now ( ) ,
2996+ } ;
2997+ this . storage . kv . put ( targetFanoutCursorKey ( mode ) , next ) ;
2998+ return { kind : "written" as const , cursor : next } ;
2999+ } ) ;
3000+ if ( result . kind === "invalid" ) return json ( { error : "fanout_cursor_store_invalid" } , 500 ) ;
3001+ if ( result . kind === "conflict" ) {
3002+ return json (
3003+ {
3004+ error : "fanout_cursor_revision_conflict" ,
3005+ current : targetFanoutCursorJson ( result . current ) ,
3006+ } ,
3007+ 409 ,
3008+ ) ;
3009+ }
3010+ return json ( targetFanoutCursorJson ( result . cursor ) , 202 ) ;
3011+ } catch ( error ) {
3012+ console . error ( `fanout cursor write failed: ${ sanitizedServerError ( error ) } ` ) ;
3013+ return json ( { error : "fanout_cursor_store_unavailable" } , 503 ) ;
3014+ }
3015+ }
3016+
29443017 async alarm ( ) {
29453018 await this . ready ;
29463019 this . cleanupLegacyCompatibilitySync ( ) ;
@@ -11989,6 +12062,52 @@ function objectValue(value) {
1198912062 return value && typeof value === "object" && ! Array . isArray ( value ) ? value : { } ;
1199012063}
1199112064
12065+ function targetFanoutModeFromPath ( path : string ) : TargetFanoutMode | null {
12066+ const match = / ^ \/ c u r s o r s \/ ( h o t - i n t a k e | n o r m a l - r e v i e w | a u d i t ) $ / . exec ( path ) ;
12067+ return ( match ?. [ 1 ] as TargetFanoutMode | undefined ) ?? null ;
12068+ }
12069+
12070+ function targetFanoutCursorKey ( mode : TargetFanoutMode ) : string {
12071+ return `${ TARGET_FANOUT_CURSOR_KEY_PREFIX } ${ mode } ` ;
12072+ }
12073+
12074+ function emptyTargetFanoutCursor ( mode : TargetFanoutMode ) : TargetFanoutCursor {
12075+ return { mode, nextCursor : 0 , revision : 0 , updatedAt : 0 } ;
12076+ }
12077+
12078+ function readTargetFanoutCursor (
12079+ value : unknown ,
12080+ mode : TargetFanoutMode ,
12081+ ) : TargetFanoutCursor | "invalid" | null {
12082+ if ( value === undefined ) return null ;
12083+ const cursor = objectValue ( value ) ;
12084+ const nextCursor = Number ( cursor . nextCursor ) ;
12085+ const revision = Number ( cursor . revision ) ;
12086+ const updatedAt = Number ( cursor . updatedAt ) ;
12087+ if (
12088+ cursor . mode !== mode ||
12089+ ! Number . isSafeInteger ( nextCursor ) ||
12090+ nextCursor < 0 ||
12091+ ! Number . isSafeInteger ( revision ) ||
12092+ revision < 1 ||
12093+ ! Number . isSafeInteger ( updatedAt ) ||
12094+ updatedAt < 1
12095+ ) {
12096+ return "invalid" ;
12097+ }
12098+ return { mode, nextCursor, revision, updatedAt } ;
12099+ }
12100+
12101+ function targetFanoutCursorJson ( cursor : TargetFanoutCursor ) {
12102+ return {
12103+ ok : true ,
12104+ mode : cursor . mode ,
12105+ next_cursor : cursor . nextCursor ,
12106+ revision : cursor . revision ,
12107+ updated_at : cursor . updatedAt > 0 ? new Date ( cursor . updatedAt ) . toISOString ( ) : null ,
12108+ } ;
12109+ }
12110+
1199212111function repoName ( repo ) {
1199312112 return String ( repo || "" ) . split ( "/" ) [ 1 ] || "" ;
1199412113}
0 commit comments