@@ -43,6 +43,16 @@ import { reviewContentCacheHit } from "./scheduler-policy.js";
4343import type { CreateReviewCommandWorkflowDependencies } from "./clawsweeper-review-command-dependencies.js" ;
4444import { prepareReviewCommand } from "./clawsweeper-review-preparation.js" ;
4545
46+ function reviewStartLeaseCommentUpdatedAt (
47+ comment : Record < string , unknown > | undefined ,
48+ ) : string | undefined {
49+ for ( const key of [ "updated_at" , "created_at" ] ) {
50+ const value = comment ?. [ key ] ;
51+ if ( typeof value === "string" && value . trim ( ) ) return value ;
52+ }
53+ return undefined ;
54+ }
55+
4656export function restoreVerifiedMaintainerPullRequestAuthorAssociation (
4757 item : Pick < Item , "kind" | "author" | "authorAssociation" | "labels" > ,
4858 repositoryPermission : ( author : string ) => string | null ,
@@ -178,6 +188,44 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
178188 // command creates itself must still be deleted, even for a read-only checkout.
179189 isSuppliedReviewStartLease ( suppliedReviewLease , lease ) ||
180190 deleteOwnedDedicatedReviewStartLease ( itemNumber , lease ) ;
191+ const claimSuppliedReviewLease = (
192+ itemNumber : number ,
193+ currentRevision : string ,
194+ ) :
195+ | { status : "claimed" ; lease : AcquiredReviewStartLease }
196+ | { status : "stale" }
197+ | { status : "held" ; retryAt : string } => {
198+ if ( ! suppliedReviewLease ) return { status : "stale" } ;
199+ const freshLeases = freshDedicatedReviewStartLeases ( {
200+ comments : issueReviewCommentState ( itemNumber ) . leaseComments ,
201+ itemNumber,
202+ headSha : currentRevision ,
203+ nowMs : Date . now ( ) ,
204+ } ) ;
205+ const winner = freshLeases [ 0 ] ;
206+ const supplied = freshLeases . find (
207+ ( lease ) =>
208+ commentId ( lease . comment ) === suppliedReviewLease . commentId &&
209+ lease . owner === suppliedReviewLease . owner ,
210+ ) ;
211+ if ( ! supplied || ! winner ) return { status : "stale" } ;
212+ if (
213+ commentId ( winner . comment ) !== suppliedReviewLease . commentId ||
214+ winner . owner !== suppliedReviewLease . owner
215+ ) {
216+ return { status : "held" , retryAt : winner . expiresAt } ;
217+ }
218+ return {
219+ status : "claimed" ,
220+ lease : {
221+ owner : suppliedReviewLease . owner ,
222+ commentId : suppliedReviewLease . commentId ,
223+ headSha : currentRevision ,
224+ comment : supplied . comment ,
225+ } ,
226+ } ;
227+ } ;
228+ const structuralCacheCoordinationEnabled = ! skipStartComment || suppliedReviewLease !== null ;
181229 let reviewLedger : ReviewActionLedger | null = null ;
182230 let activeReviewItem : Item | null = null ;
183231 let completed = 0 ;
@@ -361,7 +409,7 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
361409 reviewModel : PUBLIC_CODEX_MODEL ,
362410 explicitDispatch,
363411 maintainerRequest,
364- coordinationEnabled : ! skipStartComment ,
412+ coordinationEnabled : structuralCacheCoordinationEnabled ,
365413 } ) ;
366414 if ( ! structuralProbeDecision . hit ) {
367415 structuralCacheReasons . set (
@@ -393,50 +441,99 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
393441 }
394442 preHydrationStructuralRecord = structuralRecord ;
395443 if ( structuralRecord ) item . updatedAt = structuralRecord . activityUpdatedAt ;
396- const structuralDecision = reviewStructuralCacheDecision ( {
444+ const leaseRevision =
445+ item . kind === "pull_request"
446+ ? structuralRecord ?. pullHeadSha
447+ : priorReview ?. itemSourceRevision ;
448+ const structuralDecisionInput = {
397449 review : priorReview ,
398450 priorRecord : priorReview ?. structuralRecord ?? null ,
399451 currentRecord : structuralRecord ,
400452 reviewPolicy,
401453 reviewModel : PUBLIC_CODEX_MODEL ,
402454 explicitDispatch,
403455 maintainerRequest,
404- coordinationEnabled : ! skipStartComment ,
405- } ) ;
456+ coordinationEnabled : structuralCacheCoordinationEnabled ,
457+ } ;
458+ let suppliedLeaseClaim : ReturnType < typeof claimSuppliedReviewLease > | null = null ;
459+ let ownedReservationUpdatedAt : string | undefined ;
460+ let structuralDecision = reviewStructuralCacheDecision ( structuralDecisionInput ) ;
461+ if (
462+ structuralDecision . reason === "activity_changed" &&
463+ suppliedReviewLease &&
464+ leaseRevision
465+ ) {
466+ suppliedLeaseClaim = claimSuppliedReviewLease ( item . number , leaseRevision ) ;
467+ if ( suppliedLeaseClaim . status === "claimed" ) {
468+ ownedReservationUpdatedAt = reviewStartLeaseCommentUpdatedAt (
469+ suppliedLeaseClaim . lease . comment ,
470+ ) ;
471+ if ( ownedReservationUpdatedAt ) {
472+ structuralDecision = reviewStructuralCacheDecision ( {
473+ ...structuralDecisionInput ,
474+ ownedReservationUpdatedAt,
475+ } ) ;
476+ }
477+ }
478+ }
406479 structuralCacheReasons . set (
407480 structuralDecision . reason ,
408481 ( structuralCacheReasons . get ( structuralDecision . reason ) ?? 0 ) + 1 ,
409482 ) ;
410483 if ( structuralDecision . hit ) {
411484 const initialStructuralRecord = structuralRecord ;
412485 try {
413- const leaseRevision =
414- item . kind === "pull_request"
415- ? structuralRecord ?. pullHeadSha
416- : priorReview ?. itemSourceRevision ;
417- const startComment = postReviewStartStatusComment ( {
418- item ,
419- headSha : leaseRevision ?? "" ,
420- reviewTimeoutMs : timeoutMs ,
421- position : completed + 1 ,
422- total : candidates . length ,
423- shardIndex ,
424- shardCount ,
425- } ) ;
426- console . error (
427- `[review] ${ new Date ( ) . toISOString ( ) } shard= ${ shardIndex } / ${ shardCount } structural-cache-start-comment= ${ startComment . status } # ${ item . number } ` ,
428- ) ;
429- if ( startComment . status === "held" ) {
430- coordinationHeldRetryAt = startComment . retryAt ;
431- continue ;
432- }
433- acquiredReviewLease = startComment . lease ;
434- if ( ! acquiredReviewLease ) {
435- throw new Error (
436- `structural cache lease acquisition returned no identity for # ${ item . number } ` ,
486+ if ( suppliedReviewLease ) {
487+ const claim =
488+ suppliedLeaseClaim ??
489+ ( leaseRevision
490+ ? claimSuppliedReviewLease ( item . number , leaseRevision )
491+ : ( { status : "stale" } as const ) ) ;
492+ console . error (
493+ `[review] ${ new Date ( ) . toISOString ( ) } shard= ${ shardIndex } / ${ shardCount } structural-cache-start-comment= ${ claim . status === "claimed" ? "reserved" : claim . status === "held" ? "held" : "stale-reservation" } # ${ item . number } ` ,
494+ ) ;
495+ if ( claim . status === "held" ) {
496+ coordinationHeldRetryAt = claim . retryAt ;
497+ continue ;
498+ }
499+ if ( claim . status === "stale" ) {
500+ coordinationHeldRetryAt = new Date ( Date . now ( ) + 60_000 ) . toISOString ( ) ;
501+ leaseAcquisitionFailures += 1 ;
502+ leaseAcquisitionFailureDetails . push (
503+ `# ${ item . number } : reserved review lease is no longer fresh for the cached revision` ,
504+ ) ;
505+ continue ;
506+ }
507+ acquiredReviewLease = claim . lease ;
508+ ownedReservationUpdatedAt ??= reviewStartLeaseCommentUpdatedAt (
509+ claim . lease . comment ,
437510 ) ;
511+ acquiredReviewLeases . push ( { itemNumber : item . number , lease : claim . lease } ) ;
512+ } else {
513+ const startComment = postReviewStartStatusComment ( {
514+ item,
515+ headSha : leaseRevision ?? "" ,
516+ reviewTimeoutMs : timeoutMs ,
517+ position : completed + 1 ,
518+ total : candidates . length ,
519+ shardIndex,
520+ shardCount,
521+ } ) ;
522+ console . error (
523+ `[review] ${ new Date ( ) . toISOString ( ) } shard=${ shardIndex } /${ shardCount } structural-cache-start-comment=${ startComment . status } #${ item . number } ` ,
524+ ) ;
525+ if ( startComment . status === "held" ) {
526+ coordinationHeldRetryAt = startComment . retryAt ;
527+ continue ;
528+ }
529+ acquiredReviewLease = startComment . lease ;
530+ if ( ! acquiredReviewLease ) {
531+ throw new Error (
532+ `structural cache lease acquisition returned no identity for #${ item . number } ` ,
533+ ) ;
534+ }
535+ acquiredReviewLeases . push ( { itemNumber : item . number , lease : acquiredReviewLease } ) ;
438536 }
439- acquiredReviewLeases . push ( { itemNumber : item . number , lease : acquiredReviewLease } ) ;
440537 } catch ( error ) {
441538 leaseAcquisitionFailures += 1 ;
442539 leaseAcquisitionFailureDetails . push (
@@ -478,19 +575,22 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
478575 structuralCacheRevalidationMs += Date . now ( ) - structuralRevalidationStartedAt ;
479576 }
480577 const revalidationDecision = reviewStructuralCacheDecision ( {
481- review : priorReview
578+ // A lease posted by this command owns activity through now. A
579+ // supplied reservation owns activity only through its recorded timestamp.
580+ review : priorReview && ! suppliedReviewLease
482581 ? {
483582 ...priorReview ,
484583 reviewCommentSyncedAt : new Date ( ) . toISOString ( ) ,
485584 }
486- : null ,
585+ : priorReview ,
487586 priorRecord : initialStructuralRecord ,
488587 currentRecord : revalidatedStructuralRecord ,
489588 reviewPolicy,
490589 reviewModel : PUBLIC_CODEX_MODEL ,
491590 explicitDispatch,
492591 maintainerRequest,
493592 coordinationEnabled : true ,
593+ ...( ownedReservationUpdatedAt ? { ownedReservationUpdatedAt } : { } ) ,
494594 } ) ;
495595 const previousReviewIdentityMatches =
496596 expectedPreviousReviewDigest !== null &&
@@ -655,19 +755,8 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
655755 ) ;
656756 continue ;
657757 }
658- const freshLeases = freshDedicatedReviewStartLeases ( {
659- comments : issueReviewCommentState ( item . number ) . leaseComments ,
660- itemNumber : item . number ,
661- headSha : currentRevision ,
662- nowMs : Date . now ( ) ,
663- } ) ;
664- const winner = freshLeases [ 0 ] ;
665- const supplied = freshLeases . find (
666- ( lease ) =>
667- commentId ( lease . comment ) === suppliedReviewLease . commentId &&
668- lease . owner === suppliedReviewLease . owner ,
669- ) ;
670- if ( ! supplied || ! winner ) {
758+ const claim = claimSuppliedReviewLease ( item . number , currentRevision ) ;
759+ if ( claim . status === "stale" ) {
671760 coordinationHeldRetryAt = new Date ( Date . now ( ) + 60_000 ) . toISOString ( ) ;
672761 leaseAcquisitionFailures += 1 ;
673762 leaseAcquisitionFailureDetails . push (
@@ -678,24 +767,15 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
678767 ) ;
679768 continue ;
680769 }
681- if (
682- commentId ( winner . comment ) !== suppliedReviewLease . commentId ||
683- winner . owner !== suppliedReviewLease . owner
684- ) {
685- coordinationHeldRetryAt = winner . expiresAt ;
770+ if ( claim . status === "held" ) {
771+ coordinationHeldRetryAt = claim . retryAt ;
686772 console . error (
687773 `[review] ${ new Date ( ) . toISOString ( ) } shard=${ shardIndex } /${ shardCount } start-comment=held #${ item . number } ` ,
688774 ) ;
689775 continue ;
690776 }
691- const claimedLease : AcquiredReviewStartLease = {
692- owner : suppliedReviewLease . owner ,
693- commentId : suppliedReviewLease . commentId ,
694- headSha : currentRevision ,
695- comment : supplied . comment ,
696- } ;
697- acquiredReviewLease = claimedLease ;
698- acquiredReviewLeases . push ( { itemNumber : item . number , lease : claimedLease } ) ;
777+ acquiredReviewLease = claim . lease ;
778+ acquiredReviewLeases . push ( { itemNumber : item . number , lease : claim . lease } ) ;
699779 }
700780 if ( ! localRangeData && contextItemUpdatedAt && preHydrationStructuralRecord ) {
701781 structuralCacheRevalidations += 1 ;
0 commit comments