fix(frontend): drop infectious preference fallback, add migration latch

QA review of the previous hardening commit identified two cross-user
infection paths created by the smart-fallback read chain (CRIT-1/2),
plus a UI-desync hazard from validation guards in setters (HIGH-1).

- Extracted localStorage helpers and migration logic into
  composables/preferenceStorage.ts as a single source of truth.
- Reads now hit only the active user's namespace - no anon / legacy
  fallback in the hot path. Pre-existing values are migrated **once**
  per active user via migratePreferencesIntoUser, which adopts anon
  and legacy keys into the user namespace and then deletes the source
  keys, so a second user logging into the same browser cannot inherit
  the previous user's preferences.
- Migration latch tracks which user has already been migrated this
  session; auth store's reloadUserPreferences resets the latch on
  setUser/logout so the next active user gets a fresh migration.
- setSansFont / setMonoFont / setFontSize / setTheme now return a
  boolean; GeneralSettings.vue handlers roll the form back to the
  canonical state when validation rejects a value, preventing the
  UI from drifting out of sync with the source of truth.
- safeSetItem now console.warn on persistence failures so quota /
  private-mode issues are at least diagnosable.
- applyFont is diff-aware: only the CSS variables that actually
  changed are written to the root style, avoiding unnecessary style
  recalculation when only one knob is flipped.
- applyTheme caches the last effective mode to skip redundant
  attribute writes and Wails IPC calls.

https://claude.ai/code/session_01S1FVPf4jRfWXkTSnpMmMp7
This commit is contained in:
Claude
2026-05-02 08:55:10 +00:00
committed by lyingbug
parent 2e0b423057
commit 35a5e29a23
5 changed files with 198 additions and 120 deletions
@@ -292,23 +292,37 @@ const handleMemoryChange = (val: boolean) => {
// 处理主题变化
const handleThemeChange = (val: ThemeMode) => {
setTheme(val)
if (!setTheme(val)) {
// Setter rejected the value (validation guard); roll the form back to
// the canonical state so the UI doesn't drift.
localTheme.value = currentTheme.value
return
}
MessagePlugin.success(t('common.success'))
}
// 处理字体变化
const handleSansFontChange = (val: FontKey) => {
setSansFont(val)
if (!setSansFont(val)) {
localSansFont.value = currentSans.value
return
}
MessagePlugin.success(t('common.success'))
}
const handleMonoFontChange = (val: MonoFontKey) => {
setMonoFont(val)
if (!setMonoFont(val)) {
localMonoFont.value = currentMono.value
return
}
MessagePlugin.success(t('common.success'))
}
const handleFontSizeChange = (val: FontSizeKey) => {
setFontSize(val)
if (!setFontSize(val)) {
localFontSize.value = currentSize.value
return
}
MessagePlugin.success(t('common.success'))
}
</script>