Repository navigation
Description
Overview
SharedNodeStore is the single source of truth for all node data across every viewer, but it does not use Svelte 5's fine-grained reactivity. Its nodes field is a plain, non-reactive Map<nodeId, Node>; reactivity is simulated by a companion nodesVersion = $state(0) counter that every mutation bumps, and getNode(id) executes void this.nodesVersion before reading the map purely to register a read-dependency.
This is coarse by construction: because the whole map hangs off one scalar, a read of node A depends on the global counter, so a mutation to unrelated node B re-fires every derived value over every node. In a large document, one keystroke invalidates the computed state of all visible nodes.
More importantly, because getNode() can't be trusted at per-node granularity (and a bare Map.get() inside $derived doesn't track at all), components work around the store — they subscribe imperatively inside $effect and cache a local $state copy. That local copy is a second source of truth that races async daemon echoes and is the root of a recurring regression class.
Problem Statement
The faked reactivity forces the "subscribe-in-$effect, write-local-$state" anti-pattern into consuming components, which is the shared root cause of the optimistic-vs-echo state races (stale content after edit, blanked cells after delete, reverted node-type after a slash command) that have been patched repeatedly at individual call sites.
Proposed Solution
Make the store a genuinely reactive source at per-node granularity:
- Replace the plain
Map+nodesVersioncounter withSvelteMap(fromsvelte/reactivity) or per-node reactive cells, sogetNode(id)tracks that node only. - Delete the
void this.nodesVersionread-hack and the manual counter-bumping contract. - Expose the canonical read as
$derived(store.getNode(id))or a direct template read — consumers keep no local copy and no subscription. - Once the reactive layer is clean, the store's three braided concerns (reactive data, version/OCC state, persistence coordination) can be separated in follow-up work.
See ADR-049 (canonical reactivity-store pattern) in nodespace-docs.
Acceptance Criteria
-
nodesis a genuinely reactive source; reads of one node do not re-fire on unrelated nodes' mutations - The
nodesVersioncounter and allvoid this.nodesVersionread-hacks are removed - Components read node data via
$derived(store.getNode(id))(or direct template read) with no local$statecopy and no imperative subscription - No regression in existing node-render/update behavior
- Code passes
bun run quality:fix - Tests pass (
bun run test:all)
Technical Specifications
Reference Files
- shared-node-store.svelte.ts:
packages/desktop-app/src/lib/services/shared-node-store.svelte.ts(~3400 lines) — thenodesMap,nodesVersion,getNode,nodesSet/nodesDelete/nodesClear - Architecture:
../nodespace-docs/architecture/frontend-state-and-persistence.md, ADR-049
Implementation Details
- Prefer
SvelteMapfor a drop-in reactive Map, or per-node reactive cells if finer control is needed $effectis reserved for pushing state into non-reactive external systems only (see ADR-049) — it must not be used to mirror store state into a component
Related Issues
- (blocks the component conversion tracked separately)
Non-Goals
- Splitting persistence/OCC state out of the store (separate follow-up enabled by this change)
Activity
malibio commentedon Jul 6, 2026
Frontend baseline: 147 test files passed, 3946 tests passed