DEV Community

Sherotree
Sherotree

Posted on Fully Autonomous

Stop the agent from reading localStorage directly when the repo has a storage wrapper

The PR calls localStorage.getItem or localStorage.setItem in a component, or it JSON-parses a raw key on first render. The repo already has a storage wrapper with a key prefix, parse/serialize, an SSR guard, and maybe a schema version. Nearby preference and draft code uses it. The direct call crashes during SSR, collides with an unprefixed key, and skips the migration the wrapper already owns.

I've started treating a raw localStorage call as a contract break, not as a quicker persist. Saving a string is the easy part. A safe key, a parse path, and a server-safe read are why the wrapper is there.

Why the direct call shows up

A tutorial "remember the tab" example is localStorage.setItem("tab", id) in an effect. The model can write that without finding lib/storage or useLocalStorage. Your wrapper may be named storage.get, prefs.get, or safeStorage, which is easy to miss if the prompt only says "persist this."

You also skip quota errors, private-mode exceptions, and the rule about not reading storage during render on a server-rendered page.

A simple storage policy

  • New browser persistence uses the existing storage wrapper
  • Do not call localStorage or sessionStorage directly in a feature file
  • Do not read storage during render on an SSR path
  • A new key, prefix, or schema version is a wrapper change, with a test

If the value belongs on the server or in the existing data library, that is not a localStorage feature. If the team already persists this key through the wrapper, extend that key instead of inventing a second one.

What to put in the prompt

Use the existing storage wrapper for this preference.
Do not call localStorage or sessionStorage directly in this file.
Keep the key prefix, parse path, and SSR guard on the wrapper.
If the value should live on the server or in the existing data library, use that instead.
Enter fullscreen mode Exit fullscreen mode

Paste one existing wrapper usage. In review, search the diff for localStorage, sessionStorage, and a JSON.parse of a raw storage key in a component.

Reject / send back

Reject

  • A feature-level persist that calls localStorage or sessionStorage directly
  • A raw key with no prefix when the wrapper already namespaces keys
  • A first-render storage read that is unsafe on the server
  • A second storage helper added for one preference

Accept

  • A preference that uses the existing storage wrapper
  • A wrapper PR that adds a documented key or schema version and a test
  • A value that belongs on the server and never entered localStorage

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

Calling a raw localStorage call a contract break instead of a shortcut is the right frame — and your reject list is the part I would steal for reviews, especially "a second storage helper added for one preference," which is how the wrapper quietly dies.

One gap in the enforcement, though: the prompt and the review grep both depend on a human noticing. The version that actually holds is making the violation fail CI — a lint rule that blocks localStorage and sessionStorage in feature files, so the agent gets a red build instead of a polite request. And a related question: does your wrapper prefix keys itself, or does it trust the caller to pass a prefixed key? If storage.get accepts any raw key, an agent can still smuggle an unprefixed key through the wrapper — the prefix has to live inside the wrapper, not in the caller's discipline.

Have you tried lint enforcement for this, or does the prompt-plus-review version hold up well enough in practice?