Skip to content

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

Merged
nexxeln merged 4 commits intoanomalyco:devfrom
bentrd:fix/413-overflow-recovery
Mar 2, 2026
Merged

fix: recover from 413 Request Entity Too Large via auto-compaction#14707
nexxeln merged 4 commits intoanomalyco:devfrom
bentrd:fix/413-overflow-recovery

Conversation

@bentrd
Copy link
Contributor

@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. 👍

- When overflow triggers compaction, exclude the large message from
  summarization and replay it after, so the model can actually answer it
- Skip the isOverflow check during compaction itself — it was treating
  the compaction's own token usage as another overflow, killing the
  successful summary
- Don't let a failed compaction act as a history boundary — only
  successful ones should truncate prior context
@noamzbr
Copy link
Contributor

noamzbr commented Feb 26, 2026

Hi @bentrd, great work! I tested it on a couple session we have and found some additional cases worth addressing IMO.
Opened as a PR here - bentrd#1

When the overflow-causing message has large images attached, replaying
it raw would just overflow again. Now we replace media parts with text
placeholders before replay, same as toModelMessages does with stripMedia.

Also extracted a shared isMedia() helper to avoid duplicating the
image/pdf check in three places.
@rsteenwyk
Copy link

Hoping this can get merged soon/reviewed by a maintainer. The Copilot Pro service is definitely more aggressive with their 413's compared to OpenAI/Anthropic and it kills your session with no easy way to continue outside of reverting.

Copy link
Member

@nexxeln nexxeln left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty!

@nexxeln nexxeln merged commit be20f86 into anomalyco:dev Mar 2, 2026
9 checks passed
Grappeggia pushed a commit to teamslop/slopcode that referenced this pull request Mar 4, 2026
@timwkosatec
Copy link

#16882 this PR possibly broke the auto compation settings, see related issue

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

5 participants