Skip to content

fix: defer 1.2 GB model preload until sidebar opens (closes #116) - #205

Merged
ivdimova merged 1 commit into
devfrom
fix/v0-11-defer-model-preload
May 12, 2026
Merged

ivdimova merged 1 commit into
devfrom
fix/v0-11-defer-model-preload

Conversation

@pluginslab

Copy link
Copy Markdown
Owner

Summary

The admin-sidebar bundle loads on every wp-admin page (intentional — the toggle lives in the admin bar). But mounting `AdminSidebar.jsx` immediately ran `initializeApp()`, which fired the WebGPU check, looked up saved provider preferences, and started downloading / loading the ~1.2 GB Qwen 3 1.7B model into GPU memory.

For users who rarely open the sidebar (most page loads), that's a huge unwanted cost — both memory and the visible download progress bar in the admin bar.

Net: +40 / -2 lines, 2 files touched.

Fix

React tree still mounts on every admin page (cheap — just a few KB of components), but the WebGPU/model initialization is gated on a `hasOpened` state that flips when the user clicks the toggle for the first time.

Wiring

  • `admin-sidebar.js` dispatches a custom `wp-agentic-admin/sidebar-opened` event when the toggle adds the `is-open` class. Subsequent toggles still fire the event (harmless — `hasOpened` is already true).
  • `AdminSidebar.jsx` subscribes to the event, sets `hasOpened`, and a second useEffect that depends on `hasOpened` runs `initializeApp` when the flag flips.
  • Initial `hasOpened` reads `is-open` from the DOM, so if some upstream change makes the sidebar start open, we still initialize on first render.
  • Listener is removed once `hasOpened` becomes true (no leak).

Memory impact

Phase Before After
Page load (sidebar closed) WebGPU check + ~1.2 GB model auto-load if cached Just React mount (few KB)
First sidebar open already done WebGPU check + auto-load (same flow, deferred)
Subsequent opens n/a No-op on init path

Verification

  • `npm test` — 96 passing, unchanged
  • `npm run build` — clean
  • `composer lint` — clean
  • `npx wp-scripts lint-js` — clean

Manual test plan

  • Open Playground at `/wp-admin` (any page that's NOT the plugin's own admin page)
  • Confirm: no model loading happens — ModelStatus widget on the admin bar should be idle / not progress
  • Click the superhero icon to open the sidebar
  • Confirm: WebGPU check fires, model loads (you'll see the progress bar / status)
  • Close + reopen sidebar — no second load triggered

🤖 Generated with Claude Code

The admin-sidebar bundle loads on every wp-admin page (intentional —
the toggle lives in the admin bar). Mounting AdminSidebar.jsx ran
initializeApp() immediately, which fired the WebGPU check, looked up
saved provider preferences, and started downloading / loading the
~1.2 GB Qwen 3 1.7B model into GPU memory.

For users who rarely open the sidebar, that's a huge unwanted cost —
both memory and the visible download progress bar in the admin bar.

Fix: React tree still mounts on every admin page (cheap, just a few
KB of components), but the WebGPU/model initialization is gated on a
`hasOpened` state that flips when the user clicks the toggle for the
first time.

Wiring:
  - admin-sidebar.js dispatches a custom
    `wp-agentic-admin/sidebar-opened` event when the toggle adds the
    is-open class for the first time. Subsequent toggles still fire
    the event (harmless — hasOpened is already true).
  - AdminSidebar.jsx subscribes to the event, sets hasOpened, and a
    second useEffect that depends on hasOpened runs initializeApp
    when the flag flips.
  - Initial state of hasOpened reads is-open from the DOM, so if some
    upstream change ever makes the sidebar start open, we still
    initialize on first render.
  - Listener is removed once hasOpened becomes true (avoids leak).

Memory impact:
  - Page load: a few KB of React + listener (no change)
  - First open: full WebGPU check + cached-model auto-load (was page
    load, now first open)
  - Subsequent opens: no-op on the init path

No new tests — existing react/jest infra mocks model-loader and
doesn't exercise admin-sidebar.js (vanilla JS entry).

Build: clean.
Lint: clean.
Tests: 96 passing, unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@pluginslab
pluginslab requested a review from ivdimova May 12, 2026 10:27

@ivdimova ivdimova left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Clean implementation. Custom event pattern keeps vanilla JS and React properly decoupled. The hasOpened initialization correctly handles the edge case where the sidebar is already open on mount. Safe to merge.

@ivdimova
ivdimova merged commit 47af934 into dev May 12, 2026
4 checks passed
@ivdimova
ivdimova deleted the fix/v0-11-defer-model-preload branch May 12, 2026 21:23
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