fix(amazonq): mark oversized file context as truncated instead of cutting silently - #2871
Draft
laileni-aws wants to merge 1 commit into
Draft
Conversation
…ting silently File content added as chat context is capped at workspaceChunkMaxSize with a plain substring, so the model receives only the beginning of large files and has no way to know the file continues. It then reports the wrong file size (e.g. "this file only has 251 lines") instead of reading the rest with its tools. Add truncateContextContent() which appends an explicit truncation marker (including the real character count) while keeping the result within the same size budget, and use it for innerContext in AdditionalContextProvider. Add unit tests.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Problem
When a file is added to a chat prompt as context (for example via
@fileor as pinned context), its content is capped atworkspaceChunkMaxSize(40,960 characters) before it is sent to the model. The cap is applied with a plainsubstring(0, max)and nothing indicates that a cut happened.For a large file this is misleading: the model receives only the first ~40K characters (roughly the first 250 lines of a typical text file) with no hint that more exists, and it confidently reports the wrong file size. Example: attaching a 10,000-line file and asking about lines 1240–1570 yields an answer along the lines of "the file only contains 251 lines, those lines don't exist", even though the model has tools available to read the rest of the file.
The same file works correctly when it is the active editor file, because that path does not go through this cap, which makes the inconsistency confusing for users.
Solution
Add
truncateContextContent(content, maxLength)incontextUtils.ts. WhencontentexceedsmaxLengthit returns the leading prefix followed by an explicit marker:The marker is carved out of the same budget, so the returned string never exceeds
maxLength(the outgoing payload size is unchanged). IfmaxLengthis too small to fit the marker, it falls back to the previous plain cut.Use the helper for
innerContextinAdditionalContextProvider.getAdditionalContextinstead of the baresubstring.The model now knows the excerpt is partial and can read the remainder with its file tools instead of assuming the file ends where the excerpt ends. No public API or protocol changes.
Testing
contextUtils.test.tscover: content within the limit is unchanged, content exactly at the limit is unchanged, oversized content is capped exactly at the limit with the marker as the tail (including the original character count), and the small-limit fallback.additionalContextProvider.test.tsruns an oversized file throughgetAdditionalContextand asserts the resultinginnerContextis capped atworkspaceChunkMaxSizeand carries the marker.ts-mochaon the two affected test files: 57 passing.prettierandeslintclean on the changed files (only pre-existingimport/no-nodejs-moduleswarnings).License
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.