Skip to content

fix(core): guard saves against external overwrite (#6) - #22

Open
Yan123-tech wants to merge 1 commit into
mainfrom
feature/external-change-detection
Open

Yan123-tech wants to merge 1 commit into
mainfrom
feature/external-change-detection

Conversation

@Yan123-tech

@Yan123-tech Yan123-tech commented Oct 10, 2026 •

Copy link
Copy Markdown
Owner

Fixes #6 — a save can no longer silently overwrite a file that changed on disk while open.

What

Document.save() captures the on-disk modification date at load and compares it on save.
If the file on disk is newer (changed since the document was opened), save() throws
DocumentError.changedOnDisk instead of overwriting; it only proceeds when force: true
(the "Save Anyway" escape hatch). Document gains reloadFromDisk() and revert() for the
Reload path, and refreshes the baseline after every successful save so a later genuine
external change is still caught.

Wiring

  • AppState.performSave routes .changedOnDisk to pendingExternalChange (not a generic
    error); new revertDocument (Reload from Disk) and saveAnyway (Save Anyway). Save All is
    now driven through performSave so the conflict is surfaced there too.
  • MainWindowView presents a Revert / Save Anyway / Cancel confirmation dialog bound to
    pendingExternalChange.
  • StatusBarView shows "⚠️ Changed on disk" while a conflict is pending.

Verification (local gate — CI runs Format + Syntax only, see MEMORY.md #1)

  • swift build — clean (only the pre-existing #NoUsage warning in an unrelated file).
  • swift test — 78/78 in 18 suites.
  • swift-format lint --strict — 0 violations.

Tests: DocumentExternalChangeTests (5 cases): conflict blocks save & keeps the buffer
dirty, no false positive on a stable file, the baseline resets on save, revert reloads the
on-disk version, and untitled documents are never guarded.

Untested by swift test: the dialog and status-bar hint are thin SwiftUI/AppKit glue in the
view layer (see MEMORY.md #18 for why view-update logic is only reachable by running the app).

…#6)

When a document's file is modified on disk between load and save, `save()`
overwrote the change without warning. `Document.save()` now captures the
on-disk mtime at `load`, compares it on save, and throws
`DocumentError.changedOnDisk` when the file is newer (unless `force: true`).

- Document: `fileModificationDate` at load; baseline refreshed after each save;
  new `reloadFromDisk()`/`revert()`; `save(force:)` keeps the no-arg call sites working.
- AppState: `performSave` routes `.changedOnDisk` to `pendingExternalChange` (not a
  generic error); new `revertDocument`/`saveAnyway`; Save All now saves via
  `performSave` so conflicts surface there too.
- MainWindowView: Revert / Save Anyway / Cancel confirmation dialog.
- StatusBarView: "⚠️ Changed on disk" hint while a conflict is pending.
- DocumentExternalChangeTests (5 tests): conflict blocks save & keeps the buffer
  dirty, no false positive, baseline resets on save, revert reloads, untitled is
  never guarded.

Verified locally: swift build clean (pre-existing #NoUsage warning only),
swift test 78/78 in 18 suites, swift-format lint --strict 0 violations.
CI runs Format + Syntax only (see MEMORY.md #1) — build/test are a local gate.
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.

Watch for external file changes on open documents

1 participant