Skip to content

[BUG] V1 edit.ts isDisproportionateMatch bypasses all safety checks for single-line oldString #31785

Description

Description

packages/opencode/src/tool/edit.ts line 742: the isDisproportionateMatch() function returns false immediately when oldString is a single line, bypassing all safety checks. This allows fuzzy replacers like LineTrimmedReplacer to match significantly larger blocks than intended.

Root Cause

function isDisproportionateMatch(search: string, oldString: string) {
  const oldLines = oldString.split("\n").length
  const searchLines = search.split("\n").length
  if (searchLines >= Math.max(oldLines + 3, oldLines * 2)) return true
  if (oldLines === 1) return false  // <-- BUG: bypasses all checks
  return search.trim().length > Math.max(oldString.trim().length + 500, oldString.trim().length * 4)
}

When oldString is a single line (e.g., "return value"), the function immediately returns false meaning "not disproportionate." A fuzzy replacer could match many lines where "return value" appears, modifying a much larger section than the user/AI intended.

Steps to reproduce

  1. Try to edit a single line like "return result" in a large file
  2. If the exact match fails and falls to fuzzy replacers
  3. A much larger block containing "return result" could be matched and replaced

OpenCode version

1.17.3

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions