Files
Tom MoorandClaude f668a24663 feat: Persist policies to IndexedDB so abilities survive reload (#13138)
* feat: Persist policies to IndexedDB so abilities survive reload

Adds a StorePersistence layer that caches a store's models in IndexedDB
and hydrates them back at boot as stale data, revalidated by normal API
traffic. Enabled for the policies store, scoped per team, so cached
abilities are available immediately after a reload instead of every
policy needing a forced refetch.

- Persistence hooks in the base Store add/remove/clear methods, since
  add() updates existing models in place and map observation would miss
  those changes
- Enabled from RootStore construction (team read from localStorage) and
  again after auth.info succeeds to cover the first session after login
- Store.clear() wipes the persisted records, covering both logout paths
- deleteAllDatabases() fallback removes the policies database on
  browsers without indexedDB.databases()

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XisV6sRdexfnPSiehurKoV

* refactor: Opt stores into persistence via a persistable property

Stores now declare persistence with `persistable = true` and RootStore
sweeps all persistable stores, deriving each database name from the
store's apiEndpoint, instead of RootStore hard-coding the policies
store. The deleteAllDatabases fallback uses the same sweep.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XisV6sRdexfnPSiehurKoV

* refactor: Simplify store persistence enablement and clearing

- Move the persistable guard into Store.enablePersistence so RootStore
  can call it unconditionally on every store
- StorePersistence derives its own database name from the store and
  teamId rather than receiving a prebuilt name
- StorePersistence.clear returns a promise, making the test deterministic
  instead of sleeping

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XisV6sRdexfnPSiehurKoV

* fix: Avoid re-persisting hydrated records, await clear transaction

Hydration called store.add() for every cached record, which now triggers
persist() and scheduled a flush that wrote the just-loaded data straight
back to IndexedDB at boot. Guard persist() with a hydrating flag.

clear() awaited the objectStore.clear() request rather than the enclosing
transaction, so it could resolve before commit and miss abort errors.

* lock

* iterate

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-08-16 14:13:06 -04:00
..
2024-10-06 05:37:11 -07:00