What the agent does
The agent reads how the store saves itself today, since persistence that already exists has to keep working for people who are signed in. In a React app like satnaing/shadcn-admin, src/stores/auth-store.ts does it by hand. The initializer reads a cookie with getCookie, runs JSON.parse on it and starts with that as accessToken; setAccessToken writes the token back with setCookie, and reset removes it. src/lib/cookies.ts gives every cookie a seven-day max-age and writes values as they are, with no encoding. Only the token is stored. The sign-in form sets a user with an exp a day ahead and then the token, so after a reload the store has a token and no user. Everything, fields and actions alike, lives inside one nested auth object, and zustand resolves to 5.0.12 in pnpm-lock.yaml.
It wraps the initializer in persist from zustand/middleware and deletes the manual cookie calls from the actions. The options carry the design. name stays the existing cookie name. storage is createJSONStorage over a small adapter built on the same three cookie helpers, so the token stays where it was with the same expiry and does not move to localStorage. The adapter passes values through encodeURIComponent and back: the middleware stores JSON, and the agent's test of an unencoded write shows the cookie cut off at the first semicolon. partialize selects user and accessToken, so no function is serialized. The stored value becomes an envelope, {"state":{"auth":{...}},"version":0}, in place of today's bare string.
Two things break silently if the options stop there, and the agent finds both by running them. The middleware merges saved state into the store shallowly, so the saved auth, which holds only two fields, replaces the live one, and after a reload auth.setUser is undefined; the sign-in form would throw on its next submit. The fix is a merge function that spreads the saved fields into the current auth and keeps its actions. The second is the people already signed in. Their cookie holds the old bare string under the same name, the middleware finds no state in it and starts empty, and a version with a migrate function does not help, because migration is never offered a value without the envelope. The agent handles it in the adapter's getItem: a value that parses to a plain string is wrapped into the new shape, and a test loads an old cookie and finds the token.
Since the user is now restored too, the merge drops a user whose exp has passed together with its token. Tests go in src/stores/auth-store.test.ts and run in the project's Vitest browser mode against a real document.cookie, which the middleware makes easier: useAuthStore.persist.rehydrate() rereads the cookie on demand and persist.clearStorage() removes it, so no test needs a newly imported module. The two component tests that replace the store with vi.mock do not change. The agent runs pnpm test:browser:install, pnpm test, pnpm lint, pnpm format:check and pnpm build, the steps of .github/workflows/ci.yml.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "Move this app's Zustand auth store from hand-written cookie persistence to the persist middleware. Read package.json, pnpm-lock.yaml (the zustand version), vite.config.ts, eslint.config.js, .github/workflows/ci.yml, src/stores/auth-store.ts, src/lib/cookies.ts and its test, src/test-utils/cookies.ts, src/main.tsx, and the sign-in form and sign-out dialog with their tests first. Wrap the store in persist from zustand/middleware with: the existing cookie name; createJSONStorage over a StateStorage adapter built on getCookie, setCookie and removeCookie that URI-encodes values, so the token stays in a cookie with the same max-age; partialize that keeps only auth.user and auth.accessToken; and a merge function that spreads the saved fields into the current auth object so its actions survive rehydration. Remove the manual cookie calls from the actions. Existing visitors have a cookie holding a bare JSON string token under the same name: keep them signed in by converting that value in the adapter's getItem, and test it. In merge, drop a restored user whose exp is in the past, along with the token. Add src/stores/auth-store.test.ts in the project's browser mode covering: the stored shape, a reload that restores user and token with working actions, what the default shallow merge would have done, an old-format cookie, a malformed cookie, a token containing a semicolon, an expired user, reset, and persist.clearStorage(); use persist.rehydrate() to reread the cookie between steps. Do not change the component tests that mock the store. Run pnpm install --frozen-lockfile, pnpm test:browser:install, pnpm test, pnpm lint, pnpm format:check and pnpm build, paste the output, and open a pull request that explains each persist option and what breaks without it.",
"resources": [
{
"url": "https://github.com/your-org/your-app"
}
]
}- Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
- Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
- Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.
What comes back
A pull request with two files: src/stores/auth-store.ts rewritten around persist, and a new src/stores/auth-store.test.ts. Whether the cookie adapter lives in the store file or beside the helpers in src/lib/cookies.ts is the agent's call, stated in the description. The components are untouched, because the store's public shape, auth.user, auth.accessToken and the four actions, is the same. The description walks through the options one at a time, each with the failure it prevents: no partialize and the whole auth object is serialized with its functions silently dropped, no merge and they vanish after a reload, no encoding and a semicolon truncates the value, no legacy handling and every current session ends on deploy.
A reviewer checks the size and the secrecy of what is now stored. Browsers cap a cookie at about four kilobytes, the value is URI-encoded JSON, and a user object with a long role list grows it; the description gives the encoded length for the mock user. The cookie is readable by scripts on the page, as it was before, so nothing more sensitive than the old token should be added to partialize without thought. They also confirm with whoever runs the backend that no server code parses this cookie, since its shape is no longer a bare token.
Run it on every Linear issue labelled persist-state
Once one store is persisted this way, the next request is a sentence in a ticket: remember the sidebar, keep the table filters, save a draft. In the console, add an automation on the Project that holds the repository with a linear trigger on linear.issue.labeled and the label persist-state, and a prompt that says: persist the store or fields the issue names with the same middleware options as the auth store, choose localStorage unless the issue says cookie, write tests for reload and for the shape stored, and open a pull request. The issue goes after the prompt as the event and fills in nothing. The trigger needs a Linear connection for the automation's runner and fires only for a label applied by someone in your Rush org.
The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.
Why run it on Rush?
Persistence code passes every check that does not reload the page. The failures here, actions missing after rehydration and sessions dropped by a format change, only exist across a write, a fresh read and a second use of the store, in a real browser with a real cookie jar. The agent runs exactly that sequence in headless Chromium inside its own sandbox, for the new format and the old one, and the pull request quotes what happened. Doing the same by hand means signing in, deploying a branch and signing in again, which is why it usually is not done.
The run also leaves a trail a reviewer can follow. Each experiment, including the ones that failed and shaped the design, is in the session's transcript with its command and output. A claim like the default merge drops the actions is there as a test that failed before the merge option was added.
Frequently asked questions
Why do my Zustand actions disappear after rehydration?
Because the state is nested. The persist middleware's default merge is shallow, so a saved auth object with only data fields replaces the live auth object that also held the actions. Pass a merge option that spreads the saved fields into the current nested object, or keep actions at the top level of the store.
Will users be signed out when this ships?
Not with the legacy handling the request asks for. The old cookie holds a bare token and the new format wraps state in an envelope; without conversion the middleware ignores the old value and starts empty. The adapter converts it on first read, and a test loads an old-format cookie to prove the token comes through.
Is hydration asynchronous? Do I need a loading state?
Not with a synchronous storage such as a cookie or localStorage. In the agent's check the store reports persist.hasHydrated() as true immediately after it is created, so code that reads useAuthStore.getState() at startup, as src/main.tsx does, sees the restored token.
More Zustand jobs
The same job on other stacks
- Persist Redux Toolkit State Across Page Reloads (Redux Toolkit)
More jobs like this: Turn a Linear label into a PR
- Add Storybook to a SolidJS App (SolidJS)
- Add Streamable HTTP to a stdio MCP Server (MCP server)
- Add Structured Data to a Pelican Blog (Pelican)
- Add JSON-LD Structured Data to a Zola Site (Zola)