-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: stop reading big files that crash context #6667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution! I've reviewed the changes and the implementation looks solid overall. The approach to prevent infinite hanging on large files is well thought out. I've left some suggestions inline that could improve performance and robustness.
| * Conservative buffer percentage for file reading. | ||
| * We use a very conservative estimate to ensure files fit in context. | ||
| */ | ||
| const FILE_READ_BUFFER_PERCENTAGE = 0.4 // 40% buffer for safety |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the 40% buffer intentionally this conservative? It might be worth making this configurable or adjusting based on model capabilities. Some models might handle closer-to-limit content better than others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like we shouldn’t need to be so conservative here if the rest of the logic is working right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah sorry-- I think I just picked a big number for the simple version
- Fix Hindi translation punctuation - Fix race condition by checking stream.destroyed - Optimize newline counting with regex - Performance improvements for large file handling - Defensive programming for end parameter already in place
daniel-lxs
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @liwilliam2021 this finally defeated my file
| const remainingTokens = contextWindow - currentlyUsed | ||
| const usableTokens = Math.floor(remainingTokens * (1 - FILE_READ_BUFFER_PERCENTAGE)) | ||
|
|
||
| // Reserve space for response (use 25% of remaining or 4096, whichever is smaller) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use the common logic for this
| // For large files or when approaching limits, always limit | ||
| if (fileSizeBytes > safeCharLimit || fileSizeBytes > LARGE_FILE_SIZE) { | ||
| // Use a very conservative limit | ||
| const finalLimit = Math.min(safeCharLimit, 100000) // Cap at 100K chars |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might annoy people who are trying to use a model with a large context window to read large files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the plan for this PR was to do something stupid to fix the temporary error-- basically never reading big files
This PR has the full implementation and doesn't have that limit
#6319
|
Let's get this across the finish line |
Implements a simple, token-budget based file reading system that prevents context window overflow and tokenizer crashes. Problem: - Files could fill entire context window causing issues - tiktoken crashes with 'unreachable' error on files >5MB - PR #6667's approach was too complex with magic numbers Solution - Multi-Layer Defense: 1. Fast path: Files <100KB skip validation (no overhead) 2. Token validation: 100KB-5MB files use real token counting - Budget: (contextWindow - currentTokens) * 0.6 - Smart truncation if exceeds budget 3. Preview mode: Files >5MB get 100KB preview (prevents crashes) 4. Error recovery: Catch tokenizer 'unreachable' errors gracefully Key Features: - No magic numbers - dynamic based on actual context - Real token counting using existing tokenizer - 100KB previews for large files (perfect size for structure visibility) - Graceful error handling prevents conversation crashes - Simple implementation (~160 lines vs complex heuristics) Testing: - 17 comprehensive tests covering all scenarios - All tests passing including edge cases and error conditions Files: - src/core/tools/helpers/fileTokenBudget.ts: Core validation logic - src/core/tools/helpers/__tests__/fileTokenBudget.spec.ts: Test suite - src/core/tools/readFileTool.ts: Integration into read file tool
Implemented a simple version of: #6319
We now always stop reading after a limited read. We also use simpler heuristics that do not call the tokenizer to validate while remaining conservative.
Goal: prevent infinite hanging on large files when partial reads are off. This may limit the ability of the model to read large files which is addressed in the next PR.
Important
Introduces file size validation in
readFileTool.tsto prevent crashes from large files by limiting reads based on context size.readFileTool.tsusingvalidateFileSizeForContextfrom newcontextValidator.ts.readFileTool.spec.tsto test new validation logic and partial read notices.maxCharsparameter inread-lines.spec.tsto ensure character limit handling.showingOnlyLinestranslation in multipletools.jsonfiles for different languages.This description was created by
for 75fb09a. You can customize this summary. It will automatically update as commits are pushed.