fix(element_data_store): fix stale account_id showing wrong record on fresh load

Two guards added to the trigger effect:

1. Gate on $slct.account_id being set — prevents the fetch from firing before
   the bootstrap Sync Effect has propagated the real account_id. Without this,
   get_object's localStorage scavenge read a stale account_id (e.g. 1 from a
   prior dev/demo session) and the API returned the wrong account's record.

2. Stale-account detection — if liveQuery returns an IDB row with a non-null
   account_id that doesn't match the current account, treat it as a cache miss
   and fetch the correct record. Null (global/default) rows are still accepted.

Root cause: ae_loc is a persisted store that hydrates from localStorage before
the bootstrap Sync Effect runs. Old account-specific IDB rows scored highest in
the liveQuery sort, suppressing the trigger and leaving the wrong record visible
until the next full page refresh.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Scott Idem
2026-06-02 12:04:59 -04:00
parent a74effa6ff
commit 5fce149808

View File

@@ -165,9 +165,25 @@ $effect(() => {
$effect(() => {
const account_id = $slct.account_id;
const api_ready = !!$ae_api?.base_url;
const entry = $lq__ds_obj;
const entry = $lq__ds_obj as ae_DataStore | null | undefined;
if (browser && api_ready && !entry && ds_loading_status === 'starting') {
// Don't fire until the bootstrap Sync Effect has set a real account_id.
// Without this guard, the fetch runs with null account_id and the
// localStorage scavenge in get_object picks up a stale account_id from a
// previous session, returning the wrong account's record.
if (!browser || !account_id || !api_ready || ds_loading_status !== 'starting') return;
// Also reload when IDB has a record but it belongs to a different account
// (not null/global and not the current account). This handles the case where
// a previous dev/demo session left account-specific rows in IDB that score
// as the "best" liveQuery match even though they are for the wrong account.
const entry_is_stale_account =
entry !== undefined &&
entry !== null &&
entry.account_id !== null &&
entry.account_id !== account_id;
if (!entry || entry_is_stale_account) {
trigger = 'load__ds__code';
}
});