Skip to content

fix(amazonq): mark oversized file context as truncated instead of cutting silently - #2871

Draft
laileni-aws wants to merge 1 commit into
Amazon-Q-Developer:mainfrom
laileni-aws:fix/mark-truncated-context-content
Draft

fix(amazonq): mark oversized file context as truncated instead of cutting silently#2871
laileni-aws wants to merge 1 commit into
Amazon-Q-Developer:mainfrom
laileni-aws:fix/mark-truncated-context-content

Conversation

@laileni-aws

Copy link
Copy Markdown
Collaborator

Problem

When a file is added to a chat prompt as context (for example via @file or as pinned context), its content is capped at workspaceChunkMaxSize (40,960 characters) before it is sent to the model. The cap is applied with a plain substring(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) in contextUtils.ts. When content exceeds maxLength it returns the leading prefix followed by an explicit marker:

    [Content truncated: this file has N characters, only the beginning is included above. Read the file directly to see the rest.]
    

    The marker is carved out of the same budget, so the returned string never exceeds maxLength (the outgoing payload size is unchanged). If maxLength is too small to fit the marker, it falls back to the previous plain cut.

  • Use the helper for innerContext in AdditionalContextProvider.getAdditionalContext instead of the bare substring.

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

  • New unit tests in contextUtils.test.ts cover: 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.
  • New test in additionalContextProvider.test.ts runs an oversized file through getAdditionalContext and asserts the resulting innerContext is capped at workspaceChunkMaxSize and carries the marker.
  • ts-mocha on the two affected test files: 57 passing. prettier and eslint clean on the changed files (only pre-existing import/no-nodejs-modules warnings).

License

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

…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-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants