Skip to content

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

fix(auth): corroborate a chat-error session expiry before destroying the profile

M3gA-Mind0e52f17fb61b
diff --git a/app/src/providers/ChatRuntimeProvider.tsx b/app/src/providers/ChatRuntimeProvider.tsx
index 06e8cac456..e9358eb1fe 100644
--- a/app/src/providers/ChatRuntimeProvider.tsx
+++ b/app/src/providers/ChatRuntimeProvider.tsx
@@ -1290,8 +1290,22 @@ const ChatRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
// debounce in runReauth() ensures a concurrent socket event does not
// cause a double-clear.
if (event.error_type === 'session_expired') {
+ // `reason: 'unconfirmed'` is load-bearing, not defensive. The core
+ // classifies this error with `is_session_expired_message`, which
+ // matches the LOCAL guards "no backend session token" and
+ // "session jwt required" as well as a real backend expiry
+ // (`core/observability.rs`). Those two fire transiently before the
+ // on-disk auth profile has been read — #2758 is the bug where
+ // treating them as definitive forced a re-login even though the
+ // token had survived the restart. `unconfirmed` routes through
+ // `confirmSessionTokenGone()` in `runReauth`, so a signal raised
+ // while the token is still on disk stops short of the destructive
+ // `clearSession()`. A genuine expiry still corroborates and signs
+ // out; only the false positive is filtered.
window.dispatchEvent(
- new CustomEvent('openhuman:session-expired', { detail: { source: 'chat-error' } })
+ new CustomEvent('openhuman:session-expired', {
+ detail: { source: 'chat-error', reason: 'unconfirmed' },
+ })
);
}
}
diff --git a/app/src/providers/CoreStateProvider.tsx b/app/src/providers/CoreStateProvider.tsx
index abfcbe3f7d..520b06ebdf 100644
--- a/app/src/providers/CoreStateProvider.tsx
+++ b/app/src/providers/CoreStateProvider.tsx
@@ -785,7 +785,13 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
pendingConfirmedReauthRef.current = false;
log('auth-expired: replaying confirmed reauth suppressed during bootstrap');
window.dispatchEvent(
- new CustomEvent('openhuman:session-expired', { detail: { source: 'bootstrap-replay' } })
+ // Explicit rather than relying on the default: what is being replayed
+ // was already established as a confirmed expiry before it was queued,
+ // and saying so here keeps the replay correct if the default above is
+ // ever made stricter.
+ new CustomEvent('openhuman:session-expired', {
+ detail: { source: 'bootstrap-replay', reason: 'confirmed' },
+ })
);
}
}, [state.isBootstrapping]);
@@ -920,8 +926,22 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
typeof (event.detail as { source?: unknown }).source === 'string'
? (event.detail as { source: string }).source
: 'unknown';
- // The socket `auth:session_expired` push is an explicit backend expiry → confirmed.
- void runReauth('socket.session_expired', source, 'confirmed');
+ // The socket `auth:session_expired` push is an explicit backend expiry, and
+ // `socketService` sends no `reason` — so the default stays `confirmed`
+ // and that path is unchanged. A dispatcher that KNOWS its signal is
+ // merely suggestive says so, and is corroborated instead of trusted:
+ // `ChatRuntimeProvider`'s chat-error dispatch sends `unconfirmed`,
+ // because the core classifies local token-absent guards under the same
+ // `session_expired` error type as a real expiry.
+ const reason: AuthExpiredReason =
+ event instanceof CustomEvent &&
+ event.detail &&
+ typeof event.detail === 'object' &&
+ 'reason' in event.detail &&
+ (event.detail as { reason?: unknown }).reason === 'unconfirmed'
+ ? 'unconfirmed'
+ : 'confirmed';
+ void runReauth('socket.session_expired', source, reason);
};
window.addEventListener('core-rpc-auth-expired', onRpcExpired as EventListener);
diff --git a/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx b/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx
index ad307e2107..3893b10d8e 100644
--- a/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx
+++ b/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx
@@ -2070,6 +2070,16 @@ describe('ChatRuntimeProvider — skill tool-chain latency (#4273 AC3)', () => {
window.removeEventListener('openhuman:session-expired', handler);
expect(received).toHaveLength(1);
expect((received[0] as CustomEvent).detail?.source).toBe('chat-error');
+ // The reason is the load-bearing half, not decoration. `CoreStateProvider`
+ // defaults a reason-less `openhuman:session-expired` to `confirmed`, which
+ // SKIPS `confirmSessionTokenGone()` and runs the destructive
+ // `clearSession()`. The core reaches this error type through
+ // `is_session_expired_message`, which also matches the local guards
+ // "no backend session token" and "session jwt required" — the transient
+ // pre-profile-load signals #2758 exists to corroborate rather than trust.
+ // Dropping this field silently reintroduces that bug through a new door,
+ // so it is asserted here, at the dispatch, where the value is decided.
+ expect((received[0] as CustomEvent).detail?.reason).toBe('unconfirmed');
});
it('does not dispatch openhuman:session-expired for non-session error types', () => {
diff --git a/app/src/providers/__tests__/CoreStateProvider.test.tsx b/app/src/providers/__tests__/CoreStateProvider.test.tsx
index e97492cc76..786560acfa 100644
--- a/app/src/providers/__tests__/CoreStateProvider.test.tsx
+++ b/app/src/providers/__tests__/CoreStateProvider.test.tsx
@@ -1047,6 +1047,85 @@ describe('session expiry fixes (#5868)', () => {
// The pending reauth must replay and call logout.
await waitFor(() => expect(vi.mocked(tauriCommands.logout)).toHaveBeenCalledTimes(1));
});
+
+ it('does not sign out on a chat-error expiry while the session token is still on disk (#2758)', async () => {
+ // The failure mode this pins is a REGRESSION OF #2758, reached through a
+ // new door. `is_session_expired_message` (core/observability.rs) classifies
+ // the local guards "no backend session token" and "session jwt required"
+ // under the same `session_expired` error type as a real backend expiry, and
+ // those fire transiently before the on-disk auth profile has been read.
+ //
+ // `clearSession()` is destructive — `auth_clear_session` deletes the auth
+ // profile — so a merely-suggestive signal must be corroborated first. That
+ // is what `reason: 'unconfirmed'` buys: `runReauth` calls
+ // `confirmSessionTokenGone()` and stops when the token is still there.
+ //
+ // Revert the dispatch to `confirmed` (or drop the `reason` from the
+ // ChatRuntimeProvider dispatch) and this test fails: corroboration is
+ // skipped and the user is signed out with a perfectly good token on disk.
+ setCoreStateSnapshot({ ...getCoreStateSnapshot(), isBootstrapping: false, isReady: true });
+
+ vi.mocked(coreStateApi.fetchCoreAppSnapshot).mockResolvedValue(
+ makeSnapshot({ userId: 'u1', sessionToken: 'tok1' })
+ );
+ vi.mocked(tauriCommands.logout).mockReset();
+ vi.mocked(tauriCommands.logout).mockResolvedValue(undefined as never);
+ // The whole point: the token is STILL PRESENT. Corroboration must find it
+ // and abandon the sign-out.
+ vi.mocked(tauriCommands.getSessionToken).mockResolvedValue('tok1');
+
+ render(
+ <CoreStateProvider>
+ <Consumer />
+ </CoreStateProvider>
+ );
+ await waitFor(() => expect(screen.getByTestId('ready').textContent).toBe('ready'));
+
+ await act(async () => {
+ window.dispatchEvent(
+ new CustomEvent('openhuman:session-expired', {
+ detail: { source: 'chat-error', reason: 'unconfirmed' },
+ })
+ );
+ });
+
+ // Let the async corroboration settle before asserting the negative, so this
+ // cannot pass merely by checking too early.
+ await waitFor(() => expect(vi.mocked(tauriCommands.getSessionToken)).toHaveBeenCalled());
+ expect(vi.mocked(tauriCommands.logout)).not.toHaveBeenCalled();
+ });
+
+ it('still signs out on a chat-error expiry once the token is genuinely gone', async () => {
+ // The positive control for the test above: `unconfirmed` must not become a
+ // way to never sign out. Same dispatch, same path — only the corroboration
+ // result differs — so a fix that simply ignored chat-error expiries would
+ // pass the previous test and fail this one.
+ setCoreStateSnapshot({ ...getCoreStateSnapshot(), isBootstrapping: false, isReady: true });
+
+ vi.mocked(coreStateApi.fetchCoreAppSnapshot).mockResolvedValue(
+ makeSnapshot({ userId: 'u1', sessionToken: 'tok1' })
+ );
+ vi.mocked(tauriCommands.logout).mockReset();
+ vi.mocked(tauriCommands.logout).mockResolvedValue(undefined as never);
+ vi.mocked(tauriCommands.getSessionToken).mockResolvedValue(null);
+
+ render(
+ <CoreStateProvider>
+ <Consumer />
+ </CoreStateProvider>
+ );
+ await waitFor(() => expect(screen.getByTestId('ready').textContent).toBe('ready'));
+
+ await act(async () => {
+ window.dispatchEvent(
+ new CustomEvent('openhuman:session-expired', {
+ detail: { source: 'chat-error', reason: 'unconfirmed' },
+ })
+ );
+ });
+
+ await waitFor(() => expect(vi.mocked(tauriCommands.logout)).toHaveBeenCalledTimes(1));
+ });
});
describe('coreStatePollFailureDebugMessage', () => {