Skip to content

Commit c719117

Browse files
authored
Be safer about large file reads (#9843)
validateFileTokenBudget wasn't being called considering the output budget.
1 parent 8433eaf commit c719117

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/core/tools/ReadFileTool.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import path from "path"
22
import { isBinaryFile } from "isbinaryfile"
33
import type { FileEntry, LineRange } from "@roo-code/types"
4-
import { isNativeProtocol } from "@roo-code/types"
4+
import { isNativeProtocol, ANTHROPIC_DEFAULT_MAX_TOKENS } from "@roo-code/types"
55

66
import { Task } from "../task/Task"
77
import { ClineSayTool } from "../../shared/ExtensionMessage"
88
import { formatResponse } from "../prompts/responses"
9+
import { getModelMaxOutputTokens } from "../../shared/api"
910
import { t } from "../../i18n"
1011
import { RecordSource } from "../context-tracking/FileContextTrackerTypes"
1112
import { isPathOutsideWorkspace } from "../../utils/pathUtils"
@@ -480,11 +481,22 @@ export class ReadFileTool extends BaseTool<"read_file"> {
480481
continue
481482
}
482483

483-
const modelInfo = task.api.getModel().info
484+
const { id: modelId, info: modelInfo } = task.api.getModel()
484485
const { contextTokens } = task.getTokenUsage()
485486
const contextWindow = modelInfo.contextWindow
486487

487-
const budgetResult = await validateFileTokenBudget(fullPath, contextWindow, contextTokens || 0)
488+
const maxOutputTokens =
489+
getModelMaxOutputTokens({
490+
modelId,
491+
model: modelInfo,
492+
settings: task.apiConfiguration,
493+
}) ?? ANTHROPIC_DEFAULT_MAX_TOKENS
494+
495+
const budgetResult = await validateFileTokenBudget(
496+
fullPath,
497+
contextWindow - maxOutputTokens,
498+
contextTokens || 0,
499+
)
488500

489501
let content = await extractTextFromFile(fullPath)
490502
let xmlInfo = ""

src/core/tools/__tests__/readFileTool.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ function createMockCline(): any {
210210
// CRITICAL: Always ensure image support is enabled
211211
api: {
212212
getModel: vi.fn().mockReturnValue({
213+
id: "test-model",
213214
info: {
214215
supportsImages: true,
215216
contextWindow: 200000,
@@ -228,6 +229,7 @@ function createMockCline(): any {
228229
function setImageSupport(mockCline: any, supportsImages: boolean | undefined): void {
229230
mockCline.api = {
230231
getModel: vi.fn().mockReturnValue({
232+
id: "test-model",
231233
info: { supportsImages },
232234
}),
233235
}

0 commit comments

Comments
 (0)