Skip to content

fix: fail over rate-limited GitHub App requests - #5523

Merged
guicaulada merged 9 commits into
mainfrom
gc/fix-github-app-failover
Oct 16, 2026
Merged

guicaulada merged 9 commits into
mainfrom
gc/fix-github-app-failover

Conversation

@guicaulada

@guicaulada guicaulada commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

Multi-app clients currently keep retrying with the selected GitHub App after rate limiting. Generic HTTP 429 responses can bypass secondary-limit detection, and waiting for a rate-limit reset can consume the Lambda runtime even when another app is available.

Add shared, bounded request-level failover for scale-up, pool, scale-down, and job retry:

  • Handle every 429 and rate-limit-specific 403, including responses with nonzero primary quota remaining.
  • Select the eligible app with the highest recently observed budget, track primary resources separately, expire stale observations, and respect cooldowns even when every app is unavailable.
  • Apply Retry-After/reset deadlines to the failed app, resolve the replacement app's own installation, and retry only the failed GitHub operation. Installation lookup and token minting use the same failover policy.
  • Attempt each app at most once per operation and return exhausted failures to existing caller retry handling without sleeping for rate-limit resets.
  • Attribute response accounting to the app that actually made the request, rather than the original batch selection.

Selection remains local to each warm Lambda environment. Secondary throttling can affect multiple apps; this does not introduce distributed quota coordination or guarantee linear throughput scaling.

Job-retry failures return per-record SQS batch responses, with ReportBatchItemFailures enabled in both Terraform implementations. Failed authentication, status checks, and publications are retried; successful records are acknowledged. Single-App rate-limit waits are replaced by prompt failure and queue redelivery (or the next scheduled run), documented in the tuning guide.

Test Plan

  • Control-plane suite: 386 tests passed, including real Octokit hook integration tests for generic/secondary 429s, rate-limit 403s, ordinary permission failures, bounded exhaustion, single-app cooldowns, separate primary resources, and organization/repository installation identity.

  • Verified that successful JIT requests are not replayed when a subsequent request fails over.

  • TypeScript, ESLint, Prettier, diff checks, and the control-plane ncc build passed.

  • Explicit coverage run remains below existing thresholds on both unchanged main and this branch. Line coverage improves from 94.87% to 96.07%; branch coverage improves from 87.10% to 88.66%. No coverage thresholds were changed.

  • Not deployed; validation is local and uses mocked GitHub HTTP responses.

  • Terraform: legacy job-retry module validates; all 5 orchestration job-retry tests pass on the rebased branch, including partial-batch response wiring.

@guicaulada
guicaulada requested a review from a team as a code owner October 14, 2026 15:45
@github-actions

Copy link
Copy Markdown
Contributor

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Cross-cutting authentication and request-hook changes need human validation, and resource-selection and cooldown-isolation issues remain unresolved.

2 open findings
What changed in this PR

Adds shared, bounded GitHub App failover across control-plane operations to avoid waiting for rate-limit resets.

Changes:

  • Retries rate-limited operations with another eligible app.
  • Centralizes quota tracking and response accounting.
  • Adds failover tests and documents selection behavior.
File Description
lambdas/​functions/​control-plane/​src/​test/​compute-provider-contracts/​pool.ts Mocks failover in contract tests.
lambdas/​functions/​control-plane/​src/​scale-runners/​scale-up.ts Integrates failover into scale-up authentication.
lambdas/​functions/​control-plane/​src/​scale-runners/​scale-up.test.ts Mocks failover for existing tests.
lambdas/​functions/​control-plane/​src/​scale-runners/​scale-up-contract.test.ts Isolates scale-up contract tests.
lambdas/​functions/​control-plane/​src/​scale-runners/​scale-down.ts Adds failover and removes local accounting.
lambdas/​functions/​control-plane/​src/​scale-runners/​scale-down.test.ts Mocks failover for existing tests.
lambdas/​functions/​control-plane/​src/​scale-runners/​github-runner.ts Delegates accounting to shared clients.
lambdas/​functions/​control-plane/​src/​scale-runners/​github-runner.test.ts Updates accounting expectations.
lambdas/​functions/​control-plane/​src/​pool/​pool.ts Integrates pool-client failover.
lambdas/​functions/​control-plane/​src/​pool/​pool.test.ts Mocks failover for pool tests.
lambdas/​functions/​control-plane/​src/​github/​rate-limit.ts Separates metrics from selection accounting.
lambdas/​functions/​control-plane/​src/​github/​rate-limit.test.ts Updates metric expectations.
lambdas/​functions/​control-plane/​src/​github/​octokit.ts Adds authentication failover for job retry.
lambdas/​functions/​control-plane/​src/​github/​octokit.test.ts Isolates authentication tests.
lambdas/​functions/​control-plane/​src/​github/​auth.ts Tracks resource budgets and enforces cooldowns.
lambdas/​functions/​control-plane/​src/​github/​auth.test.ts Tests exhausted-app exclusion.
lambdas/​functions/​control-plane/​src/​github/​app-failover.ts Implements shared request-level failover.
lambdas/​functions/​control-plane/​src/​github/​app-failover.test.ts Tests failover, cooldowns, and installation identity.
docs/​rate-limits-and-tuning.md Documents failover behavior and limitations.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Active exhaustion deadlines can be overwritten, and authentication failures can select retries against the wrong quota resource.

2 open findings
2 resolved since last review

🧠 Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Exhausted failover can lose job-retry messages, and quota-selection edge cases can block eligible requests or repeat throttled authentication.

1 open finding
2 resolved since last review
Previously missed (3)

In code that hasn't changed since last review

Medium severity Prevent repeated authentication against core-throttled apps

lambdas/​functions/​control-plane/​src/​github/​app-failover.ts:84

If client initialization hits a primary core limit during a JIT request, deleting this promise lets later JIT requests repeat that initialization before reset. They select by registration quota, where the failed app can still have the highest (unknown) budget, and the factory's explicit app index bypasses core eligibility. This repeatedly sends authentication requests to an app already known to be throttled. Check initialization eligibility separately and skip unavailable uncached clients, while keeping cached clients usable for JIT. Extend the authentication-failure regression with a second JIT request.

Medium severity Avoid caching core when JIT response omits rate-limit resource

lambdas/​functions/​control-plane/​src/​github/​app-failover.ts:135

A successful JIT response without x-ratelimit-resource replaces the inferred actions_runner_registration resource with core; the new tests already use responses without this header. If core quota is then exhausted on every app, subsequent JIT requests are rejected locally even when registration quota remains. Update the route cache only when the response supplies a resource header, and add a regression covering a second JIT request after core exhaustion.

Medium severity Scope primary quota exhaustion per installation

lambdas/​functions/​control-plane/​src/​github/​auth.ts:115

Installation-authenticated core quotas are per installation, but this key shares their exhaustion across every installation of an app. With multiple organizations (covered by scale-up.test.ts:897–918), exhausting org A's quota now excludes the app for org B even though B has an independent budget. The initial auth selection can reject before B's client is created. Scope primary observations and selection to the installation or owner, including auth seeding, while retaining app-wide secondary cooldowns. Add a two-installation regression.

🧠 Review effort: Balanced

@guicaulada

Copy link
Copy Markdown
Contributor Author

Addressed the three additional findings in the latest review summary in 3c8af39:

  • Uncached installation clients must have eligible core quota before authentication; cached clients can still use registration quota. The authentication-failure regression now makes a second JIT request and confirms the exhausted app is not initialized again.
  • Responses without x-ratelimit-resource no longer overwrite the inferred JIT resource. Repeated JIT requests remain possible after all core budgets are exhausted.
  • Primary quota observations and selection are scoped by GitHub endpoint and normalized installation owner, including scoped authentication seeds. Secondary cooldowns remain app-wide. Added owner/endpoint-isolation coverage.

Validation: all 377 control-plane tests pass, along with ESLint and TypeScript checks.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Incorrect quota accounting can block usable apps, and initialization errors can trigger unsafe runner termination.

2 open findings
1 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Medium severity Record quota headers before rethrowing HTTP failures

lambdas/​functions/​control-plane/​src/​github/​app-failover.ts:130

This early rethrow skips quota observations and metrics for ordinary HTTP failures. For example, a 404 reporting zero core quota leaves an earlier positive observation eligible, so subsequent operations select an exhausted app. Record available response headers before rethrowing, without switching apps for the failed permission or validation operation. The removed caller-side error accounting no longer handles this.

🧠 Review effort: Balanced

@guicaulada

Copy link
Copy Markdown
Contributor Author

Addressed the additional finding from the Copilot review summary in c4fea0d: ordinary HTTP failures now record quota headers and metrics before being rethrown, without replaying permission or validation failures.

@atsikham

atsikham commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

This overlap with #5407, open for the same issue #5409. Your hook approach cover more, so if maintainers prefer this one I will close #5407, both can't be merged together.

Few things I see:

  1. jobRetryCheck rethrow fail the whole batch. Default batch size is 10 and no ReportBatchItemFailures, so records already published before are delivered again, can create duplicate runners. Better return batchItemFailures only for failed ones.
  2. Only rate-limit errors are rethrown, 5xx or timeout in job queued check still lose the job. fix(job-retry): fail open instead of dropping the job when isJobQueued errors #5424 fix this with fail open.
  3. Throttling plugin don't retry anymore also with single app, worth mention in docs as behavior change or preserve existing behavior.

@guicaulada
guicaulada force-pushed the gc/fix-github-app-failover branch from 7184323 to 13778d0 Compare October 15, 2026 13:32
@guicaulada

Copy link
Copy Markdown
Contributor Author

Thanks for taking a look. I compared #5407 and think we should keep this approach because it also covers JIT/registration requests and cleanup, while #5407 explicitly leaves registration requests without their own failover.

Addressed your feedback in 13778d0:

  • jobRetryCheck now returns failed message IDs, and both Terraform event source mappings enable ReportBatchItemFailures, so a failed record does not replay the successful records in its batch.
  • Failed authentication, queued-job checks (including 5xx/timeouts), and queue publications remain eligible for SQS redelivery. I used per-record retry here rather than assuming the job is queued as in fix(job-retry): fail open instead of dropping the job when isJobQueued errors #5424.
  • Documented the single-App behavior change: no in-Lambda wait for quota reset; failed queue records retry through SQS, and scheduled cleanup/pool work retries on subsequent runs.

Rebased onto current main as well.

@guicaulada
guicaulada requested a balanced review from Copilot October 15, 2026 13:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Cross-cutting authentication and runner operations warrant human review, with a cooldown-scoping issue still unresolved.

1 open finding
1 resolved since last review

🧠 Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Shared authentication and request replay affect runner creation and destructive scale-down operations, warranting final human review.

0 open findings

1 resolved since last review

🧠 Review effort: Balanced

@Brend-Smits Brend-Smits left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this on a sandbox deployment with a single GitHub App (webhook, scale-up, scale-down and job-retry lambdas). The test workflow ran fine on a freshly scaled runner, the lambda logs had no warnings or errors, and the runner was terminated and de-registered by scale-down afterwards. The Lambda test suite also passes. I didn't test real failover between multiple apps, so that part is only covered by the mocked tests.

Two small things I'd like you to look at, see the comments below.

@guicaulada
guicaulada merged commit 7453956 into main Oct 16, 2026
61 checks passed
@guicaulada
guicaulada deleted the gc/fix-github-app-failover branch October 16, 2026 19:45
Brend-Smits added a commit that referenced this pull request Oct 22, 2026
🤖 I have created a release *beep* *boop*
---


##
[8.0.0](v7.11.0...v8.0.0)
(2026-10-21)


### ⚠ BREAKING CHANGES

* **terraform:** require Terraform 1.5.6
([#5261](#5261))

### Features

* **compute-providers:** add AWS EC2 provider modules
([#5343](#5343))
([ffa299f](ffa299f))
* **dynamic-labels:** add a capacity-type label to choose spot or
on-demand per job
([#5485](#5485))
([edecd36](edecd36))
* **dynamic-labels:** add allow-list mode to the AWS dynamic labels
policy
([#5411](#5411))
([05984f6](05984f6))
* **examples:** add multi-runner-v2 example
([#5367](#5367))
([f2779cc](f2779cc))
* **github-app:** select app by rate limit budget
([#5391](#5391))
([668d8da](668d8da))
* **github-app:** use ssm manifest for extra apps
([#5282](#5282))
([33c3332](33c3332))
* **multi-runner:** add experimental config translation
([#5284](#5284))
([dd24d33](dd24d33))
* **multi-runner:** consume resolved experimental config
([#5340](#5340))
([ff8bfc2](ff8bfc2))
* **multi-runner:** move v1 state into v2 modules
([#5415](#5415))
([e8d7956](e8d7956))
* **orchestration-providers:** add webhook provider modules
([#5344](#5344))
([c05c9b3](c05c9b3))
* **runner-config:** add provider-aware runner configuration
([#5345](#5345))
([42b1261](42b1261))
* **runners:** add network_interfaces for advanced EC2 launch template
configuration
([#5427](#5427))
([1e72c14](1e72c14))
* **scale-down:** opt-in idle confirmation window before terminating
not-busy runners
([#5397](#5397))
([595f3e5](595f3e5))
* **scale-set:** add scale set orchestration in Terraform
([#5299](#5299))
([937fe15](937fe15))
* **storage:** add runner config consumer
([#5358](#5358))
([138309c](138309c))
* support native SSM expiration policies for runner token parameters
([#5265](#5265))
([2dca7f9](2dca7f9))
* **terraform:** add SSM storage provider boundary
([#5454](#5454))
([d04c5da](d04c5da))
* **terraform:** require Terraform 1.5.6
([#5261](#5261))
([f335b32](f335b32))


### Bug Fixes

* allow null amd_sev_snp/nested_virtualization in cpu_options vali…
([#5430](#5430))
([0b5ec3d](0b5ec3d))
* **control-plane:** clarify partial runner creation test name
([#5339](#5339))
([b1dd396](b1dd396))
* **deps:** bump actions/deploy-pages from 5.0.0 to 5.0.1 in the github
group
([#5418](#5418))
([1a691ea](1a691ea))
* **deps:** bump github/codeql-action/upload-sarif from 4.36.3 to 4.37.8
([#5317](#5317))
([fdb4385](fdb4385))
* **deps:** bump
google/osv-scanner-action/.github/workflows/osv-scanner-reusable-pr.yml
from 2.5.0 to 2.5.1
([#5331](#5331))
([1523a66](1523a66))
* **deps:** bump ossf/scorecard-action from 2.4.3 to 2.4.4
([#5319](#5319))
([d798070](d798070))
* **deps:** bump step-security/harden-runner from 2.19.4 to 2.21.0
([#5318](#5318))
([c81927a](c81927a))
* **deps:** bump step-security/harden-runner from 2.19.4 to 2.21.1
([#5386](#5386))
([48b0c42](48b0c42))
* **deps:** bump step-security/harden-runner from 2.21.1 to 2.22.1
([#5532](#5532))
([5ccf8b3](5ccf8b3))
* **deps:** bump terraform-linters/setup-tflint from 6.3.0 to 6.3.1
([#5419](#5419))
([5f0ecef](5f0ecef))
* **deps:** bump terraform-linters/setup-tflint from 6.3.1 to 6.3.2
([#5515](#5515))
([4c4191f](4c4191f))
* **deps:** bump the github group with 3 updates
([#5330](#5330))
([ab6edf5](ab6edf5))
* **deps:** bump the github group with 3 updates
([#5446](#5446))
([7bb9280](7bb9280))
* **deps:** bump the github group with 3 updates
([#5504](#5504))
([aa08de3](aa08de3))
* **deps:** bump the github group with 5 updates
([#5531](#5531))
([c5b9a47](c5b9a47))
* **deps:** bump zizmorcore/zizmor-action from 0.6.2 to 0.6.3
([#5387](#5387))
([62aef5d](62aef5d))
* **deps:** bump zizmorcore/zizmor-action from 0.6.3 to 0.6.4
([#5447](#5447))
([aa24dc0](aa24dc0))
* **ec2:** avoid launch template tag conflict for on-demand
capacity-type overrides
([#5522](#5522))
([dc9e820](dc9e820))
* **ec2:** improve failure observability
([#5335](#5335))
([91751ef](91751ef))
* fail over rate-limited GitHub App requests
([#5523](#5523))
([7453956](7453956))
* fix typo in variable
([#5462](#5462))
([0786125](0786125))
* **github-runner:** record the rate-limit metric even when the call
fails
([#5426](#5426))
([5026cd9](5026cd9))
* **lambda:** bump @babel/core from 7.23.9 to 7.29.7 in /lambdas
([#5181](#5181))
([3729cf7](3729cf7))
* **lambda:** bump @humanfs/node from 0.16.6 to 0.16.8 in /lambdas
([#5336](#5336))
([f7dd7cb](f7dd7cb))
* **lambda:** bump axios from 1.18.0 to 1.18.1 in /lambdas
([#5223](#5223))
([21d27f1](21d27f1))
* **lambda:** bump axios from 1.18.1 to 1.20.0 in /lambdas
([#5502](#5502))
([e2f9e5e](e2f9e5e))
* **lambda:** bump browserslist from 4.22.3 to 4.28.8 in /lambdas
([#5329](#5329))
([27e7d53](27e7d53))
* **lambda:** bump fast-uri from 3.1.2 to 3.1.5 in /lambdas
([#5242](#5242))
([9c3e28c](9c3e28c))
* **lambda:** bump fast-uri from 3.1.5 to 3.1.6 in /lambdas
([#5337](#5337))
([07bbc87](07bbc87))
* **lambda:** bump fast-uri from 3.1.6 to 3.1.8 in /lambdas
([#5488](#5488))
([3062702](3062702))
* **lambda:** bump follow-redirects from 1.16.0 to 1.16.1 in /lambdas
([#5540](#5540))
([a7b6dea](a7b6dea))
* **lambda:** bump js-yaml from 3.15.0 to 3.15.1 in /lambdas
([#5264](#5264))
([a3116bd](a3116bd))
* **lambda:** bump js-yaml from 3.15.1 to 3.15.2 in /lambdas
([#5398](#5398))
([3db123f](3db123f))
* **lambda:** bump proxy-addr from 2.0.7 to 2.0.8 in /lambdas
([#5510](#5510))
([faf57cd](faf57cd))
* **lambda:** bump source-map-js from 1.2.1 to 1.2.2 in /lambdas
([#5511](#5511))
([e058e05](e058e05))
* **lambda:** bump undici from 6.28.0 to 6.28.1 in /lambdas
([#5489](#5489))
([3ff5877](3ff5877))
* **lambda:** create log groups before functions
([#5324](#5324))
([2f844ab](2f844ab)),
closes
[#5323](#5323)
* **migration-test:** preserve unreserved job retry concurrency
([#5516](#5516))
([feba645](feba645))
* **multi-runner:** gate v2 routing with experimental feature
([#5404](#5404))
([b448845](b448845))
* **multi-runner:** make v2 inputs independent
([#5368](#5368))
([45d3d0e](45d3d0e))
* **multi-runner:** route v1 and v2 runners separately
([#5346](#5346))
([cfac174](cfac174))
* **pages:** configure GitHub Pages deployment environment
([#5334](#5334))
([f081004](f081004))
* **pool-lambda:** grant permission to terminate failed EC2 runners
([#5538](#5538))
([0043890](0043890))
* re-indent commands in CONTRIBUTING.md guide
([#5305](#5305))
([547a98b](547a98b))
* retain arm64 build coverage in lambda workflow
([#5535](#5535))
([80078d7](80078d7))
* **runners:** guard SEGMENT against set -u in start-runner.sh
([#5233](#5233))
([182b37d](182b37d))
* **runners:** support instance requirements in EC2 Fleet overrides
([#5314](#5314))
([4f80654](4f80654))
* **scale-set:** implement review followups
([#5477](#5477))
([445269c](445269c))
* **ssm:** batch housekeeper parameter deletions
([#5469](#5469))
([28a5135](28a5135))
* **ssm:** delete expired runner parameters as pages are listed
([#5464](#5464))
([d815b76](d815b76))
* **ssm:** restore incremental housekeeper cleanup
([#5508](#5508))
([22b9310](22b9310))
* **storage:** add SSM adapter diagnostics
([#5385](#5385))
([d08be31](d08be31))
* **webhook:** cache only a successful config load
([#5519](#5519))
([a2fea96](a2fea96))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

---------

Signed-off-by: Brend Smits <[email protected]>
Co-authored-by: runners-releaser[bot] <194412594+runners-releaser[bot]@users.noreply.github.com>
Co-authored-by: Brend Smits <[email protected]>
guicaulada added a commit that referenced this pull request Oct 27, 2026
Scale-down caches installation clients by owner. Scope that cache by
selected GitHub App, runner type, and owner, and select the App before
checking the cache so a changed selection uses the corresponding client.

Preserve the shared request-level failover added in #5523. Installation
lookup and token creation stay inside its client factory; the shared
failover layer handles quota observation and metrics, avoiding duplicate
response hooks in scale-down.

Validation: all 403 control-plane tests, runtime TypeScript, ESLint, and
diff checks pass. The regression verifies selection changes for the same
owner and client reuse within each App. No AWS deployment or live runner
cleanup was performed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants