Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): retry update notice after startup miss (#3090)

Battleplus608364650f6d
diff --git a/apps/desktop/src/renderer/__tests__/useUpdateNoticeUpdatePreview.test.tsx b/apps/desktop/src/renderer/__tests__/useUpdateNoticeUpdatePreview.test.tsx
index d8396a4b57..fa615b5014 100644
--- a/apps/desktop/src/renderer/__tests__/useUpdateNoticeUpdatePreview.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/useUpdateNoticeUpdatePreview.test.tsx
@@ -321,6 +321,66 @@ describe('useUpdateNotice automatic popup', () => {
expect(result.current.releaseNotes?.[0]?.topics[0]?.title).toBe('ja-1.4.2');
});
+ it('retries a transient automatic notice miss before giving up', async () => {
+ (window as unknown as { electronAPI: unknown }).electronAPI = { appVersion: '1.4.2' };
+ localStorage.setItem(STORAGE_KEY, '1.4.1');
+ let currentFetch = 0;
+ mocks.fetchReleaseNotes.mockImplementation(async (version: string, locale: string) => {
+ if (version === '1.4.2' && currentFetch++ === 0) return null;
+ return notesFor(version, locale);
+ });
+ vi.useFakeTimers();
+ try {
+ const { result } = renderHook(() => useUpdateNotice());
+
+ await act(async () => {
+ await Promise.resolve();
+ await Promise.resolve();
+ });
+ expect(result.current.open).toBe(false);
+
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(2000);
+ });
+ expect(result.current.open).toBe(true);
+ expect(result.current.releaseNotes?.map((n) => n.version)).toEqual(['1.4.2']);
+ expect(currentFetch).toBe(2);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it('does not reopen an automatic notice after manual history is opened and dismissed during retry delay', async () => {
+ (window as unknown as { electronAPI: unknown }).electronAPI = { appVersion: '1.4.2' };
+ localStorage.setItem(STORAGE_KEY, '1.4.1');
+ let currentFetch = 0;
+ mocks.fetchReleaseNotes.mockImplementation(async (version: string, locale: string) => {
+ if (version === '1.4.2' && currentFetch++ === 0) return null;
+ return notesFor(version, locale);
+ });
+ vi.useFakeTimers();
+ try {
+ const { result } = renderHook(() => useUpdateNotice());
+
+ await act(async () => {
+ await Promise.resolve();
+ await Promise.resolve();
+ });
+ expect(result.current.open).toBe(false);
+
+ act(() => { result.current.onOpen(); });
+ await act(async () => { await Promise.resolve(); });
+ expect(result.current.open).toBe(true);
+
+ act(() => { result.current.dismiss(); });
+ await act(async () => { await vi.advanceTimersByTimeAsync(2000); });
+ expect(result.current.open).toBe(false);
+ expect(currentFetch).toBe(2);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it('does not reopen an automatic notice dismissed during a locale refresh', async () => {
(window as unknown as { electronAPI: unknown }).electronAPI = { appVersion: '1.4.2' };
localStorage.setItem(STORAGE_KEY, '1.4.1');
diff --git a/apps/desktop/src/renderer/hooks/useUpdateNotice.ts b/apps/desktop/src/renderer/hooks/useUpdateNotice.ts
index 7dd09d095b..a127707ce7 100644
--- a/apps/desktop/src/renderer/hooks/useUpdateNotice.ts
+++ b/apps/desktop/src/renderer/hooks/useUpdateNotice.ts
@@ -33,6 +33,14 @@ const MAX_AGGREGATED_VERSIONS = 5;
*/
const PREVIEW_INDEX_BUDGET_MS = 3000;
+/**
+ * A fresh install/update can race the CDN publishing the current notice or
+ * the renderer mounting during startup. Retry the automatic path once after a
+ * short pause; manual and pre-install preview paths already have explicit
+ * user-driven retry/fallback behavior.
+ */
+const AUTO_NOTICE_RETRY_DELAY_MS = 2000;
+
/** Numeric semver comparison. Returns >0 if a > b, <0 if a < b, 0 if equal. */
function cmpVersion(a: string, b: string): number {
const pa = a.split('.').map(Number);
@@ -215,6 +223,11 @@ export function useUpdateNotice(): UseUpdateNoticeReturn {
// Set synchronously (before any await) in onOpen so the auto-fetch path
// can bail out if manual dialog was opened while the fetch was in flight.
const dialogOpenedRef = useRef(false);
+ // Once a user explicitly opens either manual history or a pre-install
+ // preview, suppress the pending automatic retry for this hook lifetime.
+ // This must outlive dialogOpenedRef: dismiss() intentionally resets the
+ // latter, but the automatic attempt must not resurrect after dismissal.
+ const autoNoticeSuppressedRef = useRef(false);
// Stored handle for the dismiss cleanup timer so onOpen can cancel it if
// the user re-opens the dialog before the 200ms animation delay fires.
const dismissTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
@@ -273,52 +286,64 @@ export function useUpdateNotice(): UseUpdateNoticeReturn {
if (lastRead === appVersion) return;
let cancelled = false;
+ autoNoticeSuppressedRef.current = false;
(async () => {
- // Fresh install: skip the index round-trip and show only the current
- // version's notes. We don't want to blast a new user with 30 versions
- // of history on first launch.
- const index = lastRead === null ? null : await fetchReleaseNotesIndex();
- if (cancelled) return;
-
- const targets = versionsToFetch(index, lastRead, appVersion).slice(
- -MAX_AGGREGATED_VERSIONS,
- );
- const results = await fetchVersionsForCurrentLocale(targets);
- if (cancelled) return;
- const notes = results.filter((n): n is ReleaseNotes => n !== null);
+ // A transient CDN miss should not permanently lose the upgrade notice for
+ // this process. Keep the retry bounded so a genuinely unavailable CDN
+ // does not hold the renderer in a retry loop.
+ for (let attempt = 0; attempt < 2; attempt += 1) {
+ // Fresh install: skip the index round-trip and show only the current
+ // version's notes. We don't want to blast a new user with 30 versions
+ // of history on first launch.
+ const index = lastRead === null ? null : await fetchReleaseNotesIndex();
+ if (cancelled || dialogOpenedRef.current || autoNoticeSuppressedRef.current) return;
+
+ const targets = versionsToFetch(index, lastRead, appVersion).slice(
+ -MAX_AGGREGATED_VERSIONS,
+ );
+ const results = await fetchVersionsForCurrentLocale(targets);
+ if (cancelled || dialogOpenedRef.current || autoNoticeSuppressedRef.current) return;
+ const notes = results.filter((n): n is ReleaseNotes => n !== null);
+
+ if (notes.length > 0 && notes.some((n) => n.version === appVersion)) {
+ // Compute the safe read-marker (unchanged from prior impl).
+ const isCurIdxMissing = index !== null && index.indexOf(appVersion) === -1;
+ const firstFailIdx = results.findIndex((n) => n === null);
+ if (isCurIdxMissing) {
+ readMarkerRef.current = lastRead ?? null;
+ } else if (firstFailIdx === -1) {
+ readMarkerRef.current = appVersion;
+ } else if (firstFailIdx === 0) {
+ const skipKey = `xdt-maker:notice-skip-tried-${targets[0]}`;
+ if (localStorage.getItem(skipKey) === null) {
+ localStorage.setItem(skipKey, '1');
+ readMarkerRef.current = lastRead ?? null;
+ } else {
+ readMarkerRef.current = targets[0];
+ }
+ } else {
+ readMarkerRef.current = targets[firstFailIdx - 1];
+ }
- if (notes.length === 0) return;
- if (!notes.some((n) => n.version === appVersion)) return;
-
- // Bail BEFORE computing readMarkerRef: if manual was opened while we
- // fetched, don't write the marker (which would cause onOpen's
- // readMarkerRef !== null guard to incorrectly abort the manual dialog).
- if (dialogOpenedRef.current) return;
-
- // Compute the safe read-marker (unchanged from prior impl).
- const isCurIdxMissing = index !== null && index.indexOf(appVersion) === -1;
- const firstFailIdx = results.findIndex((n) => n === null);
- if (isCurIdxMissing) {
- readMarkerRef.current = lastRead ?? null;
- } else if (firstFailIdx === -1) {
- readMarkerRef.current = appVersion;
- } else if (firstFailIdx === 0) {
- const skipKey = `xdt-maker:notice-skip-tried-${targets[0]}`;
- if (localStorage.getItem(skipKey) === null) {
- localStorage.setItem(skipKey, '1');
- readMarkerRef.current = lastRead ?? null;
- } else {
- readMarkerRef.current = targets[0];
+ notes.reverse();
+ setMode('auto');
+ setReleaseNotes(notes);
+ setOpen(true);
+ return;
}
- } else {
- readMarkerRef.current = targets[firstFailIdx - 1];
- }
- notes.reverse();
- setMode('auto');
- setReleaseNotes(notes);
- setOpen(true);
+ if (
+ attempt === 0 &&
+ !cancelled &&
+ !dialogOpenedRef.current &&
+ !autoNoticeSuppressedRef.current
+ ) {
+ await new Promise<void>((resolve) => {
+ setTimeout(resolve, AUTO_NOTICE_RETRY_DELAY_MS);
+ });
+ }
+ }
})().catch((err) => {
log.warn('auto-fetch threw:', err);
});
@@ -383,6 +408,7 @@ export function useUpdateNotice(): UseUpdateNoticeReturn {
}
// Set synchronously so the auto-fetch path can see it immediately even
// before this async IIFE has called setOpen/setMode.
+ autoNoticeSuppressedRef.current = true;
dialogOpenedRef.current = true;
const appVersion = window.electronAPI.appVersion;
@@ -453,6 +479,7 @@ export function useUpdateNotice(): UseUpdateNoticeReturn {
clearTimeout(dismissTimerRef.current);
dismissTimerRef.current = null;
}
+ autoNoticeSuppressedRef.current = true;
dialogOpenedRef.current = true;
const appVersion = window.electronAPI.appVersion;