0010: Safe document persistence
- Status: Proposed (implemented in Phase 2, task 001)
- Date: 2026-12-05
Context
The MVP saved through a predictable temporary name with File::create, which follows an existing link. It canonicalised the target at save time, so a link planted later redirected the write. Reads checked metadata and then called fs::read, so a file could grow past the limit or be a pipe that blocks forever. Change detection used size and modification time only.
Decision
- One bounded reader for open, reload and compare: open without following a final link (non-blocking on Unix), check the handle is a regular file, read at most limit + 1 bytes, and fail if metadata or the path's identity changed during the read.
- Pin the resolved path at open. Saves refuse a target or parent folder that has since become a link. Files opened through a link save to the pinned target.
- Content fingerprints. A version is identified by length and a 64-bit content hash; metadata (including Unix
ctimeand inode) only decides when to re-read. - Acknowledged versions. A save may replace the last loaded or saved version, a missing file, or one exact external version the user acknowledged.
- Atomic publish from an exclusively created random temporary file with copied permissions (and owner/group on Unix, failing closed). Linux uses
renameat2(RENAME_EXCHANGE)and verifies the displaced file; other platforms recheck metadata then rename. No remove-then-rename fallback.
Alternatives considered
- Write in place: keeps hard links and ACLs but is not atomic; a crash leaves a truncated file.
- Keep writing through links at save time: convenient, but lets anyone who can write the folder redirect a save.
- File locks: advisory on Unix and ignored by most editors and tools.
- Cryptographic hash: unnecessary for detecting accidental changes; adds a dependency.
Consequences
- Saves split hard links and do not copy ACLs or extended attributes; these limits are documented.
- Outside Linux a writer racing the final rename can still lose its change.
- Each save re-reads the target once (bounded by the document limit).
Validation
Unit tests in crates/ide-core/src/fsio.rs and session tests in crates/ide-core/tests/session.rs; evidence in Phase 2 validation.