Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): refresh on scoped Codex auth-ready
Anyer Qi3babada87bc8
diff --git a/apps/desktop/src/main/usageBroadcaster.ts b/apps/desktop/src/main/usageBroadcaster.tsindex 28c5c3914c..3e660e70f0 100644--- a/apps/desktop/src/main/usageBroadcaster.ts+++ b/apps/desktop/src/main/usageBroadcaster.ts@@ -57,7 +57,7 @@ export const USAGE_TODAY_TOKENS_CHANGED = 'usage:today-tokens-changed';export const USAGE_CODEX_ACCOUNT_CHANGED = 'usage:codex-account-changed';/** Provider-scoped signal emitted when an independent Codex account is about to be replaced. */export const USAGE_CODEX_ACCOUNT_AUTH_STARTED = 'usage:codex-account-auth-started';-/** Provider-scoped signal emitted only after a Codex account login commit completes. */+/** Provider-scoped signal emitted when the current Codex account credentials are safe to reread. */export const USAGE_CODEX_ACCOUNT_AUTH_READY = 'usage:codex-account-auth-ready';/** IPC channel: main → renderer 推 xAI(SuperGrok bridge)上游限流快照变化。 */export const USAGE_XAI_RATE_LIMIT_CHANGED = 'usage:xai-rate-limit-changed';diff --git a/apps/desktop/src/renderer/__tests__/useCodexRateLimits.test.ts b/apps/desktop/src/renderer/__tests__/useCodexRateLimits.test.tsindex 8a06038584..e806d8d6e4 100644--- a/apps/desktop/src/renderer/__tests__/useCodexRateLimits.test.ts+++ b/apps/desktop/src/renderer/__tests__/useCodexRateLimits.test.ts@@ -120,10 +120,11 @@ describe('useCodexRateLimits', () => {expect(reader).toHaveBeenCalledTimes(3);});- it('waits for the account-scoped auth-ready signal after late old pushes and reads', async () => {+ it('handles account-scoped auth-ready after late old pushes and reads', async () => {const a = { ...result, providerId: 'openai-a' };const refreshedA = { ...result, providerId: 'openai-a', primary: { usedPercent: 18, resetsAt: 'later' } };const b = { ...result, providerId: 'openai-b' };+ const refreshedB = { ...b, primary: { usedPercent: 27, resetsAt: 'later' } };const accountChanges = new Map<string, (payload: unknown) => void>();const authStarted = new Map<string, () => void>();const authReady = new Map<string, () => void>();@@ -133,6 +134,7 @@ describe('useCodexRateLimits', () => {.mockResolvedValueOnce(a).mockResolvedValueOnce(b).mockImplementationOnce(() => new Promise(resolve => { resolveOldRead = resolve; }))+ .mockResolvedValueOnce(refreshedB).mockResolvedValueOnce(refreshedA).mockImplementation(() => new Promise(() => {}));vi.stubGlobal('electronAPI', { maker: {@@ -178,18 +180,22 @@ describe('useCodexRateLimits', () => {act(() => catalogChanges.forEach(changed => changed()));expect(reader).toHaveBeenCalledTimes(3);- // Only A's post-commit auth-ready signal can release its wait and refresh it.+ // B's provider-scoped ready is valid even though this renderer did not see its start.act(() => authReady.get('openai-b')?.());- expect(reader).toHaveBeenCalledTimes(3);- act(() => authReady.get('openai-a')?.());+ await waitFor(() => expect(accountB.result.current.snapshot).toBe(refreshedB));expect(reader).toHaveBeenCalledTimes(4);+ expect(accountA.result.current.snapshot).toBeNull();++ // A's ready signal releases A's wait and refreshes only A.+ act(() => authReady.get('openai-a')?.());+ expect(reader).toHaveBeenCalledTimes(5);await waitFor(() => expect(accountA.result.current.snapshot).toBe(refreshedA));- expect(accountB.result.current.snapshot).toBe(b);+ expect(accountB.result.current.snapshot).toBe(refreshedB);// A stale pre-login response cannot overwrite the refreshed quota.await act(async () => resolveOldRead(a));expect(accountA.result.current.snapshot).toBe(refreshedA);- expect(accountB.result.current.snapshot).toBe(b);+ expect(accountB.result.current.snapshot).toBe(refreshedB);});it('does not treat an unrelated scoped clear as a credential replacement', async () => {@@ -213,6 +219,26 @@ describe('useCodexRateLimits', () => {expect(reader).toHaveBeenCalledTimes(2);});+ it('refreshes on scoped auth-ready even if the renderer missed auth-start', async () => {+ const providerId = 'openai-a';+ const before = { ...result, providerId, account: { ...result.account, accountId: 'old' } };+ const after = { ...result, providerId, account: { ...result.account, accountId: 'new' } };+ let authReady: (() => void) | undefined;+ const reader = vi.fn().mockResolvedValueOnce(before).mockResolvedValueOnce(after);+ vi.stubGlobal('electronAPI', { maker: { usage: {+ getCodexRateLimits: reader,+ onCodexAccountAuthReady: (callback: () => void, id: string) => {+ if (id === providerId) authReady = callback;+ return () => { authReady = undefined; };+ },+ } } });+ const hook = renderHook(() => useCodexRateLimits(true, providerId));+ await waitFor(() => expect(hook.result.current.snapshot).toBe(before));+ act(() => authReady?.());+ await waitFor(() => expect(hook.result.current.snapshot).toBe(after));+ expect(reader).toHaveBeenCalledTimes(2);+ });+it('isolates Cindy owner changes and late responses', async () => {let resolveOld!: (value: MobileCodexRateLimitsResult) => void;const reader = vi.fn().mockImplementationOnce(() => new Promise(resolve => { resolveOld = resolve; }))diff --git a/apps/desktop/src/renderer/hooks/useCodexRateLimits.ts b/apps/desktop/src/renderer/hooks/useCodexRateLimits.tsindex 53fadece1d..a1886759d7 100644--- a/apps/desktop/src/renderer/hooks/useCodexRateLimits.ts+++ b/apps/desktop/src/renderer/hooks/useCodexRateLimits.ts@@ -124,8 +124,7 @@ function cachedRateLimits(providerId: string): CachedRateLimits {}, providerId);if (authStartedUnsubscribe) entry.unsubscribe.push(authStartedUnsubscribe);const authReadyUnsubscribe = window.electronAPI?.maker?.usage?.onCodexAccountAuthReady?.(() => {- // Main emits this provider-scoped signal only after the new auth.json commits.- if (!entry.awaitingAuthCompletion) return;+ // Main emits this only when the explicit account replacement has settled.entry.awaitingAuthCompletion = false;if (entry.listeners.size > 0) refreshEntry(entry);}, providerId);