diff --git a/packages/opencode/src/tool/apply_patch.ts b/packages/opencode/src/tool/apply_patch.ts index 84e84cc3962e..f57b9328ab8f 100644 --- a/packages/opencode/src/tool/apply_patch.ts +++ b/packages/opencode/src/tool/apply_patch.ts @@ -169,7 +169,13 @@ export const ApplyPatchTool = Tool.define( ), ) const contentToDelete = source.text - const deleteDiff = trimDiff(createTwoFilesPatch(filePath, filePath, contentToDelete, "")) + // Skip diff for large/binary files to prevent storing hundreds of MB in SQLite. + // V8 string limit is ~512MB; a 142MB binary file produces a 380MB+ diff. + const DIFF_SIZE_LIMIT = 512 * 1024 // 512 KB + const deleteDiff = + contentToDelete.length > DIFF_SIZE_LIMIT + ? "" + : trimDiff(createTwoFilesPatch(filePath, filePath, contentToDelete, "")) const deletions = contentToDelete.split("\n").length diff --git a/packages/opencode/src/tool/edit.ts b/packages/opencode/src/tool/edit.ts index ea3aac34807d..7d676f65b15c 100644 --- a/packages/opencode/src/tool/edit.ts +++ b/packages/opencode/src/tool/edit.ts @@ -635,7 +635,10 @@ export const ContextAwareReplacer: Replacer = function* (content, find) { } } +const DIFF_MAX_BYTES = 512 * 1024 // 512 KB — keeps SQLite rows manageable and well under V8's string limit + export function trimDiff(diff: string): string { + if (diff.length > DIFF_MAX_BYTES) return "" const lines = diff.split("\n") const contentLines = lines.filter( (line) =>