fix(api): validate retry count environment values (#2172)

This commit is contained in:
Ricardo-M-L
2026-10-12 09:21:42 +08:00
committed by GitHub
parent afa71563b6
commit 76b12a64fc
2 changed files with 50 additions and 4 deletions
+46 -2
View File
@@ -149,10 +149,54 @@ describe('retry configuration', () => {
expect(getDefaultMaxRetries()).toBe(3)
})
test('falls back to default retry attempts for invalid values', async () => {
process.env.OPENCLAUDE_MAX_RETRIES = 'nope'
test('falls back to default retry attempts for malformed values', async () => {
const { getDefaultMaxRetries } = await importFreshWithRetryModule()
const malformedValues = [
'nope',
' ',
'5abc',
'2.5',
'-1',
'Infinity',
'9'.repeat(400),
]
for (const value of malformedValues) {
process.env.OPENCLAUDE_MAX_RETRIES = value
expect(getDefaultMaxRetries()).toBe(10)
}
})
test('uses default retries for a whitespace-only legacy setting', async () => {
process.env.CLAUDE_CODE_MAX_RETRIES = ' '
process.env.OPENCLAUDE_RETRY_DELAY_MS = '1'
const { getDefaultMaxRetries, withRetry } =
await importFreshWithRetryModule()
const retryableError = APIError.generate(
500,
undefined,
'internal server error',
new Headers(),
)
let attempts = 0
expect(getDefaultMaxRetries()).toBe(10)
const result = await drainAsyncGenerator(
withRetry(
async () => ({} as Anthropic),
async () => {
attempts++
if (attempts === 1) throw retryableError
return { ok: true }
},
{
model: 'test-model',
thinkingConfig: { type: 'disabled' },
},
),
)
expect(result).toEqual({ ok: true })
expect(attempts).toBe(2)
})
test('caps retry attempts to a bounded value', async () => {
+4 -2
View File
@@ -1014,8 +1014,10 @@ function validateRetryAttemptsEnvVar(
if (!value) {
return DEFAULT_MAX_RETRIES
}
const parsed = parseInt(value, 10)
if (isNaN(parsed) || parsed < 0) {
const normalized = value.trim()
const isDecimalInteger = /^\d+$/.test(normalized)
const parsed = isDecimalInteger ? Number(normalized) : Number.NaN
if (!Number.isFinite(parsed)) {
logForDebugging(
`${envVarName} Invalid value "${value}" (using default: ${DEFAULT_MAX_RETRIES})`,
)