Skip to content

fix(files): route tree-expansion memory through safeStorage - #16596

Merged
bolichen97 merged 1 commit into
mainfrom
fix/tree-expansion-safe-storage-10678
Oct 4, 2026
Merged

bolichen97 merged 1 commit into
mainfrom
fix/tree-expansion-safe-storage-10678

Conversation

@iamwhatever

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Keep the workspace file tree's expansion state surviving a page reload even when localStorage is full or holds a corrupt value.

The Files tree mirrors its expansion state to localStorage so 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

  • Migrating the ~25 remaining raw localStorage.setItem call sites to safeStorage; that incremental migration is safeStorage.ts's own documented plan, not this fix.

What changed (motivation → approach → change)

  • Symptom: the file tree starts collapsed after a reload once the origin quota is full, and permanently once a corrupt value is stored.
  • Root cause: in website/src/pierre/treeExpansionMemory.ts, the mirror write used a raw localStorage.setItem inside a bare catch (discarding a QuotaExceededError outright instead of reclaiming disposable cache and retrying), and readStore returned null both for genuinely-unusable storage and for a value that merely failed JSON.parse — so one corrupt string blocked every later write and was never overwritten.
  • Change: route the read/write through utils/safeStorage (safeGetItem / safeSetItem) and make readStore fall back to an empty record ({}) for anything unusable. safeSetItem reclaims 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 removed readStore null channel had one caller (the remember write guard) which is updated in the same diff.

Tests

New website/src/pierre/treeExpansionMemory.test.ts (3 cases):

  • Reclaims disposable cache and retries when the origin quota is full, so the write lands and a reload restores the tree.
  • Recovers from a corrupt stored value instead of being stuck on it forever.
  • Still degrades to session-only memory (never throws) when storage is unusable (private-mode SecurityError).

Local run: vitest 3/3 pass, tsc -p tsconfig.app.json clean, eslint clean 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.prototype throwing the real DOMException subtypes.

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 localStorage to safeStorage" effort is already tracked as safeStorage.ts's own incremental plan.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable) — module docstring updated to match the new quota behaviour
  • No secrets, credentials, or internal references in the diff

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 @leonlaiyc and Claude Opus 5), and @leonlaiyc is credited in the README contributors list.

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>
@iamwhatever
iamwhatever requested a review from a team October 3, 2026 18:48
@iamwhatever
iamwhatever requested a review from a team as a code owner October 3, 2026 18:48
@iamwhatever
iamwhatever requested a review from CrysisDeu October 3, 2026 18:48
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 3, 2026
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ✅ PASS

UX-level review of c7f39dd824efb55f2b8747c2f7e45f83f15b9730 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of c7f39dd824efb55f2b8747c2f7e45f83f15b9730 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

If getItem throws but setItem still works, readStore now returns {} and the next write replaces every other project's saved entry. Confirm that state can't happen.

[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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of c7f39dd824efb55f2b8747c2f7e45f83f15b9730 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: PASS

Nothing to check.

What this change ships

Inventory (4 items) — 4 justified

Intent: keep the Files tree's open folders after a reload, even when localStorage is full or holds a corrupt value (FIX, #16595).

  1. When the storage quota is full, saving the tree's open folders now frees disposable cache and tries again, so the folders stay open after a reload — justified
  2. A corrupt stored value no longer blocks every later save; the next folder expansion overwrites it — justified
  3. A full-quota save now evicts other disposable caches (virtualizer heights, through the existing safeSetItem tiers) — justified
  4. The "Folders not readable" dismissals get the same storage fix through the shared factory (treeUnreadableDismissals.ts:24) — justified

[FIRST-PRINCIPLES-REVIEWED] c7f39dd

Description read

[DESCRIPTION-READ] 15e43da5ab8dc3af4703c68cc10d79391845862a6342f56e493f3941883a3ff5

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed c7f39dd824efb55f2b8747c2f7e45f83f15b9730 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] c7f39dd

Verdict parsed from the review's SHA-scoped output markers for commit c7f39dd824efb55f2b8747c2f7e45f83f15b9730.

False positive or not applicable? A repository writer can comment:
/ai-review override fable c7f39dd824efb55f2b8747c2f7e45f83f15b9730: <one-sentence reason>

@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of c7f39dd824efb55f2b8747c2f7e45f83f15b9730 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] c7f39dd

False positive or not applicable? A repository writer can comment:
/ai-review override gpt c7f39dd824efb55f2b8747c2f7e45f83f15b9730: <one-sentence reason>

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 3, 2026
@bolichen97
bolichen97 merged commit 2a06c7e into main Oct 4, 2026
98 checks passed
@bolichen97
bolichen97 deleted the fix/tree-expansion-safe-storage-10678 branch October 4, 2026 03:13
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 4, 2026
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.

fix(files): tree-expansion mirror silently stops persisting on full quota or a corrupt stored value

3 participants