@@ -80,7 +80,12 @@ async function selectCandidateFileIds(
8080 * Superseded versions of the given files past retention: older than the cutoff or beyond the plan's
8181 * count, but never among the newest {@link KEEP_SUPERSEDED} superseded versions of a file.
8282 */
83- function selectExpiredVersions ( fileIds : string [ ] , cutoff : Date , maxSuperseded : number ) {
83+ function selectExpiredVersions (
84+ fileIds : string [ ] ,
85+ cutoff : Date ,
86+ maxSuperseded : number ,
87+ batchSize : number
88+ ) {
8489 const ranked = cleanupDb
8590 . select ( {
8691 id : workspaceFileVersion . id ,
@@ -107,7 +112,7 @@ function selectExpiredVersions(fileIds: string[], cutoff: Date, maxSuperseded: n
107112 or ( lt ( ranked . supersededAt , cutoff ) , gt ( ranked . rank , maxSuperseded ) )
108113 )
109114 )
110- . limit ( DEFAULT_DELETE_CHUNK_SIZE )
115+ . limit ( batchSize )
111116}
112117
113118/**
@@ -166,11 +171,12 @@ export async function runCleanupFileVersions(payload: CleanupJobPayload): Promis
166171 attempted < MAX_VERSIONS_PER_RUN
167172 ) {
168173 batches ++
169- const expired = await selectExpiredVersions ( fileIds , cutoff , maxSuperseded )
174+ const batchSize = Math . min ( DEFAULT_DELETE_CHUNK_SIZE , MAX_VERSIONS_PER_RUN - attempted )
175+ const expired = await selectExpiredVersions ( fileIds , cutoff , maxSuperseded , batchSize )
170176 attempted += expired . length
171177 const removed = expired . length > 0 ? await deleteVersions ( expired ) : 0
172178 deleted += removed
173- exhausted = expired . length < DEFAULT_DELETE_CHUNK_SIZE || removed === 0
179+ exhausted = expired . length < batchSize || removed === 0
174180 }
175181 if ( ! exhausted ) break
176182 }
0 commit comments