Skip to content

Comments

fix: recover from 413 Request Entity Too Large via auto-compaction#14707

Open
bentrd wants to merge 1 commit intoanomalyco:devfrom
bentrd:fix/413-overflow-recovery
Open

fix: recover from 413 Request Entity Too Large via auto-compaction#14707
bentrd wants to merge 1 commit intoanomalyco:devfrom
bentrd:fix/413-overflow-recovery

Conversation

@bentrd
Copy link

@bentrd bentrd commented Feb 22, 2026

Issue for this PR

Closes #6574

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

  1. Recognize 413 as context overflow (provider/error.ts)
  • Added /request entity too large/i to OVERFLOW_PATTERNS
  • Added statusCode === 413 catch-all in parseAPICallError()
  1. Trigger auto-compaction on overflow (session/processor.ts)
  • ContextOverflowErrorneedsCompaction = true, publish error notification
  • Wrapped retry/error-assignment in else block so overflow falls through to cleanup → return "compact"
  1. Strip media from compaction (session/message-v2.ts, session/compaction.ts)
  • Added options?: { stripMedia?: boolean } to toModelMessages()
  • Image/PDF file parts → [Attached mime: filename] text placeholders
  • Tool result attachments → []
  • Compaction calls toModelMessages(msgs, model, { stripMedia: true })
  1. Handle compaction overflow (session/compaction.ts)
  • If compaction processor returns "compact", sets ContextOverflowError on message and returns "stop"
  1. Overflow-aware continue message (session/compaction.ts, session/prompt.ts)
  • Threads overflow flag through compaction flow
  • When overflow-triggered, prepends context about stripped media to the continue message
  • Prevents the model from incorrectly saying "I can't analyze images" when images were just too large

How did you verify your code works?

Attached 3 large 4K images → 413 → notification shown → compaction triggered with stripped media → model correctly explains images were too large.

Screenshots / recordings

Screen.Recording.2026-02-22.at.19.23.26.mov

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

When large image attachments cause a 413 error, the session would enter
an infinite retry loop with no recovery path. This fixes four issues:

1. Recognize 413 as context overflow (error.ts)
   - Add /request entity too large/i pattern
   - Add statusCode === 413 check in parseAPICallError

2. Trigger auto-compaction on context overflow (processor.ts)
   - Replace empty TODO with needsCompaction = true
   - Fix break-vs-fallthrough bug that caused return undefined
     instead of return compact (root cause of infinite loop)
   - Publish error notification so user sees what happened

3. Strip media from compaction requests (message-v2.ts, compaction.ts)
   - Add stripMedia option to toModelMessages()
   - Replace image/PDF file parts with text placeholders
   - Strip tool result attachments
   - Prevents compaction from hitting the same size limit

4. Handle compaction-itself-overflows (compaction.ts)
   - When compaction also returns 413, set error and return stop
   - Prevents infinite compaction loop

Additionally, thread an overflow flag through compaction so the
post-compaction continue message explains that media was removed
due to size limits, preventing the model from incorrectly claiming
it lacks vision capabilities.
@github-actions github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Feb 22, 2026
@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found several related PRs that address context overflow and compaction issues:

  1. PR fix(session): prevent context overflow by adding safety margin to compaction check #6562 - "fix(session): prevent context overflow by adding safety margin to compaction check"

    • Related to overflow prevention in compaction
  2. PR feat(opencode): add context overflow prevention with configurable thresholds #8810 - "feat(opencode): add context overflow prevention with configurable thresholds"

    • Handles context overflow detection and thresholds
  3. PR feat(opencode): trigger compaction earlier and add multi-file read #9656 - "feat(opencode): trigger compaction earlier and add multi-file read"

    • Compaction triggering logic
  4. PR fix: auto-resume agent loop after context compaction #12970 - "fix: auto-resume agent loop after context compaction"

    • Addresses loop continuation after compaction
  5. PR fix: compaction bugs #13946 and #13980 #14245 - "fix: compaction bugs Bug: opencode run exits after compaction when compaction model's token usage exceeds overflow threshold #13946 and Bug: compaction.reserved config is ignored for models without explicit 'input' limit #13980"

    • Recent compaction bug fixes

These PRs overlap with the current PR #14707 in addressing context overflow, compaction mechanisms, and loop recovery. You may want to review them to ensure this PR complements rather than conflicts with existing solutions.

@github-actions github-actions bot removed needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Feb 22, 2026
@github-actions
Copy link
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request entity too large error

1 participant