Skip to content

mac: a failed member snapshot blocks the project replace; prompts name each file's recovery command (#806) - #810

Merged
d-q222 merged 1 commit into
mainfrom
fix/806-discard-followups
Sep 17, 2026
Merged

d-q222 merged 1 commit into
mainfrom
fix/806-discard-followups

Conversation

@d-q222

@d-q222 d-q222 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #806, the follow-ups from the review of #804.

Changes

  1. A failed snapshot write blocks the replace. Dirty-member snapshots are written and checked first. If any write fails, open, reload and fixture load refuse with .saveFailed (naming the file and the store error). Nothing is replaced, and the text stays in the editor.
  2. Recovery hints. Prompts and post-open/reload messages name the command for each file: Edit > Restore Discarded Buffer for the entry file, File > Restore Unsaved Snapshot… for member files. The titles are taken from FlashTeXMacApp.swift.
  3. Reload names what it discards. The reload summary lists the dirty members a direct reload also discards (snapshotted), along with how to recover them. bufferDirty is derived from the new entryDirty and discardedMembers fields.
  4. Fixture discard order. A fixture discard is recorded only after the fixture decodes and validates, so a rejected fixture no longer overwrites the recoverable buffer or any snapshot.
  5. Tests. The private snapshot store test helper now takes effect. There are 8 new AutosaveTests.

Tests

Independent review pending.

🤖 Generated with Claude Code

…806)

Follow-ups to #804, which made replacing a project save or snapshot every
dirty document.

- Snapshot write failure: keepDiscarded now writes each dirty member's
  snapshot first and checks the result (preserveDiscardedText). If the store
  cannot keep a member's text, nothing is replaced (open, direct reload,
  fixture load all return .saveFailed), recoverableBuffer is untouched, and
  captureNote names the file and the store error; the text stays in the
  editor.
- Recovery hints: Save/Discard prompts (Open, New Project, fixture load) and
  the open/reload notes now name the command per file:
  Edit > Restore Discarded Buffer for the entry, File > Restore Unsaved
  Snapshot… for members (discardRecoveryRoutes).
- Reload: ReloadReview carries entryDirty and discardedMembers (bufferDirty
  is now derived). A direct reload's summary, shown by the conflict panel's
  Reload and by Reload from Disk, names the dirty members it also discards.
- Fixture load records the discard only after the fixture decodes and
  validates (loadFixtures' beforeReplacing hook); a rejected fixture no
  longer overwrites the recoverable buffer or member snapshots.
- Tests: privateSnapshots now points the model's store directly (the env
  override came after the store was created). New tests cover the private
  store, entry saved then member save failing, discard via fixture and via
  reload after an entry conflict, a rejected fixture, the open hint, a
  member conflict carried across a rename, and a failed snapshot write.

Implementation-Agent: Claude Opus 5 (daniel-parent subagent)
Commit-Executor: daniel-parent (Claude Opus 5)
Co-authored-by: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
@d-q222

d-q222 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Commander (daniel-parent): independent Opus review of 25aa365f, APPROVE. No material findings. Non-blocking notes 1–4 are small; I will add them to #806's residual list. Merging after filtered tests on the merged tree.

PR #810 review: head 25aa365 (reviewer: opus, read-only)

Scope: git diff origin/main...HEAD (DirtySnapshots.swift, DocumentFiles.swift, ShellModel.swift, ProjectScaffoldViews.swift, AutosaveTests.swift). I also read the surrounding code: authorizeProjectReplacement, confirmReload/directReload/reloadFromDisk, loadFixtures, offerDirtySnapshot/restoreDirtySnapshot/snapshotSaved, ProjectDocuments.isDirty/projectRoot, and the menu definitions in FlashTeXMacApp.swift.

Material findings

None.

What I checked:

  • Replace after a failed member snapshot: keepDiscarded now checks every dirty member first (DocumentFiles.swift:606-614) and returns false before it touches recoverableBuffer or the entry's snapshot. There are three ways to replace the project, and all three stop on false:
    • open: adoptOpenedText -> .saveFailed at :639;
    • direct reload: :1013;
    • fixture load: beforeReplacing at ShellModel.swift:759, then kept at :856.
      New Project goes through openTex, so it is covered too. The controller reload (adoptReloadedDocument) does not replace members and never calls keepDiscarded, which is correct. preserveDiscardedText treats "text already equals the disk file" as kept, which is also correct.
  • Snapshot timing: snapshots are written after the user picks Discard and after the read, decode and validation succeed, just before the replace. They are not written before the save/discard decision: Cancel and Save never reach keepDiscarded.
  • Menu strings: "Restore Discarded Buffer" is in CommandGroup(after: .pasteboard), which is the Edit menu (FlashTeXMacApp.swift:203). "Restore Unsaved Snapshot…" is in CommandGroup(replacing: .newItem), the File menu (:238). Checked with xxd: both files use the same U+2026 ellipsis.
  • Refactor: for a direct reload, the old bufferDirty was hasUnsavedDocuments = isDirty || project.anyDirty. ShellModel.isDirty delegates to project.isDirty(activePath) or compares the entry's saved text, so it is a subset of anyDirty. The new entryDirty || !discardedMembers.isEmpty is exactly project.listing.contains(isDirty), so it is equivalent. For a controller reload, entryDirty = isDirty and there are no members, so nothing changed. The three existing review.bufferDirty callers (:1113, :1223 and the tests) keep their behaviour. Only the summary text changed.
  • Fixture order: after beforeReplacing() returns true, the rest of the do block has no try (assignments, bindLayout, fetchExplanations, retainMarksAfterResultBound). A fixture that has passed validation cannot reach the catch after the discard is recorded.

Non-blocking notes

  1. Partial failure leaves stale member snapshots (DocumentFiles.swift:608-613). Scenario: dirty members A and B. A's snapshot is written, B's write fails, the method returns false and nothing is replaced. A's snapshot stays in the store. The user keeps editing A and saves different text. snapshotSaved (DirtySnapshots.swift:271) removes a snapshot only on an exact byte match, so the old text is still there. When A is opened later, it is offered as "discarded when X was opened", but no discard ever happened. Restoring it is an explicit action on a clean buffer and nothing is written to disk, so no work is silently lost. The label is wrong, though. The same write also overwrites any earlier snapshot of A in its single slot, even though the attempt was aborted. That slot overwrite already happened on every successful discard before this PR.
    Suggested fix: validate the store first (for example, a probe write or directory check before the loop), or remove the snapshots this attempt wrote when a later one fails. Do not add a test for this; it would pin the current behaviour.
  2. The File menu hint points to a disabled item after a switch to another project. openTex clears files.offeredSnapshots (:644). Member snapshots are only offered when that member is opened again under its old root (ProjectDocuments.swift:883). Right after "Discarded text kept: chapter.tex via File > Restore Unsaved Snapshot…", the menu item is disabled until the user reopens the old project and the member. The command name is correct, but the hint could add "(reopen in its project)".
  3. A dirty member with no project root is skipped without a snapshot (if let root = project.projectRoot). The new routes text still names it as "via File > Restore Unsaved Snapshot…". This needs a stale baseline with documentURL nil (for example, members that came from a fixture). It is an edge case that predates this PR, but the new hint now makes a promise that is not kept there. Consider refusing, or omitting the member from the routes, when the root is nil.
  4. The entry's own snapshot result is still unchecked (:620). That is acceptable because recoverableBuffer holds it in memory, and the hint says "(this session)".
  5. Test gaps:
    • testRejectedFixtureRecordsNoDiscard uses {}, which fails at decode. The LayoutNegotiation.violation rejection path is not tested.
    • testFailedSnapshotWriteAbortsTheReplacement has one dirty member only, so the partial-failure case in note 1 is not tested.
    • The New Project prompt (ProjectScaffoldViews.swift:237) has no test; its string change is trivial.
  6. The PR description says snapshots are written "before the save or replace decision". They are written after Discard and before the replace. Only the wording is off; the code is correct.

Tests run

VERDICT: APPROVE

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.

mac: unsaved-member discard follow-ups after #804 (snapshot write unchecked, wrong recovery hint, Reload widens discard)

1 participant