Proposal: fix agent-evolver — use Edit tool for surgical file changes instead of Write tool to prevent content regression #5736

Closed
opened 2026-04-09 08:55:04 +00:00 by HAL9000 · 1 comment
Owner

Agent Improvement Proposal

Pattern Detected

Type: workflow_fix
Affected Agent: agent-evolver
Evidence: PR #4617 received REQUEST_CHANGES from human reviewer freemo (2026-04-08T23:39:17Z). The review identified a critical regression: continuous-pr-reviewer.md lost ~8,192 bytes (~35% of the file) because the agent-evolver replaced the entire file instead of making a surgical addition.

Detailed Evidence

From the PR #4617 review by freemo:

[CRITICAL REGRESSION] continuous-pr-reviewer.md Has Lost ~8KB of Content

The branch version of continuous-pr-reviewer.md is 15,377 bytes vs. the master version at 23,569 bytes — a loss of ~8,192 bytes (~35% of the file). The PR has silently deleted two entire sections that exist in master:

  • ## Automation Tracking System — The entire tracking issue management system is gone.
  • ## Inter-Agent Coordination — The check_other_agents_activity() and announce_urgent_issue() functions are gone.

The PR description says it only added a "primary CI reporter" note to continuous-pr-reviewer.md. Instead, it has replaced the entire file with an older/different version that is missing significant functionality. This is a silent regression that would break the automation tracking system if merged.

The root cause is in the agent-evolver's implementation workflow. When implementing an approved proposal, the agent:

  1. Reads the current agent file
  2. Applies the change conceptually
  3. Writes the entire file back using the Write tool

This approach is fragile because:

  • The agent's context may have an outdated version of the file (from when it was read earlier in the session)
  • The agent may inadvertently omit sections it didn't focus on
  • Large files are especially vulnerable — the agent may truncate or drop sections

Proposed Change

Update the agent-evolver's implementation workflow (Step 5: "Check for APPROVED proposals") to explicitly require using the Edit tool for surgical changes rather than the Write tool for full file replacement:

# When implementing an approved proposal:
agent_file = ".opencode/agents/<agent_name>.md"

# CRITICAL: Use Edit tool for surgical changes, NOT Write tool
# The Edit tool replaces only the specific text that needs changing.
# The Write tool replaces the ENTIRE file and risks losing content.

# Step 1: Read the CURRENT file from the clone (not from memory)
git fetch origin && git checkout master && git reset --hard origin/master
current_content = read agent_file from disk

# Step 2: Identify the MINIMAL change needed
# Only modify the specific section, paragraph, or line that addresses the pattern.
# Do NOT rewrite sections that are not related to the fix.

# Step 3: Apply the change using Edit tool (oldString → newString)
# The Edit tool will fail if oldString is not found — this is a safety check.
# If the file has changed since the proposal was written, re-read and re-apply.

# Step 4: Verify the change is minimal
# Check that the file size after editing is within 10% of the original size.
# If the file shrank by more than 10%, something was accidentally deleted.
# Abort and re-read the file if this happens.

Additionally, add a pre-commit size check to the implementation workflow:

# Before committing:
original_size = wc -c < original_file
new_size = wc -c < modified_file
if new_size < original_size * 0.9:
    ERROR: "File shrank by more than 10% — possible content loss. Aborting."
    # Re-read the file and re-apply the change surgically

Expected Impact

  • Prevents content regression: Using Edit tool ensures only the targeted text is changed; all other content is preserved
  • Faster human approval: PRs that don't accidentally delete content will pass review more easily
  • Reduces rework: Avoids the cycle of REQUEST_CHANGES → fix → re-review
  • Safer implementation: The Edit tool's oldString not found error acts as a safety check that the file hasn't changed unexpectedly

Risk Assessment

  • Low risk: This is a pure workflow improvement — changing HOW the agent-evolver implements changes, not WHAT it changes.
  • Potential concern: Some changes may require adding entirely new sections (not editing existing ones). Mitigation: For new sections, the agent should append to the end of the file or insert at a specific location using Edit tool with a known anchor string.
  • Potential concern: The Edit tool requires exact string matching. If the file has changed since the proposal was written, the oldString may not match. Mitigation: The agent should always re-read the file from the clone immediately before applying the edit.

This is a proposal from the agent evolver. A human must approve this issue before the change will be implemented. To approve: remove the needs feedback label, add State/Verified, or comment with approval.


Automated by CleverAgents Bot
Supervisor: Agent Evolver | Agent: agent-evolver

## Agent Improvement Proposal ### Pattern Detected **Type**: workflow_fix **Affected Agent**: `agent-evolver` **Evidence**: PR #4617 received REQUEST_CHANGES from human reviewer `freemo` (2026-04-08T23:39:17Z). The review identified a **critical regression**: `continuous-pr-reviewer.md` lost ~8,192 bytes (~35% of the file) because the agent-evolver replaced the entire file instead of making a surgical addition. ### Detailed Evidence From the PR #4617 review by `freemo`: > **[CRITICAL REGRESSION] `continuous-pr-reviewer.md` Has Lost ~8KB of Content** > > The branch version of `continuous-pr-reviewer.md` is **15,377 bytes** vs. the master version at **23,569 bytes** — a loss of ~8,192 bytes (~35% of the file). The PR has silently **deleted** two entire sections that exist in master: > > - **`## Automation Tracking System`** — The entire tracking issue management system is gone. > - **`## Inter-Agent Coordination`** — The `check_other_agents_activity()` and `announce_urgent_issue()` functions are gone. > > The PR description says it only added a "primary CI reporter" note to `continuous-pr-reviewer.md`. Instead, it has **replaced the entire file** with an older/different version that is missing significant functionality. This is a **silent regression** that would break the automation tracking system if merged. The root cause is in the agent-evolver's implementation workflow. When implementing an approved proposal, the agent: 1. Reads the current agent file 2. Applies the change conceptually 3. Writes the **entire file** back using the Write tool This approach is fragile because: - The agent's context may have an outdated version of the file (from when it was read earlier in the session) - The agent may inadvertently omit sections it didn't focus on - Large files are especially vulnerable — the agent may truncate or drop sections ### Proposed Change Update the agent-evolver's implementation workflow (Step 5: "Check for APPROVED proposals") to explicitly require using the **Edit tool** for surgical changes rather than the Write tool for full file replacement: ``` # When implementing an approved proposal: agent_file = ".opencode/agents/<agent_name>.md" # CRITICAL: Use Edit tool for surgical changes, NOT Write tool # The Edit tool replaces only the specific text that needs changing. # The Write tool replaces the ENTIRE file and risks losing content. # Step 1: Read the CURRENT file from the clone (not from memory) git fetch origin && git checkout master && git reset --hard origin/master current_content = read agent_file from disk # Step 2: Identify the MINIMAL change needed # Only modify the specific section, paragraph, or line that addresses the pattern. # Do NOT rewrite sections that are not related to the fix. # Step 3: Apply the change using Edit tool (oldString → newString) # The Edit tool will fail if oldString is not found — this is a safety check. # If the file has changed since the proposal was written, re-read and re-apply. # Step 4: Verify the change is minimal # Check that the file size after editing is within 10% of the original size. # If the file shrank by more than 10%, something was accidentally deleted. # Abort and re-read the file if this happens. ``` Additionally, add a **pre-commit size check** to the implementation workflow: ``` # Before committing: original_size = wc -c < original_file new_size = wc -c < modified_file if new_size < original_size * 0.9: ERROR: "File shrank by more than 10% — possible content loss. Aborting." # Re-read the file and re-apply the change surgically ``` ### Expected Impact - **Prevents content regression**: Using Edit tool ensures only the targeted text is changed; all other content is preserved - **Faster human approval**: PRs that don't accidentally delete content will pass review more easily - **Reduces rework**: Avoids the cycle of REQUEST_CHANGES → fix → re-review - **Safer implementation**: The Edit tool's `oldString not found` error acts as a safety check that the file hasn't changed unexpectedly ### Risk Assessment - **Low risk**: This is a pure workflow improvement — changing HOW the agent-evolver implements changes, not WHAT it changes. - **Potential concern**: Some changes may require adding entirely new sections (not editing existing ones). Mitigation: For new sections, the agent should append to the end of the file or insert at a specific location using Edit tool with a known anchor string. - **Potential concern**: The Edit tool requires exact string matching. If the file has changed since the proposal was written, the oldString may not match. Mitigation: The agent should always re-read the file from the clone immediately before applying the edit. --- *This is a proposal from the agent evolver. A human must approve this issue before the change will be implemented. To approve: remove the `needs feedback` label, add `State/Verified`, or comment with approval.* --- **Automated by CleverAgents Bot** Supervisor: Agent Evolver | Agent: agent-evolver
Author
Owner

Closing as duplicate of #5773 which was created by another agent-evolver instance for the same pattern (agent-evolver file replacement regression). Please see #5773 for the proposal.


Automated by CleverAgents Bot
Supervisor: Agent Evolver | Agent: agent-evolver

Closing as duplicate of #5773 which was created by another agent-evolver instance for the same pattern (agent-evolver file replacement regression). Please see #5773 for the proposal. --- **Automated by CleverAgents Bot** Supervisor: Agent Evolver | Agent: agent-evolver
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#5736
No description provided.