fix(core): bound per-agent image memory (proactive budget + inherited-context scrub)#779
Merged
Merged
Conversation
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Contributor
Greptile SummaryThis PR limits image memory in agent sessions and inherited context. The main changes are:
Confidence Score: 5/5No additional blocking issue was found in the latest changes.
Important Files Changed
Reviews (3): Last reviewed commit: "docs(core): trim verbose comments/docstr..." | Re-trigger Greptile |
- preserve sibling text/non-image blocks when eliding an image output - serialise session rewrites with a per-session lock and restore original items on failure so a rewrite can never drop or empty history - reject negative STRIX_MAX_CONTEXT_IMAGES (ge=0) so it can't silently disable the budget
Contributor
Author
The image-budget rewrite read a snapshot then cleared+re-added, but AgentCoordinator.send() appends peer/user messages to the same session from another task without that lock, so a message delivered mid-rewrite could be clobbered. Expose session_write_lock() and acquire it in both the rewrite and send() paths. Also stop silently suppressing restore failures on a failed re-add (log + re-raise instead).
Contributor
Author
This was referenced Jul 21, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Screenshot/image tool outputs accumulated without bound in agent context, so long deep scans grew process RSS roughly linearly (a handful of very large base64 blobs, flat Python object count) until the host OOM-killed the run. Two independent leaks, two fixes.
1. Images were never pruned from a running agent's session.
view_imagereturns up to a 10 MB base64input_imageblock. It persists in the agent'sSQLiteSessionfor the whole run and is re-materialised into RAM and re-sent to the model on every turn. The only existing defense (strip_all_images_from_session) is reactive — it fires only after the provider rejects a request (400/404/422) and is all-or-nothing.New
enforce_image_budget(session, max_images)keeps the most recentmax_imagesimage outputs and replaces only the olderinput_imageblocks with a text placeholder (sibling text in the same tool result is preserved). Called before each turn in the run loop.Bound is configurable:
RuntimeSettings.max_context_images(STRIX_MAX_CONTEXT_IMAGES, default3,0keeps none, negative rejected viage=0), threaded into the run viacontext["max_context_images"].2.
create_agent(inherit_context=True)duplicated every screenshot into each child.On spawn, the parent's entire turn input is
json.dumps-ed verbatim into the child's first message. That inlined every base64 screenshot as text — duplicated across all N children — and, once serialized into a text blob, it could no longer be reclaimed by the reactive strip (which only matches structuredinput_imageblocks).New
scrub_images_from_items()replaces image blocks with a text placeholder before serialisation:Net effect: per-agent image memory is capped at
max_context_imageslive screenshots, and inherited context carries none. Text/reasoning history is untouched.Concurrency safety
Session rewrites (
enforce_image_budget/strip_all_images_from_session) read a snapshot, thenclear_session()+add_items().AgentCoordinator.send()also appends peer/user messages to a session from another task. Both now acquire one shared per-sessionsession_write_lock, so a rewrite built from a snapshot can never clear away a concurrently-delivered message. On a failed re-add the original items are restored (logged, not silently suppressed) so a rewrite can't leave history empty.Changes
strix/core/sessions.py— addenforce_image_budget,scrub_images_from_items,session_write_lock, and shared_output_has_image/_elided_output/_rewrite_sessionhelpers; refactorstrip_all_images_from_sessiononto them.strix/core/agents.py— acquiresession_write_lockaround out-of-bandsend()appends.strix/core/execution.py— enforce the budget before each turn (best-effort; failures logged, never fatal).strix/core/inputs.py— scrub images out of inherited child context.strix/core/runner.py— passmax_context_imagesinto the run context.strix/config/settings.py—max_context_imagessetting (STRIX_MAX_CONTEXT_IMAGES, default 3,ge=0).Notes
ruff,ruff-format,mypy,bandit, and the touched test suites pass locally via pre-commit.Link to Devin session: https://app.devin.ai/sessions/dad023e379e942f287bcf6822463b7a4
Requested by: @0xallam