Skip to content

Conversation

@Mnehmos
Copy link
Contributor

@Mnehmos Mnehmos commented Jun 5, 2025

Related GitHub Issue

Closes: #4009

Description

This PR addresses Issue #4009, where the "Always read entire file" setting inadvertently prevented the model from utilizing line-range reads. The fix involves two main parts:

  1. System Prompt Modification:

  2. readFileTool.ts Enhancement:

    • The core logic in src/core/tools/readFileTool.ts has been enhanced to implement intelligent caching and avoid redundant file I/O.
    • It now retrieves file modification times (mtime) before reading.
    • An internal cache stores mtime and the content of loaded line ranges (or full file content).
    • Before performing a read, the tool checks this cache and the current mtime. If the requested range is already loaded and the file has not been modified since that read, the tool serves the content from the cache, skipping actual file I/O.
    • If the file has been modified (different mtime) or the range isn't cached, a fresh read is performed, and the cache is updated.
    • Comprehensive unit tests for this new caching and mtime logic were added and updated in src/core/tools/__tests__/readFileTool.test.ts.

Reviewers should pay attention to the new caching logic in readFileTool.ts and the unconditional inclusion of line_range in the read_file tool's prompt description.

Test Procedure

  1. Unit Tests:

    • The changes to read_file prompt generation are covered by updated snapshots in src/core/prompts/__tests__/system.test.ts and src/core/prompts/__tests__/custom-system-prompt.test.ts.
    • The new caching and mtime logic in readFileTool.ts is covered by extensive unit tests in src/core/tools/__tests__/readFileTool.test.ts. These tests mock file system operations to verify cache hits and misses under various conditions (file modification, different ranges, etc.).
    • Run pnpm test in the root directory. All tests should pass. Specifically, ensure tests in src/core/prompts/__tests__/ and src/core/tools/__tests__/ pass.
  2. Manual End-to-End Verification (as performed during development):

    • In a Roo Code development environment:
    • Conceptually enable "Always read entire file" (or ensure the setting doesn't prevent the model from attempting line range reads due to the prompt changes).
    • Use a large text file (e.g., path/to/your/largeTestFile.js).
    • Step 1: Instruct Roo: "Please read lines 10 to 15 (inclusive) of the file path/to/your/largeTestFile.js."
      • Expected: Roo uses read_file with the specified line range.
    • Step 2: Instruct Roo: "Now, please read lines 10 to 15 of path/to/your/largeTestFile.js again."
      • Expected: readFileTool.ts serves from cache (no new file I/O if file is unmodified).
    • Step 3: Instruct Roo: "Okay, now please read lines 50 to 55 of path/to/your/largeTestFile.js."
      • Expected: New read, cache updated.
    • Step 4: Instruct Roo: "Interesting. Now, please read lines 12 to 20 of path/to/your/largeTestFile.js."
      • Expected: Tool intelligently handles overlapping ranges with its cache.
    • Step 5 (File Modification):
      • Manually modify path/to/your/largeTestFile.js (e.g., change line 10). Save it.
      • Instruct Roo: "The file path/to/your/largeTestFile.js has been modified. Please read lines 10 to 15 of path/to/your/largeTestFile.js again."
      • Expected: readFileTool.ts detects modification, performs a fresh read, and returns updated content.
    • Throughout these steps, observe if Roo correctly formulates read_file requests with line ranges.

Type of Change

  • 🐛 Bug Fix: Non-breaking change that fixes an issue.
  • New Feature: Non-breaking change that adds functionality.
  • ♻️ Refactor: Code change that neither fixes a bug nor adds a feature (for the caching mechanism in readFileTool.ts).
  • 💅 Style: Changes that do not affect the meaning of the code (white-space, formatting, etc.).
  • 📚 Documentation: Updates to documentation files.
  • ⚙️ Build/CI: Changes to the build process or CI configuration (related to fixing esbuild.mjs and test setups).
  • 🧹 Chore: Other changes that don't modify src or test files.

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Code Quality:
    • My code adheres to the project's style guidelines.
    • There are no new linting errors or warnings (npm run lint). (Verified by successful pnpm test which includes linting).
    • All debug code (e.g., console.log) has been removed.
  • Testing:
    • New and/or updated tests have been added to cover my changes.
    • All tests pass locally (pnpm test).
    • The application builds successfully with my changes.
  • Branch Hygiene: My branch is up-to-date (rebased) with the main branch. (Assumed for PR template generation)
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Changeset: A changeset has been created using npm run changeset if this PR includes user-facing changes or dependency updates. (This change is primarily internal/developer-facing regarding tool behavior, but a changeset might be considered if the improved efficiency or reliability is user-noticeable).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Screenshots / Videos

N/A. Changes are backend/tool behavior and build/test scripts.

Documentation Updates

  • No documentation updates are required. The user-facing behavior of being able to request line ranges is now more consistent, but the fundamental way users interact with the read_file concept (if they were manually crafting tool calls, which is rare) doesn't change. The primary impact is on the model's ability to use the tool effectively and internal efficiency.

Additional Notes

To ensure the primary fix for #4009 could be successfully tested and merged, this PR includes the following targeted adjustments that were made to the 6 files in this changeset:

  1. Testing the "Always read entire file" setting prevents line-range reads #4009 Feature (readFileTool.ts caching):

    • The Jest tests in src/core/tools/__tests__/readFileTool.test.ts were refined to correctly validate the new caching and mtime logic in src/core/tools/readFileTool.ts. This involved adjusting mock setups for file system interactions, implementing proper test file lifecycle management (creation/cleanup of temporary files for read operations), and correcting assertions.
    • A previously created Vitest test file for readFileTool.ts was removed as its coverage was consolidated into the more robust Jest tests, also avoiding unrelated vscode module resolution issues.
  2. Enabling pnpm test to Pass After "Always read entire file" setting prevents line-range reads #4009 Changes:

    • A build failure during pnpm test (in the roo-cline:bundle script) was resolved by modifying src/esbuild.mjs to use a more robust directory removal method, preventing an ENOTEMPTY error.
    • Test failures in the @roo-code/vscode-webview package (also blocking pnpm test) were addressed by ensuring its Jest setup was correct. This involved modifications to webview-ui/src/setupTests.tsx to align with the package's Jest configuration.

These specific changes to the 6 files in this PR were essential for delivering the fix for #4009 with a fully passing test suite.

Get in Touch

Mnehmos


Important

Fixes issue #4009 by updating read_file tool to support line-range reads and implementing caching to avoid redundant file I/O.

  • Behavior:
    • Updates read_file tool in readFileTool.ts to support line-range reads regardless of 'Always read entire file' setting.
    • Implements caching to avoid redundant file I/O by storing file modification times and content.
    • Updates read_file tool description in read-file.ts to always include line_range parameter.
  • Testing:
    • Updates test snapshots in system.test.ts.snap.
    • Adds and refines unit tests in readFileTool.test.ts to cover caching logic and line-range reads.
  • Build/CI:
    • Fixes build script in esbuild.mjs to prevent ENOTEMPTY error during pnpm test.
    • Updates setupTests.tsx for Jest setup in webview-ui.

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

@Mnehmos Mnehmos requested review from cte, jr and mrubens as code owners June 5, 2025 22:22
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Jun 5, 2025
)
})

// Mock lucide-react icons globally using Proxy for dynamic icon handling
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a duplicate comment regarding the lucide‐react icon mocking using Proxy (the initial comment on lines 35–52 and then again on line 54). Consider removing the redundant comment to keep the file clean.

Suggested change
// Mock lucide-react icons globally using Proxy for dynamic icon handling

@daniel-lxs
Copy link
Member

Hey @KJ7LNW, I noticed you left your thoughts in the issue, what do you think of the PR?

@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Jun 5, 2025
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Jun 5, 2025
@KJ7LNW
Copy link
Contributor

KJ7LNW commented Jun 5, 2025

Hey @KJ7LNW, I noticed you left your thoughts in the issue, what do you think of the PR?

  1. The cache needs to be persistent, otherwise when you come back to the task after a vscode restart (or Roo update, crash, etc) then it will read all the files which definitely does not help the context situation.
  2. Writes update the validity of line ranges without the need for an additional read.

The additional metadata for storing modification times with line ranges is quite small, just a few integers and file names is enough.

The best place to put it is in api_conversation_history.json, please see the comment with more detail here that includes examples for updating modification times not only for reads but also the behavior for tools that perform writes:

here is an example for apply_diff and read_file will be similar :

api_conversation_history.json:

[
  {
    "role": "assistant",
    "content": [
      { "type": "text", "text": "I need to fix the login function in the authentication file <apply_diff>..." }
    ],
    "ts": 1717620045000  
  },
  {
    "role": "user",
    "content": [
      { "type": "text", "text": "I've applied a diff to fix the login function. <file><user_changes>..." }
    ],
    "ts": 1717620050000, 
     
    //
    // New structures stored in api_conversation_history.json:
    //
    "tool": {  // Tool metadata at same level as ts
      "name": "apply_diff",  // Name of the tool used
      "options": {  // Original tool parameters
        "path": "src/services/Auth.js",
      }
    },
    
    //
    // File metadata for tracking 
    //
    "files": [  
      {
        "fileName": "src/services/Auth.js",
        "lineRanges": [  
           // Only the specific lines modified by the diff, as reported by the RESULT 
           // not the request, because sometimes line numbers are fuzzy and what actually 
           // gets replaced is the part that needs to be updated in terms of being valid.
           // these lines are used to decide if they exist: writes update the validity of line 
           // ranges without the need for an additional read:
          { "start": 120, "end": 124 },
          { "start": 222, "end": 333 }
          ...
        ],
        "mtime": 1717620048000  // mtime of the file after applying the diff
      }
    ]
  }
]

@Mnehmos Mnehmos closed this by deleting the head repository Jun 5, 2025
@Mnehmos
Copy link
Contributor Author

Mnehmos commented Jun 5, 2025

sigh sorry still learning how to use github properly, was trying to resolve a different issue....

@daniel-lxs daniel-lxs moved this from PR [Needs Prelim Review] to Done in Roo Code Roadmap Jun 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

"Always read entire file" setting prevents line-range reads

3 participants