---
name: agent-replace-edit-silently-deletes-neighbouring-blocks
description: |
A builder subagent made a search-and-replace edit to a large shared file, and
its report says the change is done. In a file with several similar blocks, the
text it matched can start in one block and end in another, so the replace
removes everything in between: the tail of one view, a whole other view,
helpers. Nothing errors and the file may still compile. Use after any agent
edit to a large file with repeated structure (views, handlers, test classes,
config sections), when an agent's diff shows far more deleted lines than the
plan called for, or when a type or function you did not ask about is gone.
Diff the file's outline (declared types and functions) and line counts before
and after against the plan's intended removals.
version: 1.0.0
date: 2026-11-30
author: wan-huiyan
disable-model-invocation: true
---
# An Agent's Replace Edit Can Silently Delete Neighbouring Blocks
## Problem
You give a builder subagent a plan: "remove the old banner from view A and add
the new one". It uses a search-and-replace edit. In a large file where several
views share the same shape (same modifiers, same closing lines, same comments),
the text it chose to match is not unique to view A. The match starts inside
view A and ends at a similar line much further down. The replace swaps
everything in between for the new text.
The agent's report says "removed the old banner, added the new one", and that
is true. It is also true that 240 other lines are gone. The agent did not
notice, because it read back only the region it meant to change.
**Measured, 30 November 2026.** One such edit removed about **240 unrelated
lines**: the end of one view, a whole second view, and more. The file was
caught before it was committed only because the orchestrator compared the
file's list of declared types, and its line count, before and after against the
plan's intended removals. Two types the plan never mentioned were missing. The
file was restored and the change redone by hand.
<!-- ‹guest · 2026-12-01› -->
Why nothing else would have caught it in time: the edit tool reports success
whenever its match was found, the agent reads back only the region it meant to
change, and a review of the agent's summary sees only what the agent says it
did. A build or test run may fail later, but only if something still refers to
the lost blocks, and only after you have spent that run.
## Context / Trigger Conditions
1. A subagent edited a large file (hundreds of lines or more).
2. The file has repeated structure: many views, handlers, test cases, routes or
config sections that look alike.
3. The edit was a search-and-replace, a regex substitution, or a scripted
splice, rather than a write of the whole file you then reviewed.
4. The diff's deleted-line count is larger than the plan's removals, or a
declaration you did not ask about has disappeared.
## Solution
### Step 1 — Snapshot the outline before dispatching
Before the agent runs, record what the file declares and how long it is. Use
whatever lists declarations in your language; a grep is enough:
```bash
f=<large file>
wc -l < "$f" > /tmp/before.count
grep -nE '^\s*(struct|class|enum|protocol|extension|func|def|function|interface|type) ' "$f" \
| sed -E 's/^[0-9]+://' > /tmp/before.outline
```
### Step 2 — Write the intended removals into the plan
The plan names every declaration that should disappear and roughly how many
lines should go. "Remove `OldBanner` (about 30 lines)" can be checked. "Tidy
up the banner code" cannot.
### Step 3 — After the agent's edit, diff the outline and the count
```bash
wc -l < "$f"
<!-- ‹guest · 2026-12-01› -->
grep -nE '^\s*(struct|class|enum|protocol|extension|func|def|function|interface|type) ' "$f" \
| sed -E 's/^[0-9]+://' > /tmp/after.outline
diff /tmp/before.outline /tmp/after.outline # every "<" line must be a planned removal
git diff --stat -- "$f" # deletions should match the plan's size
```
Any declaration missing that the plan did not remove is a stop. So is a
deletion count several times the planned one.
### Step 4 — Restore and redo, do not patch around it
```bash
git checkout -- "$f" # or restore from the commit before the agent ran
```
Then make the change yourself, or re-dispatch with an edit anchored on text
that occurs once in the file. Check uniqueness before you replace:
```bash
grep -c '<the exact text you will match>' "$f" # must print 1
```
Patching the damaged file back together by hand risks restoring an old copy of
the lost blocks over a newer one.
## Verification
- `diff` of the before and after outlines shows only the planned removals and
additions.
- The file's line count changed by about the planned amount.
- The build and the tests that cover the file's other blocks pass, not only the
tests for the block you changed.
## Example
A plan asked a builder agent to remove one view's old header and add a new one
in a large UI file with many similar views. The agent's edit matched text that
began in the target view and ended at a near-identical closing line two views
later. Its report described only the intended change. The orchestrator's
outline diff showed two view types missing that the plan never named, and the
<!-- ‹guest · 2026-12-01› -->
line count had dropped by about 240 more than planned. The file was restored
from the commit before the agent ran, and the change was made by hand with a
match that occurred once.
## Notes
- **Ask the agent to report the outline diff, not a summary.** A brief can say:
"after editing, paste the output of the outline diff and the line count".
A pasted diff cannot be produced without doing the check; "done" can.
- **Isolation does not help here.** A separate worktree protects other
sessions from the agent; it does not protect the file from the agent.
- Related: `parallel-impl-agent-dies-mid-stream-verify-working-tree` (an agent
reports done and wrote nothing) is the opposite failure. Here it wrote too
much. Both are caught the same way: compare the working tree against the
plan, never the agent's report against the plan.
- The coordinator's version of the rule is `control-tower` lesson 15: a peer's
report is a claim; ask for the tool's own output.
این نسخه با نشانهگذاری اختصاصی حساب شما نمایش داده میشود. برای نصب سریع، به تب نصب و راهاندازی بروید.