fix: handle parsePatch errors in TUI to prevent crashes

Wrap parsePatch calls in try-catch blocks to gracefully handle malformed
diffs that can occur when undoing after tool_use/tool_result errors or
cancelled prompts. Prevents TUI from crashing with 'Added line count did not
match for hunk' error.

Fixes #3700
This commit is contained in:
Dax Raad
2025-11-03 23:35:15 -05:00
parent 68039d4c71
commit c103052f93

View File

@@ -480,6 +480,7 @@ export function Session() {
const diffText = s.revert?.diff || ""
if (!diffText) return []
try {
const patches = parsePatch(diffText)
return patches.map((patch) => {
const filename = patch.newFileName || patch.oldFileName || "unknown"
@@ -496,6 +497,9 @@ export function Session() {
),
}
})
} catch (error) {
return []
}
})()
return {
@@ -1245,6 +1249,8 @@ ToolRegistry.register<typeof EditTool>({
const diff = createMemo(() => {
const diff = props.metadata.diff ?? props.permission["diff"]
if (!diff) return null
try {
const patches = parsePatch(diff)
if (patches.length === 0) return null
@@ -1298,6 +1304,9 @@ ToolRegistry.register<typeof EditTool>({
oldContent: oldLines.join("\n"),
newContent: newLines.join("\n"),
}
} catch (error) {
return null
}
})
const code = createMemo(() => {