puter.js: 401 `token_auth_failed` never replays the request, so every call fails with Unauthorized after a successful re-login
Summary
In a browser (puter.env === 'web'), a 401 carrying the legacy code token_auth_failed opens the sign-in flow but never re-issues the original request. Once the user signs in successfully, the call that triggered the prompt still rejects with { status: 401, message: 'Unauthorized' }, so the user has to repeat the action by hand.
reauth_required — the other reauth signal — recovers transparently, so the two signals behave differently for the same situation.
Steps to reproduce
Open the Puter web app in a browser.
Make the stored bearer token fail authentication (e.g. sign the session out server-side, or sign in as another account on the same device so the in-tab token is no longer valid).
Call any authenticated SDK API from the devtools console, e.g.
await puter.fs.stat('/');The sign-in prompt opens. Complete it.
Actual: the promise rejects with Unauthorized (status 401) even though the new token is valid. The request is sent once.
Expected: the request is replayed once with the fresh token and resolves — the documented behaviour of fetchUrl:
A 401 carrying a reauth signal — the reauth flow is driven first and, on success, the request is replayed once with the fresh token (transparent recovery).
This affects both SDK transport paths, so it hits driver calls too (puter.fs.*, puter.kv.*, puter.drivers.call(...)).
Root cause
resolveReauth() in src/puter-js/src/lib/networkUtils.js returns one of replay / reject / null, and both callers act only on action === 'replay':
src/puter-js/src/lib/utils.js:105(handle_resp, generic XHR path)src/puter-js/src/lib/networkUtils.js:608(classifyRetry, driver-call path)
The reauth_required branch returns { action: 'replay' } after puter.triggerReauth() resolves (line 98). The token_auth_failed branch awaits puter.ui.authenticateWithPuter() and then falls out of the if to return null (line 129) — so the caller is told "this 401 is not reauth-recoverable" and the successful re-login is thrown away.
The backend still emits this code, always from the auth gate before the route handler runs:
src/backend/core/http/middleware/gates.ts:51src/backend/controllers/fs/LegacyFSController.ts:1252and:1260src/backend/controllers/auth/AuthController.ts:4496
Because the request never reached the handler, replaying it is free of side effects, and both replay paths are already one-shot (ctx.done.add('reauth') in classifyRetry, spec._replayed in replayXhr), so a fix cannot loop.
Suggested fix
Return { action: 'replay' } once authenticateWithPuter() resolves, mirroring the reauth_required branch:
if (resp?.code === 'token_auth_failed' && puter.env === 'web') {
if (!interactive) return resolveBackgroundReauth(resp, sentToken);
try {
puter.resetAuthToken();
await puter.ui.authenticateWithPuter();
return { action: 'replay' };
} catch (e) {
// ...unchanged auth_canceled rejection
}
}Acceptance criteria
- A browser call that receives
401 { code: 'token_auth_failed' }, followed by a successful sign-in, is replayed once with the fresh token and resolves on the retry. - The replay happens at most once — a
token_auth_failedthat persists after the replay still surfaces asUnauthorized. - The non-interactive path (
interactiveReauth: false, background/boot requests) is unchanged: the stale token is dropped, no UI is raised. - Dismissing the sign-in prompt still rejects with
auth_canceled; no other error shape changes. - Regression coverage added to
src/puter-js/src/lib/networkUtils.test.js.
Source: HeyPuter/puter