Skip to content

apply_patch leaves workspace corrupted on partial failure #34311

Description

@MADEVAL

Description

apply_patch writes files sequentially. When a patch touches multiple files and a later file fails (disk full, stale content, permission denied), earlier files stay modified on disk with no rollback. The workspace is left in an inconsistent state.

The tool description itself acknowledges the gap: "if a later operation fails, earlier operations remain applied." Both V1 (packages/opencode/src/tool/apply_patch.ts) and V2 (packages/core/src/tool/apply-patch.ts) have this behavior. A TODO in file-mutation.ts confirms it needs fixing.

Plugins

None

OpenCode version

dev branch (58ba99e)

Steps to reproduce

  1. Have two files in a workspace: first.txt and second.txt (second.txt has stale content)
  2. Ask OpenCode to apply a patch that deletes first.txt and updates second.txt with outdated expected content
  3. The delete succeeds, the update fails with StaleContentError
  4. first.txt is permanently deleted with no recovery

Operating System

Windows 11

Terminal

Windows Terminal

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