-
Notifications
You must be signed in to change notification settings - Fork 558
fix: Prevent GitHub API rate limiting from frequent worktree PR fetching (fixes #685) #688
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
Merged
Shironex
merged 3 commits into
v0.14.0rc
from
feature/bug-worktree-pr-fetching-still-fetch-too-frequent-26ay
Jan 24, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
f5efa85
fix: Prevent GitHub API rate limiting from frequent worktree PR fetching
Shironex b5143f4
fix: Return stale cache on GitHub PR fetch failure to prevent repeate…
Shironex 8dd6ab2
fix: Extend cache TTL on GitHub PR fetch failure to prevent retry storms
Shironex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,8 +39,15 @@ interface GitHubRemoteCacheEntry { | |
| checkedAt: number; | ||
| } | ||
|
|
||
| interface GitHubPRCacheEntry { | ||
| prs: Map<string, WorktreePRInfo>; | ||
| fetchedAt: number; | ||
| } | ||
|
|
||
| const githubRemoteCache = new Map<string, GitHubRemoteCacheEntry>(); | ||
| const githubPRCache = new Map<string, GitHubPRCacheEntry>(); | ||
| const GITHUB_REMOTE_CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes | ||
| const GITHUB_PR_CACHE_TTL_MS = 2 * 60 * 1000; // 2 minutes - avoid hitting GitHub on every poll | ||
|
|
||
| interface WorktreeInfo { | ||
| path: string; | ||
|
|
@@ -180,9 +187,21 @@ async function getGitHubRemoteStatus(projectPath: string): Promise<GitHubRemoteS | |
| * This also allows detecting PRs that were created outside the app. | ||
| * | ||
| * Uses cached GitHub remote status to avoid repeated warnings when the | ||
| * project doesn't have a GitHub remote configured. | ||
| * project doesn't have a GitHub remote configured. Results are cached | ||
| * briefly to avoid hammering GitHub on frequent worktree polls. | ||
| */ | ||
| async function fetchGitHubPRs(projectPath: string): Promise<Map<string, WorktreePRInfo>> { | ||
| async function fetchGitHubPRs( | ||
| projectPath: string, | ||
| forceRefresh = false | ||
| ): Promise<Map<string, WorktreePRInfo>> { | ||
| const now = Date.now(); | ||
| const cached = githubPRCache.get(projectPath); | ||
|
|
||
| // Return cached result if valid and not forcing refresh | ||
| if (!forceRefresh && cached && now - cached.fetchedAt < GITHUB_PR_CACHE_TTL_MS) { | ||
| return cached.prs; | ||
| } | ||
|
|
||
| const prMap = new Map<string, WorktreePRInfo>(); | ||
|
|
||
| try { | ||
|
|
@@ -225,8 +244,22 @@ async function fetchGitHubPRs(projectPath: string): Promise<Map<string, Worktree | |
| createdAt: pr.createdAt, | ||
| }); | ||
| } | ||
|
|
||
| // Only update cache on successful fetch | ||
| githubPRCache.set(projectPath, { | ||
| prs: prMap, | ||
| fetchedAt: Date.now(), | ||
| }); | ||
| } catch (error) { | ||
| // Silently fail - PR detection is optional | ||
| // On fetch failure, return stale cached data if available to avoid | ||
| // repeated API calls during GitHub API flakiness or temporary outages | ||
| if (cached) { | ||
| logger.warn(`Failed to fetch GitHub PRs, returning stale cache: ${getErrorMessage(error)}`); | ||
| // Extend cache TTL to avoid repeated retries during outages | ||
| githubPRCache.set(projectPath, { prs: cached.prs, fetchedAt: Date.now() }); | ||
| return cached.prs; | ||
| } | ||
| // No cache available, log warning and return empty map | ||
| logger.warn(`Failed to fetch GitHub PRs: ${getErrorMessage(error)}`); | ||
|
Comment on lines
+254
to
263
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. Avoid retry storms when there’s no cached PR data. 🛠️ Suggested fix if (cached) {
logger.warn(`Failed to fetch GitHub PRs, returning stale cache: ${getErrorMessage(error)}`);
// Extend cache TTL to avoid repeated retries during outages
githubPRCache.set(projectPath, { prs: cached.prs, fetchedAt: Date.now() });
return cached.prs;
}
// No cache available, log warning and return empty map
logger.warn(`Failed to fetch GitHub PRs: ${getErrorMessage(error)}`);
+ const empty = new Map<string, WorktreePRInfo>();
+ githubPRCache.set(projectPath, { prs: empty, fetchedAt: Date.now() });
+ return empty;🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
|
|
@@ -364,7 +397,7 @@ export function createListHandler() { | |
| // Only fetch GitHub PRs if includeDetails is requested (performance optimization). | ||
| // Uses --state all to detect merged/closed PRs, limited to 1000 recent PRs. | ||
| const githubPRs = includeDetails | ||
| ? await fetchGitHubPRs(projectPath) | ||
| ? await fetchGitHubPRs(projectPath, forceRefreshGitHub) | ||
| : new Map<string, WorktreePRInfo>(); | ||
|
|
||
| for (const worktree of worktrees) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.