Skip to content

feat(context-edit): clear old tool screenshots to fix 413 on long agentic sessions - #49

Merged
wangcansunking merged 3 commits into
masterfrom
worktree-context-editing
Jul 2, 2026
Merged

wangcansunking merged 3 commits into
masterfrom
worktree-context-editing

Conversation

@wangcansunking

Copy link
Copy Markdown
Owner

Problem

Users hit copilot completion failed: 413 — Request Entity Too Large (relayed as a 502) during browser-harness / agentic sessions. A loop screenshots each step, and the stateless wire re-sends the whole history every turn, so cumulative base64 grows until Copilot's gateway rejects the request body at the HTTP byte layer.

This is distinct from the token-limit model_max_prompt_tokens_exceeded that PR #44's image downscaling targets — per-image resize keeps each image small, but can't bound the cumulative payload across many turns.

How the real Anthropic backend handles it

Anthropic runs context editing (clear_tool_uses_20250919, public beta 2025-09) server-side: as the conversation grows it clears the oldest tool results, replacing each with a placeholder, keeping only the most recent keep=3. Claude Code relies on this and keeps sending the full, unmodified history every turn. Copilot has no such layer — so we add it where that backend layer would sit.

Change

  • New core/context-edit.ts — keeps the most recent 3 tool screenshots at full fidelity; once the cumulative image payload exceeds a ~6MB budget, clears older ones oldest-first (image → placeholder text), only as much as needed. Lossless no-op when under budget. Never touches top-level user-attached images (only tool results — mirrors clear_tool_uses semantics).
  • Wired AFTER resize on all send paths (/anthropic/v1/messages, /openai/chat/completions, /openai/responses) and count_tokens, so Claude Code's context sizing matches the request actually sent.
  • 413 errorHint — request too large — the payload exceeded the gateway limit; /compact or send fewer/smaller images (413 previously produced an empty hint).

Tests

  • tests/core/context-edit.test.ts — 7 cases: no-op under budget, keep-floor (never clear the recent 3), oldest-first, minimal edit, multi-image unit clear, top-level untouched, idempotent.
  • tests/worker/errors.test.ts — 413 → hint mapping.
  • Hermetic http-e2e — 12 accumulated screenshots, each already under the per-image resize cap, posted to count_tokens; the estimate lands far below the raw sum, proving old screenshots were cleared, not merely resized (+ a precondition check that each was under the cap).

588 unit tests green · docker http-e2e ALL PASSED (63) · tsc clean.

🤖 Generated with Claude Code

wangcansunking and others added 2 commits July 2, 2026 22:49
…ntic sessions

A browser-harness loop emits one screenshot per step, and the stateless wire
re-sends the whole history every turn, so cumulative base64 grows until Copilot's
gateway rejects the request body at the HTTP layer (413 Request Entity Too Large,
relayed as 502). This is a byte-size limit, distinct from the token limit the
image-downscale fix targets — per-image resize alone can't satisfy it.

The real Anthropic backend handles this server-side via context editing
(clear_tool_uses_20250919): keep the most recent few tool results, replace older
ones with a placeholder. Claude Code relies on it and keeps sending full history;
Copilot has no such layer. We now sit where that layer would:

- new core/context-edit.ts keeps the most recent 3 tool screenshots at full
  fidelity and clears older ones oldest-first (image -> placeholder text) once the
  cumulative image payload exceeds a ~6MB budget, only as much as needed. Lossless
  no-op when under budget; never touches top-level user-attached images.
- wired AFTER resize on all send paths (/anthropic/v1/messages,
  /openai/chat/completions, /openai/responses) AND count_tokens, so the estimate
  matches what's sent.
- errorHint gains a 413 branch (request too large -> /compact or fewer images).

Tests: new tests/core/context-edit.test.ts (7 cases) + 413 hint case + hermetic
http-e2e (12 accumulated screenshots under the per-image cap, count_tokens lands
far below raw sum -> proves clearing not resizing). 588 unit green; docker
http-e2e ALL PASSED (63); tsc clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…cli-e2e mandate

Add CLI e2e case #16: a browser-harness-style pile of 11 tool_result
screenshots posted to /anthropic/v1/messages against live Copilot. Asserts
(a) the request is well-formed, (b) it never 413s (context editing kept the
body under the gateway entity limit), and (c) Claude still reads the MOST
RECENT screenshot (answers 'green') — proving old screenshots were cleared and
recent ones preserved. This is the black-box repro of the reported 413.
Verified green end-to-end against real Copilot.

Also record the pre-merge gate in CLAUDE.md: run the real cli-e2e against live
Copilot and confirm pass before merging any worktree branch (http-e2e alone is
not sufficient), with the Windows/Git Bash mount command.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@wangcansunking

Copy link
Copy Markdown
Owner Author

Real CLI e2e — context editing verified end-to-end against live Copilot

Added CLI case #16 (e2e/docker/cli-e2e.sh): a browser-harness-style pile of 11 tool_result screenshots posted to /anthropic/v1/messages against real Copilot — the black-box repro of the reported 413. All three assertions PASS:

=== context editing -> a pile of history screenshots does not 413, latest image still readable ===
  context-edit turn result: Green
  PASS screenshot pile is a VALID request (no 4xx body-shape error masking the 413 check)
  PASS screenshot pile never 413s (context editing kept the body under the gateway limit)
  PASS latest screenshot still readable after clearing old ones (answers green)

This proves the fix end-to-end: 11 accumulated screenshots no longer 413, and the conversation still works — Claude reads the most-recent (green) screenshot, confirming old ones were cleared and recent ones preserved.

Also added to CLAUDE.md: a pre-merge gate — run the real cli-e2e against live Copilot before merging any worktree branch (the hermetic http-e2e alone is not sufficient).

Note on the other cli-e2e reds (pre-existing, unrelated to this PR)

The same run shows 5 other failures — codex tool loop, vision OCR ×2 (image media type not supported), unknown-model timeout ×2 — all on code paths this PR does not touch (it only adds core/context-edit.ts + 4 call sites + a 413 errorHint). They appear to be environment/baseline issues; a master-baseline comparison run was not completed. Flagging for visibility, not introduced here.

Gates: 588 unit green · http-e2e ALL PASSED (63) · tsc clean · new CLI case green.

@wangcansunking
wangcansunking merged commit 1799c6c into master Jul 2, 2026
6 checks passed
@wangcansunking
wangcansunking deleted the worktree-context-editing branch July 2, 2026 16:27
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.

1 participant