From 2b7a33332043a061df20434b4f0bd82c80343bf6 Mon Sep 17 00:00:00 2001 From: Type-Delta Date: Tue, 24 Nov 2026 17:05:37 +0700 Subject: [PATCH] fix(server): keep Claude gateway reset times for automatic resume Claude gateway 429 responses can put their cooldown after a quoted upstream error. Parse the gateway countdown on either side of that error, including when the gateway names the routing provider. Implemented with GPT-6.2 Sol through Codex in T3 Code. --- FORK.md | 4 +++ .../Adapters/ClaudeAdapterV2.test.ts | 20 +++++++++++++- .../Adapters/ClaudeAdapterV2.ts | 27 ++++++++++++------- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/FORK.md b/FORK.md index 6ae517de06505..684fc61b4302c 100644 --- a/FORK.md +++ b/FORK.md @@ -579,6 +579,10 @@ passed, the thread is still eligible, and its persisted run/message identity is scheduler's tick therefore adds at most five seconds of timing slack, while the provider reset time controls the earliest continuation. +Claude gateway 429 responses can put their cooldown after a nested upstream error and include +the provider name in the message. The adapter reads the gateway's countdown in either position +and leaves any countdown quoted inside the upstream error out of the recovery time. + The fork retains its explicit continuation prompt, which asks the provider to resume only incomplete work, inspect the current workspace and subagent state, repair partial operations, and avoid repeating completed work or expanding scope. Recovery survives a server restart, as upstream now supports. An explicit thread restart, archive, settle, a newer turn, or a changed provider selection invalidates the saved recovery. The one-hour wait and scheduler behavior are diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index befd741dcc10c..619b8840b9f04 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -10045,6 +10045,24 @@ describe("ClaudeAdapterV2 background wake turns", () => { it.effect.each([ { cooldown: " (reset after 1h2m3s)", seconds: 3723 }, { cooldown: " (reset after 4m 6s) (last error: rate limited)", seconds: 246 }, + { + cooldown: + " via provider claude (last error: rate_limit_error: Please try again later.) (reset after 1h39m47s)", + seconds: 5987, + }, + { + cooldown: " via provider claude (reset after 1m) (last error: rate limited (reset after 1h))", + seconds: 60, + }, + { + cooldown: " (last error: rate limited (reset after 1h)) (reset after 1m)", + seconds: 60, + description: "Server is temporarily limiting requests (not your usage limit)", + }, + { + cooldown: " via provider claude (last error: rate limited (reset after 1h))", + seconds: 0, + }, { cooldown: "", seconds: 0 }, ])("keeps a gateway's reset on a direct 429 with cooldown '$cooldown'", (input) => Effect.gen(function* () { @@ -10061,7 +10079,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { }), ); // An API-key session reports a gateway 429 only as text, with no rate_limit_info. - const result = `API Error: Request rejected (429) · All credentials for model claude-opus-5-5 are cooling down${input.cooldown}`; + const result = `API Error: ${input.description ?? "Request rejected (429)"} · All credentials for model claude-opus-5-5 are cooling down${input.cooldown}`; yield* Queue.offerAll(harness.sdkMessages, [ makeAssistantErrorFrame({ uuid: "00000000-0000-4000-8000-000000000654", diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 342d80d8a5566..724a884d1a33c 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -4860,6 +4860,22 @@ function gatewayCooldownResetAt( : undefined; } +function gateway429Cooldown(result: string) { + const proxyHeader = + /^API Error: (?:Request rejected \(429\)|Server is temporarily limiting requests \(not your usage limit\)) · All credentials for model (?:requested model|[^\s()]+) are cooling down(?: via provider [^\s()]+)?(?: |$)/.exec( + result, + ); + if (proxyHeader === null) + return /\(reset after (?:(\d+)h ?)?(?:(\d+)m ?)?(?:(\d+)s)?\)/.exec(result); + + // The proxy appends its countdown before or after the quoted upstream error. + const details = result.slice(proxyHeader[0].length); + return ( + /^\(reset after (?:(\d+)h ?)?(?:(\d+)m ?)?(?:(\d+)s)?\)/.exec(details) ?? + /^\(last error: [\s\S]*\) \(reset after (?:(\d+)h ?)?(?:(\d+)m ?)?(?:(\d+)s)?\)$/.exec(details) + ); +} + const CLAUDE_UNSUPPORTED_VERSION_ERROR = /^API Error: 400 Claude Code (\d+\.\d+\.\d+) does not support this model; version (\d+\.\d+\.\d+) or newer is required\./; @@ -4937,16 +4953,7 @@ function providerFailureFromResult( // API-key sessions get no rate_limit_info, so a gateway's 429 carries its reset only as text. const resetAt = apiErrorStatus === 429 && message.result.startsWith("API Error:") - ? gatewayCooldownResetAt( - // CLIProxyAPI nests upstream errors in its message; only its own cooldown counts. - (message.result.startsWith( - "API Error: Request rejected (429) · All credentials for model ", - ) - ? /^API Error: Request rejected \(429\) · All credentials for model [^\s()]+ are cooling down \(reset after (?:(\d+)h ?)?(?:(\d+)m ?)?(?:(\d+)s)?\)/ - : /\(reset after (?:(\d+)h ?)?(?:(\d+)m ?)?(?:(\d+)s)?\)/ - ).exec(message.result), - receivedAt, - ) + ? gatewayCooldownResetAt(gateway429Cooldown(message.result), receivedAt) : undefined; return makeProviderFailure({ message: listedError ?? structuredError ?? failureHint ?? message.result,