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): retain cached quota when snapshot is unavailable

qianbo066789d6a8c1
diff --git a/apps/desktop/src/renderer/__tests__/gatewayAccountUsageHooks.test.tsx b/apps/desktop/src/renderer/__tests__/gatewayAccountUsageHooks.test.tsx
index c6c2142726..1fad536e88 100644
--- a/apps/desktop/src/renderer/__tests__/gatewayAccountUsageHooks.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/gatewayAccountUsageHooks.test.tsx
@@ -133,3 +133,23 @@ it('clears the mounted and warm quota cache when the main process reports no lim
expect(reopened.result.current.usage).toBeNull();
await waitFor(() => expect(reopened.result.current.loading).toBe(false));
});
+
+
+it('preserves owner-bound warm quota when a refresh has no main snapshot', async () => {
+ state.owner = 'quota-unavailable';
+ state.membership = 'org';
+ state.quota.mockResolvedValue({ spend: 12, maxBudget: 100, currency: 'CNY', todaySpend: null, fetchedAt: 1 });
+ const first = renderHook(() => useClaudeAccountUsageResult(true));
+ await waitFor(() => expect(first.result.current.usage?.spend).toBe(12));
+ first.unmount();
+ // No active listener while main switches accounts; returning to this owner
+ // can yield null after a failed request, without invalidating its warm data.
+ state.quota.mockResolvedValue(null);
+ const reopened = renderHook(() => useClaudeAccountUsageResult(true));
+ act(() => reopened.result.current.refresh());
+ await waitFor(() => expect(state.quota).toHaveBeenCalledTimes(2));
+ await waitFor(() => expect(reopened.result.current.loading).toBe(false));
+ expect(reopened.result.current.usage?.spend).toBe(12);
+ reopened.unmount();
+ expect(renderHook(() => useClaudeAccountUsageResult(true)).result.current.usage?.spend).toBe(12);
+});
diff --git a/apps/desktop/src/renderer/hooks/useClaudeAccountUsage.ts b/apps/desktop/src/renderer/hooks/useClaudeAccountUsage.ts
index 14b54d0531..9877aeac60 100644
--- a/apps/desktop/src/renderer/hooks/useClaudeAccountUsage.ts
+++ b/apps/desktop/src/renderer/hooks/useClaudeAccountUsage.ts
@@ -101,11 +101,8 @@ export function useClaudeAccountUsageResult(enabled: boolean) {
.getAccount('claude-code')
.then((res) => {
if (cancelled) return;
- if (res === null) {
- if (lastSnapshot?.ownerKey === ownerKey) lastSnapshot = null;
- setSnapshot(null);
- return;
- }
+ // A read may return null when main has no snapshot for this owner.
+ // Only an explicit clear push is authoritative enough to evict cached quota.
if (!isSnapshot(res)) return;
lastSnapshot = { ownerKey, cachedAt: Date.now(), usage: res };
setSnapshot(lastSnapshot);
diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.md
index c3cf91e1a0..7c8a7a6c87 100644
--- a/docs/dev-rules/protocol-compatibility.md
+++ b/docs/dev-rules/protocol-compatibility.md
@@ -1763,3 +1763,5 @@ PC 同源 ghostPermissionItems 声明摘要投影并排除 tool,以请求 loca
管理员取消周期上限后,主进程明确清空当前 owner 的配额缓存,并通过现有
`usage:claude-account-changed` 广播 null;renderer 同步清除当前 owner 的快照与热缓存。
已有未知载荷消费者会忽略 null,新旧有限配额快照字段不变;网络失败仍保留既有缓存策略。
+
+配额读取 IPC 返回 null 仅表示当前无可用快照,不清除 renderer 已缓存的同账号额度;只有显式 clear push 才清除缓存。