Skip to content

SharedNodeStore fakes reactivity — replace the version-counter Map with a genuinely reactive per-node source #1490

Closed

Description

@malibio

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 + nodesVersion counter with SvelteMap (from svelte/reactivity) or per-node reactive cells, so getNode(id) tracks that node only.
  • Delete the void this.nodesVersion read-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

  • nodes is a genuinely reactive source; reads of one node do not re-fire on unrelated nodes' mutations
  • The nodesVersion counter and all void this.nodesVersion read-hacks are removed
  • Components read node data via $derived(store.getNode(id)) (or direct template read) with no local $state copy 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) — the nodes Map, nodesVersion, getNode, nodesSet/nodesDelete/nodesClear
  • Architecture: ../nodespace-docs/architecture/frontend-state-and-persistence.md, ADR-049

Implementation Details

  • Prefer SvelteMap for a drop-in reactive Map, or per-node reactive cells if finer control is needed
  • $effect is 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

  1. malibio commented on Jul 6, 2026

    @malibio
    CollaboratorAuthor

    Frontend baseline: 147 test files passed, 3946 tests passed

  2. self-assigned this
    on Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions