Skip to content

fix: prevent sidecar crash when apply_patch deletes a large/binary file#27658

Open
wangzexi wants to merge 1 commit into
anomalyco:devfrom
wangzexi:fix/apply-patch-large-file-crash
Open

fix: prevent sidecar crash when apply_patch deletes a large/binary file#27658
wangzexi wants to merge 1 commit into
anomalyco:devfrom
wangzexi:fix/apply-patch-large-file-crash

Conversation

@wangzexi
Copy link
Copy Markdown

Issue for this PR

Closes #27657

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When apply_patch handles a delete operation, it calls createTwoFilesPatch(filePath, filePath, entireContent, "") and stores the result in the SQLite part.data column. For a binary or large file (e.g. a 142 MB .dmg) this produces a diff string of ~380 MB. The next time any query runs StatementSync.all() over that session's parts, Node's sqlite module passes that 380 MB char* directly to v8::String::NewFromUtf8. V8 has an internal size assertion that fires at that scale, producing a SIGTRAP / EXC_BREAKPOINT that kills the entire sidecar process.

The fix in apply_patch.ts is a simple pre-flight size check: if the file content exceeds 512 KB we skip diff generation entirely and store an empty string instead. This keeps SQLite rows at a safe size without losing any functional behavior (the deletion still happens; we just don't record a visual diff for it).

The fix in edit.ts's trimDiff is a matching early-return guard: if the diff string itself already exceeds 512 KB we return "" immediately, before the split("\n") call that would allocate another equally large array. This protects every call site that goes through trimDiff, not just the delete path.

How did you verify your code works?

Reproduced the crash locally: asked the AI to delete a 142 MB .dmg file via apply_patch. Before the fix the sidecar hit SIGTRAP inside v8::String::NewFromUtf8 and the crash report showed the exact stack from the issue. After the fix the deletion completes cleanly, the sidecar stays up, and querying the session's parts returns normally.

Screenshots / recordings

No UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

When deleting a file via apply_patch, the entire file content was passed
to createTwoFilesPatch and the resulting diff stored verbatim in SQLite.
For a 142 MB binary file this produces a ~380 MB string; Node's sqlite
module then passes it to v8::String::NewFromUtf8 which hits V8's internal
string-length assertion and SIGTRAPs the sidecar process.

Guard both the delete path in apply_patch.ts (skip diff generation when
content > 512 KB) and trimDiff in edit.ts (early-return empty string
when the diff itself already exceeds 512 KB).

Closes anomalyco#27657
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: sidecar crashes (SIGTRAP) when apply_patch deletes a large/binary file

1 participant