#10465: Context pruning: strip image blocks instead of skipping
agents
stale
Cluster:
Context Management Enhancements
## Summary
The context pruner currently **skips** tool results that contain image blocks (`pruner.ts` lines 160-161 and 228-230). This means base64-encoded images accumulate indefinitely in the session context window, never getting pruned. In production with multi-agent setups receiving Telegram images, this causes `context overflow: prompt too large` errors.
This PR changes the pruner to **strip image blocks** from prunable tool results, replacing them with a short text note (e.g. `[1 image pruned from context]`), then applies normal soft-trim/hard-clear to the remaining text content.
### Changes
**`pruner.ts`:**
- Add `stripImageBlocks()` helper that replaces image blocks with a text placeholder
- In `softTrimToolResultMessage`: strip images first, then apply normal text trimming (instead of returning `null`)
- In `pruneContextMessages` main loop: remove the `hasImageBlocks` skip guard so image-containing tool results are included in the prunable set
- Images in the protected tail (last N assistant turns) are still preserved
**`context-pruning.test.ts`:**
- Update existing "skips tool results that contain images" test → now verifies images ARE stripped and text is pruned
- Add new test: "strips images but preserves short text" — verifies image blocks are removed while short text content is kept intact
### Context
- Base64 images in tool results are typically 150KB+ each (after JPEG optimization)
- The `IMAGE_CHAR_ESTIMATE` (8000 chars) was already counting images toward context usage, but the pruner was never acting on them
- In a real production case: 94 images / 20MB of base64 accumulated across 12 sessions, causing repeated context overflow errors
- The Anthropic API requires inline base64 (no file references), so images must be stored this way — the pruner is the right place to reclaim this space
## Test plan
- [ ] Existing context-pruning tests pass (updated test verifies new behavior)
- [ ] New test verifies image stripping with short text preservation
- [ ] Images in protected tail (last N assistants) are not affected
- [ ] Tool results without images continue to be pruned as before
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
- Updates context pruner to handle tool results containing inline image blocks by stripping image blocks and then applying existing soft-trim/hard-clear logic.
- Removes the previous guard that skipped pruning image-containing tool results, so they now participate in pruning calculations.
- Adjusts and extends unit tests to validate image stripping, hard-clear behavior, and preservation of short tool-result text.
<h3>Confidence Score: 4/5</h3>
- Mostly safe to merge, but there is one behavioral inconsistency around preserving the image-pruned placeholder after soft-trim.
- Core change (strip image blocks rather than skipping) is straightforward and covered by updated tests, but the current soft-trim path drops the image-pruned note for long tool results, which is likely unintended given the PR’s stated goal of leaving a placeholder.
- src/agents/pi-extensions/context-pruning/pruner.ts
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#5057: fix: add per-tool-result hard cap to prevent context overflow
by hanxiao · 2026-01-31
82.8%
#17730: feat: per-tool softTrim overrides for context pruning
by IrriVisionTechnologies · 2026-02-16
80.0%
#14913: fix: update context pruning to notify session metadata after prunin...
by ScreenTechnicals · 2026-02-12
79.1%
#14879: fix: persist session metadata to sessions.json after context pruning
by skylarkoo7 · 2026-02-12
78.1%
#19135: fix(agents): prune excess images from conversation history (#19099)
by pierreeurope · 2026-02-17
77.3%
#8172: fix(sessions_list): strip base64 image data to prevent context over...
by Flamrru · 2026-02-03
77.2%
#4852: fix(agents): sanitize tool pairing after compaction and history tru...
by lailoo · 2026-01-30
76.2%
#16966: fix: strip tool_use blocks from aborted/errored assistant messages
by StressTestor · 2026-02-15
75.9%
#19094: Fix empty tool_call_id and function names in provider transcript pa...
by yxshee · 2026-02-17
75.9%
#5360: fix(compaction): add emergency pruning for context overflow
by sgwannabe · 2026-01-31
75.6%