Repository navigation
fix(context-edit): dynamic whole-body budget + reactive 413 retry (fixes #52) - #54
Merged
Merged
Conversation
…sue #52 follow-up) Issue #52: the 413 returns when a screenshot-heavy session ALSO carries a large conversation. The 413 is on the WHOLE request body, but context editing budgeted only image bytes against a fixed 3.5MB cap — so a ~700k-token transcript (~2.7MB text) + 3 kept screenshots (~3.15MB) still exceeded the 5 MiB gateway wall. Fix 1 — dynamic budget: the image allowance is now GATEWAY_ENTITY_LIMIT (5 MiB) − SAFETY_MARGIN − nonImageBytes, capped by the fixed budget for the common small-text case. Big text auto-clears more screenshots. Verified on issue #52's exact 9 images: 343k text -> keep 3 (4.46MB), 500k -> keep 2, 700k -> keep 1 (3.79MB), 900k -> keep 0 — always under the wall. Fix 2 — reactive fallback: if the gateway STILL 413s, force-clear every screenshot and retry once instead of relaying a 502. Streaming retries on the first-pull 413 (before any content_block is written); non-stream wraps complete(). Tests: dynamic budget across text sizes, forceClearAllScreenshots, is413Error, end-to-end reactive-retry through the Express app (stream + non-stream), http-e2e big-text-fits-5MiB. 604 unit green; docker http-e2e ALL PASSED (68); tsc clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…namic budget) The live CLI can't simultaneously hit the 5 MiB byte wall AND stay a valid <1M-token request: base64 images tokenize at ~char/4, so a ~5 MiB body is ~1.3M tokens, over the model window. The byte-budget math is therefore asserted hermetically (http-e2e, quota-free) and case #16 stresses a big screenshot pile on its own. Case #17 proves the live consequence: a substantial mixed conversation (~100k tokens prose + a screenshot pile) is a valid turn — no 413, newest screenshot still read. Sized to ~591k tokens after edit, comfortably under the 1M window. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Owner
Author
Real CLI e2e — verified against live CopilotBoth context-editing cases PASS: The same run shows the 5 pre-existing FAILs documented against PR #48 (codex tool loop, 2× vision OCR Pre-merge CLI gate: PASS. Note on the CLI byte-wall limitThe pure 5 MiB byte-wall extreme isn't reachable as a live CLI turn: base64 images tokenize at ~char/4, so a ~5 MiB body is ~1.3M tokens — over the 1M model window. So the byte-budget math is asserted hermetically (http-e2e, quota-free), case #16 stresses a big screenshot pile, and case #17 proves the live consequence with a valid mixed-content turn. |
…it-dynamic-budget # Conflicts: # e2e/RESULTS.md # src/worker/anthropic-server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #52.
The 413 still fires — because the budget only counted images
Issue #52 showed the v0.16.x context-editing fix still 413s when a screenshot-heavy session ALSO carries a large conversation. The exact repro: 9 tool-result screenshots (~8.9MB) plus ~343k cache-read tokens of conversation text. After clearing to 3 screenshots (~5.3MB with the old 6MB budget; ~3.15MB with the current 3.5MB budget), the images fit — but the 413 is on the whole HTTP body, and images + conversation text together still overflowed Copilot's 5 MiB gateway wall.
The budget counted only
byteLen(images). A fixed image budget is blind to the text.Fix 1 — dynamic, whole-body budget
The image allowance is now computed per request:
capped by the fixed 3.5MB for the common small-text case. Big text automatically clears more screenshots. Verified on issue #52's exact 9 images across conversation sizes:
Fix 2 — reactive 413 fallback (issue's suggestion #3)
If the gateway still returns 413 (limit shifted, or content we don't byte-count tips it over), the worker force-clears every screenshot and retries once before surfacing the error — instead of relaying a 502. Streaming retries on the first-pull 413 (before any
content_blockis written); non-stream wraps thecomplete()call. Both are covered by end-to-end tests through the real Express app.Tests
tests/core/context-edit.test.ts— dynamic budget shrinks with text, backward-compat (small text untouched),forceClearAllScreenshots,is413Error.tests/worker/anthropic-server.test.ts— reactive retry for both stream and non-stream: provider throws 413 on the first call, the retry body has its screenshots cleared, response is 200 not 502.count_tokenslands under 5 MiB (the dynamic property, quota-free).Gates: 604 unit green · docker http-e2e ALL PASSED (68) ·
tscclean. Real-CLI result posted below once the live run finishes.Note on scope: the reactive 413 retry is wired on the Anthropic path (what Claude Code / issue #52 uses). The dynamic budget protects the OpenAI/Codex path too, since it lives in
editImageContextInPlace.🤖 Generated with Claude Code