A move never destroys bytes it has not verified exist elsewhere
Every file mutation a sort makes goes through app/core/resilient_file_ops.py, and each one obeys a single rule: Sortify deletes or replaces a file only after it has proved that the same bytes are durable somewhere the ledger knows about. Concretely:
- No-replace publish. A file or folder reaches its target path through a rename that fails atomically if the target exists:
renameat2(RENAME_NOREPLACE)on Linux andrenamex_np(RENAME_EXCL)on macOS, andos.renameon Windows, which never replaces. Where the filesystem lacks the native call, files fall back toos.link(src, dst)thenos.unlink(src), and folders to an exclusivemkdirreservation. Only filesystems with neither (EPERM/ENOTSUP/EMLINKfromlink, as on FAT/exFAT) fall back to check-then-rename under a process-wide lock. The copy fallback runs only onEXDEV, never on anyOSError. - Verified cross-volume copy. Copy to a staging file in the target folder,
fsyncthe file and its folder, compare a content hash with the source, publish the stage with the same no-replace primitive, and only then remove the source. - Overwrite displaces, it does not delete. The
overwriteconflict policy first moves the existing target into the session's backup folder, recorded in the ledger, and undo restores it. The backup is purged with the session's history, not at move time. - Rollback waits. When a move fails, the mover cancels pending futures and waits for running ones before rollback starts, and rollback replays the ledger for the real session id. With no session id it stops and reports instead of unwinding a random one.
- Reconciliation checks hashes. Ledger reconciliation removes a file at a destination only when its hash matches the
file_hashthe ledger recorded before the move; otherwise it leaves both files and reports a conflict.
We chose this because the five highest risks in the codebase today are all ways the current mover can lose a user's file: atomic_move_non_clobber checks lexists then calls os.rename, which replaces silently on POSIX, and treats any OSError as a cue to copy; overwrite calls resilient_remove(dest) before the move that might fail; cross-volume moves compare only sizes; fut.result() raises on the first failure while the rest of the chunk keeps moving and unwind_session starts underneath it; and reconciliation deletes the destination when both paths exist. Sortify's whole promise is that a sort can be undone, so one irreversible loss outweighs any speed the shortcuts buy.
Implementation
Points 1, 2 and the wait in point 4 are in #757 and the stacked mover fixes. Still open: point 3 (#766), refusing to sort without a snapshot or session in point 4 (#767), and point 5 (#768).
Considered options
- Keep check-then-rename and narrow the window. Rejected: no amount of re-checking closes a TOCTOU race; only an operation that fails atomically does.
link+unlinkonly. Standard library and no foreign calls, but it does not cover folders or symlinks, which the mover does move (junctions and links), it leaves the file with two names for a moment, and it fails outright on FAT/exFAT, which are common on USB drives. We kept it as the first fallback rather than the primary path.- Drop the
overwritepolicy. Simplest and safest, andCONTEXT.mdalready lists onlyskipandrename. Rejected for now becauseapp/config.pyacceptsoverwriteand users may rely on it; displacing to a backup keeps it while making it undoable. - Size check plus
fsync, no hash. Cheaper, but a truncated or corrupted copy of the same size passes. Hashing only runs on the cross-volume path, which is already I/O-bound by the copy.
Consequences
- The native rename needs per-OS
ctypescalls and a support check, and every path below it needs its own tests. The tests in #757 cover each fallback. - On the
linkfallback the file briefly has two names. A crash there leaves a duplicate, never a loss; reconciliation sees two paths with matching hashes and removes the source. - The lock-based fallback only protects against Sortify's own workers, not other programs writing into the same folder. That residual race is accepted for filesystems without hard links.
- Cross-volume moves get slower by one read of the copy. Same-volume moves cost the same as today.
- Displaced files from
overwriteuse disk space until their session's history is purged. mover.pyandledger.pystop callingresilient_removeon user files directly.scripts/validate_architecture.pycan enforce that onlyresilient_file_opsdeletes user files.- Testing this needs a filesystem seam: inject the file operations into
resilient_file_opsso tests can faillink,fsyncor the hash at each step. That replaces theunittest.mockbranches inresilient_moveand_process_move_itemrather than adding new ones.