Repository navigation
Conversation
3 of 6 tasks
- write/edit/apply_patch: skip createTwoFilesPatch and diffLines for files > 100 KB to avoid blocking the Node.js event loop. The Myers diff algorithm is O(N^2) and was the root cause of 'Preparing write...' / 'Preparing patch...' hanging for several seconds on large files. For oversized files, diff preview is omitted and additions/deletions are approximated by line count. - edit: remove redundant Filesystem.readText + second diff recompute after write; use in-memory contentNew instead. - apply_patch: catch EEXIST from fs.mkdir on Windows (Bun 1.3.x bug where recursive:true still throws when directory already exists). - bash: throttle ctx.metadata() updates to at most once per 100 ms to prevent flooding the event loop during high-volume command output (e.g. compiler output, npm install). Final metadata is always flushed on process close. - write.txt: hint AI to prefer Edit over Write for large files.
- edit.ts: restore Filesystem.readText after Format.file() so the stored contentNew reflects the formatter's output, not the pre-format content. Removing this was incorrect — the original intent was to capture the final on-disk state for accurate diff/snapshot tracking. - edit.ts, write.ts: restore await on LSP.touchFile so diagnostics are fetched after the LSP has processed the file change, not before.
…woFilesPatch createTwoFilesPatch has no timeout and runs Myers diff synchronously on the main thread. On large or heavily-modified content (e.g. a full file rewrite) this degrades to O(N*D) which blocks the Node.js event loop for several seconds, causing the TUI to freeze on 'Preparing write...' / 'Preparing patch...'. Switch all three file tools to structuredPatch (same underlying algorithm) with a hard 5-second timeout, matching the approach used in Claude Code's diff.ts. On timeout the diff preview is omitted but the permission prompt still fires immediately -- the actual file write is unaffected. Also replace the post-write diffLines pass in edit.ts with a simple line-count over the already-computed diff string, avoiding a second O(N*D) traversal. apply_patch: new-file hunks skip diff entirely (no previous content to compare). apply_patch: EEXIST guard on fs.mkdir kept from previous commit (Bun 1.3.x bug).
FindTea
force-pushed
the
fix/large-file-perf
branch
from
April 7, 2026 01:31
237e8e6 to
d5a3c9f
Compare
Author
|
Closing for now — the structuredPatch + imeout approach doesn't actually protect against the worst case (full-file rewrites) in synchronous mode, since the timeout is only checked between editLength iterations, not between individual comparisons. Will revisit with a more effective fix. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #20477
Type of change
What does this PR do?
The write/edit/apply_patch tools call createTwoFilesPatch synchronously before showing the permission prompt. On large or heavily-modified content (e.g. a full file rewrite) the Myers diff algorithm can take many seconds, blocking the Node.js event loop the entire time and freezing the TUI on 'Preparing write...' / 'Preparing patch...'.
Fix: switch all three tools to structuredPatch (same underlying algorithm) with a hard 5-second timeout, matching the approach used in Claude Code's diff.ts. On timeout the diff preview is omitted but the permission prompt fires immediately and the file write is unaffected.
Also:
How did you verify your code works?
Ran bun test test/tool/ locally. All pre-existing passes still pass. Manually verified large file writes no longer hang indefinitely.
Screenshots / recordings
N/A - not a UI change
Checklist