File tree Expand file tree Collapse file tree 1 file changed +18
-2
lines changed
services/minecraft/src/cognitive/conscious Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Original file line number Diff line number Diff 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
6273interface 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
You can’t perform that action at this time.
0 commit comments