Skip to content

fix(pruning): allow pruning old image tool results to prevent context overflow#41863

Open
smysle wants to merge 1 commit intoopenclaw:mainfrom
smysle:fix/prune-old-image-tool-results
Open

fix(pruning): allow pruning old image tool results to prevent context overflow#41863
smysle wants to merge 1 commit intoopenclaw:mainfrom
smysle:fix/prune-old-image-tool-results

Conversation

@smysle
Copy link
Copy Markdown
Contributor

@smysle smysle commented Mar 10, 2026

Closes #41789

Problem

pruneContextMessages() unconditionally skips all toolResult messages that contain image blocks:

if (hasImageBlocks(msg.content)) {
  continue; // never prunable
}

In screenshot-heavy sessions (e.g., browser automation), image tool results accumulate indefinitely and can never be removed, causing unrecoverable context overflow.

Fix

Instead of protecting all image tool results, only protect the 3 most recent ones. Older image results are added to the prunable set like regular tool results.

const MAX_PROTECTED_IMAGE_RESULTS = 3;
// ... collect image tool indexes, protect only the last N
if (hasImageBlocks(msg.content) && protectedImageIndexes.has(i)) {
  continue; // only recent images are protected
}

Trade-off

Recent screenshots are typically the most relevant for ongoing tasks. Older ones have diminishing value and their ~8KB estimated size per image adds up quickly. The constant MAX_PROTECTED_IMAGE_RESULTS = 3 can be tuned or made configurable if needed.

… overflow

Previously all tool results containing images were unconditionally
skipped during context pruning. In screenshot-heavy sessions this
caused unrecoverable context overflow since image results could
never be removed.

Now only the 3 most recent image tool results are protected from
pruning; older ones are added to the prunable set like regular tool
results.

Closes openclaw#41789
@openclaw-barnacle openclaw-barnacle bot added agents Agent runtime and tooling size: XS labels Mar 10, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 10, 2026

Greptile Summary

This PR fixes an unrecoverable context overflow bug in screenshot-heavy sessions by allowing older image tool results to be pruned, instead of unconditionally protecting all of them. The change is surgical: a pre-pass collects the indexes of all image tool results, then protects only the 3 most recent; the main pruning loop skips only those protected entries — older image results fall through into prunableToolIndexes and become eligible for hard-clearing.

The logic is consistent and correct:

  • Both the pre-pass and the main loop apply the same isToolPrunable guard, so non-prunable image results are untouched.
  • softTrimToolResultMessage still returns null for all image-containing messages (soft-trimming a binary image is not meaningful), so unprotected image results bypass the soft-trim step but are still cleared in the hard-clear phase — which is the right outcome.
  • totalChars accounting and prunableToolChars thresholds are unaffected by the change.

One finding: The module-level comment on lines 8–9 ("We currently skip pruning tool results that contain images") is now stale and should be updated to reflect the new partial-protection behaviour where only the most recent image results are shielded from pruning.

Confidence Score: 4/5

  • Safe to merge — the fix correctly handles the overflow case with no observable regressions to recent-image protection, pending the minor documentation update.
  • The logic change is small and well-scoped. The pre-pass and main loop share the same boundary conditions and guards, so the protected set is built from exactly the same population of messages that the main loop visits. The core fix is sound. The only finding is a stale module-level comment on lines 8–9, which is a minor documentation issue and not a correctness concern.
  • src/agents/pi-extensions/context-pruning/pruner.ts — update the module-level comment on lines 8–9 to reflect the new partial-protection behaviour for image results.

Comments Outside Diff (1)

  1. src/agents/pi-extensions/context-pruning/pruner.ts, line 8-9 (link)

    The module-level comment on these lines is now stale. After your PR changes, only the 3 most recent image tool results are protected from pruning; older image results can now be pruned to prevent context overflow. Please update the comment to reflect this new behaviour.

Last reviewed commit: 0a1ebf1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Context pruning skips image-containing tool results, causing unrecoverable context overflow in screenshot-heavy sessions

1 participant