feat(desktop,cli): live-reload settings and themes when the CLI changes them

Merged
KI
3mo ago
+325-60

Summary

  • superset settings ... changes now apply to a running desktop app instantly — theme switches, custom-theme imports, and typography included. No window refocus, no relaunch.
  • Follow-up to #6402, which shipped the CLI with documented propagation caveats (focus-refetch for settings, restart for themes). This removes both.

Why / Context

The theme lived in app-state.json, read once at boot; regular settings refreshed only on window refocus. That made the in-app story weak (an agent in a Superset terminal changes a setting and the visible app lags behind) and theme changes required a quit → set → relaunch dance where a running app could even clobber the external write.

How It Works

  • Desktop: the existing local notifications server (the one agent hooks already POST to) gains POST /settings-changed. The handler reads no request data — it re-reads themeState from disk into lowdb's in-memory doc (deliberately only themeState: a full db.read() could revert in-memory tabs/agent-session state with unflushed updates) and emits a SETTINGS_EXTERNAL_CHANGE event through the existing notifications.subscribe tRPC subscription. Fixing memory-vs-disk here also kills the clobber problem at its root: after the nudge, the app's own flushes preserve the CLI's theme.
  • Renderer: a new useSettingsExternalChangeListener (mounted in the authenticated layout) applies the canonical theme state via a new applyExternalThemeState store action (wholesale replace + re-apply; malformed custom-theme entries filtered) and invalidates all settings.* queries so fonts/toggles refetch immediately.
  • CLI: after every write (set, reset, theme set/import/remove) it POSTs the nudge — 500 ms timeout, best-effort, silent on failure. Messages adapt to the outcome: Applied to the running desktop app. when acknowledged, the previous focus/restart guidance otherwise (older app versions return 404 → graceful fallback; no version coupling).

Security note

The endpoint is unauthenticated on the localhost server (same as the existing agent-hook routes) but reads nothing from the request and only triggers a re-read of local files plus a renderer refetch — no data exposure, no writable surface.

Manual QA (CDP against the dev desktop, port ownership verified)

  • theme set light / theme set dark while the app runs: --sidebar CSS var flips within ~1s, no restart (dark #1a1716 ↔ light oklch(0.985 0 0))
  • set terminalFontSize 22 with the Appearance page open: live preview label updates 14px → 22px with zero focus events; reset returns it
  • Delivery verified independently via an ad-hoc notifications.subscribe subscription receiving settings-external-change
  • CLI messaging: "Applied to the running desktop app." with the app up; old fallback text with it stopped
  • Debugging detour worth recording: initial failures were not the mechanism — the workspace dev DB lacked users.deletion_requested_at (main's code ahead of the Neon parent branch), so the app sat signed-out on /sign-in where the authenticated layout (and listener) never mounts. Fixed via setup.sh --force + applying the repo's pending packages/db migration to the isolated workspace branch.

Testing

  • bun test (packages/cli): 96 tests including new nudge-helper coverage (ack → true, dead port → fast false, 404/older app → false)
  • bun run typecheck on desktop + cli; targeted biome clean
  • Desktop unit-test surface for main/renderer event plumbing is thin (no harness for the notifications server); covered by the CDP QA above

Rollout

  • No migration, no config. Old CLI + new app: nothing calls the endpoint (inert). New CLI + old app: 404 → fallback messaging identical to today's behavior.

Summary by cubic

Live‑applies CLI‑driven settings and theme changes to a running desktop app. Previously settings refreshed on window focus and themes required a restart; now both update instantly, with fallback when no app is running or on older app versions.

  • Desktop: adds POST /settings-changed on the local notifications server; re‑reads only themeState from disk into lowdb memory and emits SETTINGS_EXTERNAL_CHANGE; responds with { success, themeReloaded }.

  • Renderer: mounts useSettingsExternalChangeListener in the authenticated layout; replaces theme via applyExternalThemeState; invalidates all settings.* queries and FONT_SETTINGS_QUERY_KEY.

  • CLI: after settings set/reset and theme set/import/remove, sends a 500 ms nudge and adapts messages (“The running desktop app refreshed immediately.” / “Applied to the running desktop app.”) or falls back to focus/restart guidance on failure (e.g., 404 from older apps).

  • Security: unauthenticated localhost endpoint that reads no request data and only triggers local re‑read plus renderer refetch.

  • Docs/tests: CLI reference and skill docs updated for live refresh; README demo GIF added; nudge tests cover ack, dead port, and 404.

  • Rollout: no migration. New CLI + old app returns 404 and falls back; old CLI + new app is inert.

Summary by CodeRabbit

  • New Features
    • CLI settings and theme changes now refresh the running desktop app immediately.
    • Theme imports, removals, resets, and updates report whether the refresh succeeded.
    • Changes apply when the app regains focus or launches if immediate refresh is unavailable.
  • Bug Fixes
    • Improved validation and fallback handling for externally applied themes.
  • Documentation
    • Updated CLI and settings guidance with restart instructions for older app versions.