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): Codex 用量分槽收尾加固 (#259 review follow-up) (#269)

Dash2b2ed1ff500e
diff --git a/apps/desktop/src/main/__tests__/usageBroadcasterCodexAccount.test.ts b/apps/desktop/src/main/__tests__/usageBroadcasterCodexAccount.test.ts
index 7a83246f5c..aadbf56df2 100644
--- a/apps/desktop/src/main/__tests__/usageBroadcasterCodexAccount.test.ts
+++ b/apps/desktop/src/main/__tests__/usageBroadcasterCodexAccount.test.ts
@@ -103,6 +103,18 @@ describe('codex account usage source slots', () => {
expect(payload?.accountId).toBe('acc-1');
});
+ it('rejects a corrupted array webSnapshot on hydration (与 renderer 守卫同口径)', async () => {
+ const broadcaster = await import('../usageBroadcaster');
+ mocks.queryOne.mockResolvedValue({
+ snapshot: JSON.stringify({ ...APP_SERVER_SNAPSHOT, webSnapshot: [] }),
+ });
+
+ const payload = await broadcaster.readCodexAccountUsageSnapshot();
+ expect(payload?.primary?.usedPercent).toBe(82);
+ // 数组不是合法快照: 归 null, 不得被再次广播 / 回写
+ expect(payload?.webSnapshot ?? null).toBeNull();
+ });
+
it('hydrates a legacy app-server row into the top-level slot', async () => {
const broadcaster = await import('../usageBroadcaster');
mocks.queryOne.mockResolvedValue({ snapshot: JSON.stringify(APP_SERVER_SNAPSHOT) });
diff --git a/apps/desktop/src/main/usageBroadcaster.ts b/apps/desktop/src/main/usageBroadcaster.ts
index 6fb3d9ea01..404d55fab6 100644
--- a/apps/desktop/src/main/usageBroadcaster.ts
+++ b/apps/desktop/src/main/usageBroadcaster.ts
@@ -342,7 +342,11 @@ export function splitPersistedCodexAccountUsage(parsed: Record<string, unknown>)
const { webSnapshot, ...rest } = parsed as CodexAccountUsagePayload;
return {
appServer: hasCodexSnapshotContent(rest) ? rest : null,
- web: webSnapshot && typeof webSnapshot === 'object' ? webSnapshot : null,
+ // 拒绝数组等畸形持久化值(与 renderer 的 isRateLimitSnapshot 守卫同口径),
+ // 否则损坏行会被当有效快照再次广播 + 回写。
+ web: webSnapshot && typeof webSnapshot === 'object' && !Array.isArray(webSnapshot)
+ ? webSnapshot
+ : null,
};
}
const legacy = parsed as RateLimitSnapshot;
diff --git a/apps/desktop/src/renderer/__tests__/accountUsageSnapshot.test.ts b/apps/desktop/src/renderer/__tests__/accountUsageSnapshot.test.ts
index cb45cac919..3cc817521b 100644
--- a/apps/desktop/src/renderer/__tests__/accountUsageSnapshot.test.ts
+++ b/apps/desktop/src/renderer/__tests__/accountUsageSnapshot.test.ts
@@ -216,9 +216,9 @@ describe('mergeCodexAccountUsageSnapshot', () => {
// 白耗后台请求; review 反馈) —— bridge 槽保鲜走 main 的 bridge turn-done
// 触发 + mount 读 + 悬念期催刷
expect(hookSource).not.toContain('refreshWebUsage');
- // module 常驻订阅: chip 全部卸载期间的换号清空广播不丢 (与 claude hook 同语义)
+ // module 常驻订阅的安装行为由下方 'module subscription install' 行为测试
+ // 覆盖 (renderToString 挂真 hook), 不再用脆弱的源码字符串断言 (review 反馈)。
expect(hookSource).toContain('function ensureModuleSubscription(');
- expect(hookSource).toContain('ensureModuleSubscription();');
// web-only 组合 payload 上浮归属字段, WHAM reader 的 accountId 归属判断不失配
expect(mainSource).toContain('accountId: web.accountId');
});
@@ -274,3 +274,51 @@ describe('splitCodexAccountUsagePayload', () => {
expect('web' in bare).toBe(false);
});
});
+
+describe('module subscription install (behavior)', () => {
+ // 真行为测试 (review 反馈: 源码字符串断言验不出等价重构 / 回归):
+ // ensureModuleSubscription 在 hook render 阶段安装, renderToString 即可驱动
+ // (不需要 effects / jsdom)。stub window.electronAPI 统计注册次数。
+ it('installs the codex usage listener once, and only for codex sessions', async () => {
+ const listeners: unknown[] = [];
+ const previousWindow = (globalThis as { window?: unknown }).window;
+ (globalThis as { window?: unknown }).window = {
+ electronAPI: {
+ maker: {
+ usage: {
+ onCodexAccountChanged: (cb: unknown) => {
+ listeners.push(cb);
+ return () => {};
+ },
+ },
+ },
+ },
+ };
+ try {
+ const { resetModules } = await import('vitest').then((m) => ({ resetModules: m.vi.resetModules }));
+ resetModules();
+ const [{ useAccountUsage: freshUseAccountUsage }, { renderToString }, React] =
+ await Promise.all([
+ import('@/hooks/useAccountUsage'),
+ import('react-dom/server'),
+ import('react'),
+ ]);
+ function Probe({ vendor }: { vendor?: 'cc' | 'codex' }) {
+ freshUseAccountUsage(undefined, vendor);
+ return null;
+ }
+ // 非 codex 会话: 不注册
+ renderToString(React.createElement(Probe, { vendor: 'cc' }));
+ renderToString(React.createElement(Probe, {}));
+ expect(listeners.length).toBe(0);
+ // 首个 codex 会话: 注册一次
+ renderToString(React.createElement(Probe, { vendor: 'codex' }));
+ expect(listeners.length).toBe(1);
+ // 幂等: 再挂 codex 实例不重复注册
+ renderToString(React.createElement(Probe, { vendor: 'codex' }));
+ expect(listeners.length).toBe(1);
+ } finally {
+ (globalThis as { window?: unknown }).window = previousWindow;
+ }
+ });
+});
diff --git a/apps/desktop/src/renderer/hooks/useAccountUsage.ts b/apps/desktop/src/renderer/hooks/useAccountUsage.ts
index db3a081f64..84985758e5 100644
--- a/apps/desktop/src/renderer/hooks/useAccountUsage.ts
+++ b/apps/desktop/src/renderer/hooks/useAccountUsage.ts
@@ -264,8 +264,10 @@ export function useAccountUsage(
vendorKey: 'cc' | 'codex' | undefined,
quotaSource: CodexQuotaSource = 'app-server',
): RateLimitSnapshot | null {
- // 幂等; 首个实例装上 module 常驻订阅, 保证卸载窗口内的广播(尤其换号清空)不丢。
- ensureModuleSubscription();
+ // 幂等; 首个 codex 实例装上 module 常驻订阅, 保证之后卸载窗口内的广播(尤其
+ // 换号清空)不丢。非 codex 会话不装 —— 从没有 codex chip 消费过就没有可残留
+ // 的缓存, 常驻监听纯属白耗(review 反馈)。
+ if (vendorKey === 'codex') ensureModuleSubscription();
const [snapshot, setSnapshot] = useState<RateLimitSnapshot | null>(() =>
vendorKey === 'codex' ? selectCodexSlot(quotaSource) : null,
);