Repository navigation
fix(files): route tree-expansion memory through safeStorage - #16596
Conversation
The Files tree mirrors its expansion to localStorage so a reload does not start fully collapsed. That mirror wrote with a raw setItem inside a bare catch, which discards a QuotaExceededError outright. `utils/safeStorage` exists for exactly this write: on a quota error it drops one tier of disposable cache -- the per-session virtualizer height caches, which accumulate across every chat ever opened and are the documented reason the origin fills up -- and retries. Bypassing it meant the mirror quietly stopped persisting while megabytes of re-derivable cache sat next to it, and nothing recovered it. The read had a second dead end. `readStore` returned null when localStorage was unusable so a doomed write could be skipped, but a value that merely failed JSON.parse took the same branch. One corrupt string therefore blocked every later write and left the bad value in place, so expansion never persisted again for the life of the profile. With safeSetItem making a doomed write harmless, that channel has no remaining purpose: the read now falls back to an empty record, and the next expansion overwrites the corrupt value. Storage that is unusable rather than full still degrades to session-only memory and never throws, which the third test pins. Ported onto main's current createProjectPathMemory factory structure (the module was generalized after this change was authored); the fix is applied inside the factory, so both the expansion mirror and the sibling "Folders not readable" dismissals memory share it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: leonlaiyc <276034861+leonlaiyc@users.noreply.github.com>
UX Review (Opus 5.5) — ✅ PASSUX-level review of UX-Verdict: PASS Nothing to check. [UX-REVIEWED] c7f39dd Description read[DESCRIPTION-READ] 28d237fb52db8ed8d6685ed6fdc12866bdb9a92fd83661c2fa86d1abd8178ba3 That sha256 names everything this verdict read: the PR description, plus the 3 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS If [DESIGN-REVIEWED] c7f39dd Description read[DESCRIPTION-READ] f66e748986b83c5610f2ca4dc5d538d6ce0552086790a770113347e93440df61 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing to check. What this change shipsInventory (4 items) — 4 justifiedIntent: keep the Files tree's open folders after a reload, even when localStorage is full or holds a corrupt value (FIX, #16595).
[FIRST-PRINCIPLES-REVIEWED] c7f39dd Description read[DESCRIPTION-READ] 15e43da5ab8dc3af4703c68cc10d79391845862a6342f56e493f3941883a3ff5 That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
Problem / Motivation
Goal: Keep the workspace file tree's expansion state surviving a page reload even when
localStorageis full or holds a corrupt value.The Files tree mirrors its expansion state to
localStorageso a reload restores which directories were open. Two defects make that mirror silently stop working: once triggered, the tree starts fully collapsed on every reload and each file open costs re-expanding the path from the root.Why it matters
Both defects are reached in normal long-running use — the origin quota fills from accumulated per-session virtualizer height caches, and a single partial write leaves a corrupt value. Either one permanently disables reload persistence for the file tree with no warning, so users silently lose a convenience the feature exists to provide.
Not a goal
localStorage.setItemcall sites tosafeStorage; that incremental migration issafeStorage.ts's own documented plan, not this fix.What changed (motivation → approach → change)
website/src/pierre/treeExpansionMemory.ts, the mirror write used a rawlocalStorage.setIteminside a barecatch(discarding aQuotaExceededErroroutright instead of reclaiming disposable cache and retrying), andreadStorereturnednullboth for genuinely-unusable storage and for a value that merely failedJSON.parse— so one corrupt string blocked every later write and was never overwritten.utils/safeStorage(safeGetItem/safeSetItem) and makereadStorefall back to an empty record ({}) for anything unusable.safeSetItemreclaims a tier of disposable cache and retries on a full quota; the next expansion overwrites a corrupt value. Storage that is unusable at all (private mode) still degrades to session-only memory and never throws.Since the original PR was authored, main generalized this module into a
createProjectPathMemory(storageKey)factory shared by the expansion memory and the sibling "Folders not readable" dismissals. The fix is applied inside the factory, so it is ported faithfully to main's current structure and both instances benefit.Backwards compatibility
Removes nothing: recall semantics are unchanged (a corrupt value still yields
[]), the storage key and record shape are untouched, and the only behavior change is that doomed writes now reclaim-and-retry instead of being silently dropped. The removedreadStorenullchannel had one caller (therememberwrite guard) which is updated in the same diff.Tests
New
website/src/pierre/treeExpansionMemory.test.ts(3 cases):SecurityError).Local run:
vitest3/3 pass,tsc -p tsconfig.app.jsonclean,eslintclean on both files.Manual verification
N/A — unit coverage sufficient. The three storage-pressure branches (full quota, corrupt value, unusable storage) are exercised deterministically by the new tests with mocked
Storage.prototypethrowing the realDOMExceptionsubtypes.Screenshots / video
no-visual-delta
Why no screenshot: the diff touches only
treeExpansionMemory.ts(storage error-handling) and a new test file — no JSX, no user-facing strings, no rendered control. The only user-visible effect is the file tree correctly staying expanded across reloads, which no still image can show.Related Issues
Fixes #16595
Pattern harvest
Not generalizable: this is a specific two-defect fix in one module's storage mirror (a bare-catch raw write that should use the shared quota-reclaiming helper, and a read channel that conflated corrupt values with unusable storage). The broader "migrate raw
localStoragetosafeStorage" effort is already tracked assafeStorage.ts's own incremental plan.Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)Supersedes #10678 by @leonlaiyc, which had drifted conflicting with main. Original authorship is preserved in git (author
Leon <leon.lai.yc@gmail.com>, co-authors@leonlaiycand Claude Opus 5), and @leonlaiyc is credited in the README contributors list.