@@ -44,8 +44,8 @@ import {
4444 type ActivityRollupState ,
4545 acquireLease ,
4646 extendLease ,
47- MAINTENANCE_GAP_MS ,
4847 readRollupState ,
48+ refreshHeartbeat ,
4949 releaseLease ,
5050 resetRollup ,
5151 truncateRollupBatch ,
@@ -196,9 +196,12 @@ async function computeHour(
196196 . from ( activityRollupBlocked )
197197 . where ( eq ( activityRollupBlocked . bucketStart , hour ) )
198198 if ( block && block . retryAt . getTime ( ) > Date . now ( ) ) return { state : null , runs : 0 }
199+ /** An hour a pass ran out of budget paging: paged from the start, with no backoff or attempt. */
200+ const deferred = block ?. attempts === 0
201+ const retry = block !== undefined && ! deferred
199202 let retryBudgetMs = 0
200203 let retryInMs : number | undefined
201- if ( block ) {
204+ if ( retry ) {
202205 /**
203206 * The retry's backoff is committed before it starts, so a retry that crashes or loses its
204207 * lease is not tried again until the backoff passes.
@@ -210,11 +213,11 @@ async function computeHour(
210213 }
211214 let cells : ActivityCell [ ]
212215 try {
213- cells = await aggregateHour ( hour , block ? Date . now ( ) + retryBudgetMs : deadline , {
214- byWorkspace : Boolean ( block ) ,
216+ cells = await aggregateHour ( hour , retry ? Date . now ( ) + retryBudgetMs : deadline , {
217+ byWorkspace : block !== undefined ,
215218 } )
216219 } catch ( error ) {
217- if ( error instanceof PassDeadlineError && ! block ) {
220+ if ( error instanceof PassDeadlineError && ! retry ) {
218221 await deferPaging ( hour )
219222 return { state : null , runs : 0 }
220223 }
@@ -478,18 +481,14 @@ async function runHousekeeping(
478481}
479482
480483/**
481- * Why the rollup must start over, or null: a pass on newer code; coverage with an end missing
482- * (cleared by hand to force a rebuild, or never started); or a maintenance gap, no pass past its reset
483- * decision within {@link MAINTENANCE_GAP_MS}, during which writers stopped marking the deletes and reopened
484- * runs the cells would need. Read from the state alone, so a pass that fails before its reset
485- * commits leaves the next pass the same reason.
484+ * Why the rollup must start over regardless of its heartbeat, or null: a pass on newer code, or
485+ * coverage with an end missing (cleared by hand to force a rebuild, or never started). A maintenance
486+ * gap (see {@link refreshHeartbeat}) is the third reason. Read from the state alone, so a pass that
487+ * fails before its reset commits leaves the next pass the same reason.
486488 */
487489function resetReason ( state : ActivityRollupState ) : ActivityRollupPassResult [ 'reset' ] {
488490 if ( state . version < ACTIVITY_ROLLUP_VERSION ) return 'version'
489491 if ( ! state . coverageStart || ! state . freshThrough ) return 'coverage'
490- if ( ! state . maintainedAt || Date . now ( ) - state . maintainedAt . getTime ( ) > MAINTENANCE_GAP_MS ) {
491- return 'maintenance-gap'
492- }
493492 return null
494493}
495494
@@ -539,6 +538,19 @@ export async function runActivityRollupPass(
539538
540539 const settledThrough = new Date ( floorHour ( now ( ) ) . getTime ( ) - ACTIVITY_ROLLUP_SETTLE_MS )
541540 result . reset = resetReason ( state )
541+ if ( ! result . reset ) {
542+ /**
543+ * A pass at this version that got past its reset decision is maintaining the rollup: marks
544+ * written from now on will be consumed by it or the next. A step failing later loses no mark
545+ * (marks persist until a recompute removes them), so the heartbeat does not wait on the
546+ * steps, and a failure is counted and logged instead of forcing rebuilds every gap. The
547+ * heartbeat is renewed only if it has not expired by the database's clock, which writers
548+ * mark by; an expired one means marks may have been skipped, so the rollup is rebuilt.
549+ */
550+ const refreshed = await refreshHeartbeat ( owner )
551+ if ( refreshed ) state = refreshed
552+ else result . reset = 'maintenance-gap'
553+ }
542554 if ( result . reset ) {
543555 if ( state . coverageStart || result . reset !== 'coverage' ) {
544556 logger . warn ( 'Activity rollup starting over' , { reason : result . reset } )
@@ -548,14 +560,6 @@ export async function runActivityRollupPass(
548560 if ( Date . now ( ) >= deadline ) return { ...result , status : 'truncating' }
549561 }
550562 state = await startCoverage ( owner , settledThrough , lateEndMarginMs )
551- } else {
552- /**
553- * A pass at this version that got past its reset decision is maintaining the rollup: marks
554- * written from now on will be consumed by it or the next. A step failing later loses no mark
555- * (marks persist until a recompute removes them), so the heartbeat does not wait on the
556- * steps, and a failure is counted and logged instead of forcing rebuilds every gap.
557- */
558- state = await updateLeasedState ( owner , { maintainedAt : sql `now()` } )
559563 }
560564
561565 result . lateEndMarks = await captureLateEnds ( deadline , {
0 commit comments