Closed
Closed
Copy link
Description
Description
apply_patch's "update" handler (apply-patch.ts:125) internally reads the file content:
const source = yield* fs.readFile(target.canonical)This means the AI can apply a patch to a file it has never read. The tool reads internally just for diff application — the AI still doesn't know the file's full content. This allows the AI to submit patches that conflict with content it's unaware of.
If the AI hasn't read the file, it doesn't know:
- The exact whitespace/indentation style
- Other code nearby that the patch might interact with
- Whether the file even contains what the AI assumes
The writeIfUnchanged check at line 155 does catch if content changed between the internal read and write, but the AI never got a chance to see the full file before deciding what to patch.
Chain from #30376.
Plugins
N/A (source code analysis)
OpenCode version
main
Steps to reproduce
- AI has never read
src/foo.ts - AI calls
apply_patchwith a patch forsrc/foo.ts - Patch is applied without AI ever seeing the file's content
Screenshot and/or share link
N/A (we read the source code)
Operating System
N/A (code analysis, not environment-specific)
Terminal
N/A
Metadata
Metadata
Assignees
Labels
No labels
Activity
github-actions commentedon Jun 4, 2026
This issue might be a duplicate of an existing issue. Please check:
apply_patch's lack of read-before-edit enforcement, including the internal file read behavior described here. The two issues appear to overlap significantly — [BUG]: apply_patch internally reads files, allowing AI to patch unread files blind #30770 elaborates on a specific aspect already reported in AI providers skip reading files before editing due to missing code-level enforcement and inconsistent system prompts #30376.If the intent is to track this as a separate, more focused sub-issue, please clarify how it differs from the coverage in #30376, or consider adding this detail as a comment there instead.