Skip to content

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

fix(auth): a failed logout no longer signs the user back in

M3gA-Mind83c21739ffc1
diff --git a/app/src/components/composio/TriggerToggles.test.tsx b/app/src/components/composio/TriggerToggles.test.tsx
index f72957a85c..bdb846f2f9 100644
--- a/app/src/components/composio/TriggerToggles.test.tsx
+++ b/app/src/components/composio/TriggerToggles.test.tsx
@@ -27,7 +27,7 @@ beforeEach(() => {
mockListTriggers.mockReset();
mockEnable.mockReset();
mockDisable.mockReset();
- mockClearSession.mockReset();
+ mockClearSession.mockReset().mockResolvedValue(undefined);
});
describe('triggerSignature / activeTriggerSignature', () => {
diff --git a/app/src/components/composio/TriggerToggles.tsx b/app/src/components/composio/TriggerToggles.tsx
index 93e2e68dd4..e54d054749 100644
--- a/app/src/components/composio/TriggerToggles.tsx
+++ b/app/src/components/composio/TriggerToggles.tsx
@@ -176,7 +176,7 @@ export default function TriggerToggles({
variant="secondary"
size="xs"
className="shrink-0"
- onClick={() => void clearSession()}>
+ onClick={() => void clearSession().catch(() => undefined)}>
{t('settings.embeddings.signInAgain')}
</Button>
</div>
diff --git a/app/src/components/settings/panels/EmbeddingsPanel.tsx b/app/src/components/settings/panels/EmbeddingsPanel.tsx
index 8bb5bcd33f..6fd7041e2f 100644
--- a/app/src/components/settings/panels/EmbeddingsPanel.tsx
+++ b/app/src/components/settings/panels/EmbeddingsPanel.tsx
@@ -471,7 +471,7 @@ const EmbeddingsPanel = ({ embedded = false }: EmbeddingsPanelProps = {}) => {
variant="secondary"
size="xs"
className="shrink-0"
- onClick={() => void clearSession()}>
+ onClick={() => void clearSession().catch(() => undefined)}>
{isLocalSession
? t('settings.exitLocalSession')
: t('settings.embeddings.signInAgain')}
diff --git a/app/src/providers/CoreStateProvider.tsx b/app/src/providers/CoreStateProvider.tsx
index 4870616095..2126ab52c6 100644
--- a/app/src/providers/CoreStateProvider.tsx
+++ b/app/src/providers/CoreStateProvider.tsx
@@ -270,7 +270,14 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
const [state, setState] = useState<CoreState>(() => getCoreStateSnapshot());
const snapshotRequestIdRef = useRef(0);
const teamsRequestIdRef = useRef(0);
- const logoutGuardUntilRef = useRef(0);
+ // The session token a logout is removing. Until the core stops reporting
+ // it, a snapshot carrying that token is shown signed out — keyed on the
+ // 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);
@@ -312,15 +319,20 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
// the next live poll still surfaces it. Guarded internally against re-fire.
maybeSurfaceConfigRecovery(rawSnapshot.configRecovered, t);
if (!snapshot.sessionToken) {
- logoutGuardUntilRef.current = 0;
+ 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 =
- Date.now() < logoutGuardUntilRef.current &&
- !beforeCommit.sessionToken &&
- Boolean(snapshot.sessionToken);
+ 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);
@@ -607,7 +619,7 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
}
snapshotRequestIdRef.current += 1;
- logoutGuardUntilRef.current = 0;
+ loggingOutTokenRef.current = null;
void refresh().catch(err => {
log('refresh failed after deep-link session update: %O', sanitizeError(err));
@@ -683,7 +695,7 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
const storeSessionToken = useCallback(
async (token: string, user?: object) => {
- logoutGuardUntilRef.current = 0;
+ loggingOutTokenRef.current = null;
sessionTokenBeingStoredRef.current = token;
try {
await storeSession(token, user ?? {});
@@ -752,7 +764,8 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
}, []);
const clearSession = useCallback(async () => {
- logoutGuardUntilRef.current = Date.now() + 5_000;
+ loggingOutTokenRef.current = getCoreStateSnapshot().snapshot.sessionToken ?? null;
+ logoutRetryPendingRef.current = false;
snapshotRequestIdRef.current += 1;
commitState(previous => ({
...previous,
@@ -771,13 +784,18 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
// Wrap tauriLogout separately so a failure (e.g. the Rust-side
// SessionExpiredSubscriber already cleared the session first) does not
// prevent the subsequent refresh() — which is what actually flips the
- // router to the login screen.
+ // router to the login screen. The failure is still rethrown afterwards so
+ // the caller can tell the user the logout did not complete.
+ let logoutError: unknown = null;
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));
});
+ if (logoutError) throw logoutError;
}, [commitState, refresh]);
// When a confirmed session-expiry event arrives while the core is still
diff --git a/app/src/providers/__tests__/CoreStateProvider.test.tsx b/app/src/providers/__tests__/CoreStateProvider.test.tsx
index 571c552163..4a1286e340 100644
--- a/app/src/providers/__tests__/CoreStateProvider.test.tsx
+++ b/app/src/providers/__tests__/CoreStateProvider.test.tsx
@@ -1216,6 +1216,73 @@ describe('session expiry fixes (#5868)', () => {
});
});
+describe('failed logout keeps the user signed out', () => {
+ 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' })
+ );
+ vi.mocked(coreStateApi.listTeams).mockResolvedValue([]);
+ vi.mocked(tauriCommands.logout).mockReset();
+ vi.mocked(tauriCommands.logout).mockRejectedValue(new Error('auth_logout timed out'));
+ let ctx: CoreStateContextValue | undefined;
+ render(
+ <CoreStateProvider>
+ <Consumer captureCtx={c => (ctx = c)} />
+ </CoreStateProvider>
+ );
+ await waitFor(() => expect(screen.getByTestId('token').textContent).toBe('tok1'));
+ return () => ctx!;
+ }
+
+ it('reports the failure, retries once, 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.
+ 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);
+ });
+
+ it('lets a new login with a different token through', async () => {
+ const ctx = await renderSignedIn();
+ await act(async () => {
+ await ctx()
+ .clearSession()
+ .catch(() => undefined);
+ });
+ expect(screen.getByTestId('token').textContent).toBe('none');
+
+ vi.mocked(coreStateApi.fetchCoreAppSnapshot).mockResolvedValue(
+ makeSnapshot({ userId: 'u1', sessionToken: 'tok2' })
+ );
+ await act(async () => {
+ await ctx().refresh();
+ });
+ expect(screen.getByTestId('token').textContent).toBe('tok2');
+ });
+});
+
describe('coreStatePollFailureDebugMessage', () => {
it('describes post-bootstrap poll failures without impossible retry counters', () => {
expect(coreStatePollFailureDebugMessage(0)).toBeNull();