Skip to content

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

fix(core): prevent duplicate session event listeners on provider remount

Steven Enamakelb0755f83f16f
diff --git a/app/src/providers/CoreStateProvider.tsx b/app/src/providers/CoreStateProvider.tsx
index 921f4abebe..dd656fff80 100644
--- a/app/src/providers/CoreStateProvider.tsx
+++ b/app/src/providers/CoreStateProvider.tsx
@@ -27,6 +27,7 @@ import {
updateCoreLocalState,
} from '../services/coreStateApi';
import { daemonHealthService } from '../services/daemonHealthService';
+import { installShellSessionEventBridge } from '../services/session/shellSessionEvents';
import { socketService } from '../services/socketService';
import { store } from '../store';
import { loadAgentProfiles } from '../store/agentProfileSlice';
@@ -79,7 +80,7 @@ function sanitizeError(error: unknown): { message?: string; code?: string; statu
/**
* Positively confirm the on-disk session token is gone before an `auth_expired`
* signal is allowed to trigger the *destructive* `clearSession` (which calls
- * `auth_clear_session` → removes the auth profile from disk).
+ * `auth_logout` → the session owner removes the credential from the core).
*
* Reads the cheap disk-only `auth_get_session_token` RPC — no `auth/me` network
* call, not subject to `app_state_snapshot`'s 5s/10s timeouts. Right after the
@@ -784,7 +785,7 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
const pendingConfirmedReauthRef = useRef(false);
// Listen for deep-link auth suppression signals so that an in-flight
- // `auth_store_session` call (OAuth deep link) does not race with the
+ // session-owner store call (OAuth deep link) does not race with the
// `core-rpc-auth-expired` handler and clear the session mid-delivery.
// See issue #2377.
useEffect(() => {
@@ -931,7 +932,7 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
// An `unconfirmed` reason ("session jwt required" / "no backend session
// token") means the core has no token *loaded* — which fires transiently
// right after the identity-flip restart, before the on-disk auth profile
- // is read. `clearSession()` is destructive (auth_clear_session removes the
+ // is read. `clearSession()` is destructive (the session owner removes the
// profile from disk), so corroborate first and only sign out if the token
// is genuinely gone. A hard 401 / explicit expiry (`confirmed`) skips this.
if (reason === 'unconfirmed') {
@@ -1008,11 +1009,22 @@ export default function CoreStateProvider({ children }: { children: ReactNode })
window.addEventListener('core-rpc-auth-expired', onRpcExpired as EventListener);
window.addEventListener('openhuman:session-expired', onSocketExpired as EventListener);
+ // 3. The Tauri shell's session owner: `auth://expired` re-enters the
+ // same guarded path as the socket push; `auth://changed` refreshes
+ // the snapshot right away instead of waiting for the next poll.
+ const disposeShellBridge = installShellSessionEventBridge({
+ onChanged: () => {
+ void refresh().catch(err => {
+ log('refresh failed after shell auth change: %O', sanitizeError(err));
+ });
+ },
+ });
return () => {
window.removeEventListener('core-rpc-auth-expired', onRpcExpired as EventListener);
window.removeEventListener('openhuman:session-expired', onSocketExpired as EventListener);
+ disposeShellBridge();
};
- }, [clearSession]);
+ }, [clearSession, refresh]);
const patchSnapshot = useCallback(
(patch: Partial<CoreAppSnapshot>) => {
diff --git a/app/src/services/session/shellSessionEvents.ts b/app/src/services/session/shellSessionEvents.ts
new file mode 100644
index 0000000000..f3542caa24
--- /dev/null
+++ b/app/src/services/session/shellSessionEvents.ts
@@ -0,0 +1,54 @@
+/**
+ * Bridge the Tauri shell's session-owner events into the window events the
+ * core-state layer already reacts to.
+ *
+ * - `auth://expired` (the backend rejected the stored credential and the shell
+ * cleared it) → `openhuman:session-expired` with a `confirmed` reason, the
+ * same path the Socket.IO `auth:session_expired` push takes.
+ * - `auth://changed` (credential or current user changed) → `onChanged`, so
+ * the provider refreshes without waiting for the next poll.
+ */
+import { listen, type UnlistenFn } from '@tauri-apps/api/event';
+
+import { useShellSessionOwner } from './sessionOwner';
+
+export const SHELL_AUTH_CHANGED_EVENT = 'auth://changed';
+export const SHELL_AUTH_EXPIRED_EVENT = 'auth://expired';
+
+export const installShellSessionEventBridge = (handlers: {
+ onChanged: () => void;
+}): (() => void) => {
+ if (!useShellSessionOwner()) {
+ return () => undefined;
+ }
+ let disposed = false;
+ const unlisteners: UnlistenFn[] = [];
+ const track = (promise: Promise<UnlistenFn>) => {
+ promise
+ .then(unlisten => {
+ if (disposed) unlisten();
+ else unlisteners.push(unlisten);
+ })
+ .catch(error => {
+ console.debug('[session] shell event listener unavailable:', error);
+ });
+ };
+ track(
+ listen<{ source?: string }>(SHELL_AUTH_EXPIRED_EVENT, event => {
+ window.dispatchEvent(
+ new CustomEvent('openhuman:session-expired', {
+ detail: { source: `shell.${event.payload?.source ?? 'unknown'}`, reason: 'confirmed' },
+ })
+ );
+ })
+ );
+ track(
+ listen(SHELL_AUTH_CHANGED_EVENT, () => {
+ handlers.onChanged();
+ })
+ );
+ return () => {
+ disposed = true;
+ for (const unlisten of unlisteners.splice(0)) unlisten();
+ };
+};