← Back to Roadmap

Task 3: GitHub API Rate Limit Redesign

3-4 days Backend / Integration

Current Issue / Scenario

Users hit GitHub API rate limits during heavy sessions. AO's scm-github plugin (32K lines) shells out to the gh CLI for all GitHub interactions — PR creation, CI checks, reviews, merge readiness. With multiple agents running in parallel, the 5,000 requests/hour limit is exhausted. No caching, no request coordination, no graceful degradation.

The Problem in Numbers

ScenarioEstimated API Calls
Single agent session (spawn, work, PR)~50-100 calls
5 parallel agents~250-500 calls
Dashboard polling (5s interval, 10 sessions)~720/hr per metric
CI status checks (per PR)~60/hr per PR
Total (5 agents + dashboard)~2,000-4,000/hr

How AO calls GitHub (actual codebase)

Why this is architecture-level: The gh CLI manages its own auth and rate limiting, but AO has no visibility into it. Multiple plugins spawn independent gh processes with no coordination. When rate-limited, gh returns errors that AO doesn't handle gracefully. The fix requires either: (a) wrapping gh calls through a coordinator, or (b) migrating to Octokit for proper header-based rate limit tracking.

What To Do

How We Will Do It

Two approaches — choose based on scope appetite:

Option A: Wrap gh CLI (lower effort, faster)

Option B: Migrate to Octokit (higher effort, proper)

Recommendation: Start with Option A (wrap gh) for quick wins. Plan Option B (Octokit migration) as a separate task — it's a bigger refactor of the 32K-line scm-github plugin but gives proper rate limit control.

References

SourceWhat to learn
GitHub REST API Rate LimitsOfficial docs on limits, headers, best practices. Conditional requests are key.
Octokit + plugin-throttling@octokit/plugin-throttling — handles 403 retries and abuse detection. Useful if migrating from gh CLI to Octokit (Option B).
scm-github/src/index.ts32,165 bytes. Uses execFile("gh", [...]) for all GitHub calls. This is the primary consumer to wrap or migrate.
tracker-github pluginSecond GitHub consumer — polls for issue updates. Also needs to route through centralized layer.
packages/core/src/session-manager.ts82K bytes. Triggers GitHub calls for PR status, CI checks. Polling frequency is configurable but defaults are aggressive.

Implementation Plan

Audit Current gh CLI Usage

Grep scm-github/src/index.ts and tracker-github for all execFile/execCli calls. Map which gh subcommands are used, how often, and from where. The file uses a helper execCli(bin, args, cwd) with 30s timeout.

Create Centralized gh Executor

Build packages/core/src/gh-executor.ts. All gh CLI calls route through this. Tracks rate limit via periodic gh api rate_limit checks. Queues requests by priority.

interface GhExecutor {
  exec(args: string[], opts: { priority: 'high' | 'medium' | 'low'; cwd?: string }): Promise<string>;
  getRemainingBudget(): { remaining: number; resetAt: Date };
  getCacheStats(): { hits: number; misses: number };
}

Add Response Cache

In-memory LRU cache keyed by gh command + args. Cache stdout output with TTL per command type:

const CACHE_TTL = {
  'gh api repos/*/issues': 5 * 60 * 1000,   // 5 min
  'gh pr view': 60 * 1000,                    // 1 min
  'gh pr checks': 15 * 1000,                  // 15 sec
  'gh api repos/*/commits': 10 * 60 * 1000,  // 10 min
};

Implement Request Queue

Priority queue with 3 levels: high (user-initiated: spawn, merge, PR create), medium (dashboard refresh), low (background polling, CI status checks). When remaining budget < 20%, pause low-priority requests. When < 5%, pause medium too.

Refactor scm-github to Use Executor

Replace all execCli("gh", [...]) calls in scm-github/src/index.ts (32K lines) with ghExecutor.exec([...], { priority }). Biggest change — every function that calls gh needs to specify its priority level and accept cached responses.

Refactor tracker-github

Same treatment — route through centralized executor. Tracker polls frequently for issue updates; make it use cached responses and lower priority.

Dashboard Rate Limit Widget

Show remaining API budget in dashboard header. Color-coded: green (>50%), yellow (20-50%), red (<20%). Click to expand and see per-consumer breakdown.

Graceful Degradation

When rate-limited: show stale cached data with a "last updated X min ago" badge. Queue requests for when limit resets. Never show a raw 403 error to the user. Log rate limit events for debugging.

Acceptance Criteria