Repository navigation
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
- Read source code at
packages/opencode/src/tool/write.tsandpackages/opencode/src/tool/edit.ts - Compare with V2 core equivalents at
packages/core/src/tool/andpackages/core/src/file-mutation.ts - 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
github-actions commentedon Jun 4, 2026
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.