Skip to content

Conversation

@liwilliam2021
Copy link
Contributor

@liwilliam2021 liwilliam2021 commented Jul 28, 2025

Fix Context Overload in Read File Tool

Problem

The read_file tool could attempt to read files that exceed the available context window, causing API failures and poor user experience.

Solution

Added preemptive context validation that dynamically calculates available space and applies intelligent file size limits.

Changes

New contextValidator module:

  • Calculates remaining context space based on current token usage
  • Reads files incrementally in batches to count actual tokens
  • Returns safe maximum line count with 25% safety buffer
    Enhanced readFileTool:
  • Validates file size before reading when partial reads are off
  • Provides inline instructions to model when files are truncated to use line_range syntax
  • Respects user-specified limits when provided

Fix

Turn off partial reads, and open Roo-Code-Evals/javascript/lodash. Ask the following prompt from this task: https://app.roocode.com/share/fa7affcc-611d-46ff-89a4-d741d8593b2a

Remaining concerns:

  • The model is now shown the tool line_range if a file cannot fit in context. This means that if it reads a very large file, then normal sized files, in theory it could continue to use the line_range tool to do unnecessary partial reads even when partial reads are deactivated.
  • The 25% safety buffer is not guaranteed robust but empirically seems fine
  • Fails if you try reading the E.coli document which is one big line
  • Still fails if you try and force Roo to read the entirety of the bible. This seems to be a problem of inefficient context condensing.

Fixes #7744


Important

Introduces context validation in the Read File Tool to prevent context overload by dynamically calculating available space and applying file size limits.

  • Behavior:
    • Adds contextValidator module to calculate available context space and apply file size limits.
    • Updates readFileTool to validate file size before reading and provide instructions for truncated files.
  • Tests:
    • Adds contextValidator.test.ts to test context validation logic.
    • Updates readFileTool.spec.ts to test file reading with context validation.
    • Adds read-partial-content.spec.ts to test partial content reading.
  • Localization:
    • Updates translation files for new context-related messages in multiple languages.
  • Misc:
    • Adds providerFormat.spec.ts to test provider format logic.
    • Updates api.ts to include provider format mapping logic.

This description was created by Ellipsis for 2285015. You can customize this summary. It will automatically update as commits are pushed.

@liwilliam2021 liwilliam2021 requested review from cte, jr and mrubens as code owners July 28, 2025 22:48
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. bug Something isn't working labels Jul 28, 2025
@liwilliam2021
Copy link
Contributor Author

@cte

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Jul 28, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Jul 28, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Jul 29, 2025
Copy link
Collaborator

@cte cte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in general the strategy is good, but I left a few questions / comments. Separately, I'm going to test this in a few scenarios just to get a sense of how it behaves.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jul 29, 2025
@daniel-lxs daniel-lxs moved this from PR [Needs Prelim Review] to PR [Changes Requested] in Roo Code Roadmap Jul 29, 2025
@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Jul 30, 2025
@daniel-lxs daniel-lxs moved this from PR [Changes Requested] to PR [Needs Prelim Review] in Roo Code Roadmap Jul 30, 2025
@daniel-lxs daniel-lxs moved this from PR [Changes Requested] to PR [Needs Prelim Review] in Roo Code Roadmap Aug 2, 2025
Copy link
Member

@daniel-lxs daniel-lxs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Adding a dynamic hard cap based on the context window would be a solid improvement for the future, but this is a good first step.

@daniel-lxs daniel-lxs moved this from PR [Needs Prelim Review] to PR [Needs Review] in Roo Code Roadmap Aug 2, 2025
Copy link
Collaborator

@mrubens mrubens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would really like to find a solution that doesn’t require special casing of minified files. Can we try reading lines in chunks instead of reading to the newline every time?

@dosubot dosubot bot removed the lgtm This PR has been approved by a maintainer label Aug 2, 2025
@daniel-lxs daniel-lxs moved this from PR [Needs Review] to PR [Changes Requested] in Roo Code Roadmap Aug 4, 2025
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
@liwilliam2021
Copy link
Contributor Author

I would really like to find a solution that doesn’t require special casing of minified files. Can we try reading lines in chunks instead of reading to the newline every time?

resolved!

@Kilian-Collender
Copy link

Is there a plan to merge this soon?

@hannesrudolph
Copy link
Collaborator

Stale. Implementation not the one we want.

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

Labels

bug Something isn't working PR - Changes Requested size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Stuck at "API Request..."

7 participants