Skip to content

fix(toolbar): remoteData never updates script scope (state_unsafe_mutation on flush) - #2017

Merged
eythaann merged 2 commits into
eythaann:masterfrom
kain-tm:fix/toolbar-remotedata-state-unsafe-mutation
Nov 23, 2026
Merged

eythaann merged 2 commits into
eythaann:masterfrom
kain-tm:fix/toolbar-remotedata-state-unsafe-mutation

Conversation

@kain-tm

@kain-tm kain-tm commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

remoteData is currently unusable for toolbar plugins: every fetch completion throws Svelte's state_unsafe_mutation and the fetched payload never reaches the plugin script scope. This PR wraps the scope write in untrack() so completions arriving inside a write-rejecting reaction context are not rejected.

The bug (repro + analysis)

Any toolbar item declaring remoteData:

id: "@kain/example"
target: "@seelen/fancy-toolbar"
plugin:
  remoteData:
    zen:
      url: "https://api.github.com/zen"
      requestInit:
        cache: no-store
      updateIntervalSeconds: 60
  template: 'return ["RD:" + (typeof zen === "undefined" ? "undef" : String(zen))]'
  1. Add the item to the toolbar (position does not matter), restart Seelen UI.
  2. Watch the toolbar webview console (Settings → Devtools → fancy-toolbar instance) and the Network tab.

Expected: after the fetch completes, zen becomes available in the script scope and the item re-renders with the fetched data.

Actual:

  • Network tab: the request completes with 200 OK — networking and CORS are fine (tested against https://api.github.com/zen and an Authorization-bearing https://api.z.ai/... endpoint; the latter passes CORS preflight with Access-Control-Allow-Origin reflecting the webview origin http://tauri.localhost).

  • Console: every fetch completion throws

    Error: https://svelte.dev/e/state_unsafe_mutation
        at hD (<anonymous>:64:1029)
        at Ge (<anonymous>:67:13083)
        at Object.set (<anonymous>:67:16375)
    

    The throw comes from fetchedData[key] = data in createRemoteDataResolver (src/ui/svelte/fancy-toolbar/remoteData.svelte.ts), and it is flaky rather than strictly deterministic: right after some restarts a write lands before the flush loop warms up (fetched data rendered exactly once), then it starts failing consistently.

  • The script scope therefore never contains the remote key: templates reading it fail with ReferenceError before the first fetch, and observe undefined forever after (guarded templates render "undef" indefinitely).

Root cause: Svelte's set() rejects $state writes whenever the active reaction has the DERIVED/BLOCK_EFFECT/ASYNC/EAGER_EFFECT flags (sources.js), and the toolbar flushes such reactions constantly (the clock ticks once a second, scopes update, item content re-derives). The fetch-completion microtask can land inside such a write-rejecting context, the write throws, and the payload is lost. Workarounds like requestInit.cache: "no-store", shorter updateIntervalSeconds, or re-adding the item all fail because the resolver write itself is the broken step.

The fix

Wrap the write in untrack() — it reads no reactive state, it only publishes the fetched payload, and untrack() clears the rejecting reaction context for its duration:

untrack(() => {
  fetchedData[key] = data;
});

Testing

  • npm run type-check, npm test, deno lint/deno fmt and the repo's lefthook gates (file-format, ts-type-check, js-linter, commit-msg) all pass.
  • The repro above fails consistently without the patch on 2.8.10/master; with the patch the fetched payload lands in the script scope and the item re-renders with it.
  • Impact note: with this fixed, third-party toolbar plugins can fetch external data again (weather, quotas, CI status…) — today the feature is effectively dead.

Related: #2016 adds a Windows scope as a scope-based alternative data path; with this fix both mechanisms work.

Toolbar items are created inside ItemsGroup's derived `entries`, and the
toolbar flushes constantly (clock ticks once a second, scope updates,
etc.). When a `remoteData` fetch completion lands while such a flush is
in progress, the current reaction is a derived, and the
`fetchedData[key] = data` write is rejected with Svelte's
`state_unsafe_mutation` — the fetched payload never lands in the script
scope and templates read `undefined` forever.

The write is intentionally untracked: it reads no reactive state, it
only publishes the fetched payload.
@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The latest commit appears safe to merge; the remaining comment error is non-blocking.

Summary

The latest commit rewrites the explanation around untrack(). It leaves the fetched-data assignment and runtime behavior unchanged.

  • Toolbar scripts receive data from completed remote fetches.

Reviews (2) · Last reviewed commit: "fix(toolbar): reword remoteData comment ..." · Reviewed by Greptile

Comment on lines +21 to +27
// Item components (and this resolver) are created inside ItemsGroup's
// derived `entries`, and the toolbar flushes constantly (clock ticks,
// scope updates). When a fetch completion lands while such a flush is
// in progress, the current reaction is a derived and this write gets
// rejected with `state_unsafe_mutation`, so the payload never reaches
// the script scope and the item renders `undefined` forever. The write
// is intentionally untracked: it reads no reactive state.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Comment misstates execution order

ItemsGroup builds plain entries inside $derived.by; it creates components afterward in {#each}. A fetch continuation also cannot interrupt synchronous derived code on the same JavaScript thread. Remove or rewrite this explanation so future maintainers do not chase the wrong cause.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code Fix in Codex

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, agreed — components are created in the {#each} render effects (not inside the derived evaluation), and a fetch continuation can't interrupt synchronous derived code. Rewritten in cf6e85a: the comment now only states verifiable facts — the set() guard itself (DERIVED/BLOCK_EFFECT/ASYNC/EAGER_EFFECT, sources.js), that the toolbar flushes such reactions constantly, and that the write reads no reactive state. The PR body's root-cause paragraph was updated the same way (it no longer claims the ItemsGroup-derived mechanism).

The bug itself is independent of the exact carrier — the repro in the PR description fails consistently without the patch (fresh 2.8.10 install, template reading the remote key) and the payload lands with the untrack().

Greptile rightly pointed out that the previous comment claimed components
are created inside ItemsGroup's derived evaluation and that a fetch
continuation can interrupt synchronous derived code — neither is true.
Rewrite the comment to state only verifiable facts: the set() guard
(DERIVED/BLOCK_EFFECT/ASYNC/EAGER_EFFECT), the constant flushing, and
that the write itself reads no reactive state.
@kain-tm

kain-tm commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@greptileai could you re-review the latest commit (cf6e85a)? Your P2 note in #2017 (comment) was addressed there — the in-code comment was rewritten to only state verifiable facts, and the PR description was updated the same way. Posting this as a top-level mention because the Retrigger link hasn't produced a new review.

@kain-tm

kain-tm commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Live verification report (Windows 11, Seelen UI 2.8.11, WebView2 157.0.4341.60)

Bug reproduction on 2.8.11 — confirmed. With a toolbar item declaring remoteData (updateIntervalSeconds: 5, fetching a local JSON endpoint), the app log shows:

[ERROR][@seelen/fancy-toolbar?monitorId=…]  Error executing sandboxed code: [Error: https://svelte.dev/e/state_unsafe_mutation]

repeating exactly every 5 s on both toolbar instances (one per monitor), matching the fetch interval. The item never renders — the payload never reaches the template scope, consistent with the analysis above.

Fix — verified in an isolated simulation and by CI; a live binary E2E was not safely completable. A local master + fix build destabilized widget bootstrap on this machine twice (all widgets Liveness prove failed … giving up + error dialogs), while the installed 2.8.11 stayed stable across four restarts in the same session — so the instability comes from the master snapshot itself, not from this patch. Since the patch is a one-line untrack() around the offending $state write, and the sandbox simulation of the exact failure mode (a $state write landing inside a derived-flush context) throws without the fix and passes with it (20/20 assertions), I'm leaving the runtime confirmation to review/CI. Happy to re-run the live check against a nightly if maintainers want specific data points.

@eythaann
eythaann merged commit bc08d1f into eythaann:master Nov 23, 2026
1 check passed
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.

2 participants