Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(auth): do not clear the local-token marker on a pre-store snapshot\n\n`storeSessionToken` awaited `refresh()`, which dedupes onto any poll already in\nflight. A poll that began before `storeSession` resolved answers with the\npre-store cloud snapshot; that answer was committed, the `finally` dropped the\nlocal-token marker on it, and a late confirmed 401 could then clear the local\nsession that had just been stored.\n\nWait the in-flight poll out, then require a refresh that began after the store\ncommitted before the marker clears. The regression test drives exactly that\nordering and fails without the barrier (session token left at the stale cloud\nvalue).\n\nClaude-Session: https://claude.ai/code/session_012jp2WUNkZ8dxDVmDtu3JxX\n

Shanu535534685466
diff --git a/app/src/providers/CoreStateProvider.tsx b/app/src/providers/CoreStateProvider.tsx
index aabb4f2391..d534546f6f 100644
--- a/app/src/providers/CoreStateProvider.tsx
+++ b/app/src/providers/CoreStateProvider.tsx
@@ -707,6 +707,15 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
// timeout post-login doesn't surface as an unhandled rejection
// (OPENHUMAN-REACT-Z/Y) — the polling loop reconciles within
// `POLL_MS`.
+ // `refresh()` dedupes onto any poll already in flight. A poll that
+ // began before `storeSession` resolved answers with the pre-store
+ // snapshot; awaiting that one here would commit stale cloud identity,
+ // then the `finally` below would drop the local-token marker on it and
+ // a late confirmed 401 could clear the session just stored. Wait the
+ // stale poll out, then require a refresh that began after the store.
+ if (refreshInFlightRef.current) {
+ await refreshInFlightRef.current.catch(() => undefined);
+ }
await refresh().catch(err => {
log('refresh failed after session store: %O', sanitizeError(err));
});
diff --git a/app/src/providers/__tests__/CoreStateProvider.test.tsx b/app/src/providers/__tests__/CoreStateProvider.test.tsx
index 9204fb0162..d7b4ebf31b 100644
--- a/app/src/providers/__tests__/CoreStateProvider.test.tsx
+++ b/app/src/providers/__tests__/CoreStateProvider.test.tsx
@@ -578,6 +578,54 @@ describe('CoreStateProvider — identity-change cache clearing', () => {
await act(async () => storing);
});
+ it('keeps a stored local session when a pre-store poll settles after the store', async () => {
+ const localToken = `eyJhbGciOiJub25lIn0.${window.btoa(JSON.stringify({ sub: 'local' }))}.local`;
+ const prestorePoll = deferred<Snapshot>();
+ // Bootstrap answers with the cloud identity, the poll started *before* the
+ // store is held open, and every poll after the store sees the local session.
+ fetchSnapshot
+ .mockResolvedValueOnce(makeSnapshot({ userId: 'cloud-user', sessionToken: 'old' }))
+ .mockReturnValueOnce(prestorePoll.promise as never)
+ .mockResolvedValue(makeSnapshot({ userId: 'local', sessionToken: localToken }));
+ listTeams.mockResolvedValue([]);
+ vi.mocked(tauriCommands.storeSession).mockReset();
+ vi.mocked(tauriCommands.storeSession).mockResolvedValue(undefined as never);
+ vi.mocked(tauriCommands.logout).mockReset();
+ vi.mocked(tauriCommands.logout).mockResolvedValue(undefined as never);
+
+ let ctx: CoreStateContextValue | undefined;
+ render(
+ <CoreStateProvider>
+ <Consumer captureCtx={next => (ctx = next)} />
+ </CoreStateProvider>
+ );
+ await waitFor(() => expect(screen.getByTestId('ready').textContent).toBe('ready'));
+
+ let storing!: Promise<void>;
+ await act(async () => {
+ // A poll is already in flight when the local session is stored...
+ void ctx!.refresh();
+ storing = ctx!.storeSessionToken(localToken, { id: 'local' });
+ await new Promise(resolve => setTimeout(resolve, 0));
+ // ...and it settles with the stale cloud snapshot only after the store.
+ prestorePoll.resolve(makeSnapshot({ userId: 'cloud-user', sessionToken: 'old' }));
+ await storing;
+ });
+
+ expect(getCoreStateSnapshot().snapshot.sessionToken).toBe(localToken);
+
+ await act(async () => {
+ window.dispatchEvent(
+ new CustomEvent('core-rpc-auth-expired', {
+ detail: { method: 'openhuman.team_get_usage', source: 'rpc', reason: 'confirmed' },
+ })
+ );
+ await new Promise(resolve => setTimeout(resolve, 0));
+ });
+
+ expect(vi.mocked(tauriCommands.logout)).not.toHaveBeenCalled();
+ });
+
it('dispatching core-rpc-auth-expired triggers clearSession (and debounces repeated fires within 10s)', async () => {
fetchSnapshot.mockResolvedValue(makeSnapshot({ userId: 'u1', sessionToken: 'tok1' }));
listTeams.mockResolvedValue([]);