Skip to content

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

fix(auth): keep the logout guard until the core drops the token

M3gA-Mind2b880ffaf32a
diff --git a/app/src/providers/CoreStateProvider.tsx b/app/src/providers/CoreStateProvider.tsx
index 2126ab52c6..183f60b30e 100644
--- a/app/src/providers/CoreStateProvider.tsx
+++ b/app/src/providers/CoreStateProvider.tsx
@@ -275,9 +275,6 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
// token, not a time window, so a failed logout never silently signs the
// user back in, while a genuinely new login (a different token) gets through.
const loggingOutTokenRef = useRef<string | null>(null);
- // A failed `auth_logout` is retried once, on the next poll that still sees
- // the token.
- const logoutRetryPendingRef = useRef(false);
const bootstrapFailCountRef = useRef(0);
const refreshInFlightRef = useRef<Promise<void> | null>(null);
const isMountedRef = useRef(true);
@@ -320,19 +317,12 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
maybeSurfaceConfigRecovery(rawSnapshot.configRecovered, t);
if (!snapshot.sessionToken) {
loggingOutTokenRef.current = null;
- logoutRetryPendingRef.current = false;
}
// Capture pre-commit identity outside the setState updater so flip
// detection runs synchronously regardless of React's batching policy.
const beforeCommit = getCoreStateSnapshot().snapshot;
const shouldIgnoreTokenDuringLogout =
loggingOutTokenRef.current !== null && snapshot.sessionToken === loggingOutTokenRef.current;
- if (shouldIgnoreTokenDuringLogout && logoutRetryPendingRef.current) {
- logoutRetryPendingRef.current = false;
- void tauriLogout().catch(err => {
- log('tauriLogout retry failed: %O', sanitizeError(err));
- });
- }
const nextSnapshot = shouldIgnoreTokenDuringLogout ? toSignedOutSnapshot(snapshot) : snapshot;
const previousIdentity = snapshotIdentity(beforeCommit);
const nextIdentity = snapshotIdentity(nextSnapshot);
@@ -619,7 +609,6 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
}
snapshotRequestIdRef.current += 1;
- loggingOutTokenRef.current = null;
void refresh().catch(err => {
log('refresh failed after deep-link session update: %O', sanitizeError(err));
@@ -695,10 +684,12 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
const storeSessionToken = useCallback(
async (token: string, user?: object) => {
- loggingOutTokenRef.current = null;
sessionTokenBeingStoredRef.current = token;
try {
await storeSession(token, user ?? {});
+ // Only an explicit, successful store of the guarded token itself lifts
+ // the logout guard; any other token already passes it.
+ if (loggingOutTokenRef.current === token) loggingOutTokenRef.current = null;
// refresh() drives refreshCore, which now owns identity-flip detection
// and dispatches handleIdentityFlip when both prev and next are
// authenticated and identities differ. The previous standalone
@@ -764,8 +755,10 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
}, []);
const clearSession = useCallback(async () => {
- loggingOutTokenRef.current = getCoreStateSnapshot().snapshot.sessionToken ?? null;
- logoutRetryPendingRef.current = false;
+ // A repeated clear after a failed logout sees a null visible token while
+ // the core may still hold the guarded one: keep guarding it.
+ loggingOutTokenRef.current =
+ getCoreStateSnapshot().snapshot.sessionToken ?? loggingOutTokenRef.current;
snapshotRequestIdRef.current += 1;
commitState(previous => ({
...previous,
@@ -790,7 +783,6 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
await tauriLogout().catch(err => {
log('tauriLogout failed in clearSession — proceeding with refresh: %O', sanitizeError(err));
logoutError = err ?? new Error('logout failed');
- logoutRetryPendingRef.current = true;
});
await refresh().catch(err => {
log('refresh failed after clearSession: %O', sanitizeError(err));
diff --git a/app/src/providers/__tests__/CoreStateProvider.test.tsx b/app/src/providers/__tests__/CoreStateProvider.test.tsx
index 4a1286e340..9a95dee2d0 100644
--- a/app/src/providers/__tests__/CoreStateProvider.test.tsx
+++ b/app/src/providers/__tests__/CoreStateProvider.test.tsx
@@ -1217,13 +1217,15 @@ describe('session expiry fixes (#5868)', () => {
});
describe('failed logout keeps the user signed out', () => {
+ const tok1 = makeJwt({ sub: 'u1', exp: Math.floor(Date.now() / 1000) + 3600 });
+
async function renderSignedIn() {
resetCoreStateStore();
setActiveUserId(null);
vi.mocked(coreStateApi.fetchCoreAppSnapshot).mockReset();
// The core keeps reporting tok1 because the logout never removed it.
vi.mocked(coreStateApi.fetchCoreAppSnapshot).mockResolvedValue(
- makeSnapshot({ userId: 'u1', sessionToken: 'tok1' })
+ makeSnapshot({ userId: 'u1', sessionToken: tok1 })
);
vi.mocked(coreStateApi.listTeams).mockResolvedValue([]);
vi.mocked(tauriCommands.logout).mockReset();
@@ -1234,45 +1236,60 @@ describe('failed logout keeps the user signed out', () => {
<Consumer captureCtx={c => (ctx = c)} />
</CoreStateProvider>
);
- await waitFor(() => expect(screen.getByTestId('token').textContent).toBe('tok1'));
+ await waitFor(() => expect(screen.getByTestId('token').textContent).toBe(tok1));
+ await act(async () => {
+ await expect(ctx!.clearSession()).rejects.toThrow('auth_logout timed out');
+ });
+ expect(screen.getByTestId('token').textContent).toBe('none');
return () => ctx!;
}
- it('reports the failure, retries once, and never shows the logged-out token again', async () => {
+ it('reports the failure and never shows the logged-out token again', async () => {
const ctx = await renderSignedIn();
- await act(async () => {
- await expect(ctx().clearSession()).rejects.toThrow('auth_logout timed out');
- });
- expect(screen.getByTestId('token').textContent).toBe('none');
-
- // Polls long after any time window: the logged-out token stays hidden,
- // and the failed logout is retried exactly once.
+ // Polls long after any time window: the logged-out token stays hidden.
const realNow = Date.now;
const spy = vi.spyOn(Date, 'now').mockImplementation(() => realNow() + 60_000);
try {
await act(async () => {
await ctx().refresh();
});
- await act(async () => {
- await ctx().refresh();
- });
} finally {
spy.mockRestore();
}
expect(screen.getByTestId('token').textContent).toBe('none');
- expect(vi.mocked(tauriCommands.logout)).toHaveBeenCalledTimes(2);
+ // No automatic retry: an unkeyed logout could clear a newer session.
+ expect(vi.mocked(tauriCommands.logout)).toHaveBeenCalledTimes(1);
});
- it('lets a new login with a different token through', async () => {
+ it('keeps guarding the token when the clear is repeated', async () => {
const ctx = await renderSignedIn();
await act(async () => {
await ctx()
.clearSession()
.catch(() => undefined);
});
+ await act(async () => {
+ await ctx().refresh();
+ });
+ expect(screen.getByTestId('token').textContent).toBe('none');
+ });
+
+ it('a deep-link event carrying the logged-out token does not lift the guard', async () => {
+ const ctx = await renderSignedIn();
+ await act(async () => {
+ window.dispatchEvent(
+ new CustomEvent('core-state:session-token-updated', { detail: { sessionToken: tok1 } })
+ );
+ });
+ await act(async () => {
+ await ctx().refresh();
+ });
expect(screen.getByTestId('token').textContent).toBe('none');
+ });
+ it('lets a new login with a different token through', async () => {
+ const ctx = await renderSignedIn();
vi.mocked(coreStateApi.fetchCoreAppSnapshot).mockResolvedValue(
makeSnapshot({ userId: 'u1', sessionToken: 'tok2' })
);