You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
packages/opencode/src/tool/apply_patch.ts does not check for stale content before applying file changes. Between the verification phase (reading files, computing diffs) and the application phase (writing changes), the file content can change. If the file was modified by an external process or another tool call, apply_patch silently overwrites those changes.
Root Cause
The verification phase reads files at line 115, stores old content, and computes the new content. The application phase writes files at line 231, but never re-reads to check if the file changed between these two phases. There's a large TOCTOU window because the permission prompt runs between reading and writing.
Compare with the V2 core apply-patch.ts (packages/core/src/tool/apply-patch.ts) which correctly uses files.writeIfUnchanged() for conditional writes.
Steps to reproduce
Have a file open in another editor
AI calls apply_patch to modify the file
During the permission prompt, the user (or another process) modifies the same file
apply_patch applies its changes on top of the modified file, overwriting external edits
No error is raised - the external changes are silently lost
If this issue is intentionally scoped to just apply_patch.ts and is meant to track a fix separately from the broader #31248, please clarify. Otherwise, consider consolidating.
Activity
github-actions commentedon Jun 10, 2026
This issue might be a duplicate of existing issues. Please check:
apply_patch.ts(V1) — specifically risk "I" in that issue's table, which describes the identical TOCTOU window between the read at line 115 and the write at line 231.edittool, covering similar stale-content guard gaps.If this issue is intentionally scoped to just
apply_patch.tsand is meant to track a fix separately from the broader #31248, please clarify. Otherwise, consider consolidating.