fix: detect context_overflow error for auto-compaction

Fixes #3154

Users hit context overflow errors that don't trigger auto-compaction because
isContextOverflowError() only checks for "context overflow" (space), but some
providers return "context_overflow" (underscore). Without this fix, the error
is displayed to the user instead of automatically triggering session compaction.

Changes:
- Add context_overflow (underscore) pattern to isContextOverflowError()
- Maintain backward compatibility with existing "context overflow" (space)
- Add test coverage for underscore format with 3 sample error messages
- Both patterns now trigger auto-compaction as configured

Reviewed-by: Codex (threadId: 019c036c-d6b7-7281-a654-93a2f1776997)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
chenglun.hu 2026-01-28 15:09:00 +08:00
parent 6044bf3637
commit 53cde99755
No known key found for this signature in database
GPG Key ID: 11ECC6E33B83267C
2 changed files with 15 additions and 0 deletions

View File

@ -40,6 +40,20 @@ describe("isContextOverflowError", () => {
}
});
it("matches 'context_overflow' with underscore format", () => {
// Issue #3154: Some providers return "context_overflow" (underscore) instead of
// "context overflow" (space). Without this fix, auto-compaction is NOT triggered.
// The user sees the error instead of automatic compaction + retry.
const samples = [
"context_overflow: prompt too large for the model",
"Context_Overflow: request exceeds limit",
"ERROR: context_overflow detected",
];
for (const sample of samples) {
expect(isContextOverflowError(sample)).toBe(true);
}
});
it("ignores unrelated errors", () => {
expect(isContextOverflowError("rate limit exceeded")).toBe(false);
expect(isContextOverflowError("request size exceeds upload limit")).toBe(false);

View File

@ -21,6 +21,7 @@ export function isContextOverflowError(errorMessage?: string): boolean {
lower.includes("exceeds model context window") ||
(hasRequestSizeExceeds && hasContextWindow) ||
lower.includes("context overflow") ||
lower.includes("context_overflow") ||
(lower.includes("413") && lower.includes("too large"))
);
}