Skip to content

pitis/perch#20: Settings can restore a JSON backup, keeping a copy of the current data first - #29

Merged
pitis merged 5 commits into
mainfrom
autopilot/pitis-perch-20-restore-backup
Nov 13, 2026
Merged

pitis merged 5 commits into
mainfrom
autopilot/pitis-perch-20-restore-backup

Conversation

@pitis

@pitis pitis commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

Refs #20

Refs, not Closes: this hasn't been run in the real app yet (see Not tested). After the merge, #20 moves from gap to verify.

What changed

  • Settings › Your data › Backup has a Restore… button next to Export JSON….
  • Restore first reads the picked file. If it isn't a Perch backup (not JSON, or JSON without valid tasks), Settings says so ("… isn't a Perch backup. Nothing was changed.") and stops before asking anything.
  • A valid backup gets a confirmation: "Replace your Perch data with this backup?", with Replace and Cancel buttons. It says tasks, notes, focus history and settings will be replaced, and that a copy is kept first.
  • On Replace, Workspace.restore(_:keepingCopyIn:) writes the current data beside the store as store.before-restore-<time>.json. It uses the export format, so Restore can load that copy too. If the copy can't be written, the restore throws and nothing changes. Then it replaces the data and saves right away. Settings shows where the copy went.
  • A focus timer that was running when the backup was made comes back paused, the same as after a launch (recoverFocusAfterLaunch). Otherwise it would count all the time since the backup was made.
  • The task editor closes on restore, because its task may not exist in the backup, and an open editor keeps the notch panel from auto-closing.
  • Store writes the copy's file name with the same helper as the existing corrupt-<time> copy.
  • The settings snapshot is taller (1100 → 1340 pt). The old one stopped before Apple Intelligence and Your data.
  • product.md: restore moves from "not built" to "written, but not tried for real".

Clips aren't touched: they stay outside AppData and the backup.

Worth knowing

  • Settings are restored too, including the Reminders sync switch. If the backup had sync on and Perch has Reminders access, sync runs and reconciles the restored tasks with the Perch list. Tasks added since the backup come back from Reminders, and tasks deleted since then are dropped again. Without access it only shows the usual "doesn't have access" line, and no permission prompt appears.
  • Reading the picked file happens on the main thread, the same as Export. The file comes from an open panel, so macOS shouldn't prompt for it.

How it was tested

  • make check: 44 tests pass. Three new tests in StoreTests:
    • testRestoreKeepsACopyOfTheCurrentDataThenReplacesIt: the copy decodes back to the old data, the data is replaced, and a timer running in the backup comes back paused.
    • testRestoreChangesNothingIfTheCopyCantBeWritten
    • testFilesThatArentBackupsAreRejected: an empty file, non-JSON, [], {}, tasks of the wrong type, and a task without an id all throw DecodingError, which the Settings message depends on. An export round-trips.
  • I watched the new tests fail before the implementation (they didn't compile without restore).
  • swift build and Perch --snapshot for the before/after Settings screenshots, posted as a comment.

Not tested

  • Not run in the real app. Clicking through the open panel and the NSAlert needs a GUI session. This run doesn't have one, and the repo's rules forbid touching the real store. Someone needs to try it by hand: Export, change something, Restore, check that the data is back and that store.before-restore-*.json exists. Then try a non-backup .json file and check that it's refused.
  • The review step was run. Fixes and one open product question, how restore works with Reminders sync, are in the review comment below.

…a first

Restore… in Settings › Your data reads a file made by Export JSON…, refuses
anything that isn't a Perch backup before asking, then confirms that it
replaces everything. The current data is written beside store.json as
store.before-restore-<time>.json first, and if that fails nothing changes.
A focus timer that was running in the backup comes back paused.

The settings snapshot is taller so it shows the Your data section.
@pitis

pitis commented Nov 8, 2026

Copy link
Copy Markdown
Owner Author

Settings window, sample data (Perch --snapshot), both rendered at the new 1340 pt height so Your data is visible. The only change is the Restore… button beside Export JSON… at the bottom.

Before After
Before After

The confirmation (an NSAlert) and the result messages show up only after you pick a file, so the snapshot can't capture them.

@pitis pitis mentioned this pull request Nov 8, 2026
3 tasks
…replacing anything

Restore now writes the backup to the store before swapping it in, so a
failed save leaves everything as it was instead of saying Restored and
losing it at the next launch. The safety copy keeps a running focus
timer's time, reminders that came due before the restore don't ring
again, and restored screenshot and Calendar settings apply right away.
The confirmation shows how many tasks and note days the backup has next
to what's there now, and the messages say what failed.
@pitis

pitis commented Nov 13, 2026

Copy link
Copy Markdown
Owner Author

Review

Ran /pr-review-toolkit:review-pr (code, error handling, tests, comments) and /ponytail:ponytail-review on git diff origin/main...HEAD. Fixes are in 81cad3d. make check: 45 tests pass, and each new assertion failed before the fix. swift build gives no warnings. The Settings screen looks the same, since only the confirmation and result text changed, so no new shots.

Fixed

  1. Important: a save that failed after the swap still said "Restored". saveNow() swallows its error into a notch toast the Settings window never shows, and the next launch would load the old data. Workspace.restore now saves the backup to the store before swapping it in memory, so a failed save throws and "nothing was changed" holds. Test: the restore is on disk when restore returns.
  2. Important: the confirmation didn't say what was in the file. Any JSON with a tasks array, even {"tasks":[]}, replaced everything after one click. The alert now shows the backup's task and note-day counts next to the current ones. It also says clips and Launch at login aren't affected.
  3. Important: undoing a restore by restoring its copy lost a running focus timer's time. The copy is now written with the segment closed and paused, as quitting does. The live data isn't touched, so a failed copy still changes nothing. Test: 20 running minutes survive in the copy.
  4. Important: restored settings didn't reach the services. A restored "Keep screenshots" off left the Desktop watcher running until relaunch, against the AGENTS.md rule. Restored on did nothing until the next launch's prompt. Calendar was the same. restoreBackup now calls clips.updateScreenshotWatcher() and calendar.refresh().
  5. Suggestion, taken: reminders that came due before the restore rang again, each with a card and a chime. They're marked as already rung on restore, as importRemote does. Test added.
  6. Messages. A damaged backup said "isn't a Perch backup". Now: "Perch can't read X as a backup". A copy or save failure names what failed. Every error says nothing was changed. product.md says "files Perch can't read as a backup are refused", not "files that aren't backups".
  7. Test. The copy-failure test now also checks that onChange didn't fire, so no save, sync or wake was scheduled.

Declined

  • Two restores in the same second collide on the copy's name. The second fails with a true message and changes nothing, and two modal dialogs can't be clicked through that fast.
  • The picked file is read on the main thread. Export already writes this way, and the file comes from an open panel, so macOS doesn't prompt for it.
  • A format version in exports, local-time stamps on copies, showing the DecodingError path. These are worth doing if restore gets used, but they aren't defects.

Needs your decision: how restore should work with Reminders sync

With sync on, the next sync, about 2 s later, partly undoes the restore. Reconciler.plan confirms this:

  • Tasks deleted since the backup whose reminders are gone are deleted again, unless enough vanished to count as mass loss. Restoring to get back one or two deleted tasks fails silently.
  • Edits made in Reminders since the backup win over the restored task, because they're newer.
  • Tasks added since the backup come back from Reminders.
  • The backup's own remindersSync replaces this Mac's. A backup made before sync was on turns sync off, and turning it back on duplicates every task in Perch and in Reminders. A backup with sync on turns it on without going through Settings.

Options:

  • A. The backup wins. Restored tasks count as the newest edit and are pushed to Reminders. Tasks whose reminder is gone are recreated, not deleted. Tasks added since are removed from Reminders. This is more code, in Reconciler, and testable.
  • B. Keep this Mac's sync setting and links, and let Reminders win as now, but say so in the confirmation.
  • C. Restore turns sync off, and the confirmation says to turn it back on once the data looks right. The duplicate-on-re-enable problem still needs handling.

…-20-restore-backup

# Conflicts:
#	Sources/Perch/Snapshot.swift
Reminders sync stays this Mac's setting through a restore. While it's
on, the restored tasks count as the newest edit and are pushed to
Reminders, a task whose reminder was deleted since the backup is made
again instead of deleted, and reminders for tasks added since the
backup are removed. The confirmation says so.
…ven with sync off

A restored task keeps the reminder its current copy is linked to, so a
backup saved before sync was on no longer deletes and recreates every
reminder. The backup wins once this Mac has synced at all, not only
while sync is on, so turning sync back on later doesn't undo it.
@pitis

pitis commented Nov 13, 2026

Copy link
Copy Markdown
Owner Author

Restore and Reminders sync: option A

Decision: the restored backup wins over the Perch list in Reminders. This is in the last two commits on this branch.

  • Sync stays this Mac's setting. remindersSync and reminderListId aren't taken from the backup, so a restore never turns sync on or off.
  • Once this Mac has synced, even with sync off for now:
    • restored tasks count as the newest edit and are pushed;
    • each one keeps the reminder its current copy is linked to;
    • a task whose reminder is gone is made again instead of being deleted;
    • reminders for tasks added since the backup are removed.
  • The confirmation says: "If you sync with Reminders, the Perch list there is changed to match the backup."

Tests: testARestoredBackupWinsOverReminders and testARestoreKeepsTheRemindersOfTasksStillHere. Both restore and then run the real Reconciler.plan, and both failed before the change.

Review of the change

  • Fixed (Important): a backup from before sync was on deleted and recreated every reminder. Anything only in Reminders was lost (flags, subtasks, URLs). Links are now carried over by task id.
  • Fixed (Important): with sync off at restore time, turning it on let Reminders win. The rule now applies once this Mac has a Reminders list, not only while sync is on. That also covers a restore landing during the first sync.
  • Declined: pending deletes for reminders that are already gone are never cleared. This is in sync itself, not in this PR, and clearing them risks forgetting deletes while an account is away. Worth its own issue.
  • Declined: an old completed reminder stays in Reminders' Completed section when a task completed since the backup is restored open. This is cosmetic.

make check: 57 tests pass, and swift build gives no warnings.

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.

1 participant