-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: Add file read caching to prevent redundant reads in conversation history #4501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Mnehmos
wants to merge
7
commits into
RooCodeInc:main
from
Mnehmos:feature/4009-pr2-file-read-caching
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
49bf0c7
#4009
Mnehmos b20bd7b
Update src/core/tools/readFileTool.ts
Mnehmos 44ea42f
feat(file-reads): Implement file-read caching
Mnehmos 03ef66a
feat(file-reads): Implement file-read caching
Mnehmos 2071612
feat(cache): implement cache size limit and eviction policy and impro…
Mnehmos 369cf70
fix(cache): address memory management and error handling feedback
Mnehmos a9fff91
fix(cache):adress memory management and error handling feedback 2
Mnehmos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| function getFromEnv(key: string, defaultValue: string): string { | ||
| const value = process.env[key] | ||
| return value === undefined ? defaultValue : value | ||
| } | ||
|
|
||
| export const ROO_AGENT_CONFIG = { | ||
| fileReadCacheSize: () => parseInt(getFromEnv("ROO_FILE_READ_CACHE_SIZE", "100")), | ||
| } |
228 changes: 144 additions & 84 deletions
228
src/core/prompts/__tests__/__snapshots__/system.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
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
161 changes: 161 additions & 0 deletions
161
src/core/services/__tests__/fileReadCacheService.spec.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| import { vi, describe, it, expect, beforeEach } from "vitest" | ||
| import { | ||
| processAndFilterReadRequest, | ||
| subtractRange, | ||
| subtractRanges, | ||
| ConversationMessage, | ||
| mtimeCache, | ||
| } from "../fileReadCacheService" | ||
| import { stat } from "fs/promises" | ||
| import { lruCache } from "../../utils/lruCache" | ||
| vi.mock("fs/promises", () => ({ | ||
| stat: vi.fn(), | ||
| })) | ||
| vi.mock("../../utils/lruCache") | ||
| vi.mock("../../config/envConfig", () => ({ | ||
| ROO_AGENT_CONFIG: { | ||
| fileReadCacheSize: () => 10, | ||
| }, | ||
| })) | ||
| const mockedStat = vi.mocked(stat) | ||
| describe("fileReadCacheService", () => { | ||
| describe("subtractRange", () => { | ||
| it("should return the original range if there is no overlap", () => { | ||
| const original = { start: 1, end: 10 } | ||
| const toRemove = { start: 11, end: 20 } | ||
| expect(subtractRange(original, toRemove)).toEqual([original]) | ||
| }) | ||
| it("should return an empty array if the range is completely removed", () => { | ||
| const original = { start: 1, end: 10 } | ||
| const toRemove = { start: 1, end: 10 } | ||
| expect(subtractRange(original, toRemove)).toEqual([]) | ||
| }) | ||
| it("should subtract from the beginning", () => { | ||
| const original = { start: 1, end: 10 } | ||
| const toRemove = { start: 1, end: 5 } | ||
| expect(subtractRange(original, toRemove)).toEqual([{ start: 6, end: 10 }]) | ||
| }) | ||
| it("should subtract from the end", () => { | ||
| const original = { start: 1, end: 10 } | ||
| const toRemove = { start: 6, end: 10 } | ||
| expect(subtractRange(original, toRemove)).toEqual([{ start: 1, end: 5 }]) | ||
| }) | ||
| it("should subtract from the middle, creating two new ranges", () => { | ||
| const original = { start: 1, end: 10 } | ||
| const toRemove = { start: 4, end: 6 } | ||
| expect(subtractRange(original, toRemove)).toEqual([ | ||
| { start: 1, end: 3 }, | ||
| { start: 7, end: 10 }, | ||
| ]) | ||
| }) | ||
| }) | ||
| describe("subtractRanges", () => { | ||
| it("should subtract multiple ranges from a single original range", () => { | ||
| const originals = [{ start: 1, end: 20 }] | ||
| const toRemoves = [ | ||
| { start: 1, end: 5 }, | ||
| { start: 15, end: 20 }, | ||
| ] | ||
| expect(subtractRanges(originals, toRemoves)).toEqual([{ start: 6, end: 14 }]) | ||
| }) | ||
| }) | ||
| describe("processAndFilterReadRequest", () => { | ||
| const MOCK_FILE_PATH = "/test/file.txt" | ||
| const CURRENT_MTIME = new Date("2025-01-01T12:00:00.000Z") | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mtimeCache.clear() | ||
| vi.useFakeTimers().setSystemTime(CURRENT_MTIME) | ||
| mockedStat.mockResolvedValue({ | ||
| mtime: CURRENT_MTIME, | ||
| size: 1024, // Add size for the new cache implementation | ||
| } as any) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| afterAll(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it("should allow all when history is empty", async () => { | ||
| const requestedRanges = [{ start: 1, end: 10 }] | ||
| const result = await processAndFilterReadRequest(MOCK_FILE_PATH, requestedRanges, []) | ||
| expect(result.status).toBe("ALLOW_ALL") | ||
| expect(result.rangesToRead).toEqual(requestedRanges) | ||
| }) | ||
|
|
||
| it("should reject all when a full cache hit occurs", async () => { | ||
| const requestedRanges = [{ start: 1, end: 10 }] | ||
| const conversationHistory: ConversationMessage[] = [ | ||
| { | ||
| files: [ | ||
| { | ||
| fileName: MOCK_FILE_PATH, | ||
| mtime: CURRENT_MTIME.getTime(), | ||
| lineRanges: [{ start: 1, end: 10 }], | ||
| }, | ||
| ], | ||
| } as any, | ||
| ] | ||
| const result = await processAndFilterReadRequest(MOCK_FILE_PATH, requestedRanges, conversationHistory) | ||
| expect(result.status).toBe("REJECT_ALL") | ||
| expect(result.rangesToRead).toEqual([]) | ||
| }) | ||
|
|
||
| it("should allow partial when a partial cache hit occurs", async () => { | ||
| const requestedRanges = [{ start: 1, end: 20 }] | ||
| const conversationHistory: ConversationMessage[] = [ | ||
| { | ||
| files: [ | ||
| { | ||
| fileName: MOCK_FILE_PATH, | ||
| mtime: CURRENT_MTIME.getTime(), | ||
| lineRanges: [{ start: 1, end: 10 }], | ||
| }, | ||
| ], | ||
| } as any, | ||
| ] | ||
| const result = await processAndFilterReadRequest(MOCK_FILE_PATH, requestedRanges, conversationHistory) | ||
| expect(result.status).toBe("ALLOW_PARTIAL") | ||
| expect(result.rangesToRead).toEqual([{ start: 11, end: 20 }]) | ||
| }) | ||
|
|
||
| it("should allow all when mtime is older in history", async () => { | ||
| const requestedRanges = [{ start: 1, end: 10 }] | ||
| const conversationHistory: ConversationMessage[] = [ | ||
| { | ||
| files: [ | ||
| { | ||
| fileName: MOCK_FILE_PATH, | ||
| mtime: CURRENT_MTIME.getTime() - 100, // Older mtime | ||
| lineRanges: [{ start: 1, end: 10 }], | ||
| }, | ||
| ], | ||
| } as any, | ||
| ] | ||
| const result = await processAndFilterReadRequest(MOCK_FILE_PATH, requestedRanges, conversationHistory) | ||
| expect(result.status).toBe("ALLOW_ALL") | ||
| expect(result.rangesToRead).toEqual(requestedRanges) | ||
| }) | ||
|
|
||
| it("should allow all when file does not exist", async () => { | ||
| mockedStat.mockRejectedValue({ code: "ENOENT" }) | ||
| const requestedRanges = [{ start: 1, end: 10 }] | ||
| const result = await processAndFilterReadRequest(MOCK_FILE_PATH, requestedRanges, []) | ||
| expect(result.status).toBe("ALLOW_ALL") | ||
| expect(result.rangesToRead).toEqual(requestedRanges) | ||
| }) | ||
|
|
||
| it("should throw an error for non-ENOENT stat errors", async () => { | ||
| const error = new Error("EPERM") | ||
| mockedStat.mockRejectedValue(error) | ||
| const requestedRanges = [{ start: 1, end: 10 }] | ||
| const result = await processAndFilterReadRequest(MOCK_FILE_PATH, requestedRanges, []) | ||
| expect(result.status).toBe("ALLOW_ALL") // Fallback to allow all | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find mentions about why partial reads are being permanently enabled. Is this change intentional? since partial reads can be disabled in the settings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is intentional and addresses Issue #4009. Let me clarify what's actually happening:
The Problem Being Solved:
The "Always read entire file" setting (maxReadFileLine = -1) was prohibiting line-range reads entirely, forcing users to always read complete files even when they had specific line numbers from:
git grep -n results
Compiler/linter error messages
search_files output
Manual diffs with line references
What This Change Does:
Preserves existing behavior: When no <line_range> is specified, entire files are still read
Adds intelligent choice: Model can now choose line ranges when contextually appropriate
Maintains the setting's intent: "Always read entire file" becomes the default, not an absolute restriction
Technical Detail:
Previously: partialReadsEnabled = maxReadFileLine !== -1 meant unlimited readers couldn't see line-range options
Now: Line ranges are always available in the tool interface, letting the model make smart decisions based on context
This transforms a rigid limitation into flexible intelligence - the model gets entire files by default but can target specific lines when it has line numbers to work with.