Skip to content

[bug] Edit tool silently deletes symbol definitions without cross-reference validation #33062

Closed as not planned

Description

Description

The edit tool (packages/core/src/tool/edit.ts) performs exact text replacement via source.text.replace(oldString, newString) with only 4 validations (L165-L176):

  1. oldString !== newString (tool level)
  2. oldString is not empty (tool level)
  3. oldString exists in file (L165)
  4. Single match when replaceAll is not set (L171)

It does NOT check whether the removed text region contains symbol definitions referenced elsewhere. After replacement succeeds, no cross-reference scan verifies remaining code integrity.

Real case: editing a Python file with three consecutive class methods. The model replaced the block covering all three with a newString containing only two methods. The third method (used in __init__ via QShortcut binding) was silently dropped. The edit tool returned success. py_compile passed. Runtime crash: AttributeError.

Steps to reproduce

  1. Open a Python file with three consecutive function definitions at the same indent level, where the last function is referenced earlier in the same file (e.g., __init__ binds a shortcut to _toggle_pause)
  2. Ask the model to "refactor these three methods into two"
  3. The model produces an edit call where oldString spans all three method definitions, but newString omits the last method
  4. The edit tool matches oldString exactly and applies newString (L178-L181)
  5. The third function is silently removed
  6. py_compile passes (no syntax error since the resulting file is well-formed)
  7. The user runs the program → AttributeError at runtime because the deleted method is still referenced

Expected behavior

The edit tool should validate that the removed region does not contain symbol definitions still referenced elsewhere in the file before applying the replacement. This requires a post-hoc scan of the new file content for remaining references.

Code location

https://github.com/anomalyco/opencode/blob/dev/packages/core/src/tool/edit.ts#L160-L190

Environment

OpenCode Desktop 1.17.8, Windows 11

Plugins

None

Activity

  1. rekram1-node commented on Jun 22, 2026

    @rekram1-node
    Contributor

    STOP SPAMMING ISSUES.

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