diff --git a/src/lib/elements/element_data_store.svelte b/src/lib/elements/element_data_store.svelte index 4bf7b677..d80219ff 100644 --- a/src/lib/elements/element_data_store.svelte +++ b/src/lib/elements/element_data_store.svelte @@ -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'; } });