Skip to content

fix(desktop): keep the sign-in, settings and history through restarts, updates and version switches (1.2.3) - #27

Merged
Kkkermit merged 19 commits into
mainfrom
feature/fix-lost-data
Dec 5, 2026
Merged

Kkkermit merged 19 commits into
mainfrom
feature/fix-lost-data

Conversation

@Kkkermit

@Kkkermit Kkkermit commented Dec 5, 2026

Copy link
Copy Markdown
Owner

What and why

On Windows, restarting the PC or updating Statsify could sign you out, reset your settings and Client ID, and make your history look gone. The history itself was never deleted (it lives in its own database), but with no sign-in and no Client ID it couldn't be shown.

The cause. Everything except the history lived only in the WebView's own storage, which writes to disk a while after each change. Windows ends the app abruptly when it shuts down, and the installer does the same when it updates (tao answers WM_ENDSESSION with process::exit). So the WebView could lose:

  • its latest writes, such as a sign-in token Spotify had already replaced, which signs you out at the next start;
  • or all of its storage.

The fix. The desktop app now keeps a copy of its saved state (settings, Client ID, sign-in, folders and pins, lyrics timing, where songs stopped) in app-state.json.

  • Rust writes it atomically as soon as anything changes, and the app puts back whatever the WebView lost when it starts.
  • A generation number tells which side is newer, so switching to an older version and back never brings back older values.
  • Nothing is saved until the copy has been read, and a token refresh waits for the copy, so a used-up token never signs anyone out.
  • On Windows the file lives in the local app data folder, not the roaming one, so one sign-in's tokens can't follow you to another PC.

The history database was attacked too, and these are fixed:

  • Whole-history saves (a first import over 50,000 plays) now land in one transaction. A crash halfway used to leave only the first 50,000 plays.
  • A save that went through but reported an error could be stored twice; now it can't.
  • A leftover -wal beside a missing database is refused instead of deleted, along with the plays only it held.
  • A database from a newer version is left untouched, its log included.
  • The app explains when the history can't be opened, instead of suggesting you import it.

Also in this PR:

  • Opening offline. The app remembers which Spotify account a sign-in belongs to, so the history and folders open even when the PC starts before its network does. Pages that failed while offline now reload when the network comes up.
  • Settings across versions. Every saved setting now survives a version change in either direction. Before this, only the main settings did.
  • Blank desktop icon. The installer tells Windows to redraw icons after an update; before, the desktop shortcut showed a blank icon until clicked.
  • A layout fix. The Insights page no longer cuts off long song names on phones. stats.spec.ts failed on main on some dates because of it.
  • The version. 1.2.3 in all six places. Merging this starts Release on main, which publishes v1.2.3.

How it was tested

Four separate passes tried to break it: an independent code review, the history database, version switching, and faults and races. Every bug they found has a test that fails without its fix.

  • Unit and type checks: npm run check passes: typecheck, lint, 708 Vitest tests, build.
  • End-to-end: npm run e2e passes on the desktop project, 514 passed and 0 failed. New specs:
    • restart.spec.ts: the WebView losing everything, losing its latest writes, a lost sign-out, starting offline, a late copy, an older version and back.
    • versions.spec.ts: updating from v1.0.0 and v1.2.2, and switching back and forth.
    • storage-faults.spec.ts: a slow, failing or hung saved copy; slow or flaky networks; token endpoint errors; restart storms and sign-out races.
    • database.spec.ts: a slow, failing, hung or corrupt database; failed saves and imports; reloads mid-import.
  • Rust: cargo fmt --check, cargo clippy --all-targets -- -D warnings and cargo test pass. New:
    • app_state.rs unit tests, plus tests/app_state_ipc.rs;
    • tests/process_kill.rs, which kills the app mid-save 40 times per scenario and checks the files hold exactly one finished save;
    • database files exactly as every shipped schema made them, with and without their -wal;
    • damaged, locked, read-only, replaced and full-disk files.
  • A real old release: a round trip with the actual v1.2.2 build (current → v1.2.2 → current, and the reverse), and v1.1.1's and v1.2.2's real Rust code against this version's database.
  • Installer hook: compiled with makensis in Tauri 2.13's real installer script.

Not tested on real Windows (this was all Linux, with a stand-in for the desktop app and real process kills): the WebView2 behaviour itself, NTFS file locking and the installer. Before merging, please:

  • install the Windows build that the Desktop app workflow makes for this PR over 1.2.2;
  • restart the PC with Statsify in the tray;
  • check that you're still signed in, your settings and history are there, and the desktop icon shows.

Checklist

  • The branch is named feature/<name> and the commits follow the commit style.
  • npm run check passes (typecheck, lint, unit tests, build).
  • npm run e2e passes, or the change can't affect the UI.
  • Rust changed: cargo fmt --check, cargo clippy --all-targets -- -D warnings and cargo test pass in src-tauri/.
  • New behaviour has tests (Vitest for logic, Playwright with an axe check for UI, Rust tests for commands).
  • UI changes work at desktop, tablet and phone sizes, by keyboard, with reduced motion and in every theme.
  • Docs are updated (README, docs/, AGENTS.md) where behaviour, commands or architecture changed.
  • No secrets, tokens, personal data or real listening history in code, tests, screenshots or logs.

Generated by Claude Code

@github-actions github-actions Bot added desktop The desktop app (src-tauri) bug Something isn't working labels Dec 5, 2026
Comment thread src-tauri/src/history.rs Fixed
Kkkermit and others added 19 commits December 5, 2026 02:21
An update writes the new Statsify.exe at the old one's path, and Tauri's NSIS
template never tells the shell, so Explorer keeps what it cached for that path
and the desktop and Start menu shortcuts show a blank icon until clicked.

Installer hooks (src-tauri/windows/installer-hooks.nsh) call
SHChangeNotify(SHCNE_ASSOCCHANGED) at the end of the install section and again
once the finish page has made the desktop shortcut. Checked by compiling
Tauri 2.13's installer.nsi with the hooks in makensis, and in
scripts/tauri-config.test.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
Settings, the Client ID, the sign-in, folders and pins lived only in the
WebView's localStorage, which the WebView writes to disk a while after each
change. Windows ends the app at once when it shuts down and when the installer
updates it (tao answers WM_ENDSESSION with process::exit), so the next start
could miss the latest writes (a refresh token Spotify had already replaced:
signed out) or find the storage gone (no settings, no Client ID, no sign-in)
while the history sat untouched in SQLite.

Every write to a kept ss.* key now also goes to the shell, which writes
app-state.json beside the history database atomically (new file, flushed,
renamed; 0600 on macOS and Linux). On start-up, before the first render, the
app puts back whatever the WebView lost; removals are kept too, so a lost
sign-out can't sign anyone back in. A file it can't read is neither trusted
nor overwritten. The developer log records only which keys were saved.

Persisted stores and the other keys write through appStorage. Tests: Rust
unit and IPC tests, keptState unit tests, and e2e/restart.spec.ts, which wipes
the WebView's storage, rolls back a rotated refresh token and undoes a
sign-out on the fake desktop (each fails without the fix). A refresh that
can't reach Spotify, or that Spotify fails, keeps the sign-in (now tested).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
History and folders are kept per account, and the app only knew the account
once Spotify answered /me. A PC that starts before its network does showed
"can't reach Spotify" where the history on disk could have been.

The sign-in now remembers its account (from /me answered in the same session,
kept through refreshes), and the history, folders and import page use it until
Spotify answers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
…save before the copy is read

Switching to an older Statsify (which keeps no copy) and back could put the
copy's older settings, or a refresh token Spotify had since replaced, back over
what the older version saved. The copy now counts a generation with every
write, and the app notes it in localStorage after each save: storage behind
the copy lost writes and gets the copy back; storage level with it was changed
by an older version, and the copy takes its values. A WebView holding none of
Statsify's keys never gets to erase the copy.

Nothing is saved until the copy has been read: a copy slow to answer at
start-up no longer gets an emptied WebView's defaults written over it. The app
shows after 3 s without it, keeps trying, and takes what it puts back when it
answers.

The fake desktop gets a generation and fault injection (slow, failing or hanging
commands, across reloads) for the tests that follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
…sktop's faults

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
… copy

- A copy that answers late (a slow disk at boot) no longer loses to what the
  app did on the stale storage meanwhile. Where the copy is ahead it wins over
  those changes too. A token refresh waits for the copy (up to 20 s), and
  retries with a newer refresh token of the same sign-in before signing out.
  A spent token can no longer sign the user out and erase the good one from
  the copy.
- Save timing uses performance.now(): a clock set back at boot no longer
  holds saves (the sign-in's included) back by hours.
- A restore before the first render takes the session without starting a new
  one, so "where songs stopped" isn't cleared and saved empty. A late restore
  puts restored values back after stores clear themselves.
- Keys the shell would refuse, and values over 1 MB, stay in localStorage
  instead of failing every save. The marker is written only when the whole
  restore landed. A save the shell never answers times out after 15 s.
  main.tsx renders even if restoring throws.
- The copy lives in the local data folder: on Windows the roaming one could
  carry one sign-in's tokens to another PC. A copy from a newer format is
  refused and left untouched.
- The developer log lists the copy's keys, not its field names. The installer
  notifies Explorer without waiting for it (SHCNF_FLUSHNOWAIT).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
…t each release

Updating from v1.0.0..v1.2.2, and switching to an older release and back
(round-tripped with the real v1.2.2 build), keeps every setting, folder and
the sign-in:
- fix(storage): keep every persisted store whatever version saved it
- fix(storage): a "__proto__" key in stored playlists or timings is just dropped
- fix(storage): read back what localStorage refused, and never note it level with the copy
- test(e2e): updating from v1.0.0 and v1.2.2, and switching to an older release and back
- a restore that brings back another sign-in than the one the app started on
  starts a new session (per-account state reset), then puts the restored
  values back

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
Start-up awaited each read of app-state.json without a limit, so one read the shell never
answered (an answer lost, a share or disk stuck for a while) left the copy unread for the
whole session: nothing the user changed was saved to it, and a refresh had to wait out its
limit. A read unanswered after 10 s is now asked again beside it, at most two at once, and
whichever answers first counts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
When the saved copy answered after the app was on screen and turned out to be ahead of the
WebView, it replaced every change made meanwhile. That keeps a sign-out forced by a spent
refresh token from winning, but it also undid what the user did while waiting: a sign-out
signed them back in, the demo they tried was left again, and a setting chosen on an emptied
WebView was lost.

A change now remembers what storage held under it. Where the copy still holds that value,
the change was made on what the copy has, and it stands. Where the copy holds something
newer, the copy wins as before, keeping the fields the change touched in a persisted store's
value (with the store's defaults standing in for nothing). A WebView that kept its note but
none of its keys is no longer taken as level with the copy because the app wrote something
meanwhile, which erased the copy's keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
…rt comes up

TanStack Query takes itself to be online when it starts and follows only the online and
offline events after that. An app started without a network (a PC still connecting after it
boots, Statsify starting with Windows) never heard the network come back, so every view that
failed meanwhile waited for a click on Try again. The query layer now starts offline when the
WebView says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
…aited

After a restart that cost the WebView its sign-in, the landing page offers Connect Spotify.
If the saved copy answered late while the browser was open, it put the sign-in back into
storage, but a sign-in in progress doesn't follow the stored session; cancelling it then
returned to "signed out", although storage (and the next start) had the sign-in. A desktop
sign-in that is cancelled or fails now takes whatever session storage holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
…nd races

The saved copy slow by 1, 5 and 30 s, failing then answering, never answering (once and for
good); saving failing (disk full), slow and never answering; Spotify slow, offline at start,
flapping; token refreshes that hang, are slow or fail with 5xx, 429, a revoked token or no
network; /me failing while the remembered account opens the history; the recently-played
sync failing; restart storms during slow saves; signing out, trying the demo and signing in
while the copy is slow; an account switch while the history loads; double sign-ins; hiding
the window and pagehide flushing; quick changes and an immediate restart. Each checks that
nobody is signed out wrongly, nothing is reset or lost, and the page has no errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
…ocs for the fault and version work

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
…hone

The demo history follows today's date, and on some dates a longer name with a
longer "last played" date was cut off at 375 px (stats.spec.ts failed on main
too).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
… and old files

- fix(desktop): keep the history database whole through crashes, moves and odd files
- fix(desktop): save a whole history in staged parts that land together
- fix(history): say when the saved history can't be opened, is slow, or wasn't saved
- test(desktop): kill the app mid-save and check what is left on disk
- test(desktop): open history files exactly as every shipped version made them
- fix(desktop): leave no half-written copies or oversized logs on a full disk
- test(desktop): a damaged file put right opens without a restart; steadier kill and restart tests
- fix(desktop): answer a staged rewrite's own outcome, whatever clearing up does

A whole history over 50k plays now lands in one transaction (history_stage
parts, then history_apply with staged), a save carries the history it was
made from and is refused (history_changed) if the file holds something else,
a -wal beside a missing database or from a newer version is left untouched,
and the UI says when the history can't be opened instead of suggesting an
import. Kill tests (tests/process_kill.rs) and files from every shipped schema.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
…clippy

Rust 1.100's clippy refuses `for n in from..` (for_unbounded_range). The writers
run until the test kills them; the bound only gives the loop an end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
CodeQL took a fictional fixture id in an assertion message for sensitive data
("cleartext logging"), going by the word "account" in its names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TCk6t7edJdscNtTSeFmyQN
@Kkkermit
Kkkermit force-pushed the feature/fix-lost-data branch from d412ebc to 26f14a4 Compare December 5, 2026 02:21
@Kkkermit
Kkkermit merged commit b36233b into main Dec 5, 2026
19 checks passed
@Kkkermit
Kkkermit deleted the feature/fix-lost-data branch December 5, 2026 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working desktop The desktop app (src-tauri)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants