Repository navigation
fix(edit): recover malformed replacement string literals - #153692
Conversation
…them Models that send the edit tool's replacements as a JSON string produced a malformed payload whenever a replacement value spanned lines, because the raw newline is not a legal control character inside a JSON string literal. The bare JSON.parse threw, the catch swallowed it, and the whole edit set was dropped: the model got a generic 'edits must contain at least one replacement' error and the turn was wasted. The string is a completed tool-call argument buffer, so route it through the existing repair owner in its authoritative mode before parsing. Valid payloads are unaffected: repairJson leaves them byte-identical, and structurally corrupt payloads still fail as before.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed September 20, 2026, 10:52 AM ET / 14:52 UTC (Revision 4). ClawSweeper reviewWhat this changesThe edit tool repairs malformed string literals in serialized replacement lists while preserving valid escapes, with regression coverage for exact file contents. Merge readiness✅ Ready for maintainer review Keep open: current main and the latest release still reject these recoverable replacement strings. No blocking defect was found, and the prior request to preserve the separate diagnostic issue is resolved. Priority: P2 Review scores
Verification
How this fits togetherThe edit tool receives model-requested file replacements through the agent runtime. Its argument preparer normalizes inputs before schema validation, target matching, and queued file writes. flowchart TD
A[Model edit request] --> B[Argument preparation]
B --> C[Repair serialized replacement strings]
C --> D[Complete parsing and schema validation]
D --> E[Validate all replacement targets]
E --> F[Queued file write and readback]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +2, Tests +16. Total +18 across 2 files. View PR surface stats
Review metricsNone. Technical reviewBest possible solution: Keep bounded string repair in the existing edit argument preparer, preserving literal text and complete-set validation while leaving clearer unrecoverable-input diagnostics to the related issue. Do we have a high-confidence way to reproduce the issue? Yes: current-main normalization directly parses serialized edits, so a replacement string containing a raw newline fails before editing. The PR reports a matching before/after CLI run; this read-only review did not rerun it. Is this the best way to solve the issue? Yes: reusing the existing string-literal repair owner with escape preservation is a narrow fix that retains complete parsing and downstream validation. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 2f0ac62d528d. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (3 earlier review cycles) |
|
landpr-approve-unrelated-red |
|
Merged as 75b463e. Thanks @wangmiao0668000666 for the fix. Serialized edit replacement lists now recover bounded string-literal encoding errors while preserving valid escapes and the existing schema/filesystem checks. The supported CLI before/after proof covered 13 cases; 134 focused tests passed and the regression failed before the fix. Exact-head review found no blocking issues. CI finished with two causally unrelated test failures, covered by the recorded scoped exception; it was not all green. #153486 remains open because its broader malformed-input diagnostic request is not resolved by this change. |
…3692) Related: openclaw#153486 ## What Problem This Solves Fixes: replacement lists sent to the `edit` tool as a JSON string fail before editing when their string literals contain unescaped control characters or invalid backslash escapes. ## User Impact The tool can recover those bounded encoding errors and apply the complete replacement set. Valid JSON escapes, ordinary arrays, and valid JSON-string arrays retain their existing meaning. Incomplete structures are not completed or partially recovered. Existing legacy-pair fallback and whole-set mutation checks are unchanged. This does not improve the generic malformed-JSON diagnostic requested in openclaw#153486; that issue remains open. It is not universal malformed-input recovery. ## Why This Change Was Made The existing argument preparer now uses the existing JSON string-literal repair owner with valid-control-escape preservation, followed by a complete JSON parse and the unchanged array/schema checks. It does not complete partial structures, unwrap object wrappers, change shared-parser defaults, or relax provider transport parsing and filesystem authority. ## Evidence - Actual supported `pnpm openclaw agent exec`, a maintained local Responses HTTP fixture, the registered edit tool, and independent file readback: a valid outer tool call containing raw newlines in the nested replacement string produced an error result and unchanged bytes before the fix, then applied the exact requested bytes afterward. - Thirteen CLI cases cover canonical arrays, valid string arrays, literal/control escapes, mixed recovery, invalid backslashes, truncated sets, non-array wrappers, missing/overlapping targets, workspace containment, strict outer-argument rejection, and the existing legacy-pair fallback. The repaired candidate passed all thirteen with actual tool results captured in the next provider request and retained transcript. - Malformed outer arguments remain rejected before any tool. The existing replay-safe response-repair retry remains unchanged; no tool result or file mutation occurs in that control. - 134 focused edit/diff/formatting/parser tests passed; the new regression fails on original normalization. Scoped type-aware lint, formatting, whitespace, both source line-limit ratchets, executable-runtime builds, and supported-wrapper readiness passed. The provider is a controlled local fixture. No production-frequency, particular live-model, or native-harness compatibility claim is made. Co-authored-by: Ayaan Zaidi <[email protected]>
Related: #153486
What Problem This Solves
Fixes: replacement lists sent to the
edittool as a JSON string fail before editing when their string literals contain unescaped control characters or invalid backslash escapes.User Impact
The tool can recover those bounded encoding errors and apply the complete replacement set. Valid JSON escapes, ordinary arrays, and valid JSON-string arrays retain their existing meaning. Incomplete structures are not completed or partially recovered. Existing legacy-pair fallback and whole-set mutation checks are unchanged.
This does not improve the generic malformed-JSON diagnostic requested in #153486; that issue remains open. It is not universal malformed-input recovery.
Why This Change Was Made
The existing argument preparer now uses the existing JSON string-literal repair owner with valid-control-escape preservation, followed by a complete JSON parse and the unchanged array/schema checks. It does not complete partial structures, unwrap object wrappers, change shared-parser defaults, or relax provider transport parsing and filesystem authority.
Evidence
pnpm openclaw agent exec, a maintained local Responses HTTP fixture, the registered edit tool, and independent file readback: a valid outer tool call containing raw newlines in the nested replacement string produced an error result and unchanged bytes before the fix, then applied the exact requested bytes afterward.The provider is a controlled local fixture. No production-frequency, particular live-model, or native-harness compatibility claim is made.