fix: prevent context overflow from extremely long lines in grep_search - #144
Open
Delta062 wants to merge 1 commit into
Open
fix: prevent context overflow from extremely long lines in grep_search#144Delta062 wants to merge 1 commit into
Delta062 wants to merge 1 commit into
Conversation
- Skip lines longer than 2000 chars (likely embedded binary blobs) - Truncate match content to 300 chars with truncation notice - Prevents 1M+ character responses from base64 payloads in text files Fixes context window overflow incidents reported 2026-07-29. Diagnosed with AI assistance, reviewed and validated by human operators.
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.
Fix: Prevent context overflow from extremely long lines in grep_search
Problem
The
grep_searchendpoint returns the full content of matched lines with no length protection. When searching directories containing files with embedded binary blobs (e.g., base64-encoded payloads in shell scripts), a single match can return over 1 megabyte of content.Real-world impact
A reported incident on 2026-07-29 showed:
grep_searchcall scoped to a home directory returned 1,144,419 characters in a single responseinstall-club3090-server.shThis is particularly problematic for AI agent integrations (like Open WebUI MCP tools) where tool responses are injected directly into the model context window.
Solution
Two defensive caps added to the
_search_filefunction:Skip absurdly long lines (
_MAX_LINE_LENGTH = 2000): Lines exceeding 2000 characters are almost certainly embedded binary blobs (base64, wheels, minified assets), not real source code. Skip them entirely.Cap returned content per match (
_MAX_MATCH_CONTENT = 300): Even for legitimate long lines, truncate the returned content to 300 characters with a truncation notice. This preserves the match location (file + line number) while preventing context overflow.Why these values?
max_resultsupper bound) stays under 150K total charactersBehavior change
... [truncated, full line was N chars]This is non-breaking for normal usage — typical source files are unaffected. The protection only activates for edge cases that were already causing failures.
Testing
Verified against the original failing query:
Alternative Considered
Making these values configurable via environment variables (
OPEN_TERMINAL_MAX_LINE_LENGTH,OPEN_TERMINAL_MAX_MATCH_CONTENT) is possible but adds complexity for what should be sensible defaults. The current hardcoded values are defensive enough that configuration is not needed for the vast majority of users.If maintainers prefer configurability, I am happy to refactor — but the core safety logic should remain.