Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(usage): 用量推送后刷新账号限额详情
Dasha83ea08cbd20
diff --git a/apps/desktop/src/renderer/__tests__/useCodexRateLimits.test.ts b/apps/desktop/src/renderer/__tests__/useCodexRateLimits.test.tsindex 33a300c0d2..d868de1875 100644--- a/apps/desktop/src/renderer/__tests__/useCodexRateLimits.test.ts+++ b/apps/desktop/src/renderer/__tests__/useCodexRateLimits.test.ts@@ -80,6 +80,46 @@ describe('useCodexRateLimits', () => {expect(second.result.current.snapshot).toBeNull();});+ it('refreshes scoped usage pushes without looping on read-generated pushes', async () => {+ const callbacks = new Map<string, (payload: unknown) => void>();+ const b = { ...result, providerId: 'openai-b' };+ const next = { ...b, rateLimitResetCredits: { availableCount: 1, credits: [] } };+ let resolveNext!: (value: MobileCodexRateLimitsResult) => void;+ const reader = vi.fn().mockImplementationOnce(async () => {+ callbacks.get('openai-b')?.({});+ return b;+ }).mockImplementationOnce(() => {+ callbacks.get('openai-b')?.({});+ return new Promise(resolve => { resolveNext = resolve; });+ }).mockResolvedValue(next);+ vi.stubGlobal('electronAPI', { maker: { usage: {+ getCodexRateLimits: reader,+ onCodexAccountChanged: (callback: (payload: unknown) => void, id: string) => {+ callbacks.set(id, callback);+ return () => callbacks.delete(id);+ },+ } } });+ const hook = renderHook(() => useCodexRateLimits(true, 'openai-b'));+ await waitFor(() => expect(hook.result.current.snapshot).toBe(b));+ expect(reader).toHaveBeenCalledTimes(1);+ act(() => callbacks.get('openai')?.({}));+ expect(reader).toHaveBeenCalledTimes(1);+ act(() => callbacks.get('openai-b')?.({}));+ expect(hook.result.current.snapshot).toBe(b);+ act(() => callbacks.get('openai-b')?.({}));+ expect(reader).toHaveBeenCalledTimes(2);+ await act(async () => resolveNext(next));+ expect(hook.result.current.snapshot).toBe(next);+ expect(reader).toHaveBeenCalledTimes(2);+ hook.unmount();+ act(() => callbacks.get('openai-b')?.({}));+ expect(reader).toHaveBeenCalledTimes(2);+ const mounted = renderHook(() => useCodexRateLimits(true, 'openai-b'));+ expect(mounted.result.current.snapshot).toBe(next);+ await act(async () => {});+ expect(reader).toHaveBeenCalledTimes(3);+ });+it('does not clear independent account quota for a catalog update', async () => {const b = { ...result, providerId: 'openai-b' };let changed: (() => void) | undefined;diff --git a/apps/desktop/src/renderer/hooks/useCodexRateLimits.ts b/apps/desktop/src/renderer/hooks/useCodexRateLimits.tsindex 042388cd78..61dcccb7d0 100644--- a/apps/desktop/src/renderer/hooks/useCodexRateLimits.ts+++ b/apps/desktop/src/renderer/hooks/useCodexRateLimits.ts@@ -58,6 +58,7 @@ export interface CodexRateLimitsState {interface CachedRateLimits {snapshot: MobileCodexRateLimitsResult | null;version: number;+ pendingReads: number;listeners: Set<() => void>;unsubscribe: Array<() => void>;providerId: string;@@ -73,11 +74,12 @@ function publish(entry: CachedRateLimits, value: MobileCodexRateLimitsResult | nfunction refreshEntry(entry: CachedRateLimits): void {const version = ++entry.version;+ entry.pendingReads++;void readCodexRateLimitsSafely(readCodexRateLimitsReader(), entry.providerId).then(next => {if (entry.version !== version || !isDataOwnerGenerationCurrent(entry.owner)) return;// An unavailable read is not a logout. Explicit identity clears come by push.if (next !== null) publish(entry, next);- });+ }).finally(() => { entry.pendingReads--; });}function cachedRateLimits(providerId: string): CachedRateLimits {@@ -93,7 +95,7 @@ function cachedRateLimits(providerId: string): CachedRateLimits {const cached = snapshots.get(providerId);if (cached) return cached;const entry: CachedRateLimits = {- providerId, owner, snapshot: null, version: 0, listeners: new Set(), unsubscribe: [],+ providerId, owner, snapshot: null, version: 0, pendingReads: 0, listeners: new Set(), unsubscribe: [],};snapshots.set(providerId, entry);const clear = () => {@@ -104,6 +106,10 @@ function cachedRateLimits(providerId: string): CachedRateLimits {// seed the next panel with the previous identity's cached quota.const usageUnsubscribe = window.electronAPI?.maker?.usage?.onCodexAccountChanged?.(payload => {if (payload === null) clear();+ // The authoritative read itself emits a usage push. Suppress those echoes+ // while any read is pending rather than scheduling a recursive refresh.+ else if (entry.listeners.size > 0 && entry.pendingReads === 0+ && isDataOwnerGenerationCurrent(entry.owner)) refreshEntry(entry);}, providerId);if (usageUnsubscribe) entry.unsubscribe.push(usageUnsubscribe);if (providerId === 'openai') {