Skip to content

Unify the frontend store layer on Svelte 5 runes - #1579

Merged
malibio merged 5 commits into
mainfrom
worktree-issue-1513-rune-stores
Jul 7, 2026
Merged

malibio merged 5 commits into
mainfrom
worktree-issue-1513-rune-stores

Conversation

@malibio

@malibio malibio commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Closes #1513

malibio added 4 commits July 7, 2026 13:00
Rewrite collections, schemas, layout, settings, navigation, status-bar as
$state class instances with getters; keep free-fn delegators. Convert all
$store reads and get(store) sites in production consumers to direct state
reads. Tests updated in a follow-up commit.
Convert get(store) reads to .state; drop svelte/store get imports; rewrite
layout.test.ts persistence tests against the fresh rune-store instance and
remove writable-only subscription-mechanics tests (ADR-049). Update the
collection-refresh mock to expose a .state field instead of subscribe/set.
The e2e tier runs a plain node environment with no Svelte compiler, so a
dynamic import of a rune-store (*.svelte.ts) threw "$state is not defined".
Register the existing setup-svelte-mocks.ts as a setupFile so the rune
globals exist inside each fork worker.
@malibio

malibio commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator Author

Pragmatic Code Review — PR #1579

Review Type: Initial Review
Scope: Full diff origin/main...HEAD (4 commits, 58 files, +1345 / −1628)
Issue: #1513 — Unify the frontend store layer on Svelte 5 runes (ADR-049)


Summary

This is a clean, disciplined, net-positive refactor. Six legacy writable() stores (collections, schemas, layout, settings, navigation, status-bar) are migrated to rune-based class stores that match the existing model-store.svelte.ts precedent. The change is mechanical where it should be and thoughtful where it matters (persistence side-effects, cross-store getters, e2e rune globals). Old .ts files are deleted outright — no compat shims, no dual-paradigm bridge — exactly the greenfield posture the repo mandates. Reactivity is preserved throughout: every mutation reassigns this.state immutably, and no method mutates state in place. Verification (unit 3940 pass, svelte-check 0 errors, e2e 24 pass, pre-push gate, live smoke) is thorough and I independently re-ran the store suites (260 pass) and confirmed the key invariants below.

Recommendation: APPROVE. No blocking issues. A few optional nits only.


Requirements Check

  • ✅ Legacy writable() stores migrated to rune stores — All 6 stores are now $state-backed classes exported as singletons; derived() exports became computed getters (collectionsTree, selectedCollection, builtInSchemas, etc.). Old .ts files deleted; all production imports use the .svelte suffix.
  • ✅ Imperative get(store) reads eliminated — No get() from svelte/store remains in the migrated surface. (The one residual get import in src/lib/design/theme.ts is an out-of-scope local writable store, not part of this issue.) The daemon-readiness e2e was converted from get(builtInSchemas) to schemasStore.builtInSchemas.
  • ✅ Passes quality:fix; tests pass — No lint suppressions, no any, no console.* in the new store files; store suites pass locally; full verification green per the PR description.

Verified Invariants (review focus areas)

  • Reactivity of getters: $derived(store.getter) consumers (navigation-sidebar) re-track correctly because getters re-read $state on every access. Confirmed for collectionsTree, builtInSchemas, customSchemas, selectedCollection, selectedCollectionMembers.
  • Cross-store getters stay reactive: collectionsState.selectedCollection / selectedCollectionMembers read collectionsData.state.collections|members. Since collectionsData.state is $state, a $derived wrapping these getters re-runs when the data store changes. Sound.
  • Immutable reassignment: All 13/15/8/4/1 state writes across the stores use this.state = { ...this.state, ... }. Grep for in-place mutation (push/splice/set/delete/direct prop assign on this.state) returns zero hits.
  • #persist() fidelity vs old .subscribe(): The old navigation store persisted on every tabState.set(); the new store calls #persist() at the tail of every mutating method (createPane, closePane, setActivePane, resizePane, setActiveTab, closeTab, addTab, updateTabContent, reorderTab, moveTabBetweenPanes, clearAllTabs). The 500ms debounce, the #isInitialized guard, and the NodeExpansionCoordinator.getExpandedNodeIds enrichment are all preserved identically. Layout #persist() likewise mirrors the old subscribe. setActiveNavItem correctly does not persist — matching old behavior where navigationItems was a separate, unpersisted writable.
  • activePane not-in-persisted-shape: Both old and new hardcode activePane: 'today' on load; setActivePane triggering a persist of the other fields is unchanged behavior, not a regression.
  • Delegator completeness: Every previously free-exported mutation function is still exported as a thin delegator. Removed exports (tabState, layoutState, navigationItems, appSettings, settingsInitialCategory, statusBarVisible, builtInSchemas/customSchemas as derived) have zero dangling references in production — the only remaining textual matches are code comments and correctly-migrated field accessors (layoutStore.navigationItems, settingsStore.appSettings).

Findings

🟢 Suggestions / Nits (all optional)

1. pruneEmptyCollections mutates its input in place
📁 src/lib/stores/collections.svelte.ts:300-306

item.children = keptChildren mutates the CollectionItem objects passed in. This is safe today because the items are freshly built from a local itemMap inside buildCollectionsTree on every getter access, and never touch the reactive $state (which holds raw CollectionInfo[]). But a function named prune… returning a new array while also mutating its argument is a hidden side-effect that could bite a future caller who passes shared/reactive data.
Nit: either rename to signal the in-place edit, or build children immutably. Principle: least-surprise / referential transparency.

2. setup-svelte-mocks.ts for e2e is the right call — worth a one-line note
📁 vitest.e2e.config.ts:37

This is not a hack: it reuses the exact same setup file the main vitest.config.ts already loads. The $state mock is a non-reactive passthrough, which is fine for the e2e tests here because they either import a store only for its module-load side-effect (onDaemonReconnect registration) or poll schemasStore.builtInSchemas in a loop rather than relying on reactive re-render. Good. The config comment already explains the "why" — no change needed; flagging only to confirm the approach is sound.

3. schemasData = { loadSchemas } is a slightly redundant surface
📁 src/lib/stores/schemas.svelte.ts:57

Now that schemasStore.loadSchemas() exists, the standalone schemasData namespace is somewhat duplicative. It's justified — it preserves the collectionsData/schemasData symmetry that app-shell, navigation-sidebar, and collection-refresh consume, and it's the object the daemon-reconnect wiring targets — so keeping it is reasonable. Optional future cleanup, not for this PR.


Test Quality Assessment

The removed layout.test.ts tests were the three "Store Reactivity" cases (notifies subscribers when layoutState changes, …when navigationItems changes, multiple subscribers receive updates) plus a .subscribe()-based "update navigation items" case. These asserted the Svelte-store .subscribe() contract, which no longer exists on a rune class — they tested the removed paradigm, not behavior. Equivalent behavioral coverage (state actually changes on mutation; setActiveNavItem marks a single item active; persistence fires post-init) is retained via direct .state assertions and the rewritten persistence tests. No real coverage was lost. The unit-count delta of −4 is fully accounted for.


ADR-049 Compliance

✅ No state-to-state $effect. ✅ Persistence is an explicit #persist() method call, not an effect. ✅ Derivation via getters, not derived(). ✅ Immutable $state reassignment throughout. ✅ Matches the model-store.svelte.ts precedent.

Project Standards

✅ No console.* (uses createLogger). ✅ No any. ✅ No lint suppression / @ts-expect-error / deprecation / dead-code allow. ✅ No backward-compat shims — old files deleted.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review recommendation: APPROVE (posted as a comment because GitHub blocks self-approval on your own PR). Clean, net-positive ADR-049 rune migration — no blocking issues, three optional nits. Full report in the review comment above.

Build new CollectionItem objects instead of mutating item.children in place,
removing the hidden side-effect the reviewer flagged (collections.svelte.ts).
The collectionsTree getter runs this on every reactive read, so a pure
function is the safer contract. Behavior unchanged; 40 collections tests pass.

Skipped the other two 🟢 nits: the e2e setupFiles reuse (reviewer confirmed
sound, not a hack) and the schemasData wrapper (kept for symmetry with
collectionsData / daemon-reconnect wiring — reviewer said not for this PR).
@malibio

malibio commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator Author

Review feedback addressed

The review returned APPROVE with 3 optional 🟢 suggestions and no Critical/Important findings.

✅ Addressed (1):

  • pruneEmptyCollections mutating its input in place (collections.svelte.ts) — rewrote as a pure function that builds new CollectionItem objects instead of reassigning item.children. This getter runs on every reactive read, so a side-effect-free contract is safer. Behavior unchanged; all 40 collections tests pass. (commit cf862b1)

⏭️ Skipped (2), with reasoning:

  • e2e setupFiles fix — reviewer confirmed it's sound (reuses the exact setup file the main vitest config already loads), explicitly not a hack, no change needed.
  • schemasData = { loadSchemas } wrapper — kept for symmetry with collectionsData and the daemon-reconnect wiring; reviewer marked it optional future cleanup, not for this PR.

Quality: eslint + svelte-check clean (0 errors / 1729 files). Pre-push gate (test:all + cargo build + e2e + Rust) passed.

@malibio
malibio merged commit 375a030 into main Jul 7, 2026
2 checks passed
@malibio
malibio deleted the worktree-issue-1513-rune-stores branch July 7, 2026 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unify the frontend store layer on Svelte 5 runes

1 participant