Skip to content

fix(context-edit): dynamic whole-body budget + reactive 413 retry (fixes #52) - #54

Merged
wangcansunking merged 3 commits into
masterfrom
worktree-context-edit-dynamic-budget
Jul 3, 2026
Merged

wangcansunking merged 3 commits into
masterfrom
worktree-context-edit-dynamic-budget

Conversation

@wangcansunking

Copy link
Copy Markdown
Owner

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:

imageAllowance = GATEWAY_ENTITY_LIMIT (5 MiB) − SAFETY_MARGIN (512K) − nonImageBytes(messages)

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:

conversation text screenshots kept total body under 5 MiB?
343k tok (the issue) 3 4.46MB ✅
500k tok 2 4.04MB ✅
700k tok 1 3.79MB ✅
900k tok 0 3.43MB ✅

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_block is written); non-stream wraps the complete() 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.
  • http-e2e — big-text + screenshots count_tokens lands under 5 MiB (the dynamic property, quota-free).
  • real-CLI — case fix(setup): strip conflicting ANTHROPIC_AUTH_TOKEN #16 (~9.5MB screenshot pile) and case feat(setup): native /model picker via gateway model discovery #17 (~100k-token conversation + screenshots) vs live Copilot: no 413, newest green screenshot still read.

Gates: 604 unit green · docker http-e2e ALL PASSED (68) · tsc clean. 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

wangcansunking and others added 2 commits July 3, 2026 09:44
…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>
@wangcansunking

Copy link
Copy Markdown
Owner Author

Real CLI e2e — verified against live Copilot

Both context-editing cases PASS:

=== context editing -> a 7MB pile of history screenshots does not 413 ===
  unedited screenshot bytes ~9.5MB  context-edit turn result: Green
  PASS a ~7MB screenshot pile never 413s (context editing kept the body under the ~5 MiB gateway limit)
  PASS latest screenshot still readable after clearing old ones (answers green)

=== context editing (dynamic) -> a big conversation + screenshots stays a valid turn (issue #52) ===
  text ~0.4MB + images ~1.9MB unedited  result: Green
  PASS issue #52: big conversation + screenshots never 413s (dynamic budget cleared more images)
  PASS latest screenshot still readable with a big conversation (answers green)

The same run shows the 5 pre-existing FAILs documented against PR #48 (codex tool loop, 2× vision OCR image media type not supported, 2× unknown-model timeout) — all on paths this PR never touches.

Pre-merge CLI gate: PASS.

Note on the CLI byte-wall limit

The 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
@wangcansunking
wangcansunking merged commit e7be3dc into master Jul 3, 2026
6 checks passed
@wangcansunking
wangcansunking deleted the worktree-context-edit-dynamic-budget branch July 3, 2026 02:40
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.

413 Request Entity Too Large still fires with v0.16.0 image context-editing — IMAGE_PAYLOAD_BUDGET (6MB) above Copilot's real entity limit

1 participant