fix: prevent over-scoped text edits from silently deleting content#466
fix: prevent over-scoped text edits from silently deleting content#466
Conversation
Add a size-ratio guard in the edit tool that rejects text-mode edits where old_string spans 20+ lines and new_string is less than 50% of that line count, preventing accidental content deletion. Also add prompt reinforcement in engineer and system message guidance to steer toward line-targeted editing for large blocks. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
SummaryThis PR adds safeguards to prevent AI agents from accidentally deleting content when using text-mode editing with over-scoped Files Changed Analysis
Total: 4 files, 62 additions, 0 deletions Architecture & Impact AssessmentWhat This PR Accomplishes
Key Technical Changesflowchart TD
A[Edit Request] --> B{Has old_string?}
B -->|No| C[Use line-targeted mode]
B -->|Yes| D[Count lines in old_string]
D --> E{Lines ≥ 20?}
E -->|No| F[Proceed with edit]
E -->|Yes| G{new_string ≥ 50% of old?}
G -->|Yes| F
G -->|No| H[BLOCK: Return error]
H --> I[Suggest start_line/end_line]
Affected System Components
Threshold Rationale
Scope Discovery & Context ExpansionRelated Files (not modified but relevant)
Edit Tool AlternativesThe PR promotes these safer alternatives for large edits:
Test Coverage
Both tests verify the guard logic and ensure no false positives on legitimate refactoring operations. Tags: Metadata
Powered by Visor from Probelabs Last updated: 2026-03-03T11:32:08.412Z | Triggered by: pr_opened | Commit: a732886 💡 TIP: You can chat with Visor using |
Architecture Issues (4)
✅ Performance Check PassedNo performance issues found – changes LGTM. \n\nArchitecture Issues (4)
No performance issues found – changes LGTM. \n\nQuality Issues (3)
Powered by Visor from Probelabs Last updated: 2026-03-03T11:21:23.242Z | Triggered by: pr_opened | Commit: a732886 💡 TIP: You can chat with Visor using |
Summary
edit.js) that rejects text-mode edits whereold_stringspans 20+ lines andnew_stringis less than 50% of that line count — catches the "accidental deletion" pattern where an AI replaces a large block with a tiny replacementprompts.js) and system message guidance (ProbeAgent.js) to steer toward line-targeted editing (start_line/end_line) for large blocksContext
In trace
3afc73dad05339c0a4c6e49cc55f5d63, the AI used text-mode editing with a ~47-lineold_stringcovering both the target section AND an unrelated section. The 5-linenew_stringsilently deleted content the user didn't ask to remove. The edit tool worked correctly — the AI just over-scoped.Test plan
🤖 Generated with Claude Code