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 = [