Skip to content

Convert remaining $effect misuse to derived values and event handlers - #1555

Merged
malibio merged 2 commits into
mainfrom
worktree-issue-1511-effect-to-derived
Jul 6, 2026
Merged

malibio merged 2 commits into
mainfrom
worktree-issue-1511-effect-to-derived

Conversation

@malibio

@malibio malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Closes #1511

…closes #1511)

Replaces the deferred title-sync effect (tick().then() hack around
state_unsafe_mutation) with a derived title pushed via untrack(), matching
the pattern already used correctly in query-node-viewer.svelte. Converts the
read-and-write reloginDismissed effect into a per-episode counter on
ProSyncStore so dismissal state no longer needs to read and reset itself.
Moves the sidebar sub-panel dismissal out of an effect and into the one
handler that can trigger it. Replaces the autoOpen prop->state mirror with
a one-time initial value, matching the component's actual remount lifecycle.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

This PR does what it says: seven $effect misuses (per ADR-049's bright-line rule — effects reserved for pushing state into non-reactive external systems, never deriving values, mirroring state, or read-and-write sequencing) are converted to $derived values or event handlers. I read the full diff against origin/main, traced each of the two flagged risk areas (the pro-sync.svelte.ts episode counter and the base-node-viewer.svelte relocation) end-to-end through their call sites, verified the two "left unchanged" toggleSidebar() sites are unreachable while expanded, and ran bun run test (3948/3948 passed) and bun run quality:fix (0 errors/warnings, no incidental reformatting). This is a net positive, low-risk, well-scoped refactor. One gap keeps it from an unqualified approve.

Findings

Suggested Improvements

  • packages/desktop-app/src/lib/stores/pro-sync.svelte.ts (authRequiredEpisode, setState): This is not a mechanical, behavior-preserving refactor like the other six sites — it changes the reactive contract from a boolean dismiss-flag to a monotonic counter with state-transition detection (next === 'auth-required' && this.state !== 'auth-required'). I traced all three call sites (tier-detected's initial_status path, sync:status, signOut()) and the logic is correct: initial state is 'unspecified', so the very first transition into 'auth-required' (including via initial_status on cold start) correctly bumps the counter from 0→1, and dismissedEpisode's initial value of -1 can never accidentally equal an unbumped 0. I didn't find an edge case that breaks it. But this is exactly the kind of stateful transition-detection logic that regresses silently, and the existing pro-sync.test.ts (3 tests, untouched by this diff) has zero coverage of authRequiredEpisode or setState. Given ADR-049 itself argues a crisp rule is only valuable if reviewers/tests can verify it, I'd add 2-3 unit tests directly against ProSyncStore: (a) transitioning into auth-required bumps the counter, (b) staying in auth-required across repeated sync:status events does not re-bump it, (c) leaving and re-entering auth-required bumps it again distinctly. This is cheap insurance on the one site in the PR that's genuinely new logic rather than a pattern translation.

  • packages/desktop-app/src/lib/components/schema/generic-schema-form.svelte (isOpen initial value): Verified the safety argument holds, but it's non-obvious enough to warrant a guard comment. GenericSchemaForm is only rendered once genericSchema is truthy (base-node-viewer.svelte:1440: {:else if currentViewedNode && nodeId && genericSchema && isCustomSchemaType(...)}), and genericSchema starts null and is populated asynchronously by loadGenericSchema() before this branch ever renders — so by the time GenericSchemaForm mounts and captures autoOpen as its isOpen initial value, hasTitleTemplate (and therefore autoOpen) is already the final, correct value for that node. There's no code path where genericSchema (and thus autoOpen) changes again for an already-mounted GenericSchemaForm instance — the component is destroyed and recreated on every nodeId change via the {#key} in pane-content.svelte, and genericSchema is unconditionally reset to null on that same transition (base-node-viewer.svelte:328). The reasoning is sound, but it depends on an invariant (schema fully resolved before this component ever mounts) that lives in a different file and isn't enforced by a type or test. Nit-adjacent suggestion: a one-line comment at the GenericSchemaForm render site (base-node-viewer.svelte:1443) noting "autoOpen is captured once at mount; safe only because this branch doesn't render until genericSchema is loaded" would save the next person from re-introducing the effect when refactoring the conditional.

Nitpicks

  • base-node-viewer.svelte: The removed effect's tick().then(() => updateTabTitle(title)) deferral and the new untrack(() => updateTabTitle(computedTabTitle)) solve the same state_unsafe_mutation problem via different mechanisms (defer vs. untrack-to-avoid-circular-dependency). Confirmed the computedTabTitle formula is byte-for-byte identical to the old effect's inline title computation, and that Svelte's effect scheduling isn't declaration-order-dependent, so the TDZ-driven relocation further down the file doesn't change firing order relative to the other onMount/effect blocks. No issue found, just noting it for the record since it was flagged as a specific risk to check.
  • collection-node-viewer.svelte: the pre-existing loadCollectionData(nodeId) effect (line 51-53) lacks the untrack() wrapper that the analogous data-load effect in query-node-viewer.svelte uses. Out of scope for this PR (untouched by the diff, and the PR correctly limited itself to the onTitleChange site), but worth a follow-up since query-node-viewer.svelte is cited elsewhere in this same PR as the reference pattern.

Verification performed

  • Read full diff (git diff origin/main...HEAD), all 7 files.
  • Confirmed all three "left unchanged" decisions are reasonable: table-view.svelte's page-reset-on-new-nodeIds (legitimate local-UI-reset pattern), query-node-viewer.svelte (already correct, used as reference), calendar.svelte (documented third-party bits-ui interop workaround, correctly deferred pending live browser verification).
  • Grepped all 9 named files for remaining $effect blocks; none of the untouched ones are derivation/mirroring/read-write anti-patterns — they're DOM/event-listener registration, timers, or guarded one-shot imperative loads.
  • Confirmed navigation-sidebar.svelte's two untouched toggleSidebar() call sites (lines ~415, ~507) are both inside {:else} branches of {#if !isCollapsed}, i.e. only reachable while already collapsed — they cannot trigger the collapse-and-clear-selection condition handleToggleSidebar() now owns.
  • bun run test: 147 files, 3948 tests, all passed.
  • bun run quality:fix: 0 ESLint errors/warnings, 0 svelte-check problems, tsc clean, cargo fmt + clippy clean; git status after showed no incidental changes.

Recommendation

Approve with the one suggested follow-up (test coverage for authRequiredEpisode/setState in pro-sync.svelte.ts) — not a blocker given this is pre-release code with no users and the transition logic checks out on manual trace, but worth doing given it's the one site in this PR that's new behavior rather than a mechanical pattern conversion.

@malibio

malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator Author

Review verdict: Approve (net positive, low risk). GitHub won't let me submit a formal self-review approval on my own PR (gh pr review fails with "Can not approve your own pull request"), so recording the verdict here instead — see the review above for full findings. No blockers found; one non-blocking follow-up suggested (test coverage for the new authRequiredEpisode transition logic in pro-sync.svelte.ts).

- Adds 4 unit tests against ProSyncStore covering the episode-counter
  transition logic added in the prior commit: bumps on entry into
  auth-required, does not re-bump on redundant re-delivery of the same
  state, bumps again on a distinct re-entry, and bumps correctly when
  the transition arrives via tier-detected's initial_status payload.
- Adds a one-line comment at the GenericSchemaForm render site documenting
  why capturing autoOpen once at mount is safe (depends on genericSchema
  being fully resolved before that branch ever renders).

Skipped: the reviewer's nit about collection-node-viewer.svelte's
data-load effect missing untrack() is a pre-existing gap in code this PR
didn't touch — out of scope for #1511, left for a follow-up.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed review feedback

✅ Addressed:

  • Added 4 unit tests to pro-sync.test.ts covering the authRequiredEpisode/setState transition logic: bumps on entry into auth-required, no re-bump on redundant same-state delivery, bumps again on a distinct re-entry (leave-then-return), and bumps correctly via the cold-start tier-detected/initial_status path.
  • Added a one-line comment at the GenericSchemaForm render site (base-node-viewer.svelte) documenting the invariant that makes capturing autoOpen once at mount safe.

⏭️ Skipped:

Verification:

  • bun run test: 147 files, 3952 tests passed (4 new).
  • bun run quality:fix: 0 errors/warnings, no incidental changes.

Re-Review Decision

Decision: NO RE-REVIEW NEEDED

Rationale: Changes are additive test coverage plus a documentation comment — no logic changes to the reviewed diff, no new risk surface. The prior review already verified the authRequiredEpisode transition logic by manual trace; these tests formalize that verification without altering behavior.

@malibio
malibio merged commit 8cb73e2 into main Jul 6, 2026
@malibio
malibio deleted the worktree-issue-1511-effect-to-derived branch July 6, 2026 21:07
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.

Convert remaining $effect misuse to derived values and event handlers

1 participant