fix: parse sdk retry wait env strictly

This commit is contained in:
Peter Steinberger
2026-05-28 13:36:30 -04:00
parent bbc9a7d3fa
commit f90e266416
2 changed files with 26 additions and 0 deletions

View File

@@ -1111,6 +1111,27 @@ describe("buildGuardedModelFetch", () => {
expect(response.headers.get("x-should-retry")).toBeNull();
});
it.each(["0x10", "1e3"])(
"ignores non-decimal OPENCLAW_SDK_RETRY_MAX_WAIT_SECONDS values: %s",
async (value) => {
process.env.OPENCLAW_SDK_RETRY_MAX_WAIT_SECONDS = value;
fetchWithSsrFGuardMock.mockResolvedValue({
response: new Response(null, {
status: 429,
headers: { "retry-after": "30" },
}),
finalUrl: "https://api.anthropic.com/v1/messages",
release: vi.fn(async () => undefined),
});
const response = await buildGuardedModelFetch(anthropicModel)(
"https://api.anthropic.com/v1/messages",
{ method: "POST" },
);
expect(response.headers.get("x-should-retry")).toBeNull();
},
);
it("injects x-should-retry:false for terminal 429 responses without retry-after", async () => {
fetchWithSsrFGuardMock.mockResolvedValue({
response: new Response("Sorry, you've exceeded your weekly rate limit.", {

View File

@@ -33,6 +33,7 @@ import {
const DEFAULT_MAX_SDK_RETRY_WAIT_SECONDS = 60;
const log = createSubsystemLogger("provider-transport-fetch");
const BLOCKED_EXACT_ORIGIN_TRUST_HOSTNAME_LABELS = new Set(["instance-data"]);
const PLAIN_DECIMAL_NUMBER_RE = /^\d+(?:\.\d+)?$/;
function hasReadableSseData(block: string): boolean {
const dataLines = block
@@ -269,6 +270,10 @@ function resolveMaxSdkRetryWaitSeconds(): number | undefined {
return undefined;
}
if (!PLAIN_DECIMAL_NUMBER_RE.test(raw)) {
return DEFAULT_MAX_SDK_RETRY_WAIT_SECONDS;
}
const seconds = Number(raw);
if (Number.isFinite(seconds) && seconds > 0) {
return seconds;