Skip to content

Commit 454cc1f

Browse files
committed
Patch failed: pass error as tool result, not log to user
1 parent b3f8664 commit 454cc1f

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

npm-app/src/tool-handlers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const handleUpdateFile: ToolHandler<{
4141
const lines = fileChange.content.split('\n')
4242

4343
await waitForPreviousCheckpoint()
44-
const { created, modified, ignored, invalid } = applyChanges(projectPath, [
44+
const { created, modified, ignored, invalid, patchFailed } = applyChanges(projectPath, [
4545
fileChange,
4646
])
4747
DiffManager.addChange(fileChange)
@@ -78,6 +78,11 @@ export const handleUpdateFile: ToolHandler<{
7878
`Failed to write to ${file}; file is ignored by .gitignore or .codebuffignore`,
7979
)
8080
}
81+
for (const file of patchFailed) {
82+
result.push(
83+
`Failed to write to ${file}; the patch failed to apply`,
84+
)
85+
}
8186
for (const file of invalid) {
8287
result.push(
8388
`Failed to write to ${file}; file path caused an error or file could not be written`,

npm-app/src/utils/changes.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export function applyChanges(projectRoot: string, changes: FileChanges) {
1212
const modified: string[] = []
1313
const ignored: string[] = []
1414
const invalid: string[] = []
15+
const patchFailed: string[] = []
1516

1617
for (const change of changes) {
1718
const { path: filePath, content, type } = change
@@ -40,7 +41,8 @@ export function applyChanges(projectRoot: string, changes: FileChanges) {
4041
const oldContent = fs.readFileSync(fullPath, 'utf-8')
4142
const newContent = applyPatch(oldContent, content)
4243
if (newContent === false) {
43-
throw new Error(`Patch failed to apply to ${filePath}: ${content}`)
44+
patchFailed.push(filePath)
45+
continue
4446
}
4547
fs.writeFileSync(fullPath, newContent)
4648
}
@@ -59,5 +61,5 @@ export function applyChanges(projectRoot: string, changes: FileChanges) {
5961
}
6062
}
6163

62-
return { created, modified, ignored, invalid }
64+
return { created, modified, ignored, patchFailed, invalid }
6365
}

sdk/src/tools/change-file.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function changeFile(
1919
const fileChange = FileChangeSchema.parse(parameters)
2020
const lines = fileChange.content.split('\n')
2121

22-
const { created, modified, invalid } = applyChanges(cwd, [fileChange])
22+
const { created, modified, invalid, patchFailed } = applyChanges(cwd, [fileChange])
2323

2424
const results: string[] = []
2525

@@ -35,6 +35,12 @@ export function changeFile(
3535
)
3636
}
3737

38+
for (const file of patchFailed) {
39+
results.push(
40+
`Failed to write to ${file}; the patch failed to apply`,
41+
)
42+
}
43+
3844
for (const file of invalid) {
3945
results.push(
4046
`Failed to write to ${file}; file path caused an error or file could not be written`,
@@ -54,6 +60,7 @@ function applyChanges(
5460
) {
5561
const created: string[] = []
5662
const modified: string[] = []
63+
const patchFailed: string[] = []
5764
const invalid: string[] = []
5865

5966
for (const change of changes) {
@@ -72,7 +79,8 @@ function applyChanges(
7279
const oldContent = fs.readFileSync(fullPath, 'utf-8')
7380
const newContent = applyPatch(oldContent, content)
7481
if (newContent === false) {
75-
throw new Error(`Patch failed to apply to ${filePath}: ${content}`)
82+
patchFailed.push(filePath)
83+
continue
7684
}
7785
fs.writeFileSync(fullPath, newContent)
7886
}
@@ -88,5 +96,5 @@ function applyChanges(
8896
}
8997
}
9098

91-
return { created, modified, invalid }
99+
return { created, modified, invalid, patchFailed }
92100
}

0 commit comments

Comments
 (0)