mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-06 05:49:12 +02:00
Problem ------- On desktop, creating a new tab triggered thousands of chat-store rehydration logs per second (sustained for seconds). Same session, same workspace — nothing actually changed. `pnpm test` was clean; the bug only manifests at runtime with React 19 Activity + multi-tab. Root cause ---------- Every tab's WorkspaceRouteLayout kept its own `syncedSlugRef` to decide "did slug change since last sync". That model assumes one layout instance equals one workspace context — true on web, false on desktop where N tabs each mount their own layout. Activity remounts + tab-router-sync stirring the tab store caused per-layout refs to drift out of agreement with the module-level truth, so each ref independently called `rehydrateAllWorkspaceStores()`. The existing microtask dedup only coalesced same-tick calls; successive ticks each scheduled another iteration through every registered rehydrate fn. Fix --- Move the "did slug actually change?" decision to where the truth lives: inside `setCurrentWorkspace` itself. The singleton now: - Returns immediately when the slug is already current (idempotent). - Fires slug subscribers + persist rehydrate as internal side effects when (and only when) the slug transitions. Layouts are simplified to "feed the URL slug in"; they no longer maintain a ref guard or call rehydrate explicitly. N tabs feeding the same slug is naturally a no-op after the first — the model no longer depends on "one layout instance" as an implicit invariant. Also hardens the original render-time race that motivated the v2 refactor: both layouts now gate on `!listFetched || !workspace` so `useWorkspaceId()` in descendants is guaranteed non-null. Public API ---------- `rehydrateAllWorkspaceStores` removed from `@multica/core/platform` exports — it's now purely an internal effect of `setCurrentWorkspace`. The function itself is deleted; the rehydrate loop lives inline in `setCurrentWorkspace`. Tests ----- Four new tests covering the new semantics: single rehydrate on mount, same-slug noop across repeat calls, real workspace switch fires again, logout → re-entry into same workspace fires again. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>