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): preserve organization daily spend from quota

fmfsaisai47b1662e589d
diff --git a/apps/desktop/src/main/usage/__tests__/organizationAccountUsage.test.ts b/apps/desktop/src/main/usage/__tests__/organizationAccountUsage.test.ts
index 27148015dd..b2e1d573e1 100644
--- a/apps/desktop/src/main/usage/__tests__/organizationAccountUsage.test.ts
+++ b/apps/desktop/src/main/usage/__tests__/organizationAccountUsage.test.ts
@@ -4,13 +4,26 @@ vi.mock('../../serverApiClient.js', () => ({ serverApiFetch: fetch, ServerApiErr
vi.mock('../../clientEndpointsService.js', () => ({ getClientEndpoint: () => 'https://models.example.test' }));
import { ServerApiError } from '../../serverApiClient.js';
import { readOrganizationAccountUsage } from '../organizationAccountUsage';
-const valid = { spend: 12, maxBudget: 100, currency: 'CNY', budgetResetAt: '2026-11-01T00:00:00Z', todaySpend: null };
+const valid = { spend: 12, maxBudget: 100, currency: 'CNY', budgetResetAt: '2026-11-01T00:00:00Z' };
beforeEach(() => { fetch.mockReset().mockResolvedValue(valid); });
describe('organization account usage', () => {
it('uses authenticated Model Access and preserves the gateway currency', async () => {
- expect(await readOrganizationAccountUsage()).toMatchObject(valid);
+ expect(await readOrganizationAccountUsage()).toMatchObject({ ...valid, todaySpend: null });
expect(fetch).toHaveBeenCalledWith('/api/model-access/quota', expect.objectContaining({ redactErrorDetails: true, timeoutMs: 8000 }));
});
+ // Unit regression: the public reader discarded the server's daily spend. Existing
+ // coverage only used null; todaySpend: 0 is the smallest input exposing the bug.
+ // Missing/invalid daily data must preserve the monthly quota without inventing zero.
+ it.each([1.25, 0])('preserves a valid daily spend of %s', async todaySpend => {
+ fetch.mockResolvedValue({ ...valid, todaySpend });
+ expect(await readOrganizationAccountUsage()).toMatchObject({ ...valid, todaySpend });
+ });
+ it.each([undefined, null, -1, NaN, Infinity, -Infinity, '1.25', false, {}])(
+ 'keeps the monthly quota when daily spend is %s', async todaySpend => {
+ fetch.mockResolvedValue({ ...valid, todaySpend });
+ expect(await readOrganizationAccountUsage()).toMatchObject({ ...valid, todaySpend: null });
+ },
+ );
it.each([[404, 'NOT_FOUND'], [501, 'MODEL_ACCESS_QUOTA_NOT_SUPPORTED']])('allows legacy fallback only for an unavailable capability', async (status, code) => {
fetch.mockRejectedValue(new ServerApiError(code, status, 'safe'));
expect(await readOrganizationAccountUsage()).toBeUndefined();
diff --git a/apps/desktop/src/main/usage/organizationAccountUsage.ts b/apps/desktop/src/main/usage/organizationAccountUsage.ts
index 1671d0275d..1d305f91c3 100644
--- a/apps/desktop/src/main/usage/organizationAccountUsage.ts
+++ b/apps/desktop/src/main/usage/organizationAccountUsage.ts
@@ -17,8 +17,10 @@ export async function readOrganizationAccountUsage(): Promise<ClaudeAccountUsage
(row.currency !== 'CNY' && row.currency !== 'USD') ||
(row.budgetResetAt !== null && (typeof row.budgetResetAt !== 'string' || !Number.isFinite(Date.parse(row.budgetResetAt))))) return null;
if (row.maxBudget === null) return 'unlimited';
+ const todaySpend = typeof row.todaySpend === 'number' && Number.isFinite(row.todaySpend) && row.todaySpend >= 0
+ ? row.todaySpend : null;
return { spend: row.spend, maxBudget: row.maxBudget, currency: row.currency,
- budgetResetAt: row.budgetResetAt, todaySpend: null, fetchedAt: Date.now() };
+ budgetResetAt: row.budgetResetAt, todaySpend, fetchedAt: Date.now() };
} catch (error) {
if (error instanceof ServerApiError && (error.statusCode === 404 ||
(error.statusCode === 501 && error.code === 'MODEL_ACCESS_QUOTA_NOT_SUPPORTED'))) return undefined;
diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.md
index 1032877a2e..d7ac55b3bc 100644
--- a/docs/dev-rules/protocol-compatibility.md
+++ b/docs/dev-rules/protocol-compatibility.md
@@ -2773,7 +2773,10 @@ Claude 原生 `user` 范围投影为 `global`;控制端读取早期 Host 的 `
沿用现有 account-usage IPC 和账号作用域缓存,所以设置卡片与任务用量提示使用相同来源。
服务端尚未提供接口(404),或明确返回 501/MODEL_ACCESS_QUOTA_NOT_SUPPORTED(Nova),
才使用原有网关查询。鉴权失败、上游故障或响应无效均显示不可用,不回退或借用其他身份数据。
-本接口尚无当日统计,todaySpend 为 null;未设周期上限时不伪造有限配额。
+`todaySpend` 为有限且 ≥ 0 的数字时,表示 UTC 当天的消费;缺失、`null` 或非法值均按
+`null` 处理,表示拿不到今日消费,不影响其余额度字段。旧版服务端不返回该字段或固定返回
+`null` 时,客户端照常显示月额度,只是不显示今日消费。未设周期上限时不伪造有限配额的
+原有行为保持不变。
本地手填网关和个人计费路径保持原协议。无新环境变量或数据库变更。
管理员取消周期上限后,主进程明确清空当前 owner 的配额缓存,并通过现有