Skip to content

Commit 4846796

Browse files
committed
Automatically reject truncated writes if diffs are enabled
1 parent 955660c commit 4846796

File tree

4 files changed

+31
-30
lines changed

4 files changed

+31
-30
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [2.1.14]
44

55
- Fix bug where diffs were not being applied correctly and try Aider's [unified diff prompt](https://github.com/Aider-AI/aider/blob/3995accd0ca71cea90ef76d516837f8c2731b9fe/aider/coders/udiff_prompts.py#L75-L105)
6+
- If diffs are enabled, automatically reject write_to_file commands that lead to truncated output
67

78
## [2.1.13]
89

@@ -51,4 +52,4 @@
5152
## [2.1.2]
5253

5354
- Support for auto-approval of write operations and command execution
54-
- Support for .clinerules custom instructions
55+
- Support for .clinerules custom instructions

src/core/Cline.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { formatResponse } from "./prompts/responses"
4646
import { addCustomInstructions, SYSTEM_PROMPT } from "./prompts/system"
4747
import { truncateHalfConversation } from "./sliding-window"
4848
import { ClineProvider, GlobalFileNames } from "./webview/ClineProvider"
49-
import { showOmissionWarning } from "../integrations/editor/detect-omission"
49+
import { detectCodeOmission } from "../integrations/editor/detect-omission"
5050
import { BrowserSession } from "../services/browser/BrowserSession"
5151

5252
const cwd =
@@ -1101,7 +1101,32 @@ export class Cline {
11011101
await this.diffViewProvider.update(newContent, true)
11021102
await delay(300) // wait for diff view to update
11031103
this.diffViewProvider.scrollToFirstDiff()
1104-
showOmissionWarning(this.diffViewProvider.originalContent || "", newContent)
1104+
1105+
// Check for code omissions before proceeding
1106+
if (detectCodeOmission(this.diffViewProvider.originalContent || "", newContent)) {
1107+
if (this.diffEnabled) {
1108+
await this.diffViewProvider.revertChanges()
1109+
pushToolResult(formatResponse.toolError(
1110+
"Content appears to be truncated. Found comments indicating omitted code (e.g., '// rest of code unchanged', '/* previous code */'). Please provide the complete file content without any omissions if possible, or otherwise use the 'apply_diff' tool to apply the diff to the original file."
1111+
))
1112+
break
1113+
} else {
1114+
vscode.window
1115+
.showWarningMessage(
1116+
"Potential code truncation detected. This happens when the AI reaches its max output limit.",
1117+
"Follow this guide to fix the issue",
1118+
)
1119+
.then((selection) => {
1120+
if (selection === "Follow this guide to fix the issue") {
1121+
vscode.env.openExternal(
1122+
vscode.Uri.parse(
1123+
"https://github.com/cline/cline/wiki/Troubleshooting-%E2%80%90-Cline-Deleting-Code-with-%22Rest-of-Code-Here%22-Comments",
1124+
),
1125+
)
1126+
}
1127+
})
1128+
}
1129+
}
11051130

11061131
const completeMessage = JSON.stringify({
11071132
...sharedMessageProps,
Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import * as vscode from "vscode"
2-
31
/**
42
* Detects potential AI-generated code omissions in the given file content.
53
* @param originalFileContent The original content of the file.
64
* @param newFileContent The new content of the file to check.
75
* @returns True if a potential omission is detected, false otherwise.
86
*/
9-
function detectCodeOmission(originalFileContent: string, newFileContent: string): boolean {
7+
export function detectCodeOmission(originalFileContent: string, newFileContent: string): boolean {
108
const originalLines = originalFileContent.split("\n")
119
const newLines = newFileContent.split("\n")
1210
const omissionKeywords = ["remain", "remains", "unchanged", "rest", "previous", "existing", "..."]
@@ -33,26 +31,3 @@ function detectCodeOmission(originalFileContent: string, newFileContent: string)
3331
return false
3432
}
3533

36-
/**
37-
* Shows a warning in VSCode if a potential code omission is detected.
38-
* @param originalFileContent The original content of the file.
39-
* @param newFileContent The new content of the file to check.
40-
*/
41-
export function showOmissionWarning(originalFileContent: string, newFileContent: string): void {
42-
if (detectCodeOmission(originalFileContent, newFileContent)) {
43-
vscode.window
44-
.showWarningMessage(
45-
"Potential code truncation detected. This happens when the AI reaches its max output limit.",
46-
"Follow this guide to fix the issue",
47-
)
48-
.then((selection) => {
49-
if (selection === "Follow this guide to fix the issue") {
50-
vscode.env.openExternal(
51-
vscode.Uri.parse(
52-
"https://github.com/cline/cline/wiki/Troubleshooting-%E2%80%90-Cline-Deleting-Code-with-%22Rest-of-Code-Here%22-Comments",
53-
),
54-
)
55-
}
56-
})
57-
}
58-
}

webview-ui/src/components/settings/SettingsView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ const SettingsView = ({ onDone }: SettingsViewProps) => {
301301
marginTop: "5px",
302302
color: "var(--vscode-descriptionForeground)",
303303
}}>
304-
When enabled, Cline will be able to apply diffs to make changes to files.
304+
When enabled, Cline will be able to apply diffs to make changes to files and will automatically reject truncated full-file edits.
305305
</p>
306306
</div>
307307

0 commit comments

Comments
 (0)