Edit tool: trailing comments duplicated exponentially across repeated agent edits

Resolved 💬 3 comments Opened Mar 10, 2026 by DocChrisKing Closed May 4, 2026

Summary

When an agent (subagent) makes multiple edits to or near the same file, trailing inline comments can get duplicated in the old_string/new_string context. Each subsequent edit compounds the duplication, leading to exponential growth.

Reproduction

  1. A file has a line with a trailing comment:

``scala
import io.questions.database.Doobie._ // Provides Meta instances for tagged types
``

  1. An agent edits code elsewhere in the file. The Edit tool includes the commented line as surrounding context in old_string.
  1. The new_string replacement inadvertently duplicates the trailing comment:

``scala
import io.questions.database.Doobie._ // Provides Meta instances for tagged types // Provides Meta instances for tagged types
``

  1. On subsequent edits, the now-doubled comment is included as context again, and gets doubled again. This compounds with each edit.

Impact

In our case, a ~5KB Scala file (AdminAuditLogDao.scala) bloated to 5.5MB in a single session — the trailing comment was duplicated hundreds of times on a single line. This has happened across multiple separate sessions on the same file, always on the same import line's trailing comment.

Environment

  • Claude Code 2.1.72
  • Using custom agents (subagent_type) via the Agent tool
  • The Edit tool is the mechanism — the agent instructs edits via old_string/new_string replacements
  • Model: claude-opus-4-6

Workaround

git checkout -- <file> to restore the clean version. We maintain a memory note warning agents about this file, but the root cause is in how the Edit tool handles context near inline comments.

Expected Behavior

The Edit tool should not modify content outside the intended edit region. Trailing comments on lines used as context should be preserved exactly as-is.

🤖 Generated with Claude Code

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗