Skip to content

The agent that grows with you

fix(desktop): bound i18n locale retries and keep English fallback

mengyuyuan5353fc9b32e1
diff --git a/apps/desktop/src/i18n/context.test.tsx b/apps/desktop/src/i18n/context.test.tsx
index 729ced7a4da..092b252f665 100644
--- a/apps/desktop/src/i18n/context.test.tsx
+++ b/apps/desktop/src/i18n/context.test.tsx
@@ -1,4 +1,4 @@
-import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import type { HermesConfigRecord } from '@/hermes'
@@ -247,4 +247,67 @@ describe('I18nProvider', () => {
expect(screen.getByTestId('locale').textContent).toBe('en')
expect(screen.getByTestId('label').textContent).toBe('Language')
})
+
+ it('retries a transient config failure and applies the persisted locale', async () => {
+ const getConfig = vi
+ .fn()
+ .mockRejectedValueOnce(new Error('backend not ready yet'))
+ .mockResolvedValueOnce({ display: { language: 'zh-Hans' } })
+
+ const configClient: I18nConfigClient = {
+ getConfig,
+ saveConfig: vi.fn()
+ }
+
+ render(
+ <I18nProvider configClient={configClient}>
+ <LanguageProbe />
+ </I18nProvider>
+ )
+
+ // First attempt fails → settles on English (permanent-failure contract).
+ await waitFor(() => expect(screen.getByTestId('loading').textContent).toBe('false'))
+ expect(screen.getByTestId('locale').textContent).toBe('en')
+
+ // The bounded retry succeeds and applies the persisted language.
+ await waitFor(() => expect(screen.getByTestId('locale').textContent).toBe('zh'), { timeout: 5_000 })
+ expect(getConfig).toHaveBeenCalledTimes(2)
+ })
+
+ it('stops retrying after the bounded retry budget is exhausted', async () => {
+ vi.useFakeTimers()
+ const getConfig = vi.fn().mockRejectedValue(new Error('backend unavailable'))
+
+ const configClient: I18nConfigClient = {
+ getConfig,
+ saveConfig: vi.fn()
+ }
+
+ render(
+ <I18nProvider configClient={configClient} initialLocale="zh">
+ <LanguageProbe />
+ </I18nProvider>
+ )
+
+ // Flush the initial attempt: it fails and settles on English.
+ await act(async () => {})
+ expect(screen.getByTestId('locale').textContent).toBe('en')
+ expect(getConfig).toHaveBeenCalledTimes(1)
+
+ // Budget is 10 retries at 3s each; run the whole budget to completion.
+ for (let i = 0; i < 10; i++) {
+ await act(async () => {
+ vi.advanceTimersByTime(3_000)
+ })
+ }
+ expect(getConfig).toHaveBeenCalledTimes(11)
+
+ // No timer is left after the budget is spent — nothing fires later.
+ await act(async () => {
+ vi.advanceTimersByTime(30_000)
+ })
+ expect(getConfig).toHaveBeenCalledTimes(11)
+
+ vi.useRealTimers()
+ })
})
diff --git a/apps/desktop/src/i18n/context.tsx b/apps/desktop/src/i18n/context.tsx
index 42c37e875fa..45fa502592b 100644
--- a/apps/desktop/src/i18n/context.tsx
+++ b/apps/desktop/src/i18n/context.tsx
@@ -114,6 +114,16 @@ export function I18nProvider({ children, configClient = defaultConfigClient, ini
let cancelled = false
let retryTimer: ReturnType<typeof setTimeout> | null = null
+ let retryCount = 0
+
+ // The desktop races its own backend at startup: the renderer mounts before
+ // the backend is ready, so the first /api/config call can time out. We keep
+ // the established permanent-failure contract — a rejected config load
+ // settles on English so the UI stays usable — but bounded retries recover
+ // transient startup failures, applying the persisted display.language once
+ // the backend comes up.
+ const MAX_LOCALE_RETRIES = 10
+ const LOCALE_RETRY_DELAY_MS = 3_000
const loadLocale = () => {
setIsLoadingConfig(true)
@@ -132,16 +142,14 @@ export function I18nProvider({ children, configClient = defaultConfigClient, ini
}
setConfigLoadError(toError(error))
+ setLocaleState(DEFAULT_LOCALE)
- // The desktop races its own backend at startup: the renderer mounts
- // before the backend is ready, and the first /api/config call times
- // out (60s). Falling back to the default locale permanently made the
- // UI stuck in English until a manual language switch. Retry with a
- // short delay instead — the backend comes up, the next attempt
- // succeeds, and the persisted display.language takes effect.
- retryTimer = setTimeout(() => {
- loadLocale()
- }, 3_000)
+ if (retryCount < MAX_LOCALE_RETRIES) {
+ retryCount += 1
+ retryTimer = setTimeout(() => {
+ loadLocale()
+ }, LOCALE_RETRY_DELAY_MS)
+ }
})
.finally(() => {
if (!cancelled) {