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(usage): 防止待切换账号接收旧回合额度

Dash76ea9b71ae1e
diff --git a/apps/desktop/src/renderer/__tests__/subscriptionUsageCache.test.tsx b/apps/desktop/src/renderer/__tests__/subscriptionUsageCache.test.tsx
index ec97af12ec..5bdc29f34c 100644
--- a/apps/desktop/src/renderer/__tests__/subscriptionUsageCache.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/subscriptionUsageCache.test.tsx
@@ -132,3 +132,51 @@ it('OpenAI status quota reselects account/source caches and ignores pre-push rea
hook.rerender({ id: 'openai' });
expect(hook.result.current?.primary?.usedPercent).toBe(20);
});
+
+describe('OpenAI quota during a deferred account switch', () => {
+ it.each([
+ ['openai', 'empty'], ['openai', 'offline'],
+ ['openai-a', 'empty'], ['openai-a', 'offline'],
+ ])('keeps %s turn events out of the selected account when reads are %s', async (runningId, readResult) => {
+ setDataOwnerGeneration(`${runningId}-${readResult}`);
+ const pushes = new Map<string, Set<(value: unknown) => void>>();
+ const events = new Set<(value: unknown) => void>();
+ const reader = readResult === 'empty'
+ ? vi.fn().mockResolvedValue(null)
+ : vi.fn().mockRejectedValue(new Error('offline'));
+ vi.stubGlobal('electronAPI', { maker: {
+ onEvent: (callback: (value: unknown) => void) => {
+ events.add(callback); return () => events.delete(callback);
+ },
+ usage: {
+ getAccount: reader,
+ onCodexAccountChanged: (callback: (value: unknown) => void, id = 'openai') => {
+ const listeners = pushes.get(id) ?? new Set();
+ listeners.add(callback); pushes.set(id, listeners);
+ return () => listeners.delete(callback);
+ },
+ },
+ } });
+ const push = (id: string, value: unknown) => pushes.get(id)?.forEach(callback => callback(value));
+ const quota = (usedPercent: number) => ({ source: 'codex-app-server', primary: { usedPercent } });
+ const hook = renderHook(({ id }) => useAccountUsage('same-session', 'codex', 'app-server', 'gpt-6', id),
+ { initialProps: { id: 'openai-b' } });
+ act(() => push('openai-b', quota(20)));
+ hook.rerender({ id: runningId });
+ act(() => push(runningId, quota(40)));
+ hook.rerender({ id: 'openai-b' });
+ await act(async () => {});
+ act(() => {
+ events.forEach(callback => callback({ sessionId: 'same-session',
+ event: { type: 'account_usage', source: 'codex', data: quota(90) } }));
+ push(runningId, quota(90));
+ });
+ expect(hook.result.current?.primary?.usedPercent).toBe(20);
+ hook.rerender({ id: runningId });
+ expect(hook.result.current?.primary?.usedPercent).toBe(90);
+ hook.rerender({ id: 'openai-b' });
+ expect(hook.result.current?.primary?.usedPercent).toBe(20);
+ act(() => push('openai-b', null));
+ expect(hook.result.current).toBeNull();
+ });
+});
diff --git a/apps/desktop/src/renderer/hooks/useAccountUsage.ts b/apps/desktop/src/renderer/hooks/useAccountUsage.ts
index d906eef1b6..7b8f379392 100644
--- a/apps/desktop/src/renderer/hooks/useAccountUsage.ts
+++ b/apps/desktop/src/renderer/hooks/useAccountUsage.ts
@@ -4,9 +4,7 @@ import { getDataOwnerGeneration } from '@/contexts/dataOwnerGeneration';
*
* 数据通道:
* maker-core codex translator emit AgentEvent { type: 'account_usage', source: 'codex', data: RateLimitSnapshot }
- * → main register.ts wireSessionToIpc broadcast `maker:event` { sessionId, event }
- * → preload window.electronAPI.maker.onEvent
- * → 本 hook 按 sessionId 过滤
+ * → main 按实际运行账号归档并推送账号级快照
* ChatGPT WHAM 后台刷新 emit `usage:codex-account-changed`
* → preload window.electronAPI.maker.usage.onCodexAccountChanged
* → 本 hook 直接更新账号级快照
@@ -19,7 +17,7 @@ import { getDataOwnerGeneration } from '@/contexts/dataOwnerGeneration';
* (绝不显示不是这个会话在消耗的配额)。
*
* 设计与 useSessionSpend 对齐:
- * - 按 sessionId 过滤 (虽然 host 端是 fan-out 给所有 active subscriber, 每个 session 都收一份)
+ * - 只消费按 providerId 隔离的账号读取与推送;待生效选择不代表当前回合账号
* - Codex 账号用量是账号级数据, 切 session 时复用最近一次快照, 避免 chip 闪回占位态
* - vendorKey !== 'codex' 直接返 null (claude session 不订阅, 节省一次回调过滤开销)
*
@@ -497,7 +495,7 @@ export function useAccountUsage(
);
})
.catch(() => {
- /* Best-effort warm start; live account_usage events still update the chip. */
+ /* Best-effort warm start; account-scoped pushes still update the chip. */
});
return () => {
@@ -521,40 +519,7 @@ export function useAccountUsage(
};
}, [vendorKey, quotaSource, reselect, providerId, state]);
- useEffect(() => {
- if (vendorKey !== 'codex' || !sessionId) return;
- const api = (window as unknown as {
- electronAPI?: {
- maker?: {
- onEvent?: (cb: (data: unknown) => void) => () => void;
- };
- };
- }).electronAPI?.maker;
- if (!api?.onEvent) return;
- let cancelled = false;
- // 注: 这里不再在 turn 事件后拉 getAccount 触发 WHAM 刷新 —— CLI chip 只显示
- // app-server 槽, WHAM 刷新帮不上它, 白耗后台请求(旧行为还会把 WHAM 桶合并
- // 进单槽缓存, 正是「turn 刚结束数据被顶掉」的来源)。bridge 槽的保鲜由
- // main 的 bridge turn-done 触发 + mount 读 + 悬念期催刷负责。
- const unsubscribe = api.onEvent((data: unknown) => {
- if (cancelled) return;
- const payload = data as {
- sessionId?: string;
- event?: { type?: string; source?: string; data?: RateLimitSnapshot };
- };
- if (payload.sessionId !== sessionId) return;
- if (payload.event?.type !== 'account_usage') return;
- if (payload.event.source !== 'codex') return;
- if (!payload.event.data) return;
- // 只更新桶表(事件带 limitId, 进它自己的桶); **不**据此判会话归属 ——
- // 这是账号级 fan-out 事件(见 matchCodexBucketForModel 注释)。
- applyCodexAccountUsageSnapshot(state, payload.event.data, reselect);
- });
- return () => {
- cancelled = true;
- unsubscribe();
- };
- }, [sessionId, vendorKey, quotaSource, reselect, providerId, state]);
-
+ // 不直接消费无 providerId 的 maker:account_usage:同一 session 的待生效账号
+ // 可能已切换,事件仍属于旧回合。Main 归档后发出的账号级推送是唯一实时入口。
return snapshot;
}