diff --git a/src/hooks/todo-continuation-enforcer/token-limit-detection.ts b/src/hooks/todo-continuation-enforcer/token-limit-detection.ts index 25a2fad3d..366ac245f 100644 --- a/src/hooks/todo-continuation-enforcer/token-limit-detection.ts +++ b/src/hooks/todo-continuation-enforcer/token-limit-detection.ts @@ -1,8 +1,6 @@ -const TOKEN_LIMIT_ERROR_NAMES = new Set([ - "contextlengtherror", -]) +import { isRetryableModelError } from "../../shared/model-error-classifier" -const TOKEN_LIMIT_KEYWORDS = [ +const TOKEN_LIMIT_FALLBACK_PATTERNS = [ "prompt is too long", "is too long", "context_length_exceeded", @@ -11,16 +9,29 @@ const TOKEN_LIMIT_KEYWORDS = [ "too many tokens", ] +const TOKEN_LIMIT_ERROR_NAMES = new Set([ + "contextlengtherror", + "context_length_exceeded", +]) + export function isTokenLimitError(error: { name?: string; message?: string } | undefined): boolean { if (!error) return false - if (error.name && TOKEN_LIMIT_ERROR_NAMES.has(error.name.toLowerCase())) { - return true + const isRetryable = isRetryableModelError({ + name: error.name, + message: error.message, + }) + + if (!isRetryable && error.name) { + const errorNameLower = error.name.toLowerCase() + if (TOKEN_LIMIT_ERROR_NAMES.has(errorNameLower)) { + return true + } } if (error.message) { const lower = error.message.toLowerCase() - return TOKEN_LIMIT_KEYWORDS.some((keyword) => lower.includes(keyword)) + return TOKEN_LIMIT_FALLBACK_PATTERNS.some((pattern) => lower.includes(pattern)) } return false