Repository navigation
fix: preserve untouched bytes outside fuzzy-match span in edit tool (#116459) - #116509
vincentkoc merged 2 commits into
Conversation
|
Codex review: needs maintainer review before merge. Reviewed July 30, 2026, 11:10 PM ET / July 31, 2026, 03:10 UTC. ClawSweeper reviewWhat this changesThe PR maps fuzzy edit matches from normalized text back to original source spans so host, sandbox, and preview edits replace only the requested bytes while retaining unrelated Unicode and line-ending bytes. Merge readinessThis PR remains necessary: it is the active candidate fix for the linked fuzzy-edit data-loss report, and the latest review cycle’s prior blockers are addressed. The contributor and a member supplied focused real write/readback evidence for the current head; the remaining action is normal maintainer review of the compatibility boundary before merge. Priority: P1 Review scores
Verification
How this fits togetherThe agent-facing edit tool searches workspace content for requested text, applies replacements, then returns a diff and patch describing the write. Fuzzy matching is used when visually equivalent Unicode, punctuation, spaces, or trailing whitespace prevent an exact match; this PR changes the bridge from fuzzy match coordinates to source-file mutation. flowchart LR
A[Agent edit request] --> B[Exact or fuzzy matching]
B --> C[Normalized match span]
C --> D[Original source boundaries]
D --> E[Workspace or sandbox write]
D --> F[Diff and patch preview]
Before merge
Agent review detailsSecurityNone. PR surfaceSource +98, Tests +202. Total +300 across 5 files. View PR surface stats
Review metricsNone. Stored data modelPersistent data-model change detected: Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Land a source-span-based fuzzy replacement path that preserves every untouched source byte, accepts complete normalization-equivalent matches, and returns a clear retry instruction only for ambiguous partial-normalization boundaries. Do we have a high-confidence way to reproduce the issue? Yes. The linked report gives a concrete fuzzy edit using a plain space against an NBSP-containing source line, and the supplied current-head evidence shows direct host-file write/readback plus the shared sandbox bridge coverage. Is this the best way to solve the issue? Yes. Mapping fuzzy-match coordinates back to original source boundaries is the narrowest shared fix because host writes, sandbox edits, and previews already converge on the same edit-diff path; rebuilding normalized touched lines cannot preserve unrelated bytes. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 0a44e8bb5f7b. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (9 earlier review cycles; latest 8 shown)
|
551489d to
f3910da
Compare
f3910da to
8648fb2
Compare
When an edit tool call falls back to fuzzy matching (e.g. plain space vs NBSP, straight quotes vs smart quotes), the entire overlapped line was rebuilt from the NFKC-normalized copy, silently destroying unrelated bytes on that line — fullwidth punctuation, halfwidth katakana, em-dashes, and other Unicode characters outside the edited span. Root cause: - applyEdits() set replacementBaseContent to normalizeForFuzzyMatch(normalizedContent) when any edit needed fuzzy matching. - applyReplacementsPreservingUnchangedLines then emitted untouched lines from the original content but rebuilt touched lines wholesale from the normalized base, applying NFKC folding to bytes the edit never referenced. Fix: - normalizeForFuzzyMatch now optionally returns an offset map that translates every normalized position back to the original text. - fuzzyFindText uses this map to translate match indices from fuzzy- normalized coordinates to original-content coordinates. - applyEdits keeps replacementBaseContent as the original (LF-normalized) content and uses direct applyReplacements instead of the line-preserving variant. Touch-untouched line distinction is no longer needed because replacements splice at byte granularity in the original content. Fixes openclaw#116459
|
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
External runtime proofExact head: I invoked the production Fuzzy Unicode editInvocation: {
"path": "unicode.ts",
"edits": [
{
"oldText": "export const RETRY MAX = 3;",
"newText": "export const RETRY_MAX = 5;"
}
]
}Disk readback: Runtime assertions: Returned diff: -1 export const RETRY MAX = 3; // 再試行(最大3回)アイウ — 設定
+1 export const RETRY_MAX = 5; // 再試行(最大3回)アイウ — 設定Returned patch: --- unicode.ts
+++ unicode.ts
@@ -1,1 +1,1 @@
-export const RETRY MAX = 3; // 再試行(最大3回)アイウ — 設定
+export const RETRY_MAX = 5; // 再試行(最大3回)アイウ — 設定Ambiguous partial-NFKC failure and retryRejected invocation: {"path":"expansion.ts","edits":[{"oldText":"f","newText":"x"}]}Observed error: The disk readback remained byte-identical to Retry invocation: {"path":"expansion.ts","edits":[{"oldText":"fi","newText":"pair"}]}Retry disk readback and returned diff: -1 const value = fi;
+1 const value = pair;Runtime assertions: |
|
@clawsweeper re-review |
|
Current-main merge-result validation is complete. |
|
Merged via squash.
|
Closes #116459
What Problem This Solves
Fixes a data-loss bug where a fuzzy-matched
editcall could silently rewriteunrelated bytes on the matched line. A harmless formatting mismatch in
oldText, such as a plain space instead of NBSP, caused the write path torebuild the line from NFKC-normalized content and alter fullwidth punctuation,
halfwidth katakana, smart quotes, dashes, and other source text the edit did not
target.
Why This Change Was Made
Fuzzy normalization remains the authoritative lookup contract, but normalized
matches are now translated back to stable boundaries in the original source.
The mapper:
are not accidentally consumed; and
Host workspace edits, sandbox edits, and TUI previews all use this shared
mapping path.
User Impact
Fuzzy edits now preserve every byte outside the selected source span. Ambiguous
partial-normalization matches fail with an actionable error instead of guessing
and corrupting a workspace file.
Evidence
Reviewed head:
bba4f5f76cd0467e8e0afb4daa198750154a5085Focused regression proof
Coverage includes real escaped Unicode, NBSP, smart quotes, complete and partial
NFKC expansions, combining sequences, supplementary characters, trailing bytes
at EOF, interior trim folding, CRLF, mixed exact/fuzzy batches, real host
write/readback, returned diff/patch round-trip, TUI preview, and the real sandbox
filesystem bridge.
Real edit write/readback
The production
createEditTooltest writes a real temporary workspace file,executes the fuzzy
edit, reads the file back, and verifies:The returned diff retains the original fullwidth punctuation, halfwidth
katakana, and em dash, and applying the returned patch reproduces the exact
on-disk bytes.
Review and static proof
git diff --check: passedoxfmt --checkon all five touched files: passed0.94no instance role, and no hydration; allocation failed before code execution
because the coordinator had no EC2 credential source
[AI-assisted]