feat: add context limits for folder mentions to prevent overflow #11138
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.
Related GitHub Issue
Closes: #11137
Description
This PR attempts to address Issue #11137. Feedback and guidance are welcome.
The Problem:
When a user uses
@folderto mention a directory, the system reads ALL files in that folder without any limits. Even though individual files are truncated to 2000 lines, if a folder contains many files, the total content can explode and exceed the context window, causing errors on the first conversation.The Solution:
This PR adds two limits to prevent context window overflow when processing folder mentions:
Key implementation details:
@filementions instead of@folderfor more controlTest Procedure
Unit tests were added to verify the new limits:
src/core/mentions/__tests__/folder-limits.spec.ts- 7 new testsAll tests pass:
Existing tests still pass:
Manual testing: Use
@folder/mention on a directory with many files and verify that:Pre-Submission Checklist
Screenshots / Videos
N/A - Backend logic change, no UI impact.
Documentation Updates
Additional Notes
The constants can be adjusted if needed:
MAX_FOLDER_FILES_TO_READ = 10- A conservative default that should work for most use casesMAX_FOLDER_CONTENT_SIZE = 100_000- Approximately 100KB, which should be safe for most context windowsImportant
Adds limits to folder mentions to prevent context overflow by restricting files read and content size, with tests to verify behavior.
MAX_FOLDER_FILES_TO_READandMAX_FOLDER_CONTENT_SIZEconstants inindex.tsto limit files read and content size from folder mentions.getFileOrFolderContentWithMetadata()inindex.tsto enforce these limits, skipping files and adding truncation notices when limits are reached.folder-limits.spec.tswith 7 tests to verify file and content size limits, truncation notices, and folder listing behavior.parseMentions()inindex.tsto handle new truncation messages.This description was created by
for 4c8db13. You can customize this summary. It will automatically update as commits are pushed.