refactor(desktop): move per-window router history out of renderer localStorage

AS
2mo ago
+701-111

Stacked on #6775 — the per-window scoping this replaces only exists there; main still has the single shared router-history key. Review this diff against window-followups, and merge after it.

Problem

Renderer localStorage is one ~10 MB quota shared by every window of the profile and by every TanStack DB collection in it. The per-window history keys #6775 introduced are entity-scoped with a key minted per window, which is the shape apps/desktop/AGENTS.md asks to keep out of it.

The boot sweep answered the three questions across launches, but not within one: windows opened and closed during a long session accumulated records until the next boot, and the sweep was fire-and-forget.

What changed

The history moves to app-state.json as routerHistoryByWindow, beside tabsStateByWindow and keyed the same way.

That gets it the bound the sweep couldn't. pruneWindowScopedState already runs from the same place that writes the restorable window set — "so the two cannot drift", per its own comment — so a window's history is dropped when its window stops being restorable, on every persist rather than at the next boot.

Why not local-db, as #6795 proposed

I wrote that issue and I think it named the wrong store.

@superset/local-db is the host service's database, and a host can be remote. Per-window router history is desktop UI state about a window on this machine; putting it there would have sent it to whichever machine the host runs on and made a local window's route depend on a remote round-trip.

app-state.json already has the per-window keying, the pruning pass, and the pre-multi-window inheritance rule that this needs. A local-db table would have had to reimplement all three, and gained nothing.

Reads stay synchronous

persistentHistory is a module-level const: the router is built from the history before React mounts, and every path through electronTrpc is async. So the main process hands the record over on the command line alongside the window key it already passes, and only writes go over IPC.

buildRouterHistoryArg trims from the oldest entry until the payload fits a byte budget — Windows caps a whole command line at 32767 characters, and a history of long paths can be large while well under the entry cap. The entry the window is actually on is the last thing given up.

Migration

  • On first read a window claims its own router-history:<key> record and removes it.
  • Only the window carrying LEGACY_WINDOW_KEY may claim the bare router-history record, so no window adopts a route from another window — possibly in another organization.
  • Migration runs at module evaluation, which is before sweepDeadPersistedKeys() in renderer/index.tsx: imports are evaluated before the statements that follow them, so the read always wins the race with the sweep.
  • router-history moves to DEAD_KEYS (prefix match, covering both shapes) for profiles whose owning window never opens again, and the registry entry is removed since nothing writes those keys now.
  • sweepDeadWindowHistories and the window.liveKeys route that existed only to feed it are deleted.

Testing

  • historyStore.test.ts — 15 tests: handoff parsing including malformed payloads and out-of-range indexes, the migration rules (own record, legacy claim, ordinary window refused, corrupt record, throwing storage), and the entry cap.
  • routerHistoryArg.test.ts — 6 tests: round-trip, argv-hostile characters, trimming to the budget while keeping the current entry, and refusing to emit an oversized argument.
  • pruneWindowScopedState.test.ts now imports the real pruneByWindow instead of reimplementing the rule, and covers the identity-preservation behaviour that keeps an unchanged map from marking app-state dirty.
  • persistent-hash-history.test.ts write assertions now go through a mocked tRPC client rather than reading localStorage, so they exercise the real path.
  • bun test in apps/desktop: 2954 pass, 0 fail. Typecheck clean apart from the pre-existing duplicate-dependency errors on main (@types/hast, mermaid, @codemirror/view).
  • No CDP run: I have not verified a real relaunch restoring a route in a packaged build. The migration path and the argv trimming are covered by unit tests only.

Summary by cubic

Moves per-window router history out of renderer localStorage into app-state.json as routerHistoryByWindow, where it shares tabsStateByWindow's per-window keying and pruning rules. Records are dropped whenever their window stops being restorable on each persist, not collected until the next boot, and reads stay synchronous: the main process hands the saved history to a new window as a command-line argument because the router is built before React mounts and IPC is async. The history is capped at 100 entries in the renderer and again server-side, and trimmed to an argv byte budget so a window never restores onto a route it had navigated away from.

Uses app-state.json rather than @superset/local-db as the linked issue proposed: that is the host service's database, hosts can be remote, and app-state already provides per-window keying, pruning, and legacy-inheritance rules.

Migration

  • On first read a window claims its own router-history:<key> record and removes it.
  • Only the window with LEGACY_WINDOW_KEY may claim the bare router-history record, so a window never adopts another window's route.
  • Both key shapes move to DEAD_KEYS for profiles whose owning window never opens again.

Only the legacy window inherits the pre-multi-window tabsState; every other window starts empty, which stops duplicate pane ids from making one window's register() tear down another's listeners. Window titles now lead with the workspace name (then the org) via a fuzzy route match, so windows on the same org are distinguishable and the name survives nested workspace routes.

Summary by CodeRabbit

  • New Features
    • Router history is preserved separately for each desktop window, including newly opened windows.
    • Tab layouts and navigation state are restored correctly per window.
    • Window titles display the active workspace and organization when available.
  • Bug Fixes
    • Improved cleanup of saved state for closed windows.
    • Added safeguards for oversized or invalid navigation-history data.
  • Migration
    • Existing navigation history and tab state are migrated to per-window storage.