fix(token-limit-recovery): exclude thinking block errors from token limit detection

- Removed 'invalid_request_error' from TOKEN_LIMIT_KEYWORDS (too broad)
- Added THINKING_BLOCK_ERROR_PATTERNS to explicitly detect thinking block structure errors
- Added isThinkingBlockError() function to filter these out before token limit checks
- Prevents 'thinking block order' errors from triggering compaction instead of session-recovery

🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
YeonGyu-Kim
2026-01-02 20:28:37 +09:00
parent 823f12d88d
commit 7a896fd2b9

View File

@@ -26,9 +26,23 @@ const TOKEN_LIMIT_KEYWORDS = [
"context length", "context length",
"too many tokens", "too many tokens",
"non-empty content", "non-empty content",
"invalid_request_error",
] ]
// Patterns that indicate thinking block structure errors (NOT token limit errors)
// These should be handled by session-recovery hook, not compaction
const THINKING_BLOCK_ERROR_PATTERNS = [
/thinking.*first block/i,
/first block.*thinking/i,
/must.*start.*thinking/i,
/thinking.*redacted_thinking/i,
/expected.*thinking.*found/i,
/thinking.*disabled.*cannot.*contain/i,
]
function isThinkingBlockError(text: string): boolean {
return THINKING_BLOCK_ERROR_PATTERNS.some((pattern) => pattern.test(text))
}
const MESSAGE_INDEX_PATTERN = /messages\.(\d+)/ const MESSAGE_INDEX_PATTERN = /messages\.(\d+)/
function extractTokensFromMessage(message: string): { current: number; max: number } | null { function extractTokensFromMessage(message: string): { current: number; max: number } | null {
@@ -52,6 +66,9 @@ function extractMessageIndex(text: string): number | undefined {
} }
function isTokenLimitError(text: string): boolean { function isTokenLimitError(text: string): boolean {
if (isThinkingBlockError(text)) {
return false
}
const lower = text.toLowerCase() const lower = text.toLowerCase()
return TOKEN_LIMIT_KEYWORDS.some((kw) => lower.includes(kw.toLowerCase())) return TOKEN_LIMIT_KEYWORDS.some((kw) => lower.includes(kw.toLowerCase()))
} }