Repository navigation
feat(context-edit): clear old tool screenshots to fix 413 on long agentic sessions - #49
Conversation
…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>
Real CLI e2e — context editing verified end-to-end against live CopilotAdded CLI case #16 ( 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 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 ( Gates: 588 unit green · http-e2e ALL PASSED (63) · |
…iting # Conflicts: # e2e/RESULTS.md
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_exceededthat 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 recentkeep=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
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 — mirrorsclear_tool_usessemantics)./anthropic/v1/messages,/openai/chat/completions,/openai/responses) andcount_tokens, so Claude Code's context sizing matches the request actually sent.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.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) ·
tscclean.🤖 Generated with Claude Code