Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.
JavaScript agentic-codingai-agentsai-governanceautonomous-agentsclaude-codeclicode-reviewcodexcoding-agentsdeveloper-toolsgit-hooksllmlocal-firstmulti-agentsoftware-factoryspec-driven-developmentverificationvibe-coding
fix(relay): name a GitHub rate limit, not lost access [H5,H16]
Corey Olsonfdb1a59e4322
diff --git a/relay/github.js b/relay/github.jsindex 23c9bb38..b982f36b 100644--- a/relay/github.js+++ b/relay/github.js@@ -73,7 +73,7 @@ function tokenResult(data) {* Use GitHub's App web/device flows, without requesting OAuth scopes or following credentials to* another origin. The injected loopback provider exercises the real HTTP protocol in tests.*/-export function createGitHubClient({ clientId, clientSecret, callbackURL, oauthBase = 'https://github.com', apiBase = 'https://api.github.com', timeoutMs = 10_000, privateKey, now = Date.now }) {+export function createGitHubClient({ clientId, clientSecret, callbackURL, oauthBase = 'https://github.com', apiBase = 'https://api.github.com', timeoutMs = 10_000, privateKey, now = Date.now, log = line => process.stderr.write(line + '\n') }) {if (typeof clientId !== 'string' || !clientId || typeof clientSecret !== 'string' || !clientSecret) {throw new Refused('GITHUB_CONFIG', 'configure the GitHub App client id and secret before starting sign-in');}@@ -99,7 +99,10 @@ export function createGitHubClient({ clientId, clientSecret, callbackURL, oauthBreturn unsigned + '.' + sign('RSA-SHA256', Buffer.from(unsigned), key).toString('base64url');}- /** Bound requests and same-origin redirects; never surface provider bodies in errors. */+ /**+ * Bound requests and same-origin redirects; never surface provider bodies in errors.+ * A spent rate limit is named as one, so a 403 for quota never reads as lost repository access.+ */async function request(base, path, { form, token, json } = {}) {let url = new URL(path, base);const headers = { 'User-Agent': 'pullboard-relay', Accept: form ? 'application/json' : 'application/vnd.github+json', 'X-GitHub-Api-Version': GITHUB_API_VERSION };@@ -123,6 +126,16 @@ export function createGitHubClient({ clientId, clientSecret, callbackURL, oauthBurl = next;continue;}+ if (!response.ok) {+ const rate = ['x-ratelimit-limit', 'x-ratelimit-remaining', 'x-ratelimit-reset', 'x-ratelimit-resource'].map(name => name + '=' + (response.headers.get(name) ?? '-'));+ log(`github refused ${url.pathname} status=${response.status} ${rate.join(' ')}`);+ }+ if ([403, 429].includes(response.status) && response.headers.get('x-ratelimit-remaining') === '0') {+ await response.body?.cancel();+ const reset = Number(response.headers.get('x-ratelimit-reset'));+ const when = Number.isSafeInteger(reset) && reset > 0 ? new Date(reset * 1000).toISOString() : 'the rate limit resets';+ throw new Refused('GITHUB_RATE_LIMITED', `GitHub rate limited the relay; wait until ${when}, then retry`);+ }if ([401, 403, 404].includes(response.status)) {await response.body?.cancel();throw new Refused('NO_REPO_ACCESS', 'GitHub cannot confirm access with this sign-in; sign in again or ask for repository access');diff --git a/test/relay-auth.test.js b/test/relay-auth.test.jsnew file mode 100644index 00000000..265b8aae--- /dev/null+++ b/test/relay-auth.test.js@@ -0,0 +1,34 @@+/** GitHub refusals name a rate limit as one, never as lost repository access [H5,H16]. */+import assert from 'node:assert/strict';+import { createServer } from 'node:http';+import { once } from 'node:events';+import { test } from 'node:test';+import { createGitHubClient } from '../relay/github.js';+import { githubFixture } from './relay-fixture.js';++test('a GitHub rate limit is named as one [H5,H16]', async (t) => {+ const fixture = await githubFixture(t);+ const reset = Math.floor(Date.now() / 1000) + 600;+ const limited = createServer((req, res) => {+ res.writeHead(403, { 'Content-Type': 'application/json', 'x-ratelimit-limit': '5000', 'x-ratelimit-remaining': '0', 'x-ratelimit-reset': String(reset) });+ res.end('{"message":"API rate limit exceeded"}');+ });+ limited.listen(0, '127.0.0.1');+ await once(limited, 'listening');+ t.after(() => { limited.closeAllConnections(); limited.close(); });+ const lines = [];+ const user = { id: '7', login: 'fixture-user' };+ const rateLimited = createGitHubClient({ ...fixture.config, apiBase: 'http://127.0.0.1:' + limited.address().port, log: line => lines.push(line) });+ const resetAt = new Date(reset * 1000).toISOString();+ await assert.rejects(rateLimited.access(user, 'fixture/repository'), (error) => {+ assert.equal(error.code, 'GITHUB_RATE_LIMITED');+ assert.match(error.message, new RegExp(resetAt.replace(/[.]/g, '\\.')));+ return true;+ });+ assert.ok(lines.some(line => line.includes('403') && line.includes('x-ratelimit-remaining=0') && line.includes('x-ratelimit-reset=' + reset)), lines.join('\n'));++ fixture.state.installed = false;+ const real = createGitHubClient({ ...fixture.config, log: line => lines.push(line) });+ await assert.rejects(real.access(user, 'fixture/repository'), { code: 'NO_REPO_ACCESS' });+ assert.ok(lines.some(line => line.includes('404')), lines.join('\n'));+});