Skip to content

The agent that grows with you

fix(desktop): bound the paint-first hydration syncing badge

Tyler Lyone2084e8cd9c5
diff --git a/apps/desktop/src/sdk/index.ts b/apps/desktop/src/sdk/index.ts
index 6c3466c13d3..009c8d2c8d9 100644
--- a/apps/desktop/src/sdk/index.ts
+++ b/apps/desktop/src/sdk/index.ts
@@ -356,6 +356,10 @@ export const DEFAULT_SESSION_HYDRATION_TIMEOUT_MS = 20_000
* and paint durable history. Bot Mode opts into one retry, so its effective
* ceiling is two bounded attempts rather than an unbounded wait. */
export const BOT_CHAT_SESSION_HYDRATION_TIMEOUT_MS = 60_000
+/** Ceiling on the paint-first "Syncing…" badge. The profile gate it waits on is
+ * not guaranteed to ever fire (see beginHydrationBackgroundSync), so the badge
+ * needs a bound of its own or it outlives the wake it describes. */
+export const HYDRATION_SYNC_BADGE_TIMEOUT_MS = 30_000
let openSessionGeneration = 0
export interface PluginOpenSessionOptions {
@@ -395,18 +399,43 @@ export interface PluginNewChatOptions {
// down as soon as the active-profile gate catches up. The listener clears ONLY
// its own profile's badge: a newer wake may have replaced the badge with a
// different profile, and the stale listener must not wipe the winner's.
+//
+// The gate is not guaranteed to fire. `.listen()` is change-only, and the very
+// condition that routes a wake here — a shared-remote connection serving every
+// profile through the primary socket — is also the condition under which
+// $activeGatewayProfile NEVER becomes this profile (the reason the paint-first
+// bypass exists at all). On that path the listener is dead on arrival, so the
+// badge outlives the wake it describes and strands a permanent
+// "Syncing <profile>…" spinner with no timeout and no user-reachable
+// dismissal; only a full app restart clears it. Cap the wait so the badge can
+// never outlive the work.
function beginHydrationBackgroundSync(profile: string): void {
$hydrationSyncProfile.set(profile)
+ let timer: number | undefined
+
+ const clearOwnBadge = (): void => {
+ if ($hydrationSyncProfile.get() === profile) {
+ $hydrationSyncProfile.set(null)
+ }
+ }
+
const unlisten = $activeGatewayProfile.listen(next => {
if (normalizeProfileKey(next) === profile) {
- if ($hydrationSyncProfile.get() === profile) {
- $hydrationSyncProfile.set(null)
+ clearOwnBadge()
+
+ if (timer !== undefined) {
+ window.clearTimeout(timer)
}
unlisten()
}
})
+
+ timer = window.setTimeout(() => {
+ clearOwnBadge()
+ unlisten()
+ }, HYDRATION_SYNC_BADGE_TIMEOUT_MS)
}
function waitForFocusedSessionHydration({
diff --git a/apps/desktop/src/sdk/profile-routing.test.ts b/apps/desktop/src/sdk/profile-routing.test.ts
index 6d5f242c442..e85162d1573 100644
--- a/apps/desktop/src/sdk/profile-routing.test.ts
+++ b/apps/desktop/src/sdk/profile-routing.test.ts
@@ -122,7 +122,12 @@ vi.mock('@/store/gateway', async () => {
}
})
-const { BOT_CHAT_SESSION_HYDRATION_TIMEOUT_MS, DEFAULT_SESSION_HYDRATION_TIMEOUT_MS, host } = await import('./index')
+const {
+ BOT_CHAT_SESSION_HYDRATION_TIMEOUT_MS,
+ DEFAULT_SESSION_HYDRATION_TIMEOUT_MS,
+ HYDRATION_SYNC_BADGE_TIMEOUT_MS,
+ host
+} = await import('./index')
const { openSession: openSessionCore } = await import('@/app/open-session')
const { deleteProfile, hermesApi } = await import('@/hermes')
@@ -1519,4 +1524,40 @@ describe('shared-remote hydration gate (#89843)', () => {
// painted its badge over the winner.
expect($hydrationSyncProfile.get()).toBe('zephyr')
})
+
+ it('bounds the syncing badge when the profile gate never fires', async () => {
+ vi.useFakeTimers()
+
+ try {
+ $activeGatewayProfile.set('default')
+ vi.mocked(ensureGatewayProfile).mockImplementationOnce(async () => undefined)
+
+ const opening = host.openSession('shared-remote-stranded', {
+ awaitHydration: true,
+ expectHistory: true,
+ hydrationTimeoutMs: 250,
+ keepAllProfilesScope: false,
+ profile: 'shadow'
+ })
+
+ await Promise.resolve()
+ setMockAtom($selectedStoredSessionId, 'shared-remote-stranded')
+ setMockAtom($activeSessionId, 'runtime-shared-remote-stranded')
+ setMockAtom($messages, [{ id: 'stored-history', parts: [], role: 'assistant' }] as never)
+
+ await opening
+ expect($hydrationSyncProfile.get()).toBe('shadow')
+
+ // $activeGatewayProfile never moves to 'shadow' on a shared-remote
+ // connection, so the change-only listener is dead on arrival. Without a
+ // ceiling the badge would spin for the life of the window.
+ await vi.advanceTimersByTimeAsync(HYDRATION_SYNC_BADGE_TIMEOUT_MS - 1)
+ expect($hydrationSyncProfile.get()).toBe('shadow')
+
+ await vi.advanceTimersByTimeAsync(1)
+ expect($hydrationSyncProfile.get()).toBeNull()
+ } finally {
+ vi.useRealTimers()
+ }
+ })
})