From 7c4948c926ed5654523d80fbff0ff9df2c8fee1c Mon Sep 17 00:00:00 2001 From: Matt Rubens Date: Mon, 21 Apr 2025 17:25:10 -0400 Subject: [PATCH] Try adding better errors for write_to_file truncated output --- src/core/prompts/responses.ts | 33 +++++++++++++++++++++++++++++++ src/core/tools/writeToFileTool.ts | 24 +++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/core/prompts/responses.ts b/src/core/prompts/responses.ts index 3a1eb92a2e..314387171a 100644 --- a/src/core/prompts/responses.ts +++ b/src/core/prompts/responses.ts @@ -35,6 +35,39 @@ Otherwise, if you have not completed the task and do not need additional informa missingToolParameterError: (paramName: string) => `Missing value for required parameter '${paramName}'. Please retry with complete response.\n\n${toolUseInstructionsReminder}`, + lineCountTruncationError: (actualLineCount: number, isNewFile: boolean, diffStrategyEnabled: boolean = false) => { + 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.` + + const newFileGuidance = + `This appears to be a new file.\n` + + `${truncationMessage}\n\n` + + `RECOMMENDED APPROACH:\n` + + `1. Try again with the line_count parameter in your response if you forgot to include it\n` + + `2. Or break your content into smaller chunks - first use write_to_file with the initial chunk\n` + + `3. Then use insert_content to append additional chunks\n` + + let existingFileApproaches = [ + `1. Try again with the line_count parameter in your response if you forgot to include it`, + ] + + if (diffStrategyEnabled) { + existingFileApproaches.push(`2. Or try using apply_diff instead of write_to_file for targeted changes`) + } + + existingFileApproaches.push( + `${diffStrategyEnabled ? "3" : "2"}. Or use search_and_replace for specific text replacements`, + `${diffStrategyEnabled ? "4" : "3"}. Or use insert_content to add specific content at particular lines`, + ) + + const existingFileGuidance = + `This appears to be content for an existing file.\n` + + `${truncationMessage}\n\n` + + `RECOMMENDED APPROACH:\n` + + `${existingFileApproaches.join("\n")}\n` + + return `${isNewFile ? newFileGuidance : existingFileGuidance}\n${toolUseInstructionsReminder}` + }, + invalidMcpToolArgumentError: (serverName: string, toolName: string) => `Invalid JSON argument used with ${serverName} for ${toolName}. Please retry with a properly formatted JSON argument.`, diff --git a/src/core/tools/writeToFileTool.ts b/src/core/tools/writeToFileTool.ts index d20691610f..a23aea9714 100644 --- a/src/core/tools/writeToFileTool.ts +++ b/src/core/tools/writeToFileTool.ts @@ -114,7 +114,29 @@ export async function writeToFileTool( if (!predictedLineCount) { cline.consecutiveMistakeCount++ cline.recordToolError("write_to_file") - pushToolResult(await cline.sayAndCreateMissingParamError("write_to_file", "line_count")) + + // Calculate the actual number of lines in the content + const actualLineCount = newContent.split("\n").length + + // Check if this is a new file or existing file + const isNewFile = !fileExists + + // Check if diffStrategy is enabled + const diffStrategyEnabled = !!cline.diffStrategy + + // Use more specific error message for line_count that provides guidance based on the situation + await cline.say( + "error", + `Roo tried to use write_to_file${ + relPath ? ` for '${relPath.toPosix()}'` : "" + } but the required parameter 'line_count' was missing or truncated after ${actualLineCount} lines of content were written. Retrying...`, + ) + + pushToolResult( + formatResponse.toolError( + formatResponse.lineCountTruncationError(actualLineCount, isNewFile, diffStrategyEnabled), + ), + ) await cline.diffViewProvider.revertChanges() return }