Skip to content

Commit b237ac8

Browse files
shinohara-rinnekomeowww
authored andcommitted
feat(minecraft): add rate limit detection and backoff to brain retry logic
Add isRateLimitError helper to detect 429 status and rate limit messages, implement 500ms sleep on rate limit errors before retry
1 parent 62f1657 commit b237ac8

File tree

1 file changed

+18
-2
lines changed
  • services/minecraft/src/cognitive/conscious

1 file changed

+18
-2
lines changed

services/minecraft/src/cognitive/conscious/brain.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ function isLikelyAuthOrBadArgError(err: unknown): boolean {
5858
)
5959
}
6060

61+
function isRateLimitError(err: unknown): boolean {
62+
const status = getErrorStatus(err)
63+
if (status === 429) return true
64+
const msg = toErrorMessage(err).toLowerCase()
65+
return msg.includes('rate limit') || msg.includes('too many requests')
66+
}
67+
68+
function sleep(ms: number): Promise<void> {
69+
return new Promise(resolve => setTimeout(resolve, ms))
70+
}
71+
6172

6273
interface BrainResponse {
6374
action: ActionInstruction & { id?: string }
@@ -257,13 +268,18 @@ export class Brain {
257268
break // Success, exit retry loop
258269
} catch (err) {
259270
const remaining = maxAttempts - attempt
271+
const isRateLimit = isRateLimitError(err)
260272
const shouldRetry = remaining > 0 && !isLikelyAuthOrBadArgError(err)
261-
this.deps.logger.withError(err).error(`Brain: Decision attempt failed (attempt ${attempt}/${maxAttempts}, retry: ${shouldRetry})`)
273+
this.deps.logger.withError(err).error(`Brain: Decision attempt failed (attempt ${attempt}/${maxAttempts}, retry: ${shouldRetry}, rateLimit: ${isRateLimit})`)
262274

263275
if (!shouldRetry) {
264276
throw err // Re-throw if we can't retry
265277
}
266-
// Otherwise continue to next attempt
278+
279+
// Backoff on rate limit (429)
280+
if (isRateLimit) {
281+
await sleep(500)
282+
}
267283
}
268284
}
269285

0 commit comments

Comments
 (0)