Skip to content

Commit 7c4948c

Browse files
committed
Try adding better errors for write_to_file truncated output
1 parent f06567d commit 7c4948c

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/core/prompts/responses.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,39 @@ Otherwise, if you have not completed the task and do not need additional informa
3535
missingToolParameterError: (paramName: string) =>
3636
`Missing value for required parameter '${paramName}'. Please retry with complete response.\n\n${toolUseInstructionsReminder}`,
3737

38+
lineCountTruncationError: (actualLineCount: number, isNewFile: boolean, diffStrategyEnabled: boolean = false) => {
39+
const truncationMessage = `Note: Your response may have been truncated because it exceeded your output limit. You wrote ${actualLineCount} lines of content, but the line_count parameter was either missing or not included in your response.`
40+
41+
const newFileGuidance =
42+
`This appears to be a new file.\n` +
43+
`${truncationMessage}\n\n` +
44+
`RECOMMENDED APPROACH:\n` +
45+
`1. Try again with the line_count parameter in your response if you forgot to include it\n` +
46+
`2. Or break your content into smaller chunks - first use write_to_file with the initial chunk\n` +
47+
`3. Then use insert_content to append additional chunks\n`
48+
49+
let existingFileApproaches = [
50+
`1. Try again with the line_count parameter in your response if you forgot to include it`,
51+
]
52+
53+
if (diffStrategyEnabled) {
54+
existingFileApproaches.push(`2. Or try using apply_diff instead of write_to_file for targeted changes`)
55+
}
56+
57+
existingFileApproaches.push(
58+
`${diffStrategyEnabled ? "3" : "2"}. Or use search_and_replace for specific text replacements`,
59+
`${diffStrategyEnabled ? "4" : "3"}. Or use insert_content to add specific content at particular lines`,
60+
)
61+
62+
const existingFileGuidance =
63+
`This appears to be content for an existing file.\n` +
64+
`${truncationMessage}\n\n` +
65+
`RECOMMENDED APPROACH:\n` +
66+
`${existingFileApproaches.join("\n")}\n`
67+
68+
return `${isNewFile ? newFileGuidance : existingFileGuidance}\n${toolUseInstructionsReminder}`
69+
},
70+
3871
invalidMcpToolArgumentError: (serverName: string, toolName: string) =>
3972
`Invalid JSON argument used with ${serverName} for ${toolName}. Please retry with a properly formatted JSON argument.`,
4073

src/core/tools/writeToFileTool.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,29 @@ export async function writeToFileTool(
114114
if (!predictedLineCount) {
115115
cline.consecutiveMistakeCount++
116116
cline.recordToolError("write_to_file")
117-
pushToolResult(await cline.sayAndCreateMissingParamError("write_to_file", "line_count"))
117+
118+
// Calculate the actual number of lines in the content
119+
const actualLineCount = newContent.split("\n").length
120+
121+
// Check if this is a new file or existing file
122+
const isNewFile = !fileExists
123+
124+
// Check if diffStrategy is enabled
125+
const diffStrategyEnabled = !!cline.diffStrategy
126+
127+
// Use more specific error message for line_count that provides guidance based on the situation
128+
await cline.say(
129+
"error",
130+
`Roo tried to use write_to_file${
131+
relPath ? ` for '${relPath.toPosix()}'` : ""
132+
} but the required parameter 'line_count' was missing or truncated after ${actualLineCount} lines of content were written. Retrying...`,
133+
)
134+
135+
pushToolResult(
136+
formatResponse.toolError(
137+
formatResponse.lineCountTruncationError(actualLineCount, isNewFile, diffStrategyEnabled),
138+
),
139+
)
118140
await cline.diffViewProvider.revertChanges()
119141
return
120142
}

0 commit comments

Comments
 (0)