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(provider-share): 按供应商隔离历史用量缓存

fmfsaisai363fc4fcc66c
diff --git a/apps/desktop/src/main/device-link/__tests__/providerShareInsights.test.ts b/apps/desktop/src/main/device-link/__tests__/providerShareInsights.test.ts
index 5fb7ff4369..c102d183f8 100644
--- a/apps/desktop/src/main/device-link/__tests__/providerShareInsights.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/providerShareInsights.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it, vi } from 'vitest';
+import { afterEach, describe, expect, it, vi } from 'vitest';
import type { ProviderShareOwned } from '@cindy/device-link';
import type { ProviderUsageHistoryView } from '../../../shared/providerShare';
import { parseProviderShareInsights } from '../../../shared/providerShareInsights';
@@ -7,6 +7,7 @@ import { createProviderShareInsightsReader } from '../providerShareInsights';
/* 测试价值:通过新只读 reader 验证四种能力组合、匿名投影、独立失败和撤权时序。
* 依据:provider-sharing 的最小范围及账号隔离;旧 dispatch/management 测试没有新 reader。
* 最小失败输入:一项查询失败、在途关闭后重开、同 providerId 换账号、转来的一行用量。
+ * 缓存回归:PR #6482 P1;旧覆盖仅单供应商,补 A→B→A、过期和旧失败晚于新读取的时序。
* tier: unit;依赖注入,无 Electron/外网/真实等待。单文件耗时见执行报告。 */
const day = {
day: '2026-10-19',
@@ -94,10 +95,11 @@ function fixture() {
],
})),
};
- return { state, deps, read: createProviderShareInsightsReader(deps) };
+ return { state, deps, history, read: createProviderShareInsightsReader(deps) };
}
const request = { version: 1 };
describe('provider share insights', () => {
+ afterEach(() => vi.restoreAllMocks());
it.each([
[false, false],
[true, false],
@@ -180,6 +182,70 @@ describe('provider share insights', () => {
await read('peer', request);
expect(deps.history).toHaveBeenCalledTimes(2);
});
+ it('retains each provider history and coalesces only that provider while reads are in flight', async () => {
+ const { deps, history } = fixture();
+ let finish!: () => void;
+ const wait = new Promise<void>((resolve) => {
+ finish = resolve;
+ });
+ const load = vi.fn(async (providerId: string) => {
+ await wait;
+ return { ...history, self: { ...history.self, days: [{ ...day, model: providerId }] } };
+ });
+ const read = createProviderShareInsightsReader({
+ ...deps,
+ access: (peer) => ({ ...deps.access()!, providerId: peer, shareId: peer }),
+ shares: () => ['a', 'b'].map((id) => ({ ...deps.shares()[0], providerId: id, shareId: id })),
+ history: load,
+ });
+ const pending = ['a', 'b', 'a', 'b'].map((peer) => read(peer, request));
+ finish();
+ const views = await Promise.all(pending);
+ expect(views.map((view) => view.usage?.parties[0].days[0].model)).toEqual(['a', 'b', 'a', 'b']);
+ await read('a', request);
+ await read('b', request);
+ expect(load.mock.calls).toEqual([['a'], ['b']]);
+ // A failing retry must not remove B's still-current history.
+ vi.spyOn(Date, 'now').mockReturnValue(Date.now() + 15_000);
+ await read('b', request);
+ load.mockRejectedValueOnce(new Error('ledger failed'));
+ expect((await read('a', request)).status.usage).toBe('unavailable');
+ await read('a', request);
+ await read('b', request);
+ expect(load.mock.calls).toEqual([['a'], ['b'], ['b'], ['a'], ['a']]);
+ });
+ it.each(['expiry', 'membership', 'visibility'] as const)(
+ 'reloads cached history after %s changes',
+ async (change) => {
+ const { read, deps, state } = fixture();
+ const now = vi.spyOn(Date, 'now').mockReturnValue(1_000);
+ await read('peer', request);
+ if (change === 'expiry') now.mockReturnValue(16_000);
+ if (change === 'membership') state.revision += 1;
+ if (change === 'visibility') state.visibility.revision += 2;
+ await read('peer', request);
+ await read('peer', request);
+ expect(deps.history).toHaveBeenCalledTimes(2);
+ },
+ );
+ it('does not let an expired in-flight failure evict its successful replacement', async () => {
+ const { read, deps } = fixture();
+ const now = vi.spyOn(Date, 'now').mockReturnValue(1_000);
+ let fail!: (error: Error) => void;
+ deps.history.mockImplementationOnce(
+ () =>
+ new Promise((_, reject) => {
+ fail = reject;
+ }),
+ );
+ const old = read('peer', request);
+ now.mockReturnValue(16_000);
+ expect((await read('peer', request)).status.usage).toBe('ready');
+ fail(new Error('old failure'));
+ expect((await old).status.usage).toBe('unavailable');
+ expect((await read('peer', request)).status.usage).toBe('ready');
+ expect(deps.history).toHaveBeenCalledTimes(2);
+ });
it('rejects unauthorized members and a guest-supplied provider id', async () => {
const { read, state } = fixture();
await expect(read('peer', { version: 1, providerId: 'other' })).rejects.toThrow(
diff --git a/apps/desktop/src/main/device-link/providerShareInsights.ts b/apps/desktop/src/main/device-link/providerShareInsights.ts
index 507d40a507..f819994fca 100644
--- a/apps/desktop/src/main/device-link/providerShareInsights.ts
+++ b/apps/desktop/src/main/device-link/providerShareInsights.ts
@@ -121,13 +121,15 @@ function publicUsage(
/** 最小只读投影;权限检查保留到实际发送,包括背压队列重发。 */
export function createProviderShareInsightsReader(deps: ProviderShareInsightsDeps) {
const checks = new WeakMap<object, { peer: string; assertCurrent(): void }>();
- let cached: {
- owner: string;
- providerId: string;
- revision: number;
- expiresAt: number;
- read: Promise<ProviderUsageHistoryView>;
- } | null = null;
+ let cacheScope: { owner: string; revision: number } | null = null;
+ const histories = new Map<
+ string,
+ {
+ visibilityRevision: number;
+ expiresAt: number;
+ read: Promise<ProviderUsageHistoryView>;
+ }
+ >();
const read = async (peer: string, raw: unknown): Promise<ProviderShareInsights> => {
if (
!raw ||
@@ -160,24 +162,29 @@ export function createProviderShareInsightsReader(deps: ProviderShareInsightsDep
}
};
assertCurrent();
- if (
- visibility.usage &&
- (!cached ||
- cached.owner !== owner ||
- cached.providerId !== access.providerId ||
- cached.revision !== revision ||
- cached.expiresAt <= Date.now())
- ) {
+ if (cacheScope?.owner !== owner || cacheScope.revision !== revision) {
+ histories.clear();
+ cacheScope = { owner, revision };
+ }
+ const now = Date.now();
+ // Retain only current, unexpired provider entries, not historical authorization generations.
+ for (const [providerId, entry] of histories) {
+ if (
+ entry.expiresAt <= now ||
+ entry.visibilityRevision !== deps.visibility(providerId).revision
+ ) {
+ histories.delete(providerId);
+ }
+ }
+ let cached = histories.get(access.providerId);
+ if (!visibility.usage) {
+ histories.delete(access.providerId);
+ } else if (!cached) {
const read = deps.history(access.providerId);
- cached = {
- owner,
- providerId: access.providerId,
- revision,
- expiresAt: Date.now() + 15_000,
- read,
- };
+ cached = { visibilityRevision: visibility.revision, expiresAt: now + 15_000, read };
+ histories.set(access.providerId, cached);
void read.catch(() => {
- if (cached?.read === read) cached = null;
+ if (histories.get(access.providerId)?.read === read) histories.delete(access.providerId);
});
}
const [historyResult, quotaResult] = await Promise.allSettled([