Skip to content

The agent that grows with you

fix(desktop): a profile switch no longer keeps the previous profile's Sign in (review)

alt-glitchee320cb6c188
diff --git a/apps/desktop/src/app/contrib/controller.tsx b/apps/desktop/src/app/contrib/controller.tsx
index 9e204d62e2e..1ac6c520487 100644
--- a/apps/desktop/src/app/contrib/controller.tsx
+++ b/apps/desktop/src/app/contrib/controller.tsx
@@ -59,7 +59,6 @@ import { TRANSCRIPT_DIRECTIVE_AREA, type TranscriptDirectiveContribution } from
import { setYoloEnabled } from '@/lib/yolo-session'
import { $connectionsRegistry } from '@/store/connection-registry-state'
import { watchDeadSessionPrune } from '@/store/dead-session-prune'
-import { $freeTierSignInOpen } from '@/store/free-tier'
import { $interfaceMode, $showsAdvancedChrome, setModeContext, toggleSimpleMode } from '@/store/interface-mode'
import {
$fileBrowserOpen,
@@ -634,8 +633,6 @@ bindToolPaneCollapse(
// Without the statusbar, the rail is the only way to switch profiles or gateways.
$profiles.subscribe(profiles => setModeContext({ profileCount: profiles.length }))
$connectionsRegistry.subscribe(registry => setModeContext({ connectionCount: registry?.connections.length ?? 0 }))
-// Without the statusbar, a signed-out free-tier user would have no standing Sign in.
-$freeTierSignInOpen.subscribe(open => setModeContext({ freeTierSignInOpen: open }))
// ⌘K door onto the same pane the keybind and statusbar pill flip.
registry.register(terminalPaletteToggle)
diff --git a/apps/desktop/src/app/shell/hooks/use-status-snapshot.test.ts b/apps/desktop/src/app/shell/hooks/use-status-snapshot.test.ts
index 2284cfdba76..2ab2c5afc79 100644
--- a/apps/desktop/src/app/shell/hooks/use-status-snapshot.test.ts
+++ b/apps/desktop/src/app/shell/hooks/use-status-snapshot.test.ts
@@ -6,6 +6,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { NotificationStack } from '@/components/notifications'
import { getStatus } from '@/hermes'
import { I18nProvider, type Locale, TRANSLATIONS, type Translations } from '@/i18n'
+import { $freeTierStatus } from '@/store/free-tier'
import { $setupReadyTick, notifySetupReady } from '@/store/live-sync'
import { clearNotifications } from '@/store/notifications'
import { $desktopOnboarding } from '@/store/onboarding'
@@ -112,6 +113,27 @@ describe('useStatusSnapshot', () => {
}
)
+ it('drops the free-tier verdict on a profile or source switch, not on a gateway flap', async () => {
+ const requestGateway = vi.fn().mockRejectedValue(new Error('offline')) as unknown as GatewayRequester
+ const signedOut = { available: true, enabled: true, has_guest: true, label: '', model: '', notice_pending: false }
+
+ const { rerender } = renderHook(
+ ({ gatewayState, scope }) => useStatusSnapshot(gatewayState, requestGateway, scope),
+ { initialProps: { gatewayState: 'open', scope: 'local\0work' } }
+ )
+
+ await flushAsync()
+ $freeTierStatus.set(signedOut)
+ rerender({ gatewayState: 'connecting', scope: 'local\0work' })
+ rerender({ gatewayState: 'open', scope: 'local\0work' })
+ await flushAsync()
+ expect($freeTierStatus.get()).toEqual(signedOut)
+
+ rerender({ gatewayState: 'open', scope: 'local\0home' })
+ await flushAsync()
+ expect($freeTierStatus.get()).toBeNull()
+ })
+
it('pauses status RPCs while visible but unfocused, then catches up on focus', async () => {
vi.mocked(document.hasFocus).mockReturnValue(false)
const requestGateway = vi.fn().mockResolvedValue({}) as unknown as GatewayRequester
diff --git a/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts b/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts
index a5c62ba888e..241c034d825 100644
--- a/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts
+++ b/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts
@@ -3,7 +3,7 @@ import { useEffect, useState } from 'react'
import { getStatus } from '@/hermes'
import { type I18nContextValue, useI18n } from '@/i18n'
import { evaluateRuntimeReadiness, type RuntimeReadinessResult } from '@/lib/runtime-readiness'
-import { refreshFreeTierStatus, setFreeTierRoute } from '@/store/free-tier'
+import { $freeTierStatus, refreshFreeTierStatus, setFreeTierRoute } from '@/store/free-tier'
import { $setupReadyTick } from '@/store/live-sync'
import { dismissNotification, notify } from '@/store/notifications'
import { $desktopOnboarding } from '@/store/onboarding'
@@ -14,6 +14,10 @@ import type { StatusResponse } from '@/types/hermes'
// visibility listeners refresh immediately on return.
const REFRESH_MS = 60_000
+// The scope the cached free-tier verdict was read under. Module-level because
+// $freeTierStatus is one app-wide atom, not per hook instance.
+let freeTierScope: string | undefined
+
type GatewayRequester = <T = unknown>(method: string, params?: Record<string, unknown>) => Promise<T>
export function useStatusSnapshot(
@@ -48,6 +52,14 @@ export function useStatusSnapshot(
setStatusSnapshot(null)
publishInferenceStatus(null)
+ // The free-tier verdict belongs to one profile too. Drop it on a real switch
+ // only: a flap on the same scope keeps the last answer (refreshFreeTierStatus).
+ if (freeTierScope !== undefined && freeTierScope !== gatewayScope) {
+ $freeTierStatus.set(null)
+ }
+
+ freeTierScope = gatewayScope
+
// A closed/connecting gateway cannot have an authoritative live-runtime
// result. Clear readiness before starting the REST status leg so a hung
// getStatus() cannot leave a stale "ready" state visible after disconnect.
diff --git a/apps/desktop/src/app/shell/hooks/use-statusbar-items.test.tsx b/apps/desktop/src/app/shell/hooks/use-statusbar-items.test.tsx
index d565134d7f7..3aa6beede2d 100644
--- a/apps/desktop/src/app/shell/hooks/use-statusbar-items.test.tsx
+++ b/apps/desktop/src/app/shell/hooks/use-statusbar-items.test.tsx
@@ -2,13 +2,13 @@ import { renderHook } from '@testing-library/react'
import type { WritableAtom } from 'nanostores'
import { isValidElement, type ReactNode } from 'react'
import { MemoryRouter } from 'react-router'
-import { afterAll, afterEach, describe, expect, it, vi } from 'vitest'
+import { afterEach, describe, expect, it, vi } from 'vitest'
import { group } from '@/components/pane-shell/tree/model'
import { $layoutTree, noteActiveTreeGroup } from '@/components/pane-shell/tree/store'
import { createClientSessionState } from '@/lib/chat-runtime'
-import { $freeTierSignInOpen, $freeTierStatus } from '@/store/free-tier'
-import { INTERFACE_MODES, setInterfaceMode, setModeContext } from '@/store/interface-mode'
+import { $freeTierStatus } from '@/store/free-tier'
+import { INTERFACE_MODES, setInterfaceMode } from '@/store/interface-mode'
import { $onboardingGate } from '@/store/onboarding-gate'
import {
$connection,
@@ -287,8 +287,6 @@ describe('free-tier Sign in in every interface mode', () => {
})
const desktopBridge = window.hermesDesktop
- // The shell feeds the mode context from the same atom (app/contrib/controller.tsx).
- const unwire = $freeTierSignInOpen.subscribe(open => setModeContext({ freeTierSignInOpen: open }))
function signInChipVisible(): boolean {
const { leftStatusbarItems, statusbarItems } = renderStatusbarItems()
@@ -304,8 +302,6 @@ describe('free-tier Sign in in every interface mode', () => {
setInterfaceMode('advanced')
})
- afterAll(() => unwire())
-
it.each(INTERFACE_MODES)('shows the Sign in chip to a signed-out free-tier user in %s mode', mode => {
setInterfaceMode(mode)
$freeTierStatus.set(freeTier(true))
diff --git a/apps/desktop/src/store/free-tier.ts b/apps/desktop/src/store/free-tier.ts
index 7ea8d7820f8..2256046a814 100644
--- a/apps/desktop/src/store/free-tier.ts
+++ b/apps/desktop/src/store/free-tier.ts
@@ -1,6 +1,7 @@
import { atom, computed } from 'nanostores'
import { runFreeTierChallenge } from '@/store/free-tier-challenge'
+import { setModeContext } from '@/store/interface-mode'
import { $onboardingGate, guidedOnboardingActive } from '@/store/onboarding-gate'
import { onboardingSurfaceActive } from '@/store/onboarding-presence'
import type { FreeTierStatus } from '@/types/hermes'
@@ -35,6 +36,9 @@ export const $freeTierSignInOpen = computed(
status => Boolean(status?.available) && !guidedOnboardingActive()
)
+// Fed here, beside the atom, so every importer of this store gets the same link.
+$freeTierSignInOpen.subscribe(open => setModeContext({ freeTierSignInOpen: open }))
+
function isFreeTierStatus(value: unknown): value is FreeTierStatus {
return typeof value === 'object' && value !== null && typeof (value as FreeTierStatus).has_guest === 'boolean'
}