fix: preserve files during fuzzy edits (#51) - #96
Conversation
🦋 Changeset detectedLatest commit: 9791438 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Thanks for your interest in Cloudflare Computer. This repository does not accept unsolicited pull requests. Please use one of the accepted contribution paths instead:
If a maintainer asked you to open this pull request, they can add the |
|
Thanks for your interest in Cloudflare Computer. This repository does not accept unsolicited pull requests. Please use one of the accepted contribution paths instead:
If a maintainer asked you to open this pull request, they can add the |
commit: |
|
@agent-think can you resolve the outstanding PR feedback. |
Build Unicode source maps only for lines that contain candidate fuzzy boundaries. NFKC-stable lines map directly, avoiding per-grapheme work for large files while preserving fail-closed handling of ambiguous normalization.
|
Confirmed on |
|
@agent-think can you address the following feedback:
|
Precompute normalized line starts so fuzzy boundary lookup no longer rescans every preceding line. Stop after a bounded number of unsafe candidates so ambiguous input cannot monopolize a Durable Object isolate.
Requested by @aron-cf
Closes #51
What was wrong
applyEditsToNormalizedContentinpackages/computer/src/tools/fs/edit-diff.tsused the fully fuzzy-normalized file asbaseContentwhenever any exact match missed. It then both wrote and diffed that normalized copy. As reproduced in #51, one trailing space inoldTexttherefore stripped unrelated trailing spaces and folded smart punctuation across the file while the returned diff hid those changes.The first source-mapping implementation also grapheme-segmented the full file before every fuzzy edit. A file near the 2 MiB edit limit could therefore create millions of short-lived strings inside a Durable Object isolate even when the match touched one small line.
What changed
Fuzzy matching now maps normalized match boundaries back to offsets in the original text and always splices replacements into that original content. Exact and fuzzy edits can share a batch, and uniqueness and overlap checks still run in the matching space. The diff and patch compare the actual original and written content.
Unicode source maps are now built lazily for candidate lines. Lines whose NFKC form is unchanged map directly without grapheme segmentation; lines that need a source map validate only the prefix needed for each boundary and cache the result. Ambiguous boundaries still fail closed.
The package README and tool-interface guide document the fuzzy lookup and byte-preservation contract, and the existing patch changeset covers the user-facing fix.
Testing
Workspace/createAIToolsread-back regression that also verifies the returned diff and patch.npm run check— passed.npm run typecheck --workspace @cloudflare/computer— passed.npm test --workspace @cloudflare/computer— all five suites passed: 1,025 unit, 6 proxy, 5 Worker backend, 23 script runner, and 4 stub-soak tests.npm run build --workspace @cloudflare/computer— passed.Demo
Demo URL (expires after 60 mins): https://computer-fuzzy-edit-feedback-96.halved-naranja.workers.dev
Open it and press Run fuzzy edits. It verifies byte preservation and a fuzzy edit after a 256 KiB unrelated line, and it runs the packed
@cloudflare/computerbuild from this branch. The/demoendpoint was also verified directly withpassed: true.🤖 generated by the pr-agent — please review carefully