11const fs = require ( "node:fs" ) ;
22
3+ const DAY_MS = 24 * 60 * 60 * 1000 ;
34const OPEN_LIMIT = 8 ;
4- const CONFLICT_WINDOW_MS = 7 * 24 * 60 * 60 * 1000 ;
5+ const CONFLICT_WINDOW_MS = 7 * DAY_MS ;
56const CLOSING_WINDOW_MS = 72 * 60 * 60 * 1000 ;
67// A first run meets the whole backlog; cap it so one run cannot post hundreds of comments.
78const MAX_WARNINGS_PER_RUN = 100 ;
@@ -10,6 +11,7 @@ const MAX_CLOSES_PER_RUN = 50;
1011const MAX_LOOKUPS_PER_RUN = 300 ;
1112const MERGEABLE_ATTEMPTS = 3 ;
1213const MERGEABLE_RETRY_MS = 30_000 ;
14+ const MERGEABLE_LOOKUP_RETRY_MS = 5_000 ;
1315
1416const REBASE_LABEL = "needs-rebase" ;
1517const CLOSING_LABEL = "triage:closing" ;
@@ -151,7 +153,12 @@ async function sweep({ github, context, core, dryRun }) {
151153 }
152154 if ( ! openPrs . some ( ( pr ) => pr . mergeable === "UNKNOWN" && readsMergeable ( pr ) ) ) break ;
153155 }
154- const candidates = openPrs . filter ( ( pr ) => sweepStep ( pr ) !== "skip" ) ;
156+ // Start each day further down the list so the lookup cap cannot starve the same PRs every run.
157+ const due = openPrs . filter ( ( pr ) => sweepStep ( pr ) !== "skip" ) ;
158+ const start = due . length
159+ ? ( Math . floor ( Date . now ( ) / DAY_MS ) * MAX_LOOKUPS_PER_RUN ) % due . length
160+ : 0 ;
161+ const candidates = [ ...due . slice ( start ) , ...due . slice ( 0 , start ) ] ;
155162
156163 const plan = [ ] ;
157164 let warnings = 0 ;
@@ -174,12 +181,17 @@ async function sweep({ github, context, core, dryRun }) {
174181 core . warning ( `Stopped at ${ MAX_LOOKUPS_PER_RUN } lookups; the next run continues.` ) ;
175182 break ;
176183 }
177- lookups += 1 ;
178- // The list can be minutes old by the time we act, so decide again on fresh state. A merge to
179- // main in between resets mergeability; it cannot resolve a conflict, so keep the listed answer.
180- const detail = ( await github . graphql ( PR_DETAIL_QUERY , { ...repo , number } ) ) . repository
181- . pullRequest ;
182- if ( detail . mergeable === "UNKNOWN" ) detail . mergeable = candidate . mergeable ;
184+ // The list can be minutes old by the time we act, so decide again on fresh state. A push or a
185+ // merge to main in between resets mergeability: ask once more, and never act on the old answer.
186+ const lookup = async ( ) => {
187+ lookups += 1 ;
188+ return ( await github . graphql ( PR_DETAIL_QUERY , { ...repo , number } ) ) . repository . pullRequest ;
189+ } ;
190+ let detail = await lookup ( ) ;
191+ if ( detail . state === "OPEN" && detail . mergeable === "UNKNOWN" && readsMergeable ( detail ) ) {
192+ await new Promise ( ( resolve ) => setTimeout ( resolve , MERGEABLE_LOOKUP_RETRY_MS ) ) ;
193+ detail = await lookup ( ) ;
194+ }
183195 const step = detail . state === "OPEN" ? sweepStep ( detail ) : "skip" ;
184196 if ( step === "skip" ) continue ;
185197
@@ -260,7 +272,7 @@ async function sweep({ github, context, core, dryRun }) {
260272 return plan ;
261273}
262274
263- async function enforceOpenLimit ( { github, context, core } ) {
275+ async function enforceOpenLimit ( { github, context, core, dryRun } ) {
264276 const pr = context . payload . pull_request ;
265277 const author = pr . user . login ;
266278 const status = process . env . VOUCH_STATUS ;
@@ -273,13 +285,21 @@ async function enforceOpenLimit({ github, context, core }) {
273285 // Someone else reopening a PR is a maintainer overriding the limit.
274286 if ( context . payload . action === "reopened" && context . payload . sender . login !== author ) return ;
275287
276- const q = `repo:${ context . repo . owner } /${ context . repo . repo } is:pr is:open author:${ author } ` ;
288+ const q = `repo:${ context . repo . owner } /${ context . repo . repo } is:pr is:open author:${ author } sort:created-asc ` ;
277289 const { search } = await github . graphql ( AUTHOR_OPEN_PRS_QUERY , { q } ) ;
278- const others = search . nodes . filter ( ( node ) => node . number && node . number !== pr . number ) . length ;
290+ // A new PR counts only the older ones, so two opened together cannot close each other.
291+ const reopened = context . payload . action === "reopened" ;
292+ const others = search . nodes . filter (
293+ ( node ) => node . number && ( reopened ? node . number !== pr . number : node . number < pr . number ) ,
294+ ) . length ;
279295 if ( others < OPEN_LIMIT ) {
280296 core . info ( `PR #${ pr . number } : ${ author } has ${ others } other open PRs.` ) ;
281297 return ;
282298 }
299+ if ( dryRun ) {
300+ core . notice ( `Dry run. PR #${ pr . number } would close: ${ author } has ${ others } other open PRs.` ) ;
301+ return ;
302+ }
283303
284304 await closeWithComment (
285305 github ,
0 commit comments