Skip to content

Commit 5fb8989

Browse files
author
Gio Della-Libera
committed
fix(router): bound App-token fallback per run
Allow at most one public-read recovery for each target App credential in a process. Repeated public throttles fail closed while later privileged mutations retain their configured App-token path. Co-authored-by: Copilot <[email protected]> Copilot-Session: 3cdcdb00-ade8-4e61-85a7-8151b35f216a
1 parent d683b60 commit 5fb8989

2 files changed

Lines changed: 45 additions & 12 deletions

File tree

‎src/repair/github-cli.ts‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ export type GhRetryOptions = GhRunOptions & {
2121
attempts?: number;
2222
};
2323

24+
type PublicReadFallback = {
25+
appToken: string;
26+
options: GhRunOptions;
27+
};
28+
29+
const claimedPublicReadFallbackTokens = new Set<string>();
30+
2431
export function ghJson<T = JsonValue>(ghArgs: string[], options: GhRunOptions = {}): T {
2532
return JSON.parse(ghText(ghArgs, options) || "null") as T;
2633
}
@@ -170,17 +177,17 @@ export function ghTextWithRetry(ghArgs: string[], options: GhRetryOptions | numb
170177
const resolved = resolveRetryOptions(options);
171178
const attempts = Math.max(1, resolved.attempts ?? 6);
172179
let activeOptions: GhRunOptions = resolved;
173-
let publicReadFallback = publicReadFallbackOptions(ghArgs, resolved);
180+
const publicReadFallback = publicReadFallbackOptions(ghArgs, resolved);
174181
let lastError: unknown;
175182
for (let attempt = 1; attempt <= attempts; attempt += 1) {
176183
try {
177184
return ghText(ghArgs, activeOptions);
178185
} catch (error) {
179186
lastError = error;
180187
const retryKind = ghRetryKind(error);
181-
if (retryKind === "throttle" && publicReadFallback) {
182-
const fallback = publicReadFallback;
183-
publicReadFallback = null;
188+
const fallback =
189+
retryKind === "throttle" ? claimPublicReadFallback(publicReadFallback) : null;
190+
if (retryKind === "throttle" && fallback) {
184191
activeOptions = fallback;
185192
try {
186193
return ghText(ghArgs, fallback);
@@ -210,17 +217,17 @@ export async function ghTextWithRetryAsync(
210217
const resolved = resolveRetryOptions(options);
211218
const attempts = Math.max(1, resolved.attempts ?? 6);
212219
let activeOptions: GhRunOptions = resolved;
213-
let publicReadFallback = publicReadFallbackOptions(ghArgs, resolved);
220+
const publicReadFallback = publicReadFallbackOptions(ghArgs, resolved);
214221
let lastError: unknown;
215222
for (let attempt = 1; attempt <= attempts; attempt += 1) {
216223
try {
217224
return await ghTextAsync(ghArgs, activeOptions);
218225
} catch (error) {
219226
lastError = error;
220227
const retryKind = ghRetryKind(error);
221-
if (retryKind === "throttle" && publicReadFallback) {
222-
const fallback = publicReadFallback;
223-
publicReadFallback = null;
228+
const fallback =
229+
retryKind === "throttle" ? claimPublicReadFallback(publicReadFallback) : null;
230+
if (retryKind === "throttle" && fallback) {
224231
activeOptions = fallback;
225232
try {
226233
return await ghTextAsync(ghArgs, fallback);
@@ -322,14 +329,23 @@ function publicReadToken(
322329
function publicReadFallbackOptions(
323330
ghArgs: readonly string[],
324331
options: GhRunOptions,
325-
): GhRunOptions | null {
332+
): PublicReadFallback | null {
326333
const overrides = options.env ?? {};
327334
const publicToken = publicReadToken(ghArgs, options);
328335
const appToken = process.env.GH_TOKEN?.trim();
329336
if (!publicToken || !appToken || publicToken === appToken) {
330337
return null;
331338
}
332-
return { ...options, env: { ...overrides, GH_TOKEN: appToken } };
339+
return {
340+
appToken,
341+
options: { ...options, env: { ...overrides, GH_TOKEN: appToken } },
342+
};
343+
}
344+
345+
function claimPublicReadFallback(fallback: PublicReadFallback | null): GhRunOptions | null {
346+
if (!fallback || claimedPublicReadFallbackTokens.has(fallback.appToken)) return null;
347+
claimedPublicReadFallbackTokens.add(fallback.appToken);
348+
return fallback.options;
333349
}
334350

335351
export function ghErrorText(error: unknown): string {

‎test/repair/github-cli.test.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ test("public read throttles fall back once to the target App token", async () =>
139139
GH_BIN_ARGS: JSON.stringify([
140140
"--eval",
141141
[
142-
'if (process.env.GH_TOKEN === "public-read-token") {',
142+
'if (process.env.GH_TOKEN?.startsWith("public-read-token")) {',
143143
' process.stderr.write("gh: API rate limit exceeded for installation (HTTP 403)\\n");',
144144
" process.exit(1);",
145145
"}",
@@ -158,7 +158,24 @@ test("public read throttles fall back once to the target App token", async () =>
158158
try {
159159
const args = ["api", "repos/openclaw/openclaw/issues/123"];
160160
assert.equal(ghJsonWithRetry<{ token: string }>(args).token, "app-mutation-token");
161-
assert.equal((await ghJsonWithRetryAsync<{ token: string }>(args)).token, "app-mutation-token");
161+
assert.throws(() => ghJsonWithRetry(args), { name: "GitHubRateLimitError" });
162+
163+
process.env.GH_TOKEN = "app-mutation-token-async";
164+
process.env.CLAWSWEEPER_PUBLIC_GH_TOKEN = "public-read-token-async";
165+
assert.equal(
166+
(await ghJsonWithRetryAsync<{ token: string }>(args)).token,
167+
"app-mutation-token-async",
168+
);
169+
170+
process.env.GH_TOKEN = "app-mutation-token";
171+
process.env.CLAWSWEEPER_PUBLIC_GH_TOKEN = "public-read-token";
172+
assert.equal(
173+
ghJsonWithRetry<{ token: string }>(
174+
["api", "--method", "POST", "repos/openclaw/openclaw/issues/123/labels"],
175+
{ input: "{}" },
176+
).token,
177+
"app-mutation-token",
178+
);
162179
} finally {
163180
for (const [key, value] of Object.entries(previous)) {
164181
if (value === undefined) delete process.env[key];

0 commit comments

Comments
 (0)