Skip to content

fix(file): persist workspace writes with atomic replace - #78

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/high-severity-issues-9cfe
Draft

fix(file): persist workspace writes with atomic replace#78
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/high-severity-issues-9cfe

Conversation

@cursor

@cursor cursor Bot commented Sep 1, 2026

Copy link
Copy Markdown

Bug and impact

file_write (agent write_file tool) opened the live workspace path with fopen("w"), which truncates before any bytes are written. On ENOSPC/EFBIG/crash after truncate, the existing file was wiped. With stdio buffering, fwrite could report a full write while fclose flushed only a partial file and was ignored, returning success after destroying the original contents.

Trigger: The agent updates an existing workspace file (notes, configs, drafts) while the filesystem cannot complete the new write (disk full, or RLIMIT_FSIZE in the regression test). The prior file body is gone; write_file may even report {"status":"ok"}.

Distinct from open PR #67 (nested path collapse onto an ancestor) and #77 (skill_update skill markdown). Same truncate-then-write class as #71/#77, different path (src/tools/file.c via the file tool).

Root cause

In-place truncate-then-write with unchecked fclose.

Fix

Write content to path.tmp, fsync, then rename over the live file.

Validation

  • ./build/test_file — 35 tests passed, including test_file_write_failure_preserves_existing (RLIMIT_FSIZE=8)
  • Repro under the same limit: fwrite returned success, fclose failed, live file shrank to 8 bytes
  • Same suite under gcc ASan/UBSan: passed
Open in Web View Automation 

file_write opened the live path with fopen("w"), truncating before
fwrite. Under ENOSPC/EFBIG the original file was wiped, and stdio could
report success after fclose failed on a partial flush. Write via
temp+fsync+rename so a failed write_file leaves prior contents intact.

Co-authored-by: esadrianno <esadrianno@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant