Repository navigation
fix(dashboard): route remaining raw Web Storage writes through safeStorage - #11752
Conversation
UX Review (Fable 5, fork) — ✅ PASSUX-level review of UX-Verdict: PASS Nothing to check. [UX-REVIEWED] a6ee0b6 |
Design Review (Fable 5, fork) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing to check. [DESIGN-REVIEWED] a6ee0b6 |
GPT 5.6 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. |
First Principles Review (Fable 5, fork) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS One behavior widening to eye: the git-panel auto-open now fires after a quota reclaim the old code suppressed — confirm that re-open loop stays closed ( What this change shipsInventory (10 items) — 10 justifiedIntent: FIX — stop a full localStorage quota from white-screening the app or silently dropping the unread record, by finishing the half-done migration onto the existing
[FIRST-PRINCIPLES-REVIEWED] a6ee0b6 |
Opus 4.8 Review (fork) — ✅ no blocking findingsReviewed |
…orage A QuotaExceededError from `localStorage.setItem` on the websocket onmessage -> dispatch -> re-render path bubbles out of an event handler, which a React ErrorBoundary cannot catch, and white-screens the app. `src/utils/safeStorage.ts` exists for exactly this: on a quota error it reclaims one tier of disposable cache and retries, escalating a tier at a time, so the write survives instead of being dropped beside reclaimable cache. Seven files already imported the helper but still wrote raw, and `dashboardSlice.ts` was the visible half-migration: it adopted `safeSetItem` for `mc-unread-slots` while writing the adjacent `mc-unread-shared` record raw inside a swallow-only catch. This finishes the migration for every file that already imports the module. - dashboardSlice: the shared unread record, the per-tab sentinel mirror, and the legacy-migration write now go through the helper. - ChatPage: the webpreview marker, and the git-panel auto-open guard. The guard keeps its semantics — it still skips the open when the marker cannot persist, but now reads that from the helper's return value rather than a caught throw, so a reclaimable quota no longer suppresses the open. - SchedulePage, WebPreviewPanel, MarkdownPanel: the dismiss marker and the two sessionStorage mirrors. `safeStorage.adoptionChokepoint.test.ts` pins the rule on the source: a file that imports the module may not write raw, with a `// raw-storage-ok: <why>` marker for a deliberate exemption. Reads and removeItem stay out of scope; this is about the write that throws. Tests: a quota-reclaim case in `dashboardSlice.test.ts` fails on the old code with the record silently empty and passes with it, plus the chokepoint guard. `npx vitest run` on the touched surfaces is green; `jscpd` reports 0 clones; eslint clean on all changed files. The remaining suite failures on this box (missing optional deps `html-to-image` / `lexical`, and the app-registry/nav-label files) are pre-existing and reproduce identically on a stashed baseline.
…d write Converting a throwing `localStorage.setItem` to `safeSetItem` is not a drop-in swap when the call sits in a `try` body with work after it. The raw call threw on a full quota, the surrounding catch swallowed it, and the next statement never ran. The helper reports the same failure by returning false instead, so execution continued into the projection write. That matters here because `mc-unread-slots` is a projection of the shared record's keys and is strictly smaller than it (keys only, no timestamps). On a quota it can free space and succeed where the record just failed, leaving two persisted records that disagree: `restoreUnreadSince` trusts the record, while older tabs and the hub relay read the projection. Bail out when the authoritative write fails, restoring the protection the throw used to provide. The regression test fails on the unguarded form with `expected [ 'chat-1' ] to deeply equal [ 'chat-1', 'chat-2', 'chat-3' ]`. Also drops the `// raw-storage-ok:` exemption from the new guard. It had zero consumers — every file in scope writes through the helper — so the escape hatch was a convention supported only by symmetry with the row-write guard's exercised marker. The first real exemption can add it back alongside its reason. Tests: dashboardSlice 51/51, the touched surfaces 116/116 across 5 files, eslint clean, jscpd 0 clones, tsc unchanged at the 78 pre-existing errors.
837e186 to
a6ee0b6
Compare
iamwhatever
left a comment
There was a problem hiding this comment.
Approved after a full read of the diff on the current head: the change matches the body and linked issue, every load-bearing claim was spot-checked against main, the failure paths were traced and fail safe, tests pin the behaviour, and every review-lane item on this head is either PASS or disposed.
…orage (kirodotdev#11752) * fix(dashboard): route remaining raw Web Storage writes through safeStorage A QuotaExceededError from `localStorage.setItem` on the websocket onmessage -> dispatch -> re-render path bubbles out of an event handler, which a React ErrorBoundary cannot catch, and white-screens the app. `src/utils/safeStorage.ts` exists for exactly this: on a quota error it reclaims one tier of disposable cache and retries, escalating a tier at a time, so the write survives instead of being dropped beside reclaimable cache. Seven files already imported the helper but still wrote raw, and `dashboardSlice.ts` was the visible half-migration: it adopted `safeSetItem` for `mc-unread-slots` while writing the adjacent `mc-unread-shared` record raw inside a swallow-only catch. This finishes the migration for every file that already imports the module. - dashboardSlice: the shared unread record, the per-tab sentinel mirror, and the legacy-migration write now go through the helper. - ChatPage: the webpreview marker, and the git-panel auto-open guard. The guard keeps its semantics — it still skips the open when the marker cannot persist, but now reads that from the helper's return value rather than a caught throw, so a reclaimable quota no longer suppresses the open. - SchedulePage, WebPreviewPanel, MarkdownPanel: the dismiss marker and the two sessionStorage mirrors. `safeStorage.adoptionChokepoint.test.ts` pins the rule on the source: a file that imports the module may not write raw, with a `// raw-storage-ok: <why>` marker for a deliberate exemption. Reads and removeItem stay out of scope; this is about the write that throws. Tests: a quota-reclaim case in `dashboardSlice.test.ts` fails on the old code with the record silently empty and passes with it, plus the chokepoint guard. `npx vitest run` on the touched surfaces is green; `jscpd` reports 0 clones; eslint clean on all changed files. The remaining suite failures on this box (missing optional deps `html-to-image` / `lexical`, and the app-registry/nav-label files) are pre-existing and reproduce identically on a stashed baseline. * fix(dashboard): stop the projection write after a failed shared-record write Converting a throwing `localStorage.setItem` to `safeSetItem` is not a drop-in swap when the call sits in a `try` body with work after it. The raw call threw on a full quota, the surrounding catch swallowed it, and the next statement never ran. The helper reports the same failure by returning false instead, so execution continued into the projection write. That matters here because `mc-unread-slots` is a projection of the shared record's keys and is strictly smaller than it (keys only, no timestamps). On a quota it can free space and succeed where the record just failed, leaving two persisted records that disagree: `restoreUnreadSince` trusts the record, while older tabs and the hub relay read the projection. Bail out when the authoritative write fails, restoring the protection the throw used to provide. The regression test fails on the unguarded form with `expected [ 'chat-1' ] to deeply equal [ 'chat-1', 'chat-2', 'chat-3' ]`. Also drops the `// raw-storage-ok:` exemption from the new guard. It had zero consumers — every file in scope writes through the helper — so the escape hatch was a convention supported only by symmetry with the row-write guard's exercised marker. The first real exemption can add it back alongside its reason. Tests: dashboardSlice 51/51, the touched surfaces 116/116 across 5 files, eslint clean, jscpd 0 clones, tsc unchanged at the 78 pre-existing errors. --------- Co-authored-by: DeryFerd <DeryFerd@users.noreply.github.com>
Problem / Motivation
website/src/utils/safeStorage.tsexists because the dashboard's per-origin localStorage quota fills up in normal use. Its own header names the offenders: the per-session virtualizer height caches (vc_heights_<sid>, up to 2000 entries each) pile up across every chat session ever opened and are never reclaimed, and themc-paste-store-v1sent-paste side table can reach several MB on its own. Once the origin is full, the nextlocalStorage.setItemanywhere in the app throwsQuotaExceededError.The helper handles exactly that. On a quota error it drops one tier of disposable cache and retries, escalating a tier at a time until the write succeeds or there is nothing reclaimable left. A raw
setItemin the same situation throws instead, and the throw lands wherever the write happened to be called from.The visible half-migration was in
dashboardSlice.ts. That file adoptedsafeSetItemformc-unread-slotsand left the adjacentmc-unread-sharedrecord written raw, two lines apart in the same function:Five files were in that state, all of them already importing the helper:
store/dashboardSlice.ts,pages/ChatPage.tsx,pages/SchedulePage.tsx,components/WebPreviewPanel.tsx, andcomponents/MarkdownPanel.tsx.Why it matters
Nothing looks broken when this happens, which is what makes it easy to miss.
safeStorage.tsstates the mechanism in its header: a quota error raised on the websocketonmessage→ Redux dispatch → re-render path bubbles out of an event handler or a commit phase, which a React ErrorBoundary cannot catch, and it white-screens the whole app. That is why the module was written in the first place.Even when the app survives, the data is lost for no reason. Every one of these sites writes inside a swallow-only
try/catch, so a full quota silently drops the write while megabytes of re-derivable cache sit right next to it, reclaimable.dashboardSlice.tsis the clearest case: the unread record is what the sidebar badge and the cross-window read relay are built on, and it was the one key in the function not going through the helper that exists to protect it.There is a slower cost too. A file that imports a hardening helper and then writes raw around it teaches the next reader that the helper is optional. The
mc-unread-sharedline sat one statement away from thesafeSetItemcall that would have fixed it, which is how a half-finished migration survives review.What changed (motivation → approach → change)
The symptom is a quota error escaping on the dispatch path. The root cause is that the write does not go through the helper that already exists for it. The change routes those writes through
safeSetItem/safeSetSessionItemso a full quota reclaims and retries instead of throwing.I scoped this to files that already import
safeStorage, on purpose. That makes the PR read as "finish the migration you started" rather than a repo-wide sweep, and it keeps the diff small enough to review in one pass. There are 47 more rawsetItemcalls elsewhere insrc/, in files with no helper import; those are a separate conversation about whether each one wants the reclaim behavior or is a throwaway marker where it does not matter. Leaving them out is deliberate, not an oversight.Three of the sites needed more than a mechanical swap, and all three are worth a reviewer's attention.
The git-panel auto-open guard in
ChatPage.tsxused the throw as control flow:That guard exists because opening the panel changes
tabsCtl, which re-runs the effect, and an absent marker would make it open again forever. The helper reports whether the write landed instead of throwing, so the guard becameif (!safeSetItem(key, '1')) return. The failure path is preserved exactly: if the marker cannot be persisted, the open is still skipped. What changes is that a quota the helper can reclaim from no longer suppresses the open, which is the point of the fix.WebPreviewPanel.tsxneeded a different kind of care. ItssaveAnnotateMirrorsdid both the write and the delete inside onetry. The write moved tosafeSetSessionItem; the delete stayed a plainremoveItemwith its own guard, because removing a key frees space rather than needing it, so it is not the quota failure mode this PR is about. Splitting them keeps the exemption visible rather than hiding it inside a shared catch.The shared unread record in
dashboardSlice.tsis the third, and it is the one a plain conversion gets wrong. Converting a throwing call is not a drop-in swap when work follows it in the sametry: the rawsetItemthrew on a full quota, the surrounding catch swallowed it, and the projection write below never ran.safeSetItemreports the same failure by returningfalseinstead, so execution continued, andmc-unread-slotsis a projection of the record's keys and strictly smaller than it, so on a quota it can free space and succeed where the record just failed. That leaves two persisted records that disagree, andrestoreUnreadSincetrusts the record while older tabs and the hub relay read the projection. The fix bails out when the authoritative write fails, restoring what the throw used to guarantee.The regression guard is the part that outlives this PR.
safeStorage.adoptionChokepoint.test.tsscans the source and fails if a file that importssafeStoragealso writes raw. Reads andremoveItemare out of scope; this is about the write that throws. There is no exemption marker: every file in scope writes through the helper today, so an escape hatch would be a convention with no consumer. It follows the shape of the existingdashboardSlice.rowWriterChokepoint.test.ts, which pins the same kind of rule on the row-write chokepoint: the rule is enforced where it can be read, not left to a reviewer noticing an omission.Backwards compatibility
Compatible. Nothing that main currently accepts stops working.
Two behavior notes, both widenings rather than tightenings. The
ChatPage.tsxgit-panel guard now proceeds with the auto-open when the marker write succeeds after reclaiming space, where the old code skipped it; it still skips when the write genuinely fails. ThesessionStoragewrites inMarkdownPanel.tsxandWebPreviewPanel.tsxnow swallow a quota or SecurityError inside the helper instead of inside a localtry, which is the same outcome for the caller.No public API, config key, or persisted format changes. The storage keys and their JSON shapes are untouched.
Tests
website/src/test/dashboardSlice.test.tsgains a quota-reclaim case. It seeds a disposablevc_heights_*entry, makesStorage.prototype.setItemthrowQuotaExceededErroronly while that entry is present, dispatches amarkSlotUnreadarrival, and asserts the shared record persisted and the disposable entry was the thing sacrificed. I wrote it first and watched it fail withexpected {} to deeply equal { 'chat-1': '2026-01-01T00:00:00Z' }— the record silently empty — then made it pass. It fails again on any future revert to a raw write at that site.expected [ 'chat-1' ] to deeply equal [ 'chat-1', 'chat-2', 'chat-3' ].website/src/test/safeStorage.adoptionChokepoint.test.tsis new. It pins the rule on the source, and it also asserts its own matchers still work, so it cannot pass vacuously if the regex stops matching.safeStorage,dashboardSlice, the new chokepoint guard,slotReadRelay,ChatPage.gitPanelGate): 116 tests pass across 5 files, three consecutive runs, no flakes.npx jscpd .reports 0 clones, which matters here because thepretesthook runs the duplication gate before any test does.npx eslinton all seven changed files is clean.npx tsc -breports 78 errors on this box, and 78 with the change applied — zero added. The remainder are pre-existing missing optional deps (html-to-image,lexical) that fail identically on a stashed baseline.Manual verification
N/A — unit coverage is sufficient here. The behavior under test is "does the write survive a quota error," which is decided entirely in the helper and is directly observable in the test above; no rendering or integration path is involved.
One thing worth eyeballing if a reviewer wants to, since the tests cannot show it: with the dashboard open, fill the origin by opening enough chat sessions that the height caches accumulate, then mark a session unread. Before this change the badge would fail to persist across a reload once the quota was hit; after it, the disposable cache is dropped and the badge survives.
Related Issues
No dedicated issue for these call sites. #10678 is the same class of fix for
treeExpansionMemory.tsand is still open, which is useful evidence that the pattern is wanted rather than an argument against this PR.Screenshots / video
Why no screenshot: every site keeps its rendered output identical in the normal case; the only difference appears when the origin quota is already full, where a write now succeeds instead of being dropped, so there is no visual delta to capture. Every site in this PR keeps its rendered output identical in the normal case; the difference only shows up when the origin quota is already full, where the write now succeeds instead of being dropped. The manual steps above reproduce that state if a reviewer wants to see it, but a screenshot of a badge that persists is not distinguishable from a screenshot of a badge that was never at risk.
Pattern harvest
Rule candidate: review-prompt
Pattern: a file that imports a shared hardening helper still calls the raw primitive it wraps, for a neighbouring key in the same function.
Checklist
safeStorage.ts's own header already states the contract this PR enforces