From 7b27167af680ed798315576f75063eff284a2f7c Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 25 Aug 2026 14:23:41 +0900 Subject: [PATCH] Auto-reset Claude session on context-window overflow (prompt too long) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/session-recovery.test.ts | 20 ++++++++++++++++++++ src/session-recovery.ts | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/src/session-recovery.test.ts b/src/session-recovery.test.ts index 24e9fa8..494b77e 100644 --- a/src/session-recovery.test.ts +++ b/src/session-recovery.test.ts @@ -60,6 +60,26 @@ describe('shouldResetSessionOnAgentFailure', () => { }), ).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', () => { diff --git a/src/session-recovery.ts b/src/session-recovery.ts index 72e6abb..62bdbda 100644 --- a/src/session-recovery.ts +++ b/src/session-recovery.ts @@ -9,6 +9,12 @@ const SESSION_RESET_PATTERNS = [ /400[^\n]*thinking/i, /thinking[^\n]*block[^\n]*(?:invalid|error|signature)/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 = [