Auto-reset Claude session on context-window overflow (prompt too long)

A resumed Claude session that grew past the model's context limit failed
every turn with "Prompt is too long" — even auto-compaction could not
shrink it — so the same oversized session was reused indefinitely and the
channel looped forever on that error (no Claude reset pattern matched,
unlike Codex). Add "prompt is too long" / context-limit patterns to
SESSION_RESET_PATTERNS so the poisoned session is cleared and the next
turn starts fresh.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-08-25 14:23:41 +09:00
parent e81357e1df
commit 7b27167af6
2 changed files with 26 additions and 0 deletions

View File

@@ -60,6 +60,26 @@ describe('shouldResetSessionOnAgentFailure', () => {
}), }),
).toBe(true); ).toBe(true);
}); });
it('resets the session on context-window overflow (prompt too long)', () => {
expect(
shouldResetSessionOnAgentFailure({
result: null,
error:
'API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"prompt is too long: 215000 tokens > 200000 maximum"}}',
}),
).toBe(true);
});
it('resets the session when max_tokens exceeds the context limit', () => {
expect(
shouldResetSessionOnAgentFailure({
result:
'input length and `max_tokens` exceed context limit: 190000 + 20000 > 200000',
error: undefined,
}),
).toBe(true);
});
}); });
describe('shouldRetryFreshSessionOnAgentFailure', () => { describe('shouldRetryFreshSessionOnAgentFailure', () => {

View File

@@ -9,6 +9,12 @@ const SESSION_RESET_PATTERNS = [
/400[^\n]*thinking/i, /400[^\n]*thinking/i,
/thinking[^\n]*block[^\n]*(?:invalid|error|signature)/i, /thinking[^\n]*block[^\n]*(?:invalid|error|signature)/i,
/invalid[^\n]*signature[^\n]*thinking/i, /invalid[^\n]*signature[^\n]*thinking/i,
// Context-window overflow: the resumed session has grown past the model's
// limit, so even auto-compaction fails with "Prompt is too long". Without
// this, the same oversized session is reused every turn and the channel
// loops forever on the same error. Drop it so the next turn starts fresh.
/prompt is too long/i,
/input length and .*exceed context limit/i,
]; ];
const SESSION_RETRY_PATTERNS = [ const SESSION_RETRY_PATTERNS = [