Skip to content

fix(workspace): finish guarding browser storage reads - #827

Merged
Ishaan Gangwani (ishaan1124) merged 2 commits into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/guarded-storage-sweep
Sep 29, 2026
Merged

Ishaan Gangwani (ishaan1124) merged 2 commits into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/guarded-storage-sweep

Conversation

@aniruddhaadak80

Copy link
Copy Markdown
Contributor

Completes the storage-guard sweep started in #771. Refs #771.

Why this is a separate PR

#771 makes the workspace, artifact and session-tab stores read localStorage defensively. That is correct, but the blank page it sets out to prevent is still reachable, because three reads run before any of that code executes. The remaining sites do not overlap with #771's files, so this is a separate branch rather than more commits on a PR that is currently being rebased.

The blocker: the theme provider sits above the error boundary

This is the one that matters, and it is not a typeof-guard mistake at all, it is a completely unguarded read.

getStoredColorScheme in frontend/ui/src/theme/context.tsx:35 reads the global with no try of any kind. ThemeProvider calls it from its synchronous init at :84, createSimpleContext runs that init in the provider body, and app.tsx mounts the tree as:

<MetaProvider>            // :58
  <ThemeProvider>         // :59   <-- throws here
    <LanguageProvider>    // :60
      <UiI18nBridge>      // :61
        <ErrorBoundary>   // :62   <-- can only catch what is below it

ErrorBoundary is a descendant of the provider that throws, so there is nothing above it to catch the error. With storage blocked the app still renders a blank page, just earlier and from a different file than #771 addresses. The same file already wraps its write at :45-47 in try/catch, so the read was simply missed.

Net effect after this change: with storage blocked the workspace boots and persists nothing, instead of dying on first render.

The rest: a typeof guard sitting outside the try

A typeof check invokes the very getter that throws, so it cannot protect the read. #771 identified this pattern; these sites had it:

Site Reachability
pages/session.tsx:108-115, 123-131 sidebar collapse state and width, called at render via createSignal
atlas/SkillsPage.tsx:83-104 sessionStorage, the only two occurrences in the repo
atlas/SkillsPage.tsx:247 localStorage in a component body
components/prompt-input.tsx:948 localStorage in a component body

In each case the existence check moves inside the try, which is what entry.tsx:27-31 already does, with a comment explaining exactly why.

A subtler one: a default argument escapes its own try

atlas/right-pane-layout.ts:50-79 took storage = localStorage as a default parameter. Default parameters are evaluated at the call site, before the function body runs, so the try inside the function could never catch a throwing read. RightPane calls both functions without the argument, so the guard was inert in practice.

This contradicts a convention the repo already documents, in atlas/files/artifact-view.ts:17-21: resolve storage inside the caller's try, never as a default. Both functions now take storage? and resolve it inside the guard.

Scope

  • No CHANGELOG.md entry on purpose. fix(workspace): read the localStorage global defensively #771 owns the blocked-storage entry, and both edits would land in the same region. Whoever merges these two should reconcile the wording; as it stands fix(workspace): read the localStorage global defensively #771's "The workspace starts when browser storage is blocked" becomes accurate once this lands, rather than before.
  • No new test. The functions that matter here are component-local and not exported, and ThemeProvider cannot be exercised without a Solid render harness. I did not want to add a test that only re-asserts the try is present. The honest coverage is CI plus a manual check, and I would rather say so than pad the diff. A blocked-storage.test.ts addition in fix(workspace): read the localStorage global defensively #771 that installs a throwing getter and renders the provider would be the right follow-up.
  • One site left alone: frontend/workspace/public/openscience-theme-preload.js has three unguarded reads, but it is a classic load-time script, so a throw only skips the theme preload and the meta[name=theme-color] update. It cannot stop the app booting. Worth a separate one-line try, not worth blocking this on.

Verification

This is a single-parent commit on f1bdf252, deliberately. #771's original fix was lost because it rode in on a merge commit that a later rebase dropped; this branch carries a plain single parent so a rebase keeps it.

synthetic-sciences#771 makes the workspace, artifact and session-tab stores read
`localStorage` defensively, but the blank page it sets out to prevent is
still reachable. With storage blocked, three reads run before any of that
code executes.

The theme provider is the one that matters. `getStoredColorScheme` reads
`localStorage` with no guard at all, `ThemeProvider` calls it from its
synchronous `init`, and `app.tsx` mounts `ThemeProvider` as an ancestor of
`ErrorBoundary`. Nothing above the boundary can catch the throw, so the
app still dies on first render. This is why the changelog entry in synthetic-sciences#771
overstates what that PR delivers.

The rest are the same mistake synthetic-sciences#771 already identified, in the form where a
`typeof` guard sits outside the `try` that was meant to cover it. A
`typeof` check invokes the very getter that throws, so it cannot protect
the read:

- `pages/session.tsx` read the sidebar collapse state and width, called at
  render.
- `atlas/SkillsPage.tsx` read `sessionStorage` and `localStorage`.
- `components/prompt-input.tsx` read `localStorage` in a component body.

`atlas/right-pane-layout.ts` has a subtler version: `localStorage` is a
default parameter, and default parameters are evaluated at the call site,
before the function's own `try` can catch anything. The read path was
already called inside a `try` by `RightPane`; only the write path was
exposed. That file's sibling `artifact-view.ts` documents the correct
pattern in a comment, so this follows it.

Net effect: with storage blocked the workspace now boots and persists
nothing, instead of rendering a blank page.

Single parent, rebased onto current `main`, so a rebase carries it. This is
a separate branch from synthetic-sciences#771 on purpose: that branch is being rebased, and
these files do not overlap with it.

Refs synthetic-sciences#771
@vercel

vercel Bot commented Sep 29, 2026

Copy link
Copy Markdown

ANIRUDDHA ADAK (@aniruddhaadak80) is attempting to deploy a commit to the InkVell Team on Vercel.

A member of the Team first needs to authorize it.

Two CI corrections to the storage sweep.

`theme-lock.test.ts` pins its expectations against the *source text* of
theme/context.tsx, including the exact line

  localStorage.removeItem(STORAGE_KEYS.LEGACY_THEME_CSS_LIGHT)

Routing that block through `storageOrNothing` renamed the call and broke
the assertion. The legacy-key cleanup is now guarded in place with a
try/catch, which keeps the pinned text and matches the idiom the same file
already uses for the CSS cache a few lines above. The reads still go
through the helper, because no test pins those.

Dropping the `= localStorage` default also shortened the readPaneWidth
signature below the print width, so Prettier reflows it onto one line.
@aniruddhaadak80

Copy link
Copy Markdown
Contributor Author

Second commit pushed to fix the two checks that failed on the first one.

What CI caught

Format flagged right-pane-layout.ts only. Dropping the = localStorage default shortened the readPaneWidth signature to 119 characters, which is under printWidth: 120, so Prettier reflows it onto a single line. Fixed in 157f2443.

Frontend unit tests failed one test, and this one is worth flagging to reviewers:

(fail) OpenScience theme gallery > starts with the product palette while preserving saved themes and display mode

theme-lock.test.ts asserts against the source text of theme/context.tsx, not its behaviour. Line 25 pins this exact string:

expect(theme).toContain("localStorage.removeItem(STORAGE_KEYS.LEGACY_THEME_CSS_LIGHT)")

Routing the legacy-key cleanup through my storageOrNothing helper renamed the call and broke the assertion, even though the behaviour was identical.

Rather than edit the test, I kept the pinned text and guarded that block in place with a try/catch. That is also the idiom the same file already uses for the CSS cache a few lines earlier, so it reads more naturally than the helper would have:

try {
  localStorage.setItem(STORAGE_KEYS.THEME_ID, lockedTheme)
  localStorage.removeItem(STORAGE_KEYS.LEGACY_THEME_CSS_LIGHT)
  localStorage.removeItem(STORAGE_KEYS.LEGACY_THEME_CSS_DARK)
} catch {}

The reads still go through storageOrNothing(), because nothing pins those.

Why this is worth knowing beyond this PR

That test is a source-text assertion, so it is sensitive to renames rather than to behaviour. It passed while getStoredColorScheme read localStorage completely unguarded, which is precisely the read that breaks the app. In other words the suite could not have caught the blocker, and it can fail on a behaviour-preserving refactor. I am not proposing to change it here, but it may be worth a separate look at whether that assertion should target behaviour.

Current state

All 8 checks green on e3464fc6:

Check Result
Typecheck pass
Build (web) pass
Format pass
Test pass
Frontend unit tests pass
compatibility pass
harbor-native-trial pass
Gitleaks pass

Both commits have a single parent, so a rebase carries them.

@ishaan1124
Ishaan Gangwani (ishaan1124) merged commit 01f0ced into synthetic-sciences:main Sep 29, 2026
8 of 9 checks passed
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.

2 participants