Repository navigation
fix(prs): back off a linked PR whose background read keeps failing #2469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1153,15 +1153,26 @@ function isGraphqlUndefinedFieldError(error: unknown): boolean { | |||||||||||||||||||||||||||||
| .test(getErrorMessage(error)); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * How stale a row must be before a background read, and the floor a failed | ||||||||||||||||||||||||||||||
| * read buys: an active PR is due every couple of minutes, a closed one rarely. | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| function backgroundRefreshStaleMs(state: string | null | undefined): number { | ||||||||||||||||||||||||||||||
| const normalized = String(state ?? "").toLowerCase(); | ||||||||||||||||||||||||||||||
| return normalized === "open" || normalized === "draft" | ||||||||||||||||||||||||||||||
| ? BACKGROUND_REFRESH_MIN_STALE_MS | ||||||||||||||||||||||||||||||
| : BACKGROUND_REFRESH_CLOSED_STALE_MS; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| function isBackgroundRefreshCandidate(row: PullRequestRow, nowMs: number): boolean { | ||||||||||||||||||||||||||||||
| const state = String(row.state ?? "").toLowerCase(); | ||||||||||||||||||||||||||||||
| const isActive = state === "open" || state === "draft"; | ||||||||||||||||||||||||||||||
| const staleAfterMs = isActive ? BACKGROUND_REFRESH_MIN_STALE_MS : BACKGROUND_REFRESH_CLOSED_STALE_MS; | ||||||||||||||||||||||||||||||
| const lastSyncedAtMs = parseIsoMs(row.last_synced_at); | ||||||||||||||||||||||||||||||
| if (lastSyncedAtMs <= 0) return true; | ||||||||||||||||||||||||||||||
| return nowMs - lastSyncedAtMs >= staleAfterMs; | ||||||||||||||||||||||||||||||
| return nowMs - lastSyncedAtMs >= backgroundRefreshStaleMs(row.state); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** The per-PR key in the shared GitHub read ladder. */ | ||||||||||||||||||||||||||||||
| const backgroundPrReadKey = (prId: string): string => `pr:${prId}`; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| function compareBackgroundRefreshPriority(left: PullRequestRow, right: PullRequestRow): number { | ||||||||||||||||||||||||||||||
| const leftState = String(left.state ?? "").toLowerCase(); | ||||||||||||||||||||||||||||||
| const rightState = String(right.state ?? "").toLowerCase(); | ||||||||||||||||||||||||||||||
|
|
@@ -5902,20 +5913,37 @@ export function createPrService({ | |||||||||||||||||||||||||||||
| return updated; | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const refreshPrIds = async (prIds: string[]): Promise<PrSummary[]> => { | ||||||||||||||||||||||||||||||
| const refreshPrIds = async ( | ||||||||||||||||||||||||||||||
| prIds: string[], | ||||||||||||||||||||||||||||||
| options: { recordFailures?: boolean } = {}, | ||||||||||||||||||||||||||||||
| ): Promise<PrSummary[]> => { | ||||||||||||||||||||||||||||||
| const uniquePrIds = [...new Set(prIds.map((prId) => String(prId ?? "").trim()).filter(Boolean))]; | ||||||||||||||||||||||||||||||
| const refreshed: PrSummary[] = []; | ||||||||||||||||||||||||||||||
| const failures: Array<{ prId: string; reason: unknown }> = []; | ||||||||||||||||||||||||||||||
| // A failed row never advances `last_synced_at`, so without a ladder it is | ||||||||||||||||||||||||||||||
| // the oldest candidate on every later sweep and is re-read forever. The | ||||||||||||||||||||||||||||||
| // row's state picks the floor: an active PR keeps its couple of minutes. | ||||||||||||||||||||||||||||||
| const stateByPrId = options.recordFailures | ||||||||||||||||||||||||||||||
| ? new Map(listRows().map((row) => [row.id, row.state])) | ||||||||||||||||||||||||||||||
| : null; | ||||||||||||||||||||||||||||||
| for (let i = 0; i < uniquePrIds.length; i += REFRESH_CONCURRENCY) { | ||||||||||||||||||||||||||||||
| const batchPrIds = uniquePrIds.slice(i, i + REFRESH_CONCURRENCY); | ||||||||||||||||||||||||||||||
| const results = await Promise.allSettled(batchPrIds.map((prId) => refreshOne(prId))); | ||||||||||||||||||||||||||||||
| results.forEach((result, index) => { | ||||||||||||||||||||||||||||||
| const prId = batchPrIds[index] ?? ""; | ||||||||||||||||||||||||||||||
| if (result.status === "fulfilled") { | ||||||||||||||||||||||||||||||
| refreshed.push(result.value); | ||||||||||||||||||||||||||||||
| githubReadBackoff.clear(backgroundPrReadKey(result.value.id)); | ||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| failures.push({ prId, reason: result.reason }); | ||||||||||||||||||||||||||||||
| if (options.recordFailures && githubRateLimitResetAtMsOf(result.reason) == null) { | ||||||||||||||||||||||||||||||
| githubReadBackoff.record( | ||||||||||||||||||||||||||||||
| backgroundPrReadKey(prId), | ||||||||||||||||||||||||||||||
| result.reason, | ||||||||||||||||||||||||||||||
| backgroundRefreshStaleMs(stateByPrId?.get(prId)), | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
|
Comment on lines
+5941
to
+5945
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new per-PR ladder starts at attempt 1 on every normal background retry. Prompt To Fix With AIThis is a comment left during a code review.
Path: apps/desktop/src/main/services/prs/prService.ts
Line: 5941-5945
Comment:
**Retry delays never grow**
The new per-PR ladder starts at attempt 1 on every normal background retry. `isBackedOff()` deletes the expired entry before allowing another read, and `record()` also removes expired entries before counting attempts. A permanently failing open PR therefore keeps taking a slot roughly every two minutes instead of backing off toward fifteen minutes. Keep the failure count across cooldown expiry until a read succeeds, and add a test that advances time through repeated failures.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly. |
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rate-limit check misses errors without reset timeMedium Severity The check uses Reviewed by Cursor Bugbot for commit 577c641. Configure here.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GitHub-wide outages arm every row's backoffMedium Severity Only rate limits are kept off the per-row ladder. Outage failures ( Additional Locations (1)Reviewed by Cursor Bugbot for commit 577c641. Configure here.
Comment on lines
+5940
to
+5946
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Outages delay PR refresh recovery On GitHub outages or rate limits without a reset time, Learn moreThe fallback poll already has its own outage backoff, and the credential layer applies a rate-limit cooldown even without reset headers. GitHubRateLimitError accepts a null reset time and the failure classifier still recognizes it as rate-limited. The refresh rethrows an outage on the first attempt, but candidate selection skips the row on the next poll. If every candidate is skipped, the refresh resolves and the poller resets its failure count. Example: The fallback sweep hits a GitHub 503 while reading its only stale candidate, a closed PR. GitHub recovers a minute later, but the next sweep skips the PR instead of retrying it on the poller's outage cadence. The PR remains ineligible for 15 minutes from the failed read. Recommended fix: Skip per-PR recording for errors already recognized by
Suggested change
Was this helpful? React with 👍 or 👎 to provide feedback. |
||||||||||||||||||||||||||||||
| logger.warn("prs.refresh_failed", { prId, error: getErrorMessage(result.reason) }); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
@@ -10534,20 +10562,26 @@ export function createPrService({ | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| getReviewSnapshot, | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| async refresh(args: { prId?: string; prIds?: string[] } = {}): Promise<PrSummary[]> { | ||||||||||||||||||||||||||||||
| async refresh(args: { prId?: string; prIds?: string[]; background?: boolean } = {}): Promise<PrSummary[]> { | ||||||||||||||||||||||||||||||
| const requestedPrIds = [ | ||||||||||||||||||||||||||||||
| ...(args.prId ? [args.prId] : []), | ||||||||||||||||||||||||||||||
| ...((args.prIds ?? []).map((prId) => String(prId ?? "").trim()).filter(Boolean)), | ||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||
| if (requestedPrIds.length > 0) { | ||||||||||||||||||||||||||||||
| return withGithubStackMemberships(await refreshPrIds(requestedPrIds)); | ||||||||||||||||||||||||||||||
| const refreshed = await refreshPrIds(requestedPrIds); | ||||||||||||||||||||||||||||||
| // A requested read that succeeds restores the background sweep. | ||||||||||||||||||||||||||||||
| for (const pr of refreshed) githubReadBackoff.clear(backgroundPrReadKey(pr.id)); | ||||||||||||||||||||||||||||||
| return withGithubStackMemberships(refreshed); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const rows = listRows(); | ||||||||||||||||||||||||||||||
| const nowMs = Date.now(); | ||||||||||||||||||||||||||||||
| const hotPrIds = new Set(getHotRefreshPrIds(nowMs)); | ||||||||||||||||||||||||||||||
| const staleCandidates = rows | ||||||||||||||||||||||||||||||
| .filter((row) => !hotPrIds.has(row.id) && isBackgroundRefreshCandidate(row, nowMs)) | ||||||||||||||||||||||||||||||
| // A background sweep skips a row still inside its failure cooldown; a | ||||||||||||||||||||||||||||||
| // user-requested refresh reads every row once (the branch above). | ||||||||||||||||||||||||||||||
| .filter((row) => !args.background || !githubReadBackoff.isBackedOff(backgroundPrReadKey(row.id), nowMs)) | ||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 PR retries never back off further For a repeatedly failing PR, isBackedOff deletes the attempt count before the next eligible read. Failed open or draft PRs keep taking a sweep slot at the initial cooldown instead of retrying less often. Learn moreThe fallback poll calls Example: An open PR returns 404 on every scheduled read. The fourth failed read sets another two-minute cooldown instead of the 160 seconds the ladder assigns to attempt four. Recommended fix: Retain the attempt count beyond the retry deadline for per-PR entries, and clear it on success. Keep a separate retention deadline or bounded eviction policy so abandoned IDs do not accumulate. Preserve the existing expiration semantics for snapshot and branch callers. Was this helpful? React with 👍 or 👎 to provide feedback. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve retry fairness after the cooldown expires. With five stale open PRs, four permanent failures can still prevent the healthy fifth PR from refreshing. Set the supported polling interval to five minutes. Each poll runs after the two-minute cooldown expires, so the same four failed rows pass this filter and consume all four slots again. Track the last attempt, or rotate failed candidates, so a healthy stale row gets a slot even when the next poll occurs after cooldown expiry. Keep cooldown filtering for shorter polling intervals.
|
||||||||||||||||||||||||||||||
| .sort(compareBackgroundRefreshPriority) | ||||||||||||||||||||||||||||||
| .slice(0, BACKGROUND_REFRESH_MAX_PRS); | ||||||||||||||||||||||||||||||
| const hotCandidates = rows | ||||||||||||||||||||||||||||||
|
|
@@ -10566,7 +10600,10 @@ export function createPrService({ | |||||||||||||||||||||||||||||
| // rethrow would pin a perfectly healthy poller at max backoff forever. | ||||||||||||||||||||||||||||||
| // Only a failure that says GitHub itself is unusable counts. | ||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||
| await refreshPrIds(candidates.map((row) => row.id)); | ||||||||||||||||||||||||||||||
| await refreshPrIds( | ||||||||||||||||||||||||||||||
| candidates.map((row) => row.id), | ||||||||||||||||||||||||||||||
| args.background ? { recordFailures: true } : {}, | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||
| if (isGithubWideFailure(error)) throw error; | ||||||||||||||||||||||||||||||
| logger.warn("prs.background_refresh_rows_failed", { error: getErrorMessage(error) }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix only marks the fallback sweep as background work. When
relayHealthyis true, the automatic safety sweep still callsrefresh()without the flag, so it neither records nor checks per-PR cooldowns. If four permanently failing active PRs have the oldestlast_synced_atvalues, they still take every safety-sweep slot and keep healthy stale PRs out. Cover the relay safety sweep too, and ensure failed rows can yield slots across its fifteen-minute cadence.Prompt To Fix With AI