From 5fce1498089b441e52353a51779315965f9be2a8 Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Tue, 2 Jun 2026 12:04:59 -0400 Subject: [PATCH] fix(element_data_store): fix stale account_id showing wrong record on fresh load MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/lib/elements/element_data_store.svelte | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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'; } });