-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(read_file): VSCode decoding for native tool reads #10518
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
Open
hannesrudolph
wants to merge
1
commit into
main
Choose a base branch
from
fix/native-read-file-encoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
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,128 @@ | ||
| import { Anthropic } from "@anthropic-ai/sdk" | ||
|
|
||
| import { countTokens } from "../../utils/countTokens" | ||
|
|
||
| export interface ReadTextWithBudgetResult { | ||
| /** The content read up to the token budget */ | ||
| content: string | ||
| /** Actual token count of returned content */ | ||
| tokenCount: number | ||
| /** Total lines in the returned content */ | ||
| lineCount: number | ||
| /** Whether the entire text was read (false if truncated) */ | ||
| complete: boolean | ||
| } | ||
|
|
||
| export interface ReadTextWithBudgetOptions { | ||
| /** Maximum tokens allowed. Required. */ | ||
| budgetTokens: number | ||
| /** Number of lines to buffer before token counting (default: 256) */ | ||
| chunkLines?: number | ||
| } | ||
|
|
||
| function normalizeTextToLines(text: string): string[] { | ||
| // Normalize line endings and mirror `readFileWithTokenBudget()` behavior: | ||
| // - split on line boundaries | ||
| // - do not include a trailing empty line caused solely by a trailing newline | ||
| const lines = text.split(/\r?\n/) | ||
| if (lines.length > 0 && lines[lines.length - 1] === "") { | ||
| lines.pop() | ||
| } | ||
| return lines | ||
| } | ||
|
|
||
| async function countTextTokens(text: string): Promise<number> { | ||
| try { | ||
| const contentBlocks: Anthropic.Messages.ContentBlockParam[] = [{ type: "text", text }] | ||
| return await countTokens(contentBlocks) | ||
| } catch { | ||
| // Fallback: conservative estimate (2 chars per token) | ||
| return Math.ceil(text.length / 2) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Reads text while incrementally counting tokens, stopping when budget is reached. | ||
| * | ||
| * This is the in-memory analogue of [`readFileWithTokenBudget()`](src/integrations/misc/read-file-with-budget.ts:35). | ||
| */ | ||
| export async function readTextWithTokenBudget( | ||
| text: string, | ||
| options: ReadTextWithBudgetOptions, | ||
| ): Promise<ReadTextWithBudgetResult> { | ||
| const { budgetTokens, chunkLines = 256 } = options | ||
|
|
||
| const allLines = normalizeTextToLines(text) | ||
| if (allLines.length === 0) { | ||
| return { content: "", tokenCount: 0, lineCount: 0, complete: true } | ||
| } | ||
|
|
||
| let content = "" | ||
| let lineCount = 0 | ||
| let tokenCount = 0 | ||
| let complete = true | ||
| let lineBuffer: string[] = [] | ||
|
|
||
| const processBuffer = async (): Promise<boolean> => { | ||
| if (lineBuffer.length === 0) return true | ||
|
|
||
| const bufferText = lineBuffer.join("\n") | ||
| const currentBuffer = [...lineBuffer] | ||
| lineBuffer = [] | ||
|
|
||
| const chunkTokens = await countTextTokens(bufferText) | ||
|
|
||
| if (tokenCount + chunkTokens > budgetTokens) { | ||
| // Find cutoff within this chunk (binary search by line count) | ||
| let low = 0 | ||
| let high = currentBuffer.length | ||
| let bestFit = 0 | ||
| let bestTokens = 0 | ||
|
|
||
| while (low < high) { | ||
| const mid = Math.floor((low + high + 1) / 2) | ||
| const testContent = currentBuffer.slice(0, mid).join("\n") | ||
| const testTokens = await countTextTokens(testContent) | ||
|
|
||
| if (tokenCount + testTokens <= budgetTokens) { | ||
| bestFit = mid | ||
| bestTokens = testTokens | ||
| low = mid | ||
| } else { | ||
| high = mid - 1 | ||
| } | ||
| } | ||
|
|
||
| if (bestFit > 0) { | ||
| const fitContent = currentBuffer.slice(0, bestFit).join("\n") | ||
| content += (content.length > 0 ? "\n" : "") + fitContent | ||
| tokenCount += bestTokens | ||
| lineCount += bestFit | ||
| } | ||
|
|
||
| complete = false | ||
| return false | ||
| } | ||
|
|
||
| content += (content.length > 0 ? "\n" : "") + bufferText | ||
| tokenCount += chunkTokens | ||
| lineCount += currentBuffer.length | ||
| return true | ||
| } | ||
|
|
||
| for (const line of allLines) { | ||
| lineBuffer.push(line) | ||
| if (lineBuffer.length >= chunkLines) { | ||
| const continueReading = await processBuffer() | ||
| if (!continueReading) { | ||
| return { content, tokenCount, lineCount, complete } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (lineBuffer.length > 0) { | ||
| await processBuffer() | ||
| } | ||
|
|
||
| return { content, tokenCount, lineCount, complete } | ||
| } |
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.
When native reads use VSCode-decoded text, out-of-range <line_range> values no longer error (for example range.start beyond the file length). readLines() rejects in that case, but sliceTextLines() returns an empty string and addLineNumbers() can emit a misleading blank line like 1000 | .
Fix it with Roo Code or mention @roomote and request a fix.