[Bug] Incorrect Code Insertion Breaks Syntax and Logic Structure

Resolved 💬 3 comments Opened Sep 13, 2025 by ethank Closed Jan 7, 2026

Bug Description
Bug Report: Claude Code Repeatedly Duplicates Code Instead of Modifying

Pattern of Failure

Claude Code has a persistent pattern of adding duplicate code instead of modifying existing code, especially when using sed commands to insert text.

Specific Issue in This Session

Task: Fix citation deduplication in Ace Chat

What Should Have Happened:

  • Modify the existing deduplication logic on line 301-308
  • Add renumbering AFTER the existing uniq array is created

What Actually Happened:

  • Used sed -i '' '308r fix-dedup.txt' which inserted renumbering code INSIDE the filter function
  • Created malformed code structure:

const uniq = collectedSources.filter((s: any) => {
// ... existing logic ...
return true;
// Re-number citations after deduplication // <- WRONG LOCATION!
uniq.forEach((s: any, idx: number) => { // <- BREAKS THE FILTER!
s.n = idx + 1;
});
}); // <- MISMATCHED BRACKETS

Root Cause

  1. No understanding of code structure: Claude doesn't parse the actual JavaScript/TypeScript structure before inserting
  2. Blind line-based insertion: Uses sed with line numbers without understanding what's on those lines
  3. No validation after changes: Doesn't check if the resulting code is syntactically valid

Previous Similar Failures (Same Session)

  • Added duplicate import SummaryModal statements
  • Added duplicate Suspense wrapper components
  • Left comment headers when inserting replacement code
  • Added duplicate closing brackets ): null} )}

Recommended Fix

Claude Code needs to:

  1. Parse code structure before making modifications
  2. Use AST-aware editing instead of line-based text manipulation
  3. Validate syntax after each change
  4. Show a diff preview of what will be changed before applying

This is a critical bug that makes Claude Code unreliable for code modifications.

Environment Info

  • Platform: darwin
  • Terminal: Apple_Terminal
  • Version: 1.0.112
  • Feedback ID: de6f24cd-e83b-44a8-9127-18c55f210765

Errors

[{"error":"Error: Error: Tool Read input is invalid: File content (265.3KB) exceeds maximum allowed size (256KB). Please use offset and limit parameters to read specific portions of the file, or use the GrepTool to search for specific content.\n    at i7B (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:1884:15963)\n    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n    at async Object.call (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:1884:10682)\n    at async o35 (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:3272:18167)\n    at async h$B (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:3272:14662)\n    at async s35 (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:3272:13525)\n    at async n35 (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:3272:13062)\n    at async b$ (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:3272:10166)\n    at async b$ (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:3272:11014)\n    at async b$ (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:3272:11014)","timestamp":"2025-09-12T19:55:19.584Z"},{"error":"MCPContentTooLargeError: MCP tool \"Edit\" response (75879 tokens) exceeds maximum allowed tokens (25000). Please use pagination, filtering, or limit parameters to reduce the response size.\n    at A$0 (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:1854:5019)\n    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n    at async i7B (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:1884:16345)\n    at async Object.call (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:1884:10682)\n    at async o35 (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:3272:18167)\n    at async h$B (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:3272:14662)\n    at async s35 (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:3272:13525)\n    at async n35 (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:3272:13062)\n    at async b$ (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:3272:10166)\n    at async b$ (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:3272:11014)","timestamp":"2025-09-12T19:57:05.220Z"},{"error":"MCPContentTooLargeError: MCP tool \"MultiEdit\" response (76488 tokens) exceeds maximum allowed tokens (25000). Please use pagination, filtering, or limit parameters to reduce the response size.\n    at A$0 (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:1854:5019)\n    at process.processTicksAndRejections (node:int

Note: Error logs were truncated.

View original on GitHub ↗

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