Skip to content

V1 tool chain missing safeguards that V2 core has - stale-check, existed report, lock leak, independent locks #30772

Closed

Description

Description

The V1 tool chain in packages/opencode/src/tool/ (write.ts, edit.ts) is missing several safeguards that exist in the V2 core tool chain in packages/core/src/tool/ and packages/core/src/file-mutation.ts. These gaps create inconsistent behavior where the AI can silently corrupt files depending on which tool version a session uses.

1. V1 write tool never reports existed status

packages/opencode/src/tool/write.ts line 46 obtains exists (whether the file already existed), but the output message at line 74 always says "Wrote file successfully." regardless of whether the file was newly created or overwritten. The exists info is only put into metadata (line 97), not the visible text output. Compare with V2 core's write tool (packages/core/src/tool/write.ts line 34) which outputs "Wrote" vs "Created" based on existed status.

2. V1 edit tool has no stale-content check

packages/core/src/file-mutation.ts implements writeIfUnchanged which compares the current file bytes against expected bytes before writing (lines 158-167). The V1 edit tool at packages/opencode/src/tool/edit.ts has no equivalent check — it reads the file, applies the replacement, and writes directly. If another session or process modified the file between read and write, the V1 tool silently overwrites those changes.

3. V1 edit tool lock map memory leak

packages/opencode/src/tool/edit.ts lines 35-45:

const locks = new Map<string, Semaphore.Semaphore>()
function lock(filePath: string) { ... }

Each unique file path creates a Semaphore entry in the locks map, but entries are never removed. Compare with V2's KeyedMutex in packages/core/src/effect/keyed-mutex.ts which properly deletes entries when users reaches 0 (line 44). The V1 lock map grows unbounded over the lifetime of the session.

4. V1 and V2 locks are independent

V1 edit.ts has its own lock map. V2 file-mutation.ts uses a separate KeyedMutex. Two concurrent sessions editing the same file — one using V1 tools and one using V2 tools — would NOT share a lock and could write to the same file simultaneously, causing corruption.

5. replaceAll without minimum string length safeguard

packages/opencode/src/tool/edit.ts line 697 and packages/core/src/tool/edit.ts line 157 both use replaceAll without any minimum length check on oldString. An AI could call replaceAll with oldString: "a" or oldString: "the", causing massive text corruption.

Plugins

N/A (source code analysis)

OpenCode version

main

Steps to reproduce

  1. Read source code at packages/opencode/src/tool/write.ts and packages/opencode/src/tool/edit.ts
  2. Compare with V2 core equivalents at packages/core/src/tool/ and packages/core/src/file-mutation.ts
  3. Note V1 lacks stale-check (item 2), existed report (item 1), lock cleanup (item 3), lock coordination (item 4), and replaceAll validation (item 5)

Screenshot and/or share link

N/A (we read the source code)

Operating System

N/A (code analysis, not environment-specific)

Terminal

N/A

Activity

  1. github-actions commented on Jun 4, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    The issue itself acknowledges it is part of a chain originating from #30376. Points 1–4 in this report extend and overlap with issues already filed in that chain. It may be worth consolidating these into the existing issues rather than tracking them separately.

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