Repository navigation
Description
Goal
The safety backup made before a confirmed remote file write never overwrites or deletes a file that already exists at the backup name, and never writes through a symlink placed there.
Context
Follow-up of #1714 (PR #1728): the review noted that copy_to_backup_inner (crates/app-logic/src/orchestration/remote_files.rs:370-413) creates the backup via write_range(backup, .., true), which in the SFTP implementation uses create() (crates/ssh-transport/src/sftp.rs:388-410, truncate-or-create, follows symlinks). Two consequences:
- A symlink at the predictable name
<path>.smartssh-backup-<ts>makes the backup write go to the symlink target (pre-existing behaviour). createdis set before the first write (needed so a failed first write is cleaned up), so whencreatefails because a foreign, non-writable file already sits at that name, the cleanup removes that foreign file (new with Bound memory for remote file reads in text preview, open for editing and the backup before a file write #1714).
SftpSession::create_file_exclusive (crates/core/src/ssh/transport.rs, implemented with CREATE|EXCLUDE at crates/ssh-transport/src/sftp.rs:487-) already exists.
Requirements
- Create the backup file with
create_file_exclusivefirst; only when that succeeds is the backup marked as created (eligible for cleanup). Subsequent chunk writes usetruncate = false. - If the exclusive create fails (name taken, symlink, permission), the write fails before the target is touched; nothing at the backup name is removed or modified.
- A write failure after the exclusive create still removes the partial backup (existing behaviour, keep tests).
- Spec 0020 §4.2.4: one sentence that an existing file at the backup name is never overwritten.
Acceptance Criteria
- A pre-existing file at the backup name makes the write fail with an error; the pre-existing file is unchanged and not removed; the target is untouched.
- A symlink at the backup name (mock/local transport) is not followed: its target is unchanged, and the symlink is not removed.
- A failing first chunk write after a successful exclusive create removes the partial backup (existing test still passes).
- An empty source file still gets an empty backup, created exclusively.
- The mock
SftpSessionsupports the new call sequence; existing backup tests are adapted without weakening them. - Relevant automated tests pass
Technical Notes
Security-adjacent file handling, but no filter/confirmation/redaction path changes. Threat model: no change expected (note it in the PR). Backup name collision within the same second now fails the write instead of overwriting; a clear error message is enough (no retry).
Files: crates/app-logic/src/orchestration/remote_files.rs, crates/app-logic/src/orchestration/action_exec/tests_activity.rs, crates/core/src/ssh/mock.rs, docs/specs/0020-sftp-file-transfer.md, changelog.d
Spec impact: update 0020
Open questions
- On a name collision: fail the write (recommended, simplest and safe) or pick another timestamp/suffix and retry.
Activity
smart-ssh-coder commentedon Dec 4, 2026
Needs maintainer: The issue waits for #1733, but "blocked by" could not be set. Set it on the issue (Relationships), then remove needs-maintainer: The code this issue changes (copy_to_backup_inner in remote_files.rs, from #1714/PR #1728) exists only on release/0.8.0. It is not on develop yet. It arrives with the open back-merge PR #1733.
fix(app-logic): create the write backup exclusively (#1735)
smart-ssh-coder commentedon Dec 5, 2026
Implementation ready for review. PR #1771. Backup is now created via create_file_exclusive before any write; collisions/symlinks fail the write without touching or removing the foreign file. Tests with counter-proof added, spec 0020 and changelog updated, CI green on PR #1771.