fix(file): persist workspace writes with atomic replace - #78
Draft
cursor[bot] wants to merge 1 commit into
Draft
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug and impact
file_write(agentwrite_filetool) opened the live workspace path withfopen("w"), which truncates before any bytes are written. OnENOSPC/EFBIG/crash after truncate, the existing file was wiped. With stdio buffering,fwritecould report a full write whilefcloseflushed 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_FSIZEin the regression test). The prior file body is gone;write_filemay even report{"status":"ok"}.Distinct from open PR #67 (nested path collapse onto an ancestor) and #77 (
skill_updateskill markdown). Same truncate-then-write class as #71/#77, different path (src/tools/file.cvia the file tool).Root cause
In-place truncate-then-write with unchecked
fclose.Fix
Write content to
path.tmp,fsync, thenrenameover the live file.Validation
./build/test_file— 35 tests passed, includingtest_file_write_failure_preserves_existing(RLIMIT_FSIZE=8)fwritereturned success,fclosefailed, live file shrank to 8 bytes