Repository navigation
Description
Problem Statement
SharedNodeStore (packages/desktop-app/src/lib/services/shared-node-store.svelte.ts) is a single global singleton (SharedNodeStore.getInstance()) holding every node ever loaded by any tab/pane in a SvelteMap<string, Node>. There is no reference-counting, LRU, or any other eviction path: once a node is loaded, it stays in memory for the life of the app session, even after every tab/pane that showed it has closed.
This was verified directly, not just read from code:
ReactiveNodeService.destroy()(the exact lifecycle call everyBaseNodeViewermakes on unmount) is an explicit no-op — its own comment says "Node data reactivity now comes directly from SharedNodeStore's SvelteMap — there is no subscription to tear down."closeTab()(packages/desktop-app/src/lib/stores/navigation.svelte.ts) only cleans up scroll-position state and tab/pane bookkeeping. It never touchesSharedNodeStore.shared-node-store.svelte.tshas noevict/unload/prunelogic anywhere (grepped the full 4,475-line file — zero hits besides two unrelated doc-comments).- A real headless probe (create 10
ReactiveNodeServiceinstances simulating 10 open tabs,initializeNodes()300 distinct nodes into each = 3,000 distinct nodes, then calldestroy()on all 10 — simulating closing every tab) showed the shared node count staying at 3,000 before and after close. Heap grew ~16.5MB for those 3,000 plain node objects in a headless unit-test harness (no forced GC, so this is directional, not a production number — the real Tauri app additionally carries a rendered Svelte component + DOM per visible node, which this data-layer probe does not include).
Relationship to #302
This supersedes #302 ("Memory & Performance Optimization for Multi-Tab System", filed 2025-10-20). That issue described the problem as "multiple ReactiveNodeService instances" each independently holding node data, and proposed generic "lazy loading" + "cleanup on tab close." The actual current architecture is different: there is exactly one global node-data store shared by every viewer (ReactiveNodeService is now a thin per-viewer UI-state adapter with no node data or subscription of its own — confirmed above). So:
- The good news Memory & Performance Optimization for Multi-Tab System #302 didn't know about: reopening the same tabs/nodes repeatedly does not duplicate memory — there's one cache, not N.
- The real, still-current gap: that one shared cache only ever grows. A session that visits many different documents over time accumulates all of them in memory forever, regardless of how many tabs are currently open.
The fix shape is therefore "define and implement eviction from a shared cache once nothing open references an entry," not "add lazy loading/cleanup to N per-viewer instances" as #302's proposed solution assumed.
What already exists (do not re-implement)
- Component-code lazy loading:
node-component-loader.svelte.ts/schema-form-loader.svelte.tsalready lazy-load and cache Svelte component bundles per node type via the plugin registry. This issue is about node data, not component code — leave that alone. - A real performance test suite exists (
bun run test:perf,packages/desktop-app/src/tests/performance/architecture-benchmarks.test.ts) covering structural-op latency, initial render, multi-client sync latency, and lookup performance for a singleReactiveNodeService. Its one memory test is explicitly a "baseline estimation" (formula-based, notprocess.memoryUsage()) and does not exercise multiple concurrent viewers/tabs at all — extend this file, don't replace it.
Proposed Solution
- Add a real (not estimated) multi-tab memory measurement to the perf suite: open N simulated tabs on distinct documents, measure
SharedNodeStorenode count and heap usage, close tabs back down, measure again. This becomes the regression guard for whatever eviction policy ships. - Design and implement a reference/visibility tracking mechanism so
SharedNodeStoreknows which cached nodes are still reachable from at least one open tab/pane (accounting for the existing multi-pane case where the same node is legitimately shown in two panes at once — must not evict while any pane still shows it). - On a node becoming unreachable from every open tab/pane, make it eligible for eviction from
SharedNodeStore.nodes,PersistenceCoordinator,versions, and any other per-node bookkeeping map — after an inactivity threshold (not immediately on tab close, to avoid thrashing on quick tab switches) and never while the node has a pending/unflushed write. - Wire actual eviction into the tab-close path so the new perf-suite test in (1) demonstrates real memory being released, not just held flat.
Acceptance Criteria
- New
test:perftest measures real (process.memoryUsage()), not estimated, memory for N concurrently-open simulated tabs on distinct documents - Same test demonstrates node count / memory going back down after those tabs close and the inactivity threshold elapses — currently it stays flat
- A node open in two panes simultaneously is never evicted while either pane still shows it
- A node with a pending/unflushed write is never evicted mid-write
- No regression to
architecture-benchmarks.test.ts's existing single-viewer benchmarks
Key Areas
Files likely touched:
packages/desktop-app/src/lib/services/shared-node-store.svelte.ts— add reachability tracking + evictionpackages/desktop-app/src/lib/stores/navigation.svelte.ts—closeTab()needs to signal reachability changepackages/desktop-app/src/tests/performance/architecture-benchmarks.test.ts— add the real multi-tab memory test
Priority
Medium — confirmed real (not theoretical) unbounded growth, but bounded by session length; not a crash-causing leak in normal usage today.
Activity
mstomar125 commentedon Sep 11, 2026
Resolution summary
PR: #2571 — Addresses #2568
Design:
SharedNodeStorenow tracks reachability by walking each cached node'sstructureTreeancestor chain up to its root and checking membership inopenDocumentRootIds— the set of currently-open tabs' document root ids, pushed wholesale bynavigation.svelte.ts'sNavigationStoreon every tab-state mutation (open, close, content change, session restore) via a newupdateOpenDocumentRoots(). This reuses the parent index viewer data-loading already populates rather than adding a second membership map to keep in sync, and it handles the two-panes-same-node case for free (the shared node/root id just stays in the set as long as any open tab still reports it — no refcounting needed).A node whose root drops out of that set becomes an eviction candidate after a 30s inactivity window (long enough that quick tab-switching or an accidental close-and-reopen never trips it, short enough to reclaim memory well within a session), at which point it's cleared from
nodes,versions,pendingUpdates,persistedNodeIds,taskFieldWriteSeq, and the other per-node bookkeeping maps — but only after re-verifying it's still unreachable AND has no pending/in-flight/queued persistence operation or active atomic batch; a dirty node gets rescheduled instead of evicted. Reachability starts fully dormant (nothing ever evicts) until the first report, so nothing outside real navigation usage is affected.Real measurement (not estimated) —
TEST_FULL_PERFORMANCE=1 bun run test:perf, 10 tabs × 300 nodes = 3,000 nodes, opened/closed through the realaddTab()/closeTab()path:Node count is deterministic (every node from every closed tab is gone). Heap fully recovered — the final reading came in below the pre-test baseline.
Verification:
bun run test:all(frontend + skill + full Rust workspace, includingrust:test:app-units) andbun run quality:fix(eslint, svelte-check, tsc, cargo fmt/clippy with-D warnings) both pass clean. New tests: a dedicatedshared-node-store-eviction.test.tsunit-tests the mechanism directly (dormancy, threshold timing, pending-write guard, active-batch guard, cancel-on-reopen);architecture-benchmarks.test.tsgets the real multi-tab memory test above plus an end-to-end two-panes-share-a-node test through the real navigation store;navigation.test.tsgets 4 tests locking in thecloseTab()→SharedNodeStorewiring itself.Leaving this open for review per convention — not closing automatically.